Thomas Gleixner | 2522fe4 | 2019-05-28 09:57:20 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All rights reserved. |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <net/genetlink.h> |
| 7 | #include <linux/dlm.h> |
| 8 | #include <linux/dlm_netlink.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 10 | |
| 11 | #include "dlm_internal.h" |
| 12 | |
| 13 | static uint32_t dlm_nl_seqnum; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 14 | static uint32_t listener_nlportid; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 15 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 16 | static struct genl_family family; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 17 | |
| 18 | static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size) |
| 19 | { |
| 20 | struct sk_buff *skb; |
| 21 | void *data; |
| 22 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 23 | skb = genlmsg_new(size, GFP_NOFS); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 24 | if (!skb) |
| 25 | return -ENOMEM; |
| 26 | |
| 27 | /* add the message headers */ |
| 28 | data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd); |
| 29 | if (!data) { |
| 30 | nlmsg_free(skb); |
| 31 | return -EINVAL; |
| 32 | } |
| 33 | |
| 34 | *skbp = skb; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static struct dlm_lock_data *mk_data(struct sk_buff *skb) |
| 39 | { |
| 40 | struct nlattr *ret; |
| 41 | |
| 42 | ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data)); |
| 43 | if (!ret) |
| 44 | return NULL; |
| 45 | return nla_data(ret); |
| 46 | } |
| 47 | |
| 48 | static int send_data(struct sk_buff *skb) |
| 49 | { |
| 50 | struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data); |
| 51 | void *data = genlmsg_data(genlhdr); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 52 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 53 | genlmsg_end(skb, data); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 54 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 55 | return genlmsg_unicast(&init_net, skb, listener_nlportid); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int user_cmd(struct sk_buff *skb, struct genl_info *info) |
| 59 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 60 | listener_nlportid = info->snd_portid; |
| 61 | printk("user_cmd nlpid %u\n", listener_nlportid); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 65 | static const struct genl_small_ops dlm_nl_ops[] = { |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 66 | { |
| 67 | .cmd = DLM_CMD_HELLO, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 68 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 69 | .doit = user_cmd, |
| 70 | }, |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 73 | static struct genl_family family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 74 | .name = DLM_GENL_NAME, |
| 75 | .version = DLM_GENL_VERSION, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 76 | .small_ops = dlm_nl_ops, |
| 77 | .n_small_ops = ARRAY_SIZE(dlm_nl_ops), |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 78 | .module = THIS_MODULE, |
| 79 | }; |
| 80 | |
Denis Cheng | 3072717 | 2008-02-02 01:53:46 +0800 | [diff] [blame] | 81 | int __init dlm_netlink_init(void) |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 82 | { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 83 | return genl_register_family(&family); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Leonardo Potenza | 88ad231 | 2008-05-11 19:15:34 +0200 | [diff] [blame] | 86 | void dlm_netlink_exit(void) |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 87 | { |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 88 | genl_unregister_family(&family); |
| 89 | } |
| 90 | |
| 91 | static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb) |
| 92 | { |
| 93 | struct dlm_rsb *r = lkb->lkb_resource; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 94 | |
| 95 | memset(data, 0, sizeof(struct dlm_lock_data)); |
| 96 | |
| 97 | data->version = DLM_LOCK_DATA_VERSION; |
| 98 | data->nodeid = lkb->lkb_nodeid; |
| 99 | data->ownpid = lkb->lkb_ownpid; |
| 100 | data->id = lkb->lkb_id; |
| 101 | data->remid = lkb->lkb_remid; |
| 102 | data->status = lkb->lkb_status; |
| 103 | data->grmode = lkb->lkb_grmode; |
| 104 | data->rqmode = lkb->lkb_rqmode; |
David Teigland | d292c0c | 2008-02-06 23:27:04 -0600 | [diff] [blame] | 105 | if (lkb->lkb_ua) |
| 106 | data->xid = lkb->lkb_ua->xid; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 107 | if (r) { |
| 108 | data->lockspace_id = r->res_ls->ls_global_id; |
| 109 | data->resource_namelen = r->res_length; |
| 110 | memcpy(data->resource_name, r->res_name, r->res_length); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void dlm_timeout_warn(struct dlm_lkb *lkb) |
| 115 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 116 | struct sk_buff *send_skb; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 117 | struct dlm_lock_data *data; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 118 | size_t size; |
| 119 | int rv; |
| 120 | |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 121 | size = nla_total_size(sizeof(struct dlm_lock_data)) + |
| 122 | nla_total_size(0); /* why this? */ |
| 123 | |
| 124 | rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size); |
| 125 | if (rv < 0) |
| 126 | return; |
| 127 | |
| 128 | data = mk_data(send_skb); |
| 129 | if (!data) { |
| 130 | nlmsg_free(send_skb); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | fill_data(data, lkb); |
| 135 | |
| 136 | send_data(send_skb); |
| 137 | } |
| 138 | |