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> |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 9 | #include <net/dst_cache.h> |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 10 | #include <net/ip.h> |
| 11 | #include <net/ip6_fib.h> |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 12 | #include <net/ip6_route.h> |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 13 | #include <net/lwtunnel.h> |
| 14 | #include <net/protocol.h> |
| 15 | #include <uapi/linux/ila.h> |
Tom Herbert | 33f11d1 | 2015-12-15 15:41:35 -0800 | [diff] [blame] | 16 | #include "ila.h" |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 17 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 18 | struct ila_lwt { |
| 19 | struct ila_params p; |
| 20 | struct dst_cache dst_cache; |
| 21 | u32 connected : 1; |
| 22 | }; |
| 23 | |
| 24 | static inline struct ila_lwt *ila_lwt_lwtunnel( |
| 25 | struct lwtunnel_state *lwt) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 26 | { |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 27 | return (struct ila_lwt *)lwt->data; |
| 28 | } |
| 29 | |
| 30 | static inline struct ila_params *ila_params_lwtunnel( |
| 31 | struct lwtunnel_state *lwt) |
| 32 | { |
| 33 | return &ila_lwt_lwtunnel(lwt)->p; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 36 | static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 37 | { |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 38 | struct dst_entry *orig_dst = skb_dst(skb); |
Tom Herbert | ab3a70b | 2016-10-16 21:25:35 -0700 | [diff] [blame] | 39 | struct rt6_info *rt = (struct rt6_info *)orig_dst; |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 40 | struct ila_lwt *ilwt = ila_lwt_lwtunnel(orig_dst->lwtstate); |
| 41 | struct dst_entry *dst; |
| 42 | int err = -EINVAL; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 43 | |
| 44 | if (skb->protocol != htons(ETH_P_IPV6)) |
| 45 | goto drop; |
| 46 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 47 | ila_update_ipv6_locator(skb, ila_params_lwtunnel(orig_dst->lwtstate), |
| 48 | true); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 49 | |
Tom Herbert | ab3a70b | 2016-10-16 21:25:35 -0700 | [diff] [blame] | 50 | if (rt->rt6i_flags & (RTF_GATEWAY | RTF_CACHE)) { |
| 51 | /* Already have a next hop address in route, no need for |
| 52 | * dest cache route. |
| 53 | */ |
| 54 | return orig_dst->lwtstate->orig_output(net, sk, skb); |
| 55 | } |
| 56 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 57 | dst = dst_cache_get(&ilwt->dst_cache); |
| 58 | if (unlikely(!dst)) { |
| 59 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 60 | struct flowi6 fl6; |
| 61 | |
| 62 | /* Lookup a route for the new destination. Take into |
| 63 | * account that the base route may already have a gateway. |
| 64 | */ |
| 65 | |
| 66 | memset(&fl6, 0, sizeof(fl6)); |
| 67 | fl6.flowi6_oif = orig_dst->dev->ifindex; |
| 68 | fl6.flowi6_iif = LOOPBACK_IFINDEX; |
| 69 | fl6.daddr = *rt6_nexthop((struct rt6_info *)orig_dst, |
| 70 | &ip6h->daddr); |
| 71 | |
| 72 | dst = ip6_route_output(net, NULL, &fl6); |
| 73 | if (dst->error) { |
| 74 | err = -EHOSTUNREACH; |
| 75 | dst_release(dst); |
| 76 | goto drop; |
| 77 | } |
| 78 | |
| 79 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0); |
| 80 | if (IS_ERR(dst)) { |
| 81 | err = PTR_ERR(dst); |
| 82 | goto drop; |
| 83 | } |
| 84 | |
| 85 | if (ilwt->connected) |
| 86 | dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr); |
| 87 | } |
| 88 | |
| 89 | skb_dst_set(skb, dst); |
| 90 | return dst_output(net, sk, skb); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 91 | |
| 92 | drop: |
| 93 | kfree_skb(skb); |
stephen hemminger | 9e7b19c | 2017-05-19 09:55:49 -0700 | [diff] [blame] | 94 | return err; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int ila_input(struct sk_buff *skb) |
| 98 | { |
| 99 | struct dst_entry *dst = skb_dst(skb); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 100 | |
| 101 | if (skb->protocol != htons(ETH_P_IPV6)) |
| 102 | goto drop; |
| 103 | |
Tom Herbert | 707a2ca | 2016-06-07 16:09:44 -0700 | [diff] [blame] | 104 | ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate), false); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 105 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 106 | return dst->lwtstate->orig_input(skb); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 107 | |
| 108 | drop: |
| 109 | kfree_skb(skb); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | |
stephen hemminger | 6501f34 | 2016-08-31 15:20:51 -0700 | [diff] [blame] | 113 | static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = { |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 114 | [ILA_ATTR_LOCATOR] = { .type = NLA_U64, }, |
Tom Herbert | 90bfe66 | 2016-04-23 11:46:57 -0700 | [diff] [blame] | 115 | [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, }, |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 118 | static int ila_build_state(struct nlattr *nla, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 119 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 120 | struct lwtunnel_state **ts, |
| 121 | struct netlink_ext_ack *extack) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 122 | { |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 123 | struct ila_lwt *ilwt; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 124 | struct ila_params *p; |
| 125 | struct nlattr *tb[ILA_ATTR_MAX + 1]; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 126 | struct lwtunnel_state *newts; |
Tom Herbert | 92b78af | 2015-08-24 09:45:42 -0700 | [diff] [blame] | 127 | const struct fib6_config *cfg6 = cfg; |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 128 | struct ila_addr *iaddr; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 129 | int ret; |
| 130 | |
Tom Herbert | 92b78af | 2015-08-24 09:45:42 -0700 | [diff] [blame] | 131 | if (family != AF_INET6) |
| 132 | return -EINVAL; |
| 133 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 134 | if (cfg6->fc_dst_len < 8 * sizeof(struct ila_locator) + 3) { |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 135 | /* Need to have full locator and at least type field |
| 136 | * included in destination |
| 137 | */ |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | iaddr = (struct ila_addr *)&cfg6->fc_dst; |
| 142 | |
Tom Herbert | 90bfe66 | 2016-04-23 11:46:57 -0700 | [diff] [blame] | 143 | if (!ila_addr_is_ila(iaddr) || ila_csum_neutral_set(iaddr->ident)) { |
| 144 | /* Don't allow translation for a non-ILA address or checksum |
| 145 | * neutral flag to be set. |
| 146 | */ |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | } |
| 149 | |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 150 | ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla, ila_nl_policy, extack); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | |
| 154 | if (!tb[ILA_ATTR_LOCATOR]) |
| 155 | return -EINVAL; |
| 156 | |
Thomas Graf | f76a9db | 2016-10-21 16:10:22 +0200 | [diff] [blame] | 157 | newts = lwtunnel_state_alloc(sizeof(*ilwt)); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 158 | if (!newts) |
| 159 | return -ENOMEM; |
| 160 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 161 | ilwt = ila_lwt_lwtunnel(newts); |
| 162 | ret = dst_cache_init(&ilwt->dst_cache, GFP_ATOMIC); |
| 163 | if (ret) { |
| 164 | kfree(newts); |
| 165 | return ret; |
| 166 | } |
| 167 | |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 168 | p = ila_params_lwtunnel(newts); |
| 169 | |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 170 | p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 171 | |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 172 | /* Precompute checksum difference for translation since we |
| 173 | * know both the old locator and the new one. |
| 174 | */ |
| 175 | p->locator_match = iaddr->loc; |
| 176 | p->csum_diff = compute_csum_diff8( |
| 177 | (__be32 *)&p->locator_match, (__be32 *)&p->locator); |
Tom Herbert | 92b78af | 2015-08-24 09:45:42 -0700 | [diff] [blame] | 178 | |
Tom Herbert | 90bfe66 | 2016-04-23 11:46:57 -0700 | [diff] [blame] | 179 | if (tb[ILA_ATTR_CSUM_MODE]) |
| 180 | p->csum_mode = nla_get_u8(tb[ILA_ATTR_CSUM_MODE]); |
| 181 | |
| 182 | ila_init_saved_csum(p); |
| 183 | |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 184 | newts->type = LWTUNNEL_ENCAP_ILA; |
| 185 | newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT | |
| 186 | LWTUNNEL_STATE_INPUT_REDIRECT; |
| 187 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 188 | if (cfg6->fc_dst_len == 8 * sizeof(struct in6_addr)) |
| 189 | ilwt->connected = 1; |
| 190 | |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 191 | *ts = newts; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 196 | static void ila_destroy_state(struct lwtunnel_state *lwt) |
| 197 | { |
| 198 | dst_cache_destroy(&ila_lwt_lwtunnel(lwt)->dst_cache); |
| 199 | } |
| 200 | |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 201 | static int ila_fill_encap_info(struct sk_buff *skb, |
| 202 | struct lwtunnel_state *lwtstate) |
| 203 | { |
| 204 | struct ila_params *p = ila_params_lwtunnel(lwtstate); |
| 205 | |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 206 | if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64, |
Nicolas Dichtel | f13a82d | 2016-04-25 10:25:16 +0200 | [diff] [blame] | 207 | ILA_ATTR_PAD)) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 208 | goto nla_put_failure; |
Tom Herbert | 1ddb6b7 | 2016-05-10 11:56:32 +0200 | [diff] [blame] | 209 | if (nla_put_u8(skb, ILA_ATTR_CSUM_MODE, (__force u8)p->csum_mode)) |
Tom Herbert | 90bfe66 | 2016-04-23 11:46:57 -0700 | [diff] [blame] | 210 | goto nla_put_failure; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | |
| 214 | nla_put_failure: |
| 215 | return -EMSGSIZE; |
| 216 | } |
| 217 | |
| 218 | static int ila_encap_nlsize(struct lwtunnel_state *lwtstate) |
| 219 | { |
Tom Herbert | 1ddb6b7 | 2016-05-10 11:56:32 +0200 | [diff] [blame] | 220 | return nla_total_size_64bit(sizeof(u64)) + /* ILA_ATTR_LOCATOR */ |
| 221 | nla_total_size(sizeof(u8)) + /* ILA_ATTR_CSUM_MODE */ |
| 222 | 0; |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 226 | { |
| 227 | struct ila_params *a_p = ila_params_lwtunnel(a); |
| 228 | struct ila_params *b_p = ila_params_lwtunnel(b); |
| 229 | |
Tom Herbert | 351596a | 2016-04-23 11:46:55 -0700 | [diff] [blame] | 230 | return (a_p->locator.v64 != b_p->locator.v64); |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static const struct lwtunnel_encap_ops ila_encap_ops = { |
| 234 | .build_state = ila_build_state, |
Tom Herbert | 79ff2fc | 2016-10-14 11:25:37 -0700 | [diff] [blame] | 235 | .destroy_state = ila_destroy_state, |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 236 | .output = ila_output, |
| 237 | .input = ila_input, |
| 238 | .fill_encap = ila_fill_encap_info, |
| 239 | .get_encap_size = ila_encap_nlsize, |
| 240 | .cmp_encap = ila_encap_cmp, |
Robert Shearman | 88ff733 | 2017-01-24 16:26:47 +0000 | [diff] [blame] | 241 | .owner = THIS_MODULE, |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Tom Herbert | 33f11d1 | 2015-12-15 15:41:35 -0800 | [diff] [blame] | 244 | int ila_lwt_init(void) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 245 | { |
| 246 | return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA); |
| 247 | } |
| 248 | |
Tom Herbert | 33f11d1 | 2015-12-15 15:41:35 -0800 | [diff] [blame] | 249 | void ila_lwt_fini(void) |
Tom Herbert | 65d7ab8 | 2015-08-17 13:42:27 -0700 | [diff] [blame] | 250 | { |
| 251 | lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA); |
| 252 | } |