at v6.17 14 kB view raw
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/module.h> 15#include <linux/netfilter_defs.h> 16#include <linux/netdevice.h> 17#include <linux/sockptr.h> 18#include <net/net_namespace.h> 19 20static inline int NF_DROP_GETERR(int verdict) 21{ 22 return -(verdict >> NF_VERDICT_QBITS); 23} 24 25static __always_inline int 26NF_DROP_REASON(struct sk_buff *skb, enum skb_drop_reason reason, u32 err) 27{ 28 BUILD_BUG_ON(err > 0xffff); 29 30 kfree_skb_reason(skb, reason); 31 32 return ((err << 16) | NF_STOLEN); 33} 34 35static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, 36 const union nf_inet_addr *a2) 37{ 38#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 39 const unsigned long *ul1 = (const unsigned long *)a1; 40 const unsigned long *ul2 = (const unsigned long *)a2; 41 42 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL; 43#else 44 return a1->all[0] == a2->all[0] && 45 a1->all[1] == a2->all[1] && 46 a1->all[2] == a2->all[2] && 47 a1->all[3] == a2->all[3]; 48#endif 49} 50 51static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, 52 union nf_inet_addr *result, 53 const union nf_inet_addr *mask) 54{ 55#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 56 const unsigned long *ua = (const unsigned long *)a1; 57 unsigned long *ur = (unsigned long *)result; 58 const unsigned long *um = (const unsigned long *)mask; 59 60 ur[0] = ua[0] & um[0]; 61 ur[1] = ua[1] & um[1]; 62#else 63 result->all[0] = a1->all[0] & mask->all[0]; 64 result->all[1] = a1->all[1] & mask->all[1]; 65 result->all[2] = a1->all[2] & mask->all[2]; 66 result->all[3] = a1->all[3] & mask->all[3]; 67#endif 68} 69 70int netfilter_init(void); 71 72struct sk_buff; 73 74struct nf_hook_ops; 75 76struct sock; 77 78struct nf_hook_state { 79 u8 hook; 80 u8 pf; 81 struct net_device *in; 82 struct net_device *out; 83 struct sock *sk; 84 struct net *net; 85 int (*okfn)(struct net *, struct sock *, struct sk_buff *); 86}; 87 88typedef unsigned int nf_hookfn(void *priv, 89 struct sk_buff *skb, 90 const struct nf_hook_state *state); 91enum nf_hook_ops_type { 92 NF_HOOK_OP_UNDEFINED, 93 NF_HOOK_OP_NF_TABLES, 94 NF_HOOK_OP_BPF, 95 NF_HOOK_OP_NFT_FT, 96}; 97 98struct nf_hook_ops { 99 struct list_head list; 100 struct rcu_head rcu; 101 102 /* User fills in from here down. */ 103 nf_hookfn *hook; 104 struct net_device *dev; 105 void *priv; 106 u8 pf; 107 enum nf_hook_ops_type hook_ops_type:8; 108 unsigned int hooknum; 109 /* Hooks are ordered in ascending priority. */ 110 int priority; 111}; 112 113struct nf_hook_entry { 114 nf_hookfn *hook; 115 void *priv; 116}; 117 118struct nf_hook_entries_rcu_head { 119 struct rcu_head head; 120 void *allocation; 121}; 122 123struct nf_hook_entries { 124 u16 num_hook_entries; 125 /* padding */ 126 struct nf_hook_entry hooks[]; 127 128 /* trailer: pointers to original orig_ops of each hook, 129 * followed by rcu_head and scratch space used for freeing 130 * the structure via call_rcu. 131 * 132 * This is not part of struct nf_hook_entry since its only 133 * needed in slow path (hook register/unregister): 134 * const struct nf_hook_ops *orig_ops[] 135 * 136 * For the same reason, we store this at end -- its 137 * only needed when a hook is deleted, not during 138 * packet path processing: 139 * struct nf_hook_entries_rcu_head head 140 */ 141}; 142 143#ifdef CONFIG_NETFILTER 144static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e) 145{ 146 unsigned int n = e->num_hook_entries; 147 const void *hook_end; 148 149 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */ 150 151 return (struct nf_hook_ops **)hook_end; 152} 153 154static inline int 155nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb, 156 struct nf_hook_state *state) 157{ 158 return entry->hook(entry->priv, skb, state); 159} 160 161static inline void nf_hook_state_init(struct nf_hook_state *p, 162 unsigned int hook, 163 u_int8_t pf, 164 struct net_device *indev, 165 struct net_device *outdev, 166 struct sock *sk, 167 struct net *net, 168 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 169{ 170 p->hook = hook; 171 p->pf = pf; 172 p->in = indev; 173 p->out = outdev; 174 p->sk = sk; 175 p->net = net; 176 p->okfn = okfn; 177} 178 179 180 181struct nf_sockopt_ops { 182 struct list_head list; 183 184 u_int8_t pf; 185 186 /* Non-inclusive ranges: use 0/0/NULL to never get called. */ 187 int set_optmin; 188 int set_optmax; 189 int (*set)(struct sock *sk, int optval, sockptr_t arg, 190 unsigned int len); 191 int get_optmin; 192 int get_optmax; 193 int (*get)(struct sock *sk, int optval, void __user *user, int *len); 194 /* Use the module struct to lock set/get code in place */ 195 struct module *owner; 196}; 197 198/* Function to register/unregister hook points. */ 199int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops); 200void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops); 201int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, 202 unsigned int n); 203void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, 204 unsigned int n); 205 206/* Functions to register get/setsockopt ranges (non-inclusive). You 207 need to check permissions yourself! */ 208int nf_register_sockopt(struct nf_sockopt_ops *reg); 209void nf_unregister_sockopt(struct nf_sockopt_ops *reg); 210 211#ifdef CONFIG_JUMP_LABEL 212extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; 213#endif 214 215int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state, 216 const struct nf_hook_entries *e, unsigned int i); 217 218void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state, 219 const struct nf_hook_entries *e); 220/** 221 * nf_hook - call a netfilter hook 222 * 223 * Returns 1 if the hook has allowed the packet to pass. The function 224 * okfn must be invoked by the caller in this case. Any other return 225 * value indicates the packet has been consumed by the hook. 226 */ 227static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, 228 struct sock *sk, struct sk_buff *skb, 229 struct net_device *indev, struct net_device *outdev, 230 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 231{ 232 struct nf_hook_entries *hook_head = NULL; 233 int ret = 1; 234 235#ifdef CONFIG_JUMP_LABEL 236 if (__builtin_constant_p(pf) && 237 __builtin_constant_p(hook) && 238 !static_key_false(&nf_hooks_needed[pf][hook])) 239 return 1; 240#endif 241 242 rcu_read_lock(); 243 switch (pf) { 244 case NFPROTO_IPV4: 245 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]); 246 break; 247 case NFPROTO_IPV6: 248 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]); 249 break; 250 case NFPROTO_ARP: 251#ifdef CONFIG_NETFILTER_FAMILY_ARP 252 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp))) 253 break; 254 hook_head = rcu_dereference(net->nf.hooks_arp[hook]); 255#endif 256 break; 257 case NFPROTO_BRIDGE: 258#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 259 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]); 260#endif 261 break; 262 default: 263 WARN_ON_ONCE(1); 264 break; 265 } 266 267 if (hook_head) { 268 struct nf_hook_state state; 269 270 nf_hook_state_init(&state, hook, pf, indev, outdev, 271 sk, net, okfn); 272 273 ret = nf_hook_slow(skb, &state, hook_head, 0); 274 } 275 rcu_read_unlock(); 276 277 return ret; 278} 279 280/* Activate hook; either okfn or kfree_skb called, unless a hook 281 returns NF_STOLEN (in which case, it's up to the hook to deal with 282 the consequences). 283 284 Returns -ERRNO if packet dropped. Zero means queued, stolen or 285 accepted. 286*/ 287 288/* RR: 289 > I don't want nf_hook to return anything because people might forget 290 > about async and trust the return value to mean "packet was ok". 291 292 AK: 293 Just document it clearly, then you can expect some sense from kernel 294 coders :) 295*/ 296 297static inline int 298NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 299 struct sk_buff *skb, struct net_device *in, struct net_device *out, 300 int (*okfn)(struct net *, struct sock *, struct sk_buff *), 301 bool cond) 302{ 303 int ret; 304 305 if (!cond || 306 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1)) 307 ret = okfn(net, sk, skb); 308 return ret; 309} 310 311static inline int 312NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, 313 struct net_device *in, struct net_device *out, 314 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 315{ 316 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn); 317 if (ret == 1) 318 ret = okfn(net, sk, skb); 319 return ret; 320} 321 322static inline void 323NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 324 struct list_head *head, struct net_device *in, struct net_device *out, 325 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 326{ 327 struct nf_hook_entries *hook_head = NULL; 328 329#ifdef CONFIG_JUMP_LABEL 330 if (__builtin_constant_p(pf) && 331 __builtin_constant_p(hook) && 332 !static_key_false(&nf_hooks_needed[pf][hook])) 333 return; 334#endif 335 336 rcu_read_lock(); 337 switch (pf) { 338 case NFPROTO_IPV4: 339 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]); 340 break; 341 case NFPROTO_IPV6: 342 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]); 343 break; 344 default: 345 WARN_ON_ONCE(1); 346 break; 347 } 348 349 if (hook_head) { 350 struct nf_hook_state state; 351 352 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn); 353 354 nf_hook_slow_list(head, &state, hook_head); 355 } 356 rcu_read_unlock(); 357} 358 359/* Call setsockopt() */ 360int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt, 361 unsigned int len); 362int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, 363 int *len); 364 365struct flowi; 366struct nf_queue_entry; 367 368__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook, 369 unsigned int dataoff, u_int8_t protocol, 370 unsigned short family); 371 372__sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook, 373 unsigned int dataoff, unsigned int len, 374 u_int8_t protocol, unsigned short family); 375int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl, 376 bool strict, unsigned short family); 377 378#include <net/flow.h> 379 380struct nf_conn; 381enum nf_nat_manip_type; 382struct nlattr; 383 384struct nf_nat_hook { 385 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip, 386 const struct nlattr *attr); 387 void (*decode_session)(struct sk_buff *skb, struct flowi *fl); 388 void (*remove_nat_bysrc)(struct nf_conn *ct); 389}; 390 391extern const struct nf_nat_hook __rcu *nf_nat_hook; 392 393static inline void 394nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) 395{ 396#if IS_ENABLED(CONFIG_NF_NAT) 397 const struct nf_nat_hook *nat_hook; 398 399 rcu_read_lock(); 400 nat_hook = rcu_dereference(nf_nat_hook); 401 if (nat_hook && nat_hook->decode_session) 402 nat_hook->decode_session(skb, fl); 403 rcu_read_unlock(); 404#endif 405} 406 407#else /* !CONFIG_NETFILTER */ 408static inline int 409NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 410 struct sk_buff *skb, struct net_device *in, struct net_device *out, 411 int (*okfn)(struct net *, struct sock *, struct sk_buff *), 412 bool cond) 413{ 414 return okfn(net, sk, skb); 415} 416 417static inline int 418NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 419 struct sk_buff *skb, struct net_device *in, struct net_device *out, 420 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 421{ 422 return okfn(net, sk, skb); 423} 424 425static inline void 426NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, 427 struct list_head *head, struct net_device *in, struct net_device *out, 428 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 429{ 430 /* nothing to do */ 431} 432 433static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, 434 struct sock *sk, struct sk_buff *skb, 435 struct net_device *indev, struct net_device *outdev, 436 int (*okfn)(struct net *, struct sock *, struct sk_buff *)) 437{ 438 return 1; 439} 440struct flowi; 441static inline void 442nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) 443{ 444} 445#endif /*CONFIG_NETFILTER*/ 446 447#if IS_ENABLED(CONFIG_NF_CONNTRACK) 448#include <linux/netfilter/nf_conntrack_zones_common.h> 449 450void nf_ct_attach(struct sk_buff *, const struct sk_buff *); 451void nf_ct_set_closing(struct nf_conntrack *nfct); 452struct nf_conntrack_tuple; 453bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple, 454 const struct sk_buff *skb); 455#else 456static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} 457static inline void nf_ct_set_closing(struct nf_conntrack *nfct) {} 458struct nf_conntrack_tuple; 459static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple, 460 const struct sk_buff *skb) 461{ 462 return false; 463} 464#endif 465 466struct nf_conn; 467enum ip_conntrack_info; 468 469struct nf_ct_hook { 470 int (*update)(struct net *net, struct sk_buff *skb); 471 void (*destroy)(struct nf_conntrack *); 472 bool (*get_tuple_skb)(struct nf_conntrack_tuple *, 473 const struct sk_buff *); 474 void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb); 475 void (*set_closing)(struct nf_conntrack *nfct); 476 int (*confirm)(struct sk_buff *skb); 477 u32 (*get_id)(const struct nf_conntrack *nfct); 478}; 479extern const struct nf_ct_hook __rcu *nf_ct_hook; 480 481struct nlattr; 482 483struct nfnl_ct_hook { 484 size_t (*build_size)(const struct nf_conn *ct); 485 int (*build)(struct sk_buff *skb, struct nf_conn *ct, 486 enum ip_conntrack_info ctinfo, 487 u_int16_t ct_attr, u_int16_t ct_info_attr); 488 int (*parse)(const struct nlattr *attr, struct nf_conn *ct); 489 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, 490 u32 portid, u32 report); 491 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, 492 enum ip_conntrack_info ctinfo, s32 off); 493}; 494extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook; 495 496struct nf_defrag_hook { 497 struct module *owner; 498 int (*enable)(struct net *net); 499 void (*disable)(struct net *net); 500}; 501 502extern const struct nf_defrag_hook __rcu *nf_defrag_v4_hook; 503extern const struct nf_defrag_hook __rcu *nf_defrag_v6_hook; 504 505/* 506 * Contains bitmask of ctnetlink event subscribers, if any. 507 * Can't be pernet due to NETLINK_LISTEN_ALL_NSID setsockopt flag. 508 */ 509extern u8 nf_ctnetlink_has_listener; 510#endif /*__LINUX_NETFILTER_H*/