]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ipv6/ila/ila_xlat.c
0d57e27d1cdd2dc744a1f7bbae3032aa6955f986
[linux.git] / net / ipv6 / ila / ila_xlat.c
1 #include <linux/jhash.h>
2 #include <linux/netfilter.h>
3 #include <linux/rcupdate.h>
4 #include <linux/rhashtable.h>
5 #include <linux/vmalloc.h>
6 #include <net/genetlink.h>
7 #include <net/ila.h>
8 #include <net/netns/generic.h>
9 #include <uapi/linux/genetlink.h>
10 #include "ila.h"
11
12 struct ila_xlat_params {
13         struct ila_params ip;
14         int ifindex;
15 };
16
17 struct ila_map {
18         struct ila_xlat_params xp;
19         struct rhash_head node;
20         struct ila_map __rcu *next;
21         struct rcu_head rcu;
22 };
23
24 static unsigned int ila_net_id;
25
26 struct ila_net {
27         struct rhashtable rhash_table;
28         spinlock_t *locks; /* Bucket locks for entry manipulation */
29         unsigned int locks_mask;
30         bool hooks_registered;
31 };
32
33 #define LOCKS_PER_CPU 10
34
35 static int alloc_ila_locks(struct ila_net *ilan)
36 {
37         unsigned int i, size;
38         unsigned int nr_pcpus = num_possible_cpus();
39
40         nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
41         size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
42
43         if (sizeof(spinlock_t) != 0) {
44 #ifdef CONFIG_NUMA
45                 if (size * sizeof(spinlock_t) > PAGE_SIZE)
46                         ilan->locks = vmalloc(size * sizeof(spinlock_t));
47                 else
48 #endif
49                 ilan->locks = kmalloc_array(size, sizeof(spinlock_t),
50                                             GFP_KERNEL);
51                 if (!ilan->locks)
52                         return -ENOMEM;
53                 for (i = 0; i < size; i++)
54                         spin_lock_init(&ilan->locks[i]);
55         }
56         ilan->locks_mask = size - 1;
57
58         return 0;
59 }
60
61 static u32 hashrnd __read_mostly;
62 static __always_inline void __ila_hash_secret_init(void)
63 {
64         net_get_random_once(&hashrnd, sizeof(hashrnd));
65 }
66
67 static inline u32 ila_locator_hash(struct ila_locator loc)
68 {
69         u32 *v = (u32 *)loc.v32;
70
71         return jhash_2words(v[0], v[1], hashrnd);
72 }
73
74 static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
75                                        struct ila_locator loc)
76 {
77         return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
78 }
79
80 static inline int ila_cmp_wildcards(struct ila_map *ila,
81                                     struct ila_addr *iaddr, int ifindex)
82 {
83         return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
84 }
85
86 static inline int ila_cmp_params(struct ila_map *ila,
87                                  struct ila_xlat_params *xp)
88 {
89         return (ila->xp.ifindex != xp->ifindex);
90 }
91
92 static int ila_cmpfn(struct rhashtable_compare_arg *arg,
93                      const void *obj)
94 {
95         const struct ila_map *ila = obj;
96
97         return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
98 }
99
100 static inline int ila_order(struct ila_map *ila)
101 {
102         int score = 0;
103
104         if (ila->xp.ifindex)
105                 score += 1 << 1;
106
107         return score;
108 }
109
110 static const struct rhashtable_params rht_params = {
111         .nelem_hint = 1024,
112         .head_offset = offsetof(struct ila_map, node),
113         .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
114         .key_len = sizeof(u64), /* identifier */
115         .max_size = 1048576,
116         .min_size = 256,
117         .automatic_shrinking = true,
118         .obj_cmpfn = ila_cmpfn,
119 };
120
121 static struct genl_family ila_nl_family = {
122         .hdrsize        = 0,
123         .name           = ILA_GENL_NAME,
124         .version        = ILA_GENL_VERSION,
125         .maxattr        = ILA_ATTR_MAX,
126         .netnsok        = true,
127         .parallel_ops   = true,
128 };
129
130 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
131         [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
132         [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
133         [ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
134         [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
135 };
136
137 static int parse_nl_config(struct genl_info *info,
138                            struct ila_xlat_params *xp)
139 {
140         memset(xp, 0, sizeof(*xp));
141
142         if (info->attrs[ILA_ATTR_LOCATOR])
143                 xp->ip.locator.v64 = (__force __be64)nla_get_u64(
144                         info->attrs[ILA_ATTR_LOCATOR]);
145
146         if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
147                 xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
148                         info->attrs[ILA_ATTR_LOCATOR_MATCH]);
149
150         if (info->attrs[ILA_ATTR_CSUM_MODE])
151                 xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
152
153         if (info->attrs[ILA_ATTR_IFINDEX])
154                 xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
155
156         return 0;
157 }
158
159 /* Must be called with rcu readlock */
160 static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
161                                                    int ifindex,
162                                                    struct ila_net *ilan)
163 {
164         struct ila_map *ila;
165
166         ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
167                                      rht_params);
168         while (ila) {
169                 if (!ila_cmp_wildcards(ila, iaddr, ifindex))
170                         return ila;
171                 ila = rcu_access_pointer(ila->next);
172         }
173
174         return NULL;
175 }
176
177 /* Must be called with rcu readlock */
178 static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
179                                                    struct ila_net *ilan)
180 {
181         struct ila_map *ila;
182
183         ila = rhashtable_lookup_fast(&ilan->rhash_table,
184                                      &xp->ip.locator_match,
185                                      rht_params);
186         while (ila) {
187                 if (!ila_cmp_params(ila, xp))
188                         return ila;
189                 ila = rcu_access_pointer(ila->next);
190         }
191
192         return NULL;
193 }
194
195 static inline void ila_release(struct ila_map *ila)
196 {
197         kfree_rcu(ila, rcu);
198 }
199
200 static void ila_free_cb(void *ptr, void *arg)
201 {
202         struct ila_map *ila = (struct ila_map *)ptr, *next;
203
204         /* Assume rcu_readlock held */
205         while (ila) {
206                 next = rcu_access_pointer(ila->next);
207                 ila_release(ila);
208                 ila = next;
209         }
210 }
211
212 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral);
213
214 static unsigned int
215 ila_nf_input(void *priv,
216              struct sk_buff *skb,
217              const struct nf_hook_state *state)
218 {
219         ila_xlat_addr(skb, false);
220         return NF_ACCEPT;
221 }
222
223 static struct nf_hook_ops ila_nf_hook_ops[] __read_mostly = {
224         {
225                 .hook = ila_nf_input,
226                 .pf = NFPROTO_IPV6,
227                 .hooknum = NF_INET_PRE_ROUTING,
228                 .priority = -1,
229         },
230 };
231
232 static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
233 {
234         struct ila_net *ilan = net_generic(net, ila_net_id);
235         struct ila_map *ila, *head;
236         spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
237         int err = 0, order;
238
239         if (!ilan->hooks_registered) {
240                 /* We defer registering net hooks in the namespace until the
241                  * first mapping is added.
242                  */
243                 err = nf_register_net_hooks(net, ila_nf_hook_ops,
244                                             ARRAY_SIZE(ila_nf_hook_ops));
245                 if (err)
246                         return err;
247
248                 ilan->hooks_registered = true;
249         }
250
251         ila = kzalloc(sizeof(*ila), GFP_KERNEL);
252         if (!ila)
253                 return -ENOMEM;
254
255         ila_init_saved_csum(&xp->ip);
256
257         ila->xp = *xp;
258
259         order = ila_order(ila);
260
261         spin_lock(lock);
262
263         head = rhashtable_lookup_fast(&ilan->rhash_table,
264                                       &xp->ip.locator_match,
265                                       rht_params);
266         if (!head) {
267                 /* New entry for the rhash_table */
268                 err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
269                                                     &ila->node, rht_params);
270         } else {
271                 struct ila_map *tila = head, *prev = NULL;
272
273                 do {
274                         if (!ila_cmp_params(tila, xp)) {
275                                 err = -EEXIST;
276                                 goto out;
277                         }
278
279                         if (order > ila_order(tila))
280                                 break;
281
282                         prev = tila;
283                         tila = rcu_dereference_protected(tila->next,
284                                 lockdep_is_held(lock));
285                 } while (tila);
286
287                 if (prev) {
288                         /* Insert in sub list of head */
289                         RCU_INIT_POINTER(ila->next, tila);
290                         rcu_assign_pointer(prev->next, ila);
291                 } else {
292                         /* Make this ila new head */
293                         RCU_INIT_POINTER(ila->next, head);
294                         err = rhashtable_replace_fast(&ilan->rhash_table,
295                                                       &head->node,
296                                                       &ila->node, rht_params);
297                         if (err)
298                                 goto out;
299                 }
300         }
301
302 out:
303         spin_unlock(lock);
304
305         if (err)
306                 kfree(ila);
307
308         return err;
309 }
310
311 static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
312 {
313         struct ila_net *ilan = net_generic(net, ila_net_id);
314         struct ila_map *ila, *head, *prev;
315         spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
316         int err = -ENOENT;
317
318         spin_lock(lock);
319
320         head = rhashtable_lookup_fast(&ilan->rhash_table,
321                                       &xp->ip.locator_match, rht_params);
322         ila = head;
323
324         prev = NULL;
325
326         while (ila) {
327                 if (ila_cmp_params(ila, xp)) {
328                         prev = ila;
329                         ila = rcu_dereference_protected(ila->next,
330                                                         lockdep_is_held(lock));
331                         continue;
332                 }
333
334                 err = 0;
335
336                 if (prev) {
337                         /* Not head, just delete from list */
338                         rcu_assign_pointer(prev->next, ila->next);
339                 } else {
340                         /* It is the head. If there is something in the
341                          * sublist we need to make a new head.
342                          */
343                         head = rcu_dereference_protected(ila->next,
344                                                          lockdep_is_held(lock));
345                         if (head) {
346                                 /* Put first entry in the sublist into the
347                                  * table
348                                  */
349                                 err = rhashtable_replace_fast(
350                                         &ilan->rhash_table, &ila->node,
351                                         &head->node, rht_params);
352                                 if (err)
353                                         goto out;
354                         } else {
355                                 /* Entry no longer used */
356                                 err = rhashtable_remove_fast(&ilan->rhash_table,
357                                                              &ila->node,
358                                                              rht_params);
359                         }
360                 }
361
362                 ila_release(ila);
363
364                 break;
365         }
366
367 out:
368         spin_unlock(lock);
369
370         return err;
371 }
372
373 static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
374 {
375         struct net *net = genl_info_net(info);
376         struct ila_xlat_params p;
377         int err;
378
379         err = parse_nl_config(info, &p);
380         if (err)
381                 return err;
382
383         return ila_add_mapping(net, &p);
384 }
385
386 static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
387 {
388         struct net *net = genl_info_net(info);
389         struct ila_xlat_params xp;
390         int err;
391
392         err = parse_nl_config(info, &xp);
393         if (err)
394                 return err;
395
396         ila_del_mapping(net, &xp);
397
398         return 0;
399 }
400
401 static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
402 {
403         if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
404                               (__force u64)ila->xp.ip.locator.v64,
405                               ILA_ATTR_PAD) ||
406             nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
407                               (__force u64)ila->xp.ip.locator_match.v64,
408                               ILA_ATTR_PAD) ||
409             nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
410             nla_put_u32(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode))
411                 return -1;
412
413         return 0;
414 }
415
416 static int ila_dump_info(struct ila_map *ila,
417                          u32 portid, u32 seq, u32 flags,
418                          struct sk_buff *skb, u8 cmd)
419 {
420         void *hdr;
421
422         hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
423         if (!hdr)
424                 return -ENOMEM;
425
426         if (ila_fill_info(ila, skb) < 0)
427                 goto nla_put_failure;
428
429         genlmsg_end(skb, hdr);
430         return 0;
431
432 nla_put_failure:
433         genlmsg_cancel(skb, hdr);
434         return -EMSGSIZE;
435 }
436
437 static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
438 {
439         struct net *net = genl_info_net(info);
440         struct ila_net *ilan = net_generic(net, ila_net_id);
441         struct sk_buff *msg;
442         struct ila_xlat_params xp;
443         struct ila_map *ila;
444         int ret;
445
446         ret = parse_nl_config(info, &xp);
447         if (ret)
448                 return ret;
449
450         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
451         if (!msg)
452                 return -ENOMEM;
453
454         rcu_read_lock();
455
456         ila = ila_lookup_by_params(&xp, ilan);
457         if (ila) {
458                 ret = ila_dump_info(ila,
459                                     info->snd_portid,
460                                     info->snd_seq, 0, msg,
461                                     info->genlhdr->cmd);
462         }
463
464         rcu_read_unlock();
465
466         if (ret < 0)
467                 goto out_free;
468
469         return genlmsg_reply(msg, info);
470
471 out_free:
472         nlmsg_free(msg);
473         return ret;
474 }
475
476 struct ila_dump_iter {
477         struct rhashtable_iter rhiter;
478 };
479
480 static int ila_nl_dump_start(struct netlink_callback *cb)
481 {
482         struct net *net = sock_net(cb->skb->sk);
483         struct ila_net *ilan = net_generic(net, ila_net_id);
484         struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
485
486         return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
487                                     GFP_KERNEL);
488 }
489
490 static int ila_nl_dump_done(struct netlink_callback *cb)
491 {
492         struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
493
494         rhashtable_walk_exit(&iter->rhiter);
495
496         return 0;
497 }
498
499 static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
500 {
501         struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
502         struct rhashtable_iter *rhiter = &iter->rhiter;
503         struct ila_map *ila;
504         int ret;
505
506         ret = rhashtable_walk_start(rhiter);
507         if (ret && ret != -EAGAIN)
508                 goto done;
509
510         for (;;) {
511                 ila = rhashtable_walk_next(rhiter);
512
513                 if (IS_ERR(ila)) {
514                         if (PTR_ERR(ila) == -EAGAIN)
515                                 continue;
516                         ret = PTR_ERR(ila);
517                         goto done;
518                 } else if (!ila) {
519                         break;
520                 }
521
522                 while (ila) {
523                         ret =  ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
524                                              cb->nlh->nlmsg_seq, NLM_F_MULTI,
525                                              skb, ILA_CMD_GET);
526                         if (ret)
527                                 goto done;
528
529                         ila = rcu_access_pointer(ila->next);
530                 }
531         }
532
533         ret = skb->len;
534
535 done:
536         rhashtable_walk_stop(rhiter);
537         return ret;
538 }
539
540 static const struct genl_ops ila_nl_ops[] = {
541         {
542                 .cmd = ILA_CMD_ADD,
543                 .doit = ila_nl_cmd_add_mapping,
544                 .policy = ila_nl_policy,
545                 .flags = GENL_ADMIN_PERM,
546         },
547         {
548                 .cmd = ILA_CMD_DEL,
549                 .doit = ila_nl_cmd_del_mapping,
550                 .policy = ila_nl_policy,
551                 .flags = GENL_ADMIN_PERM,
552         },
553         {
554                 .cmd = ILA_CMD_GET,
555                 .doit = ila_nl_cmd_get_mapping,
556                 .start = ila_nl_dump_start,
557                 .dumpit = ila_nl_dump,
558                 .done = ila_nl_dump_done,
559                 .policy = ila_nl_policy,
560         },
561 };
562
563 #define ILA_HASH_TABLE_SIZE 1024
564
565 static __net_init int ila_init_net(struct net *net)
566 {
567         int err;
568         struct ila_net *ilan = net_generic(net, ila_net_id);
569
570         err = alloc_ila_locks(ilan);
571         if (err)
572                 return err;
573
574         rhashtable_init(&ilan->rhash_table, &rht_params);
575
576         return 0;
577 }
578
579 static __net_exit void ila_exit_net(struct net *net)
580 {
581         struct ila_net *ilan = net_generic(net, ila_net_id);
582
583         rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);
584
585         kvfree(ilan->locks);
586
587         if (ilan->hooks_registered)
588                 nf_unregister_net_hooks(net, ila_nf_hook_ops,
589                                         ARRAY_SIZE(ila_nf_hook_ops));
590 }
591
592 static struct pernet_operations ila_net_ops = {
593         .init = ila_init_net,
594         .exit = ila_exit_net,
595         .id   = &ila_net_id,
596         .size = sizeof(struct ila_net),
597 };
598
599 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral)
600 {
601         struct ila_map *ila;
602         struct ipv6hdr *ip6h = ipv6_hdr(skb);
603         struct net *net = dev_net(skb->dev);
604         struct ila_net *ilan = net_generic(net, ila_net_id);
605         struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
606
607         /* Assumes skb contains a valid IPv6 header that is pulled */
608
609         if (!ila_addr_is_ila(iaddr)) {
610                 /* Type indicates this is not an ILA address */
611                 return 0;
612         }
613
614         rcu_read_lock();
615
616         ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
617         if (ila)
618                 ila_update_ipv6_locator(skb, &ila->xp.ip, set_csum_neutral);
619
620         rcu_read_unlock();
621
622         return 0;
623 }
624
625 int ila_xlat_init(void)
626 {
627         int ret;
628
629         ret = register_pernet_device(&ila_net_ops);
630         if (ret)
631                 goto exit;
632
633         ret = genl_register_family_with_ops(&ila_nl_family,
634                                             ila_nl_ops);
635         if (ret < 0)
636                 goto unregister;
637
638         return 0;
639
640 unregister:
641         unregister_pernet_device(&ila_net_ops);
642 exit:
643         return ret;
644 }
645
646 void ila_xlat_fini(void)
647 {
648         genl_unregister_family(&ila_nl_family);
649         unregister_pernet_device(&ila_net_ops);
650 }