2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/sysctl.h>
69 #include <vm/vm_zone.h>
73 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/ip6_var.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/in_var.h>
83 #include <netinet/ip_var.h>
84 #include <netinet/ip_mroute.h>
86 #include <netinet/ip_fw.h>
89 #include <netinet6/ipsec.h>
97 #include <netinet/ip_dummynet.h>
99 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
101 #define COMPAT_IPFW 1
106 struct inpcbhead ripcb
;
107 struct inpcbinfo ripcbinfo
;
110 * Nominal space allocated to a raw ip socket.
116 * Raw interface to IP protocol.
120 * Initialize raw connection block q.
126 ripcbinfo
.listhead
= &ripcb
;
128 * XXX We don't use the hash list for raw IP, but it's easier
129 * to allocate a one entry hash list than it is to check all
130 * over the place for hashbase == NULL.
132 ripcbinfo
.hashbase
= hashinit(1, M_PCB
, &ripcbinfo
.hashmask
);
133 ripcbinfo
.porthashbase
= hashinit(1, M_PCB
, &ripcbinfo
.porthashmask
);
135 ripcbinfo
.ipi_zone
= (void *) zinit(sizeof(struct inpcb
),
136 (4096 * sizeof(struct inpcb
)),
141 static struct sockaddr_in ripsrc
= { sizeof(ripsrc
), AF_INET
};
143 * Setup generic address and protocol structures
144 * for raw_input routine, then pass them along with
152 register struct ip
*ip
= mtod(m
, struct ip
*);
153 register struct inpcb
*inp
;
154 struct inpcb
*last
= 0;
155 struct mbuf
*opts
= 0;
156 short need_wakeup
= 0;
158 ripsrc
.sin_addr
= ip
->ip_src
;
159 LIST_FOREACH(inp
, &ripcb
, inp_list
) {
160 if ((inp
->inp_vflag
& INP_IPV4
) == NULL
)
162 #warning do something about this
163 if (inp
->inp_ip_p
&& inp
->inp_ip_p
!= ip
->ip_p
)
165 if (inp
->inp_laddr
.s_addr
&&
166 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
168 if (inp
->inp_faddr
.s_addr
&&
169 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
172 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
174 if (last
->inp_flags
& INP_CONTROLOPTS
||
175 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
176 ip_savecontrol(last
, &opts
, ip
, n
);
177 if (last
->inp_flags
& INP_STRIPHDR
) {
179 n
->m_pkthdr
.len
-= iphlen
;
182 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
183 (struct sockaddr
*)&ripsrc
, n
,
185 /* should notify about lost packet */
186 kprintf("rip_input can't append to socket\n");
191 /* kprintf("rip_input calling sorwakeup\n"); */
200 if (last
->inp_flags
& INP_CONTROLOPTS
||
201 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
202 ip_savecontrol(last
, &opts
, ip
, m
);
203 if (last
->inp_flags
& INP_STRIPHDR
) {
205 m
->m_pkthdr
.len
-= iphlen
;
208 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
209 (struct sockaddr
*)&ripsrc
, m
, opts
) == 0) {
210 kprintf("rip_input(2) can't append to socket\n");
215 /* kprintf("rip_input calling sorwakeup\n"); */
220 ipstat
.ips_noproto
++;
221 ipstat
.ips_delivered
--;
224 sorwakeup(last
->inp_socket
);
228 * Generate IP header and pass packet to ip_output.
229 * Tack on options user may have setup with control call.
232 rip_output(m
, so
, dst
)
233 register struct mbuf
*m
;
237 register struct ip
*ip
;
238 register struct inpcb
*inp
= sotoinpcb(so
);
239 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
242 * If the user handed us a complete IP packet, use it.
243 * Otherwise, allocate an mbuf for a header and fill it in.
245 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
246 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
250 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
);
251 ip
= mtod(m
, struct ip
*);
254 ip
->ip_p
= inp
->inp_ip_p
;
255 ip
->ip_len
= m
->m_pkthdr
.len
;
256 ip
->ip_src
= inp
->inp_laddr
;
257 ip
->ip_dst
.s_addr
= dst
;
260 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
264 ip
= mtod(m
, struct ip
*);
265 /* don't allow both user specified and setsockopt options,
266 and don't allow packet length sizes that will crash */
267 if (((IP_VHL_HL(ip
->ip_vhl
) != (sizeof (*ip
) >> 2))
269 || (ip
->ip_len
> m
->m_pkthdr
.len
)
270 || (ip
->ip_len
< (IP_VHL_HL(ip
->ip_vhl
) << 2))) {
275 ip
->ip_id
= htons(ip_id
++);
276 /* XXX prevent ip_output from overwriting header fields */
277 flags
|= IP_RAWOUTPUT
;
282 m
->m_pkthdr
.rcvif
= (struct ifnet
*)so
; /*XXX*/
285 return (ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
290 * Raw IP socket option processing.
293 rip_ctloutput(so
, sopt
)
295 struct sockopt
*sopt
;
297 struct inpcb
*inp
= sotoinpcb(so
);
300 if (sopt
->sopt_level
!= IPPROTO_IP
)
305 switch (sopt
->sopt_dir
) {
307 switch (sopt
->sopt_name
) {
309 optval
= inp
->inp_flags
& INP_HDRINCL
;
310 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
314 optval
= inp
->inp_flags
& INP_STRIPHDR
;
315 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
320 if (ip_fw_ctl_ptr
== 0)
323 error
= ip_fw_ctl_ptr(sopt
);
327 if (ip_nat_ctl_ptr
== 0)
330 error
= ip_nat_ctl_ptr(sopt
);
333 case IP_DUMMYNET_GET
:
334 if (ip_dn_ctl_ptr
== NULL
)
335 error
= ENOPROTOOPT
;
337 error
= ip_dn_ctl_ptr(sopt
);
339 #endif /* DUMMYNET */
340 #endif /* COMPAT_IPFW */
350 error
= ip_mrouter_get(so
, sopt
);
354 error
= ip_ctloutput(so
, sopt
);
360 switch (sopt
->sopt_name
) {
362 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
367 inp
->inp_flags
|= INP_HDRINCL
;
369 inp
->inp_flags
&= ~INP_HDRINCL
;
373 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
378 inp
->inp_flags
|= INP_STRIPHDR
;
380 inp
->inp_flags
&= ~INP_STRIPHDR
;
389 if (ip_fw_ctl_ptr
== 0)
392 error
= ip_fw_ctl_ptr(sopt
);
396 if (ip_nat_ctl_ptr
== 0)
399 error
= ip_nat_ctl_ptr(sopt
);
402 case IP_DUMMYNET_CONFIGURE
:
403 case IP_DUMMYNET_DEL
:
404 case IP_DUMMYNET_FLUSH
:
405 if (ip_dn_ctl_ptr
== NULL
)
406 error
= ENOPROTOOPT
;
408 error
= ip_dn_ctl_ptr(sopt
);
411 #endif /* COMPAT_IPFW */
414 error
= ip_rsvp_init(so
);
418 error
= ip_rsvp_done();
421 /* XXX - should be combined */
423 error
= ip_rsvp_vif_init(so
, sopt
);
426 case IP_RSVP_VIF_OFF
:
427 error
= ip_rsvp_vif_done(so
, sopt
);
438 error
= ip_mrouter_set(so
, sopt
);
442 error
= ip_ctloutput(so
, sopt
);
452 * This function exists solely to receive the PRC_IFDOWN messages which
453 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
454 * and calls in_ifadown() to remove all routes corresponding to that address.
455 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
459 rip_ctlinput(cmd
, sa
, vip
)
464 struct in_ifaddr
*ia
;
471 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
472 ia
= ia
->ia_link
.tqe_next
) {
473 if (ia
->ia_ifa
.ifa_addr
== sa
474 && (ia
->ia_flags
& IFA_ROUTE
)) {
476 * in_ifscrub kills the interface route.
478 in_ifscrub(ia
->ia_ifp
, ia
);
480 * in_ifadown gets rid of all the rest of
481 * the routes. This is not quite the right
482 * thing to do, but at least if we are running
483 * a routing process they will come back.
485 in_ifadown(&ia
->ia_ifa
);
492 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
493 ia
= ia
->ia_link
.tqe_next
) {
494 if (ia
->ia_ifa
.ifa_addr
== sa
)
497 if (ia
== 0 || (ia
->ia_flags
& IFA_ROUTE
))
500 ifp
= ia
->ia_ifa
.ifa_ifp
;
502 if ((ifp
->if_flags
& IFF_LOOPBACK
)
503 || (ifp
->if_flags
& IFF_POINTOPOINT
))
506 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
508 ia
->ia_flags
|= IFA_ROUTE
;
513 u_long rip_sendspace
= RIPSNDQ
;
514 u_long rip_recvspace
= RIPRCVQ
;
516 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
, &rip_sendspace
,
518 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
, &rip_recvspace
,
522 rip_attach(struct socket
*so
, int proto
, struct proc
*p
)
533 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
536 if ((so
->so_state
& SS_PRIV
) == 0)
541 error
= in_pcballoc(so
, &ripcbinfo
, p
);
545 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
548 inp
= (struct inpcb
*)so
->so_pcb
;
549 inp
->inp_vflag
|= INP_IPV4
;
550 inp
->inp_ip_p
= proto
;
552 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
563 rip_detach(struct socket
*so
)
570 if (so
== ip_mrouter
)
572 ip_rsvp_force_done(so
);
580 rip_abort(struct socket
*so
)
582 soisdisconnected(so
);
583 return rip_detach(so
);
587 rip_disconnect(struct socket
*so
)
589 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
591 return rip_abort(so
);
595 rip_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
597 struct inpcb
*inp
= sotoinpcb(so
);
598 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
600 if (nam
->sa_len
!= sizeof(*addr
))
603 if (TAILQ_EMPTY(&ifnet
) || ((addr
->sin_family
!= AF_INET
) &&
604 (addr
->sin_family
!= AF_IMPLINK
)) ||
605 (addr
->sin_addr
.s_addr
&&
606 ifa_ifwithaddr((struct sockaddr
*)addr
) == 0))
607 return EADDRNOTAVAIL
;
608 inp
->inp_laddr
= addr
->sin_addr
;
613 rip_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
615 struct inpcb
*inp
= sotoinpcb(so
);
616 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
618 if (nam
->sa_len
!= sizeof(*addr
))
620 if (TAILQ_EMPTY(&ifnet
))
621 return EADDRNOTAVAIL
;
622 if ((addr
->sin_family
!= AF_INET
) &&
623 (addr
->sin_family
!= AF_IMPLINK
))
625 inp
->inp_faddr
= addr
->sin_addr
;
631 rip_shutdown(struct socket
*so
)
638 rip_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
639 struct mbuf
*control
, struct proc
*p
)
641 struct inpcb
*inp
= sotoinpcb(so
);
644 if (so
->so_state
& SS_ISCONNECTED
) {
649 dst
= inp
->inp_faddr
.s_addr
;
655 dst
= ((struct sockaddr_in
*)nam
)->sin_addr
.s_addr
;
657 return rip_output(m
, so
, dst
);
662 rip_pcblist SYSCTL_HANDLER_ARGS
665 struct inpcb
*inp
, **inp_list
;
670 * The process of preparing the TCB list is too time-consuming and
671 * resource-intensive to repeat twice on every request.
673 if (req
->oldptr
== 0) {
674 n
= ripcbinfo
.ipi_count
;
675 req
->oldidx
= 2 * (sizeof xig
)
676 + (n
+ n
/8) * sizeof(struct xinpcb
);
680 if (req
->newptr
!= 0)
684 * OK, now we're committed to doing something.
687 gencnt
= ripcbinfo
.ipi_gencnt
;
688 n
= ripcbinfo
.ipi_count
;
691 xig
.xig_len
= sizeof xig
;
693 xig
.xig_gen
= gencnt
;
694 xig
.xig_sogen
= so_gencnt
;
695 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
699 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
704 for (inp
= ripcbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
705 inp
= inp
->inp_list
.le_next
) {
706 if (inp
->inp_gencnt
<= gencnt
)
713 for (i
= 0; i
< n
; i
++) {
715 if (inp
->inp_gencnt
<= gencnt
) {
717 xi
.xi_len
= sizeof xi
;
718 /* XXX should avoid extra copy */
719 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
721 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
722 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
727 * Give the user an updated idea of our state.
728 * If the generation differs from what we told
729 * her before, she knows that something happened
730 * while we were processing this request, and it
731 * might be necessary to retry.
734 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
735 xig
.xig_sogen
= so_gencnt
;
736 xig
.xig_count
= ripcbinfo
.ipi_count
;
738 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
740 FREE(inp_list
, M_TEMP
);
745 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
, CTLFLAG_RD
, 0, 0,
746 rip_pcblist
, "S,xinpcb", "List of active raw IP sockets");
748 struct pr_usrreqs rip_usrreqs
= {
749 rip_abort
, pru_accept_notsupp
, rip_attach
, rip_bind
, rip_connect
,
750 pru_connect2_notsupp
, in_control
, rip_detach
, rip_disconnect
,
751 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
752 pru_rcvoob_notsupp
, rip_send
, pru_sense_null
, rip_shutdown
,
753 in_setsockaddr
, sosend
, soreceive
, sopoll