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