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