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$");
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
40 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
48 #include <sys/protosw.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
61 #include <net/if_var.h>
62 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/if_ether.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_mroute.h>
75 #include <netipsec/ipsec.h>
78 #include <machine/stdarg.h>
79 #include <security/mac/mac_framework.h>
81 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
82 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
83 &VNET_NAME(ip_defttl), 0,
84 "Maximum TTL on IP packets");
86 VNET_DEFINE(struct inpcbhead, ripcb);
87 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
89 #define V_ripcb VNET(ripcb)
90 #define V_ripcbinfo VNET(ripcbinfo)
93 * Control and data hooks for ipfw, dummynet, divert and so on.
94 * The data hooks are not used here but it is convenient
95 * to keep them all in one place.
97 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
98 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
100 int (*ip_dn_ctl_ptr)(struct sockopt *);
101 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
102 void (*ip_divert_ptr)(struct mbuf *, int);
103 int (*ng_ipfw_input_p)(struct mbuf **, int,
104 struct ip_fw_args *, int);
108 * Hooks for multicast routing. They all default to NULL, so leave them not
109 * initialized and rely on BSS being set to 0.
113 * The socket used to communicate with the multicast routing daemon.
115 VNET_DEFINE(struct socket *, ip_mrouter);
118 * The various mrouter and rsvp functions.
120 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
121 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_done)(void);
123 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
124 struct ip_moptions *);
125 int (*mrt_ioctl)(u_long, caddr_t, int);
126 int (*legal_vif_num)(int);
127 u_long (*ip_mcast_src)(int);
129 int (*rsvp_input_p)(struct mbuf **, int *, int);
130 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
131 void (*ip_rsvp_force_done)(struct socket *);
134 u_long rip_sendspace = 9216;
135 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
136 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
138 u_long rip_recvspace = 9216;
139 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
140 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
146 #define INP_PCBHASH_RAW_SIZE 256
147 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
148 (((proto) + (laddr) + (faddr)) % (mask) + 1)
152 rip_inshash(struct inpcb *inp)
154 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
155 struct inpcbhead *pcbhash;
158 INP_INFO_WLOCK_ASSERT(pcbinfo);
159 INP_WLOCK_ASSERT(inp);
161 if (inp->inp_ip_p != 0 &&
162 inp->inp_laddr.s_addr != INADDR_ANY &&
163 inp->inp_faddr.s_addr != INADDR_ANY) {
164 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
165 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
168 pcbhash = &pcbinfo->ipi_hashbase[hash];
169 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
173 rip_delhash(struct inpcb *inp)
176 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
177 INP_WLOCK_ASSERT(inp);
179 LIST_REMOVE(inp, inp_hash);
184 * Raw interface to IP protocol.
188 * Initialize raw connection block q.
191 rip_zone_change(void *tag)
194 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
198 rip_inpcb_init(void *mem, int size, int flags)
200 struct inpcb *inp = mem;
202 INP_LOCK_INIT(inp, "inp", "rawinp");
210 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
211 1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
212 IPI_HASHFIELDS_NONE);
213 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
214 EVENTHANDLER_PRI_ANY);
222 in_pcbinfo_destroy(&V_ripcbinfo);
228 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
229 struct sockaddr_in *ripsrc)
233 INP_LOCK_ASSERT(last);
236 /* check AH/ESP integrity. */
237 if (ipsec4_in_reject(n, last)) {
242 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
245 /* Check the minimum TTL for socket. */
246 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
249 struct mbuf *opts = NULL;
252 so = last->inp_socket;
253 if ((last->inp_flags & INP_CONTROLOPTS) ||
254 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
255 ip_savecontrol(last, &opts, ip, n);
256 SOCKBUF_LOCK(&so->so_rcv);
257 if (sbappendaddr_locked(&so->so_rcv,
258 (struct sockaddr *)ripsrc, n, opts) == 0) {
259 /* should notify about lost packet */
263 SOCKBUF_UNLOCK(&so->so_rcv);
265 sorwakeup_locked(so);
272 * Setup generic address and protocol structures for raw_input routine, then
273 * pass them along with mbuf chain.
276 rip_input(struct mbuf **mp, int *offp, int proto)
279 struct mbuf *m = *mp;
280 struct ip *ip = mtod(m, struct ip *);
281 struct inpcb *inp, *last;
282 struct sockaddr_in ripsrc;
287 bzero(&ripsrc, sizeof(ripsrc));
288 ripsrc.sin_len = sizeof(ripsrc);
289 ripsrc.sin_family = AF_INET;
290 ripsrc.sin_addr = ip->ip_src;
293 ifp = m->m_pkthdr.rcvif;
295 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
296 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
297 INP_INFO_RLOCK(&V_ripcbinfo);
298 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
299 if (inp->inp_ip_p != proto)
302 /* XXX inp locking */
303 if ((inp->inp_vflag & INP_IPV4) == 0)
306 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
308 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
310 if (jailed_without_vnet(inp->inp_cred)) {
312 * XXX: If faddr was bound to multicast group,
313 * jailed raw socket will drop datagram.
315 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
321 n = m_copy(m, 0, (int)M_COPYALL);
323 (void) rip_append(last, ip, n, &ripsrc);
324 /* XXX count dropped packet */
330 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
331 if (inp->inp_ip_p && inp->inp_ip_p != proto)
334 /* XXX inp locking */
335 if ((inp->inp_vflag & INP_IPV4) == 0)
338 if (!in_nullhost(inp->inp_laddr) &&
339 !in_hosteq(inp->inp_laddr, ip->ip_dst))
341 if (!in_nullhost(inp->inp_faddr) &&
342 !in_hosteq(inp->inp_faddr, ip->ip_src))
344 if (jailed_without_vnet(inp->inp_cred)) {
346 * Allow raw socket in jail to receive multicast;
347 * assume process had PRIV_NETINET_RAW at attach,
348 * and fall through into normal filter path if so.
350 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
351 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
355 * If this raw socket has multicast state, and we
356 * have received a multicast, check if this socket
357 * should receive it, as multicast filtering is now
358 * the responsibility of the transport layer.
360 if (inp->inp_moptions != NULL &&
361 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
363 * If the incoming datagram is for IGMP, allow it
364 * through unconditionally to the raw socket.
366 * In the case of IGMPv2, we may not have explicitly
367 * joined the group, and may have set IFF_ALLMULTI
368 * on the interface. imo_multi_filter() may discard
369 * control traffic we actually need to see.
371 * Userland multicast routing daemons should continue
372 * filter the control traffic appropriately.
376 blocked = MCAST_PASS;
377 if (proto != IPPROTO_IGMP) {
378 struct sockaddr_in group;
380 bzero(&group, sizeof(struct sockaddr_in));
381 group.sin_len = sizeof(struct sockaddr_in);
382 group.sin_family = AF_INET;
383 group.sin_addr = ip->ip_dst;
385 blocked = imo_multi_filter(inp->inp_moptions,
387 (struct sockaddr *)&group,
388 (struct sockaddr *)&ripsrc);
391 if (blocked != MCAST_PASS) {
392 IPSTAT_INC(ips_notmember);
399 n = m_copy(m, 0, (int)M_COPYALL);
401 (void) rip_append(last, ip, n, &ripsrc);
402 /* XXX count dropped packet */
408 INP_INFO_RUNLOCK(&V_ripcbinfo);
410 if (rip_append(last, ip, m, &ripsrc) != 0)
411 IPSTAT_INC(ips_delivered);
415 IPSTAT_INC(ips_noproto);
416 IPSTAT_DEC(ips_delivered);
418 return (IPPROTO_DONE);
422 * Generate IP header and pass packet to ip_output. Tack on options user may
423 * have setup with control call.
426 rip_output(struct mbuf *m, struct socket *so, ...)
430 struct inpcb *inp = sotoinpcb(so);
433 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
437 dst = va_arg(ap, u_long);
441 * If the user handed us a complete IP packet, use it. Otherwise,
442 * allocate an mbuf for a header and fill it in.
444 if ((inp->inp_flags & INP_HDRINCL) == 0) {
445 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
449 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
454 ip = mtod(m, struct ip *);
455 ip->ip_tos = inp->inp_ip_tos;
456 if (inp->inp_flags & INP_DONTFRAG)
457 ip->ip_off = htons(IP_DF);
459 ip->ip_off = htons(0);
460 ip->ip_p = inp->inp_ip_p;
461 ip->ip_len = htons(m->m_pkthdr.len);
462 ip->ip_src = inp->inp_laddr;
463 ip->ip_dst.s_addr = dst;
464 if (jailed(inp->inp_cred)) {
466 * prison_local_ip4() would be good enough but would
467 * let a source of INADDR_ANY pass, which we do not
468 * want to see from jails.
470 if (ip->ip_src.s_addr == INADDR_ANY) {
471 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
474 error = prison_local_ip4(inp->inp_cred,
483 ip->ip_ttl = inp->inp_ip_ttl;
485 if (m->m_pkthdr.len > IP_MAXPACKET) {
490 ip = mtod(m, struct ip *);
491 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
499 * Don't allow both user specified and setsockopt options,
500 * and don't allow packet length sizes that will crash.
502 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
503 || (ntohs(ip->ip_len) > m->m_pkthdr.len)
504 || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
510 * This doesn't allow application to specify ID of zero,
511 * but we got this limitation from the beginning of history.
517 * XXX prevent ip_output from overwriting header fields.
519 flags |= IP_RAWOUTPUT;
520 IPSTAT_INC(ips_rawout);
523 if (inp->inp_flags & INP_ONESBCAST)
524 flags |= IP_SENDONES;
527 mac_inpcb_create_mbuf(inp, m);
530 error = ip_output(m, inp->inp_options, NULL, flags,
531 inp->inp_moptions, inp);
537 * Raw IP socket option processing.
539 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
540 * only be created by a privileged process, and as such, socket option
541 * operations to manage system properties on any raw socket were allowed to
542 * take place without explicit additional access control checks. However,
543 * raw sockets can now also be created in jail(), and therefore explicit
544 * checks are now required. Likewise, raw sockets can be used by a process
545 * after it gives up privilege, so some caution is required. For options
546 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
547 * performed in ip_ctloutput() and therefore no check occurs here.
548 * Unilaterally checking priv_check() here breaks normal IP socket option
549 * operations on raw sockets.
551 * When adding new socket options here, make sure to add access control
552 * checks here as necessary.
554 * XXX-BZ inp locking?
557 rip_ctloutput(struct socket *so, struct sockopt *sopt)
559 struct inpcb *inp = sotoinpcb(so);
562 if (sopt->sopt_level != IPPROTO_IP) {
563 if ((sopt->sopt_level == SOL_SOCKET) &&
564 (sopt->sopt_name == SO_SETFIB)) {
565 inp->inp_inc.inc_fibnum = so->so_fibnum;
572 switch (sopt->sopt_dir) {
574 switch (sopt->sopt_name) {
576 optval = inp->inp_flags & INP_HDRINCL;
577 error = sooptcopyout(sopt, &optval, sizeof optval);
580 case IP_FW3: /* generic ipfw v.3 functions */
581 case IP_FW_ADD: /* ADD actually returns the body... */
583 case IP_FW_TABLE_GETSIZE:
584 case IP_FW_TABLE_LIST:
585 case IP_FW_NAT_GET_CONFIG:
586 case IP_FW_NAT_GET_LOG:
587 if (V_ip_fw_ctl_ptr != NULL)
588 error = V_ip_fw_ctl_ptr(sopt);
593 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
594 case IP_DUMMYNET_GET:
595 if (ip_dn_ctl_ptr != NULL)
596 error = ip_dn_ctl_ptr(sopt);
609 case MRT_API_SUPPORT:
611 case MRT_ADD_BW_UPCALL:
612 case MRT_DEL_BW_UPCALL:
613 error = priv_check(curthread, PRIV_NETINET_MROUTE);
616 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
621 error = ip_ctloutput(so, sopt);
627 switch (sopt->sopt_name) {
629 error = sooptcopyin(sopt, &optval, sizeof optval,
634 inp->inp_flags |= INP_HDRINCL;
636 inp->inp_flags &= ~INP_HDRINCL;
639 case IP_FW3: /* generic ipfw v.3 functions */
645 case IP_FW_TABLE_ADD:
646 case IP_FW_TABLE_DEL:
647 case IP_FW_TABLE_FLUSH:
650 if (V_ip_fw_ctl_ptr != NULL)
651 error = V_ip_fw_ctl_ptr(sopt);
656 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
657 case IP_DUMMYNET_CONFIGURE:
658 case IP_DUMMYNET_DEL:
659 case IP_DUMMYNET_FLUSH:
660 if (ip_dn_ctl_ptr != NULL)
661 error = ip_dn_ctl_ptr(sopt);
663 error = ENOPROTOOPT ;
667 error = priv_check(curthread, PRIV_NETINET_MROUTE);
670 error = ip_rsvp_init(so);
674 error = priv_check(curthread, PRIV_NETINET_MROUTE);
677 error = ip_rsvp_done();
681 case IP_RSVP_VIF_OFF:
682 error = priv_check(curthread, PRIV_NETINET_MROUTE);
685 error = ip_rsvp_vif ?
686 ip_rsvp_vif(so, sopt) : EINVAL;
697 case MRT_API_SUPPORT:
699 case MRT_ADD_BW_UPCALL:
700 case MRT_DEL_BW_UPCALL:
701 error = priv_check(curthread, PRIV_NETINET_MROUTE);
704 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
709 error = ip_ctloutput(so, sopt);
719 * This function exists solely to receive the PRC_IFDOWN messages which are
720 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
721 * in_ifadown() to remove all routes corresponding to that address. It also
722 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
726 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
728 struct rm_priotracker in_ifa_tracker;
729 struct in_ifaddr *ia;
736 IN_IFADDR_RLOCK(&in_ifa_tracker);
737 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
738 if (ia->ia_ifa.ifa_addr == sa
739 && (ia->ia_flags & IFA_ROUTE)) {
740 ifa_ref(&ia->ia_ifa);
741 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
743 * in_scrubprefix() kills the interface route.
745 in_scrubprefix(ia, 0);
747 * in_ifadown gets rid of all the rest of the
748 * routes. This is not quite the right thing
749 * to do, but at least if we are running a
750 * routing process they will come back.
752 in_ifadown(&ia->ia_ifa, 0);
753 ifa_free(&ia->ia_ifa);
757 if (ia == NULL) /* If ia matched, already unlocked. */
758 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
762 IN_IFADDR_RLOCK(&in_ifa_tracker);
763 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
764 if (ia->ia_ifa.ifa_addr == sa)
767 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
768 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
771 ifa_ref(&ia->ia_ifa);
772 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
774 ifp = ia->ia_ifa.ifa_ifp;
776 if ((ifp->if_flags & IFF_LOOPBACK)
777 || (ifp->if_flags & IFF_POINTOPOINT))
780 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
782 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
784 ia->ia_flags |= IFA_ROUTE;
786 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
788 ifa_free(&ia->ia_ifa);
794 rip_attach(struct socket *so, int proto, struct thread *td)
800 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
802 error = priv_check(td, PRIV_NETINET_RAW);
805 if (proto >= IPPROTO_MAX || proto < 0)
806 return EPROTONOSUPPORT;
807 error = soreserve(so, rip_sendspace, rip_recvspace);
810 INP_INFO_WLOCK(&V_ripcbinfo);
811 error = in_pcballoc(so, &V_ripcbinfo);
813 INP_INFO_WUNLOCK(&V_ripcbinfo);
816 inp = (struct inpcb *)so->so_pcb;
817 inp->inp_vflag |= INP_IPV4;
818 inp->inp_ip_p = proto;
819 inp->inp_ip_ttl = V_ip_defttl;
821 INP_INFO_WUNLOCK(&V_ripcbinfo);
827 rip_detach(struct socket *so)
832 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
833 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
834 ("rip_detach: not closed"));
836 INP_INFO_WLOCK(&V_ripcbinfo);
839 if (so == V_ip_mrouter && ip_mrouter_done)
841 if (ip_rsvp_force_done)
842 ip_rsvp_force_done(so);
843 if (so == V_ip_rsvpd)
847 INP_INFO_WUNLOCK(&V_ripcbinfo);
851 rip_dodisconnect(struct socket *so, struct inpcb *inp)
853 struct inpcbinfo *pcbinfo;
855 pcbinfo = inp->inp_pcbinfo;
856 INP_INFO_WLOCK(pcbinfo);
859 inp->inp_faddr.s_addr = INADDR_ANY;
862 so->so_state &= ~SS_ISCONNECTED;
865 INP_INFO_WUNLOCK(pcbinfo);
869 rip_abort(struct socket *so)
874 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
876 rip_dodisconnect(so, inp);
880 rip_close(struct socket *so)
885 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
887 rip_dodisconnect(so, inp);
891 rip_disconnect(struct socket *so)
895 if ((so->so_state & SS_ISCONNECTED) == 0)
899 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
901 rip_dodisconnect(so, inp);
906 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
908 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
912 if (nam->sa_len != sizeof(*addr))
915 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
920 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
922 if (TAILQ_EMPTY(&V_ifnet) ||
923 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
924 (addr->sin_addr.s_addr &&
925 (inp->inp_flags & INP_BINDANY) == 0 &&
926 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
927 return (EADDRNOTAVAIL);
929 INP_INFO_WLOCK(&V_ripcbinfo);
932 inp->inp_laddr = addr->sin_addr;
935 INP_INFO_WUNLOCK(&V_ripcbinfo);
940 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
942 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
945 if (nam->sa_len != sizeof(*addr))
947 if (TAILQ_EMPTY(&V_ifnet))
948 return (EADDRNOTAVAIL);
949 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
950 return (EAFNOSUPPORT);
953 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
955 INP_INFO_WLOCK(&V_ripcbinfo);
958 inp->inp_faddr = addr->sin_addr;
962 INP_INFO_WUNLOCK(&V_ripcbinfo);
967 rip_shutdown(struct socket *so)
972 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
981 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
982 struct mbuf *control, struct thread *td)
988 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
991 * Note: 'dst' reads below are unlocked.
993 if (so->so_state & SS_ISCONNECTED) {
998 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1004 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1006 return (rip_output(m, so, dst));
1011 rip_pcblist(SYSCTL_HANDLER_ARGS)
1014 struct inpcb *inp, **inp_list;
1019 * The process of preparing the TCB list is too time-consuming and
1020 * resource-intensive to repeat twice on every request.
1022 if (req->oldptr == 0) {
1023 n = V_ripcbinfo.ipi_count;
1024 n += imax(n / 8, 10);
1025 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1029 if (req->newptr != 0)
1033 * OK, now we're committed to doing something.
1035 INP_INFO_RLOCK(&V_ripcbinfo);
1036 gencnt = V_ripcbinfo.ipi_gencnt;
1037 n = V_ripcbinfo.ipi_count;
1038 INP_INFO_RUNLOCK(&V_ripcbinfo);
1040 xig.xig_len = sizeof xig;
1042 xig.xig_gen = gencnt;
1043 xig.xig_sogen = so_gencnt;
1044 error = SYSCTL_OUT(req, &xig, sizeof xig);
1048 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1052 INP_INFO_RLOCK(&V_ripcbinfo);
1053 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1054 inp = LIST_NEXT(inp, inp_list)) {
1056 if (inp->inp_gencnt <= gencnt &&
1057 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1059 inp_list[i++] = inp;
1063 INP_INFO_RUNLOCK(&V_ripcbinfo);
1067 for (i = 0; i < n; i++) {
1070 if (inp->inp_gencnt <= gencnt) {
1073 bzero(&xi, sizeof(xi));
1074 xi.xi_len = sizeof xi;
1075 /* XXX should avoid extra copy */
1076 bcopy(inp, &xi.xi_inp, sizeof *inp);
1077 if (inp->inp_socket)
1078 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1080 error = SYSCTL_OUT(req, &xi, sizeof xi);
1084 INP_INFO_WLOCK(&V_ripcbinfo);
1085 for (i = 0; i < n; i++) {
1088 if (!in_pcbrele_rlocked(inp))
1091 INP_INFO_WUNLOCK(&V_ripcbinfo);
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);
1107 free(inp_list, M_TEMP);
1111 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1112 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1113 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1116 struct pr_usrreqs rip_usrreqs = {
1117 .pru_abort = rip_abort,
1118 .pru_attach = rip_attach,
1119 .pru_bind = rip_bind,
1120 .pru_connect = rip_connect,
1121 .pru_control = in_control,
1122 .pru_detach = rip_detach,
1123 .pru_disconnect = rip_disconnect,
1124 .pru_peeraddr = in_getpeeraddr,
1125 .pru_send = rip_send,
1126 .pru_shutdown = rip_shutdown,
1127 .pru_sockaddr = in_getsockaddr,
1128 .pru_sosetlabel = in_pcbsosetlabel,
1129 .pru_close = rip_close,