blob: 0e94042d12891ad4388d5924315d4a8403525b3c [file] [log] [blame]
Tom Herbert33f11d12015-12-15 15:41:35 -08001#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
16static __wsum get_csum_diff(struct ipv6hdr *ip6h, struct ila_params *p)
17{
Tom Herbert351596a2016-04-23 11:46:55 -070018 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
19
Tom Herbert90bfe662016-04-23 11:46:57 -070020 if (p->locator_match.v64)
Tom Herbert33f11d12015-12-15 15:41:35 -080021 return p->csum_diff;
22 else
Tom Herbert351596a2016-04-23 11:46:55 -070023 return compute_csum_diff8((__be32 *)&iaddr->loc,
Tom Herbert33f11d12015-12-15 15:41:35 -080024 (__be32 *)&p->locator);
25}
26
Tom Herbert90bfe662016-04-23 11:46:57 -070027static void ila_csum_do_neutral(struct ila_addr *iaddr,
28 struct ila_params *p)
29{
30 __sum16 *adjust = (__force __sum16 *)&iaddr->ident.v16[3];
31 __wsum diff, fval;
32
33 /* Check if checksum adjust value has been cached */
34 if (p->locator_match.v64) {
35 diff = p->csum_diff;
36 } else {
37 diff = compute_csum_diff8((__be32 *)iaddr,
38 (__be32 *)&p->locator);
39 }
40
41 fval = (__force __wsum)(ila_csum_neutral_set(iaddr->ident) ?
42 ~CSUM_NEUTRAL_FLAG : CSUM_NEUTRAL_FLAG);
43
44 diff = csum_add(diff, fval);
45
46 *adjust = ~csum_fold(csum_add(diff, csum_unfold(*adjust)));
47
48 /* Flip the csum-neutral bit. Either we are doing a SIR->ILA
49 * translation with ILA_CSUM_NEUTRAL_MAP as the csum_method
50 * and the C-bit is not set, or we are doing an ILA-SIR
51 * tranlsation and the C-bit is set.
52 */
53 iaddr->ident.csum_neutral ^= 1;
54}
55
56static void ila_csum_adjust_transport(struct sk_buff *skb,
57 struct ila_params *p)
Tom Herbert33f11d12015-12-15 15:41:35 -080058{
59 __wsum diff;
60 struct ipv6hdr *ip6h = ipv6_hdr(skb);
Tom Herbert351596a2016-04-23 11:46:55 -070061 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
Tom Herbert33f11d12015-12-15 15:41:35 -080062 size_t nhoff = sizeof(struct ipv6hdr);
63
Tom Herbert33f11d12015-12-15 15:41:35 -080064 switch (ip6h->nexthdr) {
65 case NEXTHDR_TCP:
66 if (likely(pskb_may_pull(skb, nhoff + sizeof(struct tcphdr)))) {
67 struct tcphdr *th = (struct tcphdr *)
68 (skb_network_header(skb) + nhoff);
69
70 diff = get_csum_diff(ip6h, p);
71 inet_proto_csum_replace_by_diff(&th->check, skb,
72 diff, true);
73 }
74 break;
75 case NEXTHDR_UDP:
76 if (likely(pskb_may_pull(skb, nhoff + sizeof(struct udphdr)))) {
77 struct udphdr *uh = (struct udphdr *)
78 (skb_network_header(skb) + nhoff);
79
80 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
81 diff = get_csum_diff(ip6h, p);
82 inet_proto_csum_replace_by_diff(&uh->check, skb,
83 diff, true);
84 if (!uh->check)
85 uh->check = CSUM_MANGLED_0;
86 }
87 }
88 break;
89 case NEXTHDR_ICMP:
90 if (likely(pskb_may_pull(skb,
91 nhoff + sizeof(struct icmp6hdr)))) {
92 struct icmp6hdr *ih = (struct icmp6hdr *)
93 (skb_network_header(skb) + nhoff);
94
95 diff = get_csum_diff(ip6h, p);
96 inet_proto_csum_replace_by_diff(&ih->icmp6_cksum, skb,
97 diff, true);
98 }
99 break;
100 }
101
102 /* Now change destination address */
Tom Herbert351596a2016-04-23 11:46:55 -0700103 iaddr->loc = p->locator;
Tom Herbert33f11d12015-12-15 15:41:35 -0800104}
105
Tom Herbert90bfe662016-04-23 11:46:57 -0700106void ila_update_ipv6_locator(struct sk_buff *skb, struct ila_params *p)
107{
108 struct ipv6hdr *ip6h = ipv6_hdr(skb);
109 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
110
111 /* First deal with the transport checksum */
112 if (ila_csum_neutral_set(iaddr->ident)) {
113 /* C-bit is set in the locator indicating that this
114 * is a locator being translated to a SIR address.
115 * Perform (receiver) checksum-neutral translation.
116 */
117 ila_csum_do_neutral(iaddr, p);
118 } else {
119 switch (p->csum_mode) {
120 case ILA_CSUM_ADJUST_TRANSPORT:
121 ila_csum_adjust_transport(skb, p);
122 break;
123 case ILA_CSUM_NEUTRAL_MAP:
124 ila_csum_do_neutral(iaddr, p);
125 break;
126 case ILA_CSUM_NO_ACTION:
127 break;
128 }
129 }
130
131 /* Now change destination address */
132 iaddr->loc = p->locator;
133}
134
135void ila_init_saved_csum(struct ila_params *p)
136{
137 if (!p->locator_match.v64)
138 return;
139
140 p->csum_diff = compute_csum_diff8(
141 (__be32 *)&p->locator_match,
142 (__be32 *)&p->locator);
143}
144
Tom Herbert33f11d12015-12-15 15:41:35 -0800145static int __init ila_init(void)
146{
147 int ret;
148
149 ret = ila_lwt_init();
150
151 if (ret)
152 goto fail_lwt;
153
Tom Herbert7f00fea2015-12-15 15:41:38 -0800154 ret = ila_xlat_init();
155 if (ret)
156 goto fail_xlat;
157
158 return 0;
159fail_xlat:
160 ila_lwt_fini();
Tom Herbert33f11d12015-12-15 15:41:35 -0800161fail_lwt:
162 return ret;
163}
164
165static void __exit ila_fini(void)
166{
Tom Herbert7f00fea2015-12-15 15:41:38 -0800167 ila_xlat_fini();
Tom Herbert33f11d12015-12-15 15:41:35 -0800168 ila_lwt_fini();
169}
170
171module_init(ila_init);
172module_exit(ila_fini);
Robert Shearman84a8cbe2016-02-19 09:43:18 +0000173MODULE_ALIAS_RTNL_LWT(ILA);
Tom Herbert33f11d12015-12-15 15:41:35 -0800174MODULE_AUTHOR("Tom Herbert <tom@herbertland.com>");
175MODULE_LICENSE("GPL");