1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
493e0e116SJulian Elischer * Copyright (c) 1982, 1986, 1988, 1993
593e0e116SJulian Elischer * The Regents of the University of California. All rights reserved.
693e0e116SJulian Elischer *
793e0e116SJulian Elischer * Redistribution and use in source and binary forms, with or without
893e0e116SJulian Elischer * modification, are permitted provided that the following conditions
993e0e116SJulian Elischer * are met:
1093e0e116SJulian Elischer * 1. Redistributions of source code must retain the above copyright
1193e0e116SJulian Elischer * notice, this list of conditions and the following disclaimer.
1293e0e116SJulian Elischer * 2. Redistributions in binary form must reproduce the above copyright
1393e0e116SJulian Elischer * notice, this list of conditions and the following disclaimer in the
1493e0e116SJulian Elischer * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1693e0e116SJulian Elischer * may be used to endorse or promote products derived from this software
1793e0e116SJulian Elischer * without specific prior written permission.
1893e0e116SJulian Elischer *
1993e0e116SJulian Elischer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2093e0e116SJulian Elischer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2193e0e116SJulian Elischer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2293e0e116SJulian Elischer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2393e0e116SJulian Elischer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2493e0e116SJulian Elischer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2593e0e116SJulian Elischer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2693e0e116SJulian Elischer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2793e0e116SJulian Elischer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2893e0e116SJulian Elischer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2993e0e116SJulian Elischer * SUCH DAMAGE.
3093e0e116SJulian Elischer */
3193e0e116SJulian Elischer
324b421e2dSMike Silbersack #include <sys/cdefs.h>
331d5e9e22SEivind Eklund #include "opt_inet.h"
34aa57e971SBjoern A. Zeeb #include "opt_inet6.h"
352f4afd21SRandall Stewart #include "opt_sctp.h"
361d5e9e22SEivind Eklund
3793e0e116SJulian Elischer #include <sys/param.h>
38c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
39cec335f9SRuslan Ermilov #include <sys/kernel.h>
40960ed29cSSeigo Tanimura #include <sys/lock.h>
4193e0e116SJulian Elischer #include <sys/malloc.h>
4293e0e116SJulian Elischer #include <sys/mbuf.h>
4372584fd2SAndre Oppermann #include <sys/module.h>
4472584fd2SAndre Oppermann #include <sys/kernel.h>
45acd3428bSRobert Watson #include <sys/priv.h>
46ce178806SRobert Watson #include <sys/proc.h>
478624f434SGleb Smirnoff #include <sys/domain.h>
4893e0e116SJulian Elischer #include <sys/protosw.h>
49960ed29cSSeigo Tanimura #include <sys/socket.h>
5093e0e116SJulian Elischer #include <sys/socketvar.h>
51cec335f9SRuslan Ermilov #include <sys/sysctl.h>
52b2019e17SLuigi Rizzo #include <net/vnet.h>
5393e0e116SJulian Elischer
5493e0e116SJulian Elischer #include <net/if.h>
5576039bc8SGleb Smirnoff #include <net/if_var.h>
563d0d5b21SJustin Hibbits #include <net/if_private.h>
57b244c8adSChristian S.J. Peron #include <net/netisr.h>
5893e0e116SJulian Elischer
5993e0e116SJulian Elischer #include <netinet/in.h>
6093e0e116SJulian Elischer #include <netinet/in_pcb.h>
61960ed29cSSeigo Tanimura #include <netinet/in_systm.h>
6293e0e116SJulian Elischer #include <netinet/in_var.h>
63960ed29cSSeigo Tanimura #include <netinet/ip.h>
6493e0e116SJulian Elischer #include <netinet/ip_var.h>
652b1c7217SGleb Smirnoff #include <netinet/ip_divert.h>
66812f1d32SGleb Smirnoff #ifdef INET6
67812f1d32SGleb Smirnoff #include <netinet/ip6.h>
68812f1d32SGleb Smirnoff #include <netinet6/ip6_var.h>
69812f1d32SGleb Smirnoff #endif
7095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
712f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
722f4afd21SRandall Stewart #endif
7393e0e116SJulian Elischer
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
7593e0e116SJulian Elischer /*
7693e0e116SJulian Elischer * Divert sockets
7793e0e116SJulian Elischer */
7893e0e116SJulian Elischer
7993e0e116SJulian Elischer /*
8093e0e116SJulian Elischer * Allocate enough space to hold a full IP packet
8193e0e116SJulian Elischer */
8293e0e116SJulian Elischer #define DIVSNDQ (65536 + 100)
8393e0e116SJulian Elischer #define DIVRCVQ (65536 + 100)
8493e0e116SJulian Elischer
8593e0e116SJulian Elischer /*
86f1fb0517SGleb Smirnoff * Usually a system has very few divert ports. Previous implementation
87f1fb0517SGleb Smirnoff * used a linked list.
88f1fb0517SGleb Smirnoff */
89f1fb0517SGleb Smirnoff #define DIVHASHSIZE (1 << 3) /* 8 entries, one cache line. */
90f1fb0517SGleb Smirnoff #define DIVHASH(port) (port % DIVHASHSIZE)
91f1fb0517SGleb Smirnoff #define DCBHASH(dcb) ((dcb)->dcb_port % DIVHASHSIZE)
92f1fb0517SGleb Smirnoff
93f1fb0517SGleb Smirnoff /*
94b2019e17SLuigi Rizzo * Divert sockets work in conjunction with ipfw or other packet filters,
95b2019e17SLuigi Rizzo * see the divert(4) manpage for features.
96b2019e17SLuigi Rizzo * Packets are selected by the packet filter and tagged with an
97b2019e17SLuigi Rizzo * MTAG_IPFW_RULE tag carrying the 'divert port' number (as set by
98b2019e17SLuigi Rizzo * the packet filter) and information on the matching filter rule for
99b2019e17SLuigi Rizzo * subsequent reinjection. The divert_port is used to put the packet
100b2019e17SLuigi Rizzo * on the corresponding divert socket, while the rule number is passed
101b2019e17SLuigi Rizzo * up (at least partially) as the sin_port in the struct sockaddr.
102bb60f459SJulian Elischer *
103b2019e17SLuigi Rizzo * Packets written to the divert socket carry in sin_addr a
104b2019e17SLuigi Rizzo * destination address, and in sin_port the number of the filter rule
105b2019e17SLuigi Rizzo * after which to continue processing.
106b2019e17SLuigi Rizzo * If the destination address is INADDR_ANY, the packet is treated as
107b2019e17SLuigi Rizzo * as outgoing and sent to ip_output(); otherwise it is treated as
108b2019e17SLuigi Rizzo * incoming and sent to ip_input().
109b2019e17SLuigi Rizzo * Further, sin_zero carries some information on the interface,
110b2019e17SLuigi Rizzo * which can be used in the reinject -- see comments in the code.
1118948e4baSArchie Cobbs *
1122b25acc1SLuigi Rizzo * On reinjection, processing in ip_input() and ip_output()
1132b25acc1SLuigi Rizzo * will be exactly the same as for the original packet, except that
114b2019e17SLuigi Rizzo * packet filter processing will start at the rule number after the one
115b2019e17SLuigi Rizzo * written in the sin_port (ipfw does not allow a rule #0, so sin_port=0
116b2019e17SLuigi Rizzo * will apply the entire ruleset to the packet).
11793e0e116SJulian Elischer */
1182b1c7217SGleb Smirnoff static SYSCTL_NODE(_net_inet, OID_AUTO, divert, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1192b1c7217SGleb Smirnoff "divert(4)");
12093e0e116SJulian Elischer
1212b1c7217SGleb Smirnoff VNET_PCPUSTAT_DEFINE_STATIC(struct divstat, divstat);
1222b1c7217SGleb Smirnoff VNET_PCPUSTAT_SYSINIT(divstat);
1232b1c7217SGleb Smirnoff #ifdef VIMAGE
1242b1c7217SGleb Smirnoff VNET_PCPUSTAT_SYSUNINIT(divstat);
1252b1c7217SGleb Smirnoff #endif
1262b1c7217SGleb Smirnoff SYSCTL_VNET_PCPUSTAT(_net_inet_divert, OID_AUTO, stats, struct divstat,
1272b1c7217SGleb Smirnoff divstat, "divert(4) socket statistics");
1282b1c7217SGleb Smirnoff #define DIVSTAT_INC(name) \
1292b1c7217SGleb Smirnoff VNET_PCPUSTAT_ADD(struct divstat, divstat, div_ ## name, 1)
1302b1c7217SGleb Smirnoff
13193e0e116SJulian Elischer static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */
13293e0e116SJulian Elischer static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */
13393e0e116SJulian Elischer
1345533ec48SAlexander V. Chernikov static int div_output_inbound(int fmaily, struct socket *so, struct mbuf *m,
1355533ec48SAlexander V. Chernikov struct sockaddr_in *sin);
1365533ec48SAlexander V. Chernikov static int div_output_outbound(int family, struct socket *so, struct mbuf *m);
1375533ec48SAlexander V. Chernikov
138f1fb0517SGleb Smirnoff struct divcb {
139f1fb0517SGleb Smirnoff union {
140f1fb0517SGleb Smirnoff SLIST_ENTRY(divcb) dcb_next;
141f1fb0517SGleb Smirnoff intptr_t dcb_bound;
142f1fb0517SGleb Smirnoff #define DCB_UNBOUND ((intptr_t)-1)
143f1fb0517SGleb Smirnoff };
144f1fb0517SGleb Smirnoff struct socket *dcb_socket;
145f1fb0517SGleb Smirnoff uint16_t dcb_port;
146f1fb0517SGleb Smirnoff uint64_t dcb_gencnt;
147f1fb0517SGleb Smirnoff struct epoch_context dcb_epochctx;
148f1fb0517SGleb Smirnoff };
149d915b280SStephan Uphoff
150f1fb0517SGleb Smirnoff SLIST_HEAD(divhashhead, divcb);
151f2565d68SRobert Watson
152f1fb0517SGleb Smirnoff VNET_DEFINE_STATIC(struct divhashhead, divhash[DIVHASHSIZE]) = {};
153f1fb0517SGleb Smirnoff #define V_divhash VNET(divhash)
154f1fb0517SGleb Smirnoff VNET_DEFINE_STATIC(uint64_t, dcb_count) = 0;
155f1fb0517SGleb Smirnoff #define V_dcb_count VNET(dcb_count)
156f1fb0517SGleb Smirnoff VNET_DEFINE_STATIC(uint64_t, dcb_gencnt) = 0;
157f1fb0517SGleb Smirnoff #define V_dcb_gencnt VNET(dcb_gencnt)
1582b73aacaSMarko Zec
159f1fb0517SGleb Smirnoff static struct mtx divert_mtx;
160f1fb0517SGleb Smirnoff MTX_SYSINIT(divert, &divert_mtx, "divert(4) socket pcb lists", MTX_DEF);
161f1fb0517SGleb Smirnoff #define DIVERT_LOCK() mtx_lock(&divert_mtx)
162f1fb0517SGleb Smirnoff #define DIVERT_UNLOCK() mtx_unlock(&divert_mtx)
163db0ac6deSCy Schubert
1648948e4baSArchie Cobbs /*
1658948e4baSArchie Cobbs * Divert a packet by passing it up to the divert socket at port 'port'.
1668948e4baSArchie Cobbs */
16772584fd2SAndre Oppermann static void
divert_packet(struct mbuf * m,bool incoming)1681830dae3SGleb Smirnoff divert_packet(struct mbuf *m, bool incoming)
1698948e4baSArchie Cobbs {
170f1fb0517SGleb Smirnoff struct divcb *dcb;
1718948e4baSArchie Cobbs u_int16_t nport;
17226f91065SSam Leffler struct sockaddr_in divsrc;
173ac9d7e26SMax Laier struct m_tag *mtag;
174fabf705fSIgor Ostapenko uint16_t cookie;
175de537d63SGleb Smirnoff
176de537d63SGleb Smirnoff NET_EPOCH_ASSERT();
17793e0e116SJulian Elischer
1787173b6e5SLuigi Rizzo mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
179fabf705fSIgor Ostapenko if (mtag != NULL) {
180fabf705fSIgor Ostapenko cookie = ((struct ipfw_rule_ref *)(mtag+1))->rulenum;
181fabf705fSIgor Ostapenko nport = htons((uint16_t)
182fabf705fSIgor Ostapenko (((struct ipfw_rule_ref *)(mtag+1))->info));
183fabf705fSIgor Ostapenko } else if ((mtag = m_tag_locate(m, MTAG_PF_DIVERT, 0, NULL)) != NULL) {
184fabf705fSIgor Ostapenko cookie = ((struct pf_divert_mtag *)(mtag+1))->idir;
185c1146e6aSKristof Provost nport = htons(((struct pf_divert_mtag *)(mtag+1))->port);
186fabf705fSIgor Ostapenko } else {
187ac9d7e26SMax Laier m_freem(m);
188ac9d7e26SMax Laier return;
189ac9d7e26SMax Laier }
190e4676ba6SJulian Elischer /* Assure header */
191e4676ba6SJulian Elischer if (m->m_len < sizeof(struct ip) &&
19299d628d5SPedro F. Giffuni (m = m_pullup(m, sizeof(struct ip))) == NULL)
193e4676ba6SJulian Elischer return;
194e72c5228SGleb Smirnoff #ifdef INET
195f0cada84SAndre Oppermann /* Delayed checksums are currently not compatible with divert. */
196f0cada84SAndre Oppermann if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
197f0cada84SAndre Oppermann in_delayed_cksum(m);
198f0cada84SAndre Oppermann m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
199f0cada84SAndre Oppermann }
20095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
2012f4afd21SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
202e72c5228SGleb Smirnoff struct ip *ip;
203e72c5228SGleb Smirnoff
20439f7de58SJohn Baldwin ip = mtod(m, struct ip *);
2051966e5b5SRandall Stewart sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
2062f4afd21SRandall Stewart m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
2072f4afd21SRandall Stewart }
2082f4afd21SRandall Stewart #endif
209999c9fd7SGleb Smirnoff #endif
2104a9e9528SAndrey V. Elsukov #ifdef INET6
2114a9e9528SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
2124a9e9528SAndrey V. Elsukov in6_delayed_cksum(m, m->m_pkthdr.len -
2134a9e9528SAndrey V. Elsukov sizeof(struct ip6_hdr), sizeof(struct ip6_hdr));
2144a9e9528SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
2154a9e9528SAndrey V. Elsukov }
2164a9e9528SAndrey V. Elsukov #if defined(SCTP) || defined(SCTP_SUPPORT)
2174a9e9528SAndrey V. Elsukov if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
2184a9e9528SAndrey V. Elsukov sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
2194a9e9528SAndrey V. Elsukov m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
2204a9e9528SAndrey V. Elsukov }
2214a9e9528SAndrey V. Elsukov #endif
2224a9e9528SAndrey V. Elsukov #endif /* INET6 */
2237173b6e5SLuigi Rizzo bzero(&divsrc, sizeof(divsrc));
2247173b6e5SLuigi Rizzo divsrc.sin_len = sizeof(divsrc);
2257173b6e5SLuigi Rizzo divsrc.sin_family = AF_INET;
2267173b6e5SLuigi Rizzo /* record matching rule, in host format */
227fabf705fSIgor Ostapenko divsrc.sin_port = cookie;
228efe39c6aSJulian Elischer /*
2298948e4baSArchie Cobbs * Record receive interface address, if any.
230efe39c6aSJulian Elischer * But only for incoming packets.
231efe39c6aSJulian Elischer */
2328948e4baSArchie Cobbs if (incoming) {
23393e0e116SJulian Elischer struct ifaddr *ifa;
234b132600aSRobert Watson struct ifnet *ifp;
23593e0e116SJulian Elischer
236e4676ba6SJulian Elischer /* Sanity check */
237fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m);
23893e0e116SJulian Elischer
239436c7212SJulian Elischer /* Find IP address for receive interface */
240b132600aSRobert Watson ifp = m->m_pkthdr.rcvif;
241d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
24293e0e116SJulian Elischer if (ifa->ifa_addr->sa_family != AF_INET)
24393e0e116SJulian Elischer continue;
24493e0e116SJulian Elischer divsrc.sin_addr =
24593e0e116SJulian Elischer ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
24693e0e116SJulian Elischer break;
24793e0e116SJulian Elischer }
248bab04eb8SJulian Elischer }
249efe39c6aSJulian Elischer /*
250efe39c6aSJulian Elischer * Record the incoming interface name whenever we have one.
251efe39c6aSJulian Elischer */
252bab04eb8SJulian Elischer if (m->m_pkthdr.rcvif) {
253436c7212SJulian Elischer /*
254436c7212SJulian Elischer * Hide the actual interface name in there in the
255436c7212SJulian Elischer * sin_zero array. XXX This needs to be moved to a
256436c7212SJulian Elischer * different sockaddr type for divert, e.g.
257436c7212SJulian Elischer * sockaddr_div with multiple fields like
258436c7212SJulian Elischer * sockaddr_dl. Presently we have only 7 bytes
259436c7212SJulian Elischer * but that will do for now as most interfaces
260436c7212SJulian Elischer * are 4 or less + 2 or less bytes for unit.
261436c7212SJulian Elischer * There is probably a faster way of doing this,
262436c7212SJulian Elischer * possibly taking it from the sockaddr_dl on the iface.
263436c7212SJulian Elischer * This solves the problem of a P2P link and a LAN interface
264436c7212SJulian Elischer * having the same address, which can result in the wrong
265436c7212SJulian Elischer * interface being assigned to the packet when fed back
266436c7212SJulian Elischer * into the divert socket. Theoretically if the daemon saves
267436c7212SJulian Elischer * and re-uses the sockaddr_in as suggested in the man pages,
268436c7212SJulian Elischer * this iface name will come along for the ride.
269436c7212SJulian Elischer * (see div_output for the other half of this.)
270436c7212SJulian Elischer */
2719bf40edeSBrooks Davis strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname,
2729bf40edeSBrooks Davis sizeof(divsrc.sin_zero));
27393e0e116SJulian Elischer }
27493e0e116SJulian Elischer
27593e0e116SJulian Elischer /* Put packet on socket queue, if any */
276f1fb0517SGleb Smirnoff SLIST_FOREACH(dcb, &V_divhash[DIVHASH(nport)], dcb_next)
277f1fb0517SGleb Smirnoff if (dcb->dcb_port == nport)
278f1fb0517SGleb Smirnoff break;
279f1fb0517SGleb Smirnoff
280f1fb0517SGleb Smirnoff if (dcb != NULL) {
281f1fb0517SGleb Smirnoff struct socket *sa = dcb->dcb_socket;
282f1fb0517SGleb Smirnoff
2831e4d7da7SRobert Watson SOCKBUF_LOCK(&sa->so_rcv);
2841e4d7da7SRobert Watson if (sbappendaddr_locked(&sa->so_rcv,
285db0ac6deSCy Schubert (struct sockaddr *)&divsrc, m, NULL) == 0) {
2867045b160SRoy Marples soroverflow_locked(sa);
287f1fb0517SGleb Smirnoff m_freem(m);
2882b1c7217SGleb Smirnoff } else {
2891e4d7da7SRobert Watson sorwakeup_locked(sa);
2902b1c7217SGleb Smirnoff DIVSTAT_INC(diverted);
2912b1c7217SGleb Smirnoff }
292f1fb0517SGleb Smirnoff } else {
2932b1c7217SGleb Smirnoff DIVSTAT_INC(noport);
294f1fb0517SGleb Smirnoff m_freem(m);
29593e0e116SJulian Elischer }
29693e0e116SJulian Elischer }
29793e0e116SJulian Elischer
29893e0e116SJulian Elischer /*
29993e0e116SJulian Elischer * Deliver packet back into the IP processing machinery.
30093e0e116SJulian Elischer *
30193e0e116SJulian Elischer * If no address specified, or address is 0.0.0.0, send to ip_output();
30293e0e116SJulian Elischer * otherwise, send to ip_input() and mark as having been received on
30393e0e116SJulian Elischer * the interface with that address.
30493e0e116SJulian Elischer */
30593e0e116SJulian Elischer static int
div_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)3068fc80638SGleb Smirnoff div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
3078fc80638SGleb Smirnoff struct mbuf *control, struct thread *td)
30893e0e116SJulian Elischer {
309b9555453SGleb Smirnoff struct epoch_tracker et;
3108fc80638SGleb Smirnoff struct sockaddr_in *sin = (struct sockaddr_in *)nam;
3115533ec48SAlexander V. Chernikov const struct ip *ip;
3126daf7ebdSBrian Feldman struct m_tag *mtag;
3137173b6e5SLuigi Rizzo struct ipfw_rule_ref *dt;
314fabf705fSIgor Ostapenko struct pf_divert_mtag *pfdt;
3155533ec48SAlexander V. Chernikov int error, family;
3162b25acc1SLuigi Rizzo
3178fc80638SGleb Smirnoff if (control)
3188fc80638SGleb Smirnoff m_freem(control);
3198fc80638SGleb Smirnoff
3208fc80638SGleb Smirnoff /* Packet must have a header (but that's about it) */
3218fc80638SGleb Smirnoff if (m->m_len < sizeof (struct ip) &&
3228fc80638SGleb Smirnoff (m = m_pullup(m, sizeof (struct ip))) == NULL) {
3238fc80638SGleb Smirnoff m_freem(m);
3248fc80638SGleb Smirnoff return (EINVAL);
325f161d294SMark Johnston }
326f161d294SMark Johnston
327f161d294SMark Johnston if (sin != NULL) {
328f161d294SMark Johnston if (sin->sin_family != AF_INET) {
329f161d294SMark Johnston m_freem(m);
330f161d294SMark Johnston return (EAFNOSUPPORT);
331f161d294SMark Johnston }
332f161d294SMark Johnston if (sin->sin_len != sizeof(*sin)) {
333f161d294SMark Johnston m_freem(m);
334f161d294SMark Johnston return (EINVAL);
335f161d294SMark Johnston }
336f161d294SMark Johnston }
337f161d294SMark Johnston
338b3cf6808SGleb Smirnoff /*
339b3cf6808SGleb Smirnoff * An mbuf may hasn't come from userland, but we pretend
340b3cf6808SGleb Smirnoff * that it has.
341b3cf6808SGleb Smirnoff */
342ea0bd576SGleb Smirnoff m->m_pkthdr.rcvif = NULL;
343b3cf6808SGleb Smirnoff m->m_nextpkt = NULL;
344bc97ba51SJulian Elischer M_SETFIB(m, so->so_fibnum);
34593e0e116SJulian Elischer
3467173b6e5SLuigi Rizzo mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
3477173b6e5SLuigi Rizzo if (mtag == NULL) {
3487173b6e5SLuigi Rizzo /* this should be normal */
3497173b6e5SLuigi Rizzo mtag = m_tag_alloc(MTAG_IPFW_RULE, 0,
3507173b6e5SLuigi Rizzo sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO);
351ac9d7e26SMax Laier if (mtag == NULL) {
3525533ec48SAlexander V. Chernikov m_freem(m);
3535533ec48SAlexander V. Chernikov return (ENOBUFS);
354ac9d7e26SMax Laier }
355ac9d7e26SMax Laier m_tag_prepend(m, mtag);
3567173b6e5SLuigi Rizzo }
3577173b6e5SLuigi Rizzo dt = (struct ipfw_rule_ref *)(mtag+1);
358ac9d7e26SMax Laier
3596daf7ebdSBrian Feldman /* Loopback avoidance and state recovery */
3606daf7ebdSBrian Feldman if (sin) {
3616daf7ebdSBrian Feldman int i;
3626daf7ebdSBrian Feldman
3637173b6e5SLuigi Rizzo /* set the starting point. We provide a non-zero slot,
3647173b6e5SLuigi Rizzo * but a non_matching chain_id to skip that info and use
3657173b6e5SLuigi Rizzo * the rulenum/rule_id.
3667173b6e5SLuigi Rizzo */
3677173b6e5SLuigi Rizzo dt->slot = 1; /* dummy, chain_id is invalid */
3687173b6e5SLuigi Rizzo dt->chain_id = 0;
3697173b6e5SLuigi Rizzo dt->rulenum = sin->sin_port+1; /* host format ? */
3707173b6e5SLuigi Rizzo dt->rule_id = 0;
3715533ec48SAlexander V. Chernikov /* XXX: broken for IPv6 */
372bab04eb8SJulian Elischer /*
3732b25acc1SLuigi Rizzo * Find receive interface with the given name, stuffed
3742b25acc1SLuigi Rizzo * (if it exists) in the sin_zero[] field.
3752b25acc1SLuigi Rizzo * The name is user supplied data so don't trust its size
3762b25acc1SLuigi Rizzo * or that it is zero terminated.
377bab04eb8SJulian Elischer */
3784ee6e70eSPoul-Henning Kamp for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++)
3792b25acc1SLuigi Rizzo ;
3802b25acc1SLuigi Rizzo if ( i > 0 && i < sizeof(sin->sin_zero))
381bab04eb8SJulian Elischer m->m_pkthdr.rcvif = ifunit(sin->sin_zero);
382c977d4c7SJulian Elischer }
38393e0e116SJulian Elischer
3845533ec48SAlexander V. Chernikov ip = mtod(m, struct ip *);
385812f1d32SGleb Smirnoff switch (ip->ip_v) {
386e72c5228SGleb Smirnoff #ifdef INET
387812f1d32SGleb Smirnoff case IPVERSION:
3885533ec48SAlexander V. Chernikov family = AF_INET;
3895533ec48SAlexander V. Chernikov break;
390e72c5228SGleb Smirnoff #endif
39175831a1cSAlexander V. Chernikov #ifdef INET6
3925533ec48SAlexander V. Chernikov case IPV6_VERSION >> 4:
3935533ec48SAlexander V. Chernikov family = AF_INET6;
3945533ec48SAlexander V. Chernikov break;
39575831a1cSAlexander V. Chernikov #endif
3965533ec48SAlexander V. Chernikov default:
3975533ec48SAlexander V. Chernikov m_freem(m);
3985533ec48SAlexander V. Chernikov return (EAFNOSUPPORT);
3995533ec48SAlexander V. Chernikov }
4005533ec48SAlexander V. Chernikov
401fabf705fSIgor Ostapenko mtag = m_tag_locate(m, MTAG_PF_DIVERT, 0, NULL);
402fabf705fSIgor Ostapenko if (mtag == NULL) {
403fabf705fSIgor Ostapenko /* this should be normal */
404fabf705fSIgor Ostapenko mtag = m_tag_alloc(MTAG_PF_DIVERT, 0,
405fabf705fSIgor Ostapenko sizeof(struct pf_divert_mtag), M_NOWAIT | M_ZERO);
406fabf705fSIgor Ostapenko if (mtag == NULL) {
407fabf705fSIgor Ostapenko m_freem(m);
408fabf705fSIgor Ostapenko return (ENOBUFS);
409fabf705fSIgor Ostapenko }
410fabf705fSIgor Ostapenko m_tag_prepend(m, mtag);
411fabf705fSIgor Ostapenko }
412fabf705fSIgor Ostapenko pfdt = (struct pf_divert_mtag *)(mtag+1);
413fabf705fSIgor Ostapenko if (sin)
414fabf705fSIgor Ostapenko pfdt->idir = sin->sin_port;
415fabf705fSIgor Ostapenko
4165533ec48SAlexander V. Chernikov /* Reinject packet into the system as incoming or outgoing */
4175533ec48SAlexander V. Chernikov NET_EPOCH_ENTER(et);
4185533ec48SAlexander V. Chernikov if (!sin || sin->sin_addr.s_addr == 0) {
4195533ec48SAlexander V. Chernikov dt->info |= IPFW_IS_DIVERT | IPFW_INFO_OUT;
420fabf705fSIgor Ostapenko pfdt->ndir = PF_DIVERT_MTAG_DIR_OUT;
4215533ec48SAlexander V. Chernikov error = div_output_outbound(family, so, m);
4225533ec48SAlexander V. Chernikov } else {
4235533ec48SAlexander V. Chernikov dt->info |= IPFW_IS_DIVERT | IPFW_INFO_IN;
424fabf705fSIgor Ostapenko pfdt->ndir = PF_DIVERT_MTAG_DIR_IN;
4255533ec48SAlexander V. Chernikov error = div_output_inbound(family, so, m, sin);
4265533ec48SAlexander V. Chernikov }
4275533ec48SAlexander V. Chernikov NET_EPOCH_EXIT(et);
4285533ec48SAlexander V. Chernikov
4295533ec48SAlexander V. Chernikov return (error);
4305533ec48SAlexander V. Chernikov }
4315533ec48SAlexander V. Chernikov
4325533ec48SAlexander V. Chernikov /*
4335533ec48SAlexander V. Chernikov * Sends mbuf @m to the wire via ip[6]_output().
4345533ec48SAlexander V. Chernikov *
435a1fadf7dSMark Johnston * Returns 0 on success or an errno value on failure. @m is always consumed.
4365533ec48SAlexander V. Chernikov */
4375533ec48SAlexander V. Chernikov static int
div_output_outbound(int family,struct socket * so,struct mbuf * m)4385533ec48SAlexander V. Chernikov div_output_outbound(int family, struct socket *so, struct mbuf *m)
4395533ec48SAlexander V. Chernikov {
4405533ec48SAlexander V. Chernikov int error;
4415533ec48SAlexander V. Chernikov
4425533ec48SAlexander V. Chernikov switch (family) {
443e72c5228SGleb Smirnoff #ifdef INET
4445533ec48SAlexander V. Chernikov case AF_INET:
445e72c5228SGleb Smirnoff {
446e72c5228SGleb Smirnoff struct ip *const ip = mtod(m, struct ip *);
447e72c5228SGleb Smirnoff
448f1fb0517SGleb Smirnoff /* Don't allow packet length sizes that will crash. */
449f1fb0517SGleb Smirnoff if (((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
450a1fadf7dSMark Johnston m_freem(m);
4515533ec48SAlexander V. Chernikov return (EINVAL);
452812f1d32SGleb Smirnoff }
453812f1d32SGleb Smirnoff break;
454e72c5228SGleb Smirnoff }
455e72c5228SGleb Smirnoff #endif
456812f1d32SGleb Smirnoff #ifdef INET6
4575533ec48SAlexander V. Chernikov case AF_INET6:
458812f1d32SGleb Smirnoff {
459812f1d32SGleb Smirnoff struct ip6_hdr *const ip6 = mtod(m, struct ip6_hdr *);
460812f1d32SGleb Smirnoff
461812f1d32SGleb Smirnoff /* Don't allow packet length sizes that will crash */
462812f1d32SGleb Smirnoff if (((u_short)ntohs(ip6->ip6_plen) > m->m_pkthdr.len)) {
463a1fadf7dSMark Johnston m_freem(m);
4645533ec48SAlexander V. Chernikov return (EINVAL);
465812f1d32SGleb Smirnoff }
466217e3abcSGleb Smirnoff break;
467812f1d32SGleb Smirnoff }
468812f1d32SGleb Smirnoff #endif
469812f1d32SGleb Smirnoff }
47093e0e116SJulian Elischer
471ffcbc0e4SRobert Watson #ifdef MAC
472f1fb0517SGleb Smirnoff mac_socket_create_mbuf(so, m);
473ffcbc0e4SRobert Watson #endif
474812f1d32SGleb Smirnoff
4755533ec48SAlexander V. Chernikov error = 0;
4765533ec48SAlexander V. Chernikov switch (family) {
477e72c5228SGleb Smirnoff #ifdef INET
4785533ec48SAlexander V. Chernikov case AF_INET:
479f1fb0517SGleb Smirnoff error = ip_output(m, NULL, NULL,
480812f1d32SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0)
481812f1d32SGleb Smirnoff | IP_ALLOWBROADCAST | IP_RAWOUTPUT, NULL, NULL);
482812f1d32SGleb Smirnoff break;
483e72c5228SGleb Smirnoff #endif
484812f1d32SGleb Smirnoff #ifdef INET6
4855533ec48SAlexander V. Chernikov case AF_INET6:
486812f1d32SGleb Smirnoff error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
487812f1d32SGleb Smirnoff break;
488812f1d32SGleb Smirnoff #endif
489812f1d32SGleb Smirnoff }
4902b1c7217SGleb Smirnoff if (error == 0)
4912b1c7217SGleb Smirnoff DIVSTAT_INC(outbound);
4925533ec48SAlexander V. Chernikov
4935533ec48SAlexander V. Chernikov return (error);
4945533ec48SAlexander V. Chernikov }
4955533ec48SAlexander V. Chernikov
4965533ec48SAlexander V. Chernikov /*
4975533ec48SAlexander V. Chernikov * Schedules mbuf @m for local processing via IPv4/IPv6 netisr queue.
4985533ec48SAlexander V. Chernikov *
499a1fadf7dSMark Johnston * Returns 0 on success or an errno value on failure. @m is always consumed.
5005533ec48SAlexander V. Chernikov */
5015533ec48SAlexander V. Chernikov static int
div_output_inbound(int family,struct socket * so,struct mbuf * m,struct sockaddr_in * sin)5025533ec48SAlexander V. Chernikov div_output_inbound(int family, struct socket *so, struct mbuf *m,
5035533ec48SAlexander V. Chernikov struct sockaddr_in *sin)
5045533ec48SAlexander V. Chernikov {
5055533ec48SAlexander V. Chernikov struct ifaddr *ifa;
5065533ec48SAlexander V. Chernikov
507bab04eb8SJulian Elischer if (m->m_pkthdr.rcvif == NULL) {
508b0935ca2SJulian Elischer /*
5092b25acc1SLuigi Rizzo * No luck with the name, check by IP address.
5102b25acc1SLuigi Rizzo * Clear the port and the ifname to make sure
5112b25acc1SLuigi Rizzo * there are no distractions for ifa_ifwithaddr.
512b0935ca2SJulian Elischer */
5132b25acc1SLuigi Rizzo
5145533ec48SAlexander V. Chernikov /* XXX: broken for IPv6 */
515b0935ca2SJulian Elischer bzero(sin->sin_zero, sizeof(sin->sin_zero));
516b0935ca2SJulian Elischer sin->sin_port = 0;
5172b25acc1SLuigi Rizzo ifa = ifa_ifwithaddr((struct sockaddr *) sin);
518a1fadf7dSMark Johnston if (ifa == NULL) {
519a1fadf7dSMark Johnston m_freem(m);
5205533ec48SAlexander V. Chernikov return (EADDRNOTAVAIL);
521a1fadf7dSMark Johnston }
52293e0e116SJulian Elischer m->m_pkthdr.rcvif = ifa->ifa_ifp;
523436c7212SJulian Elischer }
524ffcbc0e4SRobert Watson #ifdef MAC
52530d239bcSRobert Watson mac_socket_create_mbuf(so, m);
526ffcbc0e4SRobert Watson #endif
527b244c8adSChristian S.J. Peron /* Send packet to input processing via netisr */
5285533ec48SAlexander V. Chernikov switch (family) {
529e72c5228SGleb Smirnoff #ifdef INET
5305533ec48SAlexander V. Chernikov case AF_INET:
531e72c5228SGleb Smirnoff {
532e72c5228SGleb Smirnoff const struct ip *ip;
533e72c5228SGleb Smirnoff
5345533ec48SAlexander V. Chernikov ip = mtod(m, struct ip *);
53538cc96a8SAndrey V. Elsukov /*
53638cc96a8SAndrey V. Elsukov * Restore M_BCAST flag when destination address is
53738cc96a8SAndrey V. Elsukov * broadcast. It is expected by ip_tryforward().
53838cc96a8SAndrey V. Elsukov */
53938cc96a8SAndrey V. Elsukov if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
54038cc96a8SAndrey V. Elsukov m->m_flags |= M_MCAST;
541*197fc4caSGleb Smirnoff else if (in_ifnet_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
54238cc96a8SAndrey V. Elsukov m->m_flags |= M_BCAST;
543d4b5cae4SRobert Watson netisr_queue_src(NETISR_IP, (uintptr_t)so, m);
5442b1c7217SGleb Smirnoff DIVSTAT_INC(inbound);
545812f1d32SGleb Smirnoff break;
546e72c5228SGleb Smirnoff }
547e72c5228SGleb Smirnoff #endif
548812f1d32SGleb Smirnoff #ifdef INET6
5495533ec48SAlexander V. Chernikov case AF_INET6:
550812f1d32SGleb Smirnoff netisr_queue_src(NETISR_IPV6, (uintptr_t)so, m);
5512b1c7217SGleb Smirnoff DIVSTAT_INC(inbound);
552812f1d32SGleb Smirnoff break;
553812f1d32SGleb Smirnoff #endif
554812f1d32SGleb Smirnoff default:
555a1fadf7dSMark Johnston m_freem(m);
5565533ec48SAlexander V. Chernikov return (EINVAL);
55793e0e116SJulian Elischer }
55893e0e116SJulian Elischer
5595533ec48SAlexander V. Chernikov return (0);
56093e0e116SJulian Elischer }
56193e0e116SJulian Elischer
5629f907986SPeter Wemm static int
div_attach(struct socket * so,int proto,struct thread * td)563b40ce416SJulian Elischer div_attach(struct socket *so, int proto, struct thread *td)
56493e0e116SJulian Elischer {
565f1fb0517SGleb Smirnoff struct divcb *dcb;
56626f91065SSam Leffler int error;
56793e0e116SJulian Elischer
568acd3428bSRobert Watson if (td != NULL) {
569acd3428bSRobert Watson error = priv_check(td, PRIV_NETINET_DIVERT);
570acd3428bSRobert Watson if (error)
571acd3428bSRobert Watson return (error);
572acd3428bSRobert Watson }
5736a800098SYoshinobu Inoue error = soreserve(so, div_sendspace, div_recvspace);
57414ba8addSRobert Watson if (error)
5756a800098SYoshinobu Inoue return error;
576f1fb0517SGleb Smirnoff dcb = malloc(sizeof(*dcb), M_PCB, M_WAITOK);
577f1fb0517SGleb Smirnoff dcb->dcb_bound = DCB_UNBOUND;
578f1fb0517SGleb Smirnoff dcb->dcb_socket = so;
579f1fb0517SGleb Smirnoff DIVERT_LOCK();
580f1fb0517SGleb Smirnoff V_dcb_count++;
581f1fb0517SGleb Smirnoff dcb->dcb_gencnt = ++V_dcb_gencnt;
582f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
583f1fb0517SGleb Smirnoff so->so_pcb = dcb;
584f1fb0517SGleb Smirnoff
585f1fb0517SGleb Smirnoff return (0);
586f1fb0517SGleb Smirnoff }
587f1fb0517SGleb Smirnoff
588f1fb0517SGleb Smirnoff static void
div_free(epoch_context_t ctx)589f1fb0517SGleb Smirnoff div_free(epoch_context_t ctx)
590f1fb0517SGleb Smirnoff {
591f1fb0517SGleb Smirnoff struct divcb *dcb = __containerof(ctx, struct divcb, dcb_epochctx);
592f1fb0517SGleb Smirnoff
593f1fb0517SGleb Smirnoff free(dcb, M_PCB);
59493e0e116SJulian Elischer }
5959f907986SPeter Wemm
596bc725eafSRobert Watson static void
div_detach(struct socket * so)5979f907986SPeter Wemm div_detach(struct socket *so)
5989f907986SPeter Wemm {
599f1fb0517SGleb Smirnoff struct divcb *dcb = so->so_pcb;
6009f907986SPeter Wemm
601f1fb0517SGleb Smirnoff so->so_pcb = NULL;
602f1fb0517SGleb Smirnoff DIVERT_LOCK();
603f1fb0517SGleb Smirnoff if (dcb->dcb_bound != DCB_UNBOUND)
604f1fb0517SGleb Smirnoff SLIST_REMOVE(&V_divhash[DCBHASH(dcb)], dcb, divcb, dcb_next);
605f1fb0517SGleb Smirnoff V_dcb_count--;
606f1fb0517SGleb Smirnoff V_dcb_gencnt++;
607f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
608f1fb0517SGleb Smirnoff NET_EPOCH_CALL(div_free, &dcb->dcb_epochctx);
6099f907986SPeter Wemm }
61093e0e116SJulian Elischer
6119f907986SPeter Wemm static int
div_bind(struct socket * so,struct sockaddr * nam,struct thread * td)612b40ce416SJulian Elischer div_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
6139f907986SPeter Wemm {
614f1fb0517SGleb Smirnoff struct divcb *dcb;
615f1fb0517SGleb Smirnoff uint16_t port;
6169f907986SPeter Wemm
6172b25acc1SLuigi Rizzo if (nam->sa_family != AF_INET)
61814ba8addSRobert Watson return EAFNOSUPPORT;
619f161d294SMark Johnston if (nam->sa_len != sizeof(struct sockaddr_in))
620f161d294SMark Johnston return EINVAL;
621f1fb0517SGleb Smirnoff port = ((struct sockaddr_in *)nam)->sin_port;
622f1fb0517SGleb Smirnoff DIVERT_LOCK();
623f1fb0517SGleb Smirnoff SLIST_FOREACH(dcb, &V_divhash[DIVHASH(port)], dcb_next)
624f1fb0517SGleb Smirnoff if (dcb->dcb_port == port) {
625f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
626f1fb0517SGleb Smirnoff return (EADDRINUSE);
627f1fb0517SGleb Smirnoff }
628f1fb0517SGleb Smirnoff dcb = so->so_pcb;
629f1fb0517SGleb Smirnoff if (dcb->dcb_bound != DCB_UNBOUND)
630f1fb0517SGleb Smirnoff SLIST_REMOVE(&V_divhash[DCBHASH(dcb)], dcb, divcb, dcb_next);
631f1fb0517SGleb Smirnoff dcb->dcb_port = port;
632f1fb0517SGleb Smirnoff SLIST_INSERT_HEAD(&V_divhash[DIVHASH(port)], dcb, dcb_next);
633f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
634f1fb0517SGleb Smirnoff
635f1fb0517SGleb Smirnoff return (0);
6369f907986SPeter Wemm }
63793e0e116SJulian Elischer
6389f907986SPeter Wemm static int
div_pcblist(SYSCTL_HANDLER_ARGS)639cec335f9SRuslan Ermilov div_pcblist(SYSCTL_HANDLER_ARGS)
640cec335f9SRuslan Ermilov {
641cec335f9SRuslan Ermilov struct xinpgen xig;
642f1fb0517SGleb Smirnoff struct divcb *dcb;
643032677ceSGleb Smirnoff int error;
644cec335f9SRuslan Ermilov
645032677ceSGleb Smirnoff if (req->newptr != 0)
646032677ceSGleb Smirnoff return EPERM;
647032677ceSGleb Smirnoff
648cec335f9SRuslan Ermilov if (req->oldptr == 0) {
649f1fb0517SGleb Smirnoff u_int n;
650032677ceSGleb Smirnoff
651f1fb0517SGleb Smirnoff n = V_dcb_count;
652c007b96aSJohn Baldwin n += imax(n / 8, 10);
653c007b96aSJohn Baldwin req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
654cec335f9SRuslan Ermilov return 0;
655cec335f9SRuslan Ermilov }
656cec335f9SRuslan Ermilov
657032677ceSGleb Smirnoff if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
65847934cefSDon Lewis return (error);
659cec335f9SRuslan Ermilov
66079db6fe7SMark Johnston bzero(&xig, sizeof(xig));
661cec335f9SRuslan Ermilov xig.xig_len = sizeof xig;
662f1fb0517SGleb Smirnoff xig.xig_count = V_dcb_count;
663f1fb0517SGleb Smirnoff xig.xig_gen = V_dcb_gencnt;
664cec335f9SRuslan Ermilov xig.xig_sogen = so_gencnt;
665cec335f9SRuslan Ermilov error = SYSCTL_OUT(req, &xig, sizeof xig);
666cec335f9SRuslan Ermilov if (error)
667cec335f9SRuslan Ermilov return error;
668cec335f9SRuslan Ermilov
669f1fb0517SGleb Smirnoff DIVERT_LOCK();
670f1fb0517SGleb Smirnoff for (int i = 0; i < DIVHASHSIZE; i++)
671f1fb0517SGleb Smirnoff SLIST_FOREACH(dcb, &V_divhash[i], dcb_next) {
672f1fb0517SGleb Smirnoff if (dcb->dcb_gencnt <= xig.xig_gen) {
673cec335f9SRuslan Ermilov struct xinpcb xi;
674cc65eb4eSGleb Smirnoff
675f1fb0517SGleb Smirnoff bzero(&xi, sizeof(xi));
676f1fb0517SGleb Smirnoff xi.xi_len = sizeof(struct xinpcb);
677f1fb0517SGleb Smirnoff sotoxsocket(dcb->dcb_socket, &xi.xi_socket);
678f1fb0517SGleb Smirnoff xi.inp_gencnt = dcb->dcb_gencnt;
679f1fb0517SGleb Smirnoff xi.inp_vflag = INP_IPV4; /* XXX: netstat(1) */
680f1fb0517SGleb Smirnoff xi.inp_inc.inc_ie.ie_lport = dcb->dcb_port;
681266f97b5SCy Schubert error = SYSCTL_OUT(req, &xi, sizeof xi);
682f1fb0517SGleb Smirnoff if (error)
683f1fb0517SGleb Smirnoff goto errout;
684db0ac6deSCy Schubert }
685db0ac6deSCy Schubert }
686d0e157f6SBjoern A. Zeeb
687cec335f9SRuslan Ermilov /*
688cec335f9SRuslan Ermilov * Give the user an updated idea of our state.
689cec335f9SRuslan Ermilov * If the generation differs from what we told
690cec335f9SRuslan Ermilov * her before, she knows that something happened
691cec335f9SRuslan Ermilov * while we were processing this request, and it
692cec335f9SRuslan Ermilov * might be necessary to retry.
693cec335f9SRuslan Ermilov */
694f1fb0517SGleb Smirnoff xig.xig_gen = V_dcb_gencnt;
695cec335f9SRuslan Ermilov xig.xig_sogen = so_gencnt;
696f1fb0517SGleb Smirnoff xig.xig_count = V_dcb_count;
697cec335f9SRuslan Ermilov error = SYSCTL_OUT(req, &xig, sizeof xig);
698f1fb0517SGleb Smirnoff
699f1fb0517SGleb Smirnoff errout:
700f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
701032677ceSGleb Smirnoff
702032677ceSGleb Smirnoff return (error);
703cec335f9SRuslan Ermilov }
7047029da5cSPawel Biernacki SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist,
7052b1c7217SGleb Smirnoff CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, div_pcblist,
7062b1c7217SGleb Smirnoff "S,xinpcb", "List of active divert sockets");
707cec335f9SRuslan Ermilov
708e7d02be1SGleb Smirnoff static struct protosw div_protosw = {
709303989a2SRuslan Ermilov .pr_type = SOCK_RAW,
710303989a2SRuslan Ermilov .pr_flags = PR_ATOMIC|PR_ADDR,
711e7d02be1SGleb Smirnoff .pr_attach = div_attach,
712e7d02be1SGleb Smirnoff .pr_bind = div_bind,
713e7d02be1SGleb Smirnoff .pr_detach = div_detach,
714e7d02be1SGleb Smirnoff .pr_send = div_send,
71572584fd2SAndre Oppermann };
71672584fd2SAndre Oppermann
7178624f434SGleb Smirnoff static struct domain divertdomain = {
7188624f434SGleb Smirnoff .dom_family = PF_DIVERT,
7198624f434SGleb Smirnoff .dom_name = "divert",
7208624f434SGleb Smirnoff .dom_nprotosw = 1,
7218624f434SGleb Smirnoff .dom_protosw = { &div_protosw },
7228624f434SGleb Smirnoff };
7238624f434SGleb Smirnoff
72472584fd2SAndre Oppermann static int
div_modevent(module_t mod,int type,void * unused)72572584fd2SAndre Oppermann div_modevent(module_t mod, int type, void *unused)
72672584fd2SAndre Oppermann {
72772584fd2SAndre Oppermann int err = 0;
72872584fd2SAndre Oppermann
72972584fd2SAndre Oppermann switch (type) {
73072584fd2SAndre Oppermann case MOD_LOAD:
7318624f434SGleb Smirnoff domain_add(&divertdomain);
73272584fd2SAndre Oppermann ip_divert_ptr = divert_packet;
73372584fd2SAndre Oppermann break;
73424fc79b0SAndre Oppermann case MOD_QUIESCE:
73524fc79b0SAndre Oppermann /*
73624fc79b0SAndre Oppermann * IPDIVERT may normally not be unloaded because of the
73724fc79b0SAndre Oppermann * potential race conditions. Tell kldunload we can't be
73824fc79b0SAndre Oppermann * unloaded unless the unload is forced.
73924fc79b0SAndre Oppermann */
74024fc79b0SAndre Oppermann err = EPERM;
74124fc79b0SAndre Oppermann break;
74272584fd2SAndre Oppermann case MOD_UNLOAD:
74372584fd2SAndre Oppermann /*
74424fc79b0SAndre Oppermann * Forced unload.
74524fc79b0SAndre Oppermann *
74672584fd2SAndre Oppermann * Module ipdivert can only be unloaded if no sockets are
74772584fd2SAndre Oppermann * connected. Maybe this can be changed later to forcefully
74872584fd2SAndre Oppermann * disconnect any open sockets.
749279128e2SRobert Watson *
7502de1a9ebSAndre Oppermann * XXXRW: Note that there is a slight race here, as a new
7512de1a9ebSAndre Oppermann * socket open request could be spinning on the lock and then
7522de1a9ebSAndre Oppermann * we destroy the lock.
7538624f434SGleb Smirnoff *
7548624f434SGleb Smirnoff * XXXGL: One more reason this code is incorrect is that it
7558624f434SGleb Smirnoff * checks only the current vnet.
75672584fd2SAndre Oppermann */
757f1fb0517SGleb Smirnoff DIVERT_LOCK();
758f1fb0517SGleb Smirnoff if (V_dcb_count != 0) {
759f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
76072584fd2SAndre Oppermann err = EBUSY;
76172584fd2SAndre Oppermann break;
76272584fd2SAndre Oppermann }
763f1fb0517SGleb Smirnoff DIVERT_UNLOCK();
76472584fd2SAndre Oppermann ip_divert_ptr = NULL;
7658624f434SGleb Smirnoff domain_remove(&divertdomain);
76672584fd2SAndre Oppermann break;
76772584fd2SAndre Oppermann default:
76824fc79b0SAndre Oppermann err = EOPNOTSUPP;
76972584fd2SAndre Oppermann break;
77072584fd2SAndre Oppermann }
77172584fd2SAndre Oppermann return err;
77272584fd2SAndre Oppermann }
77372584fd2SAndre Oppermann
77472584fd2SAndre Oppermann static moduledata_t ipdivertmod = {
77572584fd2SAndre Oppermann "ipdivert",
77672584fd2SAndre Oppermann div_modevent,
7779823d527SKevin Lo 0
77872584fd2SAndre Oppermann };
77972584fd2SAndre Oppermann
7803f58662dSBjoern A. Zeeb DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
78172584fd2SAndre Oppermann MODULE_VERSION(ipdivert, 1);
782