act_vlan.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 vlan_net_id;
  21. static struct tc_action_ops act_vlan_ops;
  22. static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
  23. struct tcf_result *res)
  24. {
  25. struct tcf_vlan *v = to_vlan(a);
  26. int action;
  27. int err;
  28. u16 tci;
  29. spin_lock(&v->tcf_lock);
  30. tcf_lastuse_update(&v->tcf_tm);
  31. bstats_update(&v->tcf_bstats, skb);
  32. action = v->tcf_action;
  33. /* Ensure 'data' points at mac_header prior calling vlan manipulating
  34. * functions.
  35. */
  36. if (skb_at_tc_ingress(skb))
  37. skb_push_rcsum(skb, skb->mac_len);
  38. switch (v->tcfv_action) {
  39. case TCA_VLAN_ACT_POP:
  40. err = skb_vlan_pop(skb);
  41. if (err)
  42. goto drop;
  43. break;
  44. case TCA_VLAN_ACT_PUSH:
  45. err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid |
  46. (v->tcfv_push_prio << VLAN_PRIO_SHIFT));
  47. if (err)
  48. goto drop;
  49. break;
  50. case TCA_VLAN_ACT_MODIFY:
  51. /* No-op if no vlan tag (either hw-accel or in-payload) */
  52. if (!skb_vlan_tagged(skb))
  53. goto unlock;
  54. /* extract existing tag (and guarantee no hw-accel tag) */
  55. if (skb_vlan_tag_present(skb)) {
  56. tci = skb_vlan_tag_get(skb);
  57. skb->vlan_tci = 0;
  58. } else {
  59. /* in-payload vlan tag, pop it */
  60. err = __skb_vlan_pop(skb, &tci);
  61. if (err)
  62. goto drop;
  63. }
  64. /* replace the vid */
  65. tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid;
  66. /* replace prio bits, if tcfv_push_prio specified */
  67. if (v->tcfv_push_prio) {
  68. tci &= ~VLAN_PRIO_MASK;
  69. tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT;
  70. }
  71. /* put updated tci as hwaccel tag */
  72. __vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci);
  73. break;
  74. default:
  75. BUG();
  76. }
  77. goto unlock;
  78. drop:
  79. action = TC_ACT_SHOT;
  80. v->tcf_qstats.drops++;
  81. unlock:
  82. if (skb_at_tc_ingress(skb))
  83. skb_pull_rcsum(skb, skb->mac_len);
  84. spin_unlock(&v->tcf_lock);
  85. return action;
  86. }
  87. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  88. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  89. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  90. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  91. [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
  92. };
  93. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  94. struct nlattr *est, struct tc_action **a,
  95. int ovr, int bind)
  96. {
  97. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  98. struct nlattr *tb[TCA_VLAN_MAX + 1];
  99. struct tc_vlan *parm;
  100. struct tcf_vlan *v;
  101. int action;
  102. __be16 push_vid = 0;
  103. __be16 push_proto = 0;
  104. u8 push_prio = 0;
  105. bool exists = false;
  106. int ret = 0, err;
  107. if (!nla)
  108. return -EINVAL;
  109. err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
  110. if (err < 0)
  111. return err;
  112. if (!tb[TCA_VLAN_PARMS])
  113. return -EINVAL;
  114. parm = nla_data(tb[TCA_VLAN_PARMS]);
  115. exists = tcf_hash_check(tn, parm->index, a, bind);
  116. if (exists && bind)
  117. return 0;
  118. switch (parm->v_action) {
  119. case TCA_VLAN_ACT_POP:
  120. break;
  121. case TCA_VLAN_ACT_PUSH:
  122. case TCA_VLAN_ACT_MODIFY:
  123. if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
  124. if (exists)
  125. tcf_hash_release(*a, bind);
  126. return -EINVAL;
  127. }
  128. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  129. if (push_vid >= VLAN_VID_MASK) {
  130. if (exists)
  131. tcf_hash_release(*a, bind);
  132. return -ERANGE;
  133. }
  134. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  135. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  136. switch (push_proto) {
  137. case htons(ETH_P_8021Q):
  138. case htons(ETH_P_8021AD):
  139. break;
  140. default:
  141. return -EPROTONOSUPPORT;
  142. }
  143. } else {
  144. push_proto = htons(ETH_P_8021Q);
  145. }
  146. if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
  147. push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
  148. break;
  149. default:
  150. if (exists)
  151. tcf_hash_release(*a, bind);
  152. return -EINVAL;
  153. }
  154. action = parm->v_action;
  155. if (!exists) {
  156. ret = tcf_hash_create(tn, parm->index, est, a,
  157. &act_vlan_ops, bind, false);
  158. if (ret)
  159. return ret;
  160. ret = ACT_P_CREATED;
  161. } else {
  162. tcf_hash_release(*a, bind);
  163. if (!ovr)
  164. return -EEXIST;
  165. }
  166. v = to_vlan(*a);
  167. spin_lock_bh(&v->tcf_lock);
  168. v->tcfv_action = action;
  169. v->tcfv_push_vid = push_vid;
  170. v->tcfv_push_prio = push_prio;
  171. v->tcfv_push_proto = push_proto;
  172. v->tcf_action = parm->action;
  173. spin_unlock_bh(&v->tcf_lock);
  174. if (ret == ACT_P_CREATED)
  175. tcf_hash_insert(tn, *a);
  176. return ret;
  177. }
  178. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  179. int bind, int ref)
  180. {
  181. unsigned char *b = skb_tail_pointer(skb);
  182. struct tcf_vlan *v = to_vlan(a);
  183. struct tc_vlan opt = {
  184. .index = v->tcf_index,
  185. .refcnt = v->tcf_refcnt - ref,
  186. .bindcnt = v->tcf_bindcnt - bind,
  187. .action = v->tcf_action,
  188. .v_action = v->tcfv_action,
  189. };
  190. struct tcf_t t;
  191. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  192. goto nla_put_failure;
  193. if ((v->tcfv_action == TCA_VLAN_ACT_PUSH ||
  194. v->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
  195. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
  196. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  197. v->tcfv_push_proto) ||
  198. (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
  199. v->tcfv_push_prio))))
  200. goto nla_put_failure;
  201. tcf_tm_dump(&t, &v->tcf_tm);
  202. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  203. goto nla_put_failure;
  204. return skb->len;
  205. nla_put_failure:
  206. nlmsg_trim(skb, b);
  207. return -1;
  208. }
  209. static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
  210. struct netlink_callback *cb, int type,
  211. const struct tc_action_ops *ops)
  212. {
  213. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  214. return tcf_generic_walker(tn, skb, cb, type, ops);
  215. }
  216. static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
  217. {
  218. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  219. return tcf_hash_search(tn, a, index);
  220. }
  221. static struct tc_action_ops act_vlan_ops = {
  222. .kind = "vlan",
  223. .type = TCA_ACT_VLAN,
  224. .owner = THIS_MODULE,
  225. .act = tcf_vlan,
  226. .dump = tcf_vlan_dump,
  227. .init = tcf_vlan_init,
  228. .walk = tcf_vlan_walker,
  229. .lookup = tcf_vlan_search,
  230. .size = sizeof(struct tcf_vlan),
  231. };
  232. static __net_init int vlan_init_net(struct net *net)
  233. {
  234. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  235. return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
  236. }
  237. static void __net_exit vlan_exit_net(struct net *net)
  238. {
  239. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  240. tc_action_net_exit(tn);
  241. }
  242. static struct pernet_operations vlan_net_ops = {
  243. .init = vlan_init_net,
  244. .exit = vlan_exit_net,
  245. .id = &vlan_net_id,
  246. .size = sizeof(struct tc_action_net),
  247. };
  248. static int __init vlan_init_module(void)
  249. {
  250. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  251. }
  252. static void __exit vlan_cleanup_module(void)
  253. {
  254. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  255. }
  256. module_init(vlan_init_module);
  257. module_exit(vlan_cleanup_module);
  258. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  259. MODULE_DESCRIPTION("vlan manipulation actions");
  260. MODULE_LICENSE("GPL v2");