blob: 6eb5e68f112a2045169233fc67fe18ac6f168d0a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tom Herbert7f00fea2015-12-15 15:41:38 -08002#include <linux/jhash.h>
3#include <linux/netfilter.h>
4#include <linux/rcupdate.h>
5#include <linux/rhashtable.h>
6#include <linux/vmalloc.h>
7#include <net/genetlink.h>
8#include <net/ila.h>
9#include <net/netns/generic.h>
10#include <uapi/linux/genetlink.h>
11#include "ila.h"
12
13struct ila_xlat_params {
14 struct ila_params ip;
Tom Herbert7f00fea2015-12-15 15:41:38 -080015 int ifindex;
Tom Herbert7f00fea2015-12-15 15:41:38 -080016};
17
18struct ila_map {
Tom Herbert351596a2016-04-23 11:46:55 -070019 struct ila_xlat_params xp;
Tom Herbert7f00fea2015-12-15 15:41:38 -080020 struct rhash_head node;
21 struct ila_map __rcu *next;
22 struct rcu_head rcu;
23};
24
25static unsigned int ila_net_id;
26
27struct ila_net {
28 struct rhashtable rhash_table;
29 spinlock_t *locks; /* Bucket locks for entry manipulation */
30 unsigned int locks_mask;
31 bool hooks_registered;
32};
33
34#define LOCKS_PER_CPU 10
35
36static int alloc_ila_locks(struct ila_net *ilan)
37{
38 unsigned int i, size;
39 unsigned int nr_pcpus = num_possible_cpus();
40
41 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
42 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
43
44 if (sizeof(spinlock_t) != 0) {
Michal Hocko847f7162017-05-08 15:57:21 -070045 ilan->locks = kvmalloc(size * sizeof(spinlock_t), GFP_KERNEL);
Tom Herbert7f00fea2015-12-15 15:41:38 -080046 if (!ilan->locks)
47 return -ENOMEM;
48 for (i = 0; i < size; i++)
49 spin_lock_init(&ilan->locks[i]);
50 }
51 ilan->locks_mask = size - 1;
52
53 return 0;
54}
55
56static u32 hashrnd __read_mostly;
57static __always_inline void __ila_hash_secret_init(void)
58{
59 net_get_random_once(&hashrnd, sizeof(hashrnd));
60}
61
Tom Herbert642c2c92016-04-23 11:46:56 -070062static inline u32 ila_locator_hash(struct ila_locator loc)
Tom Herbert7f00fea2015-12-15 15:41:38 -080063{
Tom Herbert642c2c92016-04-23 11:46:56 -070064 u32 *v = (u32 *)loc.v32;
Tom Herbert7f00fea2015-12-15 15:41:38 -080065
Arnd Bergmann0db47e32017-06-08 09:54:24 +020066 __ila_hash_secret_init();
Tom Herbert7f00fea2015-12-15 15:41:38 -080067 return jhash_2words(v[0], v[1], hashrnd);
68}
69
Tom Herbert351596a2016-04-23 11:46:55 -070070static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
Tom Herbert642c2c92016-04-23 11:46:56 -070071 struct ila_locator loc)
Tom Herbert7f00fea2015-12-15 15:41:38 -080072{
Tom Herbert642c2c92016-04-23 11:46:56 -070073 return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
Tom Herbert7f00fea2015-12-15 15:41:38 -080074}
75
Tom Herbert351596a2016-04-23 11:46:55 -070076static inline int ila_cmp_wildcards(struct ila_map *ila,
Tom Herbert642c2c92016-04-23 11:46:56 -070077 struct ila_addr *iaddr, int ifindex)
Tom Herbert7f00fea2015-12-15 15:41:38 -080078{
Tom Herbert642c2c92016-04-23 11:46:56 -070079 return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
Tom Herbert7f00fea2015-12-15 15:41:38 -080080}
81
Tom Herbert351596a2016-04-23 11:46:55 -070082static inline int ila_cmp_params(struct ila_map *ila,
83 struct ila_xlat_params *xp)
Tom Herbert7f00fea2015-12-15 15:41:38 -080084{
Tom Herbert642c2c92016-04-23 11:46:56 -070085 return (ila->xp.ifindex != xp->ifindex);
Tom Herbert7f00fea2015-12-15 15:41:38 -080086}
87
88static int ila_cmpfn(struct rhashtable_compare_arg *arg,
89 const void *obj)
90{
91 const struct ila_map *ila = obj;
92
Tom Herbert642c2c92016-04-23 11:46:56 -070093 return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
Tom Herbert7f00fea2015-12-15 15:41:38 -080094}
95
96static inline int ila_order(struct ila_map *ila)
97{
98 int score = 0;
99
Tom Herbert351596a2016-04-23 11:46:55 -0700100 if (ila->xp.ifindex)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800101 score += 1 << 1;
102
103 return score;
104}
105
106static const struct rhashtable_params rht_params = {
107 .nelem_hint = 1024,
108 .head_offset = offsetof(struct ila_map, node),
Tom Herbert642c2c92016-04-23 11:46:56 -0700109 .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
Tom Herbert7f00fea2015-12-15 15:41:38 -0800110 .key_len = sizeof(u64), /* identifier */
111 .max_size = 1048576,
112 .min_size = 256,
113 .automatic_shrinking = true,
114 .obj_cmpfn = ila_cmpfn,
115};
116
Johannes Berg489111e2016-10-24 14:40:03 +0200117static struct genl_family ila_nl_family;
Tom Herbert7f00fea2015-12-15 15:41:38 -0800118
stephen hemminger6501f342016-08-31 15:20:51 -0700119static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
Tom Herbert7f00fea2015-12-15 15:41:38 -0800120 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
121 [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
122 [ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
Tom Herbert90bfe662016-04-23 11:46:57 -0700123 [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
Tom Herbert70d5aef2017-11-05 15:58:24 -0800124 [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
Tom Herbert7f00fea2015-12-15 15:41:38 -0800125};
126
127static int parse_nl_config(struct genl_info *info,
Tom Herbert351596a2016-04-23 11:46:55 -0700128 struct ila_xlat_params *xp)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800129{
Tom Herbert351596a2016-04-23 11:46:55 -0700130 memset(xp, 0, sizeof(*xp));
Tom Herbert7f00fea2015-12-15 15:41:38 -0800131
Tom Herbert7f00fea2015-12-15 15:41:38 -0800132 if (info->attrs[ILA_ATTR_LOCATOR])
Tom Herbert351596a2016-04-23 11:46:55 -0700133 xp->ip.locator.v64 = (__force __be64)nla_get_u64(
Tom Herbert7f00fea2015-12-15 15:41:38 -0800134 info->attrs[ILA_ATTR_LOCATOR]);
135
136 if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
Tom Herbert351596a2016-04-23 11:46:55 -0700137 xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
Tom Herbert7f00fea2015-12-15 15:41:38 -0800138 info->attrs[ILA_ATTR_LOCATOR_MATCH]);
139
Tom Herbert90bfe662016-04-23 11:46:57 -0700140 if (info->attrs[ILA_ATTR_CSUM_MODE])
141 xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
Tom Herbert84287bb2017-11-05 15:58:23 -0800142 else
143 xp->ip.csum_mode = ILA_CSUM_NO_ACTION;
Tom Herbert90bfe662016-04-23 11:46:57 -0700144
Tom Herbert70d5aef2017-11-05 15:58:24 -0800145 if (info->attrs[ILA_ATTR_IDENT_TYPE])
146 xp->ip.ident_type = nla_get_u8(
147 info->attrs[ILA_ATTR_IDENT_TYPE]);
148 else
149 xp->ip.ident_type = ILA_ATYPE_USE_FORMAT;
150
Tom Herbert7f00fea2015-12-15 15:41:38 -0800151 if (info->attrs[ILA_ATTR_IFINDEX])
Tom Herbert351596a2016-04-23 11:46:55 -0700152 xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800153
Tom Herbert7f00fea2015-12-15 15:41:38 -0800154 return 0;
155}
156
157/* Must be called with rcu readlock */
Tom Herbert351596a2016-04-23 11:46:55 -0700158static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
Tom Herbert7f00fea2015-12-15 15:41:38 -0800159 int ifindex,
Tom Herbert7f00fea2015-12-15 15:41:38 -0800160 struct ila_net *ilan)
161{
162 struct ila_map *ila;
163
Tom Herbert642c2c92016-04-23 11:46:56 -0700164 ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
Tom Herbert351596a2016-04-23 11:46:55 -0700165 rht_params);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800166 while (ila) {
Tom Herbert642c2c92016-04-23 11:46:56 -0700167 if (!ila_cmp_wildcards(ila, iaddr, ifindex))
Tom Herbert7f00fea2015-12-15 15:41:38 -0800168 return ila;
169 ila = rcu_access_pointer(ila->next);
170 }
171
172 return NULL;
173}
174
175/* Must be called with rcu readlock */
Tom Herbert351596a2016-04-23 11:46:55 -0700176static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
Tom Herbert7f00fea2015-12-15 15:41:38 -0800177 struct ila_net *ilan)
178{
179 struct ila_map *ila;
180
Tom Herbert642c2c92016-04-23 11:46:56 -0700181 ila = rhashtable_lookup_fast(&ilan->rhash_table,
182 &xp->ip.locator_match,
Tom Herbert7f00fea2015-12-15 15:41:38 -0800183 rht_params);
184 while (ila) {
Tom Herbert351596a2016-04-23 11:46:55 -0700185 if (!ila_cmp_params(ila, xp))
Tom Herbert7f00fea2015-12-15 15:41:38 -0800186 return ila;
187 ila = rcu_access_pointer(ila->next);
188 }
189
190 return NULL;
191}
192
193static inline void ila_release(struct ila_map *ila)
194{
195 kfree_rcu(ila, rcu);
196}
197
198static void ila_free_cb(void *ptr, void *arg)
199{
200 struct ila_map *ila = (struct ila_map *)ptr, *next;
201
202 /* Assume rcu_readlock held */
203 while (ila) {
204 next = rcu_access_pointer(ila->next);
205 ila_release(ila);
206 ila = next;
207 }
208}
209
Tom Herbert84287bb2017-11-05 15:58:23 -0800210static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800211
212static unsigned int
213ila_nf_input(void *priv,
214 struct sk_buff *skb,
215 const struct nf_hook_state *state)
216{
Tom Herbert707a2ca2016-06-07 16:09:44 -0700217 ila_xlat_addr(skb, false);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800218 return NF_ACCEPT;
219}
220
Florian Westphal591bb272017-07-26 11:40:52 +0200221static const struct nf_hook_ops ila_nf_hook_ops[] = {
Tom Herbert7f00fea2015-12-15 15:41:38 -0800222 {
223 .hook = ila_nf_input,
224 .pf = NFPROTO_IPV6,
225 .hooknum = NF_INET_PRE_ROUTING,
226 .priority = -1,
227 },
228};
229
Tom Herbert351596a2016-04-23 11:46:55 -0700230static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800231{
232 struct ila_net *ilan = net_generic(net, ila_net_id);
233 struct ila_map *ila, *head;
Tom Herbert642c2c92016-04-23 11:46:56 -0700234 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800235 int err = 0, order;
236
237 if (!ilan->hooks_registered) {
238 /* We defer registering net hooks in the namespace until the
239 * first mapping is added.
240 */
241 err = nf_register_net_hooks(net, ila_nf_hook_ops,
242 ARRAY_SIZE(ila_nf_hook_ops));
243 if (err)
244 return err;
245
246 ilan->hooks_registered = true;
247 }
248
249 ila = kzalloc(sizeof(*ila), GFP_KERNEL);
250 if (!ila)
251 return -ENOMEM;
252
Tom Herbert90bfe662016-04-23 11:46:57 -0700253 ila_init_saved_csum(&xp->ip);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800254
Tom Herbert90bfe662016-04-23 11:46:57 -0700255 ila->xp = *xp;
Tom Herbert7f00fea2015-12-15 15:41:38 -0800256
257 order = ila_order(ila);
258
259 spin_lock(lock);
260
Tom Herbert642c2c92016-04-23 11:46:56 -0700261 head = rhashtable_lookup_fast(&ilan->rhash_table,
262 &xp->ip.locator_match,
Tom Herbert7f00fea2015-12-15 15:41:38 -0800263 rht_params);
264 if (!head) {
265 /* New entry for the rhash_table */
266 err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
267 &ila->node, rht_params);
268 } else {
269 struct ila_map *tila = head, *prev = NULL;
270
271 do {
Tom Herbert351596a2016-04-23 11:46:55 -0700272 if (!ila_cmp_params(tila, xp)) {
Tom Herbert7f00fea2015-12-15 15:41:38 -0800273 err = -EEXIST;
274 goto out;
275 }
276
277 if (order > ila_order(tila))
278 break;
279
280 prev = tila;
281 tila = rcu_dereference_protected(tila->next,
282 lockdep_is_held(lock));
283 } while (tila);
284
285 if (prev) {
286 /* Insert in sub list of head */
287 RCU_INIT_POINTER(ila->next, tila);
288 rcu_assign_pointer(prev->next, ila);
289 } else {
290 /* Make this ila new head */
291 RCU_INIT_POINTER(ila->next, head);
292 err = rhashtable_replace_fast(&ilan->rhash_table,
293 &head->node,
294 &ila->node, rht_params);
295 if (err)
296 goto out;
297 }
298 }
299
300out:
301 spin_unlock(lock);
302
303 if (err)
304 kfree(ila);
305
306 return err;
307}
308
Tom Herbert351596a2016-04-23 11:46:55 -0700309static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800310{
311 struct ila_net *ilan = net_generic(net, ila_net_id);
312 struct ila_map *ila, *head, *prev;
Tom Herbert642c2c92016-04-23 11:46:56 -0700313 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800314 int err = -ENOENT;
315
316 spin_lock(lock);
317
318 head = rhashtable_lookup_fast(&ilan->rhash_table,
Tom Herbert642c2c92016-04-23 11:46:56 -0700319 &xp->ip.locator_match, rht_params);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800320 ila = head;
321
322 prev = NULL;
323
324 while (ila) {
Tom Herbert351596a2016-04-23 11:46:55 -0700325 if (ila_cmp_params(ila, xp)) {
Tom Herbert7f00fea2015-12-15 15:41:38 -0800326 prev = ila;
327 ila = rcu_dereference_protected(ila->next,
328 lockdep_is_held(lock));
329 continue;
330 }
331
332 err = 0;
333
334 if (prev) {
335 /* Not head, just delete from list */
336 rcu_assign_pointer(prev->next, ila->next);
337 } else {
338 /* It is the head. If there is something in the
339 * sublist we need to make a new head.
340 */
341 head = rcu_dereference_protected(ila->next,
342 lockdep_is_held(lock));
343 if (head) {
344 /* Put first entry in the sublist into the
345 * table
346 */
347 err = rhashtable_replace_fast(
348 &ilan->rhash_table, &ila->node,
349 &head->node, rht_params);
350 if (err)
351 goto out;
352 } else {
353 /* Entry no longer used */
354 err = rhashtable_remove_fast(&ilan->rhash_table,
355 &ila->node,
356 rht_params);
357 }
358 }
359
360 ila_release(ila);
361
362 break;
363 }
364
365out:
366 spin_unlock(lock);
367
368 return err;
369}
370
371static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
372{
373 struct net *net = genl_info_net(info);
374 struct ila_xlat_params p;
375 int err;
376
377 err = parse_nl_config(info, &p);
378 if (err)
379 return err;
380
381 return ila_add_mapping(net, &p);
382}
383
384static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
385{
386 struct net *net = genl_info_net(info);
Tom Herbert351596a2016-04-23 11:46:55 -0700387 struct ila_xlat_params xp;
Tom Herbert7f00fea2015-12-15 15:41:38 -0800388 int err;
389
Tom Herbert351596a2016-04-23 11:46:55 -0700390 err = parse_nl_config(info, &xp);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800391 if (err)
392 return err;
393
Tom Herbert351596a2016-04-23 11:46:55 -0700394 ila_del_mapping(net, &xp);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800395
396 return 0;
397}
398
399static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
400{
Tom Herbert642c2c92016-04-23 11:46:56 -0700401 if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
Tom Herbert351596a2016-04-23 11:46:55 -0700402 (__force u64)ila->xp.ip.locator.v64,
Nicolas Dichtelf13a82d2016-04-25 10:25:16 +0200403 ILA_ATTR_PAD) ||
404 nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
Tom Herbert351596a2016-04-23 11:46:55 -0700405 (__force u64)ila->xp.ip.locator_match.v64,
Nicolas Dichtelf13a82d2016-04-25 10:25:16 +0200406 ILA_ATTR_PAD) ||
Tom Herbert90bfe662016-04-23 11:46:57 -0700407 nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
Tom Herbert70d5aef2017-11-05 15:58:24 -0800408 nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
409 nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
Tom Herbert7f00fea2015-12-15 15:41:38 -0800410 return -1;
411
412 return 0;
413}
414
415static int ila_dump_info(struct ila_map *ila,
416 u32 portid, u32 seq, u32 flags,
417 struct sk_buff *skb, u8 cmd)
418{
419 void *hdr;
420
421 hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
422 if (!hdr)
423 return -ENOMEM;
424
425 if (ila_fill_info(ila, skb) < 0)
426 goto nla_put_failure;
427
428 genlmsg_end(skb, hdr);
429 return 0;
430
431nla_put_failure:
432 genlmsg_cancel(skb, hdr);
433 return -EMSGSIZE;
434}
435
436static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
437{
438 struct net *net = genl_info_net(info);
439 struct ila_net *ilan = net_generic(net, ila_net_id);
440 struct sk_buff *msg;
Tom Herbert351596a2016-04-23 11:46:55 -0700441 struct ila_xlat_params xp;
Tom Herbert7f00fea2015-12-15 15:41:38 -0800442 struct ila_map *ila;
443 int ret;
444
Tom Herbert351596a2016-04-23 11:46:55 -0700445 ret = parse_nl_config(info, &xp);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800446 if (ret)
447 return ret;
448
449 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
450 if (!msg)
451 return -ENOMEM;
452
453 rcu_read_lock();
454
Tom Herbert351596a2016-04-23 11:46:55 -0700455 ila = ila_lookup_by_params(&xp, ilan);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800456 if (ila) {
457 ret = ila_dump_info(ila,
458 info->snd_portid,
459 info->snd_seq, 0, msg,
460 info->genlhdr->cmd);
461 }
462
463 rcu_read_unlock();
464
465 if (ret < 0)
466 goto out_free;
467
468 return genlmsg_reply(msg, info);
469
470out_free:
471 nlmsg_free(msg);
472 return ret;
473}
474
475struct ila_dump_iter {
476 struct rhashtable_iter rhiter;
477};
478
479static int ila_nl_dump_start(struct netlink_callback *cb)
480{
481 struct net *net = sock_net(cb->skb->sk);
482 struct ila_net *ilan = net_generic(net, ila_net_id);
Tom Herbert19135402016-11-01 14:55:25 -0700483 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
484
485 if (!iter) {
486 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
487 if (!iter)
488 return -ENOMEM;
489
490 cb->args[0] = (long)iter;
491 }
Tom Herbert7f00fea2015-12-15 15:41:38 -0800492
Bob Copeland8f6fd832016-03-02 10:09:19 -0500493 return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
494 GFP_KERNEL);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800495}
496
497static int ila_nl_dump_done(struct netlink_callback *cb)
498{
Tom Herbert19135402016-11-01 14:55:25 -0700499 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
Tom Herbert7f00fea2015-12-15 15:41:38 -0800500
501 rhashtable_walk_exit(&iter->rhiter);
502
Tom Herbert19135402016-11-01 14:55:25 -0700503 kfree(iter);
504
Tom Herbert7f00fea2015-12-15 15:41:38 -0800505 return 0;
506}
507
508static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
509{
Tom Herbert19135402016-11-01 14:55:25 -0700510 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
Tom Herbert7f00fea2015-12-15 15:41:38 -0800511 struct rhashtable_iter *rhiter = &iter->rhiter;
512 struct ila_map *ila;
513 int ret;
514
515 ret = rhashtable_walk_start(rhiter);
516 if (ret && ret != -EAGAIN)
517 goto done;
518
519 for (;;) {
520 ila = rhashtable_walk_next(rhiter);
521
522 if (IS_ERR(ila)) {
523 if (PTR_ERR(ila) == -EAGAIN)
524 continue;
525 ret = PTR_ERR(ila);
526 goto done;
527 } else if (!ila) {
528 break;
529 }
530
531 while (ila) {
532 ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
533 cb->nlh->nlmsg_seq, NLM_F_MULTI,
534 skb, ILA_CMD_GET);
535 if (ret)
536 goto done;
537
538 ila = rcu_access_pointer(ila->next);
539 }
540 }
541
542 ret = skb->len;
543
544done:
545 rhashtable_walk_stop(rhiter);
546 return ret;
547}
548
549static const struct genl_ops ila_nl_ops[] = {
550 {
551 .cmd = ILA_CMD_ADD,
552 .doit = ila_nl_cmd_add_mapping,
553 .policy = ila_nl_policy,
554 .flags = GENL_ADMIN_PERM,
555 },
556 {
557 .cmd = ILA_CMD_DEL,
558 .doit = ila_nl_cmd_del_mapping,
559 .policy = ila_nl_policy,
560 .flags = GENL_ADMIN_PERM,
561 },
562 {
563 .cmd = ILA_CMD_GET,
564 .doit = ila_nl_cmd_get_mapping,
565 .start = ila_nl_dump_start,
566 .dumpit = ila_nl_dump,
567 .done = ila_nl_dump_done,
568 .policy = ila_nl_policy,
569 },
570};
571
Johannes Berg56989f62016-10-24 14:40:05 +0200572static struct genl_family ila_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200573 .hdrsize = 0,
574 .name = ILA_GENL_NAME,
575 .version = ILA_GENL_VERSION,
576 .maxattr = ILA_ATTR_MAX,
577 .netnsok = true,
578 .parallel_ops = true,
579 .module = THIS_MODULE,
580 .ops = ila_nl_ops,
581 .n_ops = ARRAY_SIZE(ila_nl_ops),
582};
583
Tom Herbert7f00fea2015-12-15 15:41:38 -0800584#define ILA_HASH_TABLE_SIZE 1024
585
586static __net_init int ila_init_net(struct net *net)
587{
588 int err;
589 struct ila_net *ilan = net_generic(net, ila_net_id);
590
591 err = alloc_ila_locks(ilan);
592 if (err)
593 return err;
594
595 rhashtable_init(&ilan->rhash_table, &rht_params);
596
597 return 0;
598}
599
600static __net_exit void ila_exit_net(struct net *net)
601{
602 struct ila_net *ilan = net_generic(net, ila_net_id);
603
604 rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);
605
606 kvfree(ilan->locks);
607
608 if (ilan->hooks_registered)
609 nf_unregister_net_hooks(net, ila_nf_hook_ops,
610 ARRAY_SIZE(ila_nf_hook_ops));
611}
612
613static struct pernet_operations ila_net_ops = {
614 .init = ila_init_net,
615 .exit = ila_exit_net,
616 .id = &ila_net_id,
617 .size = sizeof(struct ila_net),
618};
619
Tom Herbert84287bb2017-11-05 15:58:23 -0800620static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800621{
622 struct ila_map *ila;
623 struct ipv6hdr *ip6h = ipv6_hdr(skb);
624 struct net *net = dev_net(skb->dev);
625 struct ila_net *ilan = net_generic(net, ila_net_id);
Tom Herbert351596a2016-04-23 11:46:55 -0700626 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800627
628 /* Assumes skb contains a valid IPv6 header that is pulled */
629
Tom Herbert70d5aef2017-11-05 15:58:24 -0800630 /* No check here that ILA type in the mapping matches what is in the
631 * address. We assume that whatever sender gaves us can be translated.
632 * The checksum mode however is relevant.
633 */
Tom Herbert7f00fea2015-12-15 15:41:38 -0800634
635 rcu_read_lock();
636
Tom Herbert642c2c92016-04-23 11:46:56 -0700637 ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800638 if (ila)
Tom Herbert84287bb2017-11-05 15:58:23 -0800639 ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800640
641 rcu_read_unlock();
642
643 return 0;
644}
645
Johannes Berg56989f62016-10-24 14:40:05 +0200646int __init ila_xlat_init(void)
Tom Herbert7f00fea2015-12-15 15:41:38 -0800647{
648 int ret;
649
650 ret = register_pernet_device(&ila_net_ops);
651 if (ret)
652 goto exit;
653
Johannes Berg489111e2016-10-24 14:40:03 +0200654 ret = genl_register_family(&ila_nl_family);
Tom Herbert7f00fea2015-12-15 15:41:38 -0800655 if (ret < 0)
656 goto unregister;
657
658 return 0;
659
660unregister:
661 unregister_pernet_device(&ila_net_ops);
662exit:
663 return ret;
664}
665
666void ila_xlat_fini(void)
667{
668 genl_unregister_family(&ila_nl_family);
669 unregister_pernet_device(&ila_net_ops);
670}