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.6 2003/08/24 23:07:08 hsu 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 <vm/vm_zone.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_var.h>
66 #include <net/ip_mroute/ip_mroute.h>
67 #include <net/ipfw/ip_fw.h>
68 #include <net/dummynet/ip_dummynet.h>
71 #include <netipsec/ipsec.h>
75 #include <netinet6/ipsec.h>
78 struct inpcbhead ripcb;
79 struct inpcbinfo ripcbinfo;
81 /* control hooks for ipfw and dummynet */
82 ip_fw_ctl_t *ip_fw_ctl_ptr;
83 ip_dn_ctl_t *ip_dn_ctl_ptr;
86 * hooks for multicast routing. They all default to NULL,
87 * so leave them not initialized and rely on BSS being set to 0.
90 /* The socket used to communicate with the multicast routing daemon. */
91 struct socket *ip_mrouter;
93 /* The various mrouter and rsvp functions */
94 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
95 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
96 int (*ip_mrouter_done)(void);
97 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
98 struct ip_moptions *);
99 int (*mrt_ioctl)(int, caddr_t);
100 int (*legal_vif_num)(int);
101 u_long (*ip_mcast_src)(int);
103 void (*rsvp_input_p)(struct mbuf *m, int off, int proto);
104 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
105 void (*ip_rsvp_force_done)(struct socket *);
108 * Nominal space allocated to a raw ip socket.
114 * Raw interface to IP protocol.
118 * Initialize raw connection block queue.
124 ripcbinfo.listhead = &ripcb;
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.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
133 maxsockets, ZONE_INTERRUPT, 0);
137 * XXX ripsrc is modified in rip_input, so we must be fix this
138 * when we want to make this code smp-friendly.
140 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
148 rip_input(struct mbuf *m, int off, int proto)
150 struct ip *ip = mtod(m, struct ip *);
152 struct inpcb *last = NULL;
153 struct mbuf *opts = NULL;
155 ripsrc.sin_addr = ip->ip_src;
156 LIST_FOREACH(inp, &ripcb, inp_list) {
158 if ((inp->inp_vflag & INP_IPV4) == 0)
161 if (inp->inp_ip_p && inp->inp_ip_p != proto)
163 if (inp->inp_laddr.s_addr != INADDR_ANY &&
164 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
166 if (inp->inp_faddr.s_addr != INADDR_ANY &&
167 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
170 struct mbuf *n = m_copypacket(m, M_DONTWAIT);
173 /* check AH/ESP integrity. */
174 if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
176 ipsecstat.in_polvio++;
177 /* do not inject data to pcb */
181 /* check AH/ESP integrity. */
182 if (ipsec4_in_reject(n, last)) {
184 /* do not inject data to pcb */
186 #endif /*FAST_IPSEC*/
188 if (last->inp_flags & INP_CONTROLOPTS ||
189 last->inp_socket->so_options & SO_TIMESTAMP)
190 ip_savecontrol(last, &opts, ip, n);
191 if (sbappendaddr(&last->inp_socket->so_rcv,
192 (struct sockaddr *)&ripsrc, n,
194 /* should notify about lost packet */
199 sorwakeup(last->inp_socket);
206 /* check AH/ESP integrity. */
207 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
209 ipsecstat.in_polvio++;
210 ipstat.ips_delivered--;
211 /* do not inject data to pcb */
215 /* check AH/ESP integrity. */
216 if (last && ipsec4_in_reject(m, last)) {
218 ipstat.ips_delivered--;
219 /* do not inject data to pcb */
221 #endif /*FAST_IPSEC*/
223 if (last->inp_flags & INP_CONTROLOPTS ||
224 last->inp_socket->so_options & SO_TIMESTAMP)
225 ip_savecontrol(last, &opts, ip, m);
226 if (sbappendaddr(&last->inp_socket->so_rcv,
227 (struct sockaddr *)&ripsrc, m, opts) == 0) {
232 sorwakeup(last->inp_socket);
235 ipstat.ips_noproto++;
236 ipstat.ips_delivered--;
241 * Generate IP header and pass packet to ip_output.
242 * Tack on options user may have setup with control call.
245 rip_output(struct mbuf *m, struct socket *so, u_long dst)
248 struct inpcb *inp = sotoinpcb(so);
249 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
252 * If the user handed us a complete IP packet, use it.
253 * Otherwise, allocate an mbuf for a header and fill it in.
255 if ((inp->inp_flags & INP_HDRINCL) == 0) {
256 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
260 M_PREPEND(m, sizeof(struct ip), M_WAIT);
261 ip = mtod(m, struct ip *);
262 ip->ip_tos = inp->inp_ip_tos;
264 ip->ip_p = inp->inp_ip_p;
265 ip->ip_len = m->m_pkthdr.len;
266 ip->ip_src = inp->inp_laddr;
267 ip->ip_dst.s_addr = dst;
268 ip->ip_ttl = inp->inp_ip_ttl;
270 if (m->m_pkthdr.len > IP_MAXPACKET) {
274 ip = mtod(m, struct ip *);
275 /* don't allow both user specified and setsockopt options,
276 and don't allow packet length sizes that will crash */
277 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
279 || (ip->ip_len > m->m_pkthdr.len)
280 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
286 ip->ip_id = ip_randomid();
288 ip->ip_id = htons(ip_id++);
290 /* XXX prevent ip_output from overwriting header fields */
291 flags |= IP_RAWOUTPUT;
295 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
296 inp->inp_moptions, inp));
300 * Raw IP socket option processing.
303 rip_ctloutput(struct socket *so, struct sockopt *sopt)
305 struct inpcb *inp = sotoinpcb(so);
308 if (sopt->sopt_level != IPPROTO_IP)
313 switch (sopt->sopt_dir) {
315 switch (sopt->sopt_name) {
317 optval = inp->inp_flags & INP_HDRINCL;
318 error = sooptcopyout(sopt, &optval, sizeof optval);
321 case IP_FW_ADD: /* ADD actually returns the body... */
324 error = ip_fw_ctl_ptr(sopt);
329 case IP_DUMMYNET_GET:
331 error = ip_dn_ctl_ptr(sopt);
344 case MRT_API_SUPPORT:
346 case MRT_ADD_BW_UPCALL:
347 case MRT_DEL_BW_UPCALL:
348 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
353 error = ip_ctloutput(so, sopt);
359 switch (sopt->sopt_name) {
361 error = sooptcopyin(sopt, &optval, sizeof optval,
366 inp->inp_flags |= INP_HDRINCL;
368 inp->inp_flags &= ~INP_HDRINCL;
377 error = ip_fw_ctl_ptr(sopt);
382 case IP_DUMMYNET_CONFIGURE:
383 case IP_DUMMYNET_DEL:
384 case IP_DUMMYNET_FLUSH:
386 error = ip_dn_ctl_ptr(sopt);
388 error = ENOPROTOOPT ;
392 error = ip_rsvp_init(so);
396 error = ip_rsvp_done();
400 case IP_RSVP_VIF_OFF:
401 error = ip_rsvp_vif ?
402 ip_rsvp_vif(so, sopt) : EINVAL;
413 case MRT_API_SUPPORT:
415 case MRT_ADD_BW_UPCALL:
416 case MRT_DEL_BW_UPCALL:
417 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
422 error = ip_ctloutput(so, sopt);
432 * This function exists solely to receive the PRC_IFDOWN messages which
433 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
434 * and calls in_ifadown() to remove all routes corresponding to that address.
435 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
439 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
441 struct in_ifaddr *ia;
448 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
449 if (ia->ia_ifa.ifa_addr == sa
450 && (ia->ia_flags & IFA_ROUTE)) {
452 * in_ifscrub kills the interface route.
454 in_ifscrub(ia->ia_ifp, ia);
456 * in_ifadown gets rid of all the rest of
457 * the routes. This is not quite the right
458 * thing to do, but at least if we are running
459 * a routing process they will come back.
461 in_ifadown(&ia->ia_ifa, 0);
468 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
469 if (ia->ia_ifa.ifa_addr == sa)
472 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
475 ifp = ia->ia_ifa.ifa_ifp;
477 if ((ifp->if_flags & IFF_LOOPBACK)
478 || (ifp->if_flags & IFF_POINTOPOINT))
481 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
483 ia->ia_flags |= IFA_ROUTE;
488 u_long rip_sendspace = RIPSNDQ;
489 u_long rip_recvspace = RIPRCVQ;
491 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
492 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
493 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
494 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
497 rip_attach(struct socket *so, int proto, struct thread *td)
505 if ((error = suser(td)) != 0)
508 error = soreserve(so, rip_sendspace, rip_recvspace);
512 error = in_pcballoc(so, &ripcbinfo, td);
516 inp = (struct inpcb *)so->so_pcb;
517 inp->inp_vflag |= INP_IPV4;
518 inp->inp_ip_p = proto;
519 inp->inp_ip_ttl = ip_defttl;
524 rip_detach(struct socket *so)
531 if (so == ip_mrouter && ip_mrouter_done)
533 if (ip_rsvp_force_done)
534 ip_rsvp_force_done(so);
542 rip_abort(struct socket *so)
544 soisdisconnected(so);
545 if (so->so_state & SS_NOFDREF)
546 return rip_detach(so);
551 rip_disconnect(struct socket *so)
553 if ((so->so_state & SS_ISCONNECTED) == 0)
555 return rip_abort(so);
559 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
561 struct inpcb *inp = sotoinpcb(so);
562 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
564 if (nam->sa_len != sizeof(*addr))
567 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
568 (addr->sin_family != AF_IMPLINK)) ||
569 (addr->sin_addr.s_addr != INADDR_ANY &&
570 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
571 return EADDRNOTAVAIL;
572 inp->inp_laddr = addr->sin_addr;
577 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
579 struct inpcb *inp = sotoinpcb(so);
580 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
582 if (nam->sa_len != sizeof(*addr))
584 if (TAILQ_EMPTY(&ifnet))
585 return EADDRNOTAVAIL;
586 if ((addr->sin_family != AF_INET) &&
587 (addr->sin_family != AF_IMPLINK))
589 inp->inp_faddr = addr->sin_addr;
595 rip_shutdown(struct socket *so)
602 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
603 struct mbuf *control, struct thread *td)
605 struct inpcb *inp = sotoinpcb(so);
608 if (so->so_state & SS_ISCONNECTED) {
613 dst = inp->inp_faddr.s_addr;
619 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
621 return rip_output(m, so, dst);
625 rip_pcblist(SYSCTL_HANDLER_ARGS)
628 struct inpcb *inp, **inp_list;
633 * The process of preparing the TCB list is too time-consuming and
634 * resource-intensive to repeat twice on every request.
636 if (req->oldptr == 0) {
637 n = ripcbinfo.ipi_count;
638 req->oldidx = 2 * (sizeof xig)
639 + (n + n/8) * sizeof(struct xinpcb);
643 if (req->newptr != 0)
647 * OK, now we're committed to doing something.
650 gencnt = ripcbinfo.ipi_gencnt;
651 n = ripcbinfo.ipi_count;
654 xig.xig_len = sizeof xig;
656 xig.xig_gen = gencnt;
657 xig.xig_sogen = so_gencnt;
658 error = SYSCTL_OUT(req, &xig, sizeof xig);
662 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
667 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
668 inp = LIST_NEXT(inp, inp_list)) {
669 if (inp->inp_gencnt <= gencnt)
676 for (i = 0; i < n; i++) {
678 if (inp->inp_gencnt <= gencnt) {
680 xi.xi_len = sizeof xi;
681 /* XXX should avoid extra copy */
682 bcopy(inp, &xi.xi_inp, sizeof *inp);
684 sotoxsocket(inp->inp_socket, &xi.xi_socket);
685 error = SYSCTL_OUT(req, &xi, sizeof xi);
690 * Give the user an updated idea of our state.
691 * If the generation differs from what we told
692 * her before, she knows that something happened
693 * while we were processing this request, and it
694 * might be necessary to retry.
697 xig.xig_gen = ripcbinfo.ipi_gencnt;
698 xig.xig_sogen = so_gencnt;
699 xig.xig_count = ripcbinfo.ipi_count;
701 error = SYSCTL_OUT(req, &xig, sizeof xig);
703 free(inp_list, M_TEMP);
707 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
708 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
710 struct pr_usrreqs rip_usrreqs = {
711 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
712 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
713 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
714 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
715 in_setsockaddr, sosend, soreceive, sopoll