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>
74 #include <netinet/ip_icmp.h>
76 #include <netipsec/ipsec_support.h>
78 #include <machine/stdarg.h>
79 #include <security/mac/mac_framework.h>
81 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
82 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
83 &VNET_NAME(ip_defttl), 0,
84 "Maximum TTL on IP packets");
86 VNET_DEFINE(struct inpcbhead, ripcb);
87 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
89 #define V_ripcb VNET(ripcb)
90 #define V_ripcbinfo VNET(ripcbinfo)
93 * Control and data hooks for ipfw, dummynet, divert and so on.
94 * The data hooks are not used here but it is convenient
95 * to keep them all in one place.
97 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
98 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
100 int (*ip_dn_ctl_ptr)(struct sockopt *);
101 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
102 void (*ip_divert_ptr)(struct mbuf *, int);
103 int (*ng_ipfw_input_p)(struct mbuf **, int,
104 struct ip_fw_args *, int);
108 * Hooks for multicast routing. They all default to NULL, so leave them not
109 * initialized and rely on BSS being set to 0.
113 * The socket used to communicate with the multicast routing daemon.
115 VNET_DEFINE(struct socket *, ip_mrouter);
118 * The various mrouter and rsvp functions.
120 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
121 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_done)(void);
123 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
124 struct ip_moptions *);
125 int (*mrt_ioctl)(u_long, caddr_t, int);
126 int (*legal_vif_num)(int);
127 u_long (*ip_mcast_src)(int);
129 int (*rsvp_input_p)(struct mbuf **, int *, int);
130 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
131 void (*ip_rsvp_force_done)(struct socket *);
134 extern struct protosw inetsw[];
136 u_long rip_sendspace = 9216;
137 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
138 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
140 u_long rip_recvspace = 9216;
141 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
142 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
148 #define INP_PCBHASH_RAW_SIZE 256
149 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
150 (((proto) + (laddr) + (faddr)) % (mask) + 1)
154 rip_inshash(struct inpcb *inp)
156 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
157 struct inpcbhead *pcbhash;
160 INP_INFO_WLOCK_ASSERT(pcbinfo);
161 INP_WLOCK_ASSERT(inp);
163 if (inp->inp_ip_p != 0 &&
164 inp->inp_laddr.s_addr != INADDR_ANY &&
165 inp->inp_faddr.s_addr != INADDR_ANY) {
166 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
167 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
170 pcbhash = &pcbinfo->ipi_hashbase[hash];
171 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
175 rip_delhash(struct inpcb *inp)
178 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
179 INP_WLOCK_ASSERT(inp);
181 LIST_REMOVE(inp, inp_hash);
186 * Raw interface to IP protocol.
190 * Initialize raw connection block q.
193 rip_zone_change(void *tag)
196 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
200 rip_inpcb_init(void *mem, int size, int flags)
202 struct inpcb *inp = mem;
204 INP_LOCK_INIT(inp, "inp", "rawinp");
212 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
213 1, "ripcb", rip_inpcb_init, NULL, 0, IPI_HASHFIELDS_NONE);
214 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
215 EVENTHANDLER_PRI_ANY);
220 rip_destroy(void *unused __unused)
223 in_pcbinfo_destroy(&V_ripcbinfo);
225 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
230 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
231 struct sockaddr_in *ripsrc)
235 INP_LOCK_ASSERT(last);
237 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
238 /* check AH/ESP integrity. */
239 if (IPSEC_ENABLED(ipv4)) {
240 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
245 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
248 /* Check the minimum TTL for socket. */
249 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
252 struct mbuf *opts = NULL;
255 so = last->inp_socket;
256 if ((last->inp_flags & INP_CONTROLOPTS) ||
257 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
258 ip_savecontrol(last, &opts, ip, n);
259 SOCKBUF_LOCK(&so->so_rcv);
260 if (sbappendaddr_locked(&so->so_rcv,
261 (struct sockaddr *)ripsrc, n, opts) == 0) {
262 /* should notify about lost packet */
266 SOCKBUF_UNLOCK(&so->so_rcv);
268 sorwakeup_locked(so);
275 * Setup generic address and protocol structures for raw_input routine, then
276 * pass them along with mbuf chain.
279 rip_input(struct mbuf **mp, int *offp, int proto)
282 struct mbuf *m = *mp;
283 struct ip *ip = mtod(m, struct ip *);
284 struct inpcb *inp, *last;
285 struct sockaddr_in ripsrc;
290 bzero(&ripsrc, sizeof(ripsrc));
291 ripsrc.sin_len = sizeof(ripsrc);
292 ripsrc.sin_family = AF_INET;
293 ripsrc.sin_addr = ip->ip_src;
296 ifp = m->m_pkthdr.rcvif;
298 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
299 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
300 INP_INFO_RLOCK(&V_ripcbinfo);
301 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
302 if (inp->inp_ip_p != proto)
305 /* XXX inp locking */
306 if ((inp->inp_vflag & INP_IPV4) == 0)
309 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
311 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
313 if (jailed_without_vnet(inp->inp_cred)) {
315 * XXX: If faddr was bound to multicast group,
316 * jailed raw socket will drop datagram.
318 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
324 n = m_copy(m, 0, (int)M_COPYALL);
326 (void) rip_append(last, ip, n, &ripsrc);
327 /* XXX count dropped packet */
333 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
334 if (inp->inp_ip_p && inp->inp_ip_p != proto)
337 /* XXX inp locking */
338 if ((inp->inp_vflag & INP_IPV4) == 0)
341 if (!in_nullhost(inp->inp_laddr) &&
342 !in_hosteq(inp->inp_laddr, ip->ip_dst))
344 if (!in_nullhost(inp->inp_faddr) &&
345 !in_hosteq(inp->inp_faddr, ip->ip_src))
347 if (jailed_without_vnet(inp->inp_cred)) {
349 * Allow raw socket in jail to receive multicast;
350 * assume process had PRIV_NETINET_RAW at attach,
351 * and fall through into normal filter path if so.
353 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
354 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
358 * If this raw socket has multicast state, and we
359 * have received a multicast, check if this socket
360 * should receive it, as multicast filtering is now
361 * the responsibility of the transport layer.
363 if (inp->inp_moptions != NULL &&
364 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
366 * If the incoming datagram is for IGMP, allow it
367 * through unconditionally to the raw socket.
369 * In the case of IGMPv2, we may not have explicitly
370 * joined the group, and may have set IFF_ALLMULTI
371 * on the interface. imo_multi_filter() may discard
372 * control traffic we actually need to see.
374 * Userland multicast routing daemons should continue
375 * filter the control traffic appropriately.
379 blocked = MCAST_PASS;
380 if (proto != IPPROTO_IGMP) {
381 struct sockaddr_in group;
383 bzero(&group, sizeof(struct sockaddr_in));
384 group.sin_len = sizeof(struct sockaddr_in);
385 group.sin_family = AF_INET;
386 group.sin_addr = ip->ip_dst;
388 blocked = imo_multi_filter(inp->inp_moptions,
390 (struct sockaddr *)&group,
391 (struct sockaddr *)&ripsrc);
394 if (blocked != MCAST_PASS) {
395 IPSTAT_INC(ips_notmember);
402 n = m_copy(m, 0, (int)M_COPYALL);
404 (void) rip_append(last, ip, n, &ripsrc);
405 /* XXX count dropped packet */
411 INP_INFO_RUNLOCK(&V_ripcbinfo);
413 if (rip_append(last, ip, m, &ripsrc) != 0)
414 IPSTAT_INC(ips_delivered);
417 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
418 IPSTAT_INC(ips_noproto);
419 IPSTAT_DEC(ips_delivered);
420 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
425 return (IPPROTO_DONE);
429 * Generate IP header and pass packet to ip_output. Tack on options user may
430 * have setup with control call.
433 rip_output(struct mbuf *m, struct socket *so, ...)
437 struct inpcb *inp = sotoinpcb(so);
440 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
444 dst = va_arg(ap, u_long);
448 * If the user handed us a complete IP packet, use it. Otherwise,
449 * allocate an mbuf for a header and fill it in.
451 if ((inp->inp_flags & INP_HDRINCL) == 0) {
452 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
456 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
461 ip = mtod(m, struct ip *);
462 ip->ip_tos = inp->inp_ip_tos;
463 if (inp->inp_flags & INP_DONTFRAG)
464 ip->ip_off = htons(IP_DF);
466 ip->ip_off = htons(0);
467 ip->ip_p = inp->inp_ip_p;
468 ip->ip_len = htons(m->m_pkthdr.len);
469 ip->ip_src = inp->inp_laddr;
470 ip->ip_dst.s_addr = dst;
471 if (jailed(inp->inp_cred)) {
473 * prison_local_ip4() would be good enough but would
474 * let a source of INADDR_ANY pass, which we do not
475 * want to see from jails.
477 if (ip->ip_src.s_addr == INADDR_ANY) {
478 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
481 error = prison_local_ip4(inp->inp_cred,
490 ip->ip_ttl = inp->inp_ip_ttl;
492 if (m->m_pkthdr.len > IP_MAXPACKET) {
497 ip = mtod(m, struct ip *);
498 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
506 * Don't allow both user specified and setsockopt options,
507 * and don't allow packet length sizes that will crash.
509 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
510 || (ntohs(ip->ip_len) > m->m_pkthdr.len)
511 || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
517 * This doesn't allow application to specify ID of zero,
518 * but we got this limitation from the beginning of history.
524 * XXX prevent ip_output from overwriting header fields.
526 flags |= IP_RAWOUTPUT;
527 IPSTAT_INC(ips_rawout);
530 if (inp->inp_flags & INP_ONESBCAST)
531 flags |= IP_SENDONES;
534 mac_inpcb_create_mbuf(inp, m);
537 error = ip_output(m, inp->inp_options, NULL, flags,
538 inp->inp_moptions, inp);
544 * Raw IP socket option processing.
546 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
547 * only be created by a privileged process, and as such, socket option
548 * operations to manage system properties on any raw socket were allowed to
549 * take place without explicit additional access control checks. However,
550 * raw sockets can now also be created in jail(), and therefore explicit
551 * checks are now required. Likewise, raw sockets can be used by a process
552 * after it gives up privilege, so some caution is required. For options
553 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
554 * performed in ip_ctloutput() and therefore no check occurs here.
555 * Unilaterally checking priv_check() here breaks normal IP socket option
556 * operations on raw sockets.
558 * When adding new socket options here, make sure to add access control
559 * checks here as necessary.
561 * XXX-BZ inp locking?
564 rip_ctloutput(struct socket *so, struct sockopt *sopt)
566 struct inpcb *inp = sotoinpcb(so);
569 if (sopt->sopt_level != IPPROTO_IP) {
570 if ((sopt->sopt_level == SOL_SOCKET) &&
571 (sopt->sopt_name == SO_SETFIB)) {
572 inp->inp_inc.inc_fibnum = so->so_fibnum;
579 switch (sopt->sopt_dir) {
581 switch (sopt->sopt_name) {
583 optval = inp->inp_flags & INP_HDRINCL;
584 error = sooptcopyout(sopt, &optval, sizeof optval);
587 case IP_FW3: /* generic ipfw v.3 functions */
588 case IP_FW_ADD: /* ADD actually returns the body... */
590 case IP_FW_TABLE_GETSIZE:
591 case IP_FW_TABLE_LIST:
592 case IP_FW_NAT_GET_CONFIG:
593 case IP_FW_NAT_GET_LOG:
594 if (V_ip_fw_ctl_ptr != NULL)
595 error = V_ip_fw_ctl_ptr(sopt);
600 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
601 case IP_DUMMYNET_GET:
602 if (ip_dn_ctl_ptr != NULL)
603 error = ip_dn_ctl_ptr(sopt);
616 case MRT_API_SUPPORT:
618 case MRT_ADD_BW_UPCALL:
619 case MRT_DEL_BW_UPCALL:
620 error = priv_check(curthread, PRIV_NETINET_MROUTE);
623 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
628 error = ip_ctloutput(so, sopt);
634 switch (sopt->sopt_name) {
636 error = sooptcopyin(sopt, &optval, sizeof optval,
641 inp->inp_flags |= INP_HDRINCL;
643 inp->inp_flags &= ~INP_HDRINCL;
646 case IP_FW3: /* generic ipfw v.3 functions */
652 case IP_FW_TABLE_ADD:
653 case IP_FW_TABLE_DEL:
654 case IP_FW_TABLE_FLUSH:
657 if (V_ip_fw_ctl_ptr != NULL)
658 error = V_ip_fw_ctl_ptr(sopt);
663 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
664 case IP_DUMMYNET_CONFIGURE:
665 case IP_DUMMYNET_DEL:
666 case IP_DUMMYNET_FLUSH:
667 if (ip_dn_ctl_ptr != NULL)
668 error = ip_dn_ctl_ptr(sopt);
670 error = ENOPROTOOPT ;
674 error = priv_check(curthread, PRIV_NETINET_MROUTE);
677 error = ip_rsvp_init(so);
681 error = priv_check(curthread, PRIV_NETINET_MROUTE);
684 error = ip_rsvp_done();
688 case IP_RSVP_VIF_OFF:
689 error = priv_check(curthread, PRIV_NETINET_MROUTE);
692 error = ip_rsvp_vif ?
693 ip_rsvp_vif(so, sopt) : EINVAL;
704 case MRT_API_SUPPORT:
706 case MRT_ADD_BW_UPCALL:
707 case MRT_DEL_BW_UPCALL:
708 error = priv_check(curthread, PRIV_NETINET_MROUTE);
711 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
716 error = ip_ctloutput(so, sopt);
726 * This function exists solely to receive the PRC_IFDOWN messages which are
727 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
728 * in_ifadown() to remove all routes corresponding to that address. It also
729 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
733 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
735 struct rm_priotracker in_ifa_tracker;
736 struct in_ifaddr *ia;
743 IN_IFADDR_RLOCK(&in_ifa_tracker);
744 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
745 if (ia->ia_ifa.ifa_addr == sa
746 && (ia->ia_flags & IFA_ROUTE)) {
747 ifa_ref(&ia->ia_ifa);
748 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
750 * in_scrubprefix() kills the interface route.
752 in_scrubprefix(ia, 0);
754 * in_ifadown gets rid of all the rest of the
755 * routes. This is not quite the right thing
756 * to do, but at least if we are running a
757 * routing process they will come back.
759 in_ifadown(&ia->ia_ifa, 0);
760 ifa_free(&ia->ia_ifa);
764 if (ia == NULL) /* If ia matched, already unlocked. */
765 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
769 IN_IFADDR_RLOCK(&in_ifa_tracker);
770 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
771 if (ia->ia_ifa.ifa_addr == sa)
774 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
775 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
778 ifa_ref(&ia->ia_ifa);
779 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
781 ifp = ia->ia_ifa.ifa_ifp;
783 if ((ifp->if_flags & IFF_LOOPBACK)
784 || (ifp->if_flags & IFF_POINTOPOINT))
787 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
789 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
791 ia->ia_flags |= IFA_ROUTE;
793 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
795 ifa_free(&ia->ia_ifa);
801 rip_attach(struct socket *so, int proto, struct thread *td)
807 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
809 error = priv_check(td, PRIV_NETINET_RAW);
812 if (proto >= IPPROTO_MAX || proto < 0)
813 return EPROTONOSUPPORT;
814 error = soreserve(so, rip_sendspace, rip_recvspace);
817 INP_INFO_WLOCK(&V_ripcbinfo);
818 error = in_pcballoc(so, &V_ripcbinfo);
820 INP_INFO_WUNLOCK(&V_ripcbinfo);
823 inp = (struct inpcb *)so->so_pcb;
824 inp->inp_vflag |= INP_IPV4;
825 inp->inp_ip_p = proto;
826 inp->inp_ip_ttl = V_ip_defttl;
828 INP_INFO_WUNLOCK(&V_ripcbinfo);
834 rip_detach(struct socket *so)
839 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
840 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
841 ("rip_detach: not closed"));
843 INP_INFO_WLOCK(&V_ripcbinfo);
846 if (so == V_ip_mrouter && ip_mrouter_done)
848 if (ip_rsvp_force_done)
849 ip_rsvp_force_done(so);
850 if (so == V_ip_rsvpd)
854 INP_INFO_WUNLOCK(&V_ripcbinfo);
858 rip_dodisconnect(struct socket *so, struct inpcb *inp)
860 struct inpcbinfo *pcbinfo;
862 pcbinfo = inp->inp_pcbinfo;
863 INP_INFO_WLOCK(pcbinfo);
866 inp->inp_faddr.s_addr = INADDR_ANY;
869 so->so_state &= ~SS_ISCONNECTED;
872 INP_INFO_WUNLOCK(pcbinfo);
876 rip_abort(struct socket *so)
881 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
883 rip_dodisconnect(so, inp);
887 rip_close(struct socket *so)
892 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
894 rip_dodisconnect(so, inp);
898 rip_disconnect(struct socket *so)
902 if ((so->so_state & SS_ISCONNECTED) == 0)
906 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
908 rip_dodisconnect(so, inp);
913 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
915 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
919 if (nam->sa_len != sizeof(*addr))
922 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
927 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
929 if (TAILQ_EMPTY(&V_ifnet) ||
930 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
931 (addr->sin_addr.s_addr &&
932 (inp->inp_flags & INP_BINDANY) == 0 &&
933 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
934 return (EADDRNOTAVAIL);
936 INP_INFO_WLOCK(&V_ripcbinfo);
939 inp->inp_laddr = addr->sin_addr;
942 INP_INFO_WUNLOCK(&V_ripcbinfo);
947 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
949 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
952 if (nam->sa_len != sizeof(*addr))
954 if (TAILQ_EMPTY(&V_ifnet))
955 return (EADDRNOTAVAIL);
956 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
957 return (EAFNOSUPPORT);
960 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
962 INP_INFO_WLOCK(&V_ripcbinfo);
965 inp->inp_faddr = addr->sin_addr;
969 INP_INFO_WUNLOCK(&V_ripcbinfo);
974 rip_shutdown(struct socket *so)
979 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
988 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
989 struct mbuf *control, struct thread *td)
995 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
998 * Note: 'dst' reads below are unlocked.
1000 if (so->so_state & SS_ISCONNECTED) {
1005 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1011 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1013 return (rip_output(m, so, dst));
1018 rip_pcblist(SYSCTL_HANDLER_ARGS)
1021 struct inpcb *inp, **inp_list;
1026 * The process of preparing the TCB list is too time-consuming and
1027 * resource-intensive to repeat twice on every request.
1029 if (req->oldptr == 0) {
1030 n = V_ripcbinfo.ipi_count;
1031 n += imax(n / 8, 10);
1032 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1036 if (req->newptr != 0)
1040 * OK, now we're committed to doing something.
1042 INP_INFO_RLOCK(&V_ripcbinfo);
1043 gencnt = V_ripcbinfo.ipi_gencnt;
1044 n = V_ripcbinfo.ipi_count;
1045 INP_INFO_RUNLOCK(&V_ripcbinfo);
1047 xig.xig_len = sizeof xig;
1049 xig.xig_gen = gencnt;
1050 xig.xig_sogen = so_gencnt;
1051 error = SYSCTL_OUT(req, &xig, sizeof xig);
1055 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1056 if (inp_list == NULL)
1059 INP_INFO_RLOCK(&V_ripcbinfo);
1060 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1061 inp = LIST_NEXT(inp, inp_list)) {
1063 if (inp->inp_gencnt <= gencnt &&
1064 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1066 inp_list[i++] = inp;
1070 INP_INFO_RUNLOCK(&V_ripcbinfo);
1074 for (i = 0; i < n; i++) {
1077 if (inp->inp_gencnt <= gencnt) {
1080 bzero(&xi, sizeof(xi));
1081 xi.xi_len = sizeof xi;
1082 /* XXX should avoid extra copy */
1083 bcopy(inp, &xi.xi_inp, sizeof *inp);
1084 if (inp->inp_socket)
1085 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1087 error = SYSCTL_OUT(req, &xi, sizeof xi);
1091 INP_INFO_WLOCK(&V_ripcbinfo);
1092 for (i = 0; i < n; i++) {
1095 if (!in_pcbrele_rlocked(inp))
1098 INP_INFO_WUNLOCK(&V_ripcbinfo);
1102 * Give the user an updated idea of our state. If the
1103 * generation differs from what we told her before, she knows
1104 * that something happened while we were processing this
1105 * request, and it might be necessary to retry.
1107 INP_INFO_RLOCK(&V_ripcbinfo);
1108 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1109 xig.xig_sogen = so_gencnt;
1110 xig.xig_count = V_ripcbinfo.ipi_count;
1111 INP_INFO_RUNLOCK(&V_ripcbinfo);
1112 error = SYSCTL_OUT(req, &xig, sizeof xig);
1114 free(inp_list, M_TEMP);
1118 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1119 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1120 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1123 struct pr_usrreqs rip_usrreqs = {
1124 .pru_abort = rip_abort,
1125 .pru_attach = rip_attach,
1126 .pru_bind = rip_bind,
1127 .pru_connect = rip_connect,
1128 .pru_control = in_control,
1129 .pru_detach = rip_detach,
1130 .pru_disconnect = rip_disconnect,
1131 .pru_peeraddr = in_getpeeraddr,
1132 .pru_send = rip_send,
1133 .pru_shutdown = rip_shutdown,
1134 .pru_sockaddr = in_getsockaddr,
1135 .pru_sosetlabel = in_pcbsosetlabel,
1136 .pru_close = rip_close,