ip6_output.c revision 1.117.2.2 1 /* $NetBSD: ip6_output.c,v 1.117.2.2 2007/06/09 23:58:13 ad Exp $ */
2 /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun 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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.117.2.2 2007/06/09 23:58:13 ad Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
70 #include "opt_pfil_hooks.h"
71
72 #include <sys/param.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/errno.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/kauth.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #ifdef PFIL_HOOKS
86 #include <net/pfil.h>
87 #endif
88
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/in_offload.h>
94 #include <netinet6/in6_offload.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/ip6protosw.h>
99 #include <netinet6/scope6_var.h>
100
101 #ifdef IPSEC
102 #include <netinet6/ipsec.h>
103 #include <netkey/key.h>
104 #endif /* IPSEC */
105
106 #ifdef FAST_IPSEC
107 #include <netipsec/ipsec.h>
108 #include <netipsec/ipsec6.h>
109 #include <netipsec/key.h>
110 #include <netipsec/xform.h>
111 #endif
112
113
114 #include <net/net_osdep.h>
115
116 #ifdef PFIL_HOOKS
117 extern struct pfil_head inet6_pfil_hook; /* XXX */
118 #endif
119
120 struct ip6_exthdrs {
121 struct mbuf *ip6e_ip6;
122 struct mbuf *ip6e_hbh;
123 struct mbuf *ip6e_dest1;
124 struct mbuf *ip6e_rthdr;
125 struct mbuf *ip6e_dest2;
126 };
127
128 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
129 int, int));
130 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct mbuf **));
131 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
132 int, int, int));
133 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
134 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
135 static int ip6_copyexthdr __P((struct mbuf **, void *, int));
136 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
137 struct ip6_frag **));
138 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
139 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
140 static int ip6_getpmtu(struct route *, struct route *, struct ifnet *,
141 const struct in6_addr *, u_long *, int *);
142 static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
143
144 #ifdef RFC2292
145 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
146 struct socket *));
147 #endif
148
149 #define IN6_NEED_CHECKSUM(ifp, csum_flags) \
150 (__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
151 (((csum_flags) & M_CSUM_UDPv6) != 0 && udp_do_loopback_cksum) || \
152 (((csum_flags) & M_CSUM_TCPv6) != 0 && tcp_do_loopback_cksum)))
153
154 /*
155 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
156 * header (with pri, len, nxt, hlim, src, dst).
157 * This function may modify ver and hlim only.
158 * The mbuf chain containing the packet will be freed.
159 * The mbuf opt, if present, will not be freed.
160 *
161 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
162 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
163 * which is rt_rmx.rmx_mtu.
164 */
165 int
166 ip6_output(
167 struct mbuf *m0,
168 struct ip6_pktopts *opt,
169 struct route *ro,
170 int flags,
171 struct ip6_moptions *im6o,
172 struct socket *so,
173 struct ifnet **ifpp /* XXX: just for statistics */
174 )
175 {
176 struct ip6_hdr *ip6, *mhip6;
177 struct ifnet *ifp, *origifp;
178 struct mbuf *m = m0;
179 int hlen, tlen, len, off;
180 bool tso;
181 struct route ip6route;
182 struct rtentry *rt = NULL;
183 const struct sockaddr_in6 *dst = NULL;
184 struct sockaddr_in6 src_sa, dst_sa;
185 int error = 0;
186 struct in6_ifaddr *ia = NULL;
187 u_long mtu;
188 int alwaysfrag, dontfrag;
189 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
190 struct ip6_exthdrs exthdrs;
191 struct in6_addr finaldst, src0, dst0;
192 u_int32_t zone;
193 struct route *ro_pmtu = NULL;
194 int hdrsplit = 0;
195 int needipsec = 0;
196 #ifdef IPSEC
197 int needipsectun = 0;
198 struct secpolicy *sp = NULL;
199
200 ip6 = mtod(m, struct ip6_hdr *);
201 #endif /* IPSEC */
202 #ifdef FAST_IPSEC
203 struct secpolicy *sp = NULL;
204 int s;
205 #endif
206
207
208 #ifdef DIAGNOSTIC
209 if ((m->m_flags & M_PKTHDR) == 0)
210 panic("ip6_output: no HDR");
211
212 if ((m->m_pkthdr.csum_flags &
213 (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) {
214 panic("ip6_output: IPv4 checksum offload flags: %d",
215 m->m_pkthdr.csum_flags);
216 }
217
218 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) ==
219 (M_CSUM_TCPv6|M_CSUM_UDPv6)) {
220 panic("ip6_output: conflicting checksum offload flags: %d",
221 m->m_pkthdr.csum_flags);
222 }
223 #endif
224
225 M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr));
226
227 #define MAKE_EXTHDR(hp, mp) \
228 do { \
229 if (hp) { \
230 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
231 error = ip6_copyexthdr((mp), (void *)(hp), \
232 ((eh)->ip6e_len + 1) << 3); \
233 if (error) \
234 goto freehdrs; \
235 } \
236 } while (/*CONSTCOND*/ 0)
237
238 bzero(&exthdrs, sizeof(exthdrs));
239 if (opt) {
240 /* Hop-by-Hop options header */
241 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
242 /* Destination options header(1st part) */
243 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
244 /* Routing header */
245 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
246 /* Destination options header(2nd part) */
247 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
248 }
249
250 #ifdef IPSEC
251 if ((flags & IPV6_FORWARDING) != 0) {
252 needipsec = 0;
253 goto skippolicycheck;
254 }
255
256 /* get a security policy for this packet */
257 if (so == NULL)
258 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
259 else {
260 if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
261 IPSEC_DIR_OUTBOUND)) {
262 needipsec = 0;
263 goto skippolicycheck;
264 }
265 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
266 }
267
268 if (sp == NULL) {
269 ipsec6stat.out_inval++;
270 goto freehdrs;
271 }
272
273 error = 0;
274
275 /* check policy */
276 switch (sp->policy) {
277 case IPSEC_POLICY_DISCARD:
278 /*
279 * This packet is just discarded.
280 */
281 ipsec6stat.out_polvio++;
282 goto freehdrs;
283
284 case IPSEC_POLICY_BYPASS:
285 case IPSEC_POLICY_NONE:
286 /* no need to do IPsec. */
287 needipsec = 0;
288 break;
289
290 case IPSEC_POLICY_IPSEC:
291 if (sp->req == NULL) {
292 /* XXX should be panic ? */
293 printf("ip6_output: No IPsec request specified.\n");
294 error = EINVAL;
295 goto freehdrs;
296 }
297 needipsec = 1;
298 break;
299
300 case IPSEC_POLICY_ENTRUST:
301 default:
302 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
303 }
304
305 skippolicycheck:;
306 #endif /* IPSEC */
307
308 /*
309 * Calculate the total length of the extension header chain.
310 * Keep the length of the unfragmentable part for fragmentation.
311 */
312 optlen = 0;
313 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
314 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
315 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
316 unfragpartlen = optlen + sizeof(struct ip6_hdr);
317 /* NOTE: we don't add AH/ESP length here. do that later. */
318 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
319
320 #ifdef FAST_IPSEC
321 /* Check the security policy (SP) for the packet */
322
323 /* XXX For moment, we doesn't support packet with extented action */
324 if (optlen !=0)
325 goto freehdrs;
326
327 sp = ipsec6_check_policy(m,so,flags,&needipsec,&error);
328 if (error != 0) {
329 /*
330 * Hack: -EINVAL is used to signal that a packet
331 * should be silently discarded. This is typically
332 * because we asked key management for an SA and
333 * it was delayed (e.g. kicked up to IKE).
334 */
335 if (error == -EINVAL)
336 error = 0;
337 goto freehdrs;
338 }
339 #endif /* FAST_IPSEC */
340
341
342 if (needipsec &&
343 (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
344 in6_delayed_cksum(m);
345 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
346 }
347
348
349 /*
350 * If we need IPsec, or there is at least one extension header,
351 * separate IP6 header from the payload.
352 */
353 if ((needipsec || optlen) && !hdrsplit) {
354 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
355 m = NULL;
356 goto freehdrs;
357 }
358 m = exthdrs.ip6e_ip6;
359 hdrsplit++;
360 }
361
362 /* adjust pointer */
363 ip6 = mtod(m, struct ip6_hdr *);
364
365 /* adjust mbuf packet header length */
366 m->m_pkthdr.len += optlen;
367 plen = m->m_pkthdr.len - sizeof(*ip6);
368
369 /* If this is a jumbo payload, insert a jumbo payload option. */
370 if (plen > IPV6_MAXPACKET) {
371 if (!hdrsplit) {
372 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
373 m = NULL;
374 goto freehdrs;
375 }
376 m = exthdrs.ip6e_ip6;
377 hdrsplit++;
378 }
379 /* adjust pointer */
380 ip6 = mtod(m, struct ip6_hdr *);
381 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
382 goto freehdrs;
383 optlen += 8; /* XXX JUMBOOPTLEN */
384 ip6->ip6_plen = 0;
385 } else
386 ip6->ip6_plen = htons(plen);
387
388 /*
389 * Concatenate headers and fill in next header fields.
390 * Here we have, on "m"
391 * IPv6 payload
392 * and we insert headers accordingly. Finally, we should be getting:
393 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
394 *
395 * during the header composing process, "m" points to IPv6 header.
396 * "mprev" points to an extension header prior to esp.
397 */
398 {
399 u_char *nexthdrp = &ip6->ip6_nxt;
400 struct mbuf *mprev = m;
401
402 /*
403 * we treat dest2 specially. this makes IPsec processing
404 * much easier. the goal here is to make mprev point the
405 * mbuf prior to dest2.
406 *
407 * result: IPv6 dest2 payload
408 * m and mprev will point to IPv6 header.
409 */
410 if (exthdrs.ip6e_dest2) {
411 if (!hdrsplit)
412 panic("assumption failed: hdr not split");
413 exthdrs.ip6e_dest2->m_next = m->m_next;
414 m->m_next = exthdrs.ip6e_dest2;
415 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
416 ip6->ip6_nxt = IPPROTO_DSTOPTS;
417 }
418
419 #define MAKE_CHAIN(m, mp, p, i)\
420 do {\
421 if (m) {\
422 if (!hdrsplit) \
423 panic("assumption failed: hdr not split"); \
424 *mtod((m), u_char *) = *(p);\
425 *(p) = (i);\
426 p = mtod((m), u_char *);\
427 (m)->m_next = (mp)->m_next;\
428 (mp)->m_next = (m);\
429 (mp) = (m);\
430 }\
431 } while (/*CONSTCOND*/ 0)
432 /*
433 * result: IPv6 hbh dest1 rthdr dest2 payload
434 * m will point to IPv6 header. mprev will point to the
435 * extension header prior to dest2 (rthdr in the above case).
436 */
437 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
438 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
439 IPPROTO_DSTOPTS);
440 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
441 IPPROTO_ROUTING);
442
443 M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data,
444 sizeof(struct ip6_hdr) + optlen);
445
446 #ifdef IPSEC
447 if (!needipsec)
448 goto skip_ipsec2;
449
450 /*
451 * pointers after IPsec headers are not valid any more.
452 * other pointers need a great care too.
453 * (IPsec routines should not mangle mbufs prior to AH/ESP)
454 */
455 exthdrs.ip6e_dest2 = NULL;
456
457 {
458 struct ip6_rthdr *rh = NULL;
459 int segleft_org = 0;
460 struct ipsec_output_state state;
461
462 if (exthdrs.ip6e_rthdr) {
463 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
464 segleft_org = rh->ip6r_segleft;
465 rh->ip6r_segleft = 0;
466 }
467
468 bzero(&state, sizeof(state));
469 state.m = m;
470 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
471 &needipsectun);
472 m = state.m;
473 if (error) {
474 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
475 /* mbuf is already reclaimed in ipsec6_output_trans. */
476 m = NULL;
477 switch (error) {
478 case EHOSTUNREACH:
479 case ENETUNREACH:
480 case EMSGSIZE:
481 case ENOBUFS:
482 case ENOMEM:
483 break;
484 default:
485 printf("ip6_output (ipsec): error code %d\n", error);
486 /* FALLTHROUGH */
487 case ENOENT:
488 /* don't show these error codes to the user */
489 error = 0;
490 break;
491 }
492 goto bad;
493 }
494 if (exthdrs.ip6e_rthdr) {
495 /* ah6_output doesn't modify mbuf chain */
496 rh->ip6r_segleft = segleft_org;
497 }
498 }
499 skip_ipsec2:;
500 #endif
501 }
502
503 /*
504 * If there is a routing header, replace destination address field
505 * with the first hop of the routing header.
506 */
507 if (exthdrs.ip6e_rthdr) {
508 struct ip6_rthdr *rh;
509 struct ip6_rthdr0 *rh0;
510 struct in6_addr *addr;
511 struct sockaddr_in6 sa;
512
513 rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
514 struct ip6_rthdr *));
515 finaldst = ip6->ip6_dst;
516 switch (rh->ip6r_type) {
517 case IPV6_RTHDR_TYPE_0:
518 rh0 = (struct ip6_rthdr0 *)rh;
519 addr = (struct in6_addr *)(rh0 + 1);
520
521 /*
522 * construct a sockaddr_in6 form of
523 * the first hop.
524 *
525 * XXX: we may not have enough
526 * information about its scope zone;
527 * there is no standard API to pass
528 * the information from the
529 * application.
530 */
531 bzero(&sa, sizeof(sa));
532 sa.sin6_family = AF_INET6;
533 sa.sin6_len = sizeof(sa);
534 sa.sin6_addr = addr[0];
535 if ((error = sa6_embedscope(&sa,
536 ip6_use_defzone)) != 0) {
537 goto bad;
538 }
539 ip6->ip6_dst = sa.sin6_addr;
540 (void)memmove(&addr[0], &addr[1],
541 sizeof(struct in6_addr) *
542 (rh0->ip6r0_segleft - 1));
543 addr[rh0->ip6r0_segleft - 1] = finaldst;
544 /* XXX */
545 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
546 break;
547 default: /* is it possible? */
548 error = EINVAL;
549 goto bad;
550 }
551 }
552
553 /* Source address validation */
554 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
555 (flags & IPV6_UNSPECSRC) == 0) {
556 error = EOPNOTSUPP;
557 ip6stat.ip6s_badscope++;
558 goto bad;
559 }
560 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
561 error = EOPNOTSUPP;
562 ip6stat.ip6s_badscope++;
563 goto bad;
564 }
565
566 ip6stat.ip6s_localout++;
567
568 /*
569 * Route packet.
570 */
571 /* initialize cached route */
572 if (ro == NULL) {
573 memset(&ip6route, 0, sizeof(ip6route));
574 ro = &ip6route;
575 }
576 ro_pmtu = ro;
577 if (opt && opt->ip6po_rthdr)
578 ro = &opt->ip6po_route;
579
580 /*
581 * if specified, try to fill in the traffic class field.
582 * do not override if a non-zero value is already set.
583 * we check the diffserv field and the ecn field separately.
584 */
585 if (opt && opt->ip6po_tclass >= 0) {
586 int mask = 0;
587
588 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
589 mask |= 0xfc;
590 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
591 mask |= 0x03;
592 if (mask != 0)
593 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
594 }
595
596 /* fill in or override the hop limit field, if necessary. */
597 if (opt && opt->ip6po_hlim != -1)
598 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
599 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
600 if (im6o != NULL)
601 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
602 else
603 ip6->ip6_hlim = ip6_defmcasthlim;
604 }
605
606 #ifdef IPSEC
607 if (needipsec && needipsectun) {
608 struct ipsec_output_state state;
609
610 /*
611 * All the extension headers will become inaccessible
612 * (since they can be encrypted).
613 * Don't panic, we need no more updates to extension headers
614 * on inner IPv6 packet (since they are now encapsulated).
615 *
616 * IPv6 [ESP|AH] IPv6 [extension headers] payload
617 */
618 bzero(&exthdrs, sizeof(exthdrs));
619 exthdrs.ip6e_ip6 = m;
620
621 bzero(&state, sizeof(state));
622 state.m = m;
623 state.ro = ro;
624 state.dst = rtcache_getdst(ro);
625
626 error = ipsec6_output_tunnel(&state, sp, flags);
627
628 m = state.m;
629 ro_pmtu = ro = state.ro;
630 dst = satocsin6(state.dst);
631 if (error) {
632 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
633 m0 = m = NULL;
634 m = NULL;
635 switch (error) {
636 case EHOSTUNREACH:
637 case ENETUNREACH:
638 case EMSGSIZE:
639 case ENOBUFS:
640 case ENOMEM:
641 break;
642 default:
643 printf("ip6_output (ipsec): error code %d\n", error);
644 /* FALLTHROUGH */
645 case ENOENT:
646 /* don't show these error codes to the user */
647 error = 0;
648 break;
649 }
650 goto bad;
651 }
652
653 exthdrs.ip6e_ip6 = m;
654 }
655 #endif /* IPSEC */
656 #ifdef FAST_IPSEC
657 if (needipsec) {
658 s = splsoftnet();
659 error = ipsec6_process_packet(m,sp->req);
660
661 /*
662 * Preserve KAME behaviour: ENOENT can be returned
663 * when an SA acquire is in progress. Don't propagate
664 * this to user-level; it confuses applications.
665 * XXX this will go away when the SADB is redone.
666 */
667 if (error == ENOENT)
668 error = 0;
669 splx(s);
670 goto done;
671 }
672 #endif /* FAST_IPSEC */
673
674
675
676 /* adjust pointer */
677 ip6 = mtod(m, struct ip6_hdr *);
678
679 bzero(&dst_sa, sizeof(dst_sa));
680 dst_sa.sin6_family = AF_INET6;
681 dst_sa.sin6_len = sizeof(dst_sa);
682 dst_sa.sin6_addr = ip6->ip6_dst;
683 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
684 &ifp, &rt, 0)) != 0) {
685 if (ifp != NULL)
686 in6_ifstat_inc(ifp, ifs6_out_discard);
687 goto bad;
688 }
689 if (rt == NULL) {
690 /*
691 * If in6_selectroute() does not return a route entry,
692 * dst may not have been updated.
693 */
694 rtcache_setdst(ro, sin6tosa(&dst_sa));
695 }
696
697 /*
698 * then rt (for unicast) and ifp must be non-NULL valid values.
699 */
700 if ((flags & IPV6_FORWARDING) == 0) {
701 /* XXX: the FORWARDING flag can be set for mrouting. */
702 in6_ifstat_inc(ifp, ifs6_out_request);
703 }
704 if (rt != NULL) {
705 ia = (struct in6_ifaddr *)(rt->rt_ifa);
706 rt->rt_use++;
707 }
708
709 /*
710 * The outgoing interface must be in the zone of source and
711 * destination addresses. We should use ia_ifp to support the
712 * case of sending packets to an address of our own.
713 */
714 if (ia != NULL && ia->ia_ifp)
715 origifp = ia->ia_ifp;
716 else
717 origifp = ifp;
718
719 src0 = ip6->ip6_src;
720 if (in6_setscope(&src0, origifp, &zone))
721 goto badscope;
722 bzero(&src_sa, sizeof(src_sa));
723 src_sa.sin6_family = AF_INET6;
724 src_sa.sin6_len = sizeof(src_sa);
725 src_sa.sin6_addr = ip6->ip6_src;
726 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
727 goto badscope;
728
729 dst0 = ip6->ip6_dst;
730 if (in6_setscope(&dst0, origifp, &zone))
731 goto badscope;
732 /* re-initialize to be sure */
733 bzero(&dst_sa, sizeof(dst_sa));
734 dst_sa.sin6_family = AF_INET6;
735 dst_sa.sin6_len = sizeof(dst_sa);
736 dst_sa.sin6_addr = ip6->ip6_dst;
737 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id)
738 goto badscope;
739
740 /* scope check is done. */
741
742 if (rt == NULL || IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
743 if (dst == NULL)
744 dst = satocsin6(rtcache_getdst(ro));
745 KASSERT(dst != NULL);
746 } else if (opt && opt->ip6po_nextroute.ro_rt != NULL) {
747 /*
748 * The nexthop is explicitly specified by the
749 * application. We assume the next hop is an IPv6
750 * address.
751 */
752 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
753 } else if ((rt->rt_flags & RTF_GATEWAY))
754 dst = (struct sockaddr_in6 *)rt->rt_gateway;
755 else if (dst == NULL)
756 dst = satocsin6(rtcache_getdst(ro));
757
758 /*
759 * XXXXXX: original code follows:
760 */
761 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
762 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
763 else {
764 struct in6_multi *in6m;
765
766 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
767
768 in6_ifstat_inc(ifp, ifs6_out_mcast);
769
770 /*
771 * Confirm that the outgoing interface supports multicast.
772 */
773 if (!(ifp->if_flags & IFF_MULTICAST)) {
774 ip6stat.ip6s_noroute++;
775 in6_ifstat_inc(ifp, ifs6_out_discard);
776 error = ENETUNREACH;
777 goto bad;
778 }
779
780 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
781 if (in6m != NULL &&
782 (im6o == NULL || im6o->im6o_multicast_loop)) {
783 /*
784 * If we belong to the destination multicast group
785 * on the outgoing interface, and the caller did not
786 * forbid loopback, loop back a copy.
787 */
788 KASSERT(dst != NULL);
789 ip6_mloopback(ifp, m, dst);
790 } else {
791 /*
792 * If we are acting as a multicast router, perform
793 * multicast forwarding as if the packet had just
794 * arrived on the interface to which we are about
795 * to send. The multicast forwarding function
796 * recursively calls this function, using the
797 * IPV6_FORWARDING flag to prevent infinite recursion.
798 *
799 * Multicasts that are looped back by ip6_mloopback(),
800 * above, will be forwarded by the ip6_input() routine,
801 * if necessary.
802 */
803 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
804 if (ip6_mforward(ip6, ifp, m) != 0) {
805 m_freem(m);
806 goto done;
807 }
808 }
809 }
810 /*
811 * Multicasts with a hoplimit of zero may be looped back,
812 * above, but must not be transmitted on a network.
813 * Also, multicasts addressed to the loopback interface
814 * are not sent -- the above call to ip6_mloopback() will
815 * loop back a copy if this host actually belongs to the
816 * destination group on the loopback interface.
817 */
818 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
819 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
820 m_freem(m);
821 goto done;
822 }
823 }
824
825 /*
826 * Fill the outgoing inteface to tell the upper layer
827 * to increment per-interface statistics.
828 */
829 if (ifpp)
830 *ifpp = ifp;
831
832 /* Determine path MTU. */
833 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
834 &alwaysfrag)) != 0)
835 goto bad;
836 #ifdef IPSEC
837 if (needipsectun)
838 mtu = IPV6_MMTU;
839 #endif
840
841 /*
842 * The caller of this function may specify to use the minimum MTU
843 * in some cases.
844 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
845 * setting. The logic is a bit complicated; by default, unicast
846 * packets will follow path MTU while multicast packets will be sent at
847 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
848 * including unicast ones will be sent at the minimum MTU. Multicast
849 * packets will always be sent at the minimum MTU unless
850 * IP6PO_MINMTU_DISABLE is explicitly specified.
851 * See RFC 3542 for more details.
852 */
853 if (mtu > IPV6_MMTU) {
854 if ((flags & IPV6_MINMTU))
855 mtu = IPV6_MMTU;
856 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
857 mtu = IPV6_MMTU;
858 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
859 (opt == NULL ||
860 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
861 mtu = IPV6_MMTU;
862 }
863 }
864
865 /*
866 * clear embedded scope identifiers if necessary.
867 * in6_clearscope will touch the addresses only when necessary.
868 */
869 in6_clearscope(&ip6->ip6_src);
870 in6_clearscope(&ip6->ip6_dst);
871
872 /*
873 * If the outgoing packet contains a hop-by-hop options header,
874 * it must be examined and processed even by the source node.
875 * (RFC 2460, section 4.)
876 */
877 if (exthdrs.ip6e_hbh) {
878 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
879 u_int32_t dummy1; /* XXX unused */
880 u_int32_t dummy2; /* XXX unused */
881
882 /*
883 * XXX: if we have to send an ICMPv6 error to the sender,
884 * we need the M_LOOP flag since icmp6_error() expects
885 * the IPv6 and the hop-by-hop options header are
886 * continuous unless the flag is set.
887 */
888 m->m_flags |= M_LOOP;
889 m->m_pkthdr.rcvif = ifp;
890 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
891 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
892 &dummy1, &dummy2) < 0) {
893 /* m was already freed at this point */
894 error = EINVAL;/* better error? */
895 goto done;
896 }
897 m->m_flags &= ~M_LOOP; /* XXX */
898 m->m_pkthdr.rcvif = NULL;
899 }
900
901 #ifdef PFIL_HOOKS
902 /*
903 * Run through list of hooks for output packets.
904 */
905 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
906 goto done;
907 if (m == NULL)
908 goto done;
909 ip6 = mtod(m, struct ip6_hdr *);
910 #endif /* PFIL_HOOKS */
911 /*
912 * Send the packet to the outgoing interface.
913 * If necessary, do IPv6 fragmentation before sending.
914 *
915 * the logic here is rather complex:
916 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
917 * 1-a: send as is if tlen <= path mtu
918 * 1-b: fragment if tlen > path mtu
919 *
920 * 2: if user asks us not to fragment (dontfrag == 1)
921 * 2-a: send as is if tlen <= interface mtu
922 * 2-b: error if tlen > interface mtu
923 *
924 * 3: if we always need to attach fragment header (alwaysfrag == 1)
925 * always fragment
926 *
927 * 4: if dontfrag == 1 && alwaysfrag == 1
928 * error, as we cannot handle this conflicting request
929 */
930 tlen = m->m_pkthdr.len;
931 tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0;
932 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
933 dontfrag = 1;
934 else
935 dontfrag = 0;
936
937 if (dontfrag && alwaysfrag) { /* case 4 */
938 /* conflicting request - can't transmit */
939 error = EMSGSIZE;
940 goto bad;
941 }
942 if (dontfrag && (!tso && tlen > IN6_LINKMTU(ifp))) { /* case 2-b */
943 /*
944 * Even if the DONTFRAG option is specified, we cannot send the
945 * packet when the data length is larger than the MTU of the
946 * outgoing interface.
947 * Notify the error by sending IPV6_PATHMTU ancillary data as
948 * well as returning an error code (the latter is not described
949 * in the API spec.)
950 */
951 u_int32_t mtu32;
952 struct ip6ctlparam ip6cp;
953
954 mtu32 = (u_int32_t)mtu;
955 bzero(&ip6cp, sizeof(ip6cp));
956 ip6cp.ip6c_cmdarg = (void *)&mtu32;
957 pfctlinput2(PRC_MSGSIZE,
958 rtcache_getdst(ro_pmtu), &ip6cp);
959
960 error = EMSGSIZE;
961 goto bad;
962 }
963
964 /*
965 * transmit packet without fragmentation
966 */
967 if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) {
968 /* case 1-a and 2-a */
969 struct in6_ifaddr *ia6;
970 int sw_csum;
971
972 ip6 = mtod(m, struct ip6_hdr *);
973 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
974 if (ia6) {
975 /* Record statistics for this interface address. */
976 ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
977 }
978 #ifdef IPSEC
979 /* clean ipsec history once it goes out of the node */
980 ipsec_delaux(m);
981 #endif
982
983 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
984 if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
985 if (IN6_NEED_CHECKSUM(ifp,
986 sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
987 in6_delayed_cksum(m);
988 }
989 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
990 }
991
992 KASSERT(dst != NULL);
993 if (__predict_true(!tso ||
994 (ifp->if_capenable & IFCAP_TSOv6) != 0)) {
995 error = nd6_output(ifp, origifp, m, dst, rt);
996 } else {
997 error = ip6_tso_output(ifp, origifp, m, dst, rt);
998 }
999 goto done;
1000 }
1001
1002 if (tso) {
1003 error = EINVAL; /* XXX */
1004 goto bad;
1005 }
1006
1007 /*
1008 * try to fragment the packet. case 1-b and 3
1009 */
1010 if (mtu < IPV6_MMTU) {
1011 /* path MTU cannot be less than IPV6_MMTU */
1012 error = EMSGSIZE;
1013 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1014 goto bad;
1015 } else if (ip6->ip6_plen == 0) {
1016 /* jumbo payload cannot be fragmented */
1017 error = EMSGSIZE;
1018 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1019 goto bad;
1020 } else {
1021 struct mbuf **mnext, *m_frgpart;
1022 struct ip6_frag *ip6f;
1023 u_int32_t id = htonl(ip6_randomid());
1024 u_char nextproto;
1025 #if 0 /* see below */
1026 struct ip6ctlparam ip6cp;
1027 u_int32_t mtu32;
1028 #endif
1029
1030 /*
1031 * Too large for the destination or interface;
1032 * fragment if possible.
1033 * Must be able to put at least 8 bytes per fragment.
1034 */
1035 hlen = unfragpartlen;
1036 if (mtu > IPV6_MAXPACKET)
1037 mtu = IPV6_MAXPACKET;
1038
1039 #if 0
1040 /*
1041 * It is believed this code is a leftover from the
1042 * development of the IPV6_RECVPATHMTU sockopt and
1043 * associated work to implement RFC3542.
1044 * It's not entirely clear what the intent of the API
1045 * is at this point, so disable this code for now.
1046 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1047 * will send notifications if the application requests.
1048 */
1049
1050 /* Notify a proper path MTU to applications. */
1051 mtu32 = (u_int32_t)mtu;
1052 bzero(&ip6cp, sizeof(ip6cp));
1053 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1054 pfctlinput2(PRC_MSGSIZE,
1055 rtcache_getdst(ro_pmtu), &ip6cp);
1056 #endif
1057
1058 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1059 if (len < 8) {
1060 error = EMSGSIZE;
1061 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1062 goto bad;
1063 }
1064
1065 mnext = &m->m_nextpkt;
1066
1067 /*
1068 * Change the next header field of the last header in the
1069 * unfragmentable part.
1070 */
1071 if (exthdrs.ip6e_rthdr) {
1072 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1073 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1074 } else if (exthdrs.ip6e_dest1) {
1075 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1076 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1077 } else if (exthdrs.ip6e_hbh) {
1078 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1079 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1080 } else {
1081 nextproto = ip6->ip6_nxt;
1082 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1083 }
1084
1085 if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6))
1086 != 0) {
1087 if (IN6_NEED_CHECKSUM(ifp,
1088 m->m_pkthdr.csum_flags &
1089 (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
1090 in6_delayed_cksum(m);
1091 }
1092 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
1093 }
1094
1095 /*
1096 * Loop through length of segment after first fragment,
1097 * make new header and copy data of each part and link onto
1098 * chain.
1099 */
1100 m0 = m;
1101 for (off = hlen; off < tlen; off += len) {
1102 struct mbuf *mlast;
1103
1104 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1105 if (!m) {
1106 error = ENOBUFS;
1107 ip6stat.ip6s_odropped++;
1108 goto sendorfree;
1109 }
1110 m->m_pkthdr.rcvif = NULL;
1111 m->m_flags = m0->m_flags & M_COPYFLAGS;
1112 *mnext = m;
1113 mnext = &m->m_nextpkt;
1114 m->m_data += max_linkhdr;
1115 mhip6 = mtod(m, struct ip6_hdr *);
1116 *mhip6 = *ip6;
1117 m->m_len = sizeof(*mhip6);
1118 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1119 if (error) {
1120 ip6stat.ip6s_odropped++;
1121 goto sendorfree;
1122 }
1123 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
1124 if (off + len >= tlen)
1125 len = tlen - off;
1126 else
1127 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1128 mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
1129 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1130 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1131 error = ENOBUFS;
1132 ip6stat.ip6s_odropped++;
1133 goto sendorfree;
1134 }
1135 for (mlast = m; mlast->m_next; mlast = mlast->m_next)
1136 ;
1137 mlast->m_next = m_frgpart;
1138 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1139 m->m_pkthdr.rcvif = (struct ifnet *)0;
1140 ip6f->ip6f_reserved = 0;
1141 ip6f->ip6f_ident = id;
1142 ip6f->ip6f_nxt = nextproto;
1143 ip6stat.ip6s_ofragments++;
1144 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1145 }
1146
1147 in6_ifstat_inc(ifp, ifs6_out_fragok);
1148 }
1149
1150 /*
1151 * Remove leading garbages.
1152 */
1153 sendorfree:
1154 m = m0->m_nextpkt;
1155 m0->m_nextpkt = 0;
1156 m_freem(m0);
1157 for (m0 = m; m; m = m0) {
1158 m0 = m->m_nextpkt;
1159 m->m_nextpkt = 0;
1160 if (error == 0) {
1161 struct in6_ifaddr *ia6;
1162 ip6 = mtod(m, struct ip6_hdr *);
1163 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1164 if (ia6) {
1165 /*
1166 * Record statistics for this interface
1167 * address.
1168 */
1169 ia6->ia_ifa.ifa_data.ifad_outbytes +=
1170 m->m_pkthdr.len;
1171 }
1172 #ifdef IPSEC
1173 /* clean ipsec history once it goes out of the node */
1174 ipsec_delaux(m);
1175 #endif
1176 KASSERT(dst != NULL);
1177 error = nd6_output(ifp, origifp, m, dst, rt);
1178 } else
1179 m_freem(m);
1180 }
1181
1182 if (error == 0)
1183 ip6stat.ip6s_fragmented++;
1184
1185 done:
1186 /* XXX Second if is invariant? */
1187 if (ro == &ip6route)
1188 rtcache_free(ro);
1189 else if (ro_pmtu == &ip6route)
1190 rtcache_free(ro_pmtu);
1191
1192 #ifdef IPSEC
1193 if (sp != NULL)
1194 key_freesp(sp);
1195 #endif /* IPSEC */
1196 #ifdef FAST_IPSEC
1197 if (sp != NULL)
1198 KEY_FREESP(&sp);
1199 #endif /* FAST_IPSEC */
1200
1201
1202 return (error);
1203
1204 freehdrs:
1205 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1206 m_freem(exthdrs.ip6e_dest1);
1207 m_freem(exthdrs.ip6e_rthdr);
1208 m_freem(exthdrs.ip6e_dest2);
1209 /* FALLTHROUGH */
1210 bad:
1211 m_freem(m);
1212 goto done;
1213 badscope:
1214 ip6stat.ip6s_badscope++;
1215 in6_ifstat_inc(origifp, ifs6_out_discard);
1216 if (error == 0)
1217 error = EHOSTUNREACH; /* XXX */
1218 goto bad;
1219 }
1220
1221 static int
1222 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen)
1223 {
1224 struct mbuf *m;
1225
1226 if (hlen > MCLBYTES)
1227 return (ENOBUFS); /* XXX */
1228
1229 MGET(m, M_DONTWAIT, MT_DATA);
1230 if (!m)
1231 return (ENOBUFS);
1232
1233 if (hlen > MLEN) {
1234 MCLGET(m, M_DONTWAIT);
1235 if ((m->m_flags & M_EXT) == 0) {
1236 m_free(m);
1237 return (ENOBUFS);
1238 }
1239 }
1240 m->m_len = hlen;
1241 if (hdr)
1242 bcopy(hdr, mtod(m, void *), hlen);
1243
1244 *mp = m;
1245 return (0);
1246 }
1247
1248 /*
1249 * Process a delayed payload checksum calculation.
1250 */
1251 void
1252 in6_delayed_cksum(struct mbuf *m)
1253 {
1254 uint16_t csum, offset;
1255
1256 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1257 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1258 KASSERT((m->m_pkthdr.csum_flags
1259 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
1260
1261 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
1262 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1263 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
1264 csum = 0xffff;
1265 }
1266
1267 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
1268 if ((offset + sizeof(csum)) > m->m_len) {
1269 m_copyback(m, offset, sizeof(csum), &csum);
1270 } else {
1271 *(uint16_t *)(mtod(m, char *) + offset) = csum;
1272 }
1273 }
1274
1275 /*
1276 * Insert jumbo payload option.
1277 */
1278 static int
1279 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1280 {
1281 struct mbuf *mopt;
1282 u_int8_t *optbuf;
1283 u_int32_t v;
1284
1285 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1286
1287 /*
1288 * If there is no hop-by-hop options header, allocate new one.
1289 * If there is one but it doesn't have enough space to store the
1290 * jumbo payload option, allocate a cluster to store the whole options.
1291 * Otherwise, use it to store the options.
1292 */
1293 if (exthdrs->ip6e_hbh == 0) {
1294 MGET(mopt, M_DONTWAIT, MT_DATA);
1295 if (mopt == 0)
1296 return (ENOBUFS);
1297 mopt->m_len = JUMBOOPTLEN;
1298 optbuf = mtod(mopt, u_int8_t *);
1299 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1300 exthdrs->ip6e_hbh = mopt;
1301 } else {
1302 struct ip6_hbh *hbh;
1303
1304 mopt = exthdrs->ip6e_hbh;
1305 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1306 /*
1307 * XXX assumption:
1308 * - exthdrs->ip6e_hbh is not referenced from places
1309 * other than exthdrs.
1310 * - exthdrs->ip6e_hbh is not an mbuf chain.
1311 */
1312 int oldoptlen = mopt->m_len;
1313 struct mbuf *n;
1314
1315 /*
1316 * XXX: give up if the whole (new) hbh header does
1317 * not fit even in an mbuf cluster.
1318 */
1319 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1320 return (ENOBUFS);
1321
1322 /*
1323 * As a consequence, we must always prepare a cluster
1324 * at this point.
1325 */
1326 MGET(n, M_DONTWAIT, MT_DATA);
1327 if (n) {
1328 MCLGET(n, M_DONTWAIT);
1329 if ((n->m_flags & M_EXT) == 0) {
1330 m_freem(n);
1331 n = NULL;
1332 }
1333 }
1334 if (!n)
1335 return (ENOBUFS);
1336 n->m_len = oldoptlen + JUMBOOPTLEN;
1337 bcopy(mtod(mopt, void *), mtod(n, void *),
1338 oldoptlen);
1339 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1340 m_freem(mopt);
1341 mopt = exthdrs->ip6e_hbh = n;
1342 } else {
1343 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1344 mopt->m_len += JUMBOOPTLEN;
1345 }
1346 optbuf[0] = IP6OPT_PADN;
1347 optbuf[1] = 0;
1348
1349 /*
1350 * Adjust the header length according to the pad and
1351 * the jumbo payload option.
1352 */
1353 hbh = mtod(mopt, struct ip6_hbh *);
1354 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1355 }
1356
1357 /* fill in the option. */
1358 optbuf[2] = IP6OPT_JUMBO;
1359 optbuf[3] = 4;
1360 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1361 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1362
1363 /* finally, adjust the packet header length */
1364 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1365
1366 return (0);
1367 #undef JUMBOOPTLEN
1368 }
1369
1370 /*
1371 * Insert fragment header and copy unfragmentable header portions.
1372 */
1373 static int
1374 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1375 struct ip6_frag **frghdrp)
1376 {
1377 struct mbuf *n, *mlast;
1378
1379 if (hlen > sizeof(struct ip6_hdr)) {
1380 n = m_copym(m0, sizeof(struct ip6_hdr),
1381 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1382 if (n == 0)
1383 return (ENOBUFS);
1384 m->m_next = n;
1385 } else
1386 n = m;
1387
1388 /* Search for the last mbuf of unfragmentable part. */
1389 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1390 ;
1391
1392 if ((mlast->m_flags & M_EXT) == 0 &&
1393 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1394 /* use the trailing space of the last mbuf for the fragment hdr */
1395 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
1396 mlast->m_len);
1397 mlast->m_len += sizeof(struct ip6_frag);
1398 m->m_pkthdr.len += sizeof(struct ip6_frag);
1399 } else {
1400 /* allocate a new mbuf for the fragment header */
1401 struct mbuf *mfrg;
1402
1403 MGET(mfrg, M_DONTWAIT, MT_DATA);
1404 if (mfrg == 0)
1405 return (ENOBUFS);
1406 mfrg->m_len = sizeof(struct ip6_frag);
1407 *frghdrp = mtod(mfrg, struct ip6_frag *);
1408 mlast->m_next = mfrg;
1409 }
1410
1411 return (0);
1412 }
1413
1414 static int
1415 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp,
1416 const struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
1417 {
1418 u_int32_t mtu = 0;
1419 int alwaysfrag = 0;
1420 int error = 0;
1421
1422 if (ro_pmtu != ro) {
1423 union {
1424 struct sockaddr dst;
1425 struct sockaddr_in6 dst6;
1426 } u;
1427
1428 /* The first hop and the final destination may differ. */
1429 sockaddr_in6_init(&u.dst6, dst, 0, 0, 0);
1430 rtcache_lookup(ro_pmtu, &u.dst);
1431 }
1432 if (ro_pmtu->ro_rt != NULL) {
1433 u_int32_t ifmtu;
1434
1435 if (ifp == NULL)
1436 ifp = ro_pmtu->ro_rt->rt_ifp;
1437 ifmtu = IN6_LINKMTU(ifp);
1438 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1439 if (mtu == 0)
1440 mtu = ifmtu;
1441 else if (mtu < IPV6_MMTU) {
1442 /*
1443 * RFC2460 section 5, last paragraph:
1444 * if we record ICMPv6 too big message with
1445 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1446 * or smaller, with fragment header attached.
1447 * (fragment header is needed regardless from the
1448 * packet size, for translators to identify packets)
1449 */
1450 alwaysfrag = 1;
1451 mtu = IPV6_MMTU;
1452 } else if (mtu > ifmtu) {
1453 /*
1454 * The MTU on the route is larger than the MTU on
1455 * the interface! This shouldn't happen, unless the
1456 * MTU of the interface has been changed after the
1457 * interface was brought up. Change the MTU in the
1458 * route to match the interface MTU (as long as the
1459 * field isn't locked).
1460 */
1461 mtu = ifmtu;
1462 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
1463 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1464 }
1465 } else if (ifp) {
1466 mtu = IN6_LINKMTU(ifp);
1467 } else
1468 error = EHOSTUNREACH; /* XXX */
1469
1470 *mtup = mtu;
1471 if (alwaysfragp)
1472 *alwaysfragp = alwaysfrag;
1473 return (error);
1474 }
1475
1476 /*
1477 * IP6 socket option processing.
1478 */
1479 int
1480 ip6_ctloutput(int op, struct socket *so, int level, int optname,
1481 struct mbuf **mp)
1482 {
1483 int privileged, optdatalen, uproto;
1484 void *optdata;
1485 struct in6pcb *in6p = sotoin6pcb(so);
1486 struct mbuf *m = *mp;
1487 int error, optval;
1488 int optlen;
1489 struct lwp *l = curlwp; /* XXX */
1490
1491 optlen = m ? m->m_len : 0;
1492 error = optval = 0;
1493 privileged = (l == 0 || kauth_authorize_generic(l->l_cred,
1494 KAUTH_GENERIC_ISSUSER, NULL)) ? 0 : 1;
1495 uproto = (int)so->so_proto->pr_protocol;
1496
1497 if (level == IPPROTO_IPV6) {
1498 switch (op) {
1499 case PRCO_SETOPT:
1500 switch (optname) {
1501 #ifdef RFC2292
1502 case IPV6_2292PKTOPTIONS:
1503 /* m is freed in ip6_pcbopts */
1504 error = ip6_pcbopts(&in6p->in6p_outputopts,
1505 m, so);
1506 break;
1507 #endif
1508
1509 /*
1510 * Use of some Hop-by-Hop options or some
1511 * Destination options, might require special
1512 * privilege. That is, normal applications
1513 * (without special privilege) might be forbidden
1514 * from setting certain options in outgoing packets,
1515 * and might never see certain options in received
1516 * packets. [RFC 2292 Section 6]
1517 * KAME specific note:
1518 * KAME prevents non-privileged users from sending or
1519 * receiving ANY hbh/dst options in order to avoid
1520 * overhead of parsing options in the kernel.
1521 */
1522 case IPV6_RECVHOPOPTS:
1523 case IPV6_RECVDSTOPTS:
1524 case IPV6_RECVRTHDRDSTOPTS:
1525 if (!privileged) {
1526 error = EPERM;
1527 break;
1528 }
1529 /* FALLTHROUGH */
1530 case IPV6_UNICAST_HOPS:
1531 case IPV6_HOPLIMIT:
1532 case IPV6_FAITH:
1533
1534 case IPV6_RECVPKTINFO:
1535 case IPV6_RECVHOPLIMIT:
1536 case IPV6_RECVRTHDR:
1537 case IPV6_RECVPATHMTU:
1538 case IPV6_RECVTCLASS:
1539 case IPV6_V6ONLY:
1540 if (optlen != sizeof(int)) {
1541 error = EINVAL;
1542 break;
1543 }
1544 optval = *mtod(m, int *);
1545 switch (optname) {
1546
1547 case IPV6_UNICAST_HOPS:
1548 if (optval < -1 || optval >= 256)
1549 error = EINVAL;
1550 else {
1551 /* -1 = kernel default */
1552 in6p->in6p_hops = optval;
1553 }
1554 break;
1555 #define OPTSET(bit) \
1556 do { \
1557 if (optval) \
1558 in6p->in6p_flags |= (bit); \
1559 else \
1560 in6p->in6p_flags &= ~(bit); \
1561 } while (/*CONSTCOND*/ 0)
1562
1563 #ifdef RFC2292
1564 #define OPTSET2292(bit) \
1565 do { \
1566 in6p->in6p_flags |= IN6P_RFC2292; \
1567 if (optval) \
1568 in6p->in6p_flags |= (bit); \
1569 else \
1570 in6p->in6p_flags &= ~(bit); \
1571 } while (/*CONSTCOND*/ 0)
1572 #endif
1573
1574 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1575
1576 case IPV6_RECVPKTINFO:
1577 #ifdef RFC2292
1578 /* cannot mix with RFC2292 */
1579 if (OPTBIT(IN6P_RFC2292)) {
1580 error = EINVAL;
1581 break;
1582 }
1583 #endif
1584 OPTSET(IN6P_PKTINFO);
1585 break;
1586
1587 case IPV6_HOPLIMIT:
1588 {
1589 struct ip6_pktopts **optp;
1590
1591 #ifdef RFC2292
1592 /* cannot mix with RFC2292 */
1593 if (OPTBIT(IN6P_RFC2292)) {
1594 error = EINVAL;
1595 break;
1596 }
1597 #endif
1598 optp = &in6p->in6p_outputopts;
1599 error = ip6_pcbopt(IPV6_HOPLIMIT,
1600 (u_char *)&optval,
1601 sizeof(optval),
1602 optp,
1603 privileged, uproto);
1604 break;
1605 }
1606
1607 case IPV6_RECVHOPLIMIT:
1608 #ifdef RFC2292
1609 /* cannot mix with RFC2292 */
1610 if (OPTBIT(IN6P_RFC2292)) {
1611 error = EINVAL;
1612 break;
1613 }
1614 #endif
1615 OPTSET(IN6P_HOPLIMIT);
1616 break;
1617
1618 case IPV6_RECVHOPOPTS:
1619 #ifdef RFC2292
1620 /* cannot mix with RFC2292 */
1621 if (OPTBIT(IN6P_RFC2292)) {
1622 error = EINVAL;
1623 break;
1624 }
1625 #endif
1626 OPTSET(IN6P_HOPOPTS);
1627 break;
1628
1629 case IPV6_RECVDSTOPTS:
1630 #ifdef RFC2292
1631 /* cannot mix with RFC2292 */
1632 if (OPTBIT(IN6P_RFC2292)) {
1633 error = EINVAL;
1634 break;
1635 }
1636 #endif
1637 OPTSET(IN6P_DSTOPTS);
1638 break;
1639
1640 case IPV6_RECVRTHDRDSTOPTS:
1641 #ifdef RFC2292
1642 /* cannot mix with RFC2292 */
1643 if (OPTBIT(IN6P_RFC2292)) {
1644 error = EINVAL;
1645 break;
1646 }
1647 #endif
1648 OPTSET(IN6P_RTHDRDSTOPTS);
1649 break;
1650
1651 case IPV6_RECVRTHDR:
1652 #ifdef RFC2292
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1655 error = EINVAL;
1656 break;
1657 }
1658 #endif
1659 OPTSET(IN6P_RTHDR);
1660 break;
1661
1662 case IPV6_FAITH:
1663 OPTSET(IN6P_FAITH);
1664 break;
1665
1666 case IPV6_RECVPATHMTU:
1667 /*
1668 * We ignore this option for TCP
1669 * sockets.
1670 * (RFC3542 leaves this case
1671 * unspecified.)
1672 */
1673 if (uproto != IPPROTO_TCP)
1674 OPTSET(IN6P_MTU);
1675 break;
1676
1677 case IPV6_V6ONLY:
1678 /*
1679 * make setsockopt(IPV6_V6ONLY)
1680 * available only prior to bind(2).
1681 * see ipng mailing list, Jun 22 2001.
1682 */
1683 if (in6p->in6p_lport ||
1684 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1685 error = EINVAL;
1686 break;
1687 }
1688 #ifdef INET6_BINDV6ONLY
1689 if (!optval)
1690 error = EINVAL;
1691 #else
1692 OPTSET(IN6P_IPV6_V6ONLY);
1693 #endif
1694 break;
1695 case IPV6_RECVTCLASS:
1696 #ifdef RFC2292
1697 /* cannot mix with RFC2292 XXX */
1698 if (OPTBIT(IN6P_RFC2292)) {
1699 error = EINVAL;
1700 break;
1701 }
1702 #endif
1703 OPTSET(IN6P_TCLASS);
1704 break;
1705
1706 }
1707 break;
1708
1709 case IPV6_OTCLASS:
1710 {
1711 struct ip6_pktopts **optp;
1712 u_int8_t tclass;
1713
1714 if (optlen != sizeof(tclass)) {
1715 error = EINVAL;
1716 break;
1717 }
1718 tclass = *mtod(m, u_int8_t *);
1719 optp = &in6p->in6p_outputopts;
1720 error = ip6_pcbopt(optname,
1721 (u_char *)&tclass,
1722 sizeof(tclass),
1723 optp,
1724 privileged, uproto);
1725 break;
1726 }
1727
1728 case IPV6_TCLASS:
1729 case IPV6_DONTFRAG:
1730 case IPV6_USE_MIN_MTU:
1731 if (optlen != sizeof(optval)) {
1732 error = EINVAL;
1733 break;
1734 }
1735 optval = *mtod(m, int *);
1736 {
1737 struct ip6_pktopts **optp;
1738 optp = &in6p->in6p_outputopts;
1739 error = ip6_pcbopt(optname,
1740 (u_char *)&optval,
1741 sizeof(optval),
1742 optp,
1743 privileged, uproto);
1744 break;
1745 }
1746
1747 #ifdef RFC2292
1748 case IPV6_2292PKTINFO:
1749 case IPV6_2292HOPLIMIT:
1750 case IPV6_2292HOPOPTS:
1751 case IPV6_2292DSTOPTS:
1752 case IPV6_2292RTHDR:
1753 /* RFC 2292 */
1754 if (optlen != sizeof(int)) {
1755 error = EINVAL;
1756 break;
1757 }
1758 optval = *mtod(m, int *);
1759 switch (optname) {
1760 case IPV6_2292PKTINFO:
1761 OPTSET2292(IN6P_PKTINFO);
1762 break;
1763 case IPV6_2292HOPLIMIT:
1764 OPTSET2292(IN6P_HOPLIMIT);
1765 break;
1766 case IPV6_2292HOPOPTS:
1767 /*
1768 * Check super-user privilege.
1769 * See comments for IPV6_RECVHOPOPTS.
1770 */
1771 if (!privileged)
1772 return (EPERM);
1773 OPTSET2292(IN6P_HOPOPTS);
1774 break;
1775 case IPV6_2292DSTOPTS:
1776 if (!privileged)
1777 return (EPERM);
1778 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1779 break;
1780 case IPV6_2292RTHDR:
1781 OPTSET2292(IN6P_RTHDR);
1782 break;
1783 }
1784 break;
1785 #endif
1786 case IPV6_PKTINFO:
1787 case IPV6_HOPOPTS:
1788 case IPV6_RTHDR:
1789 case IPV6_DSTOPTS:
1790 case IPV6_RTHDRDSTOPTS:
1791 case IPV6_NEXTHOP:
1792 {
1793 /* new advanced API (RFC3542) */
1794 u_char *optbuf;
1795 int optbuflen;
1796 struct ip6_pktopts **optp;
1797 if (!m) {
1798 error = EINVAL;
1799 break;
1800 }
1801
1802 #ifdef RFC2292
1803 /* cannot mix with RFC2292 */
1804 if (OPTBIT(IN6P_RFC2292)) {
1805 error = EINVAL;
1806 break;
1807 }
1808 #endif
1809
1810 if (m && m->m_next) {
1811 error = EINVAL; /* XXX */
1812 break;
1813 }
1814
1815 optbuf = mtod(m, u_char *);
1816 optbuflen = m->m_len;
1817 optp = &in6p->in6p_outputopts;
1818 error = ip6_pcbopt(optname, optbuf, optbuflen,
1819 optp, privileged, uproto);
1820 break;
1821 }
1822 #undef OPTSET
1823
1824 case IPV6_MULTICAST_IF:
1825 case IPV6_MULTICAST_HOPS:
1826 case IPV6_MULTICAST_LOOP:
1827 case IPV6_JOIN_GROUP:
1828 case IPV6_LEAVE_GROUP:
1829 error = ip6_setmoptions(optname,
1830 &in6p->in6p_moptions, m);
1831 break;
1832
1833 case IPV6_PORTRANGE:
1834 if (!m) {
1835 error = EINVAL;
1836 break;
1837 }
1838 optval = *mtod(m, int *);
1839
1840 switch (optval) {
1841 case IPV6_PORTRANGE_DEFAULT:
1842 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1843 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1844 break;
1845
1846 case IPV6_PORTRANGE_HIGH:
1847 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1848 in6p->in6p_flags |= IN6P_HIGHPORT;
1849 break;
1850
1851 case IPV6_PORTRANGE_LOW:
1852 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1853 in6p->in6p_flags |= IN6P_LOWPORT;
1854 break;
1855
1856 default:
1857 error = EINVAL;
1858 break;
1859 }
1860 break;
1861
1862
1863 #if defined(IPSEC) || defined(FAST_IPSEC)
1864 case IPV6_IPSEC_POLICY:
1865 {
1866 void *req = NULL;
1867 size_t len = 0;
1868 if (m) {
1869 req = mtod(m, void *);
1870 len = m->m_len;
1871 }
1872 error = ipsec6_set_policy(in6p, optname, req,
1873 len, privileged);
1874 }
1875 break;
1876 #endif /* IPSEC */
1877
1878 default:
1879 error = ENOPROTOOPT;
1880 break;
1881 }
1882 if (m)
1883 (void)m_free(m);
1884 break;
1885
1886 case PRCO_GETOPT:
1887 switch (optname) {
1888 #ifdef RFC2292
1889 case IPV6_2292PKTOPTIONS:
1890 /*
1891 * RFC3542 (effectively) deprecated the
1892 * semantics of the 2292-style pktoptions.
1893 * Since it was not reliable in nature (i.e.,
1894 * applications had to expect the lack of some
1895 * information after all), it would make sense
1896 * to simplify this part by always returning
1897 * empty data.
1898 */
1899 *mp = m_get(M_WAIT, MT_SOOPTS);
1900 (*mp)->m_len = 0;
1901 break;
1902 #endif
1903
1904 case IPV6_RECVHOPOPTS:
1905 case IPV6_RECVDSTOPTS:
1906 case IPV6_RECVRTHDRDSTOPTS:
1907 case IPV6_UNICAST_HOPS:
1908 case IPV6_RECVPKTINFO:
1909 case IPV6_RECVHOPLIMIT:
1910 case IPV6_RECVRTHDR:
1911 case IPV6_RECVPATHMTU:
1912
1913 case IPV6_FAITH:
1914 case IPV6_V6ONLY:
1915 case IPV6_PORTRANGE:
1916 case IPV6_RECVTCLASS:
1917 switch (optname) {
1918
1919 case IPV6_RECVHOPOPTS:
1920 optval = OPTBIT(IN6P_HOPOPTS);
1921 break;
1922
1923 case IPV6_RECVDSTOPTS:
1924 optval = OPTBIT(IN6P_DSTOPTS);
1925 break;
1926
1927 case IPV6_RECVRTHDRDSTOPTS:
1928 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1929 break;
1930
1931 case IPV6_UNICAST_HOPS:
1932 optval = in6p->in6p_hops;
1933 break;
1934
1935 case IPV6_RECVPKTINFO:
1936 optval = OPTBIT(IN6P_PKTINFO);
1937 break;
1938
1939 case IPV6_RECVHOPLIMIT:
1940 optval = OPTBIT(IN6P_HOPLIMIT);
1941 break;
1942
1943 case IPV6_RECVRTHDR:
1944 optval = OPTBIT(IN6P_RTHDR);
1945 break;
1946
1947 case IPV6_RECVPATHMTU:
1948 optval = OPTBIT(IN6P_MTU);
1949 break;
1950
1951 case IPV6_FAITH:
1952 optval = OPTBIT(IN6P_FAITH);
1953 break;
1954
1955 case IPV6_V6ONLY:
1956 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1957 break;
1958
1959 case IPV6_PORTRANGE:
1960 {
1961 int flags;
1962 flags = in6p->in6p_flags;
1963 if (flags & IN6P_HIGHPORT)
1964 optval = IPV6_PORTRANGE_HIGH;
1965 else if (flags & IN6P_LOWPORT)
1966 optval = IPV6_PORTRANGE_LOW;
1967 else
1968 optval = 0;
1969 break;
1970 }
1971 case IPV6_RECVTCLASS:
1972 optval = OPTBIT(IN6P_TCLASS);
1973 break;
1974
1975 }
1976 if (error)
1977 break;
1978 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1979 m->m_len = sizeof(int);
1980 *mtod(m, int *) = optval;
1981 break;
1982
1983 case IPV6_PATHMTU:
1984 {
1985 u_long pmtu = 0;
1986 struct ip6_mtuinfo mtuinfo;
1987 struct route *ro = &in6p->in6p_route;
1988
1989 if (!(so->so_state & SS_ISCONNECTED))
1990 return (ENOTCONN);
1991 /*
1992 * XXX: we dot not consider the case of source
1993 * routing, or optional information to specify
1994 * the outgoing interface.
1995 */
1996 error = ip6_getpmtu(ro, NULL, NULL,
1997 &in6p->in6p_faddr, &pmtu, NULL);
1998 if (error)
1999 break;
2000 if (pmtu > IPV6_MAXPACKET)
2001 pmtu = IPV6_MAXPACKET;
2002
2003 memset(&mtuinfo, 0, sizeof(mtuinfo));
2004 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2005 optdata = (void *)&mtuinfo;
2006 optdatalen = sizeof(mtuinfo);
2007 if (optdatalen > MCLBYTES)
2008 return (EMSGSIZE); /* XXX */
2009 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2010 if (optdatalen > MLEN)
2011 MCLGET(m, M_WAIT);
2012 m->m_len = optdatalen;
2013 memcpy(mtod(m, void *), optdata, optdatalen);
2014 break;
2015 }
2016
2017 #ifdef RFC2292
2018 case IPV6_2292PKTINFO:
2019 case IPV6_2292HOPLIMIT:
2020 case IPV6_2292HOPOPTS:
2021 case IPV6_2292RTHDR:
2022 case IPV6_2292DSTOPTS:
2023 switch (optname) {
2024 case IPV6_2292PKTINFO:
2025 optval = OPTBIT(IN6P_PKTINFO);
2026 break;
2027 case IPV6_2292HOPLIMIT:
2028 optval = OPTBIT(IN6P_HOPLIMIT);
2029 break;
2030 case IPV6_2292HOPOPTS:
2031 optval = OPTBIT(IN6P_HOPOPTS);
2032 break;
2033 case IPV6_2292RTHDR:
2034 optval = OPTBIT(IN6P_RTHDR);
2035 break;
2036 case IPV6_2292DSTOPTS:
2037 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2038 break;
2039 }
2040 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2041 m->m_len = sizeof(int);
2042 *mtod(m, int *) = optval;
2043 break;
2044 #endif
2045 case IPV6_PKTINFO:
2046 case IPV6_HOPOPTS:
2047 case IPV6_RTHDR:
2048 case IPV6_DSTOPTS:
2049 case IPV6_RTHDRDSTOPTS:
2050 case IPV6_NEXTHOP:
2051 case IPV6_OTCLASS:
2052 case IPV6_TCLASS:
2053 case IPV6_DONTFRAG:
2054 case IPV6_USE_MIN_MTU:
2055 error = ip6_getpcbopt(in6p->in6p_outputopts,
2056 optname, mp);
2057 break;
2058
2059 case IPV6_MULTICAST_IF:
2060 case IPV6_MULTICAST_HOPS:
2061 case IPV6_MULTICAST_LOOP:
2062 case IPV6_JOIN_GROUP:
2063 case IPV6_LEAVE_GROUP:
2064 error = ip6_getmoptions(optname,
2065 in6p->in6p_moptions, mp);
2066 break;
2067
2068 #if defined(IPSEC) || defined(FAST_IPSEC)
2069 case IPV6_IPSEC_POLICY:
2070 {
2071 void *req = NULL;
2072 size_t len = 0;
2073 if (m) {
2074 req = mtod(m, void *);
2075 len = m->m_len;
2076 }
2077 error = ipsec6_get_policy(in6p, req, len, mp);
2078 break;
2079 }
2080 #endif /* IPSEC */
2081
2082
2083
2084
2085 default:
2086 error = ENOPROTOOPT;
2087 break;
2088 }
2089 break;
2090 }
2091 } else {
2092 error = EINVAL;
2093 if (op == PRCO_SETOPT && *mp)
2094 (void)m_free(*mp);
2095 }
2096 return (error);
2097 }
2098
2099 int
2100 ip6_raw_ctloutput(int op, struct socket *so, int level, int optname,
2101 struct mbuf **mp)
2102 {
2103 int error = 0, optval, optlen;
2104 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2105 struct in6pcb *in6p = sotoin6pcb(so);
2106 struct mbuf *m = *mp;
2107
2108 optlen = m ? m->m_len : 0;
2109
2110 if (level != IPPROTO_IPV6) {
2111 if (op == PRCO_SETOPT && *mp)
2112 (void)m_free(*mp);
2113 return (EINVAL);
2114 }
2115
2116 switch (optname) {
2117 case IPV6_CHECKSUM:
2118 /*
2119 * For ICMPv6 sockets, no modification allowed for checksum
2120 * offset, permit "no change" values to help existing apps.
2121 *
2122 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
2123 * for an ICMPv6 socket will fail." The current
2124 * behavior does not meet RFC3542.
2125 */
2126 switch (op) {
2127 case PRCO_SETOPT:
2128 if (optlen != sizeof(int)) {
2129 error = EINVAL;
2130 break;
2131 }
2132 optval = *mtod(m, int *);
2133 if ((optval % 2) != 0) {
2134 /* the API assumes even offset values */
2135 error = EINVAL;
2136 } else if (so->so_proto->pr_protocol ==
2137 IPPROTO_ICMPV6) {
2138 if (optval != icmp6off)
2139 error = EINVAL;
2140 } else
2141 in6p->in6p_cksum = optval;
2142 break;
2143
2144 case PRCO_GETOPT:
2145 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2146 optval = icmp6off;
2147 else
2148 optval = in6p->in6p_cksum;
2149
2150 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2151 m->m_len = sizeof(int);
2152 *mtod(m, int *) = optval;
2153 break;
2154
2155 default:
2156 error = EINVAL;
2157 break;
2158 }
2159 break;
2160
2161 default:
2162 error = ENOPROTOOPT;
2163 break;
2164 }
2165
2166 if (op == PRCO_SETOPT && m)
2167 (void)m_free(m);
2168
2169 return (error);
2170 }
2171
2172 #ifdef RFC2292
2173 /*
2174 * Set up IP6 options in pcb for insertion in output packets or
2175 * specifying behavior of outgoing packets.
2176 */
2177 static int
2178 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so)
2179 {
2180 struct ip6_pktopts *opt = *pktopt;
2181 int error = 0;
2182 struct lwp *l = curlwp; /* XXX */
2183 int priv = 0;
2184
2185 /* turn off any old options. */
2186 if (opt) {
2187 #ifdef DIAGNOSTIC
2188 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2189 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2190 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2191 printf("ip6_pcbopts: all specified options are cleared.\n");
2192 #endif
2193 ip6_clearpktopts(opt, -1);
2194 } else
2195 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2196 *pktopt = NULL;
2197
2198 if (!m || m->m_len == 0) {
2199 /*
2200 * Only turning off any previous options, regardless of
2201 * whether the opt is just created or given.
2202 */
2203 free(opt, M_IP6OPT);
2204 return (0);
2205 }
2206
2207 /* set options specified by user. */
2208 if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2209 NULL))
2210 priv = 1;
2211 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2212 so->so_proto->pr_protocol)) != 0) {
2213 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2214 free(opt, M_IP6OPT);
2215 return (error);
2216 }
2217 *pktopt = opt;
2218 return (0);
2219 }
2220 #endif
2221
2222 /*
2223 * initialize ip6_pktopts. beware that there are non-zero default values in
2224 * the struct.
2225 */
2226 void
2227 ip6_initpktopts(struct ip6_pktopts *opt)
2228 {
2229
2230 memset(opt, 0, sizeof(*opt));
2231 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2232 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2233 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2234 }
2235
2236 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2237 static int
2238 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2239 int priv, int uproto)
2240 {
2241 struct ip6_pktopts *opt;
2242
2243 if (*pktopt == NULL) {
2244 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2245 M_WAITOK);
2246 ip6_initpktopts(*pktopt);
2247 }
2248 opt = *pktopt;
2249
2250 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2251 }
2252
2253 static int
2254 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
2255 {
2256 void *optdata = NULL;
2257 int optdatalen = 0;
2258 struct ip6_ext *ip6e;
2259 int error = 0;
2260 struct in6_pktinfo null_pktinfo;
2261 int deftclass = 0, on;
2262 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2263 struct mbuf *m;
2264
2265 switch (optname) {
2266 case IPV6_PKTINFO:
2267 if (pktopt && pktopt->ip6po_pktinfo)
2268 optdata = (void *)pktopt->ip6po_pktinfo;
2269 else {
2270 /* XXX: we don't have to do this every time... */
2271 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2272 optdata = (void *)&null_pktinfo;
2273 }
2274 optdatalen = sizeof(struct in6_pktinfo);
2275 break;
2276 case IPV6_OTCLASS:
2277 /* XXX */
2278 return (EINVAL);
2279 case IPV6_TCLASS:
2280 if (pktopt && pktopt->ip6po_tclass >= 0)
2281 optdata = (void *)&pktopt->ip6po_tclass;
2282 else
2283 optdata = (void *)&deftclass;
2284 optdatalen = sizeof(int);
2285 break;
2286 case IPV6_HOPOPTS:
2287 if (pktopt && pktopt->ip6po_hbh) {
2288 optdata = (void *)pktopt->ip6po_hbh;
2289 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2290 optdatalen = (ip6e->ip6e_len + 1) << 3;
2291 }
2292 break;
2293 case IPV6_RTHDR:
2294 if (pktopt && pktopt->ip6po_rthdr) {
2295 optdata = (void *)pktopt->ip6po_rthdr;
2296 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2297 optdatalen = (ip6e->ip6e_len + 1) << 3;
2298 }
2299 break;
2300 case IPV6_RTHDRDSTOPTS:
2301 if (pktopt && pktopt->ip6po_dest1) {
2302 optdata = (void *)pktopt->ip6po_dest1;
2303 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2304 optdatalen = (ip6e->ip6e_len + 1) << 3;
2305 }
2306 break;
2307 case IPV6_DSTOPTS:
2308 if (pktopt && pktopt->ip6po_dest2) {
2309 optdata = (void *)pktopt->ip6po_dest2;
2310 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2311 optdatalen = (ip6e->ip6e_len + 1) << 3;
2312 }
2313 break;
2314 case IPV6_NEXTHOP:
2315 if (pktopt && pktopt->ip6po_nexthop) {
2316 optdata = (void *)pktopt->ip6po_nexthop;
2317 optdatalen = pktopt->ip6po_nexthop->sa_len;
2318 }
2319 break;
2320 case IPV6_USE_MIN_MTU:
2321 if (pktopt)
2322 optdata = (void *)&pktopt->ip6po_minmtu;
2323 else
2324 optdata = (void *)&defminmtu;
2325 optdatalen = sizeof(int);
2326 break;
2327 case IPV6_DONTFRAG:
2328 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2329 on = 1;
2330 else
2331 on = 0;
2332 optdata = (void *)&on;
2333 optdatalen = sizeof(on);
2334 break;
2335 default: /* should not happen */
2336 #ifdef DIAGNOSTIC
2337 panic("ip6_getpcbopt: unexpected option\n");
2338 #endif
2339 return (ENOPROTOOPT);
2340 }
2341
2342 if (optdatalen > MCLBYTES)
2343 return (EMSGSIZE); /* XXX */
2344 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2345 if (optdatalen > MLEN)
2346 MCLGET(m, M_WAIT);
2347 m->m_len = optdatalen;
2348 if (optdatalen)
2349 memcpy(mtod(m, void *), optdata, optdatalen);
2350
2351 return (error);
2352 }
2353
2354 void
2355 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2356 {
2357 if (optname == -1 || optname == IPV6_PKTINFO) {
2358 if (pktopt->ip6po_pktinfo)
2359 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2360 pktopt->ip6po_pktinfo = NULL;
2361 }
2362 if (optname == -1 || optname == IPV6_HOPLIMIT)
2363 pktopt->ip6po_hlim = -1;
2364 if (optname == -1 || optname == IPV6_TCLASS)
2365 pktopt->ip6po_tclass = -1;
2366 if (optname == -1 || optname == IPV6_NEXTHOP) {
2367 rtcache_free(&pktopt->ip6po_nextroute);
2368 if (pktopt->ip6po_nexthop)
2369 free(pktopt->ip6po_nexthop, M_IP6OPT);
2370 pktopt->ip6po_nexthop = NULL;
2371 }
2372 if (optname == -1 || optname == IPV6_HOPOPTS) {
2373 if (pktopt->ip6po_hbh)
2374 free(pktopt->ip6po_hbh, M_IP6OPT);
2375 pktopt->ip6po_hbh = NULL;
2376 }
2377 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2378 if (pktopt->ip6po_dest1)
2379 free(pktopt->ip6po_dest1, M_IP6OPT);
2380 pktopt->ip6po_dest1 = NULL;
2381 }
2382 if (optname == -1 || optname == IPV6_RTHDR) {
2383 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2384 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2385 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2386 rtcache_free(&pktopt->ip6po_route);
2387 }
2388 if (optname == -1 || optname == IPV6_DSTOPTS) {
2389 if (pktopt->ip6po_dest2)
2390 free(pktopt->ip6po_dest2, M_IP6OPT);
2391 pktopt->ip6po_dest2 = NULL;
2392 }
2393 }
2394
2395 #define PKTOPT_EXTHDRCPY(type) \
2396 do { \
2397 if (src->type) { \
2398 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2399 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2400 if (dst->type == NULL && canwait == M_NOWAIT) \
2401 goto bad; \
2402 memcpy(dst->type, src->type, hlen); \
2403 } \
2404 } while (/*CONSTCOND*/ 0)
2405
2406 static int
2407 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2408 {
2409 dst->ip6po_hlim = src->ip6po_hlim;
2410 dst->ip6po_tclass = src->ip6po_tclass;
2411 dst->ip6po_flags = src->ip6po_flags;
2412 if (src->ip6po_pktinfo) {
2413 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2414 M_IP6OPT, canwait);
2415 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2416 goto bad;
2417 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2418 }
2419 if (src->ip6po_nexthop) {
2420 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2421 M_IP6OPT, canwait);
2422 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2423 goto bad;
2424 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2425 src->ip6po_nexthop->sa_len);
2426 }
2427 PKTOPT_EXTHDRCPY(ip6po_hbh);
2428 PKTOPT_EXTHDRCPY(ip6po_dest1);
2429 PKTOPT_EXTHDRCPY(ip6po_dest2);
2430 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2431 return (0);
2432
2433 bad:
2434 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2435 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2436 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2437 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2438 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2439 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2440
2441 return (ENOBUFS);
2442 }
2443 #undef PKTOPT_EXTHDRCPY
2444
2445 struct ip6_pktopts *
2446 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2447 {
2448 int error;
2449 struct ip6_pktopts *dst;
2450
2451 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2452 if (dst == NULL && canwait == M_NOWAIT)
2453 return (NULL);
2454 ip6_initpktopts(dst);
2455
2456 if ((error = copypktopts(dst, src, canwait)) != 0) {
2457 free(dst, M_IP6OPT);
2458 return (NULL);
2459 }
2460
2461 return (dst);
2462 }
2463
2464 void
2465 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2466 {
2467 if (pktopt == NULL)
2468 return;
2469
2470 ip6_clearpktopts(pktopt, -1);
2471
2472 free(pktopt, M_IP6OPT);
2473 }
2474
2475 /*
2476 * Set the IP6 multicast options in response to user setsockopt().
2477 */
2478 static int
2479 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2480 {
2481 int error = 0;
2482 u_int loop, ifindex;
2483 struct ipv6_mreq *mreq;
2484 struct ifnet *ifp;
2485 struct ip6_moptions *im6o = *im6op;
2486 struct route ro;
2487 struct in6_multi_mship *imm;
2488 struct lwp *l = curlwp; /* XXX */
2489
2490 if (im6o == NULL) {
2491 /*
2492 * No multicast option buffer attached to the pcb;
2493 * allocate one and initialize to default values.
2494 */
2495 im6o = (struct ip6_moptions *)
2496 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2497
2498 if (im6o == NULL)
2499 return (ENOBUFS);
2500 *im6op = im6o;
2501 im6o->im6o_multicast_ifp = NULL;
2502 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2503 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2504 LIST_INIT(&im6o->im6o_memberships);
2505 }
2506
2507 switch (optname) {
2508
2509 case IPV6_MULTICAST_IF:
2510 /*
2511 * Select the interface for outgoing multicast packets.
2512 */
2513 if (m == NULL || m->m_len != sizeof(u_int)) {
2514 error = EINVAL;
2515 break;
2516 }
2517 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2518 if (ifindex != 0) {
2519 if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
2520 error = ENXIO; /* XXX EINVAL? */
2521 break;
2522 }
2523 ifp = ifindex2ifnet[ifindex];
2524 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2525 error = EADDRNOTAVAIL;
2526 break;
2527 }
2528 } else
2529 ifp = NULL;
2530 im6o->im6o_multicast_ifp = ifp;
2531 break;
2532
2533 case IPV6_MULTICAST_HOPS:
2534 {
2535 /*
2536 * Set the IP6 hoplimit for outgoing multicast packets.
2537 */
2538 int optval;
2539 if (m == NULL || m->m_len != sizeof(int)) {
2540 error = EINVAL;
2541 break;
2542 }
2543 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2544 if (optval < -1 || optval >= 256)
2545 error = EINVAL;
2546 else if (optval == -1)
2547 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2548 else
2549 im6o->im6o_multicast_hlim = optval;
2550 break;
2551 }
2552
2553 case IPV6_MULTICAST_LOOP:
2554 /*
2555 * Set the loopback flag for outgoing multicast packets.
2556 * Must be zero or one.
2557 */
2558 if (m == NULL || m->m_len != sizeof(u_int)) {
2559 error = EINVAL;
2560 break;
2561 }
2562 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2563 if (loop > 1) {
2564 error = EINVAL;
2565 break;
2566 }
2567 im6o->im6o_multicast_loop = loop;
2568 break;
2569
2570 case IPV6_JOIN_GROUP:
2571 /*
2572 * Add a multicast group membership.
2573 * Group must be a valid IP6 multicast address.
2574 */
2575 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2576 error = EINVAL;
2577 break;
2578 }
2579 mreq = mtod(m, struct ipv6_mreq *);
2580 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2581 /*
2582 * We use the unspecified address to specify to accept
2583 * all multicast addresses. Only super user is allowed
2584 * to do this.
2585 */
2586 if (kauth_authorize_generic(l->l_cred,
2587 KAUTH_GENERIC_ISSUSER, NULL))
2588 {
2589 error = EACCES;
2590 break;
2591 }
2592 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2593 error = EINVAL;
2594 break;
2595 }
2596
2597 /*
2598 * If no interface was explicitly specified, choose an
2599 * appropriate one according to the given multicast address.
2600 */
2601 if (mreq->ipv6mr_interface == 0) {
2602 union {
2603 struct sockaddr dst;
2604 struct sockaddr_in6 dst6;
2605 } u;
2606
2607 /*
2608 * Look up the routing table for the
2609 * address, and choose the outgoing interface.
2610 * XXX: is it a good approach?
2611 */
2612 memset(&ro, 0, sizeof(ro));
2613 sockaddr_in6_init(&u.dst6, &mreq->ipv6mr_multiaddr, 0,
2614 0, 0);
2615 rtcache_setdst(&ro, &u.dst);
2616 rtcache_init(&ro);
2617 ifp = (ro.ro_rt != NULL) ? ro.ro_rt->rt_ifp : NULL;
2618 rtcache_free(&ro);
2619 } else {
2620 /*
2621 * If the interface is specified, validate it.
2622 */
2623 if (if_indexlim <= mreq->ipv6mr_interface ||
2624 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2625 error = ENXIO; /* XXX EINVAL? */
2626 break;
2627 }
2628 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2629 }
2630
2631 /*
2632 * See if we found an interface, and confirm that it
2633 * supports multicast
2634 */
2635 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2636 error = EADDRNOTAVAIL;
2637 break;
2638 }
2639
2640 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2641 error = EADDRNOTAVAIL; /* XXX: should not happen */
2642 break;
2643 }
2644
2645 /*
2646 * See if the membership already exists.
2647 */
2648 for (imm = im6o->im6o_memberships.lh_first;
2649 imm != NULL; imm = imm->i6mm_chain.le_next)
2650 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2651 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2652 &mreq->ipv6mr_multiaddr))
2653 break;
2654 if (imm != NULL) {
2655 error = EADDRINUSE;
2656 break;
2657 }
2658 /*
2659 * Everything looks good; add a new record to the multicast
2660 * address list for the given interface.
2661 */
2662 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2663 if (imm == NULL)
2664 break;
2665 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2666 break;
2667
2668 case IPV6_LEAVE_GROUP:
2669 /*
2670 * Drop a multicast group membership.
2671 * Group must be a valid IP6 multicast address.
2672 */
2673 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2674 error = EINVAL;
2675 break;
2676 }
2677 mreq = mtod(m, struct ipv6_mreq *);
2678
2679 /*
2680 * If an interface address was specified, get a pointer
2681 * to its ifnet structure.
2682 */
2683 if (mreq->ipv6mr_interface != 0) {
2684 if (if_indexlim <= mreq->ipv6mr_interface ||
2685 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2686 error = ENXIO; /* XXX EINVAL? */
2687 break;
2688 }
2689 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2690 } else
2691 ifp = NULL;
2692
2693 /* Fill in the scope zone ID */
2694 if (ifp) {
2695 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2696 /* XXX: should not happen */
2697 error = EADDRNOTAVAIL;
2698 break;
2699 }
2700 } else if (mreq->ipv6mr_interface != 0) {
2701 /*
2702 * XXX: This case would happens when the (positive)
2703 * index is in the valid range, but the corresponding
2704 * interface has been detached dynamically. The above
2705 * check probably avoids such case to happen here, but
2706 * we check it explicitly for safety.
2707 */
2708 error = EADDRNOTAVAIL;
2709 break;
2710 } else { /* ipv6mr_interface == 0 */
2711 struct sockaddr_in6 sa6_mc;
2712
2713 /*
2714 * The API spec says as follows:
2715 * If the interface index is specified as 0, the
2716 * system may choose a multicast group membership to
2717 * drop by matching the multicast address only.
2718 * On the other hand, we cannot disambiguate the scope
2719 * zone unless an interface is provided. Thus, we
2720 * check if there's ambiguity with the default scope
2721 * zone as the last resort.
2722 */
2723 bzero(&sa6_mc, sizeof(sa6_mc));
2724 sa6_mc.sin6_family = AF_INET6;
2725 sa6_mc.sin6_len = sizeof(sa6_mc);
2726 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2727 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2728 if (error != 0)
2729 break;
2730 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2731 }
2732
2733 /*
2734 * Find the membership in the membership list.
2735 */
2736 for (imm = im6o->im6o_memberships.lh_first;
2737 imm != NULL; imm = imm->i6mm_chain.le_next) {
2738 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2739 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2740 &mreq->ipv6mr_multiaddr))
2741 break;
2742 }
2743 if (imm == NULL) {
2744 /* Unable to resolve interface */
2745 error = EADDRNOTAVAIL;
2746 break;
2747 }
2748 /*
2749 * Give up the multicast address record to which the
2750 * membership points.
2751 */
2752 LIST_REMOVE(imm, i6mm_chain);
2753 in6_leavegroup(imm);
2754 break;
2755
2756 default:
2757 error = EOPNOTSUPP;
2758 break;
2759 }
2760
2761 /*
2762 * If all options have default values, no need to keep the mbuf.
2763 */
2764 if (im6o->im6o_multicast_ifp == NULL &&
2765 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2766 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2767 im6o->im6o_memberships.lh_first == NULL) {
2768 free(*im6op, M_IPMOPTS);
2769 *im6op = NULL;
2770 }
2771
2772 return (error);
2773 }
2774
2775 /*
2776 * Return the IP6 multicast options in response to user getsockopt().
2777 */
2778 static int
2779 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2780 {
2781 u_int *hlim, *loop, *ifindex;
2782
2783 *mp = m_get(M_WAIT, MT_SOOPTS);
2784
2785 switch (optname) {
2786
2787 case IPV6_MULTICAST_IF:
2788 ifindex = mtod(*mp, u_int *);
2789 (*mp)->m_len = sizeof(u_int);
2790 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2791 *ifindex = 0;
2792 else
2793 *ifindex = im6o->im6o_multicast_ifp->if_index;
2794 return (0);
2795
2796 case IPV6_MULTICAST_HOPS:
2797 hlim = mtod(*mp, u_int *);
2798 (*mp)->m_len = sizeof(u_int);
2799 if (im6o == NULL)
2800 *hlim = ip6_defmcasthlim;
2801 else
2802 *hlim = im6o->im6o_multicast_hlim;
2803 return (0);
2804
2805 case IPV6_MULTICAST_LOOP:
2806 loop = mtod(*mp, u_int *);
2807 (*mp)->m_len = sizeof(u_int);
2808 if (im6o == NULL)
2809 *loop = ip6_defmcasthlim;
2810 else
2811 *loop = im6o->im6o_multicast_loop;
2812 return (0);
2813
2814 default:
2815 return (EOPNOTSUPP);
2816 }
2817 }
2818
2819 /*
2820 * Discard the IP6 multicast options.
2821 */
2822 void
2823 ip6_freemoptions(struct ip6_moptions *im6o)
2824 {
2825 struct in6_multi_mship *imm;
2826
2827 if (im6o == NULL)
2828 return;
2829
2830 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2831 LIST_REMOVE(imm, i6mm_chain);
2832 in6_leavegroup(imm);
2833 }
2834 free(im6o, M_IPMOPTS);
2835 }
2836
2837 /*
2838 * Set IPv6 outgoing packet options based on advanced API.
2839 */
2840 int
2841 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2842 struct ip6_pktopts *stickyopt, int priv, int uproto)
2843 {
2844 struct cmsghdr *cm = 0;
2845
2846 if (control == NULL || opt == NULL)
2847 return (EINVAL);
2848
2849 ip6_initpktopts(opt);
2850 if (stickyopt) {
2851 int error;
2852
2853 /*
2854 * If stickyopt is provided, make a local copy of the options
2855 * for this particular packet, then override them by ancillary
2856 * objects.
2857 * XXX: copypktopts() does not copy the cached route to a next
2858 * hop (if any). This is not very good in terms of efficiency,
2859 * but we can allow this since this option should be rarely
2860 * used.
2861 */
2862 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2863 return (error);
2864 }
2865
2866 /*
2867 * XXX: Currently, we assume all the optional information is stored
2868 * in a single mbuf.
2869 */
2870 if (control->m_next)
2871 return (EINVAL);
2872
2873 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2874 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2875 int error;
2876
2877 if (control->m_len < CMSG_LEN(0))
2878 return (EINVAL);
2879
2880 cm = mtod(control, struct cmsghdr *);
2881 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2882 return (EINVAL);
2883 if (cm->cmsg_level != IPPROTO_IPV6)
2884 continue;
2885
2886 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2887 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2888 if (error)
2889 return (error);
2890 }
2891
2892 return (0);
2893 }
2894
2895 /*
2896 * Set a particular packet option, as a sticky option or an ancillary data
2897 * item. "len" can be 0 only when it's a sticky option.
2898 * We have 4 cases of combination of "sticky" and "cmsg":
2899 * "sticky=0, cmsg=0": impossible
2900 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2901 * "sticky=1, cmsg=0": RFC3542 socket option
2902 * "sticky=1, cmsg=1": RFC2292 socket option
2903 */
2904 static int
2905 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2906 int priv, int sticky, int cmsg, int uproto)
2907 {
2908 int minmtupolicy;
2909
2910 if (!sticky && !cmsg) {
2911 #ifdef DIAGNOSTIC
2912 printf("ip6_setpktopt: impossible case\n");
2913 #endif
2914 return (EINVAL);
2915 }
2916
2917 /*
2918 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2919 * not be specified in the context of RFC3542. Conversely,
2920 * RFC3542 types should not be specified in the context of RFC2292.
2921 */
2922 if (!cmsg) {
2923 switch (optname) {
2924 case IPV6_2292PKTINFO:
2925 case IPV6_2292HOPLIMIT:
2926 case IPV6_2292NEXTHOP:
2927 case IPV6_2292HOPOPTS:
2928 case IPV6_2292DSTOPTS:
2929 case IPV6_2292RTHDR:
2930 case IPV6_2292PKTOPTIONS:
2931 return (ENOPROTOOPT);
2932 }
2933 }
2934 if (sticky && cmsg) {
2935 switch (optname) {
2936 case IPV6_PKTINFO:
2937 case IPV6_HOPLIMIT:
2938 case IPV6_NEXTHOP:
2939 case IPV6_HOPOPTS:
2940 case IPV6_DSTOPTS:
2941 case IPV6_RTHDRDSTOPTS:
2942 case IPV6_RTHDR:
2943 case IPV6_USE_MIN_MTU:
2944 case IPV6_DONTFRAG:
2945 case IPV6_OTCLASS:
2946 case IPV6_TCLASS:
2947 return (ENOPROTOOPT);
2948 }
2949 }
2950
2951 switch (optname) {
2952 #ifdef RFC2292
2953 case IPV6_2292PKTINFO:
2954 #endif
2955 case IPV6_PKTINFO:
2956 {
2957 struct ifnet *ifp = NULL;
2958 struct in6_pktinfo *pktinfo;
2959
2960 if (len != sizeof(struct in6_pktinfo))
2961 return (EINVAL);
2962
2963 pktinfo = (struct in6_pktinfo *)buf;
2964
2965 /*
2966 * An application can clear any sticky IPV6_PKTINFO option by
2967 * doing a "regular" setsockopt with ipi6_addr being
2968 * in6addr_any and ipi6_ifindex being zero.
2969 * [RFC 3542, Section 6]
2970 */
2971 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2972 pktinfo->ipi6_ifindex == 0 &&
2973 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2974 ip6_clearpktopts(opt, optname);
2975 break;
2976 }
2977
2978 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2979 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2980 return (EINVAL);
2981 }
2982
2983 /* validate the interface index if specified. */
2984 if (pktinfo->ipi6_ifindex >= if_indexlim) {
2985 return (ENXIO);
2986 }
2987 if (pktinfo->ipi6_ifindex) {
2988 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
2989 if (ifp == NULL)
2990 return (ENXIO);
2991 }
2992
2993 /*
2994 * We store the address anyway, and let in6_selectsrc()
2995 * validate the specified address. This is because ipi6_addr
2996 * may not have enough information about its scope zone, and
2997 * we may need additional information (such as outgoing
2998 * interface or the scope zone of a destination address) to
2999 * disambiguate the scope.
3000 * XXX: the delay of the validation may confuse the
3001 * application when it is used as a sticky option.
3002 */
3003 if (opt->ip6po_pktinfo == NULL) {
3004 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3005 M_IP6OPT, M_NOWAIT);
3006 if (opt->ip6po_pktinfo == NULL)
3007 return (ENOBUFS);
3008 }
3009 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
3010 break;
3011 }
3012
3013 #ifdef RFC2292
3014 case IPV6_2292HOPLIMIT:
3015 #endif
3016 case IPV6_HOPLIMIT:
3017 {
3018 int *hlimp;
3019
3020 /*
3021 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3022 * to simplify the ordering among hoplimit options.
3023 */
3024 if (optname == IPV6_HOPLIMIT && sticky)
3025 return (ENOPROTOOPT);
3026
3027 if (len != sizeof(int))
3028 return (EINVAL);
3029 hlimp = (int *)buf;
3030 if (*hlimp < -1 || *hlimp > 255)
3031 return (EINVAL);
3032
3033 opt->ip6po_hlim = *hlimp;
3034 break;
3035 }
3036
3037 case IPV6_OTCLASS:
3038 if (len != sizeof(u_int8_t))
3039 return (EINVAL);
3040
3041 opt->ip6po_tclass = *(u_int8_t *)buf;
3042 break;
3043
3044 case IPV6_TCLASS:
3045 {
3046 int tclass;
3047
3048 if (len != sizeof(int))
3049 return (EINVAL);
3050 tclass = *(int *)buf;
3051 if (tclass < -1 || tclass > 255)
3052 return (EINVAL);
3053
3054 opt->ip6po_tclass = tclass;
3055 break;
3056 }
3057
3058 #ifdef RFC2292
3059 case IPV6_2292NEXTHOP:
3060 #endif
3061 case IPV6_NEXTHOP:
3062 if (!priv)
3063 return (EPERM);
3064
3065 if (len == 0) { /* just remove the option */
3066 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3067 break;
3068 }
3069
3070 /* check if cmsg_len is large enough for sa_len */
3071 if (len < sizeof(struct sockaddr) || len < *buf)
3072 return (EINVAL);
3073
3074 switch (((struct sockaddr *)buf)->sa_family) {
3075 case AF_INET6:
3076 {
3077 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3078 int error;
3079
3080 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3081 return (EINVAL);
3082
3083 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3084 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3085 return (EINVAL);
3086 }
3087 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3088 != 0) {
3089 return (error);
3090 }
3091 break;
3092 }
3093 case AF_LINK: /* eventually be supported? */
3094 default:
3095 return (EAFNOSUPPORT);
3096 }
3097
3098 /* turn off the previous option, then set the new option. */
3099 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3100 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3101 if (opt->ip6po_nexthop == NULL)
3102 return (ENOBUFS);
3103 memcpy(opt->ip6po_nexthop, buf, *buf);
3104 break;
3105
3106 #ifdef RFC2292
3107 case IPV6_2292HOPOPTS:
3108 #endif
3109 case IPV6_HOPOPTS:
3110 {
3111 struct ip6_hbh *hbh;
3112 int hbhlen;
3113
3114 /*
3115 * XXX: We don't allow a non-privileged user to set ANY HbH
3116 * options, since per-option restriction has too much
3117 * overhead.
3118 */
3119 if (!priv)
3120 return (EPERM);
3121
3122 if (len == 0) {
3123 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3124 break; /* just remove the option */
3125 }
3126
3127 /* message length validation */
3128 if (len < sizeof(struct ip6_hbh))
3129 return (EINVAL);
3130 hbh = (struct ip6_hbh *)buf;
3131 hbhlen = (hbh->ip6h_len + 1) << 3;
3132 if (len != hbhlen)
3133 return (EINVAL);
3134
3135 /* turn off the previous option, then set the new option. */
3136 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3137 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3138 if (opt->ip6po_hbh == NULL)
3139 return (ENOBUFS);
3140 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3141
3142 break;
3143 }
3144
3145 #ifdef RFC2292
3146 case IPV6_2292DSTOPTS:
3147 #endif
3148 case IPV6_DSTOPTS:
3149 case IPV6_RTHDRDSTOPTS:
3150 {
3151 struct ip6_dest *dest, **newdest = NULL;
3152 int destlen;
3153
3154 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3155 return (EPERM);
3156
3157 if (len == 0) {
3158 ip6_clearpktopts(opt, optname);
3159 break; /* just remove the option */
3160 }
3161
3162 /* message length validation */
3163 if (len < sizeof(struct ip6_dest))
3164 return (EINVAL);
3165 dest = (struct ip6_dest *)buf;
3166 destlen = (dest->ip6d_len + 1) << 3;
3167 if (len != destlen)
3168 return (EINVAL);
3169 /*
3170 * Determine the position that the destination options header
3171 * should be inserted; before or after the routing header.
3172 */
3173 switch (optname) {
3174 case IPV6_2292DSTOPTS:
3175 /*
3176 * The old advanced API is ambiguous on this point.
3177 * Our approach is to determine the position based
3178 * according to the existence of a routing header.
3179 * Note, however, that this depends on the order of the
3180 * extension headers in the ancillary data; the 1st
3181 * part of the destination options header must appear
3182 * before the routing header in the ancillary data,
3183 * too.
3184 * RFC3542 solved the ambiguity by introducing
3185 * separate ancillary data or option types.
3186 */
3187 if (opt->ip6po_rthdr == NULL)
3188 newdest = &opt->ip6po_dest1;
3189 else
3190 newdest = &opt->ip6po_dest2;
3191 break;
3192 case IPV6_RTHDRDSTOPTS:
3193 newdest = &opt->ip6po_dest1;
3194 break;
3195 case IPV6_DSTOPTS:
3196 newdest = &opt->ip6po_dest2;
3197 break;
3198 }
3199
3200 /* turn off the previous option, then set the new option. */
3201 ip6_clearpktopts(opt, optname);
3202 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3203 if (*newdest == NULL)
3204 return (ENOBUFS);
3205 memcpy(*newdest, dest, destlen);
3206
3207 break;
3208 }
3209
3210 #ifdef RFC2292
3211 case IPV6_2292RTHDR:
3212 #endif
3213 case IPV6_RTHDR:
3214 {
3215 struct ip6_rthdr *rth;
3216 int rthlen;
3217
3218 if (len == 0) {
3219 ip6_clearpktopts(opt, IPV6_RTHDR);
3220 break; /* just remove the option */
3221 }
3222
3223 /* message length validation */
3224 if (len < sizeof(struct ip6_rthdr))
3225 return (EINVAL);
3226 rth = (struct ip6_rthdr *)buf;
3227 rthlen = (rth->ip6r_len + 1) << 3;
3228 if (len != rthlen)
3229 return (EINVAL);
3230 switch (rth->ip6r_type) {
3231 case IPV6_RTHDR_TYPE_0:
3232 if (rth->ip6r_len == 0) /* must contain one addr */
3233 return (EINVAL);
3234 if (rth->ip6r_len % 2) /* length must be even */
3235 return (EINVAL);
3236 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3237 return (EINVAL);
3238 break;
3239 default:
3240 return (EINVAL); /* not supported */
3241 }
3242 /* turn off the previous option */
3243 ip6_clearpktopts(opt, IPV6_RTHDR);
3244 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3245 if (opt->ip6po_rthdr == NULL)
3246 return (ENOBUFS);
3247 memcpy(opt->ip6po_rthdr, rth, rthlen);
3248 break;
3249 }
3250
3251 case IPV6_USE_MIN_MTU:
3252 if (len != sizeof(int))
3253 return (EINVAL);
3254 minmtupolicy = *(int *)buf;
3255 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3256 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3257 minmtupolicy != IP6PO_MINMTU_ALL) {
3258 return (EINVAL);
3259 }
3260 opt->ip6po_minmtu = minmtupolicy;
3261 break;
3262
3263 case IPV6_DONTFRAG:
3264 if (len != sizeof(int))
3265 return (EINVAL);
3266
3267 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3268 /*
3269 * we ignore this option for TCP sockets.
3270 * (RFC3542 leaves this case unspecified.)
3271 */
3272 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3273 } else
3274 opt->ip6po_flags |= IP6PO_DONTFRAG;
3275 break;
3276
3277 default:
3278 return (ENOPROTOOPT);
3279 } /* end of switch */
3280
3281 return (0);
3282 }
3283
3284 /*
3285 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3286 * packet to the input queue of a specified interface. Note that this
3287 * calls the output routine of the loopback "driver", but with an interface
3288 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3289 */
3290 void
3291 ip6_mloopback(struct ifnet *ifp, struct mbuf *m,
3292 const struct sockaddr_in6 *dst)
3293 {
3294 struct mbuf *copym;
3295 struct ip6_hdr *ip6;
3296
3297 copym = m_copy(m, 0, M_COPYALL);
3298 if (copym == NULL)
3299 return;
3300
3301 /*
3302 * Make sure to deep-copy IPv6 header portion in case the data
3303 * is in an mbuf cluster, so that we can safely override the IPv6
3304 * header portion later.
3305 */
3306 if ((copym->m_flags & M_EXT) != 0 ||
3307 copym->m_len < sizeof(struct ip6_hdr)) {
3308 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3309 if (copym == NULL)
3310 return;
3311 }
3312
3313 #ifdef DIAGNOSTIC
3314 if (copym->m_len < sizeof(*ip6)) {
3315 m_freem(copym);
3316 return;
3317 }
3318 #endif
3319
3320 ip6 = mtod(copym, struct ip6_hdr *);
3321 /*
3322 * clear embedded scope identifiers if necessary.
3323 * in6_clearscope will touch the addresses only when necessary.
3324 */
3325 in6_clearscope(&ip6->ip6_src);
3326 in6_clearscope(&ip6->ip6_dst);
3327
3328 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
3329 }
3330
3331 /*
3332 * Chop IPv6 header off from the payload.
3333 */
3334 static int
3335 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3336 {
3337 struct mbuf *mh;
3338 struct ip6_hdr *ip6;
3339
3340 ip6 = mtod(m, struct ip6_hdr *);
3341 if (m->m_len > sizeof(*ip6)) {
3342 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3343 if (mh == 0) {
3344 m_freem(m);
3345 return ENOBUFS;
3346 }
3347 M_MOVE_PKTHDR(mh, m);
3348 MH_ALIGN(mh, sizeof(*ip6));
3349 m->m_len -= sizeof(*ip6);
3350 m->m_data += sizeof(*ip6);
3351 mh->m_next = m;
3352 m = mh;
3353 m->m_len = sizeof(*ip6);
3354 bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6));
3355 }
3356 exthdrs->ip6e_ip6 = m;
3357 return 0;
3358 }
3359
3360 /*
3361 * Compute IPv6 extension header length.
3362 */
3363 int
3364 ip6_optlen(struct in6pcb *in6p)
3365 {
3366 int len;
3367
3368 if (!in6p->in6p_outputopts)
3369 return 0;
3370
3371 len = 0;
3372 #define elen(x) \
3373 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3374
3375 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3376 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3377 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3378 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3379 return len;
3380 #undef elen
3381 }
3382