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 ip->ip_id = ip_newid();
512 * XXX prevent ip_output from overwriting header fields.
514 flags |= IP_RAWOUTPUT;
515 IPSTAT_INC(ips_rawout);
518 if (inp->inp_flags & INP_ONESBCAST)
519 flags |= IP_SENDONES;
522 mac_inpcb_create_mbuf(inp, m);
525 error = ip_output(m, inp->inp_options, NULL, flags,
526 inp->inp_moptions, inp);
532 * Raw IP socket option processing.
534 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
535 * only be created by a privileged process, and as such, socket option
536 * operations to manage system properties on any raw socket were allowed to
537 * take place without explicit additional access control checks. However,
538 * raw sockets can now also be created in jail(), and therefore explicit
539 * checks are now required. Likewise, raw sockets can be used by a process
540 * after it gives up privilege, so some caution is required. For options
541 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
542 * performed in ip_ctloutput() and therefore no check occurs here.
543 * Unilaterally checking priv_check() here breaks normal IP socket option
544 * operations on raw sockets.
546 * When adding new socket options here, make sure to add access control
547 * checks here as necessary.
549 * XXX-BZ inp locking?
552 rip_ctloutput(struct socket *so, struct sockopt *sopt)
554 struct inpcb *inp = sotoinpcb(so);
557 if (sopt->sopt_level != IPPROTO_IP) {
558 if ((sopt->sopt_level == SOL_SOCKET) &&
559 (sopt->sopt_name == SO_SETFIB)) {
560 inp->inp_inc.inc_fibnum = so->so_fibnum;
567 switch (sopt->sopt_dir) {
569 switch (sopt->sopt_name) {
571 optval = inp->inp_flags & INP_HDRINCL;
572 error = sooptcopyout(sopt, &optval, sizeof optval);
575 case IP_FW3: /* generic ipfw v.3 functions */
576 case IP_FW_ADD: /* ADD actually returns the body... */
578 case IP_FW_TABLE_GETSIZE:
579 case IP_FW_TABLE_LIST:
580 case IP_FW_NAT_GET_CONFIG:
581 case IP_FW_NAT_GET_LOG:
582 if (V_ip_fw_ctl_ptr != NULL)
583 error = V_ip_fw_ctl_ptr(sopt);
588 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
589 case IP_DUMMYNET_GET:
590 if (ip_dn_ctl_ptr != NULL)
591 error = ip_dn_ctl_ptr(sopt);
604 case MRT_API_SUPPORT:
606 case MRT_ADD_BW_UPCALL:
607 case MRT_DEL_BW_UPCALL:
608 error = priv_check(curthread, PRIV_NETINET_MROUTE);
611 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
616 error = ip_ctloutput(so, sopt);
622 switch (sopt->sopt_name) {
624 error = sooptcopyin(sopt, &optval, sizeof optval,
629 inp->inp_flags |= INP_HDRINCL;
631 inp->inp_flags &= ~INP_HDRINCL;
634 case IP_FW3: /* generic ipfw v.3 functions */
640 case IP_FW_TABLE_ADD:
641 case IP_FW_TABLE_DEL:
642 case IP_FW_TABLE_FLUSH:
645 if (V_ip_fw_ctl_ptr != NULL)
646 error = V_ip_fw_ctl_ptr(sopt);
651 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
652 case IP_DUMMYNET_CONFIGURE:
653 case IP_DUMMYNET_DEL:
654 case IP_DUMMYNET_FLUSH:
655 if (ip_dn_ctl_ptr != NULL)
656 error = ip_dn_ctl_ptr(sopt);
658 error = ENOPROTOOPT ;
662 error = priv_check(curthread, PRIV_NETINET_MROUTE);
665 error = ip_rsvp_init(so);
669 error = priv_check(curthread, PRIV_NETINET_MROUTE);
672 error = ip_rsvp_done();
676 case IP_RSVP_VIF_OFF:
677 error = priv_check(curthread, PRIV_NETINET_MROUTE);
680 error = ip_rsvp_vif ?
681 ip_rsvp_vif(so, sopt) : EINVAL;
692 case MRT_API_SUPPORT:
694 case MRT_ADD_BW_UPCALL:
695 case MRT_DEL_BW_UPCALL:
696 error = priv_check(curthread, PRIV_NETINET_MROUTE);
699 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
704 error = ip_ctloutput(so, sopt);
714 * This function exists solely to receive the PRC_IFDOWN messages which are
715 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
716 * in_ifadown() to remove all routes corresponding to that address. It also
717 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
721 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
723 struct in_ifaddr *ia;
731 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
732 if (ia->ia_ifa.ifa_addr == sa
733 && (ia->ia_flags & IFA_ROUTE)) {
734 ifa_ref(&ia->ia_ifa);
737 * in_scrubprefix() kills the interface route.
739 in_scrubprefix(ia, 0);
741 * in_ifadown gets rid of all the rest of the
742 * routes. This is not quite the right thing
743 * to do, but at least if we are running a
744 * routing process they will come back.
746 in_ifadown(&ia->ia_ifa, 0);
747 ifa_free(&ia->ia_ifa);
751 if (ia == NULL) /* If ia matched, already unlocked. */
757 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
758 if (ia->ia_ifa.ifa_addr == sa)
761 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
765 ifa_ref(&ia->ia_ifa);
768 ifp = ia->ia_ifa.ifa_ifp;
770 if ((ifp->if_flags & IFF_LOOPBACK)
771 || (ifp->if_flags & IFF_POINTOPOINT))
774 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
776 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
778 ia->ia_flags |= IFA_ROUTE;
780 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
782 ifa_free(&ia->ia_ifa);
788 rip_attach(struct socket *so, int proto, struct thread *td)
794 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
796 error = priv_check(td, PRIV_NETINET_RAW);
799 if (proto >= IPPROTO_MAX || proto < 0)
800 return EPROTONOSUPPORT;
801 error = soreserve(so, rip_sendspace, rip_recvspace);
804 INP_INFO_WLOCK(&V_ripcbinfo);
805 error = in_pcballoc(so, &V_ripcbinfo);
807 INP_INFO_WUNLOCK(&V_ripcbinfo);
810 inp = (struct inpcb *)so->so_pcb;
811 inp->inp_vflag |= INP_IPV4;
812 inp->inp_ip_p = proto;
813 inp->inp_ip_ttl = V_ip_defttl;
815 INP_INFO_WUNLOCK(&V_ripcbinfo);
821 rip_detach(struct socket *so)
826 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
827 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
828 ("rip_detach: not closed"));
830 INP_INFO_WLOCK(&V_ripcbinfo);
833 if (so == V_ip_mrouter && ip_mrouter_done)
835 if (ip_rsvp_force_done)
836 ip_rsvp_force_done(so);
837 if (so == V_ip_rsvpd)
841 INP_INFO_WUNLOCK(&V_ripcbinfo);
845 rip_dodisconnect(struct socket *so, struct inpcb *inp)
847 struct inpcbinfo *pcbinfo;
849 pcbinfo = inp->inp_pcbinfo;
850 INP_INFO_WLOCK(pcbinfo);
853 inp->inp_faddr.s_addr = INADDR_ANY;
856 so->so_state &= ~SS_ISCONNECTED;
859 INP_INFO_WUNLOCK(pcbinfo);
863 rip_abort(struct socket *so)
868 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
870 rip_dodisconnect(so, inp);
874 rip_close(struct socket *so)
879 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
881 rip_dodisconnect(so, inp);
885 rip_disconnect(struct socket *so)
889 if ((so->so_state & SS_ISCONNECTED) == 0)
893 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
895 rip_dodisconnect(so, inp);
900 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
902 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
906 if (nam->sa_len != sizeof(*addr))
909 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
914 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
916 if (TAILQ_EMPTY(&V_ifnet) ||
917 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
918 (addr->sin_addr.s_addr &&
919 (inp->inp_flags & INP_BINDANY) == 0 &&
920 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
921 return (EADDRNOTAVAIL);
923 INP_INFO_WLOCK(&V_ripcbinfo);
926 inp->inp_laddr = addr->sin_addr;
929 INP_INFO_WUNLOCK(&V_ripcbinfo);
934 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
936 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
939 if (nam->sa_len != sizeof(*addr))
941 if (TAILQ_EMPTY(&V_ifnet))
942 return (EADDRNOTAVAIL);
943 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
944 return (EAFNOSUPPORT);
947 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
949 INP_INFO_WLOCK(&V_ripcbinfo);
952 inp->inp_faddr = addr->sin_addr;
956 INP_INFO_WUNLOCK(&V_ripcbinfo);
961 rip_shutdown(struct socket *so)
966 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
975 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
976 struct mbuf *control, struct thread *td)
982 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
985 * Note: 'dst' reads below are unlocked.
987 if (so->so_state & SS_ISCONNECTED) {
992 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
998 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1000 return (rip_output(m, so, dst));
1005 rip_pcblist(SYSCTL_HANDLER_ARGS)
1008 struct inpcb *inp, **inp_list;
1013 * The process of preparing the TCB list is too time-consuming and
1014 * resource-intensive to repeat twice on every request.
1016 if (req->oldptr == 0) {
1017 n = V_ripcbinfo.ipi_count;
1018 n += imax(n / 8, 10);
1019 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1023 if (req->newptr != 0)
1027 * OK, now we're committed to doing something.
1029 INP_INFO_RLOCK(&V_ripcbinfo);
1030 gencnt = V_ripcbinfo.ipi_gencnt;
1031 n = V_ripcbinfo.ipi_count;
1032 INP_INFO_RUNLOCK(&V_ripcbinfo);
1034 xig.xig_len = sizeof xig;
1036 xig.xig_gen = gencnt;
1037 xig.xig_sogen = so_gencnt;
1038 error = SYSCTL_OUT(req, &xig, sizeof xig);
1042 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1046 INP_INFO_RLOCK(&V_ripcbinfo);
1047 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1048 inp = LIST_NEXT(inp, inp_list)) {
1050 if (inp->inp_gencnt <= gencnt &&
1051 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1053 inp_list[i++] = inp;
1057 INP_INFO_RUNLOCK(&V_ripcbinfo);
1061 for (i = 0; i < n; i++) {
1064 if (inp->inp_gencnt <= gencnt) {
1067 bzero(&xi, sizeof(xi));
1068 xi.xi_len = sizeof xi;
1069 /* XXX should avoid extra copy */
1070 bcopy(inp, &xi.xi_inp, sizeof *inp);
1071 if (inp->inp_socket)
1072 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1074 error = SYSCTL_OUT(req, &xi, sizeof xi);
1078 INP_INFO_WLOCK(&V_ripcbinfo);
1079 for (i = 0; i < n; i++) {
1082 if (!in_pcbrele_rlocked(inp))
1085 INP_INFO_WUNLOCK(&V_ripcbinfo);
1089 * Give the user an updated idea of our state. If the
1090 * generation differs from what we told her before, she knows
1091 * that something happened while we were processing this
1092 * request, and it might be necessary to retry.
1094 INP_INFO_RLOCK(&V_ripcbinfo);
1095 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1096 xig.xig_sogen = so_gencnt;
1097 xig.xig_count = V_ripcbinfo.ipi_count;
1098 INP_INFO_RUNLOCK(&V_ripcbinfo);
1099 error = SYSCTL_OUT(req, &xig, sizeof xig);
1101 free(inp_list, M_TEMP);
1105 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1106 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1107 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1110 struct pr_usrreqs rip_usrreqs = {
1111 .pru_abort = rip_abort,
1112 .pru_attach = rip_attach,
1113 .pru_bind = rip_bind,
1114 .pru_connect = rip_connect,
1115 .pru_control = in_control,
1116 .pru_detach = rip_detach,
1117 .pru_disconnect = rip_disconnect,
1118 .pru_peeraddr = in_getpeeraddr,
1119 .pru_send = rip_send,
1120 .pru_shutdown = rip_shutdown,
1121 .pru_sockaddr = in_getsockaddr,
1122 .pru_sosetlabel = in_pcbsosetlabel,
1123 .pru_close = rip_close,