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