lwtunnel.c 9.2 KB

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