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