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