lwtunnel.c 8.9 KB

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