act_mpls.c 13 KB

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