act_vlan.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. u32 index;
  108. if (!nla)
  109. return -EINVAL;
  110. err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy, NULL);
  111. if (err < 0)
  112. return err;
  113. if (!tb[TCA_VLAN_PARMS])
  114. return -EINVAL;
  115. parm = nla_data(tb[TCA_VLAN_PARMS]);
  116. index = parm->index;
  117. err = tcf_idr_check_alloc(tn, &index, a, bind);
  118. if (err < 0)
  119. return err;
  120. exists = err;
  121. if (exists && bind)
  122. return 0;
  123. switch (parm->v_action) {
  124. case TCA_VLAN_ACT_POP:
  125. break;
  126. case TCA_VLAN_ACT_PUSH:
  127. case TCA_VLAN_ACT_MODIFY:
  128. if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
  129. if (exists)
  130. tcf_idr_release(*a, bind);
  131. else
  132. tcf_idr_cleanup(tn, index);
  133. return -EINVAL;
  134. }
  135. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  136. if (push_vid >= VLAN_VID_MASK) {
  137. if (exists)
  138. tcf_idr_release(*a, bind);
  139. else
  140. tcf_idr_cleanup(tn, index);
  141. return -ERANGE;
  142. }
  143. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  144. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  145. switch (push_proto) {
  146. case htons(ETH_P_8021Q):
  147. case htons(ETH_P_8021AD):
  148. break;
  149. default:
  150. if (exists)
  151. tcf_idr_release(*a, bind);
  152. else
  153. tcf_idr_cleanup(tn, index);
  154. return -EPROTONOSUPPORT;
  155. }
  156. } else {
  157. push_proto = htons(ETH_P_8021Q);
  158. }
  159. if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
  160. push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
  161. break;
  162. default:
  163. if (exists)
  164. tcf_idr_release(*a, bind);
  165. else
  166. tcf_idr_cleanup(tn, index);
  167. return -EINVAL;
  168. }
  169. action = parm->v_action;
  170. if (!exists) {
  171. ret = tcf_idr_create(tn, index, est, a,
  172. &act_vlan_ops, bind, true);
  173. if (ret) {
  174. tcf_idr_cleanup(tn, index);
  175. return ret;
  176. }
  177. ret = ACT_P_CREATED;
  178. } else if (!ovr) {
  179. tcf_idr_release(*a, bind);
  180. return -EEXIST;
  181. }
  182. v = to_vlan(*a);
  183. p = kzalloc(sizeof(*p), GFP_KERNEL);
  184. if (!p) {
  185. tcf_idr_release(*a, bind);
  186. return -ENOMEM;
  187. }
  188. p->tcfv_action = action;
  189. p->tcfv_push_vid = push_vid;
  190. p->tcfv_push_prio = push_prio;
  191. p->tcfv_push_proto = push_proto;
  192. spin_lock_bh(&v->tcf_lock);
  193. v->tcf_action = parm->action;
  194. rcu_swap_protected(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
  195. spin_unlock_bh(&v->tcf_lock);
  196. if (p)
  197. kfree_rcu(p, rcu);
  198. if (ret == ACT_P_CREATED)
  199. tcf_idr_insert(tn, *a);
  200. return ret;
  201. }
  202. static void tcf_vlan_cleanup(struct tc_action *a)
  203. {
  204. struct tcf_vlan *v = to_vlan(a);
  205. struct tcf_vlan_params *p;
  206. p = rcu_dereference_protected(v->vlan_p, 1);
  207. if (p)
  208. kfree_rcu(p, rcu);
  209. }
  210. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  211. int bind, int ref)
  212. {
  213. unsigned char *b = skb_tail_pointer(skb);
  214. struct tcf_vlan *v = to_vlan(a);
  215. struct tcf_vlan_params *p;
  216. struct tc_vlan opt = {
  217. .index = v->tcf_index,
  218. .refcnt = refcount_read(&v->tcf_refcnt) - ref,
  219. .bindcnt = atomic_read(&v->tcf_bindcnt) - bind,
  220. };
  221. struct tcf_t t;
  222. spin_lock_bh(&v->tcf_lock);
  223. opt.action = v->tcf_action;
  224. p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
  225. opt.v_action = p->tcfv_action;
  226. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  227. goto nla_put_failure;
  228. if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
  229. p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
  230. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
  231. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  232. p->tcfv_push_proto) ||
  233. (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
  234. p->tcfv_push_prio))))
  235. goto nla_put_failure;
  236. tcf_tm_dump(&t, &v->tcf_tm);
  237. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  238. goto nla_put_failure;
  239. spin_unlock_bh(&v->tcf_lock);
  240. return skb->len;
  241. nla_put_failure:
  242. spin_unlock_bh(&v->tcf_lock);
  243. nlmsg_trim(skb, b);
  244. return -1;
  245. }
  246. static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
  247. struct netlink_callback *cb, int type,
  248. const struct tc_action_ops *ops,
  249. struct netlink_ext_ack *extack)
  250. {
  251. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  252. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  253. }
  254. static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index,
  255. struct netlink_ext_ack *extack)
  256. {
  257. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  258. return tcf_idr_search(tn, a, index);
  259. }
  260. static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
  261. {
  262. return nla_total_size(sizeof(struct tc_vlan))
  263. + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
  264. + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
  265. + nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
  266. }
  267. static struct tc_action_ops act_vlan_ops = {
  268. .kind = "vlan",
  269. .type = TCA_ACT_VLAN,
  270. .owner = THIS_MODULE,
  271. .act = tcf_vlan_act,
  272. .dump = tcf_vlan_dump,
  273. .init = tcf_vlan_init,
  274. .cleanup = tcf_vlan_cleanup,
  275. .walk = tcf_vlan_walker,
  276. .get_fill_size = tcf_vlan_get_fill_size,
  277. .lookup = tcf_vlan_search,
  278. .size = sizeof(struct tcf_vlan),
  279. };
  280. static __net_init int vlan_init_net(struct net *net)
  281. {
  282. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  283. return tc_action_net_init(net, tn, &act_vlan_ops);
  284. }
  285. static void __net_exit vlan_exit_net(struct list_head *net_list)
  286. {
  287. tc_action_net_exit(net_list, vlan_net_id);
  288. }
  289. static struct pernet_operations vlan_net_ops = {
  290. .init = vlan_init_net,
  291. .exit_batch = vlan_exit_net,
  292. .id = &vlan_net_id,
  293. .size = sizeof(struct tc_action_net),
  294. };
  295. static int __init vlan_init_module(void)
  296. {
  297. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  298. }
  299. static void __exit vlan_cleanup_module(void)
  300. {
  301. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  302. }
  303. module_init(vlan_init_module);
  304. module_exit(vlan_cleanup_module);
  305. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  306. MODULE_DESCRIPTION("vlan manipulation actions");
  307. MODULE_LICENSE("GPL v2");