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