ila_lwt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <linux/errno.h>
  2. #include <linux/ip.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/socket.h>
  7. #include <linux/types.h>
  8. #include <net/checksum.h>
  9. #include <net/ip.h>
  10. #include <net/ip6_fib.h>
  11. #include <net/lwtunnel.h>
  12. #include <net/protocol.h>
  13. #include <uapi/linux/ila.h>
  14. #include "ila.h"
  15. static inline struct ila_params *ila_params_lwtunnel(
  16. struct lwtunnel_state *lwstate)
  17. {
  18. return (struct ila_params *)lwstate->data;
  19. }
  20. static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  21. {
  22. struct dst_entry *dst = skb_dst(skb);
  23. if (skb->protocol != htons(ETH_P_IPV6))
  24. goto drop;
  25. ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate), true);
  26. return dst->lwtstate->orig_output(net, sk, skb);
  27. drop:
  28. kfree_skb(skb);
  29. return -EINVAL;
  30. }
  31. static int ila_input(struct sk_buff *skb)
  32. {
  33. struct dst_entry *dst = skb_dst(skb);
  34. if (skb->protocol != htons(ETH_P_IPV6))
  35. goto drop;
  36. ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate), false);
  37. return dst->lwtstate->orig_input(skb);
  38. drop:
  39. kfree_skb(skb);
  40. return -EINVAL;
  41. }
  42. static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
  43. [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
  44. [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
  45. };
  46. static int ila_build_state(struct net_device *dev, struct nlattr *nla,
  47. unsigned int family, const void *cfg,
  48. struct lwtunnel_state **ts)
  49. {
  50. struct ila_params *p;
  51. struct nlattr *tb[ILA_ATTR_MAX + 1];
  52. size_t encap_len = sizeof(*p);
  53. struct lwtunnel_state *newts;
  54. const struct fib6_config *cfg6 = cfg;
  55. struct ila_addr *iaddr;
  56. int ret;
  57. if (family != AF_INET6)
  58. return -EINVAL;
  59. if (cfg6->fc_dst_len < sizeof(struct ila_locator) + 1) {
  60. /* Need to have full locator and at least type field
  61. * included in destination
  62. */
  63. return -EINVAL;
  64. }
  65. iaddr = (struct ila_addr *)&cfg6->fc_dst;
  66. if (!ila_addr_is_ila(iaddr) || ila_csum_neutral_set(iaddr->ident)) {
  67. /* Don't allow translation for a non-ILA address or checksum
  68. * neutral flag to be set.
  69. */
  70. return -EINVAL;
  71. }
  72. ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla,
  73. ila_nl_policy);
  74. if (ret < 0)
  75. return ret;
  76. if (!tb[ILA_ATTR_LOCATOR])
  77. return -EINVAL;
  78. newts = lwtunnel_state_alloc(encap_len);
  79. if (!newts)
  80. return -ENOMEM;
  81. newts->len = encap_len;
  82. p = ila_params_lwtunnel(newts);
  83. p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  84. /* Precompute checksum difference for translation since we
  85. * know both the old locator and the new one.
  86. */
  87. p->locator_match = iaddr->loc;
  88. p->csum_diff = compute_csum_diff8(
  89. (__be32 *)&p->locator_match, (__be32 *)&p->locator);
  90. if (tb[ILA_ATTR_CSUM_MODE])
  91. p->csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]);
  92. ila_init_saved_csum(p);
  93. newts->type = LWTUNNEL_ENCAP_ILA;
  94. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  95. LWTUNNEL_STATE_INPUT_REDIRECT;
  96. *ts = newts;
  97. return 0;
  98. }
  99. static int ila_fill_encap_info(struct sk_buff *skb,
  100. struct lwtunnel_state *lwtstate)
  101. {
  102. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  103. if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
  104. ILA_ATTR_PAD))
  105. goto nla_put_failure;
  106. if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode))
  107. goto nla_put_failure;
  108. return 0;
  109. nla_put_failure:
  110. return -EMSGSIZE;
  111. }
  112. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  113. {
  114. return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */
  115. nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */
  116. 0;
  117. }
  118. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  119. {
  120. struct ila_params *a_p = ila_params_lwtunnel(a);
  121. struct ila_params *b_p = ila_params_lwtunnel(b);
  122. return (a_p->locator.v64 != b_p->locator.v64);
  123. }
  124. static const struct lwtunnel_encap_ops ila_encap_ops = {
  125. .build_state = ila_build_state,
  126. .output = ila_output,
  127. .input = ila_input,
  128. .fill_encap = ila_fill_encap_info,
  129. .get_encap_size = ila_encap_nlsize,
  130. .cmp_encap = ila_encap_cmp,
  131. .owner = THIS_MODULE,
  132. };
  133. int ila_lwt_init(void)
  134. {
  135. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  136. }
  137. void ila_lwt_fini(void)
  138. {
  139. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  140. }