1 /*
2 * net/sched/act_mirred.c packet mirroring and redirect actions
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jamal Hadi Salim (2002-4)
10 *
11 * TODO: Add ingress support (and socket redirect support)
12 *
13 */
14
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/gfp.h>
24 #include <linux/if_arp.h>
25 #include <net/net_namespace.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
28 #include <net/pkt_cls.h>
29 #include <linux/tc_act/tc_mirred.h>
30 #include <net/tc_act/tc_mirred.h>
31
32 static LIST_HEAD(mirred_list);
33 static DEFINE_SPINLOCK(mirred_list_lock);
34
tcf_mirred_is_act_redirect(int action)35 static bool tcf_mirred_is_act_redirect(int action)
36 {
37 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
38 }
39
tcf_mirred_act_wants_ingress(int action)40 static bool tcf_mirred_act_wants_ingress(int action)
41 {
42 switch (action) {
43 case TCA_EGRESS_REDIR:
44 case TCA_EGRESS_MIRROR:
45 return false;
46 case TCA_INGRESS_REDIR:
47 case TCA_INGRESS_MIRROR:
48 return true;
49 default:
50 BUG();
51 }
52 }
53
tcf_mirred_can_reinsert(int action)54 static bool tcf_mirred_can_reinsert(int action)
55 {
56 switch (action) {
57 case TC_ACT_SHOT:
58 case TC_ACT_STOLEN:
59 case TC_ACT_QUEUED:
60 case TC_ACT_TRAP:
61 return true;
62 }
63 return false;
64 }
65
tcf_mirred_dev_dereference(struct tcf_mirred * m)66 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
67 {
68 return rcu_dereference_protected(m->tcfm_dev,
69 lockdep_is_held(&m->tcf_lock));
70 }
71
tcf_mirred_release(struct tc_action * a)72 static void tcf_mirred_release(struct tc_action *a)
73 {
74 struct tcf_mirred *m = to_mirred(a);
75 struct net_device *dev;
76
77 spin_lock(&mirred_list_lock);
78 list_del(&m->tcfm_list);
79 spin_unlock(&mirred_list_lock);
80
81 /* last reference to action, no need to lock */
82 dev = rcu_dereference_protected(m->tcfm_dev, 1);
83 if (dev)
84 dev_put(dev);
85 }
86
87 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
88 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
89 };
90
91 static unsigned int mirred_net_id;
92 static struct tc_action_ops act_mirred_ops;
93
tcf_mirred_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,int ovr,int bind,bool rtnl_held,struct netlink_ext_ack * extack)94 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
95 struct nlattr *est, struct tc_action **a,
96 int ovr, int bind, bool rtnl_held,
97 struct netlink_ext_ack *extack)
98 {
99 struct tc_action_net *tn = net_generic(net, mirred_net_id);
100 struct nlattr *tb[TCA_MIRRED_MAX + 1];
101 bool mac_header_xmit = false;
102 struct tc_mirred *parm;
103 struct tcf_mirred *m;
104 struct net_device *dev;
105 bool exists = false;
106 int ret, err;
107
108 if (!nla) {
109 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
110 return -EINVAL;
111 }
112 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
113 if (ret < 0)
114 return ret;
115 if (!tb[TCA_MIRRED_PARMS]) {
116 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
117 return -EINVAL;
118 }
119 parm = nla_data(tb[TCA_MIRRED_PARMS]);
120
121 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
122 if (err < 0)
123 return err;
124 exists = err;
125 if (exists && bind)
126 return 0;
127
128 switch (parm->eaction) {
129 case TCA_EGRESS_MIRROR:
130 case TCA_EGRESS_REDIR:
131 case TCA_INGRESS_REDIR:
132 case TCA_INGRESS_MIRROR:
133 break;
134 default:
135 if (exists)
136 tcf_idr_release(*a, bind);
137 else
138 tcf_idr_cleanup(tn, parm->index);
139 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
140 return -EINVAL;
141 }
142
143 if (!exists) {
144 if (!parm->ifindex) {
145 tcf_idr_cleanup(tn, parm->index);
146 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147 return -EINVAL;
148 }
149 ret = tcf_idr_create(tn, parm->index, est, a,
150 &act_mirred_ops, bind, true);
151 if (ret) {
152 tcf_idr_cleanup(tn, parm->index);
153 return ret;
154 }
155 ret = ACT_P_CREATED;
156 } else if (!ovr) {
157 tcf_idr_release(*a, bind);
158 return -EEXIST;
159 }
160 m = to_mirred(*a);
161
162 spin_lock_bh(&m->tcf_lock);
163 m->tcf_action = parm->action;
164 m->tcfm_eaction = parm->eaction;
165
166 if (parm->ifindex) {
167 dev = dev_get_by_index(net, parm->ifindex);
168 if (!dev) {
169 spin_unlock_bh(&m->tcf_lock);
170 tcf_idr_release(*a, bind);
171 return -ENODEV;
172 }
173 mac_header_xmit = dev_is_mac_header_xmit(dev);
174 rcu_swap_protected(m->tcfm_dev, dev,
175 lockdep_is_held(&m->tcf_lock));
176 if (dev)
177 dev_put(dev);
178 m->tcfm_mac_header_xmit = mac_header_xmit;
179 }
180 spin_unlock_bh(&m->tcf_lock);
181
182 if (ret == ACT_P_CREATED) {
183 spin_lock(&mirred_list_lock);
184 list_add(&m->tcfm_list, &mirred_list);
185 spin_unlock(&mirred_list_lock);
186
187 tcf_idr_insert(tn, *a);
188 }
189
190 return ret;
191 }
192
tcf_mirred_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)193 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
194 struct tcf_result *res)
195 {
196 struct tcf_mirred *m = to_mirred(a);
197 struct sk_buff *skb2 = skb;
198 bool m_mac_header_xmit;
199 struct net_device *dev;
200 int retval, err = 0;
201 bool use_reinsert;
202 bool want_ingress;
203 bool is_redirect;
204 int m_eaction;
205 int mac_len;
206
207 tcf_lastuse_update(&m->tcf_tm);
208 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
209
210 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
211 m_eaction = READ_ONCE(m->tcfm_eaction);
212 retval = READ_ONCE(m->tcf_action);
213 dev = rcu_dereference_bh(m->tcfm_dev);
214 if (unlikely(!dev)) {
215 pr_notice_once("tc mirred: target device is gone\n");
216 goto out;
217 }
218
219 if (unlikely(!(dev->flags & IFF_UP))) {
220 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
221 dev->name);
222 goto out;
223 }
224
225 /* we could easily avoid the clone only if called by ingress and clsact;
226 * since we can't easily detect the clsact caller, skip clone only for
227 * ingress - that covers the TC S/W datapath.
228 */
229 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
230 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
231 tcf_mirred_can_reinsert(retval);
232 if (!use_reinsert) {
233 skb2 = skb_clone(skb, GFP_ATOMIC);
234 if (!skb2)
235 goto out;
236 }
237
238 /* If action's target direction differs than filter's direction,
239 * and devices expect a mac header on xmit, then mac push/pull is
240 * needed.
241 */
242 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
243 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
244 if (!skb_at_tc_ingress(skb)) {
245 /* caught at egress, act ingress: pull mac */
246 mac_len = skb_network_header(skb) - skb_mac_header(skb);
247 skb_pull_rcsum(skb2, mac_len);
248 } else {
249 /* caught at ingress, act egress: push mac */
250 skb_push_rcsum(skb2, skb->mac_len);
251 }
252 }
253
254 skb2->skb_iif = skb->dev->ifindex;
255 skb2->dev = dev;
256
257 /* mirror is always swallowed */
258 if (is_redirect) {
259 skb2->tc_redirected = 1;
260 skb2->tc_from_ingress = skb2->tc_at_ingress;
261
262 /* let's the caller reinsert the packet, if possible */
263 if (use_reinsert) {
264 res->ingress = want_ingress;
265 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
266 return TC_ACT_REINSERT;
267 }
268 }
269
270 if (!want_ingress)
271 err = dev_queue_xmit(skb2);
272 else
273 err = netif_receive_skb(skb2);
274
275 if (err) {
276 out:
277 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
278 if (tcf_mirred_is_act_redirect(m_eaction))
279 retval = TC_ACT_SHOT;
280 }
281
282 return retval;
283 }
284
tcf_stats_update(struct tc_action * a,u64 bytes,u32 packets,u64 lastuse)285 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
286 u64 lastuse)
287 {
288 struct tcf_mirred *m = to_mirred(a);
289 struct tcf_t *tm = &m->tcf_tm;
290
291 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
292 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
293 }
294
tcf_mirred_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)295 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
296 int ref)
297 {
298 unsigned char *b = skb_tail_pointer(skb);
299 struct tcf_mirred *m = to_mirred(a);
300 struct tc_mirred opt = {
301 .index = m->tcf_index,
302 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
303 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
304 };
305 struct net_device *dev;
306 struct tcf_t t;
307
308 spin_lock_bh(&m->tcf_lock);
309 opt.action = m->tcf_action;
310 opt.eaction = m->tcfm_eaction;
311 dev = tcf_mirred_dev_dereference(m);
312 if (dev)
313 opt.ifindex = dev->ifindex;
314
315 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
316 goto nla_put_failure;
317
318 tcf_tm_dump(&t, &m->tcf_tm);
319 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
320 goto nla_put_failure;
321 spin_unlock_bh(&m->tcf_lock);
322
323 return skb->len;
324
325 nla_put_failure:
326 spin_unlock_bh(&m->tcf_lock);
327 nlmsg_trim(skb, b);
328 return -1;
329 }
330
tcf_mirred_walker(struct net * net,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)331 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
332 struct netlink_callback *cb, int type,
333 const struct tc_action_ops *ops,
334 struct netlink_ext_ack *extack)
335 {
336 struct tc_action_net *tn = net_generic(net, mirred_net_id);
337
338 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
339 }
340
tcf_mirred_search(struct net * net,struct tc_action ** a,u32 index,struct netlink_ext_ack * extack)341 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index,
342 struct netlink_ext_ack *extack)
343 {
344 struct tc_action_net *tn = net_generic(net, mirred_net_id);
345
346 return tcf_idr_search(tn, a, index);
347 }
348
mirred_device_event(struct notifier_block * unused,unsigned long event,void * ptr)349 static int mirred_device_event(struct notifier_block *unused,
350 unsigned long event, void *ptr)
351 {
352 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
353 struct tcf_mirred *m;
354
355 ASSERT_RTNL();
356 if (event == NETDEV_UNREGISTER) {
357 spin_lock(&mirred_list_lock);
358 list_for_each_entry(m, &mirred_list, tcfm_list) {
359 spin_lock_bh(&m->tcf_lock);
360 if (tcf_mirred_dev_dereference(m) == dev) {
361 dev_put(dev);
362 /* Note : no rcu grace period necessary, as
363 * net_device are already rcu protected.
364 */
365 RCU_INIT_POINTER(m->tcfm_dev, NULL);
366 }
367 spin_unlock_bh(&m->tcf_lock);
368 }
369 spin_unlock(&mirred_list_lock);
370 }
371
372 return NOTIFY_DONE;
373 }
374
375 static struct notifier_block mirred_device_notifier = {
376 .notifier_call = mirred_device_event,
377 };
378
tcf_mirred_get_dev(const struct tc_action * a)379 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
380 {
381 struct tcf_mirred *m = to_mirred(a);
382 struct net_device *dev;
383
384 rcu_read_lock();
385 dev = rcu_dereference(m->tcfm_dev);
386 if (dev)
387 dev_hold(dev);
388 rcu_read_unlock();
389
390 return dev;
391 }
392
tcf_mirred_put_dev(struct net_device * dev)393 static void tcf_mirred_put_dev(struct net_device *dev)
394 {
395 dev_put(dev);
396 }
397
398 static struct tc_action_ops act_mirred_ops = {
399 .kind = "mirred",
400 .type = TCA_ACT_MIRRED,
401 .owner = THIS_MODULE,
402 .act = tcf_mirred_act,
403 .stats_update = tcf_stats_update,
404 .dump = tcf_mirred_dump,
405 .cleanup = tcf_mirred_release,
406 .init = tcf_mirred_init,
407 .walk = tcf_mirred_walker,
408 .lookup = tcf_mirred_search,
409 .size = sizeof(struct tcf_mirred),
410 .get_dev = tcf_mirred_get_dev,
411 .put_dev = tcf_mirred_put_dev,
412 };
413
mirred_init_net(struct net * net)414 static __net_init int mirred_init_net(struct net *net)
415 {
416 struct tc_action_net *tn = net_generic(net, mirred_net_id);
417
418 return tc_action_net_init(tn, &act_mirred_ops);
419 }
420
mirred_exit_net(struct list_head * net_list)421 static void __net_exit mirred_exit_net(struct list_head *net_list)
422 {
423 tc_action_net_exit(net_list, mirred_net_id);
424 }
425
426 static struct pernet_operations mirred_net_ops = {
427 .init = mirred_init_net,
428 .exit_batch = mirred_exit_net,
429 .id = &mirred_net_id,
430 .size = sizeof(struct tc_action_net),
431 };
432
433 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
434 MODULE_DESCRIPTION("Device Mirror/redirect actions");
435 MODULE_LICENSE("GPL");
436
mirred_init_module(void)437 static int __init mirred_init_module(void)
438 {
439 int err = register_netdevice_notifier(&mirred_device_notifier);
440 if (err)
441 return err;
442
443 pr_info("Mirror/redirect action on\n");
444 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
445 }
446
mirred_cleanup_module(void)447 static void __exit mirred_cleanup_module(void)
448 {
449 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
450 unregister_netdevice_notifier(&mirred_device_notifier);
451 }
452
453 module_init(mirred_init_module);
454 module_exit(mirred_cleanup_module);
455