]>
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 | ||
c7d03a00 | 30 | static unsigned int skbedit_net_id; |
a85a970a | 31 | static struct tc_action_ops act_skbedit_ops; |
ddf97ccd | 32 | |
dc7f9f6e | 33 | static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a, |
ca9b0e27 AD |
34 | struct tcf_result *res) |
35 | { | |
a85a970a | 36 | struct tcf_skbedit *d = to_skbedit(a); |
ca9b0e27 AD |
37 | |
38 | spin_lock(&d->tcf_lock); | |
9c4a4e48 | 39 | tcf_lastuse_update(&d->tcf_tm); |
bfe0d029 | 40 | bstats_update(&d->tcf_bstats, skb); |
ca9b0e27 AD |
41 | |
42 | if (d->flags & SKBEDIT_F_PRIORITY) | |
43 | skb->priority = d->priority; | |
44 | if (d->flags & SKBEDIT_F_QUEUE_MAPPING && | |
45 | skb->dev->real_num_tx_queues > d->queue_mapping) | |
46 | skb_set_queue_mapping(skb, d->queue_mapping); | |
4fe77d82 AQ |
47 | if (d->flags & SKBEDIT_F_MARK) { |
48 | skb->mark &= ~d->mask; | |
49 | skb->mark |= d->mark & d->mask; | |
50 | } | |
ff202ee1 JHS |
51 | if (d->flags & SKBEDIT_F_PTYPE) |
52 | skb->pkt_type = d->ptype; | |
ca9b0e27 AD |
53 | |
54 | spin_unlock(&d->tcf_lock); | |
55 | return d->tcf_action; | |
56 | } | |
57 | ||
58 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { | |
59 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, | |
60 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, | |
61 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, | |
1c55d62e | 62 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
ff202ee1 | 63 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
4fe77d82 | 64 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
ca9b0e27 AD |
65 | }; |
66 | ||
c1b52739 | 67 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
a85a970a | 68 | struct nlattr *est, struct tc_action **a, |
c1b52739 | 69 | int ovr, int bind) |
ca9b0e27 | 70 | { |
ddf97ccd | 71 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
ca9b0e27 AD |
72 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
73 | struct tc_skbedit *parm; | |
74 | struct tcf_skbedit *d; | |
4fe77d82 | 75 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
ff202ee1 | 76 | u16 *queue_mapping = NULL, *ptype = NULL; |
b2313077 WC |
77 | bool exists = false; |
78 | int ret = 0, err; | |
ca9b0e27 AD |
79 | |
80 | if (nla == NULL) | |
81 | return -EINVAL; | |
82 | ||
fceb6435 | 83 | err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL); |
ca9b0e27 AD |
84 | if (err < 0) |
85 | return err; | |
86 | ||
87 | if (tb[TCA_SKBEDIT_PARMS] == NULL) | |
88 | return -EINVAL; | |
89 | ||
90 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
91 | flags |= SKBEDIT_F_PRIORITY; | |
92 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); | |
93 | } | |
94 | ||
95 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
96 | flags |= SKBEDIT_F_QUEUE_MAPPING; | |
97 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); | |
98 | } | |
1c55d62e | 99 | |
ff202ee1 JHS |
100 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
101 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); | |
102 | if (!skb_pkt_type_ok(*ptype)) | |
103 | return -EINVAL; | |
104 | flags |= SKBEDIT_F_PTYPE; | |
105 | } | |
106 | ||
1c55d62e | 107 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
108 | flags |= SKBEDIT_F_MARK; | |
109 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); | |
110 | } | |
111 | ||
4fe77d82 AQ |
112 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
113 | flags |= SKBEDIT_F_MASK; | |
114 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); | |
115 | } | |
116 | ||
ca9b0e27 AD |
117 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
118 | ||
65a206c0 | 119 | exists = tcf_idr_check(tn, parm->index, a, bind); |
5e1567ae JHS |
120 | if (exists && bind) |
121 | return 0; | |
122 | ||
123 | if (!flags) { | |
f5320d86 RM |
124 | if (exists) |
125 | tcf_idr_release(*a, bind); | |
5e1567ae JHS |
126 | return -EINVAL; |
127 | } | |
128 | ||
129 | if (!exists) { | |
65a206c0 CM |
130 | ret = tcf_idr_create(tn, parm->index, est, a, |
131 | &act_skbedit_ops, bind, false); | |
86062033 WC |
132 | if (ret) |
133 | return ret; | |
ca9b0e27 | 134 | |
a85a970a | 135 | d = to_skbedit(*a); |
ca9b0e27 AD |
136 | ret = ACT_P_CREATED; |
137 | } else { | |
a85a970a | 138 | d = to_skbedit(*a); |
65a206c0 | 139 | tcf_idr_release(*a, bind); |
1a29321e | 140 | if (!ovr) |
ca9b0e27 | 141 | return -EEXIST; |
ca9b0e27 AD |
142 | } |
143 | ||
144 | spin_lock_bh(&d->tcf_lock); | |
145 | ||
146 | d->flags = flags; | |
147 | if (flags & SKBEDIT_F_PRIORITY) | |
148 | d->priority = *priority; | |
149 | if (flags & SKBEDIT_F_QUEUE_MAPPING) | |
150 | d->queue_mapping = *queue_mapping; | |
1c55d62e | 151 | if (flags & SKBEDIT_F_MARK) |
152 | d->mark = *mark; | |
ff202ee1 JHS |
153 | if (flags & SKBEDIT_F_PTYPE) |
154 | d->ptype = *ptype; | |
4fe77d82 AQ |
155 | /* default behaviour is to use all the bits */ |
156 | d->mask = 0xffffffff; | |
157 | if (flags & SKBEDIT_F_MASK) | |
158 | d->mask = *mask; | |
1c55d62e | 159 | |
ca9b0e27 AD |
160 | d->tcf_action = parm->action; |
161 | ||
162 | spin_unlock_bh(&d->tcf_lock); | |
163 | ||
164 | if (ret == ACT_P_CREATED) | |
65a206c0 | 165 | tcf_idr_insert(tn, *a); |
ca9b0e27 AD |
166 | return ret; |
167 | } | |
168 | ||
cc7ec456 ED |
169 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
170 | int bind, int ref) | |
ca9b0e27 AD |
171 | { |
172 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 173 | struct tcf_skbedit *d = to_skbedit(a); |
1c40be12 ED |
174 | struct tc_skbedit opt = { |
175 | .index = d->tcf_index, | |
176 | .refcnt = d->tcf_refcnt - ref, | |
177 | .bindcnt = d->tcf_bindcnt - bind, | |
178 | .action = d->tcf_action, | |
179 | }; | |
ca9b0e27 AD |
180 | struct tcf_t t; |
181 | ||
1b34ec43 DM |
182 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
183 | goto nla_put_failure; | |
184 | if ((d->flags & SKBEDIT_F_PRIORITY) && | |
61cc535d | 185 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority)) |
1b34ec43 DM |
186 | goto nla_put_failure; |
187 | if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) && | |
61cc535d | 188 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping)) |
1b34ec43 DM |
189 | goto nla_put_failure; |
190 | if ((d->flags & SKBEDIT_F_MARK) && | |
61cc535d | 191 | nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark)) |
1b34ec43 | 192 | goto nla_put_failure; |
ff202ee1 | 193 | if ((d->flags & SKBEDIT_F_PTYPE) && |
61cc535d | 194 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype)) |
ff202ee1 | 195 | goto nla_put_failure; |
4fe77d82 AQ |
196 | if ((d->flags & SKBEDIT_F_MASK) && |
197 | nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask)) | |
198 | goto nla_put_failure; | |
48d8ee16 JHS |
199 | |
200 | tcf_tm_dump(&t, &d->tcf_tm); | |
9854518e | 201 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
1b34ec43 | 202 | goto nla_put_failure; |
ca9b0e27 AD |
203 | return skb->len; |
204 | ||
205 | nla_put_failure: | |
206 | nlmsg_trim(skb, b); | |
207 | return -1; | |
208 | } | |
209 | ||
ddf97ccd WC |
210 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
211 | struct netlink_callback *cb, int type, | |
a85a970a | 212 | const struct tc_action_ops *ops) |
ddf97ccd WC |
213 | { |
214 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
215 | ||
a85a970a | 216 | return tcf_generic_walker(tn, skb, cb, type, ops); |
ddf97ccd WC |
217 | } |
218 | ||
a85a970a | 219 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
220 | { |
221 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
222 | ||
65a206c0 | 223 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
224 | } |
225 | ||
ca9b0e27 AD |
226 | static struct tc_action_ops act_skbedit_ops = { |
227 | .kind = "skbedit", | |
ca9b0e27 | 228 | .type = TCA_ACT_SKBEDIT, |
ca9b0e27 AD |
229 | .owner = THIS_MODULE, |
230 | .act = tcf_skbedit, | |
231 | .dump = tcf_skbedit_dump, | |
ca9b0e27 | 232 | .init = tcf_skbedit_init, |
ddf97ccd WC |
233 | .walk = tcf_skbedit_walker, |
234 | .lookup = tcf_skbedit_search, | |
a85a970a | 235 | .size = sizeof(struct tcf_skbedit), |
ddf97ccd WC |
236 | }; |
237 | ||
238 | static __net_init int skbedit_init_net(struct net *net) | |
239 | { | |
240 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
241 | ||
8f4d0354 | 242 | return tc_action_net_init(net, tn, &act_skbedit_ops); |
ddf97ccd WC |
243 | } |
244 | ||
245 | static void __net_exit skbedit_exit_net(struct net *net) | |
246 | { | |
247 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
248 | ||
249 | tc_action_net_exit(tn); | |
250 | } | |
251 | ||
252 | static struct pernet_operations skbedit_net_ops = { | |
253 | .init = skbedit_init_net, | |
254 | .exit = skbedit_exit_net, | |
255 | .id = &skbedit_net_id, | |
256 | .size = sizeof(struct tc_action_net), | |
ca9b0e27 AD |
257 | }; |
258 | ||
259 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); | |
260 | MODULE_DESCRIPTION("SKB Editing"); | |
261 | MODULE_LICENSE("GPL"); | |
262 | ||
263 | static int __init skbedit_init_module(void) | |
264 | { | |
ddf97ccd | 265 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
266 | } |
267 | ||
268 | static void __exit skbedit_cleanup_module(void) | |
269 | { | |
ddf97ccd | 270 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
271 | } |
272 | ||
273 | module_init(skbedit_init_module); | |
274 | module_exit(skbedit_cleanup_module); |