1 #include <linux/errno.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>
10 #include <net/ip6_fib.h>
11 #include <net/lwtunnel.h>
12 #include <net/protocol.h>
13 #include <uapi/linux/ila.h>
21 static inline struct ila_params
*ila_params_lwtunnel(
22 struct lwtunnel_state
*lwstate
)
24 return (struct ila_params
*)lwstate
->data
;
27 static inline __wsum
compute_csum_diff8(const __be32
*from
, const __be32
*to
)
30 ~from
[0], ~from
[1], to
[0], to
[1],
33 return csum_partial(diff
, sizeof(diff
), 0);
36 static inline __wsum
get_csum_diff(struct ipv6hdr
*ip6h
, struct ila_params
*p
)
38 if (*(__be64
*)&ip6h
->daddr
== p
->locator_match
)
41 return compute_csum_diff8((__be32
*)&ip6h
->daddr
,
42 (__be32
*)&p
->locator
);
45 static void update_ipv6_locator(struct sk_buff
*skb
, struct ila_params
*p
)
48 struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
49 size_t nhoff
= sizeof(struct ipv6hdr
);
51 /* First update checksum */
52 switch (ip6h
->nexthdr
) {
54 if (likely(pskb_may_pull(skb
, nhoff
+ sizeof(struct tcphdr
)))) {
55 struct tcphdr
*th
= (struct tcphdr
*)
56 (skb_network_header(skb
) + nhoff
);
58 diff
= get_csum_diff(ip6h
, p
);
59 inet_proto_csum_replace_by_diff(&th
->check
, skb
,
64 if (likely(pskb_may_pull(skb
, nhoff
+ sizeof(struct udphdr
)))) {
65 struct udphdr
*uh
= (struct udphdr
*)
66 (skb_network_header(skb
) + nhoff
);
68 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
69 diff
= get_csum_diff(ip6h
, p
);
70 inet_proto_csum_replace_by_diff(&uh
->check
, skb
,
73 uh
->check
= CSUM_MANGLED_0
;
78 if (likely(pskb_may_pull(skb
,
79 nhoff
+ sizeof(struct icmp6hdr
)))) {
80 struct icmp6hdr
*ih
= (struct icmp6hdr
*)
81 (skb_network_header(skb
) + nhoff
);
83 diff
= get_csum_diff(ip6h
, p
);
84 inet_proto_csum_replace_by_diff(&ih
->icmp6_cksum
, skb
,
90 /* Now change destination address */
91 *(__be64
*)&ip6h
->daddr
= p
->locator
;
94 static int ila_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
96 struct dst_entry
*dst
= skb_dst(skb
);
98 if (skb
->protocol
!= htons(ETH_P_IPV6
))
101 update_ipv6_locator(skb
, ila_params_lwtunnel(dst
->lwtstate
));
103 return dst
->lwtstate
->orig_output(net
, sk
, skb
);
110 static int ila_input(struct sk_buff
*skb
)
112 struct dst_entry
*dst
= skb_dst(skb
);
114 if (skb
->protocol
!= htons(ETH_P_IPV6
))
117 update_ipv6_locator(skb
, ila_params_lwtunnel(dst
->lwtstate
));
119 return dst
->lwtstate
->orig_input(skb
);
126 static struct nla_policy ila_nl_policy
[ILA_ATTR_MAX
+ 1] = {
127 [ILA_ATTR_LOCATOR
] = { .type
= NLA_U64
, },
130 static int ila_build_state(struct net_device
*dev
, struct nlattr
*nla
,
131 unsigned int family
, const void *cfg
,
132 struct lwtunnel_state
**ts
)
134 struct ila_params
*p
;
135 struct nlattr
*tb
[ILA_ATTR_MAX
+ 1];
136 size_t encap_len
= sizeof(*p
);
137 struct lwtunnel_state
*newts
;
138 const struct fib6_config
*cfg6
= cfg
;
141 if (family
!= AF_INET6
)
144 ret
= nla_parse_nested(tb
, ILA_ATTR_MAX
, nla
,
149 if (!tb
[ILA_ATTR_LOCATOR
])
152 newts
= lwtunnel_state_alloc(encap_len
);
156 newts
->len
= encap_len
;
157 p
= ila_params_lwtunnel(newts
);
159 p
->locator
= (__force __be64
)nla_get_u64(tb
[ILA_ATTR_LOCATOR
]);
161 if (cfg6
->fc_dst_len
> sizeof(__be64
)) {
162 /* Precompute checksum difference for translation since we
163 * know both the old locator and the new one.
165 p
->locator_match
= *(__be64
*)&cfg6
->fc_dst
;
166 p
->csum_diff
= compute_csum_diff8(
167 (__be32
*)&p
->locator_match
, (__be32
*)&p
->locator
);
170 newts
->type
= LWTUNNEL_ENCAP_ILA
;
171 newts
->flags
|= LWTUNNEL_STATE_OUTPUT_REDIRECT
|
172 LWTUNNEL_STATE_INPUT_REDIRECT
;
179 static int ila_fill_encap_info(struct sk_buff
*skb
,
180 struct lwtunnel_state
*lwtstate
)
182 struct ila_params
*p
= ila_params_lwtunnel(lwtstate
);
184 if (nla_put_u64(skb
, ILA_ATTR_LOCATOR
, (__force u64
)p
->locator
))
185 goto nla_put_failure
;
193 static int ila_encap_nlsize(struct lwtunnel_state
*lwtstate
)
195 /* No encapsulation overhead */
199 static int ila_encap_cmp(struct lwtunnel_state
*a
, struct lwtunnel_state
*b
)
201 struct ila_params
*a_p
= ila_params_lwtunnel(a
);
202 struct ila_params
*b_p
= ila_params_lwtunnel(b
);
204 return (a_p
->locator
!= b_p
->locator
);
207 static const struct lwtunnel_encap_ops ila_encap_ops
= {
208 .build_state
= ila_build_state
,
209 .output
= ila_output
,
211 .fill_encap
= ila_fill_encap_info
,
212 .get_encap_size
= ila_encap_nlsize
,
213 .cmp_encap
= ila_encap_cmp
,
216 static int __init
ila_init(void)
218 return lwtunnel_encap_add_ops(&ila_encap_ops
, LWTUNNEL_ENCAP_ILA
);
221 static void __exit
ila_fini(void)
223 lwtunnel_encap_del_ops(&ila_encap_ops
, LWTUNNEL_ENCAP_ILA
);
226 module_init(ila_init
);
227 module_exit(ila_fini
);
228 MODULE_AUTHOR("Tom Herbert <tom@herbertland.com>");
229 MODULE_LICENSE("GPL");