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