ip6_output.c revision 1.119 1 /* $NetBSD: ip6_output.c,v 1.119 2007/05/23 17:15:02 christos 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.119 2007/05/23 17:15:02 christos 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(struct mbuf **mp, void *hdr, int hlen)
1231 {
1232 struct mbuf *m;
1233
1234 if (hlen > MCLBYTES)
1235 return (ENOBUFS); /* XXX */
1236
1237 MGET(m, M_DONTWAIT, MT_DATA);
1238 if (!m)
1239 return (ENOBUFS);
1240
1241 if (hlen > MLEN) {
1242 MCLGET(m, M_DONTWAIT);
1243 if ((m->m_flags & M_EXT) == 0) {
1244 m_free(m);
1245 return (ENOBUFS);
1246 }
1247 }
1248 m->m_len = hlen;
1249 if (hdr)
1250 bcopy(hdr, mtod(m, void *), hlen);
1251
1252 *mp = m;
1253 return (0);
1254 }
1255
1256 /*
1257 * Process a delayed payload checksum calculation.
1258 */
1259 void
1260 in6_delayed_cksum(struct mbuf *m)
1261 {
1262 uint16_t csum, offset;
1263
1264 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1265 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1266 KASSERT((m->m_pkthdr.csum_flags
1267 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
1268
1269 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
1270 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1271 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
1272 csum = 0xffff;
1273 }
1274
1275 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
1276 if ((offset + sizeof(csum)) > m->m_len) {
1277 m_copyback(m, offset, sizeof(csum), &csum);
1278 } else {
1279 *(uint16_t *)(mtod(m, char *) + offset) = csum;
1280 }
1281 }
1282
1283 /*
1284 * Insert jumbo payload option.
1285 */
1286 static int
1287 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1288 {
1289 struct mbuf *mopt;
1290 u_int8_t *optbuf;
1291 u_int32_t v;
1292
1293 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1294
1295 /*
1296 * If there is no hop-by-hop options header, allocate new one.
1297 * If there is one but it doesn't have enough space to store the
1298 * jumbo payload option, allocate a cluster to store the whole options.
1299 * Otherwise, use it to store the options.
1300 */
1301 if (exthdrs->ip6e_hbh == 0) {
1302 MGET(mopt, M_DONTWAIT, MT_DATA);
1303 if (mopt == 0)
1304 return (ENOBUFS);
1305 mopt->m_len = JUMBOOPTLEN;
1306 optbuf = mtod(mopt, u_int8_t *);
1307 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1308 exthdrs->ip6e_hbh = mopt;
1309 } else {
1310 struct ip6_hbh *hbh;
1311
1312 mopt = exthdrs->ip6e_hbh;
1313 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1314 /*
1315 * XXX assumption:
1316 * - exthdrs->ip6e_hbh is not referenced from places
1317 * other than exthdrs.
1318 * - exthdrs->ip6e_hbh is not an mbuf chain.
1319 */
1320 int oldoptlen = mopt->m_len;
1321 struct mbuf *n;
1322
1323 /*
1324 * XXX: give up if the whole (new) hbh header does
1325 * not fit even in an mbuf cluster.
1326 */
1327 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1328 return (ENOBUFS);
1329
1330 /*
1331 * As a consequence, we must always prepare a cluster
1332 * at this point.
1333 */
1334 MGET(n, M_DONTWAIT, MT_DATA);
1335 if (n) {
1336 MCLGET(n, M_DONTWAIT);
1337 if ((n->m_flags & M_EXT) == 0) {
1338 m_freem(n);
1339 n = NULL;
1340 }
1341 }
1342 if (!n)
1343 return (ENOBUFS);
1344 n->m_len = oldoptlen + JUMBOOPTLEN;
1345 bcopy(mtod(mopt, void *), mtod(n, void *),
1346 oldoptlen);
1347 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1348 m_freem(mopt);
1349 mopt = exthdrs->ip6e_hbh = n;
1350 } else {
1351 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1352 mopt->m_len += JUMBOOPTLEN;
1353 }
1354 optbuf[0] = IP6OPT_PADN;
1355 optbuf[1] = 0;
1356
1357 /*
1358 * Adjust the header length according to the pad and
1359 * the jumbo payload option.
1360 */
1361 hbh = mtod(mopt, struct ip6_hbh *);
1362 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1363 }
1364
1365 /* fill in the option. */
1366 optbuf[2] = IP6OPT_JUMBO;
1367 optbuf[3] = 4;
1368 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1369 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1370
1371 /* finally, adjust the packet header length */
1372 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1373
1374 return (0);
1375 #undef JUMBOOPTLEN
1376 }
1377
1378 /*
1379 * Insert fragment header and copy unfragmentable header portions.
1380 */
1381 static int
1382 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1383 struct ip6_frag **frghdrp)
1384 {
1385 struct mbuf *n, *mlast;
1386
1387 if (hlen > sizeof(struct ip6_hdr)) {
1388 n = m_copym(m0, sizeof(struct ip6_hdr),
1389 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1390 if (n == 0)
1391 return (ENOBUFS);
1392 m->m_next = n;
1393 } else
1394 n = m;
1395
1396 /* Search for the last mbuf of unfragmentable part. */
1397 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1398 ;
1399
1400 if ((mlast->m_flags & M_EXT) == 0 &&
1401 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1402 /* use the trailing space of the last mbuf for the fragment hdr */
1403 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
1404 mlast->m_len);
1405 mlast->m_len += sizeof(struct ip6_frag);
1406 m->m_pkthdr.len += sizeof(struct ip6_frag);
1407 } else {
1408 /* allocate a new mbuf for the fragment header */
1409 struct mbuf *mfrg;
1410
1411 MGET(mfrg, M_DONTWAIT, MT_DATA);
1412 if (mfrg == 0)
1413 return (ENOBUFS);
1414 mfrg->m_len = sizeof(struct ip6_frag);
1415 *frghdrp = mtod(mfrg, struct ip6_frag *);
1416 mlast->m_next = mfrg;
1417 }
1418
1419 return (0);
1420 }
1421
1422 static int
1423 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp,
1424 const struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
1425 {
1426 u_int32_t mtu = 0;
1427 int alwaysfrag = 0;
1428 int error = 0;
1429
1430 if (ro_pmtu != ro) {
1431 union {
1432 struct sockaddr dst;
1433 struct sockaddr_in6 dst6;
1434 } u;
1435
1436 /* The first hop and the final destination may differ. */
1437 sockaddr_in6_init(&u.dst6, dst, 0, 0, 0);
1438 rtcache_lookup(ro_pmtu, &u.dst);
1439 }
1440 if (ro_pmtu->ro_rt != NULL) {
1441 u_int32_t ifmtu;
1442
1443 if (ifp == NULL)
1444 ifp = ro_pmtu->ro_rt->rt_ifp;
1445 ifmtu = IN6_LINKMTU(ifp);
1446 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1447 if (mtu == 0)
1448 mtu = ifmtu;
1449 else if (mtu < IPV6_MMTU) {
1450 /*
1451 * RFC2460 section 5, last paragraph:
1452 * if we record ICMPv6 too big message with
1453 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1454 * or smaller, with fragment header attached.
1455 * (fragment header is needed regardless from the
1456 * packet size, for translators to identify packets)
1457 */
1458 alwaysfrag = 1;
1459 mtu = IPV6_MMTU;
1460 } else if (mtu > ifmtu) {
1461 /*
1462 * The MTU on the route is larger than the MTU on
1463 * the interface! This shouldn't happen, unless the
1464 * MTU of the interface has been changed after the
1465 * interface was brought up. Change the MTU in the
1466 * route to match the interface MTU (as long as the
1467 * field isn't locked).
1468 */
1469 mtu = ifmtu;
1470 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
1471 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1472 }
1473 } else if (ifp) {
1474 mtu = IN6_LINKMTU(ifp);
1475 } else
1476 error = EHOSTUNREACH; /* XXX */
1477
1478 *mtup = mtu;
1479 if (alwaysfragp)
1480 *alwaysfragp = alwaysfrag;
1481 return (error);
1482 }
1483
1484 /*
1485 * IP6 socket option processing.
1486 */
1487 int
1488 ip6_ctloutput(int op, struct socket *so, int level, int optname,
1489 struct mbuf **mp)
1490 {
1491 int privileged, optdatalen, uproto;
1492 void *optdata;
1493 struct in6pcb *in6p = sotoin6pcb(so);
1494 struct mbuf *m = *mp;
1495 int error, optval;
1496 int optlen;
1497 struct lwp *l = curlwp; /* XXX */
1498
1499 optlen = m ? m->m_len : 0;
1500 error = optval = 0;
1501 privileged = (l == 0 || kauth_authorize_generic(l->l_cred,
1502 KAUTH_GENERIC_ISSUSER, NULL)) ? 0 : 1;
1503 uproto = (int)so->so_proto->pr_protocol;
1504
1505 if (level == IPPROTO_IPV6) {
1506 switch (op) {
1507 case PRCO_SETOPT:
1508 switch (optname) {
1509 #ifdef RFC2292
1510 case IPV6_2292PKTOPTIONS:
1511 /* m is freed in ip6_pcbopts */
1512 error = ip6_pcbopts(&in6p->in6p_outputopts,
1513 m, so);
1514 break;
1515 #endif
1516
1517 /*
1518 * Use of some Hop-by-Hop options or some
1519 * Destination options, might require special
1520 * privilege. That is, normal applications
1521 * (without special privilege) might be forbidden
1522 * from setting certain options in outgoing packets,
1523 * and might never see certain options in received
1524 * packets. [RFC 2292 Section 6]
1525 * KAME specific note:
1526 * KAME prevents non-privileged users from sending or
1527 * receiving ANY hbh/dst options in order to avoid
1528 * overhead of parsing options in the kernel.
1529 */
1530 case IPV6_RECVHOPOPTS:
1531 case IPV6_RECVDSTOPTS:
1532 case IPV6_RECVRTHDRDSTOPTS:
1533 if (!privileged) {
1534 error = EPERM;
1535 break;
1536 }
1537 /* FALLTHROUGH */
1538 case IPV6_UNICAST_HOPS:
1539 case IPV6_HOPLIMIT:
1540 case IPV6_FAITH:
1541
1542 case IPV6_RECVPKTINFO:
1543 case IPV6_RECVHOPLIMIT:
1544 case IPV6_RECVRTHDR:
1545 case IPV6_RECVPATHMTU:
1546 case IPV6_RECVTCLASS:
1547 case IPV6_V6ONLY:
1548 if (optlen != sizeof(int)) {
1549 error = EINVAL;
1550 break;
1551 }
1552 optval = *mtod(m, int *);
1553 switch (optname) {
1554
1555 case IPV6_UNICAST_HOPS:
1556 if (optval < -1 || optval >= 256)
1557 error = EINVAL;
1558 else {
1559 /* -1 = kernel default */
1560 in6p->in6p_hops = optval;
1561 }
1562 break;
1563 #define OPTSET(bit) \
1564 do { \
1565 if (optval) \
1566 in6p->in6p_flags |= (bit); \
1567 else \
1568 in6p->in6p_flags &= ~(bit); \
1569 } while (/*CONSTCOND*/ 0)
1570
1571 #ifdef RFC2292
1572 #define OPTSET2292(bit) \
1573 do { \
1574 in6p->in6p_flags |= IN6P_RFC2292; \
1575 if (optval) \
1576 in6p->in6p_flags |= (bit); \
1577 else \
1578 in6p->in6p_flags &= ~(bit); \
1579 } while (/*CONSTCOND*/ 0)
1580 #endif
1581
1582 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1583
1584 case IPV6_RECVPKTINFO:
1585 #ifdef RFC2292
1586 /* cannot mix with RFC2292 */
1587 if (OPTBIT(IN6P_RFC2292)) {
1588 error = EINVAL;
1589 break;
1590 }
1591 #endif
1592 OPTSET(IN6P_PKTINFO);
1593 break;
1594
1595 case IPV6_HOPLIMIT:
1596 {
1597 struct ip6_pktopts **optp;
1598
1599 #ifdef RFC2292
1600 /* cannot mix with RFC2292 */
1601 if (OPTBIT(IN6P_RFC2292)) {
1602 error = EINVAL;
1603 break;
1604 }
1605 #endif
1606 optp = &in6p->in6p_outputopts;
1607 error = ip6_pcbopt(IPV6_HOPLIMIT,
1608 (u_char *)&optval,
1609 sizeof(optval),
1610 optp,
1611 privileged, uproto);
1612 break;
1613 }
1614
1615 case IPV6_RECVHOPLIMIT:
1616 #ifdef RFC2292
1617 /* cannot mix with RFC2292 */
1618 if (OPTBIT(IN6P_RFC2292)) {
1619 error = EINVAL;
1620 break;
1621 }
1622 #endif
1623 OPTSET(IN6P_HOPLIMIT);
1624 break;
1625
1626 case IPV6_RECVHOPOPTS:
1627 #ifdef RFC2292
1628 /* cannot mix with RFC2292 */
1629 if (OPTBIT(IN6P_RFC2292)) {
1630 error = EINVAL;
1631 break;
1632 }
1633 #endif
1634 OPTSET(IN6P_HOPOPTS);
1635 break;
1636
1637 case IPV6_RECVDSTOPTS:
1638 #ifdef RFC2292
1639 /* cannot mix with RFC2292 */
1640 if (OPTBIT(IN6P_RFC2292)) {
1641 error = EINVAL;
1642 break;
1643 }
1644 #endif
1645 OPTSET(IN6P_DSTOPTS);
1646 break;
1647
1648 case IPV6_RECVRTHDRDSTOPTS:
1649 #ifdef RFC2292
1650 /* cannot mix with RFC2292 */
1651 if (OPTBIT(IN6P_RFC2292)) {
1652 error = EINVAL;
1653 break;
1654 }
1655 #endif
1656 OPTSET(IN6P_RTHDRDSTOPTS);
1657 break;
1658
1659 case IPV6_RECVRTHDR:
1660 #ifdef RFC2292
1661 /* cannot mix with RFC2292 */
1662 if (OPTBIT(IN6P_RFC2292)) {
1663 error = EINVAL;
1664 break;
1665 }
1666 #endif
1667 OPTSET(IN6P_RTHDR);
1668 break;
1669
1670 case IPV6_FAITH:
1671 OPTSET(IN6P_FAITH);
1672 break;
1673
1674 case IPV6_RECVPATHMTU:
1675 /*
1676 * We ignore this option for TCP
1677 * sockets.
1678 * (RFC3542 leaves this case
1679 * unspecified.)
1680 */
1681 if (uproto != IPPROTO_TCP)
1682 OPTSET(IN6P_MTU);
1683 break;
1684
1685 case IPV6_V6ONLY:
1686 /*
1687 * make setsockopt(IPV6_V6ONLY)
1688 * available only prior to bind(2).
1689 * see ipng mailing list, Jun 22 2001.
1690 */
1691 if (in6p->in6p_lport ||
1692 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1693 error = EINVAL;
1694 break;
1695 }
1696 #ifdef INET6_BINDV6ONLY
1697 if (!optval)
1698 error = EINVAL;
1699 #else
1700 OPTSET(IN6P_IPV6_V6ONLY);
1701 #endif
1702 break;
1703 case IPV6_RECVTCLASS:
1704 #ifdef RFC2292
1705 /* cannot mix with RFC2292 XXX */
1706 if (OPTBIT(IN6P_RFC2292)) {
1707 error = EINVAL;
1708 break;
1709 }
1710 #endif
1711 OPTSET(IN6P_TCLASS);
1712 break;
1713
1714 }
1715 break;
1716
1717 case IPV6_OTCLASS:
1718 {
1719 struct ip6_pktopts **optp;
1720 u_int8_t tclass;
1721
1722 if (optlen != sizeof(tclass)) {
1723 error = EINVAL;
1724 break;
1725 }
1726 tclass = *mtod(m, u_int8_t *);
1727 optp = &in6p->in6p_outputopts;
1728 error = ip6_pcbopt(optname,
1729 (u_char *)&tclass,
1730 sizeof(tclass),
1731 optp,
1732 privileged, uproto);
1733 break;
1734 }
1735
1736 case IPV6_TCLASS:
1737 case IPV6_DONTFRAG:
1738 case IPV6_USE_MIN_MTU:
1739 if (optlen != sizeof(optval)) {
1740 error = EINVAL;
1741 break;
1742 }
1743 optval = *mtod(m, int *);
1744 {
1745 struct ip6_pktopts **optp;
1746 optp = &in6p->in6p_outputopts;
1747 error = ip6_pcbopt(optname,
1748 (u_char *)&optval,
1749 sizeof(optval),
1750 optp,
1751 privileged, uproto);
1752 break;
1753 }
1754
1755 #ifdef RFC2292
1756 case IPV6_2292PKTINFO:
1757 case IPV6_2292HOPLIMIT:
1758 case IPV6_2292HOPOPTS:
1759 case IPV6_2292DSTOPTS:
1760 case IPV6_2292RTHDR:
1761 /* RFC 2292 */
1762 if (optlen != sizeof(int)) {
1763 error = EINVAL;
1764 break;
1765 }
1766 optval = *mtod(m, int *);
1767 switch (optname) {
1768 case IPV6_2292PKTINFO:
1769 OPTSET2292(IN6P_PKTINFO);
1770 break;
1771 case IPV6_2292HOPLIMIT:
1772 OPTSET2292(IN6P_HOPLIMIT);
1773 break;
1774 case IPV6_2292HOPOPTS:
1775 /*
1776 * Check super-user privilege.
1777 * See comments for IPV6_RECVHOPOPTS.
1778 */
1779 if (!privileged)
1780 return (EPERM);
1781 OPTSET2292(IN6P_HOPOPTS);
1782 break;
1783 case IPV6_2292DSTOPTS:
1784 if (!privileged)
1785 return (EPERM);
1786 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1787 break;
1788 case IPV6_2292RTHDR:
1789 OPTSET2292(IN6P_RTHDR);
1790 break;
1791 }
1792 break;
1793 #endif
1794 case IPV6_PKTINFO:
1795 case IPV6_HOPOPTS:
1796 case IPV6_RTHDR:
1797 case IPV6_DSTOPTS:
1798 case IPV6_RTHDRDSTOPTS:
1799 case IPV6_NEXTHOP:
1800 {
1801 /* new advanced API (RFC3542) */
1802 u_char *optbuf;
1803 int optbuflen;
1804 struct ip6_pktopts **optp;
1805 if (!m) {
1806 error = EINVAL;
1807 break;
1808 }
1809
1810 #ifdef RFC2292
1811 /* cannot mix with RFC2292 */
1812 if (OPTBIT(IN6P_RFC2292)) {
1813 error = EINVAL;
1814 break;
1815 }
1816 #endif
1817
1818 if (m && m->m_next) {
1819 error = EINVAL; /* XXX */
1820 break;
1821 }
1822
1823 optbuf = mtod(m, u_char *);
1824 optbuflen = m->m_len;
1825 optp = &in6p->in6p_outputopts;
1826 error = ip6_pcbopt(optname, optbuf, optbuflen,
1827 optp, privileged, uproto);
1828 break;
1829 }
1830 #undef OPTSET
1831
1832 case IPV6_MULTICAST_IF:
1833 case IPV6_MULTICAST_HOPS:
1834 case IPV6_MULTICAST_LOOP:
1835 case IPV6_JOIN_GROUP:
1836 case IPV6_LEAVE_GROUP:
1837 error = ip6_setmoptions(optname,
1838 &in6p->in6p_moptions, m);
1839 break;
1840
1841 case IPV6_PORTRANGE:
1842 if (!m) {
1843 error = EINVAL;
1844 break;
1845 }
1846 optval = *mtod(m, int *);
1847
1848 switch (optval) {
1849 case IPV6_PORTRANGE_DEFAULT:
1850 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1851 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1852 break;
1853
1854 case IPV6_PORTRANGE_HIGH:
1855 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1856 in6p->in6p_flags |= IN6P_HIGHPORT;
1857 break;
1858
1859 case IPV6_PORTRANGE_LOW:
1860 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1861 in6p->in6p_flags |= IN6P_LOWPORT;
1862 break;
1863
1864 default:
1865 error = EINVAL;
1866 break;
1867 }
1868 break;
1869
1870
1871 #if defined(IPSEC) || defined(FAST_IPSEC)
1872 case IPV6_IPSEC_POLICY:
1873 {
1874 void *req = NULL;
1875 size_t len = 0;
1876 if (m) {
1877 req = mtod(m, void *);
1878 len = m->m_len;
1879 }
1880 error = ipsec6_set_policy(in6p, optname, req,
1881 len, privileged);
1882 }
1883 break;
1884 #endif /* IPSEC */
1885
1886 default:
1887 error = ENOPROTOOPT;
1888 break;
1889 }
1890 if (m)
1891 (void)m_free(m);
1892 break;
1893
1894 case PRCO_GETOPT:
1895 switch (optname) {
1896 #ifdef RFC2292
1897 case IPV6_2292PKTOPTIONS:
1898 /*
1899 * RFC3542 (effectively) deprecated the
1900 * semantics of the 2292-style pktoptions.
1901 * Since it was not reliable in nature (i.e.,
1902 * applications had to expect the lack of some
1903 * information after all), it would make sense
1904 * to simplify this part by always returning
1905 * empty data.
1906 */
1907 *mp = m_get(M_WAIT, MT_SOOPTS);
1908 (*mp)->m_len = 0;
1909 break;
1910 #endif
1911
1912 case IPV6_RECVHOPOPTS:
1913 case IPV6_RECVDSTOPTS:
1914 case IPV6_RECVRTHDRDSTOPTS:
1915 case IPV6_UNICAST_HOPS:
1916 case IPV6_RECVPKTINFO:
1917 case IPV6_RECVHOPLIMIT:
1918 case IPV6_RECVRTHDR:
1919 case IPV6_RECVPATHMTU:
1920
1921 case IPV6_FAITH:
1922 case IPV6_V6ONLY:
1923 case IPV6_PORTRANGE:
1924 case IPV6_RECVTCLASS:
1925 switch (optname) {
1926
1927 case IPV6_RECVHOPOPTS:
1928 optval = OPTBIT(IN6P_HOPOPTS);
1929 break;
1930
1931 case IPV6_RECVDSTOPTS:
1932 optval = OPTBIT(IN6P_DSTOPTS);
1933 break;
1934
1935 case IPV6_RECVRTHDRDSTOPTS:
1936 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1937 break;
1938
1939 case IPV6_UNICAST_HOPS:
1940 optval = in6p->in6p_hops;
1941 break;
1942
1943 case IPV6_RECVPKTINFO:
1944 optval = OPTBIT(IN6P_PKTINFO);
1945 break;
1946
1947 case IPV6_RECVHOPLIMIT:
1948 optval = OPTBIT(IN6P_HOPLIMIT);
1949 break;
1950
1951 case IPV6_RECVRTHDR:
1952 optval = OPTBIT(IN6P_RTHDR);
1953 break;
1954
1955 case IPV6_RECVPATHMTU:
1956 optval = OPTBIT(IN6P_MTU);
1957 break;
1958
1959 case IPV6_FAITH:
1960 optval = OPTBIT(IN6P_FAITH);
1961 break;
1962
1963 case IPV6_V6ONLY:
1964 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1965 break;
1966
1967 case IPV6_PORTRANGE:
1968 {
1969 int flags;
1970 flags = in6p->in6p_flags;
1971 if (flags & IN6P_HIGHPORT)
1972 optval = IPV6_PORTRANGE_HIGH;
1973 else if (flags & IN6P_LOWPORT)
1974 optval = IPV6_PORTRANGE_LOW;
1975 else
1976 optval = 0;
1977 break;
1978 }
1979 case IPV6_RECVTCLASS:
1980 optval = OPTBIT(IN6P_TCLASS);
1981 break;
1982
1983 }
1984 if (error)
1985 break;
1986 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1987 m->m_len = sizeof(int);
1988 *mtod(m, int *) = optval;
1989 break;
1990
1991 case IPV6_PATHMTU:
1992 {
1993 u_long pmtu = 0;
1994 struct ip6_mtuinfo mtuinfo;
1995 struct route *ro = &in6p->in6p_route;
1996
1997 if (!(so->so_state & SS_ISCONNECTED))
1998 return (ENOTCONN);
1999 /*
2000 * XXX: we dot not consider the case of source
2001 * routing, or optional information to specify
2002 * the outgoing interface.
2003 */
2004 error = ip6_getpmtu(ro, NULL, NULL,
2005 &in6p->in6p_faddr, &pmtu, NULL);
2006 if (error)
2007 break;
2008 if (pmtu > IPV6_MAXPACKET)
2009 pmtu = IPV6_MAXPACKET;
2010
2011 memset(&mtuinfo, 0, sizeof(mtuinfo));
2012 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2013 optdata = (void *)&mtuinfo;
2014 optdatalen = sizeof(mtuinfo);
2015 if (optdatalen > MCLBYTES)
2016 return (EMSGSIZE); /* XXX */
2017 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2018 if (optdatalen > MLEN)
2019 MCLGET(m, M_WAIT);
2020 m->m_len = optdatalen;
2021 memcpy(mtod(m, void *), optdata, optdatalen);
2022 break;
2023 }
2024
2025 #ifdef RFC2292
2026 case IPV6_2292PKTINFO:
2027 case IPV6_2292HOPLIMIT:
2028 case IPV6_2292HOPOPTS:
2029 case IPV6_2292RTHDR:
2030 case IPV6_2292DSTOPTS:
2031 switch (optname) {
2032 case IPV6_2292PKTINFO:
2033 optval = OPTBIT(IN6P_PKTINFO);
2034 break;
2035 case IPV6_2292HOPLIMIT:
2036 optval = OPTBIT(IN6P_HOPLIMIT);
2037 break;
2038 case IPV6_2292HOPOPTS:
2039 optval = OPTBIT(IN6P_HOPOPTS);
2040 break;
2041 case IPV6_2292RTHDR:
2042 optval = OPTBIT(IN6P_RTHDR);
2043 break;
2044 case IPV6_2292DSTOPTS:
2045 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2046 break;
2047 }
2048 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2049 m->m_len = sizeof(int);
2050 *mtod(m, int *) = optval;
2051 break;
2052 #endif
2053 case IPV6_PKTINFO:
2054 case IPV6_HOPOPTS:
2055 case IPV6_RTHDR:
2056 case IPV6_DSTOPTS:
2057 case IPV6_RTHDRDSTOPTS:
2058 case IPV6_NEXTHOP:
2059 case IPV6_OTCLASS:
2060 case IPV6_TCLASS:
2061 case IPV6_DONTFRAG:
2062 case IPV6_USE_MIN_MTU:
2063 error = ip6_getpcbopt(in6p->in6p_outputopts,
2064 optname, mp);
2065 break;
2066
2067 case IPV6_MULTICAST_IF:
2068 case IPV6_MULTICAST_HOPS:
2069 case IPV6_MULTICAST_LOOP:
2070 case IPV6_JOIN_GROUP:
2071 case IPV6_LEAVE_GROUP:
2072 error = ip6_getmoptions(optname,
2073 in6p->in6p_moptions, mp);
2074 break;
2075
2076 #if defined(IPSEC) || defined(FAST_IPSEC)
2077 case IPV6_IPSEC_POLICY:
2078 {
2079 void *req = NULL;
2080 size_t len = 0;
2081 if (m) {
2082 req = mtod(m, void *);
2083 len = m->m_len;
2084 }
2085 error = ipsec6_get_policy(in6p, req, len, mp);
2086 break;
2087 }
2088 #endif /* IPSEC */
2089
2090
2091
2092
2093 default:
2094 error = ENOPROTOOPT;
2095 break;
2096 }
2097 break;
2098 }
2099 } else {
2100 error = EINVAL;
2101 if (op == PRCO_SETOPT && *mp)
2102 (void)m_free(*mp);
2103 }
2104 return (error);
2105 }
2106
2107 int
2108 ip6_raw_ctloutput(int op, struct socket *so, int level, int optname,
2109 struct mbuf **mp)
2110 {
2111 int error = 0, optval, optlen;
2112 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2113 struct in6pcb *in6p = sotoin6pcb(so);
2114 struct mbuf *m = *mp;
2115
2116 optlen = m ? m->m_len : 0;
2117
2118 if (level != IPPROTO_IPV6) {
2119 if (op == PRCO_SETOPT && *mp)
2120 (void)m_free(*mp);
2121 return (EINVAL);
2122 }
2123
2124 switch (optname) {
2125 case IPV6_CHECKSUM:
2126 /*
2127 * For ICMPv6 sockets, no modification allowed for checksum
2128 * offset, permit "no change" values to help existing apps.
2129 *
2130 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
2131 * for an ICMPv6 socket will fail." The current
2132 * behavior does not meet RFC3542.
2133 */
2134 switch (op) {
2135 case PRCO_SETOPT:
2136 if (optlen != sizeof(int)) {
2137 error = EINVAL;
2138 break;
2139 }
2140 optval = *mtod(m, int *);
2141 if ((optval % 2) != 0) {
2142 /* the API assumes even offset values */
2143 error = EINVAL;
2144 } else if (so->so_proto->pr_protocol ==
2145 IPPROTO_ICMPV6) {
2146 if (optval != icmp6off)
2147 error = EINVAL;
2148 } else
2149 in6p->in6p_cksum = optval;
2150 break;
2151
2152 case PRCO_GETOPT:
2153 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2154 optval = icmp6off;
2155 else
2156 optval = in6p->in6p_cksum;
2157
2158 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2159 m->m_len = sizeof(int);
2160 *mtod(m, int *) = optval;
2161 break;
2162
2163 default:
2164 error = EINVAL;
2165 break;
2166 }
2167 break;
2168
2169 default:
2170 error = ENOPROTOOPT;
2171 break;
2172 }
2173
2174 if (op == PRCO_SETOPT && m)
2175 (void)m_free(m);
2176
2177 return (error);
2178 }
2179
2180 #ifdef RFC2292
2181 /*
2182 * Set up IP6 options in pcb for insertion in output packets or
2183 * specifying behavior of outgoing packets.
2184 */
2185 static int
2186 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so)
2187 {
2188 struct ip6_pktopts *opt = *pktopt;
2189 int error = 0;
2190 struct lwp *l = curlwp; /* XXX */
2191 int priv = 0;
2192
2193 /* turn off any old options. */
2194 if (opt) {
2195 #ifdef DIAGNOSTIC
2196 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2197 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2198 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2199 printf("ip6_pcbopts: all specified options are cleared.\n");
2200 #endif
2201 ip6_clearpktopts(opt, -1);
2202 } else
2203 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2204 *pktopt = NULL;
2205
2206 if (!m || m->m_len == 0) {
2207 /*
2208 * Only turning off any previous options, regardless of
2209 * whether the opt is just created or given.
2210 */
2211 free(opt, M_IP6OPT);
2212 return (0);
2213 }
2214
2215 /* set options specified by user. */
2216 if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2217 NULL))
2218 priv = 1;
2219 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2220 so->so_proto->pr_protocol)) != 0) {
2221 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2222 free(opt, M_IP6OPT);
2223 return (error);
2224 }
2225 *pktopt = opt;
2226 return (0);
2227 }
2228 #endif
2229
2230 /*
2231 * initialize ip6_pktopts. beware that there are non-zero default values in
2232 * the struct.
2233 */
2234 void
2235 ip6_initpktopts(struct ip6_pktopts *opt)
2236 {
2237
2238 memset(opt, 0, sizeof(*opt));
2239 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2240 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2241 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2242 }
2243
2244 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2245 static int
2246 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2247 int priv, int uproto)
2248 {
2249 struct ip6_pktopts *opt;
2250
2251 if (*pktopt == NULL) {
2252 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2253 M_WAITOK);
2254 ip6_initpktopts(*pktopt);
2255 }
2256 opt = *pktopt;
2257
2258 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2259 }
2260
2261 static int
2262 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
2263 {
2264 void *optdata = NULL;
2265 int optdatalen = 0;
2266 struct ip6_ext *ip6e;
2267 int error = 0;
2268 struct in6_pktinfo null_pktinfo;
2269 int deftclass = 0, on;
2270 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2271 struct mbuf *m;
2272
2273 switch (optname) {
2274 case IPV6_PKTINFO:
2275 if (pktopt && pktopt->ip6po_pktinfo)
2276 optdata = (void *)pktopt->ip6po_pktinfo;
2277 else {
2278 /* XXX: we don't have to do this every time... */
2279 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2280 optdata = (void *)&null_pktinfo;
2281 }
2282 optdatalen = sizeof(struct in6_pktinfo);
2283 break;
2284 case IPV6_OTCLASS:
2285 /* XXX */
2286 return (EINVAL);
2287 case IPV6_TCLASS:
2288 if (pktopt && pktopt->ip6po_tclass >= 0)
2289 optdata = (void *)&pktopt->ip6po_tclass;
2290 else
2291 optdata = (void *)&deftclass;
2292 optdatalen = sizeof(int);
2293 break;
2294 case IPV6_HOPOPTS:
2295 if (pktopt && pktopt->ip6po_hbh) {
2296 optdata = (void *)pktopt->ip6po_hbh;
2297 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2298 optdatalen = (ip6e->ip6e_len + 1) << 3;
2299 }
2300 break;
2301 case IPV6_RTHDR:
2302 if (pktopt && pktopt->ip6po_rthdr) {
2303 optdata = (void *)pktopt->ip6po_rthdr;
2304 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2305 optdatalen = (ip6e->ip6e_len + 1) << 3;
2306 }
2307 break;
2308 case IPV6_RTHDRDSTOPTS:
2309 if (pktopt && pktopt->ip6po_dest1) {
2310 optdata = (void *)pktopt->ip6po_dest1;
2311 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2312 optdatalen = (ip6e->ip6e_len + 1) << 3;
2313 }
2314 break;
2315 case IPV6_DSTOPTS:
2316 if (pktopt && pktopt->ip6po_dest2) {
2317 optdata = (void *)pktopt->ip6po_dest2;
2318 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2319 optdatalen = (ip6e->ip6e_len + 1) << 3;
2320 }
2321 break;
2322 case IPV6_NEXTHOP:
2323 if (pktopt && pktopt->ip6po_nexthop) {
2324 optdata = (void *)pktopt->ip6po_nexthop;
2325 optdatalen = pktopt->ip6po_nexthop->sa_len;
2326 }
2327 break;
2328 case IPV6_USE_MIN_MTU:
2329 if (pktopt)
2330 optdata = (void *)&pktopt->ip6po_minmtu;
2331 else
2332 optdata = (void *)&defminmtu;
2333 optdatalen = sizeof(int);
2334 break;
2335 case IPV6_DONTFRAG:
2336 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2337 on = 1;
2338 else
2339 on = 0;
2340 optdata = (void *)&on;
2341 optdatalen = sizeof(on);
2342 break;
2343 default: /* should not happen */
2344 #ifdef DIAGNOSTIC
2345 panic("ip6_getpcbopt: unexpected option\n");
2346 #endif
2347 return (ENOPROTOOPT);
2348 }
2349
2350 if (optdatalen > MCLBYTES)
2351 return (EMSGSIZE); /* XXX */
2352 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2353 if (optdatalen > MLEN)
2354 MCLGET(m, M_WAIT);
2355 m->m_len = optdatalen;
2356 if (optdatalen)
2357 memcpy(mtod(m, void *), optdata, optdatalen);
2358
2359 return (error);
2360 }
2361
2362 void
2363 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2364 {
2365 if (optname == -1 || optname == IPV6_PKTINFO) {
2366 if (pktopt->ip6po_pktinfo)
2367 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2368 pktopt->ip6po_pktinfo = NULL;
2369 }
2370 if (optname == -1 || optname == IPV6_HOPLIMIT)
2371 pktopt->ip6po_hlim = -1;
2372 if (optname == -1 || optname == IPV6_TCLASS)
2373 pktopt->ip6po_tclass = -1;
2374 if (optname == -1 || optname == IPV6_NEXTHOP) {
2375 rtcache_free(&pktopt->ip6po_nextroute);
2376 if (pktopt->ip6po_nexthop)
2377 free(pktopt->ip6po_nexthop, M_IP6OPT);
2378 pktopt->ip6po_nexthop = NULL;
2379 }
2380 if (optname == -1 || optname == IPV6_HOPOPTS) {
2381 if (pktopt->ip6po_hbh)
2382 free(pktopt->ip6po_hbh, M_IP6OPT);
2383 pktopt->ip6po_hbh = NULL;
2384 }
2385 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2386 if (pktopt->ip6po_dest1)
2387 free(pktopt->ip6po_dest1, M_IP6OPT);
2388 pktopt->ip6po_dest1 = NULL;
2389 }
2390 if (optname == -1 || optname == IPV6_RTHDR) {
2391 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2392 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2393 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2394 rtcache_free(&pktopt->ip6po_route);
2395 }
2396 if (optname == -1 || optname == IPV6_DSTOPTS) {
2397 if (pktopt->ip6po_dest2)
2398 free(pktopt->ip6po_dest2, M_IP6OPT);
2399 pktopt->ip6po_dest2 = NULL;
2400 }
2401 }
2402
2403 #define PKTOPT_EXTHDRCPY(type) \
2404 do { \
2405 if (src->type) { \
2406 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2407 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2408 if (dst->type == NULL && canwait == M_NOWAIT) \
2409 goto bad; \
2410 memcpy(dst->type, src->type, hlen); \
2411 } \
2412 } while (/*CONSTCOND*/ 0)
2413
2414 static int
2415 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2416 {
2417 dst->ip6po_hlim = src->ip6po_hlim;
2418 dst->ip6po_tclass = src->ip6po_tclass;
2419 dst->ip6po_flags = src->ip6po_flags;
2420 if (src->ip6po_pktinfo) {
2421 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2422 M_IP6OPT, canwait);
2423 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2424 goto bad;
2425 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2426 }
2427 if (src->ip6po_nexthop) {
2428 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2429 M_IP6OPT, canwait);
2430 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2431 goto bad;
2432 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2433 src->ip6po_nexthop->sa_len);
2434 }
2435 PKTOPT_EXTHDRCPY(ip6po_hbh);
2436 PKTOPT_EXTHDRCPY(ip6po_dest1);
2437 PKTOPT_EXTHDRCPY(ip6po_dest2);
2438 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2439 return (0);
2440
2441 bad:
2442 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2443 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2444 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2445 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2446 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2447 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2448
2449 return (ENOBUFS);
2450 }
2451 #undef PKTOPT_EXTHDRCPY
2452
2453 struct ip6_pktopts *
2454 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2455 {
2456 int error;
2457 struct ip6_pktopts *dst;
2458
2459 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2460 if (dst == NULL && canwait == M_NOWAIT)
2461 return (NULL);
2462 ip6_initpktopts(dst);
2463
2464 if ((error = copypktopts(dst, src, canwait)) != 0) {
2465 free(dst, M_IP6OPT);
2466 return (NULL);
2467 }
2468
2469 return (dst);
2470 }
2471
2472 void
2473 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2474 {
2475 if (pktopt == NULL)
2476 return;
2477
2478 ip6_clearpktopts(pktopt, -1);
2479
2480 free(pktopt, M_IP6OPT);
2481 }
2482
2483 /*
2484 * Set the IP6 multicast options in response to user setsockopt().
2485 */
2486 static int
2487 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2488 {
2489 int error = 0;
2490 u_int loop, ifindex;
2491 struct ipv6_mreq *mreq;
2492 struct ifnet *ifp;
2493 struct ip6_moptions *im6o = *im6op;
2494 struct route ro;
2495 struct in6_multi_mship *imm;
2496 struct lwp *l = curlwp; /* XXX */
2497
2498 if (im6o == NULL) {
2499 /*
2500 * No multicast option buffer attached to the pcb;
2501 * allocate one and initialize to default values.
2502 */
2503 im6o = (struct ip6_moptions *)
2504 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2505
2506 if (im6o == NULL)
2507 return (ENOBUFS);
2508 *im6op = im6o;
2509 im6o->im6o_multicast_ifp = NULL;
2510 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2511 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2512 LIST_INIT(&im6o->im6o_memberships);
2513 }
2514
2515 switch (optname) {
2516
2517 case IPV6_MULTICAST_IF:
2518 /*
2519 * Select the interface for outgoing multicast packets.
2520 */
2521 if (m == NULL || m->m_len != sizeof(u_int)) {
2522 error = EINVAL;
2523 break;
2524 }
2525 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2526 if (ifindex != 0) {
2527 if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
2528 error = ENXIO; /* XXX EINVAL? */
2529 break;
2530 }
2531 ifp = ifindex2ifnet[ifindex];
2532 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2533 error = EADDRNOTAVAIL;
2534 break;
2535 }
2536 } else
2537 ifp = NULL;
2538 im6o->im6o_multicast_ifp = ifp;
2539 break;
2540
2541 case IPV6_MULTICAST_HOPS:
2542 {
2543 /*
2544 * Set the IP6 hoplimit for outgoing multicast packets.
2545 */
2546 int optval;
2547 if (m == NULL || m->m_len != sizeof(int)) {
2548 error = EINVAL;
2549 break;
2550 }
2551 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2552 if (optval < -1 || optval >= 256)
2553 error = EINVAL;
2554 else if (optval == -1)
2555 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2556 else
2557 im6o->im6o_multicast_hlim = optval;
2558 break;
2559 }
2560
2561 case IPV6_MULTICAST_LOOP:
2562 /*
2563 * Set the loopback flag for outgoing multicast packets.
2564 * Must be zero or one.
2565 */
2566 if (m == NULL || m->m_len != sizeof(u_int)) {
2567 error = EINVAL;
2568 break;
2569 }
2570 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2571 if (loop > 1) {
2572 error = EINVAL;
2573 break;
2574 }
2575 im6o->im6o_multicast_loop = loop;
2576 break;
2577
2578 case IPV6_JOIN_GROUP:
2579 /*
2580 * Add a multicast group membership.
2581 * Group must be a valid IP6 multicast address.
2582 */
2583 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2584 error = EINVAL;
2585 break;
2586 }
2587 mreq = mtod(m, struct ipv6_mreq *);
2588 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2589 /*
2590 * We use the unspecified address to specify to accept
2591 * all multicast addresses. Only super user is allowed
2592 * to do this.
2593 */
2594 if (kauth_authorize_generic(l->l_cred,
2595 KAUTH_GENERIC_ISSUSER, NULL))
2596 {
2597 error = EACCES;
2598 break;
2599 }
2600 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2601 error = EINVAL;
2602 break;
2603 }
2604
2605 /*
2606 * If no interface was explicitly specified, choose an
2607 * appropriate one according to the given multicast address.
2608 */
2609 if (mreq->ipv6mr_interface == 0) {
2610 union {
2611 struct sockaddr dst;
2612 struct sockaddr_in6 dst6;
2613 } u;
2614
2615 /*
2616 * Look up the routing table for the
2617 * address, and choose the outgoing interface.
2618 * XXX: is it a good approach?
2619 */
2620 memset(&ro, 0, sizeof(ro));
2621 sockaddr_in6_init(&u.dst6, &mreq->ipv6mr_multiaddr, 0,
2622 0, 0);
2623 rtcache_setdst(&ro, &u.dst);
2624 rtcache_init(&ro);
2625 ifp = (ro.ro_rt != NULL) ? ro.ro_rt->rt_ifp : NULL;
2626 rtcache_free(&ro);
2627 } else {
2628 /*
2629 * If the interface is specified, validate it.
2630 */
2631 if (if_indexlim <= mreq->ipv6mr_interface ||
2632 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2633 error = ENXIO; /* XXX EINVAL? */
2634 break;
2635 }
2636 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2637 }
2638
2639 /*
2640 * See if we found an interface, and confirm that it
2641 * supports multicast
2642 */
2643 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2644 error = EADDRNOTAVAIL;
2645 break;
2646 }
2647
2648 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2649 error = EADDRNOTAVAIL; /* XXX: should not happen */
2650 break;
2651 }
2652
2653 /*
2654 * See if the membership already exists.
2655 */
2656 for (imm = im6o->im6o_memberships.lh_first;
2657 imm != NULL; imm = imm->i6mm_chain.le_next)
2658 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2659 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2660 &mreq->ipv6mr_multiaddr))
2661 break;
2662 if (imm != NULL) {
2663 error = EADDRINUSE;
2664 break;
2665 }
2666 /*
2667 * Everything looks good; add a new record to the multicast
2668 * address list for the given interface.
2669 */
2670 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2671 if (imm == NULL)
2672 break;
2673 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2674 break;
2675
2676 case IPV6_LEAVE_GROUP:
2677 /*
2678 * Drop a multicast group membership.
2679 * Group must be a valid IP6 multicast address.
2680 */
2681 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2682 error = EINVAL;
2683 break;
2684 }
2685 mreq = mtod(m, struct ipv6_mreq *);
2686
2687 /*
2688 * If an interface address was specified, get a pointer
2689 * to its ifnet structure.
2690 */
2691 if (mreq->ipv6mr_interface != 0) {
2692 if (if_indexlim <= mreq->ipv6mr_interface ||
2693 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2694 error = ENXIO; /* XXX EINVAL? */
2695 break;
2696 }
2697 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2698 } else
2699 ifp = NULL;
2700
2701 /* Fill in the scope zone ID */
2702 if (ifp) {
2703 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2704 /* XXX: should not happen */
2705 error = EADDRNOTAVAIL;
2706 break;
2707 }
2708 } else if (mreq->ipv6mr_interface != 0) {
2709 /*
2710 * XXX: This case would happens when the (positive)
2711 * index is in the valid range, but the corresponding
2712 * interface has been detached dynamically. The above
2713 * check probably avoids such case to happen here, but
2714 * we check it explicitly for safety.
2715 */
2716 error = EADDRNOTAVAIL;
2717 break;
2718 } else { /* ipv6mr_interface == 0 */
2719 struct sockaddr_in6 sa6_mc;
2720
2721 /*
2722 * The API spec says as follows:
2723 * If the interface index is specified as 0, the
2724 * system may choose a multicast group membership to
2725 * drop by matching the multicast address only.
2726 * On the other hand, we cannot disambiguate the scope
2727 * zone unless an interface is provided. Thus, we
2728 * check if there's ambiguity with the default scope
2729 * zone as the last resort.
2730 */
2731 bzero(&sa6_mc, sizeof(sa6_mc));
2732 sa6_mc.sin6_family = AF_INET6;
2733 sa6_mc.sin6_len = sizeof(sa6_mc);
2734 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2735 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2736 if (error != 0)
2737 break;
2738 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2739 }
2740
2741 /*
2742 * Find the membership in the membership list.
2743 */
2744 for (imm = im6o->im6o_memberships.lh_first;
2745 imm != NULL; imm = imm->i6mm_chain.le_next) {
2746 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2747 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2748 &mreq->ipv6mr_multiaddr))
2749 break;
2750 }
2751 if (imm == NULL) {
2752 /* Unable to resolve interface */
2753 error = EADDRNOTAVAIL;
2754 break;
2755 }
2756 /*
2757 * Give up the multicast address record to which the
2758 * membership points.
2759 */
2760 LIST_REMOVE(imm, i6mm_chain);
2761 in6_leavegroup(imm);
2762 break;
2763
2764 default:
2765 error = EOPNOTSUPP;
2766 break;
2767 }
2768
2769 /*
2770 * If all options have default values, no need to keep the mbuf.
2771 */
2772 if (im6o->im6o_multicast_ifp == NULL &&
2773 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2774 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2775 im6o->im6o_memberships.lh_first == NULL) {
2776 free(*im6op, M_IPMOPTS);
2777 *im6op = NULL;
2778 }
2779
2780 return (error);
2781 }
2782
2783 /*
2784 * Return the IP6 multicast options in response to user getsockopt().
2785 */
2786 static int
2787 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2788 {
2789 u_int *hlim, *loop, *ifindex;
2790
2791 *mp = m_get(M_WAIT, MT_SOOPTS);
2792
2793 switch (optname) {
2794
2795 case IPV6_MULTICAST_IF:
2796 ifindex = mtod(*mp, u_int *);
2797 (*mp)->m_len = sizeof(u_int);
2798 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2799 *ifindex = 0;
2800 else
2801 *ifindex = im6o->im6o_multicast_ifp->if_index;
2802 return (0);
2803
2804 case IPV6_MULTICAST_HOPS:
2805 hlim = mtod(*mp, u_int *);
2806 (*mp)->m_len = sizeof(u_int);
2807 if (im6o == NULL)
2808 *hlim = ip6_defmcasthlim;
2809 else
2810 *hlim = im6o->im6o_multicast_hlim;
2811 return (0);
2812
2813 case IPV6_MULTICAST_LOOP:
2814 loop = mtod(*mp, u_int *);
2815 (*mp)->m_len = sizeof(u_int);
2816 if (im6o == NULL)
2817 *loop = ip6_defmcasthlim;
2818 else
2819 *loop = im6o->im6o_multicast_loop;
2820 return (0);
2821
2822 default:
2823 return (EOPNOTSUPP);
2824 }
2825 }
2826
2827 /*
2828 * Discard the IP6 multicast options.
2829 */
2830 void
2831 ip6_freemoptions(struct ip6_moptions *im6o)
2832 {
2833 struct in6_multi_mship *imm;
2834
2835 if (im6o == NULL)
2836 return;
2837
2838 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2839 LIST_REMOVE(imm, i6mm_chain);
2840 in6_leavegroup(imm);
2841 }
2842 free(im6o, M_IPMOPTS);
2843 }
2844
2845 /*
2846 * Set IPv6 outgoing packet options based on advanced API.
2847 */
2848 int
2849 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2850 struct ip6_pktopts *stickyopt, int priv, int uproto)
2851 {
2852 struct cmsghdr *cm = 0;
2853
2854 if (control == NULL || opt == NULL)
2855 return (EINVAL);
2856
2857 ip6_initpktopts(opt);
2858 if (stickyopt) {
2859 int error;
2860
2861 /*
2862 * If stickyopt is provided, make a local copy of the options
2863 * for this particular packet, then override them by ancillary
2864 * objects.
2865 * XXX: copypktopts() does not copy the cached route to a next
2866 * hop (if any). This is not very good in terms of efficiency,
2867 * but we can allow this since this option should be rarely
2868 * used.
2869 */
2870 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2871 return (error);
2872 }
2873
2874 /*
2875 * XXX: Currently, we assume all the optional information is stored
2876 * in a single mbuf.
2877 */
2878 if (control->m_next)
2879 return (EINVAL);
2880
2881 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2882 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2883 int error;
2884
2885 if (control->m_len < CMSG_LEN(0))
2886 return (EINVAL);
2887
2888 cm = mtod(control, struct cmsghdr *);
2889 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2890 return (EINVAL);
2891 if (cm->cmsg_level != IPPROTO_IPV6)
2892 continue;
2893
2894 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2895 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2896 if (error)
2897 return (error);
2898 }
2899
2900 return (0);
2901 }
2902
2903 /*
2904 * Set a particular packet option, as a sticky option or an ancillary data
2905 * item. "len" can be 0 only when it's a sticky option.
2906 * We have 4 cases of combination of "sticky" and "cmsg":
2907 * "sticky=0, cmsg=0": impossible
2908 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2909 * "sticky=1, cmsg=0": RFC3542 socket option
2910 * "sticky=1, cmsg=1": RFC2292 socket option
2911 */
2912 static int
2913 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2914 int priv, int sticky, int cmsg, int uproto)
2915 {
2916 int minmtupolicy;
2917
2918 if (!sticky && !cmsg) {
2919 #ifdef DIAGNOSTIC
2920 printf("ip6_setpktopt: impossible case\n");
2921 #endif
2922 return (EINVAL);
2923 }
2924
2925 /*
2926 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2927 * not be specified in the context of RFC3542. Conversely,
2928 * RFC3542 types should not be specified in the context of RFC2292.
2929 */
2930 if (!cmsg) {
2931 switch (optname) {
2932 case IPV6_2292PKTINFO:
2933 case IPV6_2292HOPLIMIT:
2934 case IPV6_2292NEXTHOP:
2935 case IPV6_2292HOPOPTS:
2936 case IPV6_2292DSTOPTS:
2937 case IPV6_2292RTHDR:
2938 case IPV6_2292PKTOPTIONS:
2939 return (ENOPROTOOPT);
2940 }
2941 }
2942 if (sticky && cmsg) {
2943 switch (optname) {
2944 case IPV6_PKTINFO:
2945 case IPV6_HOPLIMIT:
2946 case IPV6_NEXTHOP:
2947 case IPV6_HOPOPTS:
2948 case IPV6_DSTOPTS:
2949 case IPV6_RTHDRDSTOPTS:
2950 case IPV6_RTHDR:
2951 case IPV6_USE_MIN_MTU:
2952 case IPV6_DONTFRAG:
2953 case IPV6_OTCLASS:
2954 case IPV6_TCLASS:
2955 return (ENOPROTOOPT);
2956 }
2957 }
2958
2959 switch (optname) {
2960 #ifdef RFC2292
2961 case IPV6_2292PKTINFO:
2962 #endif
2963 case IPV6_PKTINFO:
2964 {
2965 struct ifnet *ifp = NULL;
2966 struct in6_pktinfo *pktinfo;
2967
2968 if (len != sizeof(struct in6_pktinfo))
2969 return (EINVAL);
2970
2971 pktinfo = (struct in6_pktinfo *)buf;
2972
2973 /*
2974 * An application can clear any sticky IPV6_PKTINFO option by
2975 * doing a "regular" setsockopt with ipi6_addr being
2976 * in6addr_any and ipi6_ifindex being zero.
2977 * [RFC 3542, Section 6]
2978 */
2979 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2980 pktinfo->ipi6_ifindex == 0 &&
2981 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2982 ip6_clearpktopts(opt, optname);
2983 break;
2984 }
2985
2986 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2987 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2988 return (EINVAL);
2989 }
2990
2991 /* validate the interface index if specified. */
2992 if (pktinfo->ipi6_ifindex >= if_indexlim) {
2993 return (ENXIO);
2994 }
2995 if (pktinfo->ipi6_ifindex) {
2996 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
2997 if (ifp == NULL)
2998 return (ENXIO);
2999 }
3000
3001 /*
3002 * We store the address anyway, and let in6_selectsrc()
3003 * validate the specified address. This is because ipi6_addr
3004 * may not have enough information about its scope zone, and
3005 * we may need additional information (such as outgoing
3006 * interface or the scope zone of a destination address) to
3007 * disambiguate the scope.
3008 * XXX: the delay of the validation may confuse the
3009 * application when it is used as a sticky option.
3010 */
3011 if (opt->ip6po_pktinfo == NULL) {
3012 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3013 M_IP6OPT, M_NOWAIT);
3014 if (opt->ip6po_pktinfo == NULL)
3015 return (ENOBUFS);
3016 }
3017 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
3018 break;
3019 }
3020
3021 #ifdef RFC2292
3022 case IPV6_2292HOPLIMIT:
3023 #endif
3024 case IPV6_HOPLIMIT:
3025 {
3026 int *hlimp;
3027
3028 /*
3029 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3030 * to simplify the ordering among hoplimit options.
3031 */
3032 if (optname == IPV6_HOPLIMIT && sticky)
3033 return (ENOPROTOOPT);
3034
3035 if (len != sizeof(int))
3036 return (EINVAL);
3037 hlimp = (int *)buf;
3038 if (*hlimp < -1 || *hlimp > 255)
3039 return (EINVAL);
3040
3041 opt->ip6po_hlim = *hlimp;
3042 break;
3043 }
3044
3045 case IPV6_OTCLASS:
3046 if (len != sizeof(u_int8_t))
3047 return (EINVAL);
3048
3049 opt->ip6po_tclass = *(u_int8_t *)buf;
3050 break;
3051
3052 case IPV6_TCLASS:
3053 {
3054 int tclass;
3055
3056 if (len != sizeof(int))
3057 return (EINVAL);
3058 tclass = *(int *)buf;
3059 if (tclass < -1 || tclass > 255)
3060 return (EINVAL);
3061
3062 opt->ip6po_tclass = tclass;
3063 break;
3064 }
3065
3066 #ifdef RFC2292
3067 case IPV6_2292NEXTHOP:
3068 #endif
3069 case IPV6_NEXTHOP:
3070 if (!priv)
3071 return (EPERM);
3072
3073 if (len == 0) { /* just remove the option */
3074 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3075 break;
3076 }
3077
3078 /* check if cmsg_len is large enough for sa_len */
3079 if (len < sizeof(struct sockaddr) || len < *buf)
3080 return (EINVAL);
3081
3082 switch (((struct sockaddr *)buf)->sa_family) {
3083 case AF_INET6:
3084 {
3085 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3086 int error;
3087
3088 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3089 return (EINVAL);
3090
3091 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3092 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3093 return (EINVAL);
3094 }
3095 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3096 != 0) {
3097 return (error);
3098 }
3099 break;
3100 }
3101 case AF_LINK: /* eventually be supported? */
3102 default:
3103 return (EAFNOSUPPORT);
3104 }
3105
3106 /* turn off the previous option, then set the new option. */
3107 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3108 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3109 if (opt->ip6po_nexthop == NULL)
3110 return (ENOBUFS);
3111 memcpy(opt->ip6po_nexthop, buf, *buf);
3112 break;
3113
3114 #ifdef RFC2292
3115 case IPV6_2292HOPOPTS:
3116 #endif
3117 case IPV6_HOPOPTS:
3118 {
3119 struct ip6_hbh *hbh;
3120 int hbhlen;
3121
3122 /*
3123 * XXX: We don't allow a non-privileged user to set ANY HbH
3124 * options, since per-option restriction has too much
3125 * overhead.
3126 */
3127 if (!priv)
3128 return (EPERM);
3129
3130 if (len == 0) {
3131 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3132 break; /* just remove the option */
3133 }
3134
3135 /* message length validation */
3136 if (len < sizeof(struct ip6_hbh))
3137 return (EINVAL);
3138 hbh = (struct ip6_hbh *)buf;
3139 hbhlen = (hbh->ip6h_len + 1) << 3;
3140 if (len != hbhlen)
3141 return (EINVAL);
3142
3143 /* turn off the previous option, then set the new option. */
3144 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3145 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3146 if (opt->ip6po_hbh == NULL)
3147 return (ENOBUFS);
3148 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3149
3150 break;
3151 }
3152
3153 #ifdef RFC2292
3154 case IPV6_2292DSTOPTS:
3155 #endif
3156 case IPV6_DSTOPTS:
3157 case IPV6_RTHDRDSTOPTS:
3158 {
3159 struct ip6_dest *dest, **newdest = NULL;
3160 int destlen;
3161
3162 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3163 return (EPERM);
3164
3165 if (len == 0) {
3166 ip6_clearpktopts(opt, optname);
3167 break; /* just remove the option */
3168 }
3169
3170 /* message length validation */
3171 if (len < sizeof(struct ip6_dest))
3172 return (EINVAL);
3173 dest = (struct ip6_dest *)buf;
3174 destlen = (dest->ip6d_len + 1) << 3;
3175 if (len != destlen)
3176 return (EINVAL);
3177 /*
3178 * Determine the position that the destination options header
3179 * should be inserted; before or after the routing header.
3180 */
3181 switch (optname) {
3182 case IPV6_2292DSTOPTS:
3183 /*
3184 * The old advanced API is ambiguous on this point.
3185 * Our approach is to determine the position based
3186 * according to the existence of a routing header.
3187 * Note, however, that this depends on the order of the
3188 * extension headers in the ancillary data; the 1st
3189 * part of the destination options header must appear
3190 * before the routing header in the ancillary data,
3191 * too.
3192 * RFC3542 solved the ambiguity by introducing
3193 * separate ancillary data or option types.
3194 */
3195 if (opt->ip6po_rthdr == NULL)
3196 newdest = &opt->ip6po_dest1;
3197 else
3198 newdest = &opt->ip6po_dest2;
3199 break;
3200 case IPV6_RTHDRDSTOPTS:
3201 newdest = &opt->ip6po_dest1;
3202 break;
3203 case IPV6_DSTOPTS:
3204 newdest = &opt->ip6po_dest2;
3205 break;
3206 }
3207
3208 /* turn off the previous option, then set the new option. */
3209 ip6_clearpktopts(opt, optname);
3210 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3211 if (*newdest == NULL)
3212 return (ENOBUFS);
3213 memcpy(*newdest, dest, destlen);
3214
3215 break;
3216 }
3217
3218 #ifdef RFC2292
3219 case IPV6_2292RTHDR:
3220 #endif
3221 case IPV6_RTHDR:
3222 {
3223 struct ip6_rthdr *rth;
3224 int rthlen;
3225
3226 if (len == 0) {
3227 ip6_clearpktopts(opt, IPV6_RTHDR);
3228 break; /* just remove the option */
3229 }
3230
3231 /* message length validation */
3232 if (len < sizeof(struct ip6_rthdr))
3233 return (EINVAL);
3234 rth = (struct ip6_rthdr *)buf;
3235 rthlen = (rth->ip6r_len + 1) << 3;
3236 if (len != rthlen)
3237 return (EINVAL);
3238 switch (rth->ip6r_type) {
3239 case IPV6_RTHDR_TYPE_0:
3240 if (rth->ip6r_len == 0) /* must contain one addr */
3241 return (EINVAL);
3242 if (rth->ip6r_len % 2) /* length must be even */
3243 return (EINVAL);
3244 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3245 return (EINVAL);
3246 break;
3247 default:
3248 return (EINVAL); /* not supported */
3249 }
3250 /* turn off the previous option */
3251 ip6_clearpktopts(opt, IPV6_RTHDR);
3252 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3253 if (opt->ip6po_rthdr == NULL)
3254 return (ENOBUFS);
3255 memcpy(opt->ip6po_rthdr, rth, rthlen);
3256 break;
3257 }
3258
3259 case IPV6_USE_MIN_MTU:
3260 if (len != sizeof(int))
3261 return (EINVAL);
3262 minmtupolicy = *(int *)buf;
3263 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3264 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3265 minmtupolicy != IP6PO_MINMTU_ALL) {
3266 return (EINVAL);
3267 }
3268 opt->ip6po_minmtu = minmtupolicy;
3269 break;
3270
3271 case IPV6_DONTFRAG:
3272 if (len != sizeof(int))
3273 return (EINVAL);
3274
3275 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3276 /*
3277 * we ignore this option for TCP sockets.
3278 * (RFC3542 leaves this case unspecified.)
3279 */
3280 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3281 } else
3282 opt->ip6po_flags |= IP6PO_DONTFRAG;
3283 break;
3284
3285 default:
3286 return (ENOPROTOOPT);
3287 } /* end of switch */
3288
3289 return (0);
3290 }
3291
3292 /*
3293 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3294 * packet to the input queue of a specified interface. Note that this
3295 * calls the output routine of the loopback "driver", but with an interface
3296 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3297 */
3298 void
3299 ip6_mloopback(struct ifnet *ifp, struct mbuf *m,
3300 const struct sockaddr_in6 *dst)
3301 {
3302 struct mbuf *copym;
3303 struct ip6_hdr *ip6;
3304
3305 copym = m_copy(m, 0, M_COPYALL);
3306 if (copym == NULL)
3307 return;
3308
3309 /*
3310 * Make sure to deep-copy IPv6 header portion in case the data
3311 * is in an mbuf cluster, so that we can safely override the IPv6
3312 * header portion later.
3313 */
3314 if ((copym->m_flags & M_EXT) != 0 ||
3315 copym->m_len < sizeof(struct ip6_hdr)) {
3316 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3317 if (copym == NULL)
3318 return;
3319 }
3320
3321 #ifdef DIAGNOSTIC
3322 if (copym->m_len < sizeof(*ip6)) {
3323 m_freem(copym);
3324 return;
3325 }
3326 #endif
3327
3328 ip6 = mtod(copym, struct ip6_hdr *);
3329 /*
3330 * clear embedded scope identifiers if necessary.
3331 * in6_clearscope will touch the addresses only when necessary.
3332 */
3333 in6_clearscope(&ip6->ip6_src);
3334 in6_clearscope(&ip6->ip6_dst);
3335
3336 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
3337 }
3338
3339 /*
3340 * Chop IPv6 header off from the payload.
3341 */
3342 static int
3343 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3344 {
3345 struct mbuf *mh;
3346 struct ip6_hdr *ip6;
3347
3348 ip6 = mtod(m, struct ip6_hdr *);
3349 if (m->m_len > sizeof(*ip6)) {
3350 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3351 if (mh == 0) {
3352 m_freem(m);
3353 return ENOBUFS;
3354 }
3355 M_MOVE_PKTHDR(mh, m);
3356 MH_ALIGN(mh, sizeof(*ip6));
3357 m->m_len -= sizeof(*ip6);
3358 m->m_data += sizeof(*ip6);
3359 mh->m_next = m;
3360 m = mh;
3361 m->m_len = sizeof(*ip6);
3362 bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6));
3363 }
3364 exthdrs->ip6e_ip6 = m;
3365 return 0;
3366 }
3367
3368 /*
3369 * Compute IPv6 extension header length.
3370 */
3371 int
3372 ip6_optlen(struct in6pcb *in6p)
3373 {
3374 int len;
3375
3376 if (!in6p->in6p_outputopts)
3377 return 0;
3378
3379 len = 0;
3380 #define elen(x) \
3381 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3382
3383 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3384 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3385 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3386 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3387 return len;
3388 #undef elen
3389 }
3390