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
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
37 #include <sys/param.h>
39 #include <sys/kernel.h>
42 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.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 = 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 * Nominal space allocated to a raw ip socket.
113 * Raw interface to IP protocol.
117 * Initialize raw connection block q.
120 rip_zone_change(void *tag)
123 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
129 INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
131 ripcbinfo.listhead = &ripcb;
133 * XXX We don't use the hash list for raw IP, but it's easier
134 * to allocate a one entry hash list than it is to check all
135 * over the place for hashbase == NULL.
137 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
138 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
139 ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
140 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
141 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
142 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
143 NULL, EVENTHANDLER_PRI_ANY);
146 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
149 raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
153 INP_LOCK_ASSERT(last);
155 #if defined(IPSEC) || defined(FAST_IPSEC)
156 /* check AH/ESP integrity. */
157 if (ipsec4_in_reject(n, last)) {
160 ipsecstat.in_polvio++;
162 /* do not inject data to pcb */
164 #endif /*IPSEC || FAST_IPSEC*/
166 if (!policyfail && mac_check_inpcb_deliver(last, n) != 0)
169 /* Check the minimum TTL for socket. */
170 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
173 struct mbuf *opts = NULL;
176 so = last->inp_socket;
177 if ((last->inp_flags & INP_CONTROLOPTS) ||
178 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
179 ip_savecontrol(last, &opts, ip, n);
180 SOCKBUF_LOCK(&so->so_rcv);
181 if (sbappendaddr_locked(&so->so_rcv,
182 (struct sockaddr *)&ripsrc, n, opts) == 0) {
183 /* should notify about lost packet */
187 SOCKBUF_UNLOCK(&so->so_rcv);
189 sorwakeup_locked(so);
196 * Setup generic address and protocol structures
197 * for raw_input routine, then pass them along with
201 rip_input(struct mbuf *m, int off)
203 struct ip *ip = mtod(m, struct ip *);
204 int proto = ip->ip_p;
205 struct inpcb *inp, *last;
207 INP_INFO_RLOCK(&ripcbinfo);
208 ripsrc.sin_addr = ip->ip_src;
210 LIST_FOREACH(inp, &ripcb, inp_list) {
212 if (inp->inp_ip_p && inp->inp_ip_p != proto) {
218 if ((inp->inp_vflag & INP_IPV4) == 0)
221 if (inp->inp_laddr.s_addr &&
222 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
224 if (inp->inp_faddr.s_addr &&
225 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
227 if (jailed(inp->inp_socket->so_cred))
228 if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
234 n = m_copy(m, 0, (int)M_COPYALL);
236 (void) raw_append(last, ip, n);
237 /* XXX count dropped packet */
243 if (raw_append(last, ip, m) != 0)
244 ipstat.ips_delivered--;
248 ipstat.ips_noproto++;
249 ipstat.ips_delivered--;
251 INP_INFO_RUNLOCK(&ripcbinfo);
255 * Generate IP header and pass packet to ip_output.
256 * Tack on options user may have setup with control call.
259 rip_output(struct mbuf *m, struct socket *so, u_long dst)
263 struct inpcb *inp = sotoinpcb(so);
264 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
268 * If the user handed us a complete IP packet, use it.
269 * Otherwise, allocate an mbuf for a header and fill it in.
271 if ((inp->inp_flags & INP_HDRINCL) == 0) {
272 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
276 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
281 ip = mtod(m, struct ip *);
282 ip->ip_tos = inp->inp_ip_tos;
283 if (inp->inp_flags & INP_DONTFRAG)
287 ip->ip_p = inp->inp_ip_p;
288 ip->ip_len = m->m_pkthdr.len;
289 if (jailed(inp->inp_socket->so_cred))
291 htonl(prison_getip(inp->inp_socket->so_cred));
293 ip->ip_src = inp->inp_laddr;
294 ip->ip_dst.s_addr = dst;
295 ip->ip_ttl = inp->inp_ip_ttl;
297 if (m->m_pkthdr.len > IP_MAXPACKET) {
302 ip = mtod(m, struct ip *);
303 if (jailed(inp->inp_socket->so_cred)) {
304 if (ip->ip_src.s_addr !=
305 htonl(prison_getip(inp->inp_socket->so_cred))) {
311 /* don't allow both user specified and setsockopt options,
312 and don't allow packet length sizes that will crash */
313 if (((ip->ip_hl != (sizeof (*ip) >> 2))
315 || (ip->ip_len > m->m_pkthdr.len)
316 || (ip->ip_len < (ip->ip_hl << 2))) {
322 ip->ip_id = ip_newid();
323 /* XXX prevent ip_output from overwriting header fields */
324 flags |= IP_RAWOUTPUT;
328 if (inp->inp_vflag & INP_ONESBCAST)
329 flags |= IP_SENDONES;
332 mac_create_mbuf_from_inpcb(inp, m);
335 error = ip_output(m, inp->inp_options, NULL, flags,
336 inp->inp_moptions, inp);
342 * Raw IP socket option processing.
344 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
345 * only be created by a privileged process, and as such, socket option
346 * operations to manage system properties on any raw socket were allowed to
347 * take place without explicit additional access control checks. However,
348 * raw sockets can now also be created in jail(), and therefore explicit
349 * checks are now required. Likewise, raw sockets can be used by a process
350 * after it gives up privilege, so some caution is required. For options
351 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
352 * performed in ip_ctloutput() and therefore no check occurs here.
353 * Unilaterally checking suser() here breaks normal IP socket option
354 * operations on raw sockets.
356 * When adding new socket options here, make sure to add access control
357 * checks here as necessary.
360 rip_ctloutput(struct socket *so, struct sockopt *sopt)
362 struct inpcb *inp = sotoinpcb(so);
365 if (sopt->sopt_level != IPPROTO_IP)
369 switch (sopt->sopt_dir) {
371 switch (sopt->sopt_name) {
373 optval = inp->inp_flags & INP_HDRINCL;
374 error = sooptcopyout(sopt, &optval, sizeof optval);
377 case IP_FW_ADD: /* ADD actually returns the body... */
379 case IP_FW_TABLE_GETSIZE:
380 case IP_FW_TABLE_LIST:
381 error = suser(curthread);
384 if (ip_fw_ctl_ptr != NULL)
385 error = ip_fw_ctl_ptr(sopt);
390 case IP_DUMMYNET_GET:
391 error = suser(curthread);
394 if (ip_dn_ctl_ptr != NULL)
395 error = ip_dn_ctl_ptr(sopt);
408 case MRT_API_SUPPORT:
410 case MRT_ADD_BW_UPCALL:
411 case MRT_DEL_BW_UPCALL:
412 error = suser(curthread);
415 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
420 error = ip_ctloutput(so, sopt);
426 switch (sopt->sopt_name) {
428 error = sooptcopyin(sopt, &optval, sizeof optval,
433 inp->inp_flags |= INP_HDRINCL;
435 inp->inp_flags &= ~INP_HDRINCL;
443 case IP_FW_TABLE_ADD:
444 case IP_FW_TABLE_DEL:
445 case IP_FW_TABLE_FLUSH:
446 error = suser(curthread);
449 if (ip_fw_ctl_ptr != NULL)
450 error = ip_fw_ctl_ptr(sopt);
455 case IP_DUMMYNET_CONFIGURE:
456 case IP_DUMMYNET_DEL:
457 case IP_DUMMYNET_FLUSH:
458 error = suser(curthread);
461 if (ip_dn_ctl_ptr != NULL)
462 error = ip_dn_ctl_ptr(sopt);
464 error = ENOPROTOOPT ;
468 error = suser(curthread);
471 error = ip_rsvp_init(so);
475 error = suser(curthread);
478 error = ip_rsvp_done();
482 case IP_RSVP_VIF_OFF:
483 error = suser(curthread);
486 error = ip_rsvp_vif ?
487 ip_rsvp_vif(so, sopt) : EINVAL;
498 case MRT_API_SUPPORT:
500 case MRT_ADD_BW_UPCALL:
501 case MRT_DEL_BW_UPCALL:
502 error = suser(curthread);
505 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
510 error = ip_ctloutput(so, sopt);
520 * This function exists solely to receive the PRC_IFDOWN messages which
521 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
522 * and calls in_ifadown() to remove all routes corresponding to that address.
523 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
527 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
529 struct in_ifaddr *ia;
536 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
537 if (ia->ia_ifa.ifa_addr == sa
538 && (ia->ia_flags & IFA_ROUTE)) {
540 * in_ifscrub kills the interface route.
542 in_ifscrub(ia->ia_ifp, ia);
544 * in_ifadown gets rid of all the rest of
545 * the routes. This is not quite the right
546 * thing to do, but at least if we are running
547 * a routing process they will come back.
549 in_ifadown(&ia->ia_ifa, 0);
556 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
557 if (ia->ia_ifa.ifa_addr == sa)
560 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
563 ifp = ia->ia_ifa.ifa_ifp;
565 if ((ifp->if_flags & IFF_LOOPBACK)
566 || (ifp->if_flags & IFF_POINTOPOINT))
569 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
571 ia->ia_flags |= IFA_ROUTE;
576 u_long rip_sendspace = RIPSNDQ;
577 u_long rip_recvspace = RIPRCVQ;
579 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
580 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
581 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
582 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
585 rip_attach(struct socket *so, int proto, struct thread *td)
591 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
592 if (jailed(td->td_ucred) && !jail_allow_raw_sockets)
594 if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) != 0)
596 if (proto >= IPPROTO_MAX || proto < 0)
597 return EPROTONOSUPPORT;
598 error = soreserve(so, rip_sendspace, rip_recvspace);
601 INP_INFO_WLOCK(&ripcbinfo);
602 error = in_pcballoc(so, &ripcbinfo, "rawinp");
604 INP_INFO_WUNLOCK(&ripcbinfo);
607 inp = (struct inpcb *)so->so_pcb;
609 INP_INFO_WUNLOCK(&ripcbinfo);
610 inp->inp_vflag |= INP_IPV4;
611 inp->inp_ip_p = proto;
612 inp->inp_ip_ttl = ip_defttl;
618 rip_pcbdetach(struct socket *so, struct inpcb *inp)
621 INP_INFO_WLOCK_ASSERT(&ripcbinfo);
622 INP_LOCK_ASSERT(inp);
624 if (so == ip_mrouter && ip_mrouter_done)
626 if (ip_rsvp_force_done)
627 ip_rsvp_force_done(so);
635 rip_detach(struct socket *so)
640 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
641 INP_INFO_WLOCK(&ripcbinfo);
643 rip_pcbdetach(so, inp);
644 INP_INFO_WUNLOCK(&ripcbinfo);
648 rip_abort(struct socket *so)
653 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
654 INP_INFO_WLOCK(&ripcbinfo);
656 soisdisconnected(so);
657 rip_pcbdetach(so, inp);
658 INP_INFO_WUNLOCK(&ripcbinfo);
662 rip_disconnect(struct socket *so)
666 if ((so->so_state & SS_ISCONNECTED) == 0)
670 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
671 INP_INFO_WLOCK(&ripcbinfo);
673 inp->inp_faddr.s_addr = INADDR_ANY;
675 so->so_state &= ~SS_ISCONNECTED;
678 INP_INFO_WUNLOCK(&ripcbinfo);
683 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
685 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
688 if (nam->sa_len != sizeof(*addr))
691 if (jailed(td->td_ucred)) {
692 if (addr->sin_addr.s_addr == INADDR_ANY)
693 addr->sin_addr.s_addr =
694 htonl(prison_getip(td->td_ucred));
695 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
696 return (EADDRNOTAVAIL);
699 if (TAILQ_EMPTY(&ifnet) ||
700 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
701 (addr->sin_addr.s_addr &&
702 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
703 return EADDRNOTAVAIL;
706 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
707 INP_INFO_WLOCK(&ripcbinfo);
709 inp->inp_laddr = addr->sin_addr;
711 INP_INFO_WUNLOCK(&ripcbinfo);
716 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
718 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
721 if (nam->sa_len != sizeof(*addr))
723 if (TAILQ_EMPTY(&ifnet))
724 return EADDRNOTAVAIL;
725 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
729 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
730 INP_INFO_WLOCK(&ripcbinfo);
732 inp->inp_faddr = addr->sin_addr;
735 INP_INFO_WUNLOCK(&ripcbinfo);
740 rip_shutdown(struct socket *so)
745 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
753 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
754 struct mbuf *control, struct thread *td)
760 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
762 * Note: 'dst' reads below are unlocked.
764 if (so->so_state & SS_ISCONNECTED) {
769 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
775 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
777 return rip_output(m, so, dst);
781 rip_pcblist(SYSCTL_HANDLER_ARGS)
784 struct inpcb *inp, **inp_list;
789 * The process of preparing the TCB list is too time-consuming and
790 * resource-intensive to repeat twice on every request.
792 if (req->oldptr == 0) {
793 n = ripcbinfo.ipi_count;
794 req->oldidx = 2 * (sizeof xig)
795 + (n + n/8) * sizeof(struct xinpcb);
799 if (req->newptr != 0)
803 * OK, now we're committed to doing something.
805 INP_INFO_RLOCK(&ripcbinfo);
806 gencnt = ripcbinfo.ipi_gencnt;
807 n = ripcbinfo.ipi_count;
808 INP_INFO_RUNLOCK(&ripcbinfo);
810 xig.xig_len = sizeof xig;
812 xig.xig_gen = gencnt;
813 xig.xig_sogen = so_gencnt;
814 error = SYSCTL_OUT(req, &xig, sizeof xig);
818 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
822 INP_INFO_RLOCK(&ripcbinfo);
823 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
824 inp = LIST_NEXT(inp, inp_list)) {
826 if (inp->inp_gencnt <= gencnt &&
827 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
828 /* XXX held references? */
833 INP_INFO_RUNLOCK(&ripcbinfo);
837 for (i = 0; i < n; i++) {
839 if (inp->inp_gencnt <= gencnt) {
841 bzero(&xi, sizeof(xi));
842 xi.xi_len = sizeof xi;
843 /* XXX should avoid extra copy */
844 bcopy(inp, &xi.xi_inp, sizeof *inp);
846 sotoxsocket(inp->inp_socket, &xi.xi_socket);
847 error = SYSCTL_OUT(req, &xi, sizeof xi);
852 * Give the user an updated idea of our state.
853 * If the generation differs from what we told
854 * her before, she knows that something happened
855 * while we were processing this request, and it
856 * might be necessary to retry.
858 INP_INFO_RLOCK(&ripcbinfo);
859 xig.xig_gen = ripcbinfo.ipi_gencnt;
860 xig.xig_sogen = so_gencnt;
861 xig.xig_count = ripcbinfo.ipi_count;
862 INP_INFO_RUNLOCK(&ripcbinfo);
863 error = SYSCTL_OUT(req, &xig, sizeof xig);
865 free(inp_list, M_TEMP);
870 * This is the wrapper function for in_setsockaddr. We just pass down
871 * the pcbinfo for in_setpeeraddr to lock.
874 rip_sockaddr(struct socket *so, struct sockaddr **nam)
876 return (in_setsockaddr(so, nam, &ripcbinfo));
880 * This is the wrapper function for in_setpeeraddr. We just pass down
881 * the pcbinfo for in_setpeeraddr to lock.
884 rip_peeraddr(struct socket *so, struct sockaddr **nam)
886 return (in_setpeeraddr(so, nam, &ripcbinfo));
890 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
891 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
893 struct pr_usrreqs rip_usrreqs = {
894 .pru_abort = rip_abort,
895 .pru_attach = rip_attach,
896 .pru_bind = rip_bind,
897 .pru_connect = rip_connect,
898 .pru_control = in_control,
899 .pru_detach = rip_detach,
900 .pru_disconnect = rip_disconnect,
901 .pru_peeraddr = rip_peeraddr,
902 .pru_send = rip_send,
903 .pru_shutdown = rip_shutdown,
904 .pru_sockaddr = rip_sockaddr,
905 .pru_sosetlabel = in_pcbsosetlabel