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))) {
347 * If the incoming datagram is for IGMP, allow it
348 * through unconditionally to the raw socket.
350 * In the case of IGMPv2, we may not have explicitly
351 * joined the group, and may have set IFF_ALLMULTI
352 * on the interface. imo_multi_filter() may discard
353 * control traffic we actually need to see.
355 * Userland multicast routing daemons should continue
356 * filter the control traffic appropriately.
360 blocked = MCAST_PASS;
361 if (proto != IPPROTO_IGMP) {
362 struct sockaddr_in group;
364 bzero(&group, sizeof(struct sockaddr_in));
365 group.sin_len = sizeof(struct sockaddr_in);
366 group.sin_family = AF_INET;
367 group.sin_addr = ip->ip_dst;
369 blocked = imo_multi_filter(inp->inp_moptions,
371 (struct sockaddr *)&group,
372 (struct sockaddr *)&ripsrc);
375 if (blocked != MCAST_PASS) {
376 IPSTAT_INC(ips_notmember);
383 n = m_copy(m, 0, (int)M_COPYALL);
385 (void) rip_append(last, ip, n, &ripsrc);
386 /* XXX count dropped packet */
392 INP_INFO_RUNLOCK(&V_ripcbinfo);
394 if (rip_append(last, ip, m, &ripsrc) != 0)
395 IPSTAT_INC(ips_delivered);
399 IPSTAT_INC(ips_noproto);
400 IPSTAT_DEC(ips_delivered);
405 * Generate IP header and pass packet to ip_output. Tack on options user may
406 * have setup with control call.
409 rip_output(struct mbuf *m, struct socket *so, u_long dst)
413 struct inpcb *inp = sotoinpcb(so);
414 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
418 * If the user handed us a complete IP packet, use it. Otherwise,
419 * allocate an mbuf for a header and fill it in.
421 if ((inp->inp_flags & INP_HDRINCL) == 0) {
422 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
426 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
431 ip = mtod(m, struct ip *);
432 ip->ip_tos = inp->inp_ip_tos;
433 if (inp->inp_flags & INP_DONTFRAG)
437 ip->ip_p = inp->inp_ip_p;
438 ip->ip_len = m->m_pkthdr.len;
439 ip->ip_src = inp->inp_laddr;
440 error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
446 ip->ip_dst.s_addr = dst;
447 ip->ip_ttl = inp->inp_ip_ttl;
449 if (m->m_pkthdr.len > IP_MAXPACKET) {
454 ip = mtod(m, struct ip *);
455 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
463 * Don't allow both user specified and setsockopt options,
464 * and don't allow packet length sizes that will crash.
466 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
467 || (ip->ip_len > m->m_pkthdr.len)
468 || (ip->ip_len < (ip->ip_hl << 2))) {
474 ip->ip_id = ip_newid();
477 * XXX prevent ip_output from overwriting header fields.
479 flags |= IP_RAWOUTPUT;
480 IPSTAT_INC(ips_rawout);
483 if (inp->inp_flags & INP_ONESBCAST)
484 flags |= IP_SENDONES;
487 mac_inpcb_create_mbuf(inp, m);
490 error = ip_output(m, inp->inp_options, NULL, flags,
491 inp->inp_moptions, inp);
497 * Raw IP socket option processing.
499 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
500 * only be created by a privileged process, and as such, socket option
501 * operations to manage system properties on any raw socket were allowed to
502 * take place without explicit additional access control checks. However,
503 * raw sockets can now also be created in jail(), and therefore explicit
504 * checks are now required. Likewise, raw sockets can be used by a process
505 * after it gives up privilege, so some caution is required. For options
506 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
507 * performed in ip_ctloutput() and therefore no check occurs here.
508 * Unilaterally checking priv_check() here breaks normal IP socket option
509 * operations on raw sockets.
511 * When adding new socket options here, make sure to add access control
512 * checks here as necessary.
515 rip_ctloutput(struct socket *so, struct sockopt *sopt)
517 struct inpcb *inp = sotoinpcb(so);
520 if (sopt->sopt_level != IPPROTO_IP) {
521 if ((sopt->sopt_level == SOL_SOCKET) &&
522 (sopt->sopt_name == SO_SETFIB)) {
523 inp->inp_inc.inc_fibnum = so->so_fibnum;
530 switch (sopt->sopt_dir) {
532 switch (sopt->sopt_name) {
534 optval = inp->inp_flags & INP_HDRINCL;
535 error = sooptcopyout(sopt, &optval, sizeof optval);
538 case IP_FW_ADD: /* ADD actually returns the body... */
540 case IP_FW_TABLE_GETSIZE:
541 case IP_FW_TABLE_LIST:
542 case IP_FW_NAT_GET_CONFIG:
543 case IP_FW_NAT_GET_LOG:
544 if (ip_fw_ctl_ptr != NULL)
545 error = ip_fw_ctl_ptr(sopt);
550 case IP_DUMMYNET_GET:
551 if (ip_dn_ctl_ptr != NULL)
552 error = ip_dn_ctl_ptr(sopt);
565 case MRT_API_SUPPORT:
567 case MRT_ADD_BW_UPCALL:
568 case MRT_DEL_BW_UPCALL:
569 error = priv_check(curthread, PRIV_NETINET_MROUTE);
572 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
577 error = ip_ctloutput(so, sopt);
583 switch (sopt->sopt_name) {
585 error = sooptcopyin(sopt, &optval, sizeof optval,
590 inp->inp_flags |= INP_HDRINCL;
592 inp->inp_flags &= ~INP_HDRINCL;
600 case IP_FW_TABLE_ADD:
601 case IP_FW_TABLE_DEL:
602 case IP_FW_TABLE_FLUSH:
605 if (ip_fw_ctl_ptr != NULL)
606 error = ip_fw_ctl_ptr(sopt);
611 case IP_DUMMYNET_CONFIGURE:
612 case IP_DUMMYNET_DEL:
613 case IP_DUMMYNET_FLUSH:
614 if (ip_dn_ctl_ptr != NULL)
615 error = ip_dn_ctl_ptr(sopt);
617 error = ENOPROTOOPT ;
621 error = priv_check(curthread, PRIV_NETINET_MROUTE);
624 error = ip_rsvp_init(so);
628 error = priv_check(curthread, PRIV_NETINET_MROUTE);
631 error = ip_rsvp_done();
635 case IP_RSVP_VIF_OFF:
636 error = priv_check(curthread, PRIV_NETINET_MROUTE);
639 error = ip_rsvp_vif ?
640 ip_rsvp_vif(so, sopt) : EINVAL;
651 case MRT_API_SUPPORT:
653 case MRT_ADD_BW_UPCALL:
654 case MRT_DEL_BW_UPCALL:
655 error = priv_check(curthread, PRIV_NETINET_MROUTE);
658 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
663 error = ip_ctloutput(so, sopt);
673 * This function exists solely to receive the PRC_IFDOWN messages which are
674 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
675 * in_ifadown() to remove all routes corresponding to that address. It also
676 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
680 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
682 struct in_ifaddr *ia;
690 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
691 if (ia->ia_ifa.ifa_addr == sa
692 && (ia->ia_flags & IFA_ROUTE)) {
693 ifa_ref(&ia->ia_ifa);
696 * in_ifscrub kills the interface route.
698 in_ifscrub(ia->ia_ifp, ia);
700 * in_ifadown gets rid of all the rest of the
701 * routes. This is not quite the right thing
702 * to do, but at least if we are running a
703 * routing process they will come back.
705 in_ifadown(&ia->ia_ifa, 0);
706 ifa_free(&ia->ia_ifa);
710 if (ia == NULL) /* If ia matched, already unlocked. */
716 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
717 if (ia->ia_ifa.ifa_addr == sa)
720 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
724 ifa_ref(&ia->ia_ifa);
727 ifp = ia->ia_ifa.ifa_ifp;
729 if ((ifp->if_flags & IFF_LOOPBACK)
730 || (ifp->if_flags & IFF_POINTOPOINT))
733 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
735 ia->ia_flags |= IFA_ROUTE;
736 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
737 ifa_free(&ia->ia_ifa);
742 u_long rip_sendspace = 9216;
743 u_long rip_recvspace = 9216;
745 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
746 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
747 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
748 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
751 rip_attach(struct socket *so, int proto, struct thread *td)
757 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
759 error = priv_check(td, PRIV_NETINET_RAW);
762 if (proto >= IPPROTO_MAX || proto < 0)
763 return EPROTONOSUPPORT;
764 error = soreserve(so, rip_sendspace, rip_recvspace);
767 INP_INFO_WLOCK(&V_ripcbinfo);
768 error = in_pcballoc(so, &V_ripcbinfo);
770 INP_INFO_WUNLOCK(&V_ripcbinfo);
773 inp = (struct inpcb *)so->so_pcb;
774 inp->inp_vflag |= INP_IPV4;
775 inp->inp_ip_p = proto;
776 inp->inp_ip_ttl = V_ip_defttl;
778 INP_INFO_WUNLOCK(&V_ripcbinfo);
784 rip_detach(struct socket *so)
789 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
790 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
791 ("rip_detach: not closed"));
793 INP_INFO_WLOCK(&V_ripcbinfo);
796 if (so == V_ip_mrouter && ip_mrouter_done)
798 if (ip_rsvp_force_done)
799 ip_rsvp_force_done(so);
800 if (so == V_ip_rsvpd)
804 INP_INFO_WUNLOCK(&V_ripcbinfo);
808 rip_dodisconnect(struct socket *so, struct inpcb *inp)
811 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
812 INP_WLOCK_ASSERT(inp);
815 inp->inp_faddr.s_addr = INADDR_ANY;
818 so->so_state &= ~SS_ISCONNECTED;
823 rip_abort(struct socket *so)
828 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
830 INP_INFO_WLOCK(&V_ripcbinfo);
832 rip_dodisconnect(so, inp);
834 INP_INFO_WUNLOCK(&V_ripcbinfo);
838 rip_close(struct socket *so)
843 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
845 INP_INFO_WLOCK(&V_ripcbinfo);
847 rip_dodisconnect(so, inp);
849 INP_INFO_WUNLOCK(&V_ripcbinfo);
853 rip_disconnect(struct socket *so)
857 if ((so->so_state & SS_ISCONNECTED) == 0)
861 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
863 INP_INFO_WLOCK(&V_ripcbinfo);
865 rip_dodisconnect(so, inp);
867 INP_INFO_WUNLOCK(&V_ripcbinfo);
872 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
874 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
878 if (nam->sa_len != sizeof(*addr))
881 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
886 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
888 if (TAILQ_EMPTY(&V_ifnet) ||
889 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
890 (addr->sin_addr.s_addr &&
891 (inp->inp_flags & INP_BINDANY) == 0 &&
892 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
893 return (EADDRNOTAVAIL);
895 INP_INFO_WLOCK(&V_ripcbinfo);
898 inp->inp_laddr = addr->sin_addr;
901 INP_INFO_WUNLOCK(&V_ripcbinfo);
906 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
908 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
911 if (nam->sa_len != sizeof(*addr))
913 if (TAILQ_EMPTY(&V_ifnet))
914 return (EADDRNOTAVAIL);
915 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
916 return (EAFNOSUPPORT);
919 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
921 INP_INFO_WLOCK(&V_ripcbinfo);
924 inp->inp_faddr = addr->sin_addr;
928 INP_INFO_WUNLOCK(&V_ripcbinfo);
933 rip_shutdown(struct socket *so)
938 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
947 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
948 struct mbuf *control, struct thread *td)
954 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
957 * Note: 'dst' reads below are unlocked.
959 if (so->so_state & SS_ISCONNECTED) {
964 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
970 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
972 return (rip_output(m, so, dst));
976 rip_pcblist(SYSCTL_HANDLER_ARGS)
979 struct inpcb *inp, **inp_list;
984 * The process of preparing the TCB list is too time-consuming and
985 * resource-intensive to repeat twice on every request.
987 if (req->oldptr == 0) {
988 n = V_ripcbinfo.ipi_count;
989 req->oldidx = 2 * (sizeof xig)
990 + (n + n/8) * sizeof(struct xinpcb);
994 if (req->newptr != 0)
998 * OK, now we're committed to doing something.
1000 INP_INFO_RLOCK(&V_ripcbinfo);
1001 gencnt = V_ripcbinfo.ipi_gencnt;
1002 n = V_ripcbinfo.ipi_count;
1003 INP_INFO_RUNLOCK(&V_ripcbinfo);
1005 xig.xig_len = sizeof xig;
1007 xig.xig_gen = gencnt;
1008 xig.xig_sogen = so_gencnt;
1009 error = SYSCTL_OUT(req, &xig, sizeof xig);
1013 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1017 INP_INFO_RLOCK(&V_ripcbinfo);
1018 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1019 inp = LIST_NEXT(inp, inp_list)) {
1021 if (inp->inp_gencnt <= gencnt &&
1022 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1023 /* XXX held references? */
1024 inp_list[i++] = inp;
1028 INP_INFO_RUNLOCK(&V_ripcbinfo);
1032 for (i = 0; i < n; i++) {
1035 if (inp->inp_gencnt <= gencnt) {
1038 bzero(&xi, sizeof(xi));
1039 xi.xi_len = sizeof xi;
1040 /* XXX should avoid extra copy */
1041 bcopy(inp, &xi.xi_inp, sizeof *inp);
1042 if (inp->inp_socket)
1043 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1045 error = SYSCTL_OUT(req, &xi, sizeof xi);
1051 * Give the user an updated idea of our state. If the
1052 * generation differs from what we told her before, she knows
1053 * that something happened while we were processing this
1054 * request, and it might be necessary to retry.
1056 INP_INFO_RLOCK(&V_ripcbinfo);
1057 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1058 xig.xig_sogen = so_gencnt;
1059 xig.xig_count = V_ripcbinfo.ipi_count;
1060 INP_INFO_RUNLOCK(&V_ripcbinfo);
1061 error = SYSCTL_OUT(req, &xig, sizeof xig);
1063 free(inp_list, M_TEMP);
1067 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
1068 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1070 struct pr_usrreqs rip_usrreqs = {
1071 .pru_abort = rip_abort,
1072 .pru_attach = rip_attach,
1073 .pru_bind = rip_bind,
1074 .pru_connect = rip_connect,
1075 .pru_control = in_control,
1076 .pru_detach = rip_detach,
1077 .pru_disconnect = rip_disconnect,
1078 .pru_peeraddr = in_getpeeraddr,
1079 .pru_send = rip_send,
1080 .pru_shutdown = rip_shutdown,
1081 .pru_sockaddr = in_getsockaddr,
1082 .pru_sosetlabel = in_pcbsosetlabel,
1083 .pru_close = rip_close,