ip6_output.c revision 1.50 1 /* $NetBSD: ip6_output.c,v 1.50 2002/06/07 14:43:11 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.50 2002/06/07 14:43:11 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
1246 case PRCO_SETOPT:
1247 switch (optname) {
1248 case IPV6_PKTOPTIONS:
1249 /* m is freed in ip6_pcbopts */
1250 return(ip6_pcbopts(&in6p->in6p_outputopts,
1251 m, so));
1252 case IPV6_HOPOPTS:
1253 case IPV6_DSTOPTS:
1254 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1255 error = EPERM;
1256 break;
1257 }
1258 /* FALLTHROUGH */
1259 case IPV6_UNICAST_HOPS:
1260 case IPV6_RECVOPTS:
1261 case IPV6_RECVRETOPTS:
1262 case IPV6_RECVDSTADDR:
1263 case IPV6_PKTINFO:
1264 case IPV6_HOPLIMIT:
1265 case IPV6_RTHDR:
1266 case IPV6_FAITH:
1267 case IPV6_V6ONLY:
1268 if (!m || m->m_len != sizeof(int)) {
1269 error = EINVAL;
1270 break;
1271 }
1272 optval = *mtod(m, int *);
1273 switch (optname) {
1274
1275 case IPV6_UNICAST_HOPS:
1276 if (optval < -1 || optval >= 256)
1277 error = EINVAL;
1278 else {
1279 /* -1 = kernel default */
1280 in6p->in6p_hops = optval;
1281 }
1282 break;
1283 #define OPTSET(bit) \
1284 do { \
1285 if (optval) \
1286 in6p->in6p_flags |= (bit); \
1287 else \
1288 in6p->in6p_flags &= ~(bit); \
1289 } while (0)
1290
1291 case IPV6_RECVOPTS:
1292 OPTSET(IN6P_RECVOPTS);
1293 break;
1294
1295 case IPV6_RECVRETOPTS:
1296 OPTSET(IN6P_RECVRETOPTS);
1297 break;
1298
1299 case IPV6_RECVDSTADDR:
1300 OPTSET(IN6P_RECVDSTADDR);
1301 break;
1302
1303 case IPV6_PKTINFO:
1304 OPTSET(IN6P_PKTINFO);
1305 break;
1306
1307 case IPV6_HOPLIMIT:
1308 OPTSET(IN6P_HOPLIMIT);
1309 break;
1310
1311 case IPV6_HOPOPTS:
1312 OPTSET(IN6P_HOPOPTS);
1313 break;
1314
1315 case IPV6_DSTOPTS:
1316 OPTSET(IN6P_DSTOPTS);
1317 break;
1318
1319 case IPV6_RTHDR:
1320 OPTSET(IN6P_RTHDR);
1321 break;
1322
1323 case IPV6_FAITH:
1324 OPTSET(IN6P_FAITH);
1325 break;
1326
1327 case IPV6_V6ONLY:
1328 /*
1329 * make setsockopt(IPV6_V6ONLY)
1330 * available only prior to bind(2).
1331 * see ipng mailing list, Jun 22 2001.
1332 */
1333 if (in6p->in6p_lport ||
1334 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1335 error = EINVAL;
1336 break;
1337 }
1338 #ifdef INET6_BINDV6ONLY
1339 if (!optval)
1340 error = EINVAL;
1341 #else
1342 OPTSET(IN6P_IPV6_V6ONLY);
1343 #endif
1344 break;
1345 }
1346 break;
1347 #undef OPTSET
1348
1349 case IPV6_MULTICAST_IF:
1350 case IPV6_MULTICAST_HOPS:
1351 case IPV6_MULTICAST_LOOP:
1352 case IPV6_JOIN_GROUP:
1353 case IPV6_LEAVE_GROUP:
1354 error = ip6_setmoptions(optname,
1355 &in6p->in6p_moptions, m);
1356 break;
1357
1358 case IPV6_PORTRANGE:
1359 optval = *mtod(m, int *);
1360
1361 switch (optval) {
1362 case IPV6_PORTRANGE_DEFAULT:
1363 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1364 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1365 break;
1366
1367 case IPV6_PORTRANGE_HIGH:
1368 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1369 in6p->in6p_flags |= IN6P_HIGHPORT;
1370 break;
1371
1372 case IPV6_PORTRANGE_LOW:
1373 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1374 in6p->in6p_flags |= IN6P_LOWPORT;
1375 break;
1376
1377 default:
1378 error = EINVAL;
1379 break;
1380 }
1381 break;
1382
1383 #ifdef IPSEC
1384 case IPV6_IPSEC_POLICY:
1385 {
1386 caddr_t req = NULL;
1387 size_t len = 0;
1388
1389 int priv = 0;
1390 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1391 priv = 0;
1392 else
1393 priv = 1;
1394 if (m) {
1395 req = mtod(m, caddr_t);
1396 len = m->m_len;
1397 }
1398 error = ipsec6_set_policy(in6p,
1399 optname, req, len, priv);
1400 }
1401 break;
1402 #endif /* IPSEC */
1403
1404 default:
1405 error = ENOPROTOOPT;
1406 break;
1407 }
1408 if (m)
1409 (void)m_free(m);
1410 break;
1411
1412 case PRCO_GETOPT:
1413 switch (optname) {
1414
1415 case IPV6_OPTIONS:
1416 case IPV6_RETOPTS:
1417 error = ENOPROTOOPT;
1418 break;
1419
1420 case IPV6_PKTOPTIONS:
1421 if (in6p->in6p_options) {
1422 *mp = m_copym(in6p->in6p_options, 0,
1423 M_COPYALL, M_WAIT);
1424 } else {
1425 *mp = m_get(M_WAIT, MT_SOOPTS);
1426 (*mp)->m_len = 0;
1427 }
1428 break;
1429
1430 case IPV6_HOPOPTS:
1431 case IPV6_DSTOPTS:
1432 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1433 error = EPERM;
1434 break;
1435 }
1436 /* FALLTHROUGH */
1437 case IPV6_UNICAST_HOPS:
1438 case IPV6_RECVOPTS:
1439 case IPV6_RECVRETOPTS:
1440 case IPV6_RECVDSTADDR:
1441 case IPV6_PORTRANGE:
1442 case IPV6_PKTINFO:
1443 case IPV6_HOPLIMIT:
1444 case IPV6_RTHDR:
1445 case IPV6_FAITH:
1446 case IPV6_V6ONLY:
1447 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1448 m->m_len = sizeof(int);
1449 switch (optname) {
1450
1451 case IPV6_UNICAST_HOPS:
1452 optval = in6p->in6p_hops;
1453 break;
1454
1455 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1456
1457 case IPV6_RECVOPTS:
1458 optval = OPTBIT(IN6P_RECVOPTS);
1459 break;
1460
1461 case IPV6_RECVRETOPTS:
1462 optval = OPTBIT(IN6P_RECVRETOPTS);
1463 break;
1464
1465 case IPV6_RECVDSTADDR:
1466 optval = OPTBIT(IN6P_RECVDSTADDR);
1467 break;
1468
1469 case IPV6_PORTRANGE:
1470 {
1471 int flags;
1472 flags = in6p->in6p_flags;
1473 if (flags & IN6P_HIGHPORT)
1474 optval = IPV6_PORTRANGE_HIGH;
1475 else if (flags & IN6P_LOWPORT)
1476 optval = IPV6_PORTRANGE_LOW;
1477 else
1478 optval = 0;
1479 break;
1480 }
1481
1482 case IPV6_PKTINFO:
1483 optval = OPTBIT(IN6P_PKTINFO);
1484 break;
1485
1486 case IPV6_HOPLIMIT:
1487 optval = OPTBIT(IN6P_HOPLIMIT);
1488 break;
1489
1490 case IPV6_HOPOPTS:
1491 optval = OPTBIT(IN6P_HOPOPTS);
1492 break;
1493
1494 case IPV6_DSTOPTS:
1495 optval = OPTBIT(IN6P_DSTOPTS);
1496 break;
1497
1498 case IPV6_RTHDR:
1499 optval = OPTBIT(IN6P_RTHDR);
1500 break;
1501
1502 case IPV6_FAITH:
1503 optval = OPTBIT(IN6P_FAITH);
1504 break;
1505
1506 case IPV6_V6ONLY:
1507 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1508 break;
1509 }
1510 *mtod(m, int *) = optval;
1511 break;
1512
1513 case IPV6_MULTICAST_IF:
1514 case IPV6_MULTICAST_HOPS:
1515 case IPV6_MULTICAST_LOOP:
1516 case IPV6_JOIN_GROUP:
1517 case IPV6_LEAVE_GROUP:
1518 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1519 break;
1520
1521 #ifdef IPSEC
1522 case IPV6_IPSEC_POLICY:
1523 {
1524 caddr_t req = NULL;
1525 size_t len = 0;
1526
1527 if (m) {
1528 req = mtod(m, caddr_t);
1529 len = m->m_len;
1530 }
1531 error = ipsec6_get_policy(in6p, req, len, mp);
1532 break;
1533 }
1534 #endif /* IPSEC */
1535
1536 default:
1537 error = ENOPROTOOPT;
1538 break;
1539 }
1540 break;
1541 }
1542 } else {
1543 error = EINVAL;
1544 if (op == PRCO_SETOPT && *mp)
1545 (void)m_free(*mp);
1546 }
1547 return(error);
1548 }
1549
1550 /*
1551 * Set up IP6 options in pcb for insertion in output packets.
1552 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1553 * with destination address if source routed.
1554 */
1555 static int
1556 ip6_pcbopts(pktopt, m, so)
1557 struct ip6_pktopts **pktopt;
1558 struct mbuf *m;
1559 struct socket *so;
1560 {
1561 struct ip6_pktopts *opt = *pktopt;
1562 int error = 0;
1563 struct proc *p = curproc; /* XXX */
1564 int priv = 0;
1565
1566 /* turn off any old options. */
1567 if (opt) {
1568 if (opt->ip6po_m)
1569 (void)m_free(opt->ip6po_m);
1570 } else
1571 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1572 *pktopt = 0;
1573
1574 if (!m || m->m_len == 0) {
1575 /*
1576 * Only turning off any previous options.
1577 */
1578 if (opt)
1579 free(opt, M_IP6OPT);
1580 if (m)
1581 (void)m_free(m);
1582 return(0);
1583 }
1584
1585 /* set options specified by user. */
1586 if (p && !suser(p->p_ucred, &p->p_acflag))
1587 priv = 1;
1588 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1589 (void)m_free(m);
1590 return(error);
1591 }
1592 *pktopt = opt;
1593 return(0);
1594 }
1595
1596 /*
1597 * Set the IP6 multicast options in response to user setsockopt().
1598 */
1599 static int
1600 ip6_setmoptions(optname, im6op, m)
1601 int optname;
1602 struct ip6_moptions **im6op;
1603 struct mbuf *m;
1604 {
1605 int error = 0;
1606 u_int loop, ifindex;
1607 struct ipv6_mreq *mreq;
1608 struct ifnet *ifp;
1609 struct ip6_moptions *im6o = *im6op;
1610 struct route_in6 ro;
1611 struct sockaddr_in6 *dst;
1612 struct in6_multi_mship *imm;
1613 struct proc *p = curproc; /* XXX */
1614
1615 if (im6o == NULL) {
1616 /*
1617 * No multicast option buffer attached to the pcb;
1618 * allocate one and initialize to default values.
1619 */
1620 im6o = (struct ip6_moptions *)
1621 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1622
1623 if (im6o == NULL)
1624 return(ENOBUFS);
1625 *im6op = im6o;
1626 im6o->im6o_multicast_ifp = NULL;
1627 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1628 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1629 LIST_INIT(&im6o->im6o_memberships);
1630 }
1631
1632 switch (optname) {
1633
1634 case IPV6_MULTICAST_IF:
1635 /*
1636 * Select the interface for outgoing multicast packets.
1637 */
1638 if (m == NULL || m->m_len != sizeof(u_int)) {
1639 error = EINVAL;
1640 break;
1641 }
1642 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1643 if (ifindex < 0 || if_index < ifindex) {
1644 error = ENXIO; /* XXX EINVAL? */
1645 break;
1646 }
1647 ifp = ifindex2ifnet[ifindex];
1648 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1649 error = EADDRNOTAVAIL;
1650 break;
1651 }
1652 im6o->im6o_multicast_ifp = ifp;
1653 break;
1654
1655 case IPV6_MULTICAST_HOPS:
1656 {
1657 /*
1658 * Set the IP6 hoplimit for outgoing multicast packets.
1659 */
1660 int optval;
1661 if (m == NULL || m->m_len != sizeof(int)) {
1662 error = EINVAL;
1663 break;
1664 }
1665 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1666 if (optval < -1 || optval >= 256)
1667 error = EINVAL;
1668 else if (optval == -1)
1669 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1670 else
1671 im6o->im6o_multicast_hlim = optval;
1672 break;
1673 }
1674
1675 case IPV6_MULTICAST_LOOP:
1676 /*
1677 * Set the loopback flag for outgoing multicast packets.
1678 * Must be zero or one.
1679 */
1680 if (m == NULL || m->m_len != sizeof(u_int)) {
1681 error = EINVAL;
1682 break;
1683 }
1684 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1685 if (loop > 1) {
1686 error = EINVAL;
1687 break;
1688 }
1689 im6o->im6o_multicast_loop = loop;
1690 break;
1691
1692 case IPV6_JOIN_GROUP:
1693 /*
1694 * Add a multicast group membership.
1695 * Group must be a valid IP6 multicast address.
1696 */
1697 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1698 error = EINVAL;
1699 break;
1700 }
1701 mreq = mtod(m, struct ipv6_mreq *);
1702 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1703 /*
1704 * We use the unspecified address to specify to accept
1705 * all multicast addresses. Only super user is allowed
1706 * to do this.
1707 */
1708 if (suser(p->p_ucred, &p->p_acflag))
1709 {
1710 error = EACCES;
1711 break;
1712 }
1713 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1714 error = EINVAL;
1715 break;
1716 }
1717
1718 /*
1719 * If the interface is specified, validate it.
1720 */
1721 if (mreq->ipv6mr_interface < 0
1722 || if_index < mreq->ipv6mr_interface) {
1723 error = ENXIO; /* XXX EINVAL? */
1724 break;
1725 }
1726 /*
1727 * If no interface was explicitly specified, choose an
1728 * appropriate one according to the given multicast address.
1729 */
1730 if (mreq->ipv6mr_interface == 0) {
1731 /*
1732 * If the multicast address is in node-local scope,
1733 * the interface should be a loopback interface.
1734 * Otherwise, look up the routing table for the
1735 * address, and choose the outgoing interface.
1736 * XXX: is it a good approach?
1737 */
1738 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1739 ifp = &loif[0];
1740 } else {
1741 ro.ro_rt = NULL;
1742 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1743 bzero(dst, sizeof(*dst));
1744 dst->sin6_len = sizeof(struct sockaddr_in6);
1745 dst->sin6_family = AF_INET6;
1746 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1747 rtalloc((struct route *)&ro);
1748 if (ro.ro_rt == NULL) {
1749 error = EADDRNOTAVAIL;
1750 break;
1751 }
1752 ifp = ro.ro_rt->rt_ifp;
1753 rtfree(ro.ro_rt);
1754 }
1755 } else
1756 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1757
1758 /*
1759 * See if we found an interface, and confirm that it
1760 * supports multicast
1761 */
1762 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1763 error = EADDRNOTAVAIL;
1764 break;
1765 }
1766 /*
1767 * Put interface index into the multicast address,
1768 * if the address has link-local scope.
1769 */
1770 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1771 mreq->ipv6mr_multiaddr.s6_addr16[1] =
1772 htons(mreq->ipv6mr_interface);
1773 }
1774 /*
1775 * See if the membership already exists.
1776 */
1777 for (imm = im6o->im6o_memberships.lh_first;
1778 imm != NULL; imm = imm->i6mm_chain.le_next)
1779 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1780 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1781 &mreq->ipv6mr_multiaddr))
1782 break;
1783 if (imm != NULL) {
1784 error = EADDRINUSE;
1785 break;
1786 }
1787 /*
1788 * Everything looks good; add a new record to the multicast
1789 * address list for the given interface.
1790 */
1791 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
1792 if (!imm)
1793 break;
1794 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1795 break;
1796
1797 case IPV6_LEAVE_GROUP:
1798 /*
1799 * Drop a multicast group membership.
1800 * Group must be a valid IP6 multicast address.
1801 */
1802 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1803 error = EINVAL;
1804 break;
1805 }
1806 mreq = mtod(m, struct ipv6_mreq *);
1807 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1808 if (suser(p->p_ucred, &p->p_acflag))
1809 {
1810 error = EACCES;
1811 break;
1812 }
1813 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1814 error = EINVAL;
1815 break;
1816 }
1817 /*
1818 * If an interface address was specified, get a pointer
1819 * to its ifnet structure.
1820 */
1821 if (mreq->ipv6mr_interface < 0
1822 || if_index < mreq->ipv6mr_interface) {
1823 error = ENXIO; /* XXX EINVAL? */
1824 break;
1825 }
1826 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1827 /*
1828 * Put interface index into the multicast address,
1829 * if the address has link-local scope.
1830 */
1831 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1832 mreq->ipv6mr_multiaddr.s6_addr16[1] =
1833 htons(mreq->ipv6mr_interface);
1834 }
1835 /*
1836 * Find the membership in the membership list.
1837 */
1838 for (imm = im6o->im6o_memberships.lh_first;
1839 imm != NULL; imm = imm->i6mm_chain.le_next) {
1840 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
1841 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1842 &mreq->ipv6mr_multiaddr))
1843 break;
1844 }
1845 if (imm == NULL) {
1846 /* Unable to resolve interface */
1847 error = EADDRNOTAVAIL;
1848 break;
1849 }
1850 /*
1851 * Give up the multicast address record to which the
1852 * membership points.
1853 */
1854 LIST_REMOVE(imm, i6mm_chain);
1855 in6_leavegroup(imm);
1856 break;
1857
1858 default:
1859 error = EOPNOTSUPP;
1860 break;
1861 }
1862
1863 /*
1864 * If all options have default values, no need to keep the mbuf.
1865 */
1866 if (im6o->im6o_multicast_ifp == NULL &&
1867 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1868 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1869 im6o->im6o_memberships.lh_first == NULL) {
1870 free(*im6op, M_IPMOPTS);
1871 *im6op = NULL;
1872 }
1873
1874 return(error);
1875 }
1876
1877 /*
1878 * Return the IP6 multicast options in response to user getsockopt().
1879 */
1880 static int
1881 ip6_getmoptions(optname, im6o, mp)
1882 int optname;
1883 struct ip6_moptions *im6o;
1884 struct mbuf **mp;
1885 {
1886 u_int *hlim, *loop, *ifindex;
1887
1888 *mp = m_get(M_WAIT, MT_SOOPTS);
1889
1890 switch (optname) {
1891
1892 case IPV6_MULTICAST_IF:
1893 ifindex = mtod(*mp, u_int *);
1894 (*mp)->m_len = sizeof(u_int);
1895 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1896 *ifindex = 0;
1897 else
1898 *ifindex = im6o->im6o_multicast_ifp->if_index;
1899 return(0);
1900
1901 case IPV6_MULTICAST_HOPS:
1902 hlim = mtod(*mp, u_int *);
1903 (*mp)->m_len = sizeof(u_int);
1904 if (im6o == NULL)
1905 *hlim = ip6_defmcasthlim;
1906 else
1907 *hlim = im6o->im6o_multicast_hlim;
1908 return(0);
1909
1910 case IPV6_MULTICAST_LOOP:
1911 loop = mtod(*mp, u_int *);
1912 (*mp)->m_len = sizeof(u_int);
1913 if (im6o == NULL)
1914 *loop = ip6_defmcasthlim;
1915 else
1916 *loop = im6o->im6o_multicast_loop;
1917 return(0);
1918
1919 default:
1920 return(EOPNOTSUPP);
1921 }
1922 }
1923
1924 /*
1925 * Discard the IP6 multicast options.
1926 */
1927 void
1928 ip6_freemoptions(im6o)
1929 struct ip6_moptions *im6o;
1930 {
1931 struct in6_multi_mship *imm;
1932
1933 if (im6o == NULL)
1934 return;
1935
1936 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
1937 LIST_REMOVE(imm, i6mm_chain);
1938 in6_leavegroup(imm);
1939 }
1940 free(im6o, M_IPMOPTS);
1941 }
1942
1943 /*
1944 * Set IPv6 outgoing packet options based on advanced API.
1945 */
1946 int
1947 ip6_setpktoptions(control, opt, priv)
1948 struct mbuf *control;
1949 struct ip6_pktopts *opt;
1950 int priv;
1951 {
1952 struct cmsghdr *cm = 0;
1953
1954 if (control == 0 || opt == 0)
1955 return(EINVAL);
1956
1957 bzero(opt, sizeof(*opt));
1958 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
1959
1960 /*
1961 * XXX: Currently, we assume all the optional information is stored
1962 * in a single mbuf.
1963 */
1964 if (control->m_next)
1965 return(EINVAL);
1966
1967 opt->ip6po_m = control;
1968
1969 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
1970 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1971 cm = mtod(control, struct cmsghdr *);
1972 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
1973 return(EINVAL);
1974 if (cm->cmsg_level != IPPROTO_IPV6)
1975 continue;
1976
1977 switch (cm->cmsg_type) {
1978 case IPV6_PKTINFO:
1979 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
1980 return(EINVAL);
1981 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1982 if (opt->ip6po_pktinfo->ipi6_ifindex &&
1983 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
1984 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
1985 htons(opt->ip6po_pktinfo->ipi6_ifindex);
1986
1987 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index ||
1988 opt->ip6po_pktinfo->ipi6_ifindex < 0) {
1989 return(ENXIO);
1990 }
1991
1992 /*
1993 * Check if the requested source address is indeed a
1994 * unicast address assigned to the node, and can be
1995 * used as the packet's source address.
1996 */
1997 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
1998 struct ifaddr *ia;
1999 struct in6_ifaddr *ia6;
2000 struct sockaddr_in6 sin6;
2001
2002 bzero(&sin6, sizeof(sin6));
2003 sin6.sin6_len = sizeof(sin6);
2004 sin6.sin6_family = AF_INET6;
2005 sin6.sin6_addr =
2006 opt->ip6po_pktinfo->ipi6_addr;
2007 ia = ifa_ifwithaddr(sin6tosa(&sin6));
2008 if (ia == NULL ||
2009 (opt->ip6po_pktinfo->ipi6_ifindex &&
2010 (ia->ifa_ifp->if_index !=
2011 opt->ip6po_pktinfo->ipi6_ifindex))) {
2012 return(EADDRNOTAVAIL);
2013 }
2014 ia6 = (struct in6_ifaddr *)ia;
2015 if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) {
2016 return(EADDRNOTAVAIL);
2017 }
2018
2019 /*
2020 * Check if the requested source address is
2021 * indeed a unicast address assigned to the
2022 * node.
2023 */
2024 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
2025 return(EADDRNOTAVAIL);
2026 }
2027 break;
2028
2029 case IPV6_HOPLIMIT:
2030 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2031 return(EINVAL);
2032
2033 bcopy(CMSG_DATA(cm), &opt->ip6po_hlim,
2034 sizeof(opt->ip6po_hlim));
2035 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2036 return(EINVAL);
2037 break;
2038
2039 case IPV6_NEXTHOP:
2040 if (!priv)
2041 return(EPERM);
2042
2043 /* check if cmsg_len is large enough for sa_len */
2044 if (cm->cmsg_len < sizeof(u_char) ||
2045 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2046 return(EINVAL);
2047
2048 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2049
2050 break;
2051
2052 case IPV6_HOPOPTS:
2053 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2054 return(EINVAL);
2055 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2056 if (cm->cmsg_len !=
2057 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
2058 return(EINVAL);
2059 break;
2060
2061 case IPV6_DSTOPTS:
2062 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2063 return(EINVAL);
2064
2065 /*
2066 * If there is no routing header yet, the destination
2067 * options header should be put on the 1st part.
2068 * Otherwise, the header should be on the 2nd part.
2069 * (See RFC 2460, section 4.1)
2070 */
2071 if (opt->ip6po_rthdr == NULL) {
2072 opt->ip6po_dest1 =
2073 (struct ip6_dest *)CMSG_DATA(cm);
2074 if (cm->cmsg_len !=
2075 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1) << 3));
2076 return(EINVAL);
2077 }
2078 else {
2079 opt->ip6po_dest2 =
2080 (struct ip6_dest *)CMSG_DATA(cm);
2081 if (cm->cmsg_len !=
2082 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1) << 3))
2083 return(EINVAL);
2084 }
2085 break;
2086
2087 case IPV6_RTHDR:
2088 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2089 return(EINVAL);
2090 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
2091 if (cm->cmsg_len !=
2092 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
2093 return(EINVAL);
2094 switch (opt->ip6po_rthdr->ip6r_type) {
2095 case IPV6_RTHDR_TYPE_0:
2096 if (opt->ip6po_rthdr->ip6r_segleft == 0)
2097 return(EINVAL);
2098 break;
2099 default:
2100 return(EINVAL);
2101 }
2102 break;
2103
2104 default:
2105 return(ENOPROTOOPT);
2106 }
2107 }
2108
2109 return(0);
2110 }
2111
2112 /*
2113 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2114 * packet to the input queue of a specified interface. Note that this
2115 * calls the output routine of the loopback "driver", but with an interface
2116 * pointer that might NOT be &loif -- easier than replicating that code here.
2117 */
2118 void
2119 ip6_mloopback(ifp, m, dst)
2120 struct ifnet *ifp;
2121 struct mbuf *m;
2122 struct sockaddr_in6 *dst;
2123 {
2124 struct mbuf *copym;
2125 struct ip6_hdr *ip6;
2126
2127 copym = m_copy(m, 0, M_COPYALL);
2128 if (copym == NULL)
2129 return;
2130
2131 /*
2132 * Make sure to deep-copy IPv6 header portion in case the data
2133 * is in an mbuf cluster, so that we can safely override the IPv6
2134 * header portion later.
2135 */
2136 if ((copym->m_flags & M_EXT) != 0 ||
2137 copym->m_len < sizeof(struct ip6_hdr)) {
2138 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2139 if (copym == NULL)
2140 return;
2141 }
2142
2143 #ifdef DIAGNOSTIC
2144 if (copym->m_len < sizeof(*ip6)) {
2145 m_freem(copym);
2146 return;
2147 }
2148 #endif
2149
2150 ip6 = mtod(copym, struct ip6_hdr *);
2151 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
2152 ip6->ip6_src.s6_addr16[1] = 0;
2153 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
2154 ip6->ip6_dst.s6_addr16[1] = 0;
2155
2156 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2157 }
2158
2159 /*
2160 * Chop IPv6 header off from the payload.
2161 */
2162 static int
2163 ip6_splithdr(m, exthdrs)
2164 struct mbuf *m;
2165 struct ip6_exthdrs *exthdrs;
2166 {
2167 struct mbuf *mh;
2168 struct ip6_hdr *ip6;
2169
2170 ip6 = mtod(m, struct ip6_hdr *);
2171 if (m->m_len > sizeof(*ip6)) {
2172 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2173 if (mh == 0) {
2174 m_freem(m);
2175 return ENOBUFS;
2176 }
2177 M_COPY_PKTHDR(mh, m);
2178 MH_ALIGN(mh, sizeof(*ip6));
2179 m->m_flags &= ~M_PKTHDR;
2180 m->m_len -= sizeof(*ip6);
2181 m->m_data += sizeof(*ip6);
2182 mh->m_next = m;
2183 m = mh;
2184 m->m_len = sizeof(*ip6);
2185 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2186 }
2187 exthdrs->ip6e_ip6 = m;
2188 return 0;
2189 }
2190
2191 /*
2192 * Compute IPv6 extension header length.
2193 */
2194 int
2195 ip6_optlen(in6p)
2196 struct in6pcb *in6p;
2197 {
2198 int len;
2199
2200 if (!in6p->in6p_outputopts)
2201 return 0;
2202
2203 len = 0;
2204 #define elen(x) \
2205 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2206
2207 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2208 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2209 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2210 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2211 return len;
2212 #undef elen
2213 }
2214