lwtunnel.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_IP6:
  44. case LWTUNNEL_ENCAP_IP:
  45. case LWTUNNEL_ENCAP_NONE:
  46. case __LWTUNNEL_ENCAP_MAX:
  47. /* should not have got here */
  48. WARN_ON(1);
  49. break;
  50. }
  51. return NULL;
  52. }
  53. #endif /* CONFIG_MODULES */
  54. struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
  55. {
  56. struct lwtunnel_state *lws;
  57. lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
  58. return lws;
  59. }
  60. EXPORT_SYMBOL(lwtunnel_state_alloc);
  61. static const struct lwtunnel_encap_ops __rcu *
  62. lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
  63. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
  64. unsigned int num)
  65. {
  66. if (num > LWTUNNEL_ENCAP_MAX)
  67. return -ERANGE;
  68. return !cmpxchg((const struct lwtunnel_encap_ops **)
  69. &lwtun_encaps[num],
  70. NULL, ops) ? 0 : -1;
  71. }
  72. EXPORT_SYMBOL(lwtunnel_encap_add_ops);
  73. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
  74. unsigned int encap_type)
  75. {
  76. int ret;
  77. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  78. encap_type > LWTUNNEL_ENCAP_MAX)
  79. return -ERANGE;
  80. ret = (cmpxchg((const struct lwtunnel_encap_ops **)
  81. &lwtun_encaps[encap_type],
  82. ops, NULL) == ops) ? 0 : -1;
  83. synchronize_net();
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(lwtunnel_encap_del_ops);
  87. int lwtunnel_build_state(u16 encap_type,
  88. struct nlattr *encap, unsigned int family,
  89. const void *cfg, struct lwtunnel_state **lws)
  90. {
  91. const struct lwtunnel_encap_ops *ops;
  92. int ret = -EINVAL;
  93. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  94. encap_type > LWTUNNEL_ENCAP_MAX)
  95. return ret;
  96. ret = -EOPNOTSUPP;
  97. rcu_read_lock();
  98. ops = rcu_dereference(lwtun_encaps[encap_type]);
  99. if (likely(ops && ops->build_state && try_module_get(ops->owner))) {
  100. ret = ops->build_state(encap, family, cfg, lws);
  101. if (ret)
  102. module_put(ops->owner);
  103. }
  104. rcu_read_unlock();
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(lwtunnel_build_state);
  108. int lwtunnel_valid_encap_type(u16 encap_type)
  109. {
  110. const struct lwtunnel_encap_ops *ops;
  111. int ret = -EINVAL;
  112. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  113. encap_type > LWTUNNEL_ENCAP_MAX)
  114. return ret;
  115. rcu_read_lock();
  116. ops = rcu_dereference(lwtun_encaps[encap_type]);
  117. rcu_read_unlock();
  118. #ifdef CONFIG_MODULES
  119. if (!ops) {
  120. const char *encap_type_str = lwtunnel_encap_str(encap_type);
  121. if (encap_type_str) {
  122. __rtnl_unlock();
  123. request_module("rtnl-lwt-%s", encap_type_str);
  124. rtnl_lock();
  125. rcu_read_lock();
  126. ops = rcu_dereference(lwtun_encaps[encap_type]);
  127. rcu_read_unlock();
  128. }
  129. }
  130. #endif
  131. return ops ? 0 : -EOPNOTSUPP;
  132. }
  133. EXPORT_SYMBOL(lwtunnel_valid_encap_type);
  134. int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining)
  135. {
  136. struct rtnexthop *rtnh = (struct rtnexthop *)attr;
  137. struct nlattr *nla_entype;
  138. struct nlattr *attrs;
  139. struct nlattr *nla;
  140. u16 encap_type;
  141. int attrlen;
  142. while (rtnh_ok(rtnh, remaining)) {
  143. attrlen = rtnh_attrlen(rtnh);
  144. if (attrlen > 0) {
  145. attrs = rtnh_attrs(rtnh);
  146. nla = nla_find(attrs, attrlen, RTA_ENCAP);
  147. nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  148. if (nla_entype) {
  149. encap_type = nla_get_u16(nla_entype);
  150. if (lwtunnel_valid_encap_type(encap_type) != 0)
  151. return -EOPNOTSUPP;
  152. }
  153. }
  154. rtnh = rtnh_next(rtnh, &remaining);
  155. }
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(lwtunnel_valid_encap_type_attr);
  159. void lwtstate_free(struct lwtunnel_state *lws)
  160. {
  161. const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
  162. if (ops->destroy_state) {
  163. ops->destroy_state(lws);
  164. kfree_rcu(lws, rcu);
  165. } else {
  166. kfree(lws);
  167. }
  168. module_put(ops->owner);
  169. }
  170. EXPORT_SYMBOL(lwtstate_free);
  171. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
  172. {
  173. const struct lwtunnel_encap_ops *ops;
  174. struct nlattr *nest;
  175. int ret = -EINVAL;
  176. if (!lwtstate)
  177. return 0;
  178. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  179. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  180. return 0;
  181. ret = -EOPNOTSUPP;
  182. nest = nla_nest_start(skb, RTA_ENCAP);
  183. rcu_read_lock();
  184. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  185. if (likely(ops && ops->fill_encap))
  186. ret = ops->fill_encap(skb, lwtstate);
  187. rcu_read_unlock();
  188. if (ret)
  189. goto nla_put_failure;
  190. nla_nest_end(skb, nest);
  191. ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
  192. if (ret)
  193. goto nla_put_failure;
  194. return 0;
  195. nla_put_failure:
  196. nla_nest_cancel(skb, nest);
  197. return (ret == -EOPNOTSUPP ? 0 : ret);
  198. }
  199. EXPORT_SYMBOL(lwtunnel_fill_encap);
  200. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  201. {
  202. const struct lwtunnel_encap_ops *ops;
  203. int ret = 0;
  204. if (!lwtstate)
  205. return 0;
  206. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  207. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  208. return 0;
  209. rcu_read_lock();
  210. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  211. if (likely(ops && ops->get_encap_size))
  212. ret = nla_total_size(ops->get_encap_size(lwtstate));
  213. rcu_read_unlock();
  214. return ret;
  215. }
  216. EXPORT_SYMBOL(lwtunnel_get_encap_size);
  217. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  218. {
  219. const struct lwtunnel_encap_ops *ops;
  220. int ret = 0;
  221. if (!a && !b)
  222. return 0;
  223. if (!a || !b)
  224. return 1;
  225. if (a->type != b->type)
  226. return 1;
  227. if (a->type == LWTUNNEL_ENCAP_NONE ||
  228. a->type > LWTUNNEL_ENCAP_MAX)
  229. return 0;
  230. rcu_read_lock();
  231. ops = rcu_dereference(lwtun_encaps[a->type]);
  232. if (likely(ops && ops->cmp_encap))
  233. ret = ops->cmp_encap(a, b);
  234. rcu_read_unlock();
  235. return ret;
  236. }
  237. EXPORT_SYMBOL(lwtunnel_cmp_encap);
  238. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  239. {
  240. struct dst_entry *dst = skb_dst(skb);
  241. const struct lwtunnel_encap_ops *ops;
  242. struct lwtunnel_state *lwtstate;
  243. int ret = -EINVAL;
  244. if (!dst)
  245. goto drop;
  246. lwtstate = dst->lwtstate;
  247. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  248. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  249. return 0;
  250. ret = -EOPNOTSUPP;
  251. rcu_read_lock();
  252. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  253. if (likely(ops && ops->output))
  254. ret = ops->output(net, sk, skb);
  255. rcu_read_unlock();
  256. if (ret == -EOPNOTSUPP)
  257. goto drop;
  258. return ret;
  259. drop:
  260. kfree_skb(skb);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL(lwtunnel_output);
  264. int lwtunnel_xmit(struct sk_buff *skb)
  265. {
  266. struct dst_entry *dst = skb_dst(skb);
  267. const struct lwtunnel_encap_ops *ops;
  268. struct lwtunnel_state *lwtstate;
  269. int ret = -EINVAL;
  270. if (!dst)
  271. goto drop;
  272. lwtstate = dst->lwtstate;
  273. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  274. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  275. return 0;
  276. ret = -EOPNOTSUPP;
  277. rcu_read_lock();
  278. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  279. if (likely(ops && ops->xmit))
  280. ret = ops->xmit(skb);
  281. rcu_read_unlock();
  282. if (ret == -EOPNOTSUPP)
  283. goto drop;
  284. return ret;
  285. drop:
  286. kfree_skb(skb);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(lwtunnel_xmit);
  290. int lwtunnel_input(struct sk_buff *skb)
  291. {
  292. struct dst_entry *dst = skb_dst(skb);
  293. const struct lwtunnel_encap_ops *ops;
  294. struct lwtunnel_state *lwtstate;
  295. int ret = -EINVAL;
  296. if (!dst)
  297. goto drop;
  298. lwtstate = dst->lwtstate;
  299. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  300. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  301. return 0;
  302. ret = -EOPNOTSUPP;
  303. rcu_read_lock();
  304. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  305. if (likely(ops && ops->input))
  306. ret = ops->input(skb);
  307. rcu_read_unlock();
  308. if (ret == -EOPNOTSUPP)
  309. goto drop;
  310. return ret;
  311. drop:
  312. kfree_skb(skb);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL(lwtunnel_input);