blob: de7f6d76e92806ae247f3c163c7cc6ad085389d6 [file] [log] [blame]
Tom Herbert65d7ab82015-08-17 13:42:27 -07001#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>
Tom Herbert33f11d12015-12-15 15:41:35 -080014#include "ila.h"
Tom Herbert65d7ab82015-08-17 13:42:27 -070015
16static inline struct ila_params *ila_params_lwtunnel(
17 struct lwtunnel_state *lwstate)
18{
19 return (struct ila_params *)lwstate->data;
20}
21
Eric W. Biedermanede20592015-10-07 16:48:47 -050022static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Tom Herbert65d7ab82015-08-17 13:42:27 -070023{
24 struct dst_entry *dst = skb_dst(skb);
Tom Herbert65d7ab82015-08-17 13:42:27 -070025
26 if (skb->protocol != htons(ETH_P_IPV6))
27 goto drop;
28
Tom Herbert351596a2016-04-23 11:46:55 -070029 ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
Tom Herbert65d7ab82015-08-17 13:42:27 -070030
Eric W. Biedermanede20592015-10-07 16:48:47 -050031 return dst->lwtstate->orig_output(net, sk, skb);
Tom Herbert65d7ab82015-08-17 13:42:27 -070032
33drop:
34 kfree_skb(skb);
35 return -EINVAL;
36}
37
38static int ila_input(struct sk_buff *skb)
39{
40 struct dst_entry *dst = skb_dst(skb);
Tom Herbert65d7ab82015-08-17 13:42:27 -070041
42 if (skb->protocol != htons(ETH_P_IPV6))
43 goto drop;
44
Tom Herbert351596a2016-04-23 11:46:55 -070045 ila_update_ipv6_locator(skb, ila_params_lwtunnel(dst->lwtstate));
Tom Herbert65d7ab82015-08-17 13:42:27 -070046
Jiri Benc61adedf2015-08-20 13:56:25 +020047 return dst->lwtstate->orig_input(skb);
Tom Herbert65d7ab82015-08-17 13:42:27 -070048
49drop:
50 kfree_skb(skb);
51 return -EINVAL;
52}
53
54static struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
55 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
56};
57
58static int ila_build_state(struct net_device *dev, struct nlattr *nla,
Tom Herbert127eb7c2015-08-24 09:45:41 -070059 unsigned int family, const void *cfg,
Tom Herbert65d7ab82015-08-17 13:42:27 -070060 struct lwtunnel_state **ts)
61{
62 struct ila_params *p;
63 struct nlattr *tb[ILA_ATTR_MAX + 1];
64 size_t encap_len = sizeof(*p);
65 struct lwtunnel_state *newts;
Tom Herbert92b78af2015-08-24 09:45:42 -070066 const struct fib6_config *cfg6 = cfg;
Tom Herbert351596a2016-04-23 11:46:55 -070067 struct ila_addr *iaddr;
Tom Herbert65d7ab82015-08-17 13:42:27 -070068 int ret;
69
Tom Herbert92b78af2015-08-24 09:45:42 -070070 if (family != AF_INET6)
71 return -EINVAL;
72
Tom Herbert351596a2016-04-23 11:46:55 -070073 if (cfg6->fc_dst_len < sizeof(struct ila_locator) + 1) {
74 /* Need to have full locator and at least type field
75 * included in destination
76 */
77 return -EINVAL;
78 }
79
80 iaddr = (struct ila_addr *)&cfg6->fc_dst;
81
82 if (!ila_addr_is_ila(iaddr)) {
83 /* Don't allow setting a translation for a non-ILA address */
84 return -EINVAL;
85 }
86
Tom Herbert65d7ab82015-08-17 13:42:27 -070087 ret = nla_parse_nested(tb, ILA_ATTR_MAX, nla,
88 ila_nl_policy);
89 if (ret < 0)
90 return ret;
91
92 if (!tb[ILA_ATTR_LOCATOR])
93 return -EINVAL;
94
95 newts = lwtunnel_state_alloc(encap_len);
96 if (!newts)
97 return -ENOMEM;
98
99 newts->len = encap_len;
100 p = ila_params_lwtunnel(newts);
101
Tom Herbert351596a2016-04-23 11:46:55 -0700102 p->locator.v64 = (__force __be64)nla_get_u64(tb[ILA_ATTR_LOCATOR]);
Tom Herbert65d7ab82015-08-17 13:42:27 -0700103
Tom Herbert351596a2016-04-23 11:46:55 -0700104 /* Precompute checksum difference for translation since we
105 * know both the old locator and the new one.
106 */
107 p->locator_match = iaddr->loc;
108 p->csum_diff = compute_csum_diff8(
109 (__be32 *)&p->locator_match, (__be32 *)&p->locator);
Tom Herbert92b78af2015-08-24 09:45:42 -0700110
Tom Herbert65d7ab82015-08-17 13:42:27 -0700111 newts->type = LWTUNNEL_ENCAP_ILA;
112 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
113 LWTUNNEL_STATE_INPUT_REDIRECT;
114
115 *ts = newts;
116
117 return 0;
118}
119
120static int ila_fill_encap_info(struct sk_buff *skb,
121 struct lwtunnel_state *lwtstate)
122{
123 struct ila_params *p = ila_params_lwtunnel(lwtstate);
124
Tom Herbert351596a2016-04-23 11:46:55 -0700125 if (nla_put_u64_64bit(skb, ILA_ATTR_LOCATOR, (__force u64)p->locator.v64,
Nicolas Dichtelf13a82d2016-04-25 10:25:16 +0200126 ILA_ATTR_PAD))
Tom Herbert65d7ab82015-08-17 13:42:27 -0700127 goto nla_put_failure;
128
129 return 0;
130
131nla_put_failure:
132 return -EMSGSIZE;
133}
134
135static int ila_encap_nlsize(struct lwtunnel_state *lwtstate)
136{
137 /* No encapsulation overhead */
138 return 0;
139}
140
141static int ila_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
142{
143 struct ila_params *a_p = ila_params_lwtunnel(a);
144 struct ila_params *b_p = ila_params_lwtunnel(b);
145
Tom Herbert351596a2016-04-23 11:46:55 -0700146 return (a_p->locator.v64 != b_p->locator.v64);
Tom Herbert65d7ab82015-08-17 13:42:27 -0700147}
148
149static const struct lwtunnel_encap_ops ila_encap_ops = {
150 .build_state = ila_build_state,
151 .output = ila_output,
152 .input = ila_input,
153 .fill_encap = ila_fill_encap_info,
154 .get_encap_size = ila_encap_nlsize,
155 .cmp_encap = ila_encap_cmp,
156};
157
Tom Herbert33f11d12015-12-15 15:41:35 -0800158int ila_lwt_init(void)
Tom Herbert65d7ab82015-08-17 13:42:27 -0700159{
160 return lwtunnel_encap_add_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
161}
162
Tom Herbert33f11d12015-12-15 15:41:35 -0800163void ila_lwt_fini(void)
Tom Herbert65d7ab82015-08-17 13:42:27 -0700164{
165 lwtunnel_encap_del_ops(&ila_encap_ops, LWTUNNEL_ENCAP_ILA);
166}