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 **, int, struct ip_fw_args *);
104 void (*ip_divert_ptr)(struct mbuf *, int);
105 int (*ng_ipfw_input_p)(struct mbuf **, int,
106 struct ip_fw_args *, int);
110 * Hooks for multicast routing. They all default to NULL, so leave them not
111 * initialized and rely on BSS being set to 0.
115 * The socket used to communicate with the multicast routing daemon.
117 VNET_DEFINE(struct socket *, ip_mrouter);
120 * The various mrouter and rsvp functions.
122 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
123 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
124 int (*ip_mrouter_done)(void);
125 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
126 struct ip_moptions *);
127 int (*mrt_ioctl)(u_long, caddr_t, int);
128 int (*legal_vif_num)(int);
129 u_long (*ip_mcast_src)(int);
131 int (*rsvp_input_p)(struct mbuf **, int *, int);
132 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
133 void (*ip_rsvp_force_done)(struct socket *);
136 extern struct protosw inetsw[];
138 u_long rip_sendspace = 9216;
139 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
140 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
142 u_long rip_recvspace = 9216;
143 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
144 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
150 #define INP_PCBHASH_RAW_SIZE 256
151 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
152 (((proto) + (laddr) + (faddr)) % (mask) + 1)
156 rip_inshash(struct inpcb *inp)
158 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
159 struct inpcbhead *pcbhash;
162 INP_INFO_WLOCK_ASSERT(pcbinfo);
163 INP_WLOCK_ASSERT(inp);
165 if (inp->inp_ip_p != 0 &&
166 inp->inp_laddr.s_addr != INADDR_ANY &&
167 inp->inp_faddr.s_addr != INADDR_ANY) {
168 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
169 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
172 pcbhash = &pcbinfo->ipi_hashbase[hash];
173 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
177 rip_delhash(struct inpcb *inp)
180 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
181 INP_WLOCK_ASSERT(inp);
183 LIST_REMOVE(inp, inp_hash);
188 * Raw interface to IP protocol.
192 * Initialize raw connection block q.
195 rip_zone_change(void *tag)
198 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
202 rip_inpcb_init(void *mem, int size, int flags)
204 struct inpcb *inp = mem;
206 INP_LOCK_INIT(inp, "inp", "rawinp");
214 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
215 1, "ripcb", rip_inpcb_init, IPI_HASHFIELDS_NONE);
216 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
217 EVENTHANDLER_PRI_ANY);
222 rip_destroy(void *unused __unused)
225 in_pcbinfo_destroy(&V_ripcbinfo);
227 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
232 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
233 struct sockaddr_in *ripsrc)
237 INP_LOCK_ASSERT(last);
239 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
240 /* check AH/ESP integrity. */
241 if (IPSEC_ENABLED(ipv4)) {
242 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
247 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
250 /* Check the minimum TTL for socket. */
251 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
254 struct mbuf *opts = NULL;
257 so = last->inp_socket;
258 if ((last->inp_flags & INP_CONTROLOPTS) ||
259 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
260 ip_savecontrol(last, &opts, ip, n);
261 SOCKBUF_LOCK(&so->so_rcv);
262 if (sbappendaddr_locked(&so->so_rcv,
263 (struct sockaddr *)ripsrc, n, opts) == 0) {
264 /* should notify about lost packet */
268 SOCKBUF_UNLOCK(&so->so_rcv);
270 sorwakeup_locked(so);
277 * Setup generic address and protocol structures for raw_input routine, then
278 * pass them along with mbuf chain.
281 rip_input(struct mbuf **mp, int *offp, int proto)
284 struct mbuf *m = *mp;
285 struct ip *ip = mtod(m, struct ip *);
286 struct inpcb *inp, *last;
287 struct sockaddr_in ripsrc;
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(&V_ripcbinfo);
303 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)
315 if (jailed_without_vnet(inp->inp_cred)) {
317 * XXX: If faddr was bound to multicast group,
318 * jailed raw socket will drop datagram.
320 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
326 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
328 (void) rip_append(last, ip, n, &ripsrc);
329 /* XXX count dropped packet */
335 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
336 if (inp->inp_ip_p && inp->inp_ip_p != proto)
339 /* XXX inp locking */
340 if ((inp->inp_vflag & INP_IPV4) == 0)
343 if (!in_nullhost(inp->inp_laddr) &&
344 !in_hosteq(inp->inp_laddr, ip->ip_dst))
346 if (!in_nullhost(inp->inp_faddr) &&
347 !in_hosteq(inp->inp_faddr, ip->ip_src))
349 if (jailed_without_vnet(inp->inp_cred)) {
351 * Allow raw socket in jail to receive multicast;
352 * assume process had PRIV_NETINET_RAW at attach,
353 * and fall through into normal filter path if so.
355 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
356 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
360 * If this raw socket has multicast state, and we
361 * have received a multicast, check if this socket
362 * should receive it, as multicast filtering is now
363 * the responsibility of the transport layer.
365 if (inp->inp_moptions != NULL &&
366 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
368 * If the incoming datagram is for IGMP, allow it
369 * through unconditionally to the raw socket.
371 * In the case of IGMPv2, we may not have explicitly
372 * joined the group, and may have set IFF_ALLMULTI
373 * on the interface. imo_multi_filter() may discard
374 * control traffic we actually need to see.
376 * Userland multicast routing daemons should continue
377 * filter the control traffic appropriately.
381 blocked = MCAST_PASS;
382 if (proto != IPPROTO_IGMP) {
383 struct sockaddr_in group;
385 bzero(&group, sizeof(struct sockaddr_in));
386 group.sin_len = sizeof(struct sockaddr_in);
387 group.sin_family = AF_INET;
388 group.sin_addr = ip->ip_dst;
390 blocked = imo_multi_filter(inp->inp_moptions,
392 (struct sockaddr *)&group,
393 (struct sockaddr *)&ripsrc);
396 if (blocked != MCAST_PASS) {
397 IPSTAT_INC(ips_notmember);
404 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
406 (void) rip_append(last, ip, n, &ripsrc);
407 /* XXX count dropped packet */
413 INP_INFO_RUNLOCK(&V_ripcbinfo);
415 if (rip_append(last, ip, m, &ripsrc) != 0)
416 IPSTAT_INC(ips_delivered);
419 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
420 IPSTAT_INC(ips_noproto);
421 IPSTAT_DEC(ips_delivered);
422 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
427 return (IPPROTO_DONE);
431 * Generate IP header and pass packet to ip_output. Tack on options user may
432 * have setup with control call.
435 rip_output(struct mbuf *m, struct socket *so, ...)
439 struct inpcb *inp = sotoinpcb(so);
442 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
446 dst = va_arg(ap, u_long);
450 * If the user handed us a complete IP packet, use it. Otherwise,
451 * allocate an mbuf for a header and fill it in.
453 if ((inp->inp_flags & INP_HDRINCL) == 0) {
454 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
458 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
463 ip = mtod(m, struct ip *);
464 ip->ip_tos = inp->inp_ip_tos;
465 if (inp->inp_flags & INP_DONTFRAG)
466 ip->ip_off = htons(IP_DF);
468 ip->ip_off = htons(0);
469 ip->ip_p = inp->inp_ip_p;
470 ip->ip_len = htons(m->m_pkthdr.len);
471 ip->ip_src = inp->inp_laddr;
472 ip->ip_dst.s_addr = dst;
473 if (jailed(inp->inp_cred)) {
475 * prison_local_ip4() would be good enough but would
476 * let a source of INADDR_ANY pass, which we do not
477 * want to see from jails.
479 if (ip->ip_src.s_addr == INADDR_ANY) {
480 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
483 error = prison_local_ip4(inp->inp_cred,
492 ip->ip_ttl = inp->inp_ip_ttl;
494 if (m->m_pkthdr.len > IP_MAXPACKET) {
499 ip = mtod(m, struct ip *);
500 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
508 * Don't allow both user specified and setsockopt options,
509 * and don't allow packet length sizes that will crash.
511 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
512 || (ntohs(ip->ip_len) != m->m_pkthdr.len)
513 || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
519 * This doesn't allow application to specify ID of zero,
520 * but we got this limitation from the beginning of history.
526 * XXX prevent ip_output from overwriting header fields.
528 flags |= IP_RAWOUTPUT;
529 IPSTAT_INC(ips_rawout);
532 if (inp->inp_flags & INP_ONESBCAST)
533 flags |= IP_SENDONES;
536 mac_inpcb_create_mbuf(inp, m);
539 error = ip_output(m, inp->inp_options, NULL, flags,
540 inp->inp_moptions, inp);
546 * Raw IP socket option processing.
548 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
549 * only be created by a privileged process, and as such, socket option
550 * operations to manage system properties on any raw socket were allowed to
551 * take place without explicit additional access control checks. However,
552 * raw sockets can now also be created in jail(), and therefore explicit
553 * checks are now required. Likewise, raw sockets can be used by a process
554 * after it gives up privilege, so some caution is required. For options
555 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
556 * performed in ip_ctloutput() and therefore no check occurs here.
557 * Unilaterally checking priv_check() here breaks normal IP socket option
558 * operations on raw sockets.
560 * When adding new socket options here, make sure to add access control
561 * checks here as necessary.
563 * XXX-BZ inp locking?
566 rip_ctloutput(struct socket *so, struct sockopt *sopt)
568 struct inpcb *inp = sotoinpcb(so);
571 if (sopt->sopt_level != IPPROTO_IP) {
572 if ((sopt->sopt_level == SOL_SOCKET) &&
573 (sopt->sopt_name == SO_SETFIB)) {
574 inp->inp_inc.inc_fibnum = so->so_fibnum;
581 switch (sopt->sopt_dir) {
583 switch (sopt->sopt_name) {
585 optval = inp->inp_flags & INP_HDRINCL;
586 error = sooptcopyout(sopt, &optval, sizeof optval);
589 case IP_FW3: /* generic ipfw v.3 functions */
590 case IP_FW_ADD: /* ADD actually returns the body... */
592 case IP_FW_TABLE_GETSIZE:
593 case IP_FW_TABLE_LIST:
594 case IP_FW_NAT_GET_CONFIG:
595 case IP_FW_NAT_GET_LOG:
596 if (V_ip_fw_ctl_ptr != NULL)
597 error = V_ip_fw_ctl_ptr(sopt);
602 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
603 case IP_DUMMYNET_GET:
604 if (ip_dn_ctl_ptr != NULL)
605 error = ip_dn_ctl_ptr(sopt);
618 case MRT_API_SUPPORT:
620 case MRT_ADD_BW_UPCALL:
621 case MRT_DEL_BW_UPCALL:
622 error = priv_check(curthread, PRIV_NETINET_MROUTE);
625 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
630 error = ip_ctloutput(so, sopt);
636 switch (sopt->sopt_name) {
638 error = sooptcopyin(sopt, &optval, sizeof optval,
643 inp->inp_flags |= INP_HDRINCL;
645 inp->inp_flags &= ~INP_HDRINCL;
648 case IP_FW3: /* generic ipfw v.3 functions */
654 case IP_FW_TABLE_ADD:
655 case IP_FW_TABLE_DEL:
656 case IP_FW_TABLE_FLUSH:
659 if (V_ip_fw_ctl_ptr != NULL)
660 error = V_ip_fw_ctl_ptr(sopt);
665 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
666 case IP_DUMMYNET_CONFIGURE:
667 case IP_DUMMYNET_DEL:
668 case IP_DUMMYNET_FLUSH:
669 if (ip_dn_ctl_ptr != NULL)
670 error = ip_dn_ctl_ptr(sopt);
672 error = ENOPROTOOPT ;
676 error = priv_check(curthread, PRIV_NETINET_MROUTE);
679 error = ip_rsvp_init(so);
683 error = priv_check(curthread, PRIV_NETINET_MROUTE);
686 error = ip_rsvp_done();
690 case IP_RSVP_VIF_OFF:
691 error = priv_check(curthread, PRIV_NETINET_MROUTE);
694 error = ip_rsvp_vif ?
695 ip_rsvp_vif(so, sopt) : EINVAL;
706 case MRT_API_SUPPORT:
708 case MRT_ADD_BW_UPCALL:
709 case MRT_DEL_BW_UPCALL:
710 error = priv_check(curthread, PRIV_NETINET_MROUTE);
713 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
718 error = ip_ctloutput(so, sopt);
728 * This function exists solely to receive the PRC_IFDOWN messages which are
729 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
730 * in_ifadown() to remove all routes corresponding to that address. It also
731 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
735 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
737 struct rm_priotracker in_ifa_tracker;
738 struct in_ifaddr *ia;
745 IN_IFADDR_RLOCK(&in_ifa_tracker);
746 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
747 if (ia->ia_ifa.ifa_addr == sa
748 && (ia->ia_flags & IFA_ROUTE)) {
749 ifa_ref(&ia->ia_ifa);
750 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
752 * in_scrubprefix() kills the interface route.
754 in_scrubprefix(ia, 0);
756 * in_ifadown gets rid of all the rest of the
757 * routes. This is not quite the right thing
758 * to do, but at least if we are running a
759 * routing process they will come back.
761 in_ifadown(&ia->ia_ifa, 0);
762 ifa_free(&ia->ia_ifa);
766 if (ia == NULL) /* If ia matched, already unlocked. */
767 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
771 IN_IFADDR_RLOCK(&in_ifa_tracker);
772 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
773 if (ia->ia_ifa.ifa_addr == sa)
776 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
777 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
780 ifa_ref(&ia->ia_ifa);
781 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
783 ifp = ia->ia_ifa.ifa_ifp;
785 if ((ifp->if_flags & IFF_LOOPBACK)
786 || (ifp->if_flags & IFF_POINTOPOINT))
789 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
791 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
793 ia->ia_flags |= IFA_ROUTE;
795 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
797 ifa_free(&ia->ia_ifa);
803 rip_attach(struct socket *so, int proto, struct thread *td)
809 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
811 error = priv_check(td, PRIV_NETINET_RAW);
814 if (proto >= IPPROTO_MAX || proto < 0)
815 return EPROTONOSUPPORT;
816 error = soreserve(so, rip_sendspace, rip_recvspace);
819 INP_INFO_WLOCK(&V_ripcbinfo);
820 error = in_pcballoc(so, &V_ripcbinfo);
822 INP_INFO_WUNLOCK(&V_ripcbinfo);
825 inp = (struct inpcb *)so->so_pcb;
826 inp->inp_vflag |= INP_IPV4;
827 inp->inp_ip_p = proto;
828 inp->inp_ip_ttl = V_ip_defttl;
830 INP_INFO_WUNLOCK(&V_ripcbinfo);
836 rip_detach(struct socket *so)
841 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
842 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
843 ("rip_detach: not closed"));
845 INP_INFO_WLOCK(&V_ripcbinfo);
848 if (so == V_ip_mrouter && ip_mrouter_done)
850 if (ip_rsvp_force_done)
851 ip_rsvp_force_done(so);
852 if (so == V_ip_rsvpd)
854 /* XXX defer to epoch_call */
857 INP_INFO_WUNLOCK(&V_ripcbinfo);
861 rip_dodisconnect(struct socket *so, struct inpcb *inp)
863 struct inpcbinfo *pcbinfo;
865 pcbinfo = inp->inp_pcbinfo;
866 INP_INFO_WLOCK(pcbinfo);
869 inp->inp_faddr.s_addr = INADDR_ANY;
872 so->so_state &= ~SS_ISCONNECTED;
875 INP_INFO_WUNLOCK(pcbinfo);
879 rip_abort(struct socket *so)
884 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
886 rip_dodisconnect(so, inp);
890 rip_close(struct socket *so)
895 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
897 rip_dodisconnect(so, inp);
901 rip_disconnect(struct socket *so)
905 if ((so->so_state & SS_ISCONNECTED) == 0)
909 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
911 rip_dodisconnect(so, inp);
916 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
918 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
922 if (nam->sa_len != sizeof(*addr))
925 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
930 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
932 if (CK_STAILQ_EMPTY(&V_ifnet) ||
933 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
934 (addr->sin_addr.s_addr &&
935 (inp->inp_flags & INP_BINDANY) == 0 &&
936 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
937 return (EADDRNOTAVAIL);
939 INP_INFO_WLOCK(&V_ripcbinfo);
942 inp->inp_laddr = addr->sin_addr;
945 INP_INFO_WUNLOCK(&V_ripcbinfo);
950 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
952 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
955 if (nam->sa_len != sizeof(*addr))
957 if (CK_STAILQ_EMPTY(&V_ifnet))
958 return (EADDRNOTAVAIL);
959 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
960 return (EAFNOSUPPORT);
963 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
965 INP_INFO_WLOCK(&V_ripcbinfo);
968 inp->inp_faddr = addr->sin_addr;
972 INP_INFO_WUNLOCK(&V_ripcbinfo);
977 rip_shutdown(struct socket *so)
982 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
991 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
992 struct mbuf *control, struct thread *td)
998 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1001 * Note: 'dst' reads below are unlocked.
1003 if (so->so_state & SS_ISCONNECTED) {
1008 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1014 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1016 return (rip_output(m, so, dst));
1021 rip_pcblist(SYSCTL_HANDLER_ARGS)
1024 struct in_pcblist *il;
1025 struct inpcb *inp, **inp_list;
1030 * The process of preparing the TCB list is too time-consuming and
1031 * resource-intensive to repeat twice on every request.
1033 if (req->oldptr == 0) {
1034 n = V_ripcbinfo.ipi_count;
1035 n += imax(n / 8, 10);
1036 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1040 if (req->newptr != 0)
1044 * OK, now we're committed to doing something.
1046 INP_INFO_RLOCK(&V_ripcbinfo);
1047 gencnt = V_ripcbinfo.ipi_gencnt;
1048 n = V_ripcbinfo.ipi_count;
1049 INP_INFO_RUNLOCK(&V_ripcbinfo);
1051 xig.xig_len = sizeof xig;
1053 xig.xig_gen = gencnt;
1054 xig.xig_sogen = so_gencnt;
1055 error = SYSCTL_OUT(req, &xig, sizeof xig);
1059 il = malloc(sizeof(struct in_pcblist) + n * sizeof(struct inpcb *), M_TEMP, M_WAITOK|M_ZERO_INVARIANTS);
1060 inp_list = il->il_inp_list;
1062 INP_INFO_RLOCK(&V_ripcbinfo);
1063 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1064 inp = LIST_NEXT(inp, inp_list)) {
1066 if (inp->inp_gencnt <= gencnt &&
1067 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1069 inp_list[i++] = inp;
1073 INP_INFO_RUNLOCK(&V_ripcbinfo);
1077 for (i = 0; i < n; i++) {
1080 if (inp->inp_gencnt <= gencnt) {
1083 in_pcbtoxinpcb(inp, &xi);
1085 error = SYSCTL_OUT(req, &xi, sizeof xi);
1090 il->il_pcbinfo = &V_ripcbinfo;
1091 epoch_call(net_epoch_preempt, &il->il_epoch_ctx, in_pcblist_rele_rlocked);
1095 * Give the user an updated idea of our state. If the
1096 * generation differs from what we told her before, she knows
1097 * that something happened while we were processing this
1098 * request, and it might be necessary to retry.
1100 INP_INFO_RLOCK(&V_ripcbinfo);
1101 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1102 xig.xig_sogen = so_gencnt;
1103 xig.xig_count = V_ripcbinfo.ipi_count;
1104 INP_INFO_RUNLOCK(&V_ripcbinfo);
1105 error = SYSCTL_OUT(req, &xig, sizeof xig);
1110 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1111 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1112 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1115 struct pr_usrreqs rip_usrreqs = {
1116 .pru_abort = rip_abort,
1117 .pru_attach = rip_attach,
1118 .pru_bind = rip_bind,
1119 .pru_connect = rip_connect,
1120 .pru_control = in_control,
1121 .pru_detach = rip_detach,
1122 .pru_disconnect = rip_disconnect,
1123 .pru_peeraddr = in_getpeeraddr,
1124 .pru_send = rip_send,
1125 .pru_shutdown = rip_shutdown,
1126 .pru_sockaddr = in_getsockaddr,
1127 .pru_sosetlabel = in_pcbsosetlabel,
1128 .pru_close = rip_close,