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