smsgiucv_app.c 5.4 KB

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