]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_output.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_output.c
1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $ */
2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73
74 #include <sys/param.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/errno.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84 #include <sys/kauth.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88 #include <net/dlil.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip_var.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/nd6.h>
99
100 #if IPSEC
101 #include <netinet6/ipsec.h>
102 #if INET6
103 #include <netinet6/ipsec6.h>
104 #endif
105 #include <netkey/key.h>
106 extern int ipsec_bypass;
107 #endif /* IPSEC */
108 extern lck_mtx_t *nd6_mutex;
109
110 #if CONFIG_MACF_NET
111 #include <security/mac.h>
112 #endif /* MAC_NET */
113
114 #include <netinet6/ip6_fw.h>
115
116 #include <net/net_osdep.h>
117
118 #include <netinet/kpi_ipfilter_var.h>
119
120 #ifndef __APPLE__
121 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
122 #endif
123
124
125 extern u_long route_generation;
126
127 struct ip6_exthdrs {
128 struct mbuf *ip6e_ip6;
129 struct mbuf *ip6e_hbh;
130 struct mbuf *ip6e_dest1;
131 struct mbuf *ip6e_rthdr;
132 struct mbuf *ip6e_dest2;
133 };
134
135 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
136 struct socket *, struct sockopt *sopt);
137 static int ip6_setmoptions(int, struct inpcb *, struct mbuf *);
138 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
139 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
140 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
141 struct ip6_frag **);
142 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
143 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
144
145 extern int ip_createmoptions(struct ip_moptions **imop);
146 extern int ip_addmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
147 extern int ip_dropmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
148 extern lck_mtx_t *ip6_mutex;
149
150 /*
151 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
152 * header (with pri, len, nxt, hlim, src, dst).
153 * This function may modify ver and hlim only.
154 * The mbuf chain containing the packet will be freed.
155 * The mbuf opt, if present, will not be freed.
156 *
157 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
158 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
159 * which is rt_rmx.rmx_mtu.
160 */
161 int
162 ip6_output(
163 struct mbuf *m0,
164 struct ip6_pktopts *opt,
165 struct route_in6 *ro,
166 int flags,
167 struct ip6_moptions *im6o,
168 struct ifnet **ifpp, /* XXX: just for statistics */
169 int locked)
170 {
171 struct ip6_hdr *ip6, *mhip6;
172 struct ifnet *ifp, *origifp;
173 struct mbuf *m = m0;
174 int hlen, tlen, len, off;
175 struct route_in6 ip6route;
176 struct sockaddr_in6 *dst;
177 int error = 0;
178 struct in6_ifaddr *ia = NULL;
179 u_long mtu;
180 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
181 struct ip6_exthdrs exthdrs;
182 struct in6_addr finaldst;
183 struct route_in6 *ro_pmtu = NULL;
184 int hdrsplit = 0;
185 int needipsec = 0;
186 ipfilter_t inject_filter_ref;
187
188 #if IPSEC
189 int needipsectun = 0;
190 struct socket *so = NULL;
191 struct secpolicy *sp = NULL;
192
193 if (!locked)
194 lck_mtx_lock(ip6_mutex);
195 /* for AH processing. stupid to have "socket" variable in IP layer... */
196 if (ipsec_bypass == 0)
197 {
198 so = ipsec_getsocket(m);
199 (void)ipsec_setsocket(m, NULL);
200 }
201 #endif /* IPSEC */
202
203 ip6 = mtod(m, struct ip6_hdr *);
204 inject_filter_ref = ipf_get_inject_filter(m);
205
206 #define MAKE_EXTHDR(hp, mp) \
207 do { \
208 if (hp) { \
209 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
210 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
211 ((eh)->ip6e_len + 1) << 3); \
212 if (error) \
213 goto freehdrs; \
214 } \
215 } while (0)
216
217 bzero(&exthdrs, sizeof(exthdrs));
218
219 if (opt) {
220 /* Hop-by-Hop options header */
221 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
222 /* Destination options header(1st part) */
223 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
224 /* Routing header */
225 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
226 /* Destination options header(2nd part) */
227 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
228 }
229
230 #if IPSEC
231 if (ipsec_bypass != 0)
232 goto skip_ipsec;
233
234 /* get a security policy for this packet */
235 if (so == NULL)
236 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
237 else
238 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
239
240 if (sp == NULL) {
241 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
242 goto freehdrs;
243 }
244
245 error = 0;
246
247 /* check policy */
248 switch (sp->policy) {
249 case IPSEC_POLICY_DISCARD:
250 case IPSEC_POLICY_GENERATE:
251 /*
252 * This packet is just discarded.
253 */
254 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
255 goto freehdrs;
256
257 case IPSEC_POLICY_BYPASS:
258 case IPSEC_POLICY_NONE:
259 /* no need to do IPsec. */
260 needipsec = 0;
261 break;
262
263 case IPSEC_POLICY_IPSEC:
264 if (sp->req == NULL) {
265 /* acquire a policy */
266 error = key_spdacquire(sp);
267 goto freehdrs;
268 }
269 needipsec = 1;
270 break;
271
272 case IPSEC_POLICY_ENTRUST:
273 default:
274 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
275 }
276 skip_ipsec:
277 #endif /* IPSEC */
278
279 /*
280 * Calculate the total length of the extension header chain.
281 * Keep the length of the unfragmentable part for fragmentation.
282 */
283 optlen = 0;
284 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
285 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
286 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
287 unfragpartlen = optlen + sizeof(struct ip6_hdr);
288 /* NOTE: we don't add AH/ESP length here. do that later. */
289 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
290
291 /*
292 * If we need IPsec, or there is at least one extension header,
293 * separate IP6 header from the payload.
294 */
295 if ((needipsec || optlen) && !hdrsplit) {
296 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
297 m = NULL;
298 goto freehdrs;
299 }
300 m = exthdrs.ip6e_ip6;
301 hdrsplit++;
302 }
303
304 /* adjust pointer */
305 ip6 = mtod(m, struct ip6_hdr *);
306
307 /* adjust mbuf packet header length */
308 m->m_pkthdr.len += optlen;
309 plen = m->m_pkthdr.len - sizeof(*ip6);
310
311 /* If this is a jumbo payload, insert a jumbo payload option. */
312 if (plen > IPV6_MAXPACKET) {
313 if (!hdrsplit) {
314 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
315 m = NULL;
316 goto freehdrs;
317 }
318 m = exthdrs.ip6e_ip6;
319 hdrsplit++;
320 }
321 /* adjust pointer */
322 ip6 = mtod(m, struct ip6_hdr *);
323 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
324 goto freehdrs;
325 ip6->ip6_plen = 0;
326 } else
327 ip6->ip6_plen = htons(plen);
328
329 /*
330 * Concatenate headers and fill in next header fields.
331 * Here we have, on "m"
332 * IPv6 payload
333 * and we insert headers accordingly. Finally, we should be getting:
334 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
335 *
336 * during the header composing process, "m" points to IPv6 header.
337 * "mprev" points to an extension header prior to esp.
338 */
339 {
340 u_char *nexthdrp = &ip6->ip6_nxt;
341 struct mbuf *mprev = m;
342
343 /*
344 * we treat dest2 specially. this makes IPsec processing
345 * much easier. the goal here is to make mprev point the
346 * mbuf prior to dest2.
347 *
348 * result: IPv6 dest2 payload
349 * m and mprev will point to IPv6 header.
350 */
351 if (exthdrs.ip6e_dest2) {
352 if (!hdrsplit)
353 panic("assumption failed: hdr not split");
354 exthdrs.ip6e_dest2->m_next = m->m_next;
355 m->m_next = exthdrs.ip6e_dest2;
356 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
357 ip6->ip6_nxt = IPPROTO_DSTOPTS;
358 }
359
360 #define MAKE_CHAIN(m, mp, p, i)\
361 do {\
362 if (m) {\
363 if (!hdrsplit) \
364 panic("assumption failed: hdr not split"); \
365 *mtod((m), u_char *) = *(p);\
366 *(p) = (i);\
367 p = mtod((m), u_char *);\
368 (m)->m_next = (mp)->m_next;\
369 (mp)->m_next = (m);\
370 (mp) = (m);\
371 }\
372 } while (0)
373 /*
374 * result: IPv6 hbh dest1 rthdr dest2 payload
375 * m will point to IPv6 header. mprev will point to the
376 * extension header prior to dest2 (rthdr in the above case).
377 */
378 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
379 nexthdrp, IPPROTO_HOPOPTS);
380 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
381 nexthdrp, IPPROTO_DSTOPTS);
382 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
383 nexthdrp, IPPROTO_ROUTING);
384
385 if (!TAILQ_EMPTY(&ipv6_filters)) {
386 struct ipfilter *filter;
387 int seen = (inject_filter_ref == 0);
388 int fixscope = 0;
389 struct ipf_pktopts *ippo = 0, ipf_pktopts;
390
391 if (im6o != NULL && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
392 ippo = &ipf_pktopts;
393 ippo->ippo_flags = IPPOF_MCAST_OPTS;
394 ippo->ippo_mcast_ifnet = im6o->im6o_multicast_ifp;
395 ippo->ippo_mcast_ttl = im6o->im6o_multicast_hlim;
396 ippo->ippo_mcast_loop = im6o->im6o_multicast_loop;
397 }
398
399 /* Hack: embed the scope_id in the destination */
400 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
401 (ip6->ip6_dst.s6_addr16[1] == 0) && (ro != NULL)) {
402 fixscope = 1;
403 ip6->ip6_dst.s6_addr16[1] = htons(ro->ro_dst.sin6_scope_id);
404 }
405 {
406 lck_mtx_unlock(ip6_mutex);
407 ipf_ref();
408 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
409 /*
410 * No need to proccess packet twice if we've
411 * already seen it
412 */
413 if (seen == 0) {
414 if ((struct ipfilter *)inject_filter_ref == filter)
415 seen = 1;
416 } else if (filter->ipf_filter.ipf_output) {
417 errno_t result;
418
419 result = filter->ipf_filter.ipf_output(filter->ipf_filter.cookie, (mbuf_t*)&m, ippo);
420 if (result == EJUSTRETURN) {
421 ipf_unref();
422 locked = 1; /* Don't want to take lock to unlock it right away */
423 goto done;
424 }
425 if (result != 0) {
426 ipf_unref();
427 locked = 1; /* Don't want to take lock to unlock it right away */
428 goto bad;
429 }
430 }
431 }
432 ipf_unref();
433 lck_mtx_lock(ip6_mutex);
434 }
435 ip6 = mtod(m, struct ip6_hdr *);
436 /* Hack: cleanup embedded scope_id if we put it there */
437 if (fixscope)
438 ip6->ip6_dst.s6_addr16[1] = 0;
439 }
440
441 #if IPSEC
442 if (!needipsec)
443 goto skip_ipsec2;
444
445 /*
446 * pointers after IPsec headers are not valid any more.
447 * other pointers need a great care too.
448 * (IPsec routines should not mangle mbufs prior to AH/ESP)
449 */
450 exthdrs.ip6e_dest2 = NULL;
451
452 {
453 struct ip6_rthdr *rh = NULL;
454 int segleft_org = 0;
455 struct ipsec_output_state state;
456
457 if (exthdrs.ip6e_rthdr) {
458 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
459 segleft_org = rh->ip6r_segleft;
460 rh->ip6r_segleft = 0;
461 }
462
463 bzero(&state, sizeof(state));
464 state.m = m;
465 lck_mtx_unlock(ip6_mutex);
466 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
467 &needipsectun);
468 lck_mtx_lock(ip6_mutex);
469 m = state.m;
470 if (error) {
471 /* mbuf is already reclaimed in ipsec6_output_trans. */
472 m = NULL;
473 switch (error) {
474 case EHOSTUNREACH:
475 case ENETUNREACH:
476 case EMSGSIZE:
477 case ENOBUFS:
478 case ENOMEM:
479 break;
480 default:
481 printf("ip6_output (ipsec): error code %d\n", error);
482 /* fall through */
483 case ENOENT:
484 /* don't show these error codes to the user */
485 error = 0;
486 break;
487 }
488 goto bad;
489 }
490 if (exthdrs.ip6e_rthdr) {
491 /* ah6_output doesn't modify mbuf chain */
492 rh->ip6r_segleft = segleft_org;
493 }
494 }
495 skip_ipsec2:;
496 #endif
497 }
498
499 /*
500 * If there is a routing header, replace destination address field
501 * with the first hop of the routing header.
502 */
503 if (exthdrs.ip6e_rthdr) {
504 struct ip6_rthdr *rh =
505 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
506 struct ip6_rthdr *));
507 struct ip6_rthdr0 *rh0;
508
509 finaldst = ip6->ip6_dst;
510 switch (rh->ip6r_type) {
511 case IPV6_RTHDR_TYPE_0:
512 rh0 = (struct ip6_rthdr0 *)rh;
513 ip6->ip6_dst = rh0->ip6r0_addr[0];
514 bcopy((caddr_t)&rh0->ip6r0_addr[1],
515 (caddr_t)&rh0->ip6r0_addr[0],
516 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
517 );
518 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
519 break;
520 default: /* is it possible? */
521 error = EINVAL;
522 goto bad;
523 }
524 }
525
526 /* Source address validation */
527 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
528 (flags & IPV6_DADOUTPUT) == 0) {
529 error = EOPNOTSUPP;
530 ip6stat.ip6s_badscope++;
531 goto bad;
532 }
533 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
534 error = EOPNOTSUPP;
535 ip6stat.ip6s_badscope++;
536 goto bad;
537 }
538
539 ip6stat.ip6s_localout++;
540
541 /*
542 * Route packet.
543 */
544 if (ro == 0) {
545 ro = &ip6route;
546 bzero((caddr_t)ro, sizeof(*ro));
547 }
548 ro_pmtu = ro;
549 if (opt && opt->ip6po_rthdr)
550 ro = &opt->ip6po_route;
551 dst = (struct sockaddr_in6 *)&ro->ro_dst;
552 /*
553 * If there is a cached route,
554 * check that it is to the same destination
555 * and is still up. If not, free it and try again.
556 */
557 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
558 dst->sin6_family != AF_INET6 ||
559 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst) ||
560 ro->ro_rt->generation_id != route_generation)) {
561 rtfree(ro->ro_rt);
562 ro->ro_rt = (struct rtentry *)0;
563 }
564 if (ro->ro_rt == 0) {
565 bzero(dst, sizeof(*dst));
566 dst->sin6_family = AF_INET6;
567 dst->sin6_len = sizeof(struct sockaddr_in6);
568 dst->sin6_addr = ip6->ip6_dst;
569 #if SCOPEDROUTING
570 /* XXX: sin6_scope_id should already be fixed at this point */
571 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
572 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]);
573 #endif
574 }
575 #if IPSEC
576 if (needipsec && needipsectun) {
577 struct ipsec_output_state state;
578 int tunneledv4 = 0;
579
580 /*
581 * All the extension headers will become inaccessible
582 * (since they can be encrypted).
583 * Don't panic, we need no more updates to extension headers
584 * on inner IPv6 packet (since they are now encapsulated).
585 *
586 * IPv6 [ESP|AH] IPv6 [extension headers] payload
587 */
588 bzero(&exthdrs, sizeof(exthdrs));
589 exthdrs.ip6e_ip6 = m;
590
591 bzero(&state, sizeof(state));
592 state.m = m;
593 state.ro = (struct route *)ro;
594 state.dst = (struct sockaddr *)dst;
595 lck_mtx_unlock(ip6_mutex);
596 error = ipsec6_output_tunnel(&state, sp, flags, &tunneledv4);
597 lck_mtx_lock(ip6_mutex);
598 if (tunneledv4) /* tunneled in IPv4 - packet is gone */
599 goto done;
600 m = state.m;
601 ro = (struct route_in6 *)state.ro;
602 dst = (struct sockaddr_in6 *)state.dst;
603 if (error) {
604 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
605 m0 = m = NULL;
606 m = NULL;
607 switch (error) {
608 case EHOSTUNREACH:
609 case ENETUNREACH:
610 case EMSGSIZE:
611 case ENOBUFS:
612 case ENOMEM:
613 break;
614 default:
615 printf("ip6_output (ipsec): error code %d\n", error);
616 /* fall through */
617 case ENOENT:
618 /* don't show these error codes to the user */
619 error = 0;
620 break;
621 }
622 goto bad;
623 }
624
625 exthdrs.ip6e_ip6 = m;
626 }
627 #endif /* IPSEC */
628
629 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
630 /* Unicast */
631
632 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
633 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
634 /* xxx
635 * interface selection comes here
636 * if an interface is specified from an upper layer,
637 * ifp must point it.
638 */
639 lck_mtx_lock(rt_mtx);
640 if (ro->ro_rt == 0) {
641 /*
642 * non-bsdi always clone routes, if parent is
643 * PRF_CLONING.
644 */
645 rtalloc_ign_locked((struct route *)ro, 0UL);
646 }
647 if (ro->ro_rt == 0) {
648 ip6stat.ip6s_noroute++;
649 error = EHOSTUNREACH;
650 lck_mtx_unlock(rt_mtx);
651 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
652 goto bad;
653 }
654 ia = ifatoia6(ro->ro_rt->rt_ifa);
655 ifp = ro->ro_rt->rt_ifp;
656 ro->ro_rt->rt_use++;
657 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
658 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
659 lck_mtx_unlock(rt_mtx);
660 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
661
662 in6_ifstat_inc(ifp, ifs6_out_request);
663
664 /*
665 * Check if the outgoing interface conflicts with
666 * the interface specified by ifi6_ifindex (if specified).
667 * Note that loopback interface is always okay.
668 * (this may happen when we are sending a packet to one of
669 * our own addresses.)
670 */
671 if (opt && opt->ip6po_pktinfo
672 && opt->ip6po_pktinfo->ipi6_ifindex) {
673 if (!(ifp->if_flags & IFF_LOOPBACK)
674 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
675 ip6stat.ip6s_noroute++;
676 in6_ifstat_inc(ifp, ifs6_out_discard);
677 error = EHOSTUNREACH;
678 goto bad;
679 }
680 }
681
682 if (opt && opt->ip6po_hlim != -1)
683 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
684 } else {
685 /* Multicast */
686 struct in6_multi *in6m;
687
688 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
689
690 /*
691 * See if the caller provided any multicast options
692 */
693 ifp = NULL;
694 if (im6o != NULL) {
695 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
696 if (im6o->im6o_multicast_ifp != NULL)
697 ifp = im6o->im6o_multicast_ifp;
698 } else
699 ip6->ip6_hlim = ip6_defmcasthlim;
700
701 /*
702 * See if the caller provided the outgoing interface
703 * as an ancillary data.
704 * Boundary check for ifindex is assumed to be already done.
705 */
706 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
707 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
708
709 /*
710 * If the destination is a node-local scope multicast,
711 * the packet should be loop-backed only.
712 */
713 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
714 /*
715 * If the outgoing interface is already specified,
716 * it should be a loopback interface.
717 */
718 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
719 ip6stat.ip6s_badscope++;
720 error = ENETUNREACH; /* XXX: better error? */
721 /* XXX correct ifp? */
722 in6_ifstat_inc(ifp, ifs6_out_discard);
723 goto bad;
724 } else {
725 ifp = lo_ifp;
726 }
727 }
728
729 if (opt && opt->ip6po_hlim != -1)
730 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
731
732 /*
733 * If caller did not provide an interface lookup a
734 * default in the routing table. This is either a
735 * default for the speicfied group (i.e. a host
736 * route), or a multicast default (a route for the
737 * ``net'' ff00::/8).
738 */
739 if (ifp == NULL) {
740 lck_mtx_lock(rt_mtx);
741 if (ro->ro_rt == 0) {
742 ro->ro_rt = rtalloc1_locked(
743 (struct sockaddr *)&ro->ro_dst, 0, 0UL);
744 }
745 if (ro->ro_rt == 0) {
746 ip6stat.ip6s_noroute++;
747 lck_mtx_unlock(rt_mtx);
748 error = EHOSTUNREACH;
749 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
750 goto bad;
751 }
752 ia = ifatoia6(ro->ro_rt->rt_ifa);
753 ifp = ro->ro_rt->rt_ifp;
754 ro->ro_rt->rt_use++;
755 lck_mtx_unlock(rt_mtx);
756 }
757
758 if ((flags & IPV6_FORWARDING) == 0)
759 in6_ifstat_inc(ifp, ifs6_out_request);
760 in6_ifstat_inc(ifp, ifs6_out_mcast);
761
762 /*
763 * Confirm that the outgoing interface supports multicast.
764 */
765 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
766 ip6stat.ip6s_noroute++;
767 in6_ifstat_inc(ifp, ifs6_out_discard);
768 error = ENETUNREACH;
769 goto bad;
770 }
771 ifnet_lock_shared(ifp);
772 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
773 ifnet_lock_done(ifp);
774 if (in6m != NULL &&
775 (im6o == NULL || im6o->im6o_multicast_loop)) {
776 /*
777 * If we belong to the destination multicast group
778 * on the outgoing interface, and the caller did not
779 * forbid loopback, loop back a copy.
780 */
781 ip6_mloopback(ifp, m, dst);
782 } else {
783 /*
784 * If we are acting as a multicast router, perform
785 * multicast forwarding as if the packet had just
786 * arrived on the interface to which we are about
787 * to send. The multicast forwarding function
788 * recursively calls this function, using the
789 * IPV6_FORWARDING flag to prevent infinite recursion.
790 *
791 * Multicasts that are looped back by ip6_mloopback(),
792 * above, will be forwarded by the ip6_input() routine,
793 * if necessary.
794 */
795 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
796 if (ip6_mforward(ip6, ifp, m) != 0) {
797 m_freem(m);
798 goto done;
799 }
800 }
801 }
802 /*
803 * Multicasts with a hoplimit of zero may be looped back,
804 * above, but must not be transmitted on a network.
805 * Also, multicasts addressed to the loopback interface
806 * are not sent -- the above call to ip6_mloopback() will
807 * loop back a copy if this host actually belongs to the
808 * destination group on the loopback interface.
809 */
810 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
811 m_freem(m);
812 goto done;
813 }
814 }
815
816 /*
817 * Fill the outgoing inteface to tell the upper layer
818 * to increment per-interface statistics.
819 */
820 if (ifpp)
821 *ifpp = ifp;
822
823 /*
824 * Determine path MTU.
825 */
826 if (ro_pmtu != ro) {
827 /* The first hop and the final destination may differ. */
828 struct sockaddr_in6 *sin6_fin =
829 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
830 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
831 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
832 &finaldst))) {
833 rtfree(ro_pmtu->ro_rt);
834 ro_pmtu->ro_rt = (struct rtentry *)0;
835 }
836 if (ro_pmtu->ro_rt == 0) {
837 bzero(sin6_fin, sizeof(*sin6_fin));
838 sin6_fin->sin6_family = AF_INET6;
839 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
840 sin6_fin->sin6_addr = finaldst;
841
842 rtalloc((struct route *)ro_pmtu);
843 }
844 }
845 if (ro_pmtu->ro_rt != NULL) {
846 u_int32_t ifmtu = IN6_LINKMTU(ifp);
847
848 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
849 if (mtu > ifmtu || mtu == 0) {
850 /*
851 * The MTU on the route is larger than the MTU on
852 * the interface! This shouldn't happen, unless the
853 * MTU of the interface has been changed after the
854 * interface was brought up. Change the MTU in the
855 * route to match the interface MTU (as long as the
856 * field isn't locked).
857 *
858 * if MTU on the route is 0, we need to fix the MTU.
859 * this case happens with path MTU discovery timeouts.
860 */
861 mtu = ifmtu;
862 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
863 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
864 }
865 } else {
866 mtu = IN6_LINKMTU(ifp);
867 }
868
869 /*
870 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
871 */
872 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
873 mtu = IPV6_MMTU;
874
875 /* Fake scoped addresses */
876 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
877 /*
878 * If source or destination address is a scoped address, and
879 * the packet is going to be sent to a loopback interface,
880 * we should keep the original interface.
881 */
882
883 /*
884 * XXX: this is a very experimental and temporary solution.
885 * We eventually have sockaddr_in6 and use the sin6_scope_id
886 * field of the structure here.
887 * We rely on the consistency between two scope zone ids
888 * of source and destination, which should already be assured.
889 * Larger scopes than link will be supported in the future.
890 */
891 origifp = NULL;
892 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
893 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
894 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
895 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
896 /*
897 * XXX: origifp can be NULL even in those two cases above.
898 * For example, if we remove the (only) link-local address
899 * from the loopback interface, and try to send a link-local
900 * address without link-id information. Then the source
901 * address is ::1, and the destination address is the
902 * link-local address with its s6_addr16[1] being zero.
903 * What is worse, if the packet goes to the loopback interface
904 * by a default rejected route, the null pointer would be
905 * passed to looutput, and the kernel would hang.
906 * The following last resort would prevent such disaster.
907 */
908 if (origifp == NULL)
909 origifp = ifp;
910 }
911 else
912 origifp = ifp;
913 #ifndef SCOPEDROUTING
914 /*
915 * clear embedded scope identifiers if necessary.
916 * in6_clearscope will touch the addresses only when necessary.
917 */
918 in6_clearscope(&ip6->ip6_src);
919 in6_clearscope(&ip6->ip6_dst);
920 #endif
921
922 /*
923 * Check with the firewall...
924 */
925 if (ip6_fw_enable && ip6_fw_chk_ptr) {
926 u_short port = 0;
927 m->m_pkthdr.rcvif = NULL; /* XXX */
928 /* If ipfw says divert, we have to just drop packet */
929 if (ip6_fw_chk_ptr(&ip6, ifp, &port, &m)) {
930 m_freem(m);
931 goto done;
932 }
933 if (!m) {
934 error = EACCES;
935 goto done;
936 }
937 }
938
939 /*
940 * If the outgoing packet contains a hop-by-hop options header,
941 * it must be examined and processed even by the source node.
942 * (RFC 2460, section 4.)
943 */
944 if (exthdrs.ip6e_hbh) {
945 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
946 u_int32_t dummy1; /* XXX unused */
947 u_int32_t dummy2; /* XXX unused */
948
949 #if DIAGNOSTIC
950 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
951 panic("ip6e_hbh is not continuous");
952 #endif
953 /*
954 * XXX: if we have to send an ICMPv6 error to the sender,
955 * we need the M_LOOP flag since icmp6_error() expects
956 * the IPv6 and the hop-by-hop options header are
957 * continuous unless the flag is set.
958 */
959 m->m_flags |= M_LOOP;
960 m->m_pkthdr.rcvif = ifp;
961 if (ip6_process_hopopts(m,
962 (u_int8_t *)(hbh + 1),
963 ((hbh->ip6h_len + 1) << 3) -
964 sizeof(struct ip6_hbh),
965 &dummy1, &dummy2) < 0) {
966 /* m was already freed at this point */
967 error = EINVAL;/* better error? */
968 goto done;
969 }
970 m->m_flags &= ~M_LOOP; /* XXX */
971 m->m_pkthdr.rcvif = NULL;
972 }
973
974 /*
975 * Send the packet to the outgoing interface.
976 * If necessary, do IPv6 fragmentation before sending.
977 */
978 tlen = m->m_pkthdr.len;
979 if (tlen <= mtu
980 #if notyet
981 /*
982 * On any link that cannot convey a 1280-octet packet in one piece,
983 * link-specific fragmentation and reassembly must be provided at
984 * a layer below IPv6. [RFC 2460, sec.5]
985 * Thus if the interface has ability of link-level fragmentation,
986 * we can just send the packet even if the packet size is
987 * larger than the link's MTU.
988 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
989 */
990
991 || ifp->if_flags & IFF_FRAGMENTABLE
992 #endif
993 )
994 {
995 /* Record statistics for this interface address. */
996 if (ia && !(flags & IPV6_FORWARDING)) {
997 #ifndef __APPLE__
998 ia->ia_ifa.if_opackets++;
999 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1000 #endif
1001 }
1002 #ifdef IPSEC
1003 /* clean ipsec history once it goes out of the node */
1004 ipsec_delaux(m);
1005 #endif
1006
1007 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, 1);
1008 goto done;
1009 } else if (mtu < IPV6_MMTU) {
1010 /*
1011 * note that path MTU is never less than IPV6_MMTU
1012 * (see icmp6_input).
1013 */
1014 error = EMSGSIZE;
1015 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1016 goto bad;
1017 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
1018 error = EMSGSIZE;
1019 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1020 goto bad;
1021 } else {
1022 struct mbuf **mnext, *m_frgpart;
1023 struct ip6_frag *ip6f = NULL;
1024 u_int32_t id = htonl(ip6_id++);
1025 u_char nextproto;
1026
1027 /*
1028 * Too large for the destination or interface;
1029 * fragment if possible.
1030 * Must be able to put at least 8 bytes per fragment.
1031 */
1032 hlen = unfragpartlen;
1033 if (mtu > IPV6_MAXPACKET)
1034 mtu = IPV6_MAXPACKET;
1035
1036 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1037 if (len < 8) {
1038 error = EMSGSIZE;
1039 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1040 goto bad;
1041 }
1042
1043 mnext = &m->m_nextpkt;
1044
1045 /*
1046 * Change the next header field of the last header in the
1047 * unfragmentable part.
1048 */
1049 if (exthdrs.ip6e_rthdr) {
1050 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1051 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1052 } else if (exthdrs.ip6e_dest1) {
1053 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1054 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1055 } else if (exthdrs.ip6e_hbh) {
1056 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1057 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1058 } else {
1059 nextproto = ip6->ip6_nxt;
1060 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1061 }
1062
1063 /*
1064 * Loop through length of segment after first fragment,
1065 * make new header and copy data of each part and link onto
1066 * chain.
1067 */
1068 m0 = m;
1069 for (off = hlen; off < tlen; off += len) {
1070 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
1071 if (!m) {
1072 error = ENOBUFS;
1073 ip6stat.ip6s_odropped++;
1074 goto sendorfree;
1075 }
1076 m->m_pkthdr.rcvif = NULL;
1077 m->m_flags = m0->m_flags & M_COPYFLAGS;
1078 *mnext = m;
1079 mnext = &m->m_nextpkt;
1080 m->m_data += max_linkhdr;
1081 mhip6 = mtod(m, struct ip6_hdr *);
1082 *mhip6 = *ip6;
1083 m->m_len = sizeof(*mhip6);
1084 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1085 if (error) {
1086 ip6stat.ip6s_odropped++;
1087 goto sendorfree;
1088 }
1089 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1090 if (off + len >= tlen)
1091 len = tlen - off;
1092 else
1093 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1094 mhip6->ip6_plen = htons((u_short)(len + hlen +
1095 sizeof(*ip6f) -
1096 sizeof(struct ip6_hdr)));
1097 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1098 error = ENOBUFS;
1099 ip6stat.ip6s_odropped++;
1100 goto sendorfree;
1101 }
1102 m_cat(m, m_frgpart);
1103 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1104 m->m_pkthdr.rcvif = 0;
1105 m->m_pkthdr.socket_id = m0->m_pkthdr.socket_id;
1106 #ifdef __darwin8_notyet
1107 #if CONFIG_MACF_NET
1108 mac_create_fragment(m0, m);
1109 #endif
1110 #endif
1111 ip6f->ip6f_reserved = 0;
1112 ip6f->ip6f_ident = id;
1113 ip6f->ip6f_nxt = nextproto;
1114 ip6stat.ip6s_ofragments++;
1115 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1116 }
1117
1118 in6_ifstat_inc(ifp, ifs6_out_fragok);
1119 }
1120
1121 /*
1122 * Remove leading garbages.
1123 */
1124 sendorfree:
1125 m = m0->m_nextpkt;
1126 m0->m_nextpkt = 0;
1127 m_freem(m0);
1128 for (m0 = m; m; m = m0) {
1129 m0 = m->m_nextpkt;
1130 m->m_nextpkt = 0;
1131 if (error == 0) {
1132 /* Record statistics for this interface address. */
1133 if (ia) {
1134 #ifndef __APPLE__
1135 ia->ia_ifa.if_opackets++;
1136 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1137 #endif
1138 }
1139 #if IPSEC
1140 /* clean ipsec history once it goes out of the node */
1141 ipsec_delaux(m);
1142 #endif
1143 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, 1);
1144
1145 } else
1146 m_freem(m);
1147 }
1148
1149 if (error == 0)
1150 ip6stat.ip6s_fragmented++;
1151
1152 done:
1153 if (!locked)
1154 lck_mtx_unlock(ip6_mutex);
1155 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for rtfree */
1156 rtfree(ro->ro_rt);
1157 ro->ro_rt = NULL;
1158 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1159 rtfree(ro_pmtu->ro_rt);
1160 ro_pmtu->ro_rt = NULL;
1161 }
1162
1163 #if IPSEC
1164 if (sp != NULL)
1165 key_freesp(sp, KEY_SADB_UNLOCKED);
1166 #endif /* IPSEC */
1167
1168 return(error);
1169
1170 freehdrs:
1171 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1172 m_freem(exthdrs.ip6e_dest1);
1173 m_freem(exthdrs.ip6e_rthdr);
1174 m_freem(exthdrs.ip6e_dest2);
1175 /* fall through */
1176 bad:
1177 m_freem(m);
1178 goto done;
1179 }
1180
1181 static int
1182 ip6_copyexthdr(mp, hdr, hlen)
1183 struct mbuf **mp;
1184 caddr_t hdr;
1185 int hlen;
1186 {
1187 struct mbuf *m;
1188
1189 if (hlen > MCLBYTES)
1190 return(ENOBUFS); /* XXX */
1191
1192 MGET(m, M_DONTWAIT, MT_DATA);
1193 if (!m)
1194 return(ENOBUFS);
1195
1196 if (hlen > MLEN) {
1197 MCLGET(m, M_DONTWAIT);
1198 if ((m->m_flags & M_EXT) == 0) {
1199 m_free(m);
1200 return(ENOBUFS);
1201 }
1202 }
1203 m->m_len = hlen;
1204 if (hdr)
1205 bcopy(hdr, mtod(m, caddr_t), hlen);
1206
1207 *mp = m;
1208 return(0);
1209 }
1210
1211 /*
1212 * Insert jumbo payload option.
1213 */
1214 static int
1215 ip6_insert_jumboopt(exthdrs, plen)
1216 struct ip6_exthdrs *exthdrs;
1217 u_int32_t plen;
1218 {
1219 struct mbuf *mopt;
1220 u_char *optbuf;
1221 u_int32_t v;
1222
1223 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1224
1225 /*
1226 * If there is no hop-by-hop options header, allocate new one.
1227 * If there is one but it doesn't have enough space to store the
1228 * jumbo payload option, allocate a cluster to store the whole options.
1229 * Otherwise, use it to store the options.
1230 */
1231 if (exthdrs->ip6e_hbh == 0) {
1232 MGET(mopt, M_DONTWAIT, MT_DATA);
1233 if (mopt == 0)
1234 return(ENOBUFS);
1235 mopt->m_len = JUMBOOPTLEN;
1236 optbuf = mtod(mopt, u_char *);
1237 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1238 exthdrs->ip6e_hbh = mopt;
1239 } else {
1240 struct ip6_hbh *hbh;
1241
1242 mopt = exthdrs->ip6e_hbh;
1243 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1244 /*
1245 * XXX assumption:
1246 * - exthdrs->ip6e_hbh is not referenced from places
1247 * other than exthdrs.
1248 * - exthdrs->ip6e_hbh is not an mbuf chain.
1249 */
1250 int oldoptlen = mopt->m_len;
1251 struct mbuf *n;
1252
1253 /*
1254 * XXX: give up if the whole (new) hbh header does
1255 * not fit even in an mbuf cluster.
1256 */
1257 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1258 return(ENOBUFS);
1259
1260 /*
1261 * As a consequence, we must always prepare a cluster
1262 * at this point.
1263 */
1264 MGET(n, M_DONTWAIT, MT_DATA);
1265 if (n) {
1266 MCLGET(n, M_DONTWAIT);
1267 if ((n->m_flags & M_EXT) == 0) {
1268 m_freem(n);
1269 n = NULL;
1270 }
1271 }
1272 if (!n)
1273 return(ENOBUFS);
1274 n->m_len = oldoptlen + JUMBOOPTLEN;
1275 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1276 oldoptlen);
1277 optbuf = (u_char *) (mtod(n, caddr_t) + oldoptlen);
1278 m_freem(mopt);
1279 mopt = exthdrs->ip6e_hbh = n;
1280 } else {
1281 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1282 mopt->m_len += JUMBOOPTLEN;
1283 }
1284 optbuf[0] = IP6OPT_PADN;
1285 optbuf[1] = 1;
1286
1287 /*
1288 * Adjust the header length according to the pad and
1289 * the jumbo payload option.
1290 */
1291 hbh = mtod(mopt, struct ip6_hbh *);
1292 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1293 }
1294
1295 /* fill in the option. */
1296 optbuf[2] = IP6OPT_JUMBO;
1297 optbuf[3] = 4;
1298 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1299 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1300
1301 /* finally, adjust the packet header length */
1302 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1303
1304 return(0);
1305 #undef JUMBOOPTLEN
1306 }
1307
1308 /*
1309 * Insert fragment header and copy unfragmentable header portions.
1310 */
1311 static int
1312 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1313 struct mbuf *m0, *m;
1314 int hlen;
1315 struct ip6_frag **frghdrp;
1316 {
1317 struct mbuf *n, *mlast;
1318
1319 if (hlen > sizeof(struct ip6_hdr)) {
1320 n = m_copym(m0, sizeof(struct ip6_hdr),
1321 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1322 if (n == 0)
1323 return(ENOBUFS);
1324 m->m_next = n;
1325 } else
1326 n = m;
1327
1328 /* Search for the last mbuf of unfragmentable part. */
1329 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1330 ;
1331
1332 if ((mlast->m_flags & M_EXT) == 0 &&
1333 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1334 /* use the trailing space of the last mbuf for the fragment hdr */
1335 *frghdrp =
1336 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1337 mlast->m_len += sizeof(struct ip6_frag);
1338 m->m_pkthdr.len += sizeof(struct ip6_frag);
1339 } else {
1340 /* allocate a new mbuf for the fragment header */
1341 struct mbuf *mfrg;
1342
1343 MGET(mfrg, M_DONTWAIT, MT_DATA);
1344 if (mfrg == 0)
1345 return(ENOBUFS);
1346 mfrg->m_len = sizeof(struct ip6_frag);
1347 *frghdrp = mtod(mfrg, struct ip6_frag *);
1348 mlast->m_next = mfrg;
1349 }
1350
1351 return(0);
1352 }
1353
1354 extern int load_ipfw(void);
1355
1356 /*
1357 * IP6 socket option processing.
1358 */
1359 int
1360 ip6_ctloutput(so, sopt)
1361 struct socket *so;
1362 struct sockopt *sopt;
1363 {
1364 int privileged;
1365 struct inpcb *in6p = sotoinpcb(so);
1366 int error, optval;
1367 int level, op = -1, optname = 0;
1368 int optlen = 0;
1369 struct proc *p = NULL;
1370
1371 level = error = optval = 0;
1372 if (sopt == NULL)
1373 panic("ip6_ctloutput: arg soopt is NULL");
1374 else {
1375 level = sopt->sopt_level;
1376 op = sopt->sopt_dir;
1377 optname = sopt->sopt_name;
1378 optlen = sopt->sopt_valsize;
1379 p = sopt->sopt_p;
1380 }
1381
1382 privileged = (p == 0 || proc_suser(p)) ? 0 : 1;
1383
1384 if (level == IPPROTO_IPV6) {
1385 switch (op) {
1386
1387 case SOPT_SET:
1388 switch (optname) {
1389 case IPV6_PKTOPTIONS:
1390 {
1391 struct mbuf *m;
1392
1393 if (sopt->sopt_valsize > MCLBYTES) {
1394 error = EMSGSIZE;
1395 break;
1396 }
1397 error = soopt_getm(sopt, &m); /* XXX */
1398 if (error != 0)
1399 break;
1400 error = soopt_mcopyin(sopt, m); /* XXX */
1401 if (error != 0)
1402 break;
1403 error = ip6_pcbopts(&in6p->in6p_outputopts,
1404 m, so, sopt);
1405 m_freem(m); /* XXX */
1406 break;
1407 }
1408
1409 /*
1410 * Use of some Hop-by-Hop options or some
1411 * Destination options, might require special
1412 * privilege. That is, normal applications
1413 * (without special privilege) might be forbidden
1414 * from setting certain options in outgoing packets,
1415 * and might never see certain options in received
1416 * packets. [RFC 2292 Section 6]
1417 * KAME specific note:
1418 * KAME prevents non-privileged users from sending or
1419 * receiving ANY hbh/dst options in order to avoid
1420 * overhead of parsing options in the kernel.
1421 */
1422 case IPV6_UNICAST_HOPS:
1423 case IPV6_CHECKSUM:
1424 case IPV6_FAITH:
1425
1426 case IPV6_V6ONLY:
1427 if (optlen != sizeof(int)) {
1428 error = EINVAL;
1429 break;
1430 }
1431 error = sooptcopyin(sopt, &optval,
1432 sizeof optval, sizeof optval);
1433 if (error)
1434 break;
1435 switch (optname) {
1436
1437 case IPV6_UNICAST_HOPS:
1438 if (optval < -1 || optval >= 256)
1439 error = EINVAL;
1440 else {
1441 /* -1 = kernel default */
1442 in6p->in6p_hops = optval;
1443
1444 if ((in6p->in6p_vflag &
1445 INP_IPV4) != 0)
1446 in6p->inp_ip_ttl = optval;
1447 }
1448 break;
1449 #define OPTSET(bit) \
1450 do { \
1451 if (optval) \
1452 in6p->in6p_flags |= (bit); \
1453 else \
1454 in6p->in6p_flags &= ~(bit); \
1455 } while (0)
1456 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1457
1458 case IPV6_CHECKSUM:
1459 in6p->in6p_cksum = optval;
1460 break;
1461
1462 case IPV6_FAITH:
1463 OPTSET(IN6P_FAITH);
1464 break;
1465
1466 case IPV6_V6ONLY:
1467 /*
1468 * make setsockopt(IPV6_V6ONLY)
1469 * available only prior to bind(2).
1470 * see ipng mailing list, Jun 22 2001.
1471 */
1472 if (in6p->in6p_lport ||
1473 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1474 {
1475 error = EINVAL;
1476 break;
1477 }
1478 OPTSET(IN6P_IPV6_V6ONLY);
1479 if (optval)
1480 in6p->in6p_vflag &= ~INP_IPV4;
1481 else
1482 in6p->in6p_vflag |= INP_IPV4;
1483 break;
1484 }
1485 break;
1486
1487 case IPV6_PKTINFO:
1488 case IPV6_HOPLIMIT:
1489 case IPV6_HOPOPTS:
1490 case IPV6_DSTOPTS:
1491 case IPV6_RTHDR:
1492 /* RFC 2292 */
1493 if (optlen != sizeof(int)) {
1494 error = EINVAL;
1495 break;
1496 }
1497 error = sooptcopyin(sopt, &optval,
1498 sizeof optval, sizeof optval);
1499 if (error)
1500 break;
1501 switch (optname) {
1502 case IPV6_PKTINFO:
1503 OPTSET(IN6P_PKTINFO);
1504 break;
1505 case IPV6_HOPLIMIT:
1506 OPTSET(IN6P_HOPLIMIT);
1507 break;
1508 case IPV6_HOPOPTS:
1509 /*
1510 * Check super-user privilege.
1511 * See comments for IPV6_RECVHOPOPTS.
1512 */
1513 if (!privileged)
1514 return(EPERM);
1515 OPTSET(IN6P_HOPOPTS);
1516 break;
1517 case IPV6_DSTOPTS:
1518 if (!privileged)
1519 return(EPERM);
1520 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1521 break;
1522 case IPV6_RTHDR:
1523 OPTSET(IN6P_RTHDR);
1524 break;
1525 }
1526 break;
1527 #undef OPTSET
1528
1529 case IPV6_MULTICAST_IF:
1530 case IPV6_MULTICAST_HOPS:
1531 case IPV6_MULTICAST_LOOP:
1532 case IPV6_JOIN_GROUP:
1533 case IPV6_LEAVE_GROUP:
1534 {
1535 struct mbuf *m;
1536 if (sopt->sopt_valsize > MLEN) {
1537 error = EMSGSIZE;
1538 break;
1539 }
1540 /* XXX */
1541 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
1542 if (m == 0) {
1543 error = ENOBUFS;
1544 break;
1545 }
1546 m->m_len = sopt->sopt_valsize;
1547 error = sooptcopyin(sopt, mtod(m, char *),
1548 m->m_len, m->m_len);
1549 error = ip6_setmoptions(sopt->sopt_name, in6p, m);
1550 (void)m_free(m);
1551 }
1552 break;
1553
1554 case IPV6_PORTRANGE:
1555 error = sooptcopyin(sopt, &optval,
1556 sizeof optval, sizeof optval);
1557 if (error)
1558 break;
1559
1560 switch (optval) {
1561 case IPV6_PORTRANGE_DEFAULT:
1562 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1563 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1564 break;
1565
1566 case IPV6_PORTRANGE_HIGH:
1567 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1568 in6p->in6p_flags |= IN6P_HIGHPORT;
1569 break;
1570
1571 case IPV6_PORTRANGE_LOW:
1572 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1573 in6p->in6p_flags |= IN6P_LOWPORT;
1574 break;
1575
1576 default:
1577 error = EINVAL;
1578 break;
1579 }
1580 break;
1581
1582 #if IPSEC
1583 case IPV6_IPSEC_POLICY:
1584 {
1585 caddr_t req = NULL;
1586 size_t len = 0;
1587 struct mbuf *m;
1588
1589 if (sopt->sopt_valsize > MCLBYTES) {
1590 error = EMSGSIZE;
1591 break;
1592 }
1593 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1594 break;
1595 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1596 break;
1597 if (m) {
1598 req = mtod(m, caddr_t);
1599 len = m->m_len;
1600 }
1601 error = ipsec6_set_policy(in6p, optname, req,
1602 len, privileged);
1603 m_freem(m);
1604 }
1605 break;
1606 #endif /* KAME IPSEC */
1607
1608 #if IPFIREWALL
1609 case IPV6_FW_ADD:
1610 case IPV6_FW_DEL:
1611 case IPV6_FW_FLUSH:
1612 case IPV6_FW_ZERO:
1613 {
1614 if (ip6_fw_ctl_ptr == NULL && load_ipfw() != 0)
1615 return EINVAL;
1616
1617 error = (*ip6_fw_ctl_ptr)(sopt);
1618 }
1619 break;
1620 #endif /* IPFIREWALL */
1621
1622 default:
1623 error = ENOPROTOOPT;
1624 break;
1625 }
1626 break;
1627
1628 case SOPT_GET:
1629 switch (optname) {
1630
1631 case IPV6_PKTOPTIONS:
1632 if (in6p->in6p_options) {
1633 struct mbuf *m;
1634 m = m_copym(in6p->in6p_options,
1635 0, M_COPYALL, M_WAIT);
1636 error = soopt_mcopyout(sopt, m);
1637 if (error == 0)
1638 m_freem(m);
1639 } else
1640 sopt->sopt_valsize = 0;
1641 break;
1642
1643 case IPV6_UNICAST_HOPS:
1644 case IPV6_CHECKSUM:
1645
1646 case IPV6_FAITH:
1647 case IPV6_V6ONLY:
1648 case IPV6_PORTRANGE:
1649 switch (optname) {
1650
1651 case IPV6_UNICAST_HOPS:
1652 optval = in6p->in6p_hops;
1653 break;
1654
1655 case IPV6_CHECKSUM:
1656 optval = in6p->in6p_cksum;
1657 break;
1658
1659 case IPV6_FAITH:
1660 optval = OPTBIT(IN6P_FAITH);
1661 break;
1662
1663 case IPV6_V6ONLY:
1664 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1665 break;
1666
1667 case IPV6_PORTRANGE:
1668 {
1669 int flags;
1670 flags = in6p->in6p_flags;
1671 if (flags & IN6P_HIGHPORT)
1672 optval = IPV6_PORTRANGE_HIGH;
1673 else if (flags & IN6P_LOWPORT)
1674 optval = IPV6_PORTRANGE_LOW;
1675 else
1676 optval = 0;
1677 break;
1678 }
1679 }
1680 error = sooptcopyout(sopt, &optval,
1681 sizeof optval);
1682 break;
1683
1684 case IPV6_PKTINFO:
1685 case IPV6_HOPLIMIT:
1686 case IPV6_HOPOPTS:
1687 case IPV6_RTHDR:
1688 case IPV6_DSTOPTS:
1689 if ((optname == IPV6_HOPOPTS ||
1690 optname == IPV6_DSTOPTS) &&
1691 !privileged)
1692 return(EPERM);
1693 switch (optname) {
1694 case IPV6_PKTINFO:
1695 optval = OPTBIT(IN6P_PKTINFO);
1696 break;
1697 case IPV6_HOPLIMIT:
1698 optval = OPTBIT(IN6P_HOPLIMIT);
1699 break;
1700 case IPV6_HOPOPTS:
1701 if (!privileged)
1702 return(EPERM);
1703 optval = OPTBIT(IN6P_HOPOPTS);
1704 break;
1705 case IPV6_RTHDR:
1706 optval = OPTBIT(IN6P_RTHDR);
1707 break;
1708 case IPV6_DSTOPTS:
1709 if (!privileged)
1710 return(EPERM);
1711 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1712 break;
1713 }
1714 error = sooptcopyout(sopt, &optval,
1715 sizeof optval);
1716 break;
1717
1718 case IPV6_MULTICAST_IF:
1719 case IPV6_MULTICAST_HOPS:
1720 case IPV6_MULTICAST_LOOP:
1721 case IPV6_JOIN_GROUP:
1722 case IPV6_LEAVE_GROUP:
1723 {
1724 struct mbuf *m;
1725 error = ip6_getmoptions(sopt->sopt_name,
1726 in6p->in6p_moptions, &m);
1727 if (error == 0)
1728 error = sooptcopyout(sopt,
1729 mtod(m, char *), m->m_len);
1730 m_freem(m);
1731 }
1732 break;
1733
1734 #if IPSEC
1735 case IPV6_IPSEC_POLICY:
1736 {
1737 caddr_t req = NULL;
1738 size_t len = 0;
1739 struct mbuf *m = NULL;
1740 struct mbuf **mp = &m;
1741
1742 if (sopt->sopt_valsize > MCLBYTES) {
1743 error = EMSGSIZE;
1744 break;
1745 }
1746 error = soopt_getm(sopt, &m); /* XXX */
1747 if (error != 0)
1748 break;
1749 error = soopt_mcopyin(sopt, m); /* XXX */
1750 if (error != 0)
1751 break;
1752 if (m) {
1753 req = mtod(m, caddr_t);
1754 len = m->m_len;
1755 }
1756 error = ipsec6_get_policy(in6p, req, len, mp);
1757 if (error == 0)
1758 error = soopt_mcopyout(sopt, m); /*XXX*/
1759 if (error == 0 && m)
1760 m_freem(m);
1761 break;
1762 }
1763 #endif /* KAME IPSEC */
1764
1765 #if IPFIREWALL
1766 case IPV6_FW_GET:
1767 {
1768 if (ip6_fw_ctl_ptr == NULL && load_ipfw() != 0)
1769 return EINVAL;
1770
1771 error = (*ip6_fw_ctl_ptr)(sopt);
1772 }
1773 break;
1774 #endif /* IPFIREWALL */
1775
1776 default:
1777 error = ENOPROTOOPT;
1778 break;
1779 }
1780 break;
1781 }
1782 } else {
1783 error = EINVAL;
1784 }
1785 return(error);
1786 }
1787
1788 /*
1789 * Set up IP6 options in pcb for insertion in output packets or
1790 * specifying behavior of outgoing packets.
1791 */
1792 static int
1793 ip6_pcbopts(
1794 struct ip6_pktopts **pktopt,
1795 struct mbuf *m,
1796 __unused struct socket *so,
1797 struct sockopt *sopt)
1798 {
1799 struct ip6_pktopts *opt = *pktopt;
1800 int error = 0;
1801 struct proc *p = sopt->sopt_p;
1802 int priv = 0;
1803
1804 /* turn off any old options. */
1805 if (opt) {
1806 #if DIAGNOSTIC
1807 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1808 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1809 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1810 printf("ip6_pcbopts: all specified options are cleared.\n");
1811 #endif
1812 ip6_clearpktopts(opt, 1, -1);
1813 } else
1814 opt = _MALLOC(sizeof(*opt), M_IP6OPT, M_WAITOK);
1815 *pktopt = NULL;
1816
1817 if (!m || m->m_len == 0) {
1818 /*
1819 * Only turning off any previous options, regardless of
1820 * whether the opt is just created or given.
1821 */
1822 if (opt)
1823 FREE(opt, M_IP6OPT);
1824 return(0);
1825 }
1826
1827 /* set options specified by user. */
1828 if (p && !proc_suser(p))
1829 priv = 1;
1830 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1831 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1832 FREE(opt, M_IP6OPT);
1833 return(error);
1834 }
1835 *pktopt = opt;
1836 return(0);
1837 }
1838
1839 /*
1840 * initialize ip6_pktopts. beware that there are non-zero default values in
1841 * the struct.
1842 */
1843 void
1844 init_ip6pktopts(opt)
1845 struct ip6_pktopts *opt;
1846 {
1847
1848 bzero(opt, sizeof(*opt));
1849 opt->ip6po_hlim = -1; /* -1 means default hop limit */
1850 }
1851
1852 void
1853 ip6_clearpktopts(pktopt, needfree, optname)
1854 struct ip6_pktopts *pktopt;
1855 int needfree, optname;
1856 {
1857 if (pktopt == NULL)
1858 return;
1859
1860 if (optname == -1) {
1861 if (needfree && pktopt->ip6po_pktinfo)
1862 FREE(pktopt->ip6po_pktinfo, M_IP6OPT);
1863 pktopt->ip6po_pktinfo = NULL;
1864 }
1865 if (optname == -1)
1866 pktopt->ip6po_hlim = -1;
1867 if (optname == -1) {
1868 if (needfree && pktopt->ip6po_nexthop)
1869 FREE(pktopt->ip6po_nexthop, M_IP6OPT);
1870 pktopt->ip6po_nexthop = NULL;
1871 }
1872 if (optname == -1) {
1873 if (needfree && pktopt->ip6po_hbh)
1874 FREE(pktopt->ip6po_hbh, M_IP6OPT);
1875 pktopt->ip6po_hbh = NULL;
1876 }
1877 if (optname == -1) {
1878 if (needfree && pktopt->ip6po_dest1)
1879 FREE(pktopt->ip6po_dest1, M_IP6OPT);
1880 pktopt->ip6po_dest1 = NULL;
1881 }
1882 if (optname == -1) {
1883 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1884 FREE(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1885 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1886 if (pktopt->ip6po_route.ro_rt) {
1887 rtfree(pktopt->ip6po_route.ro_rt);
1888 pktopt->ip6po_route.ro_rt = NULL;
1889 }
1890 }
1891 if (optname == -1) {
1892 if (needfree && pktopt->ip6po_dest2)
1893 FREE(pktopt->ip6po_dest2, M_IP6OPT);
1894 pktopt->ip6po_dest2 = NULL;
1895 }
1896 }
1897
1898 #define PKTOPT_EXTHDRCPY(type) \
1899 do {\
1900 if (src->type) {\
1901 int hlen =\
1902 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1903 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
1904 if (dst->type == NULL && canwait == M_NOWAIT)\
1905 goto bad;\
1906 bcopy(src->type, dst->type, hlen);\
1907 }\
1908 } while (0)
1909
1910 struct ip6_pktopts *
1911 ip6_copypktopts(src, canwait)
1912 struct ip6_pktopts *src;
1913 int canwait;
1914 {
1915 struct ip6_pktopts *dst;
1916
1917 if (src == NULL) {
1918 printf("ip6_clearpktopts: invalid argument\n");
1919 return(NULL);
1920 }
1921
1922 dst = _MALLOC(sizeof(*dst), M_IP6OPT, canwait);
1923 if (dst == NULL && canwait == M_NOWAIT)
1924 return (NULL);
1925 bzero(dst, sizeof(*dst));
1926
1927 dst->ip6po_hlim = src->ip6po_hlim;
1928 if (src->ip6po_pktinfo) {
1929 dst->ip6po_pktinfo = _MALLOC(sizeof(*dst->ip6po_pktinfo),
1930 M_IP6OPT, canwait);
1931 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
1932 goto bad;
1933 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1934 }
1935 if (src->ip6po_nexthop) {
1936 dst->ip6po_nexthop = _MALLOC(src->ip6po_nexthop->sa_len,
1937 M_IP6OPT, canwait);
1938 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
1939 goto bad;
1940 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1941 src->ip6po_nexthop->sa_len);
1942 }
1943 PKTOPT_EXTHDRCPY(ip6po_hbh);
1944 PKTOPT_EXTHDRCPY(ip6po_dest1);
1945 PKTOPT_EXTHDRCPY(ip6po_dest2);
1946 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1947 return(dst);
1948
1949 bad:
1950 if (dst->ip6po_pktinfo) FREE(dst->ip6po_pktinfo, M_IP6OPT);
1951 if (dst->ip6po_nexthop) FREE(dst->ip6po_nexthop, M_IP6OPT);
1952 if (dst->ip6po_hbh) FREE(dst->ip6po_hbh, M_IP6OPT);
1953 if (dst->ip6po_dest1) FREE(dst->ip6po_dest1, M_IP6OPT);
1954 if (dst->ip6po_dest2) FREE(dst->ip6po_dest2, M_IP6OPT);
1955 if (dst->ip6po_rthdr) FREE(dst->ip6po_rthdr, M_IP6OPT);
1956 FREE(dst, M_IP6OPT);
1957 return(NULL);
1958 }
1959 #undef PKTOPT_EXTHDRCPY
1960
1961 void
1962 ip6_freepcbopts(pktopt)
1963 struct ip6_pktopts *pktopt;
1964 {
1965 if (pktopt == NULL)
1966 return;
1967
1968 ip6_clearpktopts(pktopt, 1, -1);
1969
1970 FREE(pktopt, M_IP6OPT);
1971 }
1972
1973 /*
1974 * Set the IP6 multicast options in response to user setsockopt().
1975 */
1976 static int
1977 ip6_setmoptions(
1978 int optname,
1979 struct inpcb* in6p,
1980 struct mbuf *m)
1981 {
1982 int error = 0;
1983 u_int loop, ifindex;
1984 struct ipv6_mreq *mreq;
1985 struct ifnet *ifp;
1986 struct ip6_moptions **im6op = &in6p->in6p_moptions;
1987 struct ip6_moptions *im6o = *im6op;
1988 struct ip_moptions *imo;
1989 struct route_in6 ro;
1990 struct sockaddr_in6 *dst;
1991 struct in6_multi_mship *imm;
1992
1993 if (im6o == NULL) {
1994 /*
1995 * No multicast option buffer attached to the pcb;
1996 * allocate one and initialize to default values.
1997 */
1998 im6o = (struct ip6_moptions *)
1999 _MALLOC(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2000
2001 if (im6o == NULL)
2002 return(ENOBUFS);
2003 *im6op = im6o;
2004 im6o->im6o_multicast_ifp = NULL;
2005 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2006 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2007 LIST_INIT(&im6o->im6o_memberships);
2008 }
2009
2010 if (in6p->inp_moptions == NULL) {
2011 /*
2012 * No IPv4 multicast option buffer attached to the pcb;
2013 * call ip_createmoptions to allocate one and initialize
2014 * to default values.
2015 */
2016 error = ip_createmoptions(&in6p->inp_moptions);
2017 if (error != 0)
2018 return error;
2019 }
2020 imo = in6p->inp_moptions;
2021
2022 switch (optname) {
2023
2024 case IPV6_MULTICAST_IF:
2025 /*
2026 * Select the interface for outgoing multicast packets.
2027 */
2028 if (m == NULL || m->m_len != sizeof(u_int)) {
2029 error = EINVAL;
2030 break;
2031 }
2032 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2033 if (ifindex < 0 || if_index < ifindex) {
2034 error = ENXIO; /* XXX EINVAL? */
2035 break;
2036 }
2037 ifp = ifindex2ifnet[ifindex];
2038 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2039 error = EADDRNOTAVAIL;
2040 break;
2041 }
2042 im6o->im6o_multicast_ifp = ifp;
2043 imo->imo_multicast_ifp = ifp;
2044 break;
2045
2046 case IPV6_MULTICAST_HOPS:
2047 {
2048 /*
2049 * Set the IP6 hoplimit for outgoing multicast packets.
2050 */
2051 int optval;
2052 if (m == NULL || m->m_len != sizeof(int)) {
2053 error = EINVAL;
2054 break;
2055 }
2056 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2057 if (optval < -1 || optval >= 256)
2058 error = EINVAL;
2059 else if (optval == -1) {
2060 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2061 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2062 } else {
2063 im6o->im6o_multicast_hlim = optval;
2064 imo->imo_multicast_ttl = optval;
2065 }
2066 break;
2067 }
2068
2069 case IPV6_MULTICAST_LOOP:
2070 /*
2071 * Set the loopback flag for outgoing multicast packets.
2072 * Must be zero or one.
2073 */
2074 if (m == NULL || m->m_len != sizeof(u_int)) {
2075 error = EINVAL;
2076 break;
2077 }
2078 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2079 if (loop > 1) {
2080 error = EINVAL;
2081 break;
2082 }
2083 im6o->im6o_multicast_loop = loop;
2084 imo->imo_multicast_loop = loop;
2085 break;
2086
2087 case IPV6_JOIN_GROUP:
2088 /*
2089 * Add a multicast group membership.
2090 * Group must be a valid IP6 multicast address.
2091 */
2092 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2093 error = EINVAL;
2094 break;
2095 }
2096 mreq = mtod(m, struct ipv6_mreq *);
2097 /*
2098 * If the interface is specified, validate it.
2099 */
2100 if (mreq->ipv6mr_interface < 0
2101 || if_index < mreq->ipv6mr_interface) {
2102 error = ENXIO; /* XXX EINVAL? */
2103 break;
2104 }
2105
2106 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2107 /*
2108 * We use the unspecified address to specify to accept
2109 * all multicast addresses. Only super user is allowed
2110 * to do this.
2111 */
2112 if (suser(kauth_cred_get(), 0))
2113 {
2114 error = EACCES;
2115 break;
2116 }
2117 } else if (IN6_IS_ADDR_V4MAPPED(&mreq->ipv6mr_multiaddr)) {
2118 struct ip_mreq v4req;
2119
2120 v4req.imr_multiaddr.s_addr = mreq->ipv6mr_multiaddr.s6_addr32[3];
2121 v4req.imr_interface.s_addr = INADDR_ANY;
2122
2123 /* Find an IPv4 address on the specified interface. */
2124 if (mreq->ipv6mr_interface != 0) {
2125 struct in_ifaddr *ifa;
2126
2127 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2128
2129 lck_mtx_lock(rt_mtx);
2130 TAILQ_FOREACH(ifa, &in_ifaddrhead, ia_link) {
2131 if (ifa->ia_ifp == ifp) {
2132 v4req.imr_interface = IA_SIN(ifa)->sin_addr;
2133 break;
2134 }
2135 }
2136 lck_mtx_unlock(rt_mtx);
2137
2138 if (v4req.imr_multiaddr.s_addr == 0) {
2139 /* Interface has no IPv4 address. */
2140 error = EINVAL;
2141 break;
2142 }
2143 }
2144
2145 error = ip_addmembership(imo, &v4req);
2146 break;
2147 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2148 error = EINVAL;
2149 break;
2150 }
2151 /*
2152 * If no interface was explicitly specified, choose an
2153 * appropriate one according to the given multicast address.
2154 */
2155 if (mreq->ipv6mr_interface == 0) {
2156 /*
2157 * If the multicast address is in node-local scope,
2158 * the interface should be a loopback interface.
2159 * Otherwise, look up the routing table for the
2160 * address, and choose the outgoing interface.
2161 * XXX: is it a good approach?
2162 */
2163 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2164 ifp = lo_ifp;
2165 } else {
2166 ro.ro_rt = NULL;
2167 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2168 bzero(dst, sizeof(*dst));
2169 dst->sin6_len = sizeof(struct sockaddr_in6);
2170 dst->sin6_family = AF_INET6;
2171 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2172 rtalloc((struct route *)&ro);
2173 if (ro.ro_rt == NULL) {
2174 error = EADDRNOTAVAIL;
2175 break;
2176 }
2177 ifp = ro.ro_rt->rt_ifp;
2178 rtfree(ro.ro_rt);
2179 ro.ro_rt = NULL;
2180 }
2181 } else
2182 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2183
2184 /*
2185 * See if we found an interface, and confirm that it
2186 * supports multicast
2187 */
2188 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2189 error = EADDRNOTAVAIL;
2190 break;
2191 }
2192 /*
2193 * Put interface index into the multicast address,
2194 * if the address has link-local scope.
2195 */
2196 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2197 mreq->ipv6mr_multiaddr.s6_addr16[1]
2198 = htons(mreq->ipv6mr_interface);
2199 }
2200 /*
2201 * See if the membership already exists.
2202 */
2203 lck_mtx_lock(nd6_mutex);
2204 for (imm = im6o->im6o_memberships.lh_first;
2205 imm != NULL; imm = imm->i6mm_chain.le_next)
2206 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2207 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2208 &mreq->ipv6mr_multiaddr))
2209 break;
2210 if (imm != NULL) {
2211 error = EADDRINUSE;
2212 lck_mtx_unlock(nd6_mutex);
2213 break;
2214 }
2215 /*
2216 * Everything looks good; add a new record to the multicast
2217 * address list for the given interface.
2218 */
2219 imm = _MALLOC(sizeof(*imm), M_IPMADDR, M_WAITOK);
2220 if (imm == NULL) {
2221 error = ENOBUFS;
2222 lck_mtx_unlock(nd6_mutex);
2223 break;
2224 }
2225 if ((imm->i6mm_maddr =
2226 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error, 1)) == NULL) {
2227 FREE(imm, M_IPMADDR);
2228 lck_mtx_unlock(nd6_mutex);
2229 break;
2230 }
2231 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2232 lck_mtx_unlock(nd6_mutex);
2233 break;
2234
2235 case IPV6_LEAVE_GROUP:
2236 /*
2237 * Drop a multicast group membership.
2238 * Group must be a valid IP6 multicast address.
2239 */
2240 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2241 error = EINVAL;
2242 break;
2243 }
2244 mreq = mtod(m, struct ipv6_mreq *);
2245 /*
2246 * If an interface address was specified, get a pointer
2247 * to its ifnet structure.
2248 */
2249 if (mreq->ipv6mr_interface < 0
2250 || if_index < mreq->ipv6mr_interface) {
2251 error = ENXIO; /* XXX EINVAL? */
2252 break;
2253 }
2254 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2255
2256 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2257 if (suser(kauth_cred_get(), 0)) {
2258 error = EACCES;
2259 break;
2260 }
2261 } else if (IN6_IS_ADDR_V4MAPPED(&mreq->ipv6mr_multiaddr)) {
2262 struct ip_mreq v4req;
2263
2264 v4req.imr_multiaddr.s_addr = mreq->ipv6mr_multiaddr.s6_addr32[3];
2265 v4req.imr_interface.s_addr = INADDR_ANY;
2266
2267 if (ifp != NULL) {
2268 struct in_ifaddr *ifa;
2269
2270 lck_mtx_lock(rt_mtx);
2271 TAILQ_FOREACH(ifa, &in_ifaddrhead, ia_link) {
2272 if (ifa->ia_ifp == ifp) {
2273 v4req.imr_interface = IA_SIN(ifa)->sin_addr;
2274 break;
2275 }
2276 }
2277 lck_mtx_unlock(rt_mtx);
2278 }
2279
2280 error = ip_dropmembership(imo, &v4req);
2281 break;
2282 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2283 error = EINVAL;
2284 break;
2285 }
2286 /*
2287 * Put interface index into the multicast address,
2288 * if the address has link-local scope.
2289 */
2290 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2291 mreq->ipv6mr_multiaddr.s6_addr16[1]
2292 = htons(mreq->ipv6mr_interface);
2293 }
2294 /*
2295 * Find the membership in the membership list.
2296 */
2297 lck_mtx_lock(nd6_mutex);
2298 for (imm = im6o->im6o_memberships.lh_first;
2299 imm != NULL; imm = imm->i6mm_chain.le_next) {
2300 if ((ifp == NULL ||
2301 imm->i6mm_maddr->in6m_ifp == ifp) &&
2302 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2303 &mreq->ipv6mr_multiaddr))
2304 break;
2305 }
2306 if (imm == NULL) {
2307 /* Unable to resolve interface */
2308 error = EADDRNOTAVAIL;
2309 lck_mtx_unlock(nd6_mutex);
2310 break;
2311 }
2312 /*
2313 * Give up the multicast address record to which the
2314 * membership points.
2315 */
2316 LIST_REMOVE(imm, i6mm_chain);
2317 in6_delmulti(imm->i6mm_maddr, 1);
2318 lck_mtx_unlock(nd6_mutex);
2319 FREE(imm, M_IPMADDR);
2320 break;
2321
2322 default:
2323 error = EOPNOTSUPP;
2324 break;
2325 }
2326
2327 /*
2328 * If all options have default values, no need to keep the mbuf.
2329 */
2330 lck_mtx_lock(nd6_mutex);
2331 if (im6o->im6o_multicast_ifp == NULL &&
2332 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2333 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2334 im6o->im6o_memberships.lh_first == NULL) {
2335 FREE(*im6op, M_IPMOPTS);
2336 *im6op = NULL;
2337 }
2338 if (imo->imo_multicast_ifp == NULL &&
2339 imo->imo_multicast_vif == -1 &&
2340 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2341 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2342 imo->imo_num_memberships == 0) {
2343 ip_freemoptions(imo);
2344 in6p->inp_moptions = 0;
2345 }
2346 lck_mtx_unlock(nd6_mutex);
2347
2348 return(error);
2349 }
2350
2351 /*
2352 * Return the IP6 multicast options in response to user getsockopt().
2353 */
2354 static int
2355 ip6_getmoptions(optname, im6o, mp)
2356 int optname;
2357 struct ip6_moptions *im6o;
2358 struct mbuf **mp;
2359 {
2360 u_int *hlim, *loop, *ifindex;
2361
2362 *mp = m_get(M_WAIT, MT_HEADER); /*XXX*/
2363
2364 switch (optname) {
2365
2366 case IPV6_MULTICAST_IF:
2367 ifindex = mtod(*mp, u_int *);
2368 (*mp)->m_len = sizeof(u_int);
2369 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2370 *ifindex = 0;
2371 else
2372 *ifindex = im6o->im6o_multicast_ifp->if_index;
2373 return(0);
2374
2375 case IPV6_MULTICAST_HOPS:
2376 hlim = mtod(*mp, u_int *);
2377 (*mp)->m_len = sizeof(u_int);
2378 if (im6o == NULL)
2379 *hlim = ip6_defmcasthlim;
2380 else
2381 *hlim = im6o->im6o_multicast_hlim;
2382 return(0);
2383
2384 case IPV6_MULTICAST_LOOP:
2385 loop = mtod(*mp, u_int *);
2386 (*mp)->m_len = sizeof(u_int);
2387 if (im6o == NULL)
2388 *loop = ip6_defmcasthlim;
2389 else
2390 *loop = im6o->im6o_multicast_loop;
2391 return(0);
2392
2393 default:
2394 return(EOPNOTSUPP);
2395 }
2396 }
2397
2398 /*
2399 * Discard the IP6 multicast options.
2400 */
2401 void
2402 ip6_freemoptions(im6o)
2403 struct ip6_moptions *im6o;
2404 {
2405 struct in6_multi_mship *imm;
2406
2407 if (im6o == NULL)
2408 return;
2409
2410 lck_mtx_lock(nd6_mutex);
2411 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2412 LIST_REMOVE(imm, i6mm_chain);
2413 if (imm->i6mm_maddr)
2414 in6_delmulti(imm->i6mm_maddr, 1);
2415 FREE(imm, M_IPMADDR);
2416 }
2417 lck_mtx_unlock(nd6_mutex);
2418 FREE(im6o, M_IPMOPTS);
2419 }
2420
2421 /*
2422 * Set IPv6 outgoing packet options based on advanced API.
2423 */
2424 int
2425 ip6_setpktoptions(control, opt, priv, needcopy)
2426 struct mbuf *control;
2427 struct ip6_pktopts *opt;
2428 int priv, needcopy;
2429 {
2430 struct cmsghdr *cm = 0;
2431
2432 if (control == 0 || opt == 0)
2433 return(EINVAL);
2434
2435 init_ip6pktopts(opt);
2436
2437 /*
2438 * XXX: Currently, we assume all the optional information is stored
2439 * in a single mbuf.
2440 */
2441 if (control->m_next)
2442 return(EINVAL);
2443
2444 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2445 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2446 cm = mtod(control, struct cmsghdr *);
2447 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2448 return(EINVAL);
2449 if (cm->cmsg_level != IPPROTO_IPV6)
2450 continue;
2451
2452 /*
2453 * XXX should check if RFC2292 API is mixed with 2292bis API
2454 */
2455 switch (cm->cmsg_type) {
2456 case IPV6_PKTINFO:
2457 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2458 return(EINVAL);
2459 if (needcopy) {
2460 /* XXX: Is it really WAITOK? */
2461 opt->ip6po_pktinfo =
2462 _MALLOC(sizeof(struct in6_pktinfo),
2463 M_IP6OPT, M_WAITOK);
2464 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2465 sizeof(struct in6_pktinfo));
2466 } else
2467 opt->ip6po_pktinfo =
2468 (struct in6_pktinfo *)CMSG_DATA(cm);
2469 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2470 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2471 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2472 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2473
2474 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2475 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2476 return(ENXIO);
2477 }
2478
2479 /*
2480 * Check if the requested source address is indeed a
2481 * unicast address assigned to the node, and can be
2482 * used as the packet's source address.
2483 */
2484 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2485 struct in6_ifaddr *ia6;
2486 struct sockaddr_in6 sin6;
2487
2488 bzero(&sin6, sizeof(sin6));
2489 sin6.sin6_len = sizeof(sin6);
2490 sin6.sin6_family = AF_INET6;
2491 sin6.sin6_addr =
2492 opt->ip6po_pktinfo->ipi6_addr;
2493 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2494 if (ia6 == NULL ||
2495 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2496 IN6_IFF_NOTREADY)) != 0) {
2497 if (ia6) ifafree(&ia6->ia_ifa);
2498 return(EADDRNOTAVAIL);
2499 }
2500 ifafree(&ia6->ia_ifa);
2501 ia6 = NULL;
2502 }
2503 break;
2504
2505 case IPV6_HOPLIMIT:
2506 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2507 return(EINVAL);
2508
2509 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2510 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2511 return(EINVAL);
2512 break;
2513
2514 case IPV6_NEXTHOP:
2515 if (!priv)
2516 return(EPERM);
2517
2518 if (cm->cmsg_len < sizeof(u_char) ||
2519 /* check if cmsg_len is large enough for sa_len */
2520 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2521 return(EINVAL);
2522
2523 if (needcopy) {
2524 opt->ip6po_nexthop =
2525 _MALLOC(*CMSG_DATA(cm),
2526 M_IP6OPT, M_WAITOK);
2527 bcopy(CMSG_DATA(cm),
2528 opt->ip6po_nexthop,
2529 *CMSG_DATA(cm));
2530 } else
2531 opt->ip6po_nexthop =
2532 (struct sockaddr *)CMSG_DATA(cm);
2533 break;
2534
2535 case IPV6_HOPOPTS:
2536 {
2537 struct ip6_hbh *hbh;
2538 int hbhlen;
2539
2540 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2541 return(EINVAL);
2542 hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2543 hbhlen = (hbh->ip6h_len + 1) << 3;
2544 if (cm->cmsg_len != CMSG_LEN(hbhlen))
2545 return(EINVAL);
2546
2547 if (needcopy) {
2548 opt->ip6po_hbh =
2549 _MALLOC(hbhlen, M_IP6OPT, M_WAITOK);
2550 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2551 } else
2552 opt->ip6po_hbh = hbh;
2553 break;
2554 }
2555
2556 case IPV6_DSTOPTS:
2557 {
2558 struct ip6_dest *dest, **newdest;
2559 int destlen;
2560
2561 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2562 return(EINVAL);
2563 dest = (struct ip6_dest *)CMSG_DATA(cm);
2564 destlen = (dest->ip6d_len + 1) << 3;
2565 if (cm->cmsg_len != CMSG_LEN(destlen))
2566 return(EINVAL);
2567
2568 /*
2569 * The old advacned API is ambiguous on this
2570 * point. Our approach is to determine the
2571 * position based according to the existence
2572 * of a routing header. Note, however, that
2573 * this depends on the order of the extension
2574 * headers in the ancillary data; the 1st part
2575 * of the destination options header must
2576 * appear before the routing header in the
2577 * ancillary data, too.
2578 * RFC2292bis solved the ambiguity by
2579 * introducing separate cmsg types.
2580 */
2581 if (opt->ip6po_rthdr == NULL)
2582 newdest = &opt->ip6po_dest1;
2583 else
2584 newdest = &opt->ip6po_dest2;
2585
2586 if (needcopy) {
2587 *newdest = _MALLOC(destlen, M_IP6OPT, M_WAITOK);
2588 bcopy(dest, *newdest, destlen);
2589 } else
2590 *newdest = dest;
2591
2592 break;
2593 }
2594
2595 case IPV6_RTHDR:
2596 {
2597 struct ip6_rthdr *rth;
2598 int rthlen;
2599
2600 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2601 return(EINVAL);
2602 rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2603 rthlen = (rth->ip6r_len + 1) << 3;
2604 if (cm->cmsg_len != CMSG_LEN(rthlen))
2605 return(EINVAL);
2606
2607 switch (rth->ip6r_type) {
2608 case IPV6_RTHDR_TYPE_0:
2609 /* must contain one addr */
2610 if (rth->ip6r_len == 0)
2611 return(EINVAL);
2612 /* length must be even */
2613 if (rth->ip6r_len % 2)
2614 return(EINVAL);
2615 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2616 return(EINVAL);
2617 break;
2618 default:
2619 return(EINVAL); /* not supported */
2620 }
2621
2622 if (needcopy) {
2623 opt->ip6po_rthdr = _MALLOC(rthlen, M_IP6OPT,
2624 M_WAITOK);
2625 bcopy(rth, opt->ip6po_rthdr, rthlen);
2626 } else
2627 opt->ip6po_rthdr = rth;
2628
2629 break;
2630 }
2631
2632 default:
2633 return(ENOPROTOOPT);
2634 }
2635 }
2636
2637 return(0);
2638 }
2639
2640 /*
2641 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2642 * packet to the input queue of a specified interface. Note that this
2643 * calls the output routine of the loopback "driver", but with an interface
2644 * pointer that might NOT be &loif -- easier than replicating that code here.
2645 */
2646 void
2647 ip6_mloopback(
2648 struct ifnet *ifp,
2649 struct mbuf *m,
2650 struct sockaddr_in6 *dst)
2651 {
2652 struct mbuf *copym;
2653 struct ip6_hdr *ip6;
2654
2655 copym = m_copy(m, 0, M_COPYALL);
2656 if (copym == NULL)
2657 return;
2658
2659 /*
2660 * Make sure to deep-copy IPv6 header portion in case the data
2661 * is in an mbuf cluster, so that we can safely override the IPv6
2662 * header portion later.
2663 */
2664 if ((copym->m_flags & M_EXT) != 0 ||
2665 copym->m_len < sizeof(struct ip6_hdr)) {
2666 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2667 if (copym == NULL)
2668 return;
2669 }
2670
2671 #if DIAGNOSTIC
2672 if (copym->m_len < sizeof(*ip6)) {
2673 m_freem(copym);
2674 return;
2675 }
2676 #endif
2677
2678 ip6 = mtod(copym, struct ip6_hdr *);
2679 #ifndef SCOPEDROUTING
2680 /*
2681 * clear embedded scope identifiers if necessary.
2682 * in6_clearscope will touch the addresses only when necessary.
2683 */
2684 in6_clearscope(&ip6->ip6_src);
2685 in6_clearscope(&ip6->ip6_dst);
2686 #endif
2687
2688 #ifdef __APPLE__
2689
2690 /* Makes sure the HW checksum flags are cleaned before sending the packet */
2691
2692 copym->m_pkthdr.rcvif = 0;
2693 copym->m_pkthdr.csum_data = 0;
2694 copym->m_pkthdr.csum_flags = 0;
2695
2696 if (lo_ifp) {
2697 copym->m_pkthdr.rcvif = ifp;
2698 lck_mtx_unlock(ip6_mutex);
2699 dlil_output(lo_ifp, PF_INET6, copym, 0, (struct sockaddr *)dst, 0);
2700 lck_mtx_lock(ip6_mutex);
2701 } else
2702 m_free(copym);
2703 #else
2704 (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
2705 #endif
2706 }
2707
2708 /*
2709 * Chop IPv6 header off from the payload.
2710 */
2711 static int
2712 ip6_splithdr(m, exthdrs)
2713 struct mbuf *m;
2714 struct ip6_exthdrs *exthdrs;
2715 {
2716 struct mbuf *mh;
2717 struct ip6_hdr *ip6;
2718
2719 ip6 = mtod(m, struct ip6_hdr *);
2720 if (m->m_len > sizeof(*ip6)) {
2721 MGETHDR(mh, M_DONTWAIT, MT_HEADER); /* MAC-OK */
2722 if (mh == 0) {
2723 m_freem(m);
2724 return ENOBUFS;
2725 }
2726 M_COPY_PKTHDR(mh, m);
2727 MH_ALIGN(mh, sizeof(*ip6));
2728 m->m_flags &= ~M_PKTHDR;
2729 m->m_len -= sizeof(*ip6);
2730 m->m_data += sizeof(*ip6);
2731 mh->m_next = m;
2732 m = mh;
2733 m->m_len = sizeof(*ip6);
2734 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2735 }
2736 exthdrs->ip6e_ip6 = m;
2737 return 0;
2738 }
2739
2740 /*
2741 * Compute IPv6 extension header length.
2742 */
2743 int
2744 ip6_optlen(in6p)
2745 struct in6pcb *in6p;
2746 {
2747 int len;
2748
2749 if (!in6p->in6p_outputopts)
2750 return 0;
2751
2752 len = 0;
2753 #define elen(x) \
2754 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2755
2756 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2757 if (in6p->in6p_outputopts->ip6po_rthdr)
2758 /* dest1 is valid with rthdr only */
2759 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2760 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2761 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2762 return len;
2763 #undef elen
2764 }
2765