1*10616SSebastien.Roy@Sun.COM /*
2*10616SSebastien.Roy@Sun.COM * CDDL HEADER START
3*10616SSebastien.Roy@Sun.COM *
4*10616SSebastien.Roy@Sun.COM * The contents of this file are subject to the terms of the
5*10616SSebastien.Roy@Sun.COM * Common Development and Distribution License (the "License").
6*10616SSebastien.Roy@Sun.COM * You may not use this file except in compliance with the License.
7*10616SSebastien.Roy@Sun.COM *
8*10616SSebastien.Roy@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10616SSebastien.Roy@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*10616SSebastien.Roy@Sun.COM * See the License for the specific language governing permissions
11*10616SSebastien.Roy@Sun.COM * and limitations under the License.
12*10616SSebastien.Roy@Sun.COM *
13*10616SSebastien.Roy@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*10616SSebastien.Roy@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10616SSebastien.Roy@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*10616SSebastien.Roy@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*10616SSebastien.Roy@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*10616SSebastien.Roy@Sun.COM *
19*10616SSebastien.Roy@Sun.COM * CDDL HEADER END
20*10616SSebastien.Roy@Sun.COM */
21*10616SSebastien.Roy@Sun.COM /*
22*10616SSebastien.Roy@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23*10616SSebastien.Roy@Sun.COM * Use is subject to license terms.
24*10616SSebastien.Roy@Sun.COM */
25*10616SSebastien.Roy@Sun.COM
26*10616SSebastien.Roy@Sun.COM /*
27*10616SSebastien.Roy@Sun.COM * DL_IPV4 MAC Type plugin for the Nemo mac module
28*10616SSebastien.Roy@Sun.COM */
29*10616SSebastien.Roy@Sun.COM
30*10616SSebastien.Roy@Sun.COM #include <sys/types.h>
31*10616SSebastien.Roy@Sun.COM #include <sys/modctl.h>
32*10616SSebastien.Roy@Sun.COM #include <sys/dlpi.h>
33*10616SSebastien.Roy@Sun.COM #include <sys/mac.h>
34*10616SSebastien.Roy@Sun.COM #include <sys/mac_ipv4.h>
35*10616SSebastien.Roy@Sun.COM #include <sys/byteorder.h>
36*10616SSebastien.Roy@Sun.COM #include <sys/strsun.h>
37*10616SSebastien.Roy@Sun.COM #include <netinet/in.h>
38*10616SSebastien.Roy@Sun.COM #include <netinet/ip.h>
39*10616SSebastien.Roy@Sun.COM #include <inet/common.h>
40*10616SSebastien.Roy@Sun.COM #include <inet/ip.h>
41*10616SSebastien.Roy@Sun.COM #include <inet/iptun.h>
42*10616SSebastien.Roy@Sun.COM
43*10616SSebastien.Roy@Sun.COM static struct modlmisc mac_ipv4_modlmisc = {
44*10616SSebastien.Roy@Sun.COM &mod_miscops,
45*10616SSebastien.Roy@Sun.COM "IPv4 tunneling MAC plugin"
46*10616SSebastien.Roy@Sun.COM };
47*10616SSebastien.Roy@Sun.COM
48*10616SSebastien.Roy@Sun.COM static struct modlinkage mac_ipv4_modlinkage = {
49*10616SSebastien.Roy@Sun.COM MODREV_1,
50*10616SSebastien.Roy@Sun.COM &mac_ipv4_modlmisc,
51*10616SSebastien.Roy@Sun.COM NULL
52*10616SSebastien.Roy@Sun.COM };
53*10616SSebastien.Roy@Sun.COM
54*10616SSebastien.Roy@Sun.COM static mactype_ops_t mac_ipv4_type_ops;
55*10616SSebastien.Roy@Sun.COM
56*10616SSebastien.Roy@Sun.COM int
_init(void)57*10616SSebastien.Roy@Sun.COM _init(void)
58*10616SSebastien.Roy@Sun.COM {
59*10616SSebastien.Roy@Sun.COM mactype_register_t *mtrp;
60*10616SSebastien.Roy@Sun.COM int err;
61*10616SSebastien.Roy@Sun.COM
62*10616SSebastien.Roy@Sun.COM if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
63*10616SSebastien.Roy@Sun.COM return (ENOTSUP);
64*10616SSebastien.Roy@Sun.COM mtrp->mtr_ident = MAC_PLUGIN_IDENT_IPV4;
65*10616SSebastien.Roy@Sun.COM mtrp->mtr_ops = &mac_ipv4_type_ops;
66*10616SSebastien.Roy@Sun.COM mtrp->mtr_mactype = DL_IPV4;
67*10616SSebastien.Roy@Sun.COM mtrp->mtr_nativetype = DL_IPV4;
68*10616SSebastien.Roy@Sun.COM mtrp->mtr_addrlen = sizeof (ipaddr_t);
69*10616SSebastien.Roy@Sun.COM if ((err = mactype_register(mtrp)) == 0) {
70*10616SSebastien.Roy@Sun.COM if ((err = mod_install(&mac_ipv4_modlinkage)) != 0)
71*10616SSebastien.Roy@Sun.COM (void) mactype_unregister(MAC_PLUGIN_IDENT_IPV4);
72*10616SSebastien.Roy@Sun.COM }
73*10616SSebastien.Roy@Sun.COM mactype_free(mtrp);
74*10616SSebastien.Roy@Sun.COM return (err);
75*10616SSebastien.Roy@Sun.COM }
76*10616SSebastien.Roy@Sun.COM
77*10616SSebastien.Roy@Sun.COM int
_fini(void)78*10616SSebastien.Roy@Sun.COM _fini(void)
79*10616SSebastien.Roy@Sun.COM {
80*10616SSebastien.Roy@Sun.COM int err;
81*10616SSebastien.Roy@Sun.COM if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IPV4)) != 0)
82*10616SSebastien.Roy@Sun.COM return (err);
83*10616SSebastien.Roy@Sun.COM return (mod_remove(&mac_ipv4_modlinkage));
84*10616SSebastien.Roy@Sun.COM }
85*10616SSebastien.Roy@Sun.COM
86*10616SSebastien.Roy@Sun.COM int
_info(struct modinfo * modinfop)87*10616SSebastien.Roy@Sun.COM _info(struct modinfo *modinfop)
88*10616SSebastien.Roy@Sun.COM {
89*10616SSebastien.Roy@Sun.COM return (mod_info(&mac_ipv4_modlinkage, modinfop));
90*10616SSebastien.Roy@Sun.COM }
91*10616SSebastien.Roy@Sun.COM
92*10616SSebastien.Roy@Sun.COM /*
93*10616SSebastien.Roy@Sun.COM * MAC Type plugin operations
94*10616SSebastien.Roy@Sun.COM */
95*10616SSebastien.Roy@Sun.COM
96*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
97*10616SSebastien.Roy@Sun.COM int
mac_ipv4_unicst_verify(const void * addr,void * pdata)98*10616SSebastien.Roy@Sun.COM mac_ipv4_unicst_verify(const void *addr, void *pdata)
99*10616SSebastien.Roy@Sun.COM {
100*10616SSebastien.Roy@Sun.COM const ipaddr_t *ipaddr = addr;
101*10616SSebastien.Roy@Sun.COM return ((CLASSD(*ipaddr) || (*ipaddr == INADDR_BROADCAST)) ?
102*10616SSebastien.Roy@Sun.COM EINVAL : 0);
103*10616SSebastien.Roy@Sun.COM }
104*10616SSebastien.Roy@Sun.COM
105*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
106*10616SSebastien.Roy@Sun.COM int
mac_ipv4_multicst_verify(const void * addr,void * pdata)107*10616SSebastien.Roy@Sun.COM mac_ipv4_multicst_verify(const void *addr, void *pdata)
108*10616SSebastien.Roy@Sun.COM {
109*10616SSebastien.Roy@Sun.COM /*
110*10616SSebastien.Roy@Sun.COM * IPv4 configured tunnels do not have the concept of link-layer
111*10616SSebastien.Roy@Sun.COM * multicast.
112*10616SSebastien.Roy@Sun.COM */
113*10616SSebastien.Roy@Sun.COM return (ENOTSUP);
114*10616SSebastien.Roy@Sun.COM }
115*10616SSebastien.Roy@Sun.COM
116*10616SSebastien.Roy@Sun.COM /*
117*10616SSebastien.Roy@Sun.COM * Check the legality of an IPv4 tunnel SAP value. The only two acceptable
118*10616SSebastien.Roy@Sun.COM * values are IPPROTO_ENCAP (IPv4 in IPv4) and IPPROTO_IPV6 (IPv6 in IPv4).
119*10616SSebastien.Roy@Sun.COM */
120*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
121*10616SSebastien.Roy@Sun.COM boolean_t
mac_ipv4_sap_verify(uint32_t sap,uint32_t * bind_sap,void * pdata)122*10616SSebastien.Roy@Sun.COM mac_ipv4_sap_verify(uint32_t sap, uint32_t *bind_sap, void *pdata)
123*10616SSebastien.Roy@Sun.COM {
124*10616SSebastien.Roy@Sun.COM if (sap == IPPROTO_ENCAP || sap == IPPROTO_IPV6 || sap == 0) {
125*10616SSebastien.Roy@Sun.COM if (bind_sap != NULL)
126*10616SSebastien.Roy@Sun.COM *bind_sap = sap;
127*10616SSebastien.Roy@Sun.COM return (B_TRUE);
128*10616SSebastien.Roy@Sun.COM }
129*10616SSebastien.Roy@Sun.COM return (B_FALSE);
130*10616SSebastien.Roy@Sun.COM }
131*10616SSebastien.Roy@Sun.COM
132*10616SSebastien.Roy@Sun.COM /*
133*10616SSebastien.Roy@Sun.COM * Build an IPv4 link-layer header for tunneling. If provided, the
134*10616SSebastien.Roy@Sun.COM * template header provided by the driver supplies the header length, type
135*10616SSebastien.Roy@Sun.COM * of service, don't fragment flag, ttl, and potential options (depending
136*10616SSebastien.Roy@Sun.COM * on the header length).
137*10616SSebastien.Roy@Sun.COM */
138*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
139*10616SSebastien.Roy@Sun.COM mblk_t *
mac_ipv4_header(const void * saddr,const void * daddr,uint32_t sap,void * pdata,mblk_t * payload,size_t extra_len)140*10616SSebastien.Roy@Sun.COM mac_ipv4_header(const void *saddr, const void *daddr, uint32_t sap, void *pdata,
141*10616SSebastien.Roy@Sun.COM mblk_t *payload, size_t extra_len)
142*10616SSebastien.Roy@Sun.COM {
143*10616SSebastien.Roy@Sun.COM struct ip *iphp;
144*10616SSebastien.Roy@Sun.COM struct ip *tmpl_iphp = pdata;
145*10616SSebastien.Roy@Sun.COM mblk_t *mp;
146*10616SSebastien.Roy@Sun.COM size_t hdr_len = sizeof (struct ip);
147*10616SSebastien.Roy@Sun.COM
148*10616SSebastien.Roy@Sun.COM if (!mac_ipv4_sap_verify(sap, NULL, NULL))
149*10616SSebastien.Roy@Sun.COM return (NULL);
150*10616SSebastien.Roy@Sun.COM
151*10616SSebastien.Roy@Sun.COM if (tmpl_iphp != NULL)
152*10616SSebastien.Roy@Sun.COM hdr_len = tmpl_iphp->ip_hl * sizeof (uint32_t);
153*10616SSebastien.Roy@Sun.COM
154*10616SSebastien.Roy@Sun.COM if ((mp = allocb(hdr_len + extra_len, BPRI_HI)) == NULL)
155*10616SSebastien.Roy@Sun.COM return (NULL);
156*10616SSebastien.Roy@Sun.COM
157*10616SSebastien.Roy@Sun.COM iphp = (struct ip *)mp->b_rptr;
158*10616SSebastien.Roy@Sun.COM
159*10616SSebastien.Roy@Sun.COM bzero(iphp, hdr_len + extra_len);
160*10616SSebastien.Roy@Sun.COM if (tmpl_iphp != NULL) {
161*10616SSebastien.Roy@Sun.COM bcopy(tmpl_iphp, iphp, hdr_len);
162*10616SSebastien.Roy@Sun.COM } else {
163*10616SSebastien.Roy@Sun.COM iphp->ip_hl = IP_SIMPLE_HDR_LENGTH_IN_WORDS;
164*10616SSebastien.Roy@Sun.COM iphp->ip_off = htons(IP_DF);
165*10616SSebastien.Roy@Sun.COM iphp->ip_ttl = IPTUN_DEFAULT_HOPLIMIT;
166*10616SSebastien.Roy@Sun.COM }
167*10616SSebastien.Roy@Sun.COM
168*10616SSebastien.Roy@Sun.COM iphp->ip_v = IPVERSION;
169*10616SSebastien.Roy@Sun.COM iphp->ip_len = 0;
170*10616SSebastien.Roy@Sun.COM iphp->ip_p = (uint8_t)sap;
171*10616SSebastien.Roy@Sun.COM bcopy(saddr, &(iphp->ip_src), sizeof (struct in_addr));
172*10616SSebastien.Roy@Sun.COM bcopy(daddr, &(iphp->ip_dst), sizeof (struct in_addr));
173*10616SSebastien.Roy@Sun.COM
174*10616SSebastien.Roy@Sun.COM mp->b_wptr += hdr_len;
175*10616SSebastien.Roy@Sun.COM return (mp);
176*10616SSebastien.Roy@Sun.COM }
177*10616SSebastien.Roy@Sun.COM
178*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
179*10616SSebastien.Roy@Sun.COM int
mac_ipv4_header_info(mblk_t * mp,void * pdata,mac_header_info_t * hdr_info)180*10616SSebastien.Roy@Sun.COM mac_ipv4_header_info(mblk_t *mp, void *pdata, mac_header_info_t *hdr_info)
181*10616SSebastien.Roy@Sun.COM {
182*10616SSebastien.Roy@Sun.COM struct ip *iphp;
183*10616SSebastien.Roy@Sun.COM
184*10616SSebastien.Roy@Sun.COM if (MBLKL(mp) < sizeof (struct ip))
185*10616SSebastien.Roy@Sun.COM return (EINVAL);
186*10616SSebastien.Roy@Sun.COM
187*10616SSebastien.Roy@Sun.COM iphp = (struct ip *)mp->b_rptr;
188*10616SSebastien.Roy@Sun.COM
189*10616SSebastien.Roy@Sun.COM /*
190*10616SSebastien.Roy@Sun.COM * IPv4 tunnels don't have a concept of link-layer multicast since
191*10616SSebastien.Roy@Sun.COM * they have fixed unicast endpoints.
192*10616SSebastien.Roy@Sun.COM */
193*10616SSebastien.Roy@Sun.COM if (mac_ipv4_unicst_verify(&iphp->ip_dst, NULL) != 0)
194*10616SSebastien.Roy@Sun.COM return (EINVAL);
195*10616SSebastien.Roy@Sun.COM
196*10616SSebastien.Roy@Sun.COM hdr_info->mhi_hdrsize = iphp->ip_hl * sizeof (uint32_t);
197*10616SSebastien.Roy@Sun.COM hdr_info->mhi_pktsize = 0;
198*10616SSebastien.Roy@Sun.COM hdr_info->mhi_daddr = (const uint8_t *)&(iphp->ip_dst);
199*10616SSebastien.Roy@Sun.COM hdr_info->mhi_saddr = (const uint8_t *)&(iphp->ip_src);
200*10616SSebastien.Roy@Sun.COM hdr_info->mhi_origsap = hdr_info->mhi_bindsap = iphp->ip_p;
201*10616SSebastien.Roy@Sun.COM hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
202*10616SSebastien.Roy@Sun.COM return (0);
203*10616SSebastien.Roy@Sun.COM }
204*10616SSebastien.Roy@Sun.COM
205*10616SSebastien.Roy@Sun.COM /*
206*10616SSebastien.Roy@Sun.COM * Plugin data is either NULL or a pointer to an IPv4 header.
207*10616SSebastien.Roy@Sun.COM */
208*10616SSebastien.Roy@Sun.COM boolean_t
mac_ipv4_pdata_verify(void * pdata,size_t pdata_size)209*10616SSebastien.Roy@Sun.COM mac_ipv4_pdata_verify(void *pdata, size_t pdata_size)
210*10616SSebastien.Roy@Sun.COM {
211*10616SSebastien.Roy@Sun.COM const struct ip *iphp = pdata;
212*10616SSebastien.Roy@Sun.COM
213*10616SSebastien.Roy@Sun.COM if (pdata == NULL)
214*10616SSebastien.Roy@Sun.COM return (pdata_size == 0);
215*10616SSebastien.Roy@Sun.COM if (pdata_size < sizeof (struct ip))
216*10616SSebastien.Roy@Sun.COM return (B_FALSE);
217*10616SSebastien.Roy@Sun.COM /* Make sure that the header length field matches pdata_size */
218*10616SSebastien.Roy@Sun.COM return (pdata_size == iphp->ip_hl * sizeof (uint32_t));
219*10616SSebastien.Roy@Sun.COM }
220*10616SSebastien.Roy@Sun.COM
221*10616SSebastien.Roy@Sun.COM static mactype_ops_t mac_ipv4_type_ops = {
222*10616SSebastien.Roy@Sun.COM MTOPS_PDATA_VERIFY,
223*10616SSebastien.Roy@Sun.COM mac_ipv4_unicst_verify,
224*10616SSebastien.Roy@Sun.COM mac_ipv4_multicst_verify,
225*10616SSebastien.Roy@Sun.COM mac_ipv4_sap_verify,
226*10616SSebastien.Roy@Sun.COM mac_ipv4_header,
227*10616SSebastien.Roy@Sun.COM mac_ipv4_header_info,
228*10616SSebastien.Roy@Sun.COM mac_ipv4_pdata_verify,
229*10616SSebastien.Roy@Sun.COM NULL,
230*10616SSebastien.Roy@Sun.COM NULL,
231*10616SSebastien.Roy@Sun.COM NULL
232*10616SSebastien.Roy@Sun.COM };
233