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/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #include <netinet/ip_mroute.h>
84 #include <netinet/ip_fw.h>
87 #include <netinet6/ipsec.h>
91 #include <netinet/ip_dummynet.h>
95 extern int ipsec_bypass
;
98 struct inpcbhead ripcb
;
99 struct inpcbinfo ripcbinfo
;
102 * Nominal space allocated to a raw ip socket.
108 * Raw interface to IP protocol.
112 * Initialize raw connection block q.
118 ripcbinfo
.listhead
= &ripcb
;
120 * XXX We don't use the hash list for raw IP, but it's easier
121 * to allocate a one entry hash list than it is to check all
122 * over the place for hashbase == NULL.
124 ripcbinfo
.hashbase
= hashinit(1, M_PCB
, &ripcbinfo
.hashmask
);
125 ripcbinfo
.porthashbase
= hashinit(1, M_PCB
, &ripcbinfo
.porthashmask
);
127 ripcbinfo
.ipi_zone
= (void *) zinit(sizeof(struct inpcb
),
128 (4096 * sizeof(struct inpcb
)),
133 static struct sockaddr_in ripsrc
= { sizeof(ripsrc
), AF_INET
};
135 * Setup generic address and protocol structures
136 * for raw_input routine, then pass them along with
144 register struct ip
*ip
= mtod(m
, struct ip
*);
145 register struct inpcb
*inp
;
146 struct inpcb
*last
= 0;
147 struct mbuf
*opts
= 0;
149 ripsrc
.sin_addr
= ip
->ip_src
;
150 LIST_FOREACH(inp
, &ripcb
, inp_list
) {
152 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
155 if (inp
->inp_ip_p
&& (inp
->inp_ip_p
!= ip
->ip_p
))
157 if (inp
->inp_laddr
.s_addr
&&
158 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
160 if (inp
->inp_faddr
.s_addr
&&
161 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
164 struct mbuf
*n
= m_copy(m
, 0, (int)M_COPYALL
);
167 /* check AH/ESP integrity. */
168 if (ipsec_bypass
== 0 && n
&& ipsec4_in_reject_so(n
, last
->inp_socket
)) {
170 ipsecstat
.in_polvio
++;
171 /* do not inject data to pcb */
175 if (last
->inp_flags
& INP_CONTROLOPTS
||
176 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
177 ip_savecontrol(last
, &opts
, ip
, n
);
178 if (last
->inp_flags
& INP_STRIPHDR
) {
180 n
->m_pkthdr
.len
-= iphlen
;
183 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
184 (struct sockaddr
*)&ripsrc
, n
,
186 /* should notify about lost packet */
187 kprintf("rip_input can't append to socket\n");
192 sorwakeup(last
->inp_socket
);
199 /* check AH/ESP integrity. */
200 if (ipsec_bypass
== 0 && last
&& ipsec4_in_reject_so(m
, last
->inp_socket
)) {
202 ipsecstat
.in_polvio
++;
203 ipstat
.ips_delivered
--;
204 /* do not inject data to pcb */
208 if (last
->inp_flags
& INP_CONTROLOPTS
||
209 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
210 ip_savecontrol(last
, &opts
, ip
, m
);
211 if (last
->inp_flags
& INP_STRIPHDR
) {
213 m
->m_pkthdr
.len
-= iphlen
;
216 if (sbappendaddr(&last
->inp_socket
->so_rcv
,
217 (struct sockaddr
*)&ripsrc
, m
, opts
) == 0) {
218 kprintf("rip_input(2) can't append to socket\n");
223 sorwakeup(last
->inp_socket
);
226 ipstat
.ips_noproto
++;
227 ipstat
.ips_delivered
--;
232 * Generate IP header and pass packet to ip_output.
233 * Tack on options user may have setup with control call.
236 rip_output(m
, so
, dst
)
237 register struct mbuf
*m
;
241 register struct ip
*ip
;
242 register struct inpcb
*inp
= sotoinpcb(so
);
243 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
246 * If the user handed us a complete IP packet, use it.
247 * Otherwise, allocate an mbuf for a header and fill it in.
249 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
250 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
254 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
);
255 ip
= mtod(m
, struct ip
*);
256 ip
->ip_tos
= inp
->inp_ip_tos
;
258 ip
->ip_p
= inp
->inp_ip_p
;
259 ip
->ip_len
= m
->m_pkthdr
.len
;
260 ip
->ip_src
= inp
->inp_laddr
;
261 ip
->ip_dst
.s_addr
= dst
;
262 ip
->ip_ttl
= inp
->inp_ip_ttl
;
264 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
268 ip
= mtod(m
, struct ip
*);
269 /* don't allow both user specified and setsockopt options,
270 and don't allow packet length sizes that will crash */
271 if (((IP_VHL_HL(ip
->ip_vhl
) != (sizeof (*ip
) >> 2))
273 || (ip
->ip_len
> m
->m_pkthdr
.len
)
274 || (ip
->ip_len
< (IP_VHL_HL(ip
->ip_vhl
) << 2))) {
280 ip
->ip_id
= ip_randomid();
282 ip
->ip_id
= htons(ip_id
++);
284 /* XXX prevent ip_output from overwriting header fields */
285 flags
|= IP_RAWOUTPUT
;
290 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, so
) != 0) {
296 return (ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
301 * Raw IP socket option processing.
304 rip_ctloutput(so
, sopt
)
306 struct sockopt
*sopt
;
308 struct inpcb
*inp
= sotoinpcb(so
);
311 if (sopt
->sopt_level
!= IPPROTO_IP
)
316 switch (sopt
->sopt_dir
) {
318 switch (sopt
->sopt_name
) {
320 optval
= inp
->inp_flags
& INP_HDRINCL
;
321 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
325 optval
= inp
->inp_flags
& INP_STRIPHDR
;
326 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
333 if (ip_fw_ctl_ptr
== 0)
336 error
= ip_fw_ctl_ptr(sopt
);
340 case IP_DUMMYNET_GET
:
341 if (ip_dn_ctl_ptr
== NULL
)
342 error
= ENOPROTOOPT
;
344 error
= ip_dn_ctl_ptr(sopt
);
346 #endif /* DUMMYNET */
356 error
= ip_mrouter_get(so
, sopt
);
360 error
= ip_ctloutput(so
, sopt
);
366 switch (sopt
->sopt_name
) {
368 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
373 inp
->inp_flags
|= INP_HDRINCL
;
375 inp
->inp_flags
&= ~INP_HDRINCL
;
379 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
384 inp
->inp_flags
|= INP_STRIPHDR
;
386 inp
->inp_flags
&= ~INP_STRIPHDR
;
397 case IP_OLD_FW_FLUSH
:
399 case IP_OLD_FW_RESETLOG
:
400 if (ip_fw_ctl_ptr
== 0)
403 error
= ip_fw_ctl_ptr(sopt
);
407 case IP_DUMMYNET_CONFIGURE
:
408 case IP_DUMMYNET_DEL
:
409 case IP_DUMMYNET_FLUSH
:
410 if (ip_dn_ctl_ptr
== NULL
)
411 error
= ENOPROTOOPT
;
413 error
= ip_dn_ctl_ptr(sopt
);
418 error
= ip_rsvp_init(so
);
422 error
= ip_rsvp_done();
425 /* XXX - should be combined */
427 error
= ip_rsvp_vif_init(so
, sopt
);
430 case IP_RSVP_VIF_OFF
:
431 error
= ip_rsvp_vif_done(so
, sopt
);
442 error
= ip_mrouter_set(so
, sopt
);
446 error
= ip_ctloutput(so
, sopt
);
456 * This function exists solely to receive the PRC_IFDOWN messages which
457 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
458 * and calls in_ifadown() to remove all routes corresponding to that address.
459 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
463 rip_ctlinput(cmd
, sa
, vip
)
468 struct in_ifaddr
*ia
;
475 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
476 ia
= ia
->ia_link
.tqe_next
) {
477 if (ia
->ia_ifa
.ifa_addr
== sa
478 && (ia
->ia_flags
& IFA_ROUTE
)) {
480 * in_ifscrub kills the interface route.
482 in_ifscrub(ia
->ia_ifp
, ia
);
484 * in_ifadown gets rid of all the rest of
485 * the routes. This is not quite the right
486 * thing to do, but at least if we are running
487 * a routing process they will come back.
489 in_ifadown(&ia
->ia_ifa
, 1);
496 for (ia
= in_ifaddrhead
.tqh_first
; ia
;
497 ia
= ia
->ia_link
.tqe_next
) {
498 if (ia
->ia_ifa
.ifa_addr
== sa
)
501 if (ia
== 0 || (ia
->ia_flags
& IFA_ROUTE
))
504 ifp
= ia
->ia_ifa
.ifa_ifp
;
506 if ((ifp
->if_flags
& IFF_LOOPBACK
)
507 || (ifp
->if_flags
& IFF_POINTOPOINT
))
510 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
512 ia
->ia_flags
|= IFA_ROUTE
;
517 u_long rip_sendspace
= RIPSNDQ
;
518 u_long rip_recvspace
= RIPRCVQ
;
520 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
521 &rip_sendspace
, 0, "Maximum outgoing raw IP datagram size");
522 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
523 &rip_recvspace
, 0, "Maximum incoming raw IP datagram size");
526 rip_attach(struct socket
*so
, int proto
, struct proc
*p
)
535 if ((so
->so_state
& SS_PRIV
) == 0)
538 if (p
&& (error
= suser(p
)) != 0)
542 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
546 error
= in_pcballoc(so
, &ripcbinfo
, p
);
550 inp
= (struct inpcb
*)so
->so_pcb
;
551 inp
->inp_vflag
|= INP_IPV4
;
552 inp
->inp_ip_p
= proto
;
553 inp
->inp_ip_ttl
= ip_defttl
;
557 __private_extern__
int
558 rip_detach(struct socket
*so
)
565 if (so
== ip_mrouter
)
567 ip_rsvp_force_done(so
);
574 __private_extern__
int
575 rip_abort(struct socket
*so
)
577 soisdisconnected(so
);
578 return rip_detach(so
);
581 __private_extern__
int
582 rip_disconnect(struct socket
*so
)
584 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
586 return rip_abort(so
);
589 __private_extern__
int
590 rip_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
592 struct inpcb
*inp
= sotoinpcb(so
);
593 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
595 if (nam
->sa_len
!= sizeof(*addr
))
598 if (TAILQ_EMPTY(&ifnet
) || ((addr
->sin_family
!= AF_INET
) &&
599 (addr
->sin_family
!= AF_IMPLINK
)) ||
600 (addr
->sin_addr
.s_addr
&&
601 ifa_ifwithaddr((struct sockaddr
*)addr
) == 0))
602 return EADDRNOTAVAIL
;
603 inp
->inp_laddr
= addr
->sin_addr
;
607 __private_extern__
int
608 rip_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
610 struct inpcb
*inp
= sotoinpcb(so
);
611 struct sockaddr_in
*addr
= (struct sockaddr_in
*)nam
;
613 if (nam
->sa_len
!= sizeof(*addr
))
615 if (TAILQ_EMPTY(&ifnet
))
616 return EADDRNOTAVAIL
;
617 if ((addr
->sin_family
!= AF_INET
) &&
618 (addr
->sin_family
!= AF_IMPLINK
))
620 inp
->inp_faddr
= addr
->sin_addr
;
625 __private_extern__
int
626 rip_shutdown(struct socket
*so
)
632 __private_extern__
int
633 rip_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
634 struct mbuf
*control
, struct proc
*p
)
636 struct inpcb
*inp
= sotoinpcb(so
);
639 if (so
->so_state
& SS_ISCONNECTED
) {
644 dst
= inp
->inp_faddr
.s_addr
;
650 dst
= ((struct sockaddr_in
*)nam
)->sin_addr
.s_addr
;
652 return rip_output(m
, so
, dst
);
656 rip_pcblist SYSCTL_HANDLER_ARGS
659 struct inpcb
*inp
, **inp_list
;
664 * The process of preparing the TCB list is too time-consuming and
665 * resource-intensive to repeat twice on every request.
667 if (req
->oldptr
== 0) {
668 n
= ripcbinfo
.ipi_count
;
669 req
->oldidx
= 2 * (sizeof xig
)
670 + (n
+ n
/8) * sizeof(struct xinpcb
);
674 if (req
->newptr
!= 0)
678 * OK, now we're committed to doing something.
681 gencnt
= ripcbinfo
.ipi_gencnt
;
682 n
= ripcbinfo
.ipi_count
;
685 xig
.xig_len
= sizeof xig
;
687 xig
.xig_gen
= gencnt
;
688 xig
.xig_sogen
= so_gencnt
;
689 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
693 * We are done if there is no pcb
698 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
703 for (inp
= ripcbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
704 inp
= inp
->inp_list
.le_next
) {
705 if (inp
->inp_gencnt
<= gencnt
)
712 for (i
= 0; i
< n
; i
++) {
714 if (inp
->inp_gencnt
<= gencnt
) {
716 xi
.xi_len
= sizeof xi
;
717 /* XXX should avoid extra copy */
718 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
720 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
721 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
726 * Give the user an updated idea of our state.
727 * If the generation differs from what we told
728 * her before, she knows that something happened
729 * while we were processing this request, and it
730 * might be necessary to retry.
733 xig
.xig_gen
= ripcbinfo
.ipi_gencnt
;
734 xig
.xig_sogen
= so_gencnt
;
735 xig
.xig_count
= ripcbinfo
.ipi_count
;
737 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
739 FREE(inp_list
, M_TEMP
);
743 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
, CTLFLAG_RD
, 0, 0,
744 rip_pcblist
, "S,xinpcb", "List of active raw IP sockets");
746 struct pr_usrreqs rip_usrreqs
= {
747 rip_abort
, pru_accept_notsupp
, rip_attach
, rip_bind
, rip_connect
,
748 pru_connect2_notsupp
, in_control
, rip_detach
, rip_disconnect
,
749 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
750 pru_rcvoob_notsupp
, rip_send
, pru_sense_null
, rip_shutdown
,
751 in_setsockaddr
, sosend
, soreceive
, sopoll