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