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