ip6_output.c revision 1.88.4.1 1 /* $NetBSD: ip6_output.c,v 1.88.4.1 2006/06/21 15:11:08 yamt 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.88.4.1 2006/06/21 15:11:08 yamt 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 || kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &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
1829 case IPV6_FAITH:
1830 case IPV6_V6ONLY:
1831 case IPV6_PORTRANGE:
1832 case IPV6_RECVTCLASS:
1833 switch (optname) {
1834
1835 case IPV6_RECVHOPOPTS:
1836 optval = OPTBIT(IN6P_HOPOPTS);
1837 break;
1838
1839 case IPV6_RECVDSTOPTS:
1840 optval = OPTBIT(IN6P_DSTOPTS);
1841 break;
1842
1843 case IPV6_RECVRTHDRDSTOPTS:
1844 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1845 break;
1846
1847 case IPV6_UNICAST_HOPS:
1848 optval = in6p->in6p_hops;
1849 break;
1850
1851 case IPV6_RECVPKTINFO:
1852 optval = OPTBIT(IN6P_PKTINFO);
1853 break;
1854
1855 case IPV6_RECVHOPLIMIT:
1856 optval = OPTBIT(IN6P_HOPLIMIT);
1857 break;
1858
1859 case IPV6_RECVRTHDR:
1860 optval = OPTBIT(IN6P_RTHDR);
1861 break;
1862
1863 case IPV6_RECVPATHMTU:
1864 optval = OPTBIT(IN6P_MTU);
1865 break;
1866
1867 case IPV6_FAITH:
1868 optval = OPTBIT(IN6P_FAITH);
1869 break;
1870
1871 case IPV6_V6ONLY:
1872 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1873 break;
1874
1875 case IPV6_PORTRANGE:
1876 {
1877 int flags;
1878 flags = in6p->in6p_flags;
1879 if (flags & IN6P_HIGHPORT)
1880 optval = IPV6_PORTRANGE_HIGH;
1881 else if (flags & IN6P_LOWPORT)
1882 optval = IPV6_PORTRANGE_LOW;
1883 else
1884 optval = 0;
1885 break;
1886 }
1887 case IPV6_RECVTCLASS:
1888 optval = OPTBIT(IN6P_TCLASS);
1889 break;
1890
1891 }
1892 if (error)
1893 break;
1894 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1895 m->m_len = sizeof(int);
1896 *mtod(m, int *) = optval;
1897 break;
1898
1899 case IPV6_PATHMTU:
1900 {
1901 u_long pmtu = 0;
1902 struct ip6_mtuinfo mtuinfo;
1903 struct route_in6 *ro = (struct route_in6 *)&in6p
1904 ->in6p_route;
1905
1906 if (!(so->so_state & SS_ISCONNECTED))
1907 return (ENOTCONN);
1908 /*
1909 * XXX: we dot not consider the case of source
1910 * routing, or optional information to specify
1911 * the outgoing interface.
1912 */
1913 error = ip6_getpmtu(ro, NULL, NULL,
1914 &in6p->in6p_faddr, &pmtu, NULL);
1915 if (error)
1916 break;
1917 if (pmtu > IPV6_MAXPACKET)
1918 pmtu = IPV6_MAXPACKET;
1919
1920 memset(&mtuinfo, 0, sizeof(mtuinfo));
1921 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1922 optdata = (void *)&mtuinfo;
1923 optdatalen = sizeof(mtuinfo);
1924 if (optdatalen > MCLBYTES)
1925 return (EMSGSIZE); /* XXX */
1926 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1927 if (optdatalen > MLEN)
1928 MCLGET(m, M_WAIT);
1929 m->m_len = optdatalen;
1930 memcpy(mtod(m, void *), optdata, optdatalen);
1931 break;
1932 }
1933
1934 #ifdef RFC2292
1935 case IPV6_2292PKTINFO:
1936 case IPV6_2292HOPLIMIT:
1937 case IPV6_2292HOPOPTS:
1938 case IPV6_2292RTHDR:
1939 case IPV6_2292DSTOPTS:
1940 switch (optname) {
1941 case IPV6_2292PKTINFO:
1942 optval = OPTBIT(IN6P_PKTINFO);
1943 break;
1944 case IPV6_2292HOPLIMIT:
1945 optval = OPTBIT(IN6P_HOPLIMIT);
1946 break;
1947 case IPV6_2292HOPOPTS:
1948 optval = OPTBIT(IN6P_HOPOPTS);
1949 break;
1950 case IPV6_2292RTHDR:
1951 optval = OPTBIT(IN6P_RTHDR);
1952 break;
1953 case IPV6_2292DSTOPTS:
1954 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1955 break;
1956 }
1957 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1958 m->m_len = sizeof(int);
1959 *mtod(m, int *) = optval;
1960 break;
1961 #endif
1962 case IPV6_PKTINFO:
1963 case IPV6_HOPOPTS:
1964 case IPV6_RTHDR:
1965 case IPV6_DSTOPTS:
1966 case IPV6_RTHDRDSTOPTS:
1967 case IPV6_NEXTHOP:
1968 case IPV6_OTCLASS:
1969 case IPV6_TCLASS:
1970 case IPV6_DONTFRAG:
1971 case IPV6_USE_MIN_MTU:
1972 error = ip6_getpcbopt(in6p->in6p_outputopts,
1973 optname, mp);
1974 break;
1975
1976 case IPV6_MULTICAST_IF:
1977 case IPV6_MULTICAST_HOPS:
1978 case IPV6_MULTICAST_LOOP:
1979 case IPV6_JOIN_GROUP:
1980 case IPV6_LEAVE_GROUP:
1981 error = ip6_getmoptions(optname,
1982 in6p->in6p_moptions, mp);
1983 break;
1984
1985 #ifdef IPSEC
1986 case IPV6_IPSEC_POLICY:
1987 {
1988 caddr_t req = NULL;
1989 size_t len = 0;
1990 if (m) {
1991 req = mtod(m, caddr_t);
1992 len = m->m_len;
1993 }
1994 error = ipsec6_get_policy(in6p, req, len, mp);
1995 break;
1996 }
1997 #endif /* IPSEC */
1998
1999
2000
2001
2002 default:
2003 error = ENOPROTOOPT;
2004 break;
2005 }
2006 break;
2007 }
2008 } else {
2009 error = EINVAL;
2010 if (op == PRCO_SETOPT && *mp)
2011 (void)m_free(*mp);
2012 }
2013 return (error);
2014 }
2015
2016 int
2017 ip6_raw_ctloutput(op, so, level, optname, mp)
2018 int op;
2019 struct socket *so;
2020 int level, optname;
2021 struct mbuf **mp;
2022 {
2023 int error = 0, optval, optlen;
2024 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2025 struct in6pcb *in6p = sotoin6pcb(so);
2026 struct mbuf *m = *mp;
2027
2028 optlen = m ? m->m_len : 0;
2029
2030 if (level != IPPROTO_IPV6) {
2031 if (op == PRCO_SETOPT && *mp)
2032 (void)m_free(*mp);
2033 return (EINVAL);
2034 }
2035
2036 switch (optname) {
2037 case IPV6_CHECKSUM:
2038 /*
2039 * For ICMPv6 sockets, no modification allowed for checksum
2040 * offset, permit "no change" values to help existing apps.
2041 *
2042 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
2043 * for an ICMPv6 socket will fail." The current
2044 * behavior does not meet RFC3542.
2045 */
2046 switch (op) {
2047 case PRCO_SETOPT:
2048 if (optlen != sizeof(int)) {
2049 error = EINVAL;
2050 break;
2051 }
2052 optval = *mtod(m, int *);
2053 if ((optval % 2) != 0) {
2054 /* the API assumes even offset values */
2055 error = EINVAL;
2056 } else if (so->so_proto->pr_protocol ==
2057 IPPROTO_ICMPV6) {
2058 if (optval != icmp6off)
2059 error = EINVAL;
2060 } else
2061 in6p->in6p_cksum = optval;
2062 break;
2063
2064 case PRCO_GETOPT:
2065 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2066 optval = icmp6off;
2067 else
2068 optval = in6p->in6p_cksum;
2069
2070 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2071 m->m_len = sizeof(int);
2072 *mtod(m, int *) = optval;
2073 break;
2074
2075 default:
2076 error = EINVAL;
2077 break;
2078 }
2079 break;
2080
2081 default:
2082 error = ENOPROTOOPT;
2083 break;
2084 }
2085
2086 if (op == PRCO_SETOPT && m)
2087 (void)m_free(m);
2088
2089 return (error);
2090 }
2091
2092 #ifdef RFC2292
2093 /*
2094 * Set up IP6 options in pcb for insertion in output packets or
2095 * specifying behavior of outgoing packets.
2096 */
2097 static int
2098 ip6_pcbopts(pktopt, m, so)
2099 struct ip6_pktopts **pktopt;
2100 struct mbuf *m;
2101 struct socket *so;
2102 {
2103 struct ip6_pktopts *opt = *pktopt;
2104 int error = 0;
2105 struct proc *p = curproc; /* XXX */
2106 int priv = 0;
2107
2108 /* turn off any old options. */
2109 if (opt) {
2110 #ifdef DIAGNOSTIC
2111 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2112 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2113 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2114 printf("ip6_pcbopts: all specified options are cleared.\n");
2115 #endif
2116 ip6_clearpktopts(opt, -1);
2117 } else
2118 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2119 *pktopt = NULL;
2120
2121 if (!m || m->m_len == 0) {
2122 /*
2123 * Only turning off any previous options, regardless of
2124 * whether the opt is just created or given.
2125 */
2126 free(opt, M_IP6OPT);
2127 return (0);
2128 }
2129
2130 /* set options specified by user. */
2131 if (p && !kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag))
2132 priv = 1;
2133 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2134 so->so_proto->pr_protocol)) != 0) {
2135 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2136 free(opt, M_IP6OPT);
2137 return (error);
2138 }
2139 *pktopt = opt;
2140 return (0);
2141 }
2142 #endif
2143
2144 /*
2145 * initialize ip6_pktopts. beware that there are non-zero default values in
2146 * the struct.
2147 */
2148 void
2149 ip6_initpktopts(struct ip6_pktopts *opt)
2150 {
2151
2152 memset(opt, 0, sizeof(*opt));
2153 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2154 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2155 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2156 }
2157
2158 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2159 static int
2160 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2161 int priv, int uproto)
2162 {
2163 struct ip6_pktopts *opt;
2164
2165 if (*pktopt == NULL) {
2166 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2167 M_WAITOK);
2168 ip6_initpktopts(*pktopt);
2169 }
2170 opt = *pktopt;
2171
2172 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2173 }
2174
2175 static int
2176 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf **mp)
2177 {
2178 void *optdata = NULL;
2179 int optdatalen = 0;
2180 struct ip6_ext *ip6e;
2181 int error = 0;
2182 struct in6_pktinfo null_pktinfo;
2183 int deftclass = 0, on;
2184 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2185 struct mbuf *m;
2186
2187 switch (optname) {
2188 case IPV6_PKTINFO:
2189 if (pktopt && pktopt->ip6po_pktinfo)
2190 optdata = (void *)pktopt->ip6po_pktinfo;
2191 else {
2192 /* XXX: we don't have to do this every time... */
2193 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2194 optdata = (void *)&null_pktinfo;
2195 }
2196 optdatalen = sizeof(struct in6_pktinfo);
2197 break;
2198 case IPV6_OTCLASS:
2199 /* XXX */
2200 return (EINVAL);
2201 case IPV6_TCLASS:
2202 if (pktopt && pktopt->ip6po_tclass >= 0)
2203 optdata = (void *)&pktopt->ip6po_tclass;
2204 else
2205 optdata = (void *)&deftclass;
2206 optdatalen = sizeof(int);
2207 break;
2208 case IPV6_HOPOPTS:
2209 if (pktopt && pktopt->ip6po_hbh) {
2210 optdata = (void *)pktopt->ip6po_hbh;
2211 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2212 optdatalen = (ip6e->ip6e_len + 1) << 3;
2213 }
2214 break;
2215 case IPV6_RTHDR:
2216 if (pktopt && pktopt->ip6po_rthdr) {
2217 optdata = (void *)pktopt->ip6po_rthdr;
2218 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2219 optdatalen = (ip6e->ip6e_len + 1) << 3;
2220 }
2221 break;
2222 case IPV6_RTHDRDSTOPTS:
2223 if (pktopt && pktopt->ip6po_dest1) {
2224 optdata = (void *)pktopt->ip6po_dest1;
2225 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2226 optdatalen = (ip6e->ip6e_len + 1) << 3;
2227 }
2228 break;
2229 case IPV6_DSTOPTS:
2230 if (pktopt && pktopt->ip6po_dest2) {
2231 optdata = (void *)pktopt->ip6po_dest2;
2232 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2233 optdatalen = (ip6e->ip6e_len + 1) << 3;
2234 }
2235 break;
2236 case IPV6_NEXTHOP:
2237 if (pktopt && pktopt->ip6po_nexthop) {
2238 optdata = (void *)pktopt->ip6po_nexthop;
2239 optdatalen = pktopt->ip6po_nexthop->sa_len;
2240 }
2241 break;
2242 case IPV6_USE_MIN_MTU:
2243 if (pktopt)
2244 optdata = (void *)&pktopt->ip6po_minmtu;
2245 else
2246 optdata = (void *)&defminmtu;
2247 optdatalen = sizeof(int);
2248 break;
2249 case IPV6_DONTFRAG:
2250 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2251 on = 1;
2252 else
2253 on = 0;
2254 optdata = (void *)&on;
2255 optdatalen = sizeof(on);
2256 break;
2257 default: /* should not happen */
2258 #ifdef DIAGNOSTIC
2259 panic("ip6_getpcbopt: unexpected option\n");
2260 #endif
2261 return (ENOPROTOOPT);
2262 }
2263
2264 if (optdatalen > MCLBYTES)
2265 return (EMSGSIZE); /* XXX */
2266 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2267 if (optdatalen > MLEN)
2268 MCLGET(m, M_WAIT);
2269 m->m_len = optdatalen;
2270 if (optdatalen)
2271 memcpy(mtod(m, void *), optdata, optdatalen);
2272
2273 return (error);
2274 }
2275
2276 void
2277 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2278 {
2279 if (optname == -1 || optname == IPV6_PKTINFO) {
2280 if (pktopt->ip6po_pktinfo)
2281 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2282 pktopt->ip6po_pktinfo = NULL;
2283 }
2284 if (optname == -1 || optname == IPV6_HOPLIMIT)
2285 pktopt->ip6po_hlim = -1;
2286 if (optname == -1 || optname == IPV6_TCLASS)
2287 pktopt->ip6po_tclass = -1;
2288 if (optname == -1 || optname == IPV6_NEXTHOP) {
2289 if (pktopt->ip6po_nextroute.ro_rt) {
2290 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2291 pktopt->ip6po_nextroute.ro_rt = NULL;
2292 }
2293 if (pktopt->ip6po_nexthop)
2294 free(pktopt->ip6po_nexthop, M_IP6OPT);
2295 pktopt->ip6po_nexthop = NULL;
2296 }
2297 if (optname == -1 || optname == IPV6_HOPOPTS) {
2298 if (pktopt->ip6po_hbh)
2299 free(pktopt->ip6po_hbh, M_IP6OPT);
2300 pktopt->ip6po_hbh = NULL;
2301 }
2302 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2303 if (pktopt->ip6po_dest1)
2304 free(pktopt->ip6po_dest1, M_IP6OPT);
2305 pktopt->ip6po_dest1 = NULL;
2306 }
2307 if (optname == -1 || optname == IPV6_RTHDR) {
2308 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2309 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2310 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2311 if (pktopt->ip6po_route.ro_rt) {
2312 RTFREE(pktopt->ip6po_route.ro_rt);
2313 pktopt->ip6po_route.ro_rt = NULL;
2314 }
2315 }
2316 if (optname == -1 || optname == IPV6_DSTOPTS) {
2317 if (pktopt->ip6po_dest2)
2318 free(pktopt->ip6po_dest2, M_IP6OPT);
2319 pktopt->ip6po_dest2 = NULL;
2320 }
2321 }
2322
2323 #define PKTOPT_EXTHDRCPY(type) \
2324 do { \
2325 if (src->type) { \
2326 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2327 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2328 if (dst->type == NULL && canwait == M_NOWAIT) \
2329 goto bad; \
2330 memcpy(dst->type, src->type, hlen); \
2331 } \
2332 } while (/*CONSTCOND*/ 0)
2333
2334 static int
2335 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2336 {
2337 dst->ip6po_hlim = src->ip6po_hlim;
2338 dst->ip6po_tclass = src->ip6po_tclass;
2339 dst->ip6po_flags = src->ip6po_flags;
2340 if (src->ip6po_pktinfo) {
2341 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2342 M_IP6OPT, canwait);
2343 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2344 goto bad;
2345 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2346 }
2347 if (src->ip6po_nexthop) {
2348 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2349 M_IP6OPT, canwait);
2350 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2351 goto bad;
2352 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2353 src->ip6po_nexthop->sa_len);
2354 }
2355 PKTOPT_EXTHDRCPY(ip6po_hbh);
2356 PKTOPT_EXTHDRCPY(ip6po_dest1);
2357 PKTOPT_EXTHDRCPY(ip6po_dest2);
2358 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2359 return (0);
2360
2361 bad:
2362 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2363 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2364 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2365 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2366 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2367 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2368
2369 return (ENOBUFS);
2370 }
2371 #undef PKTOPT_EXTHDRCPY
2372
2373 struct ip6_pktopts *
2374 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2375 {
2376 int error;
2377 struct ip6_pktopts *dst;
2378
2379 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2380 if (dst == NULL && canwait == M_NOWAIT)
2381 return (NULL);
2382 ip6_initpktopts(dst);
2383
2384 if ((error = copypktopts(dst, src, canwait)) != 0) {
2385 free(dst, M_IP6OPT);
2386 return (NULL);
2387 }
2388
2389 return (dst);
2390 }
2391
2392 void
2393 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2394 {
2395 if (pktopt == NULL)
2396 return;
2397
2398 ip6_clearpktopts(pktopt, -1);
2399
2400 free(pktopt, M_IP6OPT);
2401 }
2402
2403 /*
2404 * Set the IP6 multicast options in response to user setsockopt().
2405 */
2406 static int
2407 ip6_setmoptions(optname, im6op, m)
2408 int optname;
2409 struct ip6_moptions **im6op;
2410 struct mbuf *m;
2411 {
2412 int error = 0;
2413 u_int loop, ifindex;
2414 struct ipv6_mreq *mreq;
2415 struct ifnet *ifp;
2416 struct ip6_moptions *im6o = *im6op;
2417 struct route_in6 ro;
2418 struct in6_multi_mship *imm;
2419 struct proc *p = curproc; /* XXX */
2420
2421 if (im6o == NULL) {
2422 /*
2423 * No multicast option buffer attached to the pcb;
2424 * allocate one and initialize to default values.
2425 */
2426 im6o = (struct ip6_moptions *)
2427 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2428
2429 if (im6o == NULL)
2430 return (ENOBUFS);
2431 *im6op = im6o;
2432 im6o->im6o_multicast_ifp = NULL;
2433 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2434 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2435 LIST_INIT(&im6o->im6o_memberships);
2436 }
2437
2438 switch (optname) {
2439
2440 case IPV6_MULTICAST_IF:
2441 /*
2442 * Select the interface for outgoing multicast packets.
2443 */
2444 if (m == NULL || m->m_len != sizeof(u_int)) {
2445 error = EINVAL;
2446 break;
2447 }
2448 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2449 if (ifindex != 0) {
2450 if (ifindex < 0 || if_indexlim <= ifindex ||
2451 !ifindex2ifnet[ifindex]) {
2452 error = ENXIO; /* XXX EINVAL? */
2453 break;
2454 }
2455 ifp = ifindex2ifnet[ifindex];
2456 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2457 error = EADDRNOTAVAIL;
2458 break;
2459 }
2460 } else
2461 ifp = NULL;
2462 im6o->im6o_multicast_ifp = ifp;
2463 break;
2464
2465 case IPV6_MULTICAST_HOPS:
2466 {
2467 /*
2468 * Set the IP6 hoplimit for outgoing multicast packets.
2469 */
2470 int optval;
2471 if (m == NULL || m->m_len != sizeof(int)) {
2472 error = EINVAL;
2473 break;
2474 }
2475 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2476 if (optval < -1 || optval >= 256)
2477 error = EINVAL;
2478 else if (optval == -1)
2479 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2480 else
2481 im6o->im6o_multicast_hlim = optval;
2482 break;
2483 }
2484
2485 case IPV6_MULTICAST_LOOP:
2486 /*
2487 * Set the loopback flag for outgoing multicast packets.
2488 * Must be zero or one.
2489 */
2490 if (m == NULL || m->m_len != sizeof(u_int)) {
2491 error = EINVAL;
2492 break;
2493 }
2494 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2495 if (loop > 1) {
2496 error = EINVAL;
2497 break;
2498 }
2499 im6o->im6o_multicast_loop = loop;
2500 break;
2501
2502 case IPV6_JOIN_GROUP:
2503 /*
2504 * Add a multicast group membership.
2505 * Group must be a valid IP6 multicast address.
2506 */
2507 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2508 error = EINVAL;
2509 break;
2510 }
2511 mreq = mtod(m, struct ipv6_mreq *);
2512 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2513 /*
2514 * We use the unspecified address to specify to accept
2515 * all multicast addresses. Only super user is allowed
2516 * to do this.
2517 */
2518 if (kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag))
2519 {
2520 error = EACCES;
2521 break;
2522 }
2523 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2524 error = EINVAL;
2525 break;
2526 }
2527
2528 /*
2529 * If no interface was explicitly specified, choose an
2530 * appropriate one according to the given multicast address.
2531 */
2532 if (mreq->ipv6mr_interface == 0) {
2533 struct sockaddr_in6 *dst;
2534
2535 /*
2536 * Look up the routing table for the
2537 * address, and choose the outgoing interface.
2538 * XXX: is it a good approach?
2539 */
2540 ro.ro_rt = NULL;
2541 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2542 bzero(dst, sizeof(*dst));
2543 dst->sin6_family = AF_INET6;
2544 dst->sin6_len = sizeof(*dst);
2545 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2546 rtalloc((struct route *)&ro);
2547 if (ro.ro_rt == NULL) {
2548 error = EADDRNOTAVAIL;
2549 break;
2550 }
2551 ifp = ro.ro_rt->rt_ifp;
2552 rtfree(ro.ro_rt);
2553 } else {
2554 /*
2555 * If the interface is specified, validate it.
2556 */
2557 if (mreq->ipv6mr_interface < 0 ||
2558 if_indexlim <= mreq->ipv6mr_interface ||
2559 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2560 error = ENXIO; /* XXX EINVAL? */
2561 break;
2562 }
2563 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2564 }
2565
2566 /*
2567 * See if we found an interface, and confirm that it
2568 * supports multicast
2569 */
2570 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2571 error = EADDRNOTAVAIL;
2572 break;
2573 }
2574
2575 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2576 error = EADDRNOTAVAIL; /* XXX: should not happen */
2577 break;
2578 }
2579
2580 /*
2581 * See if the membership already exists.
2582 */
2583 for (imm = im6o->im6o_memberships.lh_first;
2584 imm != NULL; imm = imm->i6mm_chain.le_next)
2585 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2586 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2587 &mreq->ipv6mr_multiaddr))
2588 break;
2589 if (imm != NULL) {
2590 error = EADDRINUSE;
2591 break;
2592 }
2593 /*
2594 * Everything looks good; add a new record to the multicast
2595 * address list for the given interface.
2596 */
2597 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2598 if (imm == NULL)
2599 break;
2600 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2601 break;
2602
2603 case IPV6_LEAVE_GROUP:
2604 /*
2605 * Drop a multicast group membership.
2606 * Group must be a valid IP6 multicast address.
2607 */
2608 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2609 error = EINVAL;
2610 break;
2611 }
2612 mreq = mtod(m, struct ipv6_mreq *);
2613
2614 /*
2615 * If an interface address was specified, get a pointer
2616 * to its ifnet structure.
2617 */
2618 if (mreq->ipv6mr_interface != 0) {
2619 if (mreq->ipv6mr_interface < 0 ||
2620 if_indexlim <= mreq->ipv6mr_interface ||
2621 !ifindex2ifnet[mreq->ipv6mr_interface]) {
2622 error = ENXIO; /* XXX EINVAL? */
2623 break;
2624 }
2625 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2626 } else
2627 ifp = NULL;
2628
2629 /* Fill in the scope zone ID */
2630 if (ifp) {
2631 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2632 /* XXX: should not happen */
2633 error = EADDRNOTAVAIL;
2634 break;
2635 }
2636 } else if (mreq->ipv6mr_interface != 0) {
2637 /*
2638 * XXX: This case would happens when the (positive)
2639 * index is in the valid range, but the corresponding
2640 * interface has been detached dynamically. The above
2641 * check probably avoids such case to happen here, but
2642 * we check it explicitly for safety.
2643 */
2644 error = EADDRNOTAVAIL;
2645 break;
2646 } else { /* ipv6mr_interface == 0 */
2647 struct sockaddr_in6 sa6_mc;
2648
2649 /*
2650 * The API spec says as follows:
2651 * If the interface index is specified as 0, the
2652 * system may choose a multicast group membership to
2653 * drop by matching the multicast address only.
2654 * On the other hand, we cannot disambiguate the scope
2655 * zone unless an interface is provided. Thus, we
2656 * check if there's ambiguity with the default scope
2657 * zone as the last resort.
2658 */
2659 bzero(&sa6_mc, sizeof(sa6_mc));
2660 sa6_mc.sin6_family = AF_INET6;
2661 sa6_mc.sin6_len = sizeof(sa6_mc);
2662 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2663 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2664 if (error != 0)
2665 break;
2666 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2667 }
2668
2669 /*
2670 * Find the membership in the membership list.
2671 */
2672 for (imm = im6o->im6o_memberships.lh_first;
2673 imm != NULL; imm = imm->i6mm_chain.le_next) {
2674 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2675 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2676 &mreq->ipv6mr_multiaddr))
2677 break;
2678 }
2679 if (imm == NULL) {
2680 /* Unable to resolve interface */
2681 error = EADDRNOTAVAIL;
2682 break;
2683 }
2684 /*
2685 * Give up the multicast address record to which the
2686 * membership points.
2687 */
2688 LIST_REMOVE(imm, i6mm_chain);
2689 in6_leavegroup(imm);
2690 break;
2691
2692 default:
2693 error = EOPNOTSUPP;
2694 break;
2695 }
2696
2697 /*
2698 * If all options have default values, no need to keep the mbuf.
2699 */
2700 if (im6o->im6o_multicast_ifp == NULL &&
2701 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2702 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2703 im6o->im6o_memberships.lh_first == NULL) {
2704 free(*im6op, M_IPMOPTS);
2705 *im6op = NULL;
2706 }
2707
2708 return (error);
2709 }
2710
2711 /*
2712 * Return the IP6 multicast options in response to user getsockopt().
2713 */
2714 static int
2715 ip6_getmoptions(optname, im6o, mp)
2716 int optname;
2717 struct ip6_moptions *im6o;
2718 struct mbuf **mp;
2719 {
2720 u_int *hlim, *loop, *ifindex;
2721
2722 *mp = m_get(M_WAIT, MT_SOOPTS);
2723
2724 switch (optname) {
2725
2726 case IPV6_MULTICAST_IF:
2727 ifindex = mtod(*mp, u_int *);
2728 (*mp)->m_len = sizeof(u_int);
2729 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2730 *ifindex = 0;
2731 else
2732 *ifindex = im6o->im6o_multicast_ifp->if_index;
2733 return (0);
2734
2735 case IPV6_MULTICAST_HOPS:
2736 hlim = mtod(*mp, u_int *);
2737 (*mp)->m_len = sizeof(u_int);
2738 if (im6o == NULL)
2739 *hlim = ip6_defmcasthlim;
2740 else
2741 *hlim = im6o->im6o_multicast_hlim;
2742 return (0);
2743
2744 case IPV6_MULTICAST_LOOP:
2745 loop = mtod(*mp, u_int *);
2746 (*mp)->m_len = sizeof(u_int);
2747 if (im6o == NULL)
2748 *loop = ip6_defmcasthlim;
2749 else
2750 *loop = im6o->im6o_multicast_loop;
2751 return (0);
2752
2753 default:
2754 return (EOPNOTSUPP);
2755 }
2756 }
2757
2758 /*
2759 * Discard the IP6 multicast options.
2760 */
2761 void
2762 ip6_freemoptions(im6o)
2763 struct ip6_moptions *im6o;
2764 {
2765 struct in6_multi_mship *imm;
2766
2767 if (im6o == NULL)
2768 return;
2769
2770 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2771 LIST_REMOVE(imm, i6mm_chain);
2772 in6_leavegroup(imm);
2773 }
2774 free(im6o, M_IPMOPTS);
2775 }
2776
2777 /*
2778 * Set IPv6 outgoing packet options based on advanced API.
2779 */
2780 int
2781 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2782 struct mbuf *control;
2783 struct ip6_pktopts *opt, *stickyopt;
2784 int priv, uproto;
2785 {
2786 struct cmsghdr *cm = 0;
2787
2788 if (control == NULL || opt == NULL)
2789 return (EINVAL);
2790
2791 ip6_initpktopts(opt);
2792 if (stickyopt) {
2793 int error;
2794
2795 /*
2796 * If stickyopt is provided, make a local copy of the options
2797 * for this particular packet, then override them by ancillary
2798 * objects.
2799 * XXX: copypktopts() does not copy the cached route to a next
2800 * hop (if any). This is not very good in terms of efficiency,
2801 * but we can allow this since this option should be rarely
2802 * used.
2803 */
2804 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2805 return (error);
2806 }
2807
2808 /*
2809 * XXX: Currently, we assume all the optional information is stored
2810 * in a single mbuf.
2811 */
2812 if (control->m_next)
2813 return (EINVAL);
2814
2815 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2816 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2817 int error;
2818
2819 if (control->m_len < CMSG_LEN(0))
2820 return (EINVAL);
2821
2822 cm = mtod(control, struct cmsghdr *);
2823 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2824 return (EINVAL);
2825 if (cm->cmsg_level != IPPROTO_IPV6)
2826 continue;
2827
2828 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2829 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2830 if (error)
2831 return (error);
2832 }
2833
2834 return (0);
2835 }
2836
2837 /*
2838 * Set a particular packet option, as a sticky option or an ancillary data
2839 * item. "len" can be 0 only when it's a sticky option.
2840 * We have 4 cases of combination of "sticky" and "cmsg":
2841 * "sticky=0, cmsg=0": impossible
2842 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2843 * "sticky=1, cmsg=0": RFC3542 socket option
2844 * "sticky=1, cmsg=1": RFC2292 socket option
2845 */
2846 static int
2847 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2848 int priv, int sticky, int cmsg, int uproto)
2849 {
2850 int minmtupolicy;
2851
2852 if (!sticky && !cmsg) {
2853 #ifdef DIAGNOSTIC
2854 printf("ip6_setpktopt: impossible case\n");
2855 #endif
2856 return (EINVAL);
2857 }
2858
2859 /*
2860 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2861 * not be specified in the context of RFC3542. Conversely,
2862 * RFC3542 types should not be specified in the context of RFC2292.
2863 */
2864 if (!cmsg) {
2865 switch (optname) {
2866 case IPV6_2292PKTINFO:
2867 case IPV6_2292HOPLIMIT:
2868 case IPV6_2292NEXTHOP:
2869 case IPV6_2292HOPOPTS:
2870 case IPV6_2292DSTOPTS:
2871 case IPV6_2292RTHDR:
2872 case IPV6_2292PKTOPTIONS:
2873 return (ENOPROTOOPT);
2874 }
2875 }
2876 if (sticky && cmsg) {
2877 switch (optname) {
2878 case IPV6_PKTINFO:
2879 case IPV6_HOPLIMIT:
2880 case IPV6_NEXTHOP:
2881 case IPV6_HOPOPTS:
2882 case IPV6_DSTOPTS:
2883 case IPV6_RTHDRDSTOPTS:
2884 case IPV6_RTHDR:
2885 case IPV6_USE_MIN_MTU:
2886 case IPV6_DONTFRAG:
2887 case IPV6_OTCLASS:
2888 case IPV6_TCLASS:
2889 return (ENOPROTOOPT);
2890 }
2891 }
2892
2893 switch (optname) {
2894 #ifdef RFC2292
2895 case IPV6_2292PKTINFO:
2896 #endif
2897 case IPV6_PKTINFO:
2898 {
2899 struct ifnet *ifp = NULL;
2900 struct in6_pktinfo *pktinfo;
2901
2902 if (len != sizeof(struct in6_pktinfo))
2903 return (EINVAL);
2904
2905 pktinfo = (struct in6_pktinfo *)buf;
2906
2907 /*
2908 * An application can clear any sticky IPV6_PKTINFO option by
2909 * doing a "regular" setsockopt with ipi6_addr being
2910 * in6addr_any and ipi6_ifindex being zero.
2911 * [RFC 3542, Section 6]
2912 */
2913 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2914 pktinfo->ipi6_ifindex == 0 &&
2915 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2916 ip6_clearpktopts(opt, optname);
2917 break;
2918 }
2919
2920 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2921 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2922 return (EINVAL);
2923 }
2924
2925 /* validate the interface index if specified. */
2926 if (pktinfo->ipi6_ifindex >= if_indexlim ||
2927 pktinfo->ipi6_ifindex < 0) {
2928 return (ENXIO);
2929 }
2930 if (pktinfo->ipi6_ifindex) {
2931 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
2932 if (ifp == NULL)
2933 return (ENXIO);
2934 }
2935
2936 /*
2937 * We store the address anyway, and let in6_selectsrc()
2938 * validate the specified address. This is because ipi6_addr
2939 * may not have enough information about its scope zone, and
2940 * we may need additional information (such as outgoing
2941 * interface or the scope zone of a destination address) to
2942 * disambiguate the scope.
2943 * XXX: the delay of the validation may confuse the
2944 * application when it is used as a sticky option.
2945 */
2946 if (opt->ip6po_pktinfo == NULL) {
2947 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2948 M_IP6OPT, M_NOWAIT);
2949 if (opt->ip6po_pktinfo == NULL)
2950 return (ENOBUFS);
2951 }
2952 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
2953 break;
2954 }
2955
2956 #ifdef RFC2292
2957 case IPV6_2292HOPLIMIT:
2958 #endif
2959 case IPV6_HOPLIMIT:
2960 {
2961 int *hlimp;
2962
2963 /*
2964 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2965 * to simplify the ordering among hoplimit options.
2966 */
2967 if (optname == IPV6_HOPLIMIT && sticky)
2968 return (ENOPROTOOPT);
2969
2970 if (len != sizeof(int))
2971 return (EINVAL);
2972 hlimp = (int *)buf;
2973 if (*hlimp < -1 || *hlimp > 255)
2974 return (EINVAL);
2975
2976 opt->ip6po_hlim = *hlimp;
2977 break;
2978 }
2979
2980 case IPV6_OTCLASS:
2981 if (len != sizeof(u_int8_t))
2982 return (EINVAL);
2983
2984 opt->ip6po_tclass = *(u_int8_t *)buf;
2985 break;
2986
2987 case IPV6_TCLASS:
2988 {
2989 int tclass;
2990
2991 if (len != sizeof(int))
2992 return (EINVAL);
2993 tclass = *(int *)buf;
2994 if (tclass < -1 || tclass > 255)
2995 return (EINVAL);
2996
2997 opt->ip6po_tclass = tclass;
2998 break;
2999 }
3000
3001 #ifdef RFC2292
3002 case IPV6_2292NEXTHOP:
3003 #endif
3004 case IPV6_NEXTHOP:
3005 if (!priv)
3006 return (EPERM);
3007
3008 if (len == 0) { /* just remove the option */
3009 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3010 break;
3011 }
3012
3013 /* check if cmsg_len is large enough for sa_len */
3014 if (len < sizeof(struct sockaddr) || len < *buf)
3015 return (EINVAL);
3016
3017 switch (((struct sockaddr *)buf)->sa_family) {
3018 case AF_INET6:
3019 {
3020 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3021 int error;
3022
3023 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3024 return (EINVAL);
3025
3026 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3027 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3028 return (EINVAL);
3029 }
3030 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3031 != 0) {
3032 return (error);
3033 }
3034 break;
3035 }
3036 case AF_LINK: /* eventually be supported? */
3037 default:
3038 return (EAFNOSUPPORT);
3039 }
3040
3041 /* turn off the previous option, then set the new option. */
3042 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3043 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3044 if (opt->ip6po_nexthop == NULL)
3045 return (ENOBUFS);
3046 memcpy(opt->ip6po_nexthop, buf, *buf);
3047 break;
3048
3049 #ifdef RFC2292
3050 case IPV6_2292HOPOPTS:
3051 #endif
3052 case IPV6_HOPOPTS:
3053 {
3054 struct ip6_hbh *hbh;
3055 int hbhlen;
3056
3057 /*
3058 * XXX: We don't allow a non-privileged user to set ANY HbH
3059 * options, since per-option restriction has too much
3060 * overhead.
3061 */
3062 if (!priv)
3063 return (EPERM);
3064
3065 if (len == 0) {
3066 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3067 break; /* just remove the option */
3068 }
3069
3070 /* message length validation */
3071 if (len < sizeof(struct ip6_hbh))
3072 return (EINVAL);
3073 hbh = (struct ip6_hbh *)buf;
3074 hbhlen = (hbh->ip6h_len + 1) << 3;
3075 if (len != hbhlen)
3076 return (EINVAL);
3077
3078 /* turn off the previous option, then set the new option. */
3079 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3080 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3081 if (opt->ip6po_hbh == NULL)
3082 return (ENOBUFS);
3083 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3084
3085 break;
3086 }
3087
3088 #ifdef RFC2292
3089 case IPV6_2292DSTOPTS:
3090 #endif
3091 case IPV6_DSTOPTS:
3092 case IPV6_RTHDRDSTOPTS:
3093 {
3094 struct ip6_dest *dest, **newdest = NULL;
3095 int destlen;
3096
3097 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3098 return (EPERM);
3099
3100 if (len == 0) {
3101 ip6_clearpktopts(opt, optname);
3102 break; /* just remove the option */
3103 }
3104
3105 /* message length validation */
3106 if (len < sizeof(struct ip6_dest))
3107 return (EINVAL);
3108 dest = (struct ip6_dest *)buf;
3109 destlen = (dest->ip6d_len + 1) << 3;
3110 if (len != destlen)
3111 return (EINVAL);
3112 /*
3113 * Determine the position that the destination options header
3114 * should be inserted; before or after the routing header.
3115 */
3116 switch (optname) {
3117 case IPV6_2292DSTOPTS:
3118 /*
3119 * The old advanced API is ambiguous on this point.
3120 * Our approach is to determine the position based
3121 * according to the existence of a routing header.
3122 * Note, however, that this depends on the order of the
3123 * extension headers in the ancillary data; the 1st
3124 * part of the destination options header must appear
3125 * before the routing header in the ancillary data,
3126 * too.
3127 * RFC3542 solved the ambiguity by introducing
3128 * separate ancillary data or option types.
3129 */
3130 if (opt->ip6po_rthdr == NULL)
3131 newdest = &opt->ip6po_dest1;
3132 else
3133 newdest = &opt->ip6po_dest2;
3134 break;
3135 case IPV6_RTHDRDSTOPTS:
3136 newdest = &opt->ip6po_dest1;
3137 break;
3138 case IPV6_DSTOPTS:
3139 newdest = &opt->ip6po_dest2;
3140 break;
3141 }
3142
3143 /* turn off the previous option, then set the new option. */
3144 ip6_clearpktopts(opt, optname);
3145 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3146 if (*newdest == NULL)
3147 return (ENOBUFS);
3148 memcpy(*newdest, dest, destlen);
3149
3150 break;
3151 }
3152
3153 #ifdef RFC2292
3154 case IPV6_2292RTHDR:
3155 #endif
3156 case IPV6_RTHDR:
3157 {
3158 struct ip6_rthdr *rth;
3159 int rthlen;
3160
3161 if (len == 0) {
3162 ip6_clearpktopts(opt, IPV6_RTHDR);
3163 break; /* just remove the option */
3164 }
3165
3166 /* message length validation */
3167 if (len < sizeof(struct ip6_rthdr))
3168 return (EINVAL);
3169 rth = (struct ip6_rthdr *)buf;
3170 rthlen = (rth->ip6r_len + 1) << 3;
3171 if (len != rthlen)
3172 return (EINVAL);
3173 switch (rth->ip6r_type) {
3174 case IPV6_RTHDR_TYPE_0:
3175 if (rth->ip6r_len == 0) /* must contain one addr */
3176 return (EINVAL);
3177 if (rth->ip6r_len % 2) /* length must be even */
3178 return (EINVAL);
3179 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3180 return (EINVAL);
3181 break;
3182 default:
3183 return (EINVAL); /* not supported */
3184 }
3185 /* turn off the previous option */
3186 ip6_clearpktopts(opt, IPV6_RTHDR);
3187 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3188 if (opt->ip6po_rthdr == NULL)
3189 return (ENOBUFS);
3190 memcpy(opt->ip6po_rthdr, rth, rthlen);
3191 break;
3192 }
3193
3194 case IPV6_USE_MIN_MTU:
3195 if (len != sizeof(int))
3196 return (EINVAL);
3197 minmtupolicy = *(int *)buf;
3198 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3199 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3200 minmtupolicy != IP6PO_MINMTU_ALL) {
3201 return (EINVAL);
3202 }
3203 opt->ip6po_minmtu = minmtupolicy;
3204 break;
3205
3206 case IPV6_DONTFRAG:
3207 if (len != sizeof(int))
3208 return (EINVAL);
3209
3210 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3211 /*
3212 * we ignore this option for TCP sockets.
3213 * (RFC3542 leaves this case unspecified.)
3214 */
3215 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3216 } else
3217 opt->ip6po_flags |= IP6PO_DONTFRAG;
3218 break;
3219
3220 default:
3221 return (ENOPROTOOPT);
3222 } /* end of switch */
3223
3224 return (0);
3225 }
3226
3227 /*
3228 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3229 * packet to the input queue of a specified interface. Note that this
3230 * calls the output routine of the loopback "driver", but with an interface
3231 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3232 */
3233 void
3234 ip6_mloopback(ifp, m, dst)
3235 struct ifnet *ifp;
3236 struct mbuf *m;
3237 struct sockaddr_in6 *dst;
3238 {
3239 struct mbuf *copym;
3240 struct ip6_hdr *ip6;
3241
3242 copym = m_copy(m, 0, M_COPYALL);
3243 if (copym == NULL)
3244 return;
3245
3246 /*
3247 * Make sure to deep-copy IPv6 header portion in case the data
3248 * is in an mbuf cluster, so that we can safely override the IPv6
3249 * header portion later.
3250 */
3251 if ((copym->m_flags & M_EXT) != 0 ||
3252 copym->m_len < sizeof(struct ip6_hdr)) {
3253 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3254 if (copym == NULL)
3255 return;
3256 }
3257
3258 #ifdef DIAGNOSTIC
3259 if (copym->m_len < sizeof(*ip6)) {
3260 m_freem(copym);
3261 return;
3262 }
3263 #endif
3264
3265 ip6 = mtod(copym, struct ip6_hdr *);
3266 /*
3267 * clear embedded scope identifiers if necessary.
3268 * in6_clearscope will touch the addresses only when necessary.
3269 */
3270 in6_clearscope(&ip6->ip6_src);
3271 in6_clearscope(&ip6->ip6_dst);
3272
3273 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
3274 }
3275
3276 /*
3277 * Chop IPv6 header off from the payload.
3278 */
3279 static int
3280 ip6_splithdr(m, exthdrs)
3281 struct mbuf *m;
3282 struct ip6_exthdrs *exthdrs;
3283 {
3284 struct mbuf *mh;
3285 struct ip6_hdr *ip6;
3286
3287 ip6 = mtod(m, struct ip6_hdr *);
3288 if (m->m_len > sizeof(*ip6)) {
3289 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3290 if (mh == 0) {
3291 m_freem(m);
3292 return ENOBUFS;
3293 }
3294 M_MOVE_PKTHDR(mh, m);
3295 MH_ALIGN(mh, sizeof(*ip6));
3296 m->m_len -= sizeof(*ip6);
3297 m->m_data += sizeof(*ip6);
3298 mh->m_next = m;
3299 m = mh;
3300 m->m_len = sizeof(*ip6);
3301 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3302 }
3303 exthdrs->ip6e_ip6 = m;
3304 return 0;
3305 }
3306
3307 /*
3308 * Compute IPv6 extension header length.
3309 */
3310 int
3311 ip6_optlen(in6p)
3312 struct in6pcb *in6p;
3313 {
3314 int len;
3315
3316 if (!in6p->in6p_outputopts)
3317 return 0;
3318
3319 len = 0;
3320 #define elen(x) \
3321 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3322
3323 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3324 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3325 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3326 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3327 return len;
3328 #undef elen
3329 }
3330