auth_gss.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /*
  2. * linux/net/sunrpc/auth_gss/auth_gss.c
  3. *
  4. * RPCSEC_GSS client authentication.
  5. *
  6. * Copyright (c) 2000 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Dug Song <dugsong@monkey.org>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/sched.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/sunrpc/auth.h>
  45. #include <linux/sunrpc/auth_gss.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include <linux/sunrpc/gss_err.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/sunrpc/rpc_pipe_fs.h>
  50. #include <linux/sunrpc/gss_api.h>
  51. #include <asm/uaccess.h>
  52. static const struct rpc_authops authgss_ops;
  53. static const struct rpc_credops gss_credops;
  54. static const struct rpc_credops gss_nullops;
  55. #define GSS_RETRY_EXPIRED 5
  56. static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
  57. #ifdef RPC_DEBUG
  58. # define RPCDBG_FACILITY RPCDBG_AUTH
  59. #endif
  60. #define GSS_CRED_SLACK (RPC_MAX_AUTH_SIZE * 2)
  61. /* length of a krb5 verifier (48), plus data added before arguments when
  62. * using integrity (two 4-byte integers): */
  63. #define GSS_VERF_SLACK 100
  64. struct gss_auth {
  65. struct kref kref;
  66. struct rpc_auth rpc_auth;
  67. struct gss_api_mech *mech;
  68. enum rpc_gss_svc service;
  69. struct rpc_clnt *client;
  70. /*
  71. * There are two upcall pipes; dentry[1], named "gssd", is used
  72. * for the new text-based upcall; dentry[0] is named after the
  73. * mechanism (for example, "krb5") and exists for
  74. * backwards-compatibility with older gssd's.
  75. */
  76. struct rpc_pipe *pipe[2];
  77. };
  78. /* pipe_version >= 0 if and only if someone has a pipe open. */
  79. static int pipe_version = -1;
  80. static atomic_t pipe_users = ATOMIC_INIT(0);
  81. static DEFINE_SPINLOCK(pipe_version_lock);
  82. static struct rpc_wait_queue pipe_version_rpc_waitqueue;
  83. static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
  84. static void gss_free_ctx(struct gss_cl_ctx *);
  85. static const struct rpc_pipe_ops gss_upcall_ops_v0;
  86. static const struct rpc_pipe_ops gss_upcall_ops_v1;
  87. static inline struct gss_cl_ctx *
  88. gss_get_ctx(struct gss_cl_ctx *ctx)
  89. {
  90. atomic_inc(&ctx->count);
  91. return ctx;
  92. }
  93. static inline void
  94. gss_put_ctx(struct gss_cl_ctx *ctx)
  95. {
  96. if (atomic_dec_and_test(&ctx->count))
  97. gss_free_ctx(ctx);
  98. }
  99. /* gss_cred_set_ctx:
  100. * called by gss_upcall_callback and gss_create_upcall in order
  101. * to set the gss context. The actual exchange of an old context
  102. * and a new one is protected by the pipe->lock.
  103. */
  104. static void
  105. gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
  106. {
  107. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  108. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  109. return;
  110. gss_get_ctx(ctx);
  111. rcu_assign_pointer(gss_cred->gc_ctx, ctx);
  112. set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  113. smp_mb__before_clear_bit();
  114. clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
  115. }
  116. static const void *
  117. simple_get_bytes(const void *p, const void *end, void *res, size_t len)
  118. {
  119. const void *q = (const void *)((const char *)p + len);
  120. if (unlikely(q > end || q < p))
  121. return ERR_PTR(-EFAULT);
  122. memcpy(res, p, len);
  123. return q;
  124. }
  125. static inline const void *
  126. simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
  127. {
  128. const void *q;
  129. unsigned int len;
  130. p = simple_get_bytes(p, end, &len, sizeof(len));
  131. if (IS_ERR(p))
  132. return p;
  133. q = (const void *)((const char *)p + len);
  134. if (unlikely(q > end || q < p))
  135. return ERR_PTR(-EFAULT);
  136. dest->data = kmemdup(p, len, GFP_NOFS);
  137. if (unlikely(dest->data == NULL))
  138. return ERR_PTR(-ENOMEM);
  139. dest->len = len;
  140. return q;
  141. }
  142. static struct gss_cl_ctx *
  143. gss_cred_get_ctx(struct rpc_cred *cred)
  144. {
  145. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  146. struct gss_cl_ctx *ctx = NULL;
  147. rcu_read_lock();
  148. if (gss_cred->gc_ctx)
  149. ctx = gss_get_ctx(gss_cred->gc_ctx);
  150. rcu_read_unlock();
  151. return ctx;
  152. }
  153. static struct gss_cl_ctx *
  154. gss_alloc_context(void)
  155. {
  156. struct gss_cl_ctx *ctx;
  157. ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
  158. if (ctx != NULL) {
  159. ctx->gc_proc = RPC_GSS_PROC_DATA;
  160. ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
  161. spin_lock_init(&ctx->gc_seq_lock);
  162. atomic_set(&ctx->count,1);
  163. }
  164. return ctx;
  165. }
  166. #define GSSD_MIN_TIMEOUT (60 * 60)
  167. static const void *
  168. gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
  169. {
  170. const void *q;
  171. unsigned int seclen;
  172. unsigned int timeout;
  173. unsigned long now = jiffies;
  174. u32 window_size;
  175. int ret;
  176. /* First unsigned int gives the remaining lifetime in seconds of the
  177. * credential - e.g. the remaining TGT lifetime for Kerberos or
  178. * the -t value passed to GSSD.
  179. */
  180. p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
  181. if (IS_ERR(p))
  182. goto err;
  183. if (timeout == 0)
  184. timeout = GSSD_MIN_TIMEOUT;
  185. ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
  186. /* Sequence number window. Determines the maximum number of
  187. * simultaneous requests
  188. */
  189. p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
  190. if (IS_ERR(p))
  191. goto err;
  192. ctx->gc_win = window_size;
  193. /* gssd signals an error by passing ctx->gc_win = 0: */
  194. if (ctx->gc_win == 0) {
  195. /*
  196. * in which case, p points to an error code. Anything other
  197. * than -EKEYEXPIRED gets converted to -EACCES.
  198. */
  199. p = simple_get_bytes(p, end, &ret, sizeof(ret));
  200. if (!IS_ERR(p))
  201. p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
  202. ERR_PTR(-EACCES);
  203. goto err;
  204. }
  205. /* copy the opaque wire context */
  206. p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
  207. if (IS_ERR(p))
  208. goto err;
  209. /* import the opaque security context */
  210. p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
  211. if (IS_ERR(p))
  212. goto err;
  213. q = (const void *)((const char *)p + seclen);
  214. if (unlikely(q > end || q < p)) {
  215. p = ERR_PTR(-EFAULT);
  216. goto err;
  217. }
  218. ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, GFP_NOFS);
  219. if (ret < 0) {
  220. p = ERR_PTR(ret);
  221. goto err;
  222. }
  223. dprintk("RPC: %s Success. gc_expiry %lu now %lu timeout %u\n",
  224. __func__, ctx->gc_expiry, now, timeout);
  225. return q;
  226. err:
  227. dprintk("RPC: %s returns %ld gc_expiry %lu now %lu timeout %u\n",
  228. __func__, -PTR_ERR(p), ctx->gc_expiry, now, timeout);
  229. return p;
  230. }
  231. #define UPCALL_BUF_LEN 128
  232. struct gss_upcall_msg {
  233. atomic_t count;
  234. kuid_t uid;
  235. struct rpc_pipe_msg msg;
  236. struct list_head list;
  237. struct gss_auth *auth;
  238. struct rpc_pipe *pipe;
  239. struct rpc_wait_queue rpc_waitqueue;
  240. wait_queue_head_t waitqueue;
  241. struct gss_cl_ctx *ctx;
  242. char databuf[UPCALL_BUF_LEN];
  243. };
  244. static int get_pipe_version(void)
  245. {
  246. int ret;
  247. spin_lock(&pipe_version_lock);
  248. if (pipe_version >= 0) {
  249. atomic_inc(&pipe_users);
  250. ret = pipe_version;
  251. } else
  252. ret = -EAGAIN;
  253. spin_unlock(&pipe_version_lock);
  254. return ret;
  255. }
  256. static void put_pipe_version(void)
  257. {
  258. if (atomic_dec_and_lock(&pipe_users, &pipe_version_lock)) {
  259. pipe_version = -1;
  260. spin_unlock(&pipe_version_lock);
  261. }
  262. }
  263. static void
  264. gss_release_msg(struct gss_upcall_msg *gss_msg)
  265. {
  266. if (!atomic_dec_and_test(&gss_msg->count))
  267. return;
  268. put_pipe_version();
  269. BUG_ON(!list_empty(&gss_msg->list));
  270. if (gss_msg->ctx != NULL)
  271. gss_put_ctx(gss_msg->ctx);
  272. rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
  273. kfree(gss_msg);
  274. }
  275. static struct gss_upcall_msg *
  276. __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
  277. {
  278. struct gss_upcall_msg *pos;
  279. list_for_each_entry(pos, &pipe->in_downcall, list) {
  280. if (!uid_eq(pos->uid, uid))
  281. continue;
  282. atomic_inc(&pos->count);
  283. dprintk("RPC: %s found msg %p\n", __func__, pos);
  284. return pos;
  285. }
  286. dprintk("RPC: %s found nothing\n", __func__);
  287. return NULL;
  288. }
  289. /* Try to add an upcall to the pipefs queue.
  290. * If an upcall owned by our uid already exists, then we return a reference
  291. * to that upcall instead of adding the new upcall.
  292. */
  293. static inline struct gss_upcall_msg *
  294. gss_add_msg(struct gss_upcall_msg *gss_msg)
  295. {
  296. struct rpc_pipe *pipe = gss_msg->pipe;
  297. struct gss_upcall_msg *old;
  298. spin_lock(&pipe->lock);
  299. old = __gss_find_upcall(pipe, gss_msg->uid);
  300. if (old == NULL) {
  301. atomic_inc(&gss_msg->count);
  302. list_add(&gss_msg->list, &pipe->in_downcall);
  303. } else
  304. gss_msg = old;
  305. spin_unlock(&pipe->lock);
  306. return gss_msg;
  307. }
  308. static void
  309. __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  310. {
  311. list_del_init(&gss_msg->list);
  312. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  313. wake_up_all(&gss_msg->waitqueue);
  314. atomic_dec(&gss_msg->count);
  315. }
  316. static void
  317. gss_unhash_msg(struct gss_upcall_msg *gss_msg)
  318. {
  319. struct rpc_pipe *pipe = gss_msg->pipe;
  320. if (list_empty(&gss_msg->list))
  321. return;
  322. spin_lock(&pipe->lock);
  323. if (!list_empty(&gss_msg->list))
  324. __gss_unhash_msg(gss_msg);
  325. spin_unlock(&pipe->lock);
  326. }
  327. static void
  328. gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
  329. {
  330. switch (gss_msg->msg.errno) {
  331. case 0:
  332. if (gss_msg->ctx == NULL)
  333. break;
  334. clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  335. gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
  336. break;
  337. case -EKEYEXPIRED:
  338. set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
  339. }
  340. gss_cred->gc_upcall_timestamp = jiffies;
  341. gss_cred->gc_upcall = NULL;
  342. rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
  343. }
  344. static void
  345. gss_upcall_callback(struct rpc_task *task)
  346. {
  347. struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
  348. struct gss_cred, gc_base);
  349. struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
  350. struct rpc_pipe *pipe = gss_msg->pipe;
  351. spin_lock(&pipe->lock);
  352. gss_handle_downcall_result(gss_cred, gss_msg);
  353. spin_unlock(&pipe->lock);
  354. task->tk_status = gss_msg->msg.errno;
  355. gss_release_msg(gss_msg);
  356. }
  357. static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
  358. {
  359. gss_msg->msg.data = &gss_msg->uid;
  360. gss_msg->msg.len = sizeof(gss_msg->uid);
  361. }
  362. static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
  363. struct rpc_clnt *clnt,
  364. const char *service_name)
  365. {
  366. struct gss_api_mech *mech = gss_msg->auth->mech;
  367. char *p = gss_msg->databuf;
  368. int len = 0;
  369. gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
  370. mech->gm_name,
  371. gss_msg->uid);
  372. p += gss_msg->msg.len;
  373. if (clnt->cl_principal) {
  374. len = sprintf(p, "target=%s ", clnt->cl_principal);
  375. p += len;
  376. gss_msg->msg.len += len;
  377. }
  378. if (service_name != NULL) {
  379. len = sprintf(p, "service=%s ", service_name);
  380. p += len;
  381. gss_msg->msg.len += len;
  382. }
  383. if (mech->gm_upcall_enctypes) {
  384. len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
  385. p += len;
  386. gss_msg->msg.len += len;
  387. }
  388. len = sprintf(p, "\n");
  389. gss_msg->msg.len += len;
  390. gss_msg->msg.data = gss_msg->databuf;
  391. BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
  392. }
  393. static void gss_encode_msg(struct gss_upcall_msg *gss_msg,
  394. struct rpc_clnt *clnt,
  395. const char *service_name)
  396. {
  397. if (pipe_version == 0)
  398. gss_encode_v0_msg(gss_msg);
  399. else /* pipe_version == 1 */
  400. gss_encode_v1_msg(gss_msg, clnt, service_name);
  401. }
  402. static struct gss_upcall_msg *
  403. gss_alloc_msg(struct gss_auth *gss_auth, struct rpc_clnt *clnt,
  404. kuid_t uid, const char *service_name)
  405. {
  406. struct gss_upcall_msg *gss_msg;
  407. int vers;
  408. gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
  409. if (gss_msg == NULL)
  410. return ERR_PTR(-ENOMEM);
  411. vers = get_pipe_version();
  412. if (vers < 0) {
  413. kfree(gss_msg);
  414. return ERR_PTR(vers);
  415. }
  416. gss_msg->pipe = gss_auth->pipe[vers];
  417. INIT_LIST_HEAD(&gss_msg->list);
  418. rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
  419. init_waitqueue_head(&gss_msg->waitqueue);
  420. atomic_set(&gss_msg->count, 1);
  421. gss_msg->uid = uid;
  422. gss_msg->auth = gss_auth;
  423. gss_encode_msg(gss_msg, clnt, service_name);
  424. return gss_msg;
  425. }
  426. static struct gss_upcall_msg *
  427. gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
  428. {
  429. struct gss_cred *gss_cred = container_of(cred,
  430. struct gss_cred, gc_base);
  431. struct gss_upcall_msg *gss_new, *gss_msg;
  432. kuid_t uid = cred->cr_uid;
  433. gss_new = gss_alloc_msg(gss_auth, clnt, uid, gss_cred->gc_principal);
  434. if (IS_ERR(gss_new))
  435. return gss_new;
  436. gss_msg = gss_add_msg(gss_new);
  437. if (gss_msg == gss_new) {
  438. int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
  439. if (res) {
  440. gss_unhash_msg(gss_new);
  441. gss_msg = ERR_PTR(res);
  442. }
  443. } else
  444. gss_release_msg(gss_new);
  445. return gss_msg;
  446. }
  447. static void warn_gssd(void)
  448. {
  449. static unsigned long ratelimit;
  450. unsigned long now = jiffies;
  451. if (time_after(now, ratelimit)) {
  452. printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
  453. "Please check user daemon is running.\n");
  454. ratelimit = now + 15*HZ;
  455. }
  456. }
  457. static inline int
  458. gss_refresh_upcall(struct rpc_task *task)
  459. {
  460. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  461. struct gss_auth *gss_auth = container_of(cred->cr_auth,
  462. struct gss_auth, rpc_auth);
  463. struct gss_cred *gss_cred = container_of(cred,
  464. struct gss_cred, gc_base);
  465. struct gss_upcall_msg *gss_msg;
  466. struct rpc_pipe *pipe;
  467. int err = 0;
  468. dprintk("RPC: %5u %s for uid %u\n",
  469. task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
  470. gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
  471. if (PTR_ERR(gss_msg) == -EAGAIN) {
  472. /* XXX: warning on the first, under the assumption we
  473. * shouldn't normally hit this case on a refresh. */
  474. warn_gssd();
  475. task->tk_timeout = 15*HZ;
  476. rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
  477. return -EAGAIN;
  478. }
  479. if (IS_ERR(gss_msg)) {
  480. err = PTR_ERR(gss_msg);
  481. goto out;
  482. }
  483. pipe = gss_msg->pipe;
  484. spin_lock(&pipe->lock);
  485. if (gss_cred->gc_upcall != NULL)
  486. rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
  487. else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
  488. task->tk_timeout = 0;
  489. gss_cred->gc_upcall = gss_msg;
  490. /* gss_upcall_callback will release the reference to gss_upcall_msg */
  491. atomic_inc(&gss_msg->count);
  492. rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
  493. } else {
  494. gss_handle_downcall_result(gss_cred, gss_msg);
  495. err = gss_msg->msg.errno;
  496. }
  497. spin_unlock(&pipe->lock);
  498. gss_release_msg(gss_msg);
  499. out:
  500. dprintk("RPC: %5u %s for uid %u result %d\n",
  501. task->tk_pid, __func__,
  502. from_kuid(&init_user_ns, cred->cr_uid), err);
  503. return err;
  504. }
  505. static inline int
  506. gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
  507. {
  508. struct rpc_pipe *pipe;
  509. struct rpc_cred *cred = &gss_cred->gc_base;
  510. struct gss_upcall_msg *gss_msg;
  511. DEFINE_WAIT(wait);
  512. int err = 0;
  513. dprintk("RPC: %s for uid %u\n",
  514. __func__, from_kuid(&init_user_ns, cred->cr_uid));
  515. retry:
  516. gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
  517. if (PTR_ERR(gss_msg) == -EAGAIN) {
  518. err = wait_event_interruptible_timeout(pipe_version_waitqueue,
  519. pipe_version >= 0, 15*HZ);
  520. if (pipe_version < 0) {
  521. warn_gssd();
  522. err = -EACCES;
  523. }
  524. if (err)
  525. goto out;
  526. goto retry;
  527. }
  528. if (IS_ERR(gss_msg)) {
  529. err = PTR_ERR(gss_msg);
  530. goto out;
  531. }
  532. pipe = gss_msg->pipe;
  533. for (;;) {
  534. prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
  535. spin_lock(&pipe->lock);
  536. if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
  537. break;
  538. }
  539. spin_unlock(&pipe->lock);
  540. if (fatal_signal_pending(current)) {
  541. err = -ERESTARTSYS;
  542. goto out_intr;
  543. }
  544. schedule();
  545. }
  546. if (gss_msg->ctx)
  547. gss_cred_set_ctx(cred, gss_msg->ctx);
  548. else
  549. err = gss_msg->msg.errno;
  550. spin_unlock(&pipe->lock);
  551. out_intr:
  552. finish_wait(&gss_msg->waitqueue, &wait);
  553. gss_release_msg(gss_msg);
  554. out:
  555. dprintk("RPC: %s for uid %u result %d\n",
  556. __func__, from_kuid(&init_user_ns, cred->cr_uid), err);
  557. return err;
  558. }
  559. #define MSG_BUF_MAXSIZE 1024
  560. static ssize_t
  561. gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
  562. {
  563. const void *p, *end;
  564. void *buf;
  565. struct gss_upcall_msg *gss_msg;
  566. struct rpc_pipe *pipe = RPC_I(filp->f_dentry->d_inode)->pipe;
  567. struct gss_cl_ctx *ctx;
  568. uid_t uid;
  569. ssize_t err = -EFBIG;
  570. if (mlen > MSG_BUF_MAXSIZE)
  571. goto out;
  572. err = -ENOMEM;
  573. buf = kmalloc(mlen, GFP_NOFS);
  574. if (!buf)
  575. goto out;
  576. err = -EFAULT;
  577. if (copy_from_user(buf, src, mlen))
  578. goto err;
  579. end = (const void *)((char *)buf + mlen);
  580. p = simple_get_bytes(buf, end, &uid, sizeof(uid));
  581. if (IS_ERR(p)) {
  582. err = PTR_ERR(p);
  583. goto err;
  584. }
  585. err = -ENOMEM;
  586. ctx = gss_alloc_context();
  587. if (ctx == NULL)
  588. goto err;
  589. err = -ENOENT;
  590. /* Find a matching upcall */
  591. spin_lock(&pipe->lock);
  592. gss_msg = __gss_find_upcall(pipe, uid);
  593. if (gss_msg == NULL) {
  594. spin_unlock(&pipe->lock);
  595. goto err_put_ctx;
  596. }
  597. list_del_init(&gss_msg->list);
  598. spin_unlock(&pipe->lock);
  599. p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
  600. if (IS_ERR(p)) {
  601. err = PTR_ERR(p);
  602. switch (err) {
  603. case -EACCES:
  604. case -EKEYEXPIRED:
  605. gss_msg->msg.errno = err;
  606. err = mlen;
  607. break;
  608. case -EFAULT:
  609. case -ENOMEM:
  610. case -EINVAL:
  611. case -ENOSYS:
  612. gss_msg->msg.errno = -EAGAIN;
  613. break;
  614. default:
  615. printk(KERN_CRIT "%s: bad return from "
  616. "gss_fill_context: %zd\n", __func__, err);
  617. BUG();
  618. }
  619. goto err_release_msg;
  620. }
  621. gss_msg->ctx = gss_get_ctx(ctx);
  622. err = mlen;
  623. err_release_msg:
  624. spin_lock(&pipe->lock);
  625. __gss_unhash_msg(gss_msg);
  626. spin_unlock(&pipe->lock);
  627. gss_release_msg(gss_msg);
  628. err_put_ctx:
  629. gss_put_ctx(ctx);
  630. err:
  631. kfree(buf);
  632. out:
  633. dprintk("RPC: %s returning %Zd\n", __func__, err);
  634. return err;
  635. }
  636. static int gss_pipe_open(struct inode *inode, int new_version)
  637. {
  638. int ret = 0;
  639. spin_lock(&pipe_version_lock);
  640. if (pipe_version < 0) {
  641. /* First open of any gss pipe determines the version: */
  642. pipe_version = new_version;
  643. rpc_wake_up(&pipe_version_rpc_waitqueue);
  644. wake_up(&pipe_version_waitqueue);
  645. } else if (pipe_version != new_version) {
  646. /* Trying to open a pipe of a different version */
  647. ret = -EBUSY;
  648. goto out;
  649. }
  650. atomic_inc(&pipe_users);
  651. out:
  652. spin_unlock(&pipe_version_lock);
  653. return ret;
  654. }
  655. static int gss_pipe_open_v0(struct inode *inode)
  656. {
  657. return gss_pipe_open(inode, 0);
  658. }
  659. static int gss_pipe_open_v1(struct inode *inode)
  660. {
  661. return gss_pipe_open(inode, 1);
  662. }
  663. static void
  664. gss_pipe_release(struct inode *inode)
  665. {
  666. struct rpc_pipe *pipe = RPC_I(inode)->pipe;
  667. struct gss_upcall_msg *gss_msg;
  668. restart:
  669. spin_lock(&pipe->lock);
  670. list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
  671. if (!list_empty(&gss_msg->msg.list))
  672. continue;
  673. gss_msg->msg.errno = -EPIPE;
  674. atomic_inc(&gss_msg->count);
  675. __gss_unhash_msg(gss_msg);
  676. spin_unlock(&pipe->lock);
  677. gss_release_msg(gss_msg);
  678. goto restart;
  679. }
  680. spin_unlock(&pipe->lock);
  681. put_pipe_version();
  682. }
  683. static void
  684. gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
  685. {
  686. struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
  687. if (msg->errno < 0) {
  688. dprintk("RPC: %s releasing msg %p\n",
  689. __func__, gss_msg);
  690. atomic_inc(&gss_msg->count);
  691. gss_unhash_msg(gss_msg);
  692. if (msg->errno == -ETIMEDOUT)
  693. warn_gssd();
  694. gss_release_msg(gss_msg);
  695. }
  696. }
  697. static void gss_pipes_dentries_destroy(struct rpc_auth *auth)
  698. {
  699. struct gss_auth *gss_auth;
  700. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  701. if (gss_auth->pipe[0]->dentry)
  702. rpc_unlink(gss_auth->pipe[0]->dentry);
  703. if (gss_auth->pipe[1]->dentry)
  704. rpc_unlink(gss_auth->pipe[1]->dentry);
  705. }
  706. static int gss_pipes_dentries_create(struct rpc_auth *auth)
  707. {
  708. int err;
  709. struct gss_auth *gss_auth;
  710. struct rpc_clnt *clnt;
  711. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  712. clnt = gss_auth->client;
  713. gss_auth->pipe[1]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
  714. "gssd",
  715. clnt, gss_auth->pipe[1]);
  716. if (IS_ERR(gss_auth->pipe[1]->dentry))
  717. return PTR_ERR(gss_auth->pipe[1]->dentry);
  718. gss_auth->pipe[0]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
  719. gss_auth->mech->gm_name,
  720. clnt, gss_auth->pipe[0]);
  721. if (IS_ERR(gss_auth->pipe[0]->dentry)) {
  722. err = PTR_ERR(gss_auth->pipe[0]->dentry);
  723. goto err_unlink_pipe_1;
  724. }
  725. return 0;
  726. err_unlink_pipe_1:
  727. rpc_unlink(gss_auth->pipe[1]->dentry);
  728. return err;
  729. }
  730. static void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt,
  731. struct rpc_auth *auth)
  732. {
  733. struct net *net = rpc_net_ns(clnt);
  734. struct super_block *sb;
  735. sb = rpc_get_sb_net(net);
  736. if (sb) {
  737. if (clnt->cl_dentry)
  738. gss_pipes_dentries_destroy(auth);
  739. rpc_put_sb_net(net);
  740. }
  741. }
  742. static int gss_pipes_dentries_create_net(struct rpc_clnt *clnt,
  743. struct rpc_auth *auth)
  744. {
  745. struct net *net = rpc_net_ns(clnt);
  746. struct super_block *sb;
  747. int err = 0;
  748. sb = rpc_get_sb_net(net);
  749. if (sb) {
  750. if (clnt->cl_dentry)
  751. err = gss_pipes_dentries_create(auth);
  752. rpc_put_sb_net(net);
  753. }
  754. return err;
  755. }
  756. /*
  757. * NOTE: we have the opportunity to use different
  758. * parameters based on the input flavor (which must be a pseudoflavor)
  759. */
  760. static struct rpc_auth *
  761. gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  762. {
  763. struct gss_auth *gss_auth;
  764. struct rpc_auth * auth;
  765. int err = -ENOMEM; /* XXX? */
  766. dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
  767. if (!try_module_get(THIS_MODULE))
  768. return ERR_PTR(err);
  769. if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
  770. goto out_dec;
  771. gss_auth->client = clnt;
  772. err = -EINVAL;
  773. gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
  774. if (!gss_auth->mech) {
  775. printk(KERN_WARNING "%s: Pseudoflavor %d not found!\n",
  776. __func__, flavor);
  777. goto err_free;
  778. }
  779. gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
  780. if (gss_auth->service == 0)
  781. goto err_put_mech;
  782. auth = &gss_auth->rpc_auth;
  783. auth->au_cslack = GSS_CRED_SLACK >> 2;
  784. auth->au_rslack = GSS_VERF_SLACK >> 2;
  785. auth->au_ops = &authgss_ops;
  786. auth->au_flavor = flavor;
  787. atomic_set(&auth->au_count, 1);
  788. kref_init(&gss_auth->kref);
  789. /*
  790. * Note: if we created the old pipe first, then someone who
  791. * examined the directory at the right moment might conclude
  792. * that we supported only the old pipe. So we instead create
  793. * the new pipe first.
  794. */
  795. gss_auth->pipe[1] = rpc_mkpipe_data(&gss_upcall_ops_v1,
  796. RPC_PIPE_WAIT_FOR_OPEN);
  797. if (IS_ERR(gss_auth->pipe[1])) {
  798. err = PTR_ERR(gss_auth->pipe[1]);
  799. goto err_put_mech;
  800. }
  801. gss_auth->pipe[0] = rpc_mkpipe_data(&gss_upcall_ops_v0,
  802. RPC_PIPE_WAIT_FOR_OPEN);
  803. if (IS_ERR(gss_auth->pipe[0])) {
  804. err = PTR_ERR(gss_auth->pipe[0]);
  805. goto err_destroy_pipe_1;
  806. }
  807. err = gss_pipes_dentries_create_net(clnt, auth);
  808. if (err)
  809. goto err_destroy_pipe_0;
  810. err = rpcauth_init_credcache(auth);
  811. if (err)
  812. goto err_unlink_pipes;
  813. return auth;
  814. err_unlink_pipes:
  815. gss_pipes_dentries_destroy_net(clnt, auth);
  816. err_destroy_pipe_0:
  817. rpc_destroy_pipe_data(gss_auth->pipe[0]);
  818. err_destroy_pipe_1:
  819. rpc_destroy_pipe_data(gss_auth->pipe[1]);
  820. err_put_mech:
  821. gss_mech_put(gss_auth->mech);
  822. err_free:
  823. kfree(gss_auth);
  824. out_dec:
  825. module_put(THIS_MODULE);
  826. return ERR_PTR(err);
  827. }
  828. static void
  829. gss_free(struct gss_auth *gss_auth)
  830. {
  831. gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth);
  832. rpc_destroy_pipe_data(gss_auth->pipe[0]);
  833. rpc_destroy_pipe_data(gss_auth->pipe[1]);
  834. gss_mech_put(gss_auth->mech);
  835. kfree(gss_auth);
  836. module_put(THIS_MODULE);
  837. }
  838. static void
  839. gss_free_callback(struct kref *kref)
  840. {
  841. struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
  842. gss_free(gss_auth);
  843. }
  844. static void
  845. gss_destroy(struct rpc_auth *auth)
  846. {
  847. struct gss_auth *gss_auth;
  848. dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
  849. auth, auth->au_flavor);
  850. rpcauth_destroy_credcache(auth);
  851. gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  852. kref_put(&gss_auth->kref, gss_free_callback);
  853. }
  854. /*
  855. * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
  856. * to the server with the GSS control procedure field set to
  857. * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
  858. * all RPCSEC_GSS state associated with that context.
  859. */
  860. static int
  861. gss_destroying_context(struct rpc_cred *cred)
  862. {
  863. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  864. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  865. struct rpc_task *task;
  866. if (gss_cred->gc_ctx == NULL ||
  867. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
  868. return 0;
  869. gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
  870. cred->cr_ops = &gss_nullops;
  871. /* Take a reference to ensure the cred will be destroyed either
  872. * by the RPC call or by the put_rpccred() below */
  873. get_rpccred(cred);
  874. task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
  875. if (!IS_ERR(task))
  876. rpc_put_task(task);
  877. put_rpccred(cred);
  878. return 1;
  879. }
  880. /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
  881. * to create a new cred or context, so they check that things have been
  882. * allocated before freeing them. */
  883. static void
  884. gss_do_free_ctx(struct gss_cl_ctx *ctx)
  885. {
  886. dprintk("RPC: %s\n", __func__);
  887. gss_delete_sec_context(&ctx->gc_gss_ctx);
  888. kfree(ctx->gc_wire_ctx.data);
  889. kfree(ctx);
  890. }
  891. static void
  892. gss_free_ctx_callback(struct rcu_head *head)
  893. {
  894. struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
  895. gss_do_free_ctx(ctx);
  896. }
  897. static void
  898. gss_free_ctx(struct gss_cl_ctx *ctx)
  899. {
  900. call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
  901. }
  902. static void
  903. gss_free_cred(struct gss_cred *gss_cred)
  904. {
  905. dprintk("RPC: %s cred=%p\n", __func__, gss_cred);
  906. kfree(gss_cred);
  907. }
  908. static void
  909. gss_free_cred_callback(struct rcu_head *head)
  910. {
  911. struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
  912. gss_free_cred(gss_cred);
  913. }
  914. static void
  915. gss_destroy_nullcred(struct rpc_cred *cred)
  916. {
  917. struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
  918. struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
  919. struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
  920. RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
  921. call_rcu(&cred->cr_rcu, gss_free_cred_callback);
  922. if (ctx)
  923. gss_put_ctx(ctx);
  924. kref_put(&gss_auth->kref, gss_free_callback);
  925. }
  926. static void
  927. gss_destroy_cred(struct rpc_cred *cred)
  928. {
  929. if (gss_destroying_context(cred))
  930. return;
  931. gss_destroy_nullcred(cred);
  932. }
  933. /*
  934. * Lookup RPCSEC_GSS cred for the current process
  935. */
  936. static struct rpc_cred *
  937. gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  938. {
  939. return rpcauth_lookup_credcache(auth, acred, flags);
  940. }
  941. static struct rpc_cred *
  942. gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  943. {
  944. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  945. struct gss_cred *cred = NULL;
  946. int err = -ENOMEM;
  947. dprintk("RPC: %s for uid %d, flavor %d\n",
  948. __func__, from_kuid(&init_user_ns, acred->uid),
  949. auth->au_flavor);
  950. if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
  951. goto out_err;
  952. rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
  953. /*
  954. * Note: in order to force a call to call_refresh(), we deliberately
  955. * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
  956. */
  957. cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
  958. cred->gc_service = gss_auth->service;
  959. cred->gc_principal = NULL;
  960. if (acred->machine_cred)
  961. cred->gc_principal = acred->principal;
  962. kref_get(&gss_auth->kref);
  963. return &cred->gc_base;
  964. out_err:
  965. dprintk("RPC: %s failed with error %d\n", __func__, err);
  966. return ERR_PTR(err);
  967. }
  968. static int
  969. gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
  970. {
  971. struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
  972. struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
  973. int err;
  974. do {
  975. err = gss_create_upcall(gss_auth, gss_cred);
  976. } while (err == -EAGAIN);
  977. return err;
  978. }
  979. static int
  980. gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
  981. {
  982. struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  983. if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
  984. goto out;
  985. /* Don't match with creds that have expired. */
  986. if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
  987. return 0;
  988. if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
  989. return 0;
  990. out:
  991. if (acred->principal != NULL) {
  992. if (gss_cred->gc_principal == NULL)
  993. return 0;
  994. return strcmp(acred->principal, gss_cred->gc_principal) == 0;
  995. }
  996. if (gss_cred->gc_principal != NULL)
  997. return 0;
  998. return uid_eq(rc->cr_uid, acred->uid);
  999. }
  1000. /*
  1001. * Marshal credentials.
  1002. * Maybe we should keep a cached credential for performance reasons.
  1003. */
  1004. static __be32 *
  1005. gss_marshal(struct rpc_task *task, __be32 *p)
  1006. {
  1007. struct rpc_rqst *req = task->tk_rqstp;
  1008. struct rpc_cred *cred = req->rq_cred;
  1009. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1010. gc_base);
  1011. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1012. __be32 *cred_len;
  1013. u32 maj_stat = 0;
  1014. struct xdr_netobj mic;
  1015. struct kvec iov;
  1016. struct xdr_buf verf_buf;
  1017. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1018. *p++ = htonl(RPC_AUTH_GSS);
  1019. cred_len = p++;
  1020. spin_lock(&ctx->gc_seq_lock);
  1021. req->rq_seqno = ctx->gc_seq++;
  1022. spin_unlock(&ctx->gc_seq_lock);
  1023. *p++ = htonl((u32) RPC_GSS_VERSION);
  1024. *p++ = htonl((u32) ctx->gc_proc);
  1025. *p++ = htonl((u32) req->rq_seqno);
  1026. *p++ = htonl((u32) gss_cred->gc_service);
  1027. p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
  1028. *cred_len = htonl((p - (cred_len + 1)) << 2);
  1029. /* We compute the checksum for the verifier over the xdr-encoded bytes
  1030. * starting with the xid and ending at the end of the credential: */
  1031. iov.iov_base = xprt_skip_transport_header(task->tk_xprt,
  1032. req->rq_snd_buf.head[0].iov_base);
  1033. iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
  1034. xdr_buf_from_iov(&iov, &verf_buf);
  1035. /* set verifier flavor*/
  1036. *p++ = htonl(RPC_AUTH_GSS);
  1037. mic.data = (u8 *)(p + 1);
  1038. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1039. if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
  1040. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1041. } else if (maj_stat != 0) {
  1042. printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
  1043. goto out_put_ctx;
  1044. }
  1045. p = xdr_encode_opaque(p, NULL, mic.len);
  1046. gss_put_ctx(ctx);
  1047. return p;
  1048. out_put_ctx:
  1049. gss_put_ctx(ctx);
  1050. return NULL;
  1051. }
  1052. static int gss_renew_cred(struct rpc_task *task)
  1053. {
  1054. struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
  1055. struct gss_cred *gss_cred = container_of(oldcred,
  1056. struct gss_cred,
  1057. gc_base);
  1058. struct rpc_auth *auth = oldcred->cr_auth;
  1059. struct auth_cred acred = {
  1060. .uid = oldcred->cr_uid,
  1061. .principal = gss_cred->gc_principal,
  1062. .machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
  1063. };
  1064. struct rpc_cred *new;
  1065. new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
  1066. if (IS_ERR(new))
  1067. return PTR_ERR(new);
  1068. task->tk_rqstp->rq_cred = new;
  1069. put_rpccred(oldcred);
  1070. return 0;
  1071. }
  1072. static int gss_cred_is_negative_entry(struct rpc_cred *cred)
  1073. {
  1074. if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
  1075. unsigned long now = jiffies;
  1076. unsigned long begin, expire;
  1077. struct gss_cred *gss_cred;
  1078. gss_cred = container_of(cred, struct gss_cred, gc_base);
  1079. begin = gss_cred->gc_upcall_timestamp;
  1080. expire = begin + gss_expired_cred_retry_delay * HZ;
  1081. if (time_in_range_open(now, begin, expire))
  1082. return 1;
  1083. }
  1084. return 0;
  1085. }
  1086. /*
  1087. * Refresh credentials. XXX - finish
  1088. */
  1089. static int
  1090. gss_refresh(struct rpc_task *task)
  1091. {
  1092. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1093. int ret = 0;
  1094. if (gss_cred_is_negative_entry(cred))
  1095. return -EKEYEXPIRED;
  1096. if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  1097. !test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
  1098. ret = gss_renew_cred(task);
  1099. if (ret < 0)
  1100. goto out;
  1101. cred = task->tk_rqstp->rq_cred;
  1102. }
  1103. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
  1104. ret = gss_refresh_upcall(task);
  1105. out:
  1106. return ret;
  1107. }
  1108. /* Dummy refresh routine: used only when destroying the context */
  1109. static int
  1110. gss_refresh_null(struct rpc_task *task)
  1111. {
  1112. return -EACCES;
  1113. }
  1114. static __be32 *
  1115. gss_validate(struct rpc_task *task, __be32 *p)
  1116. {
  1117. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1118. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1119. __be32 seq;
  1120. struct kvec iov;
  1121. struct xdr_buf verf_buf;
  1122. struct xdr_netobj mic;
  1123. u32 flav,len;
  1124. u32 maj_stat;
  1125. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1126. flav = ntohl(*p++);
  1127. if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
  1128. goto out_bad;
  1129. if (flav != RPC_AUTH_GSS)
  1130. goto out_bad;
  1131. seq = htonl(task->tk_rqstp->rq_seqno);
  1132. iov.iov_base = &seq;
  1133. iov.iov_len = sizeof(seq);
  1134. xdr_buf_from_iov(&iov, &verf_buf);
  1135. mic.data = (u8 *)p;
  1136. mic.len = len;
  1137. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
  1138. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1139. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1140. if (maj_stat) {
  1141. dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
  1142. task->tk_pid, __func__, maj_stat);
  1143. goto out_bad;
  1144. }
  1145. /* We leave it to unwrap to calculate au_rslack. For now we just
  1146. * calculate the length of the verifier: */
  1147. cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
  1148. gss_put_ctx(ctx);
  1149. dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
  1150. task->tk_pid, __func__);
  1151. return p + XDR_QUADLEN(len);
  1152. out_bad:
  1153. gss_put_ctx(ctx);
  1154. dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
  1155. return NULL;
  1156. }
  1157. static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  1158. __be32 *p, void *obj)
  1159. {
  1160. struct xdr_stream xdr;
  1161. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
  1162. encode(rqstp, &xdr, obj);
  1163. }
  1164. static inline int
  1165. gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1166. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1167. __be32 *p, void *obj)
  1168. {
  1169. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1170. struct xdr_buf integ_buf;
  1171. __be32 *integ_len = NULL;
  1172. struct xdr_netobj mic;
  1173. u32 offset;
  1174. __be32 *q;
  1175. struct kvec *iov;
  1176. u32 maj_stat = 0;
  1177. int status = -EIO;
  1178. integ_len = p++;
  1179. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1180. *p++ = htonl(rqstp->rq_seqno);
  1181. gss_wrap_req_encode(encode, rqstp, p, obj);
  1182. if (xdr_buf_subsegment(snd_buf, &integ_buf,
  1183. offset, snd_buf->len - offset))
  1184. return status;
  1185. *integ_len = htonl(integ_buf.len);
  1186. /* guess whether we're in the head or the tail: */
  1187. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1188. iov = snd_buf->tail;
  1189. else
  1190. iov = snd_buf->head;
  1191. p = iov->iov_base + iov->iov_len;
  1192. mic.data = (u8 *)(p + 1);
  1193. maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1194. status = -EIO; /* XXX? */
  1195. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1196. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1197. else if (maj_stat)
  1198. return status;
  1199. q = xdr_encode_opaque(p, NULL, mic.len);
  1200. offset = (u8 *)q - (u8 *)p;
  1201. iov->iov_len += offset;
  1202. snd_buf->len += offset;
  1203. return 0;
  1204. }
  1205. static void
  1206. priv_release_snd_buf(struct rpc_rqst *rqstp)
  1207. {
  1208. int i;
  1209. for (i=0; i < rqstp->rq_enc_pages_num; i++)
  1210. __free_page(rqstp->rq_enc_pages[i]);
  1211. kfree(rqstp->rq_enc_pages);
  1212. }
  1213. static int
  1214. alloc_enc_pages(struct rpc_rqst *rqstp)
  1215. {
  1216. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1217. int first, last, i;
  1218. if (snd_buf->page_len == 0) {
  1219. rqstp->rq_enc_pages_num = 0;
  1220. return 0;
  1221. }
  1222. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1223. last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
  1224. rqstp->rq_enc_pages_num = last - first + 1 + 1;
  1225. rqstp->rq_enc_pages
  1226. = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
  1227. GFP_NOFS);
  1228. if (!rqstp->rq_enc_pages)
  1229. goto out;
  1230. for (i=0; i < rqstp->rq_enc_pages_num; i++) {
  1231. rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
  1232. if (rqstp->rq_enc_pages[i] == NULL)
  1233. goto out_free;
  1234. }
  1235. rqstp->rq_release_snd_buf = priv_release_snd_buf;
  1236. return 0;
  1237. out_free:
  1238. rqstp->rq_enc_pages_num = i;
  1239. priv_release_snd_buf(rqstp);
  1240. out:
  1241. return -EAGAIN;
  1242. }
  1243. static inline int
  1244. gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1245. kxdreproc_t encode, struct rpc_rqst *rqstp,
  1246. __be32 *p, void *obj)
  1247. {
  1248. struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
  1249. u32 offset;
  1250. u32 maj_stat;
  1251. int status;
  1252. __be32 *opaque_len;
  1253. struct page **inpages;
  1254. int first;
  1255. int pad;
  1256. struct kvec *iov;
  1257. char *tmp;
  1258. opaque_len = p++;
  1259. offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
  1260. *p++ = htonl(rqstp->rq_seqno);
  1261. gss_wrap_req_encode(encode, rqstp, p, obj);
  1262. status = alloc_enc_pages(rqstp);
  1263. if (status)
  1264. return status;
  1265. first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
  1266. inpages = snd_buf->pages + first;
  1267. snd_buf->pages = rqstp->rq_enc_pages;
  1268. snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
  1269. /*
  1270. * Give the tail its own page, in case we need extra space in the
  1271. * head when wrapping:
  1272. *
  1273. * call_allocate() allocates twice the slack space required
  1274. * by the authentication flavor to rq_callsize.
  1275. * For GSS, slack is GSS_CRED_SLACK.
  1276. */
  1277. if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
  1278. tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
  1279. memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
  1280. snd_buf->tail[0].iov_base = tmp;
  1281. }
  1282. maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
  1283. /* slack space should prevent this ever happening: */
  1284. BUG_ON(snd_buf->len > snd_buf->buflen);
  1285. status = -EIO;
  1286. /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
  1287. * done anyway, so it's safe to put the request on the wire: */
  1288. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1289. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1290. else if (maj_stat)
  1291. return status;
  1292. *opaque_len = htonl(snd_buf->len - offset);
  1293. /* guess whether we're in the head or the tail: */
  1294. if (snd_buf->page_len || snd_buf->tail[0].iov_len)
  1295. iov = snd_buf->tail;
  1296. else
  1297. iov = snd_buf->head;
  1298. p = iov->iov_base + iov->iov_len;
  1299. pad = 3 - ((snd_buf->len - offset - 1) & 3);
  1300. memset(p, 0, pad);
  1301. iov->iov_len += pad;
  1302. snd_buf->len += pad;
  1303. return 0;
  1304. }
  1305. static int
  1306. gss_wrap_req(struct rpc_task *task,
  1307. kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
  1308. {
  1309. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1310. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1311. gc_base);
  1312. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1313. int status = -EIO;
  1314. dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
  1315. if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
  1316. /* The spec seems a little ambiguous here, but I think that not
  1317. * wrapping context destruction requests makes the most sense.
  1318. */
  1319. gss_wrap_req_encode(encode, rqstp, p, obj);
  1320. status = 0;
  1321. goto out;
  1322. }
  1323. switch (gss_cred->gc_service) {
  1324. case RPC_GSS_SVC_NONE:
  1325. gss_wrap_req_encode(encode, rqstp, p, obj);
  1326. status = 0;
  1327. break;
  1328. case RPC_GSS_SVC_INTEGRITY:
  1329. status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
  1330. break;
  1331. case RPC_GSS_SVC_PRIVACY:
  1332. status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
  1333. break;
  1334. }
  1335. out:
  1336. gss_put_ctx(ctx);
  1337. dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
  1338. return status;
  1339. }
  1340. static inline int
  1341. gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1342. struct rpc_rqst *rqstp, __be32 **p)
  1343. {
  1344. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1345. struct xdr_buf integ_buf;
  1346. struct xdr_netobj mic;
  1347. u32 data_offset, mic_offset;
  1348. u32 integ_len;
  1349. u32 maj_stat;
  1350. int status = -EIO;
  1351. integ_len = ntohl(*(*p)++);
  1352. if (integ_len & 3)
  1353. return status;
  1354. data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1355. mic_offset = integ_len + data_offset;
  1356. if (mic_offset > rcv_buf->len)
  1357. return status;
  1358. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1359. return status;
  1360. if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
  1361. mic_offset - data_offset))
  1362. return status;
  1363. if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
  1364. return status;
  1365. maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
  1366. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1367. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1368. if (maj_stat != GSS_S_COMPLETE)
  1369. return status;
  1370. return 0;
  1371. }
  1372. static inline int
  1373. gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
  1374. struct rpc_rqst *rqstp, __be32 **p)
  1375. {
  1376. struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
  1377. u32 offset;
  1378. u32 opaque_len;
  1379. u32 maj_stat;
  1380. int status = -EIO;
  1381. opaque_len = ntohl(*(*p)++);
  1382. offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
  1383. if (offset + opaque_len > rcv_buf->len)
  1384. return status;
  1385. /* remove padding: */
  1386. rcv_buf->len = offset + opaque_len;
  1387. maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
  1388. if (maj_stat == GSS_S_CONTEXT_EXPIRED)
  1389. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  1390. if (maj_stat != GSS_S_COMPLETE)
  1391. return status;
  1392. if (ntohl(*(*p)++) != rqstp->rq_seqno)
  1393. return status;
  1394. return 0;
  1395. }
  1396. static int
  1397. gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  1398. __be32 *p, void *obj)
  1399. {
  1400. struct xdr_stream xdr;
  1401. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  1402. return decode(rqstp, &xdr, obj);
  1403. }
  1404. static int
  1405. gss_unwrap_resp(struct rpc_task *task,
  1406. kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
  1407. {
  1408. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  1409. struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
  1410. gc_base);
  1411. struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
  1412. __be32 *savedp = p;
  1413. struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
  1414. int savedlen = head->iov_len;
  1415. int status = -EIO;
  1416. if (ctx->gc_proc != RPC_GSS_PROC_DATA)
  1417. goto out_decode;
  1418. switch (gss_cred->gc_service) {
  1419. case RPC_GSS_SVC_NONE:
  1420. break;
  1421. case RPC_GSS_SVC_INTEGRITY:
  1422. status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
  1423. if (status)
  1424. goto out;
  1425. break;
  1426. case RPC_GSS_SVC_PRIVACY:
  1427. status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
  1428. if (status)
  1429. goto out;
  1430. break;
  1431. }
  1432. /* take into account extra slack for integrity and privacy cases: */
  1433. cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
  1434. + (savedlen - head->iov_len);
  1435. out_decode:
  1436. status = gss_unwrap_req_decode(decode, rqstp, p, obj);
  1437. out:
  1438. gss_put_ctx(ctx);
  1439. dprintk("RPC: %5u %s returning %d\n",
  1440. task->tk_pid, __func__, status);
  1441. return status;
  1442. }
  1443. static const struct rpc_authops authgss_ops = {
  1444. .owner = THIS_MODULE,
  1445. .au_flavor = RPC_AUTH_GSS,
  1446. .au_name = "RPCSEC_GSS",
  1447. .create = gss_create,
  1448. .destroy = gss_destroy,
  1449. .lookup_cred = gss_lookup_cred,
  1450. .crcreate = gss_create_cred,
  1451. .pipes_create = gss_pipes_dentries_create,
  1452. .pipes_destroy = gss_pipes_dentries_destroy,
  1453. .list_pseudoflavors = gss_mech_list_pseudoflavors,
  1454. };
  1455. static const struct rpc_credops gss_credops = {
  1456. .cr_name = "AUTH_GSS",
  1457. .crdestroy = gss_destroy_cred,
  1458. .cr_init = gss_cred_init,
  1459. .crbind = rpcauth_generic_bind_cred,
  1460. .crmatch = gss_match,
  1461. .crmarshal = gss_marshal,
  1462. .crrefresh = gss_refresh,
  1463. .crvalidate = gss_validate,
  1464. .crwrap_req = gss_wrap_req,
  1465. .crunwrap_resp = gss_unwrap_resp,
  1466. };
  1467. static const struct rpc_credops gss_nullops = {
  1468. .cr_name = "AUTH_GSS",
  1469. .crdestroy = gss_destroy_nullcred,
  1470. .crbind = rpcauth_generic_bind_cred,
  1471. .crmatch = gss_match,
  1472. .crmarshal = gss_marshal,
  1473. .crrefresh = gss_refresh_null,
  1474. .crvalidate = gss_validate,
  1475. .crwrap_req = gss_wrap_req,
  1476. .crunwrap_resp = gss_unwrap_resp,
  1477. };
  1478. static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
  1479. .upcall = rpc_pipe_generic_upcall,
  1480. .downcall = gss_pipe_downcall,
  1481. .destroy_msg = gss_pipe_destroy_msg,
  1482. .open_pipe = gss_pipe_open_v0,
  1483. .release_pipe = gss_pipe_release,
  1484. };
  1485. static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
  1486. .upcall = rpc_pipe_generic_upcall,
  1487. .downcall = gss_pipe_downcall,
  1488. .destroy_msg = gss_pipe_destroy_msg,
  1489. .open_pipe = gss_pipe_open_v1,
  1490. .release_pipe = gss_pipe_release,
  1491. };
  1492. static __net_init int rpcsec_gss_init_net(struct net *net)
  1493. {
  1494. return gss_svc_init_net(net);
  1495. }
  1496. static __net_exit void rpcsec_gss_exit_net(struct net *net)
  1497. {
  1498. gss_svc_shutdown_net(net);
  1499. }
  1500. static struct pernet_operations rpcsec_gss_net_ops = {
  1501. .init = rpcsec_gss_init_net,
  1502. .exit = rpcsec_gss_exit_net,
  1503. };
  1504. /*
  1505. * Initialize RPCSEC_GSS module
  1506. */
  1507. static int __init init_rpcsec_gss(void)
  1508. {
  1509. int err = 0;
  1510. err = rpcauth_register(&authgss_ops);
  1511. if (err)
  1512. goto out;
  1513. err = gss_svc_init();
  1514. if (err)
  1515. goto out_unregister;
  1516. err = register_pernet_subsys(&rpcsec_gss_net_ops);
  1517. if (err)
  1518. goto out_svc_exit;
  1519. rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
  1520. return 0;
  1521. out_svc_exit:
  1522. gss_svc_shutdown();
  1523. out_unregister:
  1524. rpcauth_unregister(&authgss_ops);
  1525. out:
  1526. return err;
  1527. }
  1528. static void __exit exit_rpcsec_gss(void)
  1529. {
  1530. unregister_pernet_subsys(&rpcsec_gss_net_ops);
  1531. gss_svc_shutdown();
  1532. rpcauth_unregister(&authgss_ops);
  1533. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1534. }
  1535. MODULE_LICENSE("GPL");
  1536. module_param_named(expired_cred_retry_delay,
  1537. gss_expired_cred_retry_delay,
  1538. uint, 0644);
  1539. MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
  1540. "the RPC engine retries an expired credential");
  1541. module_init(init_rpcsec_gss)
  1542. module_exit(exit_rpcsec_gss)