lwtunnel.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_SEG6:
  39. return "SEG6";
  40. case LWTUNNEL_ENCAP_BPF:
  41. return "BPF";
  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(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(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(lwtunnel_encap_del_ops);
  86. int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
  87. struct nlattr *encap, unsigned int family,
  88. const void *cfg, struct lwtunnel_state **lws)
  89. {
  90. const struct lwtunnel_encap_ops *ops;
  91. int ret = -EINVAL;
  92. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  93. encap_type > LWTUNNEL_ENCAP_MAX)
  94. return ret;
  95. ret = -EOPNOTSUPP;
  96. rcu_read_lock();
  97. ops = rcu_dereference(lwtun_encaps[encap_type]);
  98. #ifdef CONFIG_MODULES
  99. if (!ops) {
  100. const char *encap_type_str = lwtunnel_encap_str(encap_type);
  101. if (encap_type_str) {
  102. rcu_read_unlock();
  103. request_module("rtnl-lwt-%s", encap_type_str);
  104. rcu_read_lock();
  105. ops = rcu_dereference(lwtun_encaps[encap_type]);
  106. }
  107. }
  108. #endif
  109. if (likely(ops && ops->build_state))
  110. ret = ops->build_state(dev, encap, family, cfg, lws);
  111. rcu_read_unlock();
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(lwtunnel_build_state);
  115. void lwtstate_free(struct lwtunnel_state *lws)
  116. {
  117. const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
  118. if (ops->destroy_state) {
  119. ops->destroy_state(lws);
  120. kfree_rcu(lws, rcu);
  121. } else {
  122. kfree(lws);
  123. }
  124. }
  125. EXPORT_SYMBOL(lwtstate_free);
  126. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
  127. {
  128. const struct lwtunnel_encap_ops *ops;
  129. struct nlattr *nest;
  130. int ret = -EINVAL;
  131. if (!lwtstate)
  132. return 0;
  133. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  134. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  135. return 0;
  136. ret = -EOPNOTSUPP;
  137. nest = nla_nest_start(skb, RTA_ENCAP);
  138. rcu_read_lock();
  139. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  140. if (likely(ops && ops->fill_encap))
  141. ret = ops->fill_encap(skb, lwtstate);
  142. rcu_read_unlock();
  143. if (ret)
  144. goto nla_put_failure;
  145. nla_nest_end(skb, nest);
  146. ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
  147. if (ret)
  148. goto nla_put_failure;
  149. return 0;
  150. nla_put_failure:
  151. nla_nest_cancel(skb, nest);
  152. return (ret == -EOPNOTSUPP ? 0 : ret);
  153. }
  154. EXPORT_SYMBOL(lwtunnel_fill_encap);
  155. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  156. {
  157. const struct lwtunnel_encap_ops *ops;
  158. int ret = 0;
  159. if (!lwtstate)
  160. return 0;
  161. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  162. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  163. return 0;
  164. rcu_read_lock();
  165. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  166. if (likely(ops && ops->get_encap_size))
  167. ret = nla_total_size(ops->get_encap_size(lwtstate));
  168. rcu_read_unlock();
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(lwtunnel_get_encap_size);
  172. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  173. {
  174. const struct lwtunnel_encap_ops *ops;
  175. int ret = 0;
  176. if (!a && !b)
  177. return 0;
  178. if (!a || !b)
  179. return 1;
  180. if (a->type != b->type)
  181. return 1;
  182. if (a->type == LWTUNNEL_ENCAP_NONE ||
  183. a->type > LWTUNNEL_ENCAP_MAX)
  184. return 0;
  185. rcu_read_lock();
  186. ops = rcu_dereference(lwtun_encaps[a->type]);
  187. if (likely(ops && ops->cmp_encap))
  188. ret = ops->cmp_encap(a, b);
  189. rcu_read_unlock();
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(lwtunnel_cmp_encap);
  193. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  194. {
  195. struct dst_entry *dst = skb_dst(skb);
  196. const struct lwtunnel_encap_ops *ops;
  197. struct lwtunnel_state *lwtstate;
  198. int ret = -EINVAL;
  199. if (!dst)
  200. goto drop;
  201. lwtstate = dst->lwtstate;
  202. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  203. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  204. return 0;
  205. ret = -EOPNOTSUPP;
  206. rcu_read_lock();
  207. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  208. if (likely(ops && ops->output))
  209. ret = ops->output(net, sk, skb);
  210. rcu_read_unlock();
  211. if (ret == -EOPNOTSUPP)
  212. goto drop;
  213. return ret;
  214. drop:
  215. kfree_skb(skb);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(lwtunnel_output);
  219. int lwtunnel_xmit(struct sk_buff *skb)
  220. {
  221. struct dst_entry *dst = skb_dst(skb);
  222. const struct lwtunnel_encap_ops *ops;
  223. struct lwtunnel_state *lwtstate;
  224. int ret = -EINVAL;
  225. if (!dst)
  226. goto drop;
  227. lwtstate = dst->lwtstate;
  228. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  229. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  230. return 0;
  231. ret = -EOPNOTSUPP;
  232. rcu_read_lock();
  233. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  234. if (likely(ops && ops->xmit))
  235. ret = ops->xmit(skb);
  236. rcu_read_unlock();
  237. if (ret == -EOPNOTSUPP)
  238. goto drop;
  239. return ret;
  240. drop:
  241. kfree_skb(skb);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL(lwtunnel_xmit);
  245. int lwtunnel_input(struct sk_buff *skb)
  246. {
  247. struct dst_entry *dst = skb_dst(skb);
  248. const struct lwtunnel_encap_ops *ops;
  249. struct lwtunnel_state *lwtstate;
  250. int ret = -EINVAL;
  251. if (!dst)
  252. goto drop;
  253. lwtstate = dst->lwtstate;
  254. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  255. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  256. return 0;
  257. ret = -EOPNOTSUPP;
  258. rcu_read_lock();
  259. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  260. if (likely(ops && ops->input))
  261. ret = ops->input(skb);
  262. rcu_read_unlock();
  263. if (ret == -EOPNOTSUPP)
  264. goto drop;
  265. return ret;
  266. drop:
  267. kfree_skb(skb);
  268. return ret;
  269. }
  270. EXPORT_SYMBOL(lwtunnel_input);