1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <crypto/aead.h>
38#include <crypto/aes.h>
39#include "crypto.h"
40
41#define TIPC_TX_PROBE_LIM msecs_to_jiffies(1000)
42#define TIPC_TX_LASTING_LIM msecs_to_jiffies(120000)
43#define TIPC_RX_ACTIVE_LIM msecs_to_jiffies(3000)
44#define TIPC_RX_PASSIVE_LIM msecs_to_jiffies(180000)
45#define TIPC_MAX_TFMS_DEF 10
46#define TIPC_MAX_TFMS_LIM 1000
47
48
49
50
51enum {
52 KEY_UNUSED = 0,
53 KEY_MIN,
54 KEY_1 = KEY_MIN,
55 KEY_2,
56 KEY_3,
57 KEY_MAX = KEY_3,
58};
59
60
61
62
63enum {
64 STAT_OK,
65 STAT_NOK,
66 STAT_ASYNC,
67 STAT_ASYNC_OK,
68 STAT_ASYNC_NOK,
69 STAT_BADKEYS,
70 STAT_BADMSGS = STAT_BADKEYS,
71 STAT_NOKEYS,
72 STAT_SWITCHES,
73
74 MAX_STATS,
75};
76
77
78static const char *hstats[MAX_STATS] = {"ok", "nok", "async", "async_ok",
79 "async_nok", "badmsgs", "nokeys",
80 "switches"};
81
82
83int sysctl_tipc_max_tfms __read_mostly = TIPC_MAX_TFMS_DEF;
84
85
86
87
88
89
90
91
92
93struct tipc_key {
94#define KEY_BITS (2)
95#define KEY_MASK ((1 << KEY_BITS) - 1)
96 union {
97 struct {
98#if defined(__LITTLE_ENDIAN_BITFIELD)
99 u8 pending:2,
100 active:2,
101 passive:2,
102 reserved:2;
103#elif defined(__BIG_ENDIAN_BITFIELD)
104 u8 reserved:2,
105 passive:2,
106 active:2,
107 pending:2;
108#else
109#error "Please fix <asm/byteorder.h>"
110#endif
111 } __packed;
112 u8 keys;
113 };
114};
115
116
117
118
119struct tipc_tfm {
120 struct crypto_aead *tfm;
121 struct list_head list;
122};
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138struct tipc_aead {
139#define TIPC_AEAD_HINT_LEN (5)
140 struct tipc_tfm * __percpu *tfm_entry;
141 struct tipc_crypto *crypto;
142 struct tipc_aead *cloned;
143 atomic_t users;
144 u32 salt;
145 u8 authsize;
146 u8 mode;
147 char hint[TIPC_AEAD_HINT_LEN + 1];
148 struct rcu_head rcu;
149
150 atomic64_t seqno ____cacheline_aligned;
151 refcount_t refcnt ____cacheline_aligned;
152
153} ____cacheline_aligned;
154
155
156
157
158struct tipc_crypto_stats {
159 unsigned int stat[MAX_STATS];
160};
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176struct tipc_crypto {
177 struct net *net;
178 struct tipc_node *node;
179 struct tipc_aead __rcu *aead[KEY_MAX + 1];
180 atomic_t peer_rx_active;
181 struct tipc_key key;
182 u8 working:1;
183 struct tipc_crypto_stats __percpu *stats;
184
185 atomic64_t sndnxt ____cacheline_aligned;
186 unsigned long timer1;
187 unsigned long timer2;
188 spinlock_t lock;
189
190} ____cacheline_aligned;
191
192
193struct tipc_crypto_tx_ctx {
194 struct tipc_aead *aead;
195 struct tipc_bearer *bearer;
196 struct tipc_media_addr dst;
197};
198
199
200struct tipc_crypto_rx_ctx {
201 struct tipc_aead *aead;
202 struct tipc_bearer *bearer;
203};
204
205static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead);
206static inline void tipc_aead_put(struct tipc_aead *aead);
207static void tipc_aead_free(struct rcu_head *rp);
208static int tipc_aead_users(struct tipc_aead __rcu *aead);
209static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim);
210static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim);
211static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val);
212static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead);
213static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
214 u8 mode);
215static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src);
216static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
217 unsigned int crypto_ctx_size,
218 u8 **iv, struct aead_request **req,
219 struct scatterlist **sg, int nsg);
220static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
221 struct tipc_bearer *b,
222 struct tipc_media_addr *dst,
223 struct tipc_node *__dnode);
224static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err);
225static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
226 struct sk_buff *skb, struct tipc_bearer *b);
227static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err);
228static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr);
229static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
230 u8 tx_key, struct sk_buff *skb,
231 struct tipc_crypto *__rx);
232static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
233 u8 new_passive,
234 u8 new_active,
235 u8 new_pending);
236static int tipc_crypto_key_attach(struct tipc_crypto *c,
237 struct tipc_aead *aead, u8 pos);
238static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending);
239static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
240 struct tipc_crypto *rx,
241 struct sk_buff *skb);
242static void tipc_crypto_key_synch(struct tipc_crypto *rx, u8 new_rx_active,
243 struct tipc_msg *hdr);
244static int tipc_crypto_key_revoke(struct net *net, u8 tx_key);
245static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
246 struct tipc_bearer *b,
247 struct sk_buff **skb, int err);
248static void tipc_crypto_do_cmd(struct net *net, int cmd);
249static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf);
250#ifdef TIPC_CRYPTO_DEBUG
251static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
252 char *buf);
253#endif
254
255#define key_next(cur) ((cur) % KEY_MAX + 1)
256
257#define tipc_aead_rcu_ptr(rcu_ptr, lock) \
258 rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock))
259
260#define tipc_aead_rcu_replace(rcu_ptr, ptr, lock) \
261do { \
262 typeof(rcu_ptr) __tmp = rcu_dereference_protected((rcu_ptr), \
263 lockdep_is_held(lock)); \
264 rcu_assign_pointer((rcu_ptr), (ptr)); \
265 tipc_aead_put(__tmp); \
266} while (0)
267
268#define tipc_crypto_key_detach(rcu_ptr, lock) \
269 tipc_aead_rcu_replace((rcu_ptr), NULL, lock)
270
271
272
273
274int tipc_aead_key_validate(struct tipc_aead_key *ukey)
275{
276 int keylen;
277
278
279 if (unlikely(!crypto_has_alg(ukey->alg_name, 0, 0))) {
280 pr_info("Not found cipher: \"%s\"!\n", ukey->alg_name);
281 return -ENODEV;
282 }
283
284
285 if (strcmp(ukey->alg_name, "gcm(aes)"))
286 return -ENOTSUPP;
287
288
289 keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
290 if (unlikely(keylen != TIPC_AES_GCM_KEY_SIZE_128 &&
291 keylen != TIPC_AES_GCM_KEY_SIZE_192 &&
292 keylen != TIPC_AES_GCM_KEY_SIZE_256))
293 return -EINVAL;
294
295 return 0;
296}
297
298static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead)
299{
300 struct tipc_aead *tmp;
301
302 rcu_read_lock();
303 tmp = rcu_dereference(aead);
304 if (unlikely(!tmp || !refcount_inc_not_zero(&tmp->refcnt)))
305 tmp = NULL;
306 rcu_read_unlock();
307
308 return tmp;
309}
310
311static inline void tipc_aead_put(struct tipc_aead *aead)
312{
313 if (aead && refcount_dec_and_test(&aead->refcnt))
314 call_rcu(&aead->rcu, tipc_aead_free);
315}
316
317
318
319
320
321static void tipc_aead_free(struct rcu_head *rp)
322{
323 struct tipc_aead *aead = container_of(rp, struct tipc_aead, rcu);
324 struct tipc_tfm *tfm_entry, *head, *tmp;
325
326 if (aead->cloned) {
327 tipc_aead_put(aead->cloned);
328 } else {
329 head = *this_cpu_ptr(aead->tfm_entry);
330 list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) {
331 crypto_free_aead(tfm_entry->tfm);
332 list_del(&tfm_entry->list);
333 kfree(tfm_entry);
334 }
335
336 crypto_free_aead(head->tfm);
337 list_del(&head->list);
338 kfree(head);
339 }
340 free_percpu(aead->tfm_entry);
341 kfree(aead);
342}
343
344static int tipc_aead_users(struct tipc_aead __rcu *aead)
345{
346 struct tipc_aead *tmp;
347 int users = 0;
348
349 rcu_read_lock();
350 tmp = rcu_dereference(aead);
351 if (tmp)
352 users = atomic_read(&tmp->users);
353 rcu_read_unlock();
354
355 return users;
356}
357
358static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim)
359{
360 struct tipc_aead *tmp;
361
362 rcu_read_lock();
363 tmp = rcu_dereference(aead);
364 if (tmp)
365 atomic_add_unless(&tmp->users, 1, lim);
366 rcu_read_unlock();
367}
368
369static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim)
370{
371 struct tipc_aead *tmp;
372
373 rcu_read_lock();
374 tmp = rcu_dereference(aead);
375 if (tmp)
376 atomic_add_unless(&rcu_dereference(aead)->users, -1, lim);
377 rcu_read_unlock();
378}
379
380static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
381{
382 struct tipc_aead *tmp;
383 int cur;
384
385 rcu_read_lock();
386 tmp = rcu_dereference(aead);
387 if (tmp) {
388 do {
389 cur = atomic_read(&tmp->users);
390 if (cur == val)
391 break;
392 } while (atomic_cmpxchg(&tmp->users, cur, val) != cur);
393 }
394 rcu_read_unlock();
395}
396
397
398
399
400static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
401{
402 struct tipc_tfm **tfm_entry = this_cpu_ptr(aead->tfm_entry);
403
404 *tfm_entry = list_next_entry(*tfm_entry, list);
405 return (*tfm_entry)->tfm;
406}
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
422 u8 mode)
423{
424 struct tipc_tfm *tfm_entry, *head;
425 struct crypto_aead *tfm;
426 struct tipc_aead *tmp;
427 int keylen, err, cpu;
428 int tfm_cnt = 0;
429
430 if (unlikely(*aead))
431 return -EEXIST;
432
433
434 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
435 if (unlikely(!tmp))
436 return -ENOMEM;
437
438
439 keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
440
441
442 tmp->tfm_entry = alloc_percpu(struct tipc_tfm *);
443 if (!tmp->tfm_entry) {
444 kzfree(tmp);
445 return -ENOMEM;
446 }
447
448
449 do {
450 tfm = crypto_alloc_aead(ukey->alg_name, 0, 0);
451 if (IS_ERR(tfm)) {
452 err = PTR_ERR(tfm);
453 break;
454 }
455
456 if (unlikely(!tfm_cnt &&
457 crypto_aead_ivsize(tfm) != TIPC_AES_GCM_IV_SIZE)) {
458 crypto_free_aead(tfm);
459 err = -ENOTSUPP;
460 break;
461 }
462
463 err = crypto_aead_setauthsize(tfm, TIPC_AES_GCM_TAG_SIZE);
464 err |= crypto_aead_setkey(tfm, ukey->key, keylen);
465 if (unlikely(err)) {
466 crypto_free_aead(tfm);
467 break;
468 }
469
470 tfm_entry = kmalloc(sizeof(*tfm_entry), GFP_KERNEL);
471 if (unlikely(!tfm_entry)) {
472 crypto_free_aead(tfm);
473 err = -ENOMEM;
474 break;
475 }
476 INIT_LIST_HEAD(&tfm_entry->list);
477 tfm_entry->tfm = tfm;
478
479
480 if (!tfm_cnt) {
481 head = tfm_entry;
482 for_each_possible_cpu(cpu) {
483 *per_cpu_ptr(tmp->tfm_entry, cpu) = head;
484 }
485 } else {
486 list_add_tail(&tfm_entry->list, &head->list);
487 }
488
489 } while (++tfm_cnt < sysctl_tipc_max_tfms);
490
491
492 if (!tfm_cnt) {
493 free_percpu(tmp->tfm_entry);
494 kzfree(tmp);
495 return err;
496 }
497
498
499 memcpy(tmp->hint, ukey->key, TIPC_AEAD_HINT_LEN);
500 tmp->hint[TIPC_AEAD_HINT_LEN] = '\0';
501
502
503 tmp->mode = mode;
504 tmp->cloned = NULL;
505 tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
506 memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
507 atomic_set(&tmp->users, 0);
508 atomic64_set(&tmp->seqno, 0);
509 refcount_set(&tmp->refcnt, 1);
510
511 *aead = tmp;
512 return 0;
513}
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src)
529{
530 struct tipc_aead *aead;
531 int cpu;
532
533 if (!src)
534 return -ENOKEY;
535
536 if (src->mode != CLUSTER_KEY)
537 return -EINVAL;
538
539 if (unlikely(*dst))
540 return -EEXIST;
541
542 aead = kzalloc(sizeof(*aead), GFP_ATOMIC);
543 if (unlikely(!aead))
544 return -ENOMEM;
545
546 aead->tfm_entry = alloc_percpu_gfp(struct tipc_tfm *, GFP_ATOMIC);
547 if (unlikely(!aead->tfm_entry)) {
548 kzfree(aead);
549 return -ENOMEM;
550 }
551
552 for_each_possible_cpu(cpu) {
553 *per_cpu_ptr(aead->tfm_entry, cpu) =
554 *per_cpu_ptr(src->tfm_entry, cpu);
555 }
556
557 memcpy(aead->hint, src->hint, sizeof(src->hint));
558 aead->mode = src->mode;
559 aead->salt = src->salt;
560 aead->authsize = src->authsize;
561 atomic_set(&aead->users, 0);
562 atomic64_set(&aead->seqno, 0);
563 refcount_set(&aead->refcnt, 1);
564
565 WARN_ON(!refcount_inc_not_zero(&src->refcnt));
566 aead->cloned = src;
567
568 *dst = aead;
569 return 0;
570}
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
588 unsigned int crypto_ctx_size,
589 u8 **iv, struct aead_request **req,
590 struct scatterlist **sg, int nsg)
591{
592 unsigned int iv_size, req_size;
593 unsigned int len;
594 u8 *mem;
595
596 iv_size = crypto_aead_ivsize(tfm);
597 req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
598
599 len = crypto_ctx_size;
600 len += iv_size;
601 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
602 len = ALIGN(len, crypto_tfm_ctx_alignment());
603 len += req_size;
604 len = ALIGN(len, __alignof__(struct scatterlist));
605 len += nsg * sizeof(**sg);
606
607 mem = kmalloc(len, GFP_ATOMIC);
608 if (!mem)
609 return NULL;
610
611 *iv = (u8 *)PTR_ALIGN(mem + crypto_ctx_size,
612 crypto_aead_alignmask(tfm) + 1);
613 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
614 crypto_tfm_ctx_alignment());
615 *sg = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
616 __alignof__(struct scatterlist));
617
618 return (void *)mem;
619}
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
635 struct tipc_bearer *b,
636 struct tipc_media_addr *dst,
637 struct tipc_node *__dnode)
638{
639 struct crypto_aead *tfm = tipc_aead_tfm_next(aead);
640 struct tipc_crypto_tx_ctx *tx_ctx;
641 struct aead_request *req;
642 struct sk_buff *trailer;
643 struct scatterlist *sg;
644 struct tipc_ehdr *ehdr;
645 int ehsz, len, tailen, nsg, rc;
646 void *ctx;
647 u32 salt;
648 u8 *iv;
649
650
651 len = ALIGN(skb->len, 4);
652 tailen = len - skb->len + aead->authsize;
653
654
655
656
657
658
659
660#ifdef TIPC_CRYPTO_DEBUG
661 SKB_LINEAR_ASSERT(skb);
662 if (tailen > skb_tailroom(skb)) {
663 pr_warn("TX: skb tailroom is not enough: %d, requires: %d\n",
664 skb_tailroom(skb), tailen);
665 }
666#endif
667
668 if (unlikely(!skb_cloned(skb) && tailen <= skb_tailroom(skb))) {
669 nsg = 1;
670 trailer = skb;
671 } else {
672
673
674
675
676
677
678 nsg = skb_cow_data(skb, tailen, &trailer);
679 if (unlikely(nsg < 0)) {
680 pr_err("TX: skb_cow_data() returned %d\n", nsg);
681 return nsg;
682 }
683 }
684
685 pskb_put(skb, trailer, tailen);
686
687
688 ctx = tipc_aead_mem_alloc(tfm, sizeof(*tx_ctx), &iv, &req, &sg, nsg);
689 if (unlikely(!ctx))
690 return -ENOMEM;
691 TIPC_SKB_CB(skb)->crypto_ctx = ctx;
692
693
694 sg_init_table(sg, nsg);
695 rc = skb_to_sgvec(skb, sg, 0, skb->len);
696 if (unlikely(rc < 0)) {
697 pr_err("TX: skb_to_sgvec() returned %d, nsg %d!\n", rc, nsg);
698 goto exit;
699 }
700
701
702
703
704
705
706 ehdr = (struct tipc_ehdr *)skb->data;
707 salt = aead->salt;
708 if (aead->mode == CLUSTER_KEY)
709 salt ^= ehdr->addr;
710 else if (__dnode)
711 salt ^= tipc_node_get_addr(__dnode);
712 memcpy(iv, &salt, 4);
713 memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
714
715
716 ehsz = tipc_ehdr_size(ehdr);
717 aead_request_set_tfm(req, tfm);
718 aead_request_set_ad(req, ehsz);
719 aead_request_set_crypt(req, sg, sg, len - ehsz, iv);
720
721
722 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
723 tipc_aead_encrypt_done, skb);
724 tx_ctx = (struct tipc_crypto_tx_ctx *)ctx;
725 tx_ctx->aead = aead;
726 tx_ctx->bearer = b;
727 memcpy(&tx_ctx->dst, dst, sizeof(*dst));
728
729
730 if (unlikely(!tipc_bearer_hold(b))) {
731 rc = -ENODEV;
732 goto exit;
733 }
734
735
736 rc = crypto_aead_encrypt(req);
737 if (rc == -EINPROGRESS || rc == -EBUSY)
738 return rc;
739
740 tipc_bearer_put(b);
741
742exit:
743 kfree(ctx);
744 TIPC_SKB_CB(skb)->crypto_ctx = NULL;
745 return rc;
746}
747
748static void tipc_aead_encrypt_done(struct crypto_async_request *base, int err)
749{
750 struct sk_buff *skb = base->data;
751 struct tipc_crypto_tx_ctx *tx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
752 struct tipc_bearer *b = tx_ctx->bearer;
753 struct tipc_aead *aead = tx_ctx->aead;
754 struct tipc_crypto *tx = aead->crypto;
755 struct net *net = tx->net;
756
757 switch (err) {
758 case 0:
759 this_cpu_inc(tx->stats->stat[STAT_ASYNC_OK]);
760 if (likely(test_bit(0, &b->up)))
761 b->media->send_msg(net, skb, b, &tx_ctx->dst);
762 else
763 kfree_skb(skb);
764 break;
765 case -EINPROGRESS:
766 return;
767 default:
768 this_cpu_inc(tx->stats->stat[STAT_ASYNC_NOK]);
769 kfree_skb(skb);
770 break;
771 }
772
773 kfree(tx_ctx);
774 tipc_bearer_put(b);
775 tipc_aead_put(aead);
776}
777
778
779
780
781
782
783
784
785
786
787
788
789
790static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
791 struct sk_buff *skb, struct tipc_bearer *b)
792{
793 struct tipc_crypto_rx_ctx *rx_ctx;
794 struct aead_request *req;
795 struct crypto_aead *tfm;
796 struct sk_buff *unused;
797 struct scatterlist *sg;
798 struct tipc_ehdr *ehdr;
799 int ehsz, nsg, rc;
800 void *ctx;
801 u32 salt;
802 u8 *iv;
803
804 if (unlikely(!aead))
805 return -ENOKEY;
806
807
808 if (likely(!skb_cloned(skb) &&
809 (!skb_is_nonlinear(skb) || !skb_has_frag_list(skb)))) {
810 nsg = 1 + skb_shinfo(skb)->nr_frags;
811 } else {
812 nsg = skb_cow_data(skb, 0, &unused);
813 if (unlikely(nsg < 0)) {
814 pr_err("RX: skb_cow_data() returned %d\n", nsg);
815 return nsg;
816 }
817 }
818
819
820 tfm = tipc_aead_tfm_next(aead);
821 ctx = tipc_aead_mem_alloc(tfm, sizeof(*rx_ctx), &iv, &req, &sg, nsg);
822 if (unlikely(!ctx))
823 return -ENOMEM;
824 TIPC_SKB_CB(skb)->crypto_ctx = ctx;
825
826
827 sg_init_table(sg, nsg);
828 rc = skb_to_sgvec(skb, sg, 0, skb->len);
829 if (unlikely(rc < 0)) {
830 pr_err("RX: skb_to_sgvec() returned %d, nsg %d\n", rc, nsg);
831 goto exit;
832 }
833
834
835 ehdr = (struct tipc_ehdr *)skb->data;
836 salt = aead->salt;
837 if (aead->mode == CLUSTER_KEY)
838 salt ^= ehdr->addr;
839 else if (ehdr->destined)
840 salt ^= tipc_own_addr(net);
841 memcpy(iv, &salt, 4);
842 memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
843
844
845 ehsz = tipc_ehdr_size(ehdr);
846 aead_request_set_tfm(req, tfm);
847 aead_request_set_ad(req, ehsz);
848 aead_request_set_crypt(req, sg, sg, skb->len - ehsz, iv);
849
850
851 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
852 tipc_aead_decrypt_done, skb);
853 rx_ctx = (struct tipc_crypto_rx_ctx *)ctx;
854 rx_ctx->aead = aead;
855 rx_ctx->bearer = b;
856
857
858 if (unlikely(!tipc_bearer_hold(b))) {
859 rc = -ENODEV;
860 goto exit;
861 }
862
863
864 rc = crypto_aead_decrypt(req);
865 if (rc == -EINPROGRESS || rc == -EBUSY)
866 return rc;
867
868 tipc_bearer_put(b);
869
870exit:
871 kfree(ctx);
872 TIPC_SKB_CB(skb)->crypto_ctx = NULL;
873 return rc;
874}
875
876static void tipc_aead_decrypt_done(struct crypto_async_request *base, int err)
877{
878 struct sk_buff *skb = base->data;
879 struct tipc_crypto_rx_ctx *rx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
880 struct tipc_bearer *b = rx_ctx->bearer;
881 struct tipc_aead *aead = rx_ctx->aead;
882 struct tipc_crypto_stats __percpu *stats = aead->crypto->stats;
883 struct net *net = aead->crypto->net;
884
885 switch (err) {
886 case 0:
887 this_cpu_inc(stats->stat[STAT_ASYNC_OK]);
888 break;
889 case -EINPROGRESS:
890 return;
891 default:
892 this_cpu_inc(stats->stat[STAT_ASYNC_NOK]);
893 break;
894 }
895
896 kfree(rx_ctx);
897 tipc_crypto_rcv_complete(net, aead, b, &skb, err);
898 if (likely(skb)) {
899 if (likely(test_bit(0, &b->up)))
900 tipc_rcv(net, skb, b);
901 else
902 kfree_skb(skb);
903 }
904
905 tipc_bearer_put(b);
906}
907
908static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr)
909{
910 return (ehdr->user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
911}
912
913
914
915
916
917
918
919bool tipc_ehdr_validate(struct sk_buff *skb)
920{
921 struct tipc_ehdr *ehdr;
922 int ehsz;
923
924 if (unlikely(!pskb_may_pull(skb, EHDR_MIN_SIZE)))
925 return false;
926
927 ehdr = (struct tipc_ehdr *)skb->data;
928 if (unlikely(ehdr->version != TIPC_EVERSION))
929 return false;
930 ehsz = tipc_ehdr_size(ehdr);
931 if (unlikely(!pskb_may_pull(skb, ehsz)))
932 return false;
933 if (unlikely(skb->len <= ehsz + TIPC_AES_GCM_TAG_SIZE))
934 return false;
935 if (unlikely(!ehdr->tx_key))
936 return false;
937
938 return true;
939}
940
941
942
943
944
945
946
947
948
949
950
951static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
952 u8 tx_key, struct sk_buff *skb,
953 struct tipc_crypto *__rx)
954{
955 struct tipc_msg *hdr = buf_msg(skb);
956 struct tipc_ehdr *ehdr;
957 u32 user = msg_user(hdr);
958 u64 seqno;
959 int ehsz;
960
961
962 ehsz = (user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
963 WARN_ON(skb_headroom(skb) < ehsz);
964 ehdr = (struct tipc_ehdr *)skb_push(skb, ehsz);
965
966
967
968
969
970 if (!__rx || aead->mode == CLUSTER_KEY)
971 seqno = atomic64_inc_return(&aead->seqno);
972 else
973 seqno = atomic64_inc_return(&__rx->sndnxt);
974
975
976 if (unlikely(!seqno))
977 return tipc_crypto_key_revoke(net, tx_key);
978
979
980 ehdr->seqno = cpu_to_be64(seqno);
981
982
983 ehdr->version = TIPC_EVERSION;
984 ehdr->user = 0;
985 ehdr->keepalive = 0;
986 ehdr->tx_key = tx_key;
987 ehdr->destined = (__rx) ? 1 : 0;
988 ehdr->rx_key_active = (__rx) ? __rx->key.active : 0;
989 ehdr->reserved_1 = 0;
990 ehdr->reserved_2 = 0;
991
992 switch (user) {
993 case LINK_CONFIG:
994 ehdr->user = LINK_CONFIG;
995 memcpy(ehdr->id, tipc_own_id(net), NODE_ID_LEN);
996 break;
997 default:
998 if (user == LINK_PROTOCOL && msg_type(hdr) == STATE_MSG) {
999 ehdr->user = LINK_PROTOCOL;
1000 ehdr->keepalive = msg_is_keepalive(hdr);
1001 }
1002 ehdr->addr = hdr->hdr[3];
1003 break;
1004 }
1005
1006 return ehsz;
1007}
1008
1009static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
1010 u8 new_passive,
1011 u8 new_active,
1012 u8 new_pending)
1013{
1014#ifdef TIPC_CRYPTO_DEBUG
1015 struct tipc_key old = c->key;
1016 char buf[32];
1017#endif
1018
1019 c->key.keys = ((new_passive & KEY_MASK) << (KEY_BITS * 2)) |
1020 ((new_active & KEY_MASK) << (KEY_BITS)) |
1021 ((new_pending & KEY_MASK));
1022
1023#ifdef TIPC_CRYPTO_DEBUG
1024 pr_info("%s(%s): key changing %s ::%pS\n",
1025 (c->node) ? "RX" : "TX",
1026 (c->node) ? tipc_node_get_id_str(c->node) :
1027 tipc_own_id_string(c->net),
1028 tipc_key_change_dump(old, c->key, buf),
1029 __builtin_return_address(0));
1030#endif
1031}
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044int tipc_crypto_key_init(struct tipc_crypto *c, struct tipc_aead_key *ukey,
1045 u8 mode)
1046{
1047 struct tipc_aead *aead = NULL;
1048 int rc = 0;
1049
1050
1051 rc = tipc_aead_init(&aead, ukey, mode);
1052
1053
1054 if (likely(!rc)) {
1055 rc = tipc_crypto_key_attach(c, aead, 0);
1056 if (rc < 0)
1057 tipc_aead_free(&aead->rcu);
1058 }
1059
1060 pr_info("%s(%s): key initiating, rc %d!\n",
1061 (c->node) ? "RX" : "TX",
1062 (c->node) ? tipc_node_get_id_str(c->node) :
1063 tipc_own_id_string(c->net),
1064 rc);
1065
1066 return rc;
1067}
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077static int tipc_crypto_key_attach(struct tipc_crypto *c,
1078 struct tipc_aead *aead, u8 pos)
1079{
1080 u8 new_pending, new_passive, new_key;
1081 struct tipc_key key;
1082 int rc = -EBUSY;
1083
1084 spin_lock_bh(&c->lock);
1085 key = c->key;
1086 if (key.active && key.passive)
1087 goto exit;
1088 if (key.passive && !tipc_aead_users(c->aead[key.passive]))
1089 goto exit;
1090 if (key.pending) {
1091 if (pos)
1092 goto exit;
1093 if (tipc_aead_users(c->aead[key.pending]) > 0)
1094 goto exit;
1095
1096 new_pending = key.pending;
1097 new_passive = key.passive;
1098 new_key = new_pending;
1099 } else {
1100 if (pos) {
1101 if (key.active && pos != key_next(key.active)) {
1102 new_pending = key.pending;
1103 new_passive = pos;
1104 new_key = new_passive;
1105 goto attach;
1106 } else if (!key.active && !key.passive) {
1107 new_pending = pos;
1108 new_passive = key.passive;
1109 new_key = new_pending;
1110 goto attach;
1111 }
1112 }
1113 new_pending = key_next(key.active ?: key.passive);
1114 new_passive = key.passive;
1115 new_key = new_pending;
1116 }
1117
1118attach:
1119 aead->crypto = c;
1120 tipc_crypto_key_set_state(c, new_passive, key.active, new_pending);
1121 tipc_aead_rcu_replace(c->aead[new_key], aead, &c->lock);
1122
1123 c->working = 1;
1124 c->timer1 = jiffies;
1125 c->timer2 = jiffies;
1126 rc = new_key;
1127
1128exit:
1129 spin_unlock_bh(&c->lock);
1130 return rc;
1131}
1132
1133void tipc_crypto_key_flush(struct tipc_crypto *c)
1134{
1135 int k;
1136
1137 spin_lock_bh(&c->lock);
1138 c->working = 0;
1139 tipc_crypto_key_set_state(c, 0, 0, 0);
1140 for (k = KEY_MIN; k <= KEY_MAX; k++)
1141 tipc_crypto_key_detach(c->aead[k], &c->lock);
1142 atomic_set(&c->peer_rx_active, 0);
1143 atomic64_set(&c->sndnxt, 0);
1144 spin_unlock_bh(&c->lock);
1145}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending)
1161{
1162 struct tipc_aead *tmp1, *tmp2 = NULL;
1163 struct tipc_key key;
1164 bool aligned = false;
1165 u8 new_passive = 0;
1166 int x;
1167
1168 spin_lock(&rx->lock);
1169 key = rx->key;
1170 if (key.pending == new_pending) {
1171 aligned = true;
1172 goto exit;
1173 }
1174 if (key.active)
1175 goto exit;
1176 if (!key.pending)
1177 goto exit;
1178 if (tipc_aead_users(rx->aead[key.pending]) > 0)
1179 goto exit;
1180
1181
1182 tmp1 = tipc_aead_rcu_ptr(rx->aead[key.pending], &rx->lock);
1183 if (!refcount_dec_if_one(&tmp1->refcnt))
1184 goto exit;
1185 rcu_assign_pointer(rx->aead[key.pending], NULL);
1186
1187
1188 if (key.passive) {
1189 tmp2 = rcu_replace_pointer(rx->aead[key.passive], tmp2, lockdep_is_held(&rx->lock));
1190 x = (key.passive - key.pending + new_pending) % KEY_MAX;
1191 new_passive = (x <= 0) ? x + KEY_MAX : x;
1192 }
1193
1194
1195 tipc_crypto_key_set_state(rx, new_passive, 0, new_pending);
1196 rcu_assign_pointer(rx->aead[new_pending], tmp1);
1197 if (new_passive)
1198 rcu_assign_pointer(rx->aead[new_passive], tmp2);
1199 refcount_set(&tmp1->refcnt, 1);
1200 aligned = true;
1201 pr_info("RX(%s): key is aligned!\n", tipc_node_get_id_str(rx->node));
1202
1203exit:
1204 spin_unlock(&rx->lock);
1205 return aligned;
1206}
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
1221 struct tipc_crypto *rx,
1222 struct sk_buff *skb)
1223{
1224 struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(skb);
1225 struct tipc_aead *aead = NULL;
1226 struct tipc_key key = tx->key;
1227 u8 k, i = 0;
1228
1229
1230 if (!skb_cb->tx_clone_deferred) {
1231 skb_cb->tx_clone_deferred = 1;
1232 memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1233 }
1234
1235 skb_cb->tx_clone_ctx.rx = rx;
1236 if (++skb_cb->tx_clone_ctx.recurs > 2)
1237 return NULL;
1238
1239
1240 spin_lock(&tx->lock);
1241 do {
1242 k = (i == 0) ? key.pending :
1243 ((i == 1) ? key.active : key.passive);
1244 if (!k)
1245 continue;
1246 aead = tipc_aead_rcu_ptr(tx->aead[k], &tx->lock);
1247 if (!aead)
1248 continue;
1249 if (aead->mode != CLUSTER_KEY ||
1250 aead == skb_cb->tx_clone_ctx.last) {
1251 aead = NULL;
1252 continue;
1253 }
1254
1255 skb_cb->tx_clone_ctx.last = aead;
1256 WARN_ON(skb->next);
1257 skb->next = skb_clone(skb, GFP_ATOMIC);
1258 if (unlikely(!skb->next))
1259 pr_warn("Failed to clone skb for next round if any\n");
1260 WARN_ON(!refcount_inc_not_zero(&aead->refcnt));
1261 break;
1262 } while (++i < 3);
1263 spin_unlock(&tx->lock);
1264
1265 return aead;
1266}
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280static void tipc_crypto_key_synch(struct tipc_crypto *rx, u8 new_rx_active,
1281 struct tipc_msg *hdr)
1282{
1283 struct net *net = rx->net;
1284 struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1285 u8 cur_rx_active;
1286
1287
1288 if (unlikely(!tx->key.active && !tx->key.pending))
1289 return;
1290
1291 cur_rx_active = atomic_read(&rx->peer_rx_active);
1292 if (likely(cur_rx_active == new_rx_active))
1293 return;
1294
1295
1296 if (unlikely(msg_short(hdr) ||
1297 msg_destnode(hdr) != tipc_own_addr(net)))
1298 return;
1299
1300
1301 if (atomic_cmpxchg(&rx->peer_rx_active,
1302 cur_rx_active,
1303 new_rx_active) == cur_rx_active) {
1304 if (new_rx_active)
1305 tipc_aead_users_inc(tx->aead[new_rx_active], INT_MAX);
1306 if (cur_rx_active)
1307 tipc_aead_users_dec(tx->aead[cur_rx_active], 0);
1308
1309 atomic64_set(&rx->sndnxt, 0);
1310
1311 tx->timer1 = jiffies;
1312
1313#ifdef TIPC_CRYPTO_DEBUG
1314 pr_info("TX(%s): key users changed %d-- %d++, peer RX(%s)\n",
1315 tipc_own_id_string(net), cur_rx_active,
1316 new_rx_active, tipc_node_get_id_str(rx->node));
1317#endif
1318 }
1319}
1320
1321static int tipc_crypto_key_revoke(struct net *net, u8 tx_key)
1322{
1323 struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1324 struct tipc_key key;
1325
1326 spin_lock(&tx->lock);
1327 key = tx->key;
1328 WARN_ON(!key.active || tx_key != key.active);
1329
1330
1331 tipc_crypto_key_set_state(tx, key.passive, 0, key.pending);
1332 tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
1333 spin_unlock(&tx->lock);
1334
1335 pr_warn("TX(%s): key is revoked!\n", tipc_own_id_string(net));
1336 return -EKEYREVOKED;
1337}
1338
1339int tipc_crypto_start(struct tipc_crypto **crypto, struct net *net,
1340 struct tipc_node *node)
1341{
1342 struct tipc_crypto *c;
1343
1344 if (*crypto)
1345 return -EEXIST;
1346
1347
1348 c = kzalloc(sizeof(*c), GFP_ATOMIC);
1349 if (!c)
1350 return -ENOMEM;
1351
1352
1353 c->stats = alloc_percpu_gfp(struct tipc_crypto_stats, GFP_ATOMIC);
1354 if (!c->stats) {
1355 kzfree(c);
1356 return -ENOMEM;
1357 }
1358
1359 c->working = 0;
1360 c->net = net;
1361 c->node = node;
1362 tipc_crypto_key_set_state(c, 0, 0, 0);
1363 atomic_set(&c->peer_rx_active, 0);
1364 atomic64_set(&c->sndnxt, 0);
1365 c->timer1 = jiffies;
1366 c->timer2 = jiffies;
1367 spin_lock_init(&c->lock);
1368 *crypto = c;
1369
1370 return 0;
1371}
1372
1373void tipc_crypto_stop(struct tipc_crypto **crypto)
1374{
1375 struct tipc_crypto *c, *tx, *rx;
1376 bool is_rx;
1377 u8 k;
1378
1379 if (!*crypto)
1380 return;
1381
1382 rcu_read_lock();
1383
1384 is_rx = !!((*crypto)->node);
1385 if (is_rx) {
1386 rx = *crypto;
1387 tx = tipc_net(rx->net)->crypto_tx;
1388 k = atomic_read(&rx->peer_rx_active);
1389 if (k) {
1390 tipc_aead_users_dec(tx->aead[k], 0);
1391
1392 tx->timer1 = jiffies;
1393 }
1394 }
1395
1396
1397 c = *crypto;
1398 for (k = KEY_MIN; k <= KEY_MAX; k++)
1399 tipc_aead_put(rcu_dereference(c->aead[k]));
1400 rcu_read_unlock();
1401
1402 pr_warn("%s(%s) has been purged, node left!\n",
1403 (is_rx) ? "RX" : "TX",
1404 (is_rx) ? tipc_node_get_id_str((*crypto)->node) :
1405 tipc_own_id_string((*crypto)->net));
1406
1407
1408 free_percpu(c->stats);
1409
1410 *crypto = NULL;
1411 kzfree(c);
1412}
1413
1414void tipc_crypto_timeout(struct tipc_crypto *rx)
1415{
1416 struct tipc_net *tn = tipc_net(rx->net);
1417 struct tipc_crypto *tx = tn->crypto_tx;
1418 struct tipc_key key;
1419 u8 new_pending, new_passive;
1420 int cmd;
1421
1422
1423
1424
1425
1426 spin_lock(&tx->lock);
1427 key = tx->key;
1428 if (key.active && tipc_aead_users(tx->aead[key.active]) > 0)
1429 goto s1;
1430 if (!key.pending || tipc_aead_users(tx->aead[key.pending]) <= 0)
1431 goto s1;
1432 if (time_before(jiffies, tx->timer1 + TIPC_TX_LASTING_LIM))
1433 goto s1;
1434
1435 tipc_crypto_key_set_state(tx, key.passive, key.pending, 0);
1436 if (key.active)
1437 tipc_crypto_key_detach(tx->aead[key.active], &tx->lock);
1438 this_cpu_inc(tx->stats->stat[STAT_SWITCHES]);
1439 pr_info("TX(%s): key %d is activated!\n", tipc_own_id_string(tx->net),
1440 key.pending);
1441
1442s1:
1443 spin_unlock(&tx->lock);
1444
1445
1446
1447
1448
1449 spin_lock(&rx->lock);
1450 key = rx->key;
1451 if (!key.pending || tipc_aead_users(rx->aead[key.pending]) <= 0)
1452 goto s2;
1453
1454 new_pending = (key.passive &&
1455 !tipc_aead_users(rx->aead[key.passive])) ?
1456 key.passive : 0;
1457 new_passive = (key.active) ?: ((new_pending) ? 0 : key.passive);
1458 tipc_crypto_key_set_state(rx, new_passive, key.pending, new_pending);
1459 this_cpu_inc(rx->stats->stat[STAT_SWITCHES]);
1460 pr_info("RX(%s): key %d is activated!\n",
1461 tipc_node_get_id_str(rx->node), key.pending);
1462 goto s5;
1463
1464s2:
1465
1466
1467
1468
1469
1470 key = rx->key;
1471 if (!key.pending || tipc_aead_users(rx->aead[key.pending]) > -30)
1472 goto s3;
1473 if (!key.passive || tipc_aead_users(rx->aead[key.passive]) != 0)
1474 goto s3;
1475
1476 new_pending = key.passive;
1477 new_passive = key.pending;
1478 tipc_crypto_key_set_state(rx, new_passive, key.active, new_pending);
1479 goto s5;
1480
1481s3:
1482
1483
1484
1485
1486
1487 key = rx->key;
1488 if (!key.active)
1489 goto s4;
1490 if (time_before(jiffies, rx->timer1 + TIPC_RX_ACTIVE_LIM))
1491 goto s4;
1492
1493 new_pending = (key.passive) ?: key.active;
1494 new_passive = (key.passive) ? key.active : key.pending;
1495 tipc_aead_users_set(rx->aead[new_pending], 0);
1496 if (new_passive)
1497 tipc_aead_users_set(rx->aead[new_passive], 0);
1498 tipc_crypto_key_set_state(rx, new_passive, 0, new_pending);
1499 pr_info("RX(%s): key %d is deactivated!\n",
1500 tipc_node_get_id_str(rx->node), key.active);
1501 goto s5;
1502
1503s4:
1504
1505 key = rx->key;
1506 if (!key.passive || !tipc_aead_users(rx->aead[key.passive]))
1507 goto s5;
1508 if (time_before(jiffies, rx->timer2 + TIPC_RX_PASSIVE_LIM))
1509 goto s5;
1510
1511 tipc_crypto_key_set_state(rx, 0, key.active, key.pending);
1512 tipc_crypto_key_detach(rx->aead[key.passive], &rx->lock);
1513 pr_info("RX(%s): key %d is freed!\n", tipc_node_get_id_str(rx->node),
1514 key.passive);
1515
1516s5:
1517 spin_unlock(&rx->lock);
1518
1519
1520 if (likely(sysctl_tipc_max_tfms <= TIPC_MAX_TFMS_LIM))
1521 return;
1522
1523 cmd = sysctl_tipc_max_tfms;
1524 sysctl_tipc_max_tfms = TIPC_MAX_TFMS_DEF;
1525 tipc_crypto_do_cmd(rx->net, cmd);
1526}
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550int tipc_crypto_xmit(struct net *net, struct sk_buff **skb,
1551 struct tipc_bearer *b, struct tipc_media_addr *dst,
1552 struct tipc_node *__dnode)
1553{
1554 struct tipc_crypto *__rx = tipc_node_crypto_rx(__dnode);
1555 struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1556 struct tipc_crypto_stats __percpu *stats = tx->stats;
1557 struct tipc_key key = tx->key;
1558 struct tipc_aead *aead = NULL;
1559 struct sk_buff *probe;
1560 int rc = -ENOKEY;
1561 u8 tx_key;
1562
1563
1564 if (!tx->working)
1565 return 0;
1566
1567
1568
1569
1570
1571
1572 if (unlikely(key.pending)) {
1573 tx_key = key.pending;
1574 if (!key.active)
1575 goto encrypt;
1576 if (__rx && atomic_read(&__rx->peer_rx_active) == tx_key)
1577 goto encrypt;
1578 if (TIPC_SKB_CB(*skb)->probe)
1579 goto encrypt;
1580 if (!__rx &&
1581 time_after(jiffies, tx->timer2 + TIPC_TX_PROBE_LIM)) {
1582 tx->timer2 = jiffies;
1583 probe = skb_clone(*skb, GFP_ATOMIC);
1584 if (probe) {
1585 TIPC_SKB_CB(probe)->probe = 1;
1586 tipc_crypto_xmit(net, &probe, b, dst, __dnode);
1587 if (probe)
1588 b->media->send_msg(net, probe, b, dst);
1589 }
1590 }
1591 }
1592
1593 if (likely(key.active)) {
1594 tx_key = key.active;
1595 goto encrypt;
1596 }
1597 goto exit;
1598
1599encrypt:
1600 aead = tipc_aead_get(tx->aead[tx_key]);
1601 if (unlikely(!aead))
1602 goto exit;
1603 rc = tipc_ehdr_build(net, aead, tx_key, *skb, __rx);
1604 if (likely(rc > 0))
1605 rc = tipc_aead_encrypt(aead, *skb, b, dst, __dnode);
1606
1607exit:
1608 switch (rc) {
1609 case 0:
1610 this_cpu_inc(stats->stat[STAT_OK]);
1611 break;
1612 case -EINPROGRESS:
1613 case -EBUSY:
1614 this_cpu_inc(stats->stat[STAT_ASYNC]);
1615 *skb = NULL;
1616 return rc;
1617 default:
1618 this_cpu_inc(stats->stat[STAT_NOK]);
1619 if (rc == -ENOKEY)
1620 this_cpu_inc(stats->stat[STAT_NOKEYS]);
1621 else if (rc == -EKEYREVOKED)
1622 this_cpu_inc(stats->stat[STAT_BADKEYS]);
1623 kfree_skb(*skb);
1624 *skb = NULL;
1625 break;
1626 }
1627
1628 tipc_aead_put(aead);
1629 return rc;
1630}
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654int tipc_crypto_rcv(struct net *net, struct tipc_crypto *rx,
1655 struct sk_buff **skb, struct tipc_bearer *b)
1656{
1657 struct tipc_crypto *tx = tipc_net(net)->crypto_tx;
1658 struct tipc_crypto_stats __percpu *stats;
1659 struct tipc_aead *aead = NULL;
1660 struct tipc_key key;
1661 int rc = -ENOKEY;
1662 u8 tx_key = 0;
1663
1664
1665
1666
1667 if (unlikely(!rx))
1668 goto pick_tx;
1669
1670
1671
1672
1673
1674
1675 tx_key = ((struct tipc_ehdr *)(*skb)->data)->tx_key;
1676 key = rx->key;
1677 if (likely(tx_key == key.active))
1678 goto decrypt;
1679 if (tx_key == key.pending)
1680 goto decrypt;
1681 if (tx_key == key.passive) {
1682 rx->timer2 = jiffies;
1683 if (tipc_aead_users(rx->aead[key.passive]) > 0)
1684 goto decrypt;
1685 }
1686
1687
1688 if (tipc_crypto_key_try_align(rx, tx_key))
1689 goto decrypt;
1690
1691pick_tx:
1692
1693 aead = tipc_crypto_key_pick_tx(tx, rx, *skb);
1694 if (aead)
1695 goto decrypt;
1696 goto exit;
1697
1698decrypt:
1699 rcu_read_lock();
1700 if (!aead)
1701 aead = tipc_aead_get(rx->aead[tx_key]);
1702 rc = tipc_aead_decrypt(net, aead, *skb, b);
1703 rcu_read_unlock();
1704
1705exit:
1706 stats = ((rx) ?: tx)->stats;
1707 switch (rc) {
1708 case 0:
1709 this_cpu_inc(stats->stat[STAT_OK]);
1710 break;
1711 case -EINPROGRESS:
1712 case -EBUSY:
1713 this_cpu_inc(stats->stat[STAT_ASYNC]);
1714 *skb = NULL;
1715 return rc;
1716 default:
1717 this_cpu_inc(stats->stat[STAT_NOK]);
1718 if (rc == -ENOKEY) {
1719 kfree_skb(*skb);
1720 *skb = NULL;
1721 if (rx)
1722 tipc_node_put(rx->node);
1723 this_cpu_inc(stats->stat[STAT_NOKEYS]);
1724 return rc;
1725 } else if (rc == -EBADMSG) {
1726 this_cpu_inc(stats->stat[STAT_BADMSGS]);
1727 }
1728 break;
1729 }
1730
1731 tipc_crypto_rcv_complete(net, aead, b, skb, rc);
1732 return rc;
1733}
1734
1735static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
1736 struct tipc_bearer *b,
1737 struct sk_buff **skb, int err)
1738{
1739 struct tipc_skb_cb *skb_cb = TIPC_SKB_CB(*skb);
1740 struct tipc_crypto *rx = aead->crypto;
1741 struct tipc_aead *tmp = NULL;
1742 struct tipc_ehdr *ehdr;
1743 struct tipc_node *n;
1744 u8 rx_key_active;
1745 bool destined;
1746
1747
1748 if (unlikely(!rx->node)) {
1749 rx = skb_cb->tx_clone_ctx.rx;
1750#ifdef TIPC_CRYPTO_DEBUG
1751 pr_info("TX->RX(%s): err %d, aead %p, skb->next %p, flags %x\n",
1752 (rx) ? tipc_node_get_id_str(rx->node) : "-", err, aead,
1753 (*skb)->next, skb_cb->flags);
1754 pr_info("skb_cb [recurs %d, last %p], tx->aead [%p %p %p]\n",
1755 skb_cb->tx_clone_ctx.recurs, skb_cb->tx_clone_ctx.last,
1756 aead->crypto->aead[1], aead->crypto->aead[2],
1757 aead->crypto->aead[3]);
1758#endif
1759 if (unlikely(err)) {
1760 if (err == -EBADMSG && (*skb)->next)
1761 tipc_rcv(net, (*skb)->next, b);
1762 goto free_skb;
1763 }
1764
1765 if (likely((*skb)->next)) {
1766 kfree_skb((*skb)->next);
1767 (*skb)->next = NULL;
1768 }
1769 ehdr = (struct tipc_ehdr *)(*skb)->data;
1770 if (!rx) {
1771 WARN_ON(ehdr->user != LINK_CONFIG);
1772 n = tipc_node_create(net, 0, ehdr->id, 0xffffu, 0,
1773 true);
1774 rx = tipc_node_crypto_rx(n);
1775 if (unlikely(!rx))
1776 goto free_skb;
1777 }
1778
1779
1780 if (rx->key.pending)
1781 goto rcv;
1782 if (tipc_aead_clone(&tmp, aead) < 0)
1783 goto rcv;
1784 if (tipc_crypto_key_attach(rx, tmp, ehdr->tx_key) < 0) {
1785 tipc_aead_free(&tmp->rcu);
1786 goto rcv;
1787 }
1788 tipc_aead_put(aead);
1789 aead = tipc_aead_get(tmp);
1790 }
1791
1792 if (unlikely(err)) {
1793 tipc_aead_users_dec(aead, INT_MIN);
1794 goto free_skb;
1795 }
1796
1797
1798 tipc_aead_users_set(aead, 1);
1799
1800rcv:
1801
1802 rx->timer1 = jiffies;
1803
1804
1805 ehdr = (struct tipc_ehdr *)(*skb)->data;
1806 destined = ehdr->destined;
1807 rx_key_active = ehdr->rx_key_active;
1808 skb_pull(*skb, tipc_ehdr_size(ehdr));
1809 pskb_trim(*skb, (*skb)->len - aead->authsize);
1810
1811
1812 if (unlikely(!tipc_msg_validate(skb))) {
1813 pr_err_ratelimited("Packet dropped after decryption!\n");
1814 goto free_skb;
1815 }
1816
1817
1818 if (destined)
1819 tipc_crypto_key_synch(rx, rx_key_active, buf_msg(*skb));
1820
1821
1822 skb_cb->decrypted = 1;
1823
1824
1825 if (likely(!skb_cb->tx_clone_deferred))
1826 goto exit;
1827 skb_cb->tx_clone_deferred = 0;
1828 memset(&skb_cb->tx_clone_ctx, 0, sizeof(skb_cb->tx_clone_ctx));
1829 goto exit;
1830
1831free_skb:
1832 kfree_skb(*skb);
1833 *skb = NULL;
1834
1835exit:
1836 tipc_aead_put(aead);
1837 if (rx)
1838 tipc_node_put(rx->node);
1839}
1840
1841static void tipc_crypto_do_cmd(struct net *net, int cmd)
1842{
1843 struct tipc_net *tn = tipc_net(net);
1844 struct tipc_crypto *tx = tn->crypto_tx, *rx;
1845 struct list_head *p;
1846 unsigned int stat;
1847 int i, j, cpu;
1848 char buf[200];
1849
1850
1851 switch (cmd) {
1852 case 0xfff1:
1853 goto print_stats;
1854 default:
1855 return;
1856 }
1857
1858print_stats:
1859
1860 pr_info("\n=============== TIPC Crypto Statistics ===============\n\n");
1861
1862
1863 pr_info("Key status:\n");
1864 pr_info("TX(%7.7s)\n%s", tipc_own_id_string(net),
1865 tipc_crypto_key_dump(tx, buf));
1866
1867 rcu_read_lock();
1868 for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
1869 rx = tipc_node_crypto_rx_by_list(p);
1870 pr_info("RX(%7.7s)\n%s", tipc_node_get_id_str(rx->node),
1871 tipc_crypto_key_dump(rx, buf));
1872 }
1873 rcu_read_unlock();
1874
1875
1876 for (i = 0, j = 0; i < MAX_STATS; i++)
1877 j += scnprintf(buf + j, 200 - j, "|%11s ", hstats[i]);
1878 pr_info("\nCounter %s", buf);
1879
1880 memset(buf, '-', 115);
1881 buf[115] = '\0';
1882 pr_info("%s\n", buf);
1883
1884 j = scnprintf(buf, 200, "TX(%7.7s) ", tipc_own_id_string(net));
1885 for_each_possible_cpu(cpu) {
1886 for (i = 0; i < MAX_STATS; i++) {
1887 stat = per_cpu_ptr(tx->stats, cpu)->stat[i];
1888 j += scnprintf(buf + j, 200 - j, "|%11d ", stat);
1889 }
1890 pr_info("%s", buf);
1891 j = scnprintf(buf, 200, "%12s", " ");
1892 }
1893
1894 rcu_read_lock();
1895 for (p = tn->node_list.next; p != &tn->node_list; p = p->next) {
1896 rx = tipc_node_crypto_rx_by_list(p);
1897 j = scnprintf(buf, 200, "RX(%7.7s) ",
1898 tipc_node_get_id_str(rx->node));
1899 for_each_possible_cpu(cpu) {
1900 for (i = 0; i < MAX_STATS; i++) {
1901 stat = per_cpu_ptr(rx->stats, cpu)->stat[i];
1902 j += scnprintf(buf + j, 200 - j, "|%11d ",
1903 stat);
1904 }
1905 pr_info("%s", buf);
1906 j = scnprintf(buf, 200, "%12s", " ");
1907 }
1908 }
1909 rcu_read_unlock();
1910
1911 pr_info("\n======================== Done ========================\n");
1912}
1913
1914static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf)
1915{
1916 struct tipc_key key = c->key;
1917 struct tipc_aead *aead;
1918 int k, i = 0;
1919 char *s;
1920
1921 for (k = KEY_MIN; k <= KEY_MAX; k++) {
1922 if (k == key.passive)
1923 s = "PAS";
1924 else if (k == key.active)
1925 s = "ACT";
1926 else if (k == key.pending)
1927 s = "PEN";
1928 else
1929 s = "-";
1930 i += scnprintf(buf + i, 200 - i, "\tKey%d: %s", k, s);
1931
1932 rcu_read_lock();
1933 aead = rcu_dereference(c->aead[k]);
1934 if (aead)
1935 i += scnprintf(buf + i, 200 - i,
1936 "{\"%s...\", \"%s\"}/%d:%d",
1937 aead->hint,
1938 (aead->mode == CLUSTER_KEY) ? "c" : "p",
1939 atomic_read(&aead->users),
1940 refcount_read(&aead->refcnt));
1941 rcu_read_unlock();
1942 i += scnprintf(buf + i, 200 - i, "\n");
1943 }
1944
1945 if (c->node)
1946 i += scnprintf(buf + i, 200 - i, "\tPeer RX active: %d\n",
1947 atomic_read(&c->peer_rx_active));
1948
1949 return buf;
1950}
1951
1952#ifdef TIPC_CRYPTO_DEBUG
1953static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
1954 char *buf)
1955{
1956 struct tipc_key *key = &old;
1957 int k, i = 0;
1958 char *s;
1959
1960
1961again:
1962 i += scnprintf(buf + i, 32 - i, "[");
1963 for (k = KEY_MIN; k <= KEY_MAX; k++) {
1964 if (k == key->passive)
1965 s = "pas";
1966 else if (k == key->active)
1967 s = "act";
1968 else if (k == key->pending)
1969 s = "pen";
1970 else
1971 s = "-";
1972 i += scnprintf(buf + i, 32 - i,
1973 (k != KEY_MAX) ? "%s " : "%s", s);
1974 }
1975 if (key != &new) {
1976 i += scnprintf(buf + i, 32 - i, "] -> ");
1977 key = &new;
1978 goto again;
1979 }
1980 i += scnprintf(buf + i, 32 - i, "]");
1981 return buf;
1982}
1983#endif
1984