act_mirred.c 6.7 KB

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