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