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