act_vlan.c 7.9 KB

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