lwtunnel.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #ifdef CONFIG_MODULES
  24. static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
  25. {
  26. /* Only lwt encaps implemented without using an interface for
  27. * the encap need to return a string here.
  28. */
  29. switch (encap_type) {
  30. case LWTUNNEL_ENCAP_MPLS:
  31. return "MPLS";
  32. case LWTUNNEL_ENCAP_ILA:
  33. return "ILA";
  34. case LWTUNNEL_ENCAP_SEG6:
  35. return "SEG6";
  36. case LWTUNNEL_ENCAP_BPF:
  37. return "BPF";
  38. case LWTUNNEL_ENCAP_SEG6_LOCAL:
  39. return "SEG6LOCAL";
  40. case LWTUNNEL_ENCAP_RPL:
  41. return "RPL";
  42. case LWTUNNEL_ENCAP_IP6:
  43. case LWTUNNEL_ENCAP_IP:
  44. case LWTUNNEL_ENCAP_NONE:
  45. case __LWTUNNEL_ENCAP_MAX:
  46. /* should not have got here */
  47. WARN_ON(1);
  48. break;
  49. }
  50. return NULL;
  51. }
  52. #endif /* CONFIG_MODULES */
  53. struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
  54. {
  55. struct lwtunnel_state *lws;
  56. lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
  57. return lws;
  58. }
  59. EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
  60. static const struct lwtunnel_encap_ops __rcu *
  61. lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
  62. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
  63. unsigned int num)
  64. {
  65. if (num > LWTUNNEL_ENCAP_MAX)
  66. return -ERANGE;
  67. return !cmpxchg((const struct lwtunnel_encap_ops **)
  68. &lwtun_encaps[num],
  69. NULL, ops) ? 0 : -1;
  70. }
  71. EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
  72. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
  73. unsigned int encap_type)
  74. {
  75. int ret;
  76. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  77. encap_type > LWTUNNEL_ENCAP_MAX)
  78. return -ERANGE;
  79. ret = (cmpxchg((const struct lwtunnel_encap_ops **)
  80. &lwtun_encaps[encap_type],
  81. ops, NULL) == ops) ? 0 : -1;
  82. synchronize_net();
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
  86. int lwtunnel_build_state(struct net *net, u16 encap_type,
  87. struct nlattr *encap, unsigned int family,
  88. const void *cfg, struct lwtunnel_state **lws,
  89. struct netlink_ext_ack *extack)
  90. {
  91. const struct lwtunnel_encap_ops *ops;
  92. bool found = false;
  93. int ret = -EINVAL;
  94. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  95. encap_type > LWTUNNEL_ENCAP_MAX) {
  96. NL_SET_ERR_MSG_ATTR(extack, encap,
  97. "Unknown LWT encapsulation type");
  98. return ret;
  99. }
  100. ret = -EOPNOTSUPP;
  101. rcu_read_lock();
  102. ops = rcu_dereference(lwtun_encaps[encap_type]);
  103. if (likely(ops && ops->build_state && try_module_get(ops->owner)))
  104. found = true;
  105. rcu_read_unlock();
  106. if (found) {
  107. ret = ops->build_state(net, encap, family, cfg, lws, extack);
  108. if (ret)
  109. module_put(ops->owner);
  110. } else {
  111. /* don't rely on -EOPNOTSUPP to detect match as build_state
  112. * handlers could return it
  113. */
  114. NL_SET_ERR_MSG_ATTR(extack, encap,
  115. "LWT encapsulation type not supported");
  116. }
  117. return ret;
  118. }
  119. EXPORT_SYMBOL_GPL(lwtunnel_build_state);
  120. int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
  121. {
  122. const struct lwtunnel_encap_ops *ops;
  123. int ret = -EINVAL;
  124. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  125. encap_type > LWTUNNEL_ENCAP_MAX) {
  126. NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
  127. return ret;
  128. }
  129. rcu_read_lock();
  130. ops = rcu_dereference(lwtun_encaps[encap_type]);
  131. rcu_read_unlock();
  132. #ifdef CONFIG_MODULES
  133. if (!ops) {
  134. const char *encap_type_str = lwtunnel_encap_str(encap_type);
  135. if (encap_type_str) {
  136. __rtnl_unlock();
  137. request_module("rtnl-lwt-%s", encap_type_str);
  138. rtnl_lock();
  139. rcu_read_lock();
  140. ops = rcu_dereference(lwtun_encaps[encap_type]);
  141. rcu_read_unlock();
  142. }
  143. }
  144. #endif
  145. ret = ops ? 0 : -EOPNOTSUPP;
  146. if (ret < 0)
  147. NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
  151. int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
  152. struct netlink_ext_ack *extack)
  153. {
  154. struct rtnexthop *rtnh = (struct rtnexthop *)attr;
  155. struct nlattr *nla_entype;
  156. struct nlattr *attrs;
  157. u16 encap_type;
  158. int attrlen;
  159. while (rtnh_ok(rtnh, remaining)) {
  160. attrlen = rtnh_attrlen(rtnh);
  161. if (attrlen > 0) {
  162. attrs = rtnh_attrs(rtnh);
  163. nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  164. if (nla_entype) {
  165. if (nla_len(nla_entype) < sizeof(u16)) {
  166. NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
  167. return -EINVAL;
  168. }
  169. encap_type = nla_get_u16(nla_entype);
  170. if (lwtunnel_valid_encap_type(encap_type,
  171. extack) != 0)
  172. return -EOPNOTSUPP;
  173. }
  174. }
  175. rtnh = rtnh_next(rtnh, &remaining);
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
  180. void lwtstate_free(struct lwtunnel_state *lws)
  181. {
  182. const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
  183. if (ops->destroy_state) {
  184. ops->destroy_state(lws);
  185. kfree_rcu(lws, rcu);
  186. } else {
  187. kfree(lws);
  188. }
  189. module_put(ops->owner);
  190. }
  191. EXPORT_SYMBOL_GPL(lwtstate_free);
  192. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
  193. int encap_attr, int encap_type_attr)
  194. {
  195. const struct lwtunnel_encap_ops *ops;
  196. struct nlattr *nest;
  197. int ret;
  198. if (!lwtstate)
  199. return 0;
  200. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  201. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  202. return 0;
  203. nest = nla_nest_start_noflag(skb, encap_attr);
  204. if (!nest)
  205. return -EMSGSIZE;
  206. ret = -EOPNOTSUPP;
  207. rcu_read_lock();
  208. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  209. if (likely(ops && ops->fill_encap))
  210. ret = ops->fill_encap(skb, lwtstate);
  211. rcu_read_unlock();
  212. if (ret)
  213. goto nla_put_failure;
  214. nla_nest_end(skb, nest);
  215. ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
  216. if (ret)
  217. goto nla_put_failure;
  218. return 0;
  219. nla_put_failure:
  220. nla_nest_cancel(skb, nest);
  221. return (ret == -EOPNOTSUPP ? 0 : ret);
  222. }
  223. EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
  224. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  225. {
  226. const struct lwtunnel_encap_ops *ops;
  227. int ret = 0;
  228. if (!lwtstate)
  229. return 0;
  230. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  231. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  232. return 0;
  233. rcu_read_lock();
  234. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  235. if (likely(ops && ops->get_encap_size))
  236. ret = nla_total_size(ops->get_encap_size(lwtstate));
  237. rcu_read_unlock();
  238. return ret;
  239. }
  240. EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
  241. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  242. {
  243. const struct lwtunnel_encap_ops *ops;
  244. int ret = 0;
  245. if (!a && !b)
  246. return 0;
  247. if (!a || !b)
  248. return 1;
  249. if (a->type != b->type)
  250. return 1;
  251. if (a->type == LWTUNNEL_ENCAP_NONE ||
  252. a->type > LWTUNNEL_ENCAP_MAX)
  253. return 0;
  254. rcu_read_lock();
  255. ops = rcu_dereference(lwtun_encaps[a->type]);
  256. if (likely(ops && ops->cmp_encap))
  257. ret = ops->cmp_encap(a, b);
  258. rcu_read_unlock();
  259. return ret;
  260. }
  261. EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
  262. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  263. {
  264. struct dst_entry *dst = skb_dst(skb);
  265. const struct lwtunnel_encap_ops *ops;
  266. struct lwtunnel_state *lwtstate;
  267. int ret = -EINVAL;
  268. if (!dst)
  269. goto drop;
  270. lwtstate = dst->lwtstate;
  271. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  272. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  273. return 0;
  274. ret = -EOPNOTSUPP;
  275. rcu_read_lock();
  276. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  277. if (likely(ops && ops->output))
  278. ret = ops->output(net, sk, skb);
  279. rcu_read_unlock();
  280. if (ret == -EOPNOTSUPP)
  281. goto drop;
  282. return ret;
  283. drop:
  284. kfree_skb(skb);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL_GPL(lwtunnel_output);
  288. int lwtunnel_xmit(struct sk_buff *skb)
  289. {
  290. struct dst_entry *dst = skb_dst(skb);
  291. const struct lwtunnel_encap_ops *ops;
  292. struct lwtunnel_state *lwtstate;
  293. int ret = -EINVAL;
  294. if (!dst)
  295. goto drop;
  296. lwtstate = dst->lwtstate;
  297. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  298. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  299. return 0;
  300. ret = -EOPNOTSUPP;
  301. rcu_read_lock();
  302. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  303. if (likely(ops && ops->xmit))
  304. ret = ops->xmit(skb);
  305. rcu_read_unlock();
  306. if (ret == -EOPNOTSUPP)
  307. goto drop;
  308. return ret;
  309. drop:
  310. kfree_skb(skb);
  311. return ret;
  312. }
  313. EXPORT_SYMBOL_GPL(lwtunnel_xmit);
  314. int lwtunnel_input(struct sk_buff *skb)
  315. {
  316. struct dst_entry *dst = skb_dst(skb);
  317. const struct lwtunnel_encap_ops *ops;
  318. struct lwtunnel_state *lwtstate;
  319. int ret = -EINVAL;
  320. if (!dst)
  321. goto drop;
  322. lwtstate = dst->lwtstate;
  323. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  324. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  325. return 0;
  326. ret = -EOPNOTSUPP;
  327. rcu_read_lock();
  328. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  329. if (likely(ops && ops->input))
  330. ret = ops->input(skb);
  331. rcu_read_unlock();
  332. if (ret == -EOPNOTSUPP)
  333. goto drop;
  334. return ret;
  335. drop:
  336. kfree_skb(skb);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL_GPL(lwtunnel_input);