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