2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
34 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.64.2.16 2003/08/24 08:24:38 hsu Exp $
35 * $DragonFly: src/sys/netinet/raw_ip.c,v 1.33 2008/10/28 03:07:28 sephe Exp $
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
55 #include <machine/stdarg.h>
57 #include <vm/vm_zone.h>
60 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
70 #include <net/ip_mroute/ip_mroute.h>
71 #include <net/ipfw/ip_fw.h>
72 #include <net/dummynet/ip_dummynet.h>
75 #include <netproto/ipsec/ipsec.h>
79 #include <netinet6/ipsec.h>
82 struct inpcbinfo ripcbinfo
;
84 /* control hooks for ipfw and dummynet */
85 ip_fw_ctl_t
*ip_fw_ctl_ptr
;
86 ip_dn_ctl_t
*ip_dn_ctl_ptr
;
89 * hooks for multicast routing. They all default to NULL,
90 * so leave them not initialized and rely on BSS being set to 0.
93 /* The socket used to communicate with the multicast routing daemon. */
94 struct socket
*ip_mrouter
;
96 /* The various mrouter and rsvp functions */
97 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*);
98 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*);
99 int (*ip_mrouter_done
)(void);
100 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
101 struct ip_moptions
*);
102 int (*mrt_ioctl
)(int, caddr_t
);
103 int (*legal_vif_num
)(int);
104 u_long (*ip_mcast_src
)(int);
106 void (*rsvp_input_p
)(struct mbuf
*m
, ...);
107 int (*ip_rsvp_vif
)(struct socket
*, struct sockopt
*);
108 void (*ip_rsvp_force_done
)(struct socket
*);
111 * Nominal space allocated to a raw ip socket.
117 * Raw interface to IP protocol.
121 * Initialize raw connection block queue.
126 in_pcbinfo_init(&ripcbinfo
);
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
);
134 ripcbinfo
.wildcardhashbase
= hashinit(1, M_PCB
,
135 &ripcbinfo
.wildcardhashmask
);
136 ripcbinfo
.ipi_zone
= zinit("ripcb", sizeof(struct inpcb
),
137 maxsockets
, ZONE_INTERRUPT
, 0);
141 * Setup generic address and protocol structures
142 * for raw_input routine, then pass them along with
146 rip_input(struct mbuf
*m
, ...)
148 struct sockaddr_in ripsrc
= { sizeof ripsrc
, AF_INET
};
149 struct ip
*ip
= mtod(m
, struct ip
*);
151 struct inpcb
*last
= NULL
;
152 struct mbuf
*opts
= NULL
;
157 off
= __va_arg(ap
, int);
158 proto
= __va_arg(ap
, int);
161 ripsrc
.sin_addr
= ip
->ip_src
;
162 LIST_FOREACH(inp
, &ripcbinfo
.pcblisthead
, inp_list
) {
163 if (inp
->inp_flags
& INP_PLACEMARKER
)
166 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
169 if (inp
->inp_ip_p
&& inp
->inp_ip_p
!= proto
)
171 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
&&
172 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
)
174 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
&&
175 inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
)
178 struct mbuf
*n
= m_copypacket(m
, MB_DONTWAIT
);
181 /* check AH/ESP integrity. */
182 if (n
&& ipsec4_in_reject_so(n
, last
->inp_socket
)) {
184 ipsecstat
.in_polvio
++;
185 /* do not inject data to pcb */
189 /* check AH/ESP integrity. */
190 if (ipsec4_in_reject(n
, last
)) {
192 /* do not inject data to pcb */
194 #endif /*FAST_IPSEC*/
196 if (last
->inp_flags
& INP_CONTROLOPTS
||
197 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
198 ip_savecontrol(last
, &opts
, ip
, n
);
199 if (ssb_appendaddr(&last
->inp_socket
->so_rcv
,
200 (struct sockaddr
*)&ripsrc
, n
,
202 /* should notify about lost packet */
207 sorwakeup(last
->inp_socket
);
214 /* check AH/ESP integrity. */
215 if (last
&& ipsec4_in_reject_so(m
, last
->inp_socket
)) {
217 ipsecstat
.in_polvio
++;
218 ipstat
.ips_delivered
--;
219 /* do not inject data to pcb */
223 /* check AH/ESP integrity. */
224 if (last
&& ipsec4_in_reject(m
, last
)) {
226 ipstat
.ips_delivered
--;
227 /* do not inject data to pcb */
229 #endif /*FAST_IPSEC*/
230 /* Check the minimum TTL for socket. */
231 if (last
&& ip
->ip_ttl
< last
->inp_ip_minttl
) {
233 ipstat
.ips_delivered
--;
235 if (last
->inp_flags
& INP_CONTROLOPTS
||
236 last
->inp_socket
->so_options
& SO_TIMESTAMP
)
237 ip_savecontrol(last
, &opts
, ip
, m
);
238 if (ssb_appendaddr(&last
->inp_socket
->so_rcv
,
239 (struct sockaddr
*)&ripsrc
, m
, opts
) == 0) {
244 sorwakeup(last
->inp_socket
);
247 ipstat
.ips_noproto
++;
248 ipstat
.ips_delivered
--;
253 * Generate IP header and pass packet to ip_output.
254 * Tack on options user may have setup with control call.
257 rip_output(struct mbuf
*m
, struct socket
*so
, ...)
260 struct inpcb
*inp
= so
->so_pcb
;
262 int flags
= (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
;
266 dst
= __va_arg(ap
, u_long
);
270 * If the user handed us a complete IP packet, use it.
271 * Otherwise, allocate an mbuf for a header and fill it in.
273 if ((inp
->inp_flags
& INP_HDRINCL
) == 0) {
274 if (m
->m_pkthdr
.len
+ sizeof(struct ip
) > IP_MAXPACKET
) {
278 M_PREPEND(m
, sizeof(struct ip
), MB_WAIT
);
281 ip
= mtod(m
, struct ip
*);
282 ip
->ip_tos
= inp
->inp_ip_tos
;
284 ip
->ip_p
= inp
->inp_ip_p
;
285 ip
->ip_len
= m
->m_pkthdr
.len
;
286 ip
->ip_src
= inp
->inp_laddr
;
287 ip
->ip_dst
.s_addr
= dst
;
288 ip
->ip_ttl
= inp
->inp_ip_ttl
;
292 if (m
->m_pkthdr
.len
> IP_MAXPACKET
) {
296 if (m
->m_len
< sizeof(struct ip
)) {
297 m
= m_pullup(m
, sizeof(struct ip
));
301 ip
= mtod(m
, struct ip
*);
302 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
304 /* Don't allow header length less than the minimum. */
305 if (hlen
< sizeof(struct ip
)) {
311 * Don't allow both user specified and setsockopt options.
312 * Don't allow packet length sizes that will crash.
314 if ((hlen
!= sizeof(struct ip
) && inp
->inp_options
) ||
315 ip
->ip_len
> m
->m_pkthdr
.len
|| ip
->ip_len
< hlen
) {
320 ip
->ip_id
= ip_newid();
322 /* Prevent ip_output from overwriting header fields */
323 flags
|= IP_RAWOUTPUT
;
327 return ip_output(m
, inp
->inp_options
, &inp
->inp_route
, flags
,
328 inp
->inp_moptions
, inp
);
332 * Raw IP socket option processing.
335 rip_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
337 struct inpcb
*inp
= so
->so_pcb
;
340 if (sopt
->sopt_level
!= IPPROTO_IP
)
345 switch (sopt
->sopt_dir
) {
347 switch (sopt
->sopt_name
) {
349 optval
= inp
->inp_flags
& INP_HDRINCL
;
350 soopt_from_kbuf(sopt
, &optval
, sizeof optval
);
353 case IP_FW_ADD
: /* ADD actually returns the body... */
356 error
= ip_fw_sockopt(sopt
);
361 case IP_DUMMYNET_GET
:
362 error
= ip_dn_sockopt(sopt
);
373 case MRT_API_SUPPORT
:
375 case MRT_ADD_BW_UPCALL
:
376 case MRT_DEL_BW_UPCALL
:
377 error
= ip_mrouter_get
? ip_mrouter_get(so
, sopt
) :
382 error
= ip_ctloutput(so
, sopt
);
388 switch (sopt
->sopt_name
) {
390 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
,
395 inp
->inp_flags
|= INP_HDRINCL
;
397 inp
->inp_flags
&= ~INP_HDRINCL
;
406 error
= ip_fw_ctl_ptr(sopt
);
411 case IP_DUMMYNET_CONFIGURE
:
412 case IP_DUMMYNET_DEL
:
413 case IP_DUMMYNET_FLUSH
:
414 error
= ip_dn_sockopt(sopt
);
418 error
= ip_rsvp_init(so
);
422 error
= ip_rsvp_done();
426 case IP_RSVP_VIF_OFF
:
427 error
= ip_rsvp_vif
?
428 ip_rsvp_vif(so
, sopt
) : EINVAL
;
439 case MRT_API_SUPPORT
:
441 case MRT_ADD_BW_UPCALL
:
442 case MRT_DEL_BW_UPCALL
:
443 error
= ip_mrouter_set
? ip_mrouter_set(so
, sopt
) :
448 error
= ip_ctloutput(so
, sopt
);
458 * This function exists solely to receive the PRC_IFDOWN messages which
459 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
460 * and calls in_ifadown() to remove all routes corresponding to that address.
461 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
465 rip_ctlinput(int cmd
, struct sockaddr
*sa
, void *vip
)
467 struct in_ifaddr
*ia
;
468 struct in_ifaddr_container
*iac
;
475 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
478 if (ia
->ia_ifa
.ifa_addr
== sa
&&
479 (ia
->ia_flags
& IFA_ROUTE
)) {
481 * in_ifscrub kills the interface route.
483 in_ifscrub(ia
->ia_ifp
, ia
);
485 * in_ifadown gets rid of all the rest of
486 * the routes. This is not quite the right
487 * thing to do, but at least if we are running
488 * a routing process they will come back.
490 in_ifadown(&ia
->ia_ifa
, 0);
498 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
499 if (iac
->ia
->ia_ifa
.ifa_addr
== sa
) {
504 if (ia
== NULL
|| (ia
->ia_flags
& IFA_ROUTE
))
507 ifp
= ia
->ia_ifa
.ifa_ifp
;
509 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
510 (ifp
->if_flags
& IFF_POINTOPOINT
))
513 err
= rtinit(&ia
->ia_ifa
, RTM_ADD
, flags
);
515 ia
->ia_flags
|= IFA_ROUTE
;
520 u_long rip_sendspace
= RIPSNDQ
;
521 u_long rip_recvspace
= RIPRCVQ
;
523 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, maxdgram
, CTLFLAG_RW
,
524 &rip_sendspace
, 0, "Maximum outgoing raw IP datagram size");
525 SYSCTL_INT(_net_inet_raw
, OID_AUTO
, recvspace
, CTLFLAG_RW
,
526 &rip_recvspace
, 0, "Maximum incoming raw IP datagram size");
529 rip_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
537 error
= priv_check_cred(ai
->p_ucred
, PRIV_NETINET_RAW
, NULL_CRED_OKAY
);
541 error
= soreserve(so
, rip_sendspace
, rip_recvspace
, ai
->sb_rlimit
);
545 error
= in_pcballoc(so
, &ripcbinfo
);
549 inp
= (struct inpcb
*)so
->so_pcb
;
550 inp
->inp_vflag
|= INP_IPV4
;
551 inp
->inp_ip_p
= proto
;
552 inp
->inp_ip_ttl
= ip_defttl
;
557 rip_detach(struct socket
*so
)
564 if (so
== ip_mrouter
&& ip_mrouter_done
)
566 if (ip_rsvp_force_done
)
567 ip_rsvp_force_done(so
);
575 rip_abort(struct socket
*so
)
577 soisdisconnected(so
);
578 if (so
->so_state
& SS_NOFDREF
)
579 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 thread
*td
)
594 struct inpcb
*inp
= so
->so_pcb
;
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
!= INADDR_ANY
&&
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 thread
*td
)
612 struct inpcb
*inp
= so
->so_pcb
;
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 thread
*td
)
638 struct inpcb
*inp
= so
->so_pcb
;
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
);
657 SYSCTL_PROC(_net_inet_raw
, OID_AUTO
/*XXX*/, pcblist
, CTLFLAG_RD
, &ripcbinfo
, 0,
658 in_pcblist_global
, "S,xinpcb", "List of active raw IP sockets");
660 struct pr_usrreqs rip_usrreqs
= {
661 .pru_abort
= rip_abort
,
662 .pru_accept
= pru_accept_notsupp
,
663 .pru_attach
= rip_attach
,
664 .pru_bind
= rip_bind
,
665 .pru_connect
= rip_connect
,
666 .pru_connect2
= pru_connect2_notsupp
,
667 .pru_control
= in_control
,
668 .pru_detach
= rip_detach
,
669 .pru_disconnect
= rip_disconnect
,
670 .pru_listen
= pru_listen_notsupp
,
671 .pru_peeraddr
= in_setpeeraddr
,
672 .pru_rcvd
= pru_rcvd_notsupp
,
673 .pru_rcvoob
= pru_rcvoob_notsupp
,
674 .pru_send
= rip_send
,
675 .pru_sense
= pru_sense_null
,
676 .pru_shutdown
= rip_shutdown
,
677 .pru_sockaddr
= in_setsockaddr
,
678 .pru_sosend
= sosend
,
679 .pru_soreceive
= soreceive
,