2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
37 #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/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/ip_mroute.h>
72 #include <netipsec/ipsec.h>
75 #include <security/mac/mac_framework.h>
77 VNET_DEFINE(struct inpcbhead, ripcb);
78 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
80 #define V_ripcb VNET(ripcb)
81 #define V_ripcbinfo VNET(ripcbinfo)
84 * Control and data hooks for ipfw, dummynet, divert and so on.
85 * The data hooks are not used here but it is convenient
86 * to keep them all in one place.
88 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
89 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
91 int (*ip_dn_ctl_ptr)(struct sockopt *);
92 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
93 void (*ip_divert_ptr)(struct mbuf *, int);
94 int (*ng_ipfw_input_p)(struct mbuf **, int,
95 struct ip_fw_args *, int);
98 * Hooks for multicast routing. They all default to NULL, so leave them not
99 * initialized and rely on BSS being set to 0.
103 * The socket used to communicate with the multicast routing daemon.
105 VNET_DEFINE(struct socket *, ip_mrouter);
108 * The various mrouter and rsvp functions.
110 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
111 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
112 int (*ip_mrouter_done)(void);
113 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
114 struct ip_moptions *);
115 int (*mrt_ioctl)(u_long, caddr_t, int);
116 int (*legal_vif_num)(int);
117 u_long (*ip_mcast_src)(int);
119 void (*rsvp_input_p)(struct mbuf *m, int off);
120 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
121 void (*ip_rsvp_force_done)(struct socket *);
127 #define INP_PCBHASH_RAW_SIZE 256
128 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
129 (((proto) + (laddr) + (faddr)) % (mask) + 1)
132 rip_inshash(struct inpcb *inp)
134 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
135 struct inpcbhead *pcbhash;
138 INP_INFO_WLOCK_ASSERT(pcbinfo);
139 INP_WLOCK_ASSERT(inp);
141 if (inp->inp_ip_p != 0 &&
142 inp->inp_laddr.s_addr != INADDR_ANY &&
143 inp->inp_faddr.s_addr != INADDR_ANY) {
144 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
145 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
148 pcbhash = &pcbinfo->ipi_hashbase[hash];
149 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
153 rip_delhash(struct inpcb *inp)
156 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
157 INP_WLOCK_ASSERT(inp);
159 LIST_REMOVE(inp, inp_hash);
163 * Raw interface to IP protocol.
167 * Initialize raw connection block q.
170 rip_zone_change(void *tag)
173 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
177 rip_inpcb_init(void *mem, int size, int flags)
179 struct inpcb *inp = mem;
181 INP_LOCK_INIT(inp, "inp", "rawinp");
189 INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
192 V_ripcbinfo.ipi_vnet = curvnet;
194 V_ripcbinfo.ipi_listhead = &V_ripcb;
195 V_ripcbinfo.ipi_hashbase =
196 hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
197 V_ripcbinfo.ipi_porthashbase =
198 hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
199 V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
200 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
201 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
202 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
203 EVENTHANDLER_PRI_ANY);
211 hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
212 V_ripcbinfo.ipi_hashmask);
213 hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
214 V_ripcbinfo.ipi_porthashmask);
219 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
220 struct sockaddr_in *ripsrc)
224 INP_RLOCK_ASSERT(last);
227 /* check AH/ESP integrity. */
228 if (ipsec4_in_reject(n, last)) {
233 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
236 /* Check the minimum TTL for socket. */
237 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
240 struct mbuf *opts = NULL;
243 so = last->inp_socket;
244 if ((last->inp_flags & INP_CONTROLOPTS) ||
245 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
246 ip_savecontrol(last, &opts, ip, n);
247 SOCKBUF_LOCK(&so->so_rcv);
248 if (sbappendaddr_locked(&so->so_rcv,
249 (struct sockaddr *)ripsrc, n, opts) == 0) {
250 /* should notify about lost packet */
254 SOCKBUF_UNLOCK(&so->so_rcv);
256 sorwakeup_locked(so);
263 * Setup generic address and protocol structures for raw_input routine, then
264 * pass them along with mbuf chain.
267 rip_input(struct mbuf *m, int off)
270 struct ip *ip = mtod(m, struct ip *);
271 int proto = ip->ip_p;
272 struct inpcb *inp, *last;
273 struct sockaddr_in ripsrc;
276 bzero(&ripsrc, sizeof(ripsrc));
277 ripsrc.sin_len = sizeof(ripsrc);
278 ripsrc.sin_family = AF_INET;
279 ripsrc.sin_addr = ip->ip_src;
282 ifp = m->m_pkthdr.rcvif;
284 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
285 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
286 INP_INFO_RLOCK(&V_ripcbinfo);
287 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
288 if (inp->inp_ip_p != proto)
291 /* XXX inp locking */
292 if ((inp->inp_vflag & INP_IPV4) == 0)
295 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
297 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
299 if (jailed_without_vnet(inp->inp_cred)) {
301 * XXX: If faddr was bound to multicast group,
302 * jailed raw socket will drop datagram.
304 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
310 n = m_copy(m, 0, (int)M_COPYALL);
312 (void) rip_append(last, ip, n, &ripsrc);
313 /* XXX count dropped packet */
319 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
320 if (inp->inp_ip_p && inp->inp_ip_p != proto)
323 /* XXX inp locking */
324 if ((inp->inp_vflag & INP_IPV4) == 0)
327 if (!in_nullhost(inp->inp_laddr) &&
328 !in_hosteq(inp->inp_laddr, ip->ip_dst))
330 if (!in_nullhost(inp->inp_faddr) &&
331 !in_hosteq(inp->inp_faddr, ip->ip_src))
333 if (jailed_without_vnet(inp->inp_cred)) {
335 * Allow raw socket in jail to receive multicast;
336 * assume process had PRIV_NETINET_RAW at attach,
337 * and fall through into normal filter path if so.
339 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
340 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
344 * If this raw socket has multicast state, and we
345 * have received a multicast, check if this socket
346 * should receive it, as multicast filtering is now
347 * the responsibility of the transport layer.
349 if (inp->inp_moptions != NULL &&
350 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
352 * If the incoming datagram is for IGMP, allow it
353 * through unconditionally to the raw socket.
355 * In the case of IGMPv2, we may not have explicitly
356 * joined the group, and may have set IFF_ALLMULTI
357 * on the interface. imo_multi_filter() may discard
358 * control traffic we actually need to see.
360 * Userland multicast routing daemons should continue
361 * filter the control traffic appropriately.
365 blocked = MCAST_PASS;
366 if (proto != IPPROTO_IGMP) {
367 struct sockaddr_in group;
369 bzero(&group, sizeof(struct sockaddr_in));
370 group.sin_len = sizeof(struct sockaddr_in);
371 group.sin_family = AF_INET;
372 group.sin_addr = ip->ip_dst;
374 blocked = imo_multi_filter(inp->inp_moptions,
376 (struct sockaddr *)&group,
377 (struct sockaddr *)&ripsrc);
380 if (blocked != MCAST_PASS) {
381 IPSTAT_INC(ips_notmember);
388 n = m_copy(m, 0, (int)M_COPYALL);
390 (void) rip_append(last, ip, n, &ripsrc);
391 /* XXX count dropped packet */
397 INP_INFO_RUNLOCK(&V_ripcbinfo);
399 if (rip_append(last, ip, m, &ripsrc) != 0)
400 IPSTAT_INC(ips_delivered);
404 IPSTAT_INC(ips_noproto);
405 IPSTAT_DEC(ips_delivered);
410 * Generate IP header and pass packet to ip_output. Tack on options user may
411 * have setup with control call.
414 rip_output(struct mbuf *m, struct socket *so, u_long dst)
418 struct inpcb *inp = sotoinpcb(so);
419 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
423 * If the user handed us a complete IP packet, use it. Otherwise,
424 * allocate an mbuf for a header and fill it in.
426 if ((inp->inp_flags & INP_HDRINCL) == 0) {
427 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
431 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
436 ip = mtod(m, struct ip *);
437 ip->ip_tos = inp->inp_ip_tos;
438 if (inp->inp_flags & INP_DONTFRAG)
442 ip->ip_p = inp->inp_ip_p;
443 ip->ip_len = m->m_pkthdr.len;
444 ip->ip_src = inp->inp_laddr;
445 if (jailed(inp->inp_cred)) {
447 * prison_local_ip4() would be good enough but would
448 * let a source of INADDR_ANY pass, which we do not
449 * want to see from jails. We do not go through the
450 * pain of in_pcbladdr() for raw sockets.
452 if (ip->ip_src.s_addr == INADDR_ANY)
453 error = prison_get_ip4(inp->inp_cred,
456 error = prison_local_ip4(inp->inp_cred,
464 ip->ip_dst.s_addr = dst;
465 ip->ip_ttl = inp->inp_ip_ttl;
467 if (m->m_pkthdr.len > IP_MAXPACKET) {
472 ip = mtod(m, struct ip *);
473 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
481 * Don't allow both user specified and setsockopt options,
482 * and don't allow packet length sizes that will crash.
484 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
485 || (ip->ip_len > m->m_pkthdr.len)
486 || (ip->ip_len < (ip->ip_hl << 2))) {
492 ip->ip_id = ip_newid();
495 * XXX prevent ip_output from overwriting header fields.
497 flags |= IP_RAWOUTPUT;
498 IPSTAT_INC(ips_rawout);
501 if (inp->inp_flags & INP_ONESBCAST)
502 flags |= IP_SENDONES;
505 mac_inpcb_create_mbuf(inp, m);
508 error = ip_output(m, inp->inp_options, NULL, flags,
509 inp->inp_moptions, inp);
515 * Raw IP socket option processing.
517 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
518 * only be created by a privileged process, and as such, socket option
519 * operations to manage system properties on any raw socket were allowed to
520 * take place without explicit additional access control checks. However,
521 * raw sockets can now also be created in jail(), and therefore explicit
522 * checks are now required. Likewise, raw sockets can be used by a process
523 * after it gives up privilege, so some caution is required. For options
524 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
525 * performed in ip_ctloutput() and therefore no check occurs here.
526 * Unilaterally checking priv_check() here breaks normal IP socket option
527 * operations on raw sockets.
529 * When adding new socket options here, make sure to add access control
530 * checks here as necessary.
533 rip_ctloutput(struct socket *so, struct sockopt *sopt)
535 struct inpcb *inp = sotoinpcb(so);
538 if (sopt->sopt_level != IPPROTO_IP) {
539 if ((sopt->sopt_level == SOL_SOCKET) &&
540 (sopt->sopt_name == SO_SETFIB)) {
541 inp->inp_inc.inc_fibnum = so->so_fibnum;
548 switch (sopt->sopt_dir) {
550 switch (sopt->sopt_name) {
552 optval = inp->inp_flags & INP_HDRINCL;
553 error = sooptcopyout(sopt, &optval, sizeof optval);
556 case IP_FW3: /* generic ipfw v.3 functions */
557 case IP_FW_ADD: /* ADD actually returns the body... */
559 case IP_FW_TABLE_GETSIZE:
560 case IP_FW_TABLE_LIST:
561 case IP_FW_NAT_GET_CONFIG:
562 case IP_FW_NAT_GET_LOG:
563 if (V_ip_fw_ctl_ptr != NULL)
564 error = V_ip_fw_ctl_ptr(sopt);
569 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
570 case IP_DUMMYNET_GET:
571 if (ip_dn_ctl_ptr != NULL)
572 error = ip_dn_ctl_ptr(sopt);
585 case MRT_API_SUPPORT:
587 case MRT_ADD_BW_UPCALL:
588 case MRT_DEL_BW_UPCALL:
589 error = priv_check(curthread, PRIV_NETINET_MROUTE);
592 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
597 error = ip_ctloutput(so, sopt);
603 switch (sopt->sopt_name) {
605 error = sooptcopyin(sopt, &optval, sizeof optval,
610 inp->inp_flags |= INP_HDRINCL;
612 inp->inp_flags &= ~INP_HDRINCL;
615 case IP_FW3: /* generic ipfw v.3 functions */
621 case IP_FW_TABLE_ADD:
622 case IP_FW_TABLE_DEL:
623 case IP_FW_TABLE_FLUSH:
626 if (V_ip_fw_ctl_ptr != NULL)
627 error = V_ip_fw_ctl_ptr(sopt);
632 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
633 case IP_DUMMYNET_CONFIGURE:
634 case IP_DUMMYNET_DEL:
635 case IP_DUMMYNET_FLUSH:
636 if (ip_dn_ctl_ptr != NULL)
637 error = ip_dn_ctl_ptr(sopt);
639 error = ENOPROTOOPT ;
643 error = priv_check(curthread, PRIV_NETINET_MROUTE);
646 error = ip_rsvp_init(so);
650 error = priv_check(curthread, PRIV_NETINET_MROUTE);
653 error = ip_rsvp_done();
657 case IP_RSVP_VIF_OFF:
658 error = priv_check(curthread, PRIV_NETINET_MROUTE);
661 error = ip_rsvp_vif ?
662 ip_rsvp_vif(so, sopt) : EINVAL;
673 case MRT_API_SUPPORT:
675 case MRT_ADD_BW_UPCALL:
676 case MRT_DEL_BW_UPCALL:
677 error = priv_check(curthread, PRIV_NETINET_MROUTE);
680 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
685 error = ip_ctloutput(so, sopt);
695 * This function exists solely to receive the PRC_IFDOWN messages which are
696 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
697 * in_ifadown() to remove all routes corresponding to that address. It also
698 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
702 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
704 struct in_ifaddr *ia;
712 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
713 if (ia->ia_ifa.ifa_addr == sa
714 && (ia->ia_flags & IFA_ROUTE)) {
715 ifa_ref(&ia->ia_ifa);
718 * in_ifscrub kills the interface route.
720 in_ifscrub(ia->ia_ifp, ia, 0);
722 * in_ifadown gets rid of all the rest of the
723 * routes. This is not quite the right thing
724 * to do, but at least if we are running a
725 * routing process they will come back.
727 in_ifadown(&ia->ia_ifa, 0);
728 ifa_free(&ia->ia_ifa);
732 if (ia == NULL) /* If ia matched, already unlocked. */
738 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
739 if (ia->ia_ifa.ifa_addr == sa)
742 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
746 ifa_ref(&ia->ia_ifa);
749 ifp = ia->ia_ifa.ifa_ifp;
751 if ((ifp->if_flags & IFF_LOOPBACK)
752 || (ifp->if_flags & IFF_POINTOPOINT))
755 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
757 ia->ia_flags &= ~IFA_RTSELF;
759 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
761 ia->ia_flags |= IFA_ROUTE;
763 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
765 ia->ia_flags |= IFA_RTSELF;
767 ifa_free(&ia->ia_ifa);
772 u_long rip_sendspace = 9216;
773 u_long rip_recvspace = 9216;
775 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
776 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
777 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
778 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
781 rip_attach(struct socket *so, int proto, struct thread *td)
787 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
789 error = priv_check(td, PRIV_NETINET_RAW);
792 if (proto >= IPPROTO_MAX || proto < 0)
793 return EPROTONOSUPPORT;
794 error = soreserve(so, rip_sendspace, rip_recvspace);
797 INP_INFO_WLOCK(&V_ripcbinfo);
798 error = in_pcballoc(so, &V_ripcbinfo);
800 INP_INFO_WUNLOCK(&V_ripcbinfo);
803 inp = (struct inpcb *)so->so_pcb;
804 inp->inp_vflag |= INP_IPV4;
805 inp->inp_ip_p = proto;
806 inp->inp_ip_ttl = V_ip_defttl;
808 INP_INFO_WUNLOCK(&V_ripcbinfo);
814 rip_detach(struct socket *so)
819 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
820 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
821 ("rip_detach: not closed"));
823 INP_INFO_WLOCK(&V_ripcbinfo);
826 if (so == V_ip_mrouter && ip_mrouter_done)
828 if (ip_rsvp_force_done)
829 ip_rsvp_force_done(so);
830 if (so == V_ip_rsvpd)
834 INP_INFO_WUNLOCK(&V_ripcbinfo);
838 rip_dodisconnect(struct socket *so, struct inpcb *inp)
841 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
842 INP_WLOCK_ASSERT(inp);
845 inp->inp_faddr.s_addr = INADDR_ANY;
848 so->so_state &= ~SS_ISCONNECTED;
853 rip_abort(struct socket *so)
858 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
860 INP_INFO_WLOCK(&V_ripcbinfo);
862 rip_dodisconnect(so, inp);
864 INP_INFO_WUNLOCK(&V_ripcbinfo);
868 rip_close(struct socket *so)
873 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
875 INP_INFO_WLOCK(&V_ripcbinfo);
877 rip_dodisconnect(so, inp);
879 INP_INFO_WUNLOCK(&V_ripcbinfo);
883 rip_disconnect(struct socket *so)
887 if ((so->so_state & SS_ISCONNECTED) == 0)
891 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
893 INP_INFO_WLOCK(&V_ripcbinfo);
895 rip_dodisconnect(so, inp);
897 INP_INFO_WUNLOCK(&V_ripcbinfo);
902 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
904 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
908 if (nam->sa_len != sizeof(*addr))
911 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
916 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
918 if (TAILQ_EMPTY(&V_ifnet) ||
919 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
920 (addr->sin_addr.s_addr &&
921 (inp->inp_flags & INP_BINDANY) == 0 &&
922 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
923 return (EADDRNOTAVAIL);
925 INP_INFO_WLOCK(&V_ripcbinfo);
928 inp->inp_laddr = addr->sin_addr;
931 INP_INFO_WUNLOCK(&V_ripcbinfo);
936 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
938 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
941 if (nam->sa_len != sizeof(*addr))
943 if (TAILQ_EMPTY(&V_ifnet))
944 return (EADDRNOTAVAIL);
945 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
946 return (EAFNOSUPPORT);
949 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
951 INP_INFO_WLOCK(&V_ripcbinfo);
954 inp->inp_faddr = addr->sin_addr;
958 INP_INFO_WUNLOCK(&V_ripcbinfo);
963 rip_shutdown(struct socket *so)
968 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
977 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
978 struct mbuf *control, struct thread *td)
984 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
987 * Note: 'dst' reads below are unlocked.
989 if (so->so_state & SS_ISCONNECTED) {
994 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1000 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1002 return (rip_output(m, so, dst));
1006 rip_pcblist(SYSCTL_HANDLER_ARGS)
1009 struct inpcb *inp, **inp_list;
1014 * The process of preparing the TCB list is too time-consuming and
1015 * resource-intensive to repeat twice on every request.
1017 if (req->oldptr == 0) {
1018 n = V_ripcbinfo.ipi_count;
1019 n += imax(n / 8, 10);
1020 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1024 if (req->newptr != 0)
1028 * OK, now we're committed to doing something.
1030 INP_INFO_RLOCK(&V_ripcbinfo);
1031 gencnt = V_ripcbinfo.ipi_gencnt;
1032 n = V_ripcbinfo.ipi_count;
1033 INP_INFO_RUNLOCK(&V_ripcbinfo);
1035 xig.xig_len = sizeof xig;
1037 xig.xig_gen = gencnt;
1038 xig.xig_sogen = so_gencnt;
1039 error = SYSCTL_OUT(req, &xig, sizeof xig);
1043 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1047 INP_INFO_RLOCK(&V_ripcbinfo);
1048 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1049 inp = LIST_NEXT(inp, inp_list)) {
1051 if (inp->inp_gencnt <= gencnt &&
1052 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1054 inp_list[i++] = inp;
1058 INP_INFO_RUNLOCK(&V_ripcbinfo);
1062 for (i = 0; i < n; i++) {
1065 if (inp->inp_gencnt <= gencnt) {
1068 bzero(&xi, sizeof(xi));
1069 xi.xi_len = sizeof xi;
1070 /* XXX should avoid extra copy */
1071 bcopy(inp, &xi.xi_inp, sizeof *inp);
1072 if (inp->inp_socket)
1073 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1075 error = SYSCTL_OUT(req, &xi, sizeof xi);
1079 INP_INFO_WLOCK(&V_ripcbinfo);
1080 for (i = 0; i < n; i++) {
1083 if (!in_pcbrele(inp))
1086 INP_INFO_WUNLOCK(&V_ripcbinfo);
1090 * Give the user an updated idea of our state. If the
1091 * generation differs from what we told her before, she knows
1092 * that something happened while we were processing this
1093 * request, and it might be necessary to retry.
1095 INP_INFO_RLOCK(&V_ripcbinfo);
1096 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1097 xig.xig_sogen = so_gencnt;
1098 xig.xig_count = V_ripcbinfo.ipi_count;
1099 INP_INFO_RUNLOCK(&V_ripcbinfo);
1100 error = SYSCTL_OUT(req, &xig, sizeof xig);
1102 free(inp_list, M_TEMP);
1106 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1107 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1108 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1110 struct pr_usrreqs rip_usrreqs = {
1111 .pru_abort = rip_abort,
1112 .pru_attach = rip_attach,
1113 .pru_bind = rip_bind,
1114 .pru_connect = rip_connect,
1115 .pru_control = in_control,
1116 .pru_detach = rip_detach,
1117 .pru_disconnect = rip_disconnect,
1118 .pru_peeraddr = in_getpeeraddr,
1119 .pru_send = rip_send,
1120 .pru_shutdown = rip_shutdown,
1121 .pru_sockaddr = in_getsockaddr,
1122 .pru_sosetlabel = in_pcbsosetlabel,
1123 .pru_close = rip_close,