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/rwlock.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
60 #include <net/if_var.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_mroute.h>
74 #include <netipsec/ipsec.h>
77 #include <machine/stdarg.h>
78 #include <security/mac/mac_framework.h>
80 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
81 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
82 &VNET_NAME(ip_defttl), 0,
83 "Maximum TTL on IP packets");
85 VNET_DEFINE(struct inpcbhead, ripcb);
86 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
88 #define V_ripcb VNET(ripcb)
89 #define V_ripcbinfo VNET(ripcbinfo)
92 * Control and data hooks for ipfw, dummynet, divert and so on.
93 * The data hooks are not used here but it is convenient
94 * to keep them all in one place.
96 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
97 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
99 int (*ip_dn_ctl_ptr)(struct sockopt *);
100 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
101 void (*ip_divert_ptr)(struct mbuf *, int);
102 int (*ng_ipfw_input_p)(struct mbuf **, int,
103 struct ip_fw_args *, int);
107 * Hooks for multicast routing. They all default to NULL, so leave them not
108 * initialized and rely on BSS being set to 0.
112 * The socket used to communicate with the multicast routing daemon.
114 VNET_DEFINE(struct socket *, ip_mrouter);
117 * The various mrouter and rsvp functions.
119 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
120 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
121 int (*ip_mrouter_done)(void);
122 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
123 struct ip_moptions *);
124 int (*mrt_ioctl)(u_long, caddr_t, int);
125 int (*legal_vif_num)(int);
126 u_long (*ip_mcast_src)(int);
128 int (*rsvp_input_p)(struct mbuf **, int *, int);
129 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
130 void (*ip_rsvp_force_done)(struct socket *);
133 u_long rip_sendspace = 9216;
134 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
135 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
137 u_long rip_recvspace = 9216;
138 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
139 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
145 #define INP_PCBHASH_RAW_SIZE 256
146 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
147 (((proto) + (laddr) + (faddr)) % (mask) + 1)
151 rip_inshash(struct inpcb *inp)
153 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
154 struct inpcbhead *pcbhash;
157 INP_INFO_WLOCK_ASSERT(pcbinfo);
158 INP_WLOCK_ASSERT(inp);
160 if (inp->inp_ip_p != 0 &&
161 inp->inp_laddr.s_addr != INADDR_ANY &&
162 inp->inp_faddr.s_addr != INADDR_ANY) {
163 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
164 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
167 pcbhash = &pcbinfo->ipi_hashbase[hash];
168 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
172 rip_delhash(struct inpcb *inp)
175 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
176 INP_WLOCK_ASSERT(inp);
178 LIST_REMOVE(inp, inp_hash);
183 * Raw interface to IP protocol.
187 * Initialize raw connection block q.
190 rip_zone_change(void *tag)
193 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
197 rip_inpcb_init(void *mem, int size, int flags)
199 struct inpcb *inp = mem;
201 INP_LOCK_INIT(inp, "inp", "rawinp");
209 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
210 1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
211 IPI_HASHFIELDS_NONE);
212 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
213 EVENTHANDLER_PRI_ANY);
221 in_pcbinfo_destroy(&V_ripcbinfo);
227 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
228 struct sockaddr_in *ripsrc)
232 INP_LOCK_ASSERT(last);
235 /* check AH/ESP integrity. */
236 if (ipsec4_in_reject(n, last)) {
241 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
244 /* Check the minimum TTL for socket. */
245 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
248 struct mbuf *opts = NULL;
251 so = last->inp_socket;
252 if ((last->inp_flags & INP_CONTROLOPTS) ||
253 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
254 ip_savecontrol(last, &opts, ip, n);
255 SOCKBUF_LOCK(&so->so_rcv);
256 if (sbappendaddr_locked(&so->so_rcv,
257 (struct sockaddr *)ripsrc, n, opts) == 0) {
258 /* should notify about lost packet */
262 SOCKBUF_UNLOCK(&so->so_rcv);
264 sorwakeup_locked(so);
271 * Setup generic address and protocol structures for raw_input routine, then
272 * pass them along with mbuf chain.
275 rip_input(struct mbuf **mp, int *offp, int proto)
278 struct mbuf *m = *mp;
279 struct ip *ip = mtod(m, struct ip *);
280 struct inpcb *inp, *last;
281 struct sockaddr_in ripsrc;
286 bzero(&ripsrc, sizeof(ripsrc));
287 ripsrc.sin_len = sizeof(ripsrc);
288 ripsrc.sin_family = AF_INET;
289 ripsrc.sin_addr = ip->ip_src;
292 ifp = m->m_pkthdr.rcvif;
294 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
295 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
296 INP_INFO_RLOCK(&V_ripcbinfo);
297 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
298 if (inp->inp_ip_p != proto)
301 /* XXX inp locking */
302 if ((inp->inp_vflag & INP_IPV4) == 0)
305 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
307 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
309 if (jailed_without_vnet(inp->inp_cred)) {
311 * XXX: If faddr was bound to multicast group,
312 * jailed raw socket will drop datagram.
314 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
320 n = m_copy(m, 0, (int)M_COPYALL);
322 (void) rip_append(last, ip, n, &ripsrc);
323 /* XXX count dropped packet */
329 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
330 if (inp->inp_ip_p && inp->inp_ip_p != proto)
333 /* XXX inp locking */
334 if ((inp->inp_vflag & INP_IPV4) == 0)
337 if (!in_nullhost(inp->inp_laddr) &&
338 !in_hosteq(inp->inp_laddr, ip->ip_dst))
340 if (!in_nullhost(inp->inp_faddr) &&
341 !in_hosteq(inp->inp_faddr, ip->ip_src))
343 if (jailed_without_vnet(inp->inp_cred)) {
345 * Allow raw socket in jail to receive multicast;
346 * assume process had PRIV_NETINET_RAW at attach,
347 * and fall through into normal filter path if so.
349 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
350 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
354 * If this raw socket has multicast state, and we
355 * have received a multicast, check if this socket
356 * should receive it, as multicast filtering is now
357 * the responsibility of the transport layer.
359 if (inp->inp_moptions != NULL &&
360 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
362 * If the incoming datagram is for IGMP, allow it
363 * through unconditionally to the raw socket.
365 * In the case of IGMPv2, we may not have explicitly
366 * joined the group, and may have set IFF_ALLMULTI
367 * on the interface. imo_multi_filter() may discard
368 * control traffic we actually need to see.
370 * Userland multicast routing daemons should continue
371 * filter the control traffic appropriately.
375 blocked = MCAST_PASS;
376 if (proto != IPPROTO_IGMP) {
377 struct sockaddr_in group;
379 bzero(&group, sizeof(struct sockaddr_in));
380 group.sin_len = sizeof(struct sockaddr_in);
381 group.sin_family = AF_INET;
382 group.sin_addr = ip->ip_dst;
384 blocked = imo_multi_filter(inp->inp_moptions,
386 (struct sockaddr *)&group,
387 (struct sockaddr *)&ripsrc);
390 if (blocked != MCAST_PASS) {
391 IPSTAT_INC(ips_notmember);
398 n = m_copy(m, 0, (int)M_COPYALL);
400 (void) rip_append(last, ip, n, &ripsrc);
401 /* XXX count dropped packet */
407 INP_INFO_RUNLOCK(&V_ripcbinfo);
409 if (rip_append(last, ip, m, &ripsrc) != 0)
410 IPSTAT_INC(ips_delivered);
414 IPSTAT_INC(ips_noproto);
415 IPSTAT_DEC(ips_delivered);
417 return (IPPROTO_DONE);
421 * Generate IP header and pass packet to ip_output. Tack on options user may
422 * have setup with control call.
425 rip_output(struct mbuf *m, struct socket *so, ...)
429 struct inpcb *inp = sotoinpcb(so);
432 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
436 dst = va_arg(ap, u_long);
440 * If the user handed us a complete IP packet, use it. Otherwise,
441 * allocate an mbuf for a header and fill it in.
443 if ((inp->inp_flags & INP_HDRINCL) == 0) {
444 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
448 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
453 ip = mtod(m, struct ip *);
454 ip->ip_tos = inp->inp_ip_tos;
455 if (inp->inp_flags & INP_DONTFRAG)
456 ip->ip_off = htons(IP_DF);
458 ip->ip_off = htons(0);
459 ip->ip_p = inp->inp_ip_p;
460 ip->ip_len = htons(m->m_pkthdr.len);
461 ip->ip_src = inp->inp_laddr;
462 ip->ip_dst.s_addr = dst;
463 if (jailed(inp->inp_cred)) {
465 * prison_local_ip4() would be good enough but would
466 * let a source of INADDR_ANY pass, which we do not
467 * want to see from jails.
469 if (ip->ip_src.s_addr == INADDR_ANY) {
470 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
473 error = prison_local_ip4(inp->inp_cred,
482 ip->ip_ttl = inp->inp_ip_ttl;
484 if (m->m_pkthdr.len > IP_MAXPACKET) {
489 ip = mtod(m, struct ip *);
490 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
498 * Don't allow both user specified and setsockopt options,
499 * and don't allow packet length sizes that will crash.
501 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
502 || (ntohs(ip->ip_len) > m->m_pkthdr.len)
503 || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
509 * This doesn't allow application to specify ID of zero,
510 * but we got this limitation from the beginning of history.
516 * XXX prevent ip_output from overwriting header fields.
518 flags |= IP_RAWOUTPUT;
519 IPSTAT_INC(ips_rawout);
522 if (inp->inp_flags & INP_ONESBCAST)
523 flags |= IP_SENDONES;
526 mac_inpcb_create_mbuf(inp, m);
529 error = ip_output(m, inp->inp_options, NULL, flags,
530 inp->inp_moptions, inp);
536 * Raw IP socket option processing.
538 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
539 * only be created by a privileged process, and as such, socket option
540 * operations to manage system properties on any raw socket were allowed to
541 * take place without explicit additional access control checks. However,
542 * raw sockets can now also be created in jail(), and therefore explicit
543 * checks are now required. Likewise, raw sockets can be used by a process
544 * after it gives up privilege, so some caution is required. For options
545 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
546 * performed in ip_ctloutput() and therefore no check occurs here.
547 * Unilaterally checking priv_check() here breaks normal IP socket option
548 * operations on raw sockets.
550 * When adding new socket options here, make sure to add access control
551 * checks here as necessary.
553 * XXX-BZ inp locking?
556 rip_ctloutput(struct socket *so, struct sockopt *sopt)
558 struct inpcb *inp = sotoinpcb(so);
561 if (sopt->sopt_level != IPPROTO_IP) {
562 if ((sopt->sopt_level == SOL_SOCKET) &&
563 (sopt->sopt_name == SO_SETFIB)) {
564 inp->inp_inc.inc_fibnum = so->so_fibnum;
571 switch (sopt->sopt_dir) {
573 switch (sopt->sopt_name) {
575 optval = inp->inp_flags & INP_HDRINCL;
576 error = sooptcopyout(sopt, &optval, sizeof optval);
579 case IP_FW3: /* generic ipfw v.3 functions */
580 case IP_FW_ADD: /* ADD actually returns the body... */
582 case IP_FW_TABLE_GETSIZE:
583 case IP_FW_TABLE_LIST:
584 case IP_FW_NAT_GET_CONFIG:
585 case IP_FW_NAT_GET_LOG:
586 if (V_ip_fw_ctl_ptr != NULL)
587 error = V_ip_fw_ctl_ptr(sopt);
592 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
593 case IP_DUMMYNET_GET:
594 if (ip_dn_ctl_ptr != NULL)
595 error = ip_dn_ctl_ptr(sopt);
608 case MRT_API_SUPPORT:
610 case MRT_ADD_BW_UPCALL:
611 case MRT_DEL_BW_UPCALL:
612 error = priv_check(curthread, PRIV_NETINET_MROUTE);
615 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
620 error = ip_ctloutput(so, sopt);
626 switch (sopt->sopt_name) {
628 error = sooptcopyin(sopt, &optval, sizeof optval,
633 inp->inp_flags |= INP_HDRINCL;
635 inp->inp_flags &= ~INP_HDRINCL;
638 case IP_FW3: /* generic ipfw v.3 functions */
644 case IP_FW_TABLE_ADD:
645 case IP_FW_TABLE_DEL:
646 case IP_FW_TABLE_FLUSH:
649 if (V_ip_fw_ctl_ptr != NULL)
650 error = V_ip_fw_ctl_ptr(sopt);
655 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
656 case IP_DUMMYNET_CONFIGURE:
657 case IP_DUMMYNET_DEL:
658 case IP_DUMMYNET_FLUSH:
659 if (ip_dn_ctl_ptr != NULL)
660 error = ip_dn_ctl_ptr(sopt);
662 error = ENOPROTOOPT ;
666 error = priv_check(curthread, PRIV_NETINET_MROUTE);
669 error = ip_rsvp_init(so);
673 error = priv_check(curthread, PRIV_NETINET_MROUTE);
676 error = ip_rsvp_done();
680 case IP_RSVP_VIF_OFF:
681 error = priv_check(curthread, PRIV_NETINET_MROUTE);
684 error = ip_rsvp_vif ?
685 ip_rsvp_vif(so, sopt) : EINVAL;
696 case MRT_API_SUPPORT:
698 case MRT_ADD_BW_UPCALL:
699 case MRT_DEL_BW_UPCALL:
700 error = priv_check(curthread, PRIV_NETINET_MROUTE);
703 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
708 error = ip_ctloutput(so, sopt);
718 * This function exists solely to receive the PRC_IFDOWN messages which are
719 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
720 * in_ifadown() to remove all routes corresponding to that address. It also
721 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
725 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
727 struct in_ifaddr *ia;
735 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
736 if (ia->ia_ifa.ifa_addr == sa
737 && (ia->ia_flags & IFA_ROUTE)) {
738 ifa_ref(&ia->ia_ifa);
741 * in_scrubprefix() kills the interface route.
743 in_scrubprefix(ia, 0);
745 * in_ifadown gets rid of all the rest of the
746 * routes. This is not quite the right thing
747 * to do, but at least if we are running a
748 * routing process they will come back.
750 in_ifadown(&ia->ia_ifa, 0);
751 ifa_free(&ia->ia_ifa);
755 if (ia == NULL) /* If ia matched, already unlocked. */
761 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
762 if (ia->ia_ifa.ifa_addr == sa)
765 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
769 ifa_ref(&ia->ia_ifa);
772 ifp = ia->ia_ifa.ifa_ifp;
774 if ((ifp->if_flags & IFF_LOOPBACK)
775 || (ifp->if_flags & IFF_POINTOPOINT))
778 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
780 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
782 ia->ia_flags |= IFA_ROUTE;
784 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
786 ifa_free(&ia->ia_ifa);
792 rip_attach(struct socket *so, int proto, struct thread *td)
798 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
800 error = priv_check(td, PRIV_NETINET_RAW);
803 if (proto >= IPPROTO_MAX || proto < 0)
804 return EPROTONOSUPPORT;
805 error = soreserve(so, rip_sendspace, rip_recvspace);
808 INP_INFO_WLOCK(&V_ripcbinfo);
809 error = in_pcballoc(so, &V_ripcbinfo);
811 INP_INFO_WUNLOCK(&V_ripcbinfo);
814 inp = (struct inpcb *)so->so_pcb;
815 inp->inp_vflag |= INP_IPV4;
816 inp->inp_ip_p = proto;
817 inp->inp_ip_ttl = V_ip_defttl;
819 INP_INFO_WUNLOCK(&V_ripcbinfo);
825 rip_detach(struct socket *so)
830 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
831 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
832 ("rip_detach: not closed"));
834 INP_INFO_WLOCK(&V_ripcbinfo);
837 if (so == V_ip_mrouter && ip_mrouter_done)
839 if (ip_rsvp_force_done)
840 ip_rsvp_force_done(so);
841 if (so == V_ip_rsvpd)
845 INP_INFO_WUNLOCK(&V_ripcbinfo);
849 rip_dodisconnect(struct socket *so, struct inpcb *inp)
851 struct inpcbinfo *pcbinfo;
853 pcbinfo = inp->inp_pcbinfo;
854 INP_INFO_WLOCK(pcbinfo);
857 inp->inp_faddr.s_addr = INADDR_ANY;
860 so->so_state &= ~SS_ISCONNECTED;
863 INP_INFO_WUNLOCK(pcbinfo);
867 rip_abort(struct socket *so)
872 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
874 rip_dodisconnect(so, inp);
878 rip_close(struct socket *so)
883 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
885 rip_dodisconnect(so, inp);
889 rip_disconnect(struct socket *so)
893 if ((so->so_state & SS_ISCONNECTED) == 0)
897 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
899 rip_dodisconnect(so, inp);
904 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
906 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
910 if (nam->sa_len != sizeof(*addr))
913 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
918 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
920 if (TAILQ_EMPTY(&V_ifnet) ||
921 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
922 (addr->sin_addr.s_addr &&
923 (inp->inp_flags & INP_BINDANY) == 0 &&
924 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
925 return (EADDRNOTAVAIL);
927 INP_INFO_WLOCK(&V_ripcbinfo);
930 inp->inp_laddr = addr->sin_addr;
933 INP_INFO_WUNLOCK(&V_ripcbinfo);
938 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
940 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
943 if (nam->sa_len != sizeof(*addr))
945 if (TAILQ_EMPTY(&V_ifnet))
946 return (EADDRNOTAVAIL);
947 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
948 return (EAFNOSUPPORT);
951 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
953 INP_INFO_WLOCK(&V_ripcbinfo);
956 inp->inp_faddr = addr->sin_addr;
960 INP_INFO_WUNLOCK(&V_ripcbinfo);
965 rip_shutdown(struct socket *so)
970 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
979 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
980 struct mbuf *control, struct thread *td)
986 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
989 * Note: 'dst' reads below are unlocked.
991 if (so->so_state & SS_ISCONNECTED) {
996 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1002 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1004 return (rip_output(m, so, dst));
1009 rip_pcblist(SYSCTL_HANDLER_ARGS)
1012 struct inpcb *inp, **inp_list;
1017 * The process of preparing the TCB list is too time-consuming and
1018 * resource-intensive to repeat twice on every request.
1020 if (req->oldptr == 0) {
1021 n = V_ripcbinfo.ipi_count;
1022 n += imax(n / 8, 10);
1023 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1027 if (req->newptr != 0)
1031 * OK, now we're committed to doing something.
1033 INP_INFO_RLOCK(&V_ripcbinfo);
1034 gencnt = V_ripcbinfo.ipi_gencnt;
1035 n = V_ripcbinfo.ipi_count;
1036 INP_INFO_RUNLOCK(&V_ripcbinfo);
1038 xig.xig_len = sizeof xig;
1040 xig.xig_gen = gencnt;
1041 xig.xig_sogen = so_gencnt;
1042 error = SYSCTL_OUT(req, &xig, sizeof xig);
1046 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1050 INP_INFO_RLOCK(&V_ripcbinfo);
1051 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1052 inp = LIST_NEXT(inp, inp_list)) {
1054 if (inp->inp_gencnt <= gencnt &&
1055 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1057 inp_list[i++] = inp;
1061 INP_INFO_RUNLOCK(&V_ripcbinfo);
1065 for (i = 0; i < n; i++) {
1068 if (inp->inp_gencnt <= gencnt) {
1071 bzero(&xi, sizeof(xi));
1072 xi.xi_len = sizeof xi;
1073 /* XXX should avoid extra copy */
1074 bcopy(inp, &xi.xi_inp, sizeof *inp);
1075 if (inp->inp_socket)
1076 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1078 error = SYSCTL_OUT(req, &xi, sizeof xi);
1082 INP_INFO_WLOCK(&V_ripcbinfo);
1083 for (i = 0; i < n; i++) {
1086 if (!in_pcbrele_rlocked(inp))
1089 INP_INFO_WUNLOCK(&V_ripcbinfo);
1093 * Give the user an updated idea of our state. If the
1094 * generation differs from what we told her before, she knows
1095 * that something happened while we were processing this
1096 * request, and it might be necessary to retry.
1098 INP_INFO_RLOCK(&V_ripcbinfo);
1099 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1100 xig.xig_sogen = so_gencnt;
1101 xig.xig_count = V_ripcbinfo.ipi_count;
1102 INP_INFO_RUNLOCK(&V_ripcbinfo);
1103 error = SYSCTL_OUT(req, &xig, sizeof xig);
1105 free(inp_list, M_TEMP);
1109 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1110 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1111 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1114 struct pr_usrreqs rip_usrreqs = {
1115 .pru_abort = rip_abort,
1116 .pru_attach = rip_attach,
1117 .pru_bind = rip_bind,
1118 .pru_connect = rip_connect,
1119 .pru_control = in_control,
1120 .pru_detach = rip_detach,
1121 .pru_disconnect = rip_disconnect,
1122 .pru_peeraddr = in_getpeeraddr,
1123 .pru_send = rip_send,
1124 .pru_shutdown = rip_shutdown,
1125 .pru_sockaddr = in_getsockaddr,
1126 .pru_sosetlabel = in_pcbsosetlabel,
1127 .pru_close = rip_close,