ila_lwt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/errno.h>
  3. #include <linux/ip.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/socket.h>
  8. #include <linux/types.h>
  9. #include <net/checksum.h>
  10. #include <net/dst_cache.h>
  11. #include <net/ip.h>
  12. #include <net/ip6_fib.h>
  13. #include <net/ip6_route.h>
  14. #include <net/lwtunnel.h>
  15. #include <net/protocol.h>
  16. #include <uapi/linux/ila.h>
  17. #include "ila.h"
  18. struct ila_lwt {
  19. struct ila_params p;
  20. struct dst_cache dst_cache;
  21. u32 connected : 1;
  22. };
  23. static inline struct ila_lwt *ila_lwt_lwtunnel(
  24. struct lwtunnel_state *lwt)
  25. {
  26. return (struct ila_lwt *)lwt->data;
  27. }
  28. static inline struct ila_params *ila_params_lwtunnel(
  29. struct lwtunnel_state *lwt)
  30. {
  31. return &ila_lwt_lwtunnel(lwt)->p;
  32. }
  33. static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  34. {
  35. struct dst_entry *orig_dst = skb_dst(skb);
  36. struct rt6_info *rt = (struct rt6_info *)orig_dst;
  37. struct ila_lwt *ilwt = ila_lwt_lwtunnel(orig_dst->lwtstate);
  38. struct dst_entry *dst;
  39. int err = -EINVAL;
  40. if (skb->protocol != htons(ETH_P_IPV6))
  41. goto drop;
  42. ila_update_ipv6_locator(skb, ila_params_lwtunnel(orig_dst->lwtstate),
  43. true);
  44. if (rt->rt6i_flags & (RTF_GATEWAY | RTF_CACHE)) {
  45. /* Already have a next hop address in route, no need for
  46. * dest cache route.
  47. */
  48. return orig_dst->lwtstate->orig_output(net, sk, skb);
  49. }
  50. dst = dst_cache_get(&ilwt->dst_cache);
  51. if (unlikely(!dst)) {
  52. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  53. struct flowi6 fl6;
  54. /* Lookup a route for the new destination. Take into
  55. * account that the base route may already have a gateway.
  56. */
  57. memset(&fl6, 0, sizeof(fl6));
  58. fl6.flowi6_oif = orig_dst->dev->ifindex;
  59. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  60. fl6.daddr = *rt6_nexthop((struct rt6_info *)orig_dst,
  61. &ip6h->daddr);
  62. dst = ip6_route_output(net, NULL, &fl6);
  63. if (dst->error) {
  64. err = -EHOSTUNREACH;
  65. dst_release(dst);
  66. goto drop;
  67. }
  68. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
  69. if (IS_ERR(dst)) {
  70. err = PTR_ERR(dst);
  71. goto drop;
  72. }
  73. if (ilwt->connected)
  74. dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
  75. }
  76. skb_dst_set(skb, dst);
  77. return dst_output(net, sk, skb);
  78. drop:
  79. kfree_skb(skb);
  80. return err;
  81. }
  82. static int ila_input(struct sk_buff *skb)
  83. {
  84. struct dst_entry *dst = skb_dst(skb);
  85. if (skb->protocol != htons(ETH_P_IPV6))
  86. goto drop;
  87. ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate), false);
  88. return dst->lwtstate->orig_input(skb);
  89. drop:
  90. kfree_skb(skb);
  91. return -EINVAL;
  92. }
  93. static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
  94. [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
  95. [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
  96. };
  97. static int ila_build_state(struct nlattr *nla,
  98. unsigned int family, const void *cfg,
  99. struct lwtunnel_state **ts,
  100. struct netlink_ext_ack *extack)
  101. {
  102. struct ila_lwt *ilwt;
  103. struct ila_params *p;
  104. struct nlattr *tb[ILA_ATTR_MAX + 1];
  105. struct lwtunnel_state *newts;
  106. const struct fib6_config *cfg6 = cfg;
  107. struct ila_addr *iaddr;
  108. int ret;
  109. if (family != AF_INET6)
  110. return -EINVAL;
  111. if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) {
  112. /* Need to have full locator and at least type field
  113. * included in destination
  114. */
  115. return -EINVAL;
  116. }
  117. iaddr = (struct ila_addr *)&cfg6->fc_dst;
  118. if (!ila_addr_is_ila(iaddr) || ila_csum_neutral_set(iaddr->ident)) {
  119. /* Don't allow translation for a non-ILA address or checksum
  120. * neutral flag to be set.
  121. */
  122. return -EINVAL;
  123. }
  124. ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla, ila_nl_policy, extack);
  125. if (ret < 0)
  126. return ret;
  127. if (!tb[ILA_ATTR_LOCATOR])
  128. return -EINVAL;
  129. newts = lwtunnel_state_alloc(sizeof(*ilwt));
  130. if (!newts)
  131. return -ENOMEM;
  132. ilwt = ila_lwt_lwtunnel(newts);
  133. ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC);
  134. if (ret) {
  135. kfree(newts);
  136. return ret;
  137. }
  138. p = ila_params_lwtunnel(newts);
  139. p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  140. /* Precompute checksum difference for translation since we
  141. * know both the old locator and the new one.
  142. */
  143. p->locator_match = iaddr->loc;
  144. p->csum_diff = compute_csum_diff8(
  145. (__be32 *)&p->locator_match, (__be32 *)&p->locator);
  146. if (tb[ILA_ATTR_CSUM_MODE])
  147. p->csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
  148. ila_init_saved_csum(p);
  149. newts->type = LWTUNNEL_ENCAP_ILA;
  150. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  151. LWTUNNEL_STATE_INPUT_REDIRECT;
  152. if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr))
  153. ilwt->connected = 1;
  154. *ts = newts;
  155. return 0;
  156. }
  157. static void ila_destroy_state(struct lwtunnel_state *lwt)
  158. {
  159. dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache);
  160. }
  161. static int ila_fill_encap_info(struct sk_buff *skb,
  162. struct lwtunnel_state *lwtstate)
  163. {
  164. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  165. if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
  166. ILA_ATTR_PAD))
  167. goto nla_put_failure;
  168. if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
  169. goto nla_put_failure;
  170. return 0;
  171. nla_put_failure:
  172. return -EMSGSIZE;
  173. }
  174. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  175. {
  176. return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
  177. nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */
  178. 0;
  179. }
  180. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  181. {
  182. struct ila_params *a_p = ila_params_lwtunnel(a);
  183. struct ila_params *b_p = ila_params_lwtunnel(b);
  184. return (a_p->locator.v64 != b_p->locator.v64);
  185. }
  186. static const struct lwtunnel_encap_ops ila_encap_ops = {
  187. .build_state = ila_build_state,
  188. .destroy_state = ila_destroy_state,
  189. .output = ila_output,
  190. .input = ila_input,
  191. .fill_encap = ila_fill_encap_info,
  192. .get_encap_size = ila_encap_nlsize,
  193. .cmp_encap = ila_encap_cmp,
  194. .owner = THIS_MODULE,
  195. };
  196. int ila_lwt_init(void)
  197. {
  198. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  199. }
  200. void ila_lwt_fini(void)
  201. {
  202. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  203. }