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