kernel: split patches folder up into backport, pending and hack folders
[oweals/openwrt.git] / target / linux / generic / pending-4.9 / 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 diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
10 index d919350a0e8b..9178930fca17 100644
11 --- a/net/ipv4/netfilter/ip_tables.c
12 +++ b/net/ipv4/netfilter/ip_tables.c
13 @@ -254,6 +254,33 @@ struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
14         return (void *)entry + entry->next_offset;
15  }
16  
17 +static bool
18 +ipt_handle_default_rule(struct ipt_entry *e, unsigned int *verdict)
19 +{
20 +       struct xt_entry_target *t;
21 +       struct xt_standard_target *st;
22 +
23 +       if (e->target_offset != sizeof(struct ipt_entry))
24 +               return false;
25 +
26 +       if (!(e->ip.flags & IPT_F_NO_DEF_MATCH))
27 +               return false;
28 +
29 +       t = ipt_get_target(e);
30 +       if (t->u.kernel.target->target)
31 +               return false;
32 +
33 +       st = (struct xt_standard_target *) t;
34 +       if (st->verdict == XT_RETURN)
35 +               return false;
36 +
37 +       if (st->verdict >= 0)
38 +               return false;
39 +
40 +       *verdict = (unsigned)(-st->verdict) - 1;
41 +       return true;
42 +}
43 +
44  /* Returns one of the generic firewall policies, like NF_ACCEPT. */
45  unsigned int
46  ipt_do_table(struct sk_buff *skb,
47 @@ -274,28 +301,8 @@ ipt_do_table(struct sk_buff *skb,
48         unsigned int addend;
49  
50         /* Initialization */
51 -       stackidx = 0;
52 -       ip = ip_hdr(skb);
53 -       indev = state->in ? state->in->name : nulldevname;
54 -       outdev = state->out ? state->out->name : nulldevname;
55 -       /* We handle fragments by dealing with the first fragment as
56 -        * if it was a normal packet.  All other fragments are treated
57 -        * normally, except that they will NEVER match rules that ask
58 -        * things we don't know, ie. tcp syn flag or ports).  If the
59 -        * rule is also a fragment-specific rule, non-fragments won't
60 -        * match it. */
61 -       acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
62 -       acpar.thoff   = ip_hdrlen(skb);
63 -       acpar.hotdrop = false;
64 -       acpar.net     = state->net;
65 -       acpar.in      = state->in;
66 -       acpar.out     = state->out;
67 -       acpar.family  = NFPROTO_IPV4;
68 -       acpar.hooknum = hook;
69 -
70         IP_NF_ASSERT(table->valid_hooks & (1 << hook));
71         local_bh_disable();
72 -       addend = xt_write_recseq_begin();
73         private = table->private;
74         cpu        = smp_processor_id();
75         /*
76 @@ -304,6 +311,23 @@ ipt_do_table(struct sk_buff *skb,
77          */
78         smp_read_barrier_depends();
79         table_base = private->entries;
80 +
81 +       e = get_entry(table_base, private->hook_entry[hook]);
82 +       if (ipt_handle_default_rule(e, &verdict)) {
83 +               struct xt_counters *counter;
84 +
85 +               counter = xt_get_this_cpu_counter(&e->counters);
86 +               ADD_COUNTER(*counter, skb->len, 1);
87 +               local_bh_enable();
88 +               return verdict;
89 +       }
90 +
91 +       stackidx = 0;
92 +       ip = ip_hdr(skb);
93 +       indev = state->in ? state->in->name : nulldevname;
94 +       outdev = state->out ? state->out->name : nulldevname;
95 +
96 +       addend = xt_write_recseq_begin();
97         jumpstack  = (struct ipt_entry **)private->jumpstack[cpu];
98  
99         /* Switch to alternate jumpstack if we're being invoked via TEE.
100 @@ -316,7 +340,20 @@ ipt_do_table(struct sk_buff *skb,
101         if (static_key_false(&xt_tee_enabled))
102                 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
103  
104 -       e = get_entry(table_base, private->hook_entry[hook]);
105 +       /* We handle fragments by dealing with the first fragment as
106 +        * if it was a normal packet.  All other fragments are treated
107 +        * normally, except that they will NEVER match rules that ask
108 +        * things we don't know, ie. tcp syn flag or ports).  If the
109 +        * rule is also a fragment-specific rule, non-fragments won't
110 +        * match it. */
111 +       acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
112 +       acpar.thoff   = ip_hdrlen(skb);
113 +       acpar.hotdrop = false;
114 +       acpar.net     = state->net;
115 +       acpar.in      = state->in;
116 +       acpar.out     = state->out;
117 +       acpar.family  = NFPROTO_IPV4;
118 +       acpar.hooknum = hook;
119  
120         do {
121                 const struct xt_entry_target *t;
122 -- 
123 2.11.0
124