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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
39 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_mroute.h>
68 #include <netinet/ip_fw.h>
69 #include <netinet/ip_dummynet.h>
72 #include <netipsec/ipsec.h>
75 #include <security/mac/mac_framework.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 = NULL;
82 ip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
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);
103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104 void (*ip_rsvp_force_done)(struct socket *);
107 * Raw interface to IP protocol.
111 * Initialize raw connection block q.
114 rip_zone_change(void *tag)
117 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
121 rip_inpcb_init(void *mem, int size, int flags)
123 struct inpcb *inp = mem;
125 INP_LOCK_INIT(inp, "inp", "rawinp");
133 INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
135 ripcbinfo.ipi_listhead = &ripcb;
137 * XXX We don't use the hash list for raw IP, but it's easier
138 * to allocate a one entry hash list than it is to check all
139 * over the place for hashbase == NULL.
141 ripcbinfo.ipi_hashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_hashmask);
142 ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB,
143 &ripcbinfo.ipi_porthashmask);
144 ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
145 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
146 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
147 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
148 NULL, EVENTHANDLER_PRI_ANY);
151 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
154 raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
158 INP_LOCK_ASSERT(last);
161 /* check AH/ESP integrity. */
162 if (ipsec4_in_reject(n, last)) {
167 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
170 /* Check the minimum TTL for socket. */
171 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
174 struct mbuf *opts = NULL;
177 so = last->inp_socket;
178 if ((last->inp_flags & INP_CONTROLOPTS) ||
179 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
180 ip_savecontrol(last, &opts, ip, n);
181 SOCKBUF_LOCK(&so->so_rcv);
182 if (sbappendaddr_locked(&so->so_rcv,
183 (struct sockaddr *)&ripsrc, n, opts) == 0) {
184 /* should notify about lost packet */
188 SOCKBUF_UNLOCK(&so->so_rcv);
190 sorwakeup_locked(so);
197 * Setup generic address and protocol structures
198 * for raw_input routine, then pass them along with
202 rip_input(struct mbuf *m, int off)
204 struct ip *ip = mtod(m, struct ip *);
205 int proto = ip->ip_p;
206 struct inpcb *inp, *last;
208 INP_INFO_RLOCK(&ripcbinfo);
209 ripsrc.sin_addr = ip->ip_src;
211 LIST_FOREACH(inp, &ripcb, inp_list) {
213 if (inp->inp_ip_p && inp->inp_ip_p != proto) {
219 if ((inp->inp_vflag & INP_IPV4) == 0)
222 if (inp->inp_laddr.s_addr &&
223 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
225 if (inp->inp_faddr.s_addr &&
226 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
228 if (jailed(inp->inp_socket->so_cred))
229 if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
235 n = m_copy(m, 0, (int)M_COPYALL);
237 (void) raw_append(last, ip, n);
238 /* XXX count dropped packet */
244 if (raw_append(last, ip, m) != 0)
245 ipstat.ips_delivered--;
249 ipstat.ips_noproto++;
250 ipstat.ips_delivered--;
252 INP_INFO_RUNLOCK(&ripcbinfo);
256 * Generate IP header and pass packet to ip_output.
257 * Tack on options user may have setup with control call.
260 rip_output(struct mbuf *m, struct socket *so, u_long dst)
264 struct inpcb *inp = sotoinpcb(so);
265 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
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), M_DONTWAIT);
282 ip = mtod(m, struct ip *);
283 ip->ip_tos = inp->inp_ip_tos;
284 if (inp->inp_flags & INP_DONTFRAG)
288 ip->ip_p = inp->inp_ip_p;
289 ip->ip_len = m->m_pkthdr.len;
290 if (jailed(inp->inp_socket->so_cred))
292 htonl(prison_getip(inp->inp_socket->so_cred));
294 ip->ip_src = inp->inp_laddr;
295 ip->ip_dst.s_addr = dst;
296 ip->ip_ttl = inp->inp_ip_ttl;
298 if (m->m_pkthdr.len > IP_MAXPACKET) {
303 ip = mtod(m, struct ip *);
304 if (jailed(inp->inp_socket->so_cred)) {
305 if (ip->ip_src.s_addr !=
306 htonl(prison_getip(inp->inp_socket->so_cred))) {
312 /* don't allow both user specified and setsockopt options,
313 and don't allow packet length sizes that will crash */
314 if (((ip->ip_hl != (sizeof (*ip) >> 2))
316 || (ip->ip_len > m->m_pkthdr.len)
317 || (ip->ip_len < (ip->ip_hl << 2))) {
323 ip->ip_id = ip_newid();
324 /* XXX prevent ip_output from overwriting header fields */
325 flags |= IP_RAWOUTPUT;
329 if (inp->inp_flags & INP_ONESBCAST)
330 flags |= IP_SENDONES;
333 mac_inpcb_create_mbuf(inp, m);
336 error = ip_output(m, inp->inp_options, NULL, flags,
337 inp->inp_moptions, inp);
343 * Raw IP socket option processing.
345 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
346 * only be created by a privileged process, and as such, socket option
347 * operations to manage system properties on any raw socket were allowed to
348 * take place without explicit additional access control checks. However,
349 * raw sockets can now also be created in jail(), and therefore explicit
350 * checks are now required. Likewise, raw sockets can be used by a process
351 * after it gives up privilege, so some caution is required. For options
352 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
353 * performed in ip_ctloutput() and therefore no check occurs here.
354 * Unilaterally checking priv_check() here breaks normal IP socket option
355 * operations on raw sockets.
357 * When adding new socket options here, make sure to add access control
358 * checks here as necessary.
361 rip_ctloutput(struct socket *so, struct sockopt *sopt)
363 struct inpcb *inp = sotoinpcb(so);
366 if (sopt->sopt_level != IPPROTO_IP)
370 switch (sopt->sopt_dir) {
372 switch (sopt->sopt_name) {
374 optval = inp->inp_flags & INP_HDRINCL;
375 error = sooptcopyout(sopt, &optval, sizeof optval);
378 case IP_FW_ADD: /* ADD actually returns the body... */
380 case IP_FW_TABLE_GETSIZE:
381 case IP_FW_TABLE_LIST:
382 case IP_FW_NAT_GET_CONFIG:
383 case IP_FW_NAT_GET_LOG:
385 * XXXRW: Isn't this checked one layer down? Yes, it
388 error = priv_check(curthread, PRIV_NETINET_IPFW);
391 if (ip_fw_ctl_ptr != NULL)
392 error = ip_fw_ctl_ptr(sopt);
397 case IP_DUMMYNET_GET:
398 error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
401 if (ip_dn_ctl_ptr != NULL)
402 error = ip_dn_ctl_ptr(sopt);
415 case MRT_API_SUPPORT:
417 case MRT_ADD_BW_UPCALL:
418 case MRT_DEL_BW_UPCALL:
419 error = priv_check(curthread, PRIV_NETINET_MROUTE);
422 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
427 error = ip_ctloutput(so, sopt);
433 switch (sopt->sopt_name) {
435 error = sooptcopyin(sopt, &optval, sizeof optval,
440 inp->inp_flags |= INP_HDRINCL;
442 inp->inp_flags &= ~INP_HDRINCL;
450 case IP_FW_TABLE_ADD:
451 case IP_FW_TABLE_DEL:
452 case IP_FW_TABLE_FLUSH:
456 * XXXRW: Isn't this checked one layer down?
458 error = priv_check(curthread, PRIV_NETINET_IPFW);
461 if (ip_fw_ctl_ptr != NULL)
462 error = ip_fw_ctl_ptr(sopt);
467 case IP_DUMMYNET_CONFIGURE:
468 case IP_DUMMYNET_DEL:
469 case IP_DUMMYNET_FLUSH:
470 error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
473 if (ip_dn_ctl_ptr != NULL)
474 error = ip_dn_ctl_ptr(sopt);
476 error = ENOPROTOOPT ;
480 error = priv_check(curthread, PRIV_NETINET_MROUTE);
483 error = ip_rsvp_init(so);
487 error = priv_check(curthread, PRIV_NETINET_MROUTE);
490 error = ip_rsvp_done();
494 case IP_RSVP_VIF_OFF:
495 error = priv_check(curthread, PRIV_NETINET_MROUTE);
498 error = ip_rsvp_vif ?
499 ip_rsvp_vif(so, sopt) : EINVAL;
510 case MRT_API_SUPPORT:
512 case MRT_ADD_BW_UPCALL:
513 case MRT_DEL_BW_UPCALL:
514 error = priv_check(curthread, PRIV_NETINET_MROUTE);
517 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
522 error = ip_ctloutput(so, sopt);
532 * This function exists solely to receive the PRC_IFDOWN messages which
533 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
534 * and calls in_ifadown() to remove all routes corresponding to that address.
535 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
539 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
541 struct in_ifaddr *ia;
548 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
549 if (ia->ia_ifa.ifa_addr == sa
550 && (ia->ia_flags & IFA_ROUTE)) {
552 * in_ifscrub kills the interface route.
554 in_ifscrub(ia->ia_ifp, ia);
556 * in_ifadown gets rid of all the rest of
557 * the routes. This is not quite the right
558 * thing to do, but at least if we are running
559 * a routing process they will come back.
561 in_ifadown(&ia->ia_ifa, 0);
568 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
569 if (ia->ia_ifa.ifa_addr == sa)
572 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
575 ifp = ia->ia_ifa.ifa_ifp;
577 if ((ifp->if_flags & IFF_LOOPBACK)
578 || (ifp->if_flags & IFF_POINTOPOINT))
581 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
583 ia->ia_flags |= IFA_ROUTE;
588 u_long rip_sendspace = 9216;
589 u_long rip_recvspace = 9216;
591 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
592 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
593 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
594 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
597 rip_attach(struct socket *so, int proto, struct thread *td)
603 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
605 error = priv_check(td, PRIV_NETINET_RAW);
608 if (proto >= IPPROTO_MAX || proto < 0)
609 return EPROTONOSUPPORT;
610 error = soreserve(so, rip_sendspace, rip_recvspace);
613 INP_INFO_WLOCK(&ripcbinfo);
614 error = in_pcballoc(so, &ripcbinfo);
616 INP_INFO_WUNLOCK(&ripcbinfo);
619 inp = (struct inpcb *)so->so_pcb;
620 INP_INFO_WUNLOCK(&ripcbinfo);
621 inp->inp_vflag |= INP_IPV4;
622 inp->inp_ip_p = proto;
623 inp->inp_ip_ttl = ip_defttl;
629 rip_detach(struct socket *so)
634 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
635 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
636 ("rip_detach: not closed"));
638 INP_INFO_WLOCK(&ripcbinfo);
640 if (so == ip_mrouter && ip_mrouter_done)
642 if (ip_rsvp_force_done)
643 ip_rsvp_force_done(so);
648 INP_INFO_WUNLOCK(&ripcbinfo);
652 rip_dodisconnect(struct socket *so, struct inpcb *inp)
655 INP_LOCK_ASSERT(inp);
657 inp->inp_faddr.s_addr = INADDR_ANY;
659 so->so_state &= ~SS_ISCONNECTED;
664 rip_abort(struct socket *so)
669 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
671 INP_INFO_WLOCK(&ripcbinfo);
673 rip_dodisconnect(so, inp);
675 INP_INFO_WUNLOCK(&ripcbinfo);
679 rip_close(struct socket *so)
684 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
686 INP_INFO_WLOCK(&ripcbinfo);
688 rip_dodisconnect(so, inp);
690 INP_INFO_WUNLOCK(&ripcbinfo);
694 rip_disconnect(struct socket *so)
698 if ((so->so_state & SS_ISCONNECTED) == 0)
702 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
703 INP_INFO_WLOCK(&ripcbinfo);
705 rip_dodisconnect(so, inp);
707 INP_INFO_WUNLOCK(&ripcbinfo);
712 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
714 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
717 if (nam->sa_len != sizeof(*addr))
720 if (jailed(td->td_ucred)) {
721 if (addr->sin_addr.s_addr == INADDR_ANY)
722 addr->sin_addr.s_addr =
723 htonl(prison_getip(td->td_ucred));
724 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
725 return (EADDRNOTAVAIL);
728 if (TAILQ_EMPTY(&ifnet) ||
729 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
730 (addr->sin_addr.s_addr &&
731 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
732 return EADDRNOTAVAIL;
735 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
736 INP_INFO_WLOCK(&ripcbinfo);
738 inp->inp_laddr = addr->sin_addr;
740 INP_INFO_WUNLOCK(&ripcbinfo);
745 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
747 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
750 if (nam->sa_len != sizeof(*addr))
752 if (TAILQ_EMPTY(&ifnet))
753 return EADDRNOTAVAIL;
754 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
758 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
759 INP_INFO_WLOCK(&ripcbinfo);
761 inp->inp_faddr = addr->sin_addr;
764 INP_INFO_WUNLOCK(&ripcbinfo);
769 rip_shutdown(struct socket *so)
774 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
782 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
783 struct mbuf *control, struct thread *td)
789 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
791 * Note: 'dst' reads below are unlocked.
793 if (so->so_state & SS_ISCONNECTED) {
798 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
804 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
806 return rip_output(m, so, dst);
810 rip_pcblist(SYSCTL_HANDLER_ARGS)
813 struct inpcb *inp, **inp_list;
818 * The process of preparing the TCB list is too time-consuming and
819 * resource-intensive to repeat twice on every request.
821 if (req->oldptr == 0) {
822 n = ripcbinfo.ipi_count;
823 req->oldidx = 2 * (sizeof xig)
824 + (n + n/8) * sizeof(struct xinpcb);
828 if (req->newptr != 0)
832 * OK, now we're committed to doing something.
834 INP_INFO_RLOCK(&ripcbinfo);
835 gencnt = ripcbinfo.ipi_gencnt;
836 n = ripcbinfo.ipi_count;
837 INP_INFO_RUNLOCK(&ripcbinfo);
839 xig.xig_len = sizeof xig;
841 xig.xig_gen = gencnt;
842 xig.xig_sogen = so_gencnt;
843 error = SYSCTL_OUT(req, &xig, sizeof xig);
847 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
851 INP_INFO_RLOCK(&ripcbinfo);
852 for (inp = LIST_FIRST(ripcbinfo.ipi_listhead), i = 0; inp && i < n;
853 inp = LIST_NEXT(inp, inp_list)) {
855 if (inp->inp_gencnt <= gencnt &&
856 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
857 /* XXX held references? */
862 INP_INFO_RUNLOCK(&ripcbinfo);
866 for (i = 0; i < n; i++) {
869 if (inp->inp_gencnt <= gencnt) {
871 bzero(&xi, sizeof(xi));
872 xi.xi_len = sizeof xi;
873 /* XXX should avoid extra copy */
874 bcopy(inp, &xi.xi_inp, sizeof *inp);
876 sotoxsocket(inp->inp_socket, &xi.xi_socket);
878 error = SYSCTL_OUT(req, &xi, sizeof xi);
884 * Give the user an updated idea of our state.
885 * If the generation differs from what we told
886 * her before, she knows that something happened
887 * while we were processing this request, and it
888 * might be necessary to retry.
890 INP_INFO_RLOCK(&ripcbinfo);
891 xig.xig_gen = ripcbinfo.ipi_gencnt;
892 xig.xig_sogen = so_gencnt;
893 xig.xig_count = ripcbinfo.ipi_count;
894 INP_INFO_RUNLOCK(&ripcbinfo);
895 error = SYSCTL_OUT(req, &xig, sizeof xig);
897 free(inp_list, M_TEMP);
901 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
902 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
904 struct pr_usrreqs rip_usrreqs = {
905 .pru_abort = rip_abort,
906 .pru_attach = rip_attach,
907 .pru_bind = rip_bind,
908 .pru_connect = rip_connect,
909 .pru_control = in_control,
910 .pru_detach = rip_detach,
911 .pru_disconnect = rip_disconnect,
912 .pru_peeraddr = in_getpeeraddr,
913 .pru_send = rip_send,
914 .pru_shutdown = rip_shutdown,
915 .pru_sockaddr = in_getsockaddr,
916 .pru_sosetlabel = in_pcbsosetlabel,
917 .pru_close = rip_close,