smsgiucv_app.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 pr_fmt(fmt) "smsgiucv_app: " fmt
  14. #include <linux/ctype.h>
  15. #include <linux/err.h>
  16. #include <linux/device.h>
  17. #include <linux/list.h>
  18. #include <linux/kobject.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include <net/iucv/iucv.h>
  24. #include <asm/machine.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_obj(*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. scnprintf(ev->envp[0], ENV_SENDER_LEN, ENV_SENDER_STR "%s", from);
  78. scnprintf(ev->envp[1], ENV_PREFIX_LEN, ENV_PREFIX_STR "%s",
  79. SMSG_PREFIX);
  80. scnprintf(ev->envp[2], ENV_TEXT_LEN(msg), ENV_TEXT_STR "%s", msg);
  81. return ev;
  82. }
  83. static void smsg_event_work_fn(struct work_struct *work)
  84. {
  85. LIST_HEAD(event_queue);
  86. struct smsg_app_event *p, *n;
  87. struct device *dev;
  88. dev = get_device(smsg_app_dev);
  89. if (!dev)
  90. return;
  91. spin_lock_bh(&smsg_event_queue_lock);
  92. list_splice_init(&smsg_event_queue, &event_queue);
  93. spin_unlock_bh(&smsg_event_queue_lock);
  94. list_for_each_entry_safe(p, n, &event_queue, list) {
  95. list_del(&p->list);
  96. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, p->envp);
  97. smsg_app_event_free(p);
  98. }
  99. put_device(dev);
  100. }
  101. static DECLARE_WORK(smsg_event_work, smsg_event_work_fn);
  102. static void smsg_app_callback(const char *from, char *msg)
  103. {
  104. struct smsg_app_event *se;
  105. /* check if the originating z/VM user ID matches
  106. * the configured sender. */
  107. if (sender && strlen(sender) > 0 && strcmp(from, sender) != 0)
  108. return;
  109. /* get start of message text (skip prefix and leading blanks) */
  110. msg += strlen(SMSG_PREFIX);
  111. while (*msg && isspace(*msg))
  112. msg++;
  113. if (*msg == '\0')
  114. return;
  115. /* allocate event list element and its environment */
  116. se = smsg_app_event_alloc(from, msg);
  117. if (!se)
  118. return;
  119. /* queue event and schedule work function */
  120. spin_lock(&smsg_event_queue_lock);
  121. list_add_tail(&se->list, &smsg_event_queue);
  122. spin_unlock(&smsg_event_queue_lock);
  123. schedule_work(&smsg_event_work);
  124. return;
  125. }
  126. static int __init smsgiucv_app_init(void)
  127. {
  128. struct device_driver *smsgiucv_drv;
  129. int rc;
  130. if (!machine_is_vm())
  131. return -ENODEV;
  132. smsgiucv_drv = driver_find(SMSGIUCV_DRV_NAME, &iucv_bus);
  133. if (!smsgiucv_drv)
  134. return -ENODEV;
  135. smsg_app_dev = iucv_alloc_device(NULL, smsgiucv_drv, NULL, "smsgiucv_app");
  136. if (!smsg_app_dev)
  137. return -ENOMEM;
  138. rc = device_register(smsg_app_dev);
  139. if (rc) {
  140. put_device(smsg_app_dev);
  141. goto fail;
  142. }
  143. /* convert sender to uppercase characters */
  144. if (sender) {
  145. int len = strlen(sender);
  146. while (len--)
  147. sender[len] = toupper(sender[len]);
  148. }
  149. /* register with the smsgiucv device driver */
  150. rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback);
  151. if (rc) {
  152. device_unregister(smsg_app_dev);
  153. goto fail;
  154. }
  155. rc = 0;
  156. fail:
  157. return rc;
  158. }
  159. module_init(smsgiucv_app_init);
  160. static void __exit smsgiucv_app_exit(void)
  161. {
  162. /* unregister callback */
  163. smsg_unregister_callback(SMSG_PREFIX, smsg_app_callback);
  164. /* cancel pending work and flush any queued event work */
  165. cancel_work_sync(&smsg_event_work);
  166. smsg_event_work_fn(&smsg_event_work);
  167. device_unregister(smsg_app_dev);
  168. }
  169. module_exit(smsgiucv_app_exit);
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_DESCRIPTION("Deliver z/VM CP SMSG as uevents");
  172. MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");