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