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