ip6_output.c revision 1.114.2.3 1 /* $NetBSD: ip6_output.c,v 1.114.2.3 2007/05/07 10:56:04 yamt 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.114.2.3 2007/05/07 10:56:04 yamt 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 switch (error) {
686 case EHOSTUNREACH:
687 ip6stat.ip6s_noroute++;
688 break;
689 case EADDRNOTAVAIL:
690 default:
691 break; /* XXX statistics? */
692 }
693 if (ifp != NULL)
694 in6_ifstat_inc(ifp, ifs6_out_discard);
695 goto bad;
696 }
697 if (rt == NULL) {
698 /*
699 * If in6_selectroute() does not return a route entry,
700 * dst may not have been updated.
701 */
702 rtcache_setdst(ro, sin6tosa(&dst_sa));
703 }
704
705 /*
706 * then rt (for unicast) and ifp must be non-NULL valid values.
707 */
708 if ((flags & IPV6_FORWARDING) == 0) {
709 /* XXX: the FORWARDING flag can be set for mrouting. */
710 in6_ifstat_inc(ifp, ifs6_out_request);
711 }
712 if (rt != NULL) {
713 ia = (struct in6_ifaddr *)(rt->rt_ifa);
714 rt->rt_use++;
715 }
716
717 /*
718 * The outgoing interface must be in the zone of source and
719 * destination addresses. We should use ia_ifp to support the
720 * case of sending packets to an address of our own.
721 */
722 if (ia != NULL && ia->ia_ifp)
723 origifp = ia->ia_ifp;
724 else
725 origifp = ifp;
726
727 src0 = ip6->ip6_src;
728 if (in6_setscope(&src0, origifp, &zone))
729 goto badscope;
730 bzero(&src_sa, sizeof(src_sa));
731 src_sa.sin6_family = AF_INET6;
732 src_sa.sin6_len = sizeof(src_sa);
733 src_sa.sin6_addr = ip6->ip6_src;
734 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
735 goto badscope;
736
737 dst0 = ip6->ip6_dst;
738 if (in6_setscope(&dst0, origifp, &zone))
739 goto badscope;
740 /* re-initialize to be sure */
741 bzero(&dst_sa, sizeof(dst_sa));
742 dst_sa.sin6_family = AF_INET6;
743 dst_sa.sin6_len = sizeof(dst_sa);
744 dst_sa.sin6_addr = ip6->ip6_dst;
745 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id)
746 goto badscope;
747
748 /* scope check is done. */
749
750 if (rt == NULL || IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
751 if (dst == NULL)
752 dst = satocsin6(rtcache_getdst(ro));
753 KASSERT(dst != NULL);
754 } else if (opt && opt->ip6po_nextroute.ro_rt != NULL) {
755 /*
756 * The nexthop is explicitly specified by the
757 * application. We assume the next hop is an IPv6
758 * address.
759 */
760 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
761 } else if ((rt->rt_flags & RTF_GATEWAY))
762 dst = (struct sockaddr_in6 *)rt->rt_gateway;
763 else if (dst == NULL)
764 dst = satocsin6(rtcache_getdst(ro));
765
766 /*
767 * XXXXXX: original code follows:
768 */
769 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
770 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
771 else {
772 struct in6_multi *in6m;
773
774 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
775
776 in6_ifstat_inc(ifp, ifs6_out_mcast);
777
778 /*
779 * Confirm that the outgoing interface supports multicast.
780 */
781 if (!(ifp->if_flags & IFF_MULTICAST)) {
782 ip6stat.ip6s_noroute++;
783 in6_ifstat_inc(ifp, ifs6_out_discard);
784 error = ENETUNREACH;
785 goto bad;
786 }
787
788 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
789 if (in6m != NULL &&
790 (im6o == NULL || im6o->im6o_multicast_loop)) {
791 /*
792 * If we belong to the destination multicast group
793 * on the outgoing interface, and the caller did not
794 * forbid loopback, loop back a copy.
795 */
796 KASSERT(dst != NULL);
797 ip6_mloopback(ifp, m, dst);
798 } else {
799 /*
800 * If we are acting as a multicast router, perform
801 * multicast forwarding as if the packet had just
802 * arrived on the interface to which we are about
803 * to send. The multicast forwarding function
804 * recursively calls this function, using the
805 * IPV6_FORWARDING flag to prevent infinite recursion.
806 *
807 * Multicasts that are looped back by ip6_mloopback(),
808 * above, will be forwarded by the ip6_input() routine,
809 * if necessary.
810 */
811 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
812 if (ip6_mforward(ip6, ifp, m) != 0) {
813 m_freem(m);
814 goto done;
815 }
816 }
817 }
818 /*
819 * Multicasts with a hoplimit of zero may be looped back,
820 * above, but must not be transmitted on a network.
821 * Also, multicasts addressed to the loopback interface
822 * are not sent -- the above call to ip6_mloopback() will
823 * loop back a copy if this host actually belongs to the
824 * destination group on the loopback interface.
825 */
826 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
827 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
828 m_freem(m);
829 goto done;
830 }
831 }
832
833 /*
834 * Fill the outgoing inteface to tell the upper layer
835 * to increment per-interface statistics.
836 */
837 if (ifpp)
838 *ifpp = ifp;
839
840 /* Determine path MTU. */
841 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
842 &alwaysfrag)) != 0)
843 goto bad;
844 #ifdef IPSEC
845 if (needipsectun)
846 mtu = IPV6_MMTU;
847 #endif
848
849 /*
850 * The caller of this function may specify to use the minimum MTU
851 * in some cases.
852 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
853 * setting. The logic is a bit complicated; by default, unicast
854 * packets will follow path MTU while multicast packets will be sent at
855 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
856 * including unicast ones will be sent at the minimum MTU. Multicast
857 * packets will always be sent at the minimum MTU unless
858 * IP6PO_MINMTU_DISABLE is explicitly specified.
859 * See RFC 3542 for more details.
860 */
861 if (mtu > IPV6_MMTU) {
862 if ((flags & IPV6_MINMTU))
863 mtu = IPV6_MMTU;
864 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
865 mtu = IPV6_MMTU;
866 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
867 (opt == NULL ||
868 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
869 mtu = IPV6_MMTU;
870 }
871 }
872
873 /*
874 * clear embedded scope identifiers if necessary.
875 * in6_clearscope will touch the addresses only when necessary.
876 */
877 in6_clearscope(&ip6->ip6_src);
878 in6_clearscope(&ip6->ip6_dst);
879
880 /*
881 * If the outgoing packet contains a hop-by-hop options header,
882 * it must be examined and processed even by the source node.
883 * (RFC 2460, section 4.)
884 */
885 if (exthdrs.ip6e_hbh) {
886 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
887 u_int32_t dummy1; /* XXX unused */
888 u_int32_t dummy2; /* XXX unused */
889
890 /*
891 * XXX: if we have to send an ICMPv6 error to the sender,
892 * we need the M_LOOP flag since icmp6_error() expects
893 * the IPv6 and the hop-by-hop options header are
894 * continuous unless the flag is set.
895 */
896 m->m_flags |= M_LOOP;
897 m->m_pkthdr.rcvif = ifp;
898 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
899 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
900 &dummy1, &dummy2) < 0) {
901 /* m was already freed at this point */
902 error = EINVAL;/* better error? */
903 goto done;
904 }
905 m->m_flags &= ~M_LOOP; /* XXX */
906 m->m_pkthdr.rcvif = NULL;
907 }
908
909 #ifdef PFIL_HOOKS
910 /*
911 * Run through list of hooks for output packets.
912 */
913 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
914 goto done;
915 if (m == NULL)
916 goto done;
917 ip6 = mtod(m, struct ip6_hdr *);
918 #endif /* PFIL_HOOKS */
919 /*
920 * Send the packet to the outgoing interface.
921 * If necessary, do IPv6 fragmentation before sending.
922 *
923 * the logic here is rather complex:
924 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
925 * 1-a: send as is if tlen <= path mtu
926 * 1-b: fragment if tlen > path mtu
927 *
928 * 2: if user asks us not to fragment (dontfrag == 1)
929 * 2-a: send as is if tlen <= interface mtu
930 * 2-b: error if tlen > interface mtu
931 *
932 * 3: if we always need to attach fragment header (alwaysfrag == 1)
933 * always fragment
934 *
935 * 4: if dontfrag == 1 && alwaysfrag == 1
936 * error, as we cannot handle this conflicting request
937 */
938 tlen = m->m_pkthdr.len;
939 tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0;
940 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
941 dontfrag = 1;
942 else
943 dontfrag = 0;
944
945 if (dontfrag && alwaysfrag) { /* case 4 */
946 /* conflicting request - can't transmit */
947 error = EMSGSIZE;
948 goto bad;
949 }
950 if (dontfrag && (!tso && tlen > IN6_LINKMTU(ifp))) { /* case 2-b */
951 /*
952 * Even if the DONTFRAG option is specified, we cannot send the
953 * packet when the data length is larger than the MTU of the
954 * outgoing interface.
955 * Notify the error by sending IPV6_PATHMTU ancillary data as
956 * well as returning an error code (the latter is not described
957 * in the API spec.)
958 */
959 u_int32_t mtu32;
960 struct ip6ctlparam ip6cp;
961
962 mtu32 = (u_int32_t)mtu;
963 bzero(&ip6cp, sizeof(ip6cp));
964 ip6cp.ip6c_cmdarg = (void *)&mtu32;
965 pfctlinput2(PRC_MSGSIZE,
966 rtcache_getdst(ro_pmtu), &ip6cp);
967
968 error = EMSGSIZE;
969 goto bad;
970 }
971
972 /*
973 * transmit packet without fragmentation
974 */
975 if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) {
976 /* case 1-a and 2-a */
977 struct in6_ifaddr *ia6;
978 int sw_csum;
979
980 ip6 = mtod(m, struct ip6_hdr *);
981 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
982 if (ia6) {
983 /* Record statistics for this interface address. */
984 ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
985 }
986 #ifdef IPSEC
987 /* clean ipsec history once it goes out of the node */
988 ipsec_delaux(m);
989 #endif
990
991 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
992 if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
993 if (IN6_NEED_CHECKSUM(ifp,
994 sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
995 in6_delayed_cksum(m);
996 }
997 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
998 }
999
1000 KASSERT(dst != NULL);
1001 if (__predict_true(!tso ||
1002 (ifp->if_capenable & IFCAP_TSOv6) != 0)) {
1003 error = nd6_output(ifp, origifp, m, dst, rt);
1004 } else {
1005 error = ip6_tso_output(ifp, origifp, m, dst, rt);
1006 }
1007 goto done;
1008 }
1009
1010 if (tso) {
1011 error = EINVAL; /* XXX */
1012 goto bad;
1013 }
1014
1015 /*
1016 * try to fragment the packet. case 1-b and 3
1017 */
1018 if (mtu < IPV6_MMTU) {
1019 /* path MTU cannot be less than IPV6_MMTU */
1020 error = EMSGSIZE;
1021 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1022 goto bad;
1023 } else if (ip6->ip6_plen == 0) {
1024 /* jumbo payload cannot be fragmented */
1025 error = EMSGSIZE;
1026 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1027 goto bad;
1028 } else {
1029 struct mbuf **mnext, *m_frgpart;
1030 struct ip6_frag *ip6f;
1031 u_int32_t id = htonl(ip6_randomid());
1032 u_char nextproto;
1033 #if 0 /* see below */
1034 struct ip6ctlparam ip6cp;
1035 u_int32_t mtu32;
1036 #endif
1037
1038 /*
1039 * Too large for the destination or interface;
1040 * fragment if possible.
1041 * Must be able to put at least 8 bytes per fragment.
1042 */
1043 hlen = unfragpartlen;
1044 if (mtu > IPV6_MAXPACKET)
1045 mtu = IPV6_MAXPACKET;
1046
1047 #if 0
1048 /*
1049 * It is believed this code is a leftover from the
1050 * development of the IPV6_RECVPATHMTU sockopt and
1051 * associated work to implement RFC3542.
1052 * It's not entirely clear what the intent of the API
1053 * is at this point, so disable this code for now.
1054 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1055 * will send notifications if the application requests.
1056 */
1057
1058 /* Notify a proper path MTU to applications. */
1059 mtu32 = (u_int32_t)mtu;
1060 bzero(&ip6cp, sizeof(ip6cp));
1061 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1062 pfctlinput2(PRC_MSGSIZE,
1063 rtcache_getdst(ro_pmtu), &ip6cp);
1064 #endif
1065
1066 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1067 if (len < 8) {
1068 error = EMSGSIZE;
1069 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1070 goto bad;
1071 }
1072
1073 mnext = &m->m_nextpkt;
1074
1075 /*
1076 * Change the next header field of the last header in the
1077 * unfragmentable part.
1078 */
1079 if (exthdrs.ip6e_rthdr) {
1080 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1081 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1082 } else if (exthdrs.ip6e_dest1) {
1083 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1084 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1085 } else if (exthdrs.ip6e_hbh) {
1086 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1087 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1088 } else {
1089 nextproto = ip6->ip6_nxt;
1090 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1091 }
1092
1093 if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6))
1094 != 0) {
1095 if (IN6_NEED_CHECKSUM(ifp,
1096 m->m_pkthdr.csum_flags &
1097 (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
1098 in6_delayed_cksum(m);
1099 }
1100 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
1101 }
1102
1103 /*
1104 * Loop through length of segment after first fragment,
1105 * make new header and copy data of each part and link onto
1106 * chain.
1107 */
1108 m0 = m;
1109 for (off = hlen; off < tlen; off += len) {
1110 struct mbuf *mlast;
1111
1112 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1113 if (!m) {
1114 error = ENOBUFS;
1115 ip6stat.ip6s_odropped++;
1116 goto sendorfree;
1117 }
1118 m->m_pkthdr.rcvif = NULL;
1119 m->m_flags = m0->m_flags & M_COPYFLAGS;
1120 *mnext = m;
1121 mnext = &m->m_nextpkt;
1122 m->m_data += max_linkhdr;
1123 mhip6 = mtod(m, struct ip6_hdr *);
1124 *mhip6 = *ip6;
1125 m->m_len = sizeof(*mhip6);
1126 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1127 if (error) {
1128 ip6stat.ip6s_odropped++;
1129 goto sendorfree;
1130 }
1131 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
1132 if (off + len >= tlen)
1133 len = tlen - off;
1134 else
1135 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1136 mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
1137 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1138 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1139 error = ENOBUFS;
1140 ip6stat.ip6s_odropped++;
1141 goto sendorfree;
1142 }
1143 for (mlast = m; mlast->m_next; mlast = mlast->m_next)
1144 ;
1145 mlast->m_next = m_frgpart;
1146 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1147 m->m_pkthdr.rcvif = (struct ifnet *)0;
1148 ip6f->ip6f_reserved = 0;
1149 ip6f->ip6f_ident = id;
1150 ip6f->ip6f_nxt = nextproto;
1151 ip6stat.ip6s_ofragments++;
1152 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1153 }
1154
1155 in6_ifstat_inc(ifp, ifs6_out_fragok);
1156 }
1157
1158 /*
1159 * Remove leading garbages.
1160 */
1161 sendorfree:
1162 m = m0->m_nextpkt;
1163 m0->m_nextpkt = 0;
1164 m_freem(m0);
1165 for (m0 = m; m; m = m0) {
1166 m0 = m->m_nextpkt;
1167 m->m_nextpkt = 0;
1168 if (error == 0) {
1169 struct in6_ifaddr *ia6;
1170 ip6 = mtod(m, struct ip6_hdr *);
1171 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1172 if (ia6) {
1173 /*
1174 * Record statistics for this interface
1175 * address.
1176 */
1177 ia6->ia_ifa.ifa_data.ifad_outbytes +=
1178 m->m_pkthdr.len;
1179 }
1180 #ifdef IPSEC
1181 /* clean ipsec history once it goes out of the node */
1182 ipsec_delaux(m);
1183 #endif
1184 KASSERT(dst != NULL);
1185 error = nd6_output(ifp, origifp, m, dst, rt);
1186 } else
1187 m_freem(m);
1188 }
1189
1190 if (error == 0)
1191 ip6stat.ip6s_fragmented++;
1192
1193 done:
1194 /* XXX Second if is invariant? */
1195 if (ro == &ip6route)
1196 rtcache_free(ro);
1197 else if (ro_pmtu == &ip6route)
1198 rtcache_free(ro_pmtu);
1199
1200 #ifdef IPSEC
1201 if (sp != NULL)
1202 key_freesp(sp);
1203 #endif /* IPSEC */
1204 #ifdef FAST_IPSEC
1205 if (sp != NULL)
1206 KEY_FREESP(&sp);
1207 #endif /* FAST_IPSEC */
1208
1209
1210 return (error);
1211
1212 freehdrs:
1213 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1214 m_freem(exthdrs.ip6e_dest1);
1215 m_freem(exthdrs.ip6e_rthdr);
1216 m_freem(exthdrs.ip6e_dest2);
1217 /* FALLTHROUGH */
1218 bad:
1219 m_freem(m);
1220 goto done;
1221 badscope:
1222 ip6stat.ip6s_badscope++;
1223 in6_ifstat_inc(origifp, ifs6_out_discard);
1224 if (error == 0)
1225 error = EHOSTUNREACH; /* XXX */
1226 goto bad;
1227 }
1228
1229 static int
1230 ip6_copyexthdr(mp, hdr, hlen)
1231 struct mbuf **mp;
1232 void *hdr;
1233 int hlen;
1234 {
1235 struct mbuf *m;
1236
1237 if (hlen > MCLBYTES)
1238 return (ENOBUFS); /* XXX */
1239
1240 MGET(m, M_DONTWAIT, MT_DATA);
1241 if (!m)
1242 return (ENOBUFS);
1243
1244 if (hlen > MLEN) {
1245 MCLGET(m, M_DONTWAIT);
1246 if ((m->m_flags & M_EXT) == 0) {
1247 m_free(m);
1248 return (ENOBUFS);
1249 }
1250 }
1251 m->m_len = hlen;
1252 if (hdr)
1253 bcopy(hdr, mtod(m, void *), hlen);
1254
1255 *mp = m;
1256 return (0);
1257 }
1258
1259 /*
1260 * Process a delayed payload checksum calculation.
1261 */
1262 void
1263 in6_delayed_cksum(struct mbuf *m)
1264 {
1265 uint16_t csum, offset;
1266
1267 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1268 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1269 KASSERT((m->m_pkthdr.csum_flags
1270 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
1271
1272 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
1273 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1274 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
1275 csum = 0xffff;
1276 }
1277
1278 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
1279 if ((offset + sizeof(csum)) > m->m_len) {
1280 m_copyback(m, offset, sizeof(csum), &csum);
1281 } else {
1282 *(uint16_t *)(mtod(m, char *) + offset) = csum;
1283 }
1284 }
1285
1286 /*
1287 * Insert jumbo payload option.
1288 */
1289 static int
1290 ip6_insert_jumboopt(exthdrs, plen)
1291 struct ip6_exthdrs *exthdrs;
1292 u_int32_t plen;
1293 {
1294 struct mbuf *mopt;
1295 u_int8_t *optbuf;
1296 u_int32_t v;
1297
1298 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1299
1300 /*
1301 * If there is no hop-by-hop options header, allocate new one.
1302 * If there is one but it doesn't have enough space to store the
1303 * jumbo payload option, allocate a cluster to store the whole options.
1304 * Otherwise, use it to store the options.
1305 */
1306 if (exthdrs->ip6e_hbh == 0) {
1307 MGET(mopt, M_DONTWAIT, MT_DATA);
1308 if (mopt == 0)
1309 return (ENOBUFS);
1310 mopt->m_len = JUMBOOPTLEN;
1311 optbuf = mtod(mopt, u_int8_t *);
1312 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1313 exthdrs->ip6e_hbh = mopt;
1314 } else {
1315 struct ip6_hbh *hbh;
1316
1317 mopt = exthdrs->ip6e_hbh;
1318 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1319 /*
1320 * XXX assumption:
1321 * - exthdrs->ip6e_hbh is not referenced from places
1322 * other than exthdrs.
1323 * - exthdrs->ip6e_hbh is not an mbuf chain.
1324 */
1325 int oldoptlen = mopt->m_len;
1326 struct mbuf *n;
1327
1328 /*
1329 * XXX: give up if the whole (new) hbh header does
1330 * not fit even in an mbuf cluster.
1331 */
1332 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1333 return (ENOBUFS);
1334
1335 /*
1336 * As a consequence, we must always prepare a cluster
1337 * at this point.
1338 */
1339 MGET(n, M_DONTWAIT, MT_DATA);
1340 if (n) {
1341 MCLGET(n, M_DONTWAIT);
1342 if ((n->m_flags & M_EXT) == 0) {
1343 m_freem(n);
1344 n = NULL;
1345 }
1346 }
1347 if (!n)
1348 return (ENOBUFS);
1349 n->m_len = oldoptlen + JUMBOOPTLEN;
1350 bcopy(mtod(mopt, void *), mtod(n, void *),
1351 oldoptlen);
1352 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1353 m_freem(mopt);
1354 mopt = exthdrs->ip6e_hbh = n;
1355 } else {
1356 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1357 mopt->m_len += JUMBOOPTLEN;
1358 }
1359 optbuf[0] = IP6OPT_PADN;
1360 optbuf[1] = 0;
1361
1362 /*
1363 * Adjust the header length according to the pad and
1364 * the jumbo payload option.
1365 */
1366 hbh = mtod(mopt, struct ip6_hbh *);
1367 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1368 }
1369
1370 /* fill in the option. */
1371 optbuf[2] = IP6OPT_JUMBO;
1372 optbuf[3] = 4;
1373 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1374 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1375
1376 /* finally, adjust the packet header length */
1377 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1378
1379 return (0);
1380 #undef JUMBOOPTLEN
1381 }
1382
1383 /*
1384 * Insert fragment header and copy unfragmentable header portions.
1385 */
1386 static int
1387 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1388 struct mbuf *m0, *m;
1389 int hlen;
1390 struct ip6_frag **frghdrp;
1391 {
1392 struct mbuf *n, *mlast;
1393
1394 if (hlen > sizeof(struct ip6_hdr)) {
1395 n = m_copym(m0, sizeof(struct ip6_hdr),
1396 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1397 if (n == 0)
1398 return (ENOBUFS);
1399 m->m_next = n;
1400 } else
1401 n = m;
1402
1403 /* Search for the last mbuf of unfragmentable part. */
1404 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1405 ;
1406
1407 if ((mlast->m_flags & M_EXT) == 0 &&
1408 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1409 /* use the trailing space of the last mbuf for the fragment hdr */
1410 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
1411 mlast->m_len);
1412 mlast->m_len += sizeof(struct ip6_frag);
1413 m->m_pkthdr.len += sizeof(struct ip6_frag);
1414 } else {
1415 /* allocate a new mbuf for the fragment header */
1416 struct mbuf *mfrg;
1417
1418 MGET(mfrg, M_DONTWAIT, MT_DATA);
1419 if (mfrg == 0)
1420 return (ENOBUFS);
1421 mfrg->m_len = sizeof(struct ip6_frag);
1422 *frghdrp = mtod(mfrg, struct ip6_frag *);
1423 mlast->m_next = mfrg;
1424 }
1425
1426 return (0);
1427 }
1428
1429 static int
1430 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp,
1431 const struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
1432 {
1433 u_int32_t mtu = 0;
1434 int alwaysfrag = 0;
1435 int error = 0;
1436
1437 if (ro_pmtu != ro) {
1438 union {
1439 struct sockaddr dst;
1440 struct sockaddr_in6 dst6;
1441 } u;
1442
1443 /* The first hop and the final destination may differ. */
1444 sockaddr_in6_init(&u.dst6, dst, 0, 0, 0);
1445 rtcache_lookup(ro_pmtu, &u.dst);
1446 }
1447 if (ro_pmtu->ro_rt != NULL) {
1448 u_int32_t ifmtu;
1449
1450 if (ifp == NULL)
1451 ifp = ro_pmtu->ro_rt->rt_ifp;
1452 ifmtu = IN6_LINKMTU(ifp);
1453 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1454 if (mtu == 0)
1455 mtu = ifmtu;
1456 else if (mtu < IPV6_MMTU) {
1457 /*
1458 * RFC2460 section 5, last paragraph:
1459 * if we record ICMPv6 too big message with
1460 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1461 * or smaller, with fragment header attached.
1462 * (fragment header is needed regardless from the
1463 * packet size, for translators to identify packets)
1464 */
1465 alwaysfrag = 1;
1466 mtu = IPV6_MMTU;
1467 } else if (mtu > ifmtu) {
1468 /*
1469 * The MTU on the route is larger than the MTU on
1470 * the interface! This shouldn't happen, unless the
1471 * MTU of the interface has been changed after the
1472 * interface was brought up. Change the MTU in the
1473 * route to match the interface MTU (as long as the
1474 * field isn't locked).
1475 */
1476 mtu = ifmtu;
1477 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
1478 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1479 }
1480 } else if (ifp) {
1481 mtu = IN6_LINKMTU(ifp);
1482 } else
1483 error = EHOSTUNREACH; /* XXX */
1484
1485 *mtup = mtu;
1486 if (alwaysfragp)
1487 *alwaysfragp = alwaysfrag;
1488 return (error);
1489 }
1490
1491 /*
1492 * IP6 socket option processing.
1493 */
1494 int
1495 ip6_ctloutput(int op, struct socket *so, int level, int optname,
1496 struct mbuf **mp)
1497 {
1498 int privileged, optdatalen, uproto;
1499 void *optdata;
1500 struct in6pcb *in6p = sotoin6pcb(so);
1501 struct mbuf *m = *mp;
1502 int error, optval;
1503 int optlen;
1504 struct lwp *l = curlwp; /* XXX */
1505
1506 optlen = m ? m->m_len : 0;
1507 error = optval = 0;
1508 privileged = (l == 0 || kauth_authorize_generic(l->l_cred,
1509 KAUTH_GENERIC_ISSUSER, NULL)) ? 0 : 1;
1510 uproto = (int)so->so_proto->pr_protocol;
1511
1512 if (level == IPPROTO_IPV6) {
1513 switch (op) {
1514 case PRCO_SETOPT:
1515 switch (optname) {
1516 #ifdef RFC2292
1517 case IPV6_2292PKTOPTIONS:
1518 /* m is freed in ip6_pcbopts */
1519 error = ip6_pcbopts(&in6p->in6p_outputopts,
1520 m, so);
1521 break;
1522 #endif
1523
1524 /*
1525 * Use of some Hop-by-Hop options or some
1526 * Destination options, might require special
1527 * privilege. That is, normal applications
1528 * (without special privilege) might be forbidden
1529 * from setting certain options in outgoing packets,
1530 * and might never see certain options in received
1531 * packets. [RFC 2292 Section 6]
1532 * KAME specific note:
1533 * KAME prevents non-privileged users from sending or
1534 * receiving ANY hbh/dst options in order to avoid
1535 * overhead of parsing options in the kernel.
1536 */
1537 case IPV6_RECVHOPOPTS:
1538 case IPV6_RECVDSTOPTS:
1539 case IPV6_RECVRTHDRDSTOPTS:
1540 if (!privileged) {
1541 error = EPERM;
1542 break;
1543 }
1544 /* FALLTHROUGH */
1545 case IPV6_UNICAST_HOPS:
1546 case IPV6_HOPLIMIT:
1547 case IPV6_FAITH:
1548
1549 case IPV6_RECVPKTINFO:
1550 case IPV6_RECVHOPLIMIT:
1551 case IPV6_RECVRTHDR:
1552 case IPV6_RECVPATHMTU:
1553 case IPV6_RECVTCLASS:
1554 case IPV6_V6ONLY:
1555 if (optlen != sizeof(int)) {
1556 error = EINVAL;
1557 break;
1558 }
1559 optval = *mtod(m, int *);
1560 switch (optname) {
1561
1562 case IPV6_UNICAST_HOPS:
1563 if (optval < -1 || optval >= 256)
1564 error = EINVAL;
1565 else {
1566 /* -1 = kernel default */
1567 in6p->in6p_hops = optval;
1568 }
1569 break;
1570 #define OPTSET(bit) \
1571 do { \
1572 if (optval) \
1573 in6p->in6p_flags |= (bit); \
1574 else \
1575 in6p->in6p_flags &= ~(bit); \
1576 } while (/*CONSTCOND*/ 0)
1577
1578 #ifdef RFC2292
1579 #define OPTSET2292(bit) \
1580 do { \
1581 in6p->in6p_flags |= IN6P_RFC2292; \
1582 if (optval) \
1583 in6p->in6p_flags |= (bit); \
1584 else \
1585 in6p->in6p_flags &= ~(bit); \
1586 } while (/*CONSTCOND*/ 0)
1587 #endif
1588
1589 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1590
1591 case IPV6_RECVPKTINFO:
1592 #ifdef RFC2292
1593 /* cannot mix with RFC2292 */
1594 if (OPTBIT(IN6P_RFC2292)) {
1595 error = EINVAL;
1596 break;
1597 }
1598 #endif
1599 OPTSET(IN6P_PKTINFO);
1600 break;
1601
1602 case IPV6_HOPLIMIT:
1603 {
1604 struct ip6_pktopts **optp;
1605
1606 #ifdef RFC2292
1607 /* cannot mix with RFC2292 */
1608 if (OPTBIT(IN6P_RFC2292)) {
1609 error = EINVAL;
1610 break;
1611 }
1612 #endif
1613 optp = &in6p->in6p_outputopts;
1614 error = ip6_pcbopt(IPV6_HOPLIMIT,
1615 (u_char *)&optval,
1616 sizeof(optval),
1617 optp,
1618 privileged, uproto);
1619 break;
1620 }
1621
1622 case IPV6_RECVHOPLIMIT:
1623 #ifdef RFC2292
1624 /* cannot mix with RFC2292 */
1625 if (OPTBIT(IN6P_RFC2292)) {
1626 error = EINVAL;
1627 break;
1628 }
1629 #endif
1630 OPTSET(IN6P_HOPLIMIT);
1631 break;
1632
1633 case IPV6_RECVHOPOPTS:
1634 #ifdef RFC2292
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1637 error = EINVAL;
1638 break;
1639 }
1640 #endif
1641 OPTSET(IN6P_HOPOPTS);
1642 break;
1643
1644 case IPV6_RECVDSTOPTS:
1645 #ifdef RFC2292
1646 /* cannot mix with RFC2292 */
1647 if (OPTBIT(IN6P_RFC2292)) {
1648 error = EINVAL;
1649 break;
1650 }
1651 #endif
1652 OPTSET(IN6P_DSTOPTS);
1653 break;
1654
1655 case IPV6_RECVRTHDRDSTOPTS:
1656 #ifdef RFC2292
1657 /* cannot mix with RFC2292 */
1658 if (OPTBIT(IN6P_RFC2292)) {
1659 error = EINVAL;
1660 break;
1661 }
1662 #endif
1663 OPTSET(IN6P_RTHDRDSTOPTS);
1664 break;
1665
1666 case IPV6_RECVRTHDR:
1667 #ifdef RFC2292
1668 /* cannot mix with RFC2292 */
1669 if (OPTBIT(IN6P_RFC2292)) {
1670 error = EINVAL;
1671 break;
1672 }
1673 #endif
1674 OPTSET(IN6P_RTHDR);
1675 break;
1676
1677 case IPV6_FAITH:
1678 OPTSET(IN6P_FAITH);
1679 break;
1680
1681 case IPV6_RECVPATHMTU:
1682 /*
1683 * We ignore this option for TCP
1684 * sockets.
1685 * (RFC3542 leaves this case
1686 * unspecified.)
1687 */
1688 if (uproto != IPPROTO_TCP)
1689 OPTSET(IN6P_MTU);
1690 break;
1691
1692 case IPV6_V6ONLY:
1693 /*
1694 * make setsockopt(IPV6_V6ONLY)
1695 * available only prior to bind(2).
1696 * see ipng mailing list, Jun 22 2001.
1697 */
1698 if (in6p->in6p_lport ||
1699 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1700 error = EINVAL;
1701 break;
1702 }
1703 #ifdef INET6_BINDV6ONLY
1704 if (!optval)
1705 error = EINVAL;
1706 #else
1707 OPTSET(IN6P_IPV6_V6ONLY);
1708 #endif
1709 break;
1710 case IPV6_RECVTCLASS:
1711 #ifdef RFC2292
1712 /* cannot mix with RFC2292 XXX */
1713 if (OPTBIT(IN6P_RFC2292)) {
1714 error = EINVAL;
1715 break;
1716 }
1717 #endif
1718 OPTSET(IN6P_TCLASS);
1719 break;
1720
1721 }
1722 break;
1723
1724 case IPV6_OTCLASS:
1725 {
1726 struct ip6_pktopts **optp;
1727 u_int8_t tclass;
1728
1729 if (optlen != sizeof(tclass)) {
1730 error = EINVAL;
1731 break;
1732 }
1733 tclass = *mtod(m, u_int8_t *);
1734 optp = &in6p->in6p_outputopts;
1735 error = ip6_pcbopt(optname,
1736 (u_char *)&tclass,
1737 sizeof(tclass),
1738 optp,
1739 privileged, uproto);
1740 break;
1741 }
1742
1743 case IPV6_TCLASS:
1744 case IPV6_DONTFRAG:
1745 case IPV6_USE_MIN_MTU:
1746 if (optlen != sizeof(optval)) {
1747 error = EINVAL;
1748 break;
1749 }
1750 optval = *mtod(m, int *);
1751 {
1752 struct ip6_pktopts **optp;
1753 optp = &in6p->in6p_outputopts;
1754 error = ip6_pcbopt(optname,
1755 (u_char *)&optval,
1756 sizeof(optval),
1757 optp,
1758 privileged, uproto);
1759 break;
1760 }
1761
1762 #ifdef RFC2292
1763 case IPV6_2292PKTINFO:
1764 case IPV6_2292HOPLIMIT:
1765 case IPV6_2292HOPOPTS:
1766 case IPV6_2292DSTOPTS:
1767 case IPV6_2292RTHDR:
1768 /* RFC 2292 */
1769 if (optlen != sizeof(int)) {
1770 error = EINVAL;
1771 break;
1772 }
1773 optval = *mtod(m, int *);
1774 switch (optname) {
1775 case IPV6_2292PKTINFO:
1776 OPTSET2292(IN6P_PKTINFO);
1777 break;
1778 case IPV6_2292HOPLIMIT:
1779 OPTSET2292(IN6P_HOPLIMIT);
1780 break;
1781 case IPV6_2292HOPOPTS:
1782 /*
1783 * Check super-user privilege.
1784 * See comments for IPV6_RECVHOPOPTS.
1785 */
1786 if (!privileged)
1787 return (EPERM);
1788 OPTSET2292(IN6P_HOPOPTS);
1789 break;
1790 case IPV6_2292DSTOPTS:
1791 if (!privileged)
1792 return (EPERM);
1793 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1794 break;
1795 case IPV6_2292RTHDR:
1796 OPTSET2292(IN6P_RTHDR);
1797 break;
1798 }
1799 break;
1800 #endif
1801 case IPV6_PKTINFO:
1802 case IPV6_HOPOPTS:
1803 case IPV6_RTHDR:
1804 case IPV6_DSTOPTS:
1805 case IPV6_RTHDRDSTOPTS:
1806 case IPV6_NEXTHOP:
1807 {
1808 /* new advanced API (RFC3542) */
1809 u_char *optbuf;
1810 int optbuflen;
1811 struct ip6_pktopts **optp;
1812 if (!m) {
1813 error = EINVAL;
1814 break;
1815 }
1816
1817 #ifdef RFC2292
1818 /* cannot mix with RFC2292 */
1819 if (OPTBIT(IN6P_RFC2292)) {
1820 error = EINVAL;
1821 break;
1822 }
1823 #endif
1824
1825 if (m && m->m_next) {
1826 error = EINVAL; /* XXX */
1827 break;
1828 }
1829
1830 optbuf = mtod(m, u_char *);
1831 optbuflen = m->m_len;
1832 optp = &in6p->in6p_outputopts;
1833 error = ip6_pcbopt(optname, optbuf, optbuflen,
1834 optp, privileged, uproto);
1835 break;
1836 }
1837 #undef OPTSET
1838
1839 case IPV6_MULTICAST_IF:
1840 case IPV6_MULTICAST_HOPS:
1841 case IPV6_MULTICAST_LOOP:
1842 case IPV6_JOIN_GROUP:
1843 case IPV6_LEAVE_GROUP:
1844 error = ip6_setmoptions(optname,
1845 &in6p->in6p_moptions, m);
1846 break;
1847
1848 case IPV6_PORTRANGE:
1849 if (!m) {
1850 error = EINVAL;
1851 break;
1852 }
1853 optval = *mtod(m, int *);
1854
1855 switch (optval) {
1856 case IPV6_PORTRANGE_DEFAULT:
1857 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1858 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1859 break;
1860
1861 case IPV6_PORTRANGE_HIGH:
1862 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1863 in6p->in6p_flags |= IN6P_HIGHPORT;
1864 break;
1865
1866 case IPV6_PORTRANGE_LOW:
1867 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1868 in6p->in6p_flags |= IN6P_LOWPORT;
1869 break;
1870
1871 default:
1872 error = EINVAL;
1873 break;
1874 }
1875 break;
1876
1877
1878 #if defined(IPSEC) || defined(FAST_IPSEC)
1879 case IPV6_IPSEC_POLICY:
1880 {
1881 void *req = NULL;
1882 size_t len = 0;
1883 if (m) {
1884 req = mtod(m, void *);
1885 len = m->m_len;
1886 }
1887 error = ipsec6_set_policy(in6p, optname, req,
1888 len, privileged);
1889 }
1890 break;
1891 #endif /* IPSEC */
1892
1893 default:
1894 error = ENOPROTOOPT;
1895 break;
1896 }
1897 if (m)
1898 (void)m_free(m);
1899 break;
1900
1901 case PRCO_GETOPT:
1902 switch (optname) {
1903 #ifdef RFC2292
1904 case IPV6_2292PKTOPTIONS:
1905 /*
1906 * RFC3542 (effectively) deprecated the
1907 * semantics of the 2292-style pktoptions.
1908 * Since it was not reliable in nature (i.e.,
1909 * applications had to expect the lack of some
1910 * information after all), it would make sense
1911 * to simplify this part by always returning
1912 * empty data.
1913 */
1914 *mp = m_get(M_WAIT, MT_SOOPTS);
1915 (*mp)->m_len = 0;
1916 break;
1917 #endif
1918
1919 case IPV6_RECVHOPOPTS:
1920 case IPV6_RECVDSTOPTS:
1921 case IPV6_RECVRTHDRDSTOPTS:
1922 case IPV6_UNICAST_HOPS:
1923 case IPV6_RECVPKTINFO:
1924 case IPV6_RECVHOPLIMIT:
1925 case IPV6_RECVRTHDR:
1926 case IPV6_RECVPATHMTU:
1927
1928 case IPV6_FAITH:
1929 case IPV6_V6ONLY:
1930 case IPV6_PORTRANGE:
1931 case IPV6_RECVTCLASS:
1932 switch (optname) {
1933
1934 case IPV6_RECVHOPOPTS:
1935 optval = OPTBIT(IN6P_HOPOPTS);
1936 break;
1937
1938 case IPV6_RECVDSTOPTS:
1939 optval = OPTBIT(IN6P_DSTOPTS);
1940 break;
1941
1942 case IPV6_RECVRTHDRDSTOPTS:
1943 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1944 break;
1945
1946 case IPV6_UNICAST_HOPS:
1947 optval = in6p->in6p_hops;
1948 break;
1949
1950 case IPV6_RECVPKTINFO:
1951 optval = OPTBIT(IN6P_PKTINFO);
1952 break;
1953
1954 case IPV6_RECVHOPLIMIT:
1955 optval = OPTBIT(IN6P_HOPLIMIT);
1956 break;
1957
1958 case IPV6_RECVRTHDR:
1959 optval = OPTBIT(IN6P_RTHDR);
1960 break;
1961
1962 case IPV6_RECVPATHMTU:
1963 optval = OPTBIT(IN6P_MTU);
1964 break;
1965
1966 case IPV6_FAITH:
1967 optval = OPTBIT(IN6P_FAITH);
1968 break;
1969
1970 case IPV6_V6ONLY:
1971 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1972 break;
1973
1974 case IPV6_PORTRANGE:
1975 {
1976 int flags;
1977 flags = in6p->in6p_flags;
1978 if (flags & IN6P_HIGHPORT)
1979 optval = IPV6_PORTRANGE_HIGH;
1980 else if (flags & IN6P_LOWPORT)
1981 optval = IPV6_PORTRANGE_LOW;
1982 else
1983 optval = 0;
1984 break;
1985 }
1986 case IPV6_RECVTCLASS:
1987 optval = OPTBIT(IN6P_TCLASS);
1988 break;
1989
1990 }
1991 if (error)
1992 break;
1993 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1994 m->m_len = sizeof(int);
1995 *mtod(m, int *) = optval;
1996 break;
1997
1998 case IPV6_PATHMTU:
1999 {
2000 u_long pmtu = 0;
2001 struct ip6_mtuinfo mtuinfo;
2002 struct route *ro = &in6p->in6p_route;
2003
2004 if (!(so->so_state & SS_ISCONNECTED))
2005 return (ENOTCONN);
2006 /*
2007 * XXX: we dot not consider the case of source
2008 * routing, or optional information to specify
2009 * the outgoing interface.
2010 */
2011 error = ip6_getpmtu(ro, NULL, NULL,
2012 &in6p->in6p_faddr, &pmtu, NULL);
2013 if (error)
2014 break;
2015 if (pmtu > IPV6_MAXPACKET)
2016 pmtu = IPV6_MAXPACKET;
2017
2018 memset(&mtuinfo, 0, sizeof(mtuinfo));
2019 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2020 optdata = (void *)&mtuinfo;
2021 optdatalen = sizeof(mtuinfo);
2022 if (optdatalen > MCLBYTES)
2023 return (EMSGSIZE); /* XXX */
2024 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2025 if (optdatalen > MLEN)
2026 MCLGET(m, M_WAIT);
2027 m->m_len = optdatalen;
2028 memcpy(mtod(m, void *), optdata, optdatalen);
2029 break;
2030 }
2031
2032 #ifdef RFC2292
2033 case IPV6_2292PKTINFO:
2034 case IPV6_2292HOPLIMIT:
2035 case IPV6_2292HOPOPTS:
2036 case IPV6_2292RTHDR:
2037 case IPV6_2292DSTOPTS:
2038 switch (optname) {
2039 case IPV6_2292PKTINFO:
2040 optval = OPTBIT(IN6P_PKTINFO);
2041 break;
2042 case IPV6_2292HOPLIMIT:
2043 optval = OPTBIT(IN6P_HOPLIMIT);
2044 break;
2045 case IPV6_2292HOPOPTS:
2046 optval = OPTBIT(IN6P_HOPOPTS);
2047 break;
2048 case IPV6_2292RTHDR:
2049 optval = OPTBIT(IN6P_RTHDR);
2050 break;
2051 case IPV6_2292DSTOPTS:
2052 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2053 break;
2054 }
2055 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2056 m->m_len = sizeof(int);
2057 *mtod(m, int *) = optval;
2058 break;
2059 #endif
2060 case IPV6_PKTINFO:
2061 case IPV6_HOPOPTS:
2062 case IPV6_RTHDR:
2063 case IPV6_DSTOPTS:
2064 case IPV6_RTHDRDSTOPTS:
2065 case IPV6_NEXTHOP:
2066 case IPV6_OTCLASS:
2067 case IPV6_TCLASS:
2068 case IPV6_DONTFRAG:
2069 case IPV6_USE_MIN_MTU:
2070 error = ip6_getpcbopt(in6p->in6p_outputopts,
2071 optname, mp);
2072 break;
2073
2074 case IPV6_MULTICAST_IF:
2075 case IPV6_MULTICAST_HOPS:
2076 case IPV6_MULTICAST_LOOP:
2077 case IPV6_JOIN_GROUP:
2078 case IPV6_LEAVE_GROUP:
2079 error = ip6_getmoptions(optname,
2080 in6p->in6p_moptions, mp);
2081 break;
2082
2083 #if defined(IPSEC) || defined(FAST_IPSEC)
2084 case IPV6_IPSEC_POLICY:
2085 {
2086 void *req = NULL;
2087 size_t len = 0;
2088 if (m) {
2089 req = mtod(m, void *);
2090 len = m->m_len;
2091 }
2092 error = ipsec6_get_policy(in6p, req, len, mp);
2093 break;
2094 }
2095 #endif /* IPSEC */
2096
2097
2098
2099
2100 default:
2101 error = ENOPROTOOPT;
2102 break;
2103 }
2104 break;
2105 }
2106 } else {
2107 error = EINVAL;
2108 if (op == PRCO_SETOPT && *mp)
2109 (void)m_free(*mp);
2110 }
2111 return (error);
2112 }
2113
2114 int
2115 ip6_raw_ctloutput(op, so, level, optname, mp)
2116 int op;
2117 struct socket *so;
2118 int level, optname;
2119 struct mbuf **mp;
2120 {
2121 int error = 0, optval, optlen;
2122 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2123 struct in6pcb *in6p = sotoin6pcb(so);
2124 struct mbuf *m = *mp;
2125
2126 optlen = m ? m->m_len : 0;
2127
2128 if (level != IPPROTO_IPV6) {
2129 if (op == PRCO_SETOPT && *mp)
2130 (void)m_free(*mp);
2131 return (EINVAL);
2132 }
2133
2134 switch (optname) {
2135 case IPV6_CHECKSUM:
2136 /*
2137 * For ICMPv6 sockets, no modification allowed for checksum
2138 * offset, permit "no change" values to help existing apps.
2139 *
2140 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
2141 * for an ICMPv6 socket will fail." The current
2142 * behavior does not meet RFC3542.
2143 */
2144 switch (op) {
2145 case PRCO_SETOPT:
2146 if (optlen != sizeof(int)) {
2147 error = EINVAL;
2148 break;
2149 }
2150 optval = *mtod(m, int *);
2151 if ((optval % 2) != 0) {
2152 /* the API assumes even offset values */
2153 error = EINVAL;
2154 } else if (so->so_proto->pr_protocol ==
2155 IPPROTO_ICMPV6) {
2156 if (optval != icmp6off)
2157 error = EINVAL;
2158 } else
2159 in6p->in6p_cksum = optval;
2160 break;
2161
2162 case PRCO_GETOPT:
2163 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2164 optval = icmp6off;
2165 else
2166 optval = in6p->in6p_cksum;
2167
2168 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2169 m->m_len = sizeof(int);
2170 *mtod(m, int *) = optval;
2171 break;
2172
2173 default:
2174 error = EINVAL;
2175 break;
2176 }
2177 break;
2178
2179 default:
2180 error = ENOPROTOOPT;
2181 break;
2182 }
2183
2184 if (op == PRCO_SETOPT && m)
2185 (void)m_free(m);
2186
2187 return (error);
2188 }
2189
2190 #ifdef RFC2292
2191 /*
2192 * Set up IP6 options in pcb for insertion in output packets or
2193 * specifying behavior of outgoing packets.
2194 */
2195 static int
2196 ip6_pcbopts(pktopt, m, so)
2197 struct ip6_pktopts **pktopt;
2198 struct mbuf *m;
2199 struct socket *so;
2200 {
2201 struct ip6_pktopts *opt = *pktopt;
2202 int error = 0;
2203 struct lwp *l = curlwp; /* XXX */
2204 int priv = 0;
2205
2206 /* turn off any old options. */
2207 if (opt) {
2208 #ifdef DIAGNOSTIC
2209 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2210 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2211 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2212 printf("ip6_pcbopts: all specified options are cleared.\n");
2213 #endif
2214 ip6_clearpktopts(opt, -1);
2215 } else
2216 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2217 *pktopt = NULL;
2218
2219 if (!m || m->m_len == 0) {
2220 /*
2221 * Only turning off any previous options, regardless of
2222 * whether the opt is just created or given.
2223 */
2224 free(opt, M_IP6OPT);
2225 return (0);
2226 }
2227
2228 /* set options specified by user. */
2229 if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2230 NULL))
2231 priv = 1;
2232 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2233 so->so_proto->pr_protocol)) != 0) {
2234 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2235 free(opt, M_IP6OPT);
2236 return (error);
2237 }
2238 *pktopt = opt;
2239 return (0);
2240 }
2241 #endif
2242
2243 /*
2244 * initialize ip6_pktopts. beware that there are non-zero default values in
2245 * the struct.
2246 */
2247 void
2248 ip6_initpktopts(struct ip6_pktopts *opt)
2249 {
2250
2251 memset(opt, 0, sizeof(*opt));
2252 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2253 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2254 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2255 }
2256
2257 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2258 static int
2259 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2260 int priv, int uproto)
2261 {
2262 struct ip6_pktopts *opt;
2263
2264 if (*pktopt == NULL) {
2265 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2266 M_WAITOK);
2267 ip6_initpktopts(*pktopt);
2268 }
2269 opt = *pktopt;
2270
2271 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2272 }
2273
2274 static int
2275 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
2276 {
2277 void *optdata = NULL;
2278 int optdatalen = 0;
2279 struct ip6_ext *ip6e;
2280 int error = 0;
2281 struct in6_pktinfo null_pktinfo;
2282 int deftclass = 0, on;
2283 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2284 struct mbuf *m;
2285
2286 switch (optname) {
2287 case IPV6_PKTINFO:
2288 if (pktopt && pktopt->ip6po_pktinfo)
2289 optdata = (void *)pktopt->ip6po_pktinfo;
2290 else {
2291 /* XXX: we don't have to do this every time... */
2292 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2293 optdata = (void *)&null_pktinfo;
2294 }
2295 optdatalen = sizeof(struct in6_pktinfo);
2296 break;
2297 case IPV6_OTCLASS:
2298 /* XXX */
2299 return (EINVAL);
2300 case IPV6_TCLASS:
2301 if (pktopt && pktopt->ip6po_tclass >= 0)
2302 optdata = (void *)&pktopt->ip6po_tclass;
2303 else
2304 optdata = (void *)&deftclass;
2305 optdatalen = sizeof(int);
2306 break;
2307 case IPV6_HOPOPTS:
2308 if (pktopt && pktopt->ip6po_hbh) {
2309 optdata = (void *)pktopt->ip6po_hbh;
2310 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2311 optdatalen = (ip6e->ip6e_len + 1) << 3;
2312 }
2313 break;
2314 case IPV6_RTHDR:
2315 if (pktopt && pktopt->ip6po_rthdr) {
2316 optdata = (void *)pktopt->ip6po_rthdr;
2317 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2318 optdatalen = (ip6e->ip6e_len + 1) << 3;
2319 }
2320 break;
2321 case IPV6_RTHDRDSTOPTS:
2322 if (pktopt && pktopt->ip6po_dest1) {
2323 optdata = (void *)pktopt->ip6po_dest1;
2324 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2325 optdatalen = (ip6e->ip6e_len + 1) << 3;
2326 }
2327 break;
2328 case IPV6_DSTOPTS:
2329 if (pktopt && pktopt->ip6po_dest2) {
2330 optdata = (void *)pktopt->ip6po_dest2;
2331 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2332 optdatalen = (ip6e->ip6e_len + 1) << 3;
2333 }
2334 break;
2335 case IPV6_NEXTHOP:
2336 if (pktopt && pktopt->ip6po_nexthop) {
2337 optdata = (void *)pktopt->ip6po_nexthop;
2338 optdatalen = pktopt->ip6po_nexthop->sa_len;
2339 }
2340 break;
2341 case IPV6_USE_MIN_MTU:
2342 if (pktopt)
2343 optdata = (void *)&pktopt->ip6po_minmtu;
2344 else
2345 optdata = (void *)&defminmtu;
2346 optdatalen = sizeof(int);
2347 break;
2348 case IPV6_DONTFRAG:
2349 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2350 on = 1;
2351 else
2352 on = 0;
2353 optdata = (void *)&on;
2354 optdatalen = sizeof(on);
2355 break;
2356 default: /* should not happen */
2357 #ifdef DIAGNOSTIC
2358 panic("ip6_getpcbopt: unexpected option\n");
2359 #endif
2360 return (ENOPROTOOPT);
2361 }
2362
2363 if (optdatalen > MCLBYTES)
2364 return (EMSGSIZE); /* XXX */
2365 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2366 if (optdatalen > MLEN)
2367 MCLGET(m, M_WAIT);
2368 m->m_len = optdatalen;
2369 if (optdatalen)
2370 memcpy(mtod(m, void *), optdata, optdatalen);
2371
2372 return (error);
2373 }
2374
2375 void
2376 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2377 {
2378 if (optname == -1 || optname == IPV6_PKTINFO) {
2379 if (pktopt->ip6po_pktinfo)
2380 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2381 pktopt->ip6po_pktinfo = NULL;
2382 }
2383 if (optname == -1 || optname == IPV6_HOPLIMIT)
2384 pktopt->ip6po_hlim = -1;
2385 if (optname == -1 || optname == IPV6_TCLASS)
2386 pktopt->ip6po_tclass = -1;
2387 if (optname == -1 || optname == IPV6_NEXTHOP) {
2388 rtcache_free(&pktopt->ip6po_nextroute);
2389 if (pktopt->ip6po_nexthop)
2390 free(pktopt->ip6po_nexthop, M_IP6OPT);
2391 pktopt->ip6po_nexthop = NULL;
2392 }
2393 if (optname == -1 || optname == IPV6_HOPOPTS) {
2394 if (pktopt->ip6po_hbh)
2395 free(pktopt->ip6po_hbh, M_IP6OPT);
2396 pktopt->ip6po_hbh = NULL;
2397 }
2398 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2399 if (pktopt->ip6po_dest1)
2400 free(pktopt->ip6po_dest1, M_IP6OPT);
2401 pktopt->ip6po_dest1 = NULL;
2402 }
2403 if (optname == -1 || optname == IPV6_RTHDR) {
2404 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2405 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2406 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2407 rtcache_free(&pktopt->ip6po_route);
2408 }
2409 if (optname == -1 || optname == IPV6_DSTOPTS) {
2410 if (pktopt->ip6po_dest2)
2411 free(pktopt->ip6po_dest2, M_IP6OPT);
2412 pktopt->ip6po_dest2 = NULL;
2413 }
2414 }
2415
2416 #define PKTOPT_EXTHDRCPY(type) \
2417 do { \
2418 if (src->type) { \
2419 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2420 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2421 if (dst->type == NULL && canwait == M_NOWAIT) \
2422 goto bad; \
2423 memcpy(dst->type, src->type, hlen); \
2424 } \
2425 } while (/*CONSTCOND*/ 0)
2426
2427 static int
2428 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2429 {
2430 dst->ip6po_hlim = src->ip6po_hlim;
2431 dst->ip6po_tclass = src->ip6po_tclass;
2432 dst->ip6po_flags = src->ip6po_flags;
2433 if (src->ip6po_pktinfo) {
2434 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2435 M_IP6OPT, canwait);
2436 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2437 goto bad;
2438 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2439 }
2440 if (src->ip6po_nexthop) {
2441 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2442 M_IP6OPT, canwait);
2443 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2444 goto bad;
2445 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2446 src->ip6po_nexthop->sa_len);
2447 }
2448 PKTOPT_EXTHDRCPY(ip6po_hbh);
2449 PKTOPT_EXTHDRCPY(ip6po_dest1);
2450 PKTOPT_EXTHDRCPY(ip6po_dest2);
2451 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2452 return (0);
2453
2454 bad:
2455 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2456 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2457 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2458 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2459 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2460 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2461
2462 return (ENOBUFS);
2463 }
2464 #undef PKTOPT_EXTHDRCPY
2465
2466 struct ip6_pktopts *
2467 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2468 {
2469 int error;
2470 struct ip6_pktopts *dst;
2471
2472 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2473 if (dst == NULL && canwait == M_NOWAIT)
2474 return (NULL);
2475 ip6_initpktopts(dst);
2476
2477 if ((error = copypktopts(dst, src, canwait)) != 0) {
2478 free(dst, M_IP6OPT);
2479 return (NULL);
2480 }
2481
2482 return (dst);
2483 }
2484
2485 void
2486 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2487 {
2488 if (pktopt == NULL)
2489 return;
2490
2491 ip6_clearpktopts(pktopt, -1);
2492
2493 free(pktopt, M_IP6OPT);
2494 }
2495
2496 /*
2497 * Set the IP6 multicast options in response to user setsockopt().
2498 */
2499 static int
2500 ip6_setmoptions(optname, im6op, m)
2501 int optname;
2502 struct ip6_moptions **im6op;
2503 struct mbuf *m;
2504 {
2505 int error = 0;
2506 u_int loop, ifindex;
2507 struct ipv6_mreq *mreq;
2508 struct ifnet *ifp;
2509 struct ip6_moptions *im6o = *im6op;
2510 struct route ro;
2511 struct in6_multi_mship *imm;
2512 struct lwp *l = curlwp; /* XXX */
2513
2514 if (im6o == NULL) {
2515 /*
2516 * No multicast option buffer attached to the pcb;
2517 * allocate one and initialize to default values.
2518 */
2519 im6o = (struct ip6_moptions *)
2520 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2521
2522 if (im6o == NULL)
2523 return (ENOBUFS);
2524 *im6op = im6o;
2525 im6o->im6o_multicast_ifp = NULL;
2526 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2527 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2528 LIST_INIT(&im6o->im6o_memberships);
2529 }
2530
2531 switch (optname) {
2532
2533 case IPV6_MULTICAST_IF:
2534 /*
2535 * Select the interface for outgoing multicast packets.
2536 */
2537 if (m == NULL || m->m_len != sizeof(u_int)) {
2538 error = EINVAL;
2539 break;
2540 }
2541 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2542 if (ifindex != 0) {
2543 if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
2544 error = ENXIO; /* XXX EINVAL? */
2545 break;
2546 }
2547 ifp = ifindex2ifnet[ifindex];
2548 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2549 error = EADDRNOTAVAIL;
2550 break;
2551 }
2552 } else
2553 ifp = NULL;
2554 im6o->im6o_multicast_ifp = ifp;
2555 break;
2556
2557 case IPV6_MULTICAST_HOPS:
2558 {
2559 /*
2560 * Set the IP6 hoplimit for outgoing multicast packets.
2561 */
2562 int optval;
2563 if (m == NULL || m->m_len != sizeof(int)) {
2564 error = EINVAL;
2565 break;
2566 }
2567 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2568 if (optval < -1 || optval >= 256)
2569 error = EINVAL;
2570 else if (optval == -1)
2571 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2572 else
2573 im6o->im6o_multicast_hlim = optval;
2574 break;
2575 }
2576
2577 case IPV6_MULTICAST_LOOP:
2578 /*
2579 * Set the loopback flag for outgoing multicast packets.
2580 * Must be zero or one.
2581 */
2582 if (m == NULL || m->m_len != sizeof(u_int)) {
2583 error = EINVAL;
2584 break;
2585 }
2586 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2587 if (loop > 1) {
2588 error = EINVAL;
2589 break;
2590 }
2591 im6o->im6o_multicast_loop = loop;
2592 break;
2593
2594 case IPV6_JOIN_GROUP:
2595 /*
2596 * Add a multicast group membership.
2597 * Group must be a valid IP6 multicast address.
2598 */
2599 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2600 error = EINVAL;
2601 break;
2602 }
2603 mreq = mtod(m, struct ipv6_mreq *);
2604 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2605 /*
2606 * We use the unspecified address to specify to accept
2607 * all multicast addresses. Only super user is allowed
2608 * to do this.
2609 */
2610 if (kauth_authorize_generic(l->l_cred,
2611 KAUTH_GENERIC_ISSUSER, NULL))
2612 {
2613 error = EACCES;
2614 break;
2615 }
2616 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2617 error = EINVAL;
2618 break;
2619 }
2620
2621 /*
2622 * If no interface was explicitly specified, choose an
2623 * appropriate one according to the given multicast address.
2624 */
2625 if (mreq->ipv6mr_interface == 0) {
2626 union {
2627 struct sockaddr dst;
2628 struct sockaddr_in6 dst6;
2629 } u;
2630
2631 /*
2632 * Look up the routing table for the
2633 * address, and choose the outgoing interface.
2634 * XXX: is it a good approach?
2635 */
2636 memset(&ro, 0, sizeof(ro));
2637 sockaddr_in6_init(&u.dst6, &mreq->ipv6mr_multiaddr, 0,
2638 0, 0);
2639 rtcache_setdst(&ro, &u.dst);
2640 rtcache_init(&ro);
2641 ifp = (ro.ro_rt != NULL) ? ro.ro_rt->rt_ifp : NULL;
2642 rtcache_free(&ro);
2643 } else {
2644 /*
2645 * If the interface is specified, validate it.
2646 */
2647 if (if_indexlim <= mreq->ipv6mr_interface ||
2648 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2649 error = ENXIO; /* XXX EINVAL? */
2650 break;
2651 }
2652 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2653 }
2654
2655 /*
2656 * See if we found an interface, and confirm that it
2657 * supports multicast
2658 */
2659 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2660 error = EADDRNOTAVAIL;
2661 break;
2662 }
2663
2664 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2665 error = EADDRNOTAVAIL; /* XXX: should not happen */
2666 break;
2667 }
2668
2669 /*
2670 * See if the membership already exists.
2671 */
2672 for (imm = im6o->im6o_memberships.lh_first;
2673 imm != NULL; imm = imm->i6mm_chain.le_next)
2674 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2675 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2676 &mreq->ipv6mr_multiaddr))
2677 break;
2678 if (imm != NULL) {
2679 error = EADDRINUSE;
2680 break;
2681 }
2682 /*
2683 * Everything looks good; add a new record to the multicast
2684 * address list for the given interface.
2685 */
2686 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2687 if (imm == NULL)
2688 break;
2689 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2690 break;
2691
2692 case IPV6_LEAVE_GROUP:
2693 /*
2694 * Drop a multicast group membership.
2695 * Group must be a valid IP6 multicast address.
2696 */
2697 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2698 error = EINVAL;
2699 break;
2700 }
2701 mreq = mtod(m, struct ipv6_mreq *);
2702
2703 /*
2704 * If an interface address was specified, get a pointer
2705 * to its ifnet structure.
2706 */
2707 if (mreq->ipv6mr_interface != 0) {
2708 if (if_indexlim <= mreq->ipv6mr_interface ||
2709 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2710 error = ENXIO; /* XXX EINVAL? */
2711 break;
2712 }
2713 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2714 } else
2715 ifp = NULL;
2716
2717 /* Fill in the scope zone ID */
2718 if (ifp) {
2719 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2720 /* XXX: should not happen */
2721 error = EADDRNOTAVAIL;
2722 break;
2723 }
2724 } else if (mreq->ipv6mr_interface != 0) {
2725 /*
2726 * XXX: This case would happens when the (positive)
2727 * index is in the valid range, but the corresponding
2728 * interface has been detached dynamically. The above
2729 * check probably avoids such case to happen here, but
2730 * we check it explicitly for safety.
2731 */
2732 error = EADDRNOTAVAIL;
2733 break;
2734 } else { /* ipv6mr_interface == 0 */
2735 struct sockaddr_in6 sa6_mc;
2736
2737 /*
2738 * The API spec says as follows:
2739 * If the interface index is specified as 0, the
2740 * system may choose a multicast group membership to
2741 * drop by matching the multicast address only.
2742 * On the other hand, we cannot disambiguate the scope
2743 * zone unless an interface is provided. Thus, we
2744 * check if there's ambiguity with the default scope
2745 * zone as the last resort.
2746 */
2747 bzero(&sa6_mc, sizeof(sa6_mc));
2748 sa6_mc.sin6_family = AF_INET6;
2749 sa6_mc.sin6_len = sizeof(sa6_mc);
2750 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2751 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2752 if (error != 0)
2753 break;
2754 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2755 }
2756
2757 /*
2758 * Find the membership in the membership list.
2759 */
2760 for (imm = im6o->im6o_memberships.lh_first;
2761 imm != NULL; imm = imm->i6mm_chain.le_next) {
2762 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2763 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2764 &mreq->ipv6mr_multiaddr))
2765 break;
2766 }
2767 if (imm == NULL) {
2768 /* Unable to resolve interface */
2769 error = EADDRNOTAVAIL;
2770 break;
2771 }
2772 /*
2773 * Give up the multicast address record to which the
2774 * membership points.
2775 */
2776 LIST_REMOVE(imm, i6mm_chain);
2777 in6_leavegroup(imm);
2778 break;
2779
2780 default:
2781 error = EOPNOTSUPP;
2782 break;
2783 }
2784
2785 /*
2786 * If all options have default values, no need to keep the mbuf.
2787 */
2788 if (im6o->im6o_multicast_ifp == NULL &&
2789 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2790 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2791 im6o->im6o_memberships.lh_first == NULL) {
2792 free(*im6op, M_IPMOPTS);
2793 *im6op = NULL;
2794 }
2795
2796 return (error);
2797 }
2798
2799 /*
2800 * Return the IP6 multicast options in response to user getsockopt().
2801 */
2802 static int
2803 ip6_getmoptions(optname, im6o, mp)
2804 int optname;
2805 struct ip6_moptions *im6o;
2806 struct mbuf **mp;
2807 {
2808 u_int *hlim, *loop, *ifindex;
2809
2810 *mp = m_get(M_WAIT, MT_SOOPTS);
2811
2812 switch (optname) {
2813
2814 case IPV6_MULTICAST_IF:
2815 ifindex = mtod(*mp, u_int *);
2816 (*mp)->m_len = sizeof(u_int);
2817 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2818 *ifindex = 0;
2819 else
2820 *ifindex = im6o->im6o_multicast_ifp->if_index;
2821 return (0);
2822
2823 case IPV6_MULTICAST_HOPS:
2824 hlim = mtod(*mp, u_int *);
2825 (*mp)->m_len = sizeof(u_int);
2826 if (im6o == NULL)
2827 *hlim = ip6_defmcasthlim;
2828 else
2829 *hlim = im6o->im6o_multicast_hlim;
2830 return (0);
2831
2832 case IPV6_MULTICAST_LOOP:
2833 loop = mtod(*mp, u_int *);
2834 (*mp)->m_len = sizeof(u_int);
2835 if (im6o == NULL)
2836 *loop = ip6_defmcasthlim;
2837 else
2838 *loop = im6o->im6o_multicast_loop;
2839 return (0);
2840
2841 default:
2842 return (EOPNOTSUPP);
2843 }
2844 }
2845
2846 /*
2847 * Discard the IP6 multicast options.
2848 */
2849 void
2850 ip6_freemoptions(im6o)
2851 struct ip6_moptions *im6o;
2852 {
2853 struct in6_multi_mship *imm;
2854
2855 if (im6o == NULL)
2856 return;
2857
2858 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2859 LIST_REMOVE(imm, i6mm_chain);
2860 in6_leavegroup(imm);
2861 }
2862 free(im6o, M_IPMOPTS);
2863 }
2864
2865 /*
2866 * Set IPv6 outgoing packet options based on advanced API.
2867 */
2868 int
2869 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2870 struct mbuf *control;
2871 struct ip6_pktopts *opt, *stickyopt;
2872 int priv, uproto;
2873 {
2874 struct cmsghdr *cm = 0;
2875
2876 if (control == NULL || opt == NULL)
2877 return (EINVAL);
2878
2879 ip6_initpktopts(opt);
2880 if (stickyopt) {
2881 int error;
2882
2883 /*
2884 * If stickyopt is provided, make a local copy of the options
2885 * for this particular packet, then override them by ancillary
2886 * objects.
2887 * XXX: copypktopts() does not copy the cached route to a next
2888 * hop (if any). This is not very good in terms of efficiency,
2889 * but we can allow this since this option should be rarely
2890 * used.
2891 */
2892 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2893 return (error);
2894 }
2895
2896 /*
2897 * XXX: Currently, we assume all the optional information is stored
2898 * in a single mbuf.
2899 */
2900 if (control->m_next)
2901 return (EINVAL);
2902
2903 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2904 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2905 int error;
2906
2907 if (control->m_len < CMSG_LEN(0))
2908 return (EINVAL);
2909
2910 cm = mtod(control, struct cmsghdr *);
2911 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2912 return (EINVAL);
2913 if (cm->cmsg_level != IPPROTO_IPV6)
2914 continue;
2915
2916 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2917 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2918 if (error)
2919 return (error);
2920 }
2921
2922 return (0);
2923 }
2924
2925 /*
2926 * Set a particular packet option, as a sticky option or an ancillary data
2927 * item. "len" can be 0 only when it's a sticky option.
2928 * We have 4 cases of combination of "sticky" and "cmsg":
2929 * "sticky=0, cmsg=0": impossible
2930 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2931 * "sticky=1, cmsg=0": RFC3542 socket option
2932 * "sticky=1, cmsg=1": RFC2292 socket option
2933 */
2934 static int
2935 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2936 int priv, int sticky, int cmsg, int uproto)
2937 {
2938 int minmtupolicy;
2939
2940 if (!sticky && !cmsg) {
2941 #ifdef DIAGNOSTIC
2942 printf("ip6_setpktopt: impossible case\n");
2943 #endif
2944 return (EINVAL);
2945 }
2946
2947 /*
2948 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2949 * not be specified in the context of RFC3542. Conversely,
2950 * RFC3542 types should not be specified in the context of RFC2292.
2951 */
2952 if (!cmsg) {
2953 switch (optname) {
2954 case IPV6_2292PKTINFO:
2955 case IPV6_2292HOPLIMIT:
2956 case IPV6_2292NEXTHOP:
2957 case IPV6_2292HOPOPTS:
2958 case IPV6_2292DSTOPTS:
2959 case IPV6_2292RTHDR:
2960 case IPV6_2292PKTOPTIONS:
2961 return (ENOPROTOOPT);
2962 }
2963 }
2964 if (sticky && cmsg) {
2965 switch (optname) {
2966 case IPV6_PKTINFO:
2967 case IPV6_HOPLIMIT:
2968 case IPV6_NEXTHOP:
2969 case IPV6_HOPOPTS:
2970 case IPV6_DSTOPTS:
2971 case IPV6_RTHDRDSTOPTS:
2972 case IPV6_RTHDR:
2973 case IPV6_USE_MIN_MTU:
2974 case IPV6_DONTFRAG:
2975 case IPV6_OTCLASS:
2976 case IPV6_TCLASS:
2977 return (ENOPROTOOPT);
2978 }
2979 }
2980
2981 switch (optname) {
2982 #ifdef RFC2292
2983 case IPV6_2292PKTINFO:
2984 #endif
2985 case IPV6_PKTINFO:
2986 {
2987 struct ifnet *ifp = NULL;
2988 struct in6_pktinfo *pktinfo;
2989
2990 if (len != sizeof(struct in6_pktinfo))
2991 return (EINVAL);
2992
2993 pktinfo = (struct in6_pktinfo *)buf;
2994
2995 /*
2996 * An application can clear any sticky IPV6_PKTINFO option by
2997 * doing a "regular" setsockopt with ipi6_addr being
2998 * in6addr_any and ipi6_ifindex being zero.
2999 * [RFC 3542, Section 6]
3000 */
3001 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3002 pktinfo->ipi6_ifindex == 0 &&
3003 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3004 ip6_clearpktopts(opt, optname);
3005 break;
3006 }
3007
3008 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3009 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3010 return (EINVAL);
3011 }
3012
3013 /* validate the interface index if specified. */
3014 if (pktinfo->ipi6_ifindex >= if_indexlim) {
3015 return (ENXIO);
3016 }
3017 if (pktinfo->ipi6_ifindex) {
3018 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
3019 if (ifp == NULL)
3020 return (ENXIO);
3021 }
3022
3023 /*
3024 * We store the address anyway, and let in6_selectsrc()
3025 * validate the specified address. This is because ipi6_addr
3026 * may not have enough information about its scope zone, and
3027 * we may need additional information (such as outgoing
3028 * interface or the scope zone of a destination address) to
3029 * disambiguate the scope.
3030 * XXX: the delay of the validation may confuse the
3031 * application when it is used as a sticky option.
3032 */
3033 if (opt->ip6po_pktinfo == NULL) {
3034 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3035 M_IP6OPT, M_NOWAIT);
3036 if (opt->ip6po_pktinfo == NULL)
3037 return (ENOBUFS);
3038 }
3039 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
3040 break;
3041 }
3042
3043 #ifdef RFC2292
3044 case IPV6_2292HOPLIMIT:
3045 #endif
3046 case IPV6_HOPLIMIT:
3047 {
3048 int *hlimp;
3049
3050 /*
3051 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3052 * to simplify the ordering among hoplimit options.
3053 */
3054 if (optname == IPV6_HOPLIMIT && sticky)
3055 return (ENOPROTOOPT);
3056
3057 if (len != sizeof(int))
3058 return (EINVAL);
3059 hlimp = (int *)buf;
3060 if (*hlimp < -1 || *hlimp > 255)
3061 return (EINVAL);
3062
3063 opt->ip6po_hlim = *hlimp;
3064 break;
3065 }
3066
3067 case IPV6_OTCLASS:
3068 if (len != sizeof(u_int8_t))
3069 return (EINVAL);
3070
3071 opt->ip6po_tclass = *(u_int8_t *)buf;
3072 break;
3073
3074 case IPV6_TCLASS:
3075 {
3076 int tclass;
3077
3078 if (len != sizeof(int))
3079 return (EINVAL);
3080 tclass = *(int *)buf;
3081 if (tclass < -1 || tclass > 255)
3082 return (EINVAL);
3083
3084 opt->ip6po_tclass = tclass;
3085 break;
3086 }
3087
3088 #ifdef RFC2292
3089 case IPV6_2292NEXTHOP:
3090 #endif
3091 case IPV6_NEXTHOP:
3092 if (!priv)
3093 return (EPERM);
3094
3095 if (len == 0) { /* just remove the option */
3096 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3097 break;
3098 }
3099
3100 /* check if cmsg_len is large enough for sa_len */
3101 if (len < sizeof(struct sockaddr) || len < *buf)
3102 return (EINVAL);
3103
3104 switch (((struct sockaddr *)buf)->sa_family) {
3105 case AF_INET6:
3106 {
3107 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3108 int error;
3109
3110 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3111 return (EINVAL);
3112
3113 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3114 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3115 return (EINVAL);
3116 }
3117 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3118 != 0) {
3119 return (error);
3120 }
3121 break;
3122 }
3123 case AF_LINK: /* eventually be supported? */
3124 default:
3125 return (EAFNOSUPPORT);
3126 }
3127
3128 /* turn off the previous option, then set the new option. */
3129 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3130 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3131 if (opt->ip6po_nexthop == NULL)
3132 return (ENOBUFS);
3133 memcpy(opt->ip6po_nexthop, buf, *buf);
3134 break;
3135
3136 #ifdef RFC2292
3137 case IPV6_2292HOPOPTS:
3138 #endif
3139 case IPV6_HOPOPTS:
3140 {
3141 struct ip6_hbh *hbh;
3142 int hbhlen;
3143
3144 /*
3145 * XXX: We don't allow a non-privileged user to set ANY HbH
3146 * options, since per-option restriction has too much
3147 * overhead.
3148 */
3149 if (!priv)
3150 return (EPERM);
3151
3152 if (len == 0) {
3153 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3154 break; /* just remove the option */
3155 }
3156
3157 /* message length validation */
3158 if (len < sizeof(struct ip6_hbh))
3159 return (EINVAL);
3160 hbh = (struct ip6_hbh *)buf;
3161 hbhlen = (hbh->ip6h_len + 1) << 3;
3162 if (len != hbhlen)
3163 return (EINVAL);
3164
3165 /* turn off the previous option, then set the new option. */
3166 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3167 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3168 if (opt->ip6po_hbh == NULL)
3169 return (ENOBUFS);
3170 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3171
3172 break;
3173 }
3174
3175 #ifdef RFC2292
3176 case IPV6_2292DSTOPTS:
3177 #endif
3178 case IPV6_DSTOPTS:
3179 case IPV6_RTHDRDSTOPTS:
3180 {
3181 struct ip6_dest *dest, **newdest = NULL;
3182 int destlen;
3183
3184 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3185 return (EPERM);
3186
3187 if (len == 0) {
3188 ip6_clearpktopts(opt, optname);
3189 break; /* just remove the option */
3190 }
3191
3192 /* message length validation */
3193 if (len < sizeof(struct ip6_dest))
3194 return (EINVAL);
3195 dest = (struct ip6_dest *)buf;
3196 destlen = (dest->ip6d_len + 1) << 3;
3197 if (len != destlen)
3198 return (EINVAL);
3199 /*
3200 * Determine the position that the destination options header
3201 * should be inserted; before or after the routing header.
3202 */
3203 switch (optname) {
3204 case IPV6_2292DSTOPTS:
3205 /*
3206 * The old advanced API is ambiguous on this point.
3207 * Our approach is to determine the position based
3208 * according to the existence of a routing header.
3209 * Note, however, that this depends on the order of the
3210 * extension headers in the ancillary data; the 1st
3211 * part of the destination options header must appear
3212 * before the routing header in the ancillary data,
3213 * too.
3214 * RFC3542 solved the ambiguity by introducing
3215 * separate ancillary data or option types.
3216 */
3217 if (opt->ip6po_rthdr == NULL)
3218 newdest = &opt->ip6po_dest1;
3219 else
3220 newdest = &opt->ip6po_dest2;
3221 break;
3222 case IPV6_RTHDRDSTOPTS:
3223 newdest = &opt->ip6po_dest1;
3224 break;
3225 case IPV6_DSTOPTS:
3226 newdest = &opt->ip6po_dest2;
3227 break;
3228 }
3229
3230 /* turn off the previous option, then set the new option. */
3231 ip6_clearpktopts(opt, optname);
3232 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3233 if (*newdest == NULL)
3234 return (ENOBUFS);
3235 memcpy(*newdest, dest, destlen);
3236
3237 break;
3238 }
3239
3240 #ifdef RFC2292
3241 case IPV6_2292RTHDR:
3242 #endif
3243 case IPV6_RTHDR:
3244 {
3245 struct ip6_rthdr *rth;
3246 int rthlen;
3247
3248 if (len == 0) {
3249 ip6_clearpktopts(opt, IPV6_RTHDR);
3250 break; /* just remove the option */
3251 }
3252
3253 /* message length validation */
3254 if (len < sizeof(struct ip6_rthdr))
3255 return (EINVAL);
3256 rth = (struct ip6_rthdr *)buf;
3257 rthlen = (rth->ip6r_len + 1) << 3;
3258 if (len != rthlen)
3259 return (EINVAL);
3260 switch (rth->ip6r_type) {
3261 case IPV6_RTHDR_TYPE_0:
3262 if (rth->ip6r_len == 0) /* must contain one addr */
3263 return (EINVAL);
3264 if (rth->ip6r_len % 2) /* length must be even */
3265 return (EINVAL);
3266 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3267 return (EINVAL);
3268 break;
3269 default:
3270 return (EINVAL); /* not supported */
3271 }
3272 /* turn off the previous option */
3273 ip6_clearpktopts(opt, IPV6_RTHDR);
3274 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3275 if (opt->ip6po_rthdr == NULL)
3276 return (ENOBUFS);
3277 memcpy(opt->ip6po_rthdr, rth, rthlen);
3278 break;
3279 }
3280
3281 case IPV6_USE_MIN_MTU:
3282 if (len != sizeof(int))
3283 return (EINVAL);
3284 minmtupolicy = *(int *)buf;
3285 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3286 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3287 minmtupolicy != IP6PO_MINMTU_ALL) {
3288 return (EINVAL);
3289 }
3290 opt->ip6po_minmtu = minmtupolicy;
3291 break;
3292
3293 case IPV6_DONTFRAG:
3294 if (len != sizeof(int))
3295 return (EINVAL);
3296
3297 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3298 /*
3299 * we ignore this option for TCP sockets.
3300 * (RFC3542 leaves this case unspecified.)
3301 */
3302 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3303 } else
3304 opt->ip6po_flags |= IP6PO_DONTFRAG;
3305 break;
3306
3307 default:
3308 return (ENOPROTOOPT);
3309 } /* end of switch */
3310
3311 return (0);
3312 }
3313
3314 /*
3315 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3316 * packet to the input queue of a specified interface. Note that this
3317 * calls the output routine of the loopback "driver", but with an interface
3318 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3319 */
3320 void
3321 ip6_mloopback(ifp, m, dst)
3322 struct ifnet *ifp;
3323 struct mbuf *m;
3324 const struct sockaddr_in6 *dst;
3325 {
3326 struct mbuf *copym;
3327 struct ip6_hdr *ip6;
3328
3329 copym = m_copy(m, 0, M_COPYALL);
3330 if (copym == NULL)
3331 return;
3332
3333 /*
3334 * Make sure to deep-copy IPv6 header portion in case the data
3335 * is in an mbuf cluster, so that we can safely override the IPv6
3336 * header portion later.
3337 */
3338 if ((copym->m_flags & M_EXT) != 0 ||
3339 copym->m_len < sizeof(struct ip6_hdr)) {
3340 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3341 if (copym == NULL)
3342 return;
3343 }
3344
3345 #ifdef DIAGNOSTIC
3346 if (copym->m_len < sizeof(*ip6)) {
3347 m_freem(copym);
3348 return;
3349 }
3350 #endif
3351
3352 ip6 = mtod(copym, struct ip6_hdr *);
3353 /*
3354 * clear embedded scope identifiers if necessary.
3355 * in6_clearscope will touch the addresses only when necessary.
3356 */
3357 in6_clearscope(&ip6->ip6_src);
3358 in6_clearscope(&ip6->ip6_dst);
3359
3360 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
3361 }
3362
3363 /*
3364 * Chop IPv6 header off from the payload.
3365 */
3366 static int
3367 ip6_splithdr(m, exthdrs)
3368 struct mbuf *m;
3369 struct ip6_exthdrs *exthdrs;
3370 {
3371 struct mbuf *mh;
3372 struct ip6_hdr *ip6;
3373
3374 ip6 = mtod(m, struct ip6_hdr *);
3375 if (m->m_len > sizeof(*ip6)) {
3376 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3377 if (mh == 0) {
3378 m_freem(m);
3379 return ENOBUFS;
3380 }
3381 M_MOVE_PKTHDR(mh, m);
3382 MH_ALIGN(mh, sizeof(*ip6));
3383 m->m_len -= sizeof(*ip6);
3384 m->m_data += sizeof(*ip6);
3385 mh->m_next = m;
3386 m = mh;
3387 m->m_len = sizeof(*ip6);
3388 bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6));
3389 }
3390 exthdrs->ip6e_ip6 = m;
3391 return 0;
3392 }
3393
3394 /*
3395 * Compute IPv6 extension header length.
3396 */
3397 int
3398 ip6_optlen(in6p)
3399 struct in6pcb *in6p;
3400 {
3401 int len;
3402
3403 if (!in6p->in6p_outputopts)
3404 return 0;
3405
3406 len = 0;
3407 #define elen(x) \
3408 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3409
3410 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3411 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3412 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3413 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3414 return len;
3415 #undef elen
3416 }
3417