2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
51 #include <sys/protosw.h>
52 #include <sys/rmlock.h>
53 #include <sys/rwlock.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
64 #include <net/if_var.h>
65 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_mroute.h>
76 #include <netinet/ip_icmp.h>
78 #include <netipsec/ipsec_support.h>
80 #include <machine/stdarg.h>
81 #include <security/mac/mac_framework.h>
83 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
84 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
85 &VNET_NAME(ip_defttl), 0,
86 "Maximum TTL on IP packets");
88 VNET_DEFINE(struct inpcbhead, ripcb);
89 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
91 #define V_ripcb VNET(ripcb)
92 #define V_ripcbinfo VNET(ripcbinfo)
95 * Control and data hooks for ipfw, dummynet, divert and so on.
96 * The data hooks are not used here but it is convenient
97 * to keep them all in one place.
99 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
100 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
102 int (*ip_dn_ctl_ptr)(struct sockopt *);
103 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
104 void (*ip_divert_ptr)(struct mbuf *, bool);
105 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
109 * Hooks for multicast routing. They all default to NULL, so leave them not
110 * initialized and rely on BSS being set to 0.
114 * The socket used to communicate with the multicast routing daemon.
116 VNET_DEFINE(struct socket *, ip_mrouter);
119 * The various mrouter and rsvp functions.
121 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
123 int (*ip_mrouter_done)(void);
124 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
125 struct ip_moptions *);
126 int (*mrt_ioctl)(u_long, caddr_t, int);
127 int (*legal_vif_num)(int);
128 u_long (*ip_mcast_src)(int);
130 int (*rsvp_input_p)(struct mbuf **, int *, int);
131 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
132 void (*ip_rsvp_force_done)(struct socket *);
135 extern struct protosw inetsw[];
137 u_long rip_sendspace = 9216;
138 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
139 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
141 u_long rip_recvspace = 9216;
142 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
143 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
149 #define INP_PCBHASH_RAW_SIZE 256
150 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
151 (((proto) + (laddr) + (faddr)) % (mask) + 1)
155 rip_inshash(struct inpcb *inp)
157 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
158 struct inpcbhead *pcbhash;
161 INP_INFO_WLOCK_ASSERT(pcbinfo);
162 INP_WLOCK_ASSERT(inp);
164 if (inp->inp_ip_p != 0 &&
165 inp->inp_laddr.s_addr != INADDR_ANY &&
166 inp->inp_faddr.s_addr != INADDR_ANY) {
167 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
168 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
171 pcbhash = &pcbinfo->ipi_hashbase[hash];
172 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
176 rip_delhash(struct inpcb *inp)
179 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
180 INP_WLOCK_ASSERT(inp);
182 CK_LIST_REMOVE(inp, inp_hash);
187 * Raw interface to IP protocol.
191 * Initialize raw connection block q.
194 rip_zone_change(void *tag)
197 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
201 rip_inpcb_init(void *mem, int size, int flags)
203 struct inpcb *inp = mem;
205 INP_LOCK_INIT(inp, "inp", "rawinp");
213 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
214 1, "ripcb", rip_inpcb_init, IPI_HASHFIELDS_NONE);
215 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
216 EVENTHANDLER_PRI_ANY);
221 rip_destroy(void *unused __unused)
224 in_pcbinfo_destroy(&V_ripcbinfo);
226 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
231 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
232 struct sockaddr_in *ripsrc)
236 INP_LOCK_ASSERT(last);
238 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
239 /* check AH/ESP integrity. */
240 if (IPSEC_ENABLED(ipv4)) {
241 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
246 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
249 /* Check the minimum TTL for socket. */
250 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
253 struct mbuf *opts = NULL;
256 so = last->inp_socket;
257 if ((last->inp_flags & INP_CONTROLOPTS) ||
258 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
259 ip_savecontrol(last, &opts, ip, n);
260 SOCKBUF_LOCK(&so->so_rcv);
261 if (sbappendaddr_locked(&so->so_rcv,
262 (struct sockaddr *)ripsrc, n, opts) == 0) {
263 /* should notify about lost packet */
267 SOCKBUF_UNLOCK(&so->so_rcv);
269 sorwakeup_locked(so);
276 * Setup generic address and protocol structures for raw_input routine, then
277 * pass them along with mbuf chain.
280 rip_input(struct mbuf **mp, int *offp, int proto)
283 struct mbuf *m = *mp;
284 struct ip *ip = mtod(m, struct ip *);
285 struct inpcb *inp, *last;
286 struct sockaddr_in ripsrc;
287 struct epoch_tracker et;
292 bzero(&ripsrc, sizeof(ripsrc));
293 ripsrc.sin_len = sizeof(ripsrc);
294 ripsrc.sin_family = AF_INET;
295 ripsrc.sin_addr = ip->ip_src;
298 ifp = m->m_pkthdr.rcvif;
300 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
301 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
302 INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
303 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
304 if (inp->inp_ip_p != proto)
307 /* XXX inp locking */
308 if ((inp->inp_vflag & INP_IPV4) == 0)
311 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
313 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
318 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
320 (void) rip_append(last, ip, n, &ripsrc);
321 /* XXX count dropped packet */
326 if (__predict_false(inp->inp_flags2 & INP_FREED))
328 if (jailed_without_vnet(inp->inp_cred)) {
330 * XXX: If faddr was bound to multicast group,
331 * jailed raw socket will drop datagram.
333 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
341 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
342 if (inp->inp_ip_p && inp->inp_ip_p != proto)
345 /* XXX inp locking */
346 if ((inp->inp_vflag & INP_IPV4) == 0)
349 if (!in_nullhost(inp->inp_laddr) &&
350 !in_hosteq(inp->inp_laddr, ip->ip_dst))
352 if (!in_nullhost(inp->inp_faddr) &&
353 !in_hosteq(inp->inp_faddr, ip->ip_src))
358 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
360 (void) rip_append(last, ip, n, &ripsrc);
361 /* XXX count dropped packet */
366 if (__predict_false(inp->inp_flags2 & INP_FREED))
368 if (jailed_without_vnet(inp->inp_cred)) {
370 * Allow raw socket in jail to receive multicast;
371 * assume process had PRIV_NETINET_RAW at attach,
372 * and fall through into normal filter path if so.
374 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
375 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
379 * If this raw socket has multicast state, and we
380 * have received a multicast, check if this socket
381 * should receive it, as multicast filtering is now
382 * the responsibility of the transport layer.
384 if (inp->inp_moptions != NULL &&
385 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
387 * If the incoming datagram is for IGMP, allow it
388 * through unconditionally to the raw socket.
390 * In the case of IGMPv2, we may not have explicitly
391 * joined the group, and may have set IFF_ALLMULTI
392 * on the interface. imo_multi_filter() may discard
393 * control traffic we actually need to see.
395 * Userland multicast routing daemons should continue
396 * filter the control traffic appropriately.
400 blocked = MCAST_PASS;
401 if (proto != IPPROTO_IGMP) {
402 struct sockaddr_in group;
404 bzero(&group, sizeof(struct sockaddr_in));
405 group.sin_len = sizeof(struct sockaddr_in);
406 group.sin_family = AF_INET;
407 group.sin_addr = ip->ip_dst;
409 blocked = imo_multi_filter(inp->inp_moptions,
411 (struct sockaddr *)&group,
412 (struct sockaddr *)&ripsrc);
415 if (blocked != MCAST_PASS) {
416 IPSTAT_INC(ips_notmember);
425 INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
427 if (rip_append(last, ip, m, &ripsrc) != 0)
428 IPSTAT_INC(ips_delivered);
431 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
432 IPSTAT_INC(ips_noproto);
433 IPSTAT_DEC(ips_delivered);
434 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
439 return (IPPROTO_DONE);
443 * Generate IP header and pass packet to ip_output. Tack on options user may
444 * have setup with control call.
447 rip_output(struct mbuf *m, struct socket *so, ...)
451 struct inpcb *inp = sotoinpcb(so);
454 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
457 u_char opttype, optlen, *cp;
460 dst = va_arg(ap, u_long);
464 * If the user handed us a complete IP packet, use it. Otherwise,
465 * allocate an mbuf for a header and fill it in.
467 if ((inp->inp_flags & INP_HDRINCL) == 0) {
468 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
472 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
477 ip = mtod(m, struct ip *);
478 ip->ip_tos = inp->inp_ip_tos;
479 if (inp->inp_flags & INP_DONTFRAG)
480 ip->ip_off = htons(IP_DF);
482 ip->ip_off = htons(0);
483 ip->ip_p = inp->inp_ip_p;
484 ip->ip_len = htons(m->m_pkthdr.len);
485 ip->ip_src = inp->inp_laddr;
486 ip->ip_dst.s_addr = dst;
487 if (jailed(inp->inp_cred)) {
489 * prison_local_ip4() would be good enough but would
490 * let a source of INADDR_ANY pass, which we do not
491 * want to see from jails.
493 if (ip->ip_src.s_addr == INADDR_ANY) {
494 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
497 error = prison_local_ip4(inp->inp_cred,
506 ip->ip_ttl = inp->inp_ip_ttl;
508 if (m->m_pkthdr.len > IP_MAXPACKET) {
512 ip = mtod(m, struct ip *);
513 hlen = ip->ip_hl << 2;
514 if (m->m_len < hlen) {
515 m = m_pullup(m, hlen);
518 ip = mtod(m, struct ip *);
523 * Don't allow both user specified and setsockopt options,
524 * and don't allow packet length sizes that will crash.
526 if ((hlen < sizeof (*ip))
527 || ((hlen > sizeof (*ip)) && inp->inp_options)
528 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
533 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
540 * Don't allow IP options which do not have the required
541 * structure as specified in section 3.1 of RFC 791 on
544 cp = (u_char *)(ip + 1);
545 cnt = hlen - sizeof (struct ip);
546 for (; cnt > 0; cnt -= optlen, cp += optlen) {
547 opttype = cp[IPOPT_OPTVAL];
548 if (opttype == IPOPT_EOL)
550 if (opttype == IPOPT_NOP) {
554 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
559 optlen = cp[IPOPT_OLEN];
560 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
568 * This doesn't allow application to specify ID of zero,
569 * but we got this limitation from the beginning of history.
575 * XXX prevent ip_output from overwriting header fields.
577 flags |= IP_RAWOUTPUT;
578 IPSTAT_INC(ips_rawout);
581 if (inp->inp_flags & INP_ONESBCAST)
582 flags |= IP_SENDONES;
585 mac_inpcb_create_mbuf(inp, m);
588 error = ip_output(m, inp->inp_options, NULL, flags,
589 inp->inp_moptions, inp);
595 * Raw IP socket option processing.
597 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
598 * only be created by a privileged process, and as such, socket option
599 * operations to manage system properties on any raw socket were allowed to
600 * take place without explicit additional access control checks. However,
601 * raw sockets can now also be created in jail(), and therefore explicit
602 * checks are now required. Likewise, raw sockets can be used by a process
603 * after it gives up privilege, so some caution is required. For options
604 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
605 * performed in ip_ctloutput() and therefore no check occurs here.
606 * Unilaterally checking priv_check() here breaks normal IP socket option
607 * operations on raw sockets.
609 * When adding new socket options here, make sure to add access control
610 * checks here as necessary.
612 * XXX-BZ inp locking?
615 rip_ctloutput(struct socket *so, struct sockopt *sopt)
617 struct inpcb *inp = sotoinpcb(so);
620 if (sopt->sopt_level != IPPROTO_IP) {
621 if ((sopt->sopt_level == SOL_SOCKET) &&
622 (sopt->sopt_name == SO_SETFIB)) {
623 inp->inp_inc.inc_fibnum = so->so_fibnum;
630 switch (sopt->sopt_dir) {
632 switch (sopt->sopt_name) {
634 optval = inp->inp_flags & INP_HDRINCL;
635 error = sooptcopyout(sopt, &optval, sizeof optval);
638 case IP_FW3: /* generic ipfw v.3 functions */
639 case IP_FW_ADD: /* ADD actually returns the body... */
641 case IP_FW_TABLE_GETSIZE:
642 case IP_FW_TABLE_LIST:
643 case IP_FW_NAT_GET_CONFIG:
644 case IP_FW_NAT_GET_LOG:
645 if (V_ip_fw_ctl_ptr != NULL)
646 error = V_ip_fw_ctl_ptr(sopt);
651 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
652 case IP_DUMMYNET_GET:
653 if (ip_dn_ctl_ptr != NULL)
654 error = ip_dn_ctl_ptr(sopt);
667 case MRT_API_SUPPORT:
669 case MRT_ADD_BW_UPCALL:
670 case MRT_DEL_BW_UPCALL:
671 error = priv_check(curthread, PRIV_NETINET_MROUTE);
674 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
679 error = ip_ctloutput(so, sopt);
685 switch (sopt->sopt_name) {
687 error = sooptcopyin(sopt, &optval, sizeof optval,
692 inp->inp_flags |= INP_HDRINCL;
694 inp->inp_flags &= ~INP_HDRINCL;
697 case IP_FW3: /* generic ipfw v.3 functions */
703 case IP_FW_TABLE_ADD:
704 case IP_FW_TABLE_DEL:
705 case IP_FW_TABLE_FLUSH:
708 if (V_ip_fw_ctl_ptr != NULL)
709 error = V_ip_fw_ctl_ptr(sopt);
714 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
715 case IP_DUMMYNET_CONFIGURE:
716 case IP_DUMMYNET_DEL:
717 case IP_DUMMYNET_FLUSH:
718 if (ip_dn_ctl_ptr != NULL)
719 error = ip_dn_ctl_ptr(sopt);
721 error = ENOPROTOOPT ;
725 error = priv_check(curthread, PRIV_NETINET_MROUTE);
728 error = ip_rsvp_init(so);
732 error = priv_check(curthread, PRIV_NETINET_MROUTE);
735 error = ip_rsvp_done();
739 case IP_RSVP_VIF_OFF:
740 error = priv_check(curthread, PRIV_NETINET_MROUTE);
743 error = ip_rsvp_vif ?
744 ip_rsvp_vif(so, sopt) : EINVAL;
755 case MRT_API_SUPPORT:
757 case MRT_ADD_BW_UPCALL:
758 case MRT_DEL_BW_UPCALL:
759 error = priv_check(curthread, PRIV_NETINET_MROUTE);
762 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
767 error = ip_ctloutput(so, sopt);
777 * This function exists solely to receive the PRC_IFDOWN messages which are
778 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
779 * in_ifadown() to remove all routes corresponding to that address. It also
780 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
784 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
786 struct rm_priotracker in_ifa_tracker;
787 struct in_ifaddr *ia;
794 IN_IFADDR_RLOCK(&in_ifa_tracker);
795 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
796 if (ia->ia_ifa.ifa_addr == sa
797 && (ia->ia_flags & IFA_ROUTE)) {
798 ifa_ref(&ia->ia_ifa);
799 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
801 * in_scrubprefix() kills the interface route.
803 in_scrubprefix(ia, 0);
805 * in_ifadown gets rid of all the rest of the
806 * routes. This is not quite the right thing
807 * to do, but at least if we are running a
808 * routing process they will come back.
810 in_ifadown(&ia->ia_ifa, 0);
811 ifa_free(&ia->ia_ifa);
815 if (ia == NULL) /* If ia matched, already unlocked. */
816 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
820 IN_IFADDR_RLOCK(&in_ifa_tracker);
821 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
822 if (ia->ia_ifa.ifa_addr == sa)
825 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
826 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
829 ifa_ref(&ia->ia_ifa);
830 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
832 ifp = ia->ia_ifa.ifa_ifp;
834 if ((ifp->if_flags & IFF_LOOPBACK)
835 || (ifp->if_flags & IFF_POINTOPOINT))
838 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
840 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
842 ia->ia_flags |= IFA_ROUTE;
844 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
846 ifa_free(&ia->ia_ifa);
852 rip_attach(struct socket *so, int proto, struct thread *td)
858 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
860 error = priv_check(td, PRIV_NETINET_RAW);
863 if (proto >= IPPROTO_MAX || proto < 0)
864 return EPROTONOSUPPORT;
865 error = soreserve(so, rip_sendspace, rip_recvspace);
868 INP_INFO_WLOCK(&V_ripcbinfo);
869 error = in_pcballoc(so, &V_ripcbinfo);
871 INP_INFO_WUNLOCK(&V_ripcbinfo);
874 inp = (struct inpcb *)so->so_pcb;
875 inp->inp_vflag |= INP_IPV4;
876 inp->inp_ip_p = proto;
877 inp->inp_ip_ttl = V_ip_defttl;
879 INP_INFO_WUNLOCK(&V_ripcbinfo);
885 rip_detach(struct socket *so)
890 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
891 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
892 ("rip_detach: not closed"));
894 INP_INFO_WLOCK(&V_ripcbinfo);
897 if (so == V_ip_mrouter && ip_mrouter_done)
899 if (ip_rsvp_force_done)
900 ip_rsvp_force_done(so);
901 if (so == V_ip_rsvpd)
905 INP_INFO_WUNLOCK(&V_ripcbinfo);
909 rip_dodisconnect(struct socket *so, struct inpcb *inp)
911 struct inpcbinfo *pcbinfo;
913 pcbinfo = inp->inp_pcbinfo;
914 INP_INFO_WLOCK(pcbinfo);
917 inp->inp_faddr.s_addr = INADDR_ANY;
920 so->so_state &= ~SS_ISCONNECTED;
923 INP_INFO_WUNLOCK(pcbinfo);
927 rip_abort(struct socket *so)
932 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
934 rip_dodisconnect(so, inp);
938 rip_close(struct socket *so)
943 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
945 rip_dodisconnect(so, inp);
949 rip_disconnect(struct socket *so)
953 if ((so->so_state & SS_ISCONNECTED) == 0)
957 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
959 rip_dodisconnect(so, inp);
964 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
966 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
970 if (nam->sa_len != sizeof(*addr))
973 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
978 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
980 if (CK_STAILQ_EMPTY(&V_ifnet) ||
981 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
982 (addr->sin_addr.s_addr &&
983 (inp->inp_flags & INP_BINDANY) == 0 &&
984 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
985 return (EADDRNOTAVAIL);
987 INP_INFO_WLOCK(&V_ripcbinfo);
990 inp->inp_laddr = addr->sin_addr;
993 INP_INFO_WUNLOCK(&V_ripcbinfo);
998 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1000 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1003 if (nam->sa_len != sizeof(*addr))
1005 if (CK_STAILQ_EMPTY(&V_ifnet))
1006 return (EADDRNOTAVAIL);
1007 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1008 return (EAFNOSUPPORT);
1010 inp = sotoinpcb(so);
1011 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1013 INP_INFO_WLOCK(&V_ripcbinfo);
1016 inp->inp_faddr = addr->sin_addr;
1020 INP_INFO_WUNLOCK(&V_ripcbinfo);
1025 rip_shutdown(struct socket *so)
1029 inp = sotoinpcb(so);
1030 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1039 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1040 struct mbuf *control, struct thread *td)
1045 inp = sotoinpcb(so);
1046 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1049 * Note: 'dst' reads below are unlocked.
1051 if (so->so_state & SS_ISCONNECTED) {
1056 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1062 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1064 return (rip_output(m, so, dst));
1069 rip_pcblist(SYSCTL_HANDLER_ARGS)
1072 struct inpcb *inp, **inp_list;
1075 struct epoch_tracker et;
1078 * The process of preparing the TCB list is too time-consuming and
1079 * resource-intensive to repeat twice on every request.
1081 if (req->oldptr == 0) {
1082 n = V_ripcbinfo.ipi_count;
1083 n += imax(n / 8, 10);
1084 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1088 if (req->newptr != 0)
1092 * OK, now we're committed to doing something.
1094 INP_INFO_WLOCK(&V_ripcbinfo);
1095 gencnt = V_ripcbinfo.ipi_gencnt;
1096 n = V_ripcbinfo.ipi_count;
1097 INP_INFO_WUNLOCK(&V_ripcbinfo);
1099 bzero(&xig, sizeof(xig));
1100 xig.xig_len = sizeof xig;
1102 xig.xig_gen = gencnt;
1103 xig.xig_sogen = so_gencnt;
1104 error = SYSCTL_OUT(req, &xig, sizeof xig);
1108 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1110 INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1111 for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1112 inp = CK_LIST_NEXT(inp, inp_list)) {
1114 if (inp->inp_gencnt <= gencnt &&
1115 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1117 inp_list[i++] = inp;
1121 INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
1125 for (i = 0; i < n; i++) {
1128 if (inp->inp_gencnt <= gencnt) {
1131 in_pcbtoxinpcb(inp, &xi);
1133 error = SYSCTL_OUT(req, &xi, sizeof xi);
1137 INP_INFO_WLOCK(&V_ripcbinfo);
1138 for (i = 0; i < n; i++) {
1141 if (!in_pcbrele_rlocked(inp))
1144 INP_INFO_WUNLOCK(&V_ripcbinfo);
1147 struct epoch_tracker et;
1149 * Give the user an updated idea of our state. If the
1150 * generation differs from what we told her before, she knows
1151 * that something happened while we were processing this
1152 * request, and it might be necessary to retry.
1154 INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1155 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1156 xig.xig_sogen = so_gencnt;
1157 xig.xig_count = V_ripcbinfo.ipi_count;
1158 INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
1159 error = SYSCTL_OUT(req, &xig, sizeof xig);
1161 free(inp_list, M_TEMP);
1165 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1166 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1167 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1170 struct pr_usrreqs rip_usrreqs = {
1171 .pru_abort = rip_abort,
1172 .pru_attach = rip_attach,
1173 .pru_bind = rip_bind,
1174 .pru_connect = rip_connect,
1175 .pru_control = in_control,
1176 .pru_detach = rip_detach,
1177 .pru_disconnect = rip_disconnect,
1178 .pru_peeraddr = in_getpeeraddr,
1179 .pru_send = rip_send,
1180 .pru_shutdown = rip_shutdown,
1181 .pru_sockaddr = in_getsockaddr,
1182 .pru_sosetlabel = in_pcbsosetlabel,
1183 .pru_close = rip_close,