act_vlan.c 11 KB

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