]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/raw_ip6.c
xnu-4570.51.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / raw_ip6.c
CommitLineData
b0d623f7 1/*
a39ff7e2 2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
b0d623f7
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39037602 14 *
b0d623f7
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
b0d623f7
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39037602 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
1c79356b
A
28/*
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
9bccf70c 31 *
1c79356b
A
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
9bccf70c 43 *
1c79356b
A
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
9bccf70c
A
55 *
56 * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
1c79356b
A
57 */
58
59/*
60 * Copyright (c) 1982, 1986, 1988, 1993
61 * The Regents of the University of California. All rights reserved.
62 *
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
65 * are met:
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * SUCH DAMAGE.
90 *
91 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
92 */
1c79356b
A
93#include <sys/param.h>
94#include <sys/malloc.h>
95#include <sys/proc.h>
316670eb 96#include <sys/mcache.h>
1c79356b
A
97#include <sys/mbuf.h>
98#include <sys/socket.h>
99#include <sys/protosw.h>
100#include <sys/socketvar.h>
101#include <sys/errno.h>
102#include <sys/systm.h>
103
104#include <net/if.h>
5ba3f43e 105#include <net/net_api_stats.h>
1c79356b
A
106#include <net/route.h>
107#include <net/if_types.h>
108
109#include <netinet/in.h>
110#include <netinet/in_var.h>
111#include <netinet/in_systm.h>
39037602 112#include <netinet/in_tclass.h>
1c79356b
A
113#include <netinet/ip6.h>
114#include <netinet6/ip6_var.h>
1c79356b 115#include <netinet/icmp6.h>
1c79356b 116#include <netinet/in_pcb.h>
1c79356b
A
117#include <netinet6/in6_pcb.h>
118#include <netinet6/nd6.h>
119#include <netinet6/ip6protosw.h>
9bccf70c 120#include <netinet6/scope6_var.h>
9bccf70c 121#include <netinet6/raw_ip6.h>
1c79356b
A
122
123#if IPSEC
124#include <netinet6/ipsec.h>
9bccf70c 125#include <netinet6/ipsec6.h>
1c79356b
A
126#endif /*IPSEC*/
127
fe8ab488
A
128#if NECP
129#include <net/necp.h>
130#endif
131
1c79356b
A
132/*
133 * Raw interface to IP6 protocol.
134 */
135
9bccf70c
A
136extern struct inpcbhead ripcb;
137extern struct inpcbinfo ripcbinfo;
b0d623f7
A
138extern u_int32_t rip_sendspace;
139extern u_int32_t rip_recvspace;
1c79356b 140
9bccf70c
A
141struct rip6stat rip6stat;
142
1c79356b
A
143/*
144 * Setup generic address and protocol structures
145 * for raw_input routine, then pass them along with
146 * mbuf chain.
147 */
148int
91447636
A
149rip6_input(
150 struct mbuf **mp,
6d2010ae
A
151 int *offp,
152 int proto)
1c79356b
A
153{
154 struct mbuf *m = *mp;
39236c6e
A
155 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
156 struct inpcb *in6p;
1c79356b 157 struct inpcb *last = 0;
9bccf70c 158 struct mbuf *opts = NULL;
1c79356b 159 struct sockaddr_in6 rip6src;
6d2010ae 160 int ret;
39236c6e 161 struct ifnet *ifp = m->m_pkthdr.rcvif;
9bccf70c 162
316670eb
A
163 /* Expect 32-bit aligned data pointer on strict-align platforms */
164 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
1c79356b 165
316670eb 166 rip6stat.rip6s_ipackets++;
9bccf70c 167
1c79356b 168 init_sin6(&rip6src, m); /* general init */
1c79356b 169
39236c6e 170 lck_rw_lock_shared(ripcbinfo.ipi_lock);
1c79356b 171 LIST_FOREACH(in6p, &ripcb, inp_list) {
9bccf70c 172 if ((in6p->in6p_vflag & INP_IPV6) == 0)
1c79356b
A
173 continue;
174 if (in6p->in6p_ip6_nxt &&
175 in6p->in6p_ip6_nxt != proto)
176 continue;
177 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
178 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
179 continue;
180 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
181 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
182 continue;
39236c6e 183
fe8ab488 184 if (inp_restricted_recv(in6p, ifp))
39236c6e
A
185 continue;
186
316670eb 187 if (proto == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
9bccf70c
A
188 rip6stat.rip6s_isum++;
189 if (in6_cksum(m, ip6->ip6_nxt, *offp,
190 m->m_pkthdr.len - *offp)) {
191 rip6stat.rip6s_badsum++;
192 continue;
193 }
1c79356b
A
194 }
195 if (last) {
196 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
9bccf70c 197
fe8ab488 198#if NECP
3e170ce0
A
199 if (n && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
200 &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL)) {
fe8ab488
A
201 m_freem(n);
202 /* do not inject data into pcb */
9bccf70c 203 } else
fe8ab488 204#endif /* NECP */
1c79356b 205 if (n) {
39236c6e 206 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
6d2010ae
A
207 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
208 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
209 ret = ip6_savecontrol(last, n, &opts);
210 if (ret != 0) {
211 m_freem(n);
212 m_freem(opts);
213 last = in6p;
214 continue;
39037602 215 }
6d2010ae 216 }
1c79356b
A
217 /* strip intermediate headers */
218 m_adj(n, *offp);
6d2010ae 219 so_recv_data_stat(last->in6p_socket, m, 0);
1c79356b
A
220 if (sbappendaddr(&last->in6p_socket->so_rcv,
221 (struct sockaddr *)&rip6src,
91447636 222 n, opts, NULL) == 0) {
9bccf70c 223 rip6stat.rip6s_fullsock++;
1c79356b
A
224 } else
225 sorwakeup(last->in6p_socket);
9bccf70c 226 opts = NULL;
1c79356b
A
227 }
228 }
229 last = in6p;
230 }
3e170ce0 231
fe8ab488 232#if NECP
3e170ce0
A
233 if (last && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
234 &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL)) {
fe8ab488
A
235 m_freem(m);
236 ip6stat.ip6s_delivered--;
237 /* do not inject data into pcb */
9bccf70c 238 } else
fe8ab488 239#endif /* NECP */
1c79356b 240 if (last) {
39236c6e 241 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
6d2010ae
A
242 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
243 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
244 ret = ip6_savecontrol(last, m, &opts);
245 if (ret != 0) {
246 m_freem(m);
247 m_freem(opts);
248 ip6stat.ip6s_delivered--;
249 goto unlock;
250 }
39037602 251
6d2010ae 252 }
1c79356b
A
253 /* strip intermediate headers */
254 m_adj(m, *offp);
6d2010ae 255 so_recv_data_stat(last->in6p_socket, m, 0);
1c79356b 256 if (sbappendaddr(&last->in6p_socket->so_rcv,
91447636 257 (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
9bccf70c 258 rip6stat.rip6s_fullsock++;
1c79356b
A
259 } else
260 sorwakeup(last->in6p_socket);
261 } else {
9bccf70c
A
262 rip6stat.rip6s_nosock++;
263 if (m->m_flags & M_MCAST)
264 rip6stat.rip6s_nosockmcast++;
1c79356b
A
265 if (proto == IPPROTO_NONE)
266 m_freem(m);
267 else {
268 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
269 icmp6_error(m, ICMP6_PARAM_PROB,
270 ICMP6_PARAMPROB_NEXTHEADER,
271 prvnxtp - mtod(m, char *));
272 }
273 ip6stat.ip6s_delivered--;
274 }
6d2010ae
A
275
276unlock:
39236c6e 277 lck_rw_done(ripcbinfo.ipi_lock);
6d2010ae 278
1c79356b
A
279 return IPPROTO_DONE;
280}
281
282void
91447636
A
283rip6_ctlinput(
284 int cmd,
285 struct sockaddr *sa,
5ba3f43e
A
286 void *d,
287 __unused struct ifnet *ifp)
1c79356b 288{
1c79356b
A
289 struct ip6_hdr *ip6;
290 struct mbuf *m;
6d2010ae 291 void *cmdarg = NULL;
1c79356b 292 int off = 0;
9bccf70c
A
293 struct ip6ctlparam *ip6cp = NULL;
294 const struct sockaddr_in6 *sa6_src = NULL;
91447636 295 void (*notify)(struct inpcb *, int) = in6_rtchange;
1c79356b
A
296
297 if (sa->sa_family != AF_INET6 ||
298 sa->sa_len != sizeof(struct sockaddr_in6))
299 return;
300
301 if ((unsigned)cmd >= PRC_NCMDS)
302 return;
39037602
A
303 if (PRC_IS_REDIRECT(cmd)) {
304 notify = in6_rtchange;
305 d = NULL;
306 } else if (cmd == PRC_HOSTDEAD)
1c79356b
A
307 d = NULL;
308 else if (inet6ctlerrmap[cmd] == 0)
309 return;
310
311 /* if the parameter is from icmp6, decode it. */
312 if (d != NULL) {
9bccf70c 313 ip6cp = (struct ip6ctlparam *)d;
1c79356b
A
314 m = ip6cp->ip6c_m;
315 ip6 = ip6cp->ip6c_ip6;
316 off = ip6cp->ip6c_off;
6d2010ae 317 cmdarg = ip6cp->ip6c_cmdarg;
9bccf70c 318 sa6_src = ip6cp->ip6c_src;
1c79356b
A
319 } else {
320 m = NULL;
321 ip6 = NULL;
9bccf70c 322 sa6_src = &sa6_any;
1c79356b
A
323 }
324
2d21ac55 325 (void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
6d2010ae 326 0, cmd, cmdarg, notify);
1c79356b
A
327}
328
329/*
330 * Generate IPv6 header and pass packet to ip6_output.
331 * Tack on options user may have setup with control call.
332 */
333int
91447636 334rip6_output(
39236c6e 335 struct mbuf *m,
91447636
A
336 struct socket *so,
337 struct sockaddr_in6 *dstsock,
6d2010ae
A
338 struct mbuf *control,
339 int israw)
1c79356b
A
340{
341 struct in6_addr *dst;
342 struct ip6_hdr *ip6;
343 struct inpcb *in6p;
344 u_int plen = m->m_pkthdr.len;
345 int error = 0;
39236c6e 346 struct ip6_pktopts opt, *optp = NULL;
6d2010ae 347 struct ip6_moptions *im6o = NULL;
1c79356b
A
348 struct ifnet *oifp = NULL;
349 int type = 0, code = 0; /* for ICMPv6 output statistics only */
39037602
A
350 int sotc = SO_TC_UNSPEC;
351 int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
a39ff7e2 352 struct ip6_out_args ip6oa;
6d2010ae
A
353 int flags = IPV6_OUTARGS;
354
1c79356b
A
355 in6p = sotoin6pcb(so);
356
a39ff7e2
A
357 bzero(&ip6oa, sizeof(ip6oa));
358 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
359 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
360
fe8ab488
A
361 if (in6p == NULL
362#if NECP
363 || (necp_socket_should_use_flow_divert(in6p))
364#endif /* NECP */
365 ) {
366 if (in6p == NULL)
367 error = EINVAL;
368 else
369 error = EPROTOTYPE;
39236c6e
A
370 goto bad;
371 }
372 if (dstsock != NULL && IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
373 error = EINVAL;
374 goto bad;
375 }
376
316670eb
A
377 if (in6p->inp_flags & INP_BOUND_IF) {
378 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
379 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
380 }
fe8ab488 381 if (INP_NO_CELLULAR(in6p))
316670eb 382 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
fe8ab488
A
383 if (INP_NO_EXPENSIVE(in6p))
384 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
385 if (INP_AWDL_UNRESTRICTED(in6p))
386 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
39037602
A
387 if (INP_INTCOPROC_ALLOWED(in6p))
388 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
6d2010ae 389
1c79356b
A
390 dst = &dstsock->sin6_addr;
391 if (control) {
39037602 392 sotc = so_tc_from_control(control, &netsvctype);
d41d1dae 393
39236c6e
A
394 if ((error = ip6_setpktopts(control, &opt, NULL,
395 SOCK_PROTO(so))) != 0)
1c79356b
A
396 goto bad;
397 optp = &opt;
398 } else
399 optp = in6p->in6p_outputopts;
39037602
A
400 if (sotc == SO_TC_UNSPEC) {
401 sotc = so->so_traffic_class;
402 netsvctype = so->so_netsvctype;
403 }
404 ip6oa.ip6oa_sotc = sotc;
405 ip6oa.ip6oa_netsvctype = netsvctype;
1c79356b
A
406
407 /*
408 * For an ICMPv6 packet, we should know its type and code
409 * to update statistics.
410 */
39236c6e 411 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
1c79356b
A
412 struct icmp6_hdr *icmp6;
413 if (m->m_len < sizeof(struct icmp6_hdr) &&
414 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
415 error = ENOBUFS;
416 goto bad;
417 }
418 icmp6 = mtod(m, struct icmp6_hdr *);
419 type = icmp6->icmp6_type;
420 code = icmp6->icmp6_code;
421 }
422
316670eb
A
423 if (in6p->inp_flowhash == 0)
424 in6p->inp_flowhash = inp_calc_flowhash(in6p);
39236c6e
A
425 /* update flowinfo - RFC 6437 */
426 if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
427 in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
428 in6p->inp_flow |=
429 (htonl(in6p->inp_flowhash) & IPV6_FLOWLABEL_MASK);
430 }
316670eb 431
3e170ce0 432 M_PREPEND(m, sizeof(*ip6), M_WAIT, 1);
b0d623f7
A
433 if (m == NULL) {
434 error = ENOBUFS;
435 goto bad;
436 }
1c79356b
A
437 ip6 = mtod(m, struct ip6_hdr *);
438
439 /*
440 * Next header might not be ICMP6 but use its pseudo header anyway.
441 */
442 ip6->ip6_dst = *dst;
443
6d2010ae
A
444 im6o = in6p->in6p_moptions;
445
1c79356b
A
446 /*
447 * If the scope of the destination is link-local, embed the interface
448 * index in the address.
449 *
9bccf70c 450 * XXX advanced-api value overrides sin6_scope_id
1c79356b
A
451 */
452 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
453 struct in6_pktinfo *pi;
6d2010ae 454 struct ifnet *im6o_multicast_ifp = NULL;
1c79356b 455
6d2010ae
A
456 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && im6o != NULL) {
457 IM6O_LOCK(im6o);
458 im6o_multicast_ifp = im6o->im6o_multicast_ifp;
459 IM6O_UNLOCK(im6o);
460 }
1c79356b
A
461 /*
462 * XXX Boundary check is assumed to be already done in
463 * ip6_setpktoptions().
464 */
b0d623f7 465 ifnet_head_lock_shared();
1c79356b
A
466 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
467 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
468 oifp = ifindex2ifnet[pi->ipi6_ifindex];
6d2010ae
A
469 if (oifp != NULL)
470 ifnet_reference(oifp);
9bccf70c 471 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
6d2010ae
A
472 im6o != NULL && im6o_multicast_ifp != NULL) {
473 oifp = im6o_multicast_ifp;
474 ifnet_reference(oifp);
1c79356b
A
475 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
476 } else if (dstsock->sin6_scope_id) {
39037602
A
477 /*
478 * boundary check
b0d623f7
A
479 *
480 * Sinced stsock->sin6_scope_id is unsigned, we don't
481 * need to check if it's < 0
482 */
483 if (if_index < dstsock->sin6_scope_id) {
1c79356b 484 error = ENXIO; /* XXX EINVAL? */
b0d623f7 485 ifnet_head_done();
1c79356b
A
486 goto bad;
487 }
488 ip6->ip6_dst.s6_addr16[1]
489 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
490 }
b0d623f7 491 ifnet_head_done();
1c79356b
A
492 }
493
494 /*
495 * Source address selection.
496 */
497 {
498 struct in6_addr *in6a;
91447636 499 struct in6_addr storage;
b0d623f7 500 u_short index = 0;
316670eb 501
6d2010ae
A
502 if (israw != 0 && optp && optp->ip6po_pktinfo && !IN6_IS_ADDR_UNSPECIFIED(&optp->ip6po_pktinfo->ipi6_addr)) {
503 in6a = &optp->ip6po_pktinfo->ipi6_addr;
504 flags |= IPV6_FLAG_NOSRCIFSEL;
505 } else if ((in6a = in6_selectsrc(dstsock, optp, in6p,
506 &in6p->in6p_route, NULL, &storage, ip6oa.ip6oa_boundif,
507 &error)) == 0) {
1c79356b
A
508 if (error == 0)
509 error = EADDRNOTAVAIL;
510 goto bad;
316670eb
A
511 } else {
512 ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
1c79356b
A
513 }
514 ip6->ip6_src = *in6a;
b0d623f7
A
515 if (in6p->in6p_route.ro_rt != NULL) {
516 RT_LOCK(in6p->in6p_route.ro_rt);
517 if (in6p->in6p_route.ro_rt->rt_ifp != NULL)
518 index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
519 RT_UNLOCK(in6p->in6p_route.ro_rt);
6d2010ae
A
520 if (oifp != NULL)
521 ifnet_release(oifp);
b0d623f7
A
522 ifnet_head_lock_shared();
523 if (index == 0 || if_index < index) {
524 panic("bad if_index on interface from route");
525 }
526 oifp = ifindex2ifnet[index];
6d2010ae
A
527 if (oifp != NULL)
528 ifnet_reference(oifp);
b0d623f7
A
529 ifnet_head_done();
530 }
1c79356b 531 }
9bccf70c 532 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
39236c6e 533 (in6p->inp_flow & IPV6_FLOWINFO_MASK);
9bccf70c
A
534 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
535 (IPV6_VERSION & IPV6_VERSION_MASK);
536 /* ip6_plen will be filled in ip6_output, so not fill it here. */
1c79356b
A
537 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
538 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
539
39236c6e 540 if (SOCK_PROTO(so) == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
1c79356b
A
541 struct mbuf *n;
542 int off;
543 u_int16_t *p;
544
1c79356b 545 /* compute checksum */
39236c6e 546 if (SOCK_PROTO(so) == IPPROTO_ICMPV6)
1c79356b
A
547 off = offsetof(struct icmp6_hdr, icmp6_cksum);
548 else
549 off = in6p->in6p_cksum;
6d2010ae 550 if (plen < (unsigned int)(off + 1)) {
1c79356b
A
551 error = EINVAL;
552 goto bad;
553 }
554 off += sizeof(struct ip6_hdr);
555
556 n = m;
557 while (n && n->m_len <= off) {
558 off -= n->m_len;
559 n = n->m_next;
560 }
561 if (!n)
562 goto bad;
316670eb 563 p = (u_int16_t *)(void *)(mtod(n, caddr_t) + off);
1c79356b
A
564 *p = 0;
565 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
566 }
3e170ce0 567
fe8ab488
A
568#if NECP
569 {
570 necp_kernel_policy_id policy_id;
3e170ce0 571 u_int32_t route_rule_id;
39037602
A
572
573 /*
574 * We need a route to perform NECP route rule checks
575 */
576 if (net_qos_policy_restricted != 0 &&
577 ROUTE_UNUSABLE(&in6p->in6p_route)) {
578 struct sockaddr_in6 to;
579 struct sockaddr_in6 from;
580
581 ROUTE_RELEASE(&in6p->in6p_route);
582
583 bzero(&from, sizeof(struct sockaddr_in6));
584 from.sin6_family = AF_INET6;
585 from.sin6_len = sizeof(struct sockaddr_in6);
586 from.sin6_addr = ip6->ip6_src;
587
588 bzero(&to, sizeof(struct sockaddr_in6));
589 to.sin6_family = AF_INET6;
590 to.sin6_len = sizeof(struct sockaddr_in6);
591 to.sin6_addr = ip6->ip6_dst;
592
593 in6p->in6p_route.ro_dst.sin6_family = AF_INET6;
594 in6p->in6p_route.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
595 ((struct sockaddr_in6 *)(void *)&in6p->in6p_route.ro_dst)->sin6_addr =
596 ip6->ip6_dst;
597
598 rtalloc_scoped((struct route *)&in6p->in6p_route, ip6oa.ip6oa_boundif);
599
600 inp_update_necp_policy(in6p, (struct sockaddr *)&from,
601 (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
602 in6p->inp_policyresult.results.qos_marking_gencount = 0;
603 }
604
3e170ce0
A
605 if (!necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
606 &ip6->ip6_src, &ip6->ip6_dst, NULL, &policy_id, &route_rule_id)) {
fe8ab488
A
607 error = EHOSTUNREACH;
608 goto bad;
609 }
1c79356b 610
3e170ce0 611 necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id);
39037602
A
612
613 if (net_qos_policy_restricted != 0) {
614 necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt,
615 NULL, route_rule_id);
616 }
fe8ab488
A
617 }
618#endif /* NECP */
39037602
A
619 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED))
620 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
3e170ce0 621
1c79356b 622#if IPSEC
fe8ab488 623 if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
9bccf70c
A
624 error = ENOBUFS;
625 goto bad;
626 }
1c79356b
A
627#endif /*IPSEC*/
628
39236c6e
A
629 if (ROUTE_UNUSABLE(&in6p->in6p_route))
630 ROUTE_RELEASE(&in6p->in6p_route);
91447636 631
6d2010ae
A
632 if (oifp != NULL) {
633 ifnet_release(oifp);
634 oifp = NULL;
635 }
d1ecb069 636
39037602 637 set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
39236c6e
A
638 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
639 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
640 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
641 PKTF_FLOW_RAWSOCK);
642 m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
316670eb 643
6d2010ae
A
644 if (im6o != NULL)
645 IM6O_ADDREF(im6o);
d1ecb069 646
6d2010ae
A
647 error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
648 &oifp, &ip6oa);
649
650 if (im6o != NULL)
651 IM6O_REMREF(im6o);
652
653 if (in6p->in6p_route.ro_rt != NULL) {
654 struct rtentry *rt = in6p->in6p_route.ro_rt;
316670eb 655 struct ifnet *outif;
6d2010ae
A
656
657 if ((rt->rt_flags & RTF_MULTICAST) ||
658 in6p->in6p_socket == NULL ||
659 !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
660 rt = NULL; /* unusable */
661 }
662 /*
663 * Always discard the cached route for unconnected
664 * socket or if it is a multicast route.
665 */
39236c6e
A
666 if (rt == NULL)
667 ROUTE_RELEASE(&in6p->in6p_route);
668
6d2010ae
A
669 /*
670 * If this is a connected socket and the destination
671 * route is not multicast, update outif with that of
672 * the route interface index used by IP.
673 */
674 if (rt != NULL &&
5ba3f43e 675 (outif = rt->rt_ifp) != in6p->in6p_last_outifp) {
316670eb 676 in6p->in6p_last_outifp = outif;
5ba3f43e 677 }
39236c6e
A
678 } else {
679 ROUTE_RELEASE(&in6p->in6p_route);
d1ecb069 680 }
d1ecb069 681
39236c6e 682 /*
fe8ab488
A
683 * If output interface was cellular/expensive, and this socket is
684 * denied access to it, generate an event.
39236c6e
A
685 */
686 if (error != 0 && (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED) &&
fe8ab488 687 (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p)))
39236c6e
A
688 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED|
689 SO_FILT_HINT_IFDENIED));
690
691 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
1c79356b
A
692 if (oifp)
693 icmp6_ifoutstat_inc(oifp, type, code);
694 icmp6stat.icp6s_outhist[type]++;
9bccf70c
A
695 } else
696 rip6stat.rip6s_opackets++;
1c79356b
A
697
698 goto freectl;
699
39236c6e
A
700bad:
701 if (m != NULL)
1c79356b
A
702 m_freem(m);
703
39236c6e
A
704freectl:
705 if (optp == &opt && optp->ip6po_rthdr)
706 ROUTE_RELEASE(&optp->ip6po_route);
707
708 if (control != NULL) {
1c79356b 709 if (optp == &opt)
6d2010ae 710 ip6_clearpktopts(optp, -1);
1c79356b
A
711 m_freem(control);
712 }
6d2010ae
A
713 if (oifp != NULL)
714 ifnet_release(oifp);
1c79356b
A
715 return(error);
716}
717
718/*
719 * Raw IPv6 socket option processing.
720 */
721int
91447636
A
722rip6_ctloutput(
723 struct socket *so,
724 struct sockopt *sopt)
1c79356b 725{
316670eb 726 int error, optval;
1c79356b 727
316670eb 728 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
1c79356b
A
729 if (sopt->sopt_level == IPPROTO_ICMPV6)
730 /*
731 * XXX: is it better to call icmp6_ctloutput() directly
732 * from protosw?
733 */
734 return(icmp6_ctloutput(so, sopt));
316670eb
A
735 else if (sopt->sopt_level != IPPROTO_IPV6 &&
736 !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH))
1c79356b
A
737 return (EINVAL);
738
739 error = 0;
740
741 switch (sopt->sopt_dir) {
742 case SOPT_GET:
743 switch (sopt->sopt_name) {
6d2010ae
A
744 case IPV6_CHECKSUM:
745 error = ip6_raw_ctloutput(so, sopt);
1c79356b
A
746 break;
747 default:
748 error = ip6_ctloutput(so, sopt);
749 break;
750 }
751 break;
752
753 case SOPT_SET:
754 switch (sopt->sopt_name) {
6d2010ae
A
755 case IPV6_CHECKSUM:
756 error = ip6_raw_ctloutput(so, sopt);
757 break;
316670eb
A
758
759 case SO_FLUSH:
760 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
761 sizeof (optval))) != 0)
762 break;
763
764 error = inp_flush(sotoinpcb(so), optval);
765 break;
766
1c79356b
A
767 default:
768 error = ip6_ctloutput(so, sopt);
769 break;
770 }
771 break;
772 }
773
774 return (error);
775}
776
777static int
b0d623f7 778rip6_attach(struct socket *so, int proto, struct proc *p)
1c79356b
A
779{
780 struct inpcb *inp;
2d21ac55 781 int error;
1c79356b
A
782
783 inp = sotoinpcb(so);
784 if (inp)
785 panic("rip6_attach");
b0d623f7 786 if ((error = proc_suser(p)) != 0)
1c79356b
A
787 return error;
788
9bccf70c
A
789 error = soreserve(so, rip_sendspace, rip_recvspace);
790 if (error)
791 return error;
1c79356b 792 error = in_pcballoc(so, &ripcbinfo, p);
1c79356b
A
793 if (error)
794 return error;
795 inp = (struct inpcb *)so->so_pcb;
796 inp->inp_vflag |= INP_IPV6;
b0d623f7 797 inp->in6p_ip6_nxt = (char)proto;
1c79356b
A
798 inp->in6p_hops = -1; /* use kernel default */
799 inp->in6p_cksum = -1;
1c79356b 800 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
55e303ae
A
801 sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
802 if (inp->in6p_icmp6filt == NULL)
803 return (ENOMEM);
1c79356b
A
804 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
805 return 0;
806}
807
808static int
809rip6_detach(struct socket *so)
810{
811 struct inpcb *inp;
812
813 inp = sotoinpcb(so);
814 if (inp == 0)
815 panic("rip6_detach");
9bccf70c 816 /* xxx: RSVP */
1c79356b
A
817 if (inp->in6p_icmp6filt) {
818 FREE(inp->in6p_icmp6filt, M_PCB);
819 inp->in6p_icmp6filt = NULL;
820 }
821 in6_pcbdetach(inp);
822 return 0;
823}
824
825static int
826rip6_abort(struct socket *so)
827{
828 soisdisconnected(so);
829 return rip6_detach(so);
830}
831
832static int
833rip6_disconnect(struct socket *so)
834{
835 struct inpcb *inp = sotoinpcb(so);
836
837 if ((so->so_state & SS_ISCONNECTED) == 0)
838 return ENOTCONN;
839 inp->in6p_faddr = in6addr_any;
840 return rip6_abort(so);
841}
842
843static int
39236c6e 844rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1c79356b 845{
39236c6e 846#pragma unused(p)
1c79356b 847 struct inpcb *inp = sotoinpcb(so);
39236c6e 848 struct sockaddr_in6 sin6;
6d2010ae 849 struct ifaddr *ifa = NULL;
316670eb 850 struct ifnet *outif = NULL;
39236c6e 851 int error;
1c79356b 852
fe8ab488
A
853 if (inp == NULL
854#if NECP
855 || (necp_socket_should_use_flow_divert(inp))
856#endif /* NECP */
857 )
39236c6e 858 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b 859
39236c6e
A
860 if (nam->sa_len != sizeof (struct sockaddr_in6))
861 return (EINVAL);
862
863 if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6)
864 return (EADDRNOTAVAIL);
865
866 bzero(&sin6, sizeof (sin6));
867 *(&sin6) = *SIN6(nam);
868
869 if ((error = sa6_embedscope(&sin6, ip6_use_defzone)) != 0)
870 return (error);
871
872 /* Sanitize local copy for address searches */
873 sin6.sin6_flowinfo = 0;
874 sin6.sin6_scope_id = 0;
875 sin6.sin6_port = 0;
876
877 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
878 (ifa = ifa_ifwithaddr(SA(&sin6))) == 0)
879 return (EADDRNOTAVAIL);
6d2010ae
A
880 if (ifa != NULL) {
881 IFA_LOCK(ifa);
882 if (((struct in6_ifaddr *)ifa)->ia6_flags &
883 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
884 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
885 IFA_UNLOCK(ifa);
886 IFA_REMREF(ifa);
39236c6e 887 return (EADDRNOTAVAIL);
6d2010ae 888 }
316670eb 889 outif = ifa->ifa_ifp;
6d2010ae
A
890 IFA_UNLOCK(ifa);
891 IFA_REMREF(ifa);
1c79356b 892 }
39236c6e 893 inp->in6p_laddr = sin6.sin6_addr;
316670eb 894 inp->in6p_last_outifp = outif;
5ba3f43e 895
39236c6e 896 return (0);
1c79356b
A
897}
898
899static int
2d21ac55 900rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1c79356b
A
901{
902 struct inpcb *inp = sotoinpcb(so);
316670eb 903 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)nam;
1c79356b 904 struct in6_addr *in6a = NULL;
91447636 905 struct in6_addr storage;
1c79356b 906 int error = 0;
9bccf70c
A
907#if ENABLE_DEFAULT_SCOPE
908 struct sockaddr_in6 tmp;
909#endif
316670eb
A
910 unsigned int ifscope;
911 struct ifnet *outif = NULL;
1c79356b 912
fe8ab488
A
913 if (inp == NULL
914#if NECP
915 || (necp_socket_should_use_flow_divert(inp))
916#endif /* NECP */
917 )
39236c6e 918 return (inp == NULL ? EINVAL : EPROTOTYPE);
1c79356b
A
919 if (nam->sa_len != sizeof(*addr))
920 return EINVAL;
91447636 921 if (TAILQ_EMPTY(&ifnet_head))
1c79356b
A
922 return EADDRNOTAVAIL;
923 if (addr->sin6_family != AF_INET6)
924 return EAFNOSUPPORT;
5ba3f43e
A
925
926 if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
927 so->so_flags1 |= SOF1_CONNECT_COUNTED;
928 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
929 }
930
9bccf70c
A
931#if ENABLE_DEFAULT_SCOPE
932 if (addr->sin6_scope_id == 0) { /* not change if specified */
933 /* avoid overwrites */
934 tmp = *addr;
935 addr = &tmp;
936 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
937 }
938#endif
6d2010ae 939
39037602
A
940 /* KAME hack: embed scopeid */
941 if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL) != 0)
942 return (EINVAL);
943
6d2010ae 944 ifscope = (inp->inp_flags & INP_BOUND_IF) ?
316670eb 945 inp->inp_boundifp->if_index : IFSCOPE_NONE;
6d2010ae 946
1c79356b 947 /* Source address selection. XXX: need pcblookup? */
6d2010ae
A
948 in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
949 NULL, &storage, ifscope, &error);
1c79356b
A
950 if (in6a == NULL)
951 return (error ? error : EADDRNOTAVAIL);
952 inp->in6p_laddr = *in6a;
953 inp->in6p_faddr = addr->sin6_addr;
6d2010ae 954 if (inp->in6p_route.ro_rt != NULL)
316670eb
A
955 outif = inp->in6p_route.ro_rt->rt_ifp;
956 inp->in6p_last_outifp = outif;
5ba3f43e 957
1c79356b
A
958 soisconnected(so);
959 return 0;
960}
961
962static int
963rip6_shutdown(struct socket *so)
964{
965 socantsendmore(so);
966 return 0;
967}
968
969static int
6d2010ae 970rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
39236c6e 971 struct mbuf *control, struct proc *p)
1c79356b 972{
6d2010ae 973#pragma unused(flags, p)
1c79356b
A
974 struct inpcb *inp = sotoinpcb(so);
975 struct sockaddr_in6 tmp;
316670eb 976 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)(void *)nam;
39236c6e
A
977 int error = 0;
978
fe8ab488
A
979 if (inp == NULL
980#if NECP
981 || (necp_socket_should_use_flow_divert(inp))
982#endif /* NECP */
983 ) {
984 if (inp == NULL)
985 error = EINVAL;
986 else
987 error = EPROTOTYPE;
39236c6e
A
988 goto bad;
989 }
1c79356b 990
9bccf70c 991 /* always copy sockaddr to avoid overwrites */
1c79356b 992 if (so->so_state & SS_ISCONNECTED) {
39236c6e
A
993 if (nam != NULL) {
994 error = EISCONN;
995 goto bad;
1c79356b
A
996 }
997 /* XXX */
998 bzero(&tmp, sizeof(tmp));
999 tmp.sin6_family = AF_INET6;
1000 tmp.sin6_len = sizeof(struct sockaddr_in6);
1001 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
1002 sizeof(struct in6_addr));
1003 dst = &tmp;
1004 } else {
1005 if (nam == NULL) {
39236c6e
A
1006 error = ENOTCONN;
1007 goto bad;
1c79356b 1008 }
316670eb 1009 tmp = *(struct sockaddr_in6 *)(void *)nam;
9bccf70c 1010 dst = &tmp;
1c79356b 1011 }
9bccf70c
A
1012#if ENABLE_DEFAULT_SCOPE
1013 if (dst->sin6_scope_id == 0) { /* not change if specified */
1014 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
1015 }
1016#endif
39236c6e
A
1017 return (rip6_output(m, so, dst, control, 1));
1018
1019bad:
1020 VERIFY(error != 0);
1021
1022 if (m != NULL)
1023 m_freem(m);
1024 if (control != NULL)
1025 m_freem(control);
1026
1027 return (error);
1c79356b
A
1028}
1029
1030struct pr_usrreqs rip6_usrreqs = {
39236c6e
A
1031 .pru_abort = rip6_abort,
1032 .pru_attach = rip6_attach,
1033 .pru_bind = rip6_bind,
1034 .pru_connect = rip6_connect,
1035 .pru_control = in6_control,
1036 .pru_detach = rip6_detach,
1037 .pru_disconnect = rip6_disconnect,
1038 .pru_peeraddr = in6_getpeeraddr,
1039 .pru_send = rip6_send,
1040 .pru_shutdown = rip6_shutdown,
1041 .pru_sockaddr = in6_getsockaddr,
1042 .pru_sosend = sosend,
1043 .pru_soreceive = soreceive,
1c79356b 1044};
2d21ac55
A
1045
1046__private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
39236c6e
A
1047 .pru_abort = rip6_abort,
1048 .pru_attach = icmp6_dgram_attach,
1049 .pru_bind = rip6_bind,
1050 .pru_connect = rip6_connect,
1051 .pru_control = in6_control,
1052 .pru_detach = rip6_detach,
1053 .pru_disconnect = rip6_disconnect,
1054 .pru_peeraddr = in6_getpeeraddr,
1055 .pru_send = icmp6_dgram_send,
1056 .pru_shutdown = rip6_shutdown,
1057 .pru_sockaddr = in6_getsockaddr,
1058 .pru_sosend = sosend,
1059 .pru_soreceive = soreceive,
2d21ac55 1060};