ip6_output.c revision 1.109 1 /* $NetBSD: ip6_output.c,v 1.109 2006/12/15 21:18:55 joerg 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.109 2006/12/15 21:18:55 joerg 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 /* XXX Second if is invariant? */
1141 if (ro == &ip6route)
1142 rtcache_free((struct route *)ro);
1143 else if (ro_pmtu == &ip6route)
1144 rtcache_free((struct route *)ro_pmtu);
1145
1146 #ifdef IPSEC
1147 if (sp != NULL)
1148 key_freesp(sp);
1149 #endif /* IPSEC */
1150
1151 return (error);
1152
1153 freehdrs:
1154 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1155 m_freem(exthdrs.ip6e_dest1);
1156 m_freem(exthdrs.ip6e_rthdr);
1157 m_freem(exthdrs.ip6e_dest2);
1158 /* FALLTHROUGH */
1159 bad:
1160 m_freem(m);
1161 goto done;
1162 }
1163
1164 static int
1165 ip6_copyexthdr(mp, hdr, hlen)
1166 struct mbuf **mp;
1167 caddr_t hdr;
1168 int hlen;
1169 {
1170 struct mbuf *m;
1171
1172 if (hlen > MCLBYTES)
1173 return (ENOBUFS); /* XXX */
1174
1175 MGET(m, M_DONTWAIT, MT_DATA);
1176 if (!m)
1177 return (ENOBUFS);
1178
1179 if (hlen > MLEN) {
1180 MCLGET(m, M_DONTWAIT);
1181 if ((m->m_flags & M_EXT) == 0) {
1182 m_free(m);
1183 return (ENOBUFS);
1184 }
1185 }
1186 m->m_len = hlen;
1187 if (hdr)
1188 bcopy(hdr, mtod(m, caddr_t), hlen);
1189
1190 *mp = m;
1191 return (0);
1192 }
1193
1194 /*
1195 * Process a delayed payload checksum calculation.
1196 */
1197 void
1198 in6_delayed_cksum(struct mbuf *m)
1199 {
1200 uint16_t csum, offset;
1201
1202 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1203 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1204 KASSERT((m->m_pkthdr.csum_flags
1205 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
1206
1207 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
1208 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1209 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
1210 csum = 0xffff;
1211 }
1212
1213 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
1214 if ((offset + sizeof(csum)) > m->m_len) {
1215 m_copyback(m, offset, sizeof(csum), &csum);
1216 } else {
1217 *(uint16_t *)(mtod(m, caddr_t) + offset) = csum;
1218 }
1219 }
1220
1221 /*
1222 * Insert jumbo payload option.
1223 */
1224 static int
1225 ip6_insert_jumboopt(exthdrs, plen)
1226 struct ip6_exthdrs *exthdrs;
1227 u_int32_t plen;
1228 {
1229 struct mbuf *mopt;
1230 u_int8_t *optbuf;
1231 u_int32_t v;
1232
1233 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1234
1235 /*
1236 * If there is no hop-by-hop options header, allocate new one.
1237 * If there is one but it doesn't have enough space to store the
1238 * jumbo payload option, allocate a cluster to store the whole options.
1239 * Otherwise, use it to store the options.
1240 */
1241 if (exthdrs->ip6e_hbh == 0) {
1242 MGET(mopt, M_DONTWAIT, MT_DATA);
1243 if (mopt == 0)
1244 return (ENOBUFS);
1245 mopt->m_len = JUMBOOPTLEN;
1246 optbuf = mtod(mopt, u_int8_t *);
1247 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1248 exthdrs->ip6e_hbh = mopt;
1249 } else {
1250 struct ip6_hbh *hbh;
1251
1252 mopt = exthdrs->ip6e_hbh;
1253 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1254 /*
1255 * XXX assumption:
1256 * - exthdrs->ip6e_hbh is not referenced from places
1257 * other than exthdrs.
1258 * - exthdrs->ip6e_hbh is not an mbuf chain.
1259 */
1260 int oldoptlen = mopt->m_len;
1261 struct mbuf *n;
1262
1263 /*
1264 * XXX: give up if the whole (new) hbh header does
1265 * not fit even in an mbuf cluster.
1266 */
1267 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1268 return (ENOBUFS);
1269
1270 /*
1271 * As a consequence, we must always prepare a cluster
1272 * at this point.
1273 */
1274 MGET(n, M_DONTWAIT, MT_DATA);
1275 if (n) {
1276 MCLGET(n, M_DONTWAIT);
1277 if ((n->m_flags & M_EXT) == 0) {
1278 m_freem(n);
1279 n = NULL;
1280 }
1281 }
1282 if (!n)
1283 return (ENOBUFS);
1284 n->m_len = oldoptlen + JUMBOOPTLEN;
1285 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1286 oldoptlen);
1287 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1288 m_freem(mopt);
1289 mopt = exthdrs->ip6e_hbh = n;
1290 } else {
1291 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1292 mopt->m_len += JUMBOOPTLEN;
1293 }
1294 optbuf[0] = IP6OPT_PADN;
1295 optbuf[1] = 0;
1296
1297 /*
1298 * Adjust the header length according to the pad and
1299 * the jumbo payload option.
1300 */
1301 hbh = mtod(mopt, struct ip6_hbh *);
1302 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1303 }
1304
1305 /* fill in the option. */
1306 optbuf[2] = IP6OPT_JUMBO;
1307 optbuf[3] = 4;
1308 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1309 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1310
1311 /* finally, adjust the packet header length */
1312 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1313
1314 return (0);
1315 #undef JUMBOOPTLEN
1316 }
1317
1318 /*
1319 * Insert fragment header and copy unfragmentable header portions.
1320 */
1321 static int
1322 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1323 struct mbuf *m0, *m;
1324 int hlen;
1325 struct ip6_frag **frghdrp;
1326 {
1327 struct mbuf *n, *mlast;
1328
1329 if (hlen > sizeof(struct ip6_hdr)) {
1330 n = m_copym(m0, sizeof(struct ip6_hdr),
1331 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1332 if (n == 0)
1333 return (ENOBUFS);
1334 m->m_next = n;
1335 } else
1336 n = m;
1337
1338 /* Search for the last mbuf of unfragmentable part. */
1339 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1340 ;
1341
1342 if ((mlast->m_flags & M_EXT) == 0 &&
1343 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1344 /* use the trailing space of the last mbuf for the fragment hdr */
1345 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1346 mlast->m_len);
1347 mlast->m_len += sizeof(struct ip6_frag);
1348 m->m_pkthdr.len += sizeof(struct ip6_frag);
1349 } else {
1350 /* allocate a new mbuf for the fragment header */
1351 struct mbuf *mfrg;
1352
1353 MGET(mfrg, M_DONTWAIT, MT_DATA);
1354 if (mfrg == 0)
1355 return (ENOBUFS);
1356 mfrg->m_len = sizeof(struct ip6_frag);
1357 *frghdrp = mtod(mfrg, struct ip6_frag *);
1358 mlast->m_next = mfrg;
1359 }
1360
1361 return (0);
1362 }
1363
1364 static int
1365 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1366 struct route_in6 *ro_pmtu, *ro;
1367 struct ifnet *ifp;
1368 struct in6_addr *dst;
1369 u_long *mtup;
1370 int *alwaysfragp;
1371 {
1372 u_int32_t mtu = 0;
1373 int alwaysfrag = 0;
1374 int error = 0;
1375
1376 if (ro_pmtu != ro) {
1377 /* The first hop and the final destination may differ. */
1378 struct sockaddr_in6 *sa6_dst =
1379 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1380 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1381 rtcache_free((struct route *)ro_pmtu);
1382 else
1383 rtcache_check((struct route *)ro_pmtu);
1384 if (ro_pmtu->ro_rt == NULL) {
1385 bzero(sa6_dst, sizeof(*sa6_dst)); /* for safety */
1386 sa6_dst->sin6_family = AF_INET6;
1387 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1388 sa6_dst->sin6_addr = *dst;
1389 rtcache_init((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 rtcache_free((struct route *)&pktopt->ip6po_nextroute);
2334 if (pktopt->ip6po_nexthop)
2335 free(pktopt->ip6po_nexthop, M_IP6OPT);
2336 pktopt->ip6po_nexthop = NULL;
2337 }
2338 if (optname == -1 || optname == IPV6_HOPOPTS) {
2339 if (pktopt->ip6po_hbh)
2340 free(pktopt->ip6po_hbh, M_IP6OPT);
2341 pktopt->ip6po_hbh = NULL;
2342 }
2343 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2344 if (pktopt->ip6po_dest1)
2345 free(pktopt->ip6po_dest1, M_IP6OPT);
2346 pktopt->ip6po_dest1 = NULL;
2347 }
2348 if (optname == -1 || optname == IPV6_RTHDR) {
2349 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2350 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2351 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2352 rtcache_free((struct route *)&pktopt->ip6po_route);
2353 }
2354 if (optname == -1 || optname == IPV6_DSTOPTS) {
2355 if (pktopt->ip6po_dest2)
2356 free(pktopt->ip6po_dest2, M_IP6OPT);
2357 pktopt->ip6po_dest2 = NULL;
2358 }
2359 }
2360
2361 #define PKTOPT_EXTHDRCPY(type) \
2362 do { \
2363 if (src->type) { \
2364 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2365 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2366 if (dst->type == NULL && canwait == M_NOWAIT) \
2367 goto bad; \
2368 memcpy(dst->type, src->type, hlen); \
2369 } \
2370 } while (/*CONSTCOND*/ 0)
2371
2372 static int
2373 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2374 {
2375 dst->ip6po_hlim = src->ip6po_hlim;
2376 dst->ip6po_tclass = src->ip6po_tclass;
2377 dst->ip6po_flags = src->ip6po_flags;
2378 if (src->ip6po_pktinfo) {
2379 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2380 M_IP6OPT, canwait);
2381 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2382 goto bad;
2383 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2384 }
2385 if (src->ip6po_nexthop) {
2386 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2387 M_IP6OPT, canwait);
2388 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2389 goto bad;
2390 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2391 src->ip6po_nexthop->sa_len);
2392 }
2393 PKTOPT_EXTHDRCPY(ip6po_hbh);
2394 PKTOPT_EXTHDRCPY(ip6po_dest1);
2395 PKTOPT_EXTHDRCPY(ip6po_dest2);
2396 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2397 return (0);
2398
2399 bad:
2400 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2401 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2402 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2403 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2404 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2405 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2406
2407 return (ENOBUFS);
2408 }
2409 #undef PKTOPT_EXTHDRCPY
2410
2411 struct ip6_pktopts *
2412 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2413 {
2414 int error;
2415 struct ip6_pktopts *dst;
2416
2417 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2418 if (dst == NULL && canwait == M_NOWAIT)
2419 return (NULL);
2420 ip6_initpktopts(dst);
2421
2422 if ((error = copypktopts(dst, src, canwait)) != 0) {
2423 free(dst, M_IP6OPT);
2424 return (NULL);
2425 }
2426
2427 return (dst);
2428 }
2429
2430 void
2431 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2432 {
2433 if (pktopt == NULL)
2434 return;
2435
2436 ip6_clearpktopts(pktopt, -1);
2437
2438 free(pktopt, M_IP6OPT);
2439 }
2440
2441 /*
2442 * Set the IP6 multicast options in response to user setsockopt().
2443 */
2444 static int
2445 ip6_setmoptions(optname, im6op, m)
2446 int optname;
2447 struct ip6_moptions **im6op;
2448 struct mbuf *m;
2449 {
2450 int error = 0;
2451 u_int loop, ifindex;
2452 struct ipv6_mreq *mreq;
2453 struct ifnet *ifp;
2454 struct ip6_moptions *im6o = *im6op;
2455 struct route_in6 ro;
2456 struct in6_multi_mship *imm;
2457 struct lwp *l = curlwp; /* XXX */
2458
2459 if (im6o == NULL) {
2460 /*
2461 * No multicast option buffer attached to the pcb;
2462 * allocate one and initialize to default values.
2463 */
2464 im6o = (struct ip6_moptions *)
2465 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2466
2467 if (im6o == NULL)
2468 return (ENOBUFS);
2469 *im6op = im6o;
2470 im6o->im6o_multicast_ifp = NULL;
2471 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2472 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2473 LIST_INIT(&im6o->im6o_memberships);
2474 }
2475
2476 switch (optname) {
2477
2478 case IPV6_MULTICAST_IF:
2479 /*
2480 * Select the interface for outgoing multicast packets.
2481 */
2482 if (m == NULL || m->m_len != sizeof(u_int)) {
2483 error = EINVAL;
2484 break;
2485 }
2486 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2487 if (ifindex != 0) {
2488 if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
2489 error = ENXIO; /* XXX EINVAL? */
2490 break;
2491 }
2492 ifp = ifindex2ifnet[ifindex];
2493 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2494 error = EADDRNOTAVAIL;
2495 break;
2496 }
2497 } else
2498 ifp = NULL;
2499 im6o->im6o_multicast_ifp = ifp;
2500 break;
2501
2502 case IPV6_MULTICAST_HOPS:
2503 {
2504 /*
2505 * Set the IP6 hoplimit for outgoing multicast packets.
2506 */
2507 int optval;
2508 if (m == NULL || m->m_len != sizeof(int)) {
2509 error = EINVAL;
2510 break;
2511 }
2512 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2513 if (optval < -1 || optval >= 256)
2514 error = EINVAL;
2515 else if (optval == -1)
2516 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2517 else
2518 im6o->im6o_multicast_hlim = optval;
2519 break;
2520 }
2521
2522 case IPV6_MULTICAST_LOOP:
2523 /*
2524 * Set the loopback flag for outgoing multicast packets.
2525 * Must be zero or one.
2526 */
2527 if (m == NULL || m->m_len != sizeof(u_int)) {
2528 error = EINVAL;
2529 break;
2530 }
2531 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2532 if (loop > 1) {
2533 error = EINVAL;
2534 break;
2535 }
2536 im6o->im6o_multicast_loop = loop;
2537 break;
2538
2539 case IPV6_JOIN_GROUP:
2540 /*
2541 * Add a multicast group membership.
2542 * Group must be a valid IP6 multicast address.
2543 */
2544 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2545 error = EINVAL;
2546 break;
2547 }
2548 mreq = mtod(m, struct ipv6_mreq *);
2549 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2550 /*
2551 * We use the unspecified address to specify to accept
2552 * all multicast addresses. Only super user is allowed
2553 * to do this.
2554 */
2555 if (kauth_authorize_generic(l->l_cred,
2556 KAUTH_GENERIC_ISSUSER, &l->l_acflag))
2557 {
2558 error = EACCES;
2559 break;
2560 }
2561 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2562 error = EINVAL;
2563 break;
2564 }
2565
2566 /*
2567 * If no interface was explicitly specified, choose an
2568 * appropriate one according to the given multicast address.
2569 */
2570 if (mreq->ipv6mr_interface == 0) {
2571 struct sockaddr_in6 *dst;
2572
2573 /*
2574 * Look up the routing table for the
2575 * address, and choose the outgoing interface.
2576 * XXX: is it a good approach?
2577 */
2578 bzero(&ro, sizeof(ro));
2579 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2580 dst->sin6_family = AF_INET6;
2581 dst->sin6_len = sizeof(*dst);
2582 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2583 rtcache_init((struct route *)&ro);
2584 if (ro.ro_rt == NULL) {
2585 error = EADDRNOTAVAIL;
2586 break;
2587 }
2588 ifp = ro.ro_rt->rt_ifp;
2589 rtcache_free((struct route *)&ro);
2590 } else {
2591 /*
2592 * If the interface is specified, validate it.
2593 */
2594 if (if_indexlim <= mreq->ipv6mr_interface ||
2595 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2596 error = ENXIO; /* XXX EINVAL? */
2597 break;
2598 }
2599 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2600 }
2601
2602 /*
2603 * See if we found an interface, and confirm that it
2604 * supports multicast
2605 */
2606 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2607 error = EADDRNOTAVAIL;
2608 break;
2609 }
2610
2611 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2612 error = EADDRNOTAVAIL; /* XXX: should not happen */
2613 break;
2614 }
2615
2616 /*
2617 * See if the membership already exists.
2618 */
2619 for (imm = im6o->im6o_memberships.lh_first;
2620 imm != NULL; imm = imm->i6mm_chain.le_next)
2621 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2622 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2623 &mreq->ipv6mr_multiaddr))
2624 break;
2625 if (imm != NULL) {
2626 error = EADDRINUSE;
2627 break;
2628 }
2629 /*
2630 * Everything looks good; add a new record to the multicast
2631 * address list for the given interface.
2632 */
2633 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2634 if (imm == NULL)
2635 break;
2636 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2637 break;
2638
2639 case IPV6_LEAVE_GROUP:
2640 /*
2641 * Drop a multicast group membership.
2642 * Group must be a valid IP6 multicast address.
2643 */
2644 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2645 error = EINVAL;
2646 break;
2647 }
2648 mreq = mtod(m, struct ipv6_mreq *);
2649
2650 /*
2651 * If an interface address was specified, get a pointer
2652 * to its ifnet structure.
2653 */
2654 if (mreq->ipv6mr_interface != 0) {
2655 if (if_indexlim <= mreq->ipv6mr_interface ||
2656 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2657 error = ENXIO; /* XXX EINVAL? */
2658 break;
2659 }
2660 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2661 } else
2662 ifp = NULL;
2663
2664 /* Fill in the scope zone ID */
2665 if (ifp) {
2666 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2667 /* XXX: should not happen */
2668 error = EADDRNOTAVAIL;
2669 break;
2670 }
2671 } else if (mreq->ipv6mr_interface != 0) {
2672 /*
2673 * XXX: This case would happens when the (positive)
2674 * index is in the valid range, but the corresponding
2675 * interface has been detached dynamically. The above
2676 * check probably avoids such case to happen here, but
2677 * we check it explicitly for safety.
2678 */
2679 error = EADDRNOTAVAIL;
2680 break;
2681 } else { /* ipv6mr_interface == 0 */
2682 struct sockaddr_in6 sa6_mc;
2683
2684 /*
2685 * The API spec says as follows:
2686 * If the interface index is specified as 0, the
2687 * system may choose a multicast group membership to
2688 * drop by matching the multicast address only.
2689 * On the other hand, we cannot disambiguate the scope
2690 * zone unless an interface is provided. Thus, we
2691 * check if there's ambiguity with the default scope
2692 * zone as the last resort.
2693 */
2694 bzero(&sa6_mc, sizeof(sa6_mc));
2695 sa6_mc.sin6_family = AF_INET6;
2696 sa6_mc.sin6_len = sizeof(sa6_mc);
2697 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2698 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2699 if (error != 0)
2700 break;
2701 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2702 }
2703
2704 /*
2705 * Find the membership in the membership list.
2706 */
2707 for (imm = im6o->im6o_memberships.lh_first;
2708 imm != NULL; imm = imm->i6mm_chain.le_next) {
2709 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2710 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2711 &mreq->ipv6mr_multiaddr))
2712 break;
2713 }
2714 if (imm == NULL) {
2715 /* Unable to resolve interface */
2716 error = EADDRNOTAVAIL;
2717 break;
2718 }
2719 /*
2720 * Give up the multicast address record to which the
2721 * membership points.
2722 */
2723 LIST_REMOVE(imm, i6mm_chain);
2724 in6_leavegroup(imm);
2725 break;
2726
2727 default:
2728 error = EOPNOTSUPP;
2729 break;
2730 }
2731
2732 /*
2733 * If all options have default values, no need to keep the mbuf.
2734 */
2735 if (im6o->im6o_multicast_ifp == NULL &&
2736 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2737 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2738 im6o->im6o_memberships.lh_first == NULL) {
2739 free(*im6op, M_IPMOPTS);
2740 *im6op = NULL;
2741 }
2742
2743 return (error);
2744 }
2745
2746 /*
2747 * Return the IP6 multicast options in response to user getsockopt().
2748 */
2749 static int
2750 ip6_getmoptions(optname, im6o, mp)
2751 int optname;
2752 struct ip6_moptions *im6o;
2753 struct mbuf **mp;
2754 {
2755 u_int *hlim, *loop, *ifindex;
2756
2757 *mp = m_get(M_WAIT, MT_SOOPTS);
2758
2759 switch (optname) {
2760
2761 case IPV6_MULTICAST_IF:
2762 ifindex = mtod(*mp, u_int *);
2763 (*mp)->m_len = sizeof(u_int);
2764 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2765 *ifindex = 0;
2766 else
2767 *ifindex = im6o->im6o_multicast_ifp->if_index;
2768 return (0);
2769
2770 case IPV6_MULTICAST_HOPS:
2771 hlim = mtod(*mp, u_int *);
2772 (*mp)->m_len = sizeof(u_int);
2773 if (im6o == NULL)
2774 *hlim = ip6_defmcasthlim;
2775 else
2776 *hlim = im6o->im6o_multicast_hlim;
2777 return (0);
2778
2779 case IPV6_MULTICAST_LOOP:
2780 loop = mtod(*mp, u_int *);
2781 (*mp)->m_len = sizeof(u_int);
2782 if (im6o == NULL)
2783 *loop = ip6_defmcasthlim;
2784 else
2785 *loop = im6o->im6o_multicast_loop;
2786 return (0);
2787
2788 default:
2789 return (EOPNOTSUPP);
2790 }
2791 }
2792
2793 /*
2794 * Discard the IP6 multicast options.
2795 */
2796 void
2797 ip6_freemoptions(im6o)
2798 struct ip6_moptions *im6o;
2799 {
2800 struct in6_multi_mship *imm;
2801
2802 if (im6o == NULL)
2803 return;
2804
2805 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2806 LIST_REMOVE(imm, i6mm_chain);
2807 in6_leavegroup(imm);
2808 }
2809 free(im6o, M_IPMOPTS);
2810 }
2811
2812 /*
2813 * Set IPv6 outgoing packet options based on advanced API.
2814 */
2815 int
2816 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2817 struct mbuf *control;
2818 struct ip6_pktopts *opt, *stickyopt;
2819 int priv, uproto;
2820 {
2821 struct cmsghdr *cm = 0;
2822
2823 if (control == NULL || opt == NULL)
2824 return (EINVAL);
2825
2826 ip6_initpktopts(opt);
2827 if (stickyopt) {
2828 int error;
2829
2830 /*
2831 * If stickyopt is provided, make a local copy of the options
2832 * for this particular packet, then override them by ancillary
2833 * objects.
2834 * XXX: copypktopts() does not copy the cached route to a next
2835 * hop (if any). This is not very good in terms of efficiency,
2836 * but we can allow this since this option should be rarely
2837 * used.
2838 */
2839 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2840 return (error);
2841 }
2842
2843 /*
2844 * XXX: Currently, we assume all the optional information is stored
2845 * in a single mbuf.
2846 */
2847 if (control->m_next)
2848 return (EINVAL);
2849
2850 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2851 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2852 int error;
2853
2854 if (control->m_len < CMSG_LEN(0))
2855 return (EINVAL);
2856
2857 cm = mtod(control, struct cmsghdr *);
2858 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2859 return (EINVAL);
2860 if (cm->cmsg_level != IPPROTO_IPV6)
2861 continue;
2862
2863 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2864 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2865 if (error)
2866 return (error);
2867 }
2868
2869 return (0);
2870 }
2871
2872 /*
2873 * Set a particular packet option, as a sticky option or an ancillary data
2874 * item. "len" can be 0 only when it's a sticky option.
2875 * We have 4 cases of combination of "sticky" and "cmsg":
2876 * "sticky=0, cmsg=0": impossible
2877 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2878 * "sticky=1, cmsg=0": RFC3542 socket option
2879 * "sticky=1, cmsg=1": RFC2292 socket option
2880 */
2881 static int
2882 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2883 int priv, int sticky, int cmsg, int uproto)
2884 {
2885 int minmtupolicy;
2886
2887 if (!sticky && !cmsg) {
2888 #ifdef DIAGNOSTIC
2889 printf("ip6_setpktopt: impossible case\n");
2890 #endif
2891 return (EINVAL);
2892 }
2893
2894 /*
2895 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2896 * not be specified in the context of RFC3542. Conversely,
2897 * RFC3542 types should not be specified in the context of RFC2292.
2898 */
2899 if (!cmsg) {
2900 switch (optname) {
2901 case IPV6_2292PKTINFO:
2902 case IPV6_2292HOPLIMIT:
2903 case IPV6_2292NEXTHOP:
2904 case IPV6_2292HOPOPTS:
2905 case IPV6_2292DSTOPTS:
2906 case IPV6_2292RTHDR:
2907 case IPV6_2292PKTOPTIONS:
2908 return (ENOPROTOOPT);
2909 }
2910 }
2911 if (sticky && cmsg) {
2912 switch (optname) {
2913 case IPV6_PKTINFO:
2914 case IPV6_HOPLIMIT:
2915 case IPV6_NEXTHOP:
2916 case IPV6_HOPOPTS:
2917 case IPV6_DSTOPTS:
2918 case IPV6_RTHDRDSTOPTS:
2919 case IPV6_RTHDR:
2920 case IPV6_USE_MIN_MTU:
2921 case IPV6_DONTFRAG:
2922 case IPV6_OTCLASS:
2923 case IPV6_TCLASS:
2924 return (ENOPROTOOPT);
2925 }
2926 }
2927
2928 switch (optname) {
2929 #ifdef RFC2292
2930 case IPV6_2292PKTINFO:
2931 #endif
2932 case IPV6_PKTINFO:
2933 {
2934 struct ifnet *ifp = NULL;
2935 struct in6_pktinfo *pktinfo;
2936
2937 if (len != sizeof(struct in6_pktinfo))
2938 return (EINVAL);
2939
2940 pktinfo = (struct in6_pktinfo *)buf;
2941
2942 /*
2943 * An application can clear any sticky IPV6_PKTINFO option by
2944 * doing a "regular" setsockopt with ipi6_addr being
2945 * in6addr_any and ipi6_ifindex being zero.
2946 * [RFC 3542, Section 6]
2947 */
2948 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2949 pktinfo->ipi6_ifindex == 0 &&
2950 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2951 ip6_clearpktopts(opt, optname);
2952 break;
2953 }
2954
2955 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2956 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2957 return (EINVAL);
2958 }
2959
2960 /* validate the interface index if specified. */
2961 if (pktinfo->ipi6_ifindex >= if_indexlim) {
2962 return (ENXIO);
2963 }
2964 if (pktinfo->ipi6_ifindex) {
2965 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
2966 if (ifp == NULL)
2967 return (ENXIO);
2968 }
2969
2970 /*
2971 * We store the address anyway, and let in6_selectsrc()
2972 * validate the specified address. This is because ipi6_addr
2973 * may not have enough information about its scope zone, and
2974 * we may need additional information (such as outgoing
2975 * interface or the scope zone of a destination address) to
2976 * disambiguate the scope.
2977 * XXX: the delay of the validation may confuse the
2978 * application when it is used as a sticky option.
2979 */
2980 if (opt->ip6po_pktinfo == NULL) {
2981 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2982 M_IP6OPT, M_NOWAIT);
2983 if (opt->ip6po_pktinfo == NULL)
2984 return (ENOBUFS);
2985 }
2986 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
2987 break;
2988 }
2989
2990 #ifdef RFC2292
2991 case IPV6_2292HOPLIMIT:
2992 #endif
2993 case IPV6_HOPLIMIT:
2994 {
2995 int *hlimp;
2996
2997 /*
2998 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2999 * to simplify the ordering among hoplimit options.
3000 */
3001 if (optname == IPV6_HOPLIMIT && sticky)
3002 return (ENOPROTOOPT);
3003
3004 if (len != sizeof(int))
3005 return (EINVAL);
3006 hlimp = (int *)buf;
3007 if (*hlimp < -1 || *hlimp > 255)
3008 return (EINVAL);
3009
3010 opt->ip6po_hlim = *hlimp;
3011 break;
3012 }
3013
3014 case IPV6_OTCLASS:
3015 if (len != sizeof(u_int8_t))
3016 return (EINVAL);
3017
3018 opt->ip6po_tclass = *(u_int8_t *)buf;
3019 break;
3020
3021 case IPV6_TCLASS:
3022 {
3023 int tclass;
3024
3025 if (len != sizeof(int))
3026 return (EINVAL);
3027 tclass = *(int *)buf;
3028 if (tclass < -1 || tclass > 255)
3029 return (EINVAL);
3030
3031 opt->ip6po_tclass = tclass;
3032 break;
3033 }
3034
3035 #ifdef RFC2292
3036 case IPV6_2292NEXTHOP:
3037 #endif
3038 case IPV6_NEXTHOP:
3039 if (!priv)
3040 return (EPERM);
3041
3042 if (len == 0) { /* just remove the option */
3043 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3044 break;
3045 }
3046
3047 /* check if cmsg_len is large enough for sa_len */
3048 if (len < sizeof(struct sockaddr) || len < *buf)
3049 return (EINVAL);
3050
3051 switch (((struct sockaddr *)buf)->sa_family) {
3052 case AF_INET6:
3053 {
3054 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3055 int error;
3056
3057 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3058 return (EINVAL);
3059
3060 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3061 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3062 return (EINVAL);
3063 }
3064 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3065 != 0) {
3066 return (error);
3067 }
3068 break;
3069 }
3070 case AF_LINK: /* eventually be supported? */
3071 default:
3072 return (EAFNOSUPPORT);
3073 }
3074
3075 /* turn off the previous option, then set the new option. */
3076 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3077 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3078 if (opt->ip6po_nexthop == NULL)
3079 return (ENOBUFS);
3080 memcpy(opt->ip6po_nexthop, buf, *buf);
3081 break;
3082
3083 #ifdef RFC2292
3084 case IPV6_2292HOPOPTS:
3085 #endif
3086 case IPV6_HOPOPTS:
3087 {
3088 struct ip6_hbh *hbh;
3089 int hbhlen;
3090
3091 /*
3092 * XXX: We don't allow a non-privileged user to set ANY HbH
3093 * options, since per-option restriction has too much
3094 * overhead.
3095 */
3096 if (!priv)
3097 return (EPERM);
3098
3099 if (len == 0) {
3100 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3101 break; /* just remove the option */
3102 }
3103
3104 /* message length validation */
3105 if (len < sizeof(struct ip6_hbh))
3106 return (EINVAL);
3107 hbh = (struct ip6_hbh *)buf;
3108 hbhlen = (hbh->ip6h_len + 1) << 3;
3109 if (len != hbhlen)
3110 return (EINVAL);
3111
3112 /* turn off the previous option, then set the new option. */
3113 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3114 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3115 if (opt->ip6po_hbh == NULL)
3116 return (ENOBUFS);
3117 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3118
3119 break;
3120 }
3121
3122 #ifdef RFC2292
3123 case IPV6_2292DSTOPTS:
3124 #endif
3125 case IPV6_DSTOPTS:
3126 case IPV6_RTHDRDSTOPTS:
3127 {
3128 struct ip6_dest *dest, **newdest = NULL;
3129 int destlen;
3130
3131 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3132 return (EPERM);
3133
3134 if (len == 0) {
3135 ip6_clearpktopts(opt, optname);
3136 break; /* just remove the option */
3137 }
3138
3139 /* message length validation */
3140 if (len < sizeof(struct ip6_dest))
3141 return (EINVAL);
3142 dest = (struct ip6_dest *)buf;
3143 destlen = (dest->ip6d_len + 1) << 3;
3144 if (len != destlen)
3145 return (EINVAL);
3146 /*
3147 * Determine the position that the destination options header
3148 * should be inserted; before or after the routing header.
3149 */
3150 switch (optname) {
3151 case IPV6_2292DSTOPTS:
3152 /*
3153 * The old advanced API is ambiguous on this point.
3154 * Our approach is to determine the position based
3155 * according to the existence of a routing header.
3156 * Note, however, that this depends on the order of the
3157 * extension headers in the ancillary data; the 1st
3158 * part of the destination options header must appear
3159 * before the routing header in the ancillary data,
3160 * too.
3161 * RFC3542 solved the ambiguity by introducing
3162 * separate ancillary data or option types.
3163 */
3164 if (opt->ip6po_rthdr == NULL)
3165 newdest = &opt->ip6po_dest1;
3166 else
3167 newdest = &opt->ip6po_dest2;
3168 break;
3169 case IPV6_RTHDRDSTOPTS:
3170 newdest = &opt->ip6po_dest1;
3171 break;
3172 case IPV6_DSTOPTS:
3173 newdest = &opt->ip6po_dest2;
3174 break;
3175 }
3176
3177 /* turn off the previous option, then set the new option. */
3178 ip6_clearpktopts(opt, optname);
3179 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3180 if (*newdest == NULL)
3181 return (ENOBUFS);
3182 memcpy(*newdest, dest, destlen);
3183
3184 break;
3185 }
3186
3187 #ifdef RFC2292
3188 case IPV6_2292RTHDR:
3189 #endif
3190 case IPV6_RTHDR:
3191 {
3192 struct ip6_rthdr *rth;
3193 int rthlen;
3194
3195 if (len == 0) {
3196 ip6_clearpktopts(opt, IPV6_RTHDR);
3197 break; /* just remove the option */
3198 }
3199
3200 /* message length validation */
3201 if (len < sizeof(struct ip6_rthdr))
3202 return (EINVAL);
3203 rth = (struct ip6_rthdr *)buf;
3204 rthlen = (rth->ip6r_len + 1) << 3;
3205 if (len != rthlen)
3206 return (EINVAL);
3207 switch (rth->ip6r_type) {
3208 case IPV6_RTHDR_TYPE_0:
3209 if (rth->ip6r_len == 0) /* must contain one addr */
3210 return (EINVAL);
3211 if (rth->ip6r_len % 2) /* length must be even */
3212 return (EINVAL);
3213 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3214 return (EINVAL);
3215 break;
3216 default:
3217 return (EINVAL); /* not supported */
3218 }
3219 /* turn off the previous option */
3220 ip6_clearpktopts(opt, IPV6_RTHDR);
3221 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3222 if (opt->ip6po_rthdr == NULL)
3223 return (ENOBUFS);
3224 memcpy(opt->ip6po_rthdr, rth, rthlen);
3225 break;
3226 }
3227
3228 case IPV6_USE_MIN_MTU:
3229 if (len != sizeof(int))
3230 return (EINVAL);
3231 minmtupolicy = *(int *)buf;
3232 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3233 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3234 minmtupolicy != IP6PO_MINMTU_ALL) {
3235 return (EINVAL);
3236 }
3237 opt->ip6po_minmtu = minmtupolicy;
3238 break;
3239
3240 case IPV6_DONTFRAG:
3241 if (len != sizeof(int))
3242 return (EINVAL);
3243
3244 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3245 /*
3246 * we ignore this option for TCP sockets.
3247 * (RFC3542 leaves this case unspecified.)
3248 */
3249 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3250 } else
3251 opt->ip6po_flags |= IP6PO_DONTFRAG;
3252 break;
3253
3254 default:
3255 return (ENOPROTOOPT);
3256 } /* end of switch */
3257
3258 return (0);
3259 }
3260
3261 /*
3262 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3263 * packet to the input queue of a specified interface. Note that this
3264 * calls the output routine of the loopback "driver", but with an interface
3265 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3266 */
3267 void
3268 ip6_mloopback(ifp, m, dst)
3269 struct ifnet *ifp;
3270 struct mbuf *m;
3271 struct sockaddr_in6 *dst;
3272 {
3273 struct mbuf *copym;
3274 struct ip6_hdr *ip6;
3275
3276 copym = m_copy(m, 0, M_COPYALL);
3277 if (copym == NULL)
3278 return;
3279
3280 /*
3281 * Make sure to deep-copy IPv6 header portion in case the data
3282 * is in an mbuf cluster, so that we can safely override the IPv6
3283 * header portion later.
3284 */
3285 if ((copym->m_flags & M_EXT) != 0 ||
3286 copym->m_len < sizeof(struct ip6_hdr)) {
3287 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3288 if (copym == NULL)
3289 return;
3290 }
3291
3292 #ifdef DIAGNOSTIC
3293 if (copym->m_len < sizeof(*ip6)) {
3294 m_freem(copym);
3295 return;
3296 }
3297 #endif
3298
3299 ip6 = mtod(copym, struct ip6_hdr *);
3300 /*
3301 * clear embedded scope identifiers if necessary.
3302 * in6_clearscope will touch the addresses only when necessary.
3303 */
3304 in6_clearscope(&ip6->ip6_src);
3305 in6_clearscope(&ip6->ip6_dst);
3306
3307 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
3308 }
3309
3310 /*
3311 * Chop IPv6 header off from the payload.
3312 */
3313 static int
3314 ip6_splithdr(m, exthdrs)
3315 struct mbuf *m;
3316 struct ip6_exthdrs *exthdrs;
3317 {
3318 struct mbuf *mh;
3319 struct ip6_hdr *ip6;
3320
3321 ip6 = mtod(m, struct ip6_hdr *);
3322 if (m->m_len > sizeof(*ip6)) {
3323 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3324 if (mh == 0) {
3325 m_freem(m);
3326 return ENOBUFS;
3327 }
3328 M_MOVE_PKTHDR(mh, m);
3329 MH_ALIGN(mh, sizeof(*ip6));
3330 m->m_len -= sizeof(*ip6);
3331 m->m_data += sizeof(*ip6);
3332 mh->m_next = m;
3333 m = mh;
3334 m->m_len = sizeof(*ip6);
3335 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3336 }
3337 exthdrs->ip6e_ip6 = m;
3338 return 0;
3339 }
3340
3341 /*
3342 * Compute IPv6 extension header length.
3343 */
3344 int
3345 ip6_optlen(in6p)
3346 struct in6pcb *in6p;
3347 {
3348 int len;
3349
3350 if (!in6p->in6p_outputopts)
3351 return 0;
3352
3353 len = 0;
3354 #define elen(x) \
3355 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3356
3357 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3358 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3359 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3360 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3361 return len;
3362 #undef elen
3363 }
3364