act_vlan.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* Ensure 'data' points at mac_header prior calling vlan manipulating
  31. * functions.
  32. */
  33. if (skb_at_tc_ingress(skb))
  34. skb_push_rcsum(skb, skb->mac_len);
  35. switch (v->tcfv_action) {
  36. case TCA_VLAN_ACT_POP:
  37. err = skb_vlan_pop(skb);
  38. if (err)
  39. goto drop;
  40. break;
  41. case TCA_VLAN_ACT_PUSH:
  42. err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid);
  43. if (err)
  44. goto drop;
  45. break;
  46. default:
  47. BUG();
  48. }
  49. goto unlock;
  50. drop:
  51. action = TC_ACT_SHOT;
  52. v->tcf_qstats.drops++;
  53. unlock:
  54. if (skb_at_tc_ingress(skb))
  55. skb_pull_rcsum(skb, skb->mac_len);
  56. spin_unlock(&v->tcf_lock);
  57. return action;
  58. }
  59. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  60. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  61. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  62. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  63. };
  64. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  65. struct nlattr *est, struct tc_action *a,
  66. int ovr, int bind)
  67. {
  68. struct nlattr *tb[TCA_VLAN_MAX + 1];
  69. struct tc_vlan *parm;
  70. struct tcf_vlan *v;
  71. int action;
  72. __be16 push_vid = 0;
  73. __be16 push_proto = 0;
  74. int ret = 0;
  75. int err;
  76. if (!nla)
  77. return -EINVAL;
  78. err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
  79. if (err < 0)
  80. return err;
  81. if (!tb[TCA_VLAN_PARMS])
  82. return -EINVAL;
  83. parm = nla_data(tb[TCA_VLAN_PARMS]);
  84. switch (parm->v_action) {
  85. case TCA_VLAN_ACT_POP:
  86. break;
  87. case TCA_VLAN_ACT_PUSH:
  88. if (!tb[TCA_VLAN_PUSH_VLAN_ID])
  89. return -EINVAL;
  90. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  91. if (push_vid >= VLAN_VID_MASK)
  92. return -ERANGE;
  93. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  94. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  95. switch (push_proto) {
  96. case htons(ETH_P_8021Q):
  97. case htons(ETH_P_8021AD):
  98. break;
  99. default:
  100. return -EPROTONOSUPPORT;
  101. }
  102. } else {
  103. push_proto = htons(ETH_P_8021Q);
  104. }
  105. break;
  106. default:
  107. return -EINVAL;
  108. }
  109. action = parm->v_action;
  110. if (!tcf_hash_check(parm->index, a, bind)) {
  111. ret = tcf_hash_create(parm->index, est, a, sizeof(*v),
  112. bind, false);
  113. if (ret)
  114. return ret;
  115. ret = ACT_P_CREATED;
  116. } else {
  117. if (bind)
  118. return 0;
  119. tcf_hash_release(a, bind);
  120. if (!ovr)
  121. return -EEXIST;
  122. }
  123. v = to_vlan(a);
  124. spin_lock_bh(&v->tcf_lock);
  125. v->tcfv_action = action;
  126. v->tcfv_push_vid = push_vid;
  127. v->tcfv_push_proto = push_proto;
  128. v->tcf_action = parm->action;
  129. spin_unlock_bh(&v->tcf_lock);
  130. if (ret == ACT_P_CREATED)
  131. tcf_hash_insert(a);
  132. return ret;
  133. }
  134. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  135. int bind, int ref)
  136. {
  137. unsigned char *b = skb_tail_pointer(skb);
  138. struct tcf_vlan *v = a->priv;
  139. struct tc_vlan opt = {
  140. .index = v->tcf_index,
  141. .refcnt = v->tcf_refcnt - ref,
  142. .bindcnt = v->tcf_bindcnt - bind,
  143. .action = v->tcf_action,
  144. .v_action = v->tcfv_action,
  145. };
  146. struct tcf_t t;
  147. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  148. goto nla_put_failure;
  149. if (v->tcfv_action == TCA_VLAN_ACT_PUSH &&
  150. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
  151. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, v->tcfv_push_proto)))
  152. goto nla_put_failure;
  153. t.install = jiffies_to_clock_t(jiffies - v->tcf_tm.install);
  154. t.lastuse = jiffies_to_clock_t(jiffies - v->tcf_tm.lastuse);
  155. t.expires = jiffies_to_clock_t(v->tcf_tm.expires);
  156. if (nla_put(skb, TCA_VLAN_TM, sizeof(t), &t))
  157. goto nla_put_failure;
  158. return skb->len;
  159. nla_put_failure:
  160. nlmsg_trim(skb, b);
  161. return -1;
  162. }
  163. static struct tc_action_ops act_vlan_ops = {
  164. .kind = "vlan",
  165. .type = TCA_ACT_VLAN,
  166. .owner = THIS_MODULE,
  167. .act = tcf_vlan,
  168. .dump = tcf_vlan_dump,
  169. .init = tcf_vlan_init,
  170. };
  171. static int __init vlan_init_module(void)
  172. {
  173. return tcf_register_action(&act_vlan_ops, VLAN_TAB_MASK);
  174. }
  175. static void __exit vlan_cleanup_module(void)
  176. {
  177. tcf_unregister_action(&act_vlan_ops);
  178. }
  179. module_init(vlan_init_module);
  180. module_exit(vlan_cleanup_module);
  181. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  182. MODULE_DESCRIPTION("vlan manipulation actions");
  183. MODULE_LICENSE("GPL v2");