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