1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/lockd/svc.c
4 *
5 * This is the central lockd service.
6 *
7 * FIXME: Separate the lockd NFS server functionality from the lockd NFS
8 * client functionality. Oh why didn't Sun create two separate
9 * services in the first place?
10 *
11 * Authors: Olaf Kirch (okir@monad.swb.de)
12 *
13 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
14 */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/sysctl.h>
19 #include <linux/moduleparam.h>
20
21 #include <linux/sched/signal.h>
22 #include <linux/errno.h>
23 #include <linux/in.h>
24 #include <linux/uio.h>
25 #include <linux/smp.h>
26 #include <linux/mutex.h>
27 #include <linux/freezer.h>
28 #include <linux/inetdevice.h>
29
30 #include <linux/sunrpc/types.h>
31 #include <linux/sunrpc/stats.h>
32 #include <linux/sunrpc/clnt.h>
33 #include <linux/sunrpc/svc.h>
34 #include <linux/sunrpc/svcsock.h>
35 #include <linux/sunrpc/svc_xprt.h>
36 #include <net/ip.h>
37 #include <net/addrconf.h>
38 #include <net/ipv6.h>
39 #include <linux/nfs.h>
40
41 #include "lockd.h"
42 #include "netns.h"
43 #include "procfs.h"
44 #include "netlink.h"
45
46 #define NLMDBG_FACILITY NLMDBG_SVC
47
48 static struct svc_program nlmsvc_program;
49
50 const struct nlmsvc_binding __rcu *nlmsvc_ops;
51 EXPORT_SYMBOL_GPL(nlmsvc_ops);
52
53 static DEFINE_MUTEX(nlmsvc_mutex);
54 static unsigned int nlmsvc_users;
55 static struct svc_serv *nlmsvc_serv;
56
nlmsvc_request_retry(struct timer_list * tl)57 static void nlmsvc_request_retry(struct timer_list *tl)
58 {
59 svc_wake_up(nlmsvc_serv);
60 }
61 DEFINE_TIMER(nlmsvc_retry, nlmsvc_request_retry);
62
63 unsigned int lockd_net_id;
64
65 /*
66 * These can be set at insmod time (useful for NFS as root filesystem),
67 * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
68 */
69 static unsigned long nlm_grace_period;
70 unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
71 static int nlm_udpport, nlm_tcpport;
72
73 /*
74 * Constants needed for the sysctl interface.
75 */
76 static const unsigned long nlm_grace_period_min = 0;
77 static const unsigned long nlm_grace_period_max = 240;
78 static const unsigned long nlm_timeout_min = 3;
79 static const unsigned long nlm_timeout_max = 20;
80
81 #ifdef CONFIG_SYSCTL
82 static const int nlm_port_min = 0, nlm_port_max = 65535;
83 static struct ctl_table_header * nlm_sysctl_table;
84 #endif
85
get_lockd_grace_period(struct net * net)86 static unsigned long get_lockd_grace_period(struct net *net)
87 {
88 struct lockd_net *ln = net_generic(net, lockd_net_id);
89
90 /* Return the net-ns specific grace period, if there is one */
91 if (ln->gracetime)
92 return ln->gracetime * HZ;
93
94 /* Note: nlm_timeout should always be nonzero */
95 if (nlm_grace_period)
96 return roundup(nlm_grace_period, nlm_timeout) * HZ;
97 else
98 return nlm_timeout * 5 * HZ;
99 }
100
grace_ender(struct work_struct * grace)101 static void grace_ender(struct work_struct *grace)
102 {
103 struct delayed_work *dwork = to_delayed_work(grace);
104 struct lockd_net *ln = container_of(dwork, struct lockd_net,
105 grace_period_end);
106
107 locks_end_grace(&ln->lockd_manager);
108 }
109
set_grace_period(struct net * net)110 static void set_grace_period(struct net *net)
111 {
112 unsigned long grace_period = get_lockd_grace_period(net);
113 struct lockd_net *ln = net_generic(net, lockd_net_id);
114
115 locks_start_grace(net, &ln->lockd_manager);
116 cancel_delayed_work_sync(&ln->grace_period_end);
117 schedule_delayed_work(&ln->grace_period_end, grace_period);
118 }
119
120 /*
121 * This is the lockd kernel thread
122 */
123 static int
lockd(void * vrqstp)124 lockd(void *vrqstp)
125 {
126 struct svc_rqst *rqstp = vrqstp;
127 struct net *net = &init_net;
128 struct lockd_net *ln = net_generic(net, lockd_net_id);
129
130 svc_thread_init_status(rqstp, 0);
131
132 /* try_to_freeze() is called from svc_recv() */
133 set_freezable();
134
135 dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
136
137 /*
138 * The main request loop. We don't terminate until the last
139 * NFS mount or NFS daemon has gone away.
140 */
141 while (!svc_thread_should_stop(rqstp)) {
142 nlmsvc_retry_blocked(rqstp);
143 svc_recv(rqstp, 0);
144 }
145 if (rcu_access_pointer(nlmsvc_ops))
146 nlmsvc_invalidate_all();
147 nlm_shutdown_hosts();
148 cancel_delayed_work_sync(&ln->grace_period_end);
149 locks_end_grace(&ln->lockd_manager);
150
151 dprintk("lockd_down: service stopped\n");
152
153 svc_exit_thread(rqstp);
154 return 0;
155 }
156
create_lockd_listener(struct svc_serv * serv,const char * name,struct net * net,const int family,const unsigned short port,const struct cred * cred)157 static int create_lockd_listener(struct svc_serv *serv, const char *name,
158 struct net *net, const int family,
159 const unsigned short port,
160 const struct cred *cred)
161 {
162 struct svc_xprt *xprt;
163
164 xprt = svc_find_xprt(serv, name, net, family, 0);
165 if (xprt == NULL)
166 return svc_xprt_create(serv, name, net, family, port,
167 SVC_SOCK_DEFAULTS, cred);
168 svc_xprt_put(xprt);
169 return 0;
170 }
171
create_lockd_family(struct svc_serv * serv,struct net * net,const int family,const struct cred * cred)172 static int create_lockd_family(struct svc_serv *serv, struct net *net,
173 const int family, const struct cred *cred)
174 {
175 struct lockd_net *ln = net_generic(net, lockd_net_id);
176 int err;
177
178 err = create_lockd_listener(serv, "udp", net, family,
179 ln->udp_port ? ln->udp_port : nlm_udpport, cred);
180 if (err < 0)
181 return err;
182
183 return create_lockd_listener(serv, "tcp", net, family,
184 ln->tcp_port ? ln->tcp_port : nlm_tcpport, cred);
185 }
186
187 /*
188 * Ensure there are active UDP and TCP listeners for lockd.
189 *
190 * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
191 * local services (such as rpc.statd) still require UDP, and
192 * some NFS servers do not yet support NLM over TCP.
193 *
194 * Returns zero if all listeners are available; otherwise a
195 * negative errno value is returned.
196 */
make_socks(struct svc_serv * serv,struct net * net,const struct cred * cred)197 static int make_socks(struct svc_serv *serv, struct net *net,
198 const struct cred *cred)
199 {
200 static int warned;
201 int err;
202
203 err = create_lockd_family(serv, net, PF_INET, cred);
204 if (err < 0)
205 goto out_err;
206
207 err = create_lockd_family(serv, net, PF_INET6, cred);
208 if (err < 0 && err != -EAFNOSUPPORT)
209 goto out_err;
210
211 warned = 0;
212 return 0;
213
214 out_err:
215 if (warned++ == 0)
216 printk(KERN_WARNING
217 "lockd_up: makesock failed, error=%d\n", err);
218 svc_xprt_destroy_all(serv, net, true);
219 return err;
220 }
221
lockd_up_net(struct svc_serv * serv,struct net * net,const struct cred * cred)222 static int lockd_up_net(struct svc_serv *serv, struct net *net,
223 const struct cred *cred)
224 {
225 struct lockd_net *ln = net_generic(net, lockd_net_id);
226 int error;
227
228 if (ln->nlmsvc_users++)
229 return 0;
230
231 error = svc_bind(serv, net);
232 if (error)
233 goto err_bind;
234
235 error = make_socks(serv, net, cred);
236 if (error < 0)
237 goto err_bind;
238 set_grace_period(net);
239 dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
240 return 0;
241
242 err_bind:
243 ln->nlmsvc_users--;
244 return error;
245 }
246
lockd_down_net(struct svc_serv * serv,struct net * net)247 static void lockd_down_net(struct svc_serv *serv, struct net *net)
248 {
249 struct lockd_net *ln = net_generic(net, lockd_net_id);
250
251 if (ln->nlmsvc_users) {
252 if (--ln->nlmsvc_users == 0) {
253 nlm_shutdown_hosts_net(net);
254 cancel_delayed_work_sync(&ln->grace_period_end);
255 locks_end_grace(&ln->lockd_manager);
256 svc_xprt_destroy_all(serv, net, true);
257 }
258 } else {
259 pr_err("%s: no users! net=%x\n",
260 __func__, net->ns.inum);
261 BUG();
262 }
263 }
264
lockd_inetaddr_event(struct notifier_block * this,unsigned long event,void * ptr)265 static int lockd_inetaddr_event(struct notifier_block *this,
266 unsigned long event, void *ptr)
267 {
268 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
269 struct sockaddr_in sin;
270
271 if (event != NETDEV_DOWN)
272 goto out;
273
274 if (nlmsvc_serv) {
275 dprintk("lockd_inetaddr_event: removed %pI4\n",
276 &ifa->ifa_local);
277 sin.sin_family = AF_INET;
278 sin.sin_addr.s_addr = ifa->ifa_local;
279 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin);
280 }
281
282 out:
283 return NOTIFY_DONE;
284 }
285
286 static struct notifier_block lockd_inetaddr_notifier = {
287 .notifier_call = lockd_inetaddr_event,
288 };
289
290 #if IS_ENABLED(CONFIG_IPV6)
lockd_inet6addr_event(struct notifier_block * this,unsigned long event,void * ptr)291 static int lockd_inet6addr_event(struct notifier_block *this,
292 unsigned long event, void *ptr)
293 {
294 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
295 struct sockaddr_in6 sin6;
296
297 if (event != NETDEV_DOWN)
298 goto out;
299
300 if (nlmsvc_serv) {
301 dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
302 sin6.sin6_family = AF_INET6;
303 sin6.sin6_addr = ifa->addr;
304 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
305 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
306 svc_age_temp_xprts_now(nlmsvc_serv, (struct sockaddr *)&sin6);
307 }
308
309 out:
310 return NOTIFY_DONE;
311 }
312
313 static struct notifier_block lockd_inet6addr_notifier = {
314 .notifier_call = lockd_inet6addr_event,
315 };
316 #endif
317
lockd_get(void)318 static int lockd_get(void)
319 {
320 struct svc_serv *serv;
321 unsigned int bufsize;
322 int error;
323
324 if (nlmsvc_serv) {
325 nlmsvc_users++;
326 return 0;
327 }
328
329 /*
330 * Sanity check: if there's no pid,
331 * we should be the first user ...
332 */
333 if (nlmsvc_users)
334 printk(KERN_WARNING
335 "lockd_up: no pid, %d users??\n", nlmsvc_users);
336
337 #ifdef CONFIG_LOCKD_V4
338 bufsize = 1024 + max3(nlmsvc_version1.vs_xdrsize,
339 nlmsvc_version3.vs_xdrsize,
340 nlmsvc_version4.vs_xdrsize);
341 #else
342 bufsize = 1024 + max(nlmsvc_version1.vs_xdrsize,
343 nlmsvc_version3.vs_xdrsize);
344 #endif
345 serv = svc_create(&nlmsvc_program, bufsize, lockd);
346 if (!serv) {
347 printk(KERN_WARNING "lockd_up: create service failed\n");
348 return -ENOMEM;
349 }
350
351 error = svc_set_num_threads(serv, 0, 1);
352 if (error < 0) {
353 svc_destroy(&serv);
354 return error;
355 }
356
357 nlmsvc_serv = serv;
358 register_inetaddr_notifier(&lockd_inetaddr_notifier);
359 #if IS_ENABLED(CONFIG_IPV6)
360 register_inet6addr_notifier(&lockd_inet6addr_notifier);
361 #endif
362 dprintk("lockd_up: service created\n");
363 nlmsvc_users++;
364 return 0;
365 }
366
lockd_put(void)367 static void lockd_put(void)
368 {
369 if (WARN(nlmsvc_users <= 0, "lockd_down: no users!\n"))
370 return;
371 if (--nlmsvc_users)
372 return;
373
374 unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
375 #if IS_ENABLED(CONFIG_IPV6)
376 unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
377 #endif
378
379 svc_set_num_threads(nlmsvc_serv, 0, 0);
380 timer_delete_sync(&nlmsvc_retry);
381 svc_destroy(&nlmsvc_serv);
382 dprintk("lockd_down: service destroyed\n");
383 }
384
385 /*
386 * Bring up the lockd process if it's not already up.
387 */
lockd_up(struct net * net,const struct cred * cred)388 int lockd_up(struct net *net, const struct cred *cred)
389 {
390 int error;
391
392 mutex_lock(&nlmsvc_mutex);
393
394 error = lockd_get();
395 if (error)
396 goto err;
397
398 error = lockd_up_net(nlmsvc_serv, net, cred);
399 if (error < 0) {
400 lockd_put();
401 goto err;
402 }
403
404 err:
405 mutex_unlock(&nlmsvc_mutex);
406 return error;
407 }
408 EXPORT_SYMBOL_GPL(lockd_up);
409
410 /*
411 * Decrement the user count and bring down lockd if we're the last.
412 */
413 void
lockd_down(struct net * net)414 lockd_down(struct net *net)
415 {
416 mutex_lock(&nlmsvc_mutex);
417 lockd_down_net(nlmsvc_serv, net);
418 lockd_put();
419 mutex_unlock(&nlmsvc_mutex);
420 }
421 EXPORT_SYMBOL_GPL(lockd_down);
422
423 #ifdef CONFIG_SYSCTL
424
425 /*
426 * Sysctl parameters (same as module parameters, different interface).
427 */
428
429 static const struct ctl_table nlm_sysctls[] = {
430 {
431 .procname = "nlm_grace_period",
432 .data = &nlm_grace_period,
433 .maxlen = sizeof(unsigned long),
434 .mode = 0644,
435 .proc_handler = proc_doulongvec_minmax,
436 .extra1 = (unsigned long *) &nlm_grace_period_min,
437 .extra2 = (unsigned long *) &nlm_grace_period_max,
438 },
439 {
440 .procname = "nlm_timeout",
441 .data = &nlm_timeout,
442 .maxlen = sizeof(unsigned long),
443 .mode = 0644,
444 .proc_handler = proc_doulongvec_minmax,
445 .extra1 = (unsigned long *) &nlm_timeout_min,
446 .extra2 = (unsigned long *) &nlm_timeout_max,
447 },
448 {
449 .procname = "nlm_udpport",
450 .data = &nlm_udpport,
451 .maxlen = sizeof(int),
452 .mode = 0644,
453 .proc_handler = proc_dointvec_minmax,
454 .extra1 = (int *) &nlm_port_min,
455 .extra2 = (int *) &nlm_port_max,
456 },
457 {
458 .procname = "nlm_tcpport",
459 .data = &nlm_tcpport,
460 .maxlen = sizeof(int),
461 .mode = 0644,
462 .proc_handler = proc_dointvec_minmax,
463 .extra1 = (int *) &nlm_port_min,
464 .extra2 = (int *) &nlm_port_max,
465 },
466 {
467 .procname = "nsm_use_hostnames",
468 .data = &nsm_use_hostnames,
469 .maxlen = sizeof(bool),
470 .mode = 0644,
471 .proc_handler = proc_dobool,
472 },
473 {
474 .procname = "nsm_local_state",
475 .data = &nsm_local_state,
476 .maxlen = sizeof(nsm_local_state),
477 .mode = 0644,
478 .proc_handler = proc_douintvec,
479 .extra1 = SYSCTL_ZERO,
480 },
481 };
482
483 #endif /* CONFIG_SYSCTL */
484
485 /*
486 * Module (and sysfs) parameters.
487 */
488
489 #define param_set_min_max(name, type, which_strtol, min, max) \
490 static int param_set_##name(const char *val, const struct kernel_param *kp) \
491 { \
492 char *endp; \
493 __typeof__(type) num = which_strtol(val, &endp, 0); \
494 if (endp == val || *endp || num < (min) || num > (max)) \
495 return -EINVAL; \
496 *((type *) kp->arg) = num; \
497 return 0; \
498 }
499
is_callback(u32 proc)500 static inline int is_callback(u32 proc)
501 {
502 return proc == NLMPROC_GRANTED
503 || proc == NLMPROC_GRANTED_MSG
504 || proc == NLMPROC_TEST_RES
505 || proc == NLMPROC_LOCK_RES
506 || proc == NLMPROC_CANCEL_RES
507 || proc == NLMPROC_UNLOCK_RES
508 || proc == NLMPROC_NSM_NOTIFY;
509 }
510
511
lockd_authenticate(struct svc_rqst * rqstp)512 static enum svc_auth_status lockd_authenticate(struct svc_rqst *rqstp)
513 {
514 rqstp->rq_client = NULL;
515 switch (rqstp->rq_authop->flavour) {
516 case RPC_AUTH_NULL:
517 case RPC_AUTH_UNIX:
518 rqstp->rq_auth_stat = rpc_auth_ok;
519 if (rqstp->rq_proc == 0)
520 return SVC_OK;
521 if (is_callback(rqstp->rq_proc)) {
522 /* Leave it to individual procedures to
523 * call nlmsvc_lookup_host(rqstp)
524 */
525 return SVC_OK;
526 }
527 return svc_set_client(rqstp);
528 }
529 rqstp->rq_auth_stat = rpc_autherr_badcred;
530 return SVC_DENIED;
531 }
532
533
534 param_set_min_max(port, int, simple_strtol, 0, 65535)
535 param_set_min_max(grace_period, unsigned long, simple_strtoul,
536 nlm_grace_period_min, nlm_grace_period_max)
537 param_set_min_max(timeout, unsigned long, simple_strtoul,
538 nlm_timeout_min, nlm_timeout_max)
539
540 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
541 MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
542 MODULE_LICENSE("GPL");
543
544 module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
545 &nlm_grace_period, 0644);
546 module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
547 &nlm_timeout, 0644);
548 module_param_call(nlm_udpport, param_set_port, param_get_int,
549 &nlm_udpport, 0644);
550 module_param_call(nlm_tcpport, param_set_port, param_get_int,
551 &nlm_tcpport, 0644);
552 module_param(nsm_use_hostnames, bool, 0644);
553
lockd_init_net(struct net * net)554 static int lockd_init_net(struct net *net)
555 {
556 struct lockd_net *ln = net_generic(net, lockd_net_id);
557
558 INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
559 INIT_LIST_HEAD(&ln->lockd_manager.list);
560 ln->lockd_manager.block_opens = false;
561 INIT_LIST_HEAD(&ln->nsm_handles);
562 return 0;
563 }
564
lockd_exit_net(struct net * net)565 static void lockd_exit_net(struct net *net)
566 {
567 struct lockd_net *ln = net_generic(net, lockd_net_id);
568
569 WARN_ONCE(!list_empty(&ln->lockd_manager.list),
570 "net %x %s: lockd_manager.list is not empty\n",
571 net->ns.inum, __func__);
572 WARN_ONCE(!list_empty(&ln->nsm_handles),
573 "net %x %s: nsm_handles list is not empty\n",
574 net->ns.inum, __func__);
575 WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
576 "net %x %s: grace_period_end was not cancelled\n",
577 net->ns.inum, __func__);
578 }
579
580 static struct pernet_operations lockd_net_ops = {
581 .init = lockd_init_net,
582 .exit = lockd_exit_net,
583 .id = &lockd_net_id,
584 .size = sizeof(struct lockd_net),
585 };
586
587
588 /*
589 * Initialising and terminating the module.
590 */
591
init_nlm(void)592 static int __init init_nlm(void)
593 {
594 int err;
595
596 #ifdef CONFIG_SYSCTL
597 err = -ENOMEM;
598 nlm_sysctl_table = register_sysctl("fs/nfs", nlm_sysctls);
599 if (nlm_sysctl_table == NULL)
600 goto err_sysctl;
601 #endif
602 err = register_pernet_subsys(&lockd_net_ops);
603 if (err)
604 goto err_pernet;
605
606 err = genl_register_family(&lockd_nl_family);
607 if (err)
608 goto err_netlink;
609
610 err = lockd_create_procfs();
611 if (err)
612 goto err_procfs;
613
614 return 0;
615
616 err_procfs:
617 genl_unregister_family(&lockd_nl_family);
618 err_netlink:
619 unregister_pernet_subsys(&lockd_net_ops);
620 err_pernet:
621 #ifdef CONFIG_SYSCTL
622 unregister_sysctl_table(nlm_sysctl_table);
623 err_sysctl:
624 #endif
625 return err;
626 }
627
exit_nlm(void)628 static void __exit exit_nlm(void)
629 {
630 /* FIXME: delete all NLM clients */
631 nlm_shutdown_hosts();
632 genl_unregister_family(&lockd_nl_family);
633 lockd_remove_procfs();
634 unregister_pernet_subsys(&lockd_net_ops);
635 #ifdef CONFIG_SYSCTL
636 unregister_sysctl_table(nlm_sysctl_table);
637 #endif
638 }
639
640 module_init(init_nlm);
641 module_exit(exit_nlm);
642
643 /**
644 * nlmsvc_dispatch - Process an NLM Request
645 * @rqstp: incoming request
646 *
647 * Return values:
648 * %0: Processing complete; do not send a Reply
649 * %1: Processing complete; send Reply in rqstp->rq_res
650 */
nlmsvc_dispatch(struct svc_rqst * rqstp)651 int nlmsvc_dispatch(struct svc_rqst *rqstp)
652 {
653 const struct svc_procedure *procp = rqstp->rq_procinfo;
654 __be32 *statp = rqstp->rq_accept_statp;
655
656 if (!procp->pc_decode(rqstp, &rqstp->rq_arg_stream))
657 goto out_decode_err;
658
659 *statp = procp->pc_func(rqstp);
660 if (*statp == rpc_drop_reply)
661 return 0;
662 if (*statp != rpc_success)
663 return 1;
664
665 if (!procp->pc_encode(rqstp, &rqstp->rq_res_stream))
666 goto out_encode_err;
667
668 return 1;
669
670 out_decode_err:
671 *statp = rpc_garbage_args;
672 return 1;
673
674 out_encode_err:
675 *statp = rpc_system_err;
676 return 1;
677 }
678
679 /*
680 * Define NLM program and procedures
681 */
682 static const struct svc_version *nlmsvc_version[] = {
683 [1] = &nlmsvc_version1,
684 [3] = &nlmsvc_version3,
685 #ifdef CONFIG_LOCKD_V4
686 [4] = &nlmsvc_version4,
687 #endif
688 };
689
690 #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
691 static struct svc_program nlmsvc_program = {
692 .pg_prog = NLM_PROGRAM, /* program number */
693 .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
694 .pg_vers = nlmsvc_version, /* version table */
695 .pg_name = "lockd", /* service name */
696 .pg_class = "nfsd", /* share authentication with nfsd */
697 .pg_authenticate = &lockd_authenticate, /* export authentication */
698 .pg_init_request = svc_generic_init_request,
699 .pg_rpcbind_set = svc_generic_rpcbind_set,
700 };
701
702 /**
703 * lockd_nl_server_set_doit - set the lockd server parameters via netlink
704 * @skb: reply buffer
705 * @info: netlink metadata and command arguments
706 *
707 * This updates the per-net values. When updating the values in the init_net
708 * namespace, also update the "legacy" global values.
709 *
710 * Return 0 on success or a negative errno.
711 */
lockd_nl_server_set_doit(struct sk_buff * skb,struct genl_info * info)712 int lockd_nl_server_set_doit(struct sk_buff *skb, struct genl_info *info)
713 {
714 struct net *net = genl_info_net(info);
715 struct lockd_net *ln = net_generic(net, lockd_net_id);
716 const struct nlattr *attr;
717
718 if (GENL_REQ_ATTR_CHECK(info, LOCKD_A_SERVER_GRACETIME))
719 return -EINVAL;
720
721 if (info->attrs[LOCKD_A_SERVER_GRACETIME] ||
722 info->attrs[LOCKD_A_SERVER_TCP_PORT] ||
723 info->attrs[LOCKD_A_SERVER_UDP_PORT]) {
724 attr = info->attrs[LOCKD_A_SERVER_GRACETIME];
725 if (attr) {
726 u32 gracetime = nla_get_u32(attr);
727
728 if (gracetime > nlm_grace_period_max)
729 return -EINVAL;
730
731 ln->gracetime = gracetime;
732
733 if (net == &init_net)
734 nlm_grace_period = gracetime;
735 }
736
737 attr = info->attrs[LOCKD_A_SERVER_TCP_PORT];
738 if (attr) {
739 ln->tcp_port = nla_get_u16(attr);
740 if (net == &init_net)
741 nlm_tcpport = ln->tcp_port;
742 }
743
744 attr = info->attrs[LOCKD_A_SERVER_UDP_PORT];
745 if (attr) {
746 ln->udp_port = nla_get_u16(attr);
747 if (net == &init_net)
748 nlm_udpport = ln->udp_port;
749 }
750 }
751 return 0;
752 }
753
754 /**
755 * lockd_nl_server_get_doit - get lockd server parameters via netlink
756 * @skb: reply buffer
757 * @info: netlink metadata and command arguments
758 *
759 * Return 0 on success or a negative errno.
760 */
lockd_nl_server_get_doit(struct sk_buff * skb,struct genl_info * info)761 int lockd_nl_server_get_doit(struct sk_buff *skb, struct genl_info *info)
762 {
763 struct net *net = genl_info_net(info);
764 struct lockd_net *ln = net_generic(net, lockd_net_id);
765 void *hdr;
766 int err;
767
768 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
769 if (!skb)
770 return -ENOMEM;
771
772 hdr = genlmsg_iput(skb, info);
773 if (!hdr) {
774 err = -EMSGSIZE;
775 goto err_free_msg;
776 }
777
778 err = nla_put_u32(skb, LOCKD_A_SERVER_GRACETIME, ln->gracetime) ||
779 nla_put_u16(skb, LOCKD_A_SERVER_TCP_PORT, ln->tcp_port) ||
780 nla_put_u16(skb, LOCKD_A_SERVER_UDP_PORT, ln->udp_port);
781 if (err)
782 goto err_free_msg;
783
784 genlmsg_end(skb, hdr);
785
786 return genlmsg_reply(skb, info);
787 err_free_msg:
788 nlmsg_free(skb);
789
790 return err;
791 }
792