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