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