ip6_output.c revision 1.28 1 /* $NetBSD: ip6_output.c,v 1.28 2001/01/24 09:04:17 itojun Exp $ */
2 /* $KAME: ip6_output.c,v 1.122 2000/08/19 02:12:02 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/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) {
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 mtu = ifmtu;
747 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
748 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
749 }
750 } else {
751 mtu = nd_ifinfo[ifp->if_index].linkmtu;
752 }
753
754 /* Fake scoped addresses */
755 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
756 /*
757 * If source or destination address is a scoped address, and
758 * the packet is going to be sent to a loopback interface,
759 * we should keep the original interface.
760 */
761
762 /*
763 * XXX: this is a very experimental and temporary solution.
764 * We eventually have sockaddr_in6 and use the sin6_scope_id
765 * field of the structure here.
766 * We rely on the consistency between two scope zone ids
767 * of source add destination, which should already be assured
768 * Larger scopes than link will be supported in the near
769 * future.
770 */
771 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
772 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
773 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
774 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
775 else
776 origifp = ifp;
777 }
778 else
779 origifp = ifp;
780 #ifndef FAKE_LOOPBACK_IF
781 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
782 #else
783 if (1)
784 #endif
785 {
786 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
787 ip6->ip6_src.s6_addr16[1] = 0;
788 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
789 ip6->ip6_dst.s6_addr16[1] = 0;
790 }
791
792 /*
793 * If the outgoing packet contains a hop-by-hop options header,
794 * it must be examined and processed even by the source node.
795 * (RFC 2460, section 4.)
796 */
797 if (exthdrs.ip6e_hbh) {
798 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
799 struct ip6_hbh *);
800 u_int32_t dummy1; /* XXX unused */
801 u_int32_t dummy2; /* XXX unused */
802
803 /*
804 * XXX: if we have to send an ICMPv6 error to the sender,
805 * we need the M_LOOP flag since icmp6_error() expects
806 * the IPv6 and the hop-by-hop options header are
807 * continuous unless the flag is set.
808 */
809 m->m_flags |= M_LOOP;
810 m->m_pkthdr.rcvif = ifp;
811 if (ip6_process_hopopts(m,
812 (u_int8_t *)(hbh + 1),
813 ((hbh->ip6h_len + 1) << 3) -
814 sizeof(struct ip6_hbh),
815 &dummy1, &dummy2) < 0) {
816 /* m was already freed at this point */
817 error = EINVAL;/* better error? */
818 goto done;
819 }
820 m->m_flags &= ~M_LOOP; /* XXX */
821 m->m_pkthdr.rcvif = NULL;
822 }
823
824 #ifdef PFIL_HOOKS
825 /*
826 * Run through list of hooks for output packets.
827 */
828 if ((error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp,
829 PFIL_OUT)) != 0)
830 goto done;
831 if (m == NULL)
832 goto done;
833 ip6 = mtod(m, struct ip6_hdr *);
834 #endif /* PFIL_HOOKS */
835 /*
836 * Send the packet to the outgoing interface.
837 * If necessary, do IPv6 fragmentation before sending.
838 */
839 tlen = m->m_pkthdr.len;
840 if (tlen <= mtu
841 #ifdef notyet
842 /*
843 * On any link that cannot convey a 1280-octet packet in one piece,
844 * link-specific fragmentation and reassembly must be provided at
845 * a layer below IPv6. [RFC 2460, sec.5]
846 * Thus if the interface has ability of link-level fragmentation,
847 * we can just send the packet even if the packet size is
848 * larger than the link's MTU.
849 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
850 */
851
852 || ifp->if_flags & IFF_FRAGMENTABLE
853 #endif
854 )
855 {
856 #ifdef IFA_STATS
857 struct in6_ifaddr *ia6;
858 ip6 = mtod(m, struct ip6_hdr *);
859 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
860 if (ia6) {
861 ia6->ia_ifa.ifa_data.ifad_outbytes +=
862 m->m_pkthdr.len;
863 }
864 #endif
865 #ifdef IPSEC
866 /* clean ipsec history once it goes out of the node */
867 ipsec_delaux(m);
868 #endif
869 #ifdef OLDIP6OUTPUT
870 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
871 ro->ro_rt);
872 #else
873 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
874 #endif
875 goto done;
876 } else if (mtu < IPV6_MMTU) {
877 /*
878 * note that path MTU is never less than IPV6_MMTU
879 * (see icmp6_input).
880 */
881 error = EMSGSIZE;
882 in6_ifstat_inc(ifp, ifs6_out_fragfail);
883 goto bad;
884 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
885 error = EMSGSIZE;
886 in6_ifstat_inc(ifp, ifs6_out_fragfail);
887 goto bad;
888 } else {
889 struct mbuf **mnext, *m_frgpart;
890 struct ip6_frag *ip6f;
891 u_int32_t id = htonl(ip6_id++);
892 u_char nextproto;
893
894 /*
895 * Too large for the destination or interface;
896 * fragment if possible.
897 * Must be able to put at least 8 bytes per fragment.
898 */
899 hlen = unfragpartlen;
900 if (mtu > IPV6_MAXPACKET)
901 mtu = IPV6_MAXPACKET;
902 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
903 if (len < 8) {
904 error = EMSGSIZE;
905 in6_ifstat_inc(ifp, ifs6_out_fragfail);
906 goto bad;
907 }
908
909 mnext = &m->m_nextpkt;
910
911 /*
912 * Change the next header field of the last header in the
913 * unfragmentable part.
914 */
915 if (exthdrs.ip6e_rthdr) {
916 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
917 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
918 } else if (exthdrs.ip6e_dest1) {
919 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
920 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
921 } else if (exthdrs.ip6e_hbh) {
922 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
923 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
924 } else {
925 nextproto = ip6->ip6_nxt;
926 ip6->ip6_nxt = IPPROTO_FRAGMENT;
927 }
928
929 /*
930 * Loop through length of segment after first fragment,
931 * make new header and copy data of each part and link onto chain.
932 */
933 m0 = m;
934 for (off = hlen; off < tlen; off += len) {
935 MGETHDR(m, M_DONTWAIT, MT_HEADER);
936 if (!m) {
937 error = ENOBUFS;
938 ip6stat.ip6s_odropped++;
939 goto sendorfree;
940 }
941 m->m_flags = m0->m_flags & M_COPYFLAGS;
942 *mnext = m;
943 mnext = &m->m_nextpkt;
944 m->m_data += max_linkhdr;
945 mhip6 = mtod(m, struct ip6_hdr *);
946 *mhip6 = *ip6;
947 m->m_len = sizeof(*mhip6);
948 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
949 if (error) {
950 ip6stat.ip6s_odropped++;
951 goto sendorfree;
952 }
953 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
954 if (off + len >= tlen)
955 len = tlen - off;
956 else
957 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
958 mhip6->ip6_plen = htons((u_short)(len + hlen +
959 sizeof(*ip6f) -
960 sizeof(struct ip6_hdr)));
961 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
962 error = ENOBUFS;
963 ip6stat.ip6s_odropped++;
964 goto sendorfree;
965 }
966 m_cat(m, m_frgpart);
967 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
968 m->m_pkthdr.rcvif = (struct ifnet *)0;
969 ip6f->ip6f_reserved = 0;
970 ip6f->ip6f_ident = id;
971 ip6f->ip6f_nxt = nextproto;
972 ip6stat.ip6s_ofragments++;
973 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
974 }
975
976 in6_ifstat_inc(ifp, ifs6_out_fragok);
977 }
978
979 /*
980 * Remove leading garbages.
981 */
982 sendorfree:
983 m = m0->m_nextpkt;
984 m0->m_nextpkt = 0;
985 m_freem(m0);
986 for (m0 = m; m; m = m0) {
987 m0 = m->m_nextpkt;
988 m->m_nextpkt = 0;
989 if (error == 0) {
990 #ifdef IFA_STATS
991 struct in6_ifaddr *ia6;
992 ip6 = mtod(m, struct ip6_hdr *);
993 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
994 if (ia6) {
995 ia6->ia_ifa.ifa_data.ifad_outbytes +=
996 m->m_pkthdr.len;
997 }
998 #endif
999 #ifdef IPSEC
1000 /* clean ipsec history once it goes out of the node */
1001 ipsec_delaux(m);
1002 #endif
1003 #ifdef OLDIP6OUTPUT
1004 error = (*ifp->if_output)(ifp, m,
1005 (struct sockaddr *)dst,
1006 ro->ro_rt);
1007 #else
1008 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1009 #endif
1010 } else
1011 m_freem(m);
1012 }
1013
1014 if (error == 0)
1015 ip6stat.ip6s_fragmented++;
1016
1017 done:
1018 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1019 RTFREE(ro->ro_rt);
1020 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1021 RTFREE(ro_pmtu->ro_rt);
1022 }
1023
1024 #ifdef IPSEC
1025 if (sp != NULL)
1026 key_freesp(sp);
1027 #endif /* IPSEC */
1028
1029 return(error);
1030
1031 freehdrs:
1032 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1033 m_freem(exthdrs.ip6e_dest1);
1034 m_freem(exthdrs.ip6e_rthdr);
1035 m_freem(exthdrs.ip6e_dest2);
1036 /* fall through */
1037 bad:
1038 m_freem(m);
1039 goto done;
1040 }
1041
1042 static int
1043 ip6_copyexthdr(mp, hdr, hlen)
1044 struct mbuf **mp;
1045 caddr_t hdr;
1046 int hlen;
1047 {
1048 struct mbuf *m;
1049
1050 if (hlen > MCLBYTES)
1051 return(ENOBUFS); /* XXX */
1052
1053 MGET(m, M_DONTWAIT, MT_DATA);
1054 if (!m)
1055 return(ENOBUFS);
1056
1057 if (hlen > MLEN) {
1058 MCLGET(m, M_DONTWAIT);
1059 if ((m->m_flags & M_EXT) == 0) {
1060 m_free(m);
1061 return(ENOBUFS);
1062 }
1063 }
1064 m->m_len = hlen;
1065 if (hdr)
1066 bcopy(hdr, mtod(m, caddr_t), hlen);
1067
1068 *mp = m;
1069 return(0);
1070 }
1071
1072 /*
1073 * Insert jumbo payload option.
1074 */
1075 static int
1076 ip6_insert_jumboopt(exthdrs, plen)
1077 struct ip6_exthdrs *exthdrs;
1078 u_int32_t plen;
1079 {
1080 struct mbuf *mopt;
1081 u_char *optbuf;
1082 u_int32_t v;
1083
1084 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1085
1086 /*
1087 * If there is no hop-by-hop options header, allocate new one.
1088 * If there is one but it doesn't have enough space to store the
1089 * jumbo payload option, allocate a cluster to store the whole options.
1090 * Otherwise, use it to store the options.
1091 */
1092 if (exthdrs->ip6e_hbh == 0) {
1093 MGET(mopt, M_DONTWAIT, MT_DATA);
1094 if (mopt == 0)
1095 return(ENOBUFS);
1096 mopt->m_len = JUMBOOPTLEN;
1097 optbuf = mtod(mopt, u_char *);
1098 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1099 exthdrs->ip6e_hbh = mopt;
1100 } else {
1101 struct ip6_hbh *hbh;
1102
1103 mopt = exthdrs->ip6e_hbh;
1104 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1105 /*
1106 * XXX assumption:
1107 * - exthdrs->ip6e_hbh is not referenced from places
1108 * other than exthdrs.
1109 * - exthdrs->ip6e_hbh is not an mbuf chain.
1110 */
1111 int oldoptlen = mopt->m_len;
1112 struct mbuf *n;
1113
1114 /*
1115 * XXX: give up if the whole (new) hbh header does
1116 * not fit even in an mbuf cluster.
1117 */
1118 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1119 return(ENOBUFS);
1120
1121 /*
1122 * As a consequence, we must always prepare a cluster
1123 * at this point.
1124 */
1125 MGET(n, M_DONTWAIT, MT_DATA);
1126 if (n) {
1127 MCLGET(n, M_DONTWAIT);
1128 if ((n->m_flags & M_EXT) == 0) {
1129 m_freem(n);
1130 n = NULL;
1131 }
1132 }
1133 if (!n)
1134 return(ENOBUFS);
1135 n->m_len = oldoptlen + JUMBOOPTLEN;
1136 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1137 oldoptlen);
1138 optbuf = mtod(n, caddr_t) + oldoptlen;
1139 m_freem(mopt);
1140 exthdrs->ip6e_hbh = n;
1141 } else {
1142 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1143 mopt->m_len += JUMBOOPTLEN;
1144 }
1145 optbuf[0] = IP6OPT_PADN;
1146 optbuf[1] = 1;
1147
1148 /*
1149 * Adjust the header length according to the pad and
1150 * the jumbo payload option.
1151 */
1152 hbh = mtod(mopt, struct ip6_hbh *);
1153 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1154 }
1155
1156 /* fill in the option. */
1157 optbuf[2] = IP6OPT_JUMBO;
1158 optbuf[3] = 4;
1159 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1160 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1161
1162 /* finally, adjust the packet header length */
1163 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1164
1165 return(0);
1166 #undef JUMBOOPTLEN
1167 }
1168
1169 /*
1170 * Insert fragment header and copy unfragmentable header portions.
1171 */
1172 static int
1173 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1174 struct mbuf *m0, *m;
1175 int hlen;
1176 struct ip6_frag **frghdrp;
1177 {
1178 struct mbuf *n, *mlast;
1179
1180 if (hlen > sizeof(struct ip6_hdr)) {
1181 n = m_copym(m0, sizeof(struct ip6_hdr),
1182 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1183 if (n == 0)
1184 return(ENOBUFS);
1185 m->m_next = n;
1186 } else
1187 n = m;
1188
1189 /* Search for the last mbuf of unfragmentable part. */
1190 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1191 ;
1192
1193 if ((mlast->m_flags & M_EXT) == 0 &&
1194 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1195 /* use the trailing space of the last mbuf for the fragment hdr */
1196 *frghdrp =
1197 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1198 mlast->m_len += sizeof(struct ip6_frag);
1199 m->m_pkthdr.len += sizeof(struct ip6_frag);
1200 } else {
1201 /* allocate a new mbuf for the fragment header */
1202 struct mbuf *mfrg;
1203
1204 MGET(mfrg, M_DONTWAIT, MT_DATA);
1205 if (mfrg == 0)
1206 return(ENOBUFS);
1207 mfrg->m_len = sizeof(struct ip6_frag);
1208 *frghdrp = mtod(mfrg, struct ip6_frag *);
1209 mlast->m_next = mfrg;
1210 }
1211
1212 return(0);
1213 }
1214
1215 /*
1216 * IP6 socket option processing.
1217 */
1218 int
1219 ip6_ctloutput(op, so, level, optname, mp)
1220 int op;
1221 struct socket *so;
1222 int level, optname;
1223 struct mbuf **mp;
1224 {
1225 register struct in6pcb *in6p = sotoin6pcb(so);
1226 register struct mbuf *m = *mp;
1227 register int optval = 0;
1228 int error = 0;
1229 struct proc *p = curproc; /* XXX */
1230
1231 if (level == IPPROTO_IPV6) {
1232 switch (op) {
1233
1234 case PRCO_SETOPT:
1235 switch (optname) {
1236 case IPV6_PKTOPTIONS:
1237 /* m is freed in ip6_pcbopts */
1238 return(ip6_pcbopts(&in6p->in6p_outputopts,
1239 m, so));
1240 case IPV6_HOPOPTS:
1241 case IPV6_DSTOPTS:
1242 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1243 error = EPERM;
1244 break;
1245 }
1246 /* fall through */
1247 case IPV6_UNICAST_HOPS:
1248 case IPV6_RECVOPTS:
1249 case IPV6_RECVRETOPTS:
1250 case IPV6_RECVDSTADDR:
1251 case IPV6_PKTINFO:
1252 case IPV6_HOPLIMIT:
1253 case IPV6_RTHDR:
1254 case IPV6_CHECKSUM:
1255 case IPV6_FAITH:
1256 #ifndef INET6_BINDV6ONLY
1257 case IPV6_BINDV6ONLY:
1258 #endif
1259 if (!m || m->m_len != sizeof(int))
1260 error = EINVAL;
1261 else {
1262 optval = *mtod(m, int *);
1263 switch (optname) {
1264
1265 case IPV6_UNICAST_HOPS:
1266 if (optval < -1 || optval >= 256)
1267 error = EINVAL;
1268 else {
1269 /* -1 = kernel default */
1270 in6p->in6p_hops = optval;
1271 }
1272 break;
1273 #define OPTSET(bit) \
1274 if (optval) \
1275 in6p->in6p_flags |= bit; \
1276 else \
1277 in6p->in6p_flags &= ~bit;
1278
1279 case IPV6_RECVOPTS:
1280 OPTSET(IN6P_RECVOPTS);
1281 break;
1282
1283 case IPV6_RECVRETOPTS:
1284 OPTSET(IN6P_RECVRETOPTS);
1285 break;
1286
1287 case IPV6_RECVDSTADDR:
1288 OPTSET(IN6P_RECVDSTADDR);
1289 break;
1290
1291 case IPV6_PKTINFO:
1292 OPTSET(IN6P_PKTINFO);
1293 break;
1294
1295 case IPV6_HOPLIMIT:
1296 OPTSET(IN6P_HOPLIMIT);
1297 break;
1298
1299 case IPV6_HOPOPTS:
1300 OPTSET(IN6P_HOPOPTS);
1301 break;
1302
1303 case IPV6_DSTOPTS:
1304 OPTSET(IN6P_DSTOPTS);
1305 break;
1306
1307 case IPV6_RTHDR:
1308 OPTSET(IN6P_RTHDR);
1309 break;
1310
1311 case IPV6_CHECKSUM:
1312 in6p->in6p_cksum = optval;
1313 break;
1314
1315 case IPV6_FAITH:
1316 OPTSET(IN6P_FAITH);
1317 break;
1318
1319 #ifndef INET6_BINDV6ONLY
1320 case IPV6_BINDV6ONLY:
1321 OPTSET(IN6P_BINDV6ONLY);
1322 break;
1323 #endif
1324 }
1325 }
1326 break;
1327 #undef OPTSET
1328
1329 case IPV6_MULTICAST_IF:
1330 case IPV6_MULTICAST_HOPS:
1331 case IPV6_MULTICAST_LOOP:
1332 case IPV6_JOIN_GROUP:
1333 case IPV6_LEAVE_GROUP:
1334 error = ip6_setmoptions(optname, &in6p->in6p_moptions, m);
1335 break;
1336
1337 case IPV6_PORTRANGE:
1338 optval = *mtod(m, int *);
1339
1340 switch (optval) {
1341 case IPV6_PORTRANGE_DEFAULT:
1342 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1343 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1344 break;
1345
1346 case IPV6_PORTRANGE_HIGH:
1347 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1348 in6p->in6p_flags |= IN6P_HIGHPORT;
1349 break;
1350
1351 case IPV6_PORTRANGE_LOW:
1352 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1353 in6p->in6p_flags |= IN6P_LOWPORT;
1354 break;
1355
1356 default:
1357 error = EINVAL;
1358 break;
1359 }
1360 break;
1361
1362 #ifdef IPSEC
1363 case IPV6_IPSEC_POLICY:
1364 {
1365 caddr_t req = NULL;
1366 size_t len = 0;
1367
1368 int priv = 0;
1369 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1370 priv = 0;
1371 else
1372 priv = 1;
1373 if (m) {
1374 req = mtod(m, caddr_t);
1375 len = m->m_len;
1376 }
1377 error = ipsec6_set_policy(in6p,
1378 optname, req, len, priv);
1379 }
1380 break;
1381 #endif /* IPSEC */
1382
1383 default:
1384 error = ENOPROTOOPT;
1385 break;
1386 }
1387 if (m)
1388 (void)m_free(m);
1389 break;
1390
1391 case PRCO_GETOPT:
1392 switch (optname) {
1393
1394 case IPV6_OPTIONS:
1395 case IPV6_RETOPTS:
1396 #if 0
1397 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1398 if (in6p->in6p_options) {
1399 m->m_len = in6p->in6p_options->m_len;
1400 bcopy(mtod(in6p->in6p_options, caddr_t),
1401 mtod(m, caddr_t),
1402 (unsigned)m->m_len);
1403 } else
1404 m->m_len = 0;
1405 break;
1406 #else
1407 error = ENOPROTOOPT;
1408 break;
1409 #endif
1410
1411 case IPV6_PKTOPTIONS:
1412 if (in6p->in6p_options) {
1413 *mp = m_copym(in6p->in6p_options, 0,
1414 M_COPYALL, M_WAIT);
1415 } else {
1416 *mp = m_get(M_WAIT, MT_SOOPTS);
1417 (*mp)->m_len = 0;
1418 }
1419 break;
1420
1421 case IPV6_HOPOPTS:
1422 case IPV6_DSTOPTS:
1423 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1424 error = EPERM;
1425 break;
1426 }
1427 /* fall through */
1428 case IPV6_UNICAST_HOPS:
1429 case IPV6_RECVOPTS:
1430 case IPV6_RECVRETOPTS:
1431 case IPV6_RECVDSTADDR:
1432 case IPV6_PORTRANGE:
1433 case IPV6_PKTINFO:
1434 case IPV6_HOPLIMIT:
1435 case IPV6_RTHDR:
1436 case IPV6_CHECKSUM:
1437 case IPV6_FAITH:
1438 #ifndef INET6_BINDV6ONLY
1439 case IPV6_BINDV6ONLY:
1440 #endif
1441 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1442 m->m_len = sizeof(int);
1443 switch (optname) {
1444
1445 case IPV6_UNICAST_HOPS:
1446 optval = in6p->in6p_hops;
1447 break;
1448
1449 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1450
1451 case IPV6_RECVOPTS:
1452 optval = OPTBIT(IN6P_RECVOPTS);
1453 break;
1454
1455 case IPV6_RECVRETOPTS:
1456 optval = OPTBIT(IN6P_RECVRETOPTS);
1457 break;
1458
1459 case IPV6_RECVDSTADDR:
1460 optval = OPTBIT(IN6P_RECVDSTADDR);
1461 break;
1462
1463 case IPV6_PORTRANGE:
1464 {
1465 int flags;
1466 flags = in6p->in6p_flags;
1467 if (flags & IN6P_HIGHPORT)
1468 optval = IPV6_PORTRANGE_HIGH;
1469 else if (flags & IN6P_LOWPORT)
1470 optval = IPV6_PORTRANGE_LOW;
1471 else
1472 optval = 0;
1473 break;
1474 }
1475
1476 case IPV6_PKTINFO:
1477 optval = OPTBIT(IN6P_PKTINFO);
1478 break;
1479
1480 case IPV6_HOPLIMIT:
1481 optval = OPTBIT(IN6P_HOPLIMIT);
1482 break;
1483
1484 case IPV6_HOPOPTS:
1485 optval = OPTBIT(IN6P_HOPOPTS);
1486 break;
1487
1488 case IPV6_DSTOPTS:
1489 optval = OPTBIT(IN6P_DSTOPTS);
1490 break;
1491
1492 case IPV6_RTHDR:
1493 optval = OPTBIT(IN6P_RTHDR);
1494 break;
1495
1496 case IPV6_CHECKSUM:
1497 optval = in6p->in6p_cksum;
1498 break;
1499
1500 case IPV6_FAITH:
1501 optval = OPTBIT(IN6P_FAITH);
1502 break;
1503
1504 #ifndef INET6_BINDV6ONLY
1505 case IPV6_BINDV6ONLY:
1506 optval = OPTBIT(IN6P_BINDV6ONLY);
1507 break;
1508 #endif
1509 }
1510 *mtod(m, int *) = optval;
1511 break;
1512
1513 case IPV6_MULTICAST_IF:
1514 case IPV6_MULTICAST_HOPS:
1515 case IPV6_MULTICAST_LOOP:
1516 case IPV6_JOIN_GROUP:
1517 case IPV6_LEAVE_GROUP:
1518 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1519 break;
1520
1521 #ifdef IPSEC
1522 case IPV6_IPSEC_POLICY:
1523 {
1524 caddr_t req = NULL;
1525 size_t len = 0;
1526
1527 if (m) {
1528 req = mtod(m, caddr_t);
1529 len = m->m_len;
1530 }
1531 error = ipsec6_get_policy(in6p, req, len, mp);
1532 break;
1533 }
1534 #endif /* IPSEC */
1535
1536 default:
1537 error = ENOPROTOOPT;
1538 break;
1539 }
1540 break;
1541 }
1542 } else {
1543 error = EINVAL;
1544 if (op == PRCO_SETOPT && *mp)
1545 (void)m_free(*mp);
1546 }
1547 return(error);
1548 }
1549
1550 /*
1551 * Set up IP6 options in pcb for insertion in output packets.
1552 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1553 * with destination address if source routed.
1554 */
1555 static int
1556 ip6_pcbopts(pktopt, m, so)
1557 struct ip6_pktopts **pktopt;
1558 register struct mbuf *m;
1559 struct socket *so;
1560 {
1561 register struct ip6_pktopts *opt = *pktopt;
1562 int error = 0;
1563 struct proc *p = curproc; /* XXX */
1564 int priv = 0;
1565
1566 /* turn off any old options. */
1567 if (opt) {
1568 if (opt->ip6po_m)
1569 (void)m_free(opt->ip6po_m);
1570 } else
1571 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1572 *pktopt = 0;
1573
1574 if (!m || m->m_len == 0) {
1575 /*
1576 * Only turning off any previous options.
1577 */
1578 if (opt)
1579 free(opt, M_IP6OPT);
1580 if (m)
1581 (void)m_free(m);
1582 return(0);
1583 }
1584
1585 /* set options specified by user. */
1586 if (p && !suser(p->p_ucred, &p->p_acflag))
1587 priv = 1;
1588 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1589 (void)m_free(m);
1590 return(error);
1591 }
1592 *pktopt = opt;
1593 return(0);
1594 }
1595
1596 /*
1597 * Set the IP6 multicast options in response to user setsockopt().
1598 */
1599 static int
1600 ip6_setmoptions(optname, im6op, m)
1601 int optname;
1602 struct ip6_moptions **im6op;
1603 struct mbuf *m;
1604 {
1605 int error = 0;
1606 u_int loop, ifindex;
1607 struct ipv6_mreq *mreq;
1608 struct ifnet *ifp;
1609 struct ip6_moptions *im6o = *im6op;
1610 struct route_in6 ro;
1611 struct sockaddr_in6 *dst;
1612 struct in6_multi_mship *imm;
1613 struct proc *p = curproc; /* XXX */
1614
1615 if (im6o == NULL) {
1616 /*
1617 * No multicast option buffer attached to the pcb;
1618 * allocate one and initialize to default values.
1619 */
1620 im6o = (struct ip6_moptions *)
1621 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1622
1623 if (im6o == NULL)
1624 return(ENOBUFS);
1625 *im6op = im6o;
1626 im6o->im6o_multicast_ifp = NULL;
1627 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1628 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1629 LIST_INIT(&im6o->im6o_memberships);
1630 }
1631
1632 switch (optname) {
1633
1634 case IPV6_MULTICAST_IF:
1635 /*
1636 * Select the interface for outgoing multicast packets.
1637 */
1638 if (m == NULL || m->m_len != sizeof(u_int)) {
1639 error = EINVAL;
1640 break;
1641 }
1642 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1643 if (ifindex < 0 || if_index < ifindex) {
1644 error = ENXIO; /* XXX EINVAL? */
1645 break;
1646 }
1647 ifp = ifindex2ifnet[ifindex];
1648 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1649 error = EADDRNOTAVAIL;
1650 break;
1651 }
1652 im6o->im6o_multicast_ifp = ifp;
1653 break;
1654
1655 case IPV6_MULTICAST_HOPS:
1656 {
1657 /*
1658 * Set the IP6 hoplimit for outgoing multicast packets.
1659 */
1660 int optval;
1661 if (m == NULL || m->m_len != sizeof(int)) {
1662 error = EINVAL;
1663 break;
1664 }
1665 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1666 if (optval < -1 || optval >= 256)
1667 error = EINVAL;
1668 else if (optval == -1)
1669 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1670 else
1671 im6o->im6o_multicast_hlim = optval;
1672 break;
1673 }
1674
1675 case IPV6_MULTICAST_LOOP:
1676 /*
1677 * Set the loopback flag for outgoing multicast packets.
1678 * Must be zero or one.
1679 */
1680 if (m == NULL || m->m_len != sizeof(u_int)) {
1681 error = EINVAL;
1682 break;
1683 }
1684 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1685 if (loop > 1) {
1686 error = EINVAL;
1687 break;
1688 }
1689 im6o->im6o_multicast_loop = loop;
1690 break;
1691
1692 case IPV6_JOIN_GROUP:
1693 /*
1694 * Add a multicast group membership.
1695 * Group must be a valid IP6 multicast address.
1696 */
1697 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1698 error = EINVAL;
1699 break;
1700 }
1701 mreq = mtod(m, struct ipv6_mreq *);
1702 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1703 /*
1704 * We use the unspecified address to specify to accept
1705 * all multicast addresses. Only super user is allowed
1706 * to do this.
1707 */
1708 if (suser(p->p_ucred, &p->p_acflag)) {
1709 error = EACCES;
1710 break;
1711 }
1712 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1713 error = EINVAL;
1714 break;
1715 }
1716
1717 /*
1718 * If the interface is specified, validate it.
1719 */
1720 if (mreq->ipv6mr_interface < 0
1721 || if_index < mreq->ipv6mr_interface) {
1722 error = ENXIO; /* XXX EINVAL? */
1723 break;
1724 }
1725 /*
1726 * If no interface was explicitly specified, choose an
1727 * appropriate one according to the given multicast address.
1728 */
1729 if (mreq->ipv6mr_interface == 0) {
1730 /*
1731 * If the multicast address is in node-local scope,
1732 * the interface should be a loopback interface.
1733 * Otherwise, look up the routing table for the
1734 * address, and choose the outgoing interface.
1735 * XXX: is it a good approach?
1736 */
1737 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1738 ifp = &loif[0];
1739 } else {
1740 ro.ro_rt = NULL;
1741 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1742 bzero(dst, sizeof(*dst));
1743 dst->sin6_len = sizeof(struct sockaddr_in6);
1744 dst->sin6_family = AF_INET6;
1745 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1746 rtalloc((struct route *)&ro);
1747 if (ro.ro_rt == NULL) {
1748 error = EADDRNOTAVAIL;
1749 break;
1750 }
1751 ifp = ro.ro_rt->rt_ifp;
1752 rtfree(ro.ro_rt);
1753 }
1754 } else
1755 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1756
1757 /*
1758 * See if we found an interface, and confirm that it
1759 * supports multicast
1760 */
1761 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1762 error = EADDRNOTAVAIL;
1763 break;
1764 }
1765 /*
1766 * Put interface index into the multicast address,
1767 * if the address has link-local scope.
1768 */
1769 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1770 mreq->ipv6mr_multiaddr.s6_addr16[1]
1771 = htons(mreq->ipv6mr_interface);
1772 }
1773 /*
1774 * See if the membership already exists.
1775 */
1776 for (imm = im6o->im6o_memberships.lh_first;
1777 imm != NULL; imm = imm->i6mm_chain.le_next)
1778 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1779 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1780 &mreq->ipv6mr_multiaddr))
1781 break;
1782 if (imm != NULL) {
1783 error = EADDRINUSE;
1784 break;
1785 }
1786 /*
1787 * Everything looks good; add a new record to the multicast
1788 * address list for the given interface.
1789 */
1790 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
1791 if (imm == NULL) {
1792 error = ENOBUFS;
1793 break;
1794 }
1795 if ((imm->i6mm_maddr =
1796 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
1797 free(imm, M_IPMADDR);
1798 break;
1799 }
1800 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1801 break;
1802
1803 case IPV6_LEAVE_GROUP:
1804 /*
1805 * Drop a multicast group membership.
1806 * Group must be a valid IP6 multicast address.
1807 */
1808 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1809 error = EINVAL;
1810 break;
1811 }
1812 mreq = mtod(m, struct ipv6_mreq *);
1813 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1814 if (suser(p->p_ucred, &p->p_acflag)) {
1815 error = EACCES;
1816 break;
1817 }
1818 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1819 error = EINVAL;
1820 break;
1821 }
1822 /*
1823 * If an interface address was specified, get a pointer
1824 * to its ifnet structure.
1825 */
1826 if (mreq->ipv6mr_interface < 0
1827 || if_index < mreq->ipv6mr_interface) {
1828 error = ENXIO; /* XXX EINVAL? */
1829 break;
1830 }
1831 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1832 /*
1833 * Put interface index into the multicast address,
1834 * if the address has link-local scope.
1835 */
1836 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1837 mreq->ipv6mr_multiaddr.s6_addr16[1]
1838 = htons(mreq->ipv6mr_interface);
1839 }
1840 /*
1841 * Find the membership in the membership list.
1842 */
1843 for (imm = im6o->im6o_memberships.lh_first;
1844 imm != NULL; imm = imm->i6mm_chain.le_next) {
1845 if ((ifp == NULL ||
1846 imm->i6mm_maddr->in6m_ifp == ifp) &&
1847 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1848 &mreq->ipv6mr_multiaddr))
1849 break;
1850 }
1851 if (imm == NULL) {
1852 /* Unable to resolve interface */
1853 error = EADDRNOTAVAIL;
1854 break;
1855 }
1856 /*
1857 * Give up the multicast address record to which the
1858 * membership points.
1859 */
1860 LIST_REMOVE(imm, i6mm_chain);
1861 in6_delmulti(imm->i6mm_maddr);
1862 free(imm, M_IPMADDR);
1863 break;
1864
1865 default:
1866 error = EOPNOTSUPP;
1867 break;
1868 }
1869
1870 /*
1871 * If all options have default values, no need to keep the mbuf.
1872 */
1873 if (im6o->im6o_multicast_ifp == NULL &&
1874 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1875 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1876 im6o->im6o_memberships.lh_first == NULL) {
1877 free(*im6op, M_IPMOPTS);
1878 *im6op = NULL;
1879 }
1880
1881 return(error);
1882 }
1883
1884 /*
1885 * Return the IP6 multicast options in response to user getsockopt().
1886 */
1887 static int
1888 ip6_getmoptions(optname, im6o, mp)
1889 int optname;
1890 register struct ip6_moptions *im6o;
1891 register struct mbuf **mp;
1892 {
1893 u_int *hlim, *loop, *ifindex;
1894
1895 *mp = m_get(M_WAIT, MT_SOOPTS);
1896
1897 switch (optname) {
1898
1899 case IPV6_MULTICAST_IF:
1900 ifindex = mtod(*mp, u_int *);
1901 (*mp)->m_len = sizeof(u_int);
1902 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1903 *ifindex = 0;
1904 else
1905 *ifindex = im6o->im6o_multicast_ifp->if_index;
1906 return(0);
1907
1908 case IPV6_MULTICAST_HOPS:
1909 hlim = mtod(*mp, u_int *);
1910 (*mp)->m_len = sizeof(u_int);
1911 if (im6o == NULL)
1912 *hlim = ip6_defmcasthlim;
1913 else
1914 *hlim = im6o->im6o_multicast_hlim;
1915 return(0);
1916
1917 case IPV6_MULTICAST_LOOP:
1918 loop = mtod(*mp, u_int *);
1919 (*mp)->m_len = sizeof(u_int);
1920 if (im6o == NULL)
1921 *loop = ip6_defmcasthlim;
1922 else
1923 *loop = im6o->im6o_multicast_loop;
1924 return(0);
1925
1926 default:
1927 return(EOPNOTSUPP);
1928 }
1929 }
1930
1931 /*
1932 * Discard the IP6 multicast options.
1933 */
1934 void
1935 ip6_freemoptions(im6o)
1936 register struct ip6_moptions *im6o;
1937 {
1938 struct in6_multi_mship *imm;
1939
1940 if (im6o == NULL)
1941 return;
1942
1943 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
1944 LIST_REMOVE(imm, i6mm_chain);
1945 if (imm->i6mm_maddr)
1946 in6_delmulti(imm->i6mm_maddr);
1947 free(imm, M_IPMADDR);
1948 }
1949 free(im6o, M_IPMOPTS);
1950 }
1951
1952 /*
1953 * Set IPv6 outgoing packet options based on advanced API.
1954 */
1955 int
1956 ip6_setpktoptions(control, opt, priv)
1957 struct mbuf *control;
1958 struct ip6_pktopts *opt;
1959 int priv;
1960 {
1961 register struct cmsghdr *cm = 0;
1962
1963 if (control == 0 || opt == 0)
1964 return(EINVAL);
1965
1966 bzero(opt, sizeof(*opt));
1967 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
1968
1969 /*
1970 * XXX: Currently, we assume all the optional information is stored
1971 * in a single mbuf.
1972 */
1973 if (control->m_next)
1974 return(EINVAL);
1975
1976 opt->ip6po_m = control;
1977
1978 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
1979 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1980 cm = mtod(control, struct cmsghdr *);
1981 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
1982 return(EINVAL);
1983 if (cm->cmsg_level != IPPROTO_IPV6)
1984 continue;
1985
1986 switch(cm->cmsg_type) {
1987 case IPV6_PKTINFO:
1988 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
1989 return(EINVAL);
1990 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1991 if (opt->ip6po_pktinfo->ipi6_ifindex &&
1992 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
1993 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
1994 htons(opt->ip6po_pktinfo->ipi6_ifindex);
1995
1996 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
1997 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
1998 return(ENXIO);
1999 }
2000
2001 /*
2002 * Check if the requested source address is indeed a
2003 * unicast address assigned to the node.
2004 */
2005 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2006 struct ifaddr *ia;
2007 struct sockaddr_in6 sin6;
2008
2009 bzero(&sin6, sizeof(sin6));
2010 sin6.sin6_len = sizeof(sin6);
2011 sin6.sin6_family = AF_INET6;
2012 sin6.sin6_addr =
2013 opt->ip6po_pktinfo->ipi6_addr;
2014 ia = ifa_ifwithaddr(sin6tosa(&sin6));
2015 if (ia == NULL ||
2016 (opt->ip6po_pktinfo->ipi6_ifindex &&
2017 (ia->ifa_ifp->if_index !=
2018 opt->ip6po_pktinfo->ipi6_ifindex))) {
2019 return(EADDRNOTAVAIL);
2020 }
2021 /*
2022 * Check if the requested source address is
2023 * indeed a unicast address assigned to the
2024 * node.
2025 */
2026 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
2027 return(EADDRNOTAVAIL);
2028 }
2029 break;
2030
2031 case IPV6_HOPLIMIT:
2032 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2033 return(EINVAL);
2034
2035 bcopy(CMSG_DATA(cm), &opt->ip6po_hlim,
2036 sizeof(opt->ip6po_hlim));
2037 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2038 return(EINVAL);
2039 break;
2040
2041 case IPV6_NEXTHOP:
2042 if (!priv)
2043 return(EPERM);
2044
2045 if (cm->cmsg_len < sizeof(u_char) ||
2046 /* check if cmsg_len is large enough for sa_len */
2047 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2048 return(EINVAL);
2049
2050 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2051
2052 break;
2053
2054 case IPV6_HOPOPTS:
2055 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2056 return(EINVAL);
2057 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2058 if (cm->cmsg_len !=
2059 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
2060 return(EINVAL);
2061 break;
2062
2063 case IPV6_DSTOPTS:
2064 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2065 return(EINVAL);
2066
2067 /*
2068 * If there is no routing header yet, the destination
2069 * options header should be put on the 1st part.
2070 * Otherwise, the header should be on the 2nd part.
2071 * (See RFC 2460, section 4.1)
2072 */
2073 if (opt->ip6po_rthdr == NULL) {
2074 opt->ip6po_dest1 =
2075 (struct ip6_dest *)CMSG_DATA(cm);
2076 if (cm->cmsg_len !=
2077 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
2078 << 3))
2079 return(EINVAL);
2080 }
2081 else {
2082 opt->ip6po_dest2 =
2083 (struct ip6_dest *)CMSG_DATA(cm);
2084 if (cm->cmsg_len !=
2085 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
2086 << 3))
2087 return(EINVAL);
2088 }
2089 break;
2090
2091 case IPV6_RTHDR:
2092 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2093 return(EINVAL);
2094 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
2095 if (cm->cmsg_len !=
2096 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
2097 return(EINVAL);
2098 switch(opt->ip6po_rthdr->ip6r_type) {
2099 case IPV6_RTHDR_TYPE_0:
2100 if (opt->ip6po_rthdr->ip6r_segleft == 0)
2101 return(EINVAL);
2102 break;
2103 default:
2104 return(EINVAL);
2105 }
2106 break;
2107
2108 default:
2109 return(ENOPROTOOPT);
2110 }
2111 }
2112
2113 return(0);
2114 }
2115
2116 /*
2117 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2118 * packet to the input queue of a specified interface. Note that this
2119 * calls the output routine of the loopback "driver", but with an interface
2120 * pointer that might NOT be &loif -- easier than replicating that code here.
2121 */
2122 void
2123 ip6_mloopback(ifp, m, dst)
2124 struct ifnet *ifp;
2125 register struct mbuf *m;
2126 register struct sockaddr_in6 *dst;
2127 {
2128 struct mbuf *copym;
2129 struct ip6_hdr *ip6;
2130
2131 copym = m_copy(m, 0, M_COPYALL);
2132 if (copym == NULL)
2133 return;
2134
2135 /*
2136 * Make sure to deep-copy IPv6 header portion in case the data
2137 * is in an mbuf cluster, so that we can safely override the IPv6
2138 * header portion later.
2139 */
2140 if ((copym->m_flags & M_EXT) != 0 ||
2141 copym->m_len < sizeof(struct ip6_hdr)) {
2142 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2143 if (copym == NULL)
2144 return;
2145 }
2146
2147 #ifdef DIAGNOSTIC
2148 if (copym->m_len < sizeof(*ip6)) {
2149 m_freem(copym);
2150 return;
2151 }
2152 #endif
2153
2154 #ifndef FAKE_LOOPBACK_IF
2155 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2156 #else
2157 if (1)
2158 #endif
2159 {
2160 ip6 = mtod(copym, struct ip6_hdr *);
2161 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
2162 ip6->ip6_src.s6_addr16[1] = 0;
2163 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
2164 ip6->ip6_dst.s6_addr16[1] = 0;
2165 }
2166
2167 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2168 }
2169
2170 /*
2171 * Chop IPv6 header off from the payload.
2172 */
2173 static int
2174 ip6_splithdr(m, exthdrs)
2175 struct mbuf *m;
2176 struct ip6_exthdrs *exthdrs;
2177 {
2178 struct mbuf *mh;
2179 struct ip6_hdr *ip6;
2180
2181 ip6 = mtod(m, struct ip6_hdr *);
2182 if (m->m_len > sizeof(*ip6)) {
2183 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2184 if (mh == 0) {
2185 m_freem(m);
2186 return ENOBUFS;
2187 }
2188 M_COPY_PKTHDR(mh, m);
2189 MH_ALIGN(mh, sizeof(*ip6));
2190 m->m_flags &= ~M_PKTHDR;
2191 m->m_len -= sizeof(*ip6);
2192 m->m_data += sizeof(*ip6);
2193 mh->m_next = m;
2194 m = mh;
2195 m->m_len = sizeof(*ip6);
2196 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2197 }
2198 exthdrs->ip6e_ip6 = m;
2199 return 0;
2200 }
2201
2202 /*
2203 * Compute IPv6 extension header length.
2204 */
2205 int
2206 ip6_optlen(in6p)
2207 struct in6pcb *in6p;
2208 {
2209 int len;
2210
2211 if (!in6p->in6p_outputopts)
2212 return 0;
2213
2214 len = 0;
2215 #define elen(x) \
2216 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2217
2218 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2219 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2220 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2221 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2222 return len;
2223 #undef elen
2224 }
2225