xref: /xnu-11215.81.4/bsd/netinet6/raw_ip6.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30  * All rights reserved.
31  *
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.
43  *
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.
55  *
56  * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
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  */
93 #include <sys/param.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/mcache.h>
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/net_api_stats.h>
106 #include <net/route.h>
107 #include <net/if_types.h>
108 #include <net/content_filter.h>
109 #include <net/droptap.h>
110 
111 #include <netinet/in.h>
112 #include <netinet/in_var.h>
113 #include <netinet/in_systm.h>
114 #include <netinet/in_tclass.h>
115 #include <netinet/ip6.h>
116 #include <netinet6/ip6_var.h>
117 #include <netinet/icmp6.h>
118 #include <netinet/in_pcb.h>
119 #include <netinet6/in6_pcb.h>
120 #include <netinet6/nd6.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
123 #include <netinet6/raw_ip6.h>
124 
125 #if IPSEC
126 #include <netinet6/ipsec.h>
127 #include <netinet6/ipsec6.h>
128 #endif /*IPSEC*/
129 
130 #if NECP
131 #include <net/necp.h>
132 #endif
133 
134 /*
135  * Raw interface to IP6 protocol.
136  */
137 
138 extern struct   inpcbhead ripcb;
139 extern struct   inpcbinfo ripcbinfo;
140 extern u_int32_t        rip_sendspace;
141 extern u_int32_t        rip_recvspace;
142 
143 struct rip6stat rip6stat;
144 
145 /*
146  * Setup generic address and protocol structures
147  * for raw_input routine, then pass them along with
148  * mbuf chain.
149  */
150 int
rip6_input(struct mbuf ** mp,int * offp,int proto)151 rip6_input(
152 	struct  mbuf **mp,
153 	int     *offp,
154 	int     proto)
155 {
156 	struct mbuf *__single m = *mp;
157 	struct ip6_hdr *__single ip6 = mtod(m, struct ip6_hdr *);
158 	struct inpcb *__single in6p;
159 	struct inpcb *__single last = 0;
160 	struct mbuf *__single opts = NULL;
161 	struct sockaddr_in6 rip6src;
162 	int ret;
163 	struct ifnet *__single ifp = m->m_pkthdr.rcvif;
164 	boolean_t is_wake_pkt = false;
165 
166 	/* Expect 32-bit aligned data pointer on strict-align platforms */
167 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
168 
169 	rip6stat.rip6s_ipackets++;
170 
171 	init_sin6(&rip6src, m); /* general init */
172 
173 	if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
174 		is_wake_pkt = true;
175 	}
176 
177 	lck_rw_lock_shared(&ripcbinfo.ipi_lock);
178 	LIST_FOREACH(in6p, &ripcb, inp_list) {
179 		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
180 			continue;
181 		}
182 		if (in6p->in6p_ip6_nxt &&
183 		    in6p->in6p_ip6_nxt != proto) {
184 			continue;
185 		}
186 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
187 		    !in6_are_addr_equal_scoped(&in6p->in6p_laddr, &ip6->ip6_dst, in6p->inp_lifscope, ifp->if_index)) {
188 			continue;
189 		}
190 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
191 		    !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &ip6->ip6_src, in6p->inp_fifscope, ifp->if_index)) {
192 			continue;
193 		}
194 
195 		if (inp_restricted_recv(in6p, ifp)) {
196 			continue;
197 		}
198 
199 		if (proto == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
200 			rip6stat.rip6s_isum++;
201 			if (in6_cksum(m, ip6->ip6_nxt, *offp,
202 			    m->m_pkthdr.len - *offp)) {
203 				rip6stat.rip6s_badsum++;
204 				continue;
205 			}
206 		}
207 		if (last) {
208 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
209 
210 #if NECP
211 			if (n && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
212 			    &ip6->ip6_dst, &ip6->ip6_src, ifp, 0, NULL, NULL, NULL, NULL)) {
213 				m_drop(n, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NECP_POLICY_DROP, NULL, 0);
214 				/* do not inject data into pcb */
215 			} else
216 #endif /* NECP */
217 			if (n) {
218 				if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
219 				    SOFLOW_ENABLED(last->in6p_socket) ||
220 				    SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
221 					ret = ip6_savecontrol(last, n, &opts);
222 					if (ret != 0) {
223 						m_drop(n, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_ENOBUFS, NULL, 0);
224 						m_freem(opts);
225 						opts = NULL;
226 						last = in6p;
227 						continue;
228 					}
229 				}
230 				/* strip intermediate headers */
231 				m_adj(n, *offp);
232 				so_recv_data_stat(last->in6p_socket, m, 0);
233 				if (sbappendaddr(&last->in6p_socket->so_rcv,
234 				    (struct sockaddr *)&rip6src,
235 				    n, opts, NULL) == 0) {
236 					rip6stat.rip6s_fullsock++;
237 				} else {
238 					sorwakeup(last->in6p_socket);
239 				}
240 				if (is_wake_pkt) {
241 					soevent(in6p->in6p_socket,
242 					    SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
243 				}
244 				opts = NULL;
245 			}
246 		}
247 		last = in6p;
248 	}
249 
250 #if NECP
251 	if (last && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
252 	    &ip6->ip6_dst, &ip6->ip6_src, ifp, 0, NULL, NULL, NULL, NULL)) {
253 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NECP_POLICY_DROP, NULL, 0);
254 		ip6stat.ip6s_delivered--;
255 		/* do not inject data into pcb */
256 	} else
257 #endif /* NECP */
258 	if (last) {
259 		if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
260 		    SOFLOW_ENABLED(last->in6p_socket) ||
261 		    SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
262 			ret = ip6_savecontrol(last, m, &opts);
263 			if (ret != 0) {
264 				m_freem(m);
265 				m_freem(opts);
266 				ip6stat.ip6s_delivered--;
267 				goto unlock;
268 			}
269 		}
270 		/* strip intermediate headers */
271 		m_adj(m, *offp);
272 		so_recv_data_stat(last->in6p_socket, m, 0);
273 		if (sbappendaddr(&last->in6p_socket->so_rcv,
274 		    (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
275 			rip6stat.rip6s_fullsock++;
276 		} else {
277 			sorwakeup(last->in6p_socket);
278 		}
279 		if (is_wake_pkt) {
280 			soevent(last->in6p_socket,
281 			    SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
282 		}
283 	} else {
284 		rip6stat.rip6s_nosock++;
285 		if (m->m_flags & M_MCAST) {
286 			rip6stat.rip6s_nosockmcast++;
287 		}
288 		if (proto == IPPROTO_NONE) {
289 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NO_SOCK, NULL, 0);
290 		} else {
291 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
292 			icmp6_error(m, ICMP6_PARAM_PROB,
293 			    ICMP6_PARAMPROB_NEXTHEADER,
294 			    (int)(prvnxtp - mtod(m, char *)));
295 		}
296 		ip6stat.ip6s_delivered--;
297 	}
298 
299 unlock:
300 	lck_rw_done(&ripcbinfo.ipi_lock);
301 
302 	return IPPROTO_DONE;
303 }
304 
305 void
rip6_ctlinput(int cmd,struct sockaddr * sa,void * d,__unused struct ifnet * ifp)306 rip6_ctlinput(
307 	int cmd,
308 	struct sockaddr *sa,
309 	void *d,
310 	__unused struct ifnet *ifp)
311 {
312 	struct ip6_hdr *__single ip6 = NULL;
313 	struct mbuf *__single m = NULL;
314 	void *__single cmdarg = NULL;
315 	int off = 0;
316 	struct ip6ctlparam *__single ip6cp = NULL;
317 	const struct sockaddr_in6 *__single sa6_src = NULL;
318 	void (*notify)(struct inpcb *, int) = in6_rtchange;
319 
320 	if (sa->sa_family != AF_INET6 ||
321 	    sa->sa_len != sizeof(struct sockaddr_in6)) {
322 		return;
323 	}
324 
325 	if ((unsigned)cmd >= PRC_NCMDS) {
326 		return;
327 	}
328 	if (PRC_IS_REDIRECT(cmd)) {
329 		notify = in6_rtchange;
330 		d = NULL;
331 	} else if (cmd == PRC_HOSTDEAD) {
332 		d = NULL;
333 	} else if (inet6ctlerrmap[cmd] == 0) {
334 		return;
335 	}
336 
337 	/* if the parameter is from icmp6, decode it. */
338 	if (d != NULL) {
339 		ip6cp = (struct ip6ctlparam *)d;
340 		m = ip6cp->ip6c_m;
341 		ip6 = ip6cp->ip6c_ip6;
342 		off = ip6cp->ip6c_off;
343 		cmdarg = ip6cp->ip6c_cmdarg;
344 		sa6_src = ip6cp->ip6c_src;
345 	} else {
346 		m = NULL;
347 		ip6 = NULL;
348 		cmdarg = NULL;
349 		sa6_src = &sa6_any;
350 	}
351 
352 	(void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
353 	    0, cmd, cmdarg, notify);
354 }
355 
356 /*
357  * Generate IPv6 header and pass packet to ip6_output.
358  * Tack on options user may have setup with control call.
359  */
360 int
rip6_output(struct mbuf * m,struct socket * so,struct sockaddr_in6 * dstsock,struct mbuf * control,int israw)361 rip6_output(
362 	struct mbuf *m,
363 	struct socket *so,
364 	struct sockaddr_in6 *dstsock,
365 	struct mbuf *control,
366 	int israw)
367 {
368 	struct in6_addr *__single dst;
369 	struct ip6_hdr *__single ip6;
370 	struct inpcb *__single in6p;
371 	u_int   plen = m->m_pkthdr.len;
372 	int error = 0;
373 	struct ip6_pktopts opt, *__single optp = NULL;
374 	struct ip6_moptions *__single im6o = NULL;
375 	struct ifnet *__single oifp = NULL;
376 	int type = 0, code = 0;         /* for ICMPv6 output statistics only */
377 	int sotc = SO_TC_UNSPEC;
378 	int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
379 	struct ip6_out_args ip6oa;
380 	int flags = IPV6_OUTARGS;
381 	struct sockaddr_in6 tmp;
382 #if CONTENT_FILTER
383 	struct m_tag *cfil_tag = NULL;
384 	bool cfil_faddr_use = false;
385 	uint32_t cfil_so_state_change_cnt = 0;
386 	uint32_t cfil_so_options = 0;
387 	uint32_t sifscope = IFSCOPE_NONE, difscope = IFSCOPE_NONE;
388 	struct sockaddr *__single cfil_faddr = NULL;
389 	struct sockaddr_in6 *__single cfil_sin6 = NULL;
390 #endif
391 	drop_reason_t drop_reason = DROP_REASON_UNSPECIFIED;
392 
393 	in6p = sotoin6pcb(so);
394 	if (in6p == NULL) {
395 		error = EINVAL;
396 		goto bad;
397 	}
398 
399 #if CONTENT_FILTER
400 	/*
401 	 * If socket is subject to Content Filter and no addr is passed in,
402 	 * retrieve CFIL saved state from mbuf and use it if necessary.
403 	 */
404 	if (CFIL_DGRAM_FILTERED(so) && !dstsock) {
405 		cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, NULL);
406 		if (cfil_tag) {
407 			cfil_sin6 = SIN6(cfil_faddr);
408 			if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
409 				/*
410 				 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
411 				 * the connect/disconnect logic.
412 				 */
413 				dstsock = cfil_sin6;
414 			} else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
415 			    (in6p->in6p_fport != cfil_sin6->sin6_port ||
416 			    !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &cfil_sin6->sin6_addr, in6p->inp_fifscope, cfil_sin6->sin6_scope_id))) {
417 				/*
418 				 * Socket is connected but socket state and dest addr/port changed.
419 				 * We need to use the saved faddr and socket options.
420 				 */
421 				cfil_faddr_use = true;
422 			}
423 		}
424 	}
425 #endif
426 
427 	/* always copy sockaddr to avoid overwrites */
428 	if (so->so_state & SS_ISCONNECTED) {
429 		if (dstsock != NULL) {
430 			error = EISCONN;
431 			drop_reason = DROP_REASON_IP_EISCONN;
432 			goto bad;
433 		}
434 		/* XXX */
435 		bzero(&tmp, sizeof(tmp));
436 		tmp.sin6_family = AF_INET6;
437 		tmp.sin6_len = sizeof(struct sockaddr_in6);
438 		bcopy(
439 #if CONTENT_FILTER
440 			cfil_faddr_use ? &cfil_sin6->sin6_addr :
441 #endif
442 			&in6p->in6p_faddr, &tmp.sin6_addr, sizeof(struct in6_addr));
443 		dstsock = &tmp;
444 	} else {
445 		if (dstsock == NULL) {
446 			error = ENOTCONN;
447 			goto bad;
448 		}
449 		tmp = *dstsock;
450 		dstsock = &tmp;
451 	}
452 
453 #if ENABLE_DEFAULT_SCOPE
454 	if (dstsock->sin6_scope_id == 0) { /* not change if specified  */
455 		dstsock->sin6_scope_id = scope6_addr2default(&dstsock->sin6_addr);
456 	}
457 #endif
458 
459 	bzero(&ip6oa, sizeof(ip6oa));
460 	ip6oa.ip6oa_boundif = IFSCOPE_NONE;
461 	ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
462 
463 	if (in6p == NULL
464 #if NECP
465 	    || (necp_socket_should_use_flow_divert(in6p))
466 #endif /* NECP */
467 	    ) {
468 		if (in6p == NULL) {
469 			error = EINVAL;
470 		} else {
471 			error = EPROTOTYPE;
472 		}
473 		goto bad;
474 	}
475 	if (dstsock != NULL && IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
476 		error = EINVAL;
477 		goto bad;
478 	}
479 
480 	if (in6p->inp_flags & INP_BOUND_IF) {
481 		ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
482 		ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
483 	} else if (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&in6p->in6p_faddr)) {
484 		ip6oa.ip6oa_boundif = dstsock->sin6_scope_id;
485 		ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
486 	}
487 	if (INP_NO_CELLULAR(in6p)) {
488 		ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
489 	}
490 	if (INP_NO_EXPENSIVE(in6p)) {
491 		ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
492 	}
493 	if (INP_NO_CONSTRAINED(in6p)) {
494 		ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
495 	}
496 	if (INP_AWDL_UNRESTRICTED(in6p)) {
497 		ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
498 	}
499 	if (INP_INTCOPROC_ALLOWED(in6p)) {
500 		ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
501 	}
502 	if (INP_MANAGEMENT_ALLOWED(in6p)) {
503 		ip6oa.ip6oa_flags |= IP6OAF_MANAGEMENT_ALLOWED;
504 	}
505 
506 	dst = &dstsock->sin6_addr;
507 	if (control) {
508 		sotc = so_tc_from_control(control, &netsvctype);
509 
510 		if ((error = ip6_setpktopts(control, &opt,
511 		    in6p->in6p_outputopts, SOCK_PROTO(so))) != 0) {
512 			goto bad;
513 		}
514 		optp = &opt;
515 	} else {
516 		optp = in6p->in6p_outputopts;
517 	}
518 	if (sotc == SO_TC_UNSPEC) {
519 		sotc = so->so_traffic_class;
520 		netsvctype = so->so_netsvctype;
521 	}
522 	ip6oa.ip6oa_sotc = sotc;
523 	ip6oa.ip6oa_netsvctype = netsvctype;
524 
525 	/*
526 	 * For an ICMPv6 packet, we should know its type and code
527 	 * to update statistics.
528 	 */
529 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
530 		struct icmp6_hdr *icmp6;
531 		if (m->m_len < sizeof(struct icmp6_hdr) &&
532 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
533 			error = ENOBUFS;
534 			goto bad;
535 		}
536 		icmp6 = mtod(m, struct icmp6_hdr *);
537 		type = icmp6->icmp6_type;
538 		code = icmp6->icmp6_code;
539 	}
540 
541 	if (in6p->inp_flowhash == 0) {
542 		inp_calc_flowhash(in6p);
543 		ASSERT(in6p->inp_flowhash != 0);
544 	}
545 	/* update flowinfo - RFC 6437 */
546 	if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
547 		in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
548 		in6p->inp_flow |=
549 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
550 	}
551 
552 	M_PREPEND(m, sizeof(*ip6), M_WAIT, 1);
553 	if (m == NULL) {
554 		error = ENOBUFS;
555 		goto bad;
556 	}
557 	ip6 = mtod(m, struct ip6_hdr *);
558 
559 	/*
560 	 * Next header might not be ICMP6 but use its pseudo header anyway.
561 	 */
562 	ip6->ip6_dst = *dst;
563 
564 	im6o = in6p->in6p_moptions;
565 
566 	/*
567 	 * If the scope of the destination is link-local, embed the interface
568 	 * index in the address.
569 	 *
570 	 * XXX advanced-api value overrides sin6_scope_id
571 	 */
572 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
573 		struct in6_pktinfo *pi;
574 		struct ifnet *im6o_multicast_ifp = NULL;
575 
576 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && im6o != NULL) {
577 			IM6O_LOCK(im6o);
578 			im6o_multicast_ifp = im6o->im6o_multicast_ifp;
579 			IM6O_UNLOCK(im6o);
580 		}
581 		/*
582 		 * XXX Boundary check is assumed to be already done in
583 		 * ip6_setpktoptions().
584 		 */
585 		ifnet_head_lock_shared();
586 		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
587 			if (in6_embedded_scope) {
588 				ip6->ip6_dst.s6_addr16[1] = htons((uint16_t)pi->ipi6_ifindex);
589 			}
590 			oifp = ifindex2ifnet[pi->ipi6_ifindex];
591 			difscope = pi->ipi6_ifindex;
592 			if (oifp != NULL) {
593 				ifnet_reference(oifp);
594 			}
595 		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
596 		    im6o != NULL && im6o_multicast_ifp != NULL) {
597 			oifp = im6o_multicast_ifp;
598 			ifnet_reference(oifp);
599 			if (in6_embedded_scope) {
600 				ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
601 			}
602 			difscope = oifp->if_index;
603 		} else if (dstsock->sin6_scope_id) {
604 			/*
605 			 * boundary check
606 			 *
607 			 * Sinced dstsock->sin6_scope_id is unsigned, we don't
608 			 * need to check if it's < 0
609 			 */
610 			if (!IF_INDEX_IN_RANGE(dstsock->sin6_scope_id)) {
611 				error = ENXIO;  /* XXX EINVAL? */
612 				drop_reason = DROP_REASON_IP6_BAD_SCOPE;
613 				ifnet_head_done();
614 				goto bad;
615 			}
616 			if (in6_embedded_scope) {
617 				ip6->ip6_dst.s6_addr16[1]
618 				        = htons(dstsock->sin6_scope_id & 0xffff);        /*XXX*/
619 			}
620 			difscope = dstsock->sin6_scope_id;
621 		}
622 		ifnet_head_done();
623 
624 		ip6_output_setdstifscope(m, difscope, NULL);
625 	}
626 
627 	in_pcb_check_management_entitled(in6p);
628 	in_pcb_check_ultra_constrained_entitled(in6p);
629 
630 	/*
631 	 * Source address selection.
632 	 */
633 	{
634 		struct in6_addr *__single in6a;
635 		struct in6_addr storage;
636 		u_short index = 0;
637 
638 		if (israw != 0 && optp && optp->ip6po_pktinfo && !IN6_IS_ADDR_UNSPECIFIED(&optp->ip6po_pktinfo->ipi6_addr)) {
639 			in6a = &optp->ip6po_pktinfo->ipi6_addr;
640 			flags |= IPV6_FLAG_NOSRCIFSEL;
641 			sifscope = optp->ip6po_pktinfo->ipi6_ifindex;
642 		} else {
643 			struct ifnet *__single src_ifp = NULL;
644 			in6a = in6_selectsrc(dstsock, optp, in6p,
645 			    &in6p->in6p_route, &src_ifp, &storage, ip6oa.ip6oa_boundif,
646 			    &error);
647 			if (src_ifp != NULL) {
648 				in6p->inp_lifscope  = src_ifp->if_index;
649 				ifnet_release(src_ifp);
650 			} else {
651 				in6p->inp_lifscope = ip6oa.ip6oa_boundif;
652 			}
653 			if (in6a != 0) {
654 				ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
655 			} else {
656 				if (error == 0) {
657 					error = EADDRNOTAVAIL;
658 				}
659 				goto bad;
660 			}
661 		}
662 
663 		ip6->ip6_src = *in6a;
664 		if (IN6_IS_SCOPE_EMBED(in6a) && sifscope == IFSCOPE_NONE) {
665 			sifscope = difscope;
666 		}
667 		ip6_output_setsrcifscope(m, sifscope, NULL);
668 
669 		if (in6p->in6p_route.ro_rt != NULL) {
670 			RT_LOCK(in6p->in6p_route.ro_rt);
671 			if (in6p->in6p_route.ro_rt->rt_ifp != NULL) {
672 				index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
673 			}
674 			RT_UNLOCK(in6p->in6p_route.ro_rt);
675 			if (oifp != NULL) {
676 				ifnet_release(oifp);
677 			}
678 			ifnet_head_lock_shared();
679 			if (!IF_INDEX_IN_RANGE(index)) {
680 				panic("bad if_index on interface from route");
681 			}
682 			oifp = ifindex2ifnet[index];
683 			if (oifp != NULL) {
684 				ifnet_reference(oifp);
685 			}
686 			ifnet_head_done();
687 		}
688 	}
689 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
690 	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
691 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
692 	    (IPV6_VERSION & IPV6_VERSION_MASK);
693 	/* ip6_plen will be filled in ip6_output, so not fill it here. */
694 	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
695 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
696 
697 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
698 		struct mbuf *n;
699 		int off;
700 		u_int16_t *p;
701 
702 		/* compute checksum */
703 		if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
704 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
705 		} else {
706 			off = in6p->in6p_cksum;
707 		}
708 		if (plen < (unsigned int)(off + 1)) {
709 			error = EINVAL;
710 			goto bad;
711 		}
712 		off += sizeof(struct ip6_hdr);
713 
714 		n = m;
715 		while (n && n->m_len <= off) {
716 			off -= n->m_len;
717 			n = n->m_next;
718 		}
719 		if (!n) {
720 			goto bad;
721 		}
722 		p = (u_int16_t *)(void *)(mtod(n, caddr_t) + off);
723 		*p = 0;
724 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
725 	}
726 
727 #if NECP
728 	{
729 		necp_kernel_policy_id policy_id;
730 		necp_kernel_policy_id skip_policy_id;
731 		u_int32_t route_rule_id;
732 		u_int32_t pass_flags;
733 
734 		/*
735 		 * We need a route to perform NECP route rule checks
736 		 */
737 		if ((net_qos_policy_restricted != 0 &&
738 		    ROUTE_UNUSABLE(&in6p->in6p_route))
739 #if CONTENT_FILTER
740 		    || cfil_faddr_use
741 #endif
742 		    ) {
743 			struct sockaddr_in6 to;
744 			struct sockaddr_in6 from;
745 
746 			ROUTE_RELEASE(&in6p->in6p_route);
747 
748 			bzero(&from, sizeof(struct sockaddr_in6));
749 			from.sin6_family = AF_INET6;
750 			from.sin6_len = sizeof(struct sockaddr_in6);
751 			from.sin6_addr = ip6->ip6_src;
752 
753 			bzero(&to, sizeof(struct sockaddr_in6));
754 			to.sin6_family = AF_INET6;
755 			to.sin6_len = sizeof(struct sockaddr_in6);
756 			to.sin6_addr = ip6->ip6_dst;
757 
758 			in6p->in6p_route.ro_dst.sin6_family = AF_INET6;
759 			in6p->in6p_route.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
760 			((struct sockaddr_in6 *)(void *)&in6p->in6p_route.ro_dst)->sin6_addr =
761 			    ip6->ip6_dst;
762 
763 			rtalloc_scoped((struct route *)&in6p->in6p_route, ip6oa.ip6oa_boundif);
764 
765 			inp_update_necp_policy(in6p, (struct sockaddr *)&from,
766 			    (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
767 			in6p->inp_policyresult.results.qos_marking_gencount = 0;
768 		}
769 
770 		if (!necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
771 		    &ip6->ip6_src, &ip6->ip6_dst, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
772 			error = EHOSTUNREACH;
773 			drop_reason = DROP_REASON_IP_NECP_POLICY_DROP;
774 			goto bad;
775 		}
776 
777 		necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id, skip_policy_id, pass_flags);
778 
779 		if (net_qos_policy_restricted != 0) {
780 			necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt, route_rule_id);
781 		}
782 	}
783 #endif /* NECP */
784 	if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
785 		ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
786 	}
787 
788 #if IPSEC
789 	if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
790 		error = ENOBUFS;
791 		drop_reason = DROP_REASON_IP_ENOBUFS;
792 		goto bad;
793 	}
794 #endif /*IPSEC*/
795 
796 	if (ROUTE_UNUSABLE(&in6p->in6p_route)) {
797 		ROUTE_RELEASE(&in6p->in6p_route);
798 	}
799 
800 	if (oifp != NULL) {
801 		ifnet_release(oifp);
802 		oifp = NULL;
803 	}
804 
805 	set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
806 	m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
807 	m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
808 	m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
809 	    PKTF_FLOW_RAWSOCK);
810 	m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
811 	m->m_pkthdr.tx_rawip_pid = so->last_pid;
812 	if (so->so_flags & SOF_DELEGATED) {
813 		m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
814 	} else {
815 		m->m_pkthdr.tx_rawip_e_pid = 0;
816 	}
817 #if (DEBUG || DEVELOPMENT)
818 	if (so->so_flags & SOF_MARK_WAKE_PKT) {
819 		so->so_flags &= ~SOF_MARK_WAKE_PKT;
820 		m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
821 	}
822 #endif /* (DEBUG || DEVELOPMENT) */
823 
824 	if (im6o != NULL) {
825 		IM6O_ADDREF(im6o);
826 	}
827 
828 	error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
829 	    &oifp, &ip6oa);
830 
831 	if (im6o != NULL) {
832 		IM6O_REMREF(im6o);
833 	}
834 
835 	if (in6p->in6p_route.ro_rt != NULL) {
836 		struct rtentry *rt = in6p->in6p_route.ro_rt;
837 		struct ifnet *outif;
838 
839 		if ((rt->rt_flags & RTF_MULTICAST) ||
840 		    in6p->in6p_socket == NULL ||
841 #if CONTENT_FILTER
842 		    /* Discard temporary route for cfil case */
843 		    cfil_faddr_use ||
844 #endif
845 		    !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
846 			rt = NULL;      /* unusable */
847 		}
848 		/*
849 		 * Always discard the cached route for unconnected
850 		 * socket or if it is a multicast route.
851 		 */
852 		if (rt == NULL) {
853 			ROUTE_RELEASE(&in6p->in6p_route);
854 		}
855 
856 		/*
857 		 * If this is a connected socket and the destination
858 		 * route is not multicast, update outif with that of
859 		 * the route interface index used by IP.
860 		 */
861 		if (rt != NULL) {
862 			/*
863 			 * When an NECP IP tunnel policy forces the outbound interface,
864 			 * ip6_output_list() informs the transport layer what is the actual
865 			 * outgoing interface
866 			 */
867 			if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
868 				outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
869 			} else {
870 				outif = rt->rt_ifp;
871 			}
872 			if (outif != NULL) {
873 				in6p->in6p_last_outifp = outif;
874 			}
875 		}
876 	} else {
877 		ROUTE_RELEASE(&in6p->in6p_route);
878 	}
879 
880 	/*
881 	 * If output interface was cellular/expensive, and this socket is
882 	 * denied access to it, generate an event.
883 	 */
884 	if (error != 0 && (ip6oa.ip6oa_flags & IP6OAF_R_IFDENIED) &&
885 	    (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
886 		soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
887 		    SO_FILT_HINT_IFDENIED));
888 	}
889 
890 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
891 		if (oifp) {
892 			icmp6_ifoutstat_inc(oifp, type, code);
893 		}
894 		icmp6stat.icp6s_outhist[type]++;
895 	} else {
896 		rip6stat.rip6s_opackets++;
897 	}
898 
899 	goto freectl;
900 
901 bad:
902 	if (m != NULL) {
903 		m_drop(m, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
904 	}
905 
906 freectl:
907 	if (optp == &opt && optp->ip6po_rthdr) {
908 		ROUTE_RELEASE(&optp->ip6po_route);
909 	}
910 
911 	if (control != NULL) {
912 		if (optp == &opt) {
913 			ip6_clearpktopts(optp, -1);
914 		}
915 		m_freem(control);
916 	}
917 	if (oifp != NULL) {
918 		ifnet_release(oifp);
919 	}
920 #if CONTENT_FILTER
921 	if (cfil_tag) {
922 		m_tag_free(cfil_tag);
923 	}
924 #endif
925 
926 	return error;
927 }
928 
929 /*
930  * Raw IPv6 socket option processing.
931  */
932 int
rip6_ctloutput(struct socket * so,struct sockopt * sopt)933 rip6_ctloutput(
934 	struct socket *so,
935 	struct sockopt *sopt)
936 {
937 	int error, optval;
938 
939 	/* Allow <SOL_SOCKET,SO_FLUSH/SO_BINDTODEVICE> at this level */
940 	if (sopt->sopt_level == SOL_SOCKET) {
941 		if (sopt->sopt_name == SO_BINDTODEVICE) {
942 			return ip6_ctloutput(so, sopt);
943 		} else if (sopt->sopt_name != SO_FLUSH) {
944 			return EINVAL;
945 		}
946 	} else if (sopt->sopt_level == IPPROTO_ICMPV6) {
947 		/*
948 		 * XXX: is it better to call icmp6_ctloutput() directly
949 		 * from protosw?
950 		 */
951 		return icmp6_ctloutput(so, sopt);
952 	} else if (sopt->sopt_level != IPPROTO_IPV6) {
953 		return EINVAL;
954 	}
955 
956 	error = 0;
957 
958 	switch (sopt->sopt_dir) {
959 	case SOPT_GET:
960 		switch (sopt->sopt_name) {
961 		case IPV6_CHECKSUM:
962 			error = ip6_raw_ctloutput(so, sopt);
963 			break;
964 		default:
965 			error = ip6_ctloutput(so, sopt);
966 			break;
967 		}
968 		break;
969 
970 	case SOPT_SET:
971 		switch (sopt->sopt_name) {
972 		case IPV6_CHECKSUM:
973 			error = ip6_raw_ctloutput(so, sopt);
974 			break;
975 
976 		case SO_FLUSH:
977 			if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
978 			    sizeof(optval))) != 0) {
979 				break;
980 			}
981 
982 			error = inp_flush(sotoinpcb(so), optval);
983 			break;
984 
985 		default:
986 			error = ip6_ctloutput(so, sopt);
987 			break;
988 		}
989 		break;
990 	}
991 
992 	return error;
993 }
994 
995 static int
rip6_attach(struct socket * so,int proto,struct proc * p)996 rip6_attach(struct socket *so, int proto, struct proc *p)
997 {
998 	struct inpcb *__single inp;
999 	int error;
1000 
1001 	inp = sotoinpcb(so);
1002 	if (inp) {
1003 		panic("rip6_attach");
1004 	}
1005 	if ((error = proc_suser(p)) != 0) {
1006 		return error;
1007 	}
1008 
1009 	error = soreserve(so, rip_sendspace, rip_recvspace);
1010 	if (error) {
1011 		return error;
1012 	}
1013 	error = in_pcballoc(so, &ripcbinfo, p);
1014 	if (error) {
1015 		return error;
1016 	}
1017 	inp = (struct inpcb *)so->so_pcb;
1018 	inp->inp_vflag |= INP_IPV6;
1019 	inp->in6p_ip6_nxt = (char)proto;
1020 	inp->in6p_hops = -1;    /* use kernel default */
1021 	inp->in6p_cksum = -1;
1022 	inp->in6p_icmp6filt = kalloc_type(struct icmp6_filter,
1023 	    Z_WAITOK | Z_NOFAIL);
1024 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
1025 	return 0;
1026 }
1027 
1028 static int
rip6_detach(struct socket * so)1029 rip6_detach(struct socket *so)
1030 {
1031 	struct inpcb *__single inp;
1032 
1033 	inp = sotoinpcb(so);
1034 	if (inp == 0) {
1035 		panic("rip6_detach");
1036 	}
1037 	/* xxx: RSVP */
1038 	if (inp->in6p_icmp6filt) {
1039 		kfree_type(struct icmp6_filter, inp->in6p_icmp6filt);
1040 		inp->in6p_icmp6filt = NULL;
1041 	}
1042 	in6_pcbdetach(inp);
1043 	return 0;
1044 }
1045 
1046 static int
rip6_abort(struct socket * so)1047 rip6_abort(struct socket *so)
1048 {
1049 	soisdisconnected(so);
1050 	return rip6_detach(so);
1051 }
1052 
1053 static int
rip6_disconnect(struct socket * so)1054 rip6_disconnect(struct socket *so)
1055 {
1056 	struct inpcb *__single inp = sotoinpcb(so);
1057 
1058 	if ((so->so_state & SS_ISCONNECTED) == 0) {
1059 		return ENOTCONN;
1060 	}
1061 	inp->in6p_faddr = in6addr_any;
1062 	inp->inp_fifscope = IFSCOPE_NONE;
1063 	return rip6_abort(so);
1064 }
1065 
1066 static int
rip6_bind(struct socket * so,struct sockaddr * nam,struct proc * p)1067 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1068 {
1069 #pragma unused(p)
1070 	struct inpcb *__single inp = sotoinpcb(so);
1071 	struct sockaddr_in6 sin6;
1072 	struct ifaddr *__single ifa = NULL;
1073 	struct ifnet *__single outif = NULL;
1074 	uint32_t ifscope = IFSCOPE_NONE;
1075 	int error;
1076 
1077 	if (inp == NULL
1078 #if NECP
1079 	    || (necp_socket_should_use_flow_divert(inp))
1080 #endif /* NECP */
1081 	    ) {
1082 		return inp == NULL ? EINVAL : EPROTOTYPE;
1083 	}
1084 
1085 	if (nam->sa_len != sizeof(struct sockaddr_in6)) {
1086 		return EINVAL;
1087 	}
1088 
1089 	if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6) {
1090 		return EADDRNOTAVAIL;
1091 	}
1092 
1093 	bzero(&sin6, sizeof(sin6));
1094 	*(&sin6) = *SIN6(nam);
1095 
1096 	if ((error = sa6_embedscope(&sin6, ip6_use_defzone, &ifscope)) != 0) {
1097 		return error;
1098 	}
1099 
1100 	/* Sanitize local copy for address searches */
1101 	sin6.sin6_flowinfo = 0;
1102 	sin6.sin6_port = 0;
1103 	if (in6_embedded_scope) {
1104 		sin6.sin6_scope_id = 0;
1105 	}
1106 
1107 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
1108 	    (ifa = ifa_ifwithaddr(SA(&sin6))) == 0) {
1109 		return EADDRNOTAVAIL;
1110 	}
1111 	if (ifa != NULL) {
1112 		IFA_LOCK(ifa);
1113 		if ((ifatoia6(ifa))->ia6_flags &
1114 		    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_CLAT46 |
1115 		    IN6_IFF_DETACHED | IN6_IFF_DEPRECATED)) {
1116 			IFA_UNLOCK(ifa);
1117 			ifa_remref(ifa);
1118 			return EADDRNOTAVAIL;
1119 		}
1120 		outif = ifa->ifa_ifp;
1121 		IFA_UNLOCK(ifa);
1122 		ifa_remref(ifa);
1123 	}
1124 	inp->in6p_laddr = sin6.sin6_addr;
1125 	inp->in6p_last_outifp = outif;
1126 	inp->inp_lifscope = ifscope;
1127 	in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1128 	return 0;
1129 }
1130 
1131 static int
rip6_connect(struct socket * so,struct sockaddr * nam,__unused struct proc * p)1132 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1133 {
1134 	struct inpcb *__single inp = sotoinpcb(so);
1135 	struct sockaddr_in6 *__single addr = (struct sockaddr_in6 *)(void *)nam;
1136 	struct in6_addr *__single in6a = NULL;
1137 	struct in6_addr storage;
1138 	int error = 0;
1139 #if ENABLE_DEFAULT_SCOPE
1140 	struct sockaddr_in6 tmp;
1141 #endif
1142 	unsigned int ifscope;
1143 	struct ifnet *__single outif = NULL;
1144 
1145 	if (inp == NULL
1146 #if NECP
1147 	    || (necp_socket_should_use_flow_divert(inp))
1148 #endif /* NECP */
1149 	    ) {
1150 		return inp == NULL ? EINVAL : EPROTOTYPE;
1151 	}
1152 	if (nam->sa_len != sizeof(*addr)) {
1153 		return EINVAL;
1154 	}
1155 	if (TAILQ_EMPTY(&ifnet_head)) {
1156 		return EADDRNOTAVAIL;
1157 	}
1158 	if (addr->sin6_family != AF_INET6) {
1159 		return EAFNOSUPPORT;
1160 	}
1161 
1162 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
1163 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
1164 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
1165 	}
1166 
1167 #if ENABLE_DEFAULT_SCOPE
1168 	if (addr->sin6_scope_id == 0) { /* not change if specified  */
1169 		/* avoid overwrites */
1170 		tmp = *addr;
1171 		addr = &tmp;
1172 		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
1173 	}
1174 #endif
1175 
1176 	/* KAME hack: embed scopeid */
1177 	if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&SIN6(nam)->sin6_scope_id)) != 0) {
1178 		return EINVAL;
1179 	}
1180 
1181 	ifscope = (inp->inp_flags & INP_BOUND_IF) ?
1182 	    inp->inp_boundifp->if_index : IFSCOPE_NONE;
1183 
1184 	/* Source address selection. XXX: need pcblookup? */
1185 	struct ifnet *__single src_ifp = NULL;
1186 	in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
1187 	    &src_ifp, &storage, ifscope, &error);
1188 	if (src_ifp != NULL) {
1189 		if (in6a != NULL) {
1190 			inp->inp_lifscope = in6_addr2scopeid(src_ifp, in6a);
1191 		}
1192 		ifnet_release(src_ifp);
1193 		src_ifp = NULL;
1194 	}
1195 	if (IN6_IS_SCOPE_EMBED(&addr->sin6_addr) && inp->inp_lifscope == IFSCOPE_NONE) {
1196 		inp->inp_lifscope = addr->sin6_scope_id;
1197 	}
1198 
1199 	if (in6a == NULL) {
1200 		return error ? error : EADDRNOTAVAIL;
1201 	}
1202 	inp->in6p_laddr = *in6a;
1203 	inp->in6p_faddr = addr->sin6_addr;
1204 	if (inp->in6p_route.ro_rt != NULL) {
1205 		outif = inp->in6p_route.ro_rt->rt_ifp;
1206 	}
1207 	inp->in6p_last_outifp = outif;
1208 	in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1209 	inp->inp_fifscope = addr->sin6_scope_id;
1210 	in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1211 
1212 	soisconnected(so);
1213 	return 0;
1214 }
1215 
1216 static int
rip6_shutdown(struct socket * so)1217 rip6_shutdown(struct socket *so)
1218 {
1219 	socantsendmore(so);
1220 	return 0;
1221 }
1222 
1223 static int
rip6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1224 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1225     struct mbuf *control, struct proc *p)
1226 {
1227 #pragma unused(flags, p)
1228 	struct inpcb *__single inp = sotoinpcb(so);
1229 	int error = 0;
1230 
1231 	if (inp == NULL
1232 #if NECP
1233 	    || (necp_socket_should_use_flow_divert(inp))
1234 #endif /* NECP */
1235 	    ) {
1236 		if (inp == NULL) {
1237 			error = EINVAL;
1238 		} else {
1239 			error = EPROTOTYPE;
1240 		}
1241 		goto bad;
1242 	}
1243 
1244 	so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
1245 
1246 	return rip6_output(m, so, SIN6(nam), control, 1);
1247 
1248 bad:
1249 	VERIFY(error != 0);
1250 
1251 	if (m != NULL) {
1252 		m_freem(m);
1253 	}
1254 	if (control != NULL) {
1255 		m_freem(control);
1256 	}
1257 
1258 	return error;
1259 }
1260 
1261 struct pr_usrreqs rip6_usrreqs = {
1262 	.pru_abort =            rip6_abort,
1263 	.pru_attach =           rip6_attach,
1264 	.pru_bind =             rip6_bind,
1265 	.pru_connect =          rip6_connect,
1266 	.pru_control =          in6_control,
1267 	.pru_detach =           rip6_detach,
1268 	.pru_disconnect =       rip6_disconnect,
1269 	.pru_peeraddr =         in6_getpeeraddr,
1270 	.pru_send =             rip6_send,
1271 	.pru_shutdown =         rip6_shutdown,
1272 	.pru_sockaddr =         in6_getsockaddr,
1273 	.pru_sosend =           sosend,
1274 	.pru_soreceive =        soreceive,
1275 };
1276 
1277 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
1278 	.pru_abort =            rip6_abort,
1279 	.pru_attach =           icmp6_dgram_attach,
1280 	.pru_bind =             rip6_bind,
1281 	.pru_connect =          rip6_connect,
1282 	.pru_control =          in6_control,
1283 	.pru_detach =           rip6_detach,
1284 	.pru_disconnect =       rip6_disconnect,
1285 	.pru_peeraddr =         in6_getpeeraddr,
1286 	.pru_send =             icmp6_dgram_send,
1287 	.pru_shutdown =         rip6_shutdown,
1288 	.pru_sockaddr =         in6_getsockaddr,
1289 	.pru_sosend =           sosend,
1290 	.pru_soreceive =        soreceive,
1291 };
1292