1 | /* $NetBSD: ip6_output.c,v 1.178 2016/11/10 04:13:53 ozaki-r Exp $ */ |
2 | /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun Exp $ */ |
3 | |
4 | /* |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. Neither the name of the project nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | * SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 |
35 | * The Regents of the University of California. All rights reserved. |
36 | * |
37 | * Redistribution and use in source and binary forms, with or without |
38 | * modification, are permitted provided that the following conditions |
39 | * are met: |
40 | * 1. Redistributions of source code must retain the above copyright |
41 | * notice, this list of conditions and the following disclaimer. |
42 | * 2. Redistributions in binary form must reproduce the above copyright |
43 | * notice, this list of conditions and the following disclaimer in the |
44 | * documentation and/or other materials provided with the distribution. |
45 | * 3. Neither the name of the University nor the names of its contributors |
46 | * may be used to endorse or promote products derived from this software |
47 | * without specific prior written permission. |
48 | * |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 | * SUCH DAMAGE. |
60 | * |
61 | * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 |
62 | */ |
63 | |
64 | #include <sys/cdefs.h> |
65 | __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.178 2016/11/10 04:13:53 ozaki-r Exp $" ); |
66 | |
67 | #ifdef _KERNEL_OPT |
68 | #include "opt_inet.h" |
69 | #include "opt_inet6.h" |
70 | #include "opt_ipsec.h" |
71 | #endif |
72 | |
73 | #include <sys/param.h> |
74 | #include <sys/malloc.h> |
75 | #include <sys/mbuf.h> |
76 | #include <sys/errno.h> |
77 | #include <sys/protosw.h> |
78 | #include <sys/socket.h> |
79 | #include <sys/socketvar.h> |
80 | #include <sys/syslog.h> |
81 | #include <sys/systm.h> |
82 | #include <sys/proc.h> |
83 | #include <sys/kauth.h> |
84 | |
85 | #include <net/if.h> |
86 | #include <net/route.h> |
87 | #include <net/pfil.h> |
88 | |
89 | #include <netinet/in.h> |
90 | #include <netinet/in_var.h> |
91 | #include <netinet/ip6.h> |
92 | #include <netinet/ip_var.h> |
93 | #include <netinet/icmp6.h> |
94 | #include <netinet/in_offload.h> |
95 | #include <netinet/portalgo.h> |
96 | #include <netinet6/in6_offload.h> |
97 | #include <netinet6/ip6_var.h> |
98 | #include <netinet6/ip6_private.h> |
99 | #include <netinet6/in6_pcb.h> |
100 | #include <netinet6/nd6.h> |
101 | #include <netinet6/ip6protosw.h> |
102 | #include <netinet6/scope6_var.h> |
103 | |
104 | #ifdef IPSEC |
105 | #include <netipsec/ipsec.h> |
106 | #include <netipsec/ipsec6.h> |
107 | #include <netipsec/key.h> |
108 | #include <netipsec/xform.h> |
109 | #endif |
110 | |
111 | |
112 | #include <net/net_osdep.h> |
113 | |
114 | extern pfil_head_t *inet6_pfil_hook; /* XXX */ |
115 | |
116 | struct ip6_exthdrs { |
117 | struct mbuf *ip6e_ip6; |
118 | struct mbuf *ip6e_hbh; |
119 | struct mbuf *ip6e_dest1; |
120 | struct mbuf *ip6e_rthdr; |
121 | struct mbuf *ip6e_dest2; |
122 | }; |
123 | |
124 | static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, |
125 | kauth_cred_t, int); |
126 | static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *); |
127 | static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, kauth_cred_t, |
128 | int, int, int); |
129 | static int ip6_setmoptions(const struct sockopt *, struct in6pcb *); |
130 | static int ip6_getmoptions(struct sockopt *, struct in6pcb *); |
131 | static int ip6_copyexthdr(struct mbuf **, void *, int); |
132 | static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int, |
133 | struct ip6_frag **); |
134 | static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t); |
135 | static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *); |
136 | static int ip6_getpmtu(struct rtentry *, struct ifnet *, u_long *, int *); |
137 | static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int); |
138 | static int ip6_ifaddrvalid(const struct in6_addr *); |
139 | static int ip6_handle_rthdr(struct ip6_rthdr *, struct ip6_hdr *); |
140 | |
141 | #ifdef RFC2292 |
142 | static int ip6_pcbopts(struct ip6_pktopts **, struct socket *, struct sockopt *); |
143 | #endif |
144 | |
145 | static int |
146 | ip6_handle_rthdr(struct ip6_rthdr *rh, struct ip6_hdr *ip6) |
147 | { |
148 | struct ip6_rthdr0 *rh0; |
149 | struct in6_addr *addr; |
150 | struct sockaddr_in6 sa; |
151 | int error = 0; |
152 | |
153 | switch (rh->ip6r_type) { |
154 | case IPV6_RTHDR_TYPE_0: |
155 | rh0 = (struct ip6_rthdr0 *)rh; |
156 | addr = (struct in6_addr *)(rh0 + 1); |
157 | |
158 | /* |
159 | * construct a sockaddr_in6 form of the first hop. |
160 | * |
161 | * XXX we may not have enough information about its scope zone; |
162 | * there is no standard API to pass the information from the |
163 | * application. |
164 | */ |
165 | sockaddr_in6_init(&sa, addr, 0, 0, 0); |
166 | error = sa6_embedscope(&sa, ip6_use_defzone); |
167 | if (error != 0) |
168 | break; |
169 | (void)memmove(&addr[0], &addr[1], |
170 | sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1)); |
171 | addr[rh0->ip6r0_segleft - 1] = ip6->ip6_dst; |
172 | ip6->ip6_dst = sa.sin6_addr; |
173 | /* XXX */ |
174 | in6_clearscope(addr + rh0->ip6r0_segleft - 1); |
175 | break; |
176 | default: /* is it possible? */ |
177 | error = EINVAL; |
178 | } |
179 | |
180 | return error; |
181 | } |
182 | |
183 | /* |
184 | * IP6 output. The packet in mbuf chain m contains a skeletal IP6 |
185 | * header (with pri, len, nxt, hlim, src, dst). |
186 | * This function may modify ver and hlim only. |
187 | * The mbuf chain containing the packet will be freed. |
188 | * The mbuf opt, if present, will not be freed. |
189 | * |
190 | * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and |
191 | * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, |
192 | * which is rt_rmx.rmx_mtu. |
193 | */ |
194 | int |
195 | ip6_output( |
196 | struct mbuf *m0, |
197 | struct ip6_pktopts *opt, |
198 | struct route *ro, |
199 | int flags, |
200 | struct ip6_moptions *im6o, |
201 | struct socket *so, |
202 | struct ifnet **ifpp /* XXX: just for statistics */ |
203 | ) |
204 | { |
205 | struct ip6_hdr *ip6, *mhip6; |
206 | struct ifnet *ifp = NULL, *origifp = NULL; |
207 | struct mbuf *m = m0; |
208 | int hlen, tlen, len, off; |
209 | bool tso; |
210 | struct route ip6route; |
211 | struct rtentry *rt = NULL, *rt_pmtu; |
212 | const struct sockaddr_in6 *dst; |
213 | struct sockaddr_in6 src_sa, dst_sa; |
214 | int error = 0; |
215 | struct in6_ifaddr *ia = NULL; |
216 | u_long mtu; |
217 | int alwaysfrag, dontfrag; |
218 | u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; |
219 | struct ip6_exthdrs exthdrs; |
220 | struct in6_addr finaldst, src0, dst0; |
221 | u_int32_t zone; |
222 | struct route *ro_pmtu = NULL; |
223 | int hdrsplit = 0; |
224 | int needipsec = 0; |
225 | #ifdef IPSEC |
226 | struct secpolicy *sp = NULL; |
227 | #endif |
228 | struct psref psref, psref_ia; |
229 | int bound = curlwp_bind(); |
230 | bool release_psref_ia = false; |
231 | |
232 | #ifdef DIAGNOSTIC |
233 | if ((m->m_flags & M_PKTHDR) == 0) |
234 | panic("ip6_output: no HDR" ); |
235 | |
236 | if ((m->m_pkthdr.csum_flags & |
237 | (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) { |
238 | panic("ip6_output: IPv4 checksum offload flags: %d" , |
239 | m->m_pkthdr.csum_flags); |
240 | } |
241 | |
242 | if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == |
243 | (M_CSUM_TCPv6|M_CSUM_UDPv6)) { |
244 | panic("ip6_output: conflicting checksum offload flags: %d" , |
245 | m->m_pkthdr.csum_flags); |
246 | } |
247 | #endif |
248 | |
249 | M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr)); |
250 | |
251 | #define MAKE_EXTHDR(hp, mp) \ |
252 | do { \ |
253 | if (hp) { \ |
254 | struct ip6_ext *eh = (struct ip6_ext *)(hp); \ |
255 | error = ip6_copyexthdr((mp), (void *)(hp), \ |
256 | ((eh)->ip6e_len + 1) << 3); \ |
257 | if (error) \ |
258 | goto freehdrs; \ |
259 | } \ |
260 | } while (/*CONSTCOND*/ 0) |
261 | |
262 | memset(&exthdrs, 0, sizeof(exthdrs)); |
263 | if (opt) { |
264 | /* Hop-by-Hop options header */ |
265 | MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); |
266 | /* Destination options header(1st part) */ |
267 | MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); |
268 | /* Routing header */ |
269 | MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); |
270 | /* Destination options header(2nd part) */ |
271 | MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); |
272 | } |
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 | #ifdef IPSEC |
287 | if (ipsec_used) { |
288 | /* Check the security policy (SP) for the packet */ |
289 | |
290 | sp = ipsec6_check_policy(m, so, flags, &needipsec, &error); |
291 | if (error != 0) { |
292 | /* |
293 | * Hack: -EINVAL is used to signal that a packet |
294 | * should be silently discarded. This is typically |
295 | * because we asked key management for an SA and |
296 | * it was delayed (e.g. kicked up to IKE). |
297 | */ |
298 | if (error == -EINVAL) |
299 | error = 0; |
300 | goto freehdrs; |
301 | } |
302 | } |
303 | #endif /* IPSEC */ |
304 | |
305 | |
306 | if (needipsec && |
307 | (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) { |
308 | in6_delayed_cksum(m); |
309 | m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); |
310 | } |
311 | |
312 | |
313 | /* |
314 | * If we need IPsec, or there is at least one extension header, |
315 | * separate IP6 header from the payload. |
316 | */ |
317 | if ((needipsec || optlen) && !hdrsplit) { |
318 | if ((error = ip6_splithdr(m, &exthdrs)) != 0) { |
319 | m = NULL; |
320 | goto freehdrs; |
321 | } |
322 | m = exthdrs.ip6e_ip6; |
323 | hdrsplit++; |
324 | } |
325 | |
326 | /* adjust pointer */ |
327 | ip6 = mtod(m, struct ip6_hdr *); |
328 | |
329 | /* adjust mbuf packet header length */ |
330 | m->m_pkthdr.len += optlen; |
331 | plen = m->m_pkthdr.len - sizeof(*ip6); |
332 | |
333 | /* If this is a jumbo payload, insert a jumbo payload option. */ |
334 | if (plen > IPV6_MAXPACKET) { |
335 | if (!hdrsplit) { |
336 | if ((error = ip6_splithdr(m, &exthdrs)) != 0) { |
337 | m = NULL; |
338 | goto freehdrs; |
339 | } |
340 | m = exthdrs.ip6e_ip6; |
341 | hdrsplit++; |
342 | } |
343 | /* adjust pointer */ |
344 | ip6 = mtod(m, struct ip6_hdr *); |
345 | if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) |
346 | goto freehdrs; |
347 | optlen += 8; /* XXX JUMBOOPTLEN */ |
348 | ip6->ip6_plen = 0; |
349 | } else |
350 | ip6->ip6_plen = htons(plen); |
351 | |
352 | /* |
353 | * Concatenate headers and fill in next header fields. |
354 | * Here we have, on "m" |
355 | * IPv6 payload |
356 | * and we insert headers accordingly. Finally, we should be getting: |
357 | * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] |
358 | * |
359 | * during the header composing process, "m" points to IPv6 header. |
360 | * "mprev" points to an extension header prior to esp. |
361 | */ |
362 | { |
363 | u_char *nexthdrp = &ip6->ip6_nxt; |
364 | struct mbuf *mprev = m; |
365 | |
366 | /* |
367 | * we treat dest2 specially. this makes IPsec processing |
368 | * much easier. the goal here is to make mprev point the |
369 | * mbuf prior to dest2. |
370 | * |
371 | * result: IPv6 dest2 payload |
372 | * m and mprev will point to IPv6 header. |
373 | */ |
374 | if (exthdrs.ip6e_dest2) { |
375 | if (!hdrsplit) |
376 | panic("assumption failed: hdr not split" ); |
377 | exthdrs.ip6e_dest2->m_next = m->m_next; |
378 | m->m_next = exthdrs.ip6e_dest2; |
379 | *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; |
380 | ip6->ip6_nxt = IPPROTO_DSTOPTS; |
381 | } |
382 | |
383 | #define MAKE_CHAIN(m, mp, p, i)\ |
384 | do {\ |
385 | if (m) {\ |
386 | if (!hdrsplit) \ |
387 | panic("assumption failed: hdr not split"); \ |
388 | *mtod((m), u_char *) = *(p);\ |
389 | *(p) = (i);\ |
390 | p = mtod((m), u_char *);\ |
391 | (m)->m_next = (mp)->m_next;\ |
392 | (mp)->m_next = (m);\ |
393 | (mp) = (m);\ |
394 | }\ |
395 | } while (/*CONSTCOND*/ 0) |
396 | /* |
397 | * result: IPv6 hbh dest1 rthdr dest2 payload |
398 | * m will point to IPv6 header. mprev will point to the |
399 | * extension header prior to dest2 (rthdr in the above case). |
400 | */ |
401 | MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); |
402 | MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, |
403 | IPPROTO_DSTOPTS); |
404 | MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, |
405 | IPPROTO_ROUTING); |
406 | |
407 | M_CSUM_DATA_IPv6_HL_SET(m->m_pkthdr.csum_data, |
408 | sizeof(struct ip6_hdr) + optlen); |
409 | } |
410 | |
411 | /* Need to save for pmtu */ |
412 | finaldst = ip6->ip6_dst; |
413 | |
414 | /* |
415 | * If there is a routing header, replace destination address field |
416 | * with the first hop of the routing header. |
417 | */ |
418 | if (exthdrs.ip6e_rthdr) { |
419 | struct ip6_rthdr *rh; |
420 | |
421 | rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, |
422 | struct ip6_rthdr *)); |
423 | |
424 | error = ip6_handle_rthdr(rh, ip6); |
425 | if (error != 0) |
426 | goto bad; |
427 | } |
428 | |
429 | /* Source address validation */ |
430 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && |
431 | (flags & IPV6_UNSPECSRC) == 0) { |
432 | error = EOPNOTSUPP; |
433 | IP6_STATINC(IP6_STAT_BADSCOPE); |
434 | goto bad; |
435 | } |
436 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { |
437 | error = EOPNOTSUPP; |
438 | IP6_STATINC(IP6_STAT_BADSCOPE); |
439 | goto bad; |
440 | } |
441 | |
442 | IP6_STATINC(IP6_STAT_LOCALOUT); |
443 | |
444 | /* |
445 | * Route packet. |
446 | */ |
447 | /* initialize cached route */ |
448 | if (ro == NULL) { |
449 | memset(&ip6route, 0, sizeof(ip6route)); |
450 | ro = &ip6route; |
451 | } |
452 | ro_pmtu = ro; |
453 | if (opt && opt->ip6po_rthdr) |
454 | ro = &opt->ip6po_route; |
455 | |
456 | /* |
457 | * if specified, try to fill in the traffic class field. |
458 | * do not override if a non-zero value is already set. |
459 | * we check the diffserv field and the ecn field separately. |
460 | */ |
461 | if (opt && opt->ip6po_tclass >= 0) { |
462 | int mask = 0; |
463 | |
464 | if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) |
465 | mask |= 0xfc; |
466 | if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) |
467 | mask |= 0x03; |
468 | if (mask != 0) |
469 | ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); |
470 | } |
471 | |
472 | /* fill in or override the hop limit field, if necessary. */ |
473 | if (opt && opt->ip6po_hlim != -1) |
474 | ip6->ip6_hlim = opt->ip6po_hlim & 0xff; |
475 | else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
476 | if (im6o != NULL) |
477 | ip6->ip6_hlim = im6o->im6o_multicast_hlim; |
478 | else |
479 | ip6->ip6_hlim = ip6_defmcasthlim; |
480 | } |
481 | |
482 | #ifdef IPSEC |
483 | if (needipsec) { |
484 | int s = splsoftnet(); |
485 | error = ipsec6_process_packet(m, sp->req); |
486 | |
487 | /* |
488 | * Preserve KAME behaviour: ENOENT can be returned |
489 | * when an SA acquire is in progress. Don't propagate |
490 | * this to user-level; it confuses applications. |
491 | * XXX this will go away when the SADB is redone. |
492 | */ |
493 | if (error == ENOENT) |
494 | error = 0; |
495 | splx(s); |
496 | goto done; |
497 | } |
498 | #endif /* IPSEC */ |
499 | |
500 | /* adjust pointer */ |
501 | ip6 = mtod(m, struct ip6_hdr *); |
502 | |
503 | sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0); |
504 | |
505 | /* We do not need a route for multicast */ |
506 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
507 | struct in6_pktinfo *pi = NULL; |
508 | |
509 | /* |
510 | * If the outgoing interface for the address is specified by |
511 | * the caller, use it. |
512 | */ |
513 | if (opt && (pi = opt->ip6po_pktinfo) != NULL) { |
514 | /* XXX boundary check is assumed to be already done. */ |
515 | ifp = if_get_byindex(pi->ipi6_ifindex, &psref); |
516 | } else if (im6o != NULL) { |
517 | ifp = if_get_byindex(im6o->im6o_multicast_if_index, |
518 | &psref); |
519 | } |
520 | } |
521 | |
522 | if (ifp == NULL) { |
523 | error = in6_selectroute(&dst_sa, opt, &ro, &rt, true); |
524 | if (error != 0) |
525 | goto bad; |
526 | ifp = if_get_byindex(rt->rt_ifp->if_index, &psref); |
527 | } |
528 | |
529 | if (rt == NULL) { |
530 | /* |
531 | * If in6_selectroute() does not return a route entry, |
532 | * dst may not have been updated. |
533 | */ |
534 | error = rtcache_setdst(ro, sin6tosa(&dst_sa)); |
535 | if (error) { |
536 | goto bad; |
537 | } |
538 | } |
539 | |
540 | /* |
541 | * then rt (for unicast) and ifp must be non-NULL valid values. |
542 | */ |
543 | if ((flags & IPV6_FORWARDING) == 0) { |
544 | /* XXX: the FORWARDING flag can be set for mrouting. */ |
545 | in6_ifstat_inc(ifp, ifs6_out_request); |
546 | } |
547 | if (rt != NULL) { |
548 | ia = (struct in6_ifaddr *)(rt->rt_ifa); |
549 | rt->rt_use++; |
550 | } |
551 | |
552 | /* |
553 | * The outgoing interface must be in the zone of source and |
554 | * destination addresses. We should use ia_ifp to support the |
555 | * case of sending packets to an address of our own. |
556 | */ |
557 | if (ia != NULL && ia->ia_ifp) { |
558 | origifp = ia->ia_ifp; |
559 | if (if_is_deactivated(origifp)) |
560 | goto bad; |
561 | if_acquire_NOMPSAFE(origifp, &psref_ia); |
562 | release_psref_ia = true; |
563 | } else |
564 | origifp = ifp; |
565 | |
566 | src0 = ip6->ip6_src; |
567 | if (in6_setscope(&src0, origifp, &zone)) |
568 | goto badscope; |
569 | sockaddr_in6_init(&src_sa, &ip6->ip6_src, 0, 0, 0); |
570 | if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id) |
571 | goto badscope; |
572 | |
573 | dst0 = ip6->ip6_dst; |
574 | if (in6_setscope(&dst0, origifp, &zone)) |
575 | goto badscope; |
576 | /* re-initialize to be sure */ |
577 | sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0); |
578 | if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) |
579 | goto badscope; |
580 | |
581 | /* scope check is done. */ |
582 | |
583 | /* Ensure we only send from a valid address. */ |
584 | if ((error = ip6_ifaddrvalid(&src0)) != 0) { |
585 | nd6log(LOG_ERR, |
586 | "refusing to send from invalid address %s (pid %d)\n" , |
587 | ip6_sprintf(&src0), curproc->p_pid); |
588 | IP6_STATINC(IP6_STAT_ODROPPED); |
589 | in6_ifstat_inc(origifp, ifs6_out_discard); |
590 | if (error == 1) |
591 | /* |
592 | * Address exists, but is tentative or detached. |
593 | * We can't send from it because it's invalid, |
594 | * so we drop the packet. |
595 | */ |
596 | error = 0; |
597 | else |
598 | error = EADDRNOTAVAIL; |
599 | goto bad; |
600 | } |
601 | |
602 | if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) && |
603 | !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) |
604 | dst = satocsin6(rt->rt_gateway); |
605 | else |
606 | dst = satocsin6(rtcache_getdst(ro)); |
607 | |
608 | /* |
609 | * XXXXXX: original code follows: |
610 | */ |
611 | if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) |
612 | m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ |
613 | else { |
614 | struct in6_multi *in6m; |
615 | |
616 | m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; |
617 | |
618 | in6_ifstat_inc(ifp, ifs6_out_mcast); |
619 | |
620 | /* |
621 | * Confirm that the outgoing interface supports multicast. |
622 | */ |
623 | if (!(ifp->if_flags & IFF_MULTICAST)) { |
624 | IP6_STATINC(IP6_STAT_NOROUTE); |
625 | in6_ifstat_inc(ifp, ifs6_out_discard); |
626 | error = ENETUNREACH; |
627 | goto bad; |
628 | } |
629 | |
630 | IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); |
631 | if (in6m != NULL && |
632 | (im6o == NULL || im6o->im6o_multicast_loop)) { |
633 | /* |
634 | * If we belong to the destination multicast group |
635 | * on the outgoing interface, and the caller did not |
636 | * forbid loopback, loop back a copy. |
637 | */ |
638 | KASSERT(dst != NULL); |
639 | ip6_mloopback(ifp, m, dst); |
640 | } else { |
641 | /* |
642 | * If we are acting as a multicast router, perform |
643 | * multicast forwarding as if the packet had just |
644 | * arrived on the interface to which we are about |
645 | * to send. The multicast forwarding function |
646 | * recursively calls this function, using the |
647 | * IPV6_FORWARDING flag to prevent infinite recursion. |
648 | * |
649 | * Multicasts that are looped back by ip6_mloopback(), |
650 | * above, will be forwarded by the ip6_input() routine, |
651 | * if necessary. |
652 | */ |
653 | if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { |
654 | if (ip6_mforward(ip6, ifp, m) != 0) { |
655 | m_freem(m); |
656 | goto done; |
657 | } |
658 | } |
659 | } |
660 | /* |
661 | * Multicasts with a hoplimit of zero may be looped back, |
662 | * above, but must not be transmitted on a network. |
663 | * Also, multicasts addressed to the loopback interface |
664 | * are not sent -- the above call to ip6_mloopback() will |
665 | * loop back a copy if this host actually belongs to the |
666 | * destination group on the loopback interface. |
667 | */ |
668 | if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || |
669 | IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { |
670 | m_freem(m); |
671 | goto done; |
672 | } |
673 | } |
674 | |
675 | /* |
676 | * Fill the outgoing inteface to tell the upper layer |
677 | * to increment per-interface statistics. |
678 | */ |
679 | if (ifpp) |
680 | *ifpp = ifp; |
681 | |
682 | /* Determine path MTU. */ |
683 | /* |
684 | * ro_pmtu represent final destination while |
685 | * ro might represent immediate destination. |
686 | * Use ro_pmtu destination since MTU might differ. |
687 | */ |
688 | if (ro_pmtu != ro) { |
689 | union { |
690 | struct sockaddr dst; |
691 | struct sockaddr_in6 dst6; |
692 | } u; |
693 | |
694 | /* ro_pmtu may not have a cache */ |
695 | sockaddr_in6_init(&u.dst6, &finaldst, 0, 0, 0); |
696 | rt_pmtu = rtcache_lookup(ro_pmtu, &u.dst); |
697 | } else |
698 | rt_pmtu = rtcache_validate(ro_pmtu); |
699 | error = ip6_getpmtu(rt_pmtu, ifp, &mtu, &alwaysfrag); |
700 | if (error != 0) |
701 | goto bad; |
702 | |
703 | /* |
704 | * The caller of this function may specify to use the minimum MTU |
705 | * in some cases. |
706 | * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU |
707 | * setting. The logic is a bit complicated; by default, unicast |
708 | * packets will follow path MTU while multicast packets will be sent at |
709 | * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets |
710 | * including unicast ones will be sent at the minimum MTU. Multicast |
711 | * packets will always be sent at the minimum MTU unless |
712 | * IP6PO_MINMTU_DISABLE is explicitly specified. |
713 | * See RFC 3542 for more details. |
714 | */ |
715 | if (mtu > IPV6_MMTU) { |
716 | if ((flags & IPV6_MINMTU)) |
717 | mtu = IPV6_MMTU; |
718 | else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) |
719 | mtu = IPV6_MMTU; |
720 | else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && |
721 | (opt == NULL || |
722 | opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) { |
723 | mtu = IPV6_MMTU; |
724 | } |
725 | } |
726 | |
727 | /* |
728 | * clear embedded scope identifiers if necessary. |
729 | * in6_clearscope will touch the addresses only when necessary. |
730 | */ |
731 | in6_clearscope(&ip6->ip6_src); |
732 | in6_clearscope(&ip6->ip6_dst); |
733 | |
734 | /* |
735 | * If the outgoing packet contains a hop-by-hop options header, |
736 | * it must be examined and processed even by the source node. |
737 | * (RFC 2460, section 4.) |
738 | */ |
739 | if (ip6->ip6_nxt == IPV6_HOPOPTS) { |
740 | u_int32_t dummy1; /* XXX unused */ |
741 | u_int32_t dummy2; /* XXX unused */ |
742 | int hoff = sizeof(struct ip6_hdr); |
743 | |
744 | if (ip6_hopopts_input(&dummy1, &dummy2, &m, &hoff)) { |
745 | /* m was already freed at this point */ |
746 | error = EINVAL;/* better error? */ |
747 | goto done; |
748 | } |
749 | |
750 | ip6 = mtod(m, struct ip6_hdr *); |
751 | } |
752 | |
753 | /* |
754 | * Run through list of hooks for output packets. |
755 | */ |
756 | if ((error = pfil_run_hooks(inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0) |
757 | goto done; |
758 | if (m == NULL) |
759 | goto done; |
760 | ip6 = mtod(m, struct ip6_hdr *); |
761 | |
762 | /* |
763 | * Send the packet to the outgoing interface. |
764 | * If necessary, do IPv6 fragmentation before sending. |
765 | * |
766 | * the logic here is rather complex: |
767 | * 1: normal case (dontfrag == 0, alwaysfrag == 0) |
768 | * 1-a: send as is if tlen <= path mtu |
769 | * 1-b: fragment if tlen > path mtu |
770 | * |
771 | * 2: if user asks us not to fragment (dontfrag == 1) |
772 | * 2-a: send as is if tlen <= interface mtu |
773 | * 2-b: error if tlen > interface mtu |
774 | * |
775 | * 3: if we always need to attach fragment header (alwaysfrag == 1) |
776 | * always fragment |
777 | * |
778 | * 4: if dontfrag == 1 && alwaysfrag == 1 |
779 | * error, as we cannot handle this conflicting request |
780 | */ |
781 | tlen = m->m_pkthdr.len; |
782 | tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0; |
783 | if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) |
784 | dontfrag = 1; |
785 | else |
786 | dontfrag = 0; |
787 | |
788 | if (dontfrag && alwaysfrag) { /* case 4 */ |
789 | /* conflicting request - can't transmit */ |
790 | error = EMSGSIZE; |
791 | goto bad; |
792 | } |
793 | if (dontfrag && (!tso && tlen > IN6_LINKMTU(ifp))) { /* case 2-b */ |
794 | /* |
795 | * Even if the DONTFRAG option is specified, we cannot send the |
796 | * packet when the data length is larger than the MTU of the |
797 | * outgoing interface. |
798 | * Notify the error by sending IPV6_PATHMTU ancillary data as |
799 | * well as returning an error code (the latter is not described |
800 | * in the API spec.) |
801 | */ |
802 | u_int32_t mtu32; |
803 | struct ip6ctlparam ip6cp; |
804 | |
805 | mtu32 = (u_int32_t)mtu; |
806 | memset(&ip6cp, 0, sizeof(ip6cp)); |
807 | ip6cp.ip6c_cmdarg = (void *)&mtu32; |
808 | pfctlinput2(PRC_MSGSIZE, |
809 | rtcache_getdst(ro_pmtu), &ip6cp); |
810 | |
811 | error = EMSGSIZE; |
812 | goto bad; |
813 | } |
814 | |
815 | /* |
816 | * transmit packet without fragmentation |
817 | */ |
818 | if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) { |
819 | /* case 1-a and 2-a */ |
820 | struct in6_ifaddr *ia6; |
821 | int sw_csum; |
822 | int s; |
823 | |
824 | ip6 = mtod(m, struct ip6_hdr *); |
825 | s = pserialize_read_enter(); |
826 | ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); |
827 | if (ia6) { |
828 | /* Record statistics for this interface address. */ |
829 | ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; |
830 | } |
831 | pserialize_read_exit(s); |
832 | |
833 | sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; |
834 | if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) { |
835 | if (IN6_NEED_CHECKSUM(ifp, |
836 | sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) { |
837 | in6_delayed_cksum(m); |
838 | } |
839 | m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); |
840 | } |
841 | |
842 | KASSERT(dst != NULL); |
843 | if (__predict_true(!tso || |
844 | (ifp->if_capenable & IFCAP_TSOv6) != 0)) { |
845 | error = nd6_output(ifp, origifp, m, dst, rt); |
846 | } else { |
847 | error = ip6_tso_output(ifp, origifp, m, dst, rt); |
848 | } |
849 | goto done; |
850 | } |
851 | |
852 | if (tso) { |
853 | error = EINVAL; /* XXX */ |
854 | goto bad; |
855 | } |
856 | |
857 | /* |
858 | * try to fragment the packet. case 1-b and 3 |
859 | */ |
860 | if (mtu < IPV6_MMTU) { |
861 | /* path MTU cannot be less than IPV6_MMTU */ |
862 | error = EMSGSIZE; |
863 | in6_ifstat_inc(ifp, ifs6_out_fragfail); |
864 | goto bad; |
865 | } else if (ip6->ip6_plen == 0) { |
866 | /* jumbo payload cannot be fragmented */ |
867 | error = EMSGSIZE; |
868 | in6_ifstat_inc(ifp, ifs6_out_fragfail); |
869 | goto bad; |
870 | } else { |
871 | struct mbuf **mnext, *m_frgpart; |
872 | struct ip6_frag *ip6f; |
873 | u_int32_t id = htonl(ip6_randomid()); |
874 | u_char nextproto; |
875 | #if 0 /* see below */ |
876 | struct ip6ctlparam ip6cp; |
877 | u_int32_t mtu32; |
878 | #endif |
879 | |
880 | /* |
881 | * Too large for the destination or interface; |
882 | * fragment if possible. |
883 | * Must be able to put at least 8 bytes per fragment. |
884 | */ |
885 | hlen = unfragpartlen; |
886 | if (mtu > IPV6_MAXPACKET) |
887 | mtu = IPV6_MAXPACKET; |
888 | |
889 | #if 0 |
890 | /* |
891 | * It is believed this code is a leftover from the |
892 | * development of the IPV6_RECVPATHMTU sockopt and |
893 | * associated work to implement RFC3542. |
894 | * It's not entirely clear what the intent of the API |
895 | * is at this point, so disable this code for now. |
896 | * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG |
897 | * will send notifications if the application requests. |
898 | */ |
899 | |
900 | /* Notify a proper path MTU to applications. */ |
901 | mtu32 = (u_int32_t)mtu; |
902 | memset(&ip6cp, 0, sizeof(ip6cp)); |
903 | ip6cp.ip6c_cmdarg = (void *)&mtu32; |
904 | pfctlinput2(PRC_MSGSIZE, |
905 | rtcache_getdst(ro_pmtu), &ip6cp); |
906 | #endif |
907 | |
908 | len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; |
909 | if (len < 8) { |
910 | error = EMSGSIZE; |
911 | in6_ifstat_inc(ifp, ifs6_out_fragfail); |
912 | goto bad; |
913 | } |
914 | |
915 | mnext = &m->m_nextpkt; |
916 | |
917 | /* |
918 | * Change the next header field of the last header in the |
919 | * unfragmentable part. |
920 | */ |
921 | if (exthdrs.ip6e_rthdr) { |
922 | nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); |
923 | *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; |
924 | } else if (exthdrs.ip6e_dest1) { |
925 | nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); |
926 | *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; |
927 | } else if (exthdrs.ip6e_hbh) { |
928 | nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); |
929 | *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; |
930 | } else { |
931 | nextproto = ip6->ip6_nxt; |
932 | ip6->ip6_nxt = IPPROTO_FRAGMENT; |
933 | } |
934 | |
935 | if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) |
936 | != 0) { |
937 | if (IN6_NEED_CHECKSUM(ifp, |
938 | m->m_pkthdr.csum_flags & |
939 | (M_CSUM_UDPv6|M_CSUM_TCPv6))) { |
940 | in6_delayed_cksum(m); |
941 | } |
942 | m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6); |
943 | } |
944 | |
945 | /* |
946 | * Loop through length of segment after first fragment, |
947 | * make new header and copy data of each part and link onto |
948 | * chain. |
949 | */ |
950 | m0 = m; |
951 | for (off = hlen; off < tlen; off += len) { |
952 | struct mbuf *mlast; |
953 | |
954 | MGETHDR(m, M_DONTWAIT, MT_HEADER); |
955 | if (!m) { |
956 | error = ENOBUFS; |
957 | IP6_STATINC(IP6_STAT_ODROPPED); |
958 | goto sendorfree; |
959 | } |
960 | m_reset_rcvif(m); |
961 | m->m_flags = m0->m_flags & M_COPYFLAGS; |
962 | *mnext = m; |
963 | mnext = &m->m_nextpkt; |
964 | m->m_data += max_linkhdr; |
965 | mhip6 = mtod(m, struct ip6_hdr *); |
966 | *mhip6 = *ip6; |
967 | m->m_len = sizeof(*mhip6); |
968 | /* |
969 | * ip6f must be valid if error is 0. But how |
970 | * can a compiler be expected to infer this? |
971 | */ |
972 | ip6f = NULL; |
973 | error = ip6_insertfraghdr(m0, m, hlen, &ip6f); |
974 | if (error) { |
975 | IP6_STATINC(IP6_STAT_ODROPPED); |
976 | goto sendorfree; |
977 | } |
978 | ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7)); |
979 | if (off + len >= tlen) |
980 | len = tlen - off; |
981 | else |
982 | ip6f->ip6f_offlg |= IP6F_MORE_FRAG; |
983 | mhip6->ip6_plen = htons((u_int16_t)(len + hlen + |
984 | sizeof(*ip6f) - sizeof(struct ip6_hdr))); |
985 | if ((m_frgpart = m_copy(m0, off, len)) == 0) { |
986 | error = ENOBUFS; |
987 | IP6_STATINC(IP6_STAT_ODROPPED); |
988 | goto sendorfree; |
989 | } |
990 | for (mlast = m; mlast->m_next; mlast = mlast->m_next) |
991 | ; |
992 | mlast->m_next = m_frgpart; |
993 | m->m_pkthdr.len = len + hlen + sizeof(*ip6f); |
994 | m_reset_rcvif(m); |
995 | ip6f->ip6f_reserved = 0; |
996 | ip6f->ip6f_ident = id; |
997 | ip6f->ip6f_nxt = nextproto; |
998 | IP6_STATINC(IP6_STAT_OFRAGMENTS); |
999 | in6_ifstat_inc(ifp, ifs6_out_fragcreat); |
1000 | } |
1001 | |
1002 | in6_ifstat_inc(ifp, ifs6_out_fragok); |
1003 | } |
1004 | |
1005 | /* |
1006 | * Remove leading garbages. |
1007 | */ |
1008 | sendorfree: |
1009 | m = m0->m_nextpkt; |
1010 | m0->m_nextpkt = 0; |
1011 | m_freem(m0); |
1012 | for (m0 = m; m; m = m0) { |
1013 | m0 = m->m_nextpkt; |
1014 | m->m_nextpkt = 0; |
1015 | if (error == 0) { |
1016 | struct in6_ifaddr *ia6; |
1017 | int s; |
1018 | ip6 = mtod(m, struct ip6_hdr *); |
1019 | s = pserialize_read_enter(); |
1020 | ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); |
1021 | if (ia6) { |
1022 | /* |
1023 | * Record statistics for this interface |
1024 | * address. |
1025 | */ |
1026 | ia6->ia_ifa.ifa_data.ifad_outbytes += |
1027 | m->m_pkthdr.len; |
1028 | } |
1029 | pserialize_read_exit(s); |
1030 | KASSERT(dst != NULL); |
1031 | error = nd6_output(ifp, origifp, m, dst, rt); |
1032 | } else |
1033 | m_freem(m); |
1034 | } |
1035 | |
1036 | if (error == 0) |
1037 | IP6_STATINC(IP6_STAT_FRAGMENTED); |
1038 | |
1039 | done: |
1040 | if (ro == &ip6route) |
1041 | rtcache_free(&ip6route); |
1042 | |
1043 | #ifdef IPSEC |
1044 | if (sp != NULL) |
1045 | KEY_FREESP(&sp); |
1046 | #endif /* IPSEC */ |
1047 | |
1048 | if_put(ifp, &psref); |
1049 | if (release_psref_ia) |
1050 | if_put(origifp, &psref_ia); |
1051 | curlwp_bindx(bound); |
1052 | |
1053 | return (error); |
1054 | |
1055 | freehdrs: |
1056 | m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ |
1057 | m_freem(exthdrs.ip6e_dest1); |
1058 | m_freem(exthdrs.ip6e_rthdr); |
1059 | m_freem(exthdrs.ip6e_dest2); |
1060 | /* FALLTHROUGH */ |
1061 | bad: |
1062 | m_freem(m); |
1063 | goto done; |
1064 | badscope: |
1065 | IP6_STATINC(IP6_STAT_BADSCOPE); |
1066 | in6_ifstat_inc(origifp, ifs6_out_discard); |
1067 | if (error == 0) |
1068 | error = EHOSTUNREACH; /* XXX */ |
1069 | goto bad; |
1070 | } |
1071 | |
1072 | static int |
1073 | ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen) |
1074 | { |
1075 | struct mbuf *m; |
1076 | |
1077 | if (hlen > MCLBYTES) |
1078 | return (ENOBUFS); /* XXX */ |
1079 | |
1080 | MGET(m, M_DONTWAIT, MT_DATA); |
1081 | if (!m) |
1082 | return (ENOBUFS); |
1083 | |
1084 | if (hlen > MLEN) { |
1085 | MCLGET(m, M_DONTWAIT); |
1086 | if ((m->m_flags & M_EXT) == 0) { |
1087 | m_free(m); |
1088 | return (ENOBUFS); |
1089 | } |
1090 | } |
1091 | m->m_len = hlen; |
1092 | if (hdr) |
1093 | bcopy(hdr, mtod(m, void *), hlen); |
1094 | |
1095 | *mp = m; |
1096 | return (0); |
1097 | } |
1098 | |
1099 | /* |
1100 | * Process a delayed payload checksum calculation. |
1101 | */ |
1102 | void |
1103 | in6_delayed_cksum(struct mbuf *m) |
1104 | { |
1105 | uint16_t csum, offset; |
1106 | |
1107 | KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0); |
1108 | KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0); |
1109 | KASSERT((m->m_pkthdr.csum_flags |
1110 | & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0); |
1111 | |
1112 | offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data); |
1113 | csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset); |
1114 | if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) { |
1115 | csum = 0xffff; |
1116 | } |
1117 | |
1118 | offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data); |
1119 | if ((offset + sizeof(csum)) > m->m_len) { |
1120 | m_copyback(m, offset, sizeof(csum), &csum); |
1121 | } else { |
1122 | *(uint16_t *)(mtod(m, char *) + offset) = csum; |
1123 | } |
1124 | } |
1125 | |
1126 | /* |
1127 | * Insert jumbo payload option. |
1128 | */ |
1129 | static int |
1130 | ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen) |
1131 | { |
1132 | struct mbuf *mopt; |
1133 | u_int8_t *optbuf; |
1134 | u_int32_t v; |
1135 | |
1136 | #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ |
1137 | |
1138 | /* |
1139 | * If there is no hop-by-hop options header, allocate new one. |
1140 | * If there is one but it doesn't have enough space to store the |
1141 | * jumbo payload option, allocate a cluster to store the whole options. |
1142 | * Otherwise, use it to store the options. |
1143 | */ |
1144 | if (exthdrs->ip6e_hbh == 0) { |
1145 | MGET(mopt, M_DONTWAIT, MT_DATA); |
1146 | if (mopt == 0) |
1147 | return (ENOBUFS); |
1148 | mopt->m_len = JUMBOOPTLEN; |
1149 | optbuf = mtod(mopt, u_int8_t *); |
1150 | optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ |
1151 | exthdrs->ip6e_hbh = mopt; |
1152 | } else { |
1153 | struct ip6_hbh *hbh; |
1154 | |
1155 | mopt = exthdrs->ip6e_hbh; |
1156 | if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { |
1157 | /* |
1158 | * XXX assumption: |
1159 | * - exthdrs->ip6e_hbh is not referenced from places |
1160 | * other than exthdrs. |
1161 | * - exthdrs->ip6e_hbh is not an mbuf chain. |
1162 | */ |
1163 | int oldoptlen = mopt->m_len; |
1164 | struct mbuf *n; |
1165 | |
1166 | /* |
1167 | * XXX: give up if the whole (new) hbh header does |
1168 | * not fit even in an mbuf cluster. |
1169 | */ |
1170 | if (oldoptlen + JUMBOOPTLEN > MCLBYTES) |
1171 | return (ENOBUFS); |
1172 | |
1173 | /* |
1174 | * As a consequence, we must always prepare a cluster |
1175 | * at this point. |
1176 | */ |
1177 | MGET(n, M_DONTWAIT, MT_DATA); |
1178 | if (n) { |
1179 | MCLGET(n, M_DONTWAIT); |
1180 | if ((n->m_flags & M_EXT) == 0) { |
1181 | m_freem(n); |
1182 | n = NULL; |
1183 | } |
1184 | } |
1185 | if (!n) |
1186 | return (ENOBUFS); |
1187 | n->m_len = oldoptlen + JUMBOOPTLEN; |
1188 | bcopy(mtod(mopt, void *), mtod(n, void *), |
1189 | oldoptlen); |
1190 | optbuf = mtod(n, u_int8_t *) + oldoptlen; |
1191 | m_freem(mopt); |
1192 | mopt = exthdrs->ip6e_hbh = n; |
1193 | } else { |
1194 | optbuf = mtod(mopt, u_int8_t *) + mopt->m_len; |
1195 | mopt->m_len += JUMBOOPTLEN; |
1196 | } |
1197 | optbuf[0] = IP6OPT_PADN; |
1198 | optbuf[1] = 0; |
1199 | |
1200 | /* |
1201 | * Adjust the header length according to the pad and |
1202 | * the jumbo payload option. |
1203 | */ |
1204 | hbh = mtod(mopt, struct ip6_hbh *); |
1205 | hbh->ip6h_len += (JUMBOOPTLEN >> 3); |
1206 | } |
1207 | |
1208 | /* fill in the option. */ |
1209 | optbuf[2] = IP6OPT_JUMBO; |
1210 | optbuf[3] = 4; |
1211 | v = (u_int32_t)htonl(plen + JUMBOOPTLEN); |
1212 | bcopy(&v, &optbuf[4], sizeof(u_int32_t)); |
1213 | |
1214 | /* finally, adjust the packet header length */ |
1215 | exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; |
1216 | |
1217 | return (0); |
1218 | #undef JUMBOOPTLEN |
1219 | } |
1220 | |
1221 | /* |
1222 | * Insert fragment header and copy unfragmentable header portions. |
1223 | * |
1224 | * *frghdrp will not be read, and it is guaranteed that either an |
1225 | * error is returned or that *frghdrp will point to space allocated |
1226 | * for the fragment header. |
1227 | */ |
1228 | static int |
1229 | ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen, |
1230 | struct ip6_frag **frghdrp) |
1231 | { |
1232 | struct mbuf *n, *mlast; |
1233 | |
1234 | if (hlen > sizeof(struct ip6_hdr)) { |
1235 | n = m_copym(m0, sizeof(struct ip6_hdr), |
1236 | hlen - sizeof(struct ip6_hdr), M_DONTWAIT); |
1237 | if (n == 0) |
1238 | return (ENOBUFS); |
1239 | m->m_next = n; |
1240 | } else |
1241 | n = m; |
1242 | |
1243 | /* Search for the last mbuf of unfragmentable part. */ |
1244 | for (mlast = n; mlast->m_next; mlast = mlast->m_next) |
1245 | ; |
1246 | |
1247 | if ((mlast->m_flags & M_EXT) == 0 && |
1248 | M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { |
1249 | /* use the trailing space of the last mbuf for the fragment hdr */ |
1250 | *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) + |
1251 | mlast->m_len); |
1252 | mlast->m_len += sizeof(struct ip6_frag); |
1253 | m->m_pkthdr.len += sizeof(struct ip6_frag); |
1254 | } else { |
1255 | /* allocate a new mbuf for the fragment header */ |
1256 | struct mbuf *mfrg; |
1257 | |
1258 | MGET(mfrg, M_DONTWAIT, MT_DATA); |
1259 | if (mfrg == 0) |
1260 | return (ENOBUFS); |
1261 | mfrg->m_len = sizeof(struct ip6_frag); |
1262 | *frghdrp = mtod(mfrg, struct ip6_frag *); |
1263 | mlast->m_next = mfrg; |
1264 | } |
1265 | |
1266 | return (0); |
1267 | } |
1268 | |
1269 | static int |
1270 | ip6_getpmtu(struct rtentry *rt, struct ifnet *ifp, u_long *mtup, |
1271 | int *alwaysfragp) |
1272 | { |
1273 | u_int32_t mtu = 0; |
1274 | int alwaysfrag = 0; |
1275 | int error = 0; |
1276 | |
1277 | if (rt != NULL) { |
1278 | u_int32_t ifmtu; |
1279 | |
1280 | if (ifp == NULL) |
1281 | ifp = rt->rt_ifp; |
1282 | ifmtu = IN6_LINKMTU(ifp); |
1283 | mtu = rt->rt_rmx.rmx_mtu; |
1284 | if (mtu == 0) |
1285 | mtu = ifmtu; |
1286 | else if (mtu < IPV6_MMTU) { |
1287 | /* |
1288 | * RFC2460 section 5, last paragraph: |
1289 | * if we record ICMPv6 too big message with |
1290 | * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU |
1291 | * or smaller, with fragment header attached. |
1292 | * (fragment header is needed regardless from the |
1293 | * packet size, for translators to identify packets) |
1294 | */ |
1295 | alwaysfrag = 1; |
1296 | mtu = IPV6_MMTU; |
1297 | } else if (mtu > ifmtu) { |
1298 | /* |
1299 | * The MTU on the route is larger than the MTU on |
1300 | * the interface! This shouldn't happen, unless the |
1301 | * MTU of the interface has been changed after the |
1302 | * interface was brought up. Change the MTU in the |
1303 | * route to match the interface MTU (as long as the |
1304 | * field isn't locked). |
1305 | */ |
1306 | mtu = ifmtu; |
1307 | if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) |
1308 | rt->rt_rmx.rmx_mtu = mtu; |
1309 | } |
1310 | } else if (ifp) { |
1311 | mtu = IN6_LINKMTU(ifp); |
1312 | } else |
1313 | error = EHOSTUNREACH; /* XXX */ |
1314 | |
1315 | *mtup = mtu; |
1316 | if (alwaysfragp) |
1317 | *alwaysfragp = alwaysfrag; |
1318 | return (error); |
1319 | } |
1320 | |
1321 | /* |
1322 | * IP6 socket option processing. |
1323 | */ |
1324 | int |
1325 | ip6_ctloutput(int op, struct socket *so, struct sockopt *sopt) |
1326 | { |
1327 | int optdatalen, uproto; |
1328 | void *optdata; |
1329 | struct in6pcb *in6p = sotoin6pcb(so); |
1330 | struct ip_moptions **mopts; |
1331 | int error, optval; |
1332 | int level, optname; |
1333 | |
1334 | KASSERT(sopt != NULL); |
1335 | |
1336 | level = sopt->sopt_level; |
1337 | optname = sopt->sopt_name; |
1338 | |
1339 | error = optval = 0; |
1340 | uproto = (int)so->so_proto->pr_protocol; |
1341 | |
1342 | switch (level) { |
1343 | case IPPROTO_IP: |
1344 | switch (optname) { |
1345 | case IP_ADD_MEMBERSHIP: |
1346 | case IP_DROP_MEMBERSHIP: |
1347 | case IP_MULTICAST_IF: |
1348 | case IP_MULTICAST_LOOP: |
1349 | case IP_MULTICAST_TTL: |
1350 | mopts = &in6p->in6p_v4moptions; |
1351 | switch (op) { |
1352 | case PRCO_GETOPT: |
1353 | return ip_getmoptions(*mopts, sopt); |
1354 | case PRCO_SETOPT: |
1355 | return ip_setmoptions(mopts, sopt); |
1356 | default: |
1357 | return EINVAL; |
1358 | } |
1359 | default: |
1360 | return ENOPROTOOPT; |
1361 | } |
1362 | case IPPROTO_IPV6: |
1363 | break; |
1364 | default: |
1365 | return ENOPROTOOPT; |
1366 | } |
1367 | switch (op) { |
1368 | case PRCO_SETOPT: |
1369 | switch (optname) { |
1370 | #ifdef RFC2292 |
1371 | case IPV6_2292PKTOPTIONS: |
1372 | error = ip6_pcbopts(&in6p->in6p_outputopts, so, sopt); |
1373 | break; |
1374 | #endif |
1375 | |
1376 | /* |
1377 | * Use of some Hop-by-Hop options or some |
1378 | * Destination options, might require special |
1379 | * privilege. That is, normal applications |
1380 | * (without special privilege) might be forbidden |
1381 | * from setting certain options in outgoing packets, |
1382 | * and might never see certain options in received |
1383 | * packets. [RFC 2292 Section 6] |
1384 | * KAME specific note: |
1385 | * KAME prevents non-privileged users from sending or |
1386 | * receiving ANY hbh/dst options in order to avoid |
1387 | * overhead of parsing options in the kernel. |
1388 | */ |
1389 | case IPV6_RECVHOPOPTS: |
1390 | case IPV6_RECVDSTOPTS: |
1391 | case IPV6_RECVRTHDRDSTOPTS: |
1392 | error = kauth_authorize_network(kauth_cred_get(), |
1393 | KAUTH_NETWORK_IPV6, KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, |
1394 | NULL, NULL, NULL); |
1395 | if (error) |
1396 | break; |
1397 | /* FALLTHROUGH */ |
1398 | case IPV6_UNICAST_HOPS: |
1399 | case IPV6_HOPLIMIT: |
1400 | case IPV6_FAITH: |
1401 | |
1402 | case IPV6_RECVPKTINFO: |
1403 | case IPV6_RECVHOPLIMIT: |
1404 | case IPV6_RECVRTHDR: |
1405 | case IPV6_RECVPATHMTU: |
1406 | case IPV6_RECVTCLASS: |
1407 | case IPV6_V6ONLY: |
1408 | error = sockopt_getint(sopt, &optval); |
1409 | if (error) |
1410 | break; |
1411 | switch (optname) { |
1412 | case IPV6_UNICAST_HOPS: |
1413 | if (optval < -1 || optval >= 256) |
1414 | error = EINVAL; |
1415 | else { |
1416 | /* -1 = kernel default */ |
1417 | in6p->in6p_hops = optval; |
1418 | } |
1419 | break; |
1420 | #define OPTSET(bit) \ |
1421 | do { \ |
1422 | if (optval) \ |
1423 | in6p->in6p_flags |= (bit); \ |
1424 | else \ |
1425 | in6p->in6p_flags &= ~(bit); \ |
1426 | } while (/*CONSTCOND*/ 0) |
1427 | |
1428 | #ifdef RFC2292 |
1429 | #define OPTSET2292(bit) \ |
1430 | do { \ |
1431 | in6p->in6p_flags |= IN6P_RFC2292; \ |
1432 | if (optval) \ |
1433 | in6p->in6p_flags |= (bit); \ |
1434 | else \ |
1435 | in6p->in6p_flags &= ~(bit); \ |
1436 | } while (/*CONSTCOND*/ 0) |
1437 | #endif |
1438 | |
1439 | #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) |
1440 | |
1441 | case IPV6_RECVPKTINFO: |
1442 | #ifdef RFC2292 |
1443 | /* cannot mix with RFC2292 */ |
1444 | if (OPTBIT(IN6P_RFC2292)) { |
1445 | error = EINVAL; |
1446 | break; |
1447 | } |
1448 | #endif |
1449 | OPTSET(IN6P_PKTINFO); |
1450 | break; |
1451 | |
1452 | case IPV6_HOPLIMIT: |
1453 | { |
1454 | struct ip6_pktopts **optp; |
1455 | |
1456 | #ifdef RFC2292 |
1457 | /* cannot mix with RFC2292 */ |
1458 | if (OPTBIT(IN6P_RFC2292)) { |
1459 | error = EINVAL; |
1460 | break; |
1461 | } |
1462 | #endif |
1463 | optp = &in6p->in6p_outputopts; |
1464 | error = ip6_pcbopt(IPV6_HOPLIMIT, |
1465 | (u_char *)&optval, |
1466 | sizeof(optval), |
1467 | optp, |
1468 | kauth_cred_get(), uproto); |
1469 | break; |
1470 | } |
1471 | |
1472 | case IPV6_RECVHOPLIMIT: |
1473 | #ifdef RFC2292 |
1474 | /* cannot mix with RFC2292 */ |
1475 | if (OPTBIT(IN6P_RFC2292)) { |
1476 | error = EINVAL; |
1477 | break; |
1478 | } |
1479 | #endif |
1480 | OPTSET(IN6P_HOPLIMIT); |
1481 | break; |
1482 | |
1483 | case IPV6_RECVHOPOPTS: |
1484 | #ifdef RFC2292 |
1485 | /* cannot mix with RFC2292 */ |
1486 | if (OPTBIT(IN6P_RFC2292)) { |
1487 | error = EINVAL; |
1488 | break; |
1489 | } |
1490 | #endif |
1491 | OPTSET(IN6P_HOPOPTS); |
1492 | break; |
1493 | |
1494 | case IPV6_RECVDSTOPTS: |
1495 | #ifdef RFC2292 |
1496 | /* cannot mix with RFC2292 */ |
1497 | if (OPTBIT(IN6P_RFC2292)) { |
1498 | error = EINVAL; |
1499 | break; |
1500 | } |
1501 | #endif |
1502 | OPTSET(IN6P_DSTOPTS); |
1503 | break; |
1504 | |
1505 | case IPV6_RECVRTHDRDSTOPTS: |
1506 | #ifdef RFC2292 |
1507 | /* cannot mix with RFC2292 */ |
1508 | if (OPTBIT(IN6P_RFC2292)) { |
1509 | error = EINVAL; |
1510 | break; |
1511 | } |
1512 | #endif |
1513 | OPTSET(IN6P_RTHDRDSTOPTS); |
1514 | break; |
1515 | |
1516 | case IPV6_RECVRTHDR: |
1517 | #ifdef RFC2292 |
1518 | /* cannot mix with RFC2292 */ |
1519 | if (OPTBIT(IN6P_RFC2292)) { |
1520 | error = EINVAL; |
1521 | break; |
1522 | } |
1523 | #endif |
1524 | OPTSET(IN6P_RTHDR); |
1525 | break; |
1526 | |
1527 | case IPV6_FAITH: |
1528 | OPTSET(IN6P_FAITH); |
1529 | break; |
1530 | |
1531 | case IPV6_RECVPATHMTU: |
1532 | /* |
1533 | * We ignore this option for TCP |
1534 | * sockets. |
1535 | * (RFC3542 leaves this case |
1536 | * unspecified.) |
1537 | */ |
1538 | if (uproto != IPPROTO_TCP) |
1539 | OPTSET(IN6P_MTU); |
1540 | break; |
1541 | |
1542 | case IPV6_V6ONLY: |
1543 | /* |
1544 | * make setsockopt(IPV6_V6ONLY) |
1545 | * available only prior to bind(2). |
1546 | * see ipng mailing list, Jun 22 2001. |
1547 | */ |
1548 | if (in6p->in6p_lport || |
1549 | !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { |
1550 | error = EINVAL; |
1551 | break; |
1552 | } |
1553 | #ifdef INET6_BINDV6ONLY |
1554 | if (!optval) |
1555 | error = EINVAL; |
1556 | #else |
1557 | OPTSET(IN6P_IPV6_V6ONLY); |
1558 | #endif |
1559 | break; |
1560 | case IPV6_RECVTCLASS: |
1561 | #ifdef RFC2292 |
1562 | /* cannot mix with RFC2292 XXX */ |
1563 | if (OPTBIT(IN6P_RFC2292)) { |
1564 | error = EINVAL; |
1565 | break; |
1566 | } |
1567 | #endif |
1568 | OPTSET(IN6P_TCLASS); |
1569 | break; |
1570 | |
1571 | } |
1572 | break; |
1573 | |
1574 | case IPV6_OTCLASS: |
1575 | { |
1576 | struct ip6_pktopts **optp; |
1577 | u_int8_t tclass; |
1578 | |
1579 | error = sockopt_get(sopt, &tclass, sizeof(tclass)); |
1580 | if (error) |
1581 | break; |
1582 | optp = &in6p->in6p_outputopts; |
1583 | error = ip6_pcbopt(optname, |
1584 | (u_char *)&tclass, |
1585 | sizeof(tclass), |
1586 | optp, |
1587 | kauth_cred_get(), uproto); |
1588 | break; |
1589 | } |
1590 | |
1591 | case IPV6_TCLASS: |
1592 | case IPV6_DONTFRAG: |
1593 | case IPV6_USE_MIN_MTU: |
1594 | case IPV6_PREFER_TEMPADDR: |
1595 | error = sockopt_getint(sopt, &optval); |
1596 | if (error) |
1597 | break; |
1598 | { |
1599 | struct ip6_pktopts **optp; |
1600 | optp = &in6p->in6p_outputopts; |
1601 | error = ip6_pcbopt(optname, |
1602 | (u_char *)&optval, |
1603 | sizeof(optval), |
1604 | optp, |
1605 | kauth_cred_get(), uproto); |
1606 | break; |
1607 | } |
1608 | |
1609 | #ifdef RFC2292 |
1610 | case IPV6_2292PKTINFO: |
1611 | case IPV6_2292HOPLIMIT: |
1612 | case IPV6_2292HOPOPTS: |
1613 | case IPV6_2292DSTOPTS: |
1614 | case IPV6_2292RTHDR: |
1615 | /* RFC 2292 */ |
1616 | error = sockopt_getint(sopt, &optval); |
1617 | if (error) |
1618 | break; |
1619 | |
1620 | switch (optname) { |
1621 | case IPV6_2292PKTINFO: |
1622 | OPTSET2292(IN6P_PKTINFO); |
1623 | break; |
1624 | case IPV6_2292HOPLIMIT: |
1625 | OPTSET2292(IN6P_HOPLIMIT); |
1626 | break; |
1627 | case IPV6_2292HOPOPTS: |
1628 | /* |
1629 | * Check super-user privilege. |
1630 | * See comments for IPV6_RECVHOPOPTS. |
1631 | */ |
1632 | error = |
1633 | kauth_authorize_network(kauth_cred_get(), |
1634 | KAUTH_NETWORK_IPV6, |
1635 | KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, |
1636 | NULL, NULL); |
1637 | if (error) |
1638 | return (error); |
1639 | OPTSET2292(IN6P_HOPOPTS); |
1640 | break; |
1641 | case IPV6_2292DSTOPTS: |
1642 | error = |
1643 | kauth_authorize_network(kauth_cred_get(), |
1644 | KAUTH_NETWORK_IPV6, |
1645 | KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, |
1646 | NULL, NULL); |
1647 | if (error) |
1648 | return (error); |
1649 | OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ |
1650 | break; |
1651 | case IPV6_2292RTHDR: |
1652 | OPTSET2292(IN6P_RTHDR); |
1653 | break; |
1654 | } |
1655 | break; |
1656 | #endif |
1657 | case IPV6_PKTINFO: |
1658 | case IPV6_HOPOPTS: |
1659 | case IPV6_RTHDR: |
1660 | case IPV6_DSTOPTS: |
1661 | case IPV6_RTHDRDSTOPTS: |
1662 | case IPV6_NEXTHOP: { |
1663 | /* new advanced API (RFC3542) */ |
1664 | void *optbuf; |
1665 | int optbuflen; |
1666 | struct ip6_pktopts **optp; |
1667 | |
1668 | #ifdef RFC2292 |
1669 | /* cannot mix with RFC2292 */ |
1670 | if (OPTBIT(IN6P_RFC2292)) { |
1671 | error = EINVAL; |
1672 | break; |
1673 | } |
1674 | #endif |
1675 | |
1676 | optbuflen = sopt->sopt_size; |
1677 | optbuf = malloc(optbuflen, M_IP6OPT, M_NOWAIT); |
1678 | if (optbuf == NULL) { |
1679 | error = ENOBUFS; |
1680 | break; |
1681 | } |
1682 | |
1683 | error = sockopt_get(sopt, optbuf, optbuflen); |
1684 | if (error) { |
1685 | free(optbuf, M_IP6OPT); |
1686 | break; |
1687 | } |
1688 | optp = &in6p->in6p_outputopts; |
1689 | error = ip6_pcbopt(optname, optbuf, optbuflen, |
1690 | optp, kauth_cred_get(), uproto); |
1691 | |
1692 | free(optbuf, M_IP6OPT); |
1693 | break; |
1694 | } |
1695 | #undef OPTSET |
1696 | |
1697 | case IPV6_MULTICAST_IF: |
1698 | case IPV6_MULTICAST_HOPS: |
1699 | case IPV6_MULTICAST_LOOP: |
1700 | case IPV6_JOIN_GROUP: |
1701 | case IPV6_LEAVE_GROUP: |
1702 | error = ip6_setmoptions(sopt, in6p); |
1703 | break; |
1704 | |
1705 | case IPV6_PORTRANGE: |
1706 | error = sockopt_getint(sopt, &optval); |
1707 | if (error) |
1708 | break; |
1709 | |
1710 | switch (optval) { |
1711 | case IPV6_PORTRANGE_DEFAULT: |
1712 | in6p->in6p_flags &= ~(IN6P_LOWPORT); |
1713 | in6p->in6p_flags &= ~(IN6P_HIGHPORT); |
1714 | break; |
1715 | |
1716 | case IPV6_PORTRANGE_HIGH: |
1717 | in6p->in6p_flags &= ~(IN6P_LOWPORT); |
1718 | in6p->in6p_flags |= IN6P_HIGHPORT; |
1719 | break; |
1720 | |
1721 | case IPV6_PORTRANGE_LOW: |
1722 | in6p->in6p_flags &= ~(IN6P_HIGHPORT); |
1723 | in6p->in6p_flags |= IN6P_LOWPORT; |
1724 | break; |
1725 | |
1726 | default: |
1727 | error = EINVAL; |
1728 | break; |
1729 | } |
1730 | break; |
1731 | |
1732 | case IPV6_PORTALGO: |
1733 | error = sockopt_getint(sopt, &optval); |
1734 | if (error) |
1735 | break; |
1736 | |
1737 | error = portalgo_algo_index_select( |
1738 | (struct inpcb_hdr *)in6p, optval); |
1739 | break; |
1740 | |
1741 | #if defined(IPSEC) |
1742 | case IPV6_IPSEC_POLICY: |
1743 | if (ipsec_enabled) { |
1744 | error = ipsec6_set_policy(in6p, optname, |
1745 | sopt->sopt_data, sopt->sopt_size, |
1746 | kauth_cred_get()); |
1747 | break; |
1748 | } |
1749 | /*FALLTHROUGH*/ |
1750 | #endif /* IPSEC */ |
1751 | |
1752 | default: |
1753 | error = ENOPROTOOPT; |
1754 | break; |
1755 | } |
1756 | break; |
1757 | |
1758 | case PRCO_GETOPT: |
1759 | switch (optname) { |
1760 | #ifdef RFC2292 |
1761 | case IPV6_2292PKTOPTIONS: |
1762 | /* |
1763 | * RFC3542 (effectively) deprecated the |
1764 | * semantics of the 2292-style pktoptions. |
1765 | * Since it was not reliable in nature (i.e., |
1766 | * applications had to expect the lack of some |
1767 | * information after all), it would make sense |
1768 | * to simplify this part by always returning |
1769 | * empty data. |
1770 | */ |
1771 | break; |
1772 | #endif |
1773 | |
1774 | case IPV6_RECVHOPOPTS: |
1775 | case IPV6_RECVDSTOPTS: |
1776 | case IPV6_RECVRTHDRDSTOPTS: |
1777 | case IPV6_UNICAST_HOPS: |
1778 | case IPV6_RECVPKTINFO: |
1779 | case IPV6_RECVHOPLIMIT: |
1780 | case IPV6_RECVRTHDR: |
1781 | case IPV6_RECVPATHMTU: |
1782 | |
1783 | case IPV6_FAITH: |
1784 | case IPV6_V6ONLY: |
1785 | case IPV6_PORTRANGE: |
1786 | case IPV6_RECVTCLASS: |
1787 | switch (optname) { |
1788 | |
1789 | case IPV6_RECVHOPOPTS: |
1790 | optval = OPTBIT(IN6P_HOPOPTS); |
1791 | break; |
1792 | |
1793 | case IPV6_RECVDSTOPTS: |
1794 | optval = OPTBIT(IN6P_DSTOPTS); |
1795 | break; |
1796 | |
1797 | case IPV6_RECVRTHDRDSTOPTS: |
1798 | optval = OPTBIT(IN6P_RTHDRDSTOPTS); |
1799 | break; |
1800 | |
1801 | case IPV6_UNICAST_HOPS: |
1802 | optval = in6p->in6p_hops; |
1803 | break; |
1804 | |
1805 | case IPV6_RECVPKTINFO: |
1806 | optval = OPTBIT(IN6P_PKTINFO); |
1807 | break; |
1808 | |
1809 | case IPV6_RECVHOPLIMIT: |
1810 | optval = OPTBIT(IN6P_HOPLIMIT); |
1811 | break; |
1812 | |
1813 | case IPV6_RECVRTHDR: |
1814 | optval = OPTBIT(IN6P_RTHDR); |
1815 | break; |
1816 | |
1817 | case IPV6_RECVPATHMTU: |
1818 | optval = OPTBIT(IN6P_MTU); |
1819 | break; |
1820 | |
1821 | case IPV6_FAITH: |
1822 | optval = OPTBIT(IN6P_FAITH); |
1823 | break; |
1824 | |
1825 | case IPV6_V6ONLY: |
1826 | optval = OPTBIT(IN6P_IPV6_V6ONLY); |
1827 | break; |
1828 | |
1829 | case IPV6_PORTRANGE: |
1830 | { |
1831 | int flags; |
1832 | flags = in6p->in6p_flags; |
1833 | if (flags & IN6P_HIGHPORT) |
1834 | optval = IPV6_PORTRANGE_HIGH; |
1835 | else if (flags & IN6P_LOWPORT) |
1836 | optval = IPV6_PORTRANGE_LOW; |
1837 | else |
1838 | optval = 0; |
1839 | break; |
1840 | } |
1841 | case IPV6_RECVTCLASS: |
1842 | optval = OPTBIT(IN6P_TCLASS); |
1843 | break; |
1844 | |
1845 | } |
1846 | if (error) |
1847 | break; |
1848 | error = sockopt_setint(sopt, optval); |
1849 | break; |
1850 | |
1851 | case IPV6_PATHMTU: |
1852 | { |
1853 | u_long pmtu = 0; |
1854 | struct ip6_mtuinfo mtuinfo; |
1855 | struct route *ro = &in6p->in6p_route; |
1856 | struct rtentry *rt; |
1857 | union { |
1858 | struct sockaddr dst; |
1859 | struct sockaddr_in6 dst6; |
1860 | } u; |
1861 | |
1862 | if (!(so->so_state & SS_ISCONNECTED)) |
1863 | return (ENOTCONN); |
1864 | /* |
1865 | * XXX: we dot not consider the case of source |
1866 | * routing, or optional information to specify |
1867 | * the outgoing interface. |
1868 | */ |
1869 | sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0); |
1870 | rt = rtcache_lookup(ro, &u.dst); |
1871 | error = ip6_getpmtu(rt, NULL, &pmtu, NULL); |
1872 | if (error) |
1873 | break; |
1874 | if (pmtu > IPV6_MAXPACKET) |
1875 | pmtu = IPV6_MAXPACKET; |
1876 | |
1877 | memset(&mtuinfo, 0, sizeof(mtuinfo)); |
1878 | mtuinfo.ip6m_mtu = (u_int32_t)pmtu; |
1879 | optdata = (void *)&mtuinfo; |
1880 | optdatalen = sizeof(mtuinfo); |
1881 | if (optdatalen > MCLBYTES) |
1882 | return (EMSGSIZE); /* XXX */ |
1883 | error = sockopt_set(sopt, optdata, optdatalen); |
1884 | break; |
1885 | } |
1886 | |
1887 | #ifdef RFC2292 |
1888 | case IPV6_2292PKTINFO: |
1889 | case IPV6_2292HOPLIMIT: |
1890 | case IPV6_2292HOPOPTS: |
1891 | case IPV6_2292RTHDR: |
1892 | case IPV6_2292DSTOPTS: |
1893 | switch (optname) { |
1894 | case IPV6_2292PKTINFO: |
1895 | optval = OPTBIT(IN6P_PKTINFO); |
1896 | break; |
1897 | case IPV6_2292HOPLIMIT: |
1898 | optval = OPTBIT(IN6P_HOPLIMIT); |
1899 | break; |
1900 | case IPV6_2292HOPOPTS: |
1901 | optval = OPTBIT(IN6P_HOPOPTS); |
1902 | break; |
1903 | case IPV6_2292RTHDR: |
1904 | optval = OPTBIT(IN6P_RTHDR); |
1905 | break; |
1906 | case IPV6_2292DSTOPTS: |
1907 | optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); |
1908 | break; |
1909 | } |
1910 | error = sockopt_setint(sopt, optval); |
1911 | break; |
1912 | #endif |
1913 | case IPV6_PKTINFO: |
1914 | case IPV6_HOPOPTS: |
1915 | case IPV6_RTHDR: |
1916 | case IPV6_DSTOPTS: |
1917 | case IPV6_RTHDRDSTOPTS: |
1918 | case IPV6_NEXTHOP: |
1919 | case IPV6_OTCLASS: |
1920 | case IPV6_TCLASS: |
1921 | case IPV6_DONTFRAG: |
1922 | case IPV6_USE_MIN_MTU: |
1923 | case IPV6_PREFER_TEMPADDR: |
1924 | error = ip6_getpcbopt(in6p->in6p_outputopts, |
1925 | optname, sopt); |
1926 | break; |
1927 | |
1928 | case IPV6_MULTICAST_IF: |
1929 | case IPV6_MULTICAST_HOPS: |
1930 | case IPV6_MULTICAST_LOOP: |
1931 | case IPV6_JOIN_GROUP: |
1932 | case IPV6_LEAVE_GROUP: |
1933 | error = ip6_getmoptions(sopt, in6p); |
1934 | break; |
1935 | |
1936 | case IPV6_PORTALGO: |
1937 | optval = ((struct inpcb_hdr *)in6p)->inph_portalgo; |
1938 | error = sockopt_setint(sopt, optval); |
1939 | break; |
1940 | |
1941 | #if defined(IPSEC) |
1942 | case IPV6_IPSEC_POLICY: |
1943 | if (ipsec_used) { |
1944 | struct mbuf *m = NULL; |
1945 | |
1946 | /* |
1947 | * XXX: this will return EINVAL as sopt is |
1948 | * empty |
1949 | */ |
1950 | error = ipsec6_get_policy(in6p, sopt->sopt_data, |
1951 | sopt->sopt_size, &m); |
1952 | if (!error) |
1953 | error = sockopt_setmbuf(sopt, m); |
1954 | break; |
1955 | } |
1956 | /*FALLTHROUGH*/ |
1957 | #endif /* IPSEC */ |
1958 | |
1959 | default: |
1960 | error = ENOPROTOOPT; |
1961 | break; |
1962 | } |
1963 | break; |
1964 | } |
1965 | return (error); |
1966 | } |
1967 | |
1968 | int |
1969 | ip6_raw_ctloutput(int op, struct socket *so, struct sockopt *sopt) |
1970 | { |
1971 | int error = 0, optval; |
1972 | const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); |
1973 | struct in6pcb *in6p = sotoin6pcb(so); |
1974 | int level, optname; |
1975 | |
1976 | KASSERT(sopt != NULL); |
1977 | |
1978 | level = sopt->sopt_level; |
1979 | optname = sopt->sopt_name; |
1980 | |
1981 | if (level != IPPROTO_IPV6) { |
1982 | return ENOPROTOOPT; |
1983 | } |
1984 | |
1985 | switch (optname) { |
1986 | case IPV6_CHECKSUM: |
1987 | /* |
1988 | * For ICMPv6 sockets, no modification allowed for checksum |
1989 | * offset, permit "no change" values to help existing apps. |
1990 | * |
1991 | * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM |
1992 | * for an ICMPv6 socket will fail." The current |
1993 | * behavior does not meet RFC3542. |
1994 | */ |
1995 | switch (op) { |
1996 | case PRCO_SETOPT: |
1997 | error = sockopt_getint(sopt, &optval); |
1998 | if (error) |
1999 | break; |
2000 | if ((optval % 2) != 0) { |
2001 | /* the API assumes even offset values */ |
2002 | error = EINVAL; |
2003 | } else if (so->so_proto->pr_protocol == |
2004 | IPPROTO_ICMPV6) { |
2005 | if (optval != icmp6off) |
2006 | error = EINVAL; |
2007 | } else |
2008 | in6p->in6p_cksum = optval; |
2009 | break; |
2010 | |
2011 | case PRCO_GETOPT: |
2012 | if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) |
2013 | optval = icmp6off; |
2014 | else |
2015 | optval = in6p->in6p_cksum; |
2016 | |
2017 | error = sockopt_setint(sopt, optval); |
2018 | break; |
2019 | |
2020 | default: |
2021 | error = EINVAL; |
2022 | break; |
2023 | } |
2024 | break; |
2025 | |
2026 | default: |
2027 | error = ENOPROTOOPT; |
2028 | break; |
2029 | } |
2030 | |
2031 | return (error); |
2032 | } |
2033 | |
2034 | #ifdef RFC2292 |
2035 | /* |
2036 | * Set up IP6 options in pcb for insertion in output packets or |
2037 | * specifying behavior of outgoing packets. |
2038 | */ |
2039 | static int |
2040 | ip6_pcbopts(struct ip6_pktopts **pktopt, struct socket *so, |
2041 | struct sockopt *sopt) |
2042 | { |
2043 | struct ip6_pktopts *opt = *pktopt; |
2044 | struct mbuf *m; |
2045 | int error = 0; |
2046 | |
2047 | /* turn off any old options. */ |
2048 | if (opt) { |
2049 | #ifdef DIAGNOSTIC |
2050 | if (opt->ip6po_pktinfo || opt->ip6po_nexthop || |
2051 | opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || |
2052 | opt->ip6po_rhinfo.ip6po_rhi_rthdr) |
2053 | printf("ip6_pcbopts: all specified options are cleared.\n" ); |
2054 | #endif |
2055 | ip6_clearpktopts(opt, -1); |
2056 | } else { |
2057 | opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT); |
2058 | if (opt == NULL) |
2059 | return (ENOBUFS); |
2060 | } |
2061 | *pktopt = NULL; |
2062 | |
2063 | if (sopt == NULL || sopt->sopt_size == 0) { |
2064 | /* |
2065 | * Only turning off any previous options, regardless of |
2066 | * whether the opt is just created or given. |
2067 | */ |
2068 | free(opt, M_IP6OPT); |
2069 | return (0); |
2070 | } |
2071 | |
2072 | /* set options specified by user. */ |
2073 | m = sockopt_getmbuf(sopt); |
2074 | if (m == NULL) { |
2075 | free(opt, M_IP6OPT); |
2076 | return (ENOBUFS); |
2077 | } |
2078 | |
2079 | error = ip6_setpktopts(m, opt, NULL, kauth_cred_get(), |
2080 | so->so_proto->pr_protocol); |
2081 | m_freem(m); |
2082 | if (error != 0) { |
2083 | ip6_clearpktopts(opt, -1); /* XXX: discard all options */ |
2084 | free(opt, M_IP6OPT); |
2085 | return (error); |
2086 | } |
2087 | *pktopt = opt; |
2088 | return (0); |
2089 | } |
2090 | #endif |
2091 | |
2092 | /* |
2093 | * initialize ip6_pktopts. beware that there are non-zero default values in |
2094 | * the struct. |
2095 | */ |
2096 | void |
2097 | ip6_initpktopts(struct ip6_pktopts *opt) |
2098 | { |
2099 | |
2100 | memset(opt, 0, sizeof(*opt)); |
2101 | opt->ip6po_hlim = -1; /* -1 means default hop limit */ |
2102 | opt->ip6po_tclass = -1; /* -1 means default traffic class */ |
2103 | opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; |
2104 | opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; |
2105 | } |
2106 | |
2107 | #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */ |
2108 | static int |
2109 | ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, |
2110 | kauth_cred_t cred, int uproto) |
2111 | { |
2112 | struct ip6_pktopts *opt; |
2113 | |
2114 | if (*pktopt == NULL) { |
2115 | *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, |
2116 | M_NOWAIT); |
2117 | if (*pktopt == NULL) |
2118 | return (ENOBUFS); |
2119 | |
2120 | ip6_initpktopts(*pktopt); |
2121 | } |
2122 | opt = *pktopt; |
2123 | |
2124 | return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto)); |
2125 | } |
2126 | |
2127 | static int |
2128 | ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt) |
2129 | { |
2130 | void *optdata = NULL; |
2131 | int optdatalen = 0; |
2132 | struct ip6_ext *ip6e; |
2133 | int error = 0; |
2134 | struct in6_pktinfo null_pktinfo; |
2135 | int deftclass = 0, on; |
2136 | int defminmtu = IP6PO_MINMTU_MCASTONLY; |
2137 | int defpreftemp = IP6PO_TEMPADDR_SYSTEM; |
2138 | |
2139 | switch (optname) { |
2140 | case IPV6_PKTINFO: |
2141 | if (pktopt && pktopt->ip6po_pktinfo) |
2142 | optdata = (void *)pktopt->ip6po_pktinfo; |
2143 | else { |
2144 | /* XXX: we don't have to do this every time... */ |
2145 | memset(&null_pktinfo, 0, sizeof(null_pktinfo)); |
2146 | optdata = (void *)&null_pktinfo; |
2147 | } |
2148 | optdatalen = sizeof(struct in6_pktinfo); |
2149 | break; |
2150 | case IPV6_OTCLASS: |
2151 | /* XXX */ |
2152 | return (EINVAL); |
2153 | case IPV6_TCLASS: |
2154 | if (pktopt && pktopt->ip6po_tclass >= 0) |
2155 | optdata = (void *)&pktopt->ip6po_tclass; |
2156 | else |
2157 | optdata = (void *)&deftclass; |
2158 | optdatalen = sizeof(int); |
2159 | break; |
2160 | case IPV6_HOPOPTS: |
2161 | if (pktopt && pktopt->ip6po_hbh) { |
2162 | optdata = (void *)pktopt->ip6po_hbh; |
2163 | ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; |
2164 | optdatalen = (ip6e->ip6e_len + 1) << 3; |
2165 | } |
2166 | break; |
2167 | case IPV6_RTHDR: |
2168 | if (pktopt && pktopt->ip6po_rthdr) { |
2169 | optdata = (void *)pktopt->ip6po_rthdr; |
2170 | ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; |
2171 | optdatalen = (ip6e->ip6e_len + 1) << 3; |
2172 | } |
2173 | break; |
2174 | case IPV6_RTHDRDSTOPTS: |
2175 | if (pktopt && pktopt->ip6po_dest1) { |
2176 | optdata = (void *)pktopt->ip6po_dest1; |
2177 | ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; |
2178 | optdatalen = (ip6e->ip6e_len + 1) << 3; |
2179 | } |
2180 | break; |
2181 | case IPV6_DSTOPTS: |
2182 | if (pktopt && pktopt->ip6po_dest2) { |
2183 | optdata = (void *)pktopt->ip6po_dest2; |
2184 | ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; |
2185 | optdatalen = (ip6e->ip6e_len + 1) << 3; |
2186 | } |
2187 | break; |
2188 | case IPV6_NEXTHOP: |
2189 | if (pktopt && pktopt->ip6po_nexthop) { |
2190 | optdata = (void *)pktopt->ip6po_nexthop; |
2191 | optdatalen = pktopt->ip6po_nexthop->sa_len; |
2192 | } |
2193 | break; |
2194 | case IPV6_USE_MIN_MTU: |
2195 | if (pktopt) |
2196 | optdata = (void *)&pktopt->ip6po_minmtu; |
2197 | else |
2198 | optdata = (void *)&defminmtu; |
2199 | optdatalen = sizeof(int); |
2200 | break; |
2201 | case IPV6_DONTFRAG: |
2202 | if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) |
2203 | on = 1; |
2204 | else |
2205 | on = 0; |
2206 | optdata = (void *)&on; |
2207 | optdatalen = sizeof(on); |
2208 | break; |
2209 | case IPV6_PREFER_TEMPADDR: |
2210 | if (pktopt) |
2211 | optdata = (void *)&pktopt->ip6po_prefer_tempaddr; |
2212 | else |
2213 | optdata = (void *)&defpreftemp; |
2214 | optdatalen = sizeof(int); |
2215 | break; |
2216 | default: /* should not happen */ |
2217 | #ifdef DIAGNOSTIC |
2218 | panic("ip6_getpcbopt: unexpected option\n" ); |
2219 | #endif |
2220 | return (ENOPROTOOPT); |
2221 | } |
2222 | |
2223 | error = sockopt_set(sopt, optdata, optdatalen); |
2224 | |
2225 | return (error); |
2226 | } |
2227 | |
2228 | void |
2229 | ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname) |
2230 | { |
2231 | if (optname == -1 || optname == IPV6_PKTINFO) { |
2232 | if (pktopt->ip6po_pktinfo) |
2233 | free(pktopt->ip6po_pktinfo, M_IP6OPT); |
2234 | pktopt->ip6po_pktinfo = NULL; |
2235 | } |
2236 | if (optname == -1 || optname == IPV6_HOPLIMIT) |
2237 | pktopt->ip6po_hlim = -1; |
2238 | if (optname == -1 || optname == IPV6_TCLASS) |
2239 | pktopt->ip6po_tclass = -1; |
2240 | if (optname == -1 || optname == IPV6_NEXTHOP) { |
2241 | rtcache_free(&pktopt->ip6po_nextroute); |
2242 | if (pktopt->ip6po_nexthop) |
2243 | free(pktopt->ip6po_nexthop, M_IP6OPT); |
2244 | pktopt->ip6po_nexthop = NULL; |
2245 | } |
2246 | if (optname == -1 || optname == IPV6_HOPOPTS) { |
2247 | if (pktopt->ip6po_hbh) |
2248 | free(pktopt->ip6po_hbh, M_IP6OPT); |
2249 | pktopt->ip6po_hbh = NULL; |
2250 | } |
2251 | if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { |
2252 | if (pktopt->ip6po_dest1) |
2253 | free(pktopt->ip6po_dest1, M_IP6OPT); |
2254 | pktopt->ip6po_dest1 = NULL; |
2255 | } |
2256 | if (optname == -1 || optname == IPV6_RTHDR) { |
2257 | if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) |
2258 | free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); |
2259 | pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; |
2260 | rtcache_free(&pktopt->ip6po_route); |
2261 | } |
2262 | if (optname == -1 || optname == IPV6_DSTOPTS) { |
2263 | if (pktopt->ip6po_dest2) |
2264 | free(pktopt->ip6po_dest2, M_IP6OPT); |
2265 | pktopt->ip6po_dest2 = NULL; |
2266 | } |
2267 | } |
2268 | |
2269 | #define PKTOPT_EXTHDRCPY(type) \ |
2270 | do { \ |
2271 | if (src->type) { \ |
2272 | int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ |
2273 | dst->type = malloc(hlen, M_IP6OPT, canwait); \ |
2274 | if (dst->type == NULL) \ |
2275 | goto bad; \ |
2276 | memcpy(dst->type, src->type, hlen); \ |
2277 | } \ |
2278 | } while (/*CONSTCOND*/ 0) |
2279 | |
2280 | static int |
2281 | copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait) |
2282 | { |
2283 | dst->ip6po_hlim = src->ip6po_hlim; |
2284 | dst->ip6po_tclass = src->ip6po_tclass; |
2285 | dst->ip6po_flags = src->ip6po_flags; |
2286 | dst->ip6po_minmtu = src->ip6po_minmtu; |
2287 | dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr; |
2288 | if (src->ip6po_pktinfo) { |
2289 | dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), |
2290 | M_IP6OPT, canwait); |
2291 | if (dst->ip6po_pktinfo == NULL) |
2292 | goto bad; |
2293 | *dst->ip6po_pktinfo = *src->ip6po_pktinfo; |
2294 | } |
2295 | if (src->ip6po_nexthop) { |
2296 | dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, |
2297 | M_IP6OPT, canwait); |
2298 | if (dst->ip6po_nexthop == NULL) |
2299 | goto bad; |
2300 | memcpy(dst->ip6po_nexthop, src->ip6po_nexthop, |
2301 | src->ip6po_nexthop->sa_len); |
2302 | } |
2303 | PKTOPT_EXTHDRCPY(ip6po_hbh); |
2304 | PKTOPT_EXTHDRCPY(ip6po_dest1); |
2305 | PKTOPT_EXTHDRCPY(ip6po_dest2); |
2306 | PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ |
2307 | return (0); |
2308 | |
2309 | bad: |
2310 | if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); |
2311 | if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); |
2312 | if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); |
2313 | if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); |
2314 | if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); |
2315 | if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); |
2316 | |
2317 | return (ENOBUFS); |
2318 | } |
2319 | #undef PKTOPT_EXTHDRCPY |
2320 | |
2321 | struct ip6_pktopts * |
2322 | ip6_copypktopts(struct ip6_pktopts *src, int canwait) |
2323 | { |
2324 | int error; |
2325 | struct ip6_pktopts *dst; |
2326 | |
2327 | dst = malloc(sizeof(*dst), M_IP6OPT, canwait); |
2328 | if (dst == NULL) |
2329 | return (NULL); |
2330 | ip6_initpktopts(dst); |
2331 | |
2332 | if ((error = copypktopts(dst, src, canwait)) != 0) { |
2333 | free(dst, M_IP6OPT); |
2334 | return (NULL); |
2335 | } |
2336 | |
2337 | return (dst); |
2338 | } |
2339 | |
2340 | void |
2341 | ip6_freepcbopts(struct ip6_pktopts *pktopt) |
2342 | { |
2343 | if (pktopt == NULL) |
2344 | return; |
2345 | |
2346 | ip6_clearpktopts(pktopt, -1); |
2347 | |
2348 | free(pktopt, M_IP6OPT); |
2349 | } |
2350 | |
2351 | int |
2352 | ip6_get_membership(const struct sockopt *sopt, struct ifnet **ifp, void *v, |
2353 | size_t l) |
2354 | { |
2355 | struct ipv6_mreq mreq; |
2356 | int error; |
2357 | struct in6_addr *ia = &mreq.ipv6mr_multiaddr; |
2358 | struct in_addr *ia4 = (void *)&ia->s6_addr32[3]; |
2359 | error = sockopt_get(sopt, &mreq, sizeof(mreq)); |
2360 | if (error != 0) |
2361 | return error; |
2362 | |
2363 | if (IN6_IS_ADDR_UNSPECIFIED(ia)) { |
2364 | /* |
2365 | * We use the unspecified address to specify to accept |
2366 | * all multicast addresses. Only super user is allowed |
2367 | * to do this. |
2368 | */ |
2369 | if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_IPV6, |
2370 | KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICAST, NULL, NULL, NULL)) |
2371 | return EACCES; |
2372 | } else if (IN6_IS_ADDR_V4MAPPED(ia)) { |
2373 | // Don't bother if we are not going to use ifp. |
2374 | if (l == sizeof(*ia)) { |
2375 | memcpy(v, ia, l); |
2376 | return 0; |
2377 | } |
2378 | } else if (!IN6_IS_ADDR_MULTICAST(ia)) { |
2379 | return EINVAL; |
2380 | } |
2381 | |
2382 | /* |
2383 | * If no interface was explicitly specified, choose an |
2384 | * appropriate one according to the given multicast address. |
2385 | */ |
2386 | if (mreq.ipv6mr_interface == 0) { |
2387 | struct rtentry *rt; |
2388 | union { |
2389 | struct sockaddr dst; |
2390 | struct sockaddr_in dst4; |
2391 | struct sockaddr_in6 dst6; |
2392 | } u; |
2393 | struct route ro; |
2394 | |
2395 | /* |
2396 | * Look up the routing table for the |
2397 | * address, and choose the outgoing interface. |
2398 | * XXX: is it a good approach? |
2399 | */ |
2400 | memset(&ro, 0, sizeof(ro)); |
2401 | if (IN6_IS_ADDR_V4MAPPED(ia)) |
2402 | sockaddr_in_init(&u.dst4, ia4, 0); |
2403 | else |
2404 | sockaddr_in6_init(&u.dst6, ia, 0, 0, 0); |
2405 | error = rtcache_setdst(&ro, &u.dst); |
2406 | if (error != 0) |
2407 | return error; |
2408 | *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL; |
2409 | rtcache_free(&ro); |
2410 | } else { |
2411 | /* |
2412 | * If the interface is specified, validate it. |
2413 | */ |
2414 | if ((*ifp = if_byindex(mreq.ipv6mr_interface)) == NULL) |
2415 | return ENXIO; /* XXX EINVAL? */ |
2416 | } |
2417 | if (sizeof(*ia) == l) |
2418 | memcpy(v, ia, l); |
2419 | else |
2420 | memcpy(v, ia4, l); |
2421 | return 0; |
2422 | } |
2423 | |
2424 | /* |
2425 | * Set the IP6 multicast options in response to user setsockopt(). |
2426 | */ |
2427 | static int |
2428 | ip6_setmoptions(const struct sockopt *sopt, struct in6pcb *in6p) |
2429 | { |
2430 | int error = 0; |
2431 | u_int loop, ifindex; |
2432 | struct ipv6_mreq mreq; |
2433 | struct in6_addr ia; |
2434 | struct ifnet *ifp; |
2435 | struct ip6_moptions *im6o = in6p->in6p_moptions; |
2436 | struct in6_multi_mship *imm; |
2437 | |
2438 | if (im6o == NULL) { |
2439 | /* |
2440 | * No multicast option buffer attached to the pcb; |
2441 | * allocate one and initialize to default values. |
2442 | */ |
2443 | im6o = malloc(sizeof(*im6o), M_IPMOPTS, M_NOWAIT); |
2444 | if (im6o == NULL) |
2445 | return (ENOBUFS); |
2446 | in6p->in6p_moptions = im6o; |
2447 | im6o->im6o_multicast_if_index = 0; |
2448 | im6o->im6o_multicast_hlim = ip6_defmcasthlim; |
2449 | im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; |
2450 | LIST_INIT(&im6o->im6o_memberships); |
2451 | } |
2452 | |
2453 | switch (sopt->sopt_name) { |
2454 | |
2455 | case IPV6_MULTICAST_IF: |
2456 | /* |
2457 | * Select the interface for outgoing multicast packets. |
2458 | */ |
2459 | error = sockopt_get(sopt, &ifindex, sizeof(ifindex)); |
2460 | if (error != 0) |
2461 | break; |
2462 | |
2463 | if (ifindex != 0) { |
2464 | if ((ifp = if_byindex(ifindex)) == NULL) { |
2465 | error = ENXIO; /* XXX EINVAL? */ |
2466 | break; |
2467 | } |
2468 | if ((ifp->if_flags & IFF_MULTICAST) == 0) { |
2469 | error = EADDRNOTAVAIL; |
2470 | break; |
2471 | } |
2472 | } else |
2473 | ifp = NULL; |
2474 | im6o->im6o_multicast_if_index = if_get_index(ifp); |
2475 | break; |
2476 | |
2477 | case IPV6_MULTICAST_HOPS: |
2478 | { |
2479 | /* |
2480 | * Set the IP6 hoplimit for outgoing multicast packets. |
2481 | */ |
2482 | int optval; |
2483 | |
2484 | error = sockopt_getint(sopt, &optval); |
2485 | if (error != 0) |
2486 | break; |
2487 | |
2488 | if (optval < -1 || optval >= 256) |
2489 | error = EINVAL; |
2490 | else if (optval == -1) |
2491 | im6o->im6o_multicast_hlim = ip6_defmcasthlim; |
2492 | else |
2493 | im6o->im6o_multicast_hlim = optval; |
2494 | break; |
2495 | } |
2496 | |
2497 | case IPV6_MULTICAST_LOOP: |
2498 | /* |
2499 | * Set the loopback flag for outgoing multicast packets. |
2500 | * Must be zero or one. |
2501 | */ |
2502 | error = sockopt_get(sopt, &loop, sizeof(loop)); |
2503 | if (error != 0) |
2504 | break; |
2505 | if (loop > 1) { |
2506 | error = EINVAL; |
2507 | break; |
2508 | } |
2509 | im6o->im6o_multicast_loop = loop; |
2510 | break; |
2511 | |
2512 | case IPV6_JOIN_GROUP: |
2513 | /* |
2514 | * Add a multicast group membership. |
2515 | * Group must be a valid IP6 multicast address. |
2516 | */ |
2517 | if ((error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia)))) |
2518 | return error; |
2519 | |
2520 | if (IN6_IS_ADDR_V4MAPPED(&ia)) { |
2521 | error = ip_setmoptions(&in6p->in6p_v4moptions, sopt); |
2522 | break; |
2523 | } |
2524 | /* |
2525 | * See if we found an interface, and confirm that it |
2526 | * supports multicast |
2527 | */ |
2528 | if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { |
2529 | error = EADDRNOTAVAIL; |
2530 | break; |
2531 | } |
2532 | |
2533 | if (in6_setscope(&ia, ifp, NULL)) { |
2534 | error = EADDRNOTAVAIL; /* XXX: should not happen */ |
2535 | break; |
2536 | } |
2537 | |
2538 | /* |
2539 | * See if the membership already exists. |
2540 | */ |
2541 | for (imm = im6o->im6o_memberships.lh_first; |
2542 | imm != NULL; imm = imm->i6mm_chain.le_next) |
2543 | if (imm->i6mm_maddr->in6m_ifp == ifp && |
2544 | IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, |
2545 | &ia)) |
2546 | break; |
2547 | if (imm != NULL) { |
2548 | error = EADDRINUSE; |
2549 | break; |
2550 | } |
2551 | /* |
2552 | * Everything looks good; add a new record to the multicast |
2553 | * address list for the given interface. |
2554 | */ |
2555 | imm = in6_joingroup(ifp, &ia, &error, 0); |
2556 | if (imm == NULL) |
2557 | break; |
2558 | LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); |
2559 | break; |
2560 | |
2561 | case IPV6_LEAVE_GROUP: |
2562 | /* |
2563 | * Drop a multicast group membership. |
2564 | * Group must be a valid IP6 multicast address. |
2565 | */ |
2566 | error = sockopt_get(sopt, &mreq, sizeof(mreq)); |
2567 | if (error != 0) |
2568 | break; |
2569 | |
2570 | if (IN6_IS_ADDR_V4MAPPED(&mreq.ipv6mr_multiaddr)) { |
2571 | error = ip_setmoptions(&in6p->in6p_v4moptions, sopt); |
2572 | break; |
2573 | } |
2574 | /* |
2575 | * If an interface address was specified, get a pointer |
2576 | * to its ifnet structure. |
2577 | */ |
2578 | if (mreq.ipv6mr_interface != 0) { |
2579 | if ((ifp = if_byindex(mreq.ipv6mr_interface)) == NULL) { |
2580 | error = ENXIO; /* XXX EINVAL? */ |
2581 | break; |
2582 | } |
2583 | } else |
2584 | ifp = NULL; |
2585 | |
2586 | /* Fill in the scope zone ID */ |
2587 | if (ifp) { |
2588 | if (in6_setscope(&mreq.ipv6mr_multiaddr, ifp, NULL)) { |
2589 | /* XXX: should not happen */ |
2590 | error = EADDRNOTAVAIL; |
2591 | break; |
2592 | } |
2593 | } else if (mreq.ipv6mr_interface != 0) { |
2594 | /* |
2595 | * XXX: This case would happens when the (positive) |
2596 | * index is in the valid range, but the corresponding |
2597 | * interface has been detached dynamically. The above |
2598 | * check probably avoids such case to happen here, but |
2599 | * we check it explicitly for safety. |
2600 | */ |
2601 | error = EADDRNOTAVAIL; |
2602 | break; |
2603 | } else { /* ipv6mr_interface == 0 */ |
2604 | struct sockaddr_in6 sa6_mc; |
2605 | |
2606 | /* |
2607 | * The API spec says as follows: |
2608 | * If the interface index is specified as 0, the |
2609 | * system may choose a multicast group membership to |
2610 | * drop by matching the multicast address only. |
2611 | * On the other hand, we cannot disambiguate the scope |
2612 | * zone unless an interface is provided. Thus, we |
2613 | * check if there's ambiguity with the default scope |
2614 | * zone as the last resort. |
2615 | */ |
2616 | sockaddr_in6_init(&sa6_mc, &mreq.ipv6mr_multiaddr, |
2617 | 0, 0, 0); |
2618 | error = sa6_embedscope(&sa6_mc, ip6_use_defzone); |
2619 | if (error != 0) |
2620 | break; |
2621 | mreq.ipv6mr_multiaddr = sa6_mc.sin6_addr; |
2622 | } |
2623 | |
2624 | /* |
2625 | * Find the membership in the membership list. |
2626 | */ |
2627 | for (imm = im6o->im6o_memberships.lh_first; |
2628 | imm != NULL; imm = imm->i6mm_chain.le_next) { |
2629 | if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && |
2630 | IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, |
2631 | &mreq.ipv6mr_multiaddr)) |
2632 | break; |
2633 | } |
2634 | if (imm == NULL) { |
2635 | /* Unable to resolve interface */ |
2636 | error = EADDRNOTAVAIL; |
2637 | break; |
2638 | } |
2639 | /* |
2640 | * Give up the multicast address record to which the |
2641 | * membership points. |
2642 | */ |
2643 | LIST_REMOVE(imm, i6mm_chain); |
2644 | in6_leavegroup(imm); |
2645 | break; |
2646 | |
2647 | default: |
2648 | error = EOPNOTSUPP; |
2649 | break; |
2650 | } |
2651 | |
2652 | /* |
2653 | * If all options have default values, no need to keep the mbuf. |
2654 | */ |
2655 | if (im6o->im6o_multicast_if_index == 0 && |
2656 | im6o->im6o_multicast_hlim == ip6_defmcasthlim && |
2657 | im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && |
2658 | im6o->im6o_memberships.lh_first == NULL) { |
2659 | free(in6p->in6p_moptions, M_IPMOPTS); |
2660 | in6p->in6p_moptions = NULL; |
2661 | } |
2662 | |
2663 | return (error); |
2664 | } |
2665 | |
2666 | /* |
2667 | * Return the IP6 multicast options in response to user getsockopt(). |
2668 | */ |
2669 | static int |
2670 | ip6_getmoptions(struct sockopt *sopt, struct in6pcb *in6p) |
2671 | { |
2672 | u_int optval; |
2673 | int error; |
2674 | struct ip6_moptions *im6o = in6p->in6p_moptions; |
2675 | |
2676 | switch (sopt->sopt_name) { |
2677 | case IPV6_MULTICAST_IF: |
2678 | if (im6o == NULL || im6o->im6o_multicast_if_index == 0) |
2679 | optval = 0; |
2680 | else |
2681 | optval = im6o->im6o_multicast_if_index; |
2682 | |
2683 | error = sockopt_set(sopt, &optval, sizeof(optval)); |
2684 | break; |
2685 | |
2686 | case IPV6_MULTICAST_HOPS: |
2687 | if (im6o == NULL) |
2688 | optval = ip6_defmcasthlim; |
2689 | else |
2690 | optval = im6o->im6o_multicast_hlim; |
2691 | |
2692 | error = sockopt_set(sopt, &optval, sizeof(optval)); |
2693 | break; |
2694 | |
2695 | case IPV6_MULTICAST_LOOP: |
2696 | if (im6o == NULL) |
2697 | optval = IPV6_DEFAULT_MULTICAST_LOOP; |
2698 | else |
2699 | optval = im6o->im6o_multicast_loop; |
2700 | |
2701 | error = sockopt_set(sopt, &optval, sizeof(optval)); |
2702 | break; |
2703 | |
2704 | default: |
2705 | error = EOPNOTSUPP; |
2706 | } |
2707 | |
2708 | return (error); |
2709 | } |
2710 | |
2711 | /* |
2712 | * Discard the IP6 multicast options. |
2713 | */ |
2714 | void |
2715 | ip6_freemoptions(struct ip6_moptions *im6o) |
2716 | { |
2717 | struct in6_multi_mship *imm; |
2718 | |
2719 | if (im6o == NULL) |
2720 | return; |
2721 | |
2722 | while ((imm = im6o->im6o_memberships.lh_first) != NULL) { |
2723 | LIST_REMOVE(imm, i6mm_chain); |
2724 | in6_leavegroup(imm); |
2725 | } |
2726 | free(im6o, M_IPMOPTS); |
2727 | } |
2728 | |
2729 | /* |
2730 | * Set IPv6 outgoing packet options based on advanced API. |
2731 | */ |
2732 | int |
2733 | ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt, |
2734 | struct ip6_pktopts *stickyopt, kauth_cred_t cred, int uproto) |
2735 | { |
2736 | struct cmsghdr *cm = 0; |
2737 | |
2738 | if (control == NULL || opt == NULL) |
2739 | return (EINVAL); |
2740 | |
2741 | ip6_initpktopts(opt); |
2742 | if (stickyopt) { |
2743 | int error; |
2744 | |
2745 | /* |
2746 | * If stickyopt is provided, make a local copy of the options |
2747 | * for this particular packet, then override them by ancillary |
2748 | * objects. |
2749 | * XXX: copypktopts() does not copy the cached route to a next |
2750 | * hop (if any). This is not very good in terms of efficiency, |
2751 | * but we can allow this since this option should be rarely |
2752 | * used. |
2753 | */ |
2754 | if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0) |
2755 | return (error); |
2756 | } |
2757 | |
2758 | /* |
2759 | * XXX: Currently, we assume all the optional information is stored |
2760 | * in a single mbuf. |
2761 | */ |
2762 | if (control->m_next) |
2763 | return (EINVAL); |
2764 | |
2765 | /* XXX if cm->cmsg_len is not aligned, control->m_len can become <0 */ |
2766 | for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), |
2767 | control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { |
2768 | int error; |
2769 | |
2770 | if (control->m_len < CMSG_LEN(0)) |
2771 | return (EINVAL); |
2772 | |
2773 | cm = mtod(control, struct cmsghdr *); |
2774 | if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) |
2775 | return (EINVAL); |
2776 | if (cm->cmsg_level != IPPROTO_IPV6) |
2777 | continue; |
2778 | |
2779 | error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm), |
2780 | cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto); |
2781 | if (error) |
2782 | return (error); |
2783 | } |
2784 | |
2785 | return (0); |
2786 | } |
2787 | |
2788 | /* |
2789 | * Set a particular packet option, as a sticky option or an ancillary data |
2790 | * item. "len" can be 0 only when it's a sticky option. |
2791 | * We have 4 cases of combination of "sticky" and "cmsg": |
2792 | * "sticky=0, cmsg=0": impossible |
2793 | * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data |
2794 | * "sticky=1, cmsg=0": RFC3542 socket option |
2795 | * "sticky=1, cmsg=1": RFC2292 socket option |
2796 | */ |
2797 | static int |
2798 | ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt, |
2799 | kauth_cred_t cred, int sticky, int cmsg, int uproto) |
2800 | { |
2801 | int minmtupolicy; |
2802 | int error; |
2803 | |
2804 | if (!sticky && !cmsg) { |
2805 | #ifdef DIAGNOSTIC |
2806 | printf("ip6_setpktopt: impossible case\n" ); |
2807 | #endif |
2808 | return (EINVAL); |
2809 | } |
2810 | |
2811 | /* |
2812 | * IPV6_2292xxx is for backward compatibility to RFC2292, and should |
2813 | * not be specified in the context of RFC3542. Conversely, |
2814 | * RFC3542 types should not be specified in the context of RFC2292. |
2815 | */ |
2816 | if (!cmsg) { |
2817 | switch (optname) { |
2818 | case IPV6_2292PKTINFO: |
2819 | case IPV6_2292HOPLIMIT: |
2820 | case IPV6_2292NEXTHOP: |
2821 | case IPV6_2292HOPOPTS: |
2822 | case IPV6_2292DSTOPTS: |
2823 | case IPV6_2292RTHDR: |
2824 | case IPV6_2292PKTOPTIONS: |
2825 | return (ENOPROTOOPT); |
2826 | } |
2827 | } |
2828 | if (sticky && cmsg) { |
2829 | switch (optname) { |
2830 | case IPV6_PKTINFO: |
2831 | case IPV6_HOPLIMIT: |
2832 | case IPV6_NEXTHOP: |
2833 | case IPV6_HOPOPTS: |
2834 | case IPV6_DSTOPTS: |
2835 | case IPV6_RTHDRDSTOPTS: |
2836 | case IPV6_RTHDR: |
2837 | case IPV6_USE_MIN_MTU: |
2838 | case IPV6_DONTFRAG: |
2839 | case IPV6_OTCLASS: |
2840 | case IPV6_TCLASS: |
2841 | case IPV6_PREFER_TEMPADDR: /* XXX not an RFC3542 option */ |
2842 | return (ENOPROTOOPT); |
2843 | } |
2844 | } |
2845 | |
2846 | switch (optname) { |
2847 | #ifdef RFC2292 |
2848 | case IPV6_2292PKTINFO: |
2849 | #endif |
2850 | case IPV6_PKTINFO: |
2851 | { |
2852 | struct in6_pktinfo *pktinfo; |
2853 | |
2854 | if (len != sizeof(struct in6_pktinfo)) |
2855 | return (EINVAL); |
2856 | |
2857 | pktinfo = (struct in6_pktinfo *)buf; |
2858 | |
2859 | /* |
2860 | * An application can clear any sticky IPV6_PKTINFO option by |
2861 | * doing a "regular" setsockopt with ipi6_addr being |
2862 | * in6addr_any and ipi6_ifindex being zero. |
2863 | * [RFC 3542, Section 6] |
2864 | */ |
2865 | if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && |
2866 | pktinfo->ipi6_ifindex == 0 && |
2867 | IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { |
2868 | ip6_clearpktopts(opt, optname); |
2869 | break; |
2870 | } |
2871 | |
2872 | if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && |
2873 | sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { |
2874 | return (EINVAL); |
2875 | } |
2876 | |
2877 | /* Validate the interface index if specified. */ |
2878 | if (pktinfo->ipi6_ifindex) { |
2879 | struct ifnet *ifp; |
2880 | int s = pserialize_read_enter(); |
2881 | ifp = if_byindex(pktinfo->ipi6_ifindex); |
2882 | if (ifp == NULL) { |
2883 | pserialize_read_exit(s); |
2884 | return ENXIO; |
2885 | } |
2886 | pserialize_read_exit(s); |
2887 | } |
2888 | |
2889 | /* |
2890 | * We store the address anyway, and let in6_selectsrc() |
2891 | * validate the specified address. This is because ipi6_addr |
2892 | * may not have enough information about its scope zone, and |
2893 | * we may need additional information (such as outgoing |
2894 | * interface or the scope zone of a destination address) to |
2895 | * disambiguate the scope. |
2896 | * XXX: the delay of the validation may confuse the |
2897 | * application when it is used as a sticky option. |
2898 | */ |
2899 | if (opt->ip6po_pktinfo == NULL) { |
2900 | opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), |
2901 | M_IP6OPT, M_NOWAIT); |
2902 | if (opt->ip6po_pktinfo == NULL) |
2903 | return (ENOBUFS); |
2904 | } |
2905 | memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo)); |
2906 | break; |
2907 | } |
2908 | |
2909 | #ifdef RFC2292 |
2910 | case IPV6_2292HOPLIMIT: |
2911 | #endif |
2912 | case IPV6_HOPLIMIT: |
2913 | { |
2914 | int *hlimp; |
2915 | |
2916 | /* |
2917 | * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT |
2918 | * to simplify the ordering among hoplimit options. |
2919 | */ |
2920 | if (optname == IPV6_HOPLIMIT && sticky) |
2921 | return (ENOPROTOOPT); |
2922 | |
2923 | if (len != sizeof(int)) |
2924 | return (EINVAL); |
2925 | hlimp = (int *)buf; |
2926 | if (*hlimp < -1 || *hlimp > 255) |
2927 | return (EINVAL); |
2928 | |
2929 | opt->ip6po_hlim = *hlimp; |
2930 | break; |
2931 | } |
2932 | |
2933 | case IPV6_OTCLASS: |
2934 | if (len != sizeof(u_int8_t)) |
2935 | return (EINVAL); |
2936 | |
2937 | opt->ip6po_tclass = *(u_int8_t *)buf; |
2938 | break; |
2939 | |
2940 | case IPV6_TCLASS: |
2941 | { |
2942 | int tclass; |
2943 | |
2944 | if (len != sizeof(int)) |
2945 | return (EINVAL); |
2946 | tclass = *(int *)buf; |
2947 | if (tclass < -1 || tclass > 255) |
2948 | return (EINVAL); |
2949 | |
2950 | opt->ip6po_tclass = tclass; |
2951 | break; |
2952 | } |
2953 | |
2954 | #ifdef RFC2292 |
2955 | case IPV6_2292NEXTHOP: |
2956 | #endif |
2957 | case IPV6_NEXTHOP: |
2958 | error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6, |
2959 | KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); |
2960 | if (error) |
2961 | return (error); |
2962 | |
2963 | if (len == 0) { /* just remove the option */ |
2964 | ip6_clearpktopts(opt, IPV6_NEXTHOP); |
2965 | break; |
2966 | } |
2967 | |
2968 | /* check if cmsg_len is large enough for sa_len */ |
2969 | if (len < sizeof(struct sockaddr) || len < *buf) |
2970 | return (EINVAL); |
2971 | |
2972 | switch (((struct sockaddr *)buf)->sa_family) { |
2973 | case AF_INET6: |
2974 | { |
2975 | struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; |
2976 | |
2977 | if (sa6->sin6_len != sizeof(struct sockaddr_in6)) |
2978 | return (EINVAL); |
2979 | |
2980 | if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || |
2981 | IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { |
2982 | return (EINVAL); |
2983 | } |
2984 | if ((error = sa6_embedscope(sa6, ip6_use_defzone)) |
2985 | != 0) { |
2986 | return (error); |
2987 | } |
2988 | break; |
2989 | } |
2990 | case AF_LINK: /* eventually be supported? */ |
2991 | default: |
2992 | return (EAFNOSUPPORT); |
2993 | } |
2994 | |
2995 | /* turn off the previous option, then set the new option. */ |
2996 | ip6_clearpktopts(opt, IPV6_NEXTHOP); |
2997 | opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT); |
2998 | if (opt->ip6po_nexthop == NULL) |
2999 | return (ENOBUFS); |
3000 | memcpy(opt->ip6po_nexthop, buf, *buf); |
3001 | break; |
3002 | |
3003 | #ifdef RFC2292 |
3004 | case IPV6_2292HOPOPTS: |
3005 | #endif |
3006 | case IPV6_HOPOPTS: |
3007 | { |
3008 | struct ip6_hbh *hbh; |
3009 | int hbhlen; |
3010 | |
3011 | /* |
3012 | * XXX: We don't allow a non-privileged user to set ANY HbH |
3013 | * options, since per-option restriction has too much |
3014 | * overhead. |
3015 | */ |
3016 | error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6, |
3017 | KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); |
3018 | if (error) |
3019 | return (error); |
3020 | |
3021 | if (len == 0) { |
3022 | ip6_clearpktopts(opt, IPV6_HOPOPTS); |
3023 | break; /* just remove the option */ |
3024 | } |
3025 | |
3026 | /* message length validation */ |
3027 | if (len < sizeof(struct ip6_hbh)) |
3028 | return (EINVAL); |
3029 | hbh = (struct ip6_hbh *)buf; |
3030 | hbhlen = (hbh->ip6h_len + 1) << 3; |
3031 | if (len != hbhlen) |
3032 | return (EINVAL); |
3033 | |
3034 | /* turn off the previous option, then set the new option. */ |
3035 | ip6_clearpktopts(opt, IPV6_HOPOPTS); |
3036 | opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT); |
3037 | if (opt->ip6po_hbh == NULL) |
3038 | return (ENOBUFS); |
3039 | memcpy(opt->ip6po_hbh, hbh, hbhlen); |
3040 | |
3041 | break; |
3042 | } |
3043 | |
3044 | #ifdef RFC2292 |
3045 | case IPV6_2292DSTOPTS: |
3046 | #endif |
3047 | case IPV6_DSTOPTS: |
3048 | case IPV6_RTHDRDSTOPTS: |
3049 | { |
3050 | struct ip6_dest *dest, **newdest = NULL; |
3051 | int destlen; |
3052 | |
3053 | /* XXX: see the comment for IPV6_HOPOPTS */ |
3054 | error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6, |
3055 | KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL); |
3056 | if (error) |
3057 | return (error); |
3058 | |
3059 | if (len == 0) { |
3060 | ip6_clearpktopts(opt, optname); |
3061 | break; /* just remove the option */ |
3062 | } |
3063 | |
3064 | /* message length validation */ |
3065 | if (len < sizeof(struct ip6_dest)) |
3066 | return (EINVAL); |
3067 | dest = (struct ip6_dest *)buf; |
3068 | destlen = (dest->ip6d_len + 1) << 3; |
3069 | if (len != destlen) |
3070 | return (EINVAL); |
3071 | /* |
3072 | * Determine the position that the destination options header |
3073 | * should be inserted; before or after the routing header. |
3074 | */ |
3075 | switch (optname) { |
3076 | case IPV6_2292DSTOPTS: |
3077 | /* |
3078 | * The old advanced API is ambiguous on this point. |
3079 | * Our approach is to determine the position based |
3080 | * according to the existence of a routing header. |
3081 | * Note, however, that this depends on the order of the |
3082 | * extension headers in the ancillary data; the 1st |
3083 | * part of the destination options header must appear |
3084 | * before the routing header in the ancillary data, |
3085 | * too. |
3086 | * RFC3542 solved the ambiguity by introducing |
3087 | * separate ancillary data or option types. |
3088 | */ |
3089 | if (opt->ip6po_rthdr == NULL) |
3090 | newdest = &opt->ip6po_dest1; |
3091 | else |
3092 | newdest = &opt->ip6po_dest2; |
3093 | break; |
3094 | case IPV6_RTHDRDSTOPTS: |
3095 | newdest = &opt->ip6po_dest1; |
3096 | break; |
3097 | case IPV6_DSTOPTS: |
3098 | newdest = &opt->ip6po_dest2; |
3099 | break; |
3100 | } |
3101 | |
3102 | /* turn off the previous option, then set the new option. */ |
3103 | ip6_clearpktopts(opt, optname); |
3104 | *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT); |
3105 | if (*newdest == NULL) |
3106 | return (ENOBUFS); |
3107 | memcpy(*newdest, dest, destlen); |
3108 | |
3109 | break; |
3110 | } |
3111 | |
3112 | #ifdef RFC2292 |
3113 | case IPV6_2292RTHDR: |
3114 | #endif |
3115 | case IPV6_RTHDR: |
3116 | { |
3117 | struct ip6_rthdr *rth; |
3118 | int rthlen; |
3119 | |
3120 | if (len == 0) { |
3121 | ip6_clearpktopts(opt, IPV6_RTHDR); |
3122 | break; /* just remove the option */ |
3123 | } |
3124 | |
3125 | /* message length validation */ |
3126 | if (len < sizeof(struct ip6_rthdr)) |
3127 | return (EINVAL); |
3128 | rth = (struct ip6_rthdr *)buf; |
3129 | rthlen = (rth->ip6r_len + 1) << 3; |
3130 | if (len != rthlen) |
3131 | return (EINVAL); |
3132 | switch (rth->ip6r_type) { |
3133 | case IPV6_RTHDR_TYPE_0: |
3134 | if (rth->ip6r_len == 0) /* must contain one addr */ |
3135 | return (EINVAL); |
3136 | if (rth->ip6r_len % 2) /* length must be even */ |
3137 | return (EINVAL); |
3138 | if (rth->ip6r_len / 2 != rth->ip6r_segleft) |
3139 | return (EINVAL); |
3140 | break; |
3141 | default: |
3142 | return (EINVAL); /* not supported */ |
3143 | } |
3144 | /* turn off the previous option */ |
3145 | ip6_clearpktopts(opt, IPV6_RTHDR); |
3146 | opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT); |
3147 | if (opt->ip6po_rthdr == NULL) |
3148 | return (ENOBUFS); |
3149 | memcpy(opt->ip6po_rthdr, rth, rthlen); |
3150 | break; |
3151 | } |
3152 | |
3153 | case IPV6_USE_MIN_MTU: |
3154 | if (len != sizeof(int)) |
3155 | return (EINVAL); |
3156 | minmtupolicy = *(int *)buf; |
3157 | if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && |
3158 | minmtupolicy != IP6PO_MINMTU_DISABLE && |
3159 | minmtupolicy != IP6PO_MINMTU_ALL) { |
3160 | return (EINVAL); |
3161 | } |
3162 | opt->ip6po_minmtu = minmtupolicy; |
3163 | break; |
3164 | |
3165 | case IPV6_DONTFRAG: |
3166 | if (len != sizeof(int)) |
3167 | return (EINVAL); |
3168 | |
3169 | if (uproto == IPPROTO_TCP || *(int *)buf == 0) { |
3170 | /* |
3171 | * we ignore this option for TCP sockets. |
3172 | * (RFC3542 leaves this case unspecified.) |
3173 | */ |
3174 | opt->ip6po_flags &= ~IP6PO_DONTFRAG; |
3175 | } else |
3176 | opt->ip6po_flags |= IP6PO_DONTFRAG; |
3177 | break; |
3178 | |
3179 | case IPV6_PREFER_TEMPADDR: |
3180 | { |
3181 | int preftemp; |
3182 | |
3183 | if (len != sizeof(int)) |
3184 | return (EINVAL); |
3185 | preftemp = *(int *)buf; |
3186 | switch (preftemp) { |
3187 | case IP6PO_TEMPADDR_SYSTEM: |
3188 | case IP6PO_TEMPADDR_NOTPREFER: |
3189 | case IP6PO_TEMPADDR_PREFER: |
3190 | break; |
3191 | default: |
3192 | return (EINVAL); |
3193 | } |
3194 | opt->ip6po_prefer_tempaddr = preftemp; |
3195 | break; |
3196 | } |
3197 | |
3198 | default: |
3199 | return (ENOPROTOOPT); |
3200 | } /* end of switch */ |
3201 | |
3202 | return (0); |
3203 | } |
3204 | |
3205 | /* |
3206 | * Routine called from ip6_output() to loop back a copy of an IP6 multicast |
3207 | * packet to the input queue of a specified interface. Note that this |
3208 | * calls the output routine of the loopback "driver", but with an interface |
3209 | * pointer that might NOT be lo0ifp -- easier than replicating that code here. |
3210 | */ |
3211 | void |
3212 | ip6_mloopback(struct ifnet *ifp, struct mbuf *m, |
3213 | const struct sockaddr_in6 *dst) |
3214 | { |
3215 | struct mbuf *copym; |
3216 | struct ip6_hdr *ip6; |
3217 | |
3218 | copym = m_copy(m, 0, M_COPYALL); |
3219 | if (copym == NULL) |
3220 | return; |
3221 | |
3222 | /* |
3223 | * Make sure to deep-copy IPv6 header portion in case the data |
3224 | * is in an mbuf cluster, so that we can safely override the IPv6 |
3225 | * header portion later. |
3226 | */ |
3227 | if ((copym->m_flags & M_EXT) != 0 || |
3228 | copym->m_len < sizeof(struct ip6_hdr)) { |
3229 | copym = m_pullup(copym, sizeof(struct ip6_hdr)); |
3230 | if (copym == NULL) |
3231 | return; |
3232 | } |
3233 | |
3234 | #ifdef DIAGNOSTIC |
3235 | if (copym->m_len < sizeof(*ip6)) { |
3236 | m_freem(copym); |
3237 | return; |
3238 | } |
3239 | #endif |
3240 | |
3241 | ip6 = mtod(copym, struct ip6_hdr *); |
3242 | /* |
3243 | * clear embedded scope identifiers if necessary. |
3244 | * in6_clearscope will touch the addresses only when necessary. |
3245 | */ |
3246 | in6_clearscope(&ip6->ip6_src); |
3247 | in6_clearscope(&ip6->ip6_dst); |
3248 | |
3249 | (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL); |
3250 | } |
3251 | |
3252 | /* |
3253 | * Chop IPv6 header off from the payload. |
3254 | */ |
3255 | static int |
3256 | ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs) |
3257 | { |
3258 | struct mbuf *mh; |
3259 | struct ip6_hdr *ip6; |
3260 | |
3261 | ip6 = mtod(m, struct ip6_hdr *); |
3262 | if (m->m_len > sizeof(*ip6)) { |
3263 | MGETHDR(mh, M_DONTWAIT, MT_HEADER); |
3264 | if (mh == 0) { |
3265 | m_freem(m); |
3266 | return ENOBUFS; |
3267 | } |
3268 | M_MOVE_PKTHDR(mh, m); |
3269 | MH_ALIGN(mh, sizeof(*ip6)); |
3270 | m->m_len -= sizeof(*ip6); |
3271 | m->m_data += sizeof(*ip6); |
3272 | mh->m_next = m; |
3273 | m = mh; |
3274 | m->m_len = sizeof(*ip6); |
3275 | bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6)); |
3276 | } |
3277 | exthdrs->ip6e_ip6 = m; |
3278 | return 0; |
3279 | } |
3280 | |
3281 | /* |
3282 | * Compute IPv6 extension header length. |
3283 | */ |
3284 | int |
3285 | ip6_optlen(struct in6pcb *in6p) |
3286 | { |
3287 | int len; |
3288 | |
3289 | if (!in6p->in6p_outputopts) |
3290 | return 0; |
3291 | |
3292 | len = 0; |
3293 | #define elen(x) \ |
3294 | (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) |
3295 | |
3296 | len += elen(in6p->in6p_outputopts->ip6po_hbh); |
3297 | len += elen(in6p->in6p_outputopts->ip6po_dest1); |
3298 | len += elen(in6p->in6p_outputopts->ip6po_rthdr); |
3299 | len += elen(in6p->in6p_outputopts->ip6po_dest2); |
3300 | return len; |
3301 | #undef elen |
3302 | } |
3303 | |
3304 | /* |
3305 | * Ensure sending address is valid. |
3306 | * Returns 0 on success, -1 if an error should be sent back or 1 |
3307 | * if the packet could be dropped without error (protocol dependent). |
3308 | */ |
3309 | static int |
3310 | ip6_ifaddrvalid(const struct in6_addr *addr) |
3311 | { |
3312 | struct sockaddr_in6 sin6; |
3313 | int s, error; |
3314 | struct ifaddr *ifa; |
3315 | struct in6_ifaddr *ia6; |
3316 | |
3317 | if (IN6_IS_ADDR_UNSPECIFIED(addr)) |
3318 | return 0; |
3319 | |
3320 | memset(&sin6, 0, sizeof(sin6)); |
3321 | sin6.sin6_family = AF_INET6; |
3322 | sin6.sin6_len = sizeof(sin6); |
3323 | sin6.sin6_addr = *addr; |
3324 | |
3325 | s = pserialize_read_enter(); |
3326 | ifa = ifa_ifwithaddr(sin6tosa(&sin6)); |
3327 | if ((ia6 = ifatoia6(ifa)) == NULL || |
3328 | ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) |
3329 | error = -1; |
3330 | else if (ia6->ia6_flags & (IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) |
3331 | error = 1; |
3332 | else |
3333 | error = 0; |
3334 | pserialize_read_exit(s); |
3335 | |
3336 | return error; |
3337 | } |
3338 | |