]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/raw_ip.c
c2b41a36509d2e90ffe3b9377db459c98f2e376a
[apple/xnu.git] / bsd / netinet / raw_ip.c
1 /*
2 * Copyright (c) 2000-2014 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) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
61 */
62 /*
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Version 2.0.
67 */
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/mcache.h>
75 #include <sys/proc.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <libkern/OSAtomic.h>
82 #include <kern/zalloc.h>
83
84 #include <pexpert/pexpert.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88
89 #define _IP_VHL
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip_var.h>
96
97 #if INET6
98 #include <netinet6/in6_pcb.h>
99 #endif /* INET6 */
100
101 #include <netinet/ip_fw.h>
102
103 #if IPSEC
104 #include <netinet6/ipsec.h>
105 #endif /*IPSEC*/
106
107 #if DUMMYNET
108 #include <netinet/ip_dummynet.h>
109 #endif
110
111 #if CONFIG_MACF_NET
112 #include <security/mac_framework.h>
113 #endif /* MAC_NET */
114
115 int load_ipfw(void);
116 int rip_detach(struct socket *);
117 int rip_abort(struct socket *);
118 int rip_disconnect(struct socket *);
119 int rip_bind(struct socket *, struct sockaddr *, struct proc *);
120 int rip_connect(struct socket *, struct sockaddr *, struct proc *);
121 int rip_shutdown(struct socket *);
122
123 struct inpcbhead ripcb;
124 struct inpcbinfo ripcbinfo;
125
126 /* control hooks for ipfw and dummynet */
127 #if IPFIREWALL
128 ip_fw_ctl_t *ip_fw_ctl_ptr;
129 #endif /* IPFIREWALL */
130 #if DUMMYNET
131 ip_dn_ctl_t *ip_dn_ctl_ptr;
132 #endif /* DUMMYNET */
133
134 /*
135 * Nominal space allocated to a raw ip socket.
136 */
137 #define RIPSNDQ 8192
138 #define RIPRCVQ 8192
139
140 /*
141 * Raw interface to IP protocol.
142 */
143
144 /*
145 * Initialize raw connection block q.
146 */
147 void
148 rip_init(struct protosw *pp, struct domain *dp)
149 {
150 #pragma unused(dp)
151 static int rip_initialized = 0;
152 struct inpcbinfo *pcbinfo;
153
154 VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED);
155
156 if (rip_initialized)
157 return;
158 rip_initialized = 1;
159
160 LIST_INIT(&ripcb);
161 ripcbinfo.ipi_listhead = &ripcb;
162 /*
163 * XXX We don't use the hash list for raw IP, but it's easier
164 * to allocate a one entry hash list than it is to check all
165 * over the place for ipi_hashbase == NULL.
166 */
167 ripcbinfo.ipi_hashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_hashmask);
168 ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_porthashmask);
169
170 ripcbinfo.ipi_zone = zinit(sizeof(struct inpcb),
171 (4096 * sizeof(struct inpcb)), 4096, "ripzone");
172
173 pcbinfo = &ripcbinfo;
174 /*
175 * allocate lock group attribute and group for udp pcb mutexes
176 */
177 pcbinfo->ipi_lock_grp_attr = lck_grp_attr_alloc_init();
178 pcbinfo->ipi_lock_grp = lck_grp_alloc_init("ripcb", pcbinfo->ipi_lock_grp_attr);
179
180 /*
181 * allocate the lock attribute for udp pcb mutexes
182 */
183 pcbinfo->ipi_lock_attr = lck_attr_alloc_init();
184 if ((pcbinfo->ipi_lock = lck_rw_alloc_init(pcbinfo->ipi_lock_grp,
185 pcbinfo->ipi_lock_attr)) == NULL) {
186 panic("%s: unable to allocate PCB lock\n", __func__);
187 /* NOTREACHED */
188 }
189
190 in_pcbinfo_attach(&ripcbinfo);
191 }
192
193 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET , 0, {0}, {0,0,0,0,0,0,0,0,} };
194 /*
195 * Setup generic address and protocol structures
196 * for raw_input routine, then pass them along with
197 * mbuf chain.
198 */
199 void
200 rip_input(m, iphlen)
201 struct mbuf *m;
202 int iphlen;
203 {
204 struct ip *ip = mtod(m, struct ip *);
205 struct inpcb *inp;
206 struct inpcb *last = 0;
207 struct mbuf *opts = 0;
208 int skipit = 0, ret = 0;
209 struct ifnet *ifp = m->m_pkthdr.rcvif;
210
211 /* Expect 32-bit aligned data pointer on strict-align platforms */
212 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
213
214 ripsrc.sin_addr = ip->ip_src;
215 lck_rw_lock_shared(ripcbinfo.ipi_lock);
216 LIST_FOREACH(inp, &ripcb, inp_list) {
217 #if INET6
218 if ((inp->inp_vflag & INP_IPV4) == 0)
219 continue;
220 #endif
221 if (inp->inp_ip_p && (inp->inp_ip_p != ip->ip_p))
222 continue;
223 if (inp->inp_laddr.s_addr &&
224 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
225 continue;
226 if (inp->inp_faddr.s_addr &&
227 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
228 continue;
229 if (inp_restricted_recv(inp, ifp))
230 continue;
231 if (last) {
232 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
233
234 skipit = 0;
235
236 #if NECP
237 if (n && !necp_socket_is_allowed_to_send_recv_v4(last, 0, 0,
238 &ip->ip_dst, &ip->ip_src, ifp, NULL, NULL)) {
239 m_freem(n);
240 /* do not inject data to pcb */
241 skipit = 1;
242 }
243 #endif /* NECP */
244 #if CONFIG_MACF_NET
245 if (n && skipit == 0) {
246 if (mac_inpcb_check_deliver(last, n, AF_INET,
247 SOCK_RAW) != 0) {
248 m_freem(n);
249 skipit = 1;
250 }
251 }
252 #endif
253 if (n && skipit == 0) {
254 int error = 0;
255 if ((last->inp_flags & INP_CONTROLOPTS) != 0 ||
256 (last->inp_socket->so_options & SO_TIMESTAMP) != 0 ||
257 (last->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
258 ret = ip_savecontrol(last, &opts, ip, n);
259 if (ret != 0) {
260 m_freem(n);
261 m_freem(opts);
262 last = inp;
263 continue;
264 }
265 }
266 if (last->inp_flags & INP_STRIPHDR) {
267 n->m_len -= iphlen;
268 n->m_pkthdr.len -= iphlen;
269 n->m_data += iphlen;
270 }
271 so_recv_data_stat(last->inp_socket, m, 0);
272 if (sbappendaddr(&last->inp_socket->so_rcv,
273 (struct sockaddr *)&ripsrc, n,
274 opts, &error) != 0) {
275 sorwakeup(last->inp_socket);
276 } else {
277 if (error) {
278 /* should notify about lost packet */
279 kprintf("rip_input can't append to socket\n");
280 }
281 }
282 opts = 0;
283 }
284 }
285 last = inp;
286 }
287
288 skipit = 0;
289 #if NECP
290 if (last && !necp_socket_is_allowed_to_send_recv_v4(last, 0, 0,
291 &ip->ip_dst, &ip->ip_src, ifp, NULL, NULL)) {
292 m_freem(m);
293 OSAddAtomic(1, &ipstat.ips_delivered);
294 /* do not inject data to pcb */
295 skipit = 1;
296 }
297 #endif /* NECP */
298 #if CONFIG_MACF_NET
299 if (last && skipit == 0) {
300 if (mac_inpcb_check_deliver(last, m, AF_INET, SOCK_RAW) != 0) {
301 skipit = 1;
302 m_freem(m);
303 }
304 }
305 #endif
306 if (skipit == 0) {
307 if (last) {
308 if ((last->inp_flags & INP_CONTROLOPTS) != 0 ||
309 (last->inp_socket->so_options & SO_TIMESTAMP) != 0 ||
310 (last->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
311 ret = ip_savecontrol(last, &opts, ip, m);
312 if (ret != 0) {
313 m_freem(m);
314 m_freem(opts);
315 goto unlock;
316 }
317 }
318 if (last->inp_flags & INP_STRIPHDR) {
319 m->m_len -= iphlen;
320 m->m_pkthdr.len -= iphlen;
321 m->m_data += iphlen;
322 }
323 so_recv_data_stat(last->inp_socket, m, 0);
324 if (sbappendaddr(&last->inp_socket->so_rcv,
325 (struct sockaddr *)&ripsrc, m, opts, NULL) != 0) {
326 sorwakeup(last->inp_socket);
327 } else {
328 kprintf("rip_input(2) can't append to socket\n");
329 }
330 } else {
331 m_freem(m);
332 OSAddAtomic(1, &ipstat.ips_noproto);
333 OSAddAtomic(-1, &ipstat.ips_delivered);
334 }
335 }
336 unlock:
337 /*
338 * Keep the list locked because socket filter may force the socket lock
339 * to be released when calling sbappendaddr() -- see rdar://7627704
340 */
341 lck_rw_done(ripcbinfo.ipi_lock);
342 }
343
344 /*
345 * Generate IP header and pass packet to ip_output.
346 * Tack on options user may have setup with control call.
347 */
348 int
349 rip_output(
350 struct mbuf *m,
351 struct socket *so,
352 u_int32_t dst,
353 struct mbuf *control)
354 {
355 struct ip *ip;
356 struct inpcb *inp = sotoinpcb(so);
357 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
358 struct ip_out_args ipoa =
359 { IFSCOPE_NONE, { 0 }, IPOAF_SELECT_SRCIF, 0 };
360 struct ip_moptions *imo;
361 int error = 0;
362 mbuf_svc_class_t msc = MBUF_SC_UNSPEC;
363
364 if (control != NULL) {
365 msc = mbuf_service_class_from_control(control);
366
367 m_freem(control);
368 control = NULL;
369 }
370
371 if (inp == NULL
372 #if NECP
373 || (necp_socket_should_use_flow_divert(inp))
374 #endif /* NECP */
375 ) {
376 if (m != NULL)
377 m_freem(m);
378 VERIFY(control == NULL);
379 return (inp == NULL ? EINVAL : EPROTOTYPE);
380 }
381
382 flags |= IP_OUTARGS;
383 /* If socket was bound to an ifindex, tell ip_output about it */
384 if (inp->inp_flags & INP_BOUND_IF) {
385 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
386 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
387 }
388 if (INP_NO_CELLULAR(inp))
389 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
390 if (INP_NO_EXPENSIVE(inp))
391 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
392 if (INP_AWDL_UNRESTRICTED(inp))
393 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
394
395 if (inp->inp_flowhash == 0)
396 inp->inp_flowhash = inp_calc_flowhash(inp);
397
398 /*
399 * If the user handed us a complete IP packet, use it.
400 * Otherwise, allocate an mbuf for a header and fill it in.
401 */
402 if ((inp->inp_flags & INP_HDRINCL) == 0) {
403 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
404 m_freem(m);
405 return(EMSGSIZE);
406 }
407 M_PREPEND(m, sizeof(struct ip), M_WAIT, 1);
408 if (m == NULL)
409 return ENOBUFS;
410 ip = mtod(m, struct ip *);
411 ip->ip_tos = inp->inp_ip_tos;
412 ip->ip_off = 0;
413 ip->ip_p = inp->inp_ip_p;
414 ip->ip_len = m->m_pkthdr.len;
415 ip->ip_src = inp->inp_laddr;
416 ip->ip_dst.s_addr = dst;
417 ip->ip_ttl = inp->inp_ip_ttl;
418 } else {
419 if (m->m_pkthdr.len > IP_MAXPACKET) {
420 m_freem(m);
421 return(EMSGSIZE);
422 }
423 ip = mtod(m, struct ip *);
424 /* don't allow both user specified and setsockopt options,
425 and don't allow packet length sizes that will crash */
426 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
427 && inp->inp_options)
428 || (ip->ip_len > m->m_pkthdr.len)
429 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
430 m_freem(m);
431 return EINVAL;
432 }
433 if (ip->ip_id == 0)
434 ip->ip_id = ip_randomid();
435 /* XXX prevent ip_output from overwriting header fields */
436 flags |= IP_RAWOUTPUT;
437 OSAddAtomic(1, &ipstat.ips_rawout);
438 }
439
440 if (inp->inp_laddr.s_addr != INADDR_ANY)
441 ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
442
443 #if NECP
444 {
445 necp_kernel_policy_id policy_id;
446 u_int32_t route_rule_id;
447 if (!necp_socket_is_allowed_to_send_recv_v4(inp, 0, 0,
448 &ip->ip_src, &ip->ip_dst, NULL, &policy_id, &route_rule_id)) {
449 m_freem(m);
450 return(EHOSTUNREACH);
451 }
452
453 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id);
454 }
455 #endif /* NECP */
456
457 #if IPSEC
458 if (inp->inp_sp != NULL && ipsec_setsocket(m, so) != 0) {
459 m_freem(m);
460 return ENOBUFS;
461 }
462 #endif /*IPSEC*/
463
464 if (ROUTE_UNUSABLE(&inp->inp_route))
465 ROUTE_RELEASE(&inp->inp_route);
466
467 set_packet_service_class(m, so, msc, 0);
468 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
469 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
470 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
471 PKTF_FLOW_RAWSOCK);
472 m->m_pkthdr.pkt_proto = inp->inp_ip_p;
473
474 #if CONFIG_MACF_NET
475 mac_mbuf_label_associate_inpcb(inp, m);
476 #endif
477
478 imo = inp->inp_moptions;
479 if (imo != NULL)
480 IMO_ADDREF(imo);
481 /*
482 * The domain lock is held across ip_output, so it is okay
483 * to pass the PCB cached route pointer directly to IP and
484 * the modules beneath it.
485 */
486 // TODO: PASS DOWN ROUTE RULE ID
487 error = ip_output(m, inp->inp_options, &inp->inp_route, flags,
488 imo, &ipoa);
489
490 if (imo != NULL)
491 IMO_REMREF(imo);
492
493 if (inp->inp_route.ro_rt != NULL) {
494 struct rtentry *rt = inp->inp_route.ro_rt;
495 struct ifnet *outif;
496
497 if ((rt->rt_flags & (RTF_MULTICAST|RTF_BROADCAST)) ||
498 inp->inp_socket == NULL ||
499 !(inp->inp_socket->so_state & SS_ISCONNECTED)) {
500 rt = NULL; /* unusable */
501 }
502 /*
503 * Always discard the cached route for unconnected
504 * socket or if it is a multicast route.
505 */
506 if (rt == NULL)
507 ROUTE_RELEASE(&inp->inp_route);
508
509 /*
510 * If this is a connected socket and the destination
511 * route is unicast, update outif with that of the
512 * route interface used by IP.
513 */
514 if (rt != NULL && (outif = rt->rt_ifp) != inp->inp_last_outifp)
515 inp->inp_last_outifp = outif;
516 } else {
517 ROUTE_RELEASE(&inp->inp_route);
518 }
519
520 /*
521 * If output interface was cellular/expensive, and this socket is
522 * denied access to it, generate an event.
523 */
524 if (error != 0 && (ipoa.ipoa_retflags & IPOARF_IFDENIED) &&
525 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp)))
526 soevent(so, (SO_FILT_HINT_LOCKED|SO_FILT_HINT_IFDENIED));
527
528 return (error);
529 }
530
531 #if IPFIREWALL
532 int
533 load_ipfw(void)
534 {
535 kern_return_t err;
536
537 ipfw_init();
538
539 #if DUMMYNET
540 if (!DUMMYNET_LOADED)
541 ip_dn_init();
542 #endif /* DUMMYNET */
543 err = 0;
544
545 return err == 0 && ip_fw_ctl_ptr == NULL ? -1 : err;
546 }
547 #endif /* IPFIREWALL */
548
549 /*
550 * Raw IP socket option processing.
551 */
552 int
553 rip_ctloutput(so, sopt)
554 struct socket *so;
555 struct sockopt *sopt;
556 {
557 struct inpcb *inp = sotoinpcb(so);
558 int error, optval;
559
560 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
561 if (sopt->sopt_level != IPPROTO_IP &&
562 !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH))
563 return (EINVAL);
564
565 error = 0;
566
567 switch (sopt->sopt_dir) {
568 case SOPT_GET:
569 switch (sopt->sopt_name) {
570 case IP_HDRINCL:
571 optval = inp->inp_flags & INP_HDRINCL;
572 error = sooptcopyout(sopt, &optval, sizeof optval);
573 break;
574
575 case IP_STRIPHDR:
576 optval = inp->inp_flags & INP_STRIPHDR;
577 error = sooptcopyout(sopt, &optval, sizeof optval);
578 break;
579
580 #if IPFIREWALL
581 case IP_FW_ADD:
582 case IP_FW_GET:
583 case IP_OLD_FW_ADD:
584 case IP_OLD_FW_GET:
585 if (ip_fw_ctl_ptr == 0)
586 error = load_ipfw();
587 if (ip_fw_ctl_ptr && error == 0)
588 error = ip_fw_ctl_ptr(sopt);
589 else
590 error = ENOPROTOOPT;
591 break;
592 #endif /* IPFIREWALL */
593
594 #if DUMMYNET
595 case IP_DUMMYNET_GET:
596 if (!DUMMYNET_LOADED)
597 ip_dn_init();
598 if (DUMMYNET_LOADED)
599 error = ip_dn_ctl_ptr(sopt);
600 else
601 error = ENOPROTOOPT;
602 break ;
603 #endif /* DUMMYNET */
604
605 default:
606 error = ip_ctloutput(so, sopt);
607 break;
608 }
609 break;
610
611 case SOPT_SET:
612 switch (sopt->sopt_name) {
613 case IP_HDRINCL:
614 error = sooptcopyin(sopt, &optval, sizeof optval,
615 sizeof optval);
616 if (error)
617 break;
618 if (optval)
619 inp->inp_flags |= INP_HDRINCL;
620 else
621 inp->inp_flags &= ~INP_HDRINCL;
622 break;
623
624 case IP_STRIPHDR:
625 error = sooptcopyin(sopt, &optval, sizeof optval,
626 sizeof optval);
627 if (error)
628 break;
629 if (optval)
630 inp->inp_flags |= INP_STRIPHDR;
631 else
632 inp->inp_flags &= ~INP_STRIPHDR;
633 break;
634
635 #if IPFIREWALL
636 case IP_FW_ADD:
637 case IP_FW_DEL:
638 case IP_FW_FLUSH:
639 case IP_FW_ZERO:
640 case IP_FW_RESETLOG:
641 case IP_OLD_FW_ADD:
642 case IP_OLD_FW_DEL:
643 case IP_OLD_FW_FLUSH:
644 case IP_OLD_FW_ZERO:
645 case IP_OLD_FW_RESETLOG:
646 if (ip_fw_ctl_ptr == 0)
647 error = load_ipfw();
648 if (ip_fw_ctl_ptr && error == 0)
649 error = ip_fw_ctl_ptr(sopt);
650 else
651 error = ENOPROTOOPT;
652 break;
653 #endif /* IPFIREWALL */
654
655 #if DUMMYNET
656 case IP_DUMMYNET_CONFIGURE:
657 case IP_DUMMYNET_DEL:
658 case IP_DUMMYNET_FLUSH:
659 if (!DUMMYNET_LOADED)
660 ip_dn_init();
661 if (DUMMYNET_LOADED)
662 error = ip_dn_ctl_ptr(sopt);
663 else
664 error = ENOPROTOOPT ;
665 break ;
666 #endif
667
668 case SO_FLUSH:
669 if ((error = sooptcopyin(sopt, &optval, sizeof (optval),
670 sizeof (optval))) != 0)
671 break;
672
673 error = inp_flush(inp, optval);
674 break;
675
676 default:
677 error = ip_ctloutput(so, sopt);
678 break;
679 }
680 break;
681 }
682
683 return (error);
684 }
685
686 /*
687 * This function exists solely to receive the PRC_IFDOWN messages which
688 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
689 * and calls in_ifadown() to remove all routes corresponding to that address.
690 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
691 * interface routes.
692 */
693 void
694 rip_ctlinput(
695 int cmd,
696 struct sockaddr *sa,
697 __unused void *vip)
698 {
699 struct in_ifaddr *ia;
700 struct ifnet *ifp;
701 int err;
702 int flags, done = 0;
703
704 switch (cmd) {
705 case PRC_IFDOWN:
706 lck_rw_lock_shared(in_ifaddr_rwlock);
707 for (ia = in_ifaddrhead.tqh_first; ia;
708 ia = ia->ia_link.tqe_next) {
709 IFA_LOCK(&ia->ia_ifa);
710 if (ia->ia_ifa.ifa_addr == sa &&
711 (ia->ia_flags & IFA_ROUTE)) {
712 done = 1;
713 IFA_ADDREF_LOCKED(&ia->ia_ifa);
714 IFA_UNLOCK(&ia->ia_ifa);
715 lck_rw_done(in_ifaddr_rwlock);
716 lck_mtx_lock(rnh_lock);
717 /*
718 * in_ifscrub kills the interface route.
719 */
720 in_ifscrub(ia->ia_ifp, ia, 1);
721 /*
722 * in_ifadown gets rid of all the rest of
723 * the routes. This is not quite the right
724 * thing to do, but at least if we are running
725 * a routing process they will come back.
726 */
727 in_ifadown(&ia->ia_ifa, 1);
728 lck_mtx_unlock(rnh_lock);
729 IFA_REMREF(&ia->ia_ifa);
730 break;
731 }
732 IFA_UNLOCK(&ia->ia_ifa);
733 }
734 if (!done)
735 lck_rw_done(in_ifaddr_rwlock);
736 break;
737
738 case PRC_IFUP:
739 lck_rw_lock_shared(in_ifaddr_rwlock);
740 for (ia = in_ifaddrhead.tqh_first; ia;
741 ia = ia->ia_link.tqe_next) {
742 IFA_LOCK(&ia->ia_ifa);
743 if (ia->ia_ifa.ifa_addr == sa) {
744 /* keep it locked */
745 break;
746 }
747 IFA_UNLOCK(&ia->ia_ifa);
748 }
749 if (ia == NULL || (ia->ia_flags & IFA_ROUTE) ||
750 (ia->ia_ifa.ifa_debug & IFD_NOTREADY)) {
751 if (ia != NULL)
752 IFA_UNLOCK(&ia->ia_ifa);
753 lck_rw_done(in_ifaddr_rwlock);
754 return;
755 }
756 IFA_ADDREF_LOCKED(&ia->ia_ifa);
757 IFA_UNLOCK(&ia->ia_ifa);
758 lck_rw_done(in_ifaddr_rwlock);
759
760 flags = RTF_UP;
761 ifp = ia->ia_ifa.ifa_ifp;
762
763 if ((ifp->if_flags & IFF_LOOPBACK)
764 || (ifp->if_flags & IFF_POINTOPOINT))
765 flags |= RTF_HOST;
766
767 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
768 if (err == 0) {
769 IFA_LOCK_SPIN(&ia->ia_ifa);
770 ia->ia_flags |= IFA_ROUTE;
771 IFA_UNLOCK(&ia->ia_ifa);
772 }
773 IFA_REMREF(&ia->ia_ifa);
774 break;
775 }
776 }
777
778 u_int32_t rip_sendspace = RIPSNDQ;
779 u_int32_t rip_recvspace = RIPRCVQ;
780
781 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW | CTLFLAG_LOCKED,
782 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
783 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED,
784 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
785 SYSCTL_UINT(_net_inet_raw, OID_AUTO, pcbcount, CTLFLAG_RD | CTLFLAG_LOCKED,
786 &ripcbinfo.ipi_count, 0, "Number of active PCBs");
787
788 static int
789 rip_attach(struct socket *so, int proto, struct proc *p)
790 {
791 struct inpcb *inp;
792 int error;
793
794 inp = sotoinpcb(so);
795 if (inp)
796 panic("rip_attach");
797 if ((so->so_state & SS_PRIV) == 0)
798 return (EPERM);
799
800 error = soreserve(so, rip_sendspace, rip_recvspace);
801 if (error)
802 return error;
803 error = in_pcballoc(so, &ripcbinfo, p);
804 if (error)
805 return error;
806 inp = (struct inpcb *)so->so_pcb;
807 inp->inp_vflag |= INP_IPV4;
808 inp->inp_ip_p = proto;
809 inp->inp_ip_ttl = ip_defttl;
810 return 0;
811 }
812
813 __private_extern__ int
814 rip_detach(struct socket *so)
815 {
816 struct inpcb *inp;
817
818 inp = sotoinpcb(so);
819 if (inp == 0)
820 panic("rip_detach");
821 in_pcbdetach(inp);
822 return 0;
823 }
824
825 __private_extern__ int
826 rip_abort(struct socket *so)
827 {
828 soisdisconnected(so);
829 return rip_detach(so);
830 }
831
832 __private_extern__ int
833 rip_disconnect(struct socket *so)
834 {
835 if ((so->so_state & SS_ISCONNECTED) == 0)
836 return ENOTCONN;
837 return rip_abort(so);
838 }
839
840 __private_extern__ int
841 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
842 {
843 #pragma unused(p)
844 struct inpcb *inp = sotoinpcb(so);
845 struct sockaddr_in sin;
846 struct ifaddr *ifa = NULL;
847 struct ifnet *outif = NULL;
848
849 if (inp == NULL
850 #if NECP
851 || (necp_socket_should_use_flow_divert(inp))
852 #endif /* NECP */
853 )
854 return (inp == NULL ? EINVAL : EPROTOTYPE);
855
856 if (nam->sa_len != sizeof (struct sockaddr_in))
857 return (EINVAL);
858
859 /* Sanitized local copy for interface address searches */
860 bzero(&sin, sizeof (sin));
861 sin.sin_family = AF_INET;
862 sin.sin_len = sizeof (struct sockaddr_in);
863 sin.sin_addr.s_addr = SIN(nam)->sin_addr.s_addr;
864
865 if (TAILQ_EMPTY(&ifnet_head) ||
866 (sin.sin_family != AF_INET && sin.sin_family != AF_IMPLINK) ||
867 (sin.sin_addr.s_addr && (ifa = ifa_ifwithaddr(SA(&sin))) == 0)) {
868 return (EADDRNOTAVAIL);
869 } else if (ifa) {
870 /*
871 * Opportunistically determine the outbound
872 * interface that may be used; this may not
873 * hold true if we end up using a route
874 * going over a different interface, e.g.
875 * when sending to a local address. This
876 * will get updated again after sending.
877 */
878 IFA_LOCK(ifa);
879 outif = ifa->ifa_ifp;
880 IFA_UNLOCK(ifa);
881 IFA_REMREF(ifa);
882 }
883 inp->inp_laddr = sin.sin_addr;
884 inp->inp_last_outifp = outif;
885 return (0);
886 }
887
888 __private_extern__ int
889 rip_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
890 {
891 struct inpcb *inp = sotoinpcb(so);
892 struct sockaddr_in *addr = (struct sockaddr_in *)(void *)nam;
893
894 if (inp == NULL
895 #if NECP
896 || (necp_socket_should_use_flow_divert(inp))
897 #endif /* NECP */
898 )
899 return (inp == NULL ? EINVAL : EPROTOTYPE);
900 if (nam->sa_len != sizeof(*addr))
901 return EINVAL;
902 if (TAILQ_EMPTY(&ifnet_head))
903 return EADDRNOTAVAIL;
904 if ((addr->sin_family != AF_INET) &&
905 (addr->sin_family != AF_IMPLINK))
906 return EAFNOSUPPORT;
907 inp->inp_faddr = addr->sin_addr;
908 soisconnected(so);
909
910 return 0;
911 }
912
913 __private_extern__ int
914 rip_shutdown(struct socket *so)
915 {
916 socantsendmore(so);
917 return 0;
918 }
919
920 __private_extern__ int
921 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
922 struct mbuf *control, struct proc *p)
923 {
924 #pragma unused(flags, p)
925 struct inpcb *inp = sotoinpcb(so);
926 u_int32_t dst;
927 int error = 0;
928
929 if (inp == NULL
930 #if NECP
931 || (necp_socket_should_use_flow_divert(inp) && (error = EPROTOTYPE))
932 #endif /* NECP */
933 ) {
934 if (inp == NULL)
935 error = EINVAL;
936 else
937 error = EPROTOTYPE;
938 goto bad;
939 }
940
941 if (so->so_state & SS_ISCONNECTED) {
942 if (nam != NULL) {
943 error = EISCONN;
944 goto bad;
945 }
946 dst = inp->inp_faddr.s_addr;
947 } else {
948 if (nam == NULL) {
949 error = ENOTCONN;
950 goto bad;
951 }
952 dst = ((struct sockaddr_in *)(void *)nam)->sin_addr.s_addr;
953 }
954 return (rip_output(m, so, dst, control));
955
956 bad:
957 VERIFY(error != 0);
958
959 if (m != NULL)
960 m_freem(m);
961 if (control != NULL)
962 m_freem(control);
963
964 return (error);
965 }
966
967 /* note: rip_unlock is called from different protos instead of the generic socket_unlock,
968 * it will handle the socket dealloc on last reference
969 * */
970 int
971 rip_unlock(struct socket *so, int refcount, void *debug)
972 {
973 void *lr_saved;
974 struct inpcb *inp = sotoinpcb(so);
975
976 if (debug == NULL)
977 lr_saved = __builtin_return_address(0);
978 else
979 lr_saved = debug;
980
981 if (refcount) {
982 if (so->so_usecount <= 0) {
983 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s\n",
984 so, so->so_usecount, solockhistory_nr(so));
985 /* NOTREACHED */
986 }
987 so->so_usecount--;
988 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
989 /* cleanup after last reference */
990 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
991 lck_rw_lock_exclusive(ripcbinfo.ipi_lock);
992 if (inp->inp_state != INPCB_STATE_DEAD) {
993 #if INET6
994 if (SOCK_CHECK_DOM(so, PF_INET6))
995 in6_pcbdetach(inp);
996 else
997 #endif /* INET6 */
998 in_pcbdetach(inp);
999 }
1000 in_pcbdispose(inp);
1001 lck_rw_done(ripcbinfo.ipi_lock);
1002 return(0);
1003 }
1004 }
1005 so->unlock_lr[so->next_unlock_lr] = lr_saved;
1006 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
1007 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
1008 return(0);
1009 }
1010
1011 static int
1012 rip_pcblist SYSCTL_HANDLER_ARGS
1013 {
1014 #pragma unused(oidp, arg1, arg2)
1015 int error, i, n;
1016 struct inpcb *inp, **inp_list;
1017 inp_gen_t gencnt;
1018 struct xinpgen xig;
1019
1020 /*
1021 * The process of preparing the TCB list is too time-consuming and
1022 * resource-intensive to repeat twice on every request.
1023 */
1024 lck_rw_lock_exclusive(ripcbinfo.ipi_lock);
1025 if (req->oldptr == USER_ADDR_NULL) {
1026 n = ripcbinfo.ipi_count;
1027 req->oldidx = 2 * (sizeof xig)
1028 + (n + n/8) * sizeof(struct xinpcb);
1029 lck_rw_done(ripcbinfo.ipi_lock);
1030 return 0;
1031 }
1032
1033 if (req->newptr != USER_ADDR_NULL) {
1034 lck_rw_done(ripcbinfo.ipi_lock);
1035 return EPERM;
1036 }
1037
1038 /*
1039 * OK, now we're committed to doing something.
1040 */
1041 gencnt = ripcbinfo.ipi_gencnt;
1042 n = ripcbinfo.ipi_count;
1043
1044 bzero(&xig, sizeof(xig));
1045 xig.xig_len = sizeof xig;
1046 xig.xig_count = n;
1047 xig.xig_gen = gencnt;
1048 xig.xig_sogen = so_gencnt;
1049 error = SYSCTL_OUT(req, &xig, sizeof xig);
1050 if (error) {
1051 lck_rw_done(ripcbinfo.ipi_lock);
1052 return error;
1053 }
1054 /*
1055 * We are done if there is no pcb
1056 */
1057 if (n == 0) {
1058 lck_rw_done(ripcbinfo.ipi_lock);
1059 return 0;
1060 }
1061
1062 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1063 if (inp_list == 0) {
1064 lck_rw_done(ripcbinfo.ipi_lock);
1065 return ENOMEM;
1066 }
1067
1068 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1069 inp = inp->inp_list.le_next) {
1070 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD)
1071 inp_list[i++] = inp;
1072 }
1073 n = i;
1074
1075 error = 0;
1076 for (i = 0; i < n; i++) {
1077 inp = inp_list[i];
1078 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1079 struct xinpcb xi;
1080
1081 bzero(&xi, sizeof(xi));
1082 xi.xi_len = sizeof xi;
1083 /* XXX should avoid extra copy */
1084 inpcb_to_compat(inp, &xi.xi_inp);
1085 if (inp->inp_socket)
1086 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1087 error = SYSCTL_OUT(req, &xi, sizeof xi);
1088 }
1089 }
1090 if (!error) {
1091 /*
1092 * Give the user an updated idea of our state.
1093 * If the generation differs from what we told
1094 * her before, she knows that something happened
1095 * while we were processing this request, and it
1096 * might be necessary to retry.
1097 */
1098 bzero(&xig, sizeof(xig));
1099 xig.xig_len = sizeof xig;
1100 xig.xig_gen = ripcbinfo.ipi_gencnt;
1101 xig.xig_sogen = so_gencnt;
1102 xig.xig_count = ripcbinfo.ipi_count;
1103 error = SYSCTL_OUT(req, &xig, sizeof xig);
1104 }
1105 FREE(inp_list, M_TEMP);
1106 lck_rw_done(ripcbinfo.ipi_lock);
1107 return error;
1108 }
1109
1110 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1111 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1112 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1113
1114
1115 static int
1116 rip_pcblist64 SYSCTL_HANDLER_ARGS
1117 {
1118 #pragma unused(oidp, arg1, arg2)
1119 int error, i, n;
1120 struct inpcb *inp, **inp_list;
1121 inp_gen_t gencnt;
1122 struct xinpgen xig;
1123
1124 /*
1125 * The process of preparing the TCB list is too time-consuming and
1126 * resource-intensive to repeat twice on every request.
1127 */
1128 lck_rw_lock_exclusive(ripcbinfo.ipi_lock);
1129 if (req->oldptr == USER_ADDR_NULL) {
1130 n = ripcbinfo.ipi_count;
1131 req->oldidx = 2 * (sizeof xig)
1132 + (n + n/8) * sizeof(struct xinpcb64);
1133 lck_rw_done(ripcbinfo.ipi_lock);
1134 return 0;
1135 }
1136
1137 if (req->newptr != USER_ADDR_NULL) {
1138 lck_rw_done(ripcbinfo.ipi_lock);
1139 return EPERM;
1140 }
1141
1142 /*
1143 * OK, now we're committed to doing something.
1144 */
1145 gencnt = ripcbinfo.ipi_gencnt;
1146 n = ripcbinfo.ipi_count;
1147
1148 bzero(&xig, sizeof(xig));
1149 xig.xig_len = sizeof xig;
1150 xig.xig_count = n;
1151 xig.xig_gen = gencnt;
1152 xig.xig_sogen = so_gencnt;
1153 error = SYSCTL_OUT(req, &xig, sizeof xig);
1154 if (error) {
1155 lck_rw_done(ripcbinfo.ipi_lock);
1156 return error;
1157 }
1158 /*
1159 * We are done if there is no pcb
1160 */
1161 if (n == 0) {
1162 lck_rw_done(ripcbinfo.ipi_lock);
1163 return 0;
1164 }
1165
1166 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1167 if (inp_list == 0) {
1168 lck_rw_done(ripcbinfo.ipi_lock);
1169 return ENOMEM;
1170 }
1171
1172 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1173 inp = inp->inp_list.le_next) {
1174 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD)
1175 inp_list[i++] = inp;
1176 }
1177 n = i;
1178
1179 error = 0;
1180 for (i = 0; i < n; i++) {
1181 inp = inp_list[i];
1182 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1183 struct xinpcb64 xi;
1184
1185 bzero(&xi, sizeof(xi));
1186 xi.xi_len = sizeof xi;
1187 inpcb_to_xinpcb64(inp, &xi);
1188 if (inp->inp_socket)
1189 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1190 error = SYSCTL_OUT(req, &xi, sizeof xi);
1191 }
1192 }
1193 if (!error) {
1194 /*
1195 * Give the user an updated idea of our state.
1196 * If the generation differs from what we told
1197 * her before, she knows that something happened
1198 * while we were processing this request, and it
1199 * might be necessary to retry.
1200 */
1201 bzero(&xig, sizeof(xig));
1202 xig.xig_len = sizeof xig;
1203 xig.xig_gen = ripcbinfo.ipi_gencnt;
1204 xig.xig_sogen = so_gencnt;
1205 xig.xig_count = ripcbinfo.ipi_count;
1206 error = SYSCTL_OUT(req, &xig, sizeof xig);
1207 }
1208 FREE(inp_list, M_TEMP);
1209 lck_rw_done(ripcbinfo.ipi_lock);
1210 return error;
1211 }
1212
1213 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist64,
1214 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1215 rip_pcblist64, "S,xinpcb64", "List of active raw IP sockets");
1216
1217
1218
1219 static int
1220 rip_pcblist_n SYSCTL_HANDLER_ARGS
1221 {
1222 #pragma unused(oidp, arg1, arg2)
1223 int error = 0;
1224
1225 error = get_pcblist_n(IPPROTO_IP, req, &ripcbinfo);
1226
1227 return error;
1228 }
1229
1230 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist_n,
1231 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1232 rip_pcblist_n, "S,xinpcb_n", "List of active raw IP sockets");
1233
1234 struct pr_usrreqs rip_usrreqs = {
1235 .pru_abort = rip_abort,
1236 .pru_attach = rip_attach,
1237 .pru_bind = rip_bind,
1238 .pru_connect = rip_connect,
1239 .pru_control = in_control,
1240 .pru_detach = rip_detach,
1241 .pru_disconnect = rip_disconnect,
1242 .pru_peeraddr = in_getpeeraddr,
1243 .pru_send = rip_send,
1244 .pru_shutdown = rip_shutdown,
1245 .pru_sockaddr = in_getsockaddr,
1246 .pru_sosend = sosend,
1247 .pru_soreceive = soreceive,
1248 };
1249 /* DSEP Review Done pl-20051213-v02 @3253 */