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