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