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