act_vlan.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. return -EPROTONOSUPPORT;
  141. }
  142. } else {
  143. push_proto = htons(ETH_P_8021Q);
  144. }
  145. if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
  146. push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
  147. break;
  148. default:
  149. if (exists)
  150. tcf_idr_release(*a, bind);
  151. return -EINVAL;
  152. }
  153. action = parm->v_action;
  154. if (!exists) {
  155. ret = tcf_idr_create(tn, parm->index, est, a,
  156. &act_vlan_ops, bind, false);
  157. if (ret)
  158. return ret;
  159. ret = ACT_P_CREATED;
  160. } else {
  161. tcf_idr_release(*a, bind);
  162. if (!ovr)
  163. return -EEXIST;
  164. }
  165. v = to_vlan(*a);
  166. spin_lock_bh(&v->tcf_lock);
  167. v->tcfv_action = action;
  168. v->tcfv_push_vid = push_vid;
  169. v->tcfv_push_prio = push_prio;
  170. v->tcfv_push_proto = push_proto;
  171. v->tcf_action = parm->action;
  172. spin_unlock_bh(&v->tcf_lock);
  173. if (ret == ACT_P_CREATED)
  174. tcf_idr_insert(tn, *a);
  175. return ret;
  176. }
  177. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  178. int bind, int ref)
  179. {
  180. unsigned char *b = skb_tail_pointer(skb);
  181. struct tcf_vlan *v = to_vlan(a);
  182. struct tc_vlan opt = {
  183. .index = v->tcf_index,
  184. .refcnt = v->tcf_refcnt - ref,
  185. .bindcnt = v->tcf_bindcnt - bind,
  186. .action = v->tcf_action,
  187. .v_action = v->tcfv_action,
  188. };
  189. struct tcf_t t;
  190. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  191. goto nla_put_failure;
  192. if ((v->tcfv_action == TCA_VLAN_ACT_PUSH ||
  193. v->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
  194. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
  195. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  196. v->tcfv_push_proto) ||
  197. (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
  198. v->tcfv_push_prio))))
  199. goto nla_put_failure;
  200. tcf_tm_dump(&t, &v->tcf_tm);
  201. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  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 tcf_vlan_walker(struct net *net, struct sk_buff *skb,
  209. struct netlink_callback *cb, int type,
  210. const struct tc_action_ops *ops)
  211. {
  212. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  213. return tcf_generic_walker(tn, skb, cb, type, ops);
  214. }
  215. static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
  216. {
  217. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  218. return tcf_idr_search(tn, a, index);
  219. }
  220. static struct tc_action_ops act_vlan_ops = {
  221. .kind = "vlan",
  222. .type = TCA_ACT_VLAN,
  223. .owner = THIS_MODULE,
  224. .act = tcf_vlan,
  225. .dump = tcf_vlan_dump,
  226. .init = tcf_vlan_init,
  227. .walk = tcf_vlan_walker,
  228. .lookup = tcf_vlan_search,
  229. .size = sizeof(struct tcf_vlan),
  230. };
  231. static __net_init int vlan_init_net(struct net *net)
  232. {
  233. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  234. return tc_action_net_init(tn, &act_vlan_ops);
  235. }
  236. static void __net_exit vlan_exit_net(struct net *net)
  237. {
  238. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  239. tc_action_net_exit(tn);
  240. }
  241. static struct pernet_operations vlan_net_ops = {
  242. .init = vlan_init_net,
  243. .exit = vlan_exit_net,
  244. .id = &vlan_net_id,
  245. .size = sizeof(struct tc_action_net),
  246. };
  247. static int __init vlan_init_module(void)
  248. {
  249. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  250. }
  251. static void __exit vlan_cleanup_module(void)
  252. {
  253. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  254. }
  255. module_init(vlan_init_module);
  256. module_exit(vlan_cleanup_module);
  257. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  258. MODULE_DESCRIPTION("vlan manipulation actions");
  259. MODULE_LICENSE("GPL v2");