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