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