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