act_vlan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014 Jiri Pirko <[email protected]>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/if_vlan.h>
  11. #include <net/netlink.h>
  12. #include <net/pkt_sched.h>
  13. #include <net/pkt_cls.h>
  14. #include <linux/tc_act/tc_vlan.h>
  15. #include <net/tc_act/tc_vlan.h>
  16. static struct tc_action_ops act_vlan_ops;
  17. static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a,
  18. struct tcf_result *res)
  19. {
  20. struct tcf_vlan *v = to_vlan(a);
  21. struct tcf_vlan_params *p;
  22. int action;
  23. int err;
  24. u16 tci;
  25. tcf_lastuse_update(&v->tcf_tm);
  26. tcf_action_update_bstats(&v->common, skb);
  27. /* Ensure 'data' points at mac_header prior calling vlan manipulating
  28. * functions.
  29. */
  30. if (skb_at_tc_ingress(skb))
  31. skb_push_rcsum(skb, skb->mac_len);
  32. action = READ_ONCE(v->tcf_action);
  33. p = rcu_dereference_bh(v->vlan_p);
  34. switch (p->tcfv_action) {
  35. case TCA_VLAN_ACT_POP:
  36. err = skb_vlan_pop(skb);
  37. if (err)
  38. goto drop;
  39. break;
  40. case TCA_VLAN_ACT_PUSH:
  41. err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid |
  42. (p->tcfv_push_prio << VLAN_PRIO_SHIFT));
  43. if (err)
  44. goto drop;
  45. break;
  46. case TCA_VLAN_ACT_MODIFY:
  47. /* No-op if no vlan tag (either hw-accel or in-payload) */
  48. if (!skb_vlan_tagged(skb))
  49. goto out;
  50. /* extract existing tag (and guarantee no hw-accel tag) */
  51. if (skb_vlan_tag_present(skb)) {
  52. tci = skb_vlan_tag_get(skb);
  53. __vlan_hwaccel_clear_tag(skb);
  54. } else {
  55. /* in-payload vlan tag, pop it */
  56. err = __skb_vlan_pop(skb, &tci);
  57. if (err)
  58. goto drop;
  59. }
  60. /* replace the vid */
  61. tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid;
  62. /* replace prio bits, if tcfv_push_prio specified */
  63. if (p->tcfv_push_prio_exists) {
  64. tci &= ~VLAN_PRIO_MASK;
  65. tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
  66. }
  67. /* put updated tci as hwaccel tag */
  68. __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
  69. break;
  70. case TCA_VLAN_ACT_POP_ETH:
  71. err = skb_eth_pop(skb);
  72. if (err)
  73. goto drop;
  74. break;
  75. case TCA_VLAN_ACT_PUSH_ETH:
  76. err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src);
  77. if (err)
  78. goto drop;
  79. break;
  80. default:
  81. BUG();
  82. }
  83. out:
  84. if (skb_at_tc_ingress(skb))
  85. skb_pull_rcsum(skb, skb->mac_len);
  86. return action;
  87. drop:
  88. tcf_action_inc_drop_qstats(&v->common);
  89. return TC_ACT_SHOT;
  90. }
  91. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  92. [TCA_VLAN_UNSPEC] = { .strict_start_type = TCA_VLAN_PUSH_ETH_DST },
  93. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  94. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  95. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  96. [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
  97. [TCA_VLAN_PUSH_ETH_DST] = NLA_POLICY_ETH_ADDR,
  98. [TCA_VLAN_PUSH_ETH_SRC] = NLA_POLICY_ETH_ADDR,
  99. };
  100. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  101. struct nlattr *est, struct tc_action **a,
  102. struct tcf_proto *tp, u32 flags,
  103. struct netlink_ext_ack *extack)
  104. {
  105. struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
  106. bool bind = flags & TCA_ACT_FLAGS_BIND;
  107. struct nlattr *tb[TCA_VLAN_MAX + 1];
  108. struct tcf_chain *goto_ch = NULL;
  109. bool push_prio_exists = false;
  110. struct tcf_vlan_params *p;
  111. struct tc_vlan *parm;
  112. struct tcf_vlan *v;
  113. int action;
  114. u16 push_vid = 0;
  115. __be16 push_proto = 0;
  116. u8 push_prio = 0;
  117. bool exists = false;
  118. int ret = 0, err;
  119. u32 index;
  120. if (!nla)
  121. return -EINVAL;
  122. err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy,
  123. NULL);
  124. if (err < 0)
  125. return err;
  126. if (!tb[TCA_VLAN_PARMS])
  127. return -EINVAL;
  128. parm = nla_data(tb[TCA_VLAN_PARMS]);
  129. index = parm->index;
  130. err = tcf_idr_check_alloc(tn, &index, a, bind);
  131. if (err < 0)
  132. return err;
  133. exists = err;
  134. if (exists && bind)
  135. return 0;
  136. switch (parm->v_action) {
  137. case TCA_VLAN_ACT_POP:
  138. break;
  139. case TCA_VLAN_ACT_PUSH:
  140. case TCA_VLAN_ACT_MODIFY:
  141. if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
  142. if (exists)
  143. tcf_idr_release(*a, bind);
  144. else
  145. tcf_idr_cleanup(tn, index);
  146. return -EINVAL;
  147. }
  148. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  149. if (push_vid >= VLAN_VID_MASK) {
  150. if (exists)
  151. tcf_idr_release(*a, bind);
  152. else
  153. tcf_idr_cleanup(tn, index);
  154. return -ERANGE;
  155. }
  156. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  157. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  158. switch (push_proto) {
  159. case htons(ETH_P_8021Q):
  160. case htons(ETH_P_8021AD):
  161. break;
  162. default:
  163. if (exists)
  164. tcf_idr_release(*a, bind);
  165. else
  166. tcf_idr_cleanup(tn, index);
  167. return -EPROTONOSUPPORT;
  168. }
  169. } else {
  170. push_proto = htons(ETH_P_8021Q);
  171. }
  172. push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY];
  173. if (push_prio_exists)
  174. push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
  175. break;
  176. case TCA_VLAN_ACT_POP_ETH:
  177. break;
  178. case TCA_VLAN_ACT_PUSH_ETH:
  179. if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) {
  180. if (exists)
  181. tcf_idr_release(*a, bind);
  182. else
  183. tcf_idr_cleanup(tn, index);
  184. return -EINVAL;
  185. }
  186. break;
  187. default:
  188. if (exists)
  189. tcf_idr_release(*a, bind);
  190. else
  191. tcf_idr_cleanup(tn, index);
  192. return -EINVAL;
  193. }
  194. action = parm->v_action;
  195. if (!exists) {
  196. ret = tcf_idr_create_from_flags(tn, index, est, a,
  197. &act_vlan_ops, bind, flags);
  198. if (ret) {
  199. tcf_idr_cleanup(tn, index);
  200. return ret;
  201. }
  202. ret = ACT_P_CREATED;
  203. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  204. tcf_idr_release(*a, bind);
  205. return -EEXIST;
  206. }
  207. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  208. if (err < 0)
  209. goto release_idr;
  210. v = to_vlan(*a);
  211. p = kzalloc(sizeof(*p), GFP_KERNEL);
  212. if (!p) {
  213. err = -ENOMEM;
  214. goto put_chain;
  215. }
  216. p->tcfv_action = action;
  217. p->tcfv_push_vid = push_vid;
  218. p->tcfv_push_prio = push_prio;
  219. p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH;
  220. p->tcfv_push_proto = push_proto;
  221. if (action == TCA_VLAN_ACT_PUSH_ETH) {
  222. nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST],
  223. ETH_ALEN);
  224. nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC],
  225. ETH_ALEN);
  226. }
  227. spin_lock_bh(&v->tcf_lock);
  228. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  229. p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
  230. spin_unlock_bh(&v->tcf_lock);
  231. if (goto_ch)
  232. tcf_chain_put_by_act(goto_ch);
  233. if (p)
  234. kfree_rcu(p, rcu);
  235. return ret;
  236. put_chain:
  237. if (goto_ch)
  238. tcf_chain_put_by_act(goto_ch);
  239. release_idr:
  240. tcf_idr_release(*a, bind);
  241. return err;
  242. }
  243. static void tcf_vlan_cleanup(struct tc_action *a)
  244. {
  245. struct tcf_vlan *v = to_vlan(a);
  246. struct tcf_vlan_params *p;
  247. p = rcu_dereference_protected(v->vlan_p, 1);
  248. if (p)
  249. kfree_rcu(p, rcu);
  250. }
  251. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  252. int bind, int ref)
  253. {
  254. unsigned char *b = skb_tail_pointer(skb);
  255. struct tcf_vlan *v = to_vlan(a);
  256. struct tcf_vlan_params *p;
  257. struct tc_vlan opt = {
  258. .index = v->tcf_index,
  259. .refcnt = refcount_read(&v->tcf_refcnt) - ref,
  260. .bindcnt = atomic_read(&v->tcf_bindcnt) - bind,
  261. };
  262. struct tcf_t t;
  263. spin_lock_bh(&v->tcf_lock);
  264. opt.action = v->tcf_action;
  265. p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
  266. opt.v_action = p->tcfv_action;
  267. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  268. goto nla_put_failure;
  269. if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
  270. p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
  271. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
  272. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  273. p->tcfv_push_proto) ||
  274. (p->tcfv_push_prio_exists &&
  275. nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
  276. goto nla_put_failure;
  277. if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) {
  278. if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN,
  279. p->tcfv_push_dst))
  280. goto nla_put_failure;
  281. if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN,
  282. p->tcfv_push_src))
  283. goto nla_put_failure;
  284. }
  285. tcf_tm_dump(&t, &v->tcf_tm);
  286. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  287. goto nla_put_failure;
  288. spin_unlock_bh(&v->tcf_lock);
  289. return skb->len;
  290. nla_put_failure:
  291. spin_unlock_bh(&v->tcf_lock);
  292. nlmsg_trim(skb, b);
  293. return -1;
  294. }
  295. static void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  296. u64 drops, u64 lastuse, bool hw)
  297. {
  298. struct tcf_vlan *v = to_vlan(a);
  299. struct tcf_t *tm = &v->tcf_tm;
  300. tcf_action_update_stats(a, bytes, packets, drops, hw);
  301. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  302. }
  303. static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
  304. {
  305. return nla_total_size(sizeof(struct tc_vlan))
  306. + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
  307. + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
  308. + nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
  309. }
  310. static int tcf_vlan_offload_act_setup(struct tc_action *act, void *entry_data,
  311. u32 *index_inc, bool bind,
  312. struct netlink_ext_ack *extack)
  313. {
  314. if (bind) {
  315. struct flow_action_entry *entry = entry_data;
  316. switch (tcf_vlan_action(act)) {
  317. case TCA_VLAN_ACT_PUSH:
  318. entry->id = FLOW_ACTION_VLAN_PUSH;
  319. entry->vlan.vid = tcf_vlan_push_vid(act);
  320. entry->vlan.proto = tcf_vlan_push_proto(act);
  321. entry->vlan.prio = tcf_vlan_push_prio(act);
  322. break;
  323. case TCA_VLAN_ACT_POP:
  324. entry->id = FLOW_ACTION_VLAN_POP;
  325. break;
  326. case TCA_VLAN_ACT_MODIFY:
  327. entry->id = FLOW_ACTION_VLAN_MANGLE;
  328. entry->vlan.vid = tcf_vlan_push_vid(act);
  329. entry->vlan.proto = tcf_vlan_push_proto(act);
  330. entry->vlan.prio = tcf_vlan_push_prio(act);
  331. break;
  332. case TCA_VLAN_ACT_POP_ETH:
  333. entry->id = FLOW_ACTION_VLAN_POP_ETH;
  334. break;
  335. case TCA_VLAN_ACT_PUSH_ETH:
  336. entry->id = FLOW_ACTION_VLAN_PUSH_ETH;
  337. tcf_vlan_push_eth(entry->vlan_push_eth.src, entry->vlan_push_eth.dst, act);
  338. break;
  339. default:
  340. NL_SET_ERR_MSG_MOD(extack, "Unsupported vlan action mode offload");
  341. return -EOPNOTSUPP;
  342. }
  343. *index_inc = 1;
  344. } else {
  345. struct flow_offload_action *fl_action = entry_data;
  346. switch (tcf_vlan_action(act)) {
  347. case TCA_VLAN_ACT_PUSH:
  348. fl_action->id = FLOW_ACTION_VLAN_PUSH;
  349. break;
  350. case TCA_VLAN_ACT_POP:
  351. fl_action->id = FLOW_ACTION_VLAN_POP;
  352. break;
  353. case TCA_VLAN_ACT_MODIFY:
  354. fl_action->id = FLOW_ACTION_VLAN_MANGLE;
  355. break;
  356. case TCA_VLAN_ACT_POP_ETH:
  357. fl_action->id = FLOW_ACTION_VLAN_POP_ETH;
  358. break;
  359. case TCA_VLAN_ACT_PUSH_ETH:
  360. fl_action->id = FLOW_ACTION_VLAN_PUSH_ETH;
  361. break;
  362. default:
  363. return -EOPNOTSUPP;
  364. }
  365. }
  366. return 0;
  367. }
  368. static struct tc_action_ops act_vlan_ops = {
  369. .kind = "vlan",
  370. .id = TCA_ID_VLAN,
  371. .owner = THIS_MODULE,
  372. .act = tcf_vlan_act,
  373. .dump = tcf_vlan_dump,
  374. .init = tcf_vlan_init,
  375. .cleanup = tcf_vlan_cleanup,
  376. .stats_update = tcf_vlan_stats_update,
  377. .get_fill_size = tcf_vlan_get_fill_size,
  378. .offload_act_setup = tcf_vlan_offload_act_setup,
  379. .size = sizeof(struct tcf_vlan),
  380. };
  381. static __net_init int vlan_init_net(struct net *net)
  382. {
  383. struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
  384. return tc_action_net_init(net, tn, &act_vlan_ops);
  385. }
  386. static void __net_exit vlan_exit_net(struct list_head *net_list)
  387. {
  388. tc_action_net_exit(net_list, act_vlan_ops.net_id);
  389. }
  390. static struct pernet_operations vlan_net_ops = {
  391. .init = vlan_init_net,
  392. .exit_batch = vlan_exit_net,
  393. .id = &act_vlan_ops.net_id,
  394. .size = sizeof(struct tc_action_net),
  395. };
  396. static int __init vlan_init_module(void)
  397. {
  398. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  399. }
  400. static void __exit vlan_cleanup_module(void)
  401. {
  402. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  403. }
  404. module_init(vlan_init_module);
  405. module_exit(vlan_cleanup_module);
  406. MODULE_AUTHOR("Jiri Pirko <[email protected]>");
  407. MODULE_DESCRIPTION("vlan manipulation actions");
  408. MODULE_LICENSE("GPL v2");