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_IPV6 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_ipv6.h>
35*10616SSebastien.Roy@Sun.COM #include <sys/mac_ipv4_impl.h>
36*10616SSebastien.Roy@Sun.COM #include <sys/byteorder.h>
37*10616SSebastien.Roy@Sun.COM #include <sys/strsun.h>
38*10616SSebastien.Roy@Sun.COM #include <netinet/ip6.h>
39*10616SSebastien.Roy@Sun.COM #include <inet/common.h>
40*10616SSebastien.Roy@Sun.COM #include <inet/mib2.h>
41*10616SSebastien.Roy@Sun.COM #include <inet/ip.h>
42*10616SSebastien.Roy@Sun.COM #include <inet/ip6.h>
43*10616SSebastien.Roy@Sun.COM #include <inet/iptun.h>
44*10616SSebastien.Roy@Sun.COM
45*10616SSebastien.Roy@Sun.COM static struct modlmisc mac_ipv6_modlmisc = {
46*10616SSebastien.Roy@Sun.COM &mod_miscops,
47*10616SSebastien.Roy@Sun.COM "IPv6 tunneling MAC plugin"
48*10616SSebastien.Roy@Sun.COM };
49*10616SSebastien.Roy@Sun.COM
50*10616SSebastien.Roy@Sun.COM static struct modlinkage mac_ipv6_modlinkage = {
51*10616SSebastien.Roy@Sun.COM MODREV_1,
52*10616SSebastien.Roy@Sun.COM &mac_ipv6_modlmisc,
53*10616SSebastien.Roy@Sun.COM NULL
54*10616SSebastien.Roy@Sun.COM };
55*10616SSebastien.Roy@Sun.COM
56*10616SSebastien.Roy@Sun.COM static mactype_ops_t mac_ipv6_type_ops;
57*10616SSebastien.Roy@Sun.COM
58*10616SSebastien.Roy@Sun.COM int
_init(void)59*10616SSebastien.Roy@Sun.COM _init(void)
60*10616SSebastien.Roy@Sun.COM {
61*10616SSebastien.Roy@Sun.COM mactype_register_t *mtrp;
62*10616SSebastien.Roy@Sun.COM int err;
63*10616SSebastien.Roy@Sun.COM
64*10616SSebastien.Roy@Sun.COM if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
65*10616SSebastien.Roy@Sun.COM return (EINVAL);
66*10616SSebastien.Roy@Sun.COM mtrp->mtr_ident = MAC_PLUGIN_IDENT_IPV6;
67*10616SSebastien.Roy@Sun.COM mtrp->mtr_ops = &mac_ipv6_type_ops;
68*10616SSebastien.Roy@Sun.COM mtrp->mtr_mactype = DL_IPV6;
69*10616SSebastien.Roy@Sun.COM mtrp->mtr_nativetype = DL_IPV6;
70*10616SSebastien.Roy@Sun.COM mtrp->mtr_addrlen = sizeof (in6_addr_t);
71*10616SSebastien.Roy@Sun.COM if ((err = mactype_register(mtrp)) == 0) {
72*10616SSebastien.Roy@Sun.COM if ((err = mod_install(&mac_ipv6_modlinkage)) != 0)
73*10616SSebastien.Roy@Sun.COM (void) mactype_unregister(MAC_PLUGIN_IDENT_IPV6);
74*10616SSebastien.Roy@Sun.COM }
75*10616SSebastien.Roy@Sun.COM mactype_free(mtrp);
76*10616SSebastien.Roy@Sun.COM return (err);
77*10616SSebastien.Roy@Sun.COM }
78*10616SSebastien.Roy@Sun.COM
79*10616SSebastien.Roy@Sun.COM int
_fini(void)80*10616SSebastien.Roy@Sun.COM _fini(void)
81*10616SSebastien.Roy@Sun.COM {
82*10616SSebastien.Roy@Sun.COM int err;
83*10616SSebastien.Roy@Sun.COM if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IPV6)) != 0)
84*10616SSebastien.Roy@Sun.COM return (err);
85*10616SSebastien.Roy@Sun.COM return (mod_remove(&mac_ipv6_modlinkage));
86*10616SSebastien.Roy@Sun.COM }
87*10616SSebastien.Roy@Sun.COM
88*10616SSebastien.Roy@Sun.COM int
_info(struct modinfo * modinfop)89*10616SSebastien.Roy@Sun.COM _info(struct modinfo *modinfop)
90*10616SSebastien.Roy@Sun.COM {
91*10616SSebastien.Roy@Sun.COM return (mod_info(&mac_ipv6_modlinkage, modinfop));
92*10616SSebastien.Roy@Sun.COM }
93*10616SSebastien.Roy@Sun.COM
94*10616SSebastien.Roy@Sun.COM
95*10616SSebastien.Roy@Sun.COM /*
96*10616SSebastien.Roy@Sun.COM * MAC Type plugin operations
97*10616SSebastien.Roy@Sun.COM */
98*10616SSebastien.Roy@Sun.COM
99*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
100*10616SSebastien.Roy@Sun.COM int
mac_ipv6_unicst_verify(const void * addr,void * pdata)101*10616SSebastien.Roy@Sun.COM mac_ipv6_unicst_verify(const void *addr, void *pdata)
102*10616SSebastien.Roy@Sun.COM {
103*10616SSebastien.Roy@Sun.COM const in6_addr_t *in6addr = addr;
104*10616SSebastien.Roy@Sun.COM if (IN6_IS_ADDR_UNSPECIFIED(in6addr) ||
105*10616SSebastien.Roy@Sun.COM IN6_IS_ADDR_LOOPBACK(in6addr) ||
106*10616SSebastien.Roy@Sun.COM IN6_IS_ADDR_MULTICAST(in6addr) ||
107*10616SSebastien.Roy@Sun.COM IN6_IS_ADDR_V4MAPPED(in6addr) ||
108*10616SSebastien.Roy@Sun.COM IN6_IS_ADDR_V4COMPAT(in6addr)) {
109*10616SSebastien.Roy@Sun.COM return (EINVAL);
110*10616SSebastien.Roy@Sun.COM }
111*10616SSebastien.Roy@Sun.COM return (0);
112*10616SSebastien.Roy@Sun.COM }
113*10616SSebastien.Roy@Sun.COM
114*10616SSebastien.Roy@Sun.COM /*
115*10616SSebastien.Roy@Sun.COM * Build an IPv6 link-layer header for tunneling. If provided, the
116*10616SSebastien.Roy@Sun.COM * template header provided by the driver supplies the traffic class, flow
117*10616SSebastien.Roy@Sun.COM * label, hop limit, and potential options. The template's payload length
118*10616SSebastien.Roy@Sun.COM * must either be 0 if there are no extension headers, or reflect the size
119*10616SSebastien.Roy@Sun.COM * of the extension headers if present. The template's next header value
120*10616SSebastien.Roy@Sun.COM * must either be IPPROTO_NONE if no extension headers are present, or
121*10616SSebastien.Roy@Sun.COM * reflect the type of extension header that follows (the same is true for
122*10616SSebastien.Roy@Sun.COM * the field values of the extension headers themselves.)
123*10616SSebastien.Roy@Sun.COM */
124*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
125*10616SSebastien.Roy@Sun.COM mblk_t *
mac_ipv6_header(const void * saddr,const void * daddr,uint32_t sap,void * pdata,mblk_t * payload,size_t extra_len)126*10616SSebastien.Roy@Sun.COM mac_ipv6_header(const void *saddr, const void *daddr, uint32_t sap, void *pdata,
127*10616SSebastien.Roy@Sun.COM mblk_t *payload, size_t extra_len)
128*10616SSebastien.Roy@Sun.COM {
129*10616SSebastien.Roy@Sun.COM ip6_t *ip6hp;
130*10616SSebastien.Roy@Sun.COM ip6_t *tmpl_ip6hp = pdata;
131*10616SSebastien.Roy@Sun.COM mblk_t *mp;
132*10616SSebastien.Roy@Sun.COM size_t hdr_len = sizeof (ip6_t);
133*10616SSebastien.Roy@Sun.COM uint8_t *nxt_proto;
134*10616SSebastien.Roy@Sun.COM
135*10616SSebastien.Roy@Sun.COM if (!mac_ipv4_sap_verify(sap, NULL, NULL))
136*10616SSebastien.Roy@Sun.COM return (NULL);
137*10616SSebastien.Roy@Sun.COM
138*10616SSebastien.Roy@Sun.COM if (tmpl_ip6hp != NULL)
139*10616SSebastien.Roy@Sun.COM hdr_len = sizeof (ip6_t) + tmpl_ip6hp->ip6_plen;
140*10616SSebastien.Roy@Sun.COM
141*10616SSebastien.Roy@Sun.COM if ((mp = allocb(hdr_len + extra_len, BPRI_HI)) == NULL)
142*10616SSebastien.Roy@Sun.COM return (NULL);
143*10616SSebastien.Roy@Sun.COM
144*10616SSebastien.Roy@Sun.COM ip6hp = (ip6_t *)mp->b_rptr;
145*10616SSebastien.Roy@Sun.COM
146*10616SSebastien.Roy@Sun.COM bzero(ip6hp, hdr_len + extra_len);
147*10616SSebastien.Roy@Sun.COM if (tmpl_ip6hp != NULL) {
148*10616SSebastien.Roy@Sun.COM bcopy(tmpl_ip6hp, ip6hp, hdr_len);
149*10616SSebastien.Roy@Sun.COM } else {
150*10616SSebastien.Roy@Sun.COM ip6hp->ip6_nxt = IPPROTO_NONE;
151*10616SSebastien.Roy@Sun.COM ip6hp->ip6_hlim = IPTUN_DEFAULT_HOPLIMIT;
152*10616SSebastien.Roy@Sun.COM }
153*10616SSebastien.Roy@Sun.COM
154*10616SSebastien.Roy@Sun.COM ip6hp->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
155*10616SSebastien.Roy@Sun.COM ip6hp->ip6_plen = 0;
156*10616SSebastien.Roy@Sun.COM
157*10616SSebastien.Roy@Sun.COM nxt_proto = &ip6hp->ip6_nxt;
158*10616SSebastien.Roy@Sun.COM if (*nxt_proto != IPPROTO_NONE) {
159*10616SSebastien.Roy@Sun.COM ip6_dest_t *hdrptr = (ip6_dest_t *)(ip6hp + 1);
160*10616SSebastien.Roy@Sun.COM nxt_proto = &hdrptr->ip6d_nxt;
161*10616SSebastien.Roy@Sun.COM while (*nxt_proto != IPPROTO_NONE) {
162*10616SSebastien.Roy@Sun.COM hdrptr = (ip6_dest_t *)((uint8_t *)hdrptr +
163*10616SSebastien.Roy@Sun.COM (8 * (hdrptr->ip6d_len + 1)));
164*10616SSebastien.Roy@Sun.COM nxt_proto = &hdrptr->ip6d_nxt;
165*10616SSebastien.Roy@Sun.COM }
166*10616SSebastien.Roy@Sun.COM }
167*10616SSebastien.Roy@Sun.COM *nxt_proto = (uint8_t)sap;
168*10616SSebastien.Roy@Sun.COM bcopy(saddr, &(ip6hp->ip6_src), sizeof (in6_addr_t));
169*10616SSebastien.Roy@Sun.COM bcopy(daddr, &(ip6hp->ip6_dst), sizeof (in6_addr_t));
170*10616SSebastien.Roy@Sun.COM
171*10616SSebastien.Roy@Sun.COM mp->b_wptr += hdr_len;
172*10616SSebastien.Roy@Sun.COM return (mp);
173*10616SSebastien.Roy@Sun.COM }
174*10616SSebastien.Roy@Sun.COM
175*10616SSebastien.Roy@Sun.COM /* ARGSUSED */
176*10616SSebastien.Roy@Sun.COM int
mac_ipv6_header_info(mblk_t * mp,void * pdata,mac_header_info_t * hdr_info)177*10616SSebastien.Roy@Sun.COM mac_ipv6_header_info(mblk_t *mp, void *pdata, mac_header_info_t *hdr_info)
178*10616SSebastien.Roy@Sun.COM {
179*10616SSebastien.Roy@Sun.COM ip6_t *ip6hp;
180*10616SSebastien.Roy@Sun.COM uint8_t *whereptr, *endptr;
181*10616SSebastien.Roy@Sun.COM uint8_t nexthdr;
182*10616SSebastien.Roy@Sun.COM
183*10616SSebastien.Roy@Sun.COM if (MBLKL(mp) < sizeof (ip6_t))
184*10616SSebastien.Roy@Sun.COM return (EINVAL);
185*10616SSebastien.Roy@Sun.COM
186*10616SSebastien.Roy@Sun.COM ip6hp = (ip6_t *)mp->b_rptr;
187*10616SSebastien.Roy@Sun.COM
188*10616SSebastien.Roy@Sun.COM /*
189*10616SSebastien.Roy@Sun.COM * IPv6 tunnels don't have a concept of link-layer multicast since
190*10616SSebastien.Roy@Sun.COM * they have fixed unicast endpoints.
191*10616SSebastien.Roy@Sun.COM */
192*10616SSebastien.Roy@Sun.COM if (mac_ipv6_unicst_verify(&ip6hp->ip6_dst, NULL) != 0)
193*10616SSebastien.Roy@Sun.COM return (EINVAL);
194*10616SSebastien.Roy@Sun.COM
195*10616SSebastien.Roy@Sun.COM nexthdr = ip6hp->ip6_nxt;
196*10616SSebastien.Roy@Sun.COM whereptr = (uint8_t *)(ip6hp + 1);
197*10616SSebastien.Roy@Sun.COM endptr = mp->b_wptr;
198*10616SSebastien.Roy@Sun.COM while (nexthdr != IPPROTO_ENCAP && nexthdr != IPPROTO_IPV6) {
199*10616SSebastien.Roy@Sun.COM ip6_dest_t *exthdrptr = (ip6_dest_t *)whereptr;
200*10616SSebastien.Roy@Sun.COM
201*10616SSebastien.Roy@Sun.COM if (whereptr + sizeof (ip6_dest_t) >= endptr)
202*10616SSebastien.Roy@Sun.COM return (EINVAL);
203*10616SSebastien.Roy@Sun.COM
204*10616SSebastien.Roy@Sun.COM nexthdr = exthdrptr->ip6d_nxt;
205*10616SSebastien.Roy@Sun.COM whereptr += 8 * (exthdrptr->ip6d_len + 1);
206*10616SSebastien.Roy@Sun.COM
207*10616SSebastien.Roy@Sun.COM if (whereptr > endptr)
208*10616SSebastien.Roy@Sun.COM return (EINVAL);
209*10616SSebastien.Roy@Sun.COM }
210*10616SSebastien.Roy@Sun.COM
211*10616SSebastien.Roy@Sun.COM hdr_info->mhi_hdrsize = whereptr - mp->b_rptr;
212*10616SSebastien.Roy@Sun.COM hdr_info->mhi_pktsize = 0;
213*10616SSebastien.Roy@Sun.COM hdr_info->mhi_daddr = (const uint8_t *)&(ip6hp->ip6_dst);
214*10616SSebastien.Roy@Sun.COM hdr_info->mhi_saddr = (const uint8_t *)&(ip6hp->ip6_src);
215*10616SSebastien.Roy@Sun.COM hdr_info->mhi_bindsap = hdr_info->mhi_origsap = nexthdr;
216*10616SSebastien.Roy@Sun.COM hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
217*10616SSebastien.Roy@Sun.COM return (0);
218*10616SSebastien.Roy@Sun.COM }
219*10616SSebastien.Roy@Sun.COM
220*10616SSebastien.Roy@Sun.COM /*
221*10616SSebastien.Roy@Sun.COM * This plugin's MAC plugin data is a template IPv6 header followed by
222*10616SSebastien.Roy@Sun.COM * optional extension headers. The chain of headers must be terminated by
223*10616SSebastien.Roy@Sun.COM * a header with a next header value of IPPROTO_NONE. The payload length
224*10616SSebastien.Roy@Sun.COM * of the IPv6 header must be 0 if there are no extension headers, or must
225*10616SSebastien.Roy@Sun.COM * reflect the total size of extension headers present.
226*10616SSebastien.Roy@Sun.COM */
227*10616SSebastien.Roy@Sun.COM boolean_t
mac_ipv6_pdata_verify(void * pdata,size_t pdata_size)228*10616SSebastien.Roy@Sun.COM mac_ipv6_pdata_verify(void *pdata, size_t pdata_size)
229*10616SSebastien.Roy@Sun.COM {
230*10616SSebastien.Roy@Sun.COM ip6_t *ip6hp = pdata;
231*10616SSebastien.Roy@Sun.COM uint8_t *whereptr, *endptr;
232*10616SSebastien.Roy@Sun.COM uint8_t nexthdr;
233*10616SSebastien.Roy@Sun.COM
234*10616SSebastien.Roy@Sun.COM /*
235*10616SSebastien.Roy@Sun.COM * Since the plugin does not require plugin data, it is acceptable
236*10616SSebastien.Roy@Sun.COM * for drivers to pass in NULL plugin data as long as the plugin
237*10616SSebastien.Roy@Sun.COM * data size is consistent.
238*10616SSebastien.Roy@Sun.COM */
239*10616SSebastien.Roy@Sun.COM if (pdata == NULL)
240*10616SSebastien.Roy@Sun.COM return (pdata_size == 0);
241*10616SSebastien.Roy@Sun.COM
242*10616SSebastien.Roy@Sun.COM /* First verify that we have enough data to hold an IPv6 header. */
243*10616SSebastien.Roy@Sun.COM if (pdata_size < sizeof (ip6_t))
244*10616SSebastien.Roy@Sun.COM return (B_FALSE);
245*10616SSebastien.Roy@Sun.COM /* Make sure that pdata_size is consistent with the payload length. */
246*10616SSebastien.Roy@Sun.COM if (pdata_size != sizeof (ip6_t) + ip6hp->ip6_plen)
247*10616SSebastien.Roy@Sun.COM return (B_FALSE);
248*10616SSebastien.Roy@Sun.COM
249*10616SSebastien.Roy@Sun.COM /*
250*10616SSebastien.Roy@Sun.COM * Make sure that the header chain is terminated by a header with a
251*10616SSebastien.Roy@Sun.COM * next header value of IPPROTO_NONE.
252*10616SSebastien.Roy@Sun.COM */
253*10616SSebastien.Roy@Sun.COM nexthdr = ip6hp->ip6_nxt;
254*10616SSebastien.Roy@Sun.COM if (nexthdr == IPPROTO_NONE)
255*10616SSebastien.Roy@Sun.COM return (ip6hp->ip6_plen == 0);
256*10616SSebastien.Roy@Sun.COM whereptr = (uint8_t *)(ip6hp + 1);
257*10616SSebastien.Roy@Sun.COM endptr = (uint8_t *)pdata + pdata_size;
258*10616SSebastien.Roy@Sun.COM
259*10616SSebastien.Roy@Sun.COM while (nexthdr != IPPROTO_NONE && whereptr < endptr) {
260*10616SSebastien.Roy@Sun.COM ip6_dest_t *hdrptr = (ip6_dest_t *)whereptr;
261*10616SSebastien.Roy@Sun.COM
262*10616SSebastien.Roy@Sun.COM /* make sure we're pointing at a complete header */
263*10616SSebastien.Roy@Sun.COM if (whereptr + sizeof (ip6_dest_t) > endptr)
264*10616SSebastien.Roy@Sun.COM break;
265*10616SSebastien.Roy@Sun.COM nexthdr = hdrptr->ip6d_nxt;
266*10616SSebastien.Roy@Sun.COM whereptr += 8 * (hdrptr->ip6d_len + 1);
267*10616SSebastien.Roy@Sun.COM }
268*10616SSebastien.Roy@Sun.COM
269*10616SSebastien.Roy@Sun.COM return (nexthdr == IPPROTO_NONE && whereptr == endptr);
270*10616SSebastien.Roy@Sun.COM }
271*10616SSebastien.Roy@Sun.COM
272*10616SSebastien.Roy@Sun.COM static mactype_ops_t mac_ipv6_type_ops = {
273*10616SSebastien.Roy@Sun.COM MTOPS_PDATA_VERIFY,
274*10616SSebastien.Roy@Sun.COM mac_ipv6_unicst_verify,
275*10616SSebastien.Roy@Sun.COM mac_ipv4_multicst_verify, /* neither plugin supports multicast */
276*10616SSebastien.Roy@Sun.COM mac_ipv4_sap_verify, /* same set of legal SAP values */
277*10616SSebastien.Roy@Sun.COM mac_ipv6_header,
278*10616SSebastien.Roy@Sun.COM mac_ipv6_header_info,
279*10616SSebastien.Roy@Sun.COM mac_ipv6_pdata_verify,
280*10616SSebastien.Roy@Sun.COM NULL,
281*10616SSebastien.Roy@Sun.COM NULL,
282*10616SSebastien.Roy@Sun.COM NULL
283*10616SSebastien.Roy@Sun.COM };
284