act_mirred.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * net/sched/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. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/gfp.h>
  23. #include <net/net_namespace.h>
  24. #include <net/netlink.h>
  25. #include <net/pkt_sched.h>
  26. #include <linux/tc_act/tc_mirred.h>
  27. #include <net/tc_act/tc_mirred.h>
  28. #include <linux/if_arp.h>
  29. #define MIRRED_TAB_MASK 7
  30. static struct tcf_common *tcf_mirred_ht[MIRRED_TAB_MASK + 1];
  31. static u32 mirred_idx_gen;
  32. static DEFINE_RWLOCK(mirred_lock);
  33. static LIST_HEAD(mirred_list);
  34. static struct tcf_hashinfo mirred_hash_info = {
  35. .htab = tcf_mirred_ht,
  36. .hmask = MIRRED_TAB_MASK,
  37. .lock = &mirred_lock,
  38. };
  39. static int tcf_mirred_release(struct tcf_mirred *m, int bind)
  40. {
  41. if (m) {
  42. if (bind)
  43. m->tcf_bindcnt--;
  44. m->tcf_refcnt--;
  45. if (!m->tcf_bindcnt && m->tcf_refcnt <= 0) {
  46. list_del(&m->tcfm_list);
  47. if (m->tcfm_dev)
  48. dev_put(m->tcfm_dev);
  49. tcf_hash_destroy(&m->common, &mirred_hash_info);
  50. return 1;
  51. }
  52. }
  53. return 0;
  54. }
  55. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  56. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  57. };
  58. static int tcf_mirred_init(struct nlattr *nla, struct nlattr *est,
  59. struct tc_action *a, int ovr, int bind)
  60. {
  61. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  62. struct tc_mirred *parm;
  63. struct tcf_mirred *m;
  64. struct tcf_common *pc;
  65. struct net_device *dev;
  66. int ret, ok_push = 0;
  67. if (nla == NULL)
  68. return -EINVAL;
  69. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
  70. if (ret < 0)
  71. return ret;
  72. if (tb[TCA_MIRRED_PARMS] == NULL)
  73. return -EINVAL;
  74. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  75. switch (parm->eaction) {
  76. case TCA_EGRESS_MIRROR:
  77. case TCA_EGRESS_REDIR:
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. if (parm->ifindex) {
  83. dev = __dev_get_by_index(&init_net, parm->ifindex);
  84. if (dev == NULL)
  85. return -ENODEV;
  86. switch (dev->type) {
  87. case ARPHRD_TUNNEL:
  88. case ARPHRD_TUNNEL6:
  89. case ARPHRD_SIT:
  90. case ARPHRD_IPGRE:
  91. case ARPHRD_VOID:
  92. case ARPHRD_NONE:
  93. ok_push = 0;
  94. break;
  95. default:
  96. ok_push = 1;
  97. break;
  98. }
  99. } else {
  100. dev = NULL;
  101. }
  102. pc = tcf_hash_check(parm->index, a, bind, &mirred_hash_info);
  103. if (!pc) {
  104. if (dev == NULL)
  105. return -EINVAL;
  106. pc = tcf_hash_create(parm->index, est, a, sizeof(*m), bind,
  107. &mirred_idx_gen, &mirred_hash_info);
  108. if (IS_ERR(pc))
  109. return PTR_ERR(pc);
  110. ret = ACT_P_CREATED;
  111. } else {
  112. if (!ovr) {
  113. tcf_mirred_release(to_mirred(pc), bind);
  114. return -EEXIST;
  115. }
  116. }
  117. m = to_mirred(pc);
  118. spin_lock_bh(&m->tcf_lock);
  119. m->tcf_action = parm->action;
  120. m->tcfm_eaction = parm->eaction;
  121. if (dev != NULL) {
  122. m->tcfm_ifindex = parm->ifindex;
  123. if (ret != ACT_P_CREATED)
  124. dev_put(m->tcfm_dev);
  125. dev_hold(dev);
  126. m->tcfm_dev = dev;
  127. m->tcfm_ok_push = ok_push;
  128. }
  129. spin_unlock_bh(&m->tcf_lock);
  130. if (ret == ACT_P_CREATED) {
  131. list_add(&m->tcfm_list, &mirred_list);
  132. tcf_hash_insert(pc, &mirred_hash_info);
  133. }
  134. return ret;
  135. }
  136. static int tcf_mirred_cleanup(struct tc_action *a, int bind)
  137. {
  138. struct tcf_mirred *m = a->priv;
  139. if (m)
  140. return tcf_mirred_release(m, bind);
  141. return 0;
  142. }
  143. static int tcf_mirred(struct sk_buff *skb, struct tc_action *a,
  144. struct tcf_result *res)
  145. {
  146. struct tcf_mirred *m = a->priv;
  147. struct net_device *dev;
  148. struct sk_buff *skb2;
  149. u32 at;
  150. int retval, err = 1;
  151. spin_lock(&m->tcf_lock);
  152. m->tcf_tm.lastuse = jiffies;
  153. bstats_update(&m->tcf_bstats, skb);
  154. dev = m->tcfm_dev;
  155. if (!dev) {
  156. printk_once(KERN_NOTICE "tc mirred: target device is gone\n");
  157. goto out;
  158. }
  159. if (!(dev->flags & IFF_UP)) {
  160. if (net_ratelimit())
  161. pr_notice("tc mirred to Houston: device %s is down\n",
  162. dev->name);
  163. goto out;
  164. }
  165. at = G_TC_AT(skb->tc_verd);
  166. skb2 = skb_act_clone(skb, GFP_ATOMIC, m->tcf_action);
  167. if (skb2 == NULL)
  168. goto out;
  169. if (!(at & AT_EGRESS)) {
  170. if (m->tcfm_ok_push)
  171. skb_push(skb2, skb2->dev->hard_header_len);
  172. }
  173. /* mirror is always swallowed */
  174. if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
  175. skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
  176. skb2->skb_iif = skb->dev->ifindex;
  177. skb2->dev = dev;
  178. dev_queue_xmit(skb2);
  179. err = 0;
  180. out:
  181. if (err) {
  182. m->tcf_qstats.overlimits++;
  183. /* should we be asking for packet to be dropped?
  184. * may make sense for redirect case only
  185. */
  186. retval = TC_ACT_SHOT;
  187. } else {
  188. retval = m->tcf_action;
  189. }
  190. spin_unlock(&m->tcf_lock);
  191. return retval;
  192. }
  193. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  194. {
  195. unsigned char *b = skb_tail_pointer(skb);
  196. struct tcf_mirred *m = a->priv;
  197. struct tc_mirred opt = {
  198. .index = m->tcf_index,
  199. .action = m->tcf_action,
  200. .refcnt = m->tcf_refcnt - ref,
  201. .bindcnt = m->tcf_bindcnt - bind,
  202. .eaction = m->tcfm_eaction,
  203. .ifindex = m->tcfm_ifindex,
  204. };
  205. struct tcf_t t;
  206. NLA_PUT(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt);
  207. t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
  208. t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
  209. t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
  210. NLA_PUT(skb, TCA_MIRRED_TM, sizeof(t), &t);
  211. return skb->len;
  212. nla_put_failure:
  213. nlmsg_trim(skb, b);
  214. return -1;
  215. }
  216. static int mirred_device_event(struct notifier_block *unused,
  217. unsigned long event, void *ptr)
  218. {
  219. struct net_device *dev = ptr;
  220. struct tcf_mirred *m;
  221. if (event == NETDEV_UNREGISTER)
  222. list_for_each_entry(m, &mirred_list, tcfm_list) {
  223. if (m->tcfm_dev == dev) {
  224. dev_put(dev);
  225. m->tcfm_dev = NULL;
  226. }
  227. }
  228. return NOTIFY_DONE;
  229. }
  230. static struct notifier_block mirred_device_notifier = {
  231. .notifier_call = mirred_device_event,
  232. };
  233. static struct tc_action_ops act_mirred_ops = {
  234. .kind = "mirred",
  235. .hinfo = &mirred_hash_info,
  236. .type = TCA_ACT_MIRRED,
  237. .capab = TCA_CAP_NONE,
  238. .owner = THIS_MODULE,
  239. .act = tcf_mirred,
  240. .dump = tcf_mirred_dump,
  241. .cleanup = tcf_mirred_cleanup,
  242. .lookup = tcf_hash_search,
  243. .init = tcf_mirred_init,
  244. .walk = tcf_generic_walker
  245. };
  246. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  247. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  248. MODULE_LICENSE("GPL");
  249. static int __init mirred_init_module(void)
  250. {
  251. int err = register_netdevice_notifier(&mirred_device_notifier);
  252. if (err)
  253. return err;
  254. pr_info("Mirror/redirect action on\n");
  255. return tcf_register_action(&act_mirred_ops);
  256. }
  257. static void __exit mirred_cleanup_module(void)
  258. {
  259. unregister_netdevice_notifier(&mirred_device_notifier);
  260. tcf_unregister_action(&act_mirred_ops);
  261. }
  262. module_init(mirred_init_module);
  263. module_exit(mirred_cleanup_module);