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