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