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