lwtunnel.c 8.1 KB

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