1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Deliver z/VM CP special messages (SMSG) as uevents. 4 * 5 * The driver registers for z/VM CP special messages with the 6 * "APP" prefix. Incoming messages are delivered to user space 7 * as uevents. 8 * 9 * Copyright IBM Corp. 2010 10 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> 11 * 12 */ 13 #define KMSG_COMPONENT "smsgiucv_app" 14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 15 16 #include <linux/ctype.h> 17 #include <linux/err.h> 18 #include <linux/device.h> 19 #include <linux/list.h> 20 #include <linux/kobject.h> 21 #include <linux/module.h> 22 #include <linux/slab.h> 23 #include <linux/spinlock.h> 24 #include <linux/workqueue.h> 25 #include <net/iucv/iucv.h> 26 #include <asm/machine.h> 27 #include "smsgiucv.h" 28 29 /* prefix used for SMSG registration */ 30 #define SMSG_PREFIX "APP" 31 32 /* SMSG related uevent environment variables */ 33 #define ENV_SENDER_STR "SMSG_SENDER=" 34 #define ENV_SENDER_LEN (strlen(ENV_SENDER_STR) + 8 + 1) 35 #define ENV_PREFIX_STR "SMSG_ID=" 36 #define ENV_PREFIX_LEN (strlen(ENV_PREFIX_STR) + \ 37 strlen(SMSG_PREFIX) + 1) 38 #define ENV_TEXT_STR "SMSG_TEXT=" 39 #define ENV_TEXT_LEN(msg) (strlen(ENV_TEXT_STR) + strlen((msg)) + 1) 40 41 /* z/VM user ID which is permitted to send SMSGs 42 * If the value is undefined or empty (""), special messages are 43 * accepted from any z/VM user ID. */ 44 static char *sender; 45 module_param(sender, charp, 0400); 46 MODULE_PARM_DESC(sender, "z/VM user ID from which CP SMSGs are accepted"); 47 48 /* SMSG device representation */ 49 static struct device *smsg_app_dev; 50 51 /* list element for queuing received messages for delivery */ 52 struct smsg_app_event { 53 struct list_head list; 54 char *buf; 55 char *envp[4]; 56 }; 57 58 /* queue for outgoing uevents */ 59 static LIST_HEAD(smsg_event_queue); 60 static DEFINE_SPINLOCK(smsg_event_queue_lock); 61 62 static void smsg_app_event_free(struct smsg_app_event *ev) 63 { 64 kfree(ev->buf); 65 kfree(ev); 66 } 67 68 static struct smsg_app_event *smsg_app_event_alloc(const char *from, 69 const char *msg) 70 { 71 struct smsg_app_event *ev; 72 73 ev = kzalloc(sizeof(*ev), GFP_ATOMIC); 74 if (!ev) 75 return NULL; 76 77 ev->buf = kzalloc(ENV_SENDER_LEN + ENV_PREFIX_LEN + 78 ENV_TEXT_LEN(msg), GFP_ATOMIC); 79 if (!ev->buf) { 80 kfree(ev); 81 return NULL; 82 } 83 84 /* setting up environment pointers into buf */ 85 ev->envp[0] = ev->buf; 86 ev->envp[1] = ev->envp[0] + ENV_SENDER_LEN; 87 ev->envp[2] = ev->envp[1] + ENV_PREFIX_LEN; 88 ev->envp[3] = NULL; 89 90 /* setting up environment: sender, prefix name, and message text */ 91 scnprintf(ev->envp[0], ENV_SENDER_LEN, ENV_SENDER_STR "%s", from); 92 scnprintf(ev->envp[1], ENV_PREFIX_LEN, ENV_PREFIX_STR "%s", 93 SMSG_PREFIX); 94 scnprintf(ev->envp[2], ENV_TEXT_LEN(msg), ENV_TEXT_STR "%s", msg); 95 96 return ev; 97 } 98 99 static void smsg_event_work_fn(struct work_struct *work) 100 { 101 LIST_HEAD(event_queue); 102 struct smsg_app_event *p, *n; 103 struct device *dev; 104 105 dev = get_device(smsg_app_dev); 106 if (!dev) 107 return; 108 109 spin_lock_bh(&smsg_event_queue_lock); 110 list_splice_init(&smsg_event_queue, &event_queue); 111 spin_unlock_bh(&smsg_event_queue_lock); 112 113 list_for_each_entry_safe(p, n, &event_queue, list) { 114 list_del(&p->list); 115 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, p->envp); 116 smsg_app_event_free(p); 117 } 118 119 put_device(dev); 120 } 121 static DECLARE_WORK(smsg_event_work, smsg_event_work_fn); 122 123 static void smsg_app_callback(const char *from, char *msg) 124 { 125 struct smsg_app_event *se; 126 127 /* check if the originating z/VM user ID matches 128 * the configured sender. */ 129 if (sender && strlen(sender) > 0 && strcmp(from, sender) != 0) 130 return; 131 132 /* get start of message text (skip prefix and leading blanks) */ 133 msg += strlen(SMSG_PREFIX); 134 while (*msg && isspace(*msg)) 135 msg++; 136 if (*msg == '\0') 137 return; 138 139 /* allocate event list element and its environment */ 140 se = smsg_app_event_alloc(from, msg); 141 if (!se) 142 return; 143 144 /* queue event and schedule work function */ 145 spin_lock(&smsg_event_queue_lock); 146 list_add_tail(&se->list, &smsg_event_queue); 147 spin_unlock(&smsg_event_queue_lock); 148 149 schedule_work(&smsg_event_work); 150 return; 151 } 152 153 static int __init smsgiucv_app_init(void) 154 { 155 struct device_driver *smsgiucv_drv; 156 int rc; 157 158 if (!machine_is_vm()) 159 return -ENODEV; 160 161 smsgiucv_drv = driver_find(SMSGIUCV_DRV_NAME, &iucv_bus); 162 if (!smsgiucv_drv) 163 return -ENODEV; 164 165 smsg_app_dev = iucv_alloc_device(NULL, smsgiucv_drv, NULL, KMSG_COMPONENT); 166 if (!smsg_app_dev) 167 return -ENOMEM; 168 169 rc = device_register(smsg_app_dev); 170 if (rc) { 171 put_device(smsg_app_dev); 172 goto fail; 173 } 174 175 /* convert sender to uppercase characters */ 176 if (sender) { 177 int len = strlen(sender); 178 while (len--) 179 sender[len] = toupper(sender[len]); 180 } 181 182 /* register with the smsgiucv device driver */ 183 rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback); 184 if (rc) { 185 device_unregister(smsg_app_dev); 186 goto fail; 187 } 188 189 rc = 0; 190 fail: 191 return rc; 192 } 193 module_init(smsgiucv_app_init); 194 195 static void __exit smsgiucv_app_exit(void) 196 { 197 /* unregister callback */ 198 smsg_unregister_callback(SMSG_PREFIX, smsg_app_callback); 199 200 /* cancel pending work and flush any queued event work */ 201 cancel_work_sync(&smsg_event_work); 202 smsg_event_work_fn(&smsg_event_work); 203 204 device_unregister(smsg_app_dev); 205 } 206 module_exit(smsgiucv_app_exit); 207 208 MODULE_LICENSE("GPL v2"); 209 MODULE_DESCRIPTION("Deliver z/VM CP SMSG as uevents"); 210 MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>"); 211