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 and dummynet.
84 * The data hooks are not used here but it is convenient
85 * to keep them all in one place.
87 int (*ip_fw_ctl_ptr)(struct sockopt *) = NULL;
88 int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL;
89 int (*ip_fw_chk_ptr)(struct ip_fw_args *args) = NULL;
90 int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa) = NULL;
93 * Hooks for multicast routing. They all default to NULL, so leave them not
94 * initialized and rely on BSS being set to 0.
98 * The socket used to communicate with the multicast routing daemon.
100 VNET_DEFINE(struct socket *, ip_mrouter);
103 * The various mrouter and rsvp functions.
105 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
106 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
107 int (*ip_mrouter_done)(void);
108 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
109 struct ip_moptions *);
110 int (*mrt_ioctl)(u_long, caddr_t, int);
111 int (*legal_vif_num)(int);
112 u_long (*ip_mcast_src)(int);
114 void (*rsvp_input_p)(struct mbuf *m, int off);
115 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
116 void (*ip_rsvp_force_done)(struct socket *);
122 #define INP_PCBHASH_RAW_SIZE 256
123 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
124 (((proto) + (laddr) + (faddr)) % (mask) + 1)
127 rip_inshash(struct inpcb *inp)
129 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
130 struct inpcbhead *pcbhash;
133 INP_INFO_WLOCK_ASSERT(pcbinfo);
134 INP_WLOCK_ASSERT(inp);
136 if (inp->inp_ip_p != 0 &&
137 inp->inp_laddr.s_addr != INADDR_ANY &&
138 inp->inp_faddr.s_addr != INADDR_ANY) {
139 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
140 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
143 pcbhash = &pcbinfo->ipi_hashbase[hash];
144 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
148 rip_delhash(struct inpcb *inp)
151 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
152 INP_WLOCK_ASSERT(inp);
154 LIST_REMOVE(inp, inp_hash);
158 * Raw interface to IP protocol.
162 * Initialize raw connection block q.
165 rip_zone_change(void *tag)
168 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
172 rip_inpcb_init(void *mem, int size, int flags)
174 struct inpcb *inp = mem;
176 INP_LOCK_INIT(inp, "inp", "rawinp");
184 INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
187 V_ripcbinfo.ipi_vnet = curvnet;
189 V_ripcbinfo.ipi_listhead = &V_ripcb;
190 V_ripcbinfo.ipi_hashbase =
191 hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
192 V_ripcbinfo.ipi_porthashbase =
193 hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
194 V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
195 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
196 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
197 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
198 EVENTHANDLER_PRI_ANY);
206 hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
207 V_ripcbinfo.ipi_hashmask);
208 hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
209 V_ripcbinfo.ipi_porthashmask);
214 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
215 struct sockaddr_in *ripsrc)
219 INP_RLOCK_ASSERT(last);
222 /* check AH/ESP integrity. */
223 if (ipsec4_in_reject(n, last)) {
228 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
231 /* Check the minimum TTL for socket. */
232 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
235 struct mbuf *opts = NULL;
238 so = last->inp_socket;
239 if ((last->inp_flags & INP_CONTROLOPTS) ||
240 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
241 ip_savecontrol(last, &opts, ip, n);
242 SOCKBUF_LOCK(&so->so_rcv);
243 if (sbappendaddr_locked(&so->so_rcv,
244 (struct sockaddr *)ripsrc, n, opts) == 0) {
245 /* should notify about lost packet */
249 SOCKBUF_UNLOCK(&so->so_rcv);
251 sorwakeup_locked(so);
258 * Setup generic address and protocol structures for raw_input routine, then
259 * pass them along with mbuf chain.
262 rip_input(struct mbuf *m, int off)
265 struct ip *ip = mtod(m, struct ip *);
266 int proto = ip->ip_p;
267 struct inpcb *inp, *last;
268 struct sockaddr_in ripsrc;
271 bzero(&ripsrc, sizeof(ripsrc));
272 ripsrc.sin_len = sizeof(ripsrc);
273 ripsrc.sin_family = AF_INET;
274 ripsrc.sin_addr = ip->ip_src;
277 ifp = m->m_pkthdr.rcvif;
279 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
280 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
281 INP_INFO_RLOCK(&V_ripcbinfo);
282 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
283 if (inp->inp_ip_p != proto)
286 /* XXX inp locking */
287 if ((inp->inp_vflag & INP_IPV4) == 0)
290 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
292 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
294 if (jailed(inp->inp_cred)) {
296 * XXX: If faddr was bound to multicast group,
297 * jailed raw socket will drop datagram.
299 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
305 n = m_copy(m, 0, (int)M_COPYALL);
307 (void) rip_append(last, ip, n, &ripsrc);
308 /* XXX count dropped packet */
314 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
315 if (inp->inp_ip_p && inp->inp_ip_p != proto)
318 /* XXX inp locking */
319 if ((inp->inp_vflag & INP_IPV4) == 0)
322 if (!in_nullhost(inp->inp_laddr) &&
323 !in_hosteq(inp->inp_laddr, ip->ip_dst))
325 if (!in_nullhost(inp->inp_faddr) &&
326 !in_hosteq(inp->inp_faddr, ip->ip_src))
328 if (jailed(inp->inp_cred)) {
330 * Allow raw socket in jail to receive multicast;
331 * assume process had PRIV_NETINET_RAW at attach,
332 * and fall through into normal filter path if so.
334 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
335 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
339 * If this raw socket has multicast state, and we
340 * have received a multicast, check if this socket
341 * should receive it, as multicast filtering is now
342 * the responsibility of the transport layer.
344 if (inp->inp_moptions != NULL &&
345 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
346 struct sockaddr_in group;
349 bzero(&group, sizeof(struct sockaddr_in));
350 group.sin_len = sizeof(struct sockaddr_in);
351 group.sin_family = AF_INET;
352 group.sin_addr = ip->ip_dst;
354 blocked = imo_multi_filter(inp->inp_moptions, ifp,
355 (struct sockaddr *)&group,
356 (struct sockaddr *)&ripsrc);
357 if (blocked != MCAST_PASS) {
358 IPSTAT_INC(ips_notmember);
365 n = m_copy(m, 0, (int)M_COPYALL);
367 (void) rip_append(last, ip, n, &ripsrc);
368 /* XXX count dropped packet */
374 INP_INFO_RUNLOCK(&V_ripcbinfo);
376 if (rip_append(last, ip, m, &ripsrc) != 0)
377 IPSTAT_INC(ips_delivered);
381 IPSTAT_INC(ips_noproto);
382 IPSTAT_DEC(ips_delivered);
387 * Generate IP header and pass packet to ip_output. Tack on options user may
388 * have setup with control call.
391 rip_output(struct mbuf *m, struct socket *so, u_long dst)
395 struct inpcb *inp = sotoinpcb(so);
396 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
400 * If the user handed us a complete IP packet, use it. Otherwise,
401 * allocate an mbuf for a header and fill it in.
403 if ((inp->inp_flags & INP_HDRINCL) == 0) {
404 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
408 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
413 ip = mtod(m, struct ip *);
414 ip->ip_tos = inp->inp_ip_tos;
415 if (inp->inp_flags & INP_DONTFRAG)
419 ip->ip_p = inp->inp_ip_p;
420 ip->ip_len = m->m_pkthdr.len;
421 ip->ip_src = inp->inp_laddr;
422 error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
428 ip->ip_dst.s_addr = dst;
429 ip->ip_ttl = inp->inp_ip_ttl;
431 if (m->m_pkthdr.len > IP_MAXPACKET) {
436 ip = mtod(m, struct ip *);
437 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
445 * Don't allow both user specified and setsockopt options,
446 * and don't allow packet length sizes that will crash.
448 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
449 || (ip->ip_len > m->m_pkthdr.len)
450 || (ip->ip_len < (ip->ip_hl << 2))) {
456 ip->ip_id = ip_newid();
459 * XXX prevent ip_output from overwriting header fields.
461 flags |= IP_RAWOUTPUT;
462 IPSTAT_INC(ips_rawout);
465 if (inp->inp_flags & INP_ONESBCAST)
466 flags |= IP_SENDONES;
469 mac_inpcb_create_mbuf(inp, m);
472 error = ip_output(m, inp->inp_options, NULL, flags,
473 inp->inp_moptions, inp);
479 * Raw IP socket option processing.
481 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
482 * only be created by a privileged process, and as such, socket option
483 * operations to manage system properties on any raw socket were allowed to
484 * take place without explicit additional access control checks. However,
485 * raw sockets can now also be created in jail(), and therefore explicit
486 * checks are now required. Likewise, raw sockets can be used by a process
487 * after it gives up privilege, so some caution is required. For options
488 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
489 * performed in ip_ctloutput() and therefore no check occurs here.
490 * Unilaterally checking priv_check() here breaks normal IP socket option
491 * operations on raw sockets.
493 * When adding new socket options here, make sure to add access control
494 * checks here as necessary.
497 rip_ctloutput(struct socket *so, struct sockopt *sopt)
499 struct inpcb *inp = sotoinpcb(so);
502 if (sopt->sopt_level != IPPROTO_IP) {
503 if ((sopt->sopt_level == SOL_SOCKET) &&
504 (sopt->sopt_name == SO_SETFIB)) {
505 inp->inp_inc.inc_fibnum = so->so_fibnum;
512 switch (sopt->sopt_dir) {
514 switch (sopt->sopt_name) {
516 optval = inp->inp_flags & INP_HDRINCL;
517 error = sooptcopyout(sopt, &optval, sizeof optval);
520 case IP_FW_ADD: /* ADD actually returns the body... */
522 case IP_FW_TABLE_GETSIZE:
523 case IP_FW_TABLE_LIST:
524 case IP_FW_NAT_GET_CONFIG:
525 case IP_FW_NAT_GET_LOG:
526 if (ip_fw_ctl_ptr != NULL)
527 error = ip_fw_ctl_ptr(sopt);
532 case IP_DUMMYNET_GET:
533 if (ip_dn_ctl_ptr != NULL)
534 error = ip_dn_ctl_ptr(sopt);
547 case MRT_API_SUPPORT:
549 case MRT_ADD_BW_UPCALL:
550 case MRT_DEL_BW_UPCALL:
551 error = priv_check(curthread, PRIV_NETINET_MROUTE);
554 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
559 error = ip_ctloutput(so, sopt);
565 switch (sopt->sopt_name) {
567 error = sooptcopyin(sopt, &optval, sizeof optval,
572 inp->inp_flags |= INP_HDRINCL;
574 inp->inp_flags &= ~INP_HDRINCL;
582 case IP_FW_TABLE_ADD:
583 case IP_FW_TABLE_DEL:
584 case IP_FW_TABLE_FLUSH:
587 if (ip_fw_ctl_ptr != NULL)
588 error = ip_fw_ctl_ptr(sopt);
593 case IP_DUMMYNET_CONFIGURE:
594 case IP_DUMMYNET_DEL:
595 case IP_DUMMYNET_FLUSH:
596 if (ip_dn_ctl_ptr != NULL)
597 error = ip_dn_ctl_ptr(sopt);
599 error = ENOPROTOOPT ;
603 error = priv_check(curthread, PRIV_NETINET_MROUTE);
606 error = ip_rsvp_init(so);
610 error = priv_check(curthread, PRIV_NETINET_MROUTE);
613 error = ip_rsvp_done();
617 case IP_RSVP_VIF_OFF:
618 error = priv_check(curthread, PRIV_NETINET_MROUTE);
621 error = ip_rsvp_vif ?
622 ip_rsvp_vif(so, sopt) : EINVAL;
633 case MRT_API_SUPPORT:
635 case MRT_ADD_BW_UPCALL:
636 case MRT_DEL_BW_UPCALL:
637 error = priv_check(curthread, PRIV_NETINET_MROUTE);
640 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
645 error = ip_ctloutput(so, sopt);
655 * This function exists solely to receive the PRC_IFDOWN messages which are
656 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
657 * in_ifadown() to remove all routes corresponding to that address. It also
658 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
662 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
664 struct in_ifaddr *ia;
672 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
673 if (ia->ia_ifa.ifa_addr == sa
674 && (ia->ia_flags & IFA_ROUTE)) {
675 ifa_ref(&ia->ia_ifa);
678 * in_ifscrub kills the interface route.
680 in_ifscrub(ia->ia_ifp, ia);
682 * in_ifadown gets rid of all the rest of the
683 * routes. This is not quite the right thing
684 * to do, but at least if we are running a
685 * routing process they will come back.
687 in_ifadown(&ia->ia_ifa, 0);
688 ifa_free(&ia->ia_ifa);
692 if (ia == NULL) /* If ia matched, already unlocked. */
698 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
699 if (ia->ia_ifa.ifa_addr == sa)
702 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
706 ifa_ref(&ia->ia_ifa);
709 ifp = ia->ia_ifa.ifa_ifp;
711 if ((ifp->if_flags & IFF_LOOPBACK)
712 || (ifp->if_flags & IFF_POINTOPOINT))
715 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
717 ia->ia_flags |= IFA_ROUTE;
718 ifa_free(&ia->ia_ifa);
723 u_long rip_sendspace = 9216;
724 u_long rip_recvspace = 9216;
726 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
727 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
728 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
729 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
732 rip_attach(struct socket *so, int proto, struct thread *td)
738 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
740 error = priv_check(td, PRIV_NETINET_RAW);
743 if (proto >= IPPROTO_MAX || proto < 0)
744 return EPROTONOSUPPORT;
745 error = soreserve(so, rip_sendspace, rip_recvspace);
748 INP_INFO_WLOCK(&V_ripcbinfo);
749 error = in_pcballoc(so, &V_ripcbinfo);
751 INP_INFO_WUNLOCK(&V_ripcbinfo);
754 inp = (struct inpcb *)so->so_pcb;
755 inp->inp_vflag |= INP_IPV4;
756 inp->inp_ip_p = proto;
757 inp->inp_ip_ttl = V_ip_defttl;
759 INP_INFO_WUNLOCK(&V_ripcbinfo);
765 rip_detach(struct socket *so)
770 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
771 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
772 ("rip_detach: not closed"));
774 INP_INFO_WLOCK(&V_ripcbinfo);
777 if (so == V_ip_mrouter && ip_mrouter_done)
779 if (ip_rsvp_force_done)
780 ip_rsvp_force_done(so);
781 if (so == V_ip_rsvpd)
785 INP_INFO_WUNLOCK(&V_ripcbinfo);
789 rip_dodisconnect(struct socket *so, struct inpcb *inp)
792 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
793 INP_WLOCK_ASSERT(inp);
796 inp->inp_faddr.s_addr = INADDR_ANY;
799 so->so_state &= ~SS_ISCONNECTED;
804 rip_abort(struct socket *so)
809 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
811 INP_INFO_WLOCK(&V_ripcbinfo);
813 rip_dodisconnect(so, inp);
815 INP_INFO_WUNLOCK(&V_ripcbinfo);
819 rip_close(struct socket *so)
824 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
826 INP_INFO_WLOCK(&V_ripcbinfo);
828 rip_dodisconnect(so, inp);
830 INP_INFO_WUNLOCK(&V_ripcbinfo);
834 rip_disconnect(struct socket *so)
838 if ((so->so_state & SS_ISCONNECTED) == 0)
842 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
844 INP_INFO_WLOCK(&V_ripcbinfo);
846 rip_dodisconnect(so, inp);
848 INP_INFO_WUNLOCK(&V_ripcbinfo);
853 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
855 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
859 if (nam->sa_len != sizeof(*addr))
862 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
867 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
869 if (TAILQ_EMPTY(&V_ifnet) ||
870 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
871 (addr->sin_addr.s_addr &&
872 (inp->inp_flags & INP_BINDANY) == 0 &&
873 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
874 return (EADDRNOTAVAIL);
876 INP_INFO_WLOCK(&V_ripcbinfo);
879 inp->inp_laddr = addr->sin_addr;
882 INP_INFO_WUNLOCK(&V_ripcbinfo);
887 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
889 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
892 if (nam->sa_len != sizeof(*addr))
894 if (TAILQ_EMPTY(&V_ifnet))
895 return (EADDRNOTAVAIL);
896 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
897 return (EAFNOSUPPORT);
900 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
902 INP_INFO_WLOCK(&V_ripcbinfo);
905 inp->inp_faddr = addr->sin_addr;
909 INP_INFO_WUNLOCK(&V_ripcbinfo);
914 rip_shutdown(struct socket *so)
919 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
928 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
929 struct mbuf *control, struct thread *td)
935 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
938 * Note: 'dst' reads below are unlocked.
940 if (so->so_state & SS_ISCONNECTED) {
945 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
951 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
953 return (rip_output(m, so, dst));
957 rip_pcblist(SYSCTL_HANDLER_ARGS)
960 struct inpcb *inp, **inp_list;
965 * The process of preparing the TCB list is too time-consuming and
966 * resource-intensive to repeat twice on every request.
968 if (req->oldptr == 0) {
969 n = V_ripcbinfo.ipi_count;
970 req->oldidx = 2 * (sizeof xig)
971 + (n + n/8) * sizeof(struct xinpcb);
975 if (req->newptr != 0)
979 * OK, now we're committed to doing something.
981 INP_INFO_RLOCK(&V_ripcbinfo);
982 gencnt = V_ripcbinfo.ipi_gencnt;
983 n = V_ripcbinfo.ipi_count;
984 INP_INFO_RUNLOCK(&V_ripcbinfo);
986 xig.xig_len = sizeof xig;
988 xig.xig_gen = gencnt;
989 xig.xig_sogen = so_gencnt;
990 error = SYSCTL_OUT(req, &xig, sizeof xig);
994 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
998 INP_INFO_RLOCK(&V_ripcbinfo);
999 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1000 inp = LIST_NEXT(inp, inp_list)) {
1002 if (inp->inp_gencnt <= gencnt &&
1003 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1004 /* XXX held references? */
1005 inp_list[i++] = inp;
1009 INP_INFO_RUNLOCK(&V_ripcbinfo);
1013 for (i = 0; i < n; i++) {
1016 if (inp->inp_gencnt <= gencnt) {
1019 bzero(&xi, sizeof(xi));
1020 xi.xi_len = sizeof xi;
1021 /* XXX should avoid extra copy */
1022 bcopy(inp, &xi.xi_inp, sizeof *inp);
1023 if (inp->inp_socket)
1024 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1026 error = SYSCTL_OUT(req, &xi, sizeof xi);
1032 * Give the user an updated idea of our state. If the
1033 * generation differs from what we told her before, she knows
1034 * that something happened while we were processing this
1035 * request, and it might be necessary to retry.
1037 INP_INFO_RLOCK(&V_ripcbinfo);
1038 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1039 xig.xig_sogen = so_gencnt;
1040 xig.xig_count = V_ripcbinfo.ipi_count;
1041 INP_INFO_RUNLOCK(&V_ripcbinfo);
1042 error = SYSCTL_OUT(req, &xig, sizeof xig);
1044 free(inp_list, M_TEMP);
1048 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
1049 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1051 struct pr_usrreqs rip_usrreqs = {
1052 .pru_abort = rip_abort,
1053 .pru_attach = rip_attach,
1054 .pru_bind = rip_bind,
1055 .pru_connect = rip_connect,
1056 .pru_control = in_control,
1057 .pru_detach = rip_detach,
1058 .pru_disconnect = rip_disconnect,
1059 .pru_peeraddr = in_getpeeraddr,
1060 .pru_send = rip_send,
1061 .pru_shutdown = rip_shutdown,
1062 .pru_sockaddr = in_getsockaddr,
1063 .pru_sosetlabel = in_pcbsosetlabel,
1064 .pru_close = rip_close,