ip_divert.c revision 101088
11902Swollman/*
21902Swollman * Copyright (c) 1982, 1986, 1988, 1993
31902Swollman *	The Regents of the University of California.  All rights reserved.
41902Swollman *
51902Swollman * Redistribution and use in source and binary forms, with or without
61902Swollman * modification, are permitted provided that the following conditions
71902Swollman * are met:
88870Srgrimes * 1. Redistributions of source code must retain the above copyright
91902Swollman *    notice, this list of conditions and the following disclaimer.
101902Swollman * 2. Redistributions in binary form must reproduce the above copyright
111902Swollman *    notice, this list of conditions and the following disclaimer in the
128870Srgrimes *    documentation and/or other materials provided with the distribution.
131902Swollman * 3. All advertising materials mentioning features or use of this software
141902Swollman *    must display the following acknowledgement:
151902Swollman *	This product includes software developed by the University of
168870Srgrimes *	California, Berkeley and its contributors.
171902Swollman * 4. Neither the name of the University nor the names of its contributors
181902Swollman *    may be used to endorse or promote products derived from this software
191902Swollman *    without specific prior written permission.
208870Srgrimes *
211902Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221902Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231902Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248870Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251902Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261902Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271902Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281902Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298870Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301902Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311902Swollman * SUCH DAMAGE.
3236257Swpaul *
331902Swollman * $FreeBSD: head/sys/netinet/ip_divert.c 101088 2002-07-31 16:42:47Z rwatson $
341902Swollman */
351902Swollman
361902Swollman#include "opt_inet.h"
371902Swollman#include "opt_ipfw.h"
381902Swollman#include "opt_ipdivert.h"
391902Swollman#include "opt_ipsec.h"
401902Swollman#include "opt_mac.h"
411902Swollman
421902Swollman#ifndef INET
431902Swollman#error "IPDIVERT requires INET."
441902Swollman#endif
451902Swollman
461902Swollman#include <sys/param.h>
471902Swollman#include <sys/kernel.h>
488870Srgrimes#include <sys/lock.h>
491902Swollman#include <sys/malloc.h>
501902Swollman#include <sys/mac.h>
511902Swollman#include <sys/mbuf.h>
521902Swollman#include <sys/proc.h>
531902Swollman#include <sys/protosw.h>
5411669Sphk#include <sys/signalvar.h>
551902Swollman#include <sys/socket.h>
561902Swollman#include <sys/socketvar.h>
571902Swollman#include <sys/sx.h>
581902Swollman#include <sys/sysctl.h>
591902Swollman#include <sys/systm.h>
601902Swollman
611902Swollman#include <vm/uma.h>
621902Swollman
631902Swollman#include <net/if.h>
641902Swollman#include <net/route.h>
651902Swollman
661902Swollman#include <netinet/in.h>
671902Swollman#include <netinet/in_pcb.h>
681902Swollman#include <netinet/in_systm.h>
691902Swollman#include <netinet/in_var.h>
701902Swollman#include <netinet/ip.h>
7121062Speter#include <netinet/ip_var.h>
721902Swollman
731902Swollman/*
741902Swollman * Divert sockets
751902Swollman */
761902Swollman
771902Swollman/*
781902Swollman * Allocate enough space to hold a full IP packet
791902Swollman */
801902Swollman#define	DIVSNDQ		(65536 + 100)
811902Swollman#define	DIVRCVQ		(65536 + 100)
821902Swollman
831902Swollman/*
841902Swollman * Divert sockets work in conjunction with ipfw, see the divert(4)
851902Swollman * manpage for features.
861902Swollman * Internally, packets selected by ipfw in ip_input() or ip_output(),
871902Swollman * and never diverted before, are passed to the input queue of the
881902Swollman * divert socket with a given 'divert_port' number (as specified in
891902Swollman * the matching ipfw rule), and they are tagged with a 16 bit cookie
901902Swollman * (representing the rule number of the matching ipfw rule), which
911902Swollman * is passed to process reading from the socket.
921902Swollman *
931902Swollman * Packets written to the divert socket are again tagged with a cookie
941902Swollman * (usually the same as above) and a destination address.
951902Swollman * If the destination address is INADDR_ANY then the packet is
961902Swollman * treated as outgoing and sent to ip_output(), otherwise it is
971902Swollman * treated as incoming and sent to ip_input().
9821062Speter * In both cases, the packet is tagged with the cookie.
991902Swollman *
1001902Swollman * On reinjection, processing in ip_input() and ip_output()
1011902Swollman * will be exactly the same as for the original packet, except that
1021902Swollman * ipfw processing will start at the rule number after the one
1031902Swollman * written in the cookie (so, tagging a packet with a cookie of 0
1041902Swollman * will cause it to be effectively considered as a standard packet).
1051902Swollman */
10621062Speter
1071902Swollman/* Internal variables */
1081902Swollmanstatic struct inpcbhead divcb;
1091902Swollmanstatic struct inpcbinfo divcbinfo;
11021062Speter
1111902Swollmanstatic u_long	div_sendspace = DIVSNDQ;	/* XXX sysctl ? */
1121902Swollmanstatic u_long	div_recvspace = DIVRCVQ;	/* XXX sysctl ? */
1131902Swollman
1141902Swollman/* Optimization: have this preinitialized */
11521062Speterstatic struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET };
1161902Swollman
1171902Swollman/*
1181902Swollman * Initialize divert connection block queue.
1191902Swollman */
1201902Swollmanvoid
1211902Swollmandiv_init(void)
1221902Swollman{
1231902Swollman	INP_INFO_LOCK_INIT(&divcbinfo, "div");
1241902Swollman	LIST_INIT(&divcb);
1251902Swollman	divcbinfo.listhead = &divcb;
1261902Swollman	/*
1271902Swollman	 * XXX We don't use the hash list for divert IP, but it's easier
1281902Swollman	 * to allocate a one entry hash list than it is to check all
1291902Swollman	 * over the place for hashbase == NULL.
1301902Swollman	 */
1311902Swollman	divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask);
1321902Swollman	divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask);
1331902Swollman	divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb),
1341902Swollman	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1351902Swollman	uma_zone_set_max(divcbinfo.ipi_zone, maxsockets);
1361902Swollman}
1371902Swollman
1381902Swollman/*
1391902Swollman * IPPROTO_DIVERT is not a real IP protocol; don't allow any packets
1401902Swollman * with that protocol number to enter the system from the outside.
1411902Swollman */
1421902Swollmanvoid
1431902Swollmandiv_input(struct mbuf *m, int off)
1441902Swollman{
1451902Swollman	ipstat.ips_noproto++;
1461902Swollman	m_freem(m);
1471902Swollman}
1481902Swollman
1491902Swollman/*
1508870Srgrimes * Divert a packet by passing it up to the divert socket at port 'port'.
1518870Srgrimes *
1521902Swollman * Setup generic address and protocol structures for div_input routine,
1531902Swollman * then pass them along with mbuf chain.
1541902Swollman */
1551902Swollmanvoid
1561902Swollmandivert_packet(struct mbuf *m, int incoming, int port, int rule)
1571902Swollman{
1581902Swollman	struct ip *ip;
1591902Swollman	struct inpcb *inp;
1601902Swollman	struct socket *sa;
1611902Swollman	u_int16_t nport;
1621902Swollman
1631902Swollman	/* Sanity check */
1641902Swollman	KASSERT(port != 0, ("%s: port=0", __func__));
1651902Swollman
1661902Swollman	divsrc.sin_port = rule;		/* record matching rule */
16721062Speter
1681902Swollman	/* Assure header */
1691902Swollman	if (m->m_len < sizeof(struct ip) &&
1701902Swollman	    (m = m_pullup(m, sizeof(struct ip))) == 0)
1711902Swollman		return;
1721902Swollman	ip = mtod(m, struct ip *);
1731902Swollman
1741902Swollman	/*
1751902Swollman	 * Record receive interface address, if any.
1761902Swollman	 * But only for incoming packets.
1771902Swollman	 */
1781902Swollman	divsrc.sin_addr.s_addr = 0;
17921062Speter	if (incoming) {
18021062Speter		struct ifaddr *ifa;
1811902Swollman
1821902Swollman		/* Sanity check */
1831902Swollman		KASSERT((m->m_flags & M_PKTHDR), ("%s: !PKTHDR", __func__));
1841902Swollman
1851902Swollman		/* Find IP address for receive interface */
1861902Swollman		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
1871902Swollman			if (ifa->ifa_addr == NULL)
1881902Swollman				continue;
1891902Swollman			if (ifa->ifa_addr->sa_family != AF_INET)
1901902Swollman				continue;
1911902Swollman			divsrc.sin_addr =
1921902Swollman			    ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
1931902Swollman			break;
1941902Swollman		}
1951902Swollman	}
1961902Swollman	/*
1971902Swollman	 * Record the incoming interface name whenever we have one.
1981902Swollman	 */
1991902Swollman	bzero(&divsrc.sin_zero, sizeof(divsrc.sin_zero));
2001902Swollman	if (m->m_pkthdr.rcvif) {
2011902Swollman		/*
20221062Speter		 * Hide the actual interface name in there in the
20321062Speter		 * sin_zero array. XXX This needs to be moved to a
2041902Swollman		 * different sockaddr type for divert, e.g.
2051902Swollman		 * sockaddr_div with multiple fields like
20621062Speter		 * sockaddr_dl. Presently we have only 7 bytes
20721062Speter		 * but that will do for now as most interfaces
20821062Speter		 * are 4 or less + 2 or less bytes for unit.
20921062Speter		 * There is probably a faster way of doing this,
21021062Speter		 * possibly taking it from the sockaddr_dl on the iface.
2111902Swollman		 * This solves the problem of a P2P link and a LAN interface
21221062Speter		 * having the same address, which can result in the wrong
2131902Swollman		 * interface being assigned to the packet when fed back
21421062Speter		 * into the divert socket. Theoretically if the daemon saves
2151902Swollman		 * and re-uses the sockaddr_in as suggested in the man pages,
2161902Swollman		 * this iface name will come along for the ride.
2171902Swollman		 * (see div_output for the other half of this.)
2181902Swollman		 */
2191902Swollman		snprintf(divsrc.sin_zero, sizeof(divsrc.sin_zero),
2201902Swollman			"%s%d", m->m_pkthdr.rcvif->if_name,
2211902Swollman			m->m_pkthdr.rcvif->if_unit);
2221902Swollman	}
2231902Swollman
2241902Swollman	/* Put packet on socket queue, if any */
22521062Speter	sa = NULL;
2261902Swollman	nport = htons((u_int16_t)port);
22721062Speter	LIST_FOREACH(inp, &divcb, inp_list) {
2281902Swollman		if (inp->inp_lport == nport)
2291902Swollman			sa = inp->inp_socket;
2301902Swollman	}
2311902Swollman	if (sa) {
23221062Speter		if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc,
2331902Swollman				m, (struct mbuf *)0) == 0)
2341902Swollman			m_freem(m);
2351902Swollman		else
23621062Speter			sorwakeup(sa);
23721062Speter	} else {
2381902Swollman		m_freem(m);
23921062Speter		ipstat.ips_noproto++;
2401902Swollman		ipstat.ips_delivered--;
2411902Swollman        }
2421902Swollman}
2431902Swollman
2441902Swollman/*
2451902Swollman * Deliver packet back into the IP processing machinery.
2461902Swollman *
2471902Swollman * If no address specified, or address is 0.0.0.0, send to ip_output();
2481902Swollman * otherwise, send to ip_input() and mark as having been received on
2491902Swollman * the interface with that address.
2501902Swollman */
2511902Swollmanstatic int
2521902Swollmandiv_output(struct socket *so, struct mbuf *m,
2531902Swollman	struct sockaddr_in *sin, struct mbuf *control)
2541902Swollman{
2551902Swollman	int error = 0;
2561902Swollman	struct m_hdr divert_tag;
2571902Swollman
2581902Swollman	/*
2591902Swollman	 * Prepare the tag for divert info. Note that a packet
2601902Swollman	 * with a 0 tag in mh_data is effectively untagged,
2611902Swollman	 * so we could optimize that case.
2621902Swollman	 */
2631902Swollman	divert_tag.mh_type = MT_TAG;
2648870Srgrimes	divert_tag.mh_flags = PACKET_TAG_DIVERT;
2651902Swollman	divert_tag.mh_next = m;
2661902Swollman	divert_tag.mh_data = 0;		/* the matching rule # */
2671902Swollman	m->m_pkthdr.rcvif = NULL;	/* XXX is it necessary ? */
2681902Swollman
2691902Swollman#ifdef MAC
2701902Swollman	mac_create_mbuf_from_socket(so, m);
2711902Swollman#endif
2721902Swollman
2731902Swollman	if (control)
2741902Swollman		m_freem(control);		/* XXX */
2751902Swollman
2761902Swollman	/* Loopback avoidance and state recovery */
2771902Swollman	if (sin) {
27821062Speter		int i;
2791902Swollman
2801902Swollman		divert_tag.mh_data = (caddr_t)(int)sin->sin_port;
28121062Speter		/*
28221062Speter		 * Find receive interface with the given name, stuffed
2831902Swollman		 * (if it exists) in the sin_zero[] field.
28421062Speter		 * The name is user supplied data so don't trust its size
2851902Swollman		 * or that it is zero terminated.
2861902Swollman		 */
2871902Swollman		for (i = 0; sin->sin_zero[i] && i < sizeof(sin->sin_zero); i++)
2881902Swollman			;
2891902Swollman		if ( i > 0 && i < sizeof(sin->sin_zero))
2901902Swollman			m->m_pkthdr.rcvif = ifunit(sin->sin_zero);
2911902Swollman	}
2921902Swollman
2931902Swollman	/* Reinject packet into the system as incoming or outgoing */
2941902Swollman	if (!sin || sin->sin_addr.s_addr == 0) {
2951902Swollman		struct inpcb *const inp = sotoinpcb(so);
2961902Swollman		struct ip *const ip = mtod(m, struct ip *);
2971902Swollman
2981902Swollman		/*
2991902Swollman		 * Don't allow both user specified and setsockopt options,
3001902Swollman		 * and don't allow packet length sizes that will crash
3011902Swollman		 */
3021902Swollman		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
3031902Swollman		     ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
30421062Speter			error = EINVAL;
3051902Swollman			goto cantsend;
3061902Swollman		}
3071902Swollman
3081902Swollman		/* Convert fields to host order for ip_output() */
3091902Swollman		ip->ip_len = ntohs(ip->ip_len);
3101902Swollman		ip->ip_off = ntohs(ip->ip_off);
3111902Swollman
3121902Swollman		/* Send packet to output processing */
3131902Swollman		ipstat.ips_rawout++;			/* XXX */
3141902Swollman		error = ip_output((struct mbuf *)&divert_tag,
3151902Swollman			    inp->inp_options, &inp->inp_route,
3161902Swollman			    (so->so_options & SO_DONTROUTE) |
31721062Speter			    IP_ALLOWBROADCAST | IP_RAWOUTPUT,
3181902Swollman			    inp->inp_moptions);
3191902Swollman	} else {
3201902Swollman		if (m->m_pkthdr.rcvif == NULL) {
3211902Swollman			/*
3221902Swollman			 * No luck with the name, check by IP address.
3231902Swollman			 * Clear the port and the ifname to make sure
3241902Swollman			 * there are no distractions for ifa_ifwithaddr.
3251902Swollman			 */
3261902Swollman			struct	ifaddr *ifa;
3271902Swollman
3281902Swollman			bzero(sin->sin_zero, sizeof(sin->sin_zero));
3291902Swollman			sin->sin_port = 0;
3301902Swollman			ifa = ifa_ifwithaddr((struct sockaddr *) sin);
3311902Swollman			if (ifa == NULL) {
3321902Swollman				error = EADDRNOTAVAIL;
3331902Swollman				goto cantsend;
3341902Swollman			}
3351902Swollman			m->m_pkthdr.rcvif = ifa->ifa_ifp;
3361902Swollman		}
3371902Swollman		/* Send packet to input processing */
3381902Swollman		ip_input((struct mbuf *)&divert_tag);
3391902Swollman	}
3401902Swollman
3411902Swollman	return error;
3421902Swollman
3431902Swollmancantsend:
3441902Swollman	m_freem(m);
3451902Swollman	return error;
3461902Swollman}
3471902Swollman
3481902Swollmanstatic int
3491902Swollmandiv_attach(struct socket *so, int proto, struct thread *td)
3501902Swollman{
3511902Swollman	struct inpcb *inp;
3521902Swollman	int error, s;
3531902Swollman
3541902Swollman	inp  = sotoinpcb(so);
3551902Swollman	if (inp)
3561902Swollman		panic("div_attach");
3571902Swollman	if (td && (error = suser(td)) != 0)
3581902Swollman		return error;
35921062Speter
3601902Swollman	error = soreserve(so, div_sendspace, div_recvspace);
3611902Swollman	if (error)
3621902Swollman		return error;
3631902Swollman	s = splnet();
3641902Swollman	error = in_pcballoc(so, &divcbinfo, td);
36521062Speter	splx(s);
3661902Swollman	if (error)
3671902Swollman		return error;
3681902Swollman	inp = (struct inpcb *)so->so_pcb;
3691902Swollman	inp->inp_ip_p = proto;
3701902Swollman	inp->inp_vflag |= INP_IPV4;
37121062Speter	inp->inp_flags |= INP_HDRINCL;
3721902Swollman	/* The socket is always "connected" because
3731902Swollman	   we always know "where" to send the packet */
3741902Swollman	so->so_state |= SS_ISCONNECTED;
3751902Swollman	return 0;
3761902Swollman}
3771902Swollman
3781902Swollmanstatic int
37921062Speterdiv_detach(struct socket *so)
3801902Swollman{
3811902Swollman	struct inpcb *inp;
3821902Swollman
3831902Swollman	inp = sotoinpcb(so);
3841902Swollman	if (inp == 0)
3851902Swollman		panic("div_detach");
3861902Swollman	in_pcbdetach(inp);
3871902Swollman	return 0;
3881902Swollman}
3891902Swollman
3901902Swollmanstatic int
3911902Swollmandiv_abort(struct socket *so)
3921902Swollman{
3931902Swollman	soisdisconnected(so);
3941902Swollman	return div_detach(so);
3951902Swollman}
3961902Swollman
3971902Swollmanstatic int
3981902Swollmandiv_disconnect(struct socket *so)
3991902Swollman{
4001902Swollman	if ((so->so_state & SS_ISCONNECTED) == 0)
4011902Swollman		return ENOTCONN;
4021902Swollman	return div_abort(so);
4031902Swollman}
4041902Swollman
4051902Swollmanstatic int
4061902Swollmandiv_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
4071902Swollman{
4081902Swollman	struct inpcb *inp;
4091902Swollman	int s;
4101902Swollman	int error;
4111902Swollman
4121902Swollman	s = splnet();
4131902Swollman	inp = sotoinpcb(so);
4141902Swollman	/* in_pcbbind assumes that nam is a sockaddr_in
4151902Swollman	 * and in_pcbbind requires a valid address. Since divert
4161902Swollman	 * sockets don't we need to make sure the address is
4171902Swollman	 * filled in properly.
4181902Swollman	 * XXX -- divert should not be abusing in_pcbind
4191902Swollman	 * and should probably have its own family.
4201902Swollman	 */
4211902Swollman	if (nam->sa_family != AF_INET)
4221902Swollman		error = EAFNOSUPPORT;
4231902Swollman	else {
4241902Swollman		((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY;
4251902Swollman		error = in_pcbbind(inp, nam, td);
4261902Swollman	}
4278870Srgrimes	splx(s);
4281902Swollman	return error;
4291902Swollman}
4301902Swollman
4311902Swollmanstatic int
4321902Swollmandiv_shutdown(struct socket *so)
4331902Swollman{
4341902Swollman	socantsendmore(so);
4351902Swollman	return 0;
4361902Swollman}
4371902Swollman
4381902Swollmanstatic int
4391902Swollmandiv_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
4401902Swollman	 struct mbuf *control, struct thread *td)
4411902Swollman{
4421902Swollman	/* Packet must have a header (but that's about it) */
4431902Swollman	if (m->m_len < sizeof (struct ip) &&
4441902Swollman	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
4451902Swollman		ipstat.ips_toosmall++;
4461902Swollman		m_freem(m);
4471902Swollman		return EINVAL;
4481902Swollman	}
4491902Swollman
4501902Swollman	/* Send packet */
4511902Swollman	return div_output(so, m, (struct sockaddr_in *)nam, control);
4521902Swollman}
4531902Swollman
4541902Swollmanstatic int
4551902Swollmandiv_pcblist(SYSCTL_HANDLER_ARGS)
4561902Swollman{
4571902Swollman	int error, i, n, s;
4581902Swollman	struct inpcb *inp, **inp_list;
4591902Swollman	inp_gen_t gencnt;
4601902Swollman	struct xinpgen xig;
4611902Swollman
4621902Swollman	/*
46321062Speter	 * The process of preparing the TCB list is too time-consuming and
4641902Swollman	 * resource-intensive to repeat twice on every request.
4651902Swollman	 */
4661902Swollman	if (req->oldptr == 0) {
4671902Swollman		n = divcbinfo.ipi_count;
4681902Swollman		req->oldidx = 2 * (sizeof xig)
46921062Speter			+ (n + n/8) * sizeof(struct xinpcb);
4701902Swollman		return 0;
47121062Speter	}
47221062Speter
4731902Swollman	if (req->newptr != 0)
4741902Swollman		return EPERM;
4751902Swollman
4761902Swollman	/*
4771902Swollman	 * OK, now we're committed to doing something.
4781902Swollman	 */
4791902Swollman	s = splnet();
4801902Swollman	gencnt = divcbinfo.ipi_gencnt;
4811902Swollman	n = divcbinfo.ipi_count;
4821902Swollman	splx(s);
4831902Swollman
4841902Swollman	xig.xig_len = sizeof xig;
4851902Swollman	xig.xig_count = n;
48621062Speter	xig.xig_gen = gencnt;
48721062Speter	xig.xig_sogen = so_gencnt;
4881902Swollman	error = SYSCTL_OUT(req, &xig, sizeof xig);
4891902Swollman	if (error)
4901902Swollman		return error;
4911902Swollman
4921902Swollman	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
4931902Swollman	if (inp_list == 0)
49421062Speter		return ENOMEM;
49521062Speter
4961902Swollman	s = splnet();
4971902Swollman	for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n;
4981902Swollman	     inp = LIST_NEXT(inp, inp_list)) {
4991902Swollman		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->td, inp))
5001902Swollman			inp_list[i++] = inp;
5011902Swollman	}
5021902Swollman	splx(s);
5031902Swollman	n = i;
50421062Speter
50521062Speter	error = 0;
5061902Swollman	for (i = 0; i < n; i++) {
5071902Swollman		inp = inp_list[i];
50821062Speter		if (inp->inp_gencnt <= gencnt) {
5091902Swollman			struct xinpcb xi;
5101902Swollman			xi.xi_len = sizeof xi;
5111902Swollman			/* XXX should avoid extra copy */
5121902Swollman			bcopy(inp, &xi.xi_inp, sizeof *inp);
5131902Swollman			if (inp->inp_socket)
5141902Swollman				sotoxsocket(inp->inp_socket, &xi.xi_socket);
5151902Swollman			error = SYSCTL_OUT(req, &xi, sizeof xi);
5161902Swollman		}
5171902Swollman	}
5181902Swollman	if (!error) {
5191902Swollman		/*
5201902Swollman		 * Give the user an updated idea of our state.
5211902Swollman		 * If the generation differs from what we told
5221902Swollman		 * her before, she knows that something happened
5231902Swollman		 * while we were processing this request, and it
5241902Swollman		 * might be necessary to retry.
52521062Speter		 */
5261902Swollman		s = splnet();
5271902Swollman		xig.xig_gen = divcbinfo.ipi_gencnt;
52821062Speter		xig.xig_sogen = so_gencnt;
5291902Swollman		xig.xig_count = divcbinfo.ipi_count;
5301902Swollman		splx(s);
5311902Swollman		error = SYSCTL_OUT(req, &xig, sizeof xig);
5321902Swollman	}
5331902Swollman	free(inp_list, M_TEMP);
5341902Swollman	return error;
53521062Speter}
5361902Swollman
5371902Swollman/*
5381902Swollman * This is the wrapper function for in_setsockaddr.  We just pass down
5391902Swollman * the pcbinfo for in_setpeeraddr to lock.
5401902Swollman */
5411902Swollmanstatic int
5421902Swollmandiv_sockaddr(struct socket *so, struct sockaddr **nam)
5431902Swollman{
5441902Swollman	return (in_setsockaddr(so, nam, &divcbinfo));
5451902Swollman}
5461902Swollman
54721062Speter/*
5481902Swollman * This is the wrapper function for in_setpeeraddr. We just pass down
5491902Swollman * the pcbinfo for in_setpeeraddr to lock.
5501902Swollman */
5511902Swollmanstatic int
5521902Swollmandiv_peeraddr(struct socket *so, struct sockaddr **nam)
55336087Swpaul{
55436087Swpaul	return (in_setpeeraddr(so, nam, &divcbinfo));
55536257Swpaul}
55636257Swpaul
55736257Swpaul
55836257SwpaulSYSCTL_DECL(_net_inet_divert);
55936257SwpaulSYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0,
56036087Swpaul	    div_pcblist, "S,xinpcb", "List of active divert sockets");
56136257Swpaul
56236087Swpaulstruct pr_usrreqs div_usrreqs = {
5631902Swollman	div_abort, pru_accept_notsupp, div_attach, div_bind,
5641902Swollman	pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach,
5651902Swollman	div_disconnect, pru_listen_notsupp, div_peeraddr, pru_rcvd_notsupp,
5661902Swollman	pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown,
5671902Swollman	div_sockaddr, sosend, soreceive, sopoll
5681902Swollman};
5691902Swollman