act_vlan.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_act(struct sk_buff *skb, const struct tc_action *a,
  22. struct tcf_result *res)
  23. {
  24. struct tcf_vlan *v = to_vlan(a);
  25. struct tcf_vlan_params *p;
  26. int action;
  27. int err;
  28. u16 tci;
  29. tcf_lastuse_update(&v->tcf_tm);
  30. bstats_cpu_update(this_cpu_ptr(v->common.cpu_bstats), skb);
  31. /* Ensure 'data' points at mac_header prior calling vlan manipulating
  32. * functions.
  33. */
  34. if (skb_at_tc_ingress(skb))
  35. skb_push_rcsum(skb, skb->mac_len);
  36. action = READ_ONCE(v->tcf_action);
  37. p = rcu_dereference_bh(v->vlan_p);
  38. switch (p->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, p->tcfv_push_proto, p->tcfv_push_vid |
  46. (p->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 out;
  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) | p->tcfv_push_vid;
  66. /* replace prio bits, if tcfv_push_prio specified */
  67. if (p->tcfv_push_prio) {
  68. tci &= ~VLAN_PRIO_MASK;
  69. tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
  70. }
  71. /* put updated tci as hwaccel tag */
  72. __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
  73. break;
  74. default:
  75. BUG();
  76. }
  77. out:
  78. if (skb_at_tc_ingress(skb))
  79. skb_pull_rcsum(skb, skb->mac_len);
  80. return action;
  81. drop:
  82. qstats_drop_inc(this_cpu_ptr(v->common.cpu_qstats));
  83. return TC_ACT_SHOT;
  84. }
  85. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  86. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  87. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  88. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  89. [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
  90. };
  91. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  92. struct nlattr *est, struct tc_action **a,
  93. int ovr, int bind, bool rtnl_held,
  94. struct netlink_ext_ack *extack)
  95. {
  96. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  97. struct nlattr *tb[TCA_VLAN_MAX + 1];
  98. struct tcf_vlan_params *p;
  99. struct tc_vlan *parm;
  100. struct tcf_vlan *v;
  101. int action;
  102. u16 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, NULL);
  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. err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
  116. if (err < 0)
  117. return err;
  118. exists = err;
  119. if (exists && bind)
  120. return 0;
  121. switch (parm->v_action) {
  122. case TCA_VLAN_ACT_POP:
  123. break;
  124. case TCA_VLAN_ACT_PUSH:
  125. case TCA_VLAN_ACT_MODIFY:
  126. if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
  127. if (exists)
  128. tcf_idr_release(*a, bind);
  129. else
  130. tcf_idr_cleanup(tn, parm->index);
  131. return -EINVAL;
  132. }
  133. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  134. if (push_vid >= VLAN_VID_MASK) {
  135. if (exists)
  136. tcf_idr_release(*a, bind);
  137. else
  138. tcf_idr_cleanup(tn, parm->index);
  139. return -ERANGE;
  140. }
  141. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  142. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  143. switch (push_proto) {
  144. case htons(ETH_P_8021Q):
  145. case htons(ETH_P_8021AD):
  146. break;
  147. default:
  148. if (exists)
  149. tcf_idr_release(*a, bind);
  150. else
  151. tcf_idr_cleanup(tn, parm->index);
  152. return -EPROTONOSUPPORT;
  153. }
  154. } else {
  155. push_proto = htons(ETH_P_8021Q);
  156. }
  157. if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
  158. push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
  159. break;
  160. default:
  161. if (exists)
  162. tcf_idr_release(*a, bind);
  163. else
  164. tcf_idr_cleanup(tn, parm->index);
  165. return -EINVAL;
  166. }
  167. action = parm->v_action;
  168. if (!exists) {
  169. ret = tcf_idr_create(tn, parm->index, est, a,
  170. &act_vlan_ops, bind, true);
  171. if (ret) {
  172. tcf_idr_cleanup(tn, parm->index);
  173. return ret;
  174. }
  175. ret = ACT_P_CREATED;
  176. } else if (!ovr) {
  177. tcf_idr_release(*a, bind);
  178. return -EEXIST;
  179. }
  180. v = to_vlan(*a);
  181. p = kzalloc(sizeof(*p), GFP_KERNEL);
  182. if (!p) {
  183. tcf_idr_release(*a, bind);
  184. return -ENOMEM;
  185. }
  186. p->tcfv_action = action;
  187. p->tcfv_push_vid = push_vid;
  188. p->tcfv_push_prio = push_prio;
  189. p->tcfv_push_proto = push_proto;
  190. spin_lock_bh(&v->tcf_lock);
  191. v->tcf_action = parm->action;
  192. rcu_swap_protected(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
  193. spin_unlock_bh(&v->tcf_lock);
  194. if (p)
  195. kfree_rcu(p, rcu);
  196. if (ret == ACT_P_CREATED)
  197. tcf_idr_insert(tn, *a);
  198. return ret;
  199. }
  200. static void tcf_vlan_cleanup(struct tc_action *a)
  201. {
  202. struct tcf_vlan *v = to_vlan(a);
  203. struct tcf_vlan_params *p;
  204. p = rcu_dereference_protected(v->vlan_p, 1);
  205. if (p)
  206. kfree_rcu(p, rcu);
  207. }
  208. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  209. int bind, int ref)
  210. {
  211. unsigned char *b = skb_tail_pointer(skb);
  212. struct tcf_vlan *v = to_vlan(a);
  213. struct tcf_vlan_params *p;
  214. struct tc_vlan opt = {
  215. .index = v->tcf_index,
  216. .refcnt = refcount_read(&v->tcf_refcnt) - ref,
  217. .bindcnt = atomic_read(&v->tcf_bindcnt) - bind,
  218. };
  219. struct tcf_t t;
  220. spin_lock_bh(&v->tcf_lock);
  221. opt.action = v->tcf_action;
  222. p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
  223. opt.v_action = p->tcfv_action;
  224. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  225. goto nla_put_failure;
  226. if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
  227. p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
  228. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
  229. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  230. p->tcfv_push_proto) ||
  231. (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
  232. p->tcfv_push_prio))))
  233. goto nla_put_failure;
  234. tcf_tm_dump(&t, &v->tcf_tm);
  235. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  236. goto nla_put_failure;
  237. spin_unlock_bh(&v->tcf_lock);
  238. return skb->len;
  239. nla_put_failure:
  240. spin_unlock_bh(&v->tcf_lock);
  241. nlmsg_trim(skb, b);
  242. return -1;
  243. }
  244. static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
  245. struct netlink_callback *cb, int type,
  246. const struct tc_action_ops *ops,
  247. struct netlink_ext_ack *extack)
  248. {
  249. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  250. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  251. }
  252. static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
  253. {
  254. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  255. return tcf_idr_search(tn, a, index);
  256. }
  257. static struct tc_action_ops act_vlan_ops = {
  258. .kind = "vlan",
  259. .type = TCA_ACT_VLAN,
  260. .owner = THIS_MODULE,
  261. .act = tcf_vlan_act,
  262. .dump = tcf_vlan_dump,
  263. .init = tcf_vlan_init,
  264. .cleanup = tcf_vlan_cleanup,
  265. .walk = tcf_vlan_walker,
  266. .lookup = tcf_vlan_search,
  267. .size = sizeof(struct tcf_vlan),
  268. };
  269. static __net_init int vlan_init_net(struct net *net)
  270. {
  271. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  272. return tc_action_net_init(tn, &act_vlan_ops);
  273. }
  274. static void __net_exit vlan_exit_net(struct list_head *net_list)
  275. {
  276. tc_action_net_exit(net_list, vlan_net_id);
  277. }
  278. static struct pernet_operations vlan_net_ops = {
  279. .init = vlan_init_net,
  280. .exit_batch = vlan_exit_net,
  281. .id = &vlan_net_id,
  282. .size = sizeof(struct tc_action_net),
  283. };
  284. static int __init vlan_init_module(void)
  285. {
  286. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  287. }
  288. static void __exit vlan_cleanup_module(void)
  289. {
  290. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  291. }
  292. module_init(vlan_init_module);
  293. module_exit(vlan_cleanup_module);
  294. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  295. MODULE_DESCRIPTION("vlan manipulation actions");
  296. MODULE_LICENSE("GPL v2");