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.15 2003/01/24 10:52:50 hsu Exp $
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_random_ip_id.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
52 #include <vm/vm_zone.h>
55 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_mroute.h>
66 #include <netinet/ip_fw.h>
67 #include <netinet/ip_dummynet.h>
70 #include <netipsec/ipsec.h>
74 #include <netinet6/ipsec.h>
77 struct inpcbhead ripcb;
78 struct inpcbinfo ripcbinfo;
80 /* control hooks for ipfw and dummynet */
81 ip_fw_ctl_t *ip_fw_ctl_ptr;
82 ip_dn_ctl_t *ip_dn_ctl_ptr;
85 * hooks for multicast routing. They all default to NULL,
86 * so leave them not initialized and rely on BSS being set to 0.
89 /* The socket used to communicate with the multicast routing daemon. */
90 struct socket *ip_mrouter;
92 /* The various mrouter and rsvp functions */
93 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
94 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
95 int (*ip_mrouter_done)(void);
96 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
97 struct ip_moptions *);
98 int (*mrt_ioctl)(int, caddr_t);
99 int (*legal_vif_num)(int);
100 u_long (*ip_mcast_src)(int);
102 void (*rsvp_input_p)(struct mbuf *m, int off, int proto);
103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104 void (*ip_rsvp_force_done)(struct socket *);
107 * Nominal space allocated to a raw ip socket.
113 * Raw interface to IP protocol.
117 * Initialize raw connection block queue.
123 ripcbinfo.listhead = &ripcb;
125 * XXX We don't use the hash list for raw IP, but it's easier
126 * to allocate a one entry hash list than it is to check all
127 * over the place for hashbase == NULL.
129 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
130 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
131 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
132 maxsockets, ZONE_INTERRUPT, 0);
136 * XXX ripsrc is modified in rip_input, so we must be fix this
137 * when we want to make this code smp-friendly.
139 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
142 * Setup generic address and protocol structures
143 * for raw_input routine, then pass them along with
147 rip_input(struct mbuf *m, int off, int proto)
149 struct ip *ip = mtod(m, struct ip *);
151 struct inpcb *last = NULL;
152 struct mbuf *opts = NULL;
154 ripsrc.sin_addr = ip->ip_src;
155 LIST_FOREACH(inp, &ripcb, inp_list) {
157 if ((inp->inp_vflag & INP_IPV4) == 0)
160 if (inp->inp_ip_p && inp->inp_ip_p != proto)
162 if (inp->inp_laddr.s_addr != INADDR_ANY &&
163 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
165 if (inp->inp_faddr.s_addr != INADDR_ANY &&
166 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
169 struct mbuf *n = m_copypacket(m, M_DONTWAIT);
172 /* check AH/ESP integrity. */
173 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
175 ipsecstat.in_polvio++;
176 /* do not inject data to pcb */
180 /* check AH/ESP integrity. */
181 if (ipsec4_in_reject(n, last)) {
183 /* do not inject data to pcb */
185 #endif /*FAST_IPSEC*/
187 if (last->inp_flags & INP_CONTROLOPTS ||
188 last->inp_socket->so_options & SO_TIMESTAMP)
189 ip_savecontrol(last, &opts, ip, n);
190 if (sbappendaddr(&last->inp_socket->so_rcv,
191 (struct sockaddr *)&ripsrc, n,
193 /* should notify about lost packet */
198 sorwakeup(last->inp_socket);
205 /* check AH/ESP integrity. */
206 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
208 ipsecstat.in_polvio++;
209 ipstat.ips_delivered--;
210 /* do not inject data to pcb */
214 /* check AH/ESP integrity. */
215 if (last && ipsec4_in_reject(m, last)) {
217 ipstat.ips_delivered--;
218 /* do not inject data to pcb */
220 #endif /*FAST_IPSEC*/
222 if (last->inp_flags & INP_CONTROLOPTS ||
223 last->inp_socket->so_options & SO_TIMESTAMP)
224 ip_savecontrol(last, &opts, ip, m);
225 if (sbappendaddr(&last->inp_socket->so_rcv,
226 (struct sockaddr *)&ripsrc, m, opts) == 0) {
231 sorwakeup(last->inp_socket);
234 ipstat.ips_noproto++;
235 ipstat.ips_delivered--;
240 * Generate IP header and pass packet to ip_output.
241 * Tack on options user may have setup with control call.
244 rip_output(struct mbuf *m, struct socket *so, u_long dst)
247 struct inpcb *inp = sotoinpcb(so);
248 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
251 * If the user handed us a complete IP packet, use it.
252 * Otherwise, allocate an mbuf for a header and fill it in.
254 if ((inp->inp_flags & INP_HDRINCL) == 0) {
255 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
259 M_PREPEND(m, sizeof(struct ip), M_WAIT);
260 ip = mtod(m, struct ip *);
261 ip->ip_tos = inp->inp_ip_tos;
263 ip->ip_p = inp->inp_ip_p;
264 ip->ip_len = m->m_pkthdr.len;
265 ip->ip_src = inp->inp_laddr;
266 ip->ip_dst.s_addr = dst;
267 ip->ip_ttl = inp->inp_ip_ttl;
269 if (m->m_pkthdr.len > IP_MAXPACKET) {
273 ip = mtod(m, struct ip *);
274 /* don't allow both user specified and setsockopt options,
275 and don't allow packet length sizes that will crash */
276 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
278 || (ip->ip_len > m->m_pkthdr.len)
279 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
285 ip->ip_id = ip_randomid();
287 ip->ip_id = htons(ip_id++);
289 /* XXX prevent ip_output from overwriting header fields */
290 flags |= IP_RAWOUTPUT;
294 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
295 inp->inp_moptions, inp));
299 * Raw IP socket option processing.
302 rip_ctloutput(struct socket *so, struct sockopt *sopt)
304 struct inpcb *inp = sotoinpcb(so);
307 if (sopt->sopt_level != IPPROTO_IP)
312 switch (sopt->sopt_dir) {
314 switch (sopt->sopt_name) {
316 optval = inp->inp_flags & INP_HDRINCL;
317 error = sooptcopyout(sopt, &optval, sizeof optval);
320 case IP_FW_ADD: /* ADD actually returns the body... */
323 error = ip_fw_ctl_ptr(sopt);
328 case IP_DUMMYNET_GET:
330 error = ip_dn_ctl_ptr(sopt);
343 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
348 error = ip_ctloutput(so, sopt);
354 switch (sopt->sopt_name) {
356 error = sooptcopyin(sopt, &optval, sizeof optval,
361 inp->inp_flags |= INP_HDRINCL;
363 inp->inp_flags &= ~INP_HDRINCL;
372 error = ip_fw_ctl_ptr(sopt);
377 case IP_DUMMYNET_CONFIGURE:
378 case IP_DUMMYNET_DEL:
379 case IP_DUMMYNET_FLUSH:
381 error = ip_dn_ctl_ptr(sopt);
383 error = ENOPROTOOPT ;
387 error = ip_rsvp_init(so);
391 error = ip_rsvp_done();
395 case IP_RSVP_VIF_OFF:
396 error = ip_rsvp_vif ?
397 ip_rsvp_vif(so, sopt) : EINVAL;
408 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
413 error = ip_ctloutput(so, sopt);
423 * This function exists solely to receive the PRC_IFDOWN messages which
424 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
425 * and calls in_ifadown() to remove all routes corresponding to that address.
426 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
430 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
432 struct in_ifaddr *ia;
439 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
440 if (ia->ia_ifa.ifa_addr == sa
441 && (ia->ia_flags & IFA_ROUTE)) {
443 * in_ifscrub kills the interface route.
445 in_ifscrub(ia->ia_ifp, ia);
447 * in_ifadown gets rid of all the rest of
448 * the routes. This is not quite the right
449 * thing to do, but at least if we are running
450 * a routing process they will come back.
452 in_ifadown(&ia->ia_ifa, 0);
459 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
460 if (ia->ia_ifa.ifa_addr == sa)
463 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
466 ifp = ia->ia_ifa.ifa_ifp;
468 if ((ifp->if_flags & IFF_LOOPBACK)
469 || (ifp->if_flags & IFF_POINTOPOINT))
472 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
474 ia->ia_flags |= IFA_ROUTE;
479 u_long rip_sendspace = RIPSNDQ;
480 u_long rip_recvspace = RIPRCVQ;
482 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
483 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
484 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
485 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
488 rip_attach(struct socket *so, int proto, struct proc *p)
496 if (p && (error = suser(p)) != 0)
499 error = soreserve(so, rip_sendspace, rip_recvspace);
503 error = in_pcballoc(so, &ripcbinfo, p);
507 inp = (struct inpcb *)so->so_pcb;
508 inp->inp_vflag |= INP_IPV4;
509 inp->inp_ip_p = proto;
510 inp->inp_ip_ttl = ip_defttl;
515 rip_detach(struct socket *so)
522 if (so == ip_mrouter && ip_mrouter_done)
524 if (ip_rsvp_force_done)
525 ip_rsvp_force_done(so);
533 rip_abort(struct socket *so)
535 soisdisconnected(so);
536 if (so->so_state & SS_NOFDREF)
537 return rip_detach(so);
542 rip_disconnect(struct socket *so)
544 if ((so->so_state & SS_ISCONNECTED) == 0)
546 return rip_abort(so);
550 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
552 struct inpcb *inp = sotoinpcb(so);
553 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
555 if (nam->sa_len != sizeof(*addr))
558 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
559 (addr->sin_family != AF_IMPLINK)) ||
560 (addr->sin_addr.s_addr != INADDR_ANY &&
561 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
562 return EADDRNOTAVAIL;
563 inp->inp_laddr = addr->sin_addr;
568 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
570 struct inpcb *inp = sotoinpcb(so);
571 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
573 if (nam->sa_len != sizeof(*addr))
575 if (TAILQ_EMPTY(&ifnet))
576 return EADDRNOTAVAIL;
577 if ((addr->sin_family != AF_INET) &&
578 (addr->sin_family != AF_IMPLINK))
580 inp->inp_faddr = addr->sin_addr;
586 rip_shutdown(struct socket *so)
593 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
594 struct mbuf *control, struct proc *p)
596 struct inpcb *inp = sotoinpcb(so);
599 if (so->so_state & SS_ISCONNECTED) {
604 dst = inp->inp_faddr.s_addr;
610 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
612 return rip_output(m, so, dst);
616 rip_pcblist(SYSCTL_HANDLER_ARGS)
619 struct inpcb *inp, **inp_list;
624 * The process of preparing the TCB list is too time-consuming and
625 * resource-intensive to repeat twice on every request.
627 if (req->oldptr == 0) {
628 n = ripcbinfo.ipi_count;
629 req->oldidx = 2 * (sizeof xig)
630 + (n + n/8) * sizeof(struct xinpcb);
634 if (req->newptr != 0)
638 * OK, now we're committed to doing something.
641 gencnt = ripcbinfo.ipi_gencnt;
642 n = ripcbinfo.ipi_count;
645 xig.xig_len = sizeof xig;
647 xig.xig_gen = gencnt;
648 xig.xig_sogen = so_gencnt;
649 error = SYSCTL_OUT(req, &xig, sizeof xig);
653 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
658 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
659 inp = LIST_NEXT(inp, inp_list)) {
660 if (inp->inp_gencnt <= gencnt)
667 for (i = 0; i < n; i++) {
669 if (inp->inp_gencnt <= gencnt) {
671 xi.xi_len = sizeof xi;
672 /* XXX should avoid extra copy */
673 bcopy(inp, &xi.xi_inp, sizeof *inp);
675 sotoxsocket(inp->inp_socket, &xi.xi_socket);
676 error = SYSCTL_OUT(req, &xi, sizeof xi);
681 * Give the user an updated idea of our state.
682 * If the generation differs from what we told
683 * her before, she knows that something happened
684 * while we were processing this request, and it
685 * might be necessary to retry.
688 xig.xig_gen = ripcbinfo.ipi_gencnt;
689 xig.xig_sogen = so_gencnt;
690 xig.xig_count = ripcbinfo.ipi_count;
692 error = SYSCTL_OUT(req, &xig, sizeof xig);
694 free(inp_list, M_TEMP);
698 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
699 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
701 struct pr_usrreqs rip_usrreqs = {
702 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
703 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
704 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
705 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
706 in_setsockaddr, sosend, soreceive, sopoll