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