]>
Commit | Line | Data |
---|---|---|
ca9b0e27 AD |
1 | /* |
2 | * Copyright (c) 2008, Intel Corporation. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
c057b190 | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
ca9b0e27 AD |
15 | * |
16 | * Author: Alexander Duyck <alexander.h.duyck@intel.com> | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/skbuff.h> | |
23 | #include <linux/rtnetlink.h> | |
24 | #include <net/netlink.h> | |
25 | #include <net/pkt_sched.h> | |
26 | ||
27 | #include <linux/tc_act/tc_skbedit.h> | |
28 | #include <net/tc_act/tc_skbedit.h> | |
29 | ||
30 | #define SKBEDIT_TAB_MASK 15 | |
ca9b0e27 | 31 | |
c7d03a00 | 32 | static unsigned int skbedit_net_id; |
a85a970a | 33 | static struct tc_action_ops act_skbedit_ops; |
ddf97ccd | 34 | |
dc7f9f6e | 35 | static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, |
ca9b0e27 AD |
36 | struct tcf_result *res) |
37 | { | |
a85a970a | 38 | struct tcf_skbedit *d = to_skbedit(a); |
ca9b0e27 AD |
39 | |
40 | spin_lock(&d->tcf_lock); | |
9c4a4e48 | 41 | tcf_lastuse_update(&d->tcf_tm); |
bfe0d029 | 42 | bstats_update(&d->tcf_bstats, skb); |
ca9b0e27 AD |
43 | |
44 | if (d->flags & SKBEDIT_F_PRIORITY) | |
45 | skb->priority = d->priority; | |
46 | if (d->flags & SKBEDIT_F_QUEUE_MAPPING && | |
47 | skb->dev->real_num_tx_queues > d->queue_mapping) | |
48 | skb_set_queue_mapping(skb, d->queue_mapping); | |
4fe77d82 AQ |
49 | if (d->flags & SKBEDIT_F_MARK) { |
50 | skb->mark &= ~d->mask; | |
51 | skb->mark |= d->mark & d->mask; | |
52 | } | |
ff202ee1 JHS |
53 | if (d->flags & SKBEDIT_F_PTYPE) |
54 | skb->pkt_type = d->ptype; | |
ca9b0e27 AD |
55 | |
56 | spin_unlock(&d->tcf_lock); | |
57 | return d->tcf_action; | |
58 | } | |
59 | ||
60 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { | |
61 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, | |
62 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, | |
63 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, | |
1c55d62e | 64 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
ff202ee1 | 65 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
4fe77d82 | 66 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
ca9b0e27 AD |
67 | }; |
68 | ||
c1b52739 | 69 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
a85a970a | 70 | struct nlattr *est, struct tc_action **a, |
c1b52739 | 71 | int ovr, int bind) |
ca9b0e27 | 72 | { |
ddf97ccd | 73 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
ca9b0e27 AD |
74 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
75 | struct tc_skbedit *parm; | |
76 | struct tcf_skbedit *d; | |
4fe77d82 | 77 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
ff202ee1 | 78 | u16 *queue_mapping = NULL, *ptype = NULL; |
b2313077 WC |
79 | bool exists = false; |
80 | int ret = 0, err; | |
ca9b0e27 AD |
81 | |
82 | if (nla == NULL) | |
83 | return -EINVAL; | |
84 | ||
fceb6435 | 85 | err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); |
ca9b0e27 AD |
86 | if (err < 0) |
87 | return err; | |
88 | ||
89 | if (tb[TCA_SKBEDIT_PARMS] == NULL) | |
90 | return -EINVAL; | |
91 | ||
92 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
93 | flags |= SKBEDIT_F_PRIORITY; | |
94 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); | |
95 | } | |
96 | ||
97 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
98 | flags |= SKBEDIT_F_QUEUE_MAPPING; | |
99 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); | |
100 | } | |
1c55d62e | 101 | |
ff202ee1 JHS |
102 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
103 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); | |
104 | if (!skb_pkt_type_ok(*ptype)) | |
105 | return -EINVAL; | |
106 | flags |= SKBEDIT_F_PTYPE; | |
107 | } | |
108 | ||
1c55d62e | 109 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
110 | flags |= SKBEDIT_F_MARK; | |
111 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); | |
112 | } | |
113 | ||
4fe77d82 AQ |
114 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
115 | flags |= SKBEDIT_F_MASK; | |
116 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); | |
117 | } | |
118 | ||
ca9b0e27 AD |
119 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
120 | ||
5e1567ae JHS |
121 | exists = tcf_hash_check(tn, parm->index, a, bind); |
122 | if (exists && bind) | |
123 | return 0; | |
124 | ||
125 | if (!flags) { | |
a85a970a | 126 | tcf_hash_release(*a, bind); |
5e1567ae JHS |
127 | return -EINVAL; |
128 | } | |
129 | ||
130 | if (!exists) { | |
ddf97ccd | 131 | ret = tcf_hash_create(tn, parm->index, est, a, |
a85a970a | 132 | &act_skbedit_ops, bind, false); |
86062033 WC |
133 | if (ret) |
134 | return ret; | |
ca9b0e27 | 135 | |
a85a970a | 136 | d = to_skbedit(*a); |
ca9b0e27 AD |
137 | ret = ACT_P_CREATED; |
138 | } else { | |
a85a970a WC |
139 | d = to_skbedit(*a); |
140 | tcf_hash_release(*a, bind); | |
1a29321e | 141 | if (!ovr) |
ca9b0e27 | 142 | return -EEXIST; |
ca9b0e27 AD |
143 | } |
144 | ||
145 | spin_lock_bh(&d->tcf_lock); | |
146 | ||
147 | d->flags = flags; | |
148 | if (flags & SKBEDIT_F_PRIORITY) | |
149 | d->priority = *priority; | |
150 | if (flags & SKBEDIT_F_QUEUE_MAPPING) | |
151 | d->queue_mapping = *queue_mapping; | |
1c55d62e | 152 | if (flags & SKBEDIT_F_MARK) |
153 | d->mark = *mark; | |
ff202ee1 JHS |
154 | if (flags & SKBEDIT_F_PTYPE) |
155 | d->ptype = *ptype; | |
4fe77d82 AQ |
156 | /* default behaviour is to use all the bits */ |
157 | d->mask = 0xffffffff; | |
158 | if (flags & SKBEDIT_F_MASK) | |
159 | d->mask = *mask; | |
1c55d62e | 160 | |
ca9b0e27 AD |
161 | d->tcf_action = parm->action; |
162 | ||
163 | spin_unlock_bh(&d->tcf_lock); | |
164 | ||
165 | if (ret == ACT_P_CREATED) | |
a85a970a | 166 | tcf_hash_insert(tn, *a); |
ca9b0e27 AD |
167 | return ret; |
168 | } | |
169 | ||
cc7ec456 ED |
170 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
171 | int bind, int ref) | |
ca9b0e27 AD |
172 | { |
173 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 174 | struct tcf_skbedit *d = to_skbedit(a); |
1c40be12 ED |
175 | struct tc_skbedit opt = { |
176 | .index = d->tcf_index, | |
177 | .refcnt = d->tcf_refcnt - ref, | |
178 | .bindcnt = d->tcf_bindcnt - bind, | |
179 | .action = d->tcf_action, | |
180 | }; | |
ca9b0e27 AD |
181 | struct tcf_t t; |
182 | ||
1b34ec43 DM |
183 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
184 | goto nla_put_failure; | |
185 | if ((d->flags & SKBEDIT_F_PRIORITY) && | |
61cc535d | 186 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority)) |
1b34ec43 DM |
187 | goto nla_put_failure; |
188 | if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && | |
61cc535d | 189 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping)) |
1b34ec43 DM |
190 | goto nla_put_failure; |
191 | if ((d->flags & SKBEDIT_F_MARK) && | |
61cc535d | 192 | nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark)) |
1b34ec43 | 193 | goto nla_put_failure; |
ff202ee1 | 194 | if ((d->flags & SKBEDIT_F_PTYPE) && |
61cc535d | 195 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype)) |
ff202ee1 | 196 | goto nla_put_failure; |
4fe77d82 AQ |
197 | if ((d->flags & SKBEDIT_F_MASK) && |
198 | nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask)) | |
199 | goto nla_put_failure; | |
48d8ee16 JHS |
200 | |
201 | tcf_tm_dump(&t, &d->tcf_tm); | |
9854518e | 202 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
1b34ec43 | 203 | goto nla_put_failure; |
ca9b0e27 AD |
204 | return skb->len; |
205 | ||
206 | nla_put_failure: | |
207 | nlmsg_trim(skb, b); | |
208 | return -1; | |
209 | } | |
210 | ||
ddf97ccd WC |
211 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
212 | struct netlink_callback *cb, int type, | |
a85a970a | 213 | const struct tc_action_ops *ops) |
ddf97ccd WC |
214 | { |
215 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
216 | ||
a85a970a | 217 | return tcf_generic_walker(tn, skb, cb, type, ops); |
ddf97ccd WC |
218 | } |
219 | ||
a85a970a | 220 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
221 | { |
222 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
223 | ||
224 | return tcf_hash_search(tn, a, index); | |
225 | } | |
226 | ||
ca9b0e27 AD |
227 | static struct tc_action_ops act_skbedit_ops = { |
228 | .kind = "skbedit", | |
ca9b0e27 | 229 | .type = TCA_ACT_SKBEDIT, |
ca9b0e27 AD |
230 | .owner = THIS_MODULE, |
231 | .act = tcf_skbedit, | |
232 | .dump = tcf_skbedit_dump, | |
ca9b0e27 | 233 | .init = tcf_skbedit_init, |
ddf97ccd WC |
234 | .walk = tcf_skbedit_walker, |
235 | .lookup = tcf_skbedit_search, | |
a85a970a | 236 | .size = sizeof(struct tcf_skbedit), |
ddf97ccd WC |
237 | }; |
238 | ||
239 | static __net_init int skbedit_init_net(struct net *net) | |
240 | { | |
241 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
242 | ||
243 | return tc_action_net_init(tn, &act_skbedit_ops, SKBEDIT_TAB_MASK); | |
244 | } | |
245 | ||
246 | static void __net_exit skbedit_exit_net(struct net *net) | |
247 | { | |
248 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
249 | ||
250 | tc_action_net_exit(tn); | |
251 | } | |
252 | ||
253 | static struct pernet_operations skbedit_net_ops = { | |
254 | .init = skbedit_init_net, | |
255 | .exit = skbedit_exit_net, | |
256 | .id = &skbedit_net_id, | |
257 | .size = sizeof(struct tc_action_net), | |
ca9b0e27 AD |
258 | }; |
259 | ||
260 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); | |
261 | MODULE_DESCRIPTION("SKB Editing"); | |
262 | MODULE_LICENSE("GPL"); | |
263 | ||
264 | static int __init skbedit_init_module(void) | |
265 | { | |
ddf97ccd | 266 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
267 | } |
268 | ||
269 | static void __exit skbedit_cleanup_module(void) | |
270 | { | |
ddf97ccd | 271 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
272 | } |
273 | ||
274 | module_init(skbedit_init_module); | |
275 | module_exit(skbedit_cleanup_module); |