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