11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause
3*d4a07e70Sfengbojiang *
41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1989, 1993
51eaf0ac3Slogwang * The Regents of the University of California.
61eaf0ac3Slogwang * All rights reserved.
71eaf0ac3Slogwang *
81eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
91eaf0ac3Slogwang * modification, are permitted provided that the following conditions
101eaf0ac3Slogwang * are met:
111eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
121eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
131eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
141eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
151eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
16*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors
171eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
181eaf0ac3Slogwang * without specific prior written permission.
191eaf0ac3Slogwang *
201eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301eaf0ac3Slogwang * SUCH DAMAGE.
311eaf0ac3Slogwang *
321eaf0ac3Slogwang * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
331eaf0ac3Slogwang * $FreeBSD$
341eaf0ac3Slogwang */
351eaf0ac3Slogwang
361eaf0ac3Slogwang #ifndef _NETINET_UDP_VAR_H_
371eaf0ac3Slogwang #define _NETINET_UDP_VAR_H_
381eaf0ac3Slogwang
391eaf0ac3Slogwang /*
401eaf0ac3Slogwang * UDP kernel structures and variables.
411eaf0ac3Slogwang */
421eaf0ac3Slogwang struct udpiphdr {
431eaf0ac3Slogwang struct ipovly ui_i; /* overlaid ip structure */
441eaf0ac3Slogwang struct udphdr ui_u; /* udp header */
451eaf0ac3Slogwang };
461eaf0ac3Slogwang #define ui_x1 ui_i.ih_x1
471eaf0ac3Slogwang #define ui_v ui_i.ih_x1[0]
481eaf0ac3Slogwang #define ui_pr ui_i.ih_pr
491eaf0ac3Slogwang #define ui_len ui_i.ih_len
501eaf0ac3Slogwang #define ui_src ui_i.ih_src
511eaf0ac3Slogwang #define ui_dst ui_i.ih_dst
521eaf0ac3Slogwang #define ui_sport ui_u.uh_sport
531eaf0ac3Slogwang #define ui_dport ui_u.uh_dport
541eaf0ac3Slogwang #define ui_ulen ui_u.uh_ulen
551eaf0ac3Slogwang #define ui_sum ui_u.uh_sum
561eaf0ac3Slogwang
571eaf0ac3Slogwang struct inpcb;
581eaf0ac3Slogwang struct mbuf;
591eaf0ac3Slogwang
601eaf0ac3Slogwang typedef void(*udp_tun_func_t)(struct mbuf *, int, struct inpcb *,
611eaf0ac3Slogwang const struct sockaddr *, void *);
621eaf0ac3Slogwang typedef void(*udp_tun_icmp_t)(int, struct sockaddr *, void *, void *);
631eaf0ac3Slogwang
641eaf0ac3Slogwang /*
651eaf0ac3Slogwang * UDP control block; one per udp.
661eaf0ac3Slogwang */
671eaf0ac3Slogwang struct udpcb {
681eaf0ac3Slogwang udp_tun_func_t u_tun_func; /* UDP kernel tunneling callback. */
691eaf0ac3Slogwang udp_tun_icmp_t u_icmp_func; /* UDP kernel tunneling icmp callback */
701eaf0ac3Slogwang u_int u_flags; /* Generic UDP flags. */
711eaf0ac3Slogwang uint16_t u_rxcslen; /* Coverage for incoming datagrams. */
721eaf0ac3Slogwang uint16_t u_txcslen; /* Coverage for outgoing datagrams. */
731eaf0ac3Slogwang void *u_tun_ctx; /* Tunneling callback context. */
741eaf0ac3Slogwang };
751eaf0ac3Slogwang
761eaf0ac3Slogwang #define intoudpcb(ip) ((struct udpcb *)(ip)->inp_ppcb)
771eaf0ac3Slogwang #define sotoudpcb(so) (intoudpcb(sotoinpcb(so)))
781eaf0ac3Slogwang
791eaf0ac3Slogwang /* IPsec: ESP in UDP tunneling: */
801eaf0ac3Slogwang #define UF_ESPINUDP_NON_IKE 0x00000001 /* w/ non-IKE marker .. */
811eaf0ac3Slogwang /* .. per draft-ietf-ipsec-nat-t-ike-0[01],
821eaf0ac3Slogwang * and draft-ietf-ipsec-udp-encaps-(00/)01.txt */
831eaf0ac3Slogwang #define UF_ESPINUDP 0x00000002 /* w/ non-ESP marker. */
841eaf0ac3Slogwang
851eaf0ac3Slogwang struct udpstat {
861eaf0ac3Slogwang /* input statistics: */
871eaf0ac3Slogwang uint64_t udps_ipackets; /* total input packets */
881eaf0ac3Slogwang uint64_t udps_hdrops; /* packet shorter than header */
891eaf0ac3Slogwang uint64_t udps_badsum; /* checksum error */
901eaf0ac3Slogwang uint64_t udps_nosum; /* no checksum */
911eaf0ac3Slogwang uint64_t udps_badlen; /* data length larger than packet */
921eaf0ac3Slogwang uint64_t udps_noport; /* no socket on port */
931eaf0ac3Slogwang uint64_t udps_noportbcast; /* of above, arrived as broadcast */
941eaf0ac3Slogwang uint64_t udps_fullsock; /* not delivered, input socket full */
951eaf0ac3Slogwang uint64_t udpps_pcbcachemiss; /* input packets missing pcb cache */
961eaf0ac3Slogwang uint64_t udpps_pcbhashmiss; /* input packets not for hashed pcb */
971eaf0ac3Slogwang /* output statistics: */
981eaf0ac3Slogwang uint64_t udps_opackets; /* total output packets */
991eaf0ac3Slogwang uint64_t udps_fastout; /* output packets on fast path */
1001eaf0ac3Slogwang /* of no socket on port, arrived as multicast */
1011eaf0ac3Slogwang uint64_t udps_noportmcast;
1021eaf0ac3Slogwang uint64_t udps_filtermcast; /* blocked by multicast filter */
1031eaf0ac3Slogwang };
1041eaf0ac3Slogwang
1051eaf0ac3Slogwang #ifdef _KERNEL
1061eaf0ac3Slogwang #include <sys/counter.h>
1071eaf0ac3Slogwang
1081eaf0ac3Slogwang VNET_PCPUSTAT_DECLARE(struct udpstat, udpstat);
1091eaf0ac3Slogwang /*
1101eaf0ac3Slogwang * In-kernel consumers can use these accessor macros directly to update
1111eaf0ac3Slogwang * stats.
1121eaf0ac3Slogwang */
1131eaf0ac3Slogwang #define UDPSTAT_ADD(name, val) \
1141eaf0ac3Slogwang VNET_PCPUSTAT_ADD(struct udpstat, udpstat, name, (val))
1151eaf0ac3Slogwang #define UDPSTAT_INC(name) UDPSTAT_ADD(name, 1)
1161eaf0ac3Slogwang
1171eaf0ac3Slogwang /*
1181eaf0ac3Slogwang * Kernel module consumers must use this accessor macro.
1191eaf0ac3Slogwang */
1201eaf0ac3Slogwang void kmod_udpstat_inc(int statnum);
1211eaf0ac3Slogwang #define KMOD_UDPSTAT_INC(name) \
1221eaf0ac3Slogwang kmod_udpstat_inc(offsetof(struct udpstat, name) / sizeof(uint64_t))
1231eaf0ac3Slogwang #endif
1241eaf0ac3Slogwang
1251eaf0ac3Slogwang /*
1261eaf0ac3Slogwang * Identifiers for UDP sysctl nodes.
1271eaf0ac3Slogwang */
1281eaf0ac3Slogwang #define UDPCTL_CHECKSUM 1 /* checksum UDP packets */
1291eaf0ac3Slogwang #define UDPCTL_STATS 2 /* statistics (read-only) */
1301eaf0ac3Slogwang #define UDPCTL_MAXDGRAM 3 /* max datagram size */
1311eaf0ac3Slogwang #define UDPCTL_RECVSPACE 4 /* default receive buffer space */
1321eaf0ac3Slogwang #define UDPCTL_PCBLIST 5 /* list of PCBs for UDP sockets */
1331eaf0ac3Slogwang
1341eaf0ac3Slogwang #ifdef _KERNEL
1351eaf0ac3Slogwang #include <netinet/in_pcb.h>
1361eaf0ac3Slogwang SYSCTL_DECL(_net_inet_udp);
1371eaf0ac3Slogwang
1381eaf0ac3Slogwang extern struct pr_usrreqs udp_usrreqs;
1391eaf0ac3Slogwang VNET_DECLARE(struct inpcbhead, udb);
1401eaf0ac3Slogwang VNET_DECLARE(struct inpcbinfo, udbinfo);
1411eaf0ac3Slogwang VNET_DECLARE(struct inpcbhead, ulitecb);
1421eaf0ac3Slogwang VNET_DECLARE(struct inpcbinfo, ulitecbinfo);
1431eaf0ac3Slogwang #define V_udb VNET(udb)
1441eaf0ac3Slogwang #define V_udbinfo VNET(udbinfo)
1451eaf0ac3Slogwang #define V_ulitecb VNET(ulitecb)
1461eaf0ac3Slogwang #define V_ulitecbinfo VNET(ulitecbinfo)
1471eaf0ac3Slogwang
1481eaf0ac3Slogwang extern u_long udp_sendspace;
1491eaf0ac3Slogwang extern u_long udp_recvspace;
1501eaf0ac3Slogwang VNET_DECLARE(int, udp_cksum);
1511eaf0ac3Slogwang VNET_DECLARE(int, udp_blackhole);
152*d4a07e70Sfengbojiang VNET_DECLARE(int, udp_log_in_vain);
1531eaf0ac3Slogwang #define V_udp_cksum VNET(udp_cksum)
1541eaf0ac3Slogwang #define V_udp_blackhole VNET(udp_blackhole)
155*d4a07e70Sfengbojiang #define V_udp_log_in_vain VNET(udp_log_in_vain)
156*d4a07e70Sfengbojiang
157*d4a07e70Sfengbojiang VNET_DECLARE(int, zero_checksum_port);
158*d4a07e70Sfengbojiang #define V_zero_checksum_port VNET(zero_checksum_port)
1591eaf0ac3Slogwang
1601eaf0ac3Slogwang static __inline struct inpcbinfo *
udp_get_inpcbinfo(int protocol)1611eaf0ac3Slogwang udp_get_inpcbinfo(int protocol)
1621eaf0ac3Slogwang {
1631eaf0ac3Slogwang return (protocol == IPPROTO_UDP) ? &V_udbinfo : &V_ulitecbinfo;
1641eaf0ac3Slogwang }
1651eaf0ac3Slogwang
1661eaf0ac3Slogwang static __inline struct inpcbhead *
udp_get_pcblist(int protocol)1671eaf0ac3Slogwang udp_get_pcblist(int protocol)
1681eaf0ac3Slogwang {
1691eaf0ac3Slogwang return (protocol == IPPROTO_UDP) ? &V_udb : &V_ulitecb;
1701eaf0ac3Slogwang }
1711eaf0ac3Slogwang
1721eaf0ac3Slogwang int udp_newudpcb(struct inpcb *);
1731eaf0ac3Slogwang void udp_discardcb(struct udpcb *);
1741eaf0ac3Slogwang
1751eaf0ac3Slogwang void udp_ctlinput(int, struct sockaddr *, void *);
1761eaf0ac3Slogwang void udplite_ctlinput(int, struct sockaddr *, void *);
1771eaf0ac3Slogwang int udp_ctloutput(struct socket *, struct sockopt *);
1781eaf0ac3Slogwang void udp_init(void);
1791eaf0ac3Slogwang void udplite_init(void);
1801eaf0ac3Slogwang int udp_input(struct mbuf **, int *, int);
1811eaf0ac3Slogwang void udplite_input(struct mbuf *, int);
1821eaf0ac3Slogwang struct inpcb *udp_notify(struct inpcb *inp, int errno);
1831eaf0ac3Slogwang int udp_shutdown(struct socket *so);
1841eaf0ac3Slogwang
1851eaf0ac3Slogwang int udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f,
1861eaf0ac3Slogwang udp_tun_icmp_t i, void *ctx);
1871eaf0ac3Slogwang
1881eaf0ac3Slogwang #endif /* _KERNEL */
1891eaf0ac3Slogwang
1901eaf0ac3Slogwang #endif /* _NETINET_UDP_VAR_H_ */
191