ip6_output.c revision 1.82 1 /* $NetBSD: ip6_output.c,v 1.82 2004/03/23 18:21:38 martti 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.82 2004/03/23 18:21:38 martti Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69 #include "opt_pfil_hooks.h"
70
71 #include <sys/param.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/errno.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80
81 #include <net/if.h>
82 #include <net/route.h>
83 #ifdef PFIL_HOOKS
84 #include <net/pfil.h>
85 #endif
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/ip6protosw.h>
95
96 #ifdef IPSEC
97 #include <netinet6/ipsec.h>
98 #include <netkey/key.h>
99 #endif /* IPSEC */
100
101 #include "loop.h"
102
103 #include <net/net_osdep.h>
104
105 #ifdef PFIL_HOOKS
106 extern struct pfil_head inet6_pfil_hook; /* XXX */
107 #endif
108
109 struct ip6_exthdrs {
110 struct mbuf *ip6e_ip6;
111 struct mbuf *ip6e_hbh;
112 struct mbuf *ip6e_dest1;
113 struct mbuf *ip6e_rthdr;
114 struct mbuf *ip6e_dest2;
115 };
116
117 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
118 struct socket *));
119 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
120 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
121 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
122 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
123 struct ip6_frag **));
124 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
125 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
126
127 extern struct ifnet loif[NLOOP];
128
129 /*
130 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
131 * header (with pri, len, nxt, hlim, src, dst).
132 * This function may modify ver and hlim only.
133 * The mbuf chain containing the packet will be freed.
134 * The mbuf opt, if present, will not be freed.
135 *
136 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
137 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
138 * which is rt_rmx.rmx_mtu.
139 */
140 int
141 ip6_output(m0, opt, ro, flags, im6o, so, ifpp)
142 struct mbuf *m0;
143 struct ip6_pktopts *opt;
144 struct route_in6 *ro;
145 int flags;
146 struct ip6_moptions *im6o;
147 struct socket *so;
148 struct ifnet **ifpp; /* XXX: just for statistics */
149 {
150 struct ip6_hdr *ip6, *mhip6;
151 struct ifnet *ifp, *origifp;
152 struct mbuf *m = m0;
153 int hlen, tlen, len, off;
154 struct route_in6 ip6route;
155 struct sockaddr_in6 *dst;
156 int error = 0;
157 u_long mtu;
158 int alwaysfrag, dontfrag;
159 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
160 struct ip6_exthdrs exthdrs;
161 struct in6_addr finaldst;
162 struct route_in6 *ro_pmtu = NULL;
163 int hdrsplit = 0;
164 int needipsec = 0;
165 #ifdef IPSEC
166 int needipsectun = 0;
167 struct secpolicy *sp = NULL;
168
169 ip6 = mtod(m, struct ip6_hdr *);
170 #endif /* IPSEC */
171
172 #define MAKE_EXTHDR(hp, mp) \
173 do { \
174 if (hp) { \
175 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
176 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
177 ((eh)->ip6e_len + 1) << 3); \
178 if (error) \
179 goto freehdrs; \
180 } \
181 } while (/*CONSTCOND*/ 0)
182
183 bzero(&exthdrs, sizeof(exthdrs));
184 if (opt) {
185 /* Hop-by-Hop options header */
186 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
187 /* Destination options header(1st part) */
188 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
189 /* Routing header */
190 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
191 /* Destination options header(2nd part) */
192 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
193 }
194
195 #ifdef IPSEC
196 if ((flags & IPV6_FORWARDING) != 0) {
197 needipsec = 0;
198 goto skippolicycheck;
199 }
200
201 /* get a security policy for this packet */
202 if (so == NULL)
203 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
204 else {
205 if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
206 IPSEC_DIR_OUTBOUND)) {
207 needipsec = 0;
208 goto skippolicycheck;
209 }
210 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
211 }
212
213 if (sp == NULL) {
214 ipsec6stat.out_inval++;
215 goto freehdrs;
216 }
217
218 error = 0;
219
220 /* check policy */
221 switch (sp->policy) {
222 case IPSEC_POLICY_DISCARD:
223 /*
224 * This packet is just discarded.
225 */
226 ipsec6stat.out_polvio++;
227 goto freehdrs;
228
229 case IPSEC_POLICY_BYPASS:
230 case IPSEC_POLICY_NONE:
231 /* no need to do IPsec. */
232 needipsec = 0;
233 break;
234
235 case IPSEC_POLICY_IPSEC:
236 if (sp->req == NULL) {
237 /* XXX should be panic ? */
238 printf("ip6_output: No IPsec request specified.\n");
239 error = EINVAL;
240 goto freehdrs;
241 }
242 needipsec = 1;
243 break;
244
245 case IPSEC_POLICY_ENTRUST:
246 default:
247 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
248 }
249
250 skippolicycheck:;
251 #endif /* IPSEC */
252
253 /*
254 * Calculate the total length of the extension header chain.
255 * Keep the length of the unfragmentable part for fragmentation.
256 */
257 optlen = 0;
258 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
259 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
260 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
261 unfragpartlen = optlen + sizeof(struct ip6_hdr);
262 /* NOTE: we don't add AH/ESP length here. do that later. */
263 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
264
265 /*
266 * If we need IPsec, or there is at least one extension header,
267 * separate IP6 header from the payload.
268 */
269 if ((needipsec || optlen) && !hdrsplit) {
270 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
271 m = NULL;
272 goto freehdrs;
273 }
274 m = exthdrs.ip6e_ip6;
275 hdrsplit++;
276 }
277
278 /* adjust pointer */
279 ip6 = mtod(m, struct ip6_hdr *);
280
281 /* adjust mbuf packet header length */
282 m->m_pkthdr.len += optlen;
283 plen = m->m_pkthdr.len - sizeof(*ip6);
284
285 /* If this is a jumbo payload, insert a jumbo payload option. */
286 if (plen > IPV6_MAXPACKET) {
287 if (!hdrsplit) {
288 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
289 m = NULL;
290 goto freehdrs;
291 }
292 m = exthdrs.ip6e_ip6;
293 hdrsplit++;
294 }
295 /* adjust pointer */
296 ip6 = mtod(m, struct ip6_hdr *);
297 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
298 goto freehdrs;
299 ip6->ip6_plen = 0;
300 } else
301 ip6->ip6_plen = htons(plen);
302
303 /*
304 * Concatenate headers and fill in next header fields.
305 * Here we have, on "m"
306 * IPv6 payload
307 * and we insert headers accordingly. Finally, we should be getting:
308 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
309 *
310 * during the header composing process, "m" points to IPv6 header.
311 * "mprev" points to an extension header prior to esp.
312 */
313 {
314 u_char *nexthdrp = &ip6->ip6_nxt;
315 struct mbuf *mprev = m;
316
317 /*
318 * we treat dest2 specially. this makes IPsec processing
319 * much easier. the goal here is to make mprev point the
320 * mbuf prior to dest2.
321 *
322 * result: IPv6 dest2 payload
323 * m and mprev will point to IPv6 header.
324 */
325 if (exthdrs.ip6e_dest2) {
326 if (!hdrsplit)
327 panic("assumption failed: hdr not split");
328 exthdrs.ip6e_dest2->m_next = m->m_next;
329 m->m_next = exthdrs.ip6e_dest2;
330 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
331 ip6->ip6_nxt = IPPROTO_DSTOPTS;
332 }
333
334 #define MAKE_CHAIN(m, mp, p, i)\
335 do {\
336 if (m) {\
337 if (!hdrsplit) \
338 panic("assumption failed: hdr not split"); \
339 *mtod((m), u_char *) = *(p);\
340 *(p) = (i);\
341 p = mtod((m), u_char *);\
342 (m)->m_next = (mp)->m_next;\
343 (mp)->m_next = (m);\
344 (mp) = (m);\
345 }\
346 } while (/*CONSTCOND*/ 0)
347 /*
348 * result: IPv6 hbh dest1 rthdr dest2 payload
349 * m will point to IPv6 header. mprev will point to the
350 * extension header prior to dest2 (rthdr in the above case).
351 */
352 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
353 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
354 IPPROTO_DSTOPTS);
355 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
356 IPPROTO_ROUTING);
357
358 #ifdef IPSEC
359 if (!needipsec)
360 goto skip_ipsec2;
361
362 /*
363 * pointers after IPsec headers are not valid any more.
364 * other pointers need a great care too.
365 * (IPsec routines should not mangle mbufs prior to AH/ESP)
366 */
367 exthdrs.ip6e_dest2 = NULL;
368
369 {
370 struct ip6_rthdr *rh = NULL;
371 int segleft_org = 0;
372 struct ipsec_output_state state;
373
374 if (exthdrs.ip6e_rthdr) {
375 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
376 segleft_org = rh->ip6r_segleft;
377 rh->ip6r_segleft = 0;
378 }
379
380 bzero(&state, sizeof(state));
381 state.m = m;
382 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
383 &needipsectun);
384 m = state.m;
385 if (error) {
386 /* mbuf is already reclaimed in ipsec6_output_trans. */
387 m = NULL;
388 switch (error) {
389 case EHOSTUNREACH:
390 case ENETUNREACH:
391 case EMSGSIZE:
392 case ENOBUFS:
393 case ENOMEM:
394 break;
395 default:
396 printf("ip6_output (ipsec): error code %d\n", error);
397 /* FALLTHROUGH */
398 case ENOENT:
399 /* don't show these error codes to the user */
400 error = 0;
401 break;
402 }
403 goto bad;
404 }
405 if (exthdrs.ip6e_rthdr) {
406 /* ah6_output doesn't modify mbuf chain */
407 rh->ip6r_segleft = segleft_org;
408 }
409 }
410 skip_ipsec2:;
411 #endif
412 }
413
414 /*
415 * If there is a routing header, replace destination address field
416 * with the first hop of the routing header.
417 */
418 if (exthdrs.ip6e_rthdr) {
419 struct ip6_rthdr *rh;
420 struct ip6_rthdr0 *rh0;
421 struct in6_addr *addr;
422
423 rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
424 struct ip6_rthdr *));
425 finaldst = ip6->ip6_dst;
426 switch (rh->ip6r_type) {
427 case IPV6_RTHDR_TYPE_0:
428 rh0 = (struct ip6_rthdr0 *)rh;
429 addr = (struct in6_addr *)(rh0 + 1);
430 ip6->ip6_dst = addr[0];
431 bcopy(&addr[1], &addr[0],
432 sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1));
433 addr[rh0->ip6r0_segleft - 1] = finaldst;
434 break;
435 default: /* is it possible? */
436 error = EINVAL;
437 goto bad;
438 }
439 }
440
441 /* Source address validation */
442 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
443 (flags & IPV6_UNSPECSRC) == 0) {
444 error = EOPNOTSUPP;
445 ip6stat.ip6s_badscope++;
446 goto bad;
447 }
448 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
449 error = EOPNOTSUPP;
450 ip6stat.ip6s_badscope++;
451 goto bad;
452 }
453
454 ip6stat.ip6s_localout++;
455
456 /*
457 * Route packet.
458 */
459 /* initialize cached route */
460 if (ro == 0) {
461 ro = &ip6route;
462 bzero((caddr_t)ro, sizeof(*ro));
463 }
464 ro_pmtu = ro;
465 if (opt && opt->ip6po_rthdr)
466 ro = &opt->ip6po_route;
467 dst = (struct sockaddr_in6 *)&ro->ro_dst;
468 /*
469 * If there is a cached route,
470 * check that it is to the same destination
471 * and is still up. If not, free it and try again.
472 */
473 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
474 dst->sin6_family != AF_INET6 ||
475 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
476 RTFREE(ro->ro_rt);
477 ro->ro_rt = (struct rtentry *)0;
478 }
479 if (ro->ro_rt == 0) {
480 bzero(dst, sizeof(*dst));
481 dst->sin6_family = AF_INET6;
482 dst->sin6_len = sizeof(struct sockaddr_in6);
483 dst->sin6_addr = ip6->ip6_dst;
484 }
485 #ifdef IPSEC
486 if (needipsec && needipsectun) {
487 struct ipsec_output_state state;
488
489 /*
490 * All the extension headers will become inaccessible
491 * (since they can be encrypted).
492 * Don't panic, we need no more updates to extension headers
493 * on inner IPv6 packet (since they are now encapsulated).
494 *
495 * IPv6 [ESP|AH] IPv6 [extension headers] payload
496 */
497 bzero(&exthdrs, sizeof(exthdrs));
498 exthdrs.ip6e_ip6 = m;
499
500 bzero(&state, sizeof(state));
501 state.m = m;
502 state.ro = (struct route *)ro;
503 state.dst = (struct sockaddr *)dst;
504
505 error = ipsec6_output_tunnel(&state, sp, flags);
506
507 m = state.m;
508 ro = (struct route_in6 *)state.ro;
509 dst = (struct sockaddr_in6 *)state.dst;
510 if (error) {
511 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
512 m0 = m = NULL;
513 m = NULL;
514 switch (error) {
515 case EHOSTUNREACH:
516 case ENETUNREACH:
517 case EMSGSIZE:
518 case ENOBUFS:
519 case ENOMEM:
520 break;
521 default:
522 printf("ip6_output (ipsec): error code %d\n", error);
523 /* FALLTHROUGH */
524 case ENOENT:
525 /* don't show these error codes to the user */
526 error = 0;
527 break;
528 }
529 goto bad;
530 }
531
532 exthdrs.ip6e_ip6 = m;
533 }
534 #endif /* IPSEC */
535
536 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
537 /* Unicast */
538
539 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
540 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
541 /* xxx
542 * interface selection comes here
543 * if an interface is specified from an upper layer,
544 * ifp must point it.
545 */
546 if (ro->ro_rt == 0) {
547 /*
548 * non-bsdi always clone routes, if parent is
549 * PRF_CLONING.
550 */
551 rtalloc((struct route *)ro);
552 }
553 if (ro->ro_rt == 0) {
554 ip6stat.ip6s_noroute++;
555 error = EHOSTUNREACH;
556 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
557 goto bad;
558 }
559 ifp = ro->ro_rt->rt_ifp;
560 ro->ro_rt->rt_use++;
561 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
562 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
563 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
564
565 in6_ifstat_inc(ifp, ifs6_out_request);
566
567 /*
568 * Check if the outgoing interface conflicts with
569 * the interface specified by ifi6_ifindex (if specified).
570 * Note that loopback interface is always okay.
571 * (this may happen when we are sending a packet to one of
572 * our own addresses.)
573 */
574 if (opt && opt->ip6po_pktinfo &&
575 opt->ip6po_pktinfo->ipi6_ifindex) {
576 if (!(ifp->if_flags & IFF_LOOPBACK) &&
577 ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
578 ip6stat.ip6s_noroute++;
579 in6_ifstat_inc(ifp, ifs6_out_discard);
580 error = EHOSTUNREACH;
581 goto bad;
582 }
583 }
584
585 if (opt && opt->ip6po_hlim != -1)
586 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
587 } else {
588 /* Multicast */
589 struct in6_multi *in6m;
590
591 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
592
593 /*
594 * See if the caller provided any multicast options
595 */
596 ifp = NULL;
597 if (im6o != NULL) {
598 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
599 if (im6o->im6o_multicast_ifp != NULL)
600 ifp = im6o->im6o_multicast_ifp;
601 } else
602 ip6->ip6_hlim = ip6_defmcasthlim;
603
604 /*
605 * See if the caller provided the outgoing interface
606 * as an ancillary data.
607 * Boundary check for ifindex is assumed to be already done.
608 */
609 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
610 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
611
612 /*
613 * If the destination is a node-local scope multicast,
614 * the packet should be loop-backed only.
615 */
616 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
617 /*
618 * If the outgoing interface is already specified,
619 * it should be a loopback interface.
620 */
621 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
622 ip6stat.ip6s_badscope++;
623 error = ENETUNREACH; /* XXX: better error? */
624 /* XXX correct ifp? */
625 in6_ifstat_inc(ifp, ifs6_out_discard);
626 goto bad;
627 } else {
628 ifp = &loif[0];
629 }
630 }
631
632 if (opt && opt->ip6po_hlim != -1)
633 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
634
635 /*
636 * If caller did not provide an interface lookup a
637 * default in the routing table. This is either a
638 * default for the speicfied group (i.e. a host
639 * route), or a multicast default (a route for the
640 * ``net'' ff00::/8).
641 */
642 if (ifp == NULL) {
643 if (ro->ro_rt == 0) {
644 ro->ro_rt = rtalloc1((struct sockaddr *)
645 &ro->ro_dst, 0);
646 }
647 if (ro->ro_rt == 0) {
648 ip6stat.ip6s_noroute++;
649 error = EHOSTUNREACH;
650 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
651 goto bad;
652 }
653 ifp = ro->ro_rt->rt_ifp;
654 ro->ro_rt->rt_use++;
655 }
656
657 if ((flags & IPV6_FORWARDING) == 0)
658 in6_ifstat_inc(ifp, ifs6_out_request);
659 in6_ifstat_inc(ifp, ifs6_out_mcast);
660
661 /*
662 * Confirm that the outgoing interface supports multicast.
663 */
664 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
665 ip6stat.ip6s_noroute++;
666 in6_ifstat_inc(ifp, ifs6_out_discard);
667 error = ENETUNREACH;
668 goto bad;
669 }
670 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
671 if (in6m != NULL &&
672 (im6o == NULL || im6o->im6o_multicast_loop)) {
673 /*
674 * If we belong to the destination multicast group
675 * on the outgoing interface, and the caller did not
676 * forbid loopback, loop back a copy.
677 */
678 ip6_mloopback(ifp, m, dst);
679 } else {
680 /*
681 * If we are acting as a multicast router, perform
682 * multicast forwarding as if the packet had just
683 * arrived on the interface to which we are about
684 * to send. The multicast forwarding function
685 * recursively calls this function, using the
686 * IPV6_FORWARDING flag to prevent infinite recursion.
687 *
688 * Multicasts that are looped back by ip6_mloopback(),
689 * above, will be forwarded by the ip6_input() routine,
690 * if necessary.
691 */
692 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
693 if (ip6_mforward(ip6, ifp, m) != 0) {
694 m_freem(m);
695 goto done;
696 }
697 }
698 }
699 /*
700 * Multicasts with a hoplimit of zero may be looped back,
701 * above, but must not be transmitted on a network.
702 * Also, multicasts addressed to the loopback interface
703 * are not sent -- the above call to ip6_mloopback() will
704 * loop back a copy if this host actually belongs to the
705 * destination group on the loopback interface.
706 */
707 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
708 m_freem(m);
709 goto done;
710 }
711 }
712
713 /*
714 * Fill the outgoing inteface to tell the upper layer
715 * to increment per-interface statistics.
716 */
717 if (ifpp)
718 *ifpp = ifp;
719
720 /* Determine path MTU. */
721 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
722 &alwaysfrag)) != 0)
723 goto bad;
724
725 /*
726 * The caller of this function may specify to use the minimum MTU
727 * in some cases.
728 */
729 if (mtu > IPV6_MMTU) {
730 if ((flags & IPV6_MINMTU))
731 mtu = IPV6_MMTU;
732 }
733
734 /* Fake scoped addresses */
735 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
736 /*
737 * If source or destination address is a scoped address, and
738 * the packet is going to be sent to a loopback interface,
739 * we should keep the original interface.
740 */
741
742 /*
743 * XXX: this is a very experimental and temporary solution.
744 * We eventually have sockaddr_in6 and use the sin6_scope_id
745 * field of the structure here.
746 * We rely on the consistency between two scope zone ids
747 * of source add destination, which should already be assured
748 * Larger scopes than link will be supported in the near
749 * future.
750 */
751 origifp = NULL;
752 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
753 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
754 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
755 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
756 /*
757 * XXX: origifp can be NULL even in those two cases above.
758 * For example, if we remove the (only) link-local address
759 * from the loopback interface, and try to send a link-local
760 * address without link-id information. Then the source
761 * address is ::1, and the destination address is the
762 * link-local address with its s6_addr16[1] being zero.
763 * What is worse, if the packet goes to the loopback interface
764 * by a default rejected route, the null pointer would be
765 * passed to looutput, and the kernel would hang.
766 * The following last resort would prevent such disaster.
767 */
768 if (origifp == NULL)
769 origifp = ifp;
770 } else
771 origifp = ifp;
772 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
773 ip6->ip6_src.s6_addr16[1] = 0;
774 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
775 ip6->ip6_dst.s6_addr16[1] = 0;
776
777 /*
778 * If the outgoing packet contains a hop-by-hop options header,
779 * it must be examined and processed even by the source node.
780 * (RFC 2460, section 4.)
781 */
782 if (exthdrs.ip6e_hbh) {
783 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
784 u_int32_t dummy1; /* XXX unused */
785 u_int32_t dummy2; /* XXX unused */
786
787 /*
788 * XXX: if we have to send an ICMPv6 error to the sender,
789 * we need the M_LOOP flag since icmp6_error() expects
790 * the IPv6 and the hop-by-hop options header are
791 * continuous unless the flag is set.
792 */
793 m->m_flags |= M_LOOP;
794 m->m_pkthdr.rcvif = ifp;
795 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
796 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
797 &dummy1, &dummy2) < 0) {
798 /* m was already freed at this point */
799 error = EINVAL;/* better error? */
800 goto done;
801 }
802 m->m_flags &= ~M_LOOP; /* XXX */
803 m->m_pkthdr.rcvif = NULL;
804 }
805
806 #ifdef PFIL_HOOKS
807 /*
808 * Run through list of hooks for output packets.
809 */
810 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
811 goto done;
812 if (m == NULL)
813 goto done;
814 ip6 = mtod(m, struct ip6_hdr *);
815 #endif /* PFIL_HOOKS */
816 /*
817 * Send the packet to the outgoing interface.
818 * If necessary, do IPv6 fragmentation before sending.
819 *
820 * the logic here is rather complex:
821 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
822 * 1-a: send as is if tlen <= path mtu
823 * 1-b: fragment if tlen > path mtu
824 *
825 * 2: if user asks us not to fragment (dontfrag == 1)
826 * 2-a: send as is if tlen <= interface mtu
827 * 2-b: error if tlen > interface mtu
828 *
829 * 3: if we always need to attach fragment header (alwaysfrag == 1)
830 * always fragment
831 *
832 * 4: if dontfrag == 1 && alwaysfrag == 1
833 * error, as we cannot handle this conflicting request
834 */
835 tlen = m->m_pkthdr.len;
836
837 dontfrag = 0;
838 if (dontfrag && alwaysfrag) { /* case 4 */
839 /* conflicting request - can't transmit */
840 error = EMSGSIZE;
841 goto bad;
842 }
843 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
844 /*
845 * Even if the DONTFRAG option is specified, we cannot send the
846 * packet when the data length is larger than the MTU of the
847 * outgoing interface.
848 * Notify the error by sending IPV6_PATHMTU ancillary data as
849 * well as returning an error code (the latter is not described
850 * in the API spec.)
851 */
852 u_int32_t mtu32;
853 struct ip6ctlparam ip6cp;
854
855 mtu32 = (u_int32_t)mtu;
856 bzero(&ip6cp, sizeof(ip6cp));
857 ip6cp.ip6c_cmdarg = (void *)&mtu32;
858 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
859 (void *)&ip6cp);
860
861 error = EMSGSIZE;
862 goto bad;
863 }
864
865 /*
866 * transmit packet without fragmentation
867 */
868 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
869 struct in6_ifaddr *ia6;
870
871 ip6 = mtod(m, struct ip6_hdr *);
872 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
873 if (ia6) {
874 /* Record statistics for this interface address. */
875 ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
876 }
877 #ifdef IPSEC
878 /* clean ipsec history once it goes out of the node */
879 ipsec_delaux(m);
880 #endif
881 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
882 goto done;
883 }
884
885 /*
886 * try to fragment the packet. case 1-b and 3
887 */
888 if (mtu < IPV6_MMTU) {
889 /* path MTU cannot be less than IPV6_MMTU */
890 error = EMSGSIZE;
891 in6_ifstat_inc(ifp, ifs6_out_fragfail);
892 goto bad;
893 } else if (ip6->ip6_plen == 0) {
894 /* jumbo payload cannot be fragmented */
895 error = EMSGSIZE;
896 in6_ifstat_inc(ifp, ifs6_out_fragfail);
897 goto bad;
898 } else {
899 struct mbuf **mnext, *m_frgpart;
900 struct ip6_frag *ip6f;
901 u_int32_t id = htonl(ip6_randomid());
902 u_char nextproto;
903 struct ip6ctlparam ip6cp;
904 u_int32_t mtu32;
905
906 /*
907 * Too large for the destination or interface;
908 * fragment if possible.
909 * Must be able to put at least 8 bytes per fragment.
910 */
911 hlen = unfragpartlen;
912 if (mtu > IPV6_MAXPACKET)
913 mtu = IPV6_MAXPACKET;
914
915 /* Notify a proper path MTU to applications. */
916 mtu32 = (u_int32_t)mtu;
917 bzero(&ip6cp, sizeof(ip6cp));
918 ip6cp.ip6c_cmdarg = (void *)&mtu32;
919 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
920 (void *)&ip6cp);
921
922 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
923 if (len < 8) {
924 error = EMSGSIZE;
925 in6_ifstat_inc(ifp, ifs6_out_fragfail);
926 goto bad;
927 }
928
929 mnext = &m->m_nextpkt;
930
931 /*
932 * Change the next header field of the last header in the
933 * unfragmentable part.
934 */
935 if (exthdrs.ip6e_rthdr) {
936 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
937 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
938 } else if (exthdrs.ip6e_dest1) {
939 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
940 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
941 } else if (exthdrs.ip6e_hbh) {
942 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
943 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
944 } else {
945 nextproto = ip6->ip6_nxt;
946 ip6->ip6_nxt = IPPROTO_FRAGMENT;
947 }
948
949 /*
950 * Loop through length of segment after first fragment,
951 * make new header and copy data of each part and link onto
952 * chain.
953 */
954 m0 = m;
955 for (off = hlen; off < tlen; off += len) {
956 struct mbuf *mlast;
957
958 MGETHDR(m, M_DONTWAIT, MT_HEADER);
959 if (!m) {
960 error = ENOBUFS;
961 ip6stat.ip6s_odropped++;
962 goto sendorfree;
963 }
964 m->m_pkthdr.rcvif = NULL;
965 m->m_flags = m0->m_flags & M_COPYFLAGS;
966 *mnext = m;
967 mnext = &m->m_nextpkt;
968 m->m_data += max_linkhdr;
969 mhip6 = mtod(m, struct ip6_hdr *);
970 *mhip6 = *ip6;
971 m->m_len = sizeof(*mhip6);
972 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
973 if (error) {
974 ip6stat.ip6s_odropped++;
975 goto sendorfree;
976 }
977 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
978 if (off + len >= tlen)
979 len = tlen - off;
980 else
981 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
982 mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
983 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
984 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
985 error = ENOBUFS;
986 ip6stat.ip6s_odropped++;
987 goto sendorfree;
988 }
989 for (mlast = m; mlast->m_next; mlast = mlast->m_next)
990 ;
991 mlast->m_next = m_frgpart;
992 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
993 m->m_pkthdr.rcvif = (struct ifnet *)0;
994 ip6f->ip6f_reserved = 0;
995 ip6f->ip6f_ident = id;
996 ip6f->ip6f_nxt = nextproto;
997 ip6stat.ip6s_ofragments++;
998 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
999 }
1000
1001 in6_ifstat_inc(ifp, ifs6_out_fragok);
1002 }
1003
1004 /*
1005 * Remove leading garbages.
1006 */
1007 sendorfree:
1008 m = m0->m_nextpkt;
1009 m0->m_nextpkt = 0;
1010 m_freem(m0);
1011 for (m0 = m; m; m = m0) {
1012 m0 = m->m_nextpkt;
1013 m->m_nextpkt = 0;
1014 if (error == 0) {
1015 struct in6_ifaddr *ia6;
1016 ip6 = mtod(m, struct ip6_hdr *);
1017 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1018 if (ia6) {
1019 /*
1020 * Record statistics for this interface
1021 * address.
1022 */
1023 ia6->ia_ifa.ifa_data.ifad_outbytes +=
1024 m->m_pkthdr.len;
1025 }
1026 #ifdef IPSEC
1027 /* clean ipsec history once it goes out of the node */
1028 ipsec_delaux(m);
1029 #endif
1030 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1031 } else
1032 m_freem(m);
1033 }
1034
1035 if (error == 0)
1036 ip6stat.ip6s_fragmented++;
1037
1038 done:
1039 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1040 RTFREE(ro->ro_rt);
1041 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1042 RTFREE(ro_pmtu->ro_rt);
1043 }
1044
1045 #ifdef IPSEC
1046 if (sp != NULL)
1047 key_freesp(sp);
1048 #endif /* IPSEC */
1049
1050 return (error);
1051
1052 freehdrs:
1053 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1054 m_freem(exthdrs.ip6e_dest1);
1055 m_freem(exthdrs.ip6e_rthdr);
1056 m_freem(exthdrs.ip6e_dest2);
1057 /* FALLTHROUGH */
1058 bad:
1059 m_freem(m);
1060 goto done;
1061 }
1062
1063 static int
1064 ip6_copyexthdr(mp, hdr, hlen)
1065 struct mbuf **mp;
1066 caddr_t hdr;
1067 int hlen;
1068 {
1069 struct mbuf *m;
1070
1071 if (hlen > MCLBYTES)
1072 return (ENOBUFS); /* XXX */
1073
1074 MGET(m, M_DONTWAIT, MT_DATA);
1075 if (!m)
1076 return (ENOBUFS);
1077
1078 if (hlen > MLEN) {
1079 MCLGET(m, M_DONTWAIT);
1080 if ((m->m_flags & M_EXT) == 0) {
1081 m_free(m);
1082 return (ENOBUFS);
1083 }
1084 }
1085 m->m_len = hlen;
1086 if (hdr)
1087 bcopy(hdr, mtod(m, caddr_t), hlen);
1088
1089 *mp = m;
1090 return (0);
1091 }
1092
1093 /*
1094 * Insert jumbo payload option.
1095 */
1096 static int
1097 ip6_insert_jumboopt(exthdrs, plen)
1098 struct ip6_exthdrs *exthdrs;
1099 u_int32_t plen;
1100 {
1101 struct mbuf *mopt;
1102 u_int8_t *optbuf;
1103 u_int32_t v;
1104
1105 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1106
1107 /*
1108 * If there is no hop-by-hop options header, allocate new one.
1109 * If there is one but it doesn't have enough space to store the
1110 * jumbo payload option, allocate a cluster to store the whole options.
1111 * Otherwise, use it to store the options.
1112 */
1113 if (exthdrs->ip6e_hbh == 0) {
1114 MGET(mopt, M_DONTWAIT, MT_DATA);
1115 if (mopt == 0)
1116 return (ENOBUFS);
1117 mopt->m_len = JUMBOOPTLEN;
1118 optbuf = mtod(mopt, u_int8_t *);
1119 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1120 exthdrs->ip6e_hbh = mopt;
1121 } else {
1122 struct ip6_hbh *hbh;
1123
1124 mopt = exthdrs->ip6e_hbh;
1125 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1126 /*
1127 * XXX assumption:
1128 * - exthdrs->ip6e_hbh is not referenced from places
1129 * other than exthdrs.
1130 * - exthdrs->ip6e_hbh is not an mbuf chain.
1131 */
1132 int oldoptlen = mopt->m_len;
1133 struct mbuf *n;
1134
1135 /*
1136 * XXX: give up if the whole (new) hbh header does
1137 * not fit even in an mbuf cluster.
1138 */
1139 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1140 return (ENOBUFS);
1141
1142 /*
1143 * As a consequence, we must always prepare a cluster
1144 * at this point.
1145 */
1146 MGET(n, M_DONTWAIT, MT_DATA);
1147 if (n) {
1148 MCLGET(n, M_DONTWAIT);
1149 if ((n->m_flags & M_EXT) == 0) {
1150 m_freem(n);
1151 n = NULL;
1152 }
1153 }
1154 if (!n)
1155 return (ENOBUFS);
1156 n->m_len = oldoptlen + JUMBOOPTLEN;
1157 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1158 oldoptlen);
1159 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1160 m_freem(mopt);
1161 mopt = exthdrs->ip6e_hbh = n;
1162 } else {
1163 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1164 mopt->m_len += JUMBOOPTLEN;
1165 }
1166 optbuf[0] = IP6OPT_PADN;
1167 optbuf[1] = 0;
1168
1169 /*
1170 * Adjust the header length according to the pad and
1171 * the jumbo payload option.
1172 */
1173 hbh = mtod(mopt, struct ip6_hbh *);
1174 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1175 }
1176
1177 /* fill in the option. */
1178 optbuf[2] = IP6OPT_JUMBO;
1179 optbuf[3] = 4;
1180 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1181 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1182
1183 /* finally, adjust the packet header length */
1184 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1185
1186 return (0);
1187 #undef JUMBOOPTLEN
1188 }
1189
1190 /*
1191 * Insert fragment header and copy unfragmentable header portions.
1192 */
1193 static int
1194 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1195 struct mbuf *m0, *m;
1196 int hlen;
1197 struct ip6_frag **frghdrp;
1198 {
1199 struct mbuf *n, *mlast;
1200
1201 if (hlen > sizeof(struct ip6_hdr)) {
1202 n = m_copym(m0, sizeof(struct ip6_hdr),
1203 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1204 if (n == 0)
1205 return (ENOBUFS);
1206 m->m_next = n;
1207 } else
1208 n = m;
1209
1210 /* Search for the last mbuf of unfragmentable part. */
1211 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1212 ;
1213
1214 if ((mlast->m_flags & M_EXT) == 0 &&
1215 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1216 /* use the trailing space of the last mbuf for the fragment hdr */
1217 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1218 mlast->m_len);
1219 mlast->m_len += sizeof(struct ip6_frag);
1220 m->m_pkthdr.len += sizeof(struct ip6_frag);
1221 } else {
1222 /* allocate a new mbuf for the fragment header */
1223 struct mbuf *mfrg;
1224
1225 MGET(mfrg, M_DONTWAIT, MT_DATA);
1226 if (mfrg == 0)
1227 return (ENOBUFS);
1228 mfrg->m_len = sizeof(struct ip6_frag);
1229 *frghdrp = mtod(mfrg, struct ip6_frag *);
1230 mlast->m_next = mfrg;
1231 }
1232
1233 return (0);
1234 }
1235
1236 int
1237 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1238 struct route_in6 *ro_pmtu, *ro;
1239 struct ifnet *ifp;
1240 struct in6_addr *dst;
1241 u_long *mtup;
1242 int *alwaysfragp;
1243 {
1244 u_int32_t mtu = 0;
1245 int alwaysfrag = 0;
1246 int error = 0;
1247
1248 if (ro_pmtu != ro) {
1249 /* The first hop and the final destination may differ. */
1250 struct sockaddr_in6 *sa6_dst =
1251 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1252 if (ro_pmtu->ro_rt &&
1253 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1254 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1255 RTFREE(ro_pmtu->ro_rt);
1256 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1257 }
1258 if (ro_pmtu->ro_rt == NULL) {
1259 bzero(sa6_dst, sizeof(*sa6_dst)); /* for safety */
1260 sa6_dst->sin6_family = AF_INET6;
1261 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1262 sa6_dst->sin6_addr = *dst;
1263
1264 rtalloc((struct route *)ro_pmtu);
1265 }
1266 }
1267 if (ro_pmtu->ro_rt) {
1268 u_int32_t ifmtu;
1269
1270 if (ifp == NULL)
1271 ifp = ro_pmtu->ro_rt->rt_ifp;
1272 ifmtu = IN6_LINKMTU(ifp);
1273 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1274 if (mtu == 0)
1275 mtu = ifmtu;
1276 else if (mtu < IPV6_MMTU) {
1277 /*
1278 * RFC2460 section 5, last paragraph:
1279 * if we record ICMPv6 too big message with
1280 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1281 * or smaller, with fragment header attached.
1282 * (fragment header is needed regardless from the
1283 * packet size, for translators to identify packets)
1284 */
1285 alwaysfrag = 1;
1286 mtu = IPV6_MMTU;
1287 } else if (mtu > ifmtu) {
1288 /*
1289 * The MTU on the route is larger than the MTU on
1290 * the interface! This shouldn't happen, unless the
1291 * MTU of the interface has been changed after the
1292 * interface was brought up. Change the MTU in the
1293 * route to match the interface MTU (as long as the
1294 * field isn't locked).
1295 */
1296 mtu = ifmtu;
1297 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
1298 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1299 }
1300 } else if (ifp) {
1301 mtu = IN6_LINKMTU(ifp);
1302 } else
1303 error = EHOSTUNREACH; /* XXX */
1304
1305 *mtup = mtu;
1306 if (alwaysfragp)
1307 *alwaysfragp = alwaysfrag;
1308 return (error);
1309 }
1310
1311 /*
1312 * IP6 socket option processing.
1313 */
1314 int
1315 ip6_ctloutput(op, so, level, optname, mp)
1316 int op;
1317 struct socket *so;
1318 int level, optname;
1319 struct mbuf **mp;
1320 {
1321 struct in6pcb *in6p = sotoin6pcb(so);
1322 struct mbuf *m = *mp;
1323 int optval = 0;
1324 int error = 0;
1325 struct proc *p = curproc; /* XXX */
1326
1327 if (level == IPPROTO_IPV6) {
1328 switch (op) {
1329 case PRCO_SETOPT:
1330 switch (optname) {
1331 case IPV6_PKTOPTIONS:
1332 /* m is freed in ip6_pcbopts */
1333 return (ip6_pcbopts(&in6p->in6p_outputopts,
1334 m, so));
1335 case IPV6_HOPOPTS:
1336 case IPV6_DSTOPTS:
1337 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1338 error = EPERM;
1339 break;
1340 }
1341 /* FALLTHROUGH */
1342 case IPV6_UNICAST_HOPS:
1343 case IPV6_RECVOPTS:
1344 case IPV6_RECVRETOPTS:
1345 case IPV6_RECVDSTADDR:
1346 case IPV6_PKTINFO:
1347 case IPV6_HOPLIMIT:
1348 case IPV6_RTHDR:
1349 case IPV6_FAITH:
1350 case IPV6_V6ONLY:
1351 if (!m || m->m_len != sizeof(int)) {
1352 error = EINVAL;
1353 break;
1354 }
1355 optval = *mtod(m, int *);
1356 switch (optname) {
1357
1358 case IPV6_UNICAST_HOPS:
1359 if (optval < -1 || optval >= 256)
1360 error = EINVAL;
1361 else {
1362 /* -1 = kernel default */
1363 in6p->in6p_hops = optval;
1364 }
1365 break;
1366 #define OPTSET(bit) \
1367 do { \
1368 if (optval) \
1369 in6p->in6p_flags |= (bit); \
1370 else \
1371 in6p->in6p_flags &= ~(bit); \
1372 } while (/*CONSTCOND*/ 0)
1373
1374 case IPV6_RECVOPTS:
1375 OPTSET(IN6P_RECVOPTS);
1376 break;
1377
1378 case IPV6_RECVRETOPTS:
1379 OPTSET(IN6P_RECVRETOPTS);
1380 break;
1381
1382 case IPV6_RECVDSTADDR:
1383 OPTSET(IN6P_RECVDSTADDR);
1384 break;
1385
1386 case IPV6_PKTINFO:
1387 OPTSET(IN6P_PKTINFO);
1388 break;
1389
1390 case IPV6_HOPLIMIT:
1391 OPTSET(IN6P_HOPLIMIT);
1392 break;
1393
1394 case IPV6_HOPOPTS:
1395 OPTSET(IN6P_HOPOPTS);
1396 break;
1397
1398 case IPV6_DSTOPTS:
1399 OPTSET(IN6P_DSTOPTS);
1400 break;
1401
1402 case IPV6_RTHDR:
1403 OPTSET(IN6P_RTHDR);
1404 break;
1405
1406 case IPV6_FAITH:
1407 OPTSET(IN6P_FAITH);
1408 break;
1409
1410 case IPV6_V6ONLY:
1411 /*
1412 * make setsockopt(IPV6_V6ONLY)
1413 * available only prior to bind(2).
1414 * see ipng mailing list, Jun 22 2001.
1415 */
1416 if (in6p->in6p_lport ||
1417 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1418 error = EINVAL;
1419 break;
1420 }
1421 #ifdef INET6_BINDV6ONLY
1422 if (!optval)
1423 error = EINVAL;
1424 #else
1425 OPTSET(IN6P_IPV6_V6ONLY);
1426 #endif
1427 break;
1428 }
1429 break;
1430 #undef OPTSET
1431
1432 case IPV6_MULTICAST_IF:
1433 case IPV6_MULTICAST_HOPS:
1434 case IPV6_MULTICAST_LOOP:
1435 case IPV6_JOIN_GROUP:
1436 case IPV6_LEAVE_GROUP:
1437 error = ip6_setmoptions(optname,
1438 &in6p->in6p_moptions, m);
1439 break;
1440
1441 case IPV6_PORTRANGE:
1442 optval = *mtod(m, int *);
1443
1444 switch (optval) {
1445 case IPV6_PORTRANGE_DEFAULT:
1446 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1447 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1448 break;
1449
1450 case IPV6_PORTRANGE_HIGH:
1451 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1452 in6p->in6p_flags |= IN6P_HIGHPORT;
1453 break;
1454
1455 case IPV6_PORTRANGE_LOW:
1456 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1457 in6p->in6p_flags |= IN6P_LOWPORT;
1458 break;
1459
1460 default:
1461 error = EINVAL;
1462 break;
1463 }
1464 break;
1465
1466 #ifdef IPSEC
1467 case IPV6_IPSEC_POLICY:
1468 {
1469 caddr_t req = NULL;
1470 size_t len = 0;
1471
1472 int priv = 0;
1473 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1474 priv = 0;
1475 else
1476 priv = 1;
1477 if (m) {
1478 req = mtod(m, caddr_t);
1479 len = m->m_len;
1480 }
1481 error = ipsec6_set_policy(in6p,
1482 optname, req, len, priv);
1483 }
1484 break;
1485 #endif /* IPSEC */
1486
1487 default:
1488 error = ENOPROTOOPT;
1489 break;
1490 }
1491 if (m)
1492 (void)m_free(m);
1493 break;
1494
1495 case PRCO_GETOPT:
1496 switch (optname) {
1497
1498 case IPV6_OPTIONS:
1499 case IPV6_RETOPTS:
1500 error = ENOPROTOOPT;
1501 break;
1502
1503 case IPV6_PKTOPTIONS:
1504 if (in6p->in6p_options) {
1505 *mp = m_copym(in6p->in6p_options, 0,
1506 M_COPYALL, M_WAIT);
1507 } else {
1508 *mp = m_get(M_WAIT, MT_SOOPTS);
1509 (*mp)->m_len = 0;
1510 }
1511 break;
1512
1513 case IPV6_HOPOPTS:
1514 case IPV6_DSTOPTS:
1515 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1516 error = EPERM;
1517 break;
1518 }
1519 /* FALLTHROUGH */
1520 case IPV6_UNICAST_HOPS:
1521 case IPV6_RECVOPTS:
1522 case IPV6_RECVRETOPTS:
1523 case IPV6_RECVDSTADDR:
1524 case IPV6_PORTRANGE:
1525 case IPV6_PKTINFO:
1526 case IPV6_HOPLIMIT:
1527 case IPV6_RTHDR:
1528 case IPV6_FAITH:
1529 case IPV6_V6ONLY:
1530 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1531 m->m_len = sizeof(int);
1532 switch (optname) {
1533
1534 case IPV6_UNICAST_HOPS:
1535 optval = in6p->in6p_hops;
1536 break;
1537
1538 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1539
1540 case IPV6_RECVOPTS:
1541 optval = OPTBIT(IN6P_RECVOPTS);
1542 break;
1543
1544 case IPV6_RECVRETOPTS:
1545 optval = OPTBIT(IN6P_RECVRETOPTS);
1546 break;
1547
1548 case IPV6_RECVDSTADDR:
1549 optval = OPTBIT(IN6P_RECVDSTADDR);
1550 break;
1551
1552 case IPV6_PORTRANGE:
1553 {
1554 int flags;
1555 flags = in6p->in6p_flags;
1556 if (flags & IN6P_HIGHPORT)
1557 optval = IPV6_PORTRANGE_HIGH;
1558 else if (flags & IN6P_LOWPORT)
1559 optval = IPV6_PORTRANGE_LOW;
1560 else
1561 optval = 0;
1562 break;
1563 }
1564
1565 case IPV6_PKTINFO:
1566 optval = OPTBIT(IN6P_PKTINFO);
1567 break;
1568
1569 case IPV6_HOPLIMIT:
1570 optval = OPTBIT(IN6P_HOPLIMIT);
1571 break;
1572
1573 case IPV6_HOPOPTS:
1574 optval = OPTBIT(IN6P_HOPOPTS);
1575 break;
1576
1577 case IPV6_DSTOPTS:
1578 optval = OPTBIT(IN6P_DSTOPTS);
1579 break;
1580
1581 case IPV6_RTHDR:
1582 optval = OPTBIT(IN6P_RTHDR);
1583 break;
1584
1585 case IPV6_FAITH:
1586 optval = OPTBIT(IN6P_FAITH);
1587 break;
1588
1589 case IPV6_V6ONLY:
1590 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1591 break;
1592 }
1593 *mtod(m, int *) = optval;
1594 break;
1595
1596 case IPV6_MULTICAST_IF:
1597 case IPV6_MULTICAST_HOPS:
1598 case IPV6_MULTICAST_LOOP:
1599 case IPV6_JOIN_GROUP:
1600 case IPV6_LEAVE_GROUP:
1601 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1602 break;
1603
1604 #ifdef IPSEC
1605 case IPV6_IPSEC_POLICY:
1606 {
1607 caddr_t req = NULL;
1608 size_t len = 0;
1609
1610 if (m) {
1611 req = mtod(m, caddr_t);
1612 len = m->m_len;
1613 }
1614 error = ipsec6_get_policy(in6p, req, len, mp);
1615 break;
1616 }
1617 #endif /* IPSEC */
1618
1619 default:
1620 error = ENOPROTOOPT;
1621 break;
1622 }
1623 break;
1624 }
1625 } else {
1626 error = EINVAL;
1627 if (op == PRCO_SETOPT && *mp)
1628 (void)m_free(*mp);
1629 }
1630 return (error);
1631 }
1632
1633 int
1634 ip6_raw_ctloutput(op, so, level, optname, mp)
1635 int op;
1636 struct socket *so;
1637 int level, optname;
1638 struct mbuf **mp;
1639 {
1640 int error = 0, optval, optlen;
1641 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1642 struct in6pcb *in6p = sotoin6pcb(so);
1643 struct mbuf *m = *mp;
1644
1645 optlen = m ? m->m_len : 0;
1646
1647 if (level != IPPROTO_IPV6) {
1648 if (op == PRCO_SETOPT && *mp)
1649 (void)m_free(*mp);
1650 return (EINVAL);
1651 }
1652
1653 switch (optname) {
1654 case IPV6_CHECKSUM:
1655 /*
1656 * For ICMPv6 sockets, no modification allowed for checksum
1657 * offset, permit "no change" values to help existing apps.
1658 *
1659 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM
1660 * for an ICMPv6 socket will fail."
1661 * The current behavior does not meet 2292bis.
1662 */
1663 switch (op) {
1664 case PRCO_SETOPT:
1665 if (optlen != sizeof(int)) {
1666 error = EINVAL;
1667 break;
1668 }
1669 optval = *mtod(m, int *);
1670 if ((optval % 2) != 0) {
1671 /* the API assumes even offset values */
1672 error = EINVAL;
1673 } else if (so->so_proto->pr_protocol ==
1674 IPPROTO_ICMPV6) {
1675 if (optval != icmp6off)
1676 error = EINVAL;
1677 } else
1678 in6p->in6p_cksum = optval;
1679 break;
1680
1681 case PRCO_GETOPT:
1682 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1683 optval = icmp6off;
1684 else
1685 optval = in6p->in6p_cksum;
1686
1687 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1688 m->m_len = sizeof(int);
1689 *mtod(m, int *) = optval;
1690 break;
1691
1692 default:
1693 error = EINVAL;
1694 break;
1695 }
1696 break;
1697
1698 default:
1699 error = ENOPROTOOPT;
1700 break;
1701 }
1702
1703 if (op == PRCO_SETOPT && m)
1704 (void)m_free(m);
1705
1706 return (error);
1707 }
1708
1709 /*
1710 * Set up IP6 options in pcb for insertion in output packets.
1711 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1712 * with destination address if source routed.
1713 */
1714 static int
1715 ip6_pcbopts(pktopt, m, so)
1716 struct ip6_pktopts **pktopt;
1717 struct mbuf *m;
1718 struct socket *so;
1719 {
1720 struct ip6_pktopts *opt = *pktopt;
1721 int error = 0;
1722 struct proc *p = curproc; /* XXX */
1723 int priv = 0;
1724
1725 /* turn off any old options. */
1726 if (opt) {
1727 if (opt->ip6po_m)
1728 (void)m_free(opt->ip6po_m);
1729 } else
1730 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1731 *pktopt = 0;
1732
1733 if (!m || m->m_len == 0) {
1734 /*
1735 * Only turning off any previous options.
1736 */
1737 free(opt, M_IP6OPT);
1738 if (m)
1739 (void)m_free(m);
1740 return (0);
1741 }
1742
1743 /* set options specified by user. */
1744 if (p && !suser(p->p_ucred, &p->p_acflag))
1745 priv = 1;
1746 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1747 (void)m_free(m);
1748 free(opt, M_IP6OPT);
1749 return (error);
1750 }
1751 *pktopt = opt;
1752 return (0);
1753 }
1754
1755 /*
1756 * Set the IP6 multicast options in response to user setsockopt().
1757 */
1758 static int
1759 ip6_setmoptions(optname, im6op, m)
1760 int optname;
1761 struct ip6_moptions **im6op;
1762 struct mbuf *m;
1763 {
1764 int error = 0;
1765 u_int loop, ifindex;
1766 struct ipv6_mreq *mreq;
1767 struct ifnet *ifp;
1768 struct ip6_moptions *im6o = *im6op;
1769 struct route_in6 ro;
1770 struct sockaddr_in6 *dst;
1771 struct in6_multi_mship *imm;
1772 struct proc *p = curproc; /* XXX */
1773
1774 if (im6o == NULL) {
1775 /*
1776 * No multicast option buffer attached to the pcb;
1777 * allocate one and initialize to default values.
1778 */
1779 im6o = (struct ip6_moptions *)
1780 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1781
1782 if (im6o == NULL)
1783 return (ENOBUFS);
1784 *im6op = im6o;
1785 im6o->im6o_multicast_ifp = NULL;
1786 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1787 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1788 LIST_INIT(&im6o->im6o_memberships);
1789 }
1790
1791 switch (optname) {
1792
1793 case IPV6_MULTICAST_IF:
1794 /*
1795 * Select the interface for outgoing multicast packets.
1796 */
1797 if (m == NULL || m->m_len != sizeof(u_int)) {
1798 error = EINVAL;
1799 break;
1800 }
1801 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1802 if (ifindex < 0 || if_indexlim <= ifindex ||
1803 !ifindex2ifnet[ifindex]) {
1804 error = ENXIO; /* XXX EINVAL? */
1805 break;
1806 }
1807 ifp = ifindex2ifnet[ifindex];
1808 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1809 error = EADDRNOTAVAIL;
1810 break;
1811 }
1812 im6o->im6o_multicast_ifp = ifp;
1813 break;
1814
1815 case IPV6_MULTICAST_HOPS:
1816 {
1817 /*
1818 * Set the IP6 hoplimit for outgoing multicast packets.
1819 */
1820 int optval;
1821 if (m == NULL || m->m_len != sizeof(int)) {
1822 error = EINVAL;
1823 break;
1824 }
1825 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1826 if (optval < -1 || optval >= 256)
1827 error = EINVAL;
1828 else if (optval == -1)
1829 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1830 else
1831 im6o->im6o_multicast_hlim = optval;
1832 break;
1833 }
1834
1835 case IPV6_MULTICAST_LOOP:
1836 /*
1837 * Set the loopback flag for outgoing multicast packets.
1838 * Must be zero or one.
1839 */
1840 if (m == NULL || m->m_len != sizeof(u_int)) {
1841 error = EINVAL;
1842 break;
1843 }
1844 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1845 if (loop > 1) {
1846 error = EINVAL;
1847 break;
1848 }
1849 im6o->im6o_multicast_loop = loop;
1850 break;
1851
1852 case IPV6_JOIN_GROUP:
1853 /*
1854 * Add a multicast group membership.
1855 * Group must be a valid IP6 multicast address.
1856 */
1857 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1858 error = EINVAL;
1859 break;
1860 }
1861 mreq = mtod(m, struct ipv6_mreq *);
1862 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1863 /*
1864 * We use the unspecified address to specify to accept
1865 * all multicast addresses. Only super user is allowed
1866 * to do this.
1867 */
1868 if (suser(p->p_ucred, &p->p_acflag))
1869 {
1870 error = EACCES;
1871 break;
1872 }
1873 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1874 error = EINVAL;
1875 break;
1876 }
1877
1878 /*
1879 * If the interface is specified, validate it.
1880 */
1881 if (mreq->ipv6mr_interface < 0 ||
1882 if_indexlim <= mreq->ipv6mr_interface ||
1883 !ifindex2ifnet[mreq->ipv6mr_interface]) {
1884 error = ENXIO; /* XXX EINVAL? */
1885 break;
1886 }
1887 /*
1888 * If no interface was explicitly specified, choose an
1889 * appropriate one according to the given multicast address.
1890 */
1891 if (mreq->ipv6mr_interface == 0) {
1892 /*
1893 * If the multicast address is in node-local scope,
1894 * the interface should be a loopback interface.
1895 * Otherwise, look up the routing table for the
1896 * address, and choose the outgoing interface.
1897 * XXX: is it a good approach?
1898 */
1899 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1900 ifp = &loif[0];
1901 } else {
1902 ro.ro_rt = NULL;
1903 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1904 bzero(dst, sizeof(*dst));
1905 dst->sin6_len = sizeof(struct sockaddr_in6);
1906 dst->sin6_family = AF_INET6;
1907 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1908 rtalloc((struct route *)&ro);
1909 if (ro.ro_rt == NULL) {
1910 error = EADDRNOTAVAIL;
1911 break;
1912 }
1913 ifp = ro.ro_rt->rt_ifp;
1914 rtfree(ro.ro_rt);
1915 }
1916 } else
1917 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1918
1919 /*
1920 * See if we found an interface, and confirm that it
1921 * supports multicast
1922 */
1923 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1924 error = EADDRNOTAVAIL;
1925 break;
1926 }
1927 /*
1928 * Put interface index into the multicast address,
1929 * if the address has link-local scope.
1930 */
1931 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1932 mreq->ipv6mr_multiaddr.s6_addr16[1] =
1933 htons(ifp->if_index);
1934 }
1935 /*
1936 * See if the membership already exists.
1937 */
1938 for (imm = im6o->im6o_memberships.lh_first;
1939 imm != NULL; imm = imm->i6mm_chain.le_next)
1940 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1941 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1942 &mreq->ipv6mr_multiaddr))
1943 break;
1944 if (imm != NULL) {
1945 error = EADDRINUSE;
1946 break;
1947 }
1948 /*
1949 * Everything looks good; add a new record to the multicast
1950 * address list for the given interface.
1951 */
1952 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
1953 if (!imm)
1954 break;
1955 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1956 break;
1957
1958 case IPV6_LEAVE_GROUP:
1959 /*
1960 * Drop a multicast group membership.
1961 * Group must be a valid IP6 multicast address.
1962 */
1963 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1964 error = EINVAL;
1965 break;
1966 }
1967 mreq = mtod(m, struct ipv6_mreq *);
1968 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1969 if (suser(p->p_ucred, &p->p_acflag))
1970 {
1971 error = EACCES;
1972 break;
1973 }
1974 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1975 error = EINVAL;
1976 break;
1977 }
1978 /*
1979 * If an interface address was specified, get a pointer
1980 * to its ifnet structure.
1981 */
1982 if (mreq->ipv6mr_interface < 0 ||
1983 if_indexlim <= mreq->ipv6mr_interface ||
1984 !ifindex2ifnet[mreq->ipv6mr_interface]) {
1985 error = ENXIO; /* XXX EINVAL? */
1986 break;
1987 }
1988 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1989 /*
1990 * Put interface index into the multicast address,
1991 * if the address has link-local scope.
1992 */
1993 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1994 mreq->ipv6mr_multiaddr.s6_addr16[1] =
1995 htons(mreq->ipv6mr_interface);
1996 }
1997 /*
1998 * Find the membership in the membership list.
1999 */
2000 for (imm = im6o->im6o_memberships.lh_first;
2001 imm != NULL; imm = imm->i6mm_chain.le_next) {
2002 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2003 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2004 &mreq->ipv6mr_multiaddr))
2005 break;
2006 }
2007 if (imm == NULL) {
2008 /* Unable to resolve interface */
2009 error = EADDRNOTAVAIL;
2010 break;
2011 }
2012 /*
2013 * Give up the multicast address record to which the
2014 * membership points.
2015 */
2016 LIST_REMOVE(imm, i6mm_chain);
2017 in6_leavegroup(imm);
2018 break;
2019
2020 default:
2021 error = EOPNOTSUPP;
2022 break;
2023 }
2024
2025 /*
2026 * If all options have default values, no need to keep the mbuf.
2027 */
2028 if (im6o->im6o_multicast_ifp == NULL &&
2029 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2030 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2031 im6o->im6o_memberships.lh_first == NULL) {
2032 free(*im6op, M_IPMOPTS);
2033 *im6op = NULL;
2034 }
2035
2036 return (error);
2037 }
2038
2039 /*
2040 * Return the IP6 multicast options in response to user getsockopt().
2041 */
2042 static int
2043 ip6_getmoptions(optname, im6o, mp)
2044 int optname;
2045 struct ip6_moptions *im6o;
2046 struct mbuf **mp;
2047 {
2048 u_int *hlim, *loop, *ifindex;
2049
2050 *mp = m_get(M_WAIT, MT_SOOPTS);
2051
2052 switch (optname) {
2053
2054 case IPV6_MULTICAST_IF:
2055 ifindex = mtod(*mp, u_int *);
2056 (*mp)->m_len = sizeof(u_int);
2057 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2058 *ifindex = 0;
2059 else
2060 *ifindex = im6o->im6o_multicast_ifp->if_index;
2061 return (0);
2062
2063 case IPV6_MULTICAST_HOPS:
2064 hlim = mtod(*mp, u_int *);
2065 (*mp)->m_len = sizeof(u_int);
2066 if (im6o == NULL)
2067 *hlim = ip6_defmcasthlim;
2068 else
2069 *hlim = im6o->im6o_multicast_hlim;
2070 return (0);
2071
2072 case IPV6_MULTICAST_LOOP:
2073 loop = mtod(*mp, u_int *);
2074 (*mp)->m_len = sizeof(u_int);
2075 if (im6o == NULL)
2076 *loop = ip6_defmcasthlim;
2077 else
2078 *loop = im6o->im6o_multicast_loop;
2079 return (0);
2080
2081 default:
2082 return (EOPNOTSUPP);
2083 }
2084 }
2085
2086 /*
2087 * Discard the IP6 multicast options.
2088 */
2089 void
2090 ip6_freemoptions(im6o)
2091 struct ip6_moptions *im6o;
2092 {
2093 struct in6_multi_mship *imm;
2094
2095 if (im6o == NULL)
2096 return;
2097
2098 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2099 LIST_REMOVE(imm, i6mm_chain);
2100 in6_leavegroup(imm);
2101 }
2102 free(im6o, M_IPMOPTS);
2103 }
2104
2105 /*
2106 * Set IPv6 outgoing packet options based on advanced API.
2107 */
2108 int
2109 ip6_setpktoptions(control, opt, priv)
2110 struct mbuf *control;
2111 struct ip6_pktopts *opt;
2112 int priv;
2113 {
2114 struct cmsghdr *cm = 0;
2115
2116 if (control == 0 || opt == 0)
2117 return (EINVAL);
2118
2119 bzero(opt, sizeof(*opt));
2120 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
2121
2122 /*
2123 * XXX: Currently, we assume all the optional information is stored
2124 * in a single mbuf.
2125 */
2126 if (control->m_next)
2127 return (EINVAL);
2128
2129 opt->ip6po_m = control;
2130
2131 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2132 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2133 cm = mtod(control, struct cmsghdr *);
2134 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2135 return (EINVAL);
2136 if (cm->cmsg_level != IPPROTO_IPV6)
2137 continue;
2138
2139 switch (cm->cmsg_type) {
2140 case IPV6_PKTINFO:
2141 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2142 return (EINVAL);
2143 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
2144 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2145 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2146 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2147 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2148
2149 if (opt->ip6po_pktinfo->ipi6_ifindex >= if_indexlim ||
2150 opt->ip6po_pktinfo->ipi6_ifindex < 0)
2151 return (ENXIO);
2152 if (opt->ip6po_pktinfo->ipi6_ifindex > 0 &&
2153 !ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex])
2154 return (ENXIO);
2155
2156 /*
2157 * Check if the requested source address is indeed a
2158 * unicast address assigned to the node, and can be
2159 * used as the packet's source address.
2160 */
2161 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2162 struct ifaddr *ia;
2163 struct in6_ifaddr *ia6;
2164 struct sockaddr_in6 sin6;
2165
2166 bzero(&sin6, sizeof(sin6));
2167 sin6.sin6_len = sizeof(sin6);
2168 sin6.sin6_family = AF_INET6;
2169 sin6.sin6_addr =
2170 opt->ip6po_pktinfo->ipi6_addr;
2171 ia = ifa_ifwithaddr(sin6tosa(&sin6));
2172 if (ia == NULL ||
2173 (opt->ip6po_pktinfo->ipi6_ifindex &&
2174 (ia->ifa_ifp->if_index !=
2175 opt->ip6po_pktinfo->ipi6_ifindex))) {
2176 return (EADDRNOTAVAIL);
2177 }
2178 ia6 = (struct in6_ifaddr *)ia;
2179 if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) {
2180 return (EADDRNOTAVAIL);
2181 }
2182
2183 /*
2184 * Check if the requested source address is
2185 * indeed a unicast address assigned to the
2186 * node.
2187 */
2188 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
2189 return (EADDRNOTAVAIL);
2190 }
2191 break;
2192
2193 case IPV6_HOPLIMIT:
2194 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2195 return (EINVAL);
2196 else {
2197 int t;
2198
2199 bcopy(CMSG_DATA(cm), &t, sizeof(t));
2200 if (t < -1 || t > 255)
2201 return (EINVAL);
2202 opt->ip6po_hlim = t;
2203 }
2204 break;
2205
2206 case IPV6_NEXTHOP:
2207 if (!priv)
2208 return (EPERM);
2209
2210 /* check if cmsg_len is large enough for sa_len */
2211 if (cm->cmsg_len < sizeof(u_char) ||
2212 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2213 return (EINVAL);
2214
2215 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2216
2217 break;
2218
2219 case IPV6_HOPOPTS:
2220 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2221 return (EINVAL);
2222 else {
2223 struct ip6_hbh *t;
2224
2225 t = (struct ip6_hbh *)CMSG_DATA(cm);
2226 if (cm->cmsg_len !=
2227 CMSG_LEN((t->ip6h_len + 1) << 3))
2228 return (EINVAL);
2229 opt->ip6po_hbh = t;
2230 }
2231 break;
2232
2233 case IPV6_DSTOPTS:
2234 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2235 return (EINVAL);
2236
2237 /*
2238 * If there is no routing header yet, the destination
2239 * options header should be put on the 1st part.
2240 * Otherwise, the header should be on the 2nd part.
2241 * (See RFC 2460, section 4.1)
2242 */
2243 if (opt->ip6po_rthdr == NULL) {
2244 struct ip6_dest *t;
2245
2246 t = (struct ip6_dest *)CMSG_DATA(cm);
2247 if (cm->cmsg_len !=
2248 CMSG_LEN((t->ip6d_len + 1) << 3));
2249 return (EINVAL);
2250 opt->ip6po_dest1 = t;
2251 }
2252 else {
2253 struct ip6_dest *t;
2254
2255 t = (struct ip6_dest *)CMSG_DATA(cm);
2256 if (cm->cmsg_len !=
2257 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1) << 3))
2258 return (EINVAL);
2259 opt->ip6po_dest2 = t;
2260 }
2261 break;
2262
2263 case IPV6_RTHDR:
2264 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2265 return (EINVAL);
2266 else {
2267 struct ip6_rthdr *t;
2268
2269 t = (struct ip6_rthdr *)CMSG_DATA(cm);
2270 if (cm->cmsg_len !=
2271 CMSG_LEN((t->ip6r_len + 1) << 3))
2272 return (EINVAL);
2273 switch (t->ip6r_type) {
2274 case IPV6_RTHDR_TYPE_0:
2275 if (t->ip6r_segleft == 0)
2276 return (EINVAL);
2277 break;
2278 default:
2279 return (EINVAL);
2280 }
2281 opt->ip6po_rthdr = t;
2282 }
2283 break;
2284
2285 default:
2286 return (ENOPROTOOPT);
2287 }
2288 }
2289
2290 return (0);
2291 }
2292
2293 /*
2294 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2295 * packet to the input queue of a specified interface. Note that this
2296 * calls the output routine of the loopback "driver", but with an interface
2297 * pointer that might NOT be &loif -- easier than replicating that code here.
2298 */
2299 void
2300 ip6_mloopback(ifp, m, dst)
2301 struct ifnet *ifp;
2302 struct mbuf *m;
2303 struct sockaddr_in6 *dst;
2304 {
2305 struct mbuf *copym;
2306 struct ip6_hdr *ip6;
2307
2308 copym = m_copy(m, 0, M_COPYALL);
2309 if (copym == NULL)
2310 return;
2311
2312 /*
2313 * Make sure to deep-copy IPv6 header portion in case the data
2314 * is in an mbuf cluster, so that we can safely override the IPv6
2315 * header portion later.
2316 */
2317 if ((copym->m_flags & M_EXT) != 0 ||
2318 copym->m_len < sizeof(struct ip6_hdr)) {
2319 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2320 if (copym == NULL)
2321 return;
2322 }
2323
2324 #ifdef DIAGNOSTIC
2325 if (copym->m_len < sizeof(*ip6)) {
2326 m_freem(copym);
2327 return;
2328 }
2329 #endif
2330
2331 ip6 = mtod(copym, struct ip6_hdr *);
2332 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
2333 ip6->ip6_src.s6_addr16[1] = 0;
2334 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
2335 ip6->ip6_dst.s6_addr16[1] = 0;
2336
2337 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2338 }
2339
2340 /*
2341 * Chop IPv6 header off from the payload.
2342 */
2343 static int
2344 ip6_splithdr(m, exthdrs)
2345 struct mbuf *m;
2346 struct ip6_exthdrs *exthdrs;
2347 {
2348 struct mbuf *mh;
2349 struct ip6_hdr *ip6;
2350
2351 ip6 = mtod(m, struct ip6_hdr *);
2352 if (m->m_len > sizeof(*ip6)) {
2353 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2354 if (mh == 0) {
2355 m_freem(m);
2356 return ENOBUFS;
2357 }
2358 M_COPY_PKTHDR(mh, m);
2359 MH_ALIGN(mh, sizeof(*ip6));
2360 m_tag_delete_chain(m, NULL);
2361 m->m_flags &= ~M_PKTHDR;
2362 m->m_len -= sizeof(*ip6);
2363 m->m_data += sizeof(*ip6);
2364 mh->m_next = m;
2365 m = mh;
2366 m->m_len = sizeof(*ip6);
2367 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2368 }
2369 exthdrs->ip6e_ip6 = m;
2370 return 0;
2371 }
2372
2373 /*
2374 * Compute IPv6 extension header length.
2375 */
2376 int
2377 ip6_optlen(in6p)
2378 struct in6pcb *in6p;
2379 {
2380 int len;
2381
2382 if (!in6p->in6p_outputopts)
2383 return 0;
2384
2385 len = 0;
2386 #define elen(x) \
2387 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2388
2389 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2390 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2391 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2392 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2393 return len;
2394 #undef elen
2395 }
2396