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