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