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