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