Back to home page

LXR

 
 

    


0001 #ifndef _X_TABLES_H
0002 #define _X_TABLES_H
0003 
0004 
0005 #include <linux/netdevice.h>
0006 #include <linux/static_key.h>
0007 #include <linux/netfilter.h>
0008 #include <uapi/linux/netfilter/x_tables.h>
0009 
0010 /* Test a struct->invflags and a boolean for inequality */
0011 #define NF_INVF(ptr, flag, boolean)                 \
0012     ((boolean) ^ !!((ptr)->invflags & (flag)))
0013 
0014 /**
0015  * struct xt_action_param - parameters for matches/targets
0016  *
0017  * @match:  the match extension
0018  * @target: the target extension
0019  * @matchinfo:  per-match data
0020  * @targetinfo: per-target data
0021  * @state:  pointer to hook state this packet came from
0022  * @fragoff:    packet is a fragment, this is the data offset
0023  * @thoff:  position of transport header relative to skb->data
0024  *
0025  * Fields written to by extensions:
0026  *
0027  * @hotdrop:    drop packet if we had inspection problems
0028  */
0029 struct xt_action_param {
0030     union {
0031         const struct xt_match *match;
0032         const struct xt_target *target;
0033     };
0034     union {
0035         const void *matchinfo, *targinfo;
0036     };
0037     const struct nf_hook_state *state;
0038     int fragoff;
0039     unsigned int thoff;
0040     bool hotdrop;
0041 };
0042 
0043 static inline struct net *xt_net(const struct xt_action_param *par)
0044 {
0045     return par->state->net;
0046 }
0047 
0048 static inline struct net_device *xt_in(const struct xt_action_param *par)
0049 {
0050     return par->state->in;
0051 }
0052 
0053 static inline const char *xt_inname(const struct xt_action_param *par)
0054 {
0055     return par->state->in->name;
0056 }
0057 
0058 static inline struct net_device *xt_out(const struct xt_action_param *par)
0059 {
0060     return par->state->out;
0061 }
0062 
0063 static inline const char *xt_outname(const struct xt_action_param *par)
0064 {
0065     return par->state->out->name;
0066 }
0067 
0068 static inline unsigned int xt_hooknum(const struct xt_action_param *par)
0069 {
0070     return par->state->hook;
0071 }
0072 
0073 static inline u_int8_t xt_family(const struct xt_action_param *par)
0074 {
0075     return par->state->pf;
0076 }
0077 
0078 /**
0079  * struct xt_mtchk_param - parameters for match extensions'
0080  * checkentry functions
0081  *
0082  * @net:    network namespace through which the check was invoked
0083  * @table:  table the rule is tried to be inserted into
0084  * @entryinfo:  the family-specific rule data
0085  *      (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
0086  * @match:  struct xt_match through which this function was invoked
0087  * @matchinfo:  per-match data
0088  * @hook_mask:  via which hooks the new rule is reachable
0089  * Other fields as above.
0090  */
0091 struct xt_mtchk_param {
0092     struct net *net;
0093     const char *table;
0094     const void *entryinfo;
0095     const struct xt_match *match;
0096     void *matchinfo;
0097     unsigned int hook_mask;
0098     u_int8_t family;
0099     bool nft_compat;
0100 };
0101 
0102 /**
0103  * struct xt_mdtor_param - match destructor parameters
0104  * Fields as above.
0105  */
0106 struct xt_mtdtor_param {
0107     struct net *net;
0108     const struct xt_match *match;
0109     void *matchinfo;
0110     u_int8_t family;
0111 };
0112 
0113 /**
0114  * struct xt_tgchk_param - parameters for target extensions'
0115  * checkentry functions
0116  *
0117  * @entryinfo:  the family-specific rule data
0118  *      (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
0119  *
0120  * Other fields see above.
0121  */
0122 struct xt_tgchk_param {
0123     struct net *net;
0124     const char *table;
0125     const void *entryinfo;
0126     const struct xt_target *target;
0127     void *targinfo;
0128     unsigned int hook_mask;
0129     u_int8_t family;
0130     bool nft_compat;
0131 };
0132 
0133 /* Target destructor parameters */
0134 struct xt_tgdtor_param {
0135     struct net *net;
0136     const struct xt_target *target;
0137     void *targinfo;
0138     u_int8_t family;
0139 };
0140 
0141 struct xt_match {
0142     struct list_head list;
0143 
0144     const char name[XT_EXTENSION_MAXNAMELEN];
0145     u_int8_t revision;
0146 
0147     /* Return true or false: return FALSE and set *hotdrop = 1 to
0148            force immediate packet drop. */
0149     /* Arguments changed since 2.6.9, as this must now handle
0150        non-linear skb, using skb_header_pointer and
0151        skb_ip_make_writable. */
0152     bool (*match)(const struct sk_buff *skb,
0153               struct xt_action_param *);
0154 
0155     /* Called when user tries to insert an entry of this type. */
0156     int (*checkentry)(const struct xt_mtchk_param *);
0157 
0158     /* Called when entry of this type deleted. */
0159     void (*destroy)(const struct xt_mtdtor_param *);
0160 #ifdef CONFIG_COMPAT
0161     /* Called when userspace align differs from kernel space one */
0162     void (*compat_from_user)(void *dst, const void *src);
0163     int (*compat_to_user)(void __user *dst, const void *src);
0164 #endif
0165     /* Set this to THIS_MODULE if you are a module, otherwise NULL */
0166     struct module *me;
0167 
0168     const char *table;
0169     unsigned int matchsize;
0170 #ifdef CONFIG_COMPAT
0171     unsigned int compatsize;
0172 #endif
0173     unsigned int hooks;
0174     unsigned short proto;
0175 
0176     unsigned short family;
0177 };
0178 
0179 /* Registration hooks for targets. */
0180 struct xt_target {
0181     struct list_head list;
0182 
0183     const char name[XT_EXTENSION_MAXNAMELEN];
0184     u_int8_t revision;
0185 
0186     /* Returns verdict. Argument order changed since 2.6.9, as this
0187        must now handle non-linear skbs, using skb_copy_bits and
0188        skb_ip_make_writable. */
0189     unsigned int (*target)(struct sk_buff *skb,
0190                    const struct xt_action_param *);
0191 
0192     /* Called when user tries to insert an entry of this type:
0193            hook_mask is a bitmask of hooks from which it can be
0194            called. */
0195     /* Should return 0 on success or an error code otherwise (-Exxxx). */
0196     int (*checkentry)(const struct xt_tgchk_param *);
0197 
0198     /* Called when entry of this type deleted. */
0199     void (*destroy)(const struct xt_tgdtor_param *);
0200 #ifdef CONFIG_COMPAT
0201     /* Called when userspace align differs from kernel space one */
0202     void (*compat_from_user)(void *dst, const void *src);
0203     int (*compat_to_user)(void __user *dst, const void *src);
0204 #endif
0205     /* Set this to THIS_MODULE if you are a module, otherwise NULL */
0206     struct module *me;
0207 
0208     const char *table;
0209     unsigned int targetsize;
0210 #ifdef CONFIG_COMPAT
0211     unsigned int compatsize;
0212 #endif
0213     unsigned int hooks;
0214     unsigned short proto;
0215 
0216     unsigned short family;
0217 };
0218 
0219 /* Furniture shopping... */
0220 struct xt_table {
0221     struct list_head list;
0222 
0223     /* What hooks you will enter on */
0224     unsigned int valid_hooks;
0225 
0226     /* Man behind the curtain... */
0227     struct xt_table_info *private;
0228 
0229     /* Set this to THIS_MODULE if you are a module, otherwise NULL */
0230     struct module *me;
0231 
0232     u_int8_t af;        /* address/protocol family */
0233     int priority;       /* hook order */
0234 
0235     /* called when table is needed in the given netns */
0236     int (*table_init)(struct net *net);
0237 
0238     /* A unique name... */
0239     const char name[XT_TABLE_MAXNAMELEN];
0240 };
0241 
0242 #include <linux/netfilter_ipv4.h>
0243 
0244 /* The table itself */
0245 struct xt_table_info {
0246     /* Size per table */
0247     unsigned int size;
0248     /* Number of entries: FIXME. --RR */
0249     unsigned int number;
0250     /* Initial number of entries. Needed for module usage count */
0251     unsigned int initial_entries;
0252 
0253     /* Entry points and underflows */
0254     unsigned int hook_entry[NF_INET_NUMHOOKS];
0255     unsigned int underflow[NF_INET_NUMHOOKS];
0256 
0257     /*
0258      * Number of user chains. Since tables cannot have loops, at most
0259      * @stacksize jumps (number of user chains) can possibly be made.
0260      */
0261     unsigned int stacksize;
0262     void ***jumpstack;
0263 
0264     unsigned char entries[0] __aligned(8);
0265 };
0266 
0267 int xt_register_target(struct xt_target *target);
0268 void xt_unregister_target(struct xt_target *target);
0269 int xt_register_targets(struct xt_target *target, unsigned int n);
0270 void xt_unregister_targets(struct xt_target *target, unsigned int n);
0271 
0272 int xt_register_match(struct xt_match *target);
0273 void xt_unregister_match(struct xt_match *target);
0274 int xt_register_matches(struct xt_match *match, unsigned int n);
0275 void xt_unregister_matches(struct xt_match *match, unsigned int n);
0276 
0277 int xt_check_entry_offsets(const void *base, const char *elems,
0278                unsigned int target_offset,
0279                unsigned int next_offset);
0280 
0281 unsigned int *xt_alloc_entry_offsets(unsigned int size);
0282 bool xt_find_jump_offset(const unsigned int *offsets,
0283              unsigned int target, unsigned int size);
0284 
0285 int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
0286            bool inv_proto);
0287 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
0288             bool inv_proto);
0289 
0290 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
0291                  struct xt_counters_info *info, bool compat);
0292 
0293 struct xt_table *xt_register_table(struct net *net,
0294                    const struct xt_table *table,
0295                    struct xt_table_info *bootstrap,
0296                    struct xt_table_info *newinfo);
0297 void *xt_unregister_table(struct xt_table *table);
0298 
0299 struct xt_table_info *xt_replace_table(struct xt_table *table,
0300                        unsigned int num_counters,
0301                        struct xt_table_info *newinfo,
0302                        int *error);
0303 
0304 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
0305 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
0306 struct xt_match *xt_request_find_match(u8 af, const char *name, u8 revision);
0307 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision);
0308 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
0309              int *err);
0310 
0311 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
0312                     const char *name);
0313 void xt_table_unlock(struct xt_table *t);
0314 
0315 int xt_proto_init(struct net *net, u_int8_t af);
0316 void xt_proto_fini(struct net *net, u_int8_t af);
0317 
0318 struct xt_table_info *xt_alloc_table_info(unsigned int size);
0319 void xt_free_table_info(struct xt_table_info *info);
0320 
0321 /**
0322  * xt_recseq - recursive seqcount for netfilter use
0323  * 
0324  * Packet processing changes the seqcount only if no recursion happened
0325  * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
0326  * because we use the normal seqcount convention :
0327  * Low order bit set to 1 if a writer is active.
0328  */
0329 DECLARE_PER_CPU(seqcount_t, xt_recseq);
0330 
0331 /* xt_tee_enabled - true if x_tables needs to handle reentrancy
0332  *
0333  * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
0334  */
0335 extern struct static_key xt_tee_enabled;
0336 
0337 /**
0338  * xt_write_recseq_begin - start of a write section
0339  *
0340  * Begin packet processing : all readers must wait the end
0341  * 1) Must be called with preemption disabled
0342  * 2) softirqs must be disabled too (or we should use this_cpu_add())
0343  * Returns :
0344  *  1 if no recursion on this cpu
0345  *  0 if recursion detected
0346  */
0347 static inline unsigned int xt_write_recseq_begin(void)
0348 {
0349     unsigned int addend;
0350 
0351     /*
0352      * Low order bit of sequence is set if we already
0353      * called xt_write_recseq_begin().
0354      */
0355     addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
0356 
0357     /*
0358      * This is kind of a write_seqcount_begin(), but addend is 0 or 1
0359      * We dont check addend value to avoid a test and conditional jump,
0360      * since addend is most likely 1
0361      */
0362     __this_cpu_add(xt_recseq.sequence, addend);
0363     smp_wmb();
0364 
0365     return addend;
0366 }
0367 
0368 /**
0369  * xt_write_recseq_end - end of a write section
0370  * @addend: return value from previous xt_write_recseq_begin()
0371  *
0372  * End packet processing : all readers can proceed
0373  * 1) Must be called with preemption disabled
0374  * 2) softirqs must be disabled too (or we should use this_cpu_add())
0375  */
0376 static inline void xt_write_recseq_end(unsigned int addend)
0377 {
0378     /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
0379     smp_wmb();
0380     __this_cpu_add(xt_recseq.sequence, addend);
0381 }
0382 
0383 /*
0384  * This helper is performance critical and must be inlined
0385  */
0386 static inline unsigned long ifname_compare_aligned(const char *_a,
0387                            const char *_b,
0388                            const char *_mask)
0389 {
0390     const unsigned long *a = (const unsigned long *)_a;
0391     const unsigned long *b = (const unsigned long *)_b;
0392     const unsigned long *mask = (const unsigned long *)_mask;
0393     unsigned long ret;
0394 
0395     ret = (a[0] ^ b[0]) & mask[0];
0396     if (IFNAMSIZ > sizeof(unsigned long))
0397         ret |= (a[1] ^ b[1]) & mask[1];
0398     if (IFNAMSIZ > 2 * sizeof(unsigned long))
0399         ret |= (a[2] ^ b[2]) & mask[2];
0400     if (IFNAMSIZ > 3 * sizeof(unsigned long))
0401         ret |= (a[3] ^ b[3]) & mask[3];
0402     BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
0403     return ret;
0404 }
0405 
0406 struct xt_percpu_counter_alloc_state {
0407     unsigned int off;
0408     const char __percpu *mem;
0409 };
0410 
0411 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
0412                  struct xt_counters *counter);
0413 void xt_percpu_counter_free(struct xt_counters *cnt);
0414 
0415 static inline struct xt_counters *
0416 xt_get_this_cpu_counter(struct xt_counters *cnt)
0417 {
0418     if (nr_cpu_ids > 1)
0419         return this_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt);
0420 
0421     return cnt;
0422 }
0423 
0424 static inline struct xt_counters *
0425 xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
0426 {
0427     if (nr_cpu_ids > 1)
0428         return per_cpu_ptr((void __percpu *) (unsigned long) cnt->pcnt, cpu);
0429 
0430     return cnt;
0431 }
0432 
0433 struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
0434 
0435 #ifdef CONFIG_COMPAT
0436 #include <net/compat.h>
0437 
0438 struct compat_xt_entry_match {
0439     union {
0440         struct {
0441             u_int16_t match_size;
0442             char name[XT_FUNCTION_MAXNAMELEN - 1];
0443             u_int8_t revision;
0444         } user;
0445         struct {
0446             u_int16_t match_size;
0447             compat_uptr_t match;
0448         } kernel;
0449         u_int16_t match_size;
0450     } u;
0451     unsigned char data[0];
0452 };
0453 
0454 struct compat_xt_entry_target {
0455     union {
0456         struct {
0457             u_int16_t target_size;
0458             char name[XT_FUNCTION_MAXNAMELEN - 1];
0459             u_int8_t revision;
0460         } user;
0461         struct {
0462             u_int16_t target_size;
0463             compat_uptr_t target;
0464         } kernel;
0465         u_int16_t target_size;
0466     } u;
0467     unsigned char data[0];
0468 };
0469 
0470 /* FIXME: this works only on 32 bit tasks
0471  * need to change whole approach in order to calculate align as function of
0472  * current task alignment */
0473 
0474 struct compat_xt_counters {
0475     compat_u64 pcnt, bcnt;          /* Packet and byte counters */
0476 };
0477 
0478 struct compat_xt_counters_info {
0479     char name[XT_TABLE_MAXNAMELEN];
0480     compat_uint_t num_counters;
0481     struct compat_xt_counters counters[0];
0482 };
0483 
0484 struct _compat_xt_align {
0485     __u8 u8;
0486     __u16 u16;
0487     __u32 u32;
0488     compat_u64 u64;
0489 };
0490 
0491 #define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
0492 
0493 void xt_compat_lock(u_int8_t af);
0494 void xt_compat_unlock(u_int8_t af);
0495 
0496 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
0497 void xt_compat_flush_offsets(u_int8_t af);
0498 void xt_compat_init_offsets(u_int8_t af, unsigned int number);
0499 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
0500 
0501 int xt_compat_match_offset(const struct xt_match *match);
0502 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
0503                   unsigned int *size);
0504 int xt_compat_match_to_user(const struct xt_entry_match *m,
0505                 void __user **dstptr, unsigned int *size);
0506 
0507 int xt_compat_target_offset(const struct xt_target *target);
0508 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
0509                 unsigned int *size);
0510 int xt_compat_target_to_user(const struct xt_entry_target *t,
0511                  void __user **dstptr, unsigned int *size);
0512 int xt_compat_check_entry_offsets(const void *base, const char *elems,
0513                   unsigned int target_offset,
0514                   unsigned int next_offset);
0515 
0516 #endif /* CONFIG_COMPAT */
0517 #endif /* _X_TABLES_H */