ila.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. struct ila_params {
  15. __be64 locator;
  16. __be64 locator_match;
  17. __wsum csum_diff;
  18. };
  19. static inline struct ila_params *ila_params_lwtunnel(
  20. struct lwtunnel_state *lwstate)
  21. {
  22. return (struct ila_params *)lwstate->data;
  23. }
  24. static inline __wsum compute_csum_diff8(const __be32 *from, const __be32 *to)
  25. {
  26. __be32 diff[] = {
  27. ~from[0], ~from[1], to[0], to[1],
  28. };
  29. return csum_partial(diff, sizeof(diff), 0);
  30. }
  31. static inline __wsum get_csum_diff(struct ipv6hdr *ip6h, struct ila_params *p)
  32. {
  33. if (*(__be64 *)&ip6h->daddr == p->locator_match)
  34. return p->csum_diff;
  35. else
  36. return compute_csum_diff8((__be32 *)&ip6h->daddr,
  37. (__be32 *)&p->locator);
  38. }
  39. static void update_ipv6_locator(struct sk_buff *skb, struct ila_params *p)
  40. {
  41. __wsum diff;
  42. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  43. size_t nhoff = sizeof(struct ipv6hdr);
  44. /* First update checksum */
  45. switch (ip6h->nexthdr) {
  46. case NEXTHDR_TCP:
  47. if (likely(pskb_may_pull(skb, nhoff + sizeof(struct tcphdr)))) {
  48. struct tcphdr *th = (struct tcphdr *)
  49. (skb_network_header(skb) + nhoff);
  50. diff = get_csum_diff(ip6h, p);
  51. inet_proto_csum_replace_by_diff(&th->check, skb,
  52. diff, true);
  53. }
  54. break;
  55. case NEXTHDR_UDP:
  56. if (likely(pskb_may_pull(skb, nhoff + sizeof(struct udphdr)))) {
  57. struct udphdr *uh = (struct udphdr *)
  58. (skb_network_header(skb) + nhoff);
  59. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  60. diff = get_csum_diff(ip6h, p);
  61. inet_proto_csum_replace_by_diff(&uh->check, skb,
  62. diff, true);
  63. if (!uh->check)
  64. uh->check = CSUM_MANGLED_0;
  65. }
  66. }
  67. break;
  68. case NEXTHDR_ICMP:
  69. if (likely(pskb_may_pull(skb,
  70. nhoff + sizeof(struct icmp6hdr)))) {
  71. struct icmp6hdr *ih = (struct icmp6hdr *)
  72. (skb_network_header(skb) + nhoff);
  73. diff = get_csum_diff(ip6h, p);
  74. inet_proto_csum_replace_by_diff(&ih->icmp6_cksum, skb,
  75. diff, true);
  76. }
  77. break;
  78. }
  79. /* Now change destination address */
  80. *(__be64 *)&ip6h->daddr = p->locator;
  81. }
  82. static int ila_output(struct net *net, struct sock *sk, 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. update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
  88. return dst->lwtstate->orig_output(net, sk, skb);
  89. drop:
  90. kfree_skb(skb);
  91. return -EINVAL;
  92. }
  93. static int ila_input(struct sk_buff *skb)
  94. {
  95. struct dst_entry *dst = skb_dst(skb);
  96. if (skb->protocol != htons(ETH_P_IPV6))
  97. goto drop;
  98. update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
  99. return dst->lwtstate->orig_input(skb);
  100. drop:
  101. kfree_skb(skb);
  102. return -EINVAL;
  103. }
  104. static struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
  105. [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
  106. };
  107. static int ila_build_state(struct net_device *dev, struct nlattr *nla,
  108. unsigned int family, const void *cfg,
  109. struct lwtunnel_state **ts)
  110. {
  111. struct ila_params *p;
  112. struct nlattr *tb[ILA_ATTR_MAX + 1];
  113. size_t encap_len = sizeof(*p);
  114. struct lwtunnel_state *newts;
  115. const struct fib6_config *cfg6 = cfg;
  116. int ret;
  117. if (family != AF_INET6)
  118. return -EINVAL;
  119. ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla,
  120. ila_nl_policy);
  121. if (ret < 0)
  122. return ret;
  123. if (!tb[ILA_ATTR_LOCATOR])
  124. return -EINVAL;
  125. newts = lwtunnel_state_alloc(encap_len);
  126. if (!newts)
  127. return -ENOMEM;
  128. newts->len = encap_len;
  129. p = ila_params_lwtunnel(newts);
  130. p->locator = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
  131. if (cfg6->fc_dst_len > sizeof(__be64)) {
  132. /* Precompute checksum difference for translation since we
  133. * know both the old locator and the new one.
  134. */
  135. p->locator_match = *(__be64 *)&cfg6->fc_dst;
  136. p->csum_diff = compute_csum_diff8(
  137. (__be32 *)&p->locator_match, (__be32 *)&p->locator);
  138. }
  139. newts->type = LWTUNNEL_ENCAP_ILA;
  140. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  141. LWTUNNEL_STATE_INPUT_REDIRECT;
  142. *ts = newts;
  143. return 0;
  144. }
  145. static int ila_fill_encap_info(struct sk_buff *skb,
  146. struct lwtunnel_state *lwtstate)
  147. {
  148. struct ila_params *p = ila_params_lwtunnel(lwtstate);
  149. if (nla_put_u64(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator))
  150. goto nla_put_failure;
  151. return 0;
  152. nla_put_failure:
  153. return -EMSGSIZE;
  154. }
  155. static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
  156. {
  157. /* No encapsulation overhead */
  158. return 0;
  159. }
  160. static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  161. {
  162. struct ila_params *a_p = ila_params_lwtunnel(a);
  163. struct ila_params *b_p = ila_params_lwtunnel(b);
  164. return (a_p->locator != b_p->locator);
  165. }
  166. static const struct lwtunnel_encap_ops ila_encap_ops = {
  167. .build_state = ila_build_state,
  168. .output = ila_output,
  169. .input = ila_input,
  170. .fill_encap = ila_fill_encap_info,
  171. .get_encap_size = ila_encap_nlsize,
  172. .cmp_encap = ila_encap_cmp,
  173. };
  174. static int __init ila_init(void)
  175. {
  176. return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  177. }
  178. static void __exit ila_fini(void)
  179. {
  180. lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
  181. }
  182. module_init(ila_init);
  183. module_exit(ila_fini);
  184. MODULE_AUTHOR("Tom Herbert <tom@herbertland.com>");
  185. MODULE_LICENSE("GPL");