31b99c599e7e58376d989da0b5b6ee1f7469bb3d
[pandora-kernel.git] / net / sunrpc / svcauth_unix.c
1 #include <linux/types.h>
2 #include <linux/sched.h>
3 #include <linux/module.h>
4 #include <linux/sunrpc/types.h>
5 #include <linux/sunrpc/xdr.h>
6 #include <linux/sunrpc/svcsock.h>
7 #include <linux/sunrpc/svcauth.h>
8 #include <linux/sunrpc/gss_api.h>
9 #include <linux/err.h>
10 #include <linux/seq_file.h>
11 #include <linux/hash.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <net/sock.h>
15 #include <net/ipv6.h>
16 #include <linux/kernel.h>
17 #define RPCDBG_FACILITY RPCDBG_AUTH
18
19 #include <linux/sunrpc/clnt.h>
20
21 /*
22  * AUTHUNIX and AUTHNULL credentials are both handled here.
23  * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
24  * are always nobody (-2).  i.e. we do the same IP address checks for
25  * AUTHNULL as for AUTHUNIX, and that is done here.
26  */
27
28
29 struct unix_domain {
30         struct auth_domain      h;
31         int     addr_changes;
32         /* other stuff later */
33 };
34
35 extern struct auth_ops svcauth_unix;
36
37 struct auth_domain *unix_domain_find(char *name)
38 {
39         struct auth_domain *rv;
40         struct unix_domain *new = NULL;
41
42         rv = auth_domain_lookup(name, NULL);
43         while(1) {
44                 if (rv) {
45                         if (new && rv != &new->h)
46                                 auth_domain_put(&new->h);
47
48                         if (rv->flavour != &svcauth_unix) {
49                                 auth_domain_put(rv);
50                                 return NULL;
51                         }
52                         return rv;
53                 }
54
55                 new = kmalloc(sizeof(*new), GFP_KERNEL);
56                 if (new == NULL)
57                         return NULL;
58                 kref_init(&new->h.ref);
59                 new->h.name = kstrdup(name, GFP_KERNEL);
60                 if (new->h.name == NULL) {
61                         kfree(new);
62                         return NULL;
63                 }
64                 new->h.flavour = &svcauth_unix;
65                 new->addr_changes = 0;
66                 rv = auth_domain_lookup(name, &new->h);
67         }
68 }
69 EXPORT_SYMBOL_GPL(unix_domain_find);
70
71 static void svcauth_unix_domain_release(struct auth_domain *dom)
72 {
73         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
74
75         kfree(dom->name);
76         kfree(ud);
77 }
78
79
80 /**************************************************
81  * cache for IP address to unix_domain
82  * as needed by AUTH_UNIX
83  */
84 #define IP_HASHBITS     8
85 #define IP_HASHMAX      (1<<IP_HASHBITS)
86 #define IP_HASHMASK     (IP_HASHMAX-1)
87
88 struct ip_map {
89         struct cache_head       h;
90         char                    m_class[8]; /* e.g. "nfsd" */
91         struct in6_addr         m_addr;
92         struct unix_domain      *m_client;
93         int                     m_add_change;
94 };
95 static struct cache_head        *ip_table[IP_HASHMAX];
96
97 static void ip_map_put(struct kref *kref)
98 {
99         struct cache_head *item = container_of(kref, struct cache_head, ref);
100         struct ip_map *im = container_of(item, struct ip_map,h);
101
102         if (test_bit(CACHE_VALID, &item->flags) &&
103             !test_bit(CACHE_NEGATIVE, &item->flags))
104                 auth_domain_put(&im->m_client->h);
105         kfree(im);
106 }
107
108 #if IP_HASHBITS == 8
109 /* hash_long on a 64 bit machine is currently REALLY BAD for
110  * IP addresses in reverse-endian (i.e. on a little-endian machine).
111  * So use a trivial but reliable hash instead
112  */
113 static inline int hash_ip(__be32 ip)
114 {
115         int hash = (__force u32)ip ^ ((__force u32)ip>>16);
116         return (hash ^ (hash>>8)) & 0xff;
117 }
118 #endif
119 static inline int hash_ip6(struct in6_addr ip)
120 {
121         return (hash_ip(ip.s6_addr32[0]) ^
122                 hash_ip(ip.s6_addr32[1]) ^
123                 hash_ip(ip.s6_addr32[2]) ^
124                 hash_ip(ip.s6_addr32[3]));
125 }
126 static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
127 {
128         struct ip_map *orig = container_of(corig, struct ip_map, h);
129         struct ip_map *new = container_of(cnew, struct ip_map, h);
130         return strcmp(orig->m_class, new->m_class) == 0 &&
131                ipv6_addr_equal(&orig->m_addr, &new->m_addr);
132 }
133 static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
134 {
135         struct ip_map *new = container_of(cnew, struct ip_map, h);
136         struct ip_map *item = container_of(citem, struct ip_map, h);
137
138         strcpy(new->m_class, item->m_class);
139         ipv6_addr_copy(&new->m_addr, &item->m_addr);
140 }
141 static void update(struct cache_head *cnew, struct cache_head *citem)
142 {
143         struct ip_map *new = container_of(cnew, struct ip_map, h);
144         struct ip_map *item = container_of(citem, struct ip_map, h);
145
146         kref_get(&item->m_client->h.ref);
147         new->m_client = item->m_client;
148         new->m_add_change = item->m_add_change;
149 }
150 static struct cache_head *ip_map_alloc(void)
151 {
152         struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
153         if (i)
154                 return &i->h;
155         else
156                 return NULL;
157 }
158
159 static void ip_map_request(struct cache_detail *cd,
160                                   struct cache_head *h,
161                                   char **bpp, int *blen)
162 {
163         char text_addr[40];
164         struct ip_map *im = container_of(h, struct ip_map, h);
165
166         if (ipv6_addr_v4mapped(&(im->m_addr))) {
167                 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
168         } else {
169                 snprintf(text_addr, 40, "%pI6", &im->m_addr);
170         }
171         qword_add(bpp, blen, im->m_class);
172         qword_add(bpp, blen, text_addr);
173         (*bpp)[-1] = '\n';
174 }
175
176 static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
177 {
178         return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
179 }
180
181 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
182 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
183
184 static int ip_map_parse(struct cache_detail *cd,
185                           char *mesg, int mlen)
186 {
187         /* class ipaddress [domainname] */
188         /* should be safe just to use the start of the input buffer
189          * for scratch: */
190         char *buf = mesg;
191         int len;
192         char class[8];
193         union {
194                 struct sockaddr         sa;
195                 struct sockaddr_in      s4;
196                 struct sockaddr_in6     s6;
197         } address;
198         struct sockaddr_in6 sin6;
199         int err;
200
201         struct ip_map *ipmp;
202         struct auth_domain *dom;
203         time_t expiry;
204
205         if (mesg[mlen-1] != '\n')
206                 return -EINVAL;
207         mesg[mlen-1] = 0;
208
209         /* class */
210         len = qword_get(&mesg, class, sizeof(class));
211         if (len <= 0) return -EINVAL;
212
213         /* ip address */
214         len = qword_get(&mesg, buf, mlen);
215         if (len <= 0) return -EINVAL;
216
217         if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
218                 return -EINVAL;
219         switch (address.sa.sa_family) {
220         case AF_INET:
221                 /* Form a mapped IPv4 address in sin6 */
222                 memset(&sin6, 0, sizeof(sin6));
223                 sin6.sin6_family = AF_INET6;
224                 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
225                 sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
226                 break;
227 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
228         case AF_INET6:
229                 memcpy(&sin6, &address.s6, sizeof(sin6));
230                 break;
231 #endif
232         default:
233                 return -EINVAL;
234         }
235
236         expiry = get_expiry(&mesg);
237         if (expiry ==0)
238                 return -EINVAL;
239
240         /* domainname, or empty for NEGATIVE */
241         len = qword_get(&mesg, buf, mlen);
242         if (len < 0) return -EINVAL;
243
244         if (len) {
245                 dom = unix_domain_find(buf);
246                 if (dom == NULL)
247                         return -ENOENT;
248         } else
249                 dom = NULL;
250
251         /* IPv6 scope IDs are ignored for now */
252         ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
253         if (ipmp) {
254                 err = __ip_map_update(cd, ipmp,
255                              container_of(dom, struct unix_domain, h),
256                              expiry);
257         } else
258                 err = -ENOMEM;
259
260         if (dom)
261                 auth_domain_put(dom);
262
263         cache_flush();
264         return err;
265 }
266
267 static int ip_map_show(struct seq_file *m,
268                        struct cache_detail *cd,
269                        struct cache_head *h)
270 {
271         struct ip_map *im;
272         struct in6_addr addr;
273         char *dom = "-no-domain-";
274
275         if (h == NULL) {
276                 seq_puts(m, "#class IP domain\n");
277                 return 0;
278         }
279         im = container_of(h, struct ip_map, h);
280         /* class addr domain */
281         ipv6_addr_copy(&addr, &im->m_addr);
282
283         if (test_bit(CACHE_VALID, &h->flags) &&
284             !test_bit(CACHE_NEGATIVE, &h->flags))
285                 dom = im->m_client->h.name;
286
287         if (ipv6_addr_v4mapped(&addr)) {
288                 seq_printf(m, "%s %pI4 %s\n",
289                         im->m_class, &addr.s6_addr32[3], dom);
290         } else {
291                 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
292         }
293         return 0;
294 }
295
296
297 struct cache_detail ip_map_cache = {
298         .owner          = THIS_MODULE,
299         .hash_size      = IP_HASHMAX,
300         .hash_table     = ip_table,
301         .name           = "auth.unix.ip",
302         .cache_put      = ip_map_put,
303         .cache_upcall   = ip_map_upcall,
304         .cache_parse    = ip_map_parse,
305         .cache_show     = ip_map_show,
306         .match          = ip_map_match,
307         .init           = ip_map_init,
308         .update         = update,
309         .alloc          = ip_map_alloc,
310 };
311
312 static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
313                 struct in6_addr *addr)
314 {
315         struct ip_map ip;
316         struct cache_head *ch;
317
318         strcpy(ip.m_class, class);
319         ipv6_addr_copy(&ip.m_addr, addr);
320         ch = sunrpc_cache_lookup(cd, &ip.h,
321                                  hash_str(class, IP_HASHBITS) ^
322                                  hash_ip6(*addr));
323
324         if (ch)
325                 return container_of(ch, struct ip_map, h);
326         else
327                 return NULL;
328 }
329
330 static inline struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
331 {
332         return __ip_map_lookup(&ip_map_cache, class, addr);
333 }
334
335 static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
336                 struct unix_domain *udom, time_t expiry)
337 {
338         struct ip_map ip;
339         struct cache_head *ch;
340
341         ip.m_client = udom;
342         ip.h.flags = 0;
343         if (!udom)
344                 set_bit(CACHE_NEGATIVE, &ip.h.flags);
345         else {
346                 ip.m_add_change = udom->addr_changes;
347                 /* if this is from the legacy set_client system call,
348                  * we need m_add_change to be one higher
349                  */
350                 if (expiry == NEVER)
351                         ip.m_add_change++;
352         }
353         ip.h.expiry_time = expiry;
354         ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
355                                  hash_str(ipm->m_class, IP_HASHBITS) ^
356                                  hash_ip6(ipm->m_addr));
357         if (!ch)
358                 return -ENOMEM;
359         cache_put(ch, cd);
360         return 0;
361 }
362
363 static inline int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
364 {
365         return __ip_map_update(&ip_map_cache, ipm, udom, expiry);
366 }
367
368 int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
369 {
370         struct unix_domain *udom;
371         struct ip_map *ipmp;
372
373         if (dom->flavour != &svcauth_unix)
374                 return -EINVAL;
375         udom = container_of(dom, struct unix_domain, h);
376         ipmp = ip_map_lookup("nfsd", addr);
377
378         if (ipmp)
379                 return ip_map_update(ipmp, udom, NEVER);
380         else
381                 return -ENOMEM;
382 }
383 EXPORT_SYMBOL_GPL(auth_unix_add_addr);
384
385 int auth_unix_forget_old(struct auth_domain *dom)
386 {
387         struct unix_domain *udom;
388
389         if (dom->flavour != &svcauth_unix)
390                 return -EINVAL;
391         udom = container_of(dom, struct unix_domain, h);
392         udom->addr_changes++;
393         return 0;
394 }
395 EXPORT_SYMBOL_GPL(auth_unix_forget_old);
396
397 struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
398 {
399         struct ip_map *ipm;
400         struct auth_domain *rv;
401
402         ipm = ip_map_lookup("nfsd", addr);
403
404         if (!ipm)
405                 return NULL;
406         if (cache_check(&ip_map_cache, &ipm->h, NULL))
407                 return NULL;
408
409         if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
410                 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
411                         auth_domain_put(&ipm->m_client->h);
412                 rv = NULL;
413         } else {
414                 rv = &ipm->m_client->h;
415                 kref_get(&rv->ref);
416         }
417         cache_put(&ipm->h, &ip_map_cache);
418         return rv;
419 }
420 EXPORT_SYMBOL_GPL(auth_unix_lookup);
421
422 void svcauth_unix_purge(void)
423 {
424         cache_purge(&ip_map_cache);
425 }
426 EXPORT_SYMBOL_GPL(svcauth_unix_purge);
427
428 static inline struct ip_map *
429 ip_map_cached_get(struct svc_rqst *rqstp)
430 {
431         struct ip_map *ipm = NULL;
432         struct svc_xprt *xprt = rqstp->rq_xprt;
433
434         if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
435                 spin_lock(&xprt->xpt_lock);
436                 ipm = xprt->xpt_auth_cache;
437                 if (ipm != NULL) {
438                         if (!cache_valid(&ipm->h)) {
439                                 /*
440                                  * The entry has been invalidated since it was
441                                  * remembered, e.g. by a second mount from the
442                                  * same IP address.
443                                  */
444                                 xprt->xpt_auth_cache = NULL;
445                                 spin_unlock(&xprt->xpt_lock);
446                                 cache_put(&ipm->h, &ip_map_cache);
447                                 return NULL;
448                         }
449                         cache_get(&ipm->h);
450                 }
451                 spin_unlock(&xprt->xpt_lock);
452         }
453         return ipm;
454 }
455
456 static inline void
457 ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
458 {
459         struct svc_xprt *xprt = rqstp->rq_xprt;
460
461         if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
462                 spin_lock(&xprt->xpt_lock);
463                 if (xprt->xpt_auth_cache == NULL) {
464                         /* newly cached, keep the reference */
465                         xprt->xpt_auth_cache = ipm;
466                         ipm = NULL;
467                 }
468                 spin_unlock(&xprt->xpt_lock);
469         }
470         if (ipm)
471                 cache_put(&ipm->h, &ip_map_cache);
472 }
473
474 void
475 svcauth_unix_info_release(void *info)
476 {
477         struct ip_map *ipm = info;
478         cache_put(&ipm->h, &ip_map_cache);
479 }
480
481 /****************************************************************************
482  * auth.unix.gid cache
483  * simple cache to map a UID to a list of GIDs
484  * because AUTH_UNIX aka AUTH_SYS has a max of 16
485  */
486 #define GID_HASHBITS    8
487 #define GID_HASHMAX     (1<<GID_HASHBITS)
488 #define GID_HASHMASK    (GID_HASHMAX - 1)
489
490 struct unix_gid {
491         struct cache_head       h;
492         uid_t                   uid;
493         struct group_info       *gi;
494 };
495 static struct cache_head        *gid_table[GID_HASHMAX];
496
497 static void unix_gid_put(struct kref *kref)
498 {
499         struct cache_head *item = container_of(kref, struct cache_head, ref);
500         struct unix_gid *ug = container_of(item, struct unix_gid, h);
501         if (test_bit(CACHE_VALID, &item->flags) &&
502             !test_bit(CACHE_NEGATIVE, &item->flags))
503                 put_group_info(ug->gi);
504         kfree(ug);
505 }
506
507 static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
508 {
509         struct unix_gid *orig = container_of(corig, struct unix_gid, h);
510         struct unix_gid *new = container_of(cnew, struct unix_gid, h);
511         return orig->uid == new->uid;
512 }
513 static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
514 {
515         struct unix_gid *new = container_of(cnew, struct unix_gid, h);
516         struct unix_gid *item = container_of(citem, struct unix_gid, h);
517         new->uid = item->uid;
518 }
519 static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
520 {
521         struct unix_gid *new = container_of(cnew, struct unix_gid, h);
522         struct unix_gid *item = container_of(citem, struct unix_gid, h);
523
524         get_group_info(item->gi);
525         new->gi = item->gi;
526 }
527 static struct cache_head *unix_gid_alloc(void)
528 {
529         struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
530         if (g)
531                 return &g->h;
532         else
533                 return NULL;
534 }
535
536 static void unix_gid_request(struct cache_detail *cd,
537                              struct cache_head *h,
538                              char **bpp, int *blen)
539 {
540         char tuid[20];
541         struct unix_gid *ug = container_of(h, struct unix_gid, h);
542
543         snprintf(tuid, 20, "%u", ug->uid);
544         qword_add(bpp, blen, tuid);
545         (*bpp)[-1] = '\n';
546 }
547
548 static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
549 {
550         return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
551 }
552
553 static struct unix_gid *unix_gid_lookup(uid_t uid);
554 extern struct cache_detail unix_gid_cache;
555
556 static int unix_gid_parse(struct cache_detail *cd,
557                         char *mesg, int mlen)
558 {
559         /* uid expiry Ngid gid0 gid1 ... gidN-1 */
560         int uid;
561         int gids;
562         int rv;
563         int i;
564         int err;
565         time_t expiry;
566         struct unix_gid ug, *ugp;
567
568         if (mlen <= 0 || mesg[mlen-1] != '\n')
569                 return -EINVAL;
570         mesg[mlen-1] = 0;
571
572         rv = get_int(&mesg, &uid);
573         if (rv)
574                 return -EINVAL;
575         ug.uid = uid;
576
577         expiry = get_expiry(&mesg);
578         if (expiry == 0)
579                 return -EINVAL;
580
581         rv = get_int(&mesg, &gids);
582         if (rv || gids < 0 || gids > 8192)
583                 return -EINVAL;
584
585         ug.gi = groups_alloc(gids);
586         if (!ug.gi)
587                 return -ENOMEM;
588
589         for (i = 0 ; i < gids ; i++) {
590                 int gid;
591                 rv = get_int(&mesg, &gid);
592                 err = -EINVAL;
593                 if (rv)
594                         goto out;
595                 GROUP_AT(ug.gi, i) = gid;
596         }
597
598         ugp = unix_gid_lookup(uid);
599         if (ugp) {
600                 struct cache_head *ch;
601                 ug.h.flags = 0;
602                 ug.h.expiry_time = expiry;
603                 ch = sunrpc_cache_update(&unix_gid_cache,
604                                          &ug.h, &ugp->h,
605                                          hash_long(uid, GID_HASHBITS));
606                 if (!ch)
607                         err = -ENOMEM;
608                 else {
609                         err = 0;
610                         cache_put(ch, &unix_gid_cache);
611                 }
612         } else
613                 err = -ENOMEM;
614  out:
615         if (ug.gi)
616                 put_group_info(ug.gi);
617         return err;
618 }
619
620 static int unix_gid_show(struct seq_file *m,
621                          struct cache_detail *cd,
622                          struct cache_head *h)
623 {
624         struct unix_gid *ug;
625         int i;
626         int glen;
627
628         if (h == NULL) {
629                 seq_puts(m, "#uid cnt: gids...\n");
630                 return 0;
631         }
632         ug = container_of(h, struct unix_gid, h);
633         if (test_bit(CACHE_VALID, &h->flags) &&
634             !test_bit(CACHE_NEGATIVE, &h->flags))
635                 glen = ug->gi->ngroups;
636         else
637                 glen = 0;
638
639         seq_printf(m, "%u %d:", ug->uid, glen);
640         for (i = 0; i < glen; i++)
641                 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
642         seq_printf(m, "\n");
643         return 0;
644 }
645
646 struct cache_detail unix_gid_cache = {
647         .owner          = THIS_MODULE,
648         .hash_size      = GID_HASHMAX,
649         .hash_table     = gid_table,
650         .name           = "auth.unix.gid",
651         .cache_put      = unix_gid_put,
652         .cache_upcall   = unix_gid_upcall,
653         .cache_parse    = unix_gid_parse,
654         .cache_show     = unix_gid_show,
655         .match          = unix_gid_match,
656         .init           = unix_gid_init,
657         .update         = unix_gid_update,
658         .alloc          = unix_gid_alloc,
659 };
660
661 static struct unix_gid *unix_gid_lookup(uid_t uid)
662 {
663         struct unix_gid ug;
664         struct cache_head *ch;
665
666         ug.uid = uid;
667         ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
668                                  hash_long(uid, GID_HASHBITS));
669         if (ch)
670                 return container_of(ch, struct unix_gid, h);
671         else
672                 return NULL;
673 }
674
675 static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
676 {
677         struct unix_gid *ug;
678         struct group_info *gi;
679         int ret;
680
681         ug = unix_gid_lookup(uid);
682         if (!ug)
683                 return ERR_PTR(-EAGAIN);
684         ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
685         switch (ret) {
686         case -ENOENT:
687                 return ERR_PTR(-ENOENT);
688         case -ETIMEDOUT:
689                 return ERR_PTR(-ESHUTDOWN);
690         case 0:
691                 gi = get_group_info(ug->gi);
692                 cache_put(&ug->h, &unix_gid_cache);
693                 return gi;
694         default:
695                 return ERR_PTR(-EAGAIN);
696         }
697 }
698
699 int
700 svcauth_unix_set_client(struct svc_rqst *rqstp)
701 {
702         struct sockaddr_in *sin;
703         struct sockaddr_in6 *sin6, sin6_storage;
704         struct ip_map *ipm;
705         struct group_info *gi;
706         struct svc_cred *cred = &rqstp->rq_cred;
707
708         switch (rqstp->rq_addr.ss_family) {
709         case AF_INET:
710                 sin = svc_addr_in(rqstp);
711                 sin6 = &sin6_storage;
712                 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
713                 break;
714         case AF_INET6:
715                 sin6 = svc_addr_in6(rqstp);
716                 break;
717         default:
718                 BUG();
719         }
720
721         rqstp->rq_client = NULL;
722         if (rqstp->rq_proc == 0)
723                 return SVC_OK;
724
725         ipm = ip_map_cached_get(rqstp);
726         if (ipm == NULL)
727                 ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
728                                     &sin6->sin6_addr);
729
730         if (ipm == NULL)
731                 return SVC_DENIED;
732
733         switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
734                 default:
735                         BUG();
736                 case -ETIMEDOUT:
737                         return SVC_CLOSE;
738                 case -EAGAIN:
739                         return SVC_DROP;
740                 case -ENOENT:
741                         return SVC_DENIED;
742                 case 0:
743                         rqstp->rq_client = &ipm->m_client->h;
744                         kref_get(&rqstp->rq_client->ref);
745                         ip_map_cached_put(rqstp, ipm);
746                         break;
747         }
748
749         gi = unix_gid_find(cred->cr_uid, rqstp);
750         switch (PTR_ERR(gi)) {
751         case -EAGAIN:
752                 return SVC_DROP;
753         case -ESHUTDOWN:
754                 return SVC_CLOSE;
755         case -ENOENT:
756                 break;
757         default:
758                 put_group_info(cred->cr_group_info);
759                 cred->cr_group_info = gi;
760         }
761         return SVC_OK;
762 }
763
764 EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
765
766 static int
767 svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
768 {
769         struct kvec     *argv = &rqstp->rq_arg.head[0];
770         struct kvec     *resv = &rqstp->rq_res.head[0];
771         struct svc_cred *cred = &rqstp->rq_cred;
772
773         cred->cr_group_info = NULL;
774         rqstp->rq_client = NULL;
775
776         if (argv->iov_len < 3*4)
777                 return SVC_GARBAGE;
778
779         if (svc_getu32(argv) != 0) {
780                 dprintk("svc: bad null cred\n");
781                 *authp = rpc_autherr_badcred;
782                 return SVC_DENIED;
783         }
784         if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
785                 dprintk("svc: bad null verf\n");
786                 *authp = rpc_autherr_badverf;
787                 return SVC_DENIED;
788         }
789
790         /* Signal that mapping to nobody uid/gid is required */
791         cred->cr_uid = (uid_t) -1;
792         cred->cr_gid = (gid_t) -1;
793         cred->cr_group_info = groups_alloc(0);
794         if (cred->cr_group_info == NULL)
795                 return SVC_CLOSE; /* kmalloc failure - client must retry */
796
797         /* Put NULL verifier */
798         svc_putnl(resv, RPC_AUTH_NULL);
799         svc_putnl(resv, 0);
800
801         rqstp->rq_flavor = RPC_AUTH_NULL;
802         return SVC_OK;
803 }
804
805 static int
806 svcauth_null_release(struct svc_rqst *rqstp)
807 {
808         if (rqstp->rq_client)
809                 auth_domain_put(rqstp->rq_client);
810         rqstp->rq_client = NULL;
811         if (rqstp->rq_cred.cr_group_info)
812                 put_group_info(rqstp->rq_cred.cr_group_info);
813         rqstp->rq_cred.cr_group_info = NULL;
814
815         return 0; /* don't drop */
816 }
817
818
819 struct auth_ops svcauth_null = {
820         .name           = "null",
821         .owner          = THIS_MODULE,
822         .flavour        = RPC_AUTH_NULL,
823         .accept         = svcauth_null_accept,
824         .release        = svcauth_null_release,
825         .set_client     = svcauth_unix_set_client,
826 };
827
828
829 static int
830 svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
831 {
832         struct kvec     *argv = &rqstp->rq_arg.head[0];
833         struct kvec     *resv = &rqstp->rq_res.head[0];
834         struct svc_cred *cred = &rqstp->rq_cred;
835         u32             slen, i;
836         int             len   = argv->iov_len;
837
838         cred->cr_group_info = NULL;
839         rqstp->rq_client = NULL;
840
841         if ((len -= 3*4) < 0)
842                 return SVC_GARBAGE;
843
844         svc_getu32(argv);                       /* length */
845         svc_getu32(argv);                       /* time stamp */
846         slen = XDR_QUADLEN(svc_getnl(argv));    /* machname length */
847         if (slen > 64 || (len -= (slen + 3)*4) < 0)
848                 goto badcred;
849         argv->iov_base = (void*)((__be32*)argv->iov_base + slen);       /* skip machname */
850         argv->iov_len -= slen*4;
851
852         cred->cr_uid = svc_getnl(argv);         /* uid */
853         cred->cr_gid = svc_getnl(argv);         /* gid */
854         slen = svc_getnl(argv);                 /* gids length */
855         if (slen > 16 || (len -= (slen + 2)*4) < 0)
856                 goto badcred;
857         cred->cr_group_info = groups_alloc(slen);
858         if (cred->cr_group_info == NULL)
859                 return SVC_CLOSE;
860         for (i = 0; i < slen; i++)
861                 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
862         if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
863                 *authp = rpc_autherr_badverf;
864                 return SVC_DENIED;
865         }
866
867         /* Put NULL verifier */
868         svc_putnl(resv, RPC_AUTH_NULL);
869         svc_putnl(resv, 0);
870
871         rqstp->rq_flavor = RPC_AUTH_UNIX;
872         return SVC_OK;
873
874 badcred:
875         *authp = rpc_autherr_badcred;
876         return SVC_DENIED;
877 }
878
879 static int
880 svcauth_unix_release(struct svc_rqst *rqstp)
881 {
882         /* Verifier (such as it is) is already in place.
883          */
884         if (rqstp->rq_client)
885                 auth_domain_put(rqstp->rq_client);
886         rqstp->rq_client = NULL;
887         if (rqstp->rq_cred.cr_group_info)
888                 put_group_info(rqstp->rq_cred.cr_group_info);
889         rqstp->rq_cred.cr_group_info = NULL;
890
891         return 0;
892 }
893
894
895 struct auth_ops svcauth_unix = {
896         .name           = "unix",
897         .owner          = THIS_MODULE,
898         .flavour        = RPC_AUTH_UNIX,
899         .accept         = svcauth_unix_accept,
900         .release        = svcauth_unix_release,
901         .domain_release = svcauth_unix_domain_release,
902         .set_client     = svcauth_unix_set_client,
903 };
904