kernel: start working on 3.18 support
[openwrt/openwrt.git] / target / linux / generic / patches-3.18 / 621-sched_act_connmark.patch
1 --- /dev/null
2 +++ b/net/sched/act_connmark.c
3 @@ -0,0 +1,144 @@
4 +/*
5 + * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org>
6 + *
7 + * This program is free software; you can redistribute it and/or modify it
8 + * under the terms and conditions of the GNU General Public License,
9 + * version 2, as published by the Free Software Foundation.
10 + *
11 + * This program is distributed in the hope it will be useful, but WITHOUT
12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 + * more details.
15 + *
16 + * You should have received a copy of the GNU General Public License along with
17 + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 + * Place - Suite 330, Boston, MA 02111-1307 USA.
19 + */
20 +
21 +#include <linux/module.h>
22 +#include <linux/init.h>
23 +#include <linux/kernel.h>
24 +#include <linux/skbuff.h>
25 +#include <linux/rtnetlink.h>
26 +#include <linux/pkt_cls.h>
27 +#include <linux/ip.h>
28 +#include <linux/ipv6.h>
29 +#include <net/netlink.h>
30 +#include <net/pkt_sched.h>
31 +#include <net/act_api.h>
32 +
33 +#include <net/netfilter/nf_conntrack.h>
34 +#include <net/netfilter/nf_conntrack_core.h>
35 +
36 +#define TCA_ACT_CONNMARK 20
37 +
38 +#define CONNMARK_TAB_MASK 3
39 +
40 +static struct tcf_hashinfo connmark_hash_info;
41 +
42 +static int tcf_connmark(struct sk_buff *skb, const struct tc_action *a,
43 + struct tcf_result *res)
44 +{
45 + struct nf_conn *c;
46 + enum ip_conntrack_info ctinfo;
47 + int proto;
48 + int r;
49 +
50 + if (skb->protocol == htons(ETH_P_IP)) {
51 + if (skb->len < sizeof(struct iphdr))
52 + goto out;
53 + proto = PF_INET;
54 + } else if (skb->protocol == htons(ETH_P_IPV6)) {
55 + if (skb->len < sizeof(struct ipv6hdr))
56 + goto out;
57 + proto = PF_INET6;
58 + } else
59 + goto out;
60 +
61 + r = nf_conntrack_in(dev_net(skb->dev), proto, NF_INET_PRE_ROUTING, skb);
62 + if (r != NF_ACCEPT)
63 + goto out;
64 +
65 + c = nf_ct_get(skb, &ctinfo);
66 + if (!c)
67 + goto out;
68 +
69 + skb->mark = c->mark;
70 + nf_conntrack_put(skb->nfct);
71 + skb->nfct = NULL;
72 +
73 +out:
74 + return TC_ACT_PIPE;
75 +}
76 +
77 +static int tcf_connmark_init(struct net *net, struct nlattr *nla,
78 + struct nlattr *est, struct tc_action *a,
79 + int ovr, int bind)
80 +{
81 + struct tcf_common *pc;
82 + int ret = 0;
83 +
84 + pc = tcf_hash_check(0, a, bind);
85 + if (!pc) {
86 + pc = tcf_hash_create(0, est, a, sizeof(*pc), bind);
87 + if (IS_ERR(pc))
88 + return PTR_ERR(pc);
89 +
90 + tcf_hash_insert(pc, &connmark_hash_info);
91 + ret = ACT_P_CREATED;
92 + } else {
93 + if (!ovr) {
94 + tcf_hash_release(pc, bind, &connmark_hash_info);
95 + return -EEXIST;
96 + }
97 + }
98 +
99 + return ret;
100 +}
101 +
102 +static inline int tcf_connmark_cleanup(struct tc_action *a, int bind)
103 +{
104 + if (a->priv)
105 + return tcf_hash_release(a->priv, bind, &connmark_hash_info);
106 + return 0;
107 +}
108 +
109 +static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
110 + int bind, int ref)
111 +{
112 + return skb->len;
113 +}
114 +
115 +static struct tc_action_ops act_connmark_ops = {
116 + .kind = "connmark",
117 + .hinfo = &connmark_hash_info,
118 + .type = TCA_ACT_CONNMARK,
119 + .owner = THIS_MODULE,
120 + .act = tcf_connmark,
121 + .dump = tcf_connmark_dump,
122 + .cleanup = tcf_connmark_cleanup,
123 + .init = tcf_connmark_init,
124 +};
125 +
126 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
127 +MODULE_DESCRIPTION("Connection tracking mark restoring");
128 +MODULE_LICENSE("GPL");
129 +
130 +static int __init connmark_init_module(void)
131 +{
132 + int ret;
133 +
134 + ret = tcf_hashinfo_init(&connmark_hash_info, CONNMARK_TAB_MASK);
135 + if (ret)
136 + return ret;
137 +
138 + return tcf_register_action(&act_connmark_ops);
139 +}
140 +
141 +static void __exit connmark_cleanup_module(void)
142 +{
143 + tcf_unregister_action(&act_connmark_ops);
144 +}
145 +
146 +module_init(connmark_init_module);
147 +module_exit(connmark_cleanup_module);
148 --- a/net/sched/Kconfig
149 +++ b/net/sched/Kconfig
150 @@ -717,6 +717,19 @@ config NET_ACT_CSUM
151 To compile this code as a module, choose M here: the
152 module will be called act_csum.
153
154 +config NET_ACT_CONNMARK
155 + tristate "Connection Tracking Marking"
156 + depends on NET_CLS_ACT
157 + depends on NF_CONNTRACK
158 + depends on NF_CONNTRACK_MARK
159 + ---help---
160 + Say Y here to restore the connmark from a scheduler action
161 +
162 + If unsure, say N.
163 +
164 + To compile this code as a module, choose M here: the
165 + module will be called act_connmark.
166 +
167 config NET_CLS_IND
168 bool "Incoming device classification"
169 depends on NET_CLS_U32 || NET_CLS_FW
170 --- a/net/sched/Makefile
171 +++ b/net/sched/Makefile
172 @@ -16,6 +16,7 @@ obj-$(CONFIG_NET_ACT_PEDIT) += act_pedit
173 obj-$(CONFIG_NET_ACT_SIMP) += act_simple.o
174 obj-$(CONFIG_NET_ACT_SKBEDIT) += act_skbedit.o
175 obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o
176 +obj-$(CONFIG_NET_ACT_CONNMARK) += act_connmark.o
177 obj-$(CONFIG_NET_SCH_FIFO) += sch_fifo.o
178 obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o
179 obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o