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