lwtunnel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #ifdef CONFIG_MODULES
  28. static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
  29. {
  30. /* Only lwt encaps implemented without using an interface for
  31. * the encap need to return a string here.
  32. */
  33. switch (encap_type) {
  34. case LWTUNNEL_ENCAP_MPLS:
  35. return "MPLS";
  36. case LWTUNNEL_ENCAP_ILA:
  37. return "ILA";
  38. case LWTUNNEL_ENCAP_IP6:
  39. case LWTUNNEL_ENCAP_IP:
  40. case LWTUNNEL_ENCAP_NONE:
  41. case __LWTUNNEL_ENCAP_MAX:
  42. /* should not have got here */
  43. WARN_ON(1);
  44. break;
  45. }
  46. return NULL;
  47. }
  48. #endif /* CONFIG_MODULES */
  49. struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
  50. {
  51. struct lwtunnel_state *lws;
  52. lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
  53. return lws;
  54. }
  55. EXPORT_SYMBOL(lwtunnel_state_alloc);
  56. static const struct lwtunnel_encap_ops __rcu *
  57. lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
  58. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
  59. unsigned int num)
  60. {
  61. if (num > LWTUNNEL_ENCAP_MAX)
  62. return -ERANGE;
  63. return !cmpxchg((const struct lwtunnel_encap_ops **)
  64. &lwtun_encaps[num],
  65. NULL, ops) ? 0 : -1;
  66. }
  67. EXPORT_SYMBOL(lwtunnel_encap_add_ops);
  68. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
  69. unsigned int encap_type)
  70. {
  71. int ret;
  72. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  73. encap_type > LWTUNNEL_ENCAP_MAX)
  74. return -ERANGE;
  75. ret = (cmpxchg((const struct lwtunnel_encap_ops **)
  76. &lwtun_encaps[encap_type],
  77. ops, NULL) == ops) ? 0 : -1;
  78. synchronize_net();
  79. return ret;
  80. }
  81. EXPORT_SYMBOL(lwtunnel_encap_del_ops);
  82. int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
  83. struct nlattr *encap, unsigned int family,
  84. const void *cfg, struct lwtunnel_state **lws)
  85. {
  86. const struct lwtunnel_encap_ops *ops;
  87. int ret = -EINVAL;
  88. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  89. encap_type > LWTUNNEL_ENCAP_MAX)
  90. return ret;
  91. ret = -EOPNOTSUPP;
  92. rcu_read_lock();
  93. ops = rcu_dereference(lwtun_encaps[encap_type]);
  94. #ifdef CONFIG_MODULES
  95. if (!ops) {
  96. const char *encap_type_str = lwtunnel_encap_str(encap_type);
  97. if (encap_type_str) {
  98. rcu_read_unlock();
  99. request_module("rtnl-lwt-%s", encap_type_str);
  100. rcu_read_lock();
  101. ops = rcu_dereference(lwtun_encaps[encap_type]);
  102. }
  103. }
  104. #endif
  105. if (likely(ops && ops->build_state))
  106. ret = ops->build_state(dev, encap, family, cfg, lws);
  107. rcu_read_unlock();
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(lwtunnel_build_state);
  111. void lwtstate_free(struct lwtunnel_state *lws)
  112. {
  113. const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
  114. if (ops->destroy_state) {
  115. ops->destroy_state(lws);
  116. kfree_rcu(lws, rcu);
  117. } else {
  118. kfree(lws);
  119. }
  120. }
  121. EXPORT_SYMBOL(lwtstate_free);
  122. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
  123. {
  124. const struct lwtunnel_encap_ops *ops;
  125. struct nlattr *nest;
  126. int ret = -EINVAL;
  127. if (!lwtstate)
  128. return 0;
  129. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  130. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  131. return 0;
  132. ret = -EOPNOTSUPP;
  133. nest = nla_nest_start(skb, RTA_ENCAP);
  134. rcu_read_lock();
  135. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  136. if (likely(ops && ops->fill_encap))
  137. ret = ops->fill_encap(skb, lwtstate);
  138. rcu_read_unlock();
  139. if (ret)
  140. goto nla_put_failure;
  141. nla_nest_end(skb, nest);
  142. ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
  143. if (ret)
  144. goto nla_put_failure;
  145. return 0;
  146. nla_put_failure:
  147. nla_nest_cancel(skb, nest);
  148. return (ret == -EOPNOTSUPP ? 0 : ret);
  149. }
  150. EXPORT_SYMBOL(lwtunnel_fill_encap);
  151. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  152. {
  153. const struct lwtunnel_encap_ops *ops;
  154. int ret = 0;
  155. if (!lwtstate)
  156. return 0;
  157. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  158. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  159. return 0;
  160. rcu_read_lock();
  161. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  162. if (likely(ops && ops->get_encap_size))
  163. ret = nla_total_size(ops->get_encap_size(lwtstate));
  164. rcu_read_unlock();
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(lwtunnel_get_encap_size);
  168. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  169. {
  170. const struct lwtunnel_encap_ops *ops;
  171. int ret = 0;
  172. if (!a && !b)
  173. return 0;
  174. if (!a || !b)
  175. return 1;
  176. if (a->type != b->type)
  177. return 1;
  178. if (a->type == LWTUNNEL_ENCAP_NONE ||
  179. a->type > LWTUNNEL_ENCAP_MAX)
  180. return 0;
  181. rcu_read_lock();
  182. ops = rcu_dereference(lwtun_encaps[a->type]);
  183. if (likely(ops && ops->cmp_encap))
  184. ret = ops->cmp_encap(a, b);
  185. rcu_read_unlock();
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(lwtunnel_cmp_encap);
  189. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  190. {
  191. struct dst_entry *dst = skb_dst(skb);
  192. const struct lwtunnel_encap_ops *ops;
  193. struct lwtunnel_state *lwtstate;
  194. int ret = -EINVAL;
  195. if (!dst)
  196. goto drop;
  197. lwtstate = dst->lwtstate;
  198. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  199. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  200. return 0;
  201. ret = -EOPNOTSUPP;
  202. rcu_read_lock();
  203. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  204. if (likely(ops && ops->output))
  205. ret = ops->output(net, sk, skb);
  206. rcu_read_unlock();
  207. if (ret == -EOPNOTSUPP)
  208. goto drop;
  209. return ret;
  210. drop:
  211. kfree_skb(skb);
  212. return ret;
  213. }
  214. EXPORT_SYMBOL(lwtunnel_output);
  215. int lwtunnel_xmit(struct sk_buff *skb)
  216. {
  217. struct dst_entry *dst = skb_dst(skb);
  218. const struct lwtunnel_encap_ops *ops;
  219. struct lwtunnel_state *lwtstate;
  220. int ret = -EINVAL;
  221. if (!dst)
  222. goto drop;
  223. lwtstate = dst->lwtstate;
  224. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  225. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  226. return 0;
  227. ret = -EOPNOTSUPP;
  228. rcu_read_lock();
  229. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  230. if (likely(ops && ops->xmit))
  231. ret = ops->xmit(skb);
  232. rcu_read_unlock();
  233. if (ret == -EOPNOTSUPP)
  234. goto drop;
  235. return ret;
  236. drop:
  237. kfree_skb(skb);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(lwtunnel_xmit);
  241. int lwtunnel_input(struct sk_buff *skb)
  242. {
  243. struct dst_entry *dst = skb_dst(skb);
  244. const struct lwtunnel_encap_ops *ops;
  245. struct lwtunnel_state *lwtstate;
  246. int ret = -EINVAL;
  247. if (!dst)
  248. goto drop;
  249. lwtstate = dst->lwtstate;
  250. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  251. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  252. return 0;
  253. ret = -EOPNOTSUPP;
  254. rcu_read_lock();
  255. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  256. if (likely(ops && ops->input))
  257. ret = ops->input(skb);
  258. rcu_read_unlock();
  259. if (ret == -EOPNOTSUPP)
  260. goto drop;
  261. return ret;
  262. drop:
  263. kfree_skb(skb);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(lwtunnel_input);