]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/raw_ip.c
xnu-1504.3.12.tar.gz
[apple/xnu.git] / bsd / netinet / raw_ip.c
CommitLineData
1c79356b 1/*
c910b4d9 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
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/proc.h>
91447636 75#include <sys/domain.h>
1c79356b
A
76#include <sys/protosw.h>
77#include <sys/socket.h>
78#include <sys/socketvar.h>
79#include <sys/sysctl.h>
2d21ac55
A
80#include <libkern/OSAtomic.h>
81#include <kern/zalloc.h>
1c79356b 82
2d21ac55 83#include <pexpert/pexpert.h>
1c79356b
A
84
85#include <net/if.h>
86#include <net/route.h>
87
88#define _IP_VHL
89#include <netinet/in.h>
90#include <netinet/in_systm.h>
91#include <netinet/ip.h>
1c79356b
A
92#include <netinet/in_pcb.h>
93#include <netinet/in_var.h>
94#include <netinet/ip_var.h>
95#include <netinet/ip_mroute.h>
96
b0d623f7
A
97#if INET6
98#include <netinet6/in6_pcb.h>
99#endif /* INET6 */
100
1c79356b
A
101#include <netinet/ip_fw.h>
102
103#if IPSEC
104#include <netinet6/ipsec.h>
105#endif /*IPSEC*/
106
1c79356b
A
107#if DUMMYNET
108#include <netinet/ip_dummynet.h>
109#endif
9bccf70c 110
2d21ac55
A
111#if CONFIG_MACF_NET
112#include <security/mac_framework.h>
113#endif /* MAC_NET */
114
115int load_ipfw(void);
116int rip_detach(struct socket *);
117int rip_abort(struct socket *);
118int rip_disconnect(struct socket *);
119int rip_bind(struct socket *, struct sockaddr *, struct proc *);
120int rip_connect(struct socket *, struct sockaddr *, struct proc *);
121int rip_shutdown(struct socket *);
122
9bccf70c
A
123#if IPSEC
124extern int ipsec_bypass;
1c79356b
A
125#endif
126
9bccf70c
A
127struct inpcbhead ripcb;
128struct inpcbinfo ripcbinfo;
1c79356b 129
91447636 130/* control hooks for ipfw and dummynet */
4a3eedf9 131#if IPFIREWALL
91447636
A
132ip_fw_ctl_t *ip_fw_ctl_ptr;
133#if DUMMYNET
134ip_dn_ctl_t *ip_dn_ctl_ptr;
135#endif /* DUMMYNET */
4a3eedf9 136#endif /* IPFIREWALL */
91447636 137
1c79356b
A
138/*
139 * Nominal space allocated to a raw ip socket.
140 */
141#define RIPSNDQ 8192
142#define RIPRCVQ 8192
143
144/*
145 * Raw interface to IP protocol.
146 */
147
148/*
149 * Initialize raw connection block q.
150 */
151void
152rip_init()
153{
91447636
A
154 struct inpcbinfo *pcbinfo;
155
1c79356b
A
156 LIST_INIT(&ripcb);
157 ripcbinfo.listhead = &ripcb;
158 /*
159 * XXX We don't use the hash list for raw IP, but it's easier
160 * to allocate a one entry hash list than it is to check all
161 * over the place for hashbase == NULL.
162 */
163 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
164 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
165
166 ripcbinfo.ipi_zone = (void *) zinit(sizeof(struct inpcb),
167 (4096 * sizeof(struct inpcb)),
168 4096, "ripzone");
169
91447636
A
170 pcbinfo = &ripcbinfo;
171 /*
172 * allocate lock group attribute and group for udp pcb mutexes
173 */
174 pcbinfo->mtx_grp_attr = lck_grp_attr_alloc_init();
91447636
A
175
176 pcbinfo->mtx_grp = lck_grp_alloc_init("ripcb", pcbinfo->mtx_grp_attr);
177
178 /*
179 * allocate the lock attribute for udp pcb mutexes
180 */
181 pcbinfo->mtx_attr = lck_attr_alloc_init();
91447636
A
182
183 if ((pcbinfo->mtx = lck_rw_alloc_init(pcbinfo->mtx_grp, pcbinfo->mtx_attr)) == NULL)
184 return; /* pretty much dead if this fails... */
185
1c79356b
A
186}
187
2d21ac55 188static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET , 0, {0}, {0,0,0,0,0,0,0,0,} };
1c79356b
A
189/*
190 * Setup generic address and protocol structures
191 * for raw_input routine, then pass them along with
192 * mbuf chain.
193 */
194void
195rip_input(m, iphlen)
196 struct mbuf *m;
197 int iphlen;
198{
199 register struct ip *ip = mtod(m, struct ip *);
200 register struct inpcb *inp;
201 struct inpcb *last = 0;
202 struct mbuf *opts = 0;
91447636 203 int skipit;
1c79356b
A
204
205 ripsrc.sin_addr = ip->ip_src;
91447636 206 lck_rw_lock_shared(ripcbinfo.mtx);
1c79356b 207 LIST_FOREACH(inp, &ripcb, inp_list) {
9bccf70c
A
208#if INET6
209 if ((inp->inp_vflag & INP_IPV4) == 0)
1c79356b 210 continue;
9bccf70c
A
211#endif
212 if (inp->inp_ip_p && (inp->inp_ip_p != ip->ip_p))
1c79356b
A
213 continue;
214 if (inp->inp_laddr.s_addr &&
215 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
216 continue;
217 if (inp->inp_faddr.s_addr &&
218 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
219 continue;
220 if (last) {
221 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
91447636 222
9bccf70c
A
223#if IPSEC
224 /* check AH/ESP integrity. */
91447636
A
225 skipit = 0;
226 if (ipsec_bypass == 0 && n) {
91447636
A
227 if (ipsec4_in_reject_so(n, last->inp_socket)) {
228 m_freem(n);
2d21ac55 229 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
91447636
A
230 /* do not inject data to pcb */
231 skipit = 1;
232 }
91447636 233 }
9bccf70c 234#endif /*IPSEC*/
2d21ac55
A
235#if CONFIG_MACF_NET
236 if (n && skipit == 0) {
237 if (mac_inpcb_check_deliver(last, n, AF_INET,
238 SOCK_RAW) != 0)
239 skipit = 1;
240 }
241#endif
91447636
A
242 if (n && skipit == 0) {
243 int error = 0;
1c79356b
A
244 if (last->inp_flags & INP_CONTROLOPTS ||
245 last->inp_socket->so_options & SO_TIMESTAMP)
246 ip_savecontrol(last, &opts, ip, n);
9bccf70c
A
247 if (last->inp_flags & INP_STRIPHDR) {
248 n->m_len -= iphlen;
249 n->m_pkthdr.len -= iphlen;
250 n->m_data += iphlen;
251 }
91447636 252// ###LOCK need to lock that socket?
1c79356b
A
253 if (sbappendaddr(&last->inp_socket->so_rcv,
254 (struct sockaddr *)&ripsrc, n,
91447636 255 opts, &error) != 0) {
9bccf70c 256 sorwakeup(last->inp_socket);
91447636
A
257 }
258 else {
259 if (error) {
260 /* should notify about lost packet */
261 kprintf("rip_input can't append to socket\n");
262 }
263 }
1c79356b
A
264 opts = 0;
265 }
266 }
267 last = inp;
268 }
91447636 269 lck_rw_done(ripcbinfo.mtx);
9bccf70c
A
270#if IPSEC
271 /* check AH/ESP integrity. */
91447636
A
272 skipit = 0;
273 if (ipsec_bypass == 0 && last) {
91447636
A
274 if (ipsec4_in_reject_so(m, last->inp_socket)) {
275 m_freem(m);
2d21ac55 276 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
b0d623f7 277 OSAddAtomic(1, &ipstat.ips_delivered);
91447636
A
278 /* do not inject data to pcb */
279 skipit = 1;
280 }
91447636 281 }
9bccf70c 282#endif /*IPSEC*/
2d21ac55
A
283#if CONFIG_MACF_NET
284 if (last && skipit == 0) {
285 if (mac_inpcb_check_deliver(last, m, AF_INET, SOCK_RAW) != 0)
286 skipit = 1;
287 }
288#endif
91447636
A
289 if (skipit == 0) {
290 if (last) {
291 if (last->inp_flags & INP_CONTROLOPTS ||
292 last->inp_socket->so_options & SO_TIMESTAMP)
293 ip_savecontrol(last, &opts, ip, m);
294 if (last->inp_flags & INP_STRIPHDR) {
295 m->m_len -= iphlen;
296 m->m_pkthdr.len -= iphlen;
297 m->m_data += iphlen;
298 }
299 if (sbappendaddr(&last->inp_socket->so_rcv,
300 (struct sockaddr *)&ripsrc, m, opts, NULL) != 0) {
301 sorwakeup(last->inp_socket);
302 } else {
303 kprintf("rip_input(2) can't append to socket\n");
304 }
305 } else {
1c79356b 306 m_freem(m);
b0d623f7
A
307 OSAddAtomic(1, &ipstat.ips_noproto);
308 OSAddAtomic(-1, &ipstat.ips_delivered);
91447636 309 }
9bccf70c 310 }
1c79356b
A
311}
312
313/*
314 * Generate IP header and pass packet to ip_output.
315 * Tack on options user may have setup with control call.
316 */
317int
318rip_output(m, so, dst)
319 register struct mbuf *m;
320 struct socket *so;
b0d623f7 321 u_int32_t dst;
1c79356b
A
322{
323 register struct ip *ip;
324 register struct inpcb *inp = sotoinpcb(so);
325 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
c910b4d9
A
326 struct ip_out_args ipoa;
327
328 /* If socket was bound to an ifindex, tell ip_output about it */
329 ipoa.ipoa_ifscope = (inp->inp_flags & INP_BOUND_IF) ?
330 inp->inp_boundif : IFSCOPE_NONE;
331 flags |= IP_OUTARGS;
1c79356b
A
332
333 /*
334 * If the user handed us a complete IP packet, use it.
335 * Otherwise, allocate an mbuf for a header and fill it in.
336 */
337 if ((inp->inp_flags & INP_HDRINCL) == 0) {
338 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
339 m_freem(m);
340 return(EMSGSIZE);
341 }
342 M_PREPEND(m, sizeof(struct ip), M_WAIT);
b0d623f7
A
343 if (m == NULL)
344 return ENOBUFS;
1c79356b 345 ip = mtod(m, struct ip *);
9bccf70c 346 ip->ip_tos = inp->inp_ip_tos;
1c79356b
A
347 ip->ip_off = 0;
348 ip->ip_p = inp->inp_ip_p;
349 ip->ip_len = m->m_pkthdr.len;
350 ip->ip_src = inp->inp_laddr;
351 ip->ip_dst.s_addr = dst;
9bccf70c 352 ip->ip_ttl = inp->inp_ip_ttl;
1c79356b
A
353 } else {
354 if (m->m_pkthdr.len > IP_MAXPACKET) {
355 m_freem(m);
356 return(EMSGSIZE);
357 }
358 ip = mtod(m, struct ip *);
359 /* don't allow both user specified and setsockopt options,
360 and don't allow packet length sizes that will crash */
9bccf70c 361 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
1c79356b
A
362 && inp->inp_options)
363 || (ip->ip_len > m->m_pkthdr.len)
364 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
365 m_freem(m);
366 return EINVAL;
367 }
368 if (ip->ip_id == 0)
9bccf70c
A
369#if RANDOM_IP_ID
370 ip->ip_id = ip_randomid();
371#else
1c79356b 372 ip->ip_id = htons(ip_id++);
9bccf70c 373#endif
1c79356b
A
374 /* XXX prevent ip_output from overwriting header fields */
375 flags |= IP_RAWOUTPUT;
b0d623f7 376 OSAddAtomic(1, &ipstat.ips_rawout);
1c79356b
A
377 }
378
379#if IPSEC
9bccf70c
A
380 if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
381 m_freem(m);
382 return ENOBUFS;
383 }
1c79356b
A
384#endif /*IPSEC*/
385
b0d623f7
A
386 if (inp->inp_route.ro_rt != NULL &&
387 inp->inp_route.ro_rt->generation_id != route_generation) {
91447636 388 rtfree(inp->inp_route.ro_rt);
b0d623f7 389 inp->inp_route.ro_rt = NULL;
91447636
A
390 }
391
2d21ac55
A
392#if CONFIG_MACF_NET
393 mac_mbuf_label_associate_inpcb(inp, m);
394#endif
395
b0d623f7
A
396 /*
397 * The domain lock is held across ip_output, so it is okay
398 * to pass the PCB cached route pointer directly to IP and
399 * the modules beneath it.
400 */
c910b4d9
A
401 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
402 inp->inp_moptions, &ipoa));
1c79356b
A
403}
404
2d21ac55
A
405#if IPFIREWALL
406int
407load_ipfw(void)
55e303ae
A
408{
409 kern_return_t err;
410
91447636 411 ipfw_init();
55e303ae 412
91447636
A
413#if DUMMYNET
414 if (!DUMMYNET_LOADED)
415 ip_dn_init();
416#endif /* DUMMYNET */
417 err = 0;
55e303ae
A
418
419 return err == 0 && ip_fw_ctl_ptr == NULL ? -1 : err;
420}
2d21ac55 421#endif /* IPFIREWALL */
55e303ae 422
1c79356b
A
423/*
424 * Raw IP socket option processing.
425 */
426int
427rip_ctloutput(so, sopt)
428 struct socket *so;
429 struct sockopt *sopt;
430{
431 struct inpcb *inp = sotoinpcb(so);
432 int error, optval;
433
434 if (sopt->sopt_level != IPPROTO_IP)
435 return (EINVAL);
436
437 error = 0;
438
439 switch (sopt->sopt_dir) {
440 case SOPT_GET:
441 switch (sopt->sopt_name) {
442 case IP_HDRINCL:
443 optval = inp->inp_flags & INP_HDRINCL;
444 error = sooptcopyout(sopt, &optval, sizeof optval);
445 break;
446
447 case IP_STRIPHDR:
448 optval = inp->inp_flags & INP_STRIPHDR;
449 error = sooptcopyout(sopt, &optval, sizeof optval);
450 break;
451
2d21ac55 452#if IPFIREWALL
9bccf70c 453 case IP_FW_ADD:
1c79356b 454 case IP_FW_GET:
9bccf70c
A
455 case IP_OLD_FW_ADD:
456 case IP_OLD_FW_GET:
1c79356b 457 if (ip_fw_ctl_ptr == 0)
55e303ae
A
458 error = load_ipfw();
459 if (ip_fw_ctl_ptr && error == 0)
1c79356b 460 error = ip_fw_ctl_ptr(sopt);
55e303ae
A
461 else
462 error = ENOPROTOOPT;
1c79356b 463 break;
b0d623f7 464#endif /* IPFIREWALL */
1c79356b 465
1c79356b
A
466#if DUMMYNET
467 case IP_DUMMYNET_GET:
91447636 468 if (DUMMYNET_LOADED)
1c79356b 469 error = ip_dn_ctl_ptr(sopt);
91447636
A
470 else
471 error = ENOPROTOOPT;
1c79356b
A
472 break ;
473#endif /* DUMMYNET */
1c79356b 474
2d21ac55 475#if MROUTING
1c79356b
A
476 case MRT_INIT:
477 case MRT_DONE:
478 case MRT_ADD_VIF:
479 case MRT_DEL_VIF:
480 case MRT_ADD_MFC:
481 case MRT_DEL_MFC:
482 case MRT_VERSION:
483 case MRT_ASSERT:
484 error = ip_mrouter_get(so, sopt);
485 break;
2d21ac55 486#endif /* MROUTING */
1c79356b
A
487
488 default:
489 error = ip_ctloutput(so, sopt);
490 break;
491 }
492 break;
493
494 case SOPT_SET:
495 switch (sopt->sopt_name) {
496 case IP_HDRINCL:
497 error = sooptcopyin(sopt, &optval, sizeof optval,
498 sizeof optval);
499 if (error)
500 break;
501 if (optval)
502 inp->inp_flags |= INP_HDRINCL;
503 else
504 inp->inp_flags &= ~INP_HDRINCL;
505 break;
506
507 case IP_STRIPHDR:
508 error = sooptcopyin(sopt, &optval, sizeof optval,
509 sizeof optval);
510 if (error)
511 break;
512 if (optval)
513 inp->inp_flags |= INP_STRIPHDR;
514 else
515 inp->inp_flags &= ~INP_STRIPHDR;
516 break;
517
518
2d21ac55 519#if IPFIREWALL
1c79356b
A
520 case IP_FW_ADD:
521 case IP_FW_DEL:
522 case IP_FW_FLUSH:
523 case IP_FW_ZERO:
9bccf70c
A
524 case IP_FW_RESETLOG:
525 case IP_OLD_FW_ADD:
526 case IP_OLD_FW_DEL:
527 case IP_OLD_FW_FLUSH:
528 case IP_OLD_FW_ZERO:
529 case IP_OLD_FW_RESETLOG:
1c79356b 530 if (ip_fw_ctl_ptr == 0)
55e303ae
A
531 error = load_ipfw();
532 if (ip_fw_ctl_ptr && error == 0)
1c79356b 533 error = ip_fw_ctl_ptr(sopt);
55e303ae
A
534 else
535 error = ENOPROTOOPT;
1c79356b 536 break;
2d21ac55 537#endif /* IPFIREWALL */
1c79356b 538
1c79356b
A
539#if DUMMYNET
540 case IP_DUMMYNET_CONFIGURE:
541 case IP_DUMMYNET_DEL:
542 case IP_DUMMYNET_FLUSH:
91447636 543 if (DUMMYNET_LOADED)
1c79356b 544 error = ip_dn_ctl_ptr(sopt);
91447636
A
545 else
546 error = ENOPROTOOPT ;
1c79356b
A
547 break ;
548#endif
1c79356b 549
2d21ac55 550#if MROUTING
1c79356b
A
551 case IP_RSVP_ON:
552 error = ip_rsvp_init(so);
553 break;
554
555 case IP_RSVP_OFF:
556 error = ip_rsvp_done();
557 break;
558
559 /* XXX - should be combined */
560 case IP_RSVP_VIF_ON:
561 error = ip_rsvp_vif_init(so, sopt);
562 break;
563
564 case IP_RSVP_VIF_OFF:
565 error = ip_rsvp_vif_done(so, sopt);
566 break;
2d21ac55 567
1c79356b
A
568 case MRT_INIT:
569 case MRT_DONE:
570 case MRT_ADD_VIF:
571 case MRT_DEL_VIF:
572 case MRT_ADD_MFC:
573 case MRT_DEL_MFC:
574 case MRT_VERSION:
575 case MRT_ASSERT:
576 error = ip_mrouter_set(so, sopt);
577 break;
2d21ac55 578#endif /* MROUTING */
1c79356b
A
579
580 default:
581 error = ip_ctloutput(so, sopt);
582 break;
583 }
584 break;
585 }
586
587 return (error);
588}
589
590/*
591 * This function exists solely to receive the PRC_IFDOWN messages which
592 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
593 * and calls in_ifadown() to remove all routes corresponding to that address.
594 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
595 * interface routes.
596 */
597void
2d21ac55
A
598rip_ctlinput(
599 int cmd,
600 struct sockaddr *sa,
601 __unused void *vip)
1c79356b
A
602{
603 struct in_ifaddr *ia;
604 struct ifnet *ifp;
605 int err;
b0d623f7 606 int flags, done = 0;
1c79356b
A
607
608 switch (cmd) {
609 case PRC_IFDOWN:
b0d623f7 610 lck_rw_lock_shared(in_ifaddr_rwlock);
1c79356b
A
611 for (ia = in_ifaddrhead.tqh_first; ia;
612 ia = ia->ia_link.tqe_next) {
613 if (ia->ia_ifa.ifa_addr == sa
614 && (ia->ia_flags & IFA_ROUTE)) {
b0d623f7
A
615 done = 1;
616 ifaref(&ia->ia_ifa);
617 lck_rw_done(in_ifaddr_rwlock);
618 lck_mtx_lock(rnh_lock);
1c79356b
A
619 /*
620 * in_ifscrub kills the interface route.
621 */
91447636 622 in_ifscrub(ia->ia_ifp, ia, 1);
1c79356b
A
623 /*
624 * in_ifadown gets rid of all the rest of
625 * the routes. This is not quite the right
626 * thing to do, but at least if we are running
627 * a routing process they will come back.
628 */
9bccf70c 629 in_ifadown(&ia->ia_ifa, 1);
b0d623f7
A
630 lck_mtx_unlock(rnh_lock);
631 ifafree(&ia->ia_ifa);
1c79356b
A
632 break;
633 }
634 }
b0d623f7
A
635 if (!done)
636 lck_rw_done(in_ifaddr_rwlock);
1c79356b
A
637 break;
638
639 case PRC_IFUP:
b0d623f7 640 lck_rw_lock_shared(in_ifaddr_rwlock);
1c79356b
A
641 for (ia = in_ifaddrhead.tqh_first; ia;
642 ia = ia->ia_link.tqe_next) {
643 if (ia->ia_ifa.ifa_addr == sa)
644 break;
645 }
91447636 646 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) {
b0d623f7 647 lck_rw_done(in_ifaddr_rwlock);
1c79356b 648 return;
91447636 649 }
b0d623f7
A
650 ifaref(&ia->ia_ifa);
651 lck_rw_done(in_ifaddr_rwlock);
652
1c79356b
A
653 flags = RTF_UP;
654 ifp = ia->ia_ifa.ifa_ifp;
655
656 if ((ifp->if_flags & IFF_LOOPBACK)
657 || (ifp->if_flags & IFF_POINTOPOINT))
658 flags |= RTF_HOST;
659
b0d623f7 660 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
1c79356b
A
661 if (err == 0)
662 ia->ia_flags |= IFA_ROUTE;
b0d623f7 663 ifafree(&ia->ia_ifa);
1c79356b
A
664 break;
665 }
666}
667
b0d623f7
A
668u_int32_t rip_sendspace = RIPSNDQ;
669u_int32_t rip_recvspace = RIPRCVQ;
1c79356b 670
9bccf70c
A
671SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
672 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
673SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
674 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
1c79356b
A
675
676static int
677rip_attach(struct socket *so, int proto, struct proc *p)
678{
679 struct inpcb *inp;
2d21ac55 680 int error;
1c79356b
A
681
682 inp = sotoinpcb(so);
683 if (inp)
684 panic("rip_attach");
9bccf70c
A
685 if ((so->so_state & SS_PRIV) == 0)
686 return (EPERM);
1c79356b 687
9bccf70c
A
688 error = soreserve(so, rip_sendspace, rip_recvspace);
689 if (error)
690 return error;
1c79356b 691 error = in_pcballoc(so, &ripcbinfo, p);
1c79356b
A
692 if (error)
693 return error;
694 inp = (struct inpcb *)so->so_pcb;
695 inp->inp_vflag |= INP_IPV4;
696 inp->inp_ip_p = proto;
9bccf70c 697 inp->inp_ip_ttl = ip_defttl;
1c79356b
A
698 return 0;
699}
700
9bccf70c 701__private_extern__ int
1c79356b
A
702rip_detach(struct socket *so)
703{
704 struct inpcb *inp;
705
706 inp = sotoinpcb(so);
707 if (inp == 0)
708 panic("rip_detach");
2d21ac55 709#if MROUTING
1c79356b
A
710 if (so == ip_mrouter)
711 ip_mrouter_done();
712 ip_rsvp_force_done(so);
713 if (so == ip_rsvpd)
714 ip_rsvp_done();
2d21ac55 715#endif /* MROUTING */
1c79356b
A
716 in_pcbdetach(inp);
717 return 0;
718}
719
9bccf70c 720__private_extern__ int
1c79356b
A
721rip_abort(struct socket *so)
722{
723 soisdisconnected(so);
724 return rip_detach(so);
725}
726
9bccf70c 727__private_extern__ int
1c79356b
A
728rip_disconnect(struct socket *so)
729{
730 if ((so->so_state & SS_ISCONNECTED) == 0)
731 return ENOTCONN;
732 return rip_abort(so);
733}
734
9bccf70c 735__private_extern__ int
2d21ac55 736rip_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1c79356b
A
737{
738 struct inpcb *inp = sotoinpcb(so);
739 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
91447636 740 struct ifaddr *ifa = NULL;
1c79356b
A
741
742 if (nam->sa_len != sizeof(*addr))
743 return EINVAL;
744
91447636 745 if (TAILQ_EMPTY(&ifnet_head) || ((addr->sin_family != AF_INET) &&
1c79356b
A
746 (addr->sin_family != AF_IMPLINK)) ||
747 (addr->sin_addr.s_addr &&
91447636 748 (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)) {
1c79356b 749 return EADDRNOTAVAIL;
91447636
A
750 }
751 else if (ifa) {
752 ifafree(ifa);
753 ifa = NULL;
754 }
1c79356b
A
755 inp->inp_laddr = addr->sin_addr;
756 return 0;
757}
758
9bccf70c 759__private_extern__ int
2d21ac55 760rip_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1c79356b
A
761{
762 struct inpcb *inp = sotoinpcb(so);
763 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
764
765 if (nam->sa_len != sizeof(*addr))
766 return EINVAL;
91447636 767 if (TAILQ_EMPTY(&ifnet_head))
1c79356b
A
768 return EADDRNOTAVAIL;
769 if ((addr->sin_family != AF_INET) &&
770 (addr->sin_family != AF_IMPLINK))
771 return EAFNOSUPPORT;
772 inp->inp_faddr = addr->sin_addr;
773 soisconnected(so);
774 return 0;
775}
776
9bccf70c 777__private_extern__ int
1c79356b
A
778rip_shutdown(struct socket *so)
779{
780 socantsendmore(so);
781 return 0;
782}
783
9bccf70c 784__private_extern__ int
2d21ac55
A
785rip_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam,
786 __unused struct mbuf *control, __unused struct proc *p)
1c79356b
A
787{
788 struct inpcb *inp = sotoinpcb(so);
b0d623f7 789 register u_int32_t dst;
1c79356b
A
790
791 if (so->so_state & SS_ISCONNECTED) {
792 if (nam) {
793 m_freem(m);
794 return EISCONN;
795 }
796 dst = inp->inp_faddr.s_addr;
797 } else {
798 if (nam == NULL) {
799 m_freem(m);
800 return ENOTCONN;
801 }
802 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
803 }
804 return rip_output(m, so, dst);
805}
806
0c530ab8
A
807/* note: rip_unlock is called from different protos instead of the generic socket_unlock,
808 * it will handle the socket dealloc on last reference
809 * */
91447636 810int
b0d623f7 811rip_unlock(struct socket *so, int refcount, void *debug)
91447636 812{
b0d623f7 813 void *lr_saved;
91447636 814 struct inpcb *inp = sotoinpcb(so);
0c530ab8 815
b0d623f7
A
816 if (debug == NULL)
817 lr_saved = __builtin_return_address(0);
818 else
819 lr_saved = debug;
0c530ab8 820
91447636 821 if (refcount) {
b0d623f7
A
822 if (so->so_usecount <= 0) {
823 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s\n",
824 so, so->so_usecount, solockhistory_nr(so));
825 /* NOTREACHED */
826 }
91447636
A
827 so->so_usecount--;
828 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
0c530ab8 829 /* cleanup after last reference */
91447636
A
830 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
831 lck_rw_lock_exclusive(ripcbinfo.mtx);
b0d623f7
A
832 if (inp->inp_state != INPCB_STATE_DEAD) {
833#if INET6
834 if (INP_CHECK_SOCKAF(so, AF_INET6))
835 in6_pcbdetach(inp);
836 else
837#endif /* INET6 */
838 in_pcbdetach(inp);
839 }
91447636
A
840 in_pcbdispose(inp);
841 lck_rw_done(ripcbinfo.mtx);
842 return(0);
843 }
844 }
b0d623f7 845 so->unlock_lr[so->next_unlock_lr] = lr_saved;
0c530ab8 846 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
91447636
A
847 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
848 return(0);
849}
850
1c79356b
A
851static int
852rip_pcblist SYSCTL_HANDLER_ARGS
853{
2d21ac55
A
854#pragma unused(oidp, arg1, arg2)
855 int error, i, n;
1c79356b
A
856 struct inpcb *inp, **inp_list;
857 inp_gen_t gencnt;
858 struct xinpgen xig;
859
860 /*
861 * The process of preparing the TCB list is too time-consuming and
862 * resource-intensive to repeat twice on every request.
863 */
91447636
A
864 lck_rw_lock_exclusive(ripcbinfo.mtx);
865 if (req->oldptr == USER_ADDR_NULL) {
1c79356b
A
866 n = ripcbinfo.ipi_count;
867 req->oldidx = 2 * (sizeof xig)
868 + (n + n/8) * sizeof(struct xinpcb);
91447636 869 lck_rw_done(ripcbinfo.mtx);
1c79356b
A
870 return 0;
871 }
872
91447636
A
873 if (req->newptr != USER_ADDR_NULL) {
874 lck_rw_done(ripcbinfo.mtx);
1c79356b 875 return EPERM;
91447636 876 }
1c79356b
A
877
878 /*
879 * OK, now we're committed to doing something.
880 */
1c79356b
A
881 gencnt = ripcbinfo.ipi_gencnt;
882 n = ripcbinfo.ipi_count;
3a60a9f5
A
883
884 bzero(&xig, sizeof(xig));
1c79356b
A
885 xig.xig_len = sizeof xig;
886 xig.xig_count = n;
887 xig.xig_gen = gencnt;
888 xig.xig_sogen = so_gencnt;
889 error = SYSCTL_OUT(req, &xig, sizeof xig);
91447636
A
890 if (error) {
891 lck_rw_done(ripcbinfo.mtx);
1c79356b 892 return error;
91447636 893 }
9bccf70c
A
894 /*
895 * We are done if there is no pcb
896 */
91447636
A
897 if (n == 0) {
898 lck_rw_done(ripcbinfo.mtx);
9bccf70c 899 return 0;
91447636 900 }
1c79356b
A
901
902 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
91447636
A
903 if (inp_list == 0) {
904 lck_rw_done(ripcbinfo.mtx);
1c79356b 905 return ENOMEM;
91447636 906 }
1c79356b 907
1c79356b
A
908 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
909 inp = inp->inp_list.le_next) {
91447636 910 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD)
1c79356b
A
911 inp_list[i++] = inp;
912 }
1c79356b
A
913 n = i;
914
915 error = 0;
916 for (i = 0; i < n; i++) {
917 inp = inp_list[i];
91447636 918 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1c79356b 919 struct xinpcb xi;
3a60a9f5
A
920
921 bzero(&xi, sizeof(xi));
1c79356b
A
922 xi.xi_len = sizeof xi;
923 /* XXX should avoid extra copy */
91447636 924 inpcb_to_compat(inp, &xi.xi_inp);
1c79356b
A
925 if (inp->inp_socket)
926 sotoxsocket(inp->inp_socket, &xi.xi_socket);
927 error = SYSCTL_OUT(req, &xi, sizeof xi);
928 }
929 }
930 if (!error) {
931 /*
932 * Give the user an updated idea of our state.
933 * If the generation differs from what we told
934 * her before, she knows that something happened
935 * while we were processing this request, and it
936 * might be necessary to retry.
937 */
3a60a9f5
A
938 bzero(&xig, sizeof(xig));
939 xig.xig_len = sizeof xig;
1c79356b
A
940 xig.xig_gen = ripcbinfo.ipi_gencnt;
941 xig.xig_sogen = so_gencnt;
942 xig.xig_count = ripcbinfo.ipi_count;
1c79356b
A
943 error = SYSCTL_OUT(req, &xig, sizeof xig);
944 }
945 FREE(inp_list, M_TEMP);
91447636 946 lck_rw_done(ripcbinfo.mtx);
1c79356b
A
947 return error;
948}
949
1c79356b
A
950SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
951 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
952
b0d623f7
A
953#if !CONFIG_EMBEDDED
954
955static int
956rip_pcblist64 SYSCTL_HANDLER_ARGS
957{
958#pragma unused(oidp, arg1, arg2)
959 int error, i, n;
960 struct inpcb *inp, **inp_list;
961 inp_gen_t gencnt;
962 struct xinpgen xig;
963
964 /*
965 * The process of preparing the TCB list is too time-consuming and
966 * resource-intensive to repeat twice on every request.
967 */
968 lck_rw_lock_exclusive(ripcbinfo.mtx);
969 if (req->oldptr == USER_ADDR_NULL) {
970 n = ripcbinfo.ipi_count;
971 req->oldidx = 2 * (sizeof xig)
972 + (n + n/8) * sizeof(struct xinpcb64);
973 lck_rw_done(ripcbinfo.mtx);
974 return 0;
975 }
976
977 if (req->newptr != USER_ADDR_NULL) {
978 lck_rw_done(ripcbinfo.mtx);
979 return EPERM;
980 }
981
982 /*
983 * OK, now we're committed to doing something.
984 */
985 gencnt = ripcbinfo.ipi_gencnt;
986 n = ripcbinfo.ipi_count;
987
988 bzero(&xig, sizeof(xig));
989 xig.xig_len = sizeof xig;
990 xig.xig_count = n;
991 xig.xig_gen = gencnt;
992 xig.xig_sogen = so_gencnt;
993 error = SYSCTL_OUT(req, &xig, sizeof xig);
994 if (error) {
995 lck_rw_done(ripcbinfo.mtx);
996 return error;
997 }
998 /*
999 * We are done if there is no pcb
1000 */
1001 if (n == 0) {
1002 lck_rw_done(ripcbinfo.mtx);
1003 return 0;
1004 }
1005
1006 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1007 if (inp_list == 0) {
1008 lck_rw_done(ripcbinfo.mtx);
1009 return ENOMEM;
1010 }
1011
1012 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
1013 inp = inp->inp_list.le_next) {
1014 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD)
1015 inp_list[i++] = inp;
1016 }
1017 n = i;
1018
1019 error = 0;
1020 for (i = 0; i < n; i++) {
1021 inp = inp_list[i];
1022 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1023 struct xinpcb64 xi;
1024
1025 bzero(&xi, sizeof(xi));
1026 xi.xi_len = sizeof xi;
1027 inpcb_to_xinpcb64(inp, &xi);
1028 if (inp->inp_socket)
1029 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1030 error = SYSCTL_OUT(req, &xi, sizeof xi);
1031 }
1032 }
1033 if (!error) {
1034 /*
1035 * Give the user an updated idea of our state.
1036 * If the generation differs from what we told
1037 * her before, she knows that something happened
1038 * while we were processing this request, and it
1039 * might be necessary to retry.
1040 */
1041 bzero(&xig, sizeof(xig));
1042 xig.xig_len = sizeof xig;
1043 xig.xig_gen = ripcbinfo.ipi_gencnt;
1044 xig.xig_sogen = so_gencnt;
1045 xig.xig_count = ripcbinfo.ipi_count;
1046 error = SYSCTL_OUT(req, &xig, sizeof xig);
1047 }
1048 FREE(inp_list, M_TEMP);
1049 lck_rw_done(ripcbinfo.mtx);
1050 return error;
1051}
1052
1053SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist64, CTLFLAG_RD, 0, 0,
1054 rip_pcblist64, "S,xinpcb64", "List of active raw IP sockets");
1055
1056#endif /* !CONFIG_EMBEDDED */
1057
1c79356b
A
1058struct pr_usrreqs rip_usrreqs = {
1059 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
1060 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
1061 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
9bccf70c 1062 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
91447636 1063 in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
1c79356b 1064};
2d21ac55 1065/* DSEP Review Done pl-20051213-v02 @3253 */