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