act_vlan.c 12 KB

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