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>
43 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/rmlock.h>
51 #include <sys/rwlock.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
62 #include <net/if_var.h>
63 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_mroute.h>
76 #include <netipsec/ipsec.h>
79 #include <machine/stdarg.h>
80 #include <security/mac/mac_framework.h>
82 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
83 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
84 &VNET_NAME(ip_defttl), 0,
85 "Maximum TTL on IP packets");
87 VNET_DEFINE(struct inpcbhead, ripcb);
88 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
90 #define V_ripcb VNET(ripcb)
91 #define V_ripcbinfo VNET(ripcbinfo)
94 * Control and data hooks for ipfw, dummynet, divert and so on.
95 * The data hooks are not used here but it is convenient
96 * to keep them all in one place.
98 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
99 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
101 int (*ip_dn_ctl_ptr)(struct sockopt *);
102 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
103 void (*ip_divert_ptr)(struct mbuf *, int);
104 int (*ng_ipfw_input_p)(struct mbuf **, int,
105 struct ip_fw_args *, int);
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 u_long rip_sendspace = 9216;
136 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
137 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
139 u_long rip_recvspace = 9216;
140 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
141 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
147 #define INP_PCBHASH_RAW_SIZE 256
148 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
149 (((proto) + (laddr) + (faddr)) % (mask) + 1)
153 rip_inshash(struct inpcb *inp)
155 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
156 struct inpcbhead *pcbhash;
159 INP_INFO_WLOCK_ASSERT(pcbinfo);
160 INP_WLOCK_ASSERT(inp);
162 if (inp->inp_ip_p != 0 &&
163 inp->inp_laddr.s_addr != INADDR_ANY &&
164 inp->inp_faddr.s_addr != INADDR_ANY) {
165 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
166 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
169 pcbhash = &pcbinfo->ipi_hashbase[hash];
170 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
174 rip_delhash(struct inpcb *inp)
177 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
178 INP_WLOCK_ASSERT(inp);
180 LIST_REMOVE(inp, inp_hash);
185 * Raw interface to IP protocol.
189 * Initialize raw connection block q.
192 rip_zone_change(void *tag)
195 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
199 rip_inpcb_init(void *mem, int size, int flags)
201 struct inpcb *inp = mem;
203 INP_LOCK_INIT(inp, "inp", "rawinp");
211 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
212 1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
213 IPI_HASHFIELDS_NONE);
214 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
215 EVENTHANDLER_PRI_ANY);
223 in_pcbinfo_destroy(&V_ripcbinfo);
229 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
230 struct sockaddr_in *ripsrc)
234 INP_LOCK_ASSERT(last);
237 /* check AH/ESP integrity. */
238 if (ipsec4_in_reject(n, last)) {
243 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
246 /* Check the minimum TTL for socket. */
247 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
250 struct mbuf *opts = NULL;
253 so = last->inp_socket;
254 if ((last->inp_flags & INP_CONTROLOPTS) ||
255 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
256 ip_savecontrol(last, &opts, ip, n);
257 SOCKBUF_LOCK(&so->so_rcv);
258 if (sbappendaddr_locked(&so->so_rcv,
259 (struct sockaddr *)ripsrc, n, opts) == 0) {
260 /* should notify about lost packet */
264 SOCKBUF_UNLOCK(&so->so_rcv);
266 sorwakeup_locked(so);
273 * Setup generic address and protocol structures for raw_input routine, then
274 * pass them along with mbuf chain.
277 rip_input(struct mbuf **mp, int *offp, int proto)
280 struct mbuf *m = *mp;
281 struct ip *ip = mtod(m, struct ip *);
282 struct inpcb *inp, *last;
283 struct sockaddr_in ripsrc;
288 bzero(&ripsrc, sizeof(ripsrc));
289 ripsrc.sin_len = sizeof(ripsrc);
290 ripsrc.sin_family = AF_INET;
291 ripsrc.sin_addr = ip->ip_src;
294 ifp = m->m_pkthdr.rcvif;
296 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
297 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
298 INP_INFO_RLOCK(&V_ripcbinfo);
299 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
300 if (inp->inp_ip_p != proto)
303 /* XXX inp locking */
304 if ((inp->inp_vflag & INP_IPV4) == 0)
307 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
309 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
311 if (jailed_without_vnet(inp->inp_cred)) {
313 * XXX: If faddr was bound to multicast group,
314 * jailed raw socket will drop datagram.
316 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
322 n = m_copy(m, 0, (int)M_COPYALL);
324 (void) rip_append(last, ip, n, &ripsrc);
325 /* XXX count dropped packet */
331 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
332 if (inp->inp_ip_p && inp->inp_ip_p != proto)
335 /* XXX inp locking */
336 if ((inp->inp_vflag & INP_IPV4) == 0)
339 if (!in_nullhost(inp->inp_laddr) &&
340 !in_hosteq(inp->inp_laddr, ip->ip_dst))
342 if (!in_nullhost(inp->inp_faddr) &&
343 !in_hosteq(inp->inp_faddr, ip->ip_src))
345 if (jailed_without_vnet(inp->inp_cred)) {
347 * Allow raw socket in jail to receive multicast;
348 * assume process had PRIV_NETINET_RAW at attach,
349 * and fall through into normal filter path if so.
351 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
352 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
356 * If this raw socket has multicast state, and we
357 * have received a multicast, check if this socket
358 * should receive it, as multicast filtering is now
359 * the responsibility of the transport layer.
361 if (inp->inp_moptions != NULL &&
362 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
364 * If the incoming datagram is for IGMP, allow it
365 * through unconditionally to the raw socket.
367 * In the case of IGMPv2, we may not have explicitly
368 * joined the group, and may have set IFF_ALLMULTI
369 * on the interface. imo_multi_filter() may discard
370 * control traffic we actually need to see.
372 * Userland multicast routing daemons should continue
373 * filter the control traffic appropriately.
377 blocked = MCAST_PASS;
378 if (proto != IPPROTO_IGMP) {
379 struct sockaddr_in group;
381 bzero(&group, sizeof(struct sockaddr_in));
382 group.sin_len = sizeof(struct sockaddr_in);
383 group.sin_family = AF_INET;
384 group.sin_addr = ip->ip_dst;
386 blocked = imo_multi_filter(inp->inp_moptions,
388 (struct sockaddr *)&group,
389 (struct sockaddr *)&ripsrc);
392 if (blocked != MCAST_PASS) {
393 IPSTAT_INC(ips_notmember);
400 n = m_copy(m, 0, (int)M_COPYALL);
402 (void) rip_append(last, ip, n, &ripsrc);
403 /* XXX count dropped packet */
409 INP_INFO_RUNLOCK(&V_ripcbinfo);
411 if (rip_append(last, ip, m, &ripsrc) != 0)
412 IPSTAT_INC(ips_delivered);
416 IPSTAT_INC(ips_noproto);
417 IPSTAT_DEC(ips_delivered);
419 return (IPPROTO_DONE);
423 * Generate IP header and pass packet to ip_output. Tack on options user may
424 * have setup with control call.
427 rip_output(struct mbuf *m, struct socket *so, ...)
431 struct inpcb *inp = sotoinpcb(so);
434 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
438 dst = va_arg(ap, u_long);
442 * If the user handed us a complete IP packet, use it. Otherwise,
443 * allocate an mbuf for a header and fill it in.
445 if ((inp->inp_flags & INP_HDRINCL) == 0) {
446 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
450 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
455 ip = mtod(m, struct ip *);
456 ip->ip_tos = inp->inp_ip_tos;
457 if (inp->inp_flags & INP_DONTFRAG)
458 ip->ip_off = htons(IP_DF);
460 ip->ip_off = htons(0);
461 ip->ip_p = inp->inp_ip_p;
462 ip->ip_len = htons(m->m_pkthdr.len);
463 ip->ip_src = inp->inp_laddr;
464 ip->ip_dst.s_addr = dst;
465 if (jailed(inp->inp_cred)) {
467 * prison_local_ip4() would be good enough but would
468 * let a source of INADDR_ANY pass, which we do not
469 * want to see from jails.
471 if (ip->ip_src.s_addr == INADDR_ANY) {
472 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
475 error = prison_local_ip4(inp->inp_cred,
484 ip->ip_ttl = inp->inp_ip_ttl;
486 if (m->m_pkthdr.len > IP_MAXPACKET) {
491 ip = mtod(m, struct ip *);
492 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
500 * Don't allow both user specified and setsockopt options,
501 * and don't allow packet length sizes that will crash.
503 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
504 || (ntohs(ip->ip_len) > m->m_pkthdr.len)
505 || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
511 * This doesn't allow application to specify ID of zero,
512 * but we got this limitation from the beginning of history.
518 * XXX prevent ip_output from overwriting header fields.
520 flags |= IP_RAWOUTPUT;
521 IPSTAT_INC(ips_rawout);
524 if (inp->inp_flags & INP_ONESBCAST)
525 flags |= IP_SENDONES;
528 mac_inpcb_create_mbuf(inp, m);
531 error = ip_output(m, inp->inp_options, NULL, flags,
532 inp->inp_moptions, inp);
538 * Raw IP socket option processing.
540 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
541 * only be created by a privileged process, and as such, socket option
542 * operations to manage system properties on any raw socket were allowed to
543 * take place without explicit additional access control checks. However,
544 * raw sockets can now also be created in jail(), and therefore explicit
545 * checks are now required. Likewise, raw sockets can be used by a process
546 * after it gives up privilege, so some caution is required. For options
547 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
548 * performed in ip_ctloutput() and therefore no check occurs here.
549 * Unilaterally checking priv_check() here breaks normal IP socket option
550 * operations on raw sockets.
552 * When adding new socket options here, make sure to add access control
553 * checks here as necessary.
555 * XXX-BZ inp locking?
558 rip_ctloutput(struct socket *so, struct sockopt *sopt)
560 struct inpcb *inp = sotoinpcb(so);
563 if (sopt->sopt_level != IPPROTO_IP) {
564 if ((sopt->sopt_level == SOL_SOCKET) &&
565 (sopt->sopt_name == SO_SETFIB)) {
566 inp->inp_inc.inc_fibnum = so->so_fibnum;
573 switch (sopt->sopt_dir) {
575 switch (sopt->sopt_name) {
577 optval = inp->inp_flags & INP_HDRINCL;
578 error = sooptcopyout(sopt, &optval, sizeof optval);
581 case IP_FW3: /* generic ipfw v.3 functions */
582 case IP_FW_ADD: /* ADD actually returns the body... */
584 case IP_FW_TABLE_GETSIZE:
585 case IP_FW_TABLE_LIST:
586 case IP_FW_NAT_GET_CONFIG:
587 case IP_FW_NAT_GET_LOG:
588 if (V_ip_fw_ctl_ptr != NULL)
589 error = V_ip_fw_ctl_ptr(sopt);
594 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
595 case IP_DUMMYNET_GET:
596 if (ip_dn_ctl_ptr != NULL)
597 error = ip_dn_ctl_ptr(sopt);
610 case MRT_API_SUPPORT:
612 case MRT_ADD_BW_UPCALL:
613 case MRT_DEL_BW_UPCALL:
614 error = priv_check(curthread, PRIV_NETINET_MROUTE);
617 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
622 error = ip_ctloutput(so, sopt);
628 switch (sopt->sopt_name) {
630 error = sooptcopyin(sopt, &optval, sizeof optval,
635 inp->inp_flags |= INP_HDRINCL;
637 inp->inp_flags &= ~INP_HDRINCL;
640 case IP_FW3: /* generic ipfw v.3 functions */
646 case IP_FW_TABLE_ADD:
647 case IP_FW_TABLE_DEL:
648 case IP_FW_TABLE_FLUSH:
651 if (V_ip_fw_ctl_ptr != NULL)
652 error = V_ip_fw_ctl_ptr(sopt);
657 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
658 case IP_DUMMYNET_CONFIGURE:
659 case IP_DUMMYNET_DEL:
660 case IP_DUMMYNET_FLUSH:
661 if (ip_dn_ctl_ptr != NULL)
662 error = ip_dn_ctl_ptr(sopt);
664 error = ENOPROTOOPT ;
668 error = priv_check(curthread, PRIV_NETINET_MROUTE);
671 error = ip_rsvp_init(so);
675 error = priv_check(curthread, PRIV_NETINET_MROUTE);
678 error = ip_rsvp_done();
682 case IP_RSVP_VIF_OFF:
683 error = priv_check(curthread, PRIV_NETINET_MROUTE);
686 error = ip_rsvp_vif ?
687 ip_rsvp_vif(so, sopt) : EINVAL;
698 case MRT_API_SUPPORT:
700 case MRT_ADD_BW_UPCALL:
701 case MRT_DEL_BW_UPCALL:
702 error = priv_check(curthread, PRIV_NETINET_MROUTE);
705 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
710 error = ip_ctloutput(so, sopt);
720 * This function exists solely to receive the PRC_IFDOWN messages which are
721 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
722 * in_ifadown() to remove all routes corresponding to that address. It also
723 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
727 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
729 struct rm_priotracker in_ifa_tracker;
730 struct in_ifaddr *ia;
737 IN_IFADDR_RLOCK(&in_ifa_tracker);
738 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
739 if (ia->ia_ifa.ifa_addr == sa
740 && (ia->ia_flags & IFA_ROUTE)) {
741 ifa_ref(&ia->ia_ifa);
742 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
744 * in_scrubprefix() kills the interface route.
746 in_scrubprefix(ia, 0);
748 * in_ifadown gets rid of all the rest of the
749 * routes. This is not quite the right thing
750 * to do, but at least if we are running a
751 * routing process they will come back.
753 in_ifadown(&ia->ia_ifa, 0);
754 ifa_free(&ia->ia_ifa);
758 if (ia == NULL) /* If ia matched, already unlocked. */
759 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
763 IN_IFADDR_RLOCK(&in_ifa_tracker);
764 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
765 if (ia->ia_ifa.ifa_addr == sa)
768 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
769 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
772 ifa_ref(&ia->ia_ifa);
773 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
775 ifp = ia->ia_ifa.ifa_ifp;
777 if ((ifp->if_flags & IFF_LOOPBACK)
778 || (ifp->if_flags & IFF_POINTOPOINT))
781 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
783 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
785 ia->ia_flags |= IFA_ROUTE;
787 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
789 ifa_free(&ia->ia_ifa);
795 rip_attach(struct socket *so, int proto, struct thread *td)
801 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
803 error = priv_check(td, PRIV_NETINET_RAW);
806 if (proto >= IPPROTO_MAX || proto < 0)
807 return EPROTONOSUPPORT;
808 error = soreserve(so, rip_sendspace, rip_recvspace);
811 INP_INFO_WLOCK(&V_ripcbinfo);
812 error = in_pcballoc(so, &V_ripcbinfo);
814 INP_INFO_WUNLOCK(&V_ripcbinfo);
817 inp = (struct inpcb *)so->so_pcb;
818 inp->inp_vflag |= INP_IPV4;
819 inp->inp_ip_p = proto;
820 inp->inp_ip_ttl = V_ip_defttl;
822 INP_INFO_WUNLOCK(&V_ripcbinfo);
828 rip_detach(struct socket *so)
833 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
834 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
835 ("rip_detach: not closed"));
837 INP_INFO_WLOCK(&V_ripcbinfo);
840 if (so == V_ip_mrouter && ip_mrouter_done)
842 if (ip_rsvp_force_done)
843 ip_rsvp_force_done(so);
844 if (so == V_ip_rsvpd)
848 INP_INFO_WUNLOCK(&V_ripcbinfo);
852 rip_dodisconnect(struct socket *so, struct inpcb *inp)
854 struct inpcbinfo *pcbinfo;
856 pcbinfo = inp->inp_pcbinfo;
857 INP_INFO_WLOCK(pcbinfo);
860 inp->inp_faddr.s_addr = INADDR_ANY;
863 so->so_state &= ~SS_ISCONNECTED;
866 INP_INFO_WUNLOCK(pcbinfo);
870 rip_abort(struct socket *so)
875 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
877 rip_dodisconnect(so, inp);
881 rip_close(struct socket *so)
886 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
888 rip_dodisconnect(so, inp);
892 rip_disconnect(struct socket *so)
896 if ((so->so_state & SS_ISCONNECTED) == 0)
900 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
902 rip_dodisconnect(so, inp);
907 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
909 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
913 if (nam->sa_len != sizeof(*addr))
916 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
921 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
923 if (TAILQ_EMPTY(&V_ifnet) ||
924 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
925 (addr->sin_addr.s_addr &&
926 (inp->inp_flags & INP_BINDANY) == 0 &&
927 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
928 return (EADDRNOTAVAIL);
930 INP_INFO_WLOCK(&V_ripcbinfo);
933 inp->inp_laddr = addr->sin_addr;
936 INP_INFO_WUNLOCK(&V_ripcbinfo);
941 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
943 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
946 if (nam->sa_len != sizeof(*addr))
948 if (TAILQ_EMPTY(&V_ifnet))
949 return (EADDRNOTAVAIL);
950 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
951 return (EAFNOSUPPORT);
954 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
956 INP_INFO_WLOCK(&V_ripcbinfo);
959 inp->inp_faddr = addr->sin_addr;
963 INP_INFO_WUNLOCK(&V_ripcbinfo);
968 rip_shutdown(struct socket *so)
973 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
982 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
983 struct mbuf *control, struct thread *td)
989 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
992 * Note: 'dst' reads below are unlocked.
994 if (so->so_state & SS_ISCONNECTED) {
999 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1005 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1007 return (rip_output(m, so, dst));
1012 rip_pcblist(SYSCTL_HANDLER_ARGS)
1015 struct inpcb *inp, **inp_list;
1020 * The process of preparing the TCB list is too time-consuming and
1021 * resource-intensive to repeat twice on every request.
1023 if (req->oldptr == 0) {
1024 n = V_ripcbinfo.ipi_count;
1025 n += imax(n / 8, 10);
1026 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1030 if (req->newptr != 0)
1034 * OK, now we're committed to doing something.
1036 INP_INFO_RLOCK(&V_ripcbinfo);
1037 gencnt = V_ripcbinfo.ipi_gencnt;
1038 n = V_ripcbinfo.ipi_count;
1039 INP_INFO_RUNLOCK(&V_ripcbinfo);
1041 xig.xig_len = sizeof xig;
1043 xig.xig_gen = gencnt;
1044 xig.xig_sogen = so_gencnt;
1045 error = SYSCTL_OUT(req, &xig, sizeof xig);
1049 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1053 INP_INFO_RLOCK(&V_ripcbinfo);
1054 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1055 inp = LIST_NEXT(inp, inp_list)) {
1057 if (inp->inp_gencnt <= gencnt &&
1058 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1060 inp_list[i++] = inp;
1064 INP_INFO_RUNLOCK(&V_ripcbinfo);
1068 for (i = 0; i < n; i++) {
1071 if (inp->inp_gencnt <= gencnt) {
1074 bzero(&xi, sizeof(xi));
1075 xi.xi_len = sizeof xi;
1076 /* XXX should avoid extra copy */
1077 bcopy(inp, &xi.xi_inp, sizeof *inp);
1078 if (inp->inp_socket)
1079 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1081 error = SYSCTL_OUT(req, &xi, sizeof xi);
1085 INP_INFO_WLOCK(&V_ripcbinfo);
1086 for (i = 0; i < n; i++) {
1089 if (!in_pcbrele_rlocked(inp))
1092 INP_INFO_WUNLOCK(&V_ripcbinfo);
1096 * Give the user an updated idea of our state. If the
1097 * generation differs from what we told her before, she knows
1098 * that something happened while we were processing this
1099 * request, and it might be necessary to retry.
1101 INP_INFO_RLOCK(&V_ripcbinfo);
1102 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1103 xig.xig_sogen = so_gencnt;
1104 xig.xig_count = V_ripcbinfo.ipi_count;
1105 INP_INFO_RUNLOCK(&V_ripcbinfo);
1106 error = SYSCTL_OUT(req, &xig, sizeof xig);
1108 free(inp_list, M_TEMP);
1112 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1113 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1114 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1117 struct pr_usrreqs rip_usrreqs = {
1118 .pru_abort = rip_abort,
1119 .pru_attach = rip_attach,
1120 .pru_bind = rip_bind,
1121 .pru_connect = rip_connect,
1122 .pru_control = in_control,
1123 .pru_detach = rip_detach,
1124 .pru_disconnect = rip_disconnect,
1125 .pru_peeraddr = in_getpeeraddr,
1126 .pru_send = rip_send,
1127 .pru_shutdown = rip_shutdown,
1128 .pru_sockaddr = in_getsockaddr,
1129 .pru_sosetlabel = in_pcbsosetlabel,
1130 .pru_close = rip_close,