act_vlan.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/if_vlan.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <linux/tc_act/tc_vlan.h>
  18. #include <net/tc_act/tc_vlan.h>
  19. #define VLAN_TAB_MASK 15
  20. static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
  21. struct tcf_result *res)
  22. {
  23. struct tcf_vlan *v = a->priv;
  24. int action;
  25. int err;
  26. spin_lock(&v->tcf_lock);
  27. v->tcf_tm.lastuse = jiffies;
  28. bstats_update(&v->tcf_bstats, skb);
  29. action = v->tcf_action;
  30. switch (v->tcfv_action) {
  31. case TCA_VLAN_ACT_POP:
  32. err = skb_vlan_pop(skb);
  33. if (err)
  34. goto drop;
  35. break;
  36. case TCA_VLAN_ACT_PUSH:
  37. err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid);
  38. if (err)
  39. goto drop;
  40. break;
  41. default:
  42. BUG();
  43. }
  44. goto unlock;
  45. drop:
  46. action = TC_ACT_SHOT;
  47. v->tcf_qstats.drops++;
  48. unlock:
  49. spin_unlock(&v->tcf_lock);
  50. return action;
  51. }
  52. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  53. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  54. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  55. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  56. };
  57. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  58. struct nlattr *est, struct tc_action *a,
  59. int ovr, int bind)
  60. {
  61. struct nlattr *tb[TCA_VLAN_MAX + 1];
  62. struct tc_vlan *parm;
  63. struct tcf_vlan *v;
  64. int action;
  65. __be16 push_vid = 0;
  66. __be16 push_proto = 0;
  67. int ret = 0;
  68. int err;
  69. if (!nla)
  70. return -EINVAL;
  71. err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
  72. if (err < 0)
  73. return err;
  74. if (!tb[TCA_VLAN_PARMS])
  75. return -EINVAL;
  76. parm = nla_data(tb[TCA_VLAN_PARMS]);
  77. switch (parm->v_action) {
  78. case TCA_VLAN_ACT_POP:
  79. break;
  80. case TCA_VLAN_ACT_PUSH:
  81. if (!tb[TCA_VLAN_PUSH_VLAN_ID])
  82. return -EINVAL;
  83. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  84. if (push_vid >= VLAN_VID_MASK)
  85. return -ERANGE;
  86. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  87. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  88. switch (push_proto) {
  89. case htons(ETH_P_8021Q):
  90. case htons(ETH_P_8021AD):
  91. break;
  92. default:
  93. return -EPROTONOSUPPORT;
  94. }
  95. } else {
  96. push_proto = htons(ETH_P_8021Q);
  97. }
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. action = parm->v_action;
  103. if (!tcf_hash_check(parm->index, a, bind)) {
  104. ret = tcf_hash_create(parm->index, est, a, sizeof(*v), bind);
  105. if (ret)
  106. return ret;
  107. ret = ACT_P_CREATED;
  108. } else {
  109. if (bind)
  110. return 0;
  111. tcf_hash_release(a, bind);
  112. if (!ovr)
  113. return -EEXIST;
  114. }
  115. v = to_vlan(a);
  116. spin_lock_bh(&v->tcf_lock);
  117. v->tcfv_action = action;
  118. v->tcfv_push_vid = push_vid;
  119. v->tcfv_push_proto = push_proto;
  120. v->tcf_action = parm->action;
  121. spin_unlock_bh(&v->tcf_lock);
  122. if (ret == ACT_P_CREATED)
  123. tcf_hash_insert(a);
  124. return ret;
  125. }
  126. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  127. int bind, int ref)
  128. {
  129. unsigned char *b = skb_tail_pointer(skb);
  130. struct tcf_vlan *v = a->priv;
  131. struct tc_vlan opt = {
  132. .index = v->tcf_index,
  133. .refcnt = v->tcf_refcnt - ref,
  134. .bindcnt = v->tcf_bindcnt - bind,
  135. .action = v->tcf_action,
  136. .v_action = v->tcfv_action,
  137. };
  138. struct tcf_t t;
  139. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  140. goto nla_put_failure;
  141. if (v->tcfv_action == TCA_VLAN_ACT_PUSH &&
  142. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
  143. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, v->tcfv_push_proto)))
  144. goto nla_put_failure;
  145. t.install = jiffies_to_clock_t(jiffies - v->tcf_tm.install);
  146. t.lastuse = jiffies_to_clock_t(jiffies - v->tcf_tm.lastuse);
  147. t.expires = jiffies_to_clock_t(v->tcf_tm.expires);
  148. if (nla_put(skb, TCA_VLAN_TM, sizeof(t), &t))
  149. goto nla_put_failure;
  150. return skb->len;
  151. nla_put_failure:
  152. nlmsg_trim(skb, b);
  153. return -1;
  154. }
  155. static struct tc_action_ops act_vlan_ops = {
  156. .kind = "vlan",
  157. .type = TCA_ACT_VLAN,
  158. .owner = THIS_MODULE,
  159. .act = tcf_vlan,
  160. .dump = tcf_vlan_dump,
  161. .init = tcf_vlan_init,
  162. };
  163. static int __init vlan_init_module(void)
  164. {
  165. return tcf_register_action(&act_vlan_ops, VLAN_TAB_MASK);
  166. }
  167. static void __exit vlan_cleanup_module(void)
  168. {
  169. tcf_unregister_action(&act_vlan_ops);
  170. }
  171. module_init(vlan_init_module);
  172. module_exit(vlan_cleanup_module);
  173. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  174. MODULE_DESCRIPTION("vlan manipulation actions");
  175. MODULE_LICENSE("GPL v2");