lwtunnel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lwtunnel Infrastructure for light weight tunnels like mpls
  4. *
  5. * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
  6. */
  7. #include <linux/capability.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/lwtunnel.h>
  16. #include <linux/in.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <net/lwtunnel.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/ip6_fib.h>
  22. #include <net/rtnh.h>
  23. #include "dev.h"
  24. DEFINE_STATIC_KEY_FALSE(nf_hooks_lwtunnel_enabled);
  25. EXPORT_SYMBOL_GPL(nf_hooks_lwtunnel_enabled);
  26. #ifdef CONFIG_MODULES
  27. static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
  28. {
  29. /* Only lwt encaps implemented without using an interface for
  30. * the encap need to return a string here.
  31. */
  32. switch (encap_type) {
  33. case LWTUNNEL_ENCAP_MPLS:
  34. return "MPLS";
  35. case LWTUNNEL_ENCAP_ILA:
  36. return "ILA";
  37. case LWTUNNEL_ENCAP_SEG6:
  38. return "SEG6";
  39. case LWTUNNEL_ENCAP_BPF:
  40. return "BPF";
  41. case LWTUNNEL_ENCAP_SEG6_LOCAL:
  42. return "SEG6LOCAL";
  43. case LWTUNNEL_ENCAP_RPL:
  44. return "RPL";
  45. case LWTUNNEL_ENCAP_IOAM6:
  46. return "IOAM6";
  47. case LWTUNNEL_ENCAP_XFRM:
  48. /* module autoload not supported for encap type */
  49. return NULL;
  50. case LWTUNNEL_ENCAP_IP6:
  51. case LWTUNNEL_ENCAP_IP:
  52. case LWTUNNEL_ENCAP_NONE:
  53. case __LWTUNNEL_ENCAP_MAX:
  54. /* should not have got here */
  55. WARN_ON(1);
  56. break;
  57. }
  58. return NULL;
  59. }
  60. #endif /* CONFIG_MODULES */
  61. struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
  62. {
  63. struct lwtunnel_state *lws;
  64. lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
  65. return lws;
  66. }
  67. EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
  68. static const struct lwtunnel_encap_ops __rcu *
  69. lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
  70. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
  71. unsigned int num)
  72. {
  73. if (num > LWTUNNEL_ENCAP_MAX)
  74. return -ERANGE;
  75. return !cmpxchg((const struct lwtunnel_encap_ops **)
  76. &lwtun_encaps[num],
  77. NULL, ops) ? 0 : -1;
  78. }
  79. EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
  80. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
  81. unsigned int encap_type)
  82. {
  83. int ret;
  84. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  85. encap_type > LWTUNNEL_ENCAP_MAX)
  86. return -ERANGE;
  87. ret = (cmpxchg((const struct lwtunnel_encap_ops **)
  88. &lwtun_encaps[encap_type],
  89. ops, NULL) == ops) ? 0 : -1;
  90. synchronize_net();
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
  94. int lwtunnel_build_state(struct net *net, u16 encap_type,
  95. struct nlattr *encap, unsigned int family,
  96. const void *cfg, struct lwtunnel_state **lws,
  97. struct netlink_ext_ack *extack)
  98. {
  99. const struct lwtunnel_encap_ops *ops;
  100. bool found = false;
  101. int ret = -EINVAL;
  102. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  103. encap_type > LWTUNNEL_ENCAP_MAX) {
  104. NL_SET_ERR_MSG_ATTR(extack, encap,
  105. "Unknown LWT encapsulation type");
  106. return ret;
  107. }
  108. ret = -EOPNOTSUPP;
  109. rcu_read_lock();
  110. ops = rcu_dereference(lwtun_encaps[encap_type]);
  111. if (likely(ops && ops->build_state && try_module_get(ops->owner)))
  112. found = true;
  113. rcu_read_unlock();
  114. if (found) {
  115. ret = ops->build_state(net, encap, family, cfg, lws, extack);
  116. if (ret)
  117. module_put(ops->owner);
  118. } else {
  119. /* don't rely on -EOPNOTSUPP to detect match as build_state
  120. * handlers could return it
  121. */
  122. NL_SET_ERR_MSG_ATTR(extack, encap,
  123. "LWT encapsulation type not supported");
  124. }
  125. return ret;
  126. }
  127. EXPORT_SYMBOL_GPL(lwtunnel_build_state);
  128. int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
  129. {
  130. const struct lwtunnel_encap_ops *ops;
  131. int ret = -EINVAL;
  132. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  133. encap_type > LWTUNNEL_ENCAP_MAX) {
  134. NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
  135. return ret;
  136. }
  137. rcu_read_lock();
  138. ops = rcu_dereference(lwtun_encaps[encap_type]);
  139. rcu_read_unlock();
  140. #ifdef CONFIG_MODULES
  141. if (!ops) {
  142. const char *encap_type_str = lwtunnel_encap_str(encap_type);
  143. if (encap_type_str) {
  144. __rtnl_unlock();
  145. request_module("rtnl-lwt-%s", encap_type_str);
  146. rtnl_lock();
  147. rcu_read_lock();
  148. ops = rcu_dereference(lwtun_encaps[encap_type]);
  149. rcu_read_unlock();
  150. }
  151. }
  152. #endif
  153. ret = ops ? 0 : -EOPNOTSUPP;
  154. if (ret < 0)
  155. NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
  156. return ret;
  157. }
  158. EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
  159. int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
  160. struct netlink_ext_ack *extack)
  161. {
  162. struct rtnexthop *rtnh = (struct rtnexthop *)attr;
  163. struct nlattr *nla_entype;
  164. struct nlattr *attrs;
  165. u16 encap_type;
  166. int attrlen;
  167. while (rtnh_ok(rtnh, remaining)) {
  168. attrlen = rtnh_attrlen(rtnh);
  169. if (attrlen > 0) {
  170. attrs = rtnh_attrs(rtnh);
  171. nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  172. if (nla_entype) {
  173. if (nla_len(nla_entype) < sizeof(u16)) {
  174. NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
  175. return -EINVAL;
  176. }
  177. encap_type = nla_get_u16(nla_entype);
  178. if (lwtunnel_valid_encap_type(encap_type,
  179. extack) != 0)
  180. return -EOPNOTSUPP;
  181. }
  182. }
  183. rtnh = rtnh_next(rtnh, &remaining);
  184. }
  185. return 0;
  186. }
  187. EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
  188. void lwtstate_free(struct lwtunnel_state *lws)
  189. {
  190. const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
  191. if (ops->destroy_state) {
  192. ops->destroy_state(lws);
  193. kfree_rcu(lws, rcu);
  194. } else {
  195. kfree(lws);
  196. }
  197. module_put(ops->owner);
  198. }
  199. EXPORT_SYMBOL_GPL(lwtstate_free);
  200. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
  201. int encap_attr, int encap_type_attr)
  202. {
  203. const struct lwtunnel_encap_ops *ops;
  204. struct nlattr *nest;
  205. int ret;
  206. if (!lwtstate)
  207. return 0;
  208. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  209. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  210. return 0;
  211. nest = nla_nest_start_noflag(skb, encap_attr);
  212. if (!nest)
  213. return -EMSGSIZE;
  214. ret = -EOPNOTSUPP;
  215. rcu_read_lock();
  216. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  217. if (likely(ops && ops->fill_encap))
  218. ret = ops->fill_encap(skb, lwtstate);
  219. rcu_read_unlock();
  220. if (ret)
  221. goto nla_put_failure;
  222. nla_nest_end(skb, nest);
  223. ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
  224. if (ret)
  225. goto nla_put_failure;
  226. return 0;
  227. nla_put_failure:
  228. nla_nest_cancel(skb, nest);
  229. return (ret == -EOPNOTSUPP ? 0 : ret);
  230. }
  231. EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
  232. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  233. {
  234. const struct lwtunnel_encap_ops *ops;
  235. int ret = 0;
  236. if (!lwtstate)
  237. return 0;
  238. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  239. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  240. return 0;
  241. rcu_read_lock();
  242. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  243. if (likely(ops && ops->get_encap_size))
  244. ret = nla_total_size(ops->get_encap_size(lwtstate));
  245. rcu_read_unlock();
  246. return ret;
  247. }
  248. EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
  249. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  250. {
  251. const struct lwtunnel_encap_ops *ops;
  252. int ret = 0;
  253. if (!a && !b)
  254. return 0;
  255. if (!a || !b)
  256. return 1;
  257. if (a->type != b->type)
  258. return 1;
  259. if (a->type == LWTUNNEL_ENCAP_NONE ||
  260. a->type > LWTUNNEL_ENCAP_MAX)
  261. return 0;
  262. rcu_read_lock();
  263. ops = rcu_dereference(lwtun_encaps[a->type]);
  264. if (likely(ops && ops->cmp_encap))
  265. ret = ops->cmp_encap(a, b);
  266. rcu_read_unlock();
  267. return ret;
  268. }
  269. EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
  270. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  271. {
  272. const struct lwtunnel_encap_ops *ops;
  273. struct lwtunnel_state *lwtstate;
  274. struct dst_entry *dst;
  275. int ret;
  276. local_bh_disable();
  277. if (dev_xmit_recursion()) {
  278. net_crit_ratelimited("%s(): recursion limit reached on datapath\n",
  279. __func__);
  280. ret = -ENETDOWN;
  281. goto drop;
  282. }
  283. dst = skb_dst(skb);
  284. if (!dst) {
  285. ret = -EINVAL;
  286. goto drop;
  287. }
  288. lwtstate = dst->lwtstate;
  289. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  290. lwtstate->type > LWTUNNEL_ENCAP_MAX) {
  291. ret = 0;
  292. goto out;
  293. }
  294. ret = -EOPNOTSUPP;
  295. rcu_read_lock();
  296. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  297. if (likely(ops && ops->output)) {
  298. dev_xmit_recursion_inc();
  299. ret = ops->output(net, sk, skb);
  300. dev_xmit_recursion_dec();
  301. }
  302. rcu_read_unlock();
  303. if (ret == -EOPNOTSUPP)
  304. goto drop;
  305. goto out;
  306. drop:
  307. kfree_skb(skb);
  308. out:
  309. local_bh_enable();
  310. return ret;
  311. }
  312. EXPORT_SYMBOL_GPL(lwtunnel_output);
  313. int lwtunnel_xmit(struct sk_buff *skb)
  314. {
  315. const struct lwtunnel_encap_ops *ops;
  316. struct lwtunnel_state *lwtstate;
  317. struct dst_entry *dst;
  318. int ret;
  319. local_bh_disable();
  320. if (dev_xmit_recursion()) {
  321. net_crit_ratelimited("%s(): recursion limit reached on datapath\n",
  322. __func__);
  323. ret = -ENETDOWN;
  324. goto drop;
  325. }
  326. dst = skb_dst(skb);
  327. if (!dst) {
  328. ret = -EINVAL;
  329. goto drop;
  330. }
  331. lwtstate = dst->lwtstate;
  332. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  333. lwtstate->type > LWTUNNEL_ENCAP_MAX) {
  334. ret = 0;
  335. goto out;
  336. }
  337. ret = -EOPNOTSUPP;
  338. rcu_read_lock();
  339. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  340. if (likely(ops && ops->xmit)) {
  341. dev_xmit_recursion_inc();
  342. ret = ops->xmit(skb);
  343. dev_xmit_recursion_dec();
  344. }
  345. rcu_read_unlock();
  346. if (ret == -EOPNOTSUPP)
  347. goto drop;
  348. goto out;
  349. drop:
  350. kfree_skb(skb);
  351. out:
  352. local_bh_enable();
  353. return ret;
  354. }
  355. EXPORT_SYMBOL_GPL(lwtunnel_xmit);
  356. int lwtunnel_input(struct sk_buff *skb)
  357. {
  358. const struct lwtunnel_encap_ops *ops;
  359. struct lwtunnel_state *lwtstate;
  360. struct dst_entry *dst;
  361. int ret;
  362. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  363. if (dev_xmit_recursion()) {
  364. net_crit_ratelimited("%s(): recursion limit reached on datapath\n",
  365. __func__);
  366. ret = -ENETDOWN;
  367. goto drop;
  368. }
  369. dst = skb_dst(skb);
  370. if (!dst) {
  371. ret = -EINVAL;
  372. goto drop;
  373. }
  374. lwtstate = dst->lwtstate;
  375. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  376. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  377. return 0;
  378. ret = -EOPNOTSUPP;
  379. rcu_read_lock();
  380. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  381. if (likely(ops && ops->input)) {
  382. dev_xmit_recursion_inc();
  383. ret = ops->input(skb);
  384. dev_xmit_recursion_dec();
  385. }
  386. rcu_read_unlock();
  387. if (ret == -EOPNOTSUPP)
  388. goto drop;
  389. return ret;
  390. drop:
  391. kfree_skb(skb);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL_GPL(lwtunnel_input);