net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / net / ceph / auth_x.c
1
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/err.h>
5 #include <linux/module.h>
6 #include <linux/random.h>
7 #include <linux/slab.h>
8
9 #include <linux/ceph/decode.h>
10 #include <linux/ceph/auth.h>
11
12 #include "crypto.h"
13 #include "auth_x.h"
14 #include "auth_x_protocol.h"
15
16 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
17
18 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
19 {
20         struct ceph_x_info *xi = ac->private;
21         int need;
22
23         ceph_x_validate_tickets(ac, &need);
24         dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
25              ac->want_keys, need, xi->have_keys);
26         return (ac->want_keys & xi->have_keys) == ac->want_keys;
27 }
28
29 static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
30 {
31         struct ceph_x_info *xi = ac->private;
32         int need;
33
34         ceph_x_validate_tickets(ac, &need);
35         dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
36              ac->want_keys, need, xi->have_keys);
37         return need != 0;
38 }
39
40 static int ceph_x_encrypt_buflen(int ilen)
41 {
42         return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
43                 sizeof(u32);
44 }
45
46 static int ceph_x_encrypt(struct ceph_crypto_key *secret,
47                           void *ibuf, int ilen, void *obuf, size_t olen)
48 {
49         struct ceph_x_encrypt_header head = {
50                 .struct_v = 1,
51                 .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
52         };
53         size_t len = olen - sizeof(u32);
54         int ret;
55
56         ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
57                             &head, sizeof(head), ibuf, ilen);
58         if (ret)
59                 return ret;
60         ceph_encode_32(&obuf, len);
61         return len + sizeof(u32);
62 }
63
64 static int ceph_x_decrypt(struct ceph_crypto_key *secret,
65                           void **p, void *end, void **obuf, size_t olen)
66 {
67         struct ceph_x_encrypt_header head;
68         size_t head_len = sizeof(head);
69         int len, ret;
70
71         len = ceph_decode_32(p);
72         if (*p + len > end)
73                 return -EINVAL;
74
75         dout("ceph_x_decrypt len %d\n", len);
76         if (*obuf == NULL) {
77                 *obuf = kmalloc(len, GFP_NOFS);
78                 if (!*obuf)
79                         return -ENOMEM;
80                 olen = len;
81         }
82
83         ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
84         if (ret)
85                 return ret;
86         if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
87                 return -EPERM;
88         *p += len;
89         return olen;
90 }
91
92 /*
93  * get existing (or insert new) ticket handler
94  */
95 static struct ceph_x_ticket_handler *
96 get_ticket_handler(struct ceph_auth_client *ac, int service)
97 {
98         struct ceph_x_ticket_handler *th;
99         struct ceph_x_info *xi = ac->private;
100         struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
101
102         while (*p) {
103                 parent = *p;
104                 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
105                 if (service < th->service)
106                         p = &(*p)->rb_left;
107                 else if (service > th->service)
108                         p = &(*p)->rb_right;
109                 else
110                         return th;
111         }
112
113         /* add it */
114         th = kzalloc(sizeof(*th), GFP_NOFS);
115         if (!th)
116                 return ERR_PTR(-ENOMEM);
117         th->service = service;
118         rb_link_node(&th->node, parent, p);
119         rb_insert_color(&th->node, &xi->ticket_handlers);
120         return th;
121 }
122
123 static void remove_ticket_handler(struct ceph_auth_client *ac,
124                                   struct ceph_x_ticket_handler *th)
125 {
126         struct ceph_x_info *xi = ac->private;
127
128         dout("remove_ticket_handler %p %d\n", th, th->service);
129         rb_erase(&th->node, &xi->ticket_handlers);
130         ceph_crypto_key_destroy(&th->session_key);
131         if (th->ticket_blob)
132                 ceph_buffer_put(th->ticket_blob);
133         kfree(th);
134 }
135
136 static int process_one_ticket(struct ceph_auth_client *ac,
137                               struct ceph_crypto_key *secret,
138                               void **p, void *end)
139 {
140         struct ceph_x_info *xi = ac->private;
141         int type;
142         u8 tkt_struct_v, blob_struct_v;
143         struct ceph_x_ticket_handler *th;
144         void *dbuf = NULL;
145         void *dp, *dend;
146         int dlen;
147         char is_enc;
148         struct timespec validity;
149         struct ceph_crypto_key old_key;
150         void *ticket_buf = NULL;
151         void *tp, *tpend;
152         struct ceph_timespec new_validity;
153         struct ceph_crypto_key new_session_key;
154         struct ceph_buffer *new_ticket_blob;
155         unsigned long new_expires, new_renew_after;
156         u64 new_secret_id;
157         int ret;
158
159         ceph_decode_need(p, end, sizeof(u32) + 1, bad);
160
161         type = ceph_decode_32(p);
162         dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
163
164         tkt_struct_v = ceph_decode_8(p);
165         if (tkt_struct_v != 1)
166                 goto bad;
167
168         th = get_ticket_handler(ac, type);
169         if (IS_ERR(th)) {
170                 ret = PTR_ERR(th);
171                 goto out;
172         }
173
174         /* blob for me */
175         dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
176         if (dlen <= 0) {
177                 ret = dlen;
178                 goto out;
179         }
180         dout(" decrypted %d bytes\n", dlen);
181         dp = dbuf;
182         dend = dp + dlen;
183
184         tkt_struct_v = ceph_decode_8(&dp);
185         if (tkt_struct_v != 1)
186                 goto bad;
187
188         memcpy(&old_key, &th->session_key, sizeof(old_key));
189         ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
190         if (ret)
191                 goto out;
192
193         ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
194         ceph_decode_timespec(&validity, &new_validity);
195         new_expires = get_seconds() + validity.tv_sec;
196         new_renew_after = new_expires - (validity.tv_sec / 4);
197         dout(" expires=%lu renew_after=%lu\n", new_expires,
198              new_renew_after);
199
200         /* ticket blob for service */
201         ceph_decode_8_safe(p, end, is_enc, bad);
202         if (is_enc) {
203                 /* encrypted */
204                 dout(" encrypted ticket\n");
205                 dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0);
206                 if (dlen < 0) {
207                         ret = dlen;
208                         goto out;
209                 }
210                 tp = ticket_buf;
211                 dlen = ceph_decode_32(&tp);
212         } else {
213                 /* unencrypted */
214                 ceph_decode_32_safe(p, end, dlen, bad);
215                 ticket_buf = kmalloc(dlen, GFP_NOFS);
216                 if (!ticket_buf) {
217                         ret = -ENOMEM;
218                         goto out;
219                 }
220                 tp = ticket_buf;
221                 ceph_decode_need(p, end, dlen, bad);
222                 ceph_decode_copy(p, ticket_buf, dlen);
223         }
224         tpend = tp + dlen;
225         dout(" ticket blob is %d bytes\n", dlen);
226         ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad);
227         blob_struct_v = ceph_decode_8(&tp);
228         new_secret_id = ceph_decode_64(&tp);
229         ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend);
230         if (ret)
231                 goto out;
232
233         /* all is well, update our ticket */
234         ceph_crypto_key_destroy(&th->session_key);
235         if (th->ticket_blob)
236                 ceph_buffer_put(th->ticket_blob);
237         th->session_key = new_session_key;
238         th->ticket_blob = new_ticket_blob;
239         th->validity = new_validity;
240         th->secret_id = new_secret_id;
241         th->expires = new_expires;
242         th->renew_after = new_renew_after;
243         dout(" got ticket service %d (%s) secret_id %lld len %d\n",
244              type, ceph_entity_type_name(type), th->secret_id,
245              (int)th->ticket_blob->vec.iov_len);
246         xi->have_keys |= th->service;
247
248 out:
249         kfree(ticket_buf);
250         kfree(dbuf);
251         return ret;
252
253 bad:
254         ret = -EINVAL;
255         goto out;
256 }
257
258 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
259                                     struct ceph_crypto_key *secret,
260                                     void *buf, void *end)
261 {
262         void *p = buf;
263         u8 reply_struct_v;
264         u32 num;
265         int ret;
266
267         ceph_decode_8_safe(&p, end, reply_struct_v, bad);
268         if (reply_struct_v != 1)
269                 return -EINVAL;
270
271         ceph_decode_32_safe(&p, end, num, bad);
272         dout("%d tickets\n", num);
273
274         while (num--) {
275                 ret = process_one_ticket(ac, secret, &p, end);
276                 if (ret)
277                         return ret;
278         }
279
280         return 0;
281
282 bad:
283         return -EINVAL;
284 }
285
286 static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
287                                    struct ceph_x_ticket_handler *th,
288                                    struct ceph_x_authorizer *au)
289 {
290         int maxlen;
291         struct ceph_x_authorize_a *msg_a;
292         struct ceph_x_authorize_b msg_b;
293         void *p, *end;
294         int ret;
295         int ticket_blob_len =
296                 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
297
298         dout("build_authorizer for %s %p\n",
299              ceph_entity_type_name(th->service), au);
300
301         maxlen = sizeof(*msg_a) + sizeof(msg_b) +
302                 ceph_x_encrypt_buflen(ticket_blob_len);
303         dout("  need len %d\n", maxlen);
304         if (au->buf && au->buf->alloc_len < maxlen) {
305                 ceph_buffer_put(au->buf);
306                 au->buf = NULL;
307         }
308         if (!au->buf) {
309                 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
310                 if (!au->buf)
311                         return -ENOMEM;
312         }
313         au->service = th->service;
314
315         msg_a = au->buf->vec.iov_base;
316         msg_a->struct_v = 1;
317         msg_a->global_id = cpu_to_le64(ac->global_id);
318         msg_a->service_id = cpu_to_le32(th->service);
319         msg_a->ticket_blob.struct_v = 1;
320         msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
321         msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
322         if (ticket_blob_len) {
323                 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
324                        th->ticket_blob->vec.iov_len);
325         }
326         dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
327              le64_to_cpu(msg_a->ticket_blob.secret_id));
328
329         p = msg_a + 1;
330         p += ticket_blob_len;
331         end = au->buf->vec.iov_base + au->buf->vec.iov_len;
332
333         get_random_bytes(&au->nonce, sizeof(au->nonce));
334         msg_b.struct_v = 1;
335         msg_b.nonce = cpu_to_le64(au->nonce);
336         ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b),
337                              p, end - p);
338         if (ret < 0)
339                 goto out_buf;
340         p += ret;
341         au->buf->vec.iov_len = p - au->buf->vec.iov_base;
342         dout(" built authorizer nonce %llx len %d\n", au->nonce,
343              (int)au->buf->vec.iov_len);
344         BUG_ON(au->buf->vec.iov_len > maxlen);
345         return 0;
346
347 out_buf:
348         ceph_buffer_put(au->buf);
349         au->buf = NULL;
350         return ret;
351 }
352
353 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
354                                 void **p, void *end)
355 {
356         ceph_decode_need(p, end, 1 + sizeof(u64), bad);
357         ceph_encode_8(p, 1);
358         ceph_encode_64(p, th->secret_id);
359         if (th->ticket_blob) {
360                 const char *buf = th->ticket_blob->vec.iov_base;
361                 u32 len = th->ticket_blob->vec.iov_len;
362
363                 ceph_encode_32_safe(p, end, len, bad);
364                 ceph_encode_copy_safe(p, end, buf, len, bad);
365         } else {
366                 ceph_encode_32_safe(p, end, 0, bad);
367         }
368
369         return 0;
370 bad:
371         return -ERANGE;
372 }
373
374 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
375 {
376         int want = ac->want_keys;
377         struct ceph_x_info *xi = ac->private;
378         int service;
379
380         *pneed = ac->want_keys & ~(xi->have_keys);
381
382         for (service = 1; service <= want; service <<= 1) {
383                 struct ceph_x_ticket_handler *th;
384
385                 if (!(ac->want_keys & service))
386                         continue;
387
388                 if (*pneed & service)
389                         continue;
390
391                 th = get_ticket_handler(ac, service);
392
393                 if (IS_ERR(th)) {
394                         *pneed |= service;
395                         continue;
396                 }
397
398                 if (get_seconds() >= th->renew_after)
399                         *pneed |= service;
400                 if (get_seconds() >= th->expires)
401                         xi->have_keys &= ~service;
402         }
403 }
404
405
406 static int ceph_x_build_request(struct ceph_auth_client *ac,
407                                 void *buf, void *end)
408 {
409         struct ceph_x_info *xi = ac->private;
410         int need;
411         struct ceph_x_request_header *head = buf;
412         int ret;
413         struct ceph_x_ticket_handler *th =
414                 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
415
416         if (IS_ERR(th))
417                 return PTR_ERR(th);
418
419         ceph_x_validate_tickets(ac, &need);
420
421         dout("build_request want %x have %x need %x\n",
422              ac->want_keys, xi->have_keys, need);
423
424         if (need & CEPH_ENTITY_TYPE_AUTH) {
425                 struct ceph_x_authenticate *auth = (void *)(head + 1);
426                 void *p = auth + 1;
427                 struct ceph_x_challenge_blob tmp;
428                 char tmp_enc[40];
429                 u64 *u;
430
431                 if (p > end)
432                         return -ERANGE;
433
434                 dout(" get_auth_session_key\n");
435                 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
436
437                 /* encrypt and hash */
438                 get_random_bytes(&auth->client_challenge, sizeof(u64));
439                 tmp.client_challenge = auth->client_challenge;
440                 tmp.server_challenge = cpu_to_le64(xi->server_challenge);
441                 ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
442                                      tmp_enc, sizeof(tmp_enc));
443                 if (ret < 0)
444                         return ret;
445
446                 auth->struct_v = 1;
447                 auth->key = 0;
448                 for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
449                         auth->key ^= *(__le64 *)u;
450                 dout(" server_challenge %llx client_challenge %llx key %llx\n",
451                      xi->server_challenge, le64_to_cpu(auth->client_challenge),
452                      le64_to_cpu(auth->key));
453
454                 /* now encode the old ticket if exists */
455                 ret = ceph_x_encode_ticket(th, &p, end);
456                 if (ret < 0)
457                         return ret;
458
459                 return p - buf;
460         }
461
462         if (need) {
463                 void *p = head + 1;
464                 struct ceph_x_service_ticket_request *req;
465
466                 if (p > end)
467                         return -ERANGE;
468                 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
469
470                 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
471                 if (ret)
472                         return ret;
473                 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
474                                  xi->auth_authorizer.buf->vec.iov_len);
475
476                 req = p;
477                 req->keys = cpu_to_le32(need);
478                 p += sizeof(*req);
479                 return p - buf;
480         }
481
482         return 0;
483 }
484
485 static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
486                                void *buf, void *end)
487 {
488         struct ceph_x_info *xi = ac->private;
489         struct ceph_x_reply_header *head = buf;
490         struct ceph_x_ticket_handler *th;
491         int len = end - buf;
492         int op;
493         int ret;
494
495         if (result)
496                 return result;  /* XXX hmm? */
497
498         if (xi->starting) {
499                 /* it's a hello */
500                 struct ceph_x_server_challenge *sc = buf;
501
502                 if (len != sizeof(*sc))
503                         return -EINVAL;
504                 xi->server_challenge = le64_to_cpu(sc->server_challenge);
505                 dout("handle_reply got server challenge %llx\n",
506                      xi->server_challenge);
507                 xi->starting = false;
508                 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
509                 return -EAGAIN;
510         }
511
512         op = le16_to_cpu(head->op);
513         result = le32_to_cpu(head->result);
514         dout("handle_reply op %d result %d\n", op, result);
515         switch (op) {
516         case CEPHX_GET_AUTH_SESSION_KEY:
517                 /* verify auth key */
518                 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
519                                                buf + sizeof(*head), end);
520                 break;
521
522         case CEPHX_GET_PRINCIPAL_SESSION_KEY:
523                 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
524                 if (IS_ERR(th))
525                         return PTR_ERR(th);
526                 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
527                                                buf + sizeof(*head), end);
528                 break;
529
530         default:
531                 return -EINVAL;
532         }
533         if (ret)
534                 return ret;
535         if (ac->want_keys == xi->have_keys)
536                 return 0;
537         return -EAGAIN;
538 }
539
540 static int ceph_x_create_authorizer(
541         struct ceph_auth_client *ac, int peer_type,
542         struct ceph_authorizer **a,
543         void **buf, size_t *len,
544         void **reply_buf, size_t *reply_len)
545 {
546         struct ceph_x_authorizer *au;
547         struct ceph_x_ticket_handler *th;
548         int ret;
549
550         th = get_ticket_handler(ac, peer_type);
551         if (IS_ERR(th))
552                 return PTR_ERR(th);
553
554         au = kzalloc(sizeof(*au), GFP_NOFS);
555         if (!au)
556                 return -ENOMEM;
557
558         ret = ceph_x_build_authorizer(ac, th, au);
559         if (ret) {
560                 kfree(au);
561                 return ret;
562         }
563
564         *a = (struct ceph_authorizer *)au;
565         *buf = au->buf->vec.iov_base;
566         *len = au->buf->vec.iov_len;
567         *reply_buf = au->reply_buf;
568         *reply_len = sizeof(au->reply_buf);
569         return 0;
570 }
571
572 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
573                                           struct ceph_authorizer *a, size_t len)
574 {
575         struct ceph_x_authorizer *au = (void *)a;
576         struct ceph_x_ticket_handler *th;
577         int ret = 0;
578         struct ceph_x_authorize_reply reply;
579         void *preply = &reply;
580         void *p = au->reply_buf;
581         void *end = p + sizeof(au->reply_buf);
582
583         th = get_ticket_handler(ac, au->service);
584         if (IS_ERR(th))
585                 return PTR_ERR(th);
586         ret = ceph_x_decrypt(&th->session_key, &p, end, &preply, sizeof(reply));
587         if (ret < 0)
588                 return ret;
589         if (ret != sizeof(reply))
590                 return -EPERM;
591
592         if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
593                 ret = -EPERM;
594         else
595                 ret = 0;
596         dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
597              au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
598         return ret;
599 }
600
601 static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
602                                       struct ceph_authorizer *a)
603 {
604         struct ceph_x_authorizer *au = (void *)a;
605
606         ceph_buffer_put(au->buf);
607         kfree(au);
608 }
609
610
611 static void ceph_x_reset(struct ceph_auth_client *ac)
612 {
613         struct ceph_x_info *xi = ac->private;
614
615         dout("reset\n");
616         xi->starting = true;
617         xi->server_challenge = 0;
618 }
619
620 static void ceph_x_destroy(struct ceph_auth_client *ac)
621 {
622         struct ceph_x_info *xi = ac->private;
623         struct rb_node *p;
624
625         dout("ceph_x_destroy %p\n", ac);
626         ceph_crypto_key_destroy(&xi->secret);
627
628         while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
629                 struct ceph_x_ticket_handler *th =
630                         rb_entry(p, struct ceph_x_ticket_handler, node);
631                 remove_ticket_handler(ac, th);
632         }
633
634         if (xi->auth_authorizer.buf)
635                 ceph_buffer_put(xi->auth_authorizer.buf);
636
637         kfree(ac->private);
638         ac->private = NULL;
639 }
640
641 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
642                                    int peer_type)
643 {
644         struct ceph_x_ticket_handler *th;
645
646         th = get_ticket_handler(ac, peer_type);
647         if (!IS_ERR(th))
648                 remove_ticket_handler(ac, th);
649 }
650
651
652 static const struct ceph_auth_client_ops ceph_x_ops = {
653         .name = "x",
654         .is_authenticated = ceph_x_is_authenticated,
655         .should_authenticate = ceph_x_should_authenticate,
656         .build_request = ceph_x_build_request,
657         .handle_reply = ceph_x_handle_reply,
658         .create_authorizer = ceph_x_create_authorizer,
659         .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
660         .destroy_authorizer = ceph_x_destroy_authorizer,
661         .invalidate_authorizer = ceph_x_invalidate_authorizer,
662         .reset =  ceph_x_reset,
663         .destroy = ceph_x_destroy,
664 };
665
666
667 int ceph_x_init(struct ceph_auth_client *ac)
668 {
669         struct ceph_x_info *xi;
670         int ret;
671
672         dout("ceph_x_init %p\n", ac);
673         ret = -ENOMEM;
674         xi = kzalloc(sizeof(*xi), GFP_NOFS);
675         if (!xi)
676                 goto out;
677
678         ret = -EINVAL;
679         if (!ac->key) {
680                 pr_err("no secret set (for auth_x protocol)\n");
681                 goto out_nomem;
682         }
683
684         ret = ceph_crypto_key_clone(&xi->secret, ac->key);
685         if (ret < 0) {
686                 pr_err("cannot clone key: %d\n", ret);
687                 goto out_nomem;
688         }
689
690         xi->starting = true;
691         xi->ticket_handlers = RB_ROOT;
692
693         ac->protocol = CEPH_AUTH_CEPHX;
694         ac->private = xi;
695         ac->ops = &ceph_x_ops;
696         return 0;
697
698 out_nomem:
699         kfree(xi);
700 out:
701         return ret;
702 }
703
704