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. #include <net/tc_wrapper.h>
  17. static struct tc_action_ops act_mpls_ops;
  18. #define ACT_MPLS_TTL_DEFAULT 255
  19. static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
  20. struct tcf_mpls_params *p, bool set_bos)
  21. {
  22. u32 new_lse = 0;
  23. if (lse)
  24. new_lse = be32_to_cpu(lse->label_stack_entry);
  25. if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
  26. new_lse &= ~MPLS_LS_LABEL_MASK;
  27. new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
  28. }
  29. if (p->tcfm_ttl) {
  30. new_lse &= ~MPLS_LS_TTL_MASK;
  31. new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
  32. }
  33. if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
  34. new_lse &= ~MPLS_LS_TC_MASK;
  35. new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
  36. }
  37. if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
  38. new_lse &= ~MPLS_LS_S_MASK;
  39. new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
  40. } else if (set_bos) {
  41. new_lse |= 1 << MPLS_LS_S_SHIFT;
  42. }
  43. return cpu_to_be32(new_lse);
  44. }
  45. TC_INDIRECT_SCOPE int tcf_mpls_act(struct sk_buff *skb,
  46. const struct tc_action *a,
  47. struct tcf_result *res)
  48. {
  49. struct tcf_mpls *m = to_mpls(a);
  50. struct tcf_mpls_params *p;
  51. __be32 new_lse;
  52. int mac_len;
  53. tcf_lastuse_update(&m->tcf_tm);
  54. bstats_update(this_cpu_ptr(m->common.cpu_bstats), skb);
  55. /* Ensure 'data' points at mac_header prior calling mpls manipulating
  56. * functions.
  57. */
  58. if (skb_at_tc_ingress(skb)) {
  59. skb_push_rcsum(skb, skb->mac_len);
  60. mac_len = skb->mac_len;
  61. } else {
  62. mac_len = skb_network_offset(skb);
  63. }
  64. p = rcu_dereference_bh(m->mpls_p);
  65. switch (p->tcfm_action) {
  66. case TCA_MPLS_ACT_POP:
  67. if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
  68. skb->dev && skb->dev->type == ARPHRD_ETHER))
  69. goto drop;
  70. break;
  71. case TCA_MPLS_ACT_PUSH:
  72. new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
  73. if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
  74. skb->dev && skb->dev->type == ARPHRD_ETHER))
  75. goto drop;
  76. break;
  77. case TCA_MPLS_ACT_MAC_PUSH:
  78. if (skb_vlan_tag_present(skb)) {
  79. if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
  80. skb_vlan_tag_get(skb),
  81. ETH_HLEN) < 0)
  82. goto drop;
  83. skb->protocol = skb->vlan_proto;
  84. __vlan_hwaccel_clear_tag(skb);
  85. }
  86. new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
  87. !eth_p_mpls(skb->protocol));
  88. if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
  89. goto drop;
  90. break;
  91. case TCA_MPLS_ACT_MODIFY:
  92. if (!pskb_may_pull(skb,
  93. skb_network_offset(skb) + MPLS_HLEN))
  94. goto drop;
  95. new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
  96. if (skb_mpls_update_lse(skb, new_lse))
  97. goto drop;
  98. break;
  99. case TCA_MPLS_ACT_DEC_TTL:
  100. if (skb_mpls_dec_ttl(skb))
  101. goto drop;
  102. break;
  103. }
  104. if (skb_at_tc_ingress(skb))
  105. skb_pull_rcsum(skb, skb->mac_len);
  106. return p->action;
  107. drop:
  108. qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
  109. return TC_ACT_SHOT;
  110. }
  111. static int valid_label(const struct nlattr *attr,
  112. struct netlink_ext_ack *extack)
  113. {
  114. const u32 *label = nla_data(attr);
  115. if (nla_len(attr) != sizeof(*label)) {
  116. NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length");
  117. return -EINVAL;
  118. }
  119. if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
  120. NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
  121. return -EINVAL;
  122. }
  123. return 0;
  124. }
  125. static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
  126. [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
  127. [TCA_MPLS_PROTO] = { .type = NLA_U16 },
  128. [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
  129. valid_label),
  130. [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
  131. [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
  132. [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
  133. };
  134. static int tcf_mpls_init(struct net *net, struct nlattr *nla,
  135. struct nlattr *est, struct tc_action **a,
  136. struct tcf_proto *tp, u32 flags,
  137. struct netlink_ext_ack *extack)
  138. {
  139. struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
  140. bool bind = flags & TCA_ACT_FLAGS_BIND;
  141. struct nlattr *tb[TCA_MPLS_MAX + 1];
  142. struct tcf_chain *goto_ch = NULL;
  143. struct tcf_mpls_params *p;
  144. struct tc_mpls *parm;
  145. bool exists = false;
  146. struct tcf_mpls *m;
  147. int ret = 0, err;
  148. u8 mpls_ttl = 0;
  149. u32 index;
  150. if (!nla) {
  151. NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
  152. return -EINVAL;
  153. }
  154. err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
  155. if (err < 0)
  156. return err;
  157. if (!tb[TCA_MPLS_PARMS]) {
  158. NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
  159. return -EINVAL;
  160. }
  161. parm = nla_data(tb[TCA_MPLS_PARMS]);
  162. index = parm->index;
  163. err = tcf_idr_check_alloc(tn, &index, a, bind);
  164. if (err < 0)
  165. return err;
  166. exists = err;
  167. if (exists && bind)
  168. return ACT_P_BOUND;
  169. if (!exists) {
  170. ret = tcf_idr_create(tn, index, est, a, &act_mpls_ops, bind,
  171. true, flags);
  172. if (ret) {
  173. tcf_idr_cleanup(tn, index);
  174. return ret;
  175. }
  176. ret = ACT_P_CREATED;
  177. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  178. tcf_idr_release(*a, bind);
  179. return -EEXIST;
  180. }
  181. /* Verify parameters against action type. */
  182. switch (parm->m_action) {
  183. case TCA_MPLS_ACT_POP:
  184. if (!tb[TCA_MPLS_PROTO]) {
  185. NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
  186. err = -EINVAL;
  187. goto release_idr;
  188. }
  189. if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
  190. NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
  191. err = -EINVAL;
  192. goto release_idr;
  193. }
  194. if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
  195. tb[TCA_MPLS_BOS]) {
  196. NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
  197. err = -EINVAL;
  198. goto release_idr;
  199. }
  200. break;
  201. case TCA_MPLS_ACT_DEC_TTL:
  202. if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
  203. tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
  204. NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
  205. err = -EINVAL;
  206. goto release_idr;
  207. }
  208. break;
  209. case TCA_MPLS_ACT_PUSH:
  210. case TCA_MPLS_ACT_MAC_PUSH:
  211. if (!tb[TCA_MPLS_LABEL]) {
  212. NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
  213. err = -EINVAL;
  214. goto release_idr;
  215. }
  216. if (tb[TCA_MPLS_PROTO] &&
  217. !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
  218. NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
  219. err = -EPROTONOSUPPORT;
  220. goto release_idr;
  221. }
  222. /* Push needs a TTL - if not specified, set a default value. */
  223. if (!tb[TCA_MPLS_TTL]) {
  224. #if IS_ENABLED(CONFIG_MPLS)
  225. mpls_ttl = net->mpls.default_ttl ?
  226. net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
  227. #else
  228. mpls_ttl = ACT_MPLS_TTL_DEFAULT;
  229. #endif
  230. }
  231. break;
  232. case TCA_MPLS_ACT_MODIFY:
  233. if (tb[TCA_MPLS_PROTO]) {
  234. NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
  235. err = -EINVAL;
  236. goto release_idr;
  237. }
  238. break;
  239. default:
  240. NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
  241. err = -EINVAL;
  242. goto release_idr;
  243. }
  244. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  245. if (err < 0)
  246. goto release_idr;
  247. m = to_mpls(*a);
  248. p = kzalloc_obj(*p);
  249. if (!p) {
  250. err = -ENOMEM;
  251. goto put_chain;
  252. }
  253. p->tcfm_action = parm->m_action;
  254. p->tcfm_label = nla_get_u32_default(tb[TCA_MPLS_LABEL],
  255. ACT_MPLS_LABEL_NOT_SET);
  256. p->tcfm_tc = nla_get_u8_default(tb[TCA_MPLS_TC], ACT_MPLS_TC_NOT_SET);
  257. p->tcfm_ttl = nla_get_u8_default(tb[TCA_MPLS_TTL], mpls_ttl);
  258. p->tcfm_bos = nla_get_u8_default(tb[TCA_MPLS_BOS],
  259. ACT_MPLS_BOS_NOT_SET);
  260. p->tcfm_proto = nla_get_be16_default(tb[TCA_MPLS_PROTO],
  261. htons(ETH_P_MPLS_UC));
  262. p->action = parm->action;
  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. const struct tcf_mpls *m = to_mpls(a);
  292. const 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. rcu_read_lock();
  300. p = rcu_dereference(m->mpls_p);
  301. opt.m_action = p->tcfm_action;
  302. opt.action = p->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. rcu_read_unlock();
  322. return skb->len;
  323. nla_put_failure:
  324. rcu_read_unlock();
  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. MODULE_ALIAS_NET_ACT("mpls");
  395. static __net_init int mpls_init_net(struct net *net)
  396. {
  397. struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
  398. return tc_action_net_init(net, tn, &act_mpls_ops);
  399. }
  400. static void __net_exit mpls_exit_net(struct list_head *net_list)
  401. {
  402. tc_action_net_exit(net_list, act_mpls_ops.net_id);
  403. }
  404. static struct pernet_operations mpls_net_ops = {
  405. .init = mpls_init_net,
  406. .exit_batch = mpls_exit_net,
  407. .id = &act_mpls_ops.net_id,
  408. .size = sizeof(struct tc_action_net),
  409. };
  410. static int __init mpls_init_module(void)
  411. {
  412. return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
  413. }
  414. static void __exit mpls_cleanup_module(void)
  415. {
  416. tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
  417. }
  418. module_init(mpls_init_module);
  419. module_exit(mpls_cleanup_module);
  420. MODULE_SOFTDEP("post: mpls_gso");
  421. MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
  422. MODULE_LICENSE("GPL");
  423. MODULE_DESCRIPTION("MPLS manipulation actions");