Add luci mirror repository
[librecmc/librecmc.git] / target / linux / generic / pending-4.19 / 611-netfilter_match_bypass_default_table.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: netfilter: match bypass default table
3
4 Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 ---
6  net/ipv4/netfilter/ip_tables.c | 79 +++++++++++++++++++++++++++++++-----------
7  1 file changed, 58 insertions(+), 21 deletions(-)
8
9 --- a/net/ipv4/netfilter/ip_tables.c
10 +++ b/net/ipv4/netfilter/ip_tables.c
11 @@ -249,6 +249,33 @@ struct ipt_entry *ipt_next_entry(const s
12         return (void *)entry + entry->next_offset;
13  }
14  
15 +static bool
16 +ipt_handle_default_rule(struct ipt_entry *e, unsigned int *verdict)
17 +{
18 +       struct xt_entry_target *t;
19 +       struct xt_standard_target *st;
20 +
21 +       if (e->target_offset != sizeof(struct ipt_entry))
22 +               return false;
23 +
24 +       if (!(e->ip.flags & IPT_F_NO_DEF_MATCH))
25 +               return false;
26 +
27 +       t = ipt_get_target(e);
28 +       if (t->u.kernel.target->target)
29 +               return false;
30 +
31 +       st = (struct xt_standard_target *) t;
32 +       if (st->verdict == XT_RETURN)
33 +               return false;
34 +
35 +       if (st->verdict >= 0)
36 +               return false;
37 +
38 +       *verdict = (unsigned)(-st->verdict) - 1;
39 +       return true;
40 +}
41 +
42  /* Returns one of the generic firewall policies, like NF_ACCEPT. */
43  unsigned int
44  ipt_do_table(struct sk_buff *skb,
45 @@ -269,27 +296,28 @@ ipt_do_table(struct sk_buff *skb,
46         unsigned int addend;
47  
48         /* Initialization */
49 +       WARN_ON(!(table->valid_hooks & (1 << hook)));
50 +       local_bh_disable();
51 +       private = READ_ONCE(table->private); /* Address dependency. */
52 +       cpu        = smp_processor_id();
53 +       table_base = private->entries;
54 +
55 +       e = get_entry(table_base, private->hook_entry[hook]);
56 +       if (ipt_handle_default_rule(e, &verdict)) {
57 +               struct xt_counters *counter;
58 +
59 +               counter = xt_get_this_cpu_counter(&e->counters);
60 +               ADD_COUNTER(*counter, skb->len, 1);
61 +               local_bh_enable();
62 +               return verdict;
63 +       }
64 +
65         stackidx = 0;
66         ip = ip_hdr(skb);
67         indev = state->in ? state->in->name : nulldevname;
68         outdev = state->out ? state->out->name : nulldevname;
69 -       /* We handle fragments by dealing with the first fragment as
70 -        * if it was a normal packet.  All other fragments are treated
71 -        * normally, except that they will NEVER match rules that ask
72 -        * things we don't know, ie. tcp syn flag or ports).  If the
73 -        * rule is also a fragment-specific rule, non-fragments won't
74 -        * match it. */
75 -       acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
76 -       acpar.thoff   = ip_hdrlen(skb);
77 -       acpar.hotdrop = false;
78 -       acpar.state   = state;
79  
80 -       WARN_ON(!(table->valid_hooks & (1 << hook)));
81 -       local_bh_disable();
82         addend = xt_write_recseq_begin();
83 -       private = READ_ONCE(table->private); /* Address dependency. */
84 -       cpu        = smp_processor_id();
85 -       table_base = private->entries;
86         jumpstack  = (struct ipt_entry **)private->jumpstack[cpu];
87  
88         /* Switch to alternate jumpstack if we're being invoked via TEE.
89 @@ -302,7 +330,16 @@ ipt_do_table(struct sk_buff *skb,
90         if (static_key_false(&xt_tee_enabled))
91                 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
92  
93 -       e = get_entry(table_base, private->hook_entry[hook]);
94 +       /* We handle fragments by dealing with the first fragment as
95 +        * if it was a normal packet.  All other fragments are treated
96 +        * normally, except that they will NEVER match rules that ask
97 +        * things we don't know, ie. tcp syn flag or ports).  If the
98 +        * rule is also a fragment-specific rule, non-fragments won't
99 +        * match it. */
100 +       acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
101 +       acpar.thoff   = ip_hdrlen(skb);
102 +       acpar.hotdrop = false;
103 +       acpar.state   = state;
104  
105         do {
106                 const struct xt_entry_target *t;