lwtunnel.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
  28. {
  29. struct lwtunnel_state *lws;
  30. lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
  31. return lws;
  32. }
  33. EXPORT_SYMBOL(lwtunnel_state_alloc);
  34. static const struct lwtunnel_encap_ops __rcu *
  35. lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
  36. int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
  37. unsigned int num)
  38. {
  39. if (num > LWTUNNEL_ENCAP_MAX)
  40. return -ERANGE;
  41. return !cmpxchg((const struct lwtunnel_encap_ops **)
  42. &lwtun_encaps[num],
  43. NULL, ops) ? 0 : -1;
  44. }
  45. EXPORT_SYMBOL(lwtunnel_encap_add_ops);
  46. int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
  47. unsigned int encap_type)
  48. {
  49. int ret;
  50. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  51. encap_type > LWTUNNEL_ENCAP_MAX)
  52. return -ERANGE;
  53. ret = (cmpxchg((const struct lwtunnel_encap_ops **)
  54. &lwtun_encaps[encap_type],
  55. ops, NULL) == ops) ? 0 : -1;
  56. synchronize_net();
  57. return ret;
  58. }
  59. EXPORT_SYMBOL(lwtunnel_encap_del_ops);
  60. int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
  61. struct nlattr *encap, unsigned int family,
  62. const void *cfg, struct lwtunnel_state **lws)
  63. {
  64. const struct lwtunnel_encap_ops *ops;
  65. int ret = -EINVAL;
  66. if (encap_type == LWTUNNEL_ENCAP_NONE ||
  67. encap_type > LWTUNNEL_ENCAP_MAX)
  68. return ret;
  69. ret = -EOPNOTSUPP;
  70. rcu_read_lock();
  71. ops = rcu_dereference(lwtun_encaps[encap_type]);
  72. if (likely(ops && ops->build_state))
  73. ret = ops->build_state(dev, encap, family, cfg, lws);
  74. rcu_read_unlock();
  75. return ret;
  76. }
  77. EXPORT_SYMBOL(lwtunnel_build_state);
  78. int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
  79. {
  80. const struct lwtunnel_encap_ops *ops;
  81. struct nlattr *nest;
  82. int ret = -EINVAL;
  83. if (!lwtstate)
  84. return 0;
  85. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  86. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  87. return 0;
  88. ret = -EOPNOTSUPP;
  89. nest = nla_nest_start(skb, RTA_ENCAP);
  90. rcu_read_lock();
  91. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  92. if (likely(ops && ops->fill_encap))
  93. ret = ops->fill_encap(skb, lwtstate);
  94. rcu_read_unlock();
  95. if (ret)
  96. goto nla_put_failure;
  97. nla_nest_end(skb, nest);
  98. ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
  99. if (ret)
  100. goto nla_put_failure;
  101. return 0;
  102. nla_put_failure:
  103. nla_nest_cancel(skb, nest);
  104. return (ret == -EOPNOTSUPP ? 0 : ret);
  105. }
  106. EXPORT_SYMBOL(lwtunnel_fill_encap);
  107. int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
  108. {
  109. const struct lwtunnel_encap_ops *ops;
  110. int ret = 0;
  111. if (!lwtstate)
  112. return 0;
  113. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  114. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  115. return 0;
  116. rcu_read_lock();
  117. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  118. if (likely(ops && ops->get_encap_size))
  119. ret = nla_total_size(ops->get_encap_size(lwtstate));
  120. rcu_read_unlock();
  121. return ret;
  122. }
  123. EXPORT_SYMBOL(lwtunnel_get_encap_size);
  124. int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  125. {
  126. const struct lwtunnel_encap_ops *ops;
  127. int ret = 0;
  128. if (!a && !b)
  129. return 0;
  130. if (!a || !b)
  131. return 1;
  132. if (a->type != b->type)
  133. return 1;
  134. if (a->type == LWTUNNEL_ENCAP_NONE ||
  135. a->type > LWTUNNEL_ENCAP_MAX)
  136. return 0;
  137. rcu_read_lock();
  138. ops = rcu_dereference(lwtun_encaps[a->type]);
  139. if (likely(ops && ops->cmp_encap))
  140. ret = ops->cmp_encap(a, b);
  141. rcu_read_unlock();
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(lwtunnel_cmp_encap);
  145. int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  146. {
  147. struct dst_entry *dst = skb_dst(skb);
  148. const struct lwtunnel_encap_ops *ops;
  149. struct lwtunnel_state *lwtstate;
  150. int ret = -EINVAL;
  151. if (!dst)
  152. goto drop;
  153. lwtstate = dst->lwtstate;
  154. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  155. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  156. return 0;
  157. ret = -EOPNOTSUPP;
  158. rcu_read_lock();
  159. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  160. if (likely(ops && ops->output))
  161. ret = ops->output(net, sk, skb);
  162. rcu_read_unlock();
  163. if (ret == -EOPNOTSUPP)
  164. goto drop;
  165. return ret;
  166. drop:
  167. kfree_skb(skb);
  168. return ret;
  169. }
  170. EXPORT_SYMBOL(lwtunnel_output);
  171. int lwtunnel_input(struct sk_buff *skb)
  172. {
  173. struct dst_entry *dst = skb_dst(skb);
  174. const struct lwtunnel_encap_ops *ops;
  175. struct lwtunnel_state *lwtstate;
  176. int ret = -EINVAL;
  177. if (!dst)
  178. goto drop;
  179. lwtstate = dst->lwtstate;
  180. if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
  181. lwtstate->type > LWTUNNEL_ENCAP_MAX)
  182. return 0;
  183. ret = -EOPNOTSUPP;
  184. rcu_read_lock();
  185. ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
  186. if (likely(ops && ops->input))
  187. ret = ops->input(skb);
  188. rcu_read_unlock();
  189. if (ret == -EOPNOTSUPP)
  190. goto drop;
  191. return ret;
  192. drop:
  193. kfree_skb(skb);
  194. return ret;
  195. }
  196. EXPORT_SYMBOL(lwtunnel_input);