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/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_mroute.h>
71 #include <netipsec/ipsec.h>
74 #include <security/mac/mac_framework.h>
76 VNET_DEFINE(struct inpcbhead, ripcb);
77 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
79 #define V_ripcb VNET(ripcb)
80 #define V_ripcbinfo VNET(ripcbinfo)
83 * Control and data hooks for ipfw, dummynet, divert and so on.
84 * The data hooks are not used here but it is convenient
85 * to keep them all in one place.
87 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
88 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
90 int (*ip_dn_ctl_ptr)(struct sockopt *);
91 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
92 void (*ip_divert_ptr)(struct mbuf *, int);
93 int (*ng_ipfw_input_p)(struct mbuf **, int,
94 struct ip_fw_args *, int);
97 * Hooks for multicast routing. They all default to NULL, so leave them not
98 * initialized and rely on BSS being set to 0.
102 * The socket used to communicate with the multicast routing daemon.
104 VNET_DEFINE(struct socket *, ip_mrouter);
107 * The various mrouter and rsvp functions.
109 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
110 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
111 int (*ip_mrouter_done)(void);
112 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
113 struct ip_moptions *);
114 int (*mrt_ioctl)(u_long, caddr_t, int);
115 int (*legal_vif_num)(int);
116 u_long (*ip_mcast_src)(int);
118 void (*rsvp_input_p)(struct mbuf *m, int off);
119 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
120 void (*ip_rsvp_force_done)(struct socket *);
126 #define INP_PCBHASH_RAW_SIZE 256
127 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
128 (((proto) + (laddr) + (faddr)) % (mask) + 1)
131 rip_inshash(struct inpcb *inp)
133 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
134 struct inpcbhead *pcbhash;
137 INP_INFO_WLOCK_ASSERT(pcbinfo);
138 INP_WLOCK_ASSERT(inp);
140 if (inp->inp_ip_p != 0 &&
141 inp->inp_laddr.s_addr != INADDR_ANY &&
142 inp->inp_faddr.s_addr != INADDR_ANY) {
143 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
144 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
147 pcbhash = &pcbinfo->ipi_hashbase[hash];
148 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
152 rip_delhash(struct inpcb *inp)
155 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
156 INP_WLOCK_ASSERT(inp);
158 LIST_REMOVE(inp, inp_hash);
162 * Raw interface to IP protocol.
166 * Initialize raw connection block q.
169 rip_zone_change(void *tag)
172 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
176 rip_inpcb_init(void *mem, int size, int flags)
178 struct inpcb *inp = mem;
180 INP_LOCK_INIT(inp, "inp", "rawinp");
188 INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
191 V_ripcbinfo.ipi_vnet = curvnet;
193 V_ripcbinfo.ipi_listhead = &V_ripcb;
194 V_ripcbinfo.ipi_hashbase =
195 hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
196 V_ripcbinfo.ipi_porthashbase =
197 hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
198 V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
199 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
200 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
201 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
202 EVENTHANDLER_PRI_ANY);
210 hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
211 V_ripcbinfo.ipi_hashmask);
212 hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
213 V_ripcbinfo.ipi_porthashmask);
218 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
219 struct sockaddr_in *ripsrc)
223 INP_RLOCK_ASSERT(last);
226 /* check AH/ESP integrity. */
227 if (ipsec4_in_reject(n, last)) {
232 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
235 /* Check the minimum TTL for socket. */
236 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
239 struct mbuf *opts = NULL;
242 so = last->inp_socket;
243 if ((last->inp_flags & INP_CONTROLOPTS) ||
244 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
245 ip_savecontrol(last, &opts, ip, n);
246 SOCKBUF_LOCK(&so->so_rcv);
247 if (sbappendaddr_locked(&so->so_rcv,
248 (struct sockaddr *)ripsrc, n, opts) == 0) {
249 /* should notify about lost packet */
253 SOCKBUF_UNLOCK(&so->so_rcv);
255 sorwakeup_locked(so);
262 * Setup generic address and protocol structures for raw_input routine, then
263 * pass them along with mbuf chain.
266 rip_input(struct mbuf *m, int off)
269 struct ip *ip = mtod(m, struct ip *);
270 int proto = ip->ip_p;
271 struct inpcb *inp, *last;
272 struct sockaddr_in ripsrc;
275 bzero(&ripsrc, sizeof(ripsrc));
276 ripsrc.sin_len = sizeof(ripsrc);
277 ripsrc.sin_family = AF_INET;
278 ripsrc.sin_addr = ip->ip_src;
281 ifp = m->m_pkthdr.rcvif;
283 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
284 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
285 INP_INFO_RLOCK(&V_ripcbinfo);
286 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
287 if (inp->inp_ip_p != proto)
290 /* XXX inp locking */
291 if ((inp->inp_vflag & INP_IPV4) == 0)
294 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
296 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
298 if (jailed_without_vnet(inp->inp_cred)) {
300 * XXX: If faddr was bound to multicast group,
301 * jailed raw socket will drop datagram.
303 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
309 n = m_copy(m, 0, (int)M_COPYALL);
311 (void) rip_append(last, ip, n, &ripsrc);
312 /* XXX count dropped packet */
318 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
319 if (inp->inp_ip_p && inp->inp_ip_p != proto)
322 /* XXX inp locking */
323 if ((inp->inp_vflag & INP_IPV4) == 0)
326 if (!in_nullhost(inp->inp_laddr) &&
327 !in_hosteq(inp->inp_laddr, ip->ip_dst))
329 if (!in_nullhost(inp->inp_faddr) &&
330 !in_hosteq(inp->inp_faddr, ip->ip_src))
332 if (jailed_without_vnet(inp->inp_cred)) {
334 * Allow raw socket in jail to receive multicast;
335 * assume process had PRIV_NETINET_RAW at attach,
336 * and fall through into normal filter path if so.
338 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
339 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
343 * If this raw socket has multicast state, and we
344 * have received a multicast, check if this socket
345 * should receive it, as multicast filtering is now
346 * the responsibility of the transport layer.
348 if (inp->inp_moptions != NULL &&
349 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
351 * If the incoming datagram is for IGMP, allow it
352 * through unconditionally to the raw socket.
354 * In the case of IGMPv2, we may not have explicitly
355 * joined the group, and may have set IFF_ALLMULTI
356 * on the interface. imo_multi_filter() may discard
357 * control traffic we actually need to see.
359 * Userland multicast routing daemons should continue
360 * filter the control traffic appropriately.
364 blocked = MCAST_PASS;
365 if (proto != IPPROTO_IGMP) {
366 struct sockaddr_in group;
368 bzero(&group, sizeof(struct sockaddr_in));
369 group.sin_len = sizeof(struct sockaddr_in);
370 group.sin_family = AF_INET;
371 group.sin_addr = ip->ip_dst;
373 blocked = imo_multi_filter(inp->inp_moptions,
375 (struct sockaddr *)&group,
376 (struct sockaddr *)&ripsrc);
379 if (blocked != MCAST_PASS) {
380 IPSTAT_INC(ips_notmember);
387 n = m_copy(m, 0, (int)M_COPYALL);
389 (void) rip_append(last, ip, n, &ripsrc);
390 /* XXX count dropped packet */
396 INP_INFO_RUNLOCK(&V_ripcbinfo);
398 if (rip_append(last, ip, m, &ripsrc) != 0)
399 IPSTAT_INC(ips_delivered);
403 IPSTAT_INC(ips_noproto);
404 IPSTAT_DEC(ips_delivered);
409 * Generate IP header and pass packet to ip_output. Tack on options user may
410 * have setup with control call.
413 rip_output(struct mbuf *m, struct socket *so, u_long dst)
417 struct inpcb *inp = sotoinpcb(so);
418 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
422 * If the user handed us a complete IP packet, use it. Otherwise,
423 * allocate an mbuf for a header and fill it in.
425 if ((inp->inp_flags & INP_HDRINCL) == 0) {
426 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
430 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
435 ip = mtod(m, struct ip *);
436 ip->ip_tos = inp->inp_ip_tos;
437 if (inp->inp_flags & INP_DONTFRAG)
441 ip->ip_p = inp->inp_ip_p;
442 ip->ip_len = m->m_pkthdr.len;
443 ip->ip_src = inp->inp_laddr;
444 if (jailed(inp->inp_cred)) {
446 * prison_local_ip4() would be good enough but would
447 * let a source of INADDR_ANY pass, which we do not
448 * want to see from jails. We do not go through the
449 * pain of in_pcbladdr() for raw sockets.
451 if (ip->ip_src.s_addr == INADDR_ANY)
452 error = prison_get_ip4(inp->inp_cred,
455 error = prison_local_ip4(inp->inp_cred,
463 ip->ip_dst.s_addr = dst;
464 ip->ip_ttl = inp->inp_ip_ttl;
466 if (m->m_pkthdr.len > IP_MAXPACKET) {
471 ip = mtod(m, struct ip *);
472 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
480 * Don't allow both user specified and setsockopt options,
481 * and don't allow packet length sizes that will crash.
483 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
484 || (ip->ip_len > m->m_pkthdr.len)
485 || (ip->ip_len < (ip->ip_hl << 2))) {
491 ip->ip_id = ip_newid();
494 * XXX prevent ip_output from overwriting header fields.
496 flags |= IP_RAWOUTPUT;
497 IPSTAT_INC(ips_rawout);
500 if (inp->inp_flags & INP_ONESBCAST)
501 flags |= IP_SENDONES;
504 mac_inpcb_create_mbuf(inp, m);
507 error = ip_output(m, inp->inp_options, NULL, flags,
508 inp->inp_moptions, inp);
514 * Raw IP socket option processing.
516 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
517 * only be created by a privileged process, and as such, socket option
518 * operations to manage system properties on any raw socket were allowed to
519 * take place without explicit additional access control checks. However,
520 * raw sockets can now also be created in jail(), and therefore explicit
521 * checks are now required. Likewise, raw sockets can be used by a process
522 * after it gives up privilege, so some caution is required. For options
523 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
524 * performed in ip_ctloutput() and therefore no check occurs here.
525 * Unilaterally checking priv_check() here breaks normal IP socket option
526 * operations on raw sockets.
528 * When adding new socket options here, make sure to add access control
529 * checks here as necessary.
532 rip_ctloutput(struct socket *so, struct sockopt *sopt)
534 struct inpcb *inp = sotoinpcb(so);
537 if (sopt->sopt_level != IPPROTO_IP) {
538 if ((sopt->sopt_level == SOL_SOCKET) &&
539 (sopt->sopt_name == SO_SETFIB)) {
540 inp->inp_inc.inc_fibnum = so->so_fibnum;
547 switch (sopt->sopt_dir) {
549 switch (sopt->sopt_name) {
551 optval = inp->inp_flags & INP_HDRINCL;
552 error = sooptcopyout(sopt, &optval, sizeof optval);
555 case IP_FW3: /* generic ipfw v.3 functions */
556 case IP_FW_ADD: /* ADD actually returns the body... */
558 case IP_FW_TABLE_GETSIZE:
559 case IP_FW_TABLE_LIST:
560 case IP_FW_NAT_GET_CONFIG:
561 case IP_FW_NAT_GET_LOG:
562 if (V_ip_fw_ctl_ptr != NULL)
563 error = V_ip_fw_ctl_ptr(sopt);
568 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
569 case IP_DUMMYNET_GET:
570 if (ip_dn_ctl_ptr != NULL)
571 error = ip_dn_ctl_ptr(sopt);
584 case MRT_API_SUPPORT:
586 case MRT_ADD_BW_UPCALL:
587 case MRT_DEL_BW_UPCALL:
588 error = priv_check(curthread, PRIV_NETINET_MROUTE);
591 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
596 error = ip_ctloutput(so, sopt);
602 switch (sopt->sopt_name) {
604 error = sooptcopyin(sopt, &optval, sizeof optval,
609 inp->inp_flags |= INP_HDRINCL;
611 inp->inp_flags &= ~INP_HDRINCL;
614 case IP_FW3: /* generic ipfw v.3 functions */
620 case IP_FW_TABLE_ADD:
621 case IP_FW_TABLE_DEL:
622 case IP_FW_TABLE_FLUSH:
625 if (V_ip_fw_ctl_ptr != NULL)
626 error = V_ip_fw_ctl_ptr(sopt);
631 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
632 case IP_DUMMYNET_CONFIGURE:
633 case IP_DUMMYNET_DEL:
634 case IP_DUMMYNET_FLUSH:
635 if (ip_dn_ctl_ptr != NULL)
636 error = ip_dn_ctl_ptr(sopt);
638 error = ENOPROTOOPT ;
642 error = priv_check(curthread, PRIV_NETINET_MROUTE);
645 error = ip_rsvp_init(so);
649 error = priv_check(curthread, PRIV_NETINET_MROUTE);
652 error = ip_rsvp_done();
656 case IP_RSVP_VIF_OFF:
657 error = priv_check(curthread, PRIV_NETINET_MROUTE);
660 error = ip_rsvp_vif ?
661 ip_rsvp_vif(so, sopt) : EINVAL;
672 case MRT_API_SUPPORT:
674 case MRT_ADD_BW_UPCALL:
675 case MRT_DEL_BW_UPCALL:
676 error = priv_check(curthread, PRIV_NETINET_MROUTE);
679 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
684 error = ip_ctloutput(so, sopt);
694 * This function exists solely to receive the PRC_IFDOWN messages which are
695 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
696 * in_ifadown() to remove all routes corresponding to that address. It also
697 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
701 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
703 struct in_ifaddr *ia;
711 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
712 if (ia->ia_ifa.ifa_addr == sa
713 && (ia->ia_flags & IFA_ROUTE)) {
714 ifa_ref(&ia->ia_ifa);
717 * in_ifscrub kills the interface route.
719 in_ifscrub(ia->ia_ifp, ia);
721 * in_ifadown gets rid of all the rest of the
722 * routes. This is not quite the right thing
723 * to do, but at least if we are running a
724 * routing process they will come back.
726 in_ifadown(&ia->ia_ifa, 0);
727 ifa_free(&ia->ia_ifa);
731 if (ia == NULL) /* If ia matched, already unlocked. */
737 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
738 if (ia->ia_ifa.ifa_addr == sa)
741 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
745 ifa_ref(&ia->ia_ifa);
748 ifp = ia->ia_ifa.ifa_ifp;
750 if ((ifp->if_flags & IFF_LOOPBACK)
751 || (ifp->if_flags & IFF_POINTOPOINT))
754 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
756 ia->ia_flags |= IFA_ROUTE;
757 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
758 ifa_free(&ia->ia_ifa);
763 u_long rip_sendspace = 9216;
764 u_long rip_recvspace = 9216;
766 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
767 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
768 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
769 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
772 rip_attach(struct socket *so, int proto, struct thread *td)
778 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
780 error = priv_check(td, PRIV_NETINET_RAW);
783 if (proto >= IPPROTO_MAX || proto < 0)
784 return EPROTONOSUPPORT;
785 error = soreserve(so, rip_sendspace, rip_recvspace);
788 INP_INFO_WLOCK(&V_ripcbinfo);
789 error = in_pcballoc(so, &V_ripcbinfo);
791 INP_INFO_WUNLOCK(&V_ripcbinfo);
794 inp = (struct inpcb *)so->so_pcb;
795 inp->inp_vflag |= INP_IPV4;
796 inp->inp_ip_p = proto;
797 inp->inp_ip_ttl = V_ip_defttl;
799 INP_INFO_WUNLOCK(&V_ripcbinfo);
805 rip_detach(struct socket *so)
810 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
811 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
812 ("rip_detach: not closed"));
814 INP_INFO_WLOCK(&V_ripcbinfo);
817 if (so == V_ip_mrouter && ip_mrouter_done)
819 if (ip_rsvp_force_done)
820 ip_rsvp_force_done(so);
821 if (so == V_ip_rsvpd)
825 INP_INFO_WUNLOCK(&V_ripcbinfo);
829 rip_dodisconnect(struct socket *so, struct inpcb *inp)
832 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
833 INP_WLOCK_ASSERT(inp);
836 inp->inp_faddr.s_addr = INADDR_ANY;
839 so->so_state &= ~SS_ISCONNECTED;
844 rip_abort(struct socket *so)
849 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
851 INP_INFO_WLOCK(&V_ripcbinfo);
853 rip_dodisconnect(so, inp);
855 INP_INFO_WUNLOCK(&V_ripcbinfo);
859 rip_close(struct socket *so)
864 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
866 INP_INFO_WLOCK(&V_ripcbinfo);
868 rip_dodisconnect(so, inp);
870 INP_INFO_WUNLOCK(&V_ripcbinfo);
874 rip_disconnect(struct socket *so)
878 if ((so->so_state & SS_ISCONNECTED) == 0)
882 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
884 INP_INFO_WLOCK(&V_ripcbinfo);
886 rip_dodisconnect(so, inp);
888 INP_INFO_WUNLOCK(&V_ripcbinfo);
893 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
895 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
899 if (nam->sa_len != sizeof(*addr))
902 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
907 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
909 if (TAILQ_EMPTY(&V_ifnet) ||
910 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
911 (addr->sin_addr.s_addr &&
912 (inp->inp_flags & INP_BINDANY) == 0 &&
913 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
914 return (EADDRNOTAVAIL);
916 INP_INFO_WLOCK(&V_ripcbinfo);
919 inp->inp_laddr = addr->sin_addr;
922 INP_INFO_WUNLOCK(&V_ripcbinfo);
927 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
929 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
932 if (nam->sa_len != sizeof(*addr))
934 if (TAILQ_EMPTY(&V_ifnet))
935 return (EADDRNOTAVAIL);
936 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
937 return (EAFNOSUPPORT);
940 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
942 INP_INFO_WLOCK(&V_ripcbinfo);
945 inp->inp_faddr = addr->sin_addr;
949 INP_INFO_WUNLOCK(&V_ripcbinfo);
954 rip_shutdown(struct socket *so)
959 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
968 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
969 struct mbuf *control, struct thread *td)
975 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
978 * Note: 'dst' reads below are unlocked.
980 if (so->so_state & SS_ISCONNECTED) {
985 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
991 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
993 return (rip_output(m, so, dst));
997 rip_pcblist(SYSCTL_HANDLER_ARGS)
1000 struct inpcb *inp, **inp_list;
1005 * The process of preparing the TCB list is too time-consuming and
1006 * resource-intensive to repeat twice on every request.
1008 if (req->oldptr == 0) {
1009 n = V_ripcbinfo.ipi_count;
1010 req->oldidx = 2 * (sizeof xig)
1011 + (n + n/8) * sizeof(struct xinpcb);
1015 if (req->newptr != 0)
1019 * OK, now we're committed to doing something.
1021 INP_INFO_RLOCK(&V_ripcbinfo);
1022 gencnt = V_ripcbinfo.ipi_gencnt;
1023 n = V_ripcbinfo.ipi_count;
1024 INP_INFO_RUNLOCK(&V_ripcbinfo);
1026 xig.xig_len = sizeof xig;
1028 xig.xig_gen = gencnt;
1029 xig.xig_sogen = so_gencnt;
1030 error = SYSCTL_OUT(req, &xig, sizeof xig);
1034 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1038 INP_INFO_RLOCK(&V_ripcbinfo);
1039 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1040 inp = LIST_NEXT(inp, inp_list)) {
1042 if (inp->inp_gencnt <= gencnt &&
1043 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1045 inp_list[i++] = inp;
1049 INP_INFO_RUNLOCK(&V_ripcbinfo);
1053 for (i = 0; i < n; i++) {
1056 if (inp->inp_gencnt <= gencnt) {
1059 bzero(&xi, sizeof(xi));
1060 xi.xi_len = sizeof xi;
1061 /* XXX should avoid extra copy */
1062 bcopy(inp, &xi.xi_inp, sizeof *inp);
1063 if (inp->inp_socket)
1064 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1066 error = SYSCTL_OUT(req, &xi, sizeof xi);
1070 INP_INFO_WLOCK(&V_ripcbinfo);
1071 for (i = 0; i < n; i++) {
1074 if (!in_pcbrele(inp))
1077 INP_INFO_WUNLOCK(&V_ripcbinfo);
1081 * Give the user an updated idea of our state. If the
1082 * generation differs from what we told her before, she knows
1083 * that something happened while we were processing this
1084 * request, and it might be necessary to retry.
1086 INP_INFO_RLOCK(&V_ripcbinfo);
1087 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1088 xig.xig_sogen = so_gencnt;
1089 xig.xig_count = V_ripcbinfo.ipi_count;
1090 INP_INFO_RUNLOCK(&V_ripcbinfo);
1091 error = SYSCTL_OUT(req, &xig, sizeof xig);
1093 free(inp_list, M_TEMP);
1097 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
1098 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1100 struct pr_usrreqs rip_usrreqs = {
1101 .pru_abort = rip_abort,
1102 .pru_attach = rip_attach,
1103 .pru_bind = rip_bind,
1104 .pru_connect = rip_connect,
1105 .pru_control = in_control,
1106 .pru_detach = rip_detach,
1107 .pru_disconnect = rip_disconnect,
1108 .pru_peeraddr = in_getpeeraddr,
1109 .pru_send = rip_send,
1110 .pru_shutdown = rip_shutdown,
1111 .pru_sockaddr = in_getsockaddr,
1112 .pru_sosetlabel = in_pcbsosetlabel,
1113 .pru_close = rip_close,