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