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