xref: /freebsd/sys/netinet/udp_var.h (revision 08e638c089ab57531f08994d03c9dde54c4744f9)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
53329b236SRobert Watson  *	The Regents of the University of California.
63329b236SRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33707f139eSPaul Richards #ifndef _NETINET_UDP_VAR_H_
34707f139eSPaul Richards #define	_NETINET_UDP_VAR_H_
35707f139eSPaul Richards 
36742e7210SKristof Provost #include <sys/types.h>
37aa70361dSKristof Provost #include <netinet/ip_var.h>
38aa70361dSKristof Provost #include <netinet/udp.h>
39aa70361dSKristof Provost 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * UDP kernel structures and variables.
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes struct udpiphdr {
44df8bae1dSRodney W. Grimes 	struct ipovly	ui_i;		/* overlaid ip structure */
45df8bae1dSRodney W. Grimes 	struct udphdr	ui_u;		/* udp header */
46df8bae1dSRodney W. Grimes };
47df8bae1dSRodney W. Grimes #define	ui_x1		ui_i.ih_x1
4857f60867SMark Johnston #define	ui_v		ui_i.ih_x1[0]
49df8bae1dSRodney W. Grimes #define	ui_pr		ui_i.ih_pr
50df8bae1dSRodney W. Grimes #define	ui_len		ui_i.ih_len
51df8bae1dSRodney W. Grimes #define	ui_src		ui_i.ih_src
52df8bae1dSRodney W. Grimes #define	ui_dst		ui_i.ih_dst
53df8bae1dSRodney W. Grimes #define	ui_sport	ui_u.uh_sport
54df8bae1dSRodney W. Grimes #define	ui_dport	ui_u.uh_dport
55df8bae1dSRodney W. Grimes #define	ui_ulen		ui_u.uh_ulen
56df8bae1dSRodney W. Grimes #define	ui_sum		ui_u.uh_sum
57df8bae1dSRodney W. Grimes 
586a9148feSBjoern A. Zeeb /*
590c0d8a4fSGleb Smirnoff  * Identifiers for UDP sysctl nodes.
606a9148feSBjoern A. Zeeb  */
610c0d8a4fSGleb Smirnoff #define	UDPCTL_CHECKSUM		1	/* checksum UDP packets */
620c0d8a4fSGleb Smirnoff #define	UDPCTL_STATS		2	/* statistics (read-only) */
630c0d8a4fSGleb Smirnoff #define	UDPCTL_MAXDGRAM		3	/* max datagram size */
640c0d8a4fSGleb Smirnoff #define	UDPCTL_RECVSPACE	4	/* default receive buffer space */
650c0d8a4fSGleb Smirnoff #define	UDPCTL_PCBLIST		5	/* list of PCBs for UDP sockets */
666a9148feSBjoern A. Zeeb 
677b495c44SVANHULLEBUS Yvan 				/* IPsec: ESP in UDP tunneling: */
687b495c44SVANHULLEBUS Yvan #define	UF_ESPINUDP_NON_IKE	0x00000001	/* w/ non-IKE marker .. */
697b495c44SVANHULLEBUS Yvan 	/* .. per draft-ietf-ipsec-nat-t-ike-0[01],
707b495c44SVANHULLEBUS Yvan 	 * and draft-ietf-ipsec-udp-encaps-(00/)01.txt */
717b495c44SVANHULLEBUS Yvan #define	UF_ESPINUDP		0x00000002	/* w/ non-ESP marker. */
727b495c44SVANHULLEBUS Yvan 
73df8bae1dSRodney W. Grimes struct udpstat {
74df8bae1dSRodney W. Grimes 				/* input statistics: */
75c80211e3SAndrey V. Elsukov 	uint64_t udps_ipackets;		/* total input packets */
76c80211e3SAndrey V. Elsukov 	uint64_t udps_hdrops;		/* packet shorter than header */
77c80211e3SAndrey V. Elsukov 	uint64_t udps_badsum;		/* checksum error */
78c80211e3SAndrey V. Elsukov 	uint64_t udps_nosum;		/* no checksum */
79c80211e3SAndrey V. Elsukov 	uint64_t udps_badlen;		/* data length larger than packet */
80c80211e3SAndrey V. Elsukov 	uint64_t udps_noport;		/* no socket on port */
81c80211e3SAndrey V. Elsukov 	uint64_t udps_noportbcast;	/* of above, arrived as broadcast */
82c80211e3SAndrey V. Elsukov 	uint64_t udps_fullsock;		/* not delivered, input socket full */
83c80211e3SAndrey V. Elsukov 	uint64_t udpps_pcbcachemiss;	/* input packets missing pcb cache */
84c80211e3SAndrey V. Elsukov 	uint64_t udpps_pcbhashmiss;	/* input packets not for hashed pcb */
85df8bae1dSRodney W. Grimes 				/* output statistics: */
86c80211e3SAndrey V. Elsukov 	uint64_t udps_opackets;		/* total output packets */
87c80211e3SAndrey V. Elsukov 	uint64_t udps_fastout;		/* output packets on fast path */
8876429de4SYoshinobu Inoue 	/* of no socket on port, arrived as multicast */
89c80211e3SAndrey V. Elsukov 	uint64_t udps_noportmcast;
90c80211e3SAndrey V. Elsukov 	uint64_t udps_filtermcast;	/* blocked by multicast filter */
91df8bae1dSRodney W. Grimes };
92df8bae1dSRodney W. Grimes 
93026decb8SRobert Watson #ifdef _KERNEL
940c0d8a4fSGleb Smirnoff #include <netinet/in_pcb.h>
955b7cb97cSAndrey V. Elsukov #include <sys/counter.h>
9660d8dbbeSKristof Provost #include <netinet/in_kdtrace.h>
970c0d8a4fSGleb Smirnoff struct mbuf;
980c0d8a4fSGleb Smirnoff 
990c0d8a4fSGleb Smirnoff typedef bool	udp_tun_func_t(struct mbuf *, int, struct inpcb *,
1000c0d8a4fSGleb Smirnoff 		    const struct sockaddr *, void *);
1010c0d8a4fSGleb Smirnoff typedef union {
1020c0d8a4fSGleb Smirnoff 	struct icmp *icmp;
1030c0d8a4fSGleb Smirnoff 	struct ip6ctlparam *ip6cp;
1040c0d8a4fSGleb Smirnoff } udp_tun_icmp_param_t __attribute__((__transparent_union__));
1050c0d8a4fSGleb Smirnoff typedef void	udp_tun_icmp_t(udp_tun_icmp_param_t);
1060c0d8a4fSGleb Smirnoff 
1070c0d8a4fSGleb Smirnoff /*
1080c0d8a4fSGleb Smirnoff  * UDP control block; one per udp.
1090c0d8a4fSGleb Smirnoff  */
1100c0d8a4fSGleb Smirnoff struct udpcb {
111483fe965SGleb Smirnoff 	struct inpcb	u_inpcb;
112483fe965SGleb Smirnoff #define	u_start_zero	u_tun_func
113483fe965SGleb Smirnoff #define	u_zero_size	(sizeof(struct udpcb) - \
114483fe965SGleb Smirnoff 			    offsetof(struct udpcb, u_start_zero))
1150c0d8a4fSGleb Smirnoff 	udp_tun_func_t	*u_tun_func;	/* UDP kernel tunneling callback. */
1160c0d8a4fSGleb Smirnoff 	udp_tun_icmp_t  *u_icmp_func;	/* UDP kernel tunneling icmp callback */
1170c0d8a4fSGleb Smirnoff 	u_int		u_flags;	/* Generic UDP flags. */
1180c0d8a4fSGleb Smirnoff 	uint16_t	u_rxcslen;	/* Coverage for incoming datagrams. */
1190c0d8a4fSGleb Smirnoff 	uint16_t	u_txcslen;	/* Coverage for outgoing datagrams. */
1200c0d8a4fSGleb Smirnoff 	void 		*u_tun_ctx;	/* Tunneling callback context. */
1210c0d8a4fSGleb Smirnoff };
1220c0d8a4fSGleb Smirnoff 
123de4bfd6bSReid Linnemann #define	intoudpcb(ip)	__containerof((ip), struct udpcb, u_inpcb)
1240c0d8a4fSGleb Smirnoff #define	sotoudpcb(so)	(intoudpcb(sotoinpcb(so)))
1255b7cb97cSAndrey V. Elsukov 
1265b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DECLARE(struct udpstat, udpstat);
127315e3e38SRobert Watson /*
128315e3e38SRobert Watson  * In-kernel consumers can use these accessor macros directly to update
129315e3e38SRobert Watson  * stats.
130315e3e38SRobert Watson  */
1315b7cb97cSAndrey V. Elsukov #define UDPSTAT_ADD(name, val)                                           \
13260d8dbbeSKristof Provost 	do {                                                             \
13360d8dbbeSKristof Provost 		MIB_SDT_PROBE1(udp, count, name, (val));                 \
13460d8dbbeSKristof Provost 		VNET_PCPUSTAT_ADD(struct udpstat, udpstat, name, (val)); \
13560d8dbbeSKristof Provost 	} while (0)
136026decb8SRobert Watson #define UDPSTAT_INC(name) UDPSTAT_ADD(name, 1)
137315e3e38SRobert Watson 
138315e3e38SRobert Watson /*
139315e3e38SRobert Watson  * Kernel module consumers must use this accessor macro.
140315e3e38SRobert Watson  */
141315e3e38SRobert Watson void	kmod_udpstat_inc(int statnum);
142315e3e38SRobert Watson #define KMOD_UDPSTAT_INC(name)                                          \
14360d8dbbeSKristof Provost 	do {                                                            \
14460d8dbbeSKristof Provost 		MIB_SDT_PROBE1(udp, count, name, 1);                    \
14560d8dbbeSKristof Provost 		kmod_udpstat_inc(                                       \
14660d8dbbeSKristof Provost 		    offsetof(struct udpstat, name) / sizeof(uint64_t)); \
14760d8dbbeSKristof Provost 	} while (0)
148026decb8SRobert Watson 
149ce02431fSDoug Rabson SYSCTL_DECL(_net_inet_udp);
150ce02431fSDoug Rabson 
151eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, udbinfo);
152e06e816fSKevin Lo VNET_DECLARE(struct inpcbinfo, ulitecbinfo);
1531e77c105SRobert Watson #define	V_udbinfo		VNET(udbinfo)
154e06e816fSKevin Lo #define	V_ulitecbinfo		VNET(ulitecbinfo)
155eddfbb76SRobert Watson 
15686413abfSBjoern A. Zeeb extern u_long			udp_sendspace;
15786413abfSBjoern A. Zeeb extern u_long			udp_recvspace;
158*08e638c0SMark Johnston VNET_DECLARE(int, udp_bind_all_fibs);
15982cea7e6SBjoern A. Zeeb VNET_DECLARE(int, udp_blackhole);
1603ea9a7cfSGleb Smirnoff VNET_DECLARE(bool, udp_blackhole_local);
161*08e638c0SMark Johnston VNET_DECLARE(int, udp_cksum);
162334fc582SBjoern A. Zeeb VNET_DECLARE(int, udp_log_in_vain);
163*08e638c0SMark Johnston #define	V_udp_bind_all_fibs	VNET(udp_bind_all_fibs)
16482cea7e6SBjoern A. Zeeb #define	V_udp_blackhole		VNET(udp_blackhole)
1653ea9a7cfSGleb Smirnoff #define	V_udp_blackhole_local	VNET(udp_blackhole_local)
166*08e638c0SMark Johnston #define	V_udp_cksum		VNET(udp_cksum)
167334fc582SBjoern A. Zeeb #define	V_udp_log_in_vain	VNET(udp_log_in_vain)
168df8bae1dSRodney W. Grimes 
16972cc43dfSNavdeep Parhar VNET_DECLARE(int, zero_checksum_port);
17072cc43dfSNavdeep Parhar #define	V_zero_checksum_port	VNET(zero_checksum_port)
17172cc43dfSNavdeep Parhar 
172e06e816fSKevin Lo static __inline struct inpcbinfo *
udp_get_inpcbinfo(int protocol)173a86e5c96SBjoern A. Zeeb udp_get_inpcbinfo(int protocol)
174e06e816fSKevin Lo {
175e06e816fSKevin Lo 	return (protocol == IPPROTO_UDP) ? &V_udbinfo : &V_ulitecbinfo;
176e06e816fSKevin Lo }
177e06e816fSKevin Lo 
1787b495c44SVANHULLEBUS Yvan int		udp_ctloutput(struct socket *, struct sockopt *);
179e06e816fSKevin Lo void		udplite_input(struct mbuf *, int);
1803329b236SRobert Watson struct inpcb	*udp_notify(struct inpcb *inp, int errno);
1815bba2728SGleb Smirnoff int		udp_shutdown(struct socket *, enum shutdown_how);
182c7c7ea4bSRandall Stewart 
18381d3ec17SBryan Venteicher int		udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f,
184abb901c5SRandall Stewart 		    udp_tun_icmp_t i, void *ctx);
185707f139eSPaul Richards 
1861aed3b34SGleb Smirnoff #ifdef _SYS_PROTOSW_H_
1871aed3b34SGleb Smirnoff pr_abort_t	udp_abort;
1881aed3b34SGleb Smirnoff pr_disconnect_t	udp_disconnect;
1891aed3b34SGleb Smirnoff pr_send_t	udp_send;
1901aed3b34SGleb Smirnoff #endif
1911aed3b34SGleb Smirnoff 
192e06e816fSKevin Lo #endif /* _KERNEL */
193e06e816fSKevin Lo 
194e06e816fSKevin Lo #endif /* _NETINET_UDP_VAR_H_ */
195