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