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