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