ip6_output.c revision 1.44.4.4 1 /* $NetBSD: ip6_output.c,v 1.44.4.4 2004/02/07 20:06:04 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.4 2004/02/07 20:06:04 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 if (!m || m->m_len != sizeof(int)) {
1352 error = EINVAL;
1353 break;
1354 }
1355 optval = *mtod(m, int *);
1356 switch (optname) {
1357
1358 case IPV6_UNICAST_HOPS:
1359 if (optval < -1 || optval >= 256)
1360 error = EINVAL;
1361 else {
1362 /* -1 = kernel default */
1363 in6p->in6p_hops = optval;
1364 }
1365 break;
1366 #define OPTSET(bit) \
1367 if (optval) \
1368 in6p->in6p_flags |= bit; \
1369 else \
1370 in6p->in6p_flags &= ~bit;
1371
1372 case IPV6_RECVOPTS:
1373 OPTSET(IN6P_RECVOPTS);
1374 break;
1375
1376 case IPV6_RECVRETOPTS:
1377 OPTSET(IN6P_RECVRETOPTS);
1378 break;
1379
1380 case IPV6_RECVDSTADDR:
1381 OPTSET(IN6P_RECVDSTADDR);
1382 break;
1383
1384 case IPV6_PKTINFO:
1385 OPTSET(IN6P_PKTINFO);
1386 break;
1387
1388 case IPV6_HOPLIMIT:
1389 OPTSET(IN6P_HOPLIMIT);
1390 break;
1391
1392 case IPV6_HOPOPTS:
1393 OPTSET(IN6P_HOPOPTS);
1394 break;
1395
1396 case IPV6_DSTOPTS:
1397 OPTSET(IN6P_DSTOPTS);
1398 break;
1399
1400 case IPV6_RTHDR:
1401 OPTSET(IN6P_RTHDR);
1402 break;
1403
1404 case IPV6_FAITH:
1405 OPTSET(IN6P_FAITH);
1406 break;
1407
1408 case IPV6_V6ONLY:
1409 /*
1410 * make setsockopt(IPV6_V6ONLY)
1411 * available only prior to bind(2).
1412 * see ipng mailing list, Jun 22 2001.
1413 */
1414 if (in6p->in6p_lport ||
1415 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1416 {
1417 error = EINVAL;
1418 break;
1419 }
1420 #ifdef INET6_BINDV6ONLY
1421 if (!optval)
1422 error = EINVAL;
1423 #else
1424 OPTSET(IN6P_IPV6_V6ONLY);
1425 #endif
1426 break;
1427 }
1428 break;
1429 #undef OPTSET
1430
1431 case IPV6_MULTICAST_IF:
1432 case IPV6_MULTICAST_HOPS:
1433 case IPV6_MULTICAST_LOOP:
1434 case IPV6_JOIN_GROUP:
1435 case IPV6_LEAVE_GROUP:
1436 error = ip6_setmoptions(optname, &in6p->in6p_moptions, m);
1437 break;
1438
1439 case IPV6_PORTRANGE:
1440 optval = *mtod(m, int *);
1441
1442 switch (optval) {
1443 case IPV6_PORTRANGE_DEFAULT:
1444 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1445 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1446 break;
1447
1448 case IPV6_PORTRANGE_HIGH:
1449 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1450 in6p->in6p_flags |= IN6P_HIGHPORT;
1451 break;
1452
1453 case IPV6_PORTRANGE_LOW:
1454 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1455 in6p->in6p_flags |= IN6P_LOWPORT;
1456 break;
1457
1458 default:
1459 error = EINVAL;
1460 break;
1461 }
1462 break;
1463
1464 #ifdef IPSEC
1465 case IPV6_IPSEC_POLICY:
1466 {
1467 caddr_t req = NULL;
1468 size_t len = 0;
1469
1470 int priv = 0;
1471 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1472 priv = 0;
1473 else
1474 priv = 1;
1475 if (m) {
1476 req = mtod(m, caddr_t);
1477 len = m->m_len;
1478 }
1479 error = ipsec6_set_policy(in6p,
1480 optname, req, len, priv);
1481 }
1482 break;
1483 #endif /* IPSEC */
1484
1485 default:
1486 error = ENOPROTOOPT;
1487 break;
1488 }
1489 if (m)
1490 (void)m_free(m);
1491 break;
1492
1493 case PRCO_GETOPT:
1494 switch (optname) {
1495
1496 case IPV6_OPTIONS:
1497 case IPV6_RETOPTS:
1498 #if 0
1499 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1500 if (in6p->in6p_options) {
1501 m->m_len = in6p->in6p_options->m_len;
1502 bcopy(mtod(in6p->in6p_options, caddr_t),
1503 mtod(m, caddr_t),
1504 (unsigned)m->m_len);
1505 } else
1506 m->m_len = 0;
1507 break;
1508 #else
1509 error = ENOPROTOOPT;
1510 break;
1511 #endif
1512
1513 case IPV6_PKTOPTIONS:
1514 if (in6p->in6p_options) {
1515 *mp = m_copym(in6p->in6p_options, 0,
1516 M_COPYALL, M_WAIT);
1517 } else {
1518 *mp = m_get(M_WAIT, MT_SOOPTS);
1519 (*mp)->m_len = 0;
1520 }
1521 break;
1522
1523 case IPV6_HOPOPTS:
1524 case IPV6_DSTOPTS:
1525 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
1526 error = EPERM;
1527 break;
1528 }
1529 /* fall through */
1530 case IPV6_UNICAST_HOPS:
1531 case IPV6_RECVOPTS:
1532 case IPV6_RECVRETOPTS:
1533 case IPV6_RECVDSTADDR:
1534 case IPV6_PORTRANGE:
1535 case IPV6_PKTINFO:
1536 case IPV6_HOPLIMIT:
1537 case IPV6_RTHDR:
1538 case IPV6_FAITH:
1539 case IPV6_V6ONLY:
1540 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1541 m->m_len = sizeof(int);
1542 switch (optname) {
1543
1544 case IPV6_UNICAST_HOPS:
1545 optval = in6p->in6p_hops;
1546 break;
1547
1548 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
1549
1550 case IPV6_RECVOPTS:
1551 optval = OPTBIT(IN6P_RECVOPTS);
1552 break;
1553
1554 case IPV6_RECVRETOPTS:
1555 optval = OPTBIT(IN6P_RECVRETOPTS);
1556 break;
1557
1558 case IPV6_RECVDSTADDR:
1559 optval = OPTBIT(IN6P_RECVDSTADDR);
1560 break;
1561
1562 case IPV6_PORTRANGE:
1563 {
1564 int flags;
1565 flags = in6p->in6p_flags;
1566 if (flags & IN6P_HIGHPORT)
1567 optval = IPV6_PORTRANGE_HIGH;
1568 else if (flags & IN6P_LOWPORT)
1569 optval = IPV6_PORTRANGE_LOW;
1570 else
1571 optval = 0;
1572 break;
1573 }
1574
1575 case IPV6_PKTINFO:
1576 optval = OPTBIT(IN6P_PKTINFO);
1577 break;
1578
1579 case IPV6_HOPLIMIT:
1580 optval = OPTBIT(IN6P_HOPLIMIT);
1581 break;
1582
1583 case IPV6_HOPOPTS:
1584 optval = OPTBIT(IN6P_HOPOPTS);
1585 break;
1586
1587 case IPV6_DSTOPTS:
1588 optval = OPTBIT(IN6P_DSTOPTS);
1589 break;
1590
1591 case IPV6_RTHDR:
1592 optval = OPTBIT(IN6P_RTHDR);
1593 break;
1594
1595 case IPV6_FAITH:
1596 optval = OPTBIT(IN6P_FAITH);
1597 break;
1598
1599 case IPV6_V6ONLY:
1600 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1601 break;
1602 }
1603 *mtod(m, int *) = optval;
1604 break;
1605
1606 case IPV6_MULTICAST_IF:
1607 case IPV6_MULTICAST_HOPS:
1608 case IPV6_MULTICAST_LOOP:
1609 case IPV6_JOIN_GROUP:
1610 case IPV6_LEAVE_GROUP:
1611 error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
1612 break;
1613
1614 #ifdef IPSEC
1615 case IPV6_IPSEC_POLICY:
1616 {
1617 caddr_t req = NULL;
1618 size_t len = 0;
1619
1620 if (m) {
1621 req = mtod(m, caddr_t);
1622 len = m->m_len;
1623 }
1624 error = ipsec6_get_policy(in6p, req, len, mp);
1625 break;
1626 }
1627 #endif /* IPSEC */
1628
1629 default:
1630 error = ENOPROTOOPT;
1631 break;
1632 }
1633 break;
1634 }
1635 } else {
1636 error = EINVAL;
1637 if (op == PRCO_SETOPT && *mp)
1638 (void)m_free(*mp);
1639 }
1640 return(error);
1641 }
1642
1643 /*
1644 * Set up IP6 options in pcb for insertion in output packets.
1645 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1646 * with destination address if source routed.
1647 */
1648 static int
1649 ip6_pcbopts(pktopt, m, so)
1650 struct ip6_pktopts **pktopt;
1651 struct mbuf *m;
1652 struct socket *so;
1653 {
1654 struct ip6_pktopts *opt = *pktopt;
1655 int error = 0;
1656 struct proc *p = curproc; /* XXX */
1657 int priv = 0;
1658
1659 /* turn off any old options. */
1660 if (opt) {
1661 if (opt->ip6po_m)
1662 (void)m_free(opt->ip6po_m);
1663 } else
1664 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1665 *pktopt = 0;
1666
1667 if (!m || m->m_len == 0) {
1668 /*
1669 * Only turning off any previous options.
1670 */
1671 if (opt)
1672 free(opt, M_IP6OPT);
1673 if (m)
1674 (void)m_free(m);
1675 return(0);
1676 }
1677
1678 /* set options specified by user. */
1679 if (p && !suser(p->p_ucred, &p->p_acflag))
1680 priv = 1;
1681 if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
1682 (void)m_free(m);
1683 return(error);
1684 }
1685 *pktopt = opt;
1686 return(0);
1687 }
1688
1689 /*
1690 * Set the IP6 multicast options in response to user setsockopt().
1691 */
1692 static int
1693 ip6_setmoptions(optname, im6op, m)
1694 int optname;
1695 struct ip6_moptions **im6op;
1696 struct mbuf *m;
1697 {
1698 int error = 0;
1699 u_int loop, ifindex;
1700 struct ipv6_mreq *mreq;
1701 struct ifnet *ifp;
1702 struct ip6_moptions *im6o = *im6op;
1703 struct route_in6 ro;
1704 struct sockaddr_in6 *dst;
1705 struct in6_multi_mship *imm;
1706 struct proc *p = curproc; /* XXX */
1707
1708 if (im6o == NULL) {
1709 /*
1710 * No multicast option buffer attached to the pcb;
1711 * allocate one and initialize to default values.
1712 */
1713 im6o = (struct ip6_moptions *)
1714 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1715
1716 if (im6o == NULL)
1717 return(ENOBUFS);
1718 *im6op = im6o;
1719 im6o->im6o_multicast_ifp = NULL;
1720 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1721 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1722 LIST_INIT(&im6o->im6o_memberships);
1723 }
1724
1725 switch (optname) {
1726
1727 case IPV6_MULTICAST_IF:
1728 /*
1729 * Select the interface for outgoing multicast packets.
1730 */
1731 if (m == NULL || m->m_len != sizeof(u_int)) {
1732 error = EINVAL;
1733 break;
1734 }
1735 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1736 if (ifindex < 0 || if_index < ifindex) {
1737 error = ENXIO; /* XXX EINVAL? */
1738 break;
1739 }
1740 ifp = ifindex2ifnet[ifindex];
1741 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1742 error = EADDRNOTAVAIL;
1743 break;
1744 }
1745 im6o->im6o_multicast_ifp = ifp;
1746 break;
1747
1748 case IPV6_MULTICAST_HOPS:
1749 {
1750 /*
1751 * Set the IP6 hoplimit for outgoing multicast packets.
1752 */
1753 int optval;
1754 if (m == NULL || m->m_len != sizeof(int)) {
1755 error = EINVAL;
1756 break;
1757 }
1758 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1759 if (optval < -1 || optval >= 256)
1760 error = EINVAL;
1761 else if (optval == -1)
1762 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1763 else
1764 im6o->im6o_multicast_hlim = optval;
1765 break;
1766 }
1767
1768 case IPV6_MULTICAST_LOOP:
1769 /*
1770 * Set the loopback flag for outgoing multicast packets.
1771 * Must be zero or one.
1772 */
1773 if (m == NULL || m->m_len != sizeof(u_int)) {
1774 error = EINVAL;
1775 break;
1776 }
1777 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
1778 if (loop > 1) {
1779 error = EINVAL;
1780 break;
1781 }
1782 im6o->im6o_multicast_loop = loop;
1783 break;
1784
1785 case IPV6_JOIN_GROUP:
1786 /*
1787 * Add a multicast group membership.
1788 * Group must be a valid IP6 multicast address.
1789 */
1790 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1791 error = EINVAL;
1792 break;
1793 }
1794 mreq = mtod(m, struct ipv6_mreq *);
1795 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1796 /*
1797 * We use the unspecified address to specify to accept
1798 * all multicast addresses. Only super user is allowed
1799 * to do this.
1800 */
1801 if (suser(p->p_ucred, &p->p_acflag))
1802 {
1803 error = EACCES;
1804 break;
1805 }
1806 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1807 error = EINVAL;
1808 break;
1809 }
1810
1811 /*
1812 * If the interface is specified, validate it.
1813 */
1814 if (mreq->ipv6mr_interface < 0
1815 || if_index < mreq->ipv6mr_interface) {
1816 error = ENXIO; /* XXX EINVAL? */
1817 break;
1818 }
1819 /*
1820 * If no interface was explicitly specified, choose an
1821 * appropriate one according to the given multicast address.
1822 */
1823 if (mreq->ipv6mr_interface == 0) {
1824 /*
1825 * If the multicast address is in node-local scope,
1826 * the interface should be a loopback interface.
1827 * Otherwise, look up the routing table for the
1828 * address, and choose the outgoing interface.
1829 * XXX: is it a good approach?
1830 */
1831 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
1832 ifp = &loif[0];
1833 } else {
1834 ro.ro_rt = NULL;
1835 dst = (struct sockaddr_in6 *)&ro.ro_dst;
1836 bzero(dst, sizeof(*dst));
1837 dst->sin6_len = sizeof(struct sockaddr_in6);
1838 dst->sin6_family = AF_INET6;
1839 dst->sin6_addr = mreq->ipv6mr_multiaddr;
1840 rtalloc((struct route *)&ro);
1841 if (ro.ro_rt == NULL) {
1842 error = EADDRNOTAVAIL;
1843 break;
1844 }
1845 ifp = ro.ro_rt->rt_ifp;
1846 rtfree(ro.ro_rt);
1847 }
1848 } else
1849 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1850
1851 /*
1852 * See if we found an interface, and confirm that it
1853 * supports multicast
1854 */
1855 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1856 error = EADDRNOTAVAIL;
1857 break;
1858 }
1859 /*
1860 * Put interface index into the multicast address,
1861 * if the address has link-local scope.
1862 */
1863 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1864 mreq->ipv6mr_multiaddr.s6_addr16[1]
1865 = htons(mreq->ipv6mr_interface);
1866 }
1867 /*
1868 * See if the membership already exists.
1869 */
1870 for (imm = im6o->im6o_memberships.lh_first;
1871 imm != NULL; imm = imm->i6mm_chain.le_next)
1872 if (imm->i6mm_maddr->in6m_ifp == ifp &&
1873 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1874 &mreq->ipv6mr_multiaddr))
1875 break;
1876 if (imm != NULL) {
1877 error = EADDRINUSE;
1878 break;
1879 }
1880 /*
1881 * Everything looks good; add a new record to the multicast
1882 * address list for the given interface.
1883 */
1884 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
1885 if (!imm)
1886 break;
1887 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1888 break;
1889
1890 case IPV6_LEAVE_GROUP:
1891 /*
1892 * Drop a multicast group membership.
1893 * Group must be a valid IP6 multicast address.
1894 */
1895 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1896 error = EINVAL;
1897 break;
1898 }
1899 mreq = mtod(m, struct ipv6_mreq *);
1900 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1901 if (suser(p->p_ucred, &p->p_acflag))
1902 {
1903 error = EACCES;
1904 break;
1905 }
1906 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1907 error = EINVAL;
1908 break;
1909 }
1910 /*
1911 * If an interface address was specified, get a pointer
1912 * to its ifnet structure.
1913 */
1914 if (mreq->ipv6mr_interface < 0
1915 || if_index < mreq->ipv6mr_interface) {
1916 error = ENXIO; /* XXX EINVAL? */
1917 break;
1918 }
1919 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
1920 /*
1921 * Put interface index into the multicast address,
1922 * if the address has link-local scope.
1923 */
1924 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
1925 mreq->ipv6mr_multiaddr.s6_addr16[1]
1926 = htons(mreq->ipv6mr_interface);
1927 }
1928 /*
1929 * Find the membership in the membership list.
1930 */
1931 for (imm = im6o->im6o_memberships.lh_first;
1932 imm != NULL; imm = imm->i6mm_chain.le_next) {
1933 if ((ifp == NULL ||
1934 imm->i6mm_maddr->in6m_ifp == ifp) &&
1935 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
1936 &mreq->ipv6mr_multiaddr))
1937 break;
1938 }
1939 if (imm == NULL) {
1940 /* Unable to resolve interface */
1941 error = EADDRNOTAVAIL;
1942 break;
1943 }
1944 /*
1945 * Give up the multicast address record to which the
1946 * membership points.
1947 */
1948 LIST_REMOVE(imm, i6mm_chain);
1949 in6_leavegroup(imm);
1950 break;
1951
1952 default:
1953 error = EOPNOTSUPP;
1954 break;
1955 }
1956
1957 /*
1958 * If all options have default values, no need to keep the mbuf.
1959 */
1960 if (im6o->im6o_multicast_ifp == NULL &&
1961 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
1962 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
1963 im6o->im6o_memberships.lh_first == NULL) {
1964 free(*im6op, M_IPMOPTS);
1965 *im6op = NULL;
1966 }
1967
1968 return(error);
1969 }
1970
1971 /*
1972 * Return the IP6 multicast options in response to user getsockopt().
1973 */
1974 static int
1975 ip6_getmoptions(optname, im6o, mp)
1976 int optname;
1977 struct ip6_moptions *im6o;
1978 struct mbuf **mp;
1979 {
1980 u_int *hlim, *loop, *ifindex;
1981
1982 *mp = m_get(M_WAIT, MT_SOOPTS);
1983
1984 switch (optname) {
1985
1986 case IPV6_MULTICAST_IF:
1987 ifindex = mtod(*mp, u_int *);
1988 (*mp)->m_len = sizeof(u_int);
1989 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
1990 *ifindex = 0;
1991 else
1992 *ifindex = im6o->im6o_multicast_ifp->if_index;
1993 return(0);
1994
1995 case IPV6_MULTICAST_HOPS:
1996 hlim = mtod(*mp, u_int *);
1997 (*mp)->m_len = sizeof(u_int);
1998 if (im6o == NULL)
1999 *hlim = ip6_defmcasthlim;
2000 else
2001 *hlim = im6o->im6o_multicast_hlim;
2002 return(0);
2003
2004 case IPV6_MULTICAST_LOOP:
2005 loop = mtod(*mp, u_int *);
2006 (*mp)->m_len = sizeof(u_int);
2007 if (im6o == NULL)
2008 *loop = ip6_defmcasthlim;
2009 else
2010 *loop = im6o->im6o_multicast_loop;
2011 return(0);
2012
2013 default:
2014 return(EOPNOTSUPP);
2015 }
2016 }
2017
2018 /*
2019 * Discard the IP6 multicast options.
2020 */
2021 void
2022 ip6_freemoptions(im6o)
2023 struct ip6_moptions *im6o;
2024 {
2025 struct in6_multi_mship *imm;
2026
2027 if (im6o == NULL)
2028 return;
2029
2030 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2031 LIST_REMOVE(imm, i6mm_chain);
2032 in6_leavegroup(imm);
2033 }
2034 free(im6o, M_IPMOPTS);
2035 }
2036
2037 /*
2038 * Set IPv6 outgoing packet options based on advanced API.
2039 */
2040 int
2041 ip6_setpktoptions(control, opt, priv)
2042 struct mbuf *control;
2043 struct ip6_pktopts *opt;
2044 int priv;
2045 {
2046 struct cmsghdr *cm = 0;
2047
2048 if (control == 0 || opt == 0)
2049 return(EINVAL);
2050
2051 bzero(opt, sizeof(*opt));
2052 opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
2053
2054 /*
2055 * XXX: Currently, we assume all the optional information is stored
2056 * in a single mbuf.
2057 */
2058 if (control->m_next)
2059 return(EINVAL);
2060
2061 opt->ip6po_m = control;
2062
2063 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2064 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2065 cm = mtod(control, struct cmsghdr *);
2066 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2067 return(EINVAL);
2068 if (cm->cmsg_level != IPPROTO_IPV6)
2069 continue;
2070
2071 switch (cm->cmsg_type) {
2072 case IPV6_PKTINFO:
2073 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2074 return(EINVAL);
2075 opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
2076 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2077 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2078 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2079 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2080
2081 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2082 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2083 return(ENXIO);
2084 }
2085
2086 /*
2087 * Check if the requested source address is indeed a
2088 * unicast address assigned to the node, and can be
2089 * used as the packet's source address.
2090 */
2091 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2092 struct ifaddr *ia;
2093 struct in6_ifaddr *ia6;
2094 struct sockaddr_in6 sin6;
2095
2096 bzero(&sin6, sizeof(sin6));
2097 sin6.sin6_len = sizeof(sin6);
2098 sin6.sin6_family = AF_INET6;
2099 sin6.sin6_addr =
2100 opt->ip6po_pktinfo->ipi6_addr;
2101 ia = ifa_ifwithaddr(sin6tosa(&sin6));
2102 if (ia == NULL ||
2103 (opt->ip6po_pktinfo->ipi6_ifindex &&
2104 (ia->ifa_ifp->if_index !=
2105 opt->ip6po_pktinfo->ipi6_ifindex))) {
2106 return(EADDRNOTAVAIL);
2107 }
2108 ia6 = (struct in6_ifaddr *)ia;
2109 if ((ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) != 0) {
2110 return(EADDRNOTAVAIL);
2111 }
2112
2113 /*
2114 * Check if the requested source address is
2115 * indeed a unicast address assigned to the
2116 * node.
2117 */
2118 if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
2119 return(EADDRNOTAVAIL);
2120 }
2121 break;
2122
2123 case IPV6_HOPLIMIT:
2124 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2125 return(EINVAL);
2126
2127 bcopy(CMSG_DATA(cm), &opt->ip6po_hlim,
2128 sizeof(opt->ip6po_hlim));
2129 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2130 return(EINVAL);
2131 break;
2132
2133 case IPV6_NEXTHOP:
2134 if (!priv)
2135 return(EPERM);
2136
2137 if (cm->cmsg_len < sizeof(u_char) ||
2138 /* check if cmsg_len is large enough for sa_len */
2139 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2140 return(EINVAL);
2141
2142 opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
2143
2144 break;
2145
2146 case IPV6_HOPOPTS:
2147 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2148 return(EINVAL);
2149 opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2150 if (cm->cmsg_len !=
2151 CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
2152 return(EINVAL);
2153 break;
2154
2155 case IPV6_DSTOPTS:
2156 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2157 return(EINVAL);
2158
2159 /*
2160 * If there is no routing header yet, the destination
2161 * options header should be put on the 1st part.
2162 * Otherwise, the header should be on the 2nd part.
2163 * (See RFC 2460, section 4.1)
2164 */
2165 if (opt->ip6po_rthdr == NULL) {
2166 opt->ip6po_dest1 =
2167 (struct ip6_dest *)CMSG_DATA(cm);
2168 if (cm->cmsg_len !=
2169 CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
2170 << 3))
2171 return(EINVAL);
2172 }
2173 else {
2174 opt->ip6po_dest2 =
2175 (struct ip6_dest *)CMSG_DATA(cm);
2176 if (cm->cmsg_len !=
2177 CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
2178 << 3))
2179 return(EINVAL);
2180 }
2181 break;
2182
2183 case IPV6_RTHDR:
2184 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2185 return(EINVAL);
2186 opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
2187 if (cm->cmsg_len !=
2188 CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
2189 return(EINVAL);
2190 switch (opt->ip6po_rthdr->ip6r_type) {
2191 case IPV6_RTHDR_TYPE_0:
2192 if (opt->ip6po_rthdr->ip6r_segleft == 0)
2193 return(EINVAL);
2194 break;
2195 default:
2196 return(EINVAL);
2197 }
2198 break;
2199
2200 default:
2201 return(ENOPROTOOPT);
2202 }
2203 }
2204
2205 return(0);
2206 }
2207
2208 /*
2209 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2210 * packet to the input queue of a specified interface. Note that this
2211 * calls the output routine of the loopback "driver", but with an interface
2212 * pointer that might NOT be &loif -- easier than replicating that code here.
2213 */
2214 void
2215 ip6_mloopback(ifp, m, dst)
2216 struct ifnet *ifp;
2217 struct mbuf *m;
2218 struct sockaddr_in6 *dst;
2219 {
2220 struct mbuf *copym;
2221 struct ip6_hdr *ip6;
2222
2223 copym = m_copy(m, 0, M_COPYALL);
2224 if (copym == NULL)
2225 return;
2226
2227 /*
2228 * Make sure to deep-copy IPv6 header portion in case the data
2229 * is in an mbuf cluster, so that we can safely override the IPv6
2230 * header portion later.
2231 */
2232 if ((copym->m_flags & M_EXT) != 0 ||
2233 copym->m_len < sizeof(struct ip6_hdr)) {
2234 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2235 if (copym == NULL)
2236 return;
2237 }
2238
2239 #ifdef DIAGNOSTIC
2240 if (copym->m_len < sizeof(*ip6)) {
2241 m_freem(copym);
2242 return;
2243 }
2244 #endif
2245
2246 ip6 = mtod(copym, struct ip6_hdr *);
2247 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
2248 ip6->ip6_src.s6_addr16[1] = 0;
2249 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
2250 ip6->ip6_dst.s6_addr16[1] = 0;
2251
2252 (void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
2253 }
2254
2255 /*
2256 * Chop IPv6 header off from the payload.
2257 */
2258 static int
2259 ip6_splithdr(m, exthdrs)
2260 struct mbuf *m;
2261 struct ip6_exthdrs *exthdrs;
2262 {
2263 struct mbuf *mh;
2264 struct ip6_hdr *ip6;
2265
2266 ip6 = mtod(m, struct ip6_hdr *);
2267 if (m->m_len > sizeof(*ip6)) {
2268 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2269 if (mh == 0) {
2270 m_freem(m);
2271 return ENOBUFS;
2272 }
2273 M_COPY_PKTHDR(mh, m);
2274 MH_ALIGN(mh, sizeof(*ip6));
2275 m->m_flags &= ~M_PKTHDR;
2276 m->m_len -= sizeof(*ip6);
2277 m->m_data += sizeof(*ip6);
2278 mh->m_next = m;
2279 m = mh;
2280 m->m_len = sizeof(*ip6);
2281 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2282 }
2283 exthdrs->ip6e_ip6 = m;
2284 return 0;
2285 }
2286
2287 /*
2288 * Compute IPv6 extension header length.
2289 */
2290 int
2291 ip6_optlen(in6p)
2292 struct in6pcb *in6p;
2293 {
2294 int len;
2295
2296 if (!in6p->in6p_outputopts)
2297 return 0;
2298
2299 len = 0;
2300 #define elen(x) \
2301 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2302
2303 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2304 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2305 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2306 len += elen(in6p->in6p_outputopts->ip6po_dest2);
2307 return len;
2308 #undef elen
2309 }
2310