Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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/err.h>
  9 #include <linux/seq_file.h>
 10 #include <linux/hash.h>
 11 
 12 #define RPCDBG_FACILITY RPCDBG_AUTH
 13 
 14 
 15 /*
 16  * AUTHUNIX and AUTHNULL credentials are both handled here.
 17  * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
 18  * are always nobody (-2).  i.e. we do the same IP address checks for
 19  * AUTHNULL as for AUTHUNIX, and that is done here.
 20  */
 21 
 22 
 23 static char *strdup(char *s)
 24 {
 25         char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
 26         if (rv)
 27                 strcpy(rv, s);
 28         return rv;
 29 }
 30 
 31 struct unix_domain {
 32         struct auth_domain      h;
 33         int     addr_changes;
 34         /* other stuff later */
 35 };
 36 
 37 struct auth_domain *unix_domain_find(char *name)
 38 {
 39         struct auth_domain *rv, ud;
 40         struct unix_domain *new;
 41 
 42         ud.name = name;
 43         
 44         rv = auth_domain_lookup(&ud, 0);
 45 
 46  foundit:
 47         if (rv && rv->flavour != RPC_AUTH_UNIX) {
 48                 auth_domain_put(rv);
 49                 return NULL;
 50         }
 51         if (rv)
 52                 return rv;
 53 
 54         new = kmalloc(sizeof(*new), GFP_KERNEL);
 55         if (new == NULL)
 56                 return NULL;
 57         cache_init(&new->h.h);
 58         new->h.name = strdup(name);
 59         new->h.flavour = RPC_AUTH_UNIX;
 60         new->addr_changes = 0;
 61         new->h.h.expiry_time = NEVER;
 62 
 63         rv = auth_domain_lookup(&new->h, 2);
 64         if (rv == &new->h) {
 65                 if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
 66         } else {
 67                 auth_domain_put(&new->h);
 68                 goto foundit;
 69         }
 70 
 71         return rv;
 72 }
 73 
 74 static void svcauth_unix_domain_release(struct auth_domain *dom)
 75 {
 76         struct unix_domain *ud = container_of(dom, struct unix_domain, h);
 77 
 78         kfree(dom->name);
 79         kfree(ud);
 80 }
 81 
 82 
 83 /**************************************************
 84  * cache for IP address to unix_domain
 85  * as needed by AUTH_UNIX
 86  */
 87 #define IP_HASHBITS     8
 88 #define IP_HASHMAX      (1<<IP_HASHBITS)
 89 #define IP_HASHMASK     (IP_HASHMAX-1)
 90 
 91 struct ip_map {
 92         struct cache_head       h;
 93         char                    m_class[8]; /* e.g. "nfsd" */
 94         struct in_addr          m_addr;
 95         struct unix_domain      *m_client;
 96         int                     m_add_change;
 97 };
 98 static struct cache_head        *ip_table[IP_HASHMAX];
 99 
100 static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
101 {
102         struct ip_map *im = container_of(item, struct ip_map,h);
103         if (cache_put(item, cd)) {
104                 if (test_bit(CACHE_VALID, &item->flags) &&
105                     !test_bit(CACHE_NEGATIVE, &item->flags))
106                         auth_domain_put(&im->m_client->h);
107                 kfree(im);
108         }
109 }
110 
111 static inline int ip_map_hash(struct ip_map *item)
112 {
113         return hash_str(item->m_class, IP_HASHBITS) ^ 
114                 hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
115 }
116 static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
117 {
118         return strcmp(tmp->m_class, item->m_class) == 0
119                 && tmp->m_addr.s_addr == item->m_addr.s_addr;
120 }
121 static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
122 {
123         strcpy(new->m_class, item->m_class);
124         new->m_addr.s_addr = item->m_addr.s_addr;
125 }
126 static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
127 {
128         cache_get(&item->m_client->h.h);
129         new->m_client = item->m_client;
130         new->m_add_change = item->m_add_change;
131 }
132 
133 static void ip_map_request(struct cache_detail *cd,
134                                   struct cache_head *h,
135                                   char **bpp, int *blen)
136 {
137         char text_addr[20];
138         struct ip_map *im = container_of(h, struct ip_map, h);
139         __u32 addr = im->m_addr.s_addr;
140         
141         snprintf(text_addr, 20, "%u.%u.%u.%u",
142                  ntohl(addr) >> 24 & 0xff,
143                  ntohl(addr) >> 16 & 0xff,
144                  ntohl(addr) >>  8 & 0xff,
145                  ntohl(addr) >>  0 & 0xff);
146 
147         qword_add(bpp, blen, im->m_class);
148         qword_add(bpp, blen, text_addr);
149         (*bpp)[-1] = '\n';
150 }
151 
152 static struct ip_map *ip_map_lookup(struct ip_map *, int);
153 
154 static int ip_map_parse(struct cache_detail *cd,
155                           char *mesg, int mlen)
156 {
157         /* class ipaddress [domainname] */
158         /* should be safe just to use the start of the input buffer
159          * for scratch: */
160         char *buf = mesg;
161         int len;
162         int b1,b2,b3,b4;
163         char c;
164         struct ip_map ipm, *ipmp;
165         struct auth_domain *dom;
166         time_t expiry;
167 
168         if (mesg[mlen-1] != '\n')
169                 return -EINVAL;
170         mesg[mlen-1] = 0;
171 
172         /* class */
173         len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
174         if (len <= 0) return -EINVAL;
175 
176         /* ip address */
177         len = qword_get(&mesg, buf, mlen);
178         if (len <= 0) return -EINVAL;
179 
180         if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
181                 return -EINVAL;
182         
183         expiry = get_expiry(&mesg);
184         if (expiry ==0)
185                 return -EINVAL;
186 
187         /* domainname, or empty for NEGATIVE */
188         len = qword_get(&mesg, buf, mlen);
189         if (len < 0) return -EINVAL;
190 
191         if (len) {
192                 dom = unix_domain_find(buf);
193                 if (dom == NULL)
194                         return -ENOENT;
195         } else
196                 dom = NULL;
197 
198         ipm.m_addr.s_addr =
199                 htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
200         ipm.h.flags = 0;
201         if (dom) {
202                 ipm.m_client = container_of(dom, struct unix_domain, h);
203                 ipm.m_add_change = ipm.m_client->addr_changes;
204         } else
205                 set_bit(CACHE_NEGATIVE, &ipm.h.flags);
206         ipm.h.expiry_time = expiry;
207 
208         ipmp = ip_map_lookup(&ipm, 1);
209         if (ipmp)
210                 ip_map_put(&ipmp->h, &ip_map_cache);
211         if (dom)
212                 auth_domain_put(dom);
213         if (!ipmp)
214                 return -ENOMEM;
215         cache_flush();
216         return 0;
217 }
218 
219 static int ip_map_show(struct seq_file *m,
220                        struct cache_detail *cd,
221                        struct cache_head *h)
222 {
223         struct ip_map *im;
224         struct in_addr addr;
225         char *dom = "-no-domain-";
226 
227         if (h == NULL) {
228                 seq_puts(m, "#class IP domain\n");
229                 return 0;
230         }
231         im = container_of(h, struct ip_map, h);
232         /* class addr domain */
233         addr = im->m_addr;
234 
235         if (test_bit(CACHE_VALID, &h->flags) && 
236             !test_bit(CACHE_NEGATIVE, &h->flags))
237                 dom = im->m_client->h.name;
238 
239         seq_printf(m, "%s %d.%d.%d.%d %s\n",
240                    im->m_class,
241                    htonl(addr.s_addr) >> 24 & 0xff,
242                    htonl(addr.s_addr) >> 16 & 0xff,
243                    htonl(addr.s_addr) >>  8 & 0xff,
244                    htonl(addr.s_addr) >>  0 & 0xff,
245                    dom
246                    );
247         return 0;
248 }
249         
250 
251 struct cache_detail ip_map_cache = {
252         .hash_size      = IP_HASHMAX,
253         .hash_table     = ip_table,
254         .name           = "auth.unix.ip",
255         .cache_put      = ip_map_put,
256         .cache_request  = ip_map_request,
257         .cache_parse    = ip_map_parse,
258         .cache_show     = ip_map_show,
259 };
260 
261 static DefineSimpleCacheLookup(ip_map, 0)
262 
263 
264 int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
265 {
266         struct unix_domain *udom;
267         struct ip_map ip, *ipmp;
268 
269         if (dom->flavour != RPC_AUTH_UNIX)
270                 return -EINVAL;
271         udom = container_of(dom, struct unix_domain, h);
272         strcpy(ip.m_class, "nfsd");
273         ip.m_addr = addr;
274         ip.m_client = udom;
275         ip.m_add_change = udom->addr_changes+1;
276         ip.h.flags = 0;
277         ip.h.expiry_time = NEVER;
278         
279         ipmp = ip_map_lookup(&ip, 1);
280 
281         if (ipmp) {
282                 ip_map_put(&ipmp->h, &ip_map_cache);
283                 return 0;
284         } else
285                 return -ENOMEM;
286 }
287 
288 int auth_unix_forget_old(struct auth_domain *dom)
289 {
290         struct unix_domain *udom;
291         
292         if (dom->flavour != RPC_AUTH_UNIX)
293                 return -EINVAL;
294         udom = container_of(dom, struct unix_domain, h);
295         udom->addr_changes++;
296         return 0;
297 }
298 
299 struct auth_domain *auth_unix_lookup(struct in_addr addr)
300 {
301         struct ip_map key, *ipm;
302         struct auth_domain *rv;
303 
304         strcpy(key.m_class, "nfsd");
305         key.m_addr = addr;
306 
307         ipm = ip_map_lookup(&key, 0);
308 
309         if (!ipm)
310                 return NULL;
311         if (cache_check(&ip_map_cache, &ipm->h, NULL))
312                 return NULL;
313 
314         if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
315                 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
316                         auth_domain_put(&ipm->m_client->h);
317                 rv = NULL;
318         } else {
319                 rv = &ipm->m_client->h;
320                 cache_get(&rv->h);
321         }
322         ip_map_put(&ipm->h, &ip_map_cache);
323         return rv;
324 }
325 
326 void svcauth_unix_purge(void)
327 {
328         cache_purge(&ip_map_cache);
329         cache_purge(&auth_domain_cache);
330 }
331 
332 static int
333 svcauth_unix_set_client(struct svc_rqst *rqstp)
334 {
335         struct ip_map key, *ipm;
336 
337         rqstp->rq_client = NULL;
338         if (rqstp->rq_proc == 0)
339                 return SVC_OK;
340 
341         strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
342         key.m_addr = rqstp->rq_addr.sin_addr;
343 
344         ipm = ip_map_lookup(&key, 0);
345 
346         if (ipm == NULL)
347                 return SVC_DENIED;
348 
349         switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
350                 default:
351                         BUG();
352                 case -EAGAIN:
353                         return SVC_DROP;
354                 case -ENOENT:
355                         return SVC_DENIED;
356                 case 0:
357                         rqstp->rq_client = &ipm->m_client->h;
358                         cache_get(&rqstp->rq_client->h);
359                         ip_map_put(&ipm->h, &ip_map_cache);
360                         break;
361         }
362         return SVC_OK;
363 }
364 
365 static int
366 svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
367 {
368         struct kvec     *argv = &rqstp->rq_arg.head[0];
369         struct kvec     *resv = &rqstp->rq_res.head[0];
370         struct svc_cred *cred = &rqstp->rq_cred;
371         int             rv=0;
372 
373         cred->cr_group_info = NULL;
374         rqstp->rq_client = NULL;
375 
376         if (argv->iov_len < 3*4)
377                 return SVC_GARBAGE;
378 
379         if (svc_getu32(argv) != 0) { 
380                 dprintk("svc: bad null cred\n");
381                 *authp = rpc_autherr_badcred;
382                 return SVC_DENIED;
383         }
384         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
385                 dprintk("svc: bad null verf\n");
386                 *authp = rpc_autherr_badverf;
387                 return SVC_DENIED;
388         }
389 
390         /* Signal that mapping to nobody uid/gid is required */
391         cred->cr_uid = (uid_t) -1;
392         cred->cr_gid = (gid_t) -1;
393         cred->cr_group_info = groups_alloc(0);
394         if (cred->cr_group_info == NULL)
395                 return SVC_DROP; /* kmalloc failure - client must retry */
396 
397         rv = svcauth_unix_set_client(rqstp);
398         if (rv == SVC_DENIED)
399                 goto badcred;
400 
401         /* Put NULL verifier */
402         svc_putu32(resv, RPC_AUTH_NULL);
403         svc_putu32(resv, 0);
404 
405         return rv;
406 
407 badcred:
408         *authp = rpc_autherr_badcred;
409         return SVC_DENIED;
410 }
411 
412 static int
413 svcauth_null_release(struct svc_rqst *rqstp)
414 {
415         if (rqstp->rq_client)
416                 auth_domain_put(rqstp->rq_client);
417         rqstp->rq_client = NULL;
418         if (rqstp->rq_cred.cr_group_info)
419                 put_group_info(rqstp->rq_cred.cr_group_info);
420         rqstp->rq_cred.cr_group_info = NULL;
421 
422         return 0; /* don't drop */
423 }
424 
425 
426 struct auth_ops svcauth_null = {
427         .name           = "null",
428         .owner          = THIS_MODULE,
429         .flavour        = RPC_AUTH_NULL,
430         .accept         = svcauth_null_accept,
431         .release        = svcauth_null_release,
432 };
433 
434 
435 static int
436 svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
437 {
438         struct kvec     *argv = &rqstp->rq_arg.head[0];
439         struct kvec     *resv = &rqstp->rq_res.head[0];
440         struct svc_cred *cred = &rqstp->rq_cred;
441         u32             slen, i;
442         int             len   = argv->iov_len;
443         int             rv=0;
444 
445         cred->cr_group_info = NULL;
446         rqstp->rq_client = NULL;
447 
448         if ((len -= 3*4) < 0)
449                 return SVC_GARBAGE;
450 
451         svc_getu32(argv);                       /* length */
452         svc_getu32(argv);                       /* time stamp */
453         slen = XDR_QUADLEN(ntohl(svc_getu32(argv)));    /* machname length */
454         if (slen > 64 || (len -= (slen + 3)*4) < 0)
455                 goto badcred;
456         argv->iov_base = (void*)((u32*)argv->iov_base + slen);  /* skip machname */
457         argv->iov_len -= slen*4;
458 
459         cred->cr_uid = ntohl(svc_getu32(argv));         /* uid */
460         cred->cr_gid = ntohl(svc_getu32(argv));         /* gid */
461         slen = ntohl(svc_getu32(argv));                 /* gids length */
462         if (slen > 16 || (len -= (slen + 2)*4) < 0)
463                 goto badcred;
464         cred->cr_group_info = groups_alloc(slen);
465         if (cred->cr_group_info == NULL)
466                 return SVC_DROP;
467         for (i = 0; i < slen; i++)
468                 GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
469 
470         if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
471                 *authp = rpc_autherr_badverf;
472                 return SVC_DENIED;
473         }
474 
475         rv = svcauth_unix_set_client(rqstp);
476         if (rv == SVC_DENIED)
477                 goto badcred;
478 
479         /* Put NULL verifier */
480         svc_putu32(resv, RPC_AUTH_NULL);
481         svc_putu32(resv, 0);
482 
483         return rv;
484 
485 badcred:
486         *authp = rpc_autherr_badcred;
487         return SVC_DENIED;
488 }
489 
490 static int
491 svcauth_unix_release(struct svc_rqst *rqstp)
492 {
493         /* Verifier (such as it is) is already in place.
494          */
495         if (rqstp->rq_client)
496                 auth_domain_put(rqstp->rq_client);
497         rqstp->rq_client = NULL;
498         if (rqstp->rq_cred.cr_group_info)
499                 put_group_info(rqstp->rq_cred.cr_group_info);
500         rqstp->rq_cred.cr_group_info = NULL;
501 
502         return 0;
503 }
504 
505 
506 struct auth_ops svcauth_unix = {
507         .name           = "unix",
508         .owner          = THIS_MODULE,
509         .flavour        = RPC_AUTH_UNIX,
510         .accept         = svcauth_unix_accept,
511         .release        = svcauth_unix_release,
512         .domain_release = svcauth_unix_domain_release,
513 };
514 
515 
  This page was automatically generated by the LXR engine.