xref: /xnu-11417.121.6/bsd/netinet6/raw_ip6.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
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 	if (INP_ULTRA_CONSTRAINED_ALLOWED(in6p)) {
506 		ip6oa.ip6oa_flags |= IP6OAF_ULTRA_CONSTRAINED_ALLOWED;
507 	}
508 
509 	dst = &dstsock->sin6_addr;
510 	if (control) {
511 		sotc = so_tc_from_control(control, &netsvctype);
512 
513 		if ((error = ip6_setpktopts(control, &opt,
514 		    in6p->in6p_outputopts, SOCK_PROTO(so))) != 0) {
515 			goto bad;
516 		}
517 		optp = &opt;
518 	} else {
519 		optp = in6p->in6p_outputopts;
520 	}
521 	if (sotc == SO_TC_UNSPEC) {
522 		sotc = so->so_traffic_class;
523 		netsvctype = so->so_netsvctype;
524 	}
525 	ip6oa.ip6oa_sotc = sotc;
526 	ip6oa.ip6oa_netsvctype = netsvctype;
527 
528 	/*
529 	 * For an ICMPv6 packet, we should know its type and code
530 	 * to update statistics.
531 	 */
532 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
533 		struct icmp6_hdr *icmp6;
534 		if (m->m_len < sizeof(struct icmp6_hdr) &&
535 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
536 			error = ENOBUFS;
537 			goto bad;
538 		}
539 		icmp6 = mtod(m, struct icmp6_hdr *);
540 		type = icmp6->icmp6_type;
541 		code = icmp6->icmp6_code;
542 	}
543 
544 	if (in6p->inp_flowhash == 0) {
545 		inp_calc_flowhash(in6p);
546 		ASSERT(in6p->inp_flowhash != 0);
547 	}
548 	/* update flowinfo - RFC 6437 */
549 	if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
550 		in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
551 		in6p->inp_flow |=
552 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
553 	}
554 
555 	M_PREPEND(m, sizeof(*ip6), M_WAIT, 1);
556 	if (m == NULL) {
557 		error = ENOBUFS;
558 		goto bad;
559 	}
560 	ip6 = mtod(m, struct ip6_hdr *);
561 
562 	/*
563 	 * Next header might not be ICMP6 but use its pseudo header anyway.
564 	 */
565 	ip6->ip6_dst = *dst;
566 
567 	im6o = in6p->in6p_moptions;
568 
569 	/*
570 	 * If the scope of the destination is link-local, embed the interface
571 	 * index in the address.
572 	 *
573 	 * XXX advanced-api value overrides sin6_scope_id
574 	 */
575 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
576 		struct in6_pktinfo *pi;
577 		struct ifnet *im6o_multicast_ifp = NULL;
578 
579 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && im6o != NULL) {
580 			IM6O_LOCK(im6o);
581 			im6o_multicast_ifp = im6o->im6o_multicast_ifp;
582 			IM6O_UNLOCK(im6o);
583 		}
584 		/*
585 		 * XXX Boundary check is assumed to be already done in
586 		 * ip6_setpktoptions().
587 		 */
588 		ifnet_head_lock_shared();
589 		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
590 			if (in6_embedded_scope) {
591 				ip6->ip6_dst.s6_addr16[1] = htons((uint16_t)pi->ipi6_ifindex);
592 			}
593 			oifp = ifindex2ifnet[pi->ipi6_ifindex];
594 			difscope = pi->ipi6_ifindex;
595 			if (oifp != NULL) {
596 				ifnet_reference(oifp);
597 			}
598 		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
599 		    im6o != NULL && im6o_multicast_ifp != NULL) {
600 			oifp = im6o_multicast_ifp;
601 			ifnet_reference(oifp);
602 			if (in6_embedded_scope) {
603 				ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
604 			}
605 			difscope = oifp->if_index;
606 		} else if (dstsock->sin6_scope_id) {
607 			/*
608 			 * boundary check
609 			 *
610 			 * Sinced dstsock->sin6_scope_id is unsigned, we don't
611 			 * need to check if it's < 0
612 			 */
613 			if (!IF_INDEX_IN_RANGE(dstsock->sin6_scope_id)) {
614 				error = ENXIO;  /* XXX EINVAL? */
615 				drop_reason = DROP_REASON_IP6_BAD_SCOPE;
616 				ifnet_head_done();
617 				goto bad;
618 			}
619 			if (in6_embedded_scope) {
620 				ip6->ip6_dst.s6_addr16[1]
621 				        = htons(dstsock->sin6_scope_id & 0xffff);        /*XXX*/
622 			}
623 			difscope = dstsock->sin6_scope_id;
624 		}
625 		ifnet_head_done();
626 
627 		ip6_output_setdstifscope(m, difscope, NULL);
628 	}
629 
630 	in_pcb_check_management_entitled(in6p);
631 	in_pcb_check_ultra_constrained_entitled(in6p);
632 
633 	/*
634 	 * Source address selection.
635 	 */
636 	{
637 		struct in6_addr *__single in6a;
638 		struct in6_addr storage;
639 		u_short index = 0;
640 
641 		if (israw != 0 && optp && optp->ip6po_pktinfo && !IN6_IS_ADDR_UNSPECIFIED(&optp->ip6po_pktinfo->ipi6_addr)) {
642 			in6a = &optp->ip6po_pktinfo->ipi6_addr;
643 			flags |= IPV6_FLAG_NOSRCIFSEL;
644 			sifscope = optp->ip6po_pktinfo->ipi6_ifindex;
645 		} else {
646 			struct ifnet *__single src_ifp = NULL;
647 			in6a = in6_selectsrc(dstsock, optp, in6p,
648 			    &in6p->in6p_route, &src_ifp, &storage, ip6oa.ip6oa_boundif,
649 			    &error);
650 			if (src_ifp != NULL) {
651 				in6p->inp_lifscope  = src_ifp->if_index;
652 				ifnet_release(src_ifp);
653 			} else {
654 				in6p->inp_lifscope = ip6oa.ip6oa_boundif;
655 			}
656 			if (in6a != 0) {
657 				ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
658 			} else {
659 				if (error == 0) {
660 					error = EADDRNOTAVAIL;
661 				}
662 				goto bad;
663 			}
664 		}
665 
666 		ip6->ip6_src = *in6a;
667 		if (IN6_IS_SCOPE_EMBED(in6a) && sifscope == IFSCOPE_NONE) {
668 			sifscope = difscope;
669 		}
670 		ip6_output_setsrcifscope(m, sifscope, NULL);
671 
672 		if (in6p->in6p_route.ro_rt != NULL) {
673 			RT_LOCK(in6p->in6p_route.ro_rt);
674 			if (in6p->in6p_route.ro_rt->rt_ifp != NULL) {
675 				index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
676 			}
677 			RT_UNLOCK(in6p->in6p_route.ro_rt);
678 			if (oifp != NULL) {
679 				ifnet_release(oifp);
680 			}
681 			ifnet_head_lock_shared();
682 			if (!IF_INDEX_IN_RANGE(index)) {
683 				panic("bad if_index on interface from route");
684 			}
685 			oifp = ifindex2ifnet[index];
686 			if (oifp != NULL) {
687 				ifnet_reference(oifp);
688 			}
689 			ifnet_head_done();
690 		}
691 	}
692 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
693 	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
694 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
695 	    (IPV6_VERSION & IPV6_VERSION_MASK);
696 	/* ip6_plen will be filled in ip6_output, so not fill it here. */
697 	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
698 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
699 
700 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
701 		struct mbuf *n;
702 		int off;
703 		u_int16_t *p;
704 
705 		/* compute checksum */
706 		if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
707 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
708 		} else {
709 			off = in6p->in6p_cksum;
710 		}
711 		if (plen < (unsigned int)(off + 1)) {
712 			error = EINVAL;
713 			goto bad;
714 		}
715 		off += sizeof(struct ip6_hdr);
716 
717 		n = m;
718 		while (n && n->m_len <= off) {
719 			off -= n->m_len;
720 			n = n->m_next;
721 		}
722 		if (!n) {
723 			goto bad;
724 		}
725 		p = (u_int16_t *)(void *)(mtod(n, caddr_t) + off);
726 		*p = 0;
727 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
728 	}
729 
730 #if NECP
731 	{
732 		necp_kernel_policy_id policy_id;
733 		necp_kernel_policy_id skip_policy_id;
734 		u_int32_t route_rule_id;
735 		u_int32_t pass_flags;
736 
737 		/*
738 		 * We need a route to perform NECP route rule checks
739 		 */
740 		if ((net_qos_policy_restricted != 0 &&
741 		    ROUTE_UNUSABLE(&in6p->in6p_route))
742 #if CONTENT_FILTER
743 		    || cfil_faddr_use
744 #endif
745 		    ) {
746 			struct sockaddr_in6 to;
747 			struct sockaddr_in6 from;
748 
749 			ROUTE_RELEASE(&in6p->in6p_route);
750 
751 			bzero(&from, sizeof(struct sockaddr_in6));
752 			from.sin6_family = AF_INET6;
753 			from.sin6_len = sizeof(struct sockaddr_in6);
754 			from.sin6_addr = ip6->ip6_src;
755 
756 			bzero(&to, sizeof(struct sockaddr_in6));
757 			to.sin6_family = AF_INET6;
758 			to.sin6_len = sizeof(struct sockaddr_in6);
759 			to.sin6_addr = ip6->ip6_dst;
760 
761 			in6p->in6p_route.ro_dst.sin6_family = AF_INET6;
762 			in6p->in6p_route.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
763 			((struct sockaddr_in6 *)(void *)&in6p->in6p_route.ro_dst)->sin6_addr =
764 			    ip6->ip6_dst;
765 
766 			rtalloc_scoped((struct route *)&in6p->in6p_route, ip6oa.ip6oa_boundif);
767 
768 			inp_update_necp_policy(in6p, (struct sockaddr *)&from,
769 			    (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
770 			in6p->inp_policyresult.results.qos_marking_gencount = 0;
771 		}
772 
773 		if (!necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
774 		    &ip6->ip6_src, &ip6->ip6_dst, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
775 			error = EHOSTUNREACH;
776 			drop_reason = DROP_REASON_IP_NECP_POLICY_DROP;
777 			goto bad;
778 		}
779 
780 		necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id, skip_policy_id, pass_flags);
781 
782 		if (net_qos_policy_restricted != 0) {
783 			necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt, route_rule_id);
784 		}
785 	}
786 #endif /* NECP */
787 	if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
788 		ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
789 	}
790 
791 #if IPSEC
792 	if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
793 		error = ENOBUFS;
794 		drop_reason = DROP_REASON_IP_ENOBUFS;
795 		goto bad;
796 	}
797 #endif /*IPSEC*/
798 
799 	if (ROUTE_UNUSABLE(&in6p->in6p_route)) {
800 		ROUTE_RELEASE(&in6p->in6p_route);
801 	}
802 
803 	if (oifp != NULL) {
804 		ifnet_release(oifp);
805 		oifp = NULL;
806 	}
807 
808 	set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
809 	m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
810 	m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
811 	m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
812 	    PKTF_FLOW_RAWSOCK);
813 	m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
814 	m->m_pkthdr.tx_rawip_pid = so->last_pid;
815 	if (so->so_flags & SOF_DELEGATED) {
816 		m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
817 	} else {
818 		m->m_pkthdr.tx_rawip_e_pid = 0;
819 	}
820 #if (DEBUG || DEVELOPMENT)
821 	if (so->so_flags & SOF_MARK_WAKE_PKT) {
822 		so->so_flags &= ~SOF_MARK_WAKE_PKT;
823 		m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
824 	}
825 #endif /* (DEBUG || DEVELOPMENT) */
826 
827 	if (im6o != NULL) {
828 		IM6O_ADDREF(im6o);
829 	}
830 
831 	error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
832 	    &oifp, &ip6oa);
833 
834 	if (im6o != NULL) {
835 		IM6O_REMREF(im6o);
836 	}
837 
838 	if (in6p->in6p_route.ro_rt != NULL) {
839 		struct rtentry *rt = in6p->in6p_route.ro_rt;
840 		struct ifnet *outif;
841 
842 		if ((rt->rt_flags & RTF_MULTICAST) ||
843 		    in6p->in6p_socket == NULL ||
844 #if CONTENT_FILTER
845 		    /* Discard temporary route for cfil case */
846 		    cfil_faddr_use ||
847 #endif
848 		    !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
849 			rt = NULL;      /* unusable */
850 		}
851 		/*
852 		 * Always discard the cached route for unconnected
853 		 * socket or if it is a multicast route.
854 		 */
855 		if (rt == NULL) {
856 			ROUTE_RELEASE(&in6p->in6p_route);
857 		}
858 
859 		/*
860 		 * If this is a connected socket and the destination
861 		 * route is not multicast, update outif with that of
862 		 * the route interface index used by IP.
863 		 */
864 		if (rt != NULL) {
865 			/*
866 			 * When an NECP IP tunnel policy forces the outbound interface,
867 			 * ip6_output_list() informs the transport layer what is the actual
868 			 * outgoing interface
869 			 */
870 			if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
871 				outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
872 			} else {
873 				outif = rt->rt_ifp;
874 			}
875 			if (outif != NULL) {
876 				in6p->in6p_last_outifp = outif;
877 			}
878 		}
879 	} else {
880 		ROUTE_RELEASE(&in6p->in6p_route);
881 	}
882 
883 	/*
884 	 * If output interface was cellular/expensive, and this socket is
885 	 * denied access to it, generate an event.
886 	 */
887 	if (error != 0 && (ip6oa.ip6oa_flags & IP6OAF_R_IFDENIED) &&
888 	    (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
889 		soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
890 		    SO_FILT_HINT_IFDENIED));
891 	}
892 
893 	if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
894 		if (oifp) {
895 			icmp6_ifoutstat_inc(oifp, type, code);
896 		}
897 		icmp6stat.icp6s_outhist[type]++;
898 	} else {
899 		rip6stat.rip6s_opackets++;
900 	}
901 
902 	goto freectl;
903 
904 bad:
905 	if (m != NULL) {
906 		m_drop(m, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
907 	}
908 
909 freectl:
910 	if (optp == &opt && optp->ip6po_rthdr) {
911 		ROUTE_RELEASE(&optp->ip6po_route);
912 	}
913 
914 	if (control != NULL) {
915 		if (optp == &opt) {
916 			ip6_clearpktopts(optp, -1);
917 		}
918 		m_freem(control);
919 	}
920 	if (oifp != NULL) {
921 		ifnet_release(oifp);
922 	}
923 #if CONTENT_FILTER
924 	if (cfil_tag) {
925 		m_tag_free(cfil_tag);
926 	}
927 #endif
928 
929 	return error;
930 }
931 
932 /*
933  * Raw IPv6 socket option processing.
934  */
935 int
rip6_ctloutput(struct socket * so,struct sockopt * sopt)936 rip6_ctloutput(
937 	struct socket *so,
938 	struct sockopt *sopt)
939 {
940 	int error, optval;
941 
942 	/* Allow <SOL_SOCKET,SO_FLUSH/SO_BINDTODEVICE> at this level */
943 	if (sopt->sopt_level == SOL_SOCKET) {
944 		if (sopt->sopt_name == SO_BINDTODEVICE) {
945 			return ip6_ctloutput(so, sopt);
946 		} else if (sopt->sopt_name != SO_FLUSH) {
947 			return EINVAL;
948 		}
949 	} else if (sopt->sopt_level == IPPROTO_ICMPV6) {
950 		/*
951 		 * XXX: is it better to call icmp6_ctloutput() directly
952 		 * from protosw?
953 		 */
954 		return icmp6_ctloutput(so, sopt);
955 	} else if (sopt->sopt_level != IPPROTO_IPV6) {
956 		return EINVAL;
957 	}
958 
959 	error = 0;
960 
961 	switch (sopt->sopt_dir) {
962 	case SOPT_GET:
963 		switch (sopt->sopt_name) {
964 		case IPV6_CHECKSUM:
965 			error = ip6_raw_ctloutput(so, sopt);
966 			break;
967 		default:
968 			error = ip6_ctloutput(so, sopt);
969 			break;
970 		}
971 		break;
972 
973 	case SOPT_SET:
974 		switch (sopt->sopt_name) {
975 		case IPV6_CHECKSUM:
976 			error = ip6_raw_ctloutput(so, sopt);
977 			break;
978 
979 		case SO_FLUSH:
980 			if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
981 			    sizeof(optval))) != 0) {
982 				break;
983 			}
984 
985 			error = inp_flush(sotoinpcb(so), optval);
986 			break;
987 
988 		default:
989 			error = ip6_ctloutput(so, sopt);
990 			break;
991 		}
992 		break;
993 	}
994 
995 	return error;
996 }
997 
998 static int
rip6_attach(struct socket * so,int proto,struct proc * p)999 rip6_attach(struct socket *so, int proto, struct proc *p)
1000 {
1001 	struct inpcb *__single inp;
1002 	int error;
1003 
1004 	inp = sotoinpcb(so);
1005 	if (inp) {
1006 		panic("rip6_attach");
1007 	}
1008 	if ((error = proc_suser(p)) != 0) {
1009 		return error;
1010 	}
1011 
1012 	error = soreserve(so, rip_sendspace, rip_recvspace);
1013 	if (error) {
1014 		return error;
1015 	}
1016 	error = in_pcballoc(so, &ripcbinfo, p);
1017 	if (error) {
1018 		return error;
1019 	}
1020 	inp = (struct inpcb *)so->so_pcb;
1021 	inp->inp_vflag |= INP_IPV6;
1022 	inp->in6p_ip6_nxt = (char)proto;
1023 	inp->in6p_hops = -1;    /* use kernel default */
1024 	inp->in6p_cksum = -1;
1025 	inp->in6p_icmp6filt = kalloc_type(struct icmp6_filter,
1026 	    Z_WAITOK | Z_NOFAIL);
1027 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
1028 	return 0;
1029 }
1030 
1031 static int
rip6_detach(struct socket * so)1032 rip6_detach(struct socket *so)
1033 {
1034 	struct inpcb *__single inp;
1035 
1036 	inp = sotoinpcb(so);
1037 	if (inp == 0) {
1038 		panic("rip6_detach");
1039 	}
1040 	/* xxx: RSVP */
1041 	if (inp->in6p_icmp6filt) {
1042 		kfree_type(struct icmp6_filter, inp->in6p_icmp6filt);
1043 		inp->in6p_icmp6filt = NULL;
1044 	}
1045 	in6_pcbdetach(inp);
1046 	return 0;
1047 }
1048 
1049 static int
rip6_abort(struct socket * so)1050 rip6_abort(struct socket *so)
1051 {
1052 	soisdisconnected(so);
1053 	return rip6_detach(so);
1054 }
1055 
1056 static int
rip6_disconnect(struct socket * so)1057 rip6_disconnect(struct socket *so)
1058 {
1059 	struct inpcb *__single inp = sotoinpcb(so);
1060 
1061 	if ((so->so_state & SS_ISCONNECTED) == 0) {
1062 		return ENOTCONN;
1063 	}
1064 	inp->in6p_faddr = in6addr_any;
1065 	inp->inp_fifscope = IFSCOPE_NONE;
1066 	return rip6_abort(so);
1067 }
1068 
1069 static int
rip6_bind(struct socket * so,struct sockaddr * nam,struct proc * p)1070 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1071 {
1072 #pragma unused(p)
1073 	struct inpcb *__single inp = sotoinpcb(so);
1074 	struct sockaddr_in6 sin6;
1075 	struct ifaddr *__single ifa = NULL;
1076 	struct ifnet *__single outif = NULL;
1077 	uint32_t ifscope = IFSCOPE_NONE;
1078 	int error;
1079 
1080 	if (inp == NULL
1081 #if NECP
1082 	    || (necp_socket_should_use_flow_divert(inp))
1083 #endif /* NECP */
1084 	    ) {
1085 		return inp == NULL ? EINVAL : EPROTOTYPE;
1086 	}
1087 
1088 	if (nam->sa_len != sizeof(struct sockaddr_in6)) {
1089 		return EINVAL;
1090 	}
1091 
1092 	if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6) {
1093 		return EADDRNOTAVAIL;
1094 	}
1095 
1096 	bzero(&sin6, sizeof(sin6));
1097 	*(&sin6) = *SIN6(nam);
1098 
1099 	if ((error = sa6_embedscope(&sin6, ip6_use_defzone, &ifscope)) != 0) {
1100 		return error;
1101 	}
1102 
1103 	/* Sanitize local copy for address searches */
1104 	sin6.sin6_flowinfo = 0;
1105 	sin6.sin6_port = 0;
1106 	if (in6_embedded_scope) {
1107 		sin6.sin6_scope_id = 0;
1108 	}
1109 
1110 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
1111 	    (ifa = ifa_ifwithaddr(SA(&sin6))) == 0) {
1112 		return EADDRNOTAVAIL;
1113 	}
1114 	if (ifa != NULL) {
1115 		IFA_LOCK(ifa);
1116 		if ((ifatoia6(ifa))->ia6_flags &
1117 		    (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_CLAT46 |
1118 		    IN6_IFF_DETACHED | IN6_IFF_DEPRECATED)) {
1119 			IFA_UNLOCK(ifa);
1120 			ifa_remref(ifa);
1121 			return EADDRNOTAVAIL;
1122 		}
1123 		outif = ifa->ifa_ifp;
1124 		IFA_UNLOCK(ifa);
1125 		ifa_remref(ifa);
1126 	}
1127 	inp->in6p_laddr = sin6.sin6_addr;
1128 	inp->in6p_last_outifp = outif;
1129 	inp->inp_lifscope = ifscope;
1130 	in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1131 	return 0;
1132 }
1133 
1134 static int
rip6_connect(struct socket * so,struct sockaddr * nam,__unused struct proc * p)1135 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1136 {
1137 	struct inpcb *__single inp = sotoinpcb(so);
1138 	struct sockaddr_in6 *__single addr = (struct sockaddr_in6 *)(void *)nam;
1139 	struct in6_addr *__single in6a = NULL;
1140 	struct in6_addr storage;
1141 	int error = 0;
1142 #if ENABLE_DEFAULT_SCOPE
1143 	struct sockaddr_in6 tmp;
1144 #endif
1145 	unsigned int ifscope;
1146 	struct ifnet *__single outif = NULL;
1147 
1148 	if (inp == NULL
1149 #if NECP
1150 	    || (necp_socket_should_use_flow_divert(inp))
1151 #endif /* NECP */
1152 	    ) {
1153 		return inp == NULL ? EINVAL : EPROTOTYPE;
1154 	}
1155 	if (nam->sa_len != sizeof(*addr)) {
1156 		return EINVAL;
1157 	}
1158 	if (TAILQ_EMPTY(&ifnet_head)) {
1159 		return EADDRNOTAVAIL;
1160 	}
1161 	if (addr->sin6_family != AF_INET6) {
1162 		return EAFNOSUPPORT;
1163 	}
1164 
1165 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
1166 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
1167 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
1168 	}
1169 
1170 #if ENABLE_DEFAULT_SCOPE
1171 	if (addr->sin6_scope_id == 0) { /* not change if specified  */
1172 		/* avoid overwrites */
1173 		tmp = *addr;
1174 		addr = &tmp;
1175 		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
1176 	}
1177 #endif
1178 
1179 	/* KAME hack: embed scopeid */
1180 	if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&SIN6(nam)->sin6_scope_id)) != 0) {
1181 		return EINVAL;
1182 	}
1183 
1184 	ifscope = (inp->inp_flags & INP_BOUND_IF) ?
1185 	    inp->inp_boundifp->if_index : IFSCOPE_NONE;
1186 
1187 	/* Source address selection. XXX: need pcblookup? */
1188 	struct ifnet *__single src_ifp = NULL;
1189 	in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
1190 	    &src_ifp, &storage, ifscope, &error);
1191 	if (src_ifp != NULL) {
1192 		if (in6a != NULL) {
1193 			inp->inp_lifscope = in6_addr2scopeid(src_ifp, in6a);
1194 		}
1195 		ifnet_release(src_ifp);
1196 		src_ifp = NULL;
1197 	}
1198 	if (IN6_IS_SCOPE_EMBED(&addr->sin6_addr) && inp->inp_lifscope == IFSCOPE_NONE) {
1199 		inp->inp_lifscope = addr->sin6_scope_id;
1200 	}
1201 
1202 	if (in6a == NULL) {
1203 		return error ? error : EADDRNOTAVAIL;
1204 	}
1205 	inp->in6p_laddr = *in6a;
1206 	inp->in6p_faddr = addr->sin6_addr;
1207 	if (inp->in6p_route.ro_rt != NULL) {
1208 		outif = inp->in6p_route.ro_rt->rt_ifp;
1209 	}
1210 	inp->in6p_last_outifp = outif;
1211 	in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1212 	inp->inp_fifscope = addr->sin6_scope_id;
1213 	in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1214 
1215 	soisconnected(so);
1216 	return 0;
1217 }
1218 
1219 static int
rip6_shutdown(struct socket * so)1220 rip6_shutdown(struct socket *so)
1221 {
1222 	socantsendmore(so);
1223 	return 0;
1224 }
1225 
1226 static int
rip6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1227 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1228     struct mbuf *control, struct proc *p)
1229 {
1230 #pragma unused(flags, p)
1231 	struct inpcb *__single inp = sotoinpcb(so);
1232 	int error = 0;
1233 
1234 	if (inp == NULL
1235 #if NECP
1236 	    || (necp_socket_should_use_flow_divert(inp))
1237 #endif /* NECP */
1238 	    ) {
1239 		if (inp == NULL) {
1240 			error = EINVAL;
1241 		} else {
1242 			error = EPROTOTYPE;
1243 		}
1244 		goto bad;
1245 	}
1246 
1247 	so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
1248 
1249 	return rip6_output(m, so, SIN6(nam), control, 1);
1250 
1251 bad:
1252 	VERIFY(error != 0);
1253 
1254 	if (m != NULL) {
1255 		m_freem(m);
1256 	}
1257 	if (control != NULL) {
1258 		m_freem(control);
1259 	}
1260 
1261 	return error;
1262 }
1263 
1264 struct pr_usrreqs rip6_usrreqs = {
1265 	.pru_abort =            rip6_abort,
1266 	.pru_attach =           rip6_attach,
1267 	.pru_bind =             rip6_bind,
1268 	.pru_connect =          rip6_connect,
1269 	.pru_control =          in6_control,
1270 	.pru_detach =           rip6_detach,
1271 	.pru_disconnect =       rip6_disconnect,
1272 	.pru_peeraddr =         in6_getpeeraddr,
1273 	.pru_send =             rip6_send,
1274 	.pru_shutdown =         rip6_shutdown,
1275 	.pru_sockaddr =         in6_getsockaddr,
1276 	.pru_sosend =           sosend,
1277 	.pru_soreceive =        soreceive,
1278 };
1279 
1280 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
1281 	.pru_abort =            rip6_abort,
1282 	.pru_attach =           icmp6_dgram_attach,
1283 	.pru_bind =             rip6_bind,
1284 	.pru_connect =          rip6_connect,
1285 	.pru_control =          in6_control,
1286 	.pru_detach =           rip6_detach,
1287 	.pru_disconnect =       rip6_disconnect,
1288 	.pru_peeraddr =         in6_getpeeraddr,
1289 	.pru_send =             icmp6_dgram_send,
1290 	.pru_shutdown =         rip6_shutdown,
1291 	.pru_sockaddr =         in6_getsockaddr,
1292 	.pru_sosend =           sosend,
1293 	.pru_soreceive =        soreceive,
1294 };
1295