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 <kern/uipc_socket.h>
105
106 #include <net/if.h>
107 #include <net/net_api_stats.h>
108 #include <net/route.h>
109 #include <net/if_types.h>
110 #include <net/content_filter.h>
111 #include <net/droptap.h>
112
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/in_tclass.h>
117 #include <netinet/ip6.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet/icmp6.h>
120 #include <netinet/in_pcb.h>
121 #include <netinet6/in6_pcb.h>
122 #include <netinet6/nd6.h>
123 #include <netinet6/ip6protosw.h>
124 #include <netinet6/scope6_var.h>
125 #include <netinet6/raw_ip6.h>
126
127 #if IPSEC
128 #include <netinet6/ipsec.h>
129 #include <netinet6/ipsec6.h>
130 #endif /*IPSEC*/
131
132 #if NECP
133 #include <net/necp.h>
134 #endif
135
136 /*
137 * Raw interface to IP6 protocol.
138 */
139
140 extern struct inpcbhead ripcb;
141 extern struct inpcbinfo ripcbinfo;
142 extern u_int32_t rip_sendspace;
143 extern u_int32_t rip_recvspace;
144
145 struct rip6stat rip6stat;
146
147 /*
148 * Setup generic address and protocol structures
149 * for raw_input routine, then pass them along with
150 * mbuf chain.
151 */
152 int
rip6_input(struct mbuf ** mp,int * offp,int proto)153 rip6_input(
154 struct mbuf **mp,
155 int *offp,
156 int proto)
157 {
158 struct mbuf *__single m = *mp;
159 struct ip6_hdr *__single ip6 = mtod(m, struct ip6_hdr *);
160 struct inpcb *__single in6p;
161 struct inpcb *__single last = 0;
162 struct mbuf *__single opts = NULL;
163 struct sockaddr_in6 rip6src;
164 int ret;
165 struct ifnet *__single ifp = m->m_pkthdr.rcvif;
166 boolean_t is_wake_pkt = false;
167
168 /* Expect 32-bit aligned data pointer on strict-align platforms */
169 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
170
171 rip6stat.rip6s_ipackets++;
172
173 init_sin6(&rip6src, m); /* general init */
174
175 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
176 is_wake_pkt = true;
177 }
178
179 lck_rw_lock_shared(&ripcbinfo.ipi_lock);
180 LIST_FOREACH(in6p, &ripcb, inp_list) {
181 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
182 continue;
183 }
184 if (in6p->in6p_ip6_nxt &&
185 in6p->in6p_ip6_nxt != proto) {
186 continue;
187 }
188 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
189 !in6_are_addr_equal_scoped(&in6p->in6p_laddr, &ip6->ip6_dst, in6p->inp_lifscope, ifp->if_index)) {
190 continue;
191 }
192 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
193 !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &ip6->ip6_src, in6p->inp_fifscope, ifp->if_index)) {
194 continue;
195 }
196
197 if (inp_restricted_recv(in6p, ifp)) {
198 continue;
199 }
200
201 if (proto == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
202 rip6stat.rip6s_isum++;
203 if (in6_cksum(m, ip6->ip6_nxt, *offp,
204 m->m_pkthdr.len - *offp)) {
205 rip6stat.rip6s_badsum++;
206 continue;
207 }
208 }
209 if (last) {
210 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
211
212 #if NECP
213 if (n && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
214 &ip6->ip6_dst, &ip6->ip6_src, ifp, 0, NULL, NULL, NULL, NULL)) {
215 m_drop(n, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NECP_POLICY_DROP, NULL, 0);
216 /* do not inject data into pcb */
217 } else
218 #endif /* NECP */
219 if (n) {
220 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
221 SOFLOW_ENABLED(last->in6p_socket) ||
222 SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
223 ret = ip6_savecontrol(last, n, &opts);
224 if (ret != 0) {
225 m_drop(n, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_ENOBUFS, NULL, 0);
226 m_freem(opts);
227 opts = NULL;
228 last = in6p;
229 continue;
230 }
231 }
232 /* strip intermediate headers */
233 m_adj(n, *offp);
234 so_recv_data_stat(last->in6p_socket, m, 0);
235 if (sbappendaddr(&last->in6p_socket->so_rcv,
236 (struct sockaddr *)&rip6src,
237 n, opts, NULL) == 0) {
238 rip6stat.rip6s_fullsock++;
239 } else {
240 sorwakeup(last->in6p_socket);
241 }
242 if (is_wake_pkt) {
243 soevent(in6p->in6p_socket,
244 SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
245 }
246 opts = NULL;
247 }
248 }
249 last = in6p;
250 }
251
252 #if NECP
253 if (last && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
254 &ip6->ip6_dst, &ip6->ip6_src, ifp, 0, NULL, NULL, NULL, NULL)) {
255 m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NECP_POLICY_DROP, NULL, 0);
256 ip6stat.ip6s_delivered--;
257 /* do not inject data into pcb */
258 } else
259 #endif /* NECP */
260 if (last) {
261 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
262 SOFLOW_ENABLED(last->in6p_socket) ||
263 SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
264 ret = ip6_savecontrol(last, m, &opts);
265 if (ret != 0) {
266 m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_ENOBUFS, NULL, 0);
267 m = NULL;
268 m_freem(opts);
269 ip6stat.ip6s_delivered--;
270 goto unlock;
271 }
272 }
273 /* strip intermediate headers */
274 m_adj(m, *offp);
275 so_recv_data_stat(last->in6p_socket, m, 0);
276 if (sbappendaddr(&last->in6p_socket->so_rcv,
277 (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
278 rip6stat.rip6s_fullsock++;
279 } else {
280 sorwakeup(last->in6p_socket);
281 }
282 if (is_wake_pkt) {
283 soevent(last->in6p_socket,
284 SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
285 }
286 } else {
287 rip6stat.rip6s_nosock++;
288 if (m->m_flags & M_MCAST) {
289 rip6stat.rip6s_nosockmcast++;
290 }
291 if (proto == IPPROTO_NONE) {
292 m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NO_SOCK, NULL, 0);
293 } else {
294 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
295 icmp6_error(m, ICMP6_PARAM_PROB,
296 ICMP6_PARAMPROB_NEXTHEADER,
297 (int)(prvnxtp - mtod(m, char *)));
298 }
299 ip6stat.ip6s_delivered--;
300 }
301
302 unlock:
303 lck_rw_done(&ripcbinfo.ipi_lock);
304
305 return IPPROTO_DONE;
306 }
307
308 void
rip6_ctlinput(int cmd,struct sockaddr * sa,void * d,__unused struct ifnet * ifp)309 rip6_ctlinput(
310 int cmd,
311 struct sockaddr *sa,
312 void *d,
313 __unused struct ifnet *ifp)
314 {
315 struct ip6_hdr *__single ip6 = NULL;
316 struct mbuf *__single m = NULL;
317 void *__single cmdarg = NULL;
318 int off = 0;
319 struct ip6ctlparam *__single ip6cp = NULL;
320 const struct sockaddr_in6 *__single sa6_src = NULL;
321 void (*notify)(struct inpcb *, int) = in6_rtchange;
322
323 if (sa->sa_family != AF_INET6 ||
324 sa->sa_len != sizeof(struct sockaddr_in6)) {
325 return;
326 }
327
328 if ((unsigned)cmd >= PRC_NCMDS) {
329 return;
330 }
331 if (PRC_IS_REDIRECT(cmd)) {
332 notify = in6_rtchange;
333 d = NULL;
334 } else if (cmd == PRC_HOSTDEAD) {
335 d = NULL;
336 } else if (inet6ctlerrmap[cmd] == 0) {
337 return;
338 }
339
340 /* if the parameter is from icmp6, decode it. */
341 if (d != NULL) {
342 ip6cp = (struct ip6ctlparam *)d;
343 m = ip6cp->ip6c_m;
344 ip6 = ip6cp->ip6c_ip6;
345 off = ip6cp->ip6c_off;
346 cmdarg = ip6cp->ip6c_cmdarg;
347 sa6_src = ip6cp->ip6c_src;
348 } else {
349 m = NULL;
350 ip6 = NULL;
351 cmdarg = NULL;
352 sa6_src = &sa6_any;
353 }
354
355 (void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
356 0, cmd, cmdarg, notify);
357 }
358
359 /*
360 * Generate IPv6 header and pass packet to ip6_output.
361 * Tack on options user may have setup with control call.
362 */
363 int
rip6_output(struct mbuf * m,struct socket * so,struct sockaddr_in6 * dstsock,struct mbuf * control,int israw)364 rip6_output(
365 struct mbuf *m,
366 struct socket *so,
367 struct sockaddr_in6 *dstsock,
368 struct mbuf *control,
369 int israw)
370 {
371 struct in6_addr *__single dst;
372 struct ip6_hdr *__single ip6;
373 struct inpcb *__single in6p;
374 u_int plen = m->m_pkthdr.len;
375 int error = 0;
376 struct ip6_pktopts opt, *__single optp = NULL;
377 struct ip6_moptions *__single im6o = NULL;
378 struct ifnet *__single oifp = NULL;
379 int type = 0, code = 0; /* for ICMPv6 output statistics only */
380 struct sock_cm_info sockcminfo;
381 struct ip6_out_args ip6oa;
382 int flags = IPV6_OUTARGS;
383 struct sockaddr_in6 tmp;
384 #if CONTENT_FILTER
385 struct m_tag *cfil_tag = NULL;
386 bool cfil_faddr_use = false;
387 uint32_t cfil_so_state_change_cnt = 0;
388 uint32_t cfil_so_options = 0;
389 uint32_t sifscope = IFSCOPE_NONE, difscope = IFSCOPE_NONE;
390 struct sockaddr *__single cfil_faddr = NULL;
391 struct sockaddr_in6 *__single cfil_sin6 = NULL;
392 #endif
393 drop_reason_t drop_reason = DROP_REASON_UNSPECIFIED;
394
395 in6p = sotoin6pcb(so);
396 if (in6p == NULL) {
397 error = EINVAL;
398 goto bad;
399 }
400
401 #if CONTENT_FILTER
402 /*
403 * If socket is subject to Content Filter and no addr is passed in,
404 * retrieve CFIL saved state from mbuf and use it if necessary.
405 */
406 if (CFIL_DGRAM_FILTERED(so) && !dstsock) {
407 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, NULL);
408 if (cfil_tag) {
409 cfil_sin6 = SIN6(cfil_faddr);
410 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
411 /*
412 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
413 * the connect/disconnect logic.
414 */
415 dstsock = cfil_sin6;
416 } else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
417 (in6p->in6p_fport != cfil_sin6->sin6_port ||
418 !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &cfil_sin6->sin6_addr, in6p->inp_fifscope, cfil_sin6->sin6_scope_id))) {
419 /*
420 * Socket is connected but socket state and dest addr/port changed.
421 * We need to use the saved faddr and socket options.
422 */
423 cfil_faddr_use = true;
424 }
425 }
426 }
427 #endif
428
429 /* always copy sockaddr to avoid overwrites */
430 if (so->so_state & SS_ISCONNECTED) {
431 if (dstsock != NULL) {
432 error = EISCONN;
433 drop_reason = DROP_REASON_IP_EISCONN;
434 goto bad;
435 }
436 /* XXX */
437 bzero(&tmp, sizeof(tmp));
438 tmp.sin6_family = AF_INET6;
439 tmp.sin6_len = sizeof(struct sockaddr_in6);
440 bcopy(
441 #if CONTENT_FILTER
442 cfil_faddr_use ? &cfil_sin6->sin6_addr :
443 #endif
444 &in6p->in6p_faddr, &tmp.sin6_addr, sizeof(struct in6_addr));
445 dstsock = &tmp;
446 } else {
447 if (dstsock == NULL) {
448 error = ENOTCONN;
449 goto bad;
450 }
451 tmp = *dstsock;
452 dstsock = &tmp;
453 }
454
455 #if ENABLE_DEFAULT_SCOPE
456 if (dstsock->sin6_scope_id == 0) { /* not change if specified */
457 dstsock->sin6_scope_id = scope6_addr2default(&dstsock->sin6_addr);
458 }
459 #endif
460
461 bzero(&ip6oa, sizeof(ip6oa));
462 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
463 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
464
465 if (in6p == NULL
466 #if NECP
467 || (necp_socket_should_use_flow_divert(in6p))
468 #endif /* NECP */
469 ) {
470 if (in6p == NULL) {
471 error = EINVAL;
472 } else {
473 error = EPROTOTYPE;
474 }
475 goto bad;
476 }
477 if (dstsock != NULL && IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
478 error = EINVAL;
479 goto bad;
480 }
481
482 if (in6p->inp_flags & INP_BOUND_IF) {
483 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
484 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
485 } else if (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&in6p->in6p_faddr)) {
486 ip6oa.ip6oa_boundif = dstsock->sin6_scope_id;
487 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
488 }
489 if (INP_NO_CELLULAR(in6p)) {
490 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
491 }
492 if (INP_NO_EXPENSIVE(in6p)) {
493 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
494 }
495 if (INP_NO_CONSTRAINED(in6p)) {
496 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
497 }
498 if (INP_AWDL_UNRESTRICTED(in6p)) {
499 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
500 }
501 if (INP_INTCOPROC_ALLOWED(in6p)) {
502 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
503 }
504 if (INP_MANAGEMENT_ALLOWED(in6p)) {
505 ip6oa.ip6oa_flags |= IP6OAF_MANAGEMENT_ALLOWED;
506 }
507 if (INP_ULTRA_CONSTRAINED_ALLOWED(in6p)) {
508 ip6oa.ip6oa_flags |= IP6OAF_ULTRA_CONSTRAINED_ALLOWED;
509 }
510
511 sock_init_cm_info(&sockcminfo, so);
512
513 dst = &dstsock->sin6_addr;
514 if (control) {
515 sock_parse_cm_info(control, &sockcminfo);
516
517 if ((error = ip6_setpktopts(control, &opt,
518 in6p->in6p_outputopts, SOCK_PROTO(so))) != 0) {
519 goto bad;
520 }
521 optp = &opt;
522 } else {
523 optp = in6p->in6p_outputopts;
524 }
525 ip6oa.ip6oa_sotc = sockcminfo.sotc;
526 ip6oa.ip6oa_netsvctype = sockcminfo.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, sockcminfo.sotc, PKT_SCF_IPV6);
809 if (sockcminfo.tx_time) {
810 mbuf_set_tx_time(m, sockcminfo.tx_time);
811 }
812 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
813 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
814 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
815 PKTF_FLOW_RAWSOCK);
816 m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
817 m->m_pkthdr.tx_rawip_pid = so->last_pid;
818 if (so->so_flags & SOF_DELEGATED) {
819 m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
820 } else {
821 m->m_pkthdr.tx_rawip_e_pid = 0;
822 }
823 #if (DEBUG || DEVELOPMENT)
824 if (so->so_flags & SOF_MARK_WAKE_PKT) {
825 so->so_flags &= ~SOF_MARK_WAKE_PKT;
826 m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
827 }
828 #endif /* (DEBUG || DEVELOPMENT) */
829
830 if (im6o != NULL) {
831 IM6O_ADDREF(im6o);
832 }
833
834 error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
835 &oifp, &ip6oa);
836
837 if (im6o != NULL) {
838 IM6O_REMREF(im6o);
839 }
840
841 if (in6p->in6p_route.ro_rt != NULL) {
842 struct rtentry *rt = in6p->in6p_route.ro_rt;
843 struct ifnet *outif;
844
845 if ((rt->rt_flags & RTF_MULTICAST) ||
846 in6p->in6p_socket == NULL ||
847 #if CONTENT_FILTER
848 /* Discard temporary route for cfil case */
849 cfil_faddr_use ||
850 #endif
851 !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
852 rt = NULL; /* unusable */
853 }
854 /*
855 * Always discard the cached route for unconnected
856 * socket or if it is a multicast route.
857 */
858 if (rt == NULL) {
859 ROUTE_RELEASE(&in6p->in6p_route);
860 }
861
862 /*
863 * If this is a connected socket and the destination
864 * route is not multicast, update outif with that of
865 * the route interface index used by IP.
866 */
867 if (rt != NULL) {
868 /*
869 * When an NECP IP tunnel policy forces the outbound interface,
870 * ip6_output_list() informs the transport layer what is the actual
871 * outgoing interface
872 */
873 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
874 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
875 } else {
876 outif = rt->rt_ifp;
877 }
878 if (outif != NULL) {
879 in6p->in6p_last_outifp = outif;
880 }
881 }
882 } else {
883 ROUTE_RELEASE(&in6p->in6p_route);
884 }
885
886 /*
887 * If output interface was cellular/expensive, and this socket is
888 * denied access to it, generate an event.
889 */
890 if (error != 0 && (ip6oa.ip6oa_flags & IP6OAF_R_IFDENIED) &&
891 (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
892 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
893 SO_FILT_HINT_IFDENIED));
894 }
895
896 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
897 if (oifp) {
898 icmp6_ifoutstat_inc(oifp, type, code);
899 }
900 icmp6stat.icp6s_outhist[type]++;
901 } else {
902 rip6stat.rip6s_opackets++;
903 }
904
905 goto freectl;
906
907 bad:
908 if (m != NULL) {
909 m_drop_if(m, oifp, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
910 }
911
912 freectl:
913 if (optp == &opt && optp->ip6po_rthdr) {
914 ROUTE_RELEASE(&optp->ip6po_route);
915 }
916
917 if (control != NULL) {
918 if (optp == &opt) {
919 ip6_clearpktopts(optp, -1);
920 }
921 m_freem(control);
922 }
923 if (oifp != NULL) {
924 ifnet_release(oifp);
925 }
926 #if CONTENT_FILTER
927 if (cfil_tag) {
928 m_tag_free(cfil_tag);
929 }
930 #endif
931
932 return error;
933 }
934
935 /*
936 * Raw IPv6 socket option processing.
937 */
938 int
rip6_ctloutput(struct socket * so,struct sockopt * sopt)939 rip6_ctloutput(
940 struct socket *so,
941 struct sockopt *sopt)
942 {
943 int error, optval;
944
945 /* Allow <SOL_SOCKET,SO_FLUSH/SO_BINDTODEVICE> at this level */
946 if (sopt->sopt_level == SOL_SOCKET) {
947 if (sopt->sopt_name == SO_BINDTODEVICE) {
948 return ip6_ctloutput(so, sopt);
949 } else if (sopt->sopt_name != SO_FLUSH) {
950 return EINVAL;
951 }
952 } else if (sopt->sopt_level == IPPROTO_ICMPV6) {
953 /*
954 * XXX: is it better to call icmp6_ctloutput() directly
955 * from protosw?
956 */
957 return icmp6_ctloutput(so, sopt);
958 } else if (sopt->sopt_level != IPPROTO_IPV6) {
959 return EINVAL;
960 }
961
962 error = 0;
963
964 switch (sopt->sopt_dir) {
965 case SOPT_GET:
966 switch (sopt->sopt_name) {
967 case IPV6_CHECKSUM:
968 error = ip6_raw_ctloutput(so, sopt);
969 break;
970 default:
971 error = ip6_ctloutput(so, sopt);
972 break;
973 }
974 break;
975
976 case SOPT_SET:
977 switch (sopt->sopt_name) {
978 case IPV6_CHECKSUM:
979 error = ip6_raw_ctloutput(so, sopt);
980 break;
981
982 case SO_FLUSH:
983 if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
984 sizeof(optval))) != 0) {
985 break;
986 }
987
988 error = inp_flush(sotoinpcb(so), optval);
989 break;
990
991 default:
992 error = ip6_ctloutput(so, sopt);
993 break;
994 }
995 break;
996 }
997
998 return error;
999 }
1000
1001 static int
rip6_attach(struct socket * so,int proto,struct proc * p)1002 rip6_attach(struct socket *so, int proto, struct proc *p)
1003 {
1004 struct inpcb *__single inp;
1005 int error;
1006
1007 inp = sotoinpcb(so);
1008 if (inp) {
1009 panic("rip6_attach");
1010 }
1011 if ((error = proc_suser(p)) != 0) {
1012 return error;
1013 }
1014
1015 error = soreserve(so, rip_sendspace, rip_recvspace);
1016 if (error) {
1017 return error;
1018 }
1019 error = in_pcballoc(so, &ripcbinfo, p);
1020 if (error) {
1021 return error;
1022 }
1023 inp = (struct inpcb *)so->so_pcb;
1024 inp->inp_vflag |= INP_IPV6;
1025 inp->in6p_ip6_nxt = (char)proto;
1026 inp->in6p_hops = -1; /* use kernel default */
1027 inp->in6p_cksum = -1;
1028 inp->in6p_icmp6filt = kalloc_type(struct icmp6_filter,
1029 Z_WAITOK | Z_NOFAIL);
1030 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
1031 return 0;
1032 }
1033
1034 static int
rip6_detach(struct socket * so)1035 rip6_detach(struct socket *so)
1036 {
1037 struct inpcb *__single inp;
1038
1039 inp = sotoinpcb(so);
1040 if (inp == 0) {
1041 panic("rip6_detach");
1042 }
1043 /* xxx: RSVP */
1044 if (inp->in6p_icmp6filt) {
1045 kfree_type(struct icmp6_filter, inp->in6p_icmp6filt);
1046 inp->in6p_icmp6filt = NULL;
1047 }
1048 in6_pcbdetach(inp);
1049 return 0;
1050 }
1051
1052 static int
rip6_abort(struct socket * so)1053 rip6_abort(struct socket *so)
1054 {
1055 soisdisconnected(so);
1056 return rip6_detach(so);
1057 }
1058
1059 static int
rip6_disconnect(struct socket * so)1060 rip6_disconnect(struct socket *so)
1061 {
1062 struct inpcb *__single inp = sotoinpcb(so);
1063
1064 if ((so->so_state & SS_ISCONNECTED) == 0) {
1065 return ENOTCONN;
1066 }
1067 inp->in6p_faddr = in6addr_any;
1068 inp->inp_fifscope = IFSCOPE_NONE;
1069 return rip6_abort(so);
1070 }
1071
1072 static int
rip6_bind(struct socket * so,struct sockaddr * nam,struct proc * p)1073 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1074 {
1075 #pragma unused(p)
1076 struct inpcb *__single inp = sotoinpcb(so);
1077 struct sockaddr_in6 sin6;
1078 struct ifaddr *__single ifa = NULL;
1079 struct ifnet *__single outif = NULL;
1080 uint32_t ifscope = IFSCOPE_NONE;
1081 int error;
1082
1083 if (inp == NULL
1084 #if NECP
1085 || (necp_socket_should_use_flow_divert(inp))
1086 #endif /* NECP */
1087 ) {
1088 return inp == NULL ? EINVAL : EPROTOTYPE;
1089 }
1090
1091 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
1092 return EINVAL;
1093 }
1094
1095 if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6) {
1096 return EADDRNOTAVAIL;
1097 }
1098
1099 bzero(&sin6, sizeof(sin6));
1100 *(&sin6) = *SIN6(nam);
1101
1102 if ((error = sa6_embedscope(&sin6, ip6_use_defzone, &ifscope)) != 0) {
1103 return error;
1104 }
1105
1106 /* Sanitize local copy for address searches */
1107 sin6.sin6_flowinfo = 0;
1108 sin6.sin6_port = 0;
1109 if (in6_embedded_scope) {
1110 sin6.sin6_scope_id = 0;
1111 }
1112
1113 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
1114 (ifa = ifa_ifwithaddr(SA(&sin6))) == 0) {
1115 return EADDRNOTAVAIL;
1116 }
1117 if (ifa != NULL) {
1118 IFA_LOCK(ifa);
1119 if ((ifatoia6(ifa))->ia6_flags &
1120 (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_CLAT46 |
1121 IN6_IFF_DETACHED | IN6_IFF_DEPRECATED)) {
1122 IFA_UNLOCK(ifa);
1123 ifa_remref(ifa);
1124 return EADDRNOTAVAIL;
1125 }
1126 outif = ifa->ifa_ifp;
1127 IFA_UNLOCK(ifa);
1128 ifa_remref(ifa);
1129 }
1130 inp->in6p_laddr = sin6.sin6_addr;
1131 inp->in6p_last_outifp = outif;
1132 inp->inp_lifscope = ifscope;
1133 in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1134 return 0;
1135 }
1136
1137 static int
rip6_connect(struct socket * so,struct sockaddr * nam,__unused struct proc * p)1138 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1139 {
1140 struct inpcb *__single inp = sotoinpcb(so);
1141 struct sockaddr_in6 *__single addr = (struct sockaddr_in6 *)(void *)nam;
1142 struct in6_addr *__single in6a = NULL;
1143 struct in6_addr storage;
1144 int error = 0;
1145 #if ENABLE_DEFAULT_SCOPE
1146 struct sockaddr_in6 tmp;
1147 #endif
1148 unsigned int ifscope;
1149 struct ifnet *__single outif = NULL;
1150
1151 if (inp == NULL
1152 #if NECP
1153 || (necp_socket_should_use_flow_divert(inp))
1154 #endif /* NECP */
1155 ) {
1156 return inp == NULL ? EINVAL : EPROTOTYPE;
1157 }
1158 if (nam->sa_len != sizeof(*addr)) {
1159 return EINVAL;
1160 }
1161 if (TAILQ_EMPTY(&ifnet_head)) {
1162 return EADDRNOTAVAIL;
1163 }
1164 if (addr->sin6_family != AF_INET6) {
1165 return EAFNOSUPPORT;
1166 }
1167
1168 if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
1169 so->so_flags1 |= SOF1_CONNECT_COUNTED;
1170 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
1171 }
1172
1173 #if ENABLE_DEFAULT_SCOPE
1174 if (addr->sin6_scope_id == 0) { /* not change if specified */
1175 /* avoid overwrites */
1176 tmp = *addr;
1177 addr = &tmp;
1178 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
1179 }
1180 #endif
1181
1182 /* KAME hack: embed scopeid */
1183 if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&SIN6(nam)->sin6_scope_id)) != 0) {
1184 return EINVAL;
1185 }
1186
1187 ifscope = (inp->inp_flags & INP_BOUND_IF) ?
1188 inp->inp_boundifp->if_index : IFSCOPE_NONE;
1189
1190 /* Source address selection. XXX: need pcblookup? */
1191 struct ifnet *__single src_ifp = NULL;
1192 in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
1193 &src_ifp, &storage, ifscope, &error);
1194 if (src_ifp != NULL) {
1195 if (in6a != NULL) {
1196 inp->inp_lifscope = in6_addr2scopeid(src_ifp, in6a);
1197 }
1198 ifnet_release(src_ifp);
1199 src_ifp = NULL;
1200 }
1201 if (IN6_IS_SCOPE_EMBED(&addr->sin6_addr) && inp->inp_lifscope == IFSCOPE_NONE) {
1202 inp->inp_lifscope = addr->sin6_scope_id;
1203 }
1204
1205 if (in6a == NULL) {
1206 return error ? error : EADDRNOTAVAIL;
1207 }
1208 inp->in6p_laddr = *in6a;
1209 inp->in6p_faddr = addr->sin6_addr;
1210 if (inp->in6p_route.ro_rt != NULL) {
1211 outif = inp->in6p_route.ro_rt->rt_ifp;
1212 }
1213 inp->in6p_last_outifp = outif;
1214 in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1215 inp->inp_fifscope = addr->sin6_scope_id;
1216 in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1217
1218 soisconnected(so);
1219 return 0;
1220 }
1221
1222 static int
rip6_shutdown(struct socket * so)1223 rip6_shutdown(struct socket *so)
1224 {
1225 socantsendmore(so);
1226 return 0;
1227 }
1228
1229 static int
rip6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1230 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1231 struct mbuf *control, struct proc *p)
1232 {
1233 #pragma unused(flags, p)
1234 struct inpcb *__single inp = sotoinpcb(so);
1235 int error = 0;
1236
1237 if (inp == NULL
1238 #if NECP
1239 || (necp_socket_should_use_flow_divert(inp))
1240 #endif /* NECP */
1241 ) {
1242 if (inp == NULL) {
1243 error = EINVAL;
1244 } else {
1245 error = EPROTOTYPE;
1246 }
1247 goto bad;
1248 }
1249
1250 so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
1251
1252 return rip6_output(m, so, SIN6(nam), control, 1);
1253
1254 bad:
1255 VERIFY(error != 0);
1256
1257 if (m != NULL) {
1258 m_freem(m);
1259 }
1260 if (control != NULL) {
1261 m_freem(control);
1262 }
1263
1264 return error;
1265 }
1266
1267 struct pr_usrreqs rip6_usrreqs = {
1268 .pru_abort = rip6_abort,
1269 .pru_attach = rip6_attach,
1270 .pru_bind = rip6_bind,
1271 .pru_connect = rip6_connect,
1272 .pru_control = in6_control,
1273 .pru_detach = rip6_detach,
1274 .pru_disconnect = rip6_disconnect,
1275 .pru_peeraddr = in6_getpeeraddr,
1276 .pru_send = rip6_send,
1277 .pru_shutdown = rip6_shutdown,
1278 .pru_sockaddr = in6_getsockaddr,
1279 .pru_sosend = sosend,
1280 .pru_soreceive = soreceive,
1281 };
1282
1283 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
1284 .pru_abort = rip6_abort,
1285 .pru_attach = icmp6_dgram_attach,
1286 .pru_bind = rip6_bind,
1287 .pru_connect = rip6_connect,
1288 .pru_control = in6_control,
1289 .pru_detach = rip6_detach,
1290 .pru_disconnect = rip6_disconnect,
1291 .pru_peeraddr = in6_getpeeraddr,
1292 .pru_send = icmp6_dgram_send,
1293 .pru_shutdown = rip6_shutdown,
1294 .pru_sockaddr = in6_getsockaddr,
1295 .pru_sosend = sosend,
1296 .pru_soreceive = soreceive,
1297 };
1298