11eaf0ac3Slogwang /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause 3*d4a07e70Sfengbojiang * 41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1993 51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved. 61eaf0ac3Slogwang * 71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 81eaf0ac3Slogwang * modification, are permitted provided that the following conditions 91eaf0ac3Slogwang * are met: 101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 15*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors 161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software 171eaf0ac3Slogwang * without specific prior written permission. 181eaf0ac3Slogwang * 191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291eaf0ac3Slogwang * SUCH DAMAGE. 301eaf0ac3Slogwang * 311eaf0ac3Slogwang * @(#)ip_var.h 8.2 (Berkeley) 1/9/95 321eaf0ac3Slogwang * $FreeBSD$ 331eaf0ac3Slogwang */ 341eaf0ac3Slogwang 351eaf0ac3Slogwang #ifndef _NETINET_IP_VAR_H_ 361eaf0ac3Slogwang #define _NETINET_IP_VAR_H_ 371eaf0ac3Slogwang 381eaf0ac3Slogwang #include <sys/queue.h> 39*d4a07e70Sfengbojiang #include <sys/epoch.h> 401eaf0ac3Slogwang 411eaf0ac3Slogwang /* 421eaf0ac3Slogwang * Overlay for ip header used by other protocols (tcp, udp). 431eaf0ac3Slogwang */ 441eaf0ac3Slogwang struct ipovly { 451eaf0ac3Slogwang u_char ih_x1[9]; /* (unused) */ 461eaf0ac3Slogwang u_char ih_pr; /* protocol */ 471eaf0ac3Slogwang u_short ih_len; /* protocol length */ 481eaf0ac3Slogwang struct in_addr ih_src; /* source internet address */ 491eaf0ac3Slogwang struct in_addr ih_dst; /* destination internet address */ 501eaf0ac3Slogwang }; 511eaf0ac3Slogwang 521eaf0ac3Slogwang #ifdef _KERNEL 531eaf0ac3Slogwang /* 541eaf0ac3Slogwang * Ip reassembly queue structure. Each fragment 551eaf0ac3Slogwang * being reassembled is attached to one of these structures. 561eaf0ac3Slogwang * They are timed out after ipq_ttl drops to 0, and may also 571eaf0ac3Slogwang * be reclaimed if memory becomes tight. 581eaf0ac3Slogwang */ 591eaf0ac3Slogwang struct ipq { 601eaf0ac3Slogwang TAILQ_ENTRY(ipq) ipq_list; /* to other reass headers */ 611eaf0ac3Slogwang u_char ipq_ttl; /* time for reass q to live */ 621eaf0ac3Slogwang u_char ipq_p; /* protocol of this fragment */ 631eaf0ac3Slogwang u_short ipq_id; /* sequence id for reassembly */ 64*d4a07e70Sfengbojiang int ipq_maxoff; /* total length of packet */ 651eaf0ac3Slogwang struct mbuf *ipq_frags; /* to ip headers of fragments */ 661eaf0ac3Slogwang struct in_addr ipq_src,ipq_dst; 671eaf0ac3Slogwang u_char ipq_nfrags; /* # frags in this packet */ 681eaf0ac3Slogwang struct label *ipq_label; /* MAC label */ 691eaf0ac3Slogwang }; 701eaf0ac3Slogwang #endif /* _KERNEL */ 711eaf0ac3Slogwang 721eaf0ac3Slogwang /* 731eaf0ac3Slogwang * Structure stored in mbuf in inpcb.ip_options 741eaf0ac3Slogwang * and passed to ip_output when ip options are in use. 751eaf0ac3Slogwang * The actual length of the options (including ipopt_dst) 761eaf0ac3Slogwang * is in m_len. 771eaf0ac3Slogwang */ 781eaf0ac3Slogwang #define MAX_IPOPTLEN 40 791eaf0ac3Slogwang 801eaf0ac3Slogwang struct ipoption { 811eaf0ac3Slogwang struct in_addr ipopt_dst; /* first-hop dst if source routed */ 821eaf0ac3Slogwang char ipopt_list[MAX_IPOPTLEN]; /* options proper */ 831eaf0ac3Slogwang }; 841eaf0ac3Slogwang 85*d4a07e70Sfengbojiang #if defined(_NETINET_IN_VAR_H_) && defined(_KERNEL) 861eaf0ac3Slogwang /* 871eaf0ac3Slogwang * Structure attached to inpcb.ip_moptions and 881eaf0ac3Slogwang * passed to ip_output when IP multicast options are in use. 891eaf0ac3Slogwang * This structure is lazy-allocated. 901eaf0ac3Slogwang */ 911eaf0ac3Slogwang struct ip_moptions { 921eaf0ac3Slogwang struct ifnet *imo_multicast_ifp; /* ifp for outgoing multicasts */ 931eaf0ac3Slogwang struct in_addr imo_multicast_addr; /* ifindex/addr on MULTICAST_IF */ 941eaf0ac3Slogwang u_long imo_multicast_vif; /* vif num outgoing multicasts */ 951eaf0ac3Slogwang u_char imo_multicast_ttl; /* TTL for outgoing multicasts */ 961eaf0ac3Slogwang u_char imo_multicast_loop; /* 1 => hear sends if a member */ 97*d4a07e70Sfengbojiang struct ip_mfilter_head imo_head; /* group membership list */ 981eaf0ac3Slogwang }; 99*d4a07e70Sfengbojiang #else 100*d4a07e70Sfengbojiang struct ip_moptions; 101*d4a07e70Sfengbojiang #endif 1021eaf0ac3Slogwang 1031eaf0ac3Slogwang struct ipstat { 1041eaf0ac3Slogwang uint64_t ips_total; /* total packets received */ 1051eaf0ac3Slogwang uint64_t ips_badsum; /* checksum bad */ 1061eaf0ac3Slogwang uint64_t ips_tooshort; /* packet too short */ 1071eaf0ac3Slogwang uint64_t ips_toosmall; /* not enough data */ 1081eaf0ac3Slogwang uint64_t ips_badhlen; /* ip header length < data size */ 1091eaf0ac3Slogwang uint64_t ips_badlen; /* ip length < ip header length */ 1101eaf0ac3Slogwang uint64_t ips_fragments; /* fragments received */ 1111eaf0ac3Slogwang uint64_t ips_fragdropped; /* frags dropped (dups, out of space) */ 1121eaf0ac3Slogwang uint64_t ips_fragtimeout; /* fragments timed out */ 1131eaf0ac3Slogwang uint64_t ips_forward; /* packets forwarded */ 1141eaf0ac3Slogwang uint64_t ips_fastforward; /* packets fast forwarded */ 1151eaf0ac3Slogwang uint64_t ips_cantforward; /* packets rcvd for unreachable dest */ 1161eaf0ac3Slogwang uint64_t ips_redirectsent; /* packets forwarded on same net */ 1171eaf0ac3Slogwang uint64_t ips_noproto; /* unknown or unsupported protocol */ 1181eaf0ac3Slogwang uint64_t ips_delivered; /* datagrams delivered to upper level*/ 1191eaf0ac3Slogwang uint64_t ips_localout; /* total ip packets generated here */ 1201eaf0ac3Slogwang uint64_t ips_odropped; /* lost packets due to nobufs, etc. */ 1211eaf0ac3Slogwang uint64_t ips_reassembled; /* total packets reassembled ok */ 1221eaf0ac3Slogwang uint64_t ips_fragmented; /* datagrams successfully fragmented */ 1231eaf0ac3Slogwang uint64_t ips_ofragments; /* output fragments created */ 1241eaf0ac3Slogwang uint64_t ips_cantfrag; /* don't fragment flag was set, etc. */ 1251eaf0ac3Slogwang uint64_t ips_badoptions; /* error in option processing */ 1261eaf0ac3Slogwang uint64_t ips_noroute; /* packets discarded due to no route */ 1271eaf0ac3Slogwang uint64_t ips_badvers; /* ip version != 4 */ 1281eaf0ac3Slogwang uint64_t ips_rawout; /* total raw ip packets generated */ 1291eaf0ac3Slogwang uint64_t ips_toolong; /* ip length > max ip packet size */ 1301eaf0ac3Slogwang uint64_t ips_notmember; /* multicasts for unregistered grps */ 1311eaf0ac3Slogwang uint64_t ips_nogif; /* no match gif found */ 1321eaf0ac3Slogwang uint64_t ips_badaddr; /* invalid address on header */ 1331eaf0ac3Slogwang }; 1341eaf0ac3Slogwang 1351eaf0ac3Slogwang #ifdef _KERNEL 1361eaf0ac3Slogwang 1371eaf0ac3Slogwang #include <sys/counter.h> 1381eaf0ac3Slogwang #include <net/vnet.h> 1391eaf0ac3Slogwang 1401eaf0ac3Slogwang VNET_PCPUSTAT_DECLARE(struct ipstat, ipstat); 1411eaf0ac3Slogwang /* 1421eaf0ac3Slogwang * In-kernel consumers can use these accessor macros directly to update 1431eaf0ac3Slogwang * stats. 1441eaf0ac3Slogwang */ 1451eaf0ac3Slogwang #define IPSTAT_ADD(name, val) \ 1461eaf0ac3Slogwang VNET_PCPUSTAT_ADD(struct ipstat, ipstat, name, (val)) 1471eaf0ac3Slogwang #define IPSTAT_SUB(name, val) IPSTAT_ADD(name, -(val)) 1481eaf0ac3Slogwang #define IPSTAT_INC(name) IPSTAT_ADD(name, 1) 1491eaf0ac3Slogwang #define IPSTAT_DEC(name) IPSTAT_SUB(name, 1) 1501eaf0ac3Slogwang 1511eaf0ac3Slogwang /* 1521eaf0ac3Slogwang * Kernel module consumers must use this accessor macro. 1531eaf0ac3Slogwang */ 1541eaf0ac3Slogwang void kmod_ipstat_inc(int statnum); 1551eaf0ac3Slogwang #define KMOD_IPSTAT_INC(name) \ 1561eaf0ac3Slogwang kmod_ipstat_inc(offsetof(struct ipstat, name) / sizeof(uint64_t)) 1571eaf0ac3Slogwang void kmod_ipstat_dec(int statnum); 1581eaf0ac3Slogwang #define KMOD_IPSTAT_DEC(name) \ 1591eaf0ac3Slogwang kmod_ipstat_dec(offsetof(struct ipstat, name) / sizeof(uint64_t)) 1601eaf0ac3Slogwang 1611eaf0ac3Slogwang /* flags passed to ip_output as last parameter */ 1621eaf0ac3Slogwang #define IP_FORWARDING 0x1 /* most of ip header exists */ 1631eaf0ac3Slogwang #define IP_RAWOUTPUT 0x2 /* raw ip header exists */ 1641eaf0ac3Slogwang #define IP_SENDONES 0x4 /* send all-ones broadcast */ 1651eaf0ac3Slogwang #define IP_SENDTOIF 0x8 /* send on specific ifnet */ 1661eaf0ac3Slogwang #define IP_ROUTETOIF SO_DONTROUTE /* 0x10 bypass routing tables */ 1671eaf0ac3Slogwang #define IP_ALLOWBROADCAST SO_BROADCAST /* 0x20 can send broadcast packets */ 1681eaf0ac3Slogwang #define IP_NODEFAULTFLOWID 0x40 /* Don't set the flowid from inp */ 169*d4a07e70Sfengbojiang #define IP_NO_SND_TAG_RL 0x80 /* Don't send down the ratelimit tag */ 1701eaf0ac3Slogwang 1711eaf0ac3Slogwang #ifdef __NO_STRICT_ALIGNMENT 1721eaf0ac3Slogwang #define IP_HDR_ALIGNED_P(ip) 1 1731eaf0ac3Slogwang #else 1741eaf0ac3Slogwang #define IP_HDR_ALIGNED_P(ip) ((((intptr_t) (ip)) & 3) == 0) 1751eaf0ac3Slogwang #endif 1761eaf0ac3Slogwang 1771eaf0ac3Slogwang struct ip; 1781eaf0ac3Slogwang struct inpcb; 1791eaf0ac3Slogwang struct route; 1801eaf0ac3Slogwang struct sockopt; 181*d4a07e70Sfengbojiang struct inpcbinfo; 1821eaf0ac3Slogwang 1831eaf0ac3Slogwang VNET_DECLARE(int, ip_defttl); /* default IP ttl */ 1841eaf0ac3Slogwang VNET_DECLARE(int, ipforwarding); /* ip forwarding */ 185*d4a07e70Sfengbojiang VNET_DECLARE(int, ipsendredirects); 1861eaf0ac3Slogwang #ifdef IPSTEALTH 1871eaf0ac3Slogwang VNET_DECLARE(int, ipstealth); /* stealth forwarding */ 1881eaf0ac3Slogwang #endif 1891eaf0ac3Slogwang extern u_char ip_protox[]; 1901eaf0ac3Slogwang VNET_DECLARE(struct socket *, ip_rsvpd); /* reservation protocol daemon*/ 1911eaf0ac3Slogwang VNET_DECLARE(struct socket *, ip_mrouter); /* multicast routing daemon */ 1921eaf0ac3Slogwang extern int (*legal_vif_num)(int); 1931eaf0ac3Slogwang extern u_long (*ip_mcast_src)(int); 1941eaf0ac3Slogwang VNET_DECLARE(int, rsvp_on); 1951eaf0ac3Slogwang VNET_DECLARE(int, drop_redirect); 1961eaf0ac3Slogwang extern struct pr_usrreqs rip_usrreqs; 1971eaf0ac3Slogwang 1981eaf0ac3Slogwang #define V_ip_id VNET(ip_id) 1991eaf0ac3Slogwang #define V_ip_defttl VNET(ip_defttl) 2001eaf0ac3Slogwang #define V_ipforwarding VNET(ipforwarding) 201*d4a07e70Sfengbojiang #define V_ipsendredirects VNET(ipsendredirects) 2021eaf0ac3Slogwang #ifdef IPSTEALTH 2031eaf0ac3Slogwang #define V_ipstealth VNET(ipstealth) 2041eaf0ac3Slogwang #endif 2051eaf0ac3Slogwang #define V_ip_rsvpd VNET(ip_rsvpd) 2061eaf0ac3Slogwang #define V_ip_mrouter VNET(ip_mrouter) 2071eaf0ac3Slogwang #define V_rsvp_on VNET(rsvp_on) 2081eaf0ac3Slogwang #define V_drop_redirect VNET(drop_redirect) 2091eaf0ac3Slogwang 2101eaf0ac3Slogwang void inp_freemoptions(struct ip_moptions *); 2111eaf0ac3Slogwang int inp_getmoptions(struct inpcb *, struct sockopt *); 2121eaf0ac3Slogwang int inp_setmoptions(struct inpcb *, struct sockopt *); 2131eaf0ac3Slogwang 2141eaf0ac3Slogwang int ip_ctloutput(struct socket *, struct sockopt *sopt); 2151eaf0ac3Slogwang void ip_drain(void); 2161eaf0ac3Slogwang int ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 2171eaf0ac3Slogwang u_long if_hwassist_flags); 2181eaf0ac3Slogwang void ip_forward(struct mbuf *m, int srcrt); 2191eaf0ac3Slogwang void ip_init(void); 2201eaf0ac3Slogwang extern int 2211eaf0ac3Slogwang (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 2221eaf0ac3Slogwang struct ip_moptions *); 2231eaf0ac3Slogwang int ip_output(struct mbuf *, 2241eaf0ac3Slogwang struct mbuf *, struct route *, int, struct ip_moptions *, 2251eaf0ac3Slogwang struct inpcb *); 2261eaf0ac3Slogwang int ipproto_register(short); 2271eaf0ac3Slogwang int ipproto_unregister(short); 2281eaf0ac3Slogwang struct mbuf * 2291eaf0ac3Slogwang ip_reass(struct mbuf *); 2301eaf0ac3Slogwang void ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *, 2311eaf0ac3Slogwang struct mbuf *); 2321eaf0ac3Slogwang void ip_slowtimo(void); 2331eaf0ac3Slogwang void ip_fillid(struct ip *); 2341eaf0ac3Slogwang int rip_ctloutput(struct socket *, struct sockopt *); 2351eaf0ac3Slogwang void rip_ctlinput(int, struct sockaddr *, void *); 2361eaf0ac3Slogwang void rip_init(void); 2371eaf0ac3Slogwang int rip_input(struct mbuf **, int *, int); 2381eaf0ac3Slogwang int rip_output(struct mbuf *, struct socket *, ...); 2391eaf0ac3Slogwang int ipip_input(struct mbuf **, int *, int); 2401eaf0ac3Slogwang int rsvp_input(struct mbuf **, int *, int); 2411eaf0ac3Slogwang int ip_rsvp_init(struct socket *); 2421eaf0ac3Slogwang int ip_rsvp_done(void); 2431eaf0ac3Slogwang extern int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 2441eaf0ac3Slogwang extern void (*ip_rsvp_force_done)(struct socket *); 2451eaf0ac3Slogwang extern int (*rsvp_input_p)(struct mbuf **, int *, int); 2461eaf0ac3Slogwang 247*d4a07e70Sfengbojiang VNET_DECLARE(struct pfil_head *, inet_pfil_head); 248*d4a07e70Sfengbojiang #define V_inet_pfil_head VNET(inet_pfil_head) 249*d4a07e70Sfengbojiang #define PFIL_INET_NAME "inet" 2501eaf0ac3Slogwang 2511eaf0ac3Slogwang void in_delayed_cksum(struct mbuf *m); 2521eaf0ac3Slogwang 2531eaf0ac3Slogwang /* Hooks for ipfw, dummynet, divert etc. Most are declared in raw_ip.c */ 2541eaf0ac3Slogwang /* 2551eaf0ac3Slogwang * Reference to an ipfw or packet filter rule that can be carried 2561eaf0ac3Slogwang * outside critical sections. 2571eaf0ac3Slogwang * A rule is identified by rulenum:rule_id which is ordered. 2581eaf0ac3Slogwang * In version chain_id the rule can be found in slot 'slot', so 2591eaf0ac3Slogwang * we don't need a lookup if chain_id == chain->id. 2601eaf0ac3Slogwang * 2611eaf0ac3Slogwang * On exit from the firewall this structure refers to the rule after 2621eaf0ac3Slogwang * the matching one (slot points to the new rule; rulenum:rule_id-1 2631eaf0ac3Slogwang * is the matching rule), and additional info (e.g. info often contains 2641eaf0ac3Slogwang * the insn argument or tablearg in the low 16 bits, in host format). 2651eaf0ac3Slogwang * On entry, the structure is valid if slot>0, and refers to the starting 2661eaf0ac3Slogwang * rules. 'info' contains the reason for reinject, e.g. divert port, 2671eaf0ac3Slogwang * divert direction, and so on. 2681eaf0ac3Slogwang */ 2691eaf0ac3Slogwang struct ipfw_rule_ref { 2701eaf0ac3Slogwang uint32_t slot; /* slot for matching rule */ 2711eaf0ac3Slogwang uint32_t rulenum; /* matching rule number */ 2721eaf0ac3Slogwang uint32_t rule_id; /* matching rule id */ 2731eaf0ac3Slogwang uint32_t chain_id; /* ruleset id */ 2741eaf0ac3Slogwang uint32_t info; /* see below */ 2751eaf0ac3Slogwang }; 2761eaf0ac3Slogwang 2771eaf0ac3Slogwang enum { 2781eaf0ac3Slogwang IPFW_INFO_MASK = 0x0000ffff, 2791eaf0ac3Slogwang IPFW_INFO_OUT = 0x00000000, /* outgoing, just for convenience */ 2801eaf0ac3Slogwang IPFW_INFO_IN = 0x80000000, /* incoming, overloads dir */ 2811eaf0ac3Slogwang IPFW_ONEPASS = 0x40000000, /* One-pass, do not reinject */ 2821eaf0ac3Slogwang IPFW_IS_MASK = 0x30000000, /* which source ? */ 2831eaf0ac3Slogwang IPFW_IS_DIVERT = 0x20000000, 2841eaf0ac3Slogwang IPFW_IS_DUMMYNET =0x10000000, 2851eaf0ac3Slogwang IPFW_IS_PIPE = 0x08000000, /* pipe=1, queue = 0 */ 2861eaf0ac3Slogwang }; 2871eaf0ac3Slogwang #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */ 2881eaf0ac3Slogwang #define MTAG_IPFW_RULE 1262273568 /* rule reference */ 2891eaf0ac3Slogwang #define MTAG_IPFW_CALL 1308397630 /* call stack */ 2901eaf0ac3Slogwang 2911eaf0ac3Slogwang struct ip_fw_args; 2921eaf0ac3Slogwang typedef int (*ip_fw_chk_ptr_t)(struct ip_fw_args *args); 2931eaf0ac3Slogwang typedef int (*ip_fw_ctl_ptr_t)(struct sockopt *); 2941eaf0ac3Slogwang VNET_DECLARE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr); 2951eaf0ac3Slogwang #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr) 2961eaf0ac3Slogwang 2971eaf0ac3Slogwang /* Divert hooks. */ 298*d4a07e70Sfengbojiang extern void (*ip_divert_ptr)(struct mbuf *m, bool incoming); 2991eaf0ac3Slogwang /* ng_ipfw hooks -- XXX make it the same as divert and dummynet */ 300*d4a07e70Sfengbojiang extern int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool); 3011eaf0ac3Slogwang extern int (*ip_dn_ctl_ptr)(struct sockopt *); 302*d4a07e70Sfengbojiang extern int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *); 3031eaf0ac3Slogwang #endif /* _KERNEL */ 3041eaf0ac3Slogwang 3051eaf0ac3Slogwang #endif /* !_NETINET_IP_VAR_H_ */ 306