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.18 2004/12/03 20:29:53 joerg Exp $
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_random_ip_id.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
53 #include <machine/stdarg.h>
55 #include <vm/vm_zone.h>
58 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
68 #include <net/ip_mroute/ip_mroute.h>
69 #include <net/ipfw/ip_fw.h>
70 #include <net/dummynet/ip_dummynet.h>
73 #include <netproto/ipsec/ipsec.h>
77 #include <netinet6/ipsec.h>
80 struct inpcbinfo ripcbinfo;
82 /* control hooks for ipfw and dummynet */
83 ip_fw_ctl_t *ip_fw_ctl_ptr;
84 ip_dn_ctl_t *ip_dn_ctl_ptr;
87 * hooks for multicast routing. They all default to NULL,
88 * so leave them not initialized and rely on BSS being set to 0.
91 /* The socket used to communicate with the multicast routing daemon. */
92 struct socket *ip_mrouter;
94 /* The various mrouter and rsvp functions */
95 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
96 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
97 int (*ip_mrouter_done)(void);
98 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
99 struct ip_moptions *);
100 int (*mrt_ioctl)(int, caddr_t);
101 int (*legal_vif_num)(int);
102 u_long (*ip_mcast_src)(int);
104 void (*rsvp_input_p)(struct mbuf *m, ...);
105 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
106 void (*ip_rsvp_force_done)(struct socket *);
109 * Nominal space allocated to a raw ip socket.
115 * Raw interface to IP protocol.
119 * Initialize raw connection block queue.
124 in_pcbinfo_init(&ripcbinfo);
126 * XXX We don't use the hash list for raw IP, but it's easier
127 * to allocate a one entry hash list than it is to check all
128 * over the place for hashbase == NULL.
130 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
131 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
132 ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
133 &ripcbinfo.wildcardhashmask);
134 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
135 maxsockets, ZONE_INTERRUPT, 0);
139 * XXX ripsrc is modified in rip_input, so we must be fix this
140 * when we want to make this code smp-friendly.
142 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
145 * Setup generic address and protocol structures
146 * for raw_input routine, then pass them along with
150 rip_input(struct mbuf *m, ...)
152 struct ip *ip = mtod(m, struct ip *);
154 struct inpcb *last = NULL;
155 struct mbuf *opts = NULL;
160 off = __va_arg(ap, int);
161 proto = __va_arg(ap, int);
164 ripsrc.sin_addr = ip->ip_src;
165 LIST_FOREACH(inp, &ripcbinfo.pcblisthead, inp_list) {
166 if (inp->inp_flags & INP_PLACEMARKER)
169 if ((inp->inp_vflag & INP_IPV4) == 0)
172 if (inp->inp_ip_p && inp->inp_ip_p != proto)
174 if (inp->inp_laddr.s_addr != INADDR_ANY &&
175 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
177 if (inp->inp_faddr.s_addr != INADDR_ANY &&
178 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
181 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
184 /* check AH/ESP integrity. */
185 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
187 ipsecstat.in_polvio++;
188 /* do not inject data to pcb */
192 /* check AH/ESP integrity. */
193 if (ipsec4_in_reject(n, last)) {
195 /* do not inject data to pcb */
197 #endif /*FAST_IPSEC*/
199 if (last->inp_flags & INP_CONTROLOPTS ||
200 last->inp_socket->so_options & SO_TIMESTAMP)
201 ip_savecontrol(last, &opts, ip, n);
202 if (sbappendaddr(&last->inp_socket->so_rcv,
203 (struct sockaddr *)&ripsrc, n,
205 /* should notify about lost packet */
210 sorwakeup(last->inp_socket);
217 /* check AH/ESP integrity. */
218 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
220 ipsecstat.in_polvio++;
221 ipstat.ips_delivered--;
222 /* do not inject data to pcb */
226 /* check AH/ESP integrity. */
227 if (last && ipsec4_in_reject(m, last)) {
229 ipstat.ips_delivered--;
230 /* do not inject data to pcb */
232 #endif /*FAST_IPSEC*/
234 if (last->inp_flags & INP_CONTROLOPTS ||
235 last->inp_socket->so_options & SO_TIMESTAMP)
236 ip_savecontrol(last, &opts, ip, m);
237 if (sbappendaddr(&last->inp_socket->so_rcv,
238 (struct sockaddr *)&ripsrc, m, opts) == 0) {
243 sorwakeup(last->inp_socket);
246 ipstat.ips_noproto++;
247 ipstat.ips_delivered--;
252 * Generate IP header and pass packet to ip_output.
253 * Tack on options user may have setup with control call.
256 rip_output(struct mbuf *m, struct socket *so, ...)
259 struct inpcb *inp = sotoinpcb(so);
261 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
265 dst = __va_arg(ap, u_long);
269 * If the user handed us a complete IP packet, use it.
270 * Otherwise, allocate an mbuf for a header and fill it in.
272 if ((inp->inp_flags & INP_HDRINCL) == 0) {
273 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
277 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
280 ip = mtod(m, struct ip *);
281 ip->ip_tos = inp->inp_ip_tos;
283 ip->ip_p = inp->inp_ip_p;
284 ip->ip_len = m->m_pkthdr.len;
285 ip->ip_src = inp->inp_laddr;
286 ip->ip_dst.s_addr = dst;
287 ip->ip_ttl = inp->inp_ip_ttl;
289 if (m->m_pkthdr.len > IP_MAXPACKET) {
293 ip = mtod(m, struct ip *);
294 /* don't allow both user specified and setsockopt options,
295 and don't allow packet length sizes that will crash */
296 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
298 || (ip->ip_len > m->m_pkthdr.len)
299 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
305 ip->ip_id = ip_randomid();
307 ip->ip_id = htons(ip_id++);
309 /* XXX prevent ip_output from overwriting header fields */
310 flags |= IP_RAWOUTPUT;
314 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
315 inp->inp_moptions, inp));
319 * Raw IP socket option processing.
322 rip_ctloutput(struct socket *so, struct sockopt *sopt)
324 struct inpcb *inp = sotoinpcb(so);
327 if (sopt->sopt_level != IPPROTO_IP)
332 switch (sopt->sopt_dir) {
334 switch (sopt->sopt_name) {
336 optval = inp->inp_flags & INP_HDRINCL;
337 error = sooptcopyout(sopt, &optval, sizeof optval);
340 case IP_FW_ADD: /* ADD actually returns the body... */
343 error = ip_fw_ctl_ptr(sopt);
348 case IP_DUMMYNET_GET:
350 error = ip_dn_ctl_ptr(sopt);
363 case MRT_API_SUPPORT:
365 case MRT_ADD_BW_UPCALL:
366 case MRT_DEL_BW_UPCALL:
367 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
372 error = ip_ctloutput(so, sopt);
378 switch (sopt->sopt_name) {
380 error = sooptcopyin(sopt, &optval, sizeof optval,
385 inp->inp_flags |= INP_HDRINCL;
387 inp->inp_flags &= ~INP_HDRINCL;
396 error = ip_fw_ctl_ptr(sopt);
401 case IP_DUMMYNET_CONFIGURE:
402 case IP_DUMMYNET_DEL:
403 case IP_DUMMYNET_FLUSH:
405 error = ip_dn_ctl_ptr(sopt);
407 error = ENOPROTOOPT ;
411 error = ip_rsvp_init(so);
415 error = ip_rsvp_done();
419 case IP_RSVP_VIF_OFF:
420 error = ip_rsvp_vif ?
421 ip_rsvp_vif(so, sopt) : EINVAL;
432 case MRT_API_SUPPORT:
434 case MRT_ADD_BW_UPCALL:
435 case MRT_DEL_BW_UPCALL:
436 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
441 error = ip_ctloutput(so, sopt);
451 * This function exists solely to receive the PRC_IFDOWN messages which
452 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
453 * and calls in_ifadown() to remove all routes corresponding to that address.
454 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
458 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
460 struct in_ifaddr *ia;
467 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
468 if (ia->ia_ifa.ifa_addr == sa
469 && (ia->ia_flags & IFA_ROUTE)) {
471 * in_ifscrub kills the interface route.
473 in_ifscrub(ia->ia_ifp, ia);
475 * in_ifadown gets rid of all the rest of
476 * the routes. This is not quite the right
477 * thing to do, but at least if we are running
478 * a routing process they will come back.
480 in_ifadown(&ia->ia_ifa, 0);
487 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
488 if (ia->ia_ifa.ifa_addr == sa)
491 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
494 ifp = ia->ia_ifa.ifa_ifp;
496 if ((ifp->if_flags & IFF_LOOPBACK)
497 || (ifp->if_flags & IFF_POINTOPOINT))
500 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
502 ia->ia_flags |= IFA_ROUTE;
507 u_long rip_sendspace = RIPSNDQ;
508 u_long rip_recvspace = RIPRCVQ;
510 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
511 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
512 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
513 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
516 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
524 if ((error = suser_cred(ai->p_ucred, NULL_CRED_OKAY)) != 0)
527 error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
531 error = in_pcballoc(so, &ripcbinfo);
535 inp = (struct inpcb *)so->so_pcb;
536 inp->inp_vflag |= INP_IPV4;
537 inp->inp_ip_p = proto;
538 inp->inp_ip_ttl = ip_defttl;
543 rip_detach(struct socket *so)
550 if (so == ip_mrouter && ip_mrouter_done)
552 if (ip_rsvp_force_done)
553 ip_rsvp_force_done(so);
561 rip_abort(struct socket *so)
563 soisdisconnected(so);
564 if (so->so_state & SS_NOFDREF)
565 return rip_detach(so);
570 rip_disconnect(struct socket *so)
572 if ((so->so_state & SS_ISCONNECTED) == 0)
574 return rip_abort(so);
578 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
580 struct inpcb *inp = sotoinpcb(so);
581 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
583 if (nam->sa_len != sizeof(*addr))
586 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
587 (addr->sin_family != AF_IMPLINK)) ||
588 (addr->sin_addr.s_addr != INADDR_ANY &&
589 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
590 return EADDRNOTAVAIL;
591 inp->inp_laddr = addr->sin_addr;
596 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
598 struct inpcb *inp = sotoinpcb(so);
599 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
601 if (nam->sa_len != sizeof(*addr))
603 if (TAILQ_EMPTY(&ifnet))
604 return EADDRNOTAVAIL;
605 if ((addr->sin_family != AF_INET) &&
606 (addr->sin_family != AF_IMPLINK))
608 inp->inp_faddr = addr->sin_addr;
614 rip_shutdown(struct socket *so)
621 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
622 struct mbuf *control, struct thread *td)
624 struct inpcb *inp = sotoinpcb(so);
627 if (so->so_state & SS_ISCONNECTED) {
632 dst = inp->inp_faddr.s_addr;
638 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
640 return rip_output(m, so, dst);
643 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, &ripcbinfo, 0,
644 in_pcblist_global, "S,xinpcb", "List of active raw IP sockets");
646 struct pr_usrreqs rip_usrreqs = {
647 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
648 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
649 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
650 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
651 in_setsockaddr, sosend, soreceive, sopoll