1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_NETFILTER_H 3 #define __LINUX_NETFILTER_H 4 5 #include <linux/init.h> 6 #include <linux/skbuff.h> 7 #include <linux/net.h> 8 #include <linux/if.h> 9 #include <linux/in.h> 10 #include <linux/in6.h> 11 #include <linux/wait.h> 12 #include <linux/list.h> 13 #include <linux/static_key.h> 14 #include <linux/netfilter_defs.h> 15 #include <linux/netdevice.h> 16 #include <net/net_namespace.h> 17 18 #ifdef CONFIG_NETFILTER 19 static inline int NF_DROP_GETERR(int verdict) 20 { 21 return -(verdict >> NF_VERDICT_QBITS); 22 } 23 24 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, 25 const union nf_inet_addr *a2) 26 { 27 return a1->all[0] == a2->all[0] && 28 a1->all[1] == a2->all[1] && 29 a1->all[2] == a2->all[2] && 30 a1->all[3] == a2->all[3]; 31 } 32 33 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, 34 union nf_inet_addr *result, 35 const union nf_inet_addr *mask) 36 { 37 result->all[0] = a1->all[0] & mask->all[0]; 38 result->all[1] = a1->all[1] & mask->all[1]; 39 result->all[2] = a1->all[2] & mask->all[2]; 40 result->all[3] = a1->all[3] & mask->all[3]; 41 } 42 43 int netfilter_init(void); 44 45 struct sk_buff; 46 47 struct nf_hook_ops; 48 49 struct sock; 50 51 struct nf_hook_state { 52 unsigned int hook; 53 u_int8_t pf; 54 struct net_device *in; 55 struct net_device *out; 56 struct sock *sk; 57 struct net *net; 58 int (*okfn)(struct net *, struct sock *, struct sk_buff *); 59 }; 60 61 typedef unsigned int nf_hookfn(void *priv, 62 struct sk_buff *skb, 63 const struct nf_hook_state *state); 64 struct nf_hook_ops { 65 /* User fills in from here down. */ 66 nf_hookfn *hook; 67 struct net_device *dev; 68 void *priv; 69 u_int8_t pf; 70 unsigned int hooknum; 71 /* Hooks are ordered in ascending priority. */ 72 int priority; 73 }; 74 75 struct nf_hook_entry { 76 nf_hookfn *hook; 77 void *priv; 78 }; 79 80 struct nf_hook_entries_rcu_head { 81 struct rcu_head head; 82 void *allocation; 83 }; 84 85 struct nf_hook_entries { 86 u16 num_hook_entries; 87 /* padding */ 88 struct nf_hook_entry hooks[]; 89 90 /* trailer: pointers to original orig_ops of each hook, 91 * followed by rcu_head and scratch space used for freeing 92 * the structure via call_rcu. 93 * 94 * This is not part of struct nf_hook_entry since its only 95 * needed in slow path (hook register/unregister): 96 * const struct nf_hook_ops *orig_ops[] 97 * 98 * For the same reason, we store this at end -- its 99 * only needed when a hook is deleted, not during 100 * packet path processing: 101 * struct nf_hook_entries_rcu_head head 102 */ 103 }; 104 105 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e) 106 { 107 unsigned int n = e->num_hook_entries; 108 const void *hook_end; 109 110 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */ 111 112 return (struct nf_hook_ops **)hook_end; 113 } 114 115 static inline int 116 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb, 117 struct nf_hook_state *state) 118 { 119 return entry->hook(entry->priv, skb, state); 120 } 121 122 static inline void nf_hook_state_init(struct nf_hook_state *p, 123 unsigned int hook, 124 u_int8_t pf, 125 struct net_device *indev, 126 struct net_device *outdev, 127 struct sock *sk, 128 struct net *net, 129 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 130 { 131 p->hook = hook; 132 p->pf = pf; 133 p->in = indev; 134 p->out = outdev; 135 p->sk = sk; 136 p->net = net; 137 p->okfn = okfn; 138 } 139 140 141 142 struct nf_sockopt_ops { 143 struct list_head list; 144 145 u_int8_t pf; 146 147 /* Non-inclusive ranges: use 0/0/NULL to never get called. */ 148 int set_optmin; 149 int set_optmax; 150 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); 151 #ifdef CONFIG_COMPAT 152 int (*compat_set)(struct sock *sk, int optval, 153 void __user *user, unsigned int len); 154 #endif 155 int get_optmin; 156 int get_optmax; 157 int (*get)(struct sock *sk, int optval, void __user *user, int *len); 158 #ifdef CONFIG_COMPAT 159 int (*compat_get)(struct sock *sk, int optval, 160 void __user *user, int *len); 161 #endif 162 /* Use the module struct to lock set/get code in place */ 163 struct module *owner; 164 }; 165 166 /* Function to register/unregister hook points. */ 167 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops); 168 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops); 169 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, 170 unsigned int n); 171 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, 172 unsigned int n); 173 174 /* Functions to register get/setsockopt ranges (non-inclusive). You 175 need to check permissions yourself! */ 176 int nf_register_sockopt(struct nf_sockopt_ops *reg); 177 void nf_unregister_sockopt(struct nf_sockopt_ops *reg); 178 179 #ifdef HAVE_JUMP_LABEL 180 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; 181 #endif 182 183 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state, 184 const struct nf_hook_entries *e, unsigned int i); 185 186 /** 187 * nf_hook - call a netfilter hook 188 * 189 * Returns 1 if the hook has allowed the packet to pass. The function 190 * okfn must be invoked by the caller in this case. Any other return 191 * value indicates the packet has been consumed by the hook. 192 */ 193 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, 194 struct sock *sk, struct sk_buff *skb, 195 struct net_device *indev, struct net_device *outdev, 196 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 197 { 198 struct nf_hook_entries *hook_head = NULL; 199 int ret = 1; 200 201 #ifdef HAVE_JUMP_LABEL 202 if (__builtin_constant_p(pf) && 203 __builtin_constant_p(hook) && 204 !static_key_false(&nf_hooks_needed[pf][hook])) 205 return 1; 206 #endif 207 208 rcu_read_lock(); 209 switch (pf) { 210 case NFPROTO_IPV4: 211 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]); 212 break; 213 case NFPROTO_IPV6: 214 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]); 215 break; 216 case NFPROTO_ARP: 217 #ifdef CONFIG_NETFILTER_FAMILY_ARP 218 hook_head = rcu_dereference(net->nf.hooks_arp[hook]); 219 #endif 220 break; 221 case NFPROTO_BRIDGE: 222 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 223 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]); 224 #endif 225 break; 226 #if IS_ENABLED(CONFIG_DECNET) 227 case NFPROTO_DECNET: 228 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]); 229 break; 230 #endif 231 default: 232 WARN_ON_ONCE(1); 233 break; 234 } 235 236 if (hook_head) { 237 struct nf_hook_state state; 238 239 nf_hook_state_init(&state, hook, pf, indev, outdev, 240 sk, net, okfn); 241 242 ret = nf_hook_slow(skb, &state, hook_head, 0); 243 } 244 rcu_read_unlock(); 245 246 return ret; 247 } 248 249 /* Activate hook; either okfn or kfree_skb called, unless a hook 250 returns NF_STOLEN (in which case, it's up to the hook to deal with 251 the consequences). 252 253 Returns -ERRNO if packet dropped. Zero means queued, stolen or 254 accepted. 255 */ 256 257 /* RR: 258 > I don't want nf_hook to return anything because people might forget 259 > about async and trust the return value to mean "packet was ok". 260 261 AK: 262 Just document it clearly, then you can expect some sense from kernel 263 coders :) 264 */ 265 266 static inline int 267 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 268 struct sk_buff *skb, struct net_device *in, struct net_device *out, 269 int (*okfn)(struct net *, struct sock *, struct sk_buff *), 270 bool cond) 271 { 272 int ret; 273 274 if (!cond || 275 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1)) 276 ret = okfn(net, sk, skb); 277 return ret; 278 } 279 280 static inline int 281 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, 282 struct net_device *in, struct net_device *out, 283 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 284 { 285 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn); 286 if (ret == 1) 287 ret = okfn(net, sk, skb); 288 return ret; 289 } 290 291 /* Call setsockopt() */ 292 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, 293 unsigned int len); 294 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, 295 int *len); 296 #ifdef CONFIG_COMPAT 297 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, 298 char __user *opt, unsigned int len); 299 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, 300 char __user *opt, int *len); 301 #endif 302 303 /* Call this before modifying an existing packet: ensures it is 304 modifiable and linear to the point you care about (writable_len). 305 Returns true or false. */ 306 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); 307 308 struct flowi; 309 struct nf_queue_entry; 310 311 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook, 312 unsigned int dataoff, u_int8_t protocol, 313 unsigned short family); 314 315 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook, 316 unsigned int dataoff, unsigned int len, 317 u_int8_t protocol, unsigned short family); 318 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl, 319 bool strict, unsigned short family); 320 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry); 321 322 #include <net/flow.h> 323 324 struct nf_conn; 325 enum nf_nat_manip_type; 326 struct nlattr; 327 enum ip_conntrack_dir; 328 329 struct nf_nat_hook { 330 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip, 331 const struct nlattr *attr); 332 void (*decode_session)(struct sk_buff *skb, struct flowi *fl); 333 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct, 334 enum nf_nat_manip_type mtype, 335 enum ip_conntrack_dir dir); 336 }; 337 338 extern struct nf_nat_hook __rcu *nf_nat_hook; 339 340 static inline void 341 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) 342 { 343 #ifdef CONFIG_NF_NAT_NEEDED 344 struct nf_nat_hook *nat_hook; 345 346 rcu_read_lock(); 347 nat_hook = rcu_dereference(nf_nat_hook); 348 if (nat_hook && nat_hook->decode_session) 349 nat_hook->decode_session(skb, fl); 350 rcu_read_unlock(); 351 #endif 352 } 353 354 #else /* !CONFIG_NETFILTER */ 355 static inline int 356 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 357 struct sk_buff *skb, struct net_device *in, struct net_device *out, 358 int (*okfn)(struct net *, struct sock *, struct sk_buff *), 359 bool cond) 360 { 361 return okfn(net, sk, skb); 362 } 363 364 static inline int 365 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 366 struct sk_buff *skb, struct net_device *in, struct net_device *out, 367 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 368 { 369 return okfn(net, sk, skb); 370 } 371 372 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, 373 struct sock *sk, struct sk_buff *skb, 374 struct net_device *indev, struct net_device *outdev, 375 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 376 { 377 return 1; 378 } 379 struct flowi; 380 static inline void 381 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) 382 { 383 } 384 #endif /*CONFIG_NETFILTER*/ 385 386 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 387 #include <linux/netfilter/nf_conntrack_zones_common.h> 388 389 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu; 390 void nf_ct_attach(struct sk_buff *, const struct sk_buff *); 391 #else 392 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} 393 #endif 394 395 struct nf_conn; 396 enum ip_conntrack_info; 397 398 struct nf_ct_hook { 399 int (*update)(struct net *net, struct sk_buff *skb); 400 void (*destroy)(struct nf_conntrack *); 401 }; 402 extern struct nf_ct_hook __rcu *nf_ct_hook; 403 404 struct nlattr; 405 406 struct nfnl_ct_hook { 407 struct nf_conn *(*get_ct)(const struct sk_buff *skb, 408 enum ip_conntrack_info *ctinfo); 409 size_t (*build_size)(const struct nf_conn *ct); 410 int (*build)(struct sk_buff *skb, struct nf_conn *ct, 411 enum ip_conntrack_info ctinfo, 412 u_int16_t ct_attr, u_int16_t ct_info_attr); 413 int (*parse)(const struct nlattr *attr, struct nf_conn *ct); 414 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, 415 u32 portid, u32 report); 416 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, 417 enum ip_conntrack_info ctinfo, s32 off); 418 }; 419 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook; 420 421 /** 422 * nf_skb_duplicated - TEE target has sent a packet 423 * 424 * When a xtables target sends a packet, the OUTPUT and POSTROUTING 425 * hooks are traversed again, i.e. nft and xtables are invoked recursively. 426 * 427 * This is used by xtables TEE target to prevent the duplicated skb from 428 * being duplicated again. 429 */ 430 DECLARE_PER_CPU(bool, nf_skb_duplicated); 431 432 #endif /*__LINUX_NETFILTER_H*/ 433