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;
157 ripsrc
.sin_addr
= ip
->ip_src
;
158 LIST_FOREACH(inp
, &ripcb
, inp_list
) {
159 if ((inp
->inp_vflag
& INP_IPV4
) == NULL
)
161 #warning do something about this
162 if (inp
->inp_ip_p
&& inp
->inp_ip_p
!= ip
->ip_p
)
164 if (inp
->inp_laddr
.s_addr
&&
165 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
167 if (inp
->inp_faddr
.s_addr
&&
168 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
171 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
173 if (last
->inp_flags
& INP_CONTROLOPTS
||
174 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
175 ip_savecontrol(last
, &opts
, ip
, n
);
176 if (last
->inp_flags
& INP_STRIPHDR
) {
178 n
->m_pkthdr
.len
-= iphlen
;
181 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
182 (struct sockaddr
*)&ripsrc
, n
,
184 /* should notify about lost packet */
185 kprintf("rip_input can't append to socket\n");
190 /* kprintf("rip_input calling sorwakeup\n"); */
191 sorwakeup(last
->inp_socket
);
199 if (last
->inp_flags
& INP_CONTROLOPTS
||
200 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
201 ip_savecontrol(last
, &opts
, ip
, m
);
202 if (last
->inp_flags
& INP_STRIPHDR
) {
204 m
->m_pkthdr
.len
-= iphlen
;
207 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
208 (struct sockaddr
*)&ripsrc
, m
, opts
) == 0) {
209 kprintf("rip_input(2) can't append to socket\n");
214 /* kprintf("rip_input calling sorwakeup\n"); */
215 sorwakeup(last
->inp_socket
);
219 ipstat
.ips_noproto
++;
220 ipstat
.ips_delivered
--;
225 * Generate IP header and pass packet to ip_output.
226 * Tack on options user may have setup with control call.
229 rip_output(m
, so
, dst
)
230 register struct mbuf
*m
;
234 register struct ip
*ip
;
235 register struct inpcb
*inp
= sotoinpcb(so
);
236 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
239 * If the user handed us a complete IP packet, use it.
240 * Otherwise, allocate an mbuf for a header and fill it in.
242 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
243 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
247 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
);
248 ip
= mtod(m
, struct ip
*);
251 ip
->ip_p
= inp
->inp_ip_p
;
252 ip
->ip_len
= m
->m_pkthdr
.len
;
253 ip
->ip_src
= inp
->inp_laddr
;
254 ip
->ip_dst
.s_addr
= dst
;
257 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
261 ip
= mtod(m
, struct ip
*);
262 /* don't allow both user specified and setsockopt options,
263 and don't allow packet length sizes that will crash */
264 if (((IP_VHL_HL(ip
->ip_vhl
) != (sizeof (*ip
) >> 2))
266 || (ip
->ip_len
> m
->m_pkthdr
.len
)
267 || (ip
->ip_len
< (IP_VHL_HL(ip
->ip_vhl
) << 2))) {
272 ip
->ip_id
= htons(ip_id
++);
273 /* XXX prevent ip_output from overwriting header fields */
274 flags
|= IP_RAWOUTPUT
;
279 m
->m_pkthdr
.rcvif
= (struct ifnet
*)so
; /*XXX*/
282 return (ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
287 * Raw IP socket option processing.
290 rip_ctloutput(so
, sopt
)
292 struct sockopt
*sopt
;
294 struct inpcb
*inp
= sotoinpcb(so
);
297 if (sopt
->sopt_level
!= IPPROTO_IP
)
302 switch (sopt
->sopt_dir
) {
304 switch (sopt
->sopt_name
) {
306 optval
= inp
->inp_flags
& INP_HDRINCL
;
307 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
311 optval
= inp
->inp_flags
& INP_STRIPHDR
;
312 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
317 if (ip_fw_ctl_ptr
== 0)
320 error
= ip_fw_ctl_ptr(sopt
);
324 if (ip_nat_ctl_ptr
== 0)
327 error
= ip_nat_ctl_ptr(sopt
);
330 case IP_DUMMYNET_GET
:
331 if (ip_dn_ctl_ptr
== NULL
)
332 error
= ENOPROTOOPT
;
334 error
= ip_dn_ctl_ptr(sopt
);
336 #endif /* DUMMYNET */
337 #endif /* COMPAT_IPFW */
347 error
= ip_mrouter_get(so
, sopt
);
351 error
= ip_ctloutput(so
, sopt
);
357 switch (sopt
->sopt_name
) {
359 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
364 inp
->inp_flags
|= INP_HDRINCL
;
366 inp
->inp_flags
&= ~INP_HDRINCL
;
370 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
375 inp
->inp_flags
|= INP_STRIPHDR
;
377 inp
->inp_flags
&= ~INP_STRIPHDR
;
386 if (ip_fw_ctl_ptr
== 0)
389 error
= ip_fw_ctl_ptr(sopt
);
393 if (ip_nat_ctl_ptr
== 0)
396 error
= ip_nat_ctl_ptr(sopt
);
399 case IP_DUMMYNET_CONFIGURE
:
400 case IP_DUMMYNET_DEL
:
401 case IP_DUMMYNET_FLUSH
:
402 if (ip_dn_ctl_ptr
== NULL
)
403 error
= ENOPROTOOPT
;
405 error
= ip_dn_ctl_ptr(sopt
);
408 #endif /* COMPAT_IPFW */
411 error
= ip_rsvp_init(so
);
415 error
= ip_rsvp_done();
418 /* XXX - should be combined */
420 error
= ip_rsvp_vif_init(so
, sopt
);
423 case IP_RSVP_VIF_OFF
:
424 error
= ip_rsvp_vif_done(so
, sopt
);
435 error
= ip_mrouter_set(so
, sopt
);
439 error
= ip_ctloutput(so
, sopt
);
449 * This function exists solely to receive the PRC_IFDOWN messages which
450 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
451 * and calls in_ifadown() to remove all routes corresponding to that address.
452 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
456 rip_ctlinput(cmd
, sa
, vip
)
461 struct in_ifaddr
*ia
;
468 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
469 ia
= ia
->ia_link
.tqe_next
) {
470 if (ia
->ia_ifa
.ifa_addr
== sa
471 && (ia
->ia_flags
& IFA_ROUTE
)) {
473 * in_ifscrub kills the interface route.
475 in_ifscrub(ia
->ia_ifp
, ia
);
477 * in_ifadown gets rid of all the rest of
478 * the routes. This is not quite the right
479 * thing to do, but at least if we are running
480 * a routing process they will come back.
482 in_ifadown(&ia
->ia_ifa
);
489 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
490 ia
= ia
->ia_link
.tqe_next
) {
491 if (ia
->ia_ifa
.ifa_addr
== sa
)
494 if (ia
== 0 || (ia
->ia_flags
& IFA_ROUTE
))
497 ifp
= ia
->ia_ifa
.ifa_ifp
;
499 if ((ifp
->if_flags
& IFF_LOOPBACK
)
500 || (ifp
->if_flags
& IFF_POINTOPOINT
))
503 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
505 ia
->ia_flags
|= IFA_ROUTE
;
510 u_long rip_sendspace
= RIPSNDQ
;
511 u_long rip_recvspace
= RIPRCVQ
;
513 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
, &rip_sendspace
,
515 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
, &rip_recvspace
,
519 rip_attach(struct socket
*so
, int proto
, struct proc
*p
)
530 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
533 if ((so
->so_state
& SS_PRIV
) == 0)
538 error
= in_pcballoc(so
, &ripcbinfo
, p
);
542 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
545 inp
= (struct inpcb
*)so
->so_pcb
;
546 inp
->inp_vflag
|= INP_IPV4
;
547 inp
->inp_ip_p
= proto
;
549 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
560 rip_detach(struct socket
*so
)
567 if (so
== ip_mrouter
)
569 ip_rsvp_force_done(so
);
577 rip_abort(struct socket
*so
)
579 soisdisconnected(so
);
580 return rip_detach(so
);
584 rip_disconnect(struct socket
*so
)
586 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
588 return rip_abort(so
);
592 rip_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
594 struct inpcb
*inp
= sotoinpcb(so
);
595 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
597 if (nam
->sa_len
!= sizeof(*addr
))
600 if (TAILQ_EMPTY(&ifnet
) || ((addr
->sin_family
!= AF_INET
) &&
601 (addr
->sin_family
!= AF_IMPLINK
)) ||
602 (addr
->sin_addr
.s_addr
&&
603 ifa_ifwithaddr((struct sockaddr
*)addr
) == 0))
604 return EADDRNOTAVAIL
;
605 inp
->inp_laddr
= addr
->sin_addr
;
610 rip_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
612 struct inpcb
*inp
= sotoinpcb(so
);
613 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
615 if (nam
->sa_len
!= sizeof(*addr
))
617 if (TAILQ_EMPTY(&ifnet
))
618 return EADDRNOTAVAIL
;
619 if ((addr
->sin_family
!= AF_INET
) &&
620 (addr
->sin_family
!= AF_IMPLINK
))
622 inp
->inp_faddr
= addr
->sin_addr
;
628 rip_shutdown(struct socket
*so
)
635 rip_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
636 struct mbuf
*control
, struct proc
*p
)
638 struct inpcb
*inp
= sotoinpcb(so
);
641 if (so
->so_state
& SS_ISCONNECTED
) {
646 dst
= inp
->inp_faddr
.s_addr
;
652 dst
= ((struct sockaddr_in
*)nam
)->sin_addr
.s_addr
;
654 return rip_output(m
, so
, dst
);
659 rip_pcblist SYSCTL_HANDLER_ARGS
662 struct inpcb
*inp
, **inp_list
;
667 * The process of preparing the TCB list is too time-consuming and
668 * resource-intensive to repeat twice on every request.
670 if (req
->oldptr
== 0) {
671 n
= ripcbinfo
.ipi_count
;
672 req
->oldidx
= 2 * (sizeof xig
)
673 + (n
+ n
/8) * sizeof(struct xinpcb
);
677 if (req
->newptr
!= 0)
681 * OK, now we're committed to doing something.
684 gencnt
= ripcbinfo
.ipi_gencnt
;
685 n
= ripcbinfo
.ipi_count
;
688 xig
.xig_len
= sizeof xig
;
690 xig
.xig_gen
= gencnt
;
691 xig
.xig_sogen
= so_gencnt
;
692 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
696 * We are done if there is no pcb
701 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
706 for (inp
= ripcbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
707 inp
= inp
->inp_list
.le_next
) {
708 if (inp
->inp_gencnt
<= gencnt
)
715 for (i
= 0; i
< n
; i
++) {
717 if (inp
->inp_gencnt
<= gencnt
) {
719 xi
.xi_len
= sizeof xi
;
720 /* XXX should avoid extra copy */
721 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
723 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
724 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
729 * Give the user an updated idea of our state.
730 * If the generation differs from what we told
731 * her before, she knows that something happened
732 * while we were processing this request, and it
733 * might be necessary to retry.
736 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
737 xig
.xig_sogen
= so_gencnt
;
738 xig
.xig_count
= ripcbinfo
.ipi_count
;
740 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
742 FREE(inp_list
, M_TEMP
);
747 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
, CTLFLAG_RD
, 0, 0,
748 rip_pcblist
, "S,xinpcb", "List of active raw IP sockets");
750 struct pr_usrreqs rip_usrreqs
= {
751 rip_abort
, pru_accept_notsupp
, rip_attach
, rip_bind
, rip_connect
,
752 pru_connect2_notsupp
, in_control
, rip_detach
, rip_disconnect
,
753 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
754 pru_rcvoob_notsupp
, rip_send
, pru_sense_null
, rip_shutdown
,
755 in_setsockaddr
, sosend
, soreceive
, sopoll