ip6_output.c revision 1.18 1 /* $NetBSD: ip6_output.c,v 1.18 2000/03/29 03:38:53 simonb Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
65 */
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69 #include "opt_pfil_hooks.h"
70
71 #include <sys/param.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/errno.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80
81 #include <net/if.h>
82 #include <net/route.h>
83 #ifdef PFIL_HOOKS
84 #include <net/pfil.h>
85 #endif
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/nd6.h>
94
95 #ifdef IPSEC
96 #include <netinet6/ipsec.h>
97 #include <netkey/key.h>
98 #include <netkey/key_debug.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 struct ip6_exthdrs {
110 struct mbuf *ip6e_ip6;
111 struct mbuf *ip6e_hbh;
112 struct mbuf *ip6e_dest1;
113 struct mbuf *ip6e_rthdr;
114 struct mbuf *ip6e_dest2;
115 };
116
117 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
118 struct socket *));
119 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
120 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
121 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
122 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
123 struct ip6_frag **));
124 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
125 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
126
127 extern struct ifnet loif[NLOOP];
128
129 /*
130 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
131 * header (with pri, len, nxt, hlim, src, dst).
132 * This function may modify ver and hlim only.
133 * The mbuf chain containing the packet will be freed.
134 * The mbuf opt, if present, will not be freed.
135 */
136 int
137 ip6_output(m0, opt, ro, flags, im6o, ifpp)
138 struct mbuf *m0;
139 struct ip6_pktopts *opt;
140 struct route_in6 *ro;
141 int flags;
142 struct ip6_moptions *im6o;
143 struct ifnet **ifpp; /* XXX: just for statistics */
144 {
145 struct ip6_hdr *ip6, *mhip6;
146 struct ifnet *ifp;
147 struct mbuf *m = m0;
148 int hlen, tlen, len, off;
149 struct route_in6 ip6route;
150 struct sockaddr_in6 *dst;
151 int error = 0;
152 struct in6_ifaddr *ia;
153 u_long mtu;
154 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
155 struct ip6_exthdrs exthdrs;
156 struct in6_addr finaldst;
157 struct route_in6 *ro_pmtu = NULL;
158 int hdrsplit = 0;
159 int needipsec = 0;
160 #ifdef PFIL_HOOKS
161 struct packet_filter_hook *pfh;
162 struct mbuf *m1;
163 int rv;
164 #endif /* PFIL_HOOKS */
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 ipsec_setsocket(m, NULL);
173 ip6 = mtod(m, struct ip6_hdr *);
174 #endif /* IPSEC */
175
176 #define MAKE_EXTHDR(hp,mp) \
177 { \
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 }
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 bad;
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 bad;
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 bad;
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 {\
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 }
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 /*IPESC*/
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 * NetBSD/OpenBSD always clones 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 there is the outgoing interface conflicts with
559 * the interface specified by ifi6_ifindex(if specified).
560 * Note that loopback interface is always okay.
561 * (this happens when we are sending packet toward my
562 * interface)
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 }
618 else {
619 ifp = &loif[0];
620 }
621 }
622
623 if (opt && opt->ip6po_hlim != -1)
624 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
625
626 /*
627 * If caller did not provide an interface lookup a
628 * default in the routing table. This is either a
629 * default for the speicfied group (i.e. a host
630 * route), or a multicast default (a route for the
631 * ``net'' ff00::/8).
632 */
633 if (ifp == NULL) {
634 if (ro->ro_rt == 0) {
635 ro->ro_rt = rtalloc1((struct sockaddr *)
636 &ro->ro_dst, 0
637 );
638 }
639 if (ro->ro_rt == 0) {
640 ip6stat.ip6s_noroute++;
641 error = EHOSTUNREACH;
642 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
643 goto bad;
644 }
645 ia = ifatoia6(ro->ro_rt->rt_ifa);
646 ifp = ro->ro_rt->rt_ifp;
647 ro->ro_rt->rt_use++;
648 }
649
650 if ((flags & IPV6_FORWARDING) == 0)
651 in6_ifstat_inc(ifp, ifs6_out_request);
652 in6_ifstat_inc(ifp, ifs6_out_mcast);
653
654 /*
655 * Confirm that the outgoing interface supports multicast.
656 */
657 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
658 ip6stat.ip6s_noroute++;
659 in6_ifstat_inc(ifp, ifs6_out_discard);
660 error = ENETUNREACH;
661 goto bad;
662 }
663 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
664 if (in6m != NULL &&
665 (im6o == NULL || im6o->im6o_multicast_loop)) {
666 /*
667 * If we belong to the destination multicast group
668 * on the outgoing interface, and the caller did not
669 * forbid loopback, loop back a copy.
670 */
671 ip6_mloopback(ifp, m, dst);
672 } else {
673 /*
674 * If we are acting as a multicast router, perform
675 * multicast forwarding as if the packet had just
676 * arrived on the interface to which we are about
677 * to send. The multicast forwarding function
678 * recursively calls this function, using the
679 * IPV6_FORWARDING flag to prevent infinite recursion.
680 *
681 * Multicasts that are looped back by ip6_mloopback(),
682 * above, will be forwarded by the ip6_input() routine,
683 * if necessary.
684 */
685 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
686 if (ip6_mforward(ip6, ifp, m) != NULL) {
687 m_freem(m);
688 goto done;
689 }
690 }
691 }
692 /*
693 * Multicasts with a hoplimit of zero may be looped back,
694 * above, but must not be transmitted on a network.
695 * Also, multicasts addressed to the loopback interface
696 * are not sent -- the above call to ip6_mloopback() will
697 * loop back a copy if this host actually belongs to the
698 * destination group on the loopback interface.
699 */
700 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
701 m_freem(m);
702 goto done;
703 }
704 }
705
706 /*
707 * Fill the outgoing inteface to tell the upper layer
708 * to increment per-interface statistics.
709 */
710 if (ifpp)
711 *ifpp = ifp;
712
713 /*
714 * Determine path MTU.
715 */
716 if (ro_pmtu != ro) {
717 /* The first hop and the final destination may differ. */
718 struct sockaddr_in6 *sin6_fin =
719 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
720 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
721 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
722 &finaldst))) {
723 RTFREE(ro_pmtu->ro_rt);
724 ro_pmtu->ro_rt = (struct rtentry *)0;
725 }
726 if (ro_pmtu->ro_rt == 0) {
727 bzero(sin6_fin, sizeof(*sin6_fin));
728 sin6_fin->sin6_family = AF_INET6;
729 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
730 sin6_fin->sin6_addr = finaldst;
731
732 rtalloc((struct route *)ro_pmtu);
733 }
734 }
735 if (ro_pmtu->ro_rt != NULL) {
736 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
737
738 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
739 if (mtu > ifmtu) {
740 /*
741 * The MTU on the route is larger than the MTU on
742 * the interface! This shouldn't happen, unless the
743 * MTU of the interface has been changed after the
744 * interface was brought up. Change the MTU in the
745 * route to match the interface MTU (as long as the
746 * field isn't locked).
747 */
748 mtu = ifmtu;
749 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
750 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
751 }
752 } else {
753 mtu = nd_ifinfo[ifp->if_index].linkmtu;
754 }
755
756 /*
757 * Fake link-local scope-class addresses
758 */
759 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
760 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
761 ip6->ip6_src.s6_addr16[1] = 0;
762 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
763 ip6->ip6_dst.s6_addr16[1] = 0;
764 }
765
766 /*
767 * If the outgoing packet contains a hop-by-hop options header,
768 * it must be examined and processed even by the source node.
769 * (RFC 2460, section 4.)
770 */
771 if (exthdrs.ip6e_hbh) {
772 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
773 struct ip6_hbh *);
774 u_int32_t dummy1; /* XXX unused */
775 u_int32_t dummy2; /* XXX unused */
776
777 /*
778 * XXX: if we have to send an ICMPv6 error to the sender,
779 * we need the M_LOOP flag since icmp6_error() expects
780 * the IPv6 and the hop-by-hop options header are
781 * continuous unless the flag is set.
782 */
783 m->m_flags |= M_LOOP;
784 m->m_pkthdr.rcvif = ifp;
785 if (ip6_process_hopopts(m,
786 (u_int8_t *)(hbh + 1),
787 ((hbh->ip6h_len + 1) << 3) -
788 sizeof(struct ip6_hbh),
789 &dummy1, &dummy2) < 0) {
790 /* m was already freed at this point */
791 error = EINVAL;/* better error? */
792 goto done;
793 }
794 m->m_flags &= ~M_LOOP; /* XXX */
795 m->m_pkthdr.rcvif = NULL;
796 }
797
798 #ifdef PFIL_HOOKS
799 /*
800 * Run through list of hooks for output packets.
801 */
802 m1 = m;
803 pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IPV6]].pr_pfh);
804 for (; pfh; pfh = pfh->pfil_link.tqe_next)
805 if (pfh->pfil_func) {
806 rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1);
807 if (rv) {
808 error = EHOSTUNREACH;
809 goto done;
810 }
811 m = m1;
812 if (m == NULL)
813 goto done;
814 ip6 = mtod(m, struct ip6_hdr *);
815 }
816 #endif /* PFIL_HOOKS */
817 /*
818 * Send the packet to the outgoing interface.
819 * If necessary, do IPv6 fragmentation before sending.
820 */
821 tlen = m->m_pkthdr.len;
822 if (tlen <= mtu
823 #ifdef notyet
824 /*
825 * On any link that cannot convey a 1280-octet packet in one piece,
826 * link-specific fragmentation and reassembly must be provided at
827 * a layer below IPv6. [RFC 2460, sec.5]
828 * Thus if the interface has ability of link-level fragmentation,
829 * we can just send the packet even if the packet size is
830 * larger than the link's MTU.
831 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
832 */
833
834 || ifp->if_flags & IFF_FRAGMENTABLE
835 #endif
836 )
837 {
838 #ifdef IFA_STATS
839 if (IFA_STATS) {
840 struct in6_ifaddr *ia6;
841 ip6 = mtod(m, struct ip6_hdr *);
842 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
843 if (ia6) {
844 ia->ia_ifa.ifa_data.ifad_outbytes +=
845 m->m_pkthdr.len;
846 }
847 }
848 #endif
849 #ifdef OLDIP6OUTPUT
850 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
851 ro->ro_rt);
852 #else
853 error = nd6_output(ifp, m, dst, ro->ro_rt);
854 #endif
855 goto done;
856 } else if (mtu < IPV6_MMTU) {
857 /*
858 * note that path MTU is never less than IPV6_MMTU
859 * (see icmp6_input).
860 */
861 error = EMSGSIZE;
862 in6_ifstat_inc(ifp, ifs6_out_fragfail);
863 goto bad;
864 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
865 error = EMSGSIZE;
866 in6_ifstat_inc(ifp, ifs6_out_fragfail);
867 goto bad;
868 } else {
869 struct mbuf **mnext, *m_frgpart;
870 struct ip6_frag *ip6f;
871 u_int32_t id = htonl(ip6_id++);
872 u_char nextproto;
873
874 /*
875 * Too large for the destination or interface;
876 * fragment if possible.
877 * Must be able to put at least 8 bytes per fragment.
878 */
879 hlen = unfragpartlen;
880 if (mtu > IPV6_MAXPACKET)
881 mtu = IPV6_MAXPACKET;
882 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
883 if (len < 8) {
884 error = EMSGSIZE;
885 in6_ifstat_inc(ifp, ifs6_out_fragfail);
886 goto bad;
887 }
888
889 mnext = &m->m_nextpkt;
890
891 /*
892 * Change the next header field of the last header in the
893 * unfragmentable part.
894 */
895 if (exthdrs.ip6e_rthdr) {
896 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
897 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
898 }
899 else if (exthdrs.ip6e_dest1) {
900 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
901 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
902 }
903 else if (exthdrs.ip6e_hbh) {
904 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
905 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
906 }
907 else {
908 nextproto = ip6->ip6_nxt;
909 ip6->ip6_nxt = IPPROTO_FRAGMENT;
910 }
911
912 /*
913 * Loop through length of segment after first fragment,
914 * make new header and copy data of each part and link onto chain.
915 */
916 m0 = m;
917 for (off = hlen; off < tlen; off += len) {
918 MGETHDR(m, M_DONTWAIT, MT_HEADER);
919 if (!m) {
920 error = ENOBUFS;
921 ip6stat.ip6s_odropped++;
922 goto sendorfree;
923 }
924 m->m_flags = m0->m_flags & M_COPYFLAGS;
925 *mnext = m;
926 mnext = &m->m_nextpkt;
927 m->m_data += max_linkhdr;
928 mhip6 = mtod(m, struct ip6_hdr *);
929 *mhip6 = *ip6;
930 m->m_len = sizeof(*mhip6);
931 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
932 if (error) {
933 ip6stat.ip6s_odropped++;
934 goto sendorfree;
935 }
936 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
937 if (off + len >= tlen)
938 len = tlen - off;
939 else
940 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
941 mhip6->ip6_plen = htons((u_short)(len + hlen +
942 sizeof(*ip6f) -
943 sizeof(struct ip6_hdr)));
944 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
945 error = ENOBUFS;
946 ip6stat.ip6s_odropped++;
947 goto sendorfree;
948 }
949 m_cat(m, m_frgpart);
950 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
951 m->m_pkthdr.rcvif = (struct ifnet *)0;
952 ip6f->ip6f_reserved = 0;
953 ip6f->ip6f_ident = id;
954 ip6f->ip6f_nxt = nextproto;
955 ip6stat.ip6s_ofragments++;
956 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
957 }
958
959 in6_ifstat_inc(ifp, ifs6_out_fragok);
960 }
961
962 /*
963 * Remove leading garbages.
964 */
965 sendorfree:
966 m = m0->m_nextpkt;
967 m0->m_nextpkt = 0;
968 m_freem(m0);
969 for (m0 = m; m; m = m0) {
970 m0 = m->m_nextpkt;
971 m->m_nextpkt = 0;
972 if (error == 0) {
973 #ifdef IFA_STATS
974 if (IFA_STATS) {
975 struct in6_ifaddr *ia6;
976 ip6 = mtod(m, struct ip6_hdr *);
977 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
978 if (ia6) {
979 ia->ia_ifa.ifa_data.ifad_outbytes +=
980 m->m_pkthdr.len;
981 }
982 }
983 #endif
984 #ifdef OLDIP6OUTPUT
985 error = (*ifp->if_output)(ifp, m,
986 (struct sockaddr *)dst,
987 ro->ro_rt);
988 #else
989 error = nd6_output(ifp, m, dst, ro->ro_rt);
990 #endif
991 }
992 else
993 m_freem(m);
994 }
995
996 if (error == 0)
997 ip6stat.ip6s_fragmented++;
998
999 done:
1000 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1001 RTFREE(ro->ro_rt);
1002 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1003 RTFREE(ro_pmtu->ro_rt);
1004 }
1005
1006 #ifdef IPSEC
1007 if (sp != NULL)
1008 key_freesp(sp);
1009 #endif /* IPSEC */
1010
1011 return(error);
1012
1013 freehdrs:
1014 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1015 m_freem(exthdrs.ip6e_dest1);
1016 m_freem(exthdrs.ip6e_rthdr);
1017 m_freem(exthdrs.ip6e_dest2);
1018 /* fall through */
1019 bad:
1020 m_freem(m);
1021 goto done;
1022 }
1023
1024 static int
1025 ip6_copyexthdr(mp, hdr, hlen)
1026 struct mbuf **mp;
1027 caddr_t hdr;
1028 int hlen;
1029 {
1030 struct mbuf *m;
1031
1032 if (hlen > MCLBYTES)
1033 return(ENOBUFS); /* XXX */
1034
1035 MGET(m, M_DONTWAIT, MT_DATA);
1036 if (!m)
1037 return(ENOBUFS);
1038
1039 if (hlen > MLEN) {
1040 MCLGET(m, M_DONTWAIT);
1041 if ((m->m_flags & M_EXT) == 0) {
1042 m_free(m);
1043 return(ENOBUFS);
1044 }
1045 }
1046 m->m_len = hlen;
1047 if (hdr)
1048 bcopy(hdr, mtod(m, caddr_t), hlen);
1049
1050 *mp = m;
1051 return(0);
1052 }
1053
1054 /*
1055 * Insert jumbo payload option.
1056 */
1057 static int
1058 ip6_insert_jumboopt(exthdrs, plen)
1059 struct ip6_exthdrs *exthdrs;
1060 u_int32_t plen;
1061 {
1062 struct mbuf *mopt;
1063 u_char *optbuf;
1064
1065 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1066
1067 /*
1068 * If there is no hop-by-hop options header, allocate new one.
1069 * If there is one but it doesn't have enough space to store the
1070 * jumbo payload option, allocate a cluster to store the whole options.
1071 * Otherwise, use it to store the options.
1072 */
1073 if (exthdrs->ip6e_hbh == 0) {
1074 MGET(mopt, M_DONTWAIT, MT_DATA);
1075 if (mopt == 0)
1076 return(ENOBUFS);
1077 mopt->m_len = JUMBOOPTLEN;
1078 optbuf = mtod(mopt, u_char *);
1079 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1080 exthdrs->ip6e_hbh = mopt;
1081 }
1082 else {
1083 struct ip6_hbh *hbh;
1084
1085 mopt = exthdrs->ip6e_hbh;
1086 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1087 caddr_t oldoptp = mtod(mopt, caddr_t);
1088 int oldoptlen = mopt->m_len;
1089
1090 if (mopt->m_flags & M_EXT)
1091 return(ENOBUFS); /* XXX */
1092 MCLGET(mopt, M_DONTWAIT);
1093 if ((mopt->m_flags & M_EXT) == 0)
1094 return(ENOBUFS);
1095
1096 bcopy(oldoptp, mtod(mopt, caddr_t), oldoptlen);
1097 optbuf = mtod(mopt, caddr_t) + oldoptlen;
1098 mopt->m_len = oldoptlen + JUMBOOPTLEN;
1099 }
1100 else {
1101 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1102 mopt->m_len += JUMBOOPTLEN;
1103 }
1104 optbuf[0] = IP6OPT_PADN;
1105 optbuf[1] = 1;
1106
1107 /*
1108 * Adjust the header length according to the pad and
1109 * the jumbo payload option.
1110 */
1111 hbh = mtod(mopt, struct ip6_hbh *);
1112 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1113 }
1114
1115 /* fill in the option. */
1116 optbuf[2] = IP6OPT_JUMBO;
1117 optbuf[3] = 4;
1118 *(u_int32_t *)&optbuf[4] = htonl(plen + JUMBOOPTLEN);
1119
1120 /* finally, adjust the packet header length */
1121 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1122
1123 return(0);
1124 #undef JUMBOOPTLEN
1125 }
1126
1127 /*
1128 * Insert fragment header and copy unfragmentable header portions.
1129 */
1130 static int
1131 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1132 struct mbuf *m0, *m;
1133 int hlen;
1134 struct ip6_frag **frghdrp;
1135 {
1136 struct mbuf *n, *mlast;
1137
1138 if (hlen > sizeof(struct ip6_hdr)) {
1139 n = m_copym(m0, sizeof(struct ip6_hdr),
1140 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1141 if (n == 0)
1142 return(ENOBUFS);
1143 m->m_next = n;
1144 }
1145 else
1146 n = m;
1147
1148 /* Search for the last mbuf of unfragmentable part. */
1149 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1150 ;
1151
1152 if ((mlast->m_flags & M_EXT) == 0 &&
1153 M_TRAILINGSPACE(mlast) < sizeof(struct ip6_frag)) {
1154 /* use the trailing space of the last mbuf for the fragment hdr */
1155 *frghdrp =
1156 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1157 mlast->m_len += sizeof(struct ip6_frag);
1158 m->m_pkthdr.len += sizeof(struct ip6_frag);
1159 }
1160 else {
1161 /* allocate a new mbuf for the fragment header */
1162 struct mbuf *mfrg;
1163
1164 MGET(mfrg, M_DONTWAIT, MT_DATA);
1165 if (mfrg == 0)
1166 return(ENOBUFS);
1167 mfrg->m_len = sizeof(struct ip6_frag);
1168 *frghdrp = mtod(mfrg, struct ip6_frag *);
1169 mlast->m_next = mfrg;
1170 }
1171
1172 return(0);
1173 }
1174
1175 /*
1176 * IP6 socket option processing.
1177 */
1178 int
1179 ip6_ctloutput(op, so, level, optname, mp)
1180 int op;
1181 struct socket *so;
1182 int level, optname;
1183 struct mbuf **mp;
1184 {
1185 register struct in6pcb *in6p = sotoin6pcb(so);
1186 register struct mbuf *m = *mp;
1187 register int optval = 0;
1188 int error = 0;
1189 struct proc *p = curproc; /* XXX */
1190
1191 if (level == IPPROTO_IPV6)
1192 switch (op) {
1193
1194 case PRCO_SETOPT:
1195 switch (optname) {
1196 case IPV6_PKTOPTIONS:
1197 return(ip6_pcbopts(&in6p->in6p_outputopts,
1198 m, so));
1199 case IPV6_HOPOPTS:
1200 case IPV6_DSTOPTS:
1201 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1202 error = EPERM;
1203 break;
1204 }
1205 /* fall through */
1206 case IPV6_UNICAST_HOPS:
1207 case IPV6_RECVOPTS:
1208 case IPV6_RECVRETOPTS:
1209 case IPV6_RECVDSTADDR:
1210 case IPV6_PKTINFO:
1211 case IPV6_HOPLIMIT:
1212 case IPV6_RTHDR:
1213 case IPV6_CHECKSUM:
1214 case IPV6_FAITH:
1215 #ifndef INET6_BINDV6ONLY
1216 case IPV6_BINDV6ONLY:
1217 #endif
1218 if (!m || m->m_len != sizeof(int))
1219 error = EINVAL;
1220 else {
1221 optval = *mtod(m, int *);
1222 switch (optname) {
1223
1224 case IPV6_UNICAST_HOPS:
1225 if (optval < -1 || optval >= 256)
1226 error = EINVAL;
1227 else {
1228 /* -1 = kernel default */
1229 in6p->in6p_hops = optval;
1230 }
1231 break;
1232 #define OPTSET(bit) \
1233 if (optval) \
1234 in6p->in6p_flags |= bit; \
1235 else \
1236 in6p->in6p_flags &= ~bit;
1237
1238 case IPV6_RECVOPTS:
1239 OPTSET(IN6P_RECVOPTS);
1240 break;
1241
1242 case IPV6_RECVRETOPTS:
1243 OPTSET(IN6P_RECVRETOPTS);
1244 break;
1245
1246 case IPV6_RECVDSTADDR:
1247 OPTSET(IN6P_RECVDSTADDR);
1248 break;
1249
1250 case IPV6_PKTINFO:
1251 OPTSET(IN6P_PKTINFO);
1252 break;
1253
1254 case IPV6_HOPLIMIT:
1255 OPTSET(IN6P_HOPLIMIT);
1256 break;
1257
1258 case IPV6_HOPOPTS:
1259 OPTSET(IN6P_HOPOPTS);
1260 break;
1261
1262 case IPV6_DSTOPTS:
1263 OPTSET(IN6P_DSTOPTS);
1264 break;
1265
1266 case IPV6_RTHDR:
1267 OPTSET(IN6P_RTHDR);
1268 break;
1269
1270 case IPV6_CHECKSUM:
1271 in6p->in6p_cksum = optval;
1272 break;
1273
1274 case IPV6_FAITH:
1275 OPTSET(IN6P_FAITH);
1276 break;
1277
1278 #ifndef INET6_BINDV6ONLY
1279 case IPV6_BINDV6ONLY:
1280 OPTSET(IN6P_BINDV6ONLY);
1281 break;
1282 #endif
1283 }
1284 }
1285 break;
1286 #undef OPTSET
1287
1288 case IPV6_MULTICAST_IF:
1289 case IPV6_MULTICAST_HOPS:
1290 case IPV6_MULTICAST_LOOP:
1291 case IPV6_JOIN_GROUP:
1292 case IPV6_LEAVE_GROUP:
1293 error = ip6_setmoptions(optname, &in6p->in6p_moptions, m);
1294 break;
1295
1296 case IPV6_PORTRANGE:
1297 optval = *mtod(m, int *);
1298
1299 switch (optval) {
1300 case IPV6_PORTRANGE_DEFAULT:
1301 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1302 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1303 break;
1304
1305 case IPV6_PORTRANGE_HIGH:
1306 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1307 in6p->in6p_flags |= IN6P_HIGHPORT;
1308 break;
1309
1310 case IPV6_PORTRANGE_LOW:
1311 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1312 in6p->in6p_flags |= IN6P_LOWPORT;
1313 break;
1314
1315 default:
1316 error = EINVAL;
1317 break;
1318 }
1319 break;
1320
1321 #ifdef IPSEC
1322 case IPV6_IPSEC_POLICY:
1323 {
1324 caddr_t req = NULL;
1325 size_t len = 0;
1326
1327 int priv = 0;
1328 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1329 priv = 0;
1330 else
1331 priv = 1;
1332 if (m) {
1333 req = mtod(m, caddr_t);
1334 len = m->m_len;
1335 }
1336 error = ipsec6_set_policy(in6p,
1337 optname, req, len, priv);
1338 }
1339 break;
1340 #endif /* IPSEC */
1341
1342 default:
1343 error = ENOPROTOOPT;
1344 break;
1345 }
1346 if (m)
1347 (void)m_free(m);
1348 break;
1349
1350 case PRCO_GETOPT:
1351 switch (optname) {
1352
1353 case IPV6_OPTIONS:
1354 case IPV6_RETOPTS:
1355 #if 0
1356 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1357 if (in6p->in6p_options) {
1358 m->m_len = in6p->in6p_options->m_len;
1359 bcopy(mtod(in6p->in6p_options, caddr_t),
1360 mtod(m, caddr_t),
1361 (unsigned)m->m_len);
1362 } else
1363 m->m_len = 0;
1364 break;
1365 #else
1366 error = ENOPROTOOPT;
1367 break;
1368 #endif
1369
1370 case IPV6_PKTOPTIONS:
1371 if (in6p->in6p_options) {
1372 *mp = m_copym(in6p->in6p_options, 0,
1373 M_COPYALL, M_WAIT);
1374 } else {
1375 *mp = m_get(M_WAIT, MT_SOOPTS);
1376 (*mp)->m_len = 0;
1377 }
1378 break;
1379
1380 case IPV6_HOPOPTS:
1381 case IPV6_DSTOPTS:
1382 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1383 error = EPERM;
1384 break;
1385 }
1386 /* fall through */
1387 case IPV6_UNICAST_HOPS:
1388 case IPV6_RECVOPTS:
1389 case IPV6_RECVRETOPTS:
1390 case IPV6_RECVDSTADDR:
1391 case IPV6_PORTRANGE:
1392 case IPV6_PKTINFO:
1393 case IPV6_HOPLIMIT:
1394 case IPV6_RTHDR:
1395 case IPV6_CHECKSUM:
1396 case IPV6_FAITH:
1397 #ifndef INET6_BINDV6ONLY
1398 case IPV6_BINDV6ONLY:
1399 #endif
1400 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1401 m->m_len = sizeof(int);
1402 switch (optname) {
1403
1404 case IPV6_UNICAST_HOPS:
1405 optval = in6p->in6p_hops;
1406 break;
1407
1408 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1409
1410 case IPV6_RECVOPTS:
1411 optval = OPTBIT(IN6P_RECVOPTS);
1412 break;
1413
1414 case IPV6_RECVRETOPTS:
1415 optval = OPTBIT(IN6P_RECVRETOPTS);
1416 break;
1417
1418 case IPV6_RECVDSTADDR:
1419 optval = OPTBIT(IN6P_RECVDSTADDR);
1420 break;
1421
1422 case IPV6_PORTRANGE:
1423 {
1424 int flags;
1425 flags = in6p->in6p_flags;
1426 if (flags & IN6P_HIGHPORT)
1427 optval = IPV6_PORTRANGE_HIGH;
1428 else if (flags & IN6P_LOWPORT)
1429 optval = IPV6_PORTRANGE_LOW;
1430 else
1431 optval = 0;
1432 break;
1433 }
1434
1435 case IPV6_PKTINFO:
1436 optval = OPTBIT(IN6P_PKTINFO);
1437 break;
1438
1439 case IPV6_HOPLIMIT:
1440 optval = OPTBIT(IN6P_HOPLIMIT);
1441 break;
1442
1443 case IPV6_HOPOPTS:
1444 optval = OPTBIT(IN6P_HOPOPTS);
1445 break;
1446
1447 case IPV6_DSTOPTS:
1448 optval = OPTBIT(IN6P_DSTOPTS);
1449 break;
1450
1451 case IPV6_RTHDR:
1452 optval = OPTBIT(IN6P_RTHDR);
1453 break;
1454
1455 case IPV6_CHECKSUM:
1456 optval = in6p->in6p_cksum;
1457 break;
1458
1459 case IPV6_FAITH:
1460 optval = OPTBIT(IN6P_FAITH);
1461 break;
1462
1463 #ifndef INET6_BINDV6ONLY
1464 case IPV6_BINDV6ONLY:
1465 optval = OPTBIT(IN6P_BINDV6ONLY);
1466 break;
1467 #endif
1468 }
1469 *mtod(m, int *) = optval;
1470 break;
1471
1472 case IPV6_MULTICAST_IF:
1473 case IPV6_MULTICAST_HOPS:
1474 case IPV6_MULTICAST_LOOP:
1475 case IPV6_JOIN_GROUP:
1476 case IPV6_LEAVE_GROUP:
1477 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1478 break;
1479
1480 #ifdef IPSEC
1481 case IPV6_IPSEC_POLICY:
1482 {
1483 caddr_t req = NULL;
1484 size_t len = 0;
1485
1486 if (m) {
1487 req = mtod(m, caddr_t);
1488 len = m->m_len;
1489 }
1490 error = ipsec6_get_policy(in6p, req, len, mp);
1491 break;
1492 }
1493 #endif /* IPSEC */
1494
1495 default:
1496 error = ENOPROTOOPT;
1497 break;
1498 }
1499 break;
1500 }
1501 else {
1502 error = EINVAL;
1503 if (op == PRCO_SETOPT && *mp)
1504 (void)m_free(*mp);
1505 }
1506 return(error);
1507 }
1508
1509 /*
1510 * Set up IP6 options in pcb for insertion in output packets.
1511 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1512 * with destination address if source routed.
1513 */
1514 static int
1515 ip6_pcbopts(pktopt, m, so)
1516 struct ip6_pktopts **pktopt;
1517 register struct mbuf *m;
1518 struct socket *so;
1519 {
1520 register struct ip6_pktopts *opt = *pktopt;
1521 int error = 0;
1522 struct proc *p = curproc; /* XXX */
1523 int priv = 0;
1524
1525 /* turn off any old options. */
1526 if (opt) {
1527 if (opt->ip6po_m)
1528 (void)m_free(opt->ip6po_m);
1529 }
1530 else
1531 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1532 *pktopt = 0;
1533
1534 if (!m || m->m_len == 0) {
1535 /*
1536 * Only turning off any previous options.
1537 */
1538 if (opt)
1539 free(opt, M_IP6OPT);
1540 if (m)
1541 (void)m_free(m);
1542 return(0);
1543 }
1544
1545 /* set options specified by user. */
1546 if (p && !suser(p->p_ucred, &p->p_acflag))
1547 priv = 1;
1548 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1549 (void)m_free(m);
1550 return(error);
1551 }
1552 *pktopt = opt;
1553 return(0);
1554 }
1555
1556 /*
1557 * Set the IP6 multicast options in response to user setsockopt().
1558 */
1559 static int
1560 ip6_setmoptions(optname, im6op, m)
1561 int optname;
1562 struct ip6_moptions **im6op;
1563 struct mbuf *m;
1564 {
1565 int error = 0;
1566 u_int loop, ifindex;
1567 struct ipv6_mreq *mreq;
1568 struct ifnet *ifp;
1569 struct ip6_moptions *im6o = *im6op;
1570 struct route_in6 ro;
1571 struct sockaddr_in6 *dst;
1572 struct in6_multi_mship *imm;
1573 struct proc *p = curproc; /* XXX */
1574
1575 if (im6o == NULL) {
1576 /*
1577 * No multicast option buffer attached to the pcb;
1578 * allocate one and initialize to default values.
1579 */
1580 im6o = (struct ip6_moptions *)
1581 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1582
1583 if (im6o == NULL)
1584 return(ENOBUFS);
1585 *im6op = im6o;
1586 im6o->im6o_multicast_ifp = NULL;
1587 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1588 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1589 LIST_INIT(&im6o->im6o_memberships);
1590 }
1591
1592 switch (optname) {
1593
1594 case IPV6_MULTICAST_IF:
1595 /*
1596 * Select the interface for outgoing multicast packets.
1597 */
1598 if (m == NULL || m->m_len != sizeof(u_int)) {
1599 error = EINVAL;
1600 break;
1601 }
1602 ifindex = *(mtod(m, u_int *));
1603 if (ifindex < 0 || if_index < ifindex) {
1604 error = ENXIO; /* XXX EINVAL? */
1605 break;
1606 }
1607 ifp = ifindex2ifnet[ifindex];
1608 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1609 error = EADDRNOTAVAIL;
1610 break;
1611 }
1612 im6o->im6o_multicast_ifp = ifp;
1613 break;
1614
1615 case IPV6_MULTICAST_HOPS:
1616 {
1617 /*
1618 * Set the IP6 hoplimit for outgoing multicast packets.
1619 */
1620 int optval;
1621 if (m == NULL || m->m_len != sizeof(int)) {
1622 error = EINVAL;
1623 break;
1624 }
1625 optval = *(mtod(m, u_int *));
1626 if (optval < -1 || optval >= 256)
1627 error = EINVAL;
1628 else if (optval == -1)
1629 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1630 else
1631 im6o->im6o_multicast_hlim = optval;
1632 break;
1633 }
1634
1635 case IPV6_MULTICAST_LOOP:
1636 /*
1637 * Set the loopback flag for outgoing multicast packets.
1638 * Must be zero or one.
1639 */
1640 if (m == NULL || m->m_len != sizeof(u_int) ||
1641 (loop = *(mtod(m, u_int *))) > 1) {
1642 error = EINVAL;
1643 break;
1644 }
1645 im6o->im6o_multicast_loop = loop;
1646 break;
1647
1648 case IPV6_JOIN_GROUP:
1649 /*
1650 * Add a multicast group membership.
1651 * Group must be a valid IP6 multicast address.
1652 */
1653 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1654 error = EINVAL;
1655 break;
1656 }
1657 mreq = mtod(m, struct ipv6_mreq *);
1658 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1659 /*
1660 * We use the unspecified address to specify to accept
1661 * all multicast addresses. Only super user is allowed
1662 * to do this.
1663 */
1664 if (suser(p->p_ucred, &p->p_acflag)) {
1665 error = EACCES;
1666 break;
1667 }
1668 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1669 error = EINVAL;
1670 break;
1671 }
1672
1673 /*
1674 * If the interface is specified, validate it.
1675 */
1676 if (mreq->ipv6mr_interface < 0
1677 || if_index < mreq->ipv6mr_interface) {
1678 error = ENXIO; /* XXX EINVAL? */
1679 break;
1680 }
1681 /*
1682 * If no interface was explicitly specified, choose an
1683 * appropriate one according to the given multicast address.
1684 */
1685 if (mreq->ipv6mr_interface == 0) {
1686 /*
1687 * If the multicast address is in node-local scope,
1688 * the interface should be a loopback interface.
1689 * Otherwise, look up the routing table for the
1690 * address, and choose the outgoing interface.
1691 * XXX: is it a good approach?
1692 */
1693 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1694 ifp = &loif[0];
1695 }
1696 else {
1697 ro.ro_rt = NULL;
1698 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1699 bzero(dst, sizeof(*dst));
1700 dst->sin6_len = sizeof(struct sockaddr_in6);
1701 dst->sin6_family = AF_INET6;
1702 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1703 rtalloc((struct route *)&ro);
1704 if (ro.ro_rt == NULL) {
1705 error = EADDRNOTAVAIL;
1706 break;
1707 }
1708 ifp = ro.ro_rt->rt_ifp;
1709 rtfree(ro.ro_rt);
1710 }
1711 } else
1712 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1713
1714 /*
1715 * See if we found an interface, and confirm that it
1716 * supports multicast
1717 */
1718 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1719 error = EADDRNOTAVAIL;
1720 break;
1721 }
1722 /*
1723 * Put interface index into the multicast address,
1724 * if the address has link-local scope.
1725 */
1726 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1727 mreq->ipv6mr_multiaddr.s6_addr16[1]
1728 = htons(mreq->ipv6mr_interface);
1729 }
1730 /*
1731 * See if the membership already exists.
1732 */
1733 for (imm = im6o->im6o_memberships.lh_first;
1734 imm != NULL; imm = imm->i6mm_chain.le_next)
1735 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1736 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1737 &mreq->ipv6mr_multiaddr))
1738 break;
1739 if (imm != NULL) {
1740 error = EADDRINUSE;
1741 break;
1742 }
1743 /*
1744 * Everything looks good; add a new record to the multicast
1745 * address list for the given interface.
1746 */
1747 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
1748 if (imm == NULL) {
1749 error = ENOBUFS;
1750 break;
1751 }
1752 if ((imm->i6mm_maddr =
1753 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
1754 free(imm, M_IPMADDR);
1755 break;
1756 }
1757 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1758 break;
1759
1760 case IPV6_LEAVE_GROUP:
1761 /*
1762 * Drop a multicast group membership.
1763 * Group must be a valid IP6 multicast address.
1764 */
1765 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1766 error = EINVAL;
1767 break;
1768 }
1769 mreq = mtod(m, struct ipv6_mreq *);
1770 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1771 if (suser(p->p_ucred, &p->p_acflag)) {
1772 error = EACCES;
1773 break;
1774 }
1775 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1776 error = EINVAL;
1777 break;
1778 }
1779 /*
1780 * If an interface address was specified, get a pointer
1781 * to its ifnet structure.
1782 */
1783 if (mreq->ipv6mr_interface < 0
1784 || if_index < mreq->ipv6mr_interface) {
1785 error = ENXIO; /* XXX EINVAL? */
1786 break;
1787 }
1788 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1789 /*
1790 * Put interface index into the multicast address,
1791 * if the address has link-local scope.
1792 */
1793 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1794 mreq->ipv6mr_multiaddr.s6_addr16[1]
1795 = htons(mreq->ipv6mr_interface);
1796 }
1797 /*
1798 * Find the membership in the membership list.
1799 */
1800 for (imm = im6o->im6o_memberships.lh_first;
1801 imm != NULL; imm = imm->i6mm_chain.le_next) {
1802 if ((ifp == NULL ||
1803 imm->i6mm_maddr->in6m_ifp == ifp) &&
1804 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1805 &mreq->ipv6mr_multiaddr))
1806 break;
1807 }
1808 if (imm == NULL) {
1809 /* Unable to resolve interface */
1810 error = EADDRNOTAVAIL;
1811 break;
1812 }
1813 /*
1814 * Give up the multicast address record to which the
1815 * membership points.
1816 */
1817 LIST_REMOVE(imm, i6mm_chain);
1818 in6_delmulti(imm->i6mm_maddr);
1819 free(imm, M_IPMADDR);
1820 break;
1821
1822 default:
1823 error = EOPNOTSUPP;
1824 break;
1825 }
1826
1827 /*
1828 * If all options have default values, no need to keep the mbuf.
1829 */
1830 if (im6o->im6o_multicast_ifp == NULL &&
1831 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1832 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1833 im6o->im6o_memberships.lh_first == NULL) {
1834 free(*im6op, M_IPMOPTS);
1835 *im6op = NULL;
1836 }
1837
1838 return(error);
1839 }
1840
1841 /*
1842 * Return the IP6 multicast options in response to user getsockopt().
1843 */
1844 static int
1845 ip6_getmoptions(optname, im6o, mp)
1846 int optname;
1847 register struct ip6_moptions *im6o;
1848 register struct mbuf **mp;
1849 {
1850 u_int *hlim, *loop, *ifindex;
1851
1852 *mp = m_get(M_WAIT, MT_SOOPTS);
1853
1854 switch (optname) {
1855
1856 case IPV6_MULTICAST_IF:
1857 ifindex = mtod(*mp, u_int *);
1858 (*mp)->m_len = sizeof(u_int);
1859 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1860 *ifindex = 0;
1861 else
1862 *ifindex = im6o->im6o_multicast_ifp->if_index;
1863 return(0);
1864
1865 case IPV6_MULTICAST_HOPS:
1866 hlim = mtod(*mp, u_int *);
1867 (*mp)->m_len = sizeof(u_int);
1868 if (im6o == NULL)
1869 *hlim = ip6_defmcasthlim;
1870 else
1871 *hlim = im6o->im6o_multicast_hlim;
1872 return(0);
1873
1874 case IPV6_MULTICAST_LOOP:
1875 loop = mtod(*mp, u_int *);
1876 (*mp)->m_len = sizeof(u_int);
1877 if (im6o == NULL)
1878 *loop = ip6_defmcasthlim;
1879 else
1880 *loop = im6o->im6o_multicast_loop;
1881 return(0);
1882
1883 default:
1884 return(EOPNOTSUPP);
1885 }
1886 }
1887
1888 /*
1889 * Discard the IP6 multicast options.
1890 */
1891 void
1892 ip6_freemoptions(im6o)
1893 register struct ip6_moptions *im6o;
1894 {
1895 struct in6_multi_mship *imm;
1896
1897 if (im6o == NULL)
1898 return;
1899
1900 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
1901 LIST_REMOVE(imm, i6mm_chain);
1902 if (imm->i6mm_maddr)
1903 in6_delmulti(imm->i6mm_maddr);
1904 free(imm, M_IPMADDR);
1905 }
1906 free(im6o, M_IPMOPTS);
1907 }
1908
1909 /*
1910 * Set IPv6 outgoing packet options based on advanced API.
1911 */
1912 int
1913 ip6_setpktoptions(control, opt, priv)
1914 struct mbuf *control;
1915 struct ip6_pktopts *opt;
1916 int priv;
1917 {
1918 register struct cmsghdr *cm = 0;
1919
1920 if (control == 0 || opt == 0)
1921 return(EINVAL);
1922
1923 bzero(opt, sizeof(*opt));
1924 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
1925
1926 /*
1927 * XXX: Currently, we assume all the optional information is stored
1928 * in a single mbuf.
1929 */
1930 if (control->m_next)
1931 return(EINVAL);
1932
1933 opt->ip6po_m = control;
1934
1935 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
1936 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1937 cm = mtod(control, struct cmsghdr *);
1938 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
1939 return(EINVAL);
1940 if (cm->cmsg_level != IPPROTO_IPV6)
1941 continue;
1942
1943 switch(cm->cmsg_type) {
1944 case IPV6_PKTINFO:
1945 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
1946 return(EINVAL);
1947 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1948 if (opt->ip6po_pktinfo->ipi6_ifindex &&
1949 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
1950 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
1951 htons(opt->ip6po_pktinfo->ipi6_ifindex);
1952
1953 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
1954 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
1955 return(ENXIO);
1956 }
1957
1958 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
1959 struct ifaddr *ia;
1960 struct sockaddr_in6 sin6;
1961
1962 bzero(&sin6, sizeof(sin6));
1963 sin6.sin6_len = sizeof(sin6);
1964 sin6.sin6_family = AF_INET6;
1965 sin6.sin6_addr =
1966 opt->ip6po_pktinfo->ipi6_addr;
1967 ia = ifa_ifwithaddr(sin6tosa(&sin6));
1968 if (ia == NULL ||
1969 (opt->ip6po_pktinfo->ipi6_ifindex &&
1970 (ia->ifa_ifp->if_index !=
1971 opt->ip6po_pktinfo->ipi6_ifindex))) {
1972 return(EADDRNOTAVAIL);
1973 }
1974 /*
1975 * Check if the requested source address is
1976 * indeed a unicast address assigned to the
1977 * node.
1978 */
1979 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
1980 return(EADDRNOTAVAIL);
1981 }
1982 break;
1983
1984 case IPV6_HOPLIMIT:
1985 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
1986 return(EINVAL);
1987
1988 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
1989 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
1990 return(EINVAL);
1991 break;
1992
1993 case IPV6_NEXTHOP:
1994 if (!priv)
1995 return(EPERM);
1996 if (cm->cmsg_len < sizeof(u_char) ||
1997 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
1998 return(EINVAL);
1999
2000 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2001
2002 break;
2003
2004 case IPV6_HOPOPTS:
2005 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2006 return(EINVAL);
2007 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2008 if (cm->cmsg_len !=
2009 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
2010 return(EINVAL);
2011 break;
2012
2013 case IPV6_DSTOPTS:
2014 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2015 return(EINVAL);
2016
2017 /*
2018 * If there is no routing header yet, the destination
2019 * options header should be put on the 1st part.
2020 * Otherwise, the header should be on the 2nd part.
2021 * (See RFC 2460, section 4.1)
2022 */
2023 if (opt->ip6po_rthdr == NULL) {
2024 opt->ip6po_dest1 =
2025 (struct ip6_dest *)CMSG_DATA(cm);
2026 if (cm->cmsg_len !=
2027 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
2028 << 3))
2029 return(EINVAL);
2030 }
2031 else {
2032 opt->ip6po_dest2 =
2033 (struct ip6_dest *)CMSG_DATA(cm);
2034 if (cm->cmsg_len !=
2035 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
2036 << 3))
2037 return(EINVAL);
2038 }
2039 break;
2040
2041 case IPV6_RTHDR:
2042 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2043 return(EINVAL);
2044 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
2045 if (cm->cmsg_len !=
2046 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
2047 return(EINVAL);
2048 switch(opt->ip6po_rthdr->ip6r_type) {
2049 case IPV6_RTHDR_TYPE_0:
2050 if (opt->ip6po_rthdr->ip6r_segleft == 0)
2051 return(EINVAL);
2052 break;
2053 default:
2054 return(EINVAL);
2055 }
2056 break;
2057
2058 default:
2059 return(ENOPROTOOPT);
2060 }
2061 }
2062
2063 return(0);
2064 }
2065
2066 /*
2067 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2068 * packet to the input queue of a specified interface. Note that this
2069 * calls the output routine of the loopback "driver", but with an interface
2070 * pointer that might NOT be &loif -- easier than replicating that code here.
2071 */
2072 void
2073 ip6_mloopback(ifp, m, dst)
2074 struct ifnet *ifp;
2075 register struct mbuf *m;
2076 register struct sockaddr_in6 *dst;
2077 {
2078 struct mbuf *copym;
2079
2080 copym = m_copy(m, 0, M_COPYALL);
2081 if (copym != NULL)
2082 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2083 }
2084
2085 /*
2086 * Chop IPv6 header off from the payload.
2087 */
2088 static int
2089 ip6_splithdr(m, exthdrs)
2090 struct mbuf *m;
2091 struct ip6_exthdrs *exthdrs;
2092 {
2093 struct mbuf *mh;
2094 struct ip6_hdr *ip6;
2095
2096 ip6 = mtod(m, struct ip6_hdr *);
2097 if (m->m_len > sizeof(*ip6)) {
2098 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2099 if (mh == 0) {
2100 m_freem(m);
2101 return ENOBUFS;
2102 }
2103 M_COPY_PKTHDR(mh, m);
2104 MH_ALIGN(mh, sizeof(*ip6));
2105 m->m_flags &= ~M_PKTHDR;
2106 m->m_len -= sizeof(*ip6);
2107 m->m_data += sizeof(*ip6);
2108 mh->m_next = m;
2109 m = mh;
2110 m->m_len = sizeof(*ip6);
2111 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2112 }
2113 exthdrs->ip6e_ip6 = m;
2114 return 0;
2115 }
2116
2117 /*
2118 * Compute IPv6 extension header length.
2119 */
2120 int
2121 ip6_optlen(in6p)
2122 struct in6pcb *in6p;
2123 {
2124 int len;
2125
2126 if (!in6p->in6p_outputopts)
2127 return 0;
2128
2129 len = 0;
2130 #define elen(x) \
2131 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2132
2133 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2134 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2135 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2136 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2137 return len;
2138 #undef elen
2139 }
2140