lwtunnel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
  112. {
  113. const struct lwtunnel_encap_ops *ops;
  114. struct nlattr *nest;
  115. int ret = -EINVAL;
  116. if (!lwtstate)
  117. return 0;
  118. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  119. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  120. return 0;
  121. ret = -EOPNOTSUPP;
  122. nest = nla_nest_start(skb, RTA_ENCAP);
  123. rcu_read_lock();
  124. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  125. if (likely(ops && ops->fill_encap))
  126. ret = ops->fill_encap(skb, lwtstate);
  127. rcu_read_unlock();
  128. if (ret)
  129. goto nla_put_failure;
  130. nla_nest_end(skb, nest);
  131. ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
  132. if (ret)
  133. goto nla_put_failure;
  134. return 0;
  135. nla_put_failure:
  136. nla_nest_cancel(skb, nest);
  137. return (ret == -EOPNOTSUPP ? 0 : ret);
  138. }
  139. EXPORT_SYMBOL(lwtunnel_fill_encap);
  140. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  141. {
  142. const struct lwtunnel_encap_ops *ops;
  143. int ret = 0;
  144. if (!lwtstate)
  145. return 0;
  146. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  147. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  148. return 0;
  149. rcu_read_lock();
  150. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  151. if (likely(ops && ops->get_encap_size))
  152. ret = nla_total_size(ops->get_encap_size(lwtstate));
  153. rcu_read_unlock();
  154. return ret;
  155. }
  156. EXPORT_SYMBOL(lwtunnel_get_encap_size);
  157. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  158. {
  159. const struct lwtunnel_encap_ops *ops;
  160. int ret = 0;
  161. if (!a && !b)
  162. return 0;
  163. if (!a || !b)
  164. return 1;
  165. if (a->type != b->type)
  166. return 1;
  167. if (a->type == LWTUNNEL_ENCAP_NONE ||
  168. a->type > LWTUNNEL_ENCAP_MAX)
  169. return 0;
  170. rcu_read_lock();
  171. ops = rcu_dereference(lwtun_encaps[a->type]);
  172. if (likely(ops && ops->cmp_encap))
  173. ret = ops->cmp_encap(a, b);
  174. rcu_read_unlock();
  175. return ret;
  176. }
  177. EXPORT_SYMBOL(lwtunnel_cmp_encap);
  178. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  179. {
  180. struct dst_entry *dst = skb_dst(skb);
  181. const struct lwtunnel_encap_ops *ops;
  182. struct lwtunnel_state *lwtstate;
  183. int ret = -EINVAL;
  184. if (!dst)
  185. goto drop;
  186. lwtstate = dst->lwtstate;
  187. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  188. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  189. return 0;
  190. ret = -EOPNOTSUPP;
  191. rcu_read_lock();
  192. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  193. if (likely(ops && ops->output))
  194. ret = ops->output(net, sk, skb);
  195. rcu_read_unlock();
  196. if (ret == -EOPNOTSUPP)
  197. goto drop;
  198. return ret;
  199. drop:
  200. kfree_skb(skb);
  201. return ret;
  202. }
  203. EXPORT_SYMBOL(lwtunnel_output);
  204. int lwtunnel_input(struct sk_buff *skb)
  205. {
  206. struct dst_entry *dst = skb_dst(skb);
  207. const struct lwtunnel_encap_ops *ops;
  208. struct lwtunnel_state *lwtstate;
  209. int ret = -EINVAL;
  210. if (!dst)
  211. goto drop;
  212. lwtstate = dst->lwtstate;
  213. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  214. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  215. return 0;
  216. ret = -EOPNOTSUPP;
  217. rcu_read_lock();
  218. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  219. if (likely(ops && ops->input))
  220. ret = ops->input(skb);
  221. rcu_read_unlock();
  222. if (ret == -EOPNOTSUPP)
  223. goto drop;
  224. return ret;
  225. drop:
  226. kfree_skb(skb);
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(lwtunnel_input);