Commit | Line | Data |
---|---|---|
86da71b5 JHS |
1 | /* |
2 | * net/sched/act_skbmod.c skb data modifier | |
3 | * | |
4 | * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/rtnetlink.h> | |
17 | #include <net/netlink.h> | |
18 | #include <net/pkt_sched.h> | |
19 | ||
20 | #include <linux/tc_act/tc_skbmod.h> | |
21 | #include <net/tc_act/tc_skbmod.h> | |
22 | ||
c7d03a00 | 23 | static unsigned int skbmod_net_id; |
86da71b5 JHS |
24 | static struct tc_action_ops act_skbmod_ops; |
25 | ||
26 | #define MAX_EDIT_LEN ETH_HLEN | |
27 | static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a, | |
28 | struct tcf_result *res) | |
29 | { | |
30 | struct tcf_skbmod *d = to_skbmod(a); | |
31 | int action; | |
32 | struct tcf_skbmod_params *p; | |
33 | u64 flags; | |
34 | int err; | |
35 | ||
36 | tcf_lastuse_update(&d->tcf_tm); | |
37 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); | |
38 | ||
39 | /* XXX: if you are going to edit more fields beyond ethernet header | |
40 | * (example when you add IP header replacement or vlan swap) | |
41 | * then MAX_EDIT_LEN needs to change appropriately | |
42 | */ | |
43 | err = skb_ensure_writable(skb, MAX_EDIT_LEN); | |
44 | if (unlikely(err)) { /* best policy is to drop on the floor */ | |
45 | qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats)); | |
46 | return TC_ACT_SHOT; | |
47 | } | |
48 | ||
49 | rcu_read_lock(); | |
50 | action = READ_ONCE(d->tcf_action); | |
51 | if (unlikely(action == TC_ACT_SHOT)) { | |
52 | qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats)); | |
53 | rcu_read_unlock(); | |
54 | return action; | |
55 | } | |
56 | ||
57 | p = rcu_dereference(d->skbmod_p); | |
58 | flags = p->flags; | |
59 | if (flags & SKBMOD_F_DMAC) | |
60 | ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst); | |
61 | if (flags & SKBMOD_F_SMAC) | |
62 | ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src); | |
63 | if (flags & SKBMOD_F_ETYPE) | |
64 | eth_hdr(skb)->h_proto = p->eth_type; | |
65 | rcu_read_unlock(); | |
66 | ||
67 | if (flags & SKBMOD_F_SWAPMAC) { | |
68 | u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */ | |
69 | /*XXX: I am sure we can come up with more efficient swapping*/ | |
70 | ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest); | |
71 | ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source); | |
72 | ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr); | |
73 | } | |
74 | ||
75 | return action; | |
76 | } | |
77 | ||
78 | static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = { | |
79 | [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) }, | |
80 | [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN }, | |
81 | [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN }, | |
82 | [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 }, | |
83 | }; | |
84 | ||
85 | static int tcf_skbmod_init(struct net *net, struct nlattr *nla, | |
86 | struct nlattr *est, struct tc_action **a, | |
87 | int ovr, int bind) | |
88 | { | |
89 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
90 | struct nlattr *tb[TCA_SKBMOD_MAX + 1]; | |
91 | struct tcf_skbmod_params *p, *p_old; | |
92 | struct tc_skbmod *parm; | |
93 | struct tcf_skbmod *d; | |
94 | bool exists = false; | |
95 | u8 *daddr = NULL; | |
96 | u8 *saddr = NULL; | |
97 | u16 eth_type = 0; | |
98 | u32 lflags = 0; | |
99 | int ret = 0, err; | |
100 | ||
101 | if (!nla) | |
102 | return -EINVAL; | |
103 | ||
fceb6435 | 104 | err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL); |
86da71b5 JHS |
105 | if (err < 0) |
106 | return err; | |
107 | ||
108 | if (!tb[TCA_SKBMOD_PARMS]) | |
109 | return -EINVAL; | |
110 | ||
111 | if (tb[TCA_SKBMOD_DMAC]) { | |
112 | daddr = nla_data(tb[TCA_SKBMOD_DMAC]); | |
113 | lflags |= SKBMOD_F_DMAC; | |
114 | } | |
115 | ||
116 | if (tb[TCA_SKBMOD_SMAC]) { | |
117 | saddr = nla_data(tb[TCA_SKBMOD_SMAC]); | |
118 | lflags |= SKBMOD_F_SMAC; | |
119 | } | |
120 | ||
121 | if (tb[TCA_SKBMOD_ETYPE]) { | |
122 | eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]); | |
123 | lflags |= SKBMOD_F_ETYPE; | |
124 | } | |
125 | ||
126 | parm = nla_data(tb[TCA_SKBMOD_PARMS]); | |
127 | if (parm->flags & SKBMOD_F_SWAPMAC) | |
128 | lflags = SKBMOD_F_SWAPMAC; | |
129 | ||
65a206c0 | 130 | exists = tcf_idr_check(tn, parm->index, a, bind); |
86da71b5 JHS |
131 | if (exists && bind) |
132 | return 0; | |
133 | ||
134 | if (!lflags) | |
135 | return -EINVAL; | |
136 | ||
137 | if (!exists) { | |
65a206c0 CM |
138 | ret = tcf_idr_create(tn, parm->index, est, a, |
139 | &act_skbmod_ops, bind, true); | |
86da71b5 JHS |
140 | if (ret) |
141 | return ret; | |
142 | ||
143 | ret = ACT_P_CREATED; | |
144 | } else { | |
65a206c0 | 145 | tcf_idr_release(*a, bind); |
86da71b5 JHS |
146 | if (!ovr) |
147 | return -EEXIST; | |
148 | } | |
149 | ||
150 | d = to_skbmod(*a); | |
151 | ||
152 | ASSERT_RTNL(); | |
153 | p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL); | |
154 | if (unlikely(!p)) { | |
155 | if (ovr) | |
65a206c0 | 156 | tcf_idr_release(*a, bind); |
86da71b5 JHS |
157 | return -ENOMEM; |
158 | } | |
159 | ||
160 | p->flags = lflags; | |
161 | d->tcf_action = parm->action; | |
162 | ||
163 | p_old = rtnl_dereference(d->skbmod_p); | |
164 | ||
165 | if (ovr) | |
166 | spin_lock_bh(&d->tcf_lock); | |
167 | ||
168 | if (lflags & SKBMOD_F_DMAC) | |
169 | ether_addr_copy(p->eth_dst, daddr); | |
170 | if (lflags & SKBMOD_F_SMAC) | |
171 | ether_addr_copy(p->eth_src, saddr); | |
172 | if (lflags & SKBMOD_F_ETYPE) | |
173 | p->eth_type = htons(eth_type); | |
174 | ||
175 | rcu_assign_pointer(d->skbmod_p, p); | |
176 | if (ovr) | |
177 | spin_unlock_bh(&d->tcf_lock); | |
178 | ||
179 | if (p_old) | |
180 | kfree_rcu(p_old, rcu); | |
181 | ||
182 | if (ret == ACT_P_CREATED) | |
65a206c0 | 183 | tcf_idr_insert(tn, *a); |
86da71b5 JHS |
184 | return ret; |
185 | } | |
186 | ||
9a63b255 | 187 | static void tcf_skbmod_cleanup(struct tc_action *a) |
86da71b5 JHS |
188 | { |
189 | struct tcf_skbmod *d = to_skbmod(a); | |
190 | struct tcf_skbmod_params *p; | |
191 | ||
192 | p = rcu_dereference_protected(d->skbmod_p, 1); | |
2d433610 DC |
193 | if (p) |
194 | kfree_rcu(p, rcu); | |
86da71b5 JHS |
195 | } |
196 | ||
197 | static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a, | |
198 | int bind, int ref) | |
199 | { | |
200 | struct tcf_skbmod *d = to_skbmod(a); | |
201 | unsigned char *b = skb_tail_pointer(skb); | |
202 | struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p); | |
203 | struct tc_skbmod opt = { | |
204 | .index = d->tcf_index, | |
205 | .refcnt = d->tcf_refcnt - ref, | |
206 | .bindcnt = d->tcf_bindcnt - bind, | |
207 | .action = d->tcf_action, | |
208 | }; | |
209 | struct tcf_t t; | |
210 | ||
211 | opt.flags = p->flags; | |
212 | if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt)) | |
213 | goto nla_put_failure; | |
214 | if ((p->flags & SKBMOD_F_DMAC) && | |
215 | nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst)) | |
216 | goto nla_put_failure; | |
217 | if ((p->flags & SKBMOD_F_SMAC) && | |
218 | nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src)) | |
219 | goto nla_put_failure; | |
220 | if ((p->flags & SKBMOD_F_ETYPE) && | |
221 | nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type))) | |
222 | goto nla_put_failure; | |
223 | ||
224 | tcf_tm_dump(&t, &d->tcf_tm); | |
225 | if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD)) | |
226 | goto nla_put_failure; | |
227 | ||
228 | return skb->len; | |
229 | nla_put_failure: | |
86da71b5 JHS |
230 | nlmsg_trim(skb, b); |
231 | return -1; | |
232 | } | |
233 | ||
234 | static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb, | |
235 | struct netlink_callback *cb, int type, | |
236 | const struct tc_action_ops *ops) | |
237 | { | |
238 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
239 | ||
240 | return tcf_generic_walker(tn, skb, cb, type, ops); | |
241 | } | |
242 | ||
243 | static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index) | |
244 | { | |
245 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
246 | ||
65a206c0 | 247 | return tcf_idr_search(tn, a, index); |
86da71b5 JHS |
248 | } |
249 | ||
250 | static struct tc_action_ops act_skbmod_ops = { | |
251 | .kind = "skbmod", | |
252 | .type = TCA_ACT_SKBMOD, | |
253 | .owner = THIS_MODULE, | |
254 | .act = tcf_skbmod_run, | |
255 | .dump = tcf_skbmod_dump, | |
256 | .init = tcf_skbmod_init, | |
257 | .cleanup = tcf_skbmod_cleanup, | |
258 | .walk = tcf_skbmod_walker, | |
259 | .lookup = tcf_skbmod_search, | |
260 | .size = sizeof(struct tcf_skbmod), | |
261 | }; | |
262 | ||
263 | static __net_init int skbmod_init_net(struct net *net) | |
264 | { | |
265 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
266 | ||
c7e460ce | 267 | return tc_action_net_init(tn, &act_skbmod_ops); |
86da71b5 JHS |
268 | } |
269 | ||
039af9c6 | 270 | static void __net_exit skbmod_exit_net(struct list_head *net_list) |
86da71b5 | 271 | { |
039af9c6 | 272 | tc_action_net_exit(net_list, skbmod_net_id); |
86da71b5 JHS |
273 | } |
274 | ||
275 | static struct pernet_operations skbmod_net_ops = { | |
276 | .init = skbmod_init_net, | |
039af9c6 | 277 | .exit_batch = skbmod_exit_net, |
86da71b5 JHS |
278 | .id = &skbmod_net_id, |
279 | .size = sizeof(struct tc_action_net), | |
280 | }; | |
281 | ||
282 | MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>"); | |
283 | MODULE_DESCRIPTION("SKB data mod-ing"); | |
284 | MODULE_LICENSE("GPL"); | |
285 | ||
286 | static int __init skbmod_init_module(void) | |
287 | { | |
288 | return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops); | |
289 | } | |
290 | ||
291 | static void __exit skbmod_cleanup_module(void) | |
292 | { | |
293 | tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops); | |
294 | } | |
295 | ||
296 | module_init(skbmod_init_module); | |
297 | module_exit(skbmod_cleanup_module); |