1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Header for use in defining a given L4 protocol for connection tracking. 4 * 5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> 6 * - generalized L3 protocol dependent part. 7 * 8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_protcol.h 9 */ 10 11 #ifndef _NF_CONNTRACK_L4PROTO_H 12 #define _NF_CONNTRACK_L4PROTO_H 13 #include <linux/netlink.h> 14 #include <net/netlink.h> 15 #include <net/netfilter/nf_conntrack.h> 16 #include <net/netns/generic.h> 17 18 struct seq_file; 19 20 struct nf_conntrack_l4proto { 21 /* L4 Protocol number. */ 22 u_int8_t l4proto; 23 24 /* Resolve clashes on insertion races. */ 25 bool allow_clash; 26 27 /* protoinfo nlattr size, closes a hole */ 28 u16 nlattr_size; 29 30 /* called by gc worker if table is full */ 31 bool (*can_early_drop)(const struct nf_conn *ct); 32 33 /* convert protoinfo to nfnetink attributes */ 34 int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla, 35 struct nf_conn *ct, bool destroy); 36 37 /* convert nfnetlink attributes to protoinfo */ 38 int (*from_nlattr)(struct nlattr *tb[], struct nf_conn *ct); 39 40 int (*tuple_to_nlattr)(struct sk_buff *skb, 41 const struct nf_conntrack_tuple *t); 42 /* Calculate tuple nlattr size */ 43 unsigned int (*nlattr_tuple_size)(void); 44 int (*nlattr_to_tuple)(struct nlattr *tb[], 45 struct nf_conntrack_tuple *t, 46 u_int32_t flags); 47 const struct nla_policy *nla_policy; 48 49 struct { 50 int (*nlattr_to_obj)(struct nlattr *tb[], 51 struct net *net, void *data); 52 int (*obj_to_nlattr)(struct sk_buff *skb, const void *data); 53 54 u16 obj_size; 55 u16 nlattr_max; 56 const struct nla_policy *nla_policy; 57 } ctnl_timeout; 58 #ifdef CONFIG_NF_CONNTRACK_PROCFS 59 /* Print out the private part of the conntrack. */ 60 void (*print_conntrack)(struct seq_file *s, struct nf_conn *); 61 #endif 62 }; 63 64 bool icmp_pkt_to_tuple(const struct sk_buff *skb, 65 unsigned int dataoff, 66 struct net *net, 67 struct nf_conntrack_tuple *tuple); 68 69 bool icmpv6_pkt_to_tuple(const struct sk_buff *skb, 70 unsigned int dataoff, 71 struct net *net, 72 struct nf_conntrack_tuple *tuple); 73 74 bool nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple *tuple, 75 const struct nf_conntrack_tuple *orig); 76 bool nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple *tuple, 77 const struct nf_conntrack_tuple *orig); 78 79 int nf_conntrack_inet_error(struct nf_conn *tmpl, struct sk_buff *skb, 80 unsigned int dataoff, 81 const struct nf_hook_state *state, 82 u8 l4proto, 83 union nf_inet_addr *outer_daddr); 84 85 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl, 86 struct sk_buff *skb, 87 unsigned int dataoff, 88 const struct nf_hook_state *state); 89 90 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl, 91 struct sk_buff *skb, 92 unsigned int dataoff, 93 const struct nf_hook_state *state); 94 95 int nf_conntrack_icmp_packet(struct nf_conn *ct, 96 struct sk_buff *skb, 97 enum ip_conntrack_info ctinfo, 98 const struct nf_hook_state *state); 99 100 int nf_conntrack_icmpv6_packet(struct nf_conn *ct, 101 struct sk_buff *skb, 102 enum ip_conntrack_info ctinfo, 103 const struct nf_hook_state *state); 104 105 int nf_conntrack_udp_packet(struct nf_conn *ct, 106 struct sk_buff *skb, 107 unsigned int dataoff, 108 enum ip_conntrack_info ctinfo, 109 const struct nf_hook_state *state); 110 int nf_conntrack_udplite_packet(struct nf_conn *ct, 111 struct sk_buff *skb, 112 unsigned int dataoff, 113 enum ip_conntrack_info ctinfo, 114 const struct nf_hook_state *state); 115 int nf_conntrack_tcp_packet(struct nf_conn *ct, 116 struct sk_buff *skb, 117 unsigned int dataoff, 118 enum ip_conntrack_info ctinfo, 119 const struct nf_hook_state *state); 120 int nf_conntrack_dccp_packet(struct nf_conn *ct, 121 struct sk_buff *skb, 122 unsigned int dataoff, 123 enum ip_conntrack_info ctinfo, 124 const struct nf_hook_state *state); 125 int nf_conntrack_sctp_packet(struct nf_conn *ct, 126 struct sk_buff *skb, 127 unsigned int dataoff, 128 enum ip_conntrack_info ctinfo, 129 const struct nf_hook_state *state); 130 int nf_conntrack_gre_packet(struct nf_conn *ct, 131 struct sk_buff *skb, 132 unsigned int dataoff, 133 enum ip_conntrack_info ctinfo, 134 const struct nf_hook_state *state); 135 136 void nf_conntrack_generic_init_net(struct net *net); 137 void nf_conntrack_tcp_init_net(struct net *net); 138 void nf_conntrack_udp_init_net(struct net *net); 139 void nf_conntrack_gre_init_net(struct net *net); 140 void nf_conntrack_dccp_init_net(struct net *net); 141 void nf_conntrack_sctp_init_net(struct net *net); 142 void nf_conntrack_icmp_init_net(struct net *net); 143 void nf_conntrack_icmpv6_init_net(struct net *net); 144 145 /* Existing built-in generic protocol */ 146 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic; 147 148 #define MAX_NF_CT_PROTO IPPROTO_UDPLITE 149 150 const struct nf_conntrack_l4proto *nf_ct_l4proto_find(u8 l4proto); 151 152 /* Generic netlink helpers */ 153 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb, 154 const struct nf_conntrack_tuple *tuple); 155 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[], 156 struct nf_conntrack_tuple *t, 157 u_int32_t flags); 158 unsigned int nf_ct_port_nlattr_tuple_size(void); 159 extern const struct nla_policy nf_ct_port_nla_policy[]; 160 161 #ifdef CONFIG_SYSCTL 162 __printf(3, 4) __cold 163 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb, 164 const struct nf_conn *ct, 165 const char *fmt, ...); 166 __printf(5, 6) __cold 167 void nf_l4proto_log_invalid(const struct sk_buff *skb, 168 struct net *net, 169 u16 pf, u8 protonum, 170 const char *fmt, ...); 171 #else 172 static inline __printf(5, 6) __cold 173 void nf_l4proto_log_invalid(const struct sk_buff *skb, struct net *net, 174 u16 pf, u8 protonum, const char *fmt, ...) {} 175 static inline __printf(3, 4) __cold 176 void nf_ct_l4proto_log_invalid(const struct sk_buff *skb, 177 const struct nf_conn *ct, 178 const char *fmt, ...) { } 179 #endif /* CONFIG_SYSCTL */ 180 181 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 182 static inline struct nf_generic_net *nf_generic_pernet(struct net *net) 183 { 184 return &net->ct.nf_ct_proto.generic; 185 } 186 187 static inline struct nf_tcp_net *nf_tcp_pernet(struct net *net) 188 { 189 return &net->ct.nf_ct_proto.tcp; 190 } 191 192 static inline struct nf_udp_net *nf_udp_pernet(struct net *net) 193 { 194 return &net->ct.nf_ct_proto.udp; 195 } 196 197 static inline struct nf_icmp_net *nf_icmp_pernet(struct net *net) 198 { 199 return &net->ct.nf_ct_proto.icmp; 200 } 201 202 static inline struct nf_icmp_net *nf_icmpv6_pernet(struct net *net) 203 { 204 return &net->ct.nf_ct_proto.icmpv6; 205 } 206 207 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */ 208 static inline void nf_ct_set_tcp_be_liberal(struct nf_conn *ct) 209 { 210 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 211 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL; 212 } 213 214 /* Caller must check nf_ct_protonum(ct) is IPPROTO_TCP before calling. */ 215 static inline bool nf_conntrack_tcp_established(const struct nf_conn *ct) 216 { 217 return ct->proto.tcp.state == TCP_CONNTRACK_ESTABLISHED && 218 test_bit(IPS_ASSURED_BIT, &ct->status); 219 } 220 #endif 221 222 #ifdef CONFIG_NF_CT_PROTO_DCCP 223 static inline struct nf_dccp_net *nf_dccp_pernet(struct net *net) 224 { 225 return &net->ct.nf_ct_proto.dccp; 226 } 227 #endif 228 229 #ifdef CONFIG_NF_CT_PROTO_SCTP 230 static inline struct nf_sctp_net *nf_sctp_pernet(struct net *net) 231 { 232 return &net->ct.nf_ct_proto.sctp; 233 } 234 #endif 235 236 #ifdef CONFIG_NF_CT_PROTO_GRE 237 static inline struct nf_gre_net *nf_gre_pernet(struct net *net) 238 { 239 return &net->ct.nf_ct_proto.gre; 240 } 241 #endif 242 243 #endif /*_NF_CONNTRACK_PROTOCOL_H*/ 244