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