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