act_mpls.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2019 Netronome Systems, Inc. */
  3. #include <linux/if_arp.h>
  4. #include <linux/init.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/mpls.h>
  8. #include <linux/rtnetlink.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/tc_act/tc_mpls.h>
  11. #include <net/mpls.h>
  12. #include <net/netlink.h>
  13. #include <net/pkt_sched.h>
  14. #include <net/pkt_cls.h>
  15. #include <net/tc_act/tc_mpls.h>
  16. static struct tc_action_ops act_mpls_ops;
  17. #define ACT_MPLS_TTL_DEFAULT 255
  18. static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
  19. struct tcf_mpls_params *p, bool set_bos)
  20. {
  21. u32 new_lse = 0;
  22. if (lse)
  23. new_lse = be32_to_cpu(lse->label_stack_entry);
  24. if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
  25. new_lse &= ~MPLS_LS_LABEL_MASK;
  26. new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
  27. }
  28. if (p->tcfm_ttl) {
  29. new_lse &= ~MPLS_LS_TTL_MASK;
  30. new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
  31. }
  32. if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
  33. new_lse &= ~MPLS_LS_TC_MASK;
  34. new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
  35. }
  36. if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
  37. new_lse &= ~MPLS_LS_S_MASK;
  38. new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
  39. } else if (set_bos) {
  40. new_lse |= 1 << MPLS_LS_S_SHIFT;
  41. }
  42. return cpu_to_be32(new_lse);
  43. }
  44. static int tcf_mpls_act(struct sk_buff *skb, const struct tc_action *a,
  45. struct tcf_result *res)
  46. {
  47. struct tcf_mpls *m = to_mpls(a);
  48. struct tcf_mpls_params *p;
  49. __be32 new_lse;
  50. int ret, mac_len;
  51. tcf_lastuse_update(&m->tcf_tm);
  52. bstats_update(this_cpu_ptr(m->common.cpu_bstats), skb);
  53. /* Ensure 'data' points at mac_header prior calling mpls manipulating
  54. * functions.
  55. */
  56. if (skb_at_tc_ingress(skb)) {
  57. skb_push_rcsum(skb, skb->mac_len);
  58. mac_len = skb->mac_len;
  59. } else {
  60. mac_len = skb_network_header(skb) - skb_mac_header(skb);
  61. }
  62. ret = READ_ONCE(m->tcf_action);
  63. p = rcu_dereference_bh(m->mpls_p);
  64. switch (p->tcfm_action) {
  65. case TCA_MPLS_ACT_POP:
  66. if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
  67. skb->dev && skb->dev->type == ARPHRD_ETHER))
  68. goto drop;
  69. break;
  70. case TCA_MPLS_ACT_PUSH:
  71. new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
  72. if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
  73. skb->dev && skb->dev->type == ARPHRD_ETHER))
  74. goto drop;
  75. break;
  76. case TCA_MPLS_ACT_MAC_PUSH:
  77. if (skb_vlan_tag_present(skb)) {
  78. if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
  79. skb_vlan_tag_get(skb),
  80. ETH_HLEN) < 0)
  81. goto drop;
  82. skb->protocol = skb->vlan_proto;
  83. __vlan_hwaccel_clear_tag(skb);
  84. }
  85. new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
  86. !eth_p_mpls(skb->protocol));
  87. if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
  88. goto drop;
  89. break;
  90. case TCA_MPLS_ACT_MODIFY:
  91. if (!pskb_may_pull(skb,
  92. skb_network_offset(skb) + MPLS_HLEN))
  93. goto drop;
  94. new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
  95. if (skb_mpls_update_lse(skb, new_lse))
  96. goto drop;
  97. break;
  98. case TCA_MPLS_ACT_DEC_TTL:
  99. if (skb_mpls_dec_ttl(skb))
  100. goto drop;
  101. break;
  102. }
  103. if (skb_at_tc_ingress(skb))
  104. skb_pull_rcsum(skb, skb->mac_len);
  105. return ret;
  106. drop:
  107. qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
  108. return TC_ACT_SHOT;
  109. }
  110. static int valid_label(const struct nlattr *attr,
  111. struct netlink_ext_ack *extack)
  112. {
  113. const u32 *label = nla_data(attr);
  114. if (nla_len(attr) != sizeof(*label)) {
  115. NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length");
  116. return -EINVAL;
  117. }
  118. if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
  119. NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
  125. [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
  126. [TCA_MPLS_PROTO] = { .type = NLA_U16 },
  127. [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
  128. valid_label),
  129. [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
  130. [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
  131. [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
  132. };
  133. static int tcf_mpls_init(struct net *net, struct nlattr *nla,
  134. struct nlattr *est, struct tc_action **a,
  135. struct tcf_proto *tp, u32 flags,
  136. struct netlink_ext_ack *extack)
  137. {
  138. struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
  139. bool bind = flags & TCA_ACT_FLAGS_BIND;
  140. struct nlattr *tb[TCA_MPLS_MAX + 1];
  141. struct tcf_chain *goto_ch = NULL;
  142. struct tcf_mpls_params *p;
  143. struct tc_mpls *parm;
  144. bool exists = false;
  145. struct tcf_mpls *m;
  146. int ret = 0, err;
  147. u8 mpls_ttl = 0;
  148. u32 index;
  149. if (!nla) {
  150. NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
  151. return -EINVAL;
  152. }
  153. err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
  154. if (err < 0)
  155. return err;
  156. if (!tb[TCA_MPLS_PARMS]) {
  157. NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
  158. return -EINVAL;
  159. }
  160. parm = nla_data(tb[TCA_MPLS_PARMS]);
  161. index = parm->index;
  162. err = tcf_idr_check_alloc(tn, &index, a, bind);
  163. if (err < 0)
  164. return err;
  165. exists = err;
  166. if (exists && bind)
  167. return 0;
  168. if (!exists) {
  169. ret = tcf_idr_create(tn, index, est, a, &act_mpls_ops, bind,
  170. true, flags);
  171. if (ret) {
  172. tcf_idr_cleanup(tn, index);
  173. return ret;
  174. }
  175. ret = ACT_P_CREATED;
  176. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  177. tcf_idr_release(*a, bind);
  178. return -EEXIST;
  179. }
  180. /* Verify parameters against action type. */
  181. switch (parm->m_action) {
  182. case TCA_MPLS_ACT_POP:
  183. if (!tb[TCA_MPLS_PROTO]) {
  184. NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
  185. err = -EINVAL;
  186. goto release_idr;
  187. }
  188. if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
  189. NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
  190. err = -EINVAL;
  191. goto release_idr;
  192. }
  193. if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
  194. tb[TCA_MPLS_BOS]) {
  195. NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
  196. err = -EINVAL;
  197. goto release_idr;
  198. }
  199. break;
  200. case TCA_MPLS_ACT_DEC_TTL:
  201. if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
  202. tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
  203. NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
  204. err = -EINVAL;
  205. goto release_idr;
  206. }
  207. break;
  208. case TCA_MPLS_ACT_PUSH:
  209. case TCA_MPLS_ACT_MAC_PUSH:
  210. if (!tb[TCA_MPLS_LABEL]) {
  211. NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
  212. err = -EINVAL;
  213. goto release_idr;
  214. }
  215. if (tb[TCA_MPLS_PROTO] &&
  216. !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
  217. NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
  218. err = -EPROTONOSUPPORT;
  219. goto release_idr;
  220. }
  221. /* Push needs a TTL - if not specified, set a default value. */
  222. if (!tb[TCA_MPLS_TTL]) {
  223. #if IS_ENABLED(CONFIG_MPLS)
  224. mpls_ttl = net->mpls.default_ttl ?
  225. net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
  226. #else
  227. mpls_ttl = ACT_MPLS_TTL_DEFAULT;
  228. #endif
  229. }
  230. break;
  231. case TCA_MPLS_ACT_MODIFY:
  232. if (tb[TCA_MPLS_PROTO]) {
  233. NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
  234. err = -EINVAL;
  235. goto release_idr;
  236. }
  237. break;
  238. default:
  239. NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
  240. err = -EINVAL;
  241. goto release_idr;
  242. }
  243. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  244. if (err < 0)
  245. goto release_idr;
  246. m = to_mpls(*a);
  247. p = kzalloc(sizeof(*p), GFP_KERNEL);
  248. if (!p) {
  249. err = -ENOMEM;
  250. goto put_chain;
  251. }
  252. p->tcfm_action = parm->m_action;
  253. p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
  254. ACT_MPLS_LABEL_NOT_SET;
  255. p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
  256. ACT_MPLS_TC_NOT_SET;
  257. p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
  258. mpls_ttl;
  259. p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
  260. ACT_MPLS_BOS_NOT_SET;
  261. p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
  262. htons(ETH_P_MPLS_UC);
  263. spin_lock_bh(&m->tcf_lock);
  264. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  265. p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
  266. spin_unlock_bh(&m->tcf_lock);
  267. if (goto_ch)
  268. tcf_chain_put_by_act(goto_ch);
  269. if (p)
  270. kfree_rcu(p, rcu);
  271. return ret;
  272. put_chain:
  273. if (goto_ch)
  274. tcf_chain_put_by_act(goto_ch);
  275. release_idr:
  276. tcf_idr_release(*a, bind);
  277. return err;
  278. }
  279. static void tcf_mpls_cleanup(struct tc_action *a)
  280. {
  281. struct tcf_mpls *m = to_mpls(a);
  282. struct tcf_mpls_params *p;
  283. p = rcu_dereference_protected(m->mpls_p, 1);
  284. if (p)
  285. kfree_rcu(p, rcu);
  286. }
  287. static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
  288. int bind, int ref)
  289. {
  290. unsigned char *b = skb_tail_pointer(skb);
  291. struct tcf_mpls *m = to_mpls(a);
  292. struct tcf_mpls_params *p;
  293. struct tc_mpls opt = {
  294. .index = m->tcf_index,
  295. .refcnt = refcount_read(&m->tcf_refcnt) - ref,
  296. .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
  297. };
  298. struct tcf_t t;
  299. spin_lock_bh(&m->tcf_lock);
  300. opt.action = m->tcf_action;
  301. p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
  302. opt.m_action = p->tcfm_action;
  303. if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
  304. goto nla_put_failure;
  305. if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
  306. nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
  307. goto nla_put_failure;
  308. if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
  309. nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
  310. goto nla_put_failure;
  311. if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
  312. goto nla_put_failure;
  313. if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
  314. nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
  315. goto nla_put_failure;
  316. if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
  317. goto nla_put_failure;
  318. tcf_tm_dump(&t, &m->tcf_tm);
  319. if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
  320. goto nla_put_failure;
  321. spin_unlock_bh(&m->tcf_lock);
  322. return skb->len;
  323. nla_put_failure:
  324. spin_unlock_bh(&m->tcf_lock);
  325. nlmsg_trim(skb, b);
  326. return -EMSGSIZE;
  327. }
  328. static int tcf_mpls_offload_act_setup(struct tc_action *act, void *entry_data,
  329. u32 *index_inc, bool bind,
  330. struct netlink_ext_ack *extack)
  331. {
  332. if (bind) {
  333. struct flow_action_entry *entry = entry_data;
  334. switch (tcf_mpls_action(act)) {
  335. case TCA_MPLS_ACT_PUSH:
  336. entry->id = FLOW_ACTION_MPLS_PUSH;
  337. entry->mpls_push.proto = tcf_mpls_proto(act);
  338. entry->mpls_push.label = tcf_mpls_label(act);
  339. entry->mpls_push.tc = tcf_mpls_tc(act);
  340. entry->mpls_push.bos = tcf_mpls_bos(act);
  341. entry->mpls_push.ttl = tcf_mpls_ttl(act);
  342. break;
  343. case TCA_MPLS_ACT_POP:
  344. entry->id = FLOW_ACTION_MPLS_POP;
  345. entry->mpls_pop.proto = tcf_mpls_proto(act);
  346. break;
  347. case TCA_MPLS_ACT_MODIFY:
  348. entry->id = FLOW_ACTION_MPLS_MANGLE;
  349. entry->mpls_mangle.label = tcf_mpls_label(act);
  350. entry->mpls_mangle.tc = tcf_mpls_tc(act);
  351. entry->mpls_mangle.bos = tcf_mpls_bos(act);
  352. entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
  353. break;
  354. case TCA_MPLS_ACT_DEC_TTL:
  355. NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"dec_ttl\" option is used");
  356. return -EOPNOTSUPP;
  357. case TCA_MPLS_ACT_MAC_PUSH:
  358. NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"mac_push\" option is used");
  359. return -EOPNOTSUPP;
  360. default:
  361. NL_SET_ERR_MSG_MOD(extack, "Unsupported MPLS mode offload");
  362. return -EOPNOTSUPP;
  363. }
  364. *index_inc = 1;
  365. } else {
  366. struct flow_offload_action *fl_action = entry_data;
  367. switch (tcf_mpls_action(act)) {
  368. case TCA_MPLS_ACT_PUSH:
  369. fl_action->id = FLOW_ACTION_MPLS_PUSH;
  370. break;
  371. case TCA_MPLS_ACT_POP:
  372. fl_action->id = FLOW_ACTION_MPLS_POP;
  373. break;
  374. case TCA_MPLS_ACT_MODIFY:
  375. fl_action->id = FLOW_ACTION_MPLS_MANGLE;
  376. break;
  377. default:
  378. return -EOPNOTSUPP;
  379. }
  380. }
  381. return 0;
  382. }
  383. static struct tc_action_ops act_mpls_ops = {
  384. .kind = "mpls",
  385. .id = TCA_ID_MPLS,
  386. .owner = THIS_MODULE,
  387. .act = tcf_mpls_act,
  388. .dump = tcf_mpls_dump,
  389. .init = tcf_mpls_init,
  390. .cleanup = tcf_mpls_cleanup,
  391. .offload_act_setup = tcf_mpls_offload_act_setup,
  392. .size = sizeof(struct tcf_mpls),
  393. };
  394. static __net_init int mpls_init_net(struct net *net)
  395. {
  396. struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
  397. return tc_action_net_init(net, tn, &act_mpls_ops);
  398. }
  399. static void __net_exit mpls_exit_net(struct list_head *net_list)
  400. {
  401. tc_action_net_exit(net_list, act_mpls_ops.net_id);
  402. }
  403. static struct pernet_operations mpls_net_ops = {
  404. .init = mpls_init_net,
  405. .exit_batch = mpls_exit_net,
  406. .id = &act_mpls_ops.net_id,
  407. .size = sizeof(struct tc_action_net),
  408. };
  409. static int __init mpls_init_module(void)
  410. {
  411. return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
  412. }
  413. static void __exit mpls_cleanup_module(void)
  414. {
  415. tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
  416. }
  417. module_init(mpls_init_module);
  418. module_exit(mpls_cleanup_module);
  419. MODULE_SOFTDEP("post: mpls_gso");
  420. MODULE_AUTHOR("Netronome Systems <[email protected]>");
  421. MODULE_LICENSE("GPL");
  422. MODULE_DESCRIPTION("MPLS manipulation actions");