summaryrefslogtreecommitdiff
path: root/drivers/s390/net/smsgiucv_app.c
blob: 0a263999f7ae44b181ac7dc786908698c42a9974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: GPL-2.0
/*
 * Deliver z/VM CP special messages (SMSG) as uevents.
 *
 * The driver registers for z/VM CP special messages with the
 * "APP" prefix. Incoming messages are delivered to user space
 * as uevents.
 *
 * Copyright IBM Corp. 2010
 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
 *
 */
#define KMSG_COMPONENT		"smsgiucv_app"
#define pr_fmt(fmt)		KMSG_COMPONENT ": " fmt

#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <net/iucv/iucv.h>
#include "smsgiucv.h"

/* prefix used for SMSG registration */
#define SMSG_PREFIX		"APP"

/* SMSG related uevent environment variables */
#define ENV_SENDER_STR		"SMSG_SENDER="
#define ENV_SENDER_LEN		(strlen(ENV_SENDER_STR) + 8 + 1)
#define ENV_PREFIX_STR		"SMSG_ID="
#define ENV_PREFIX_LEN		(strlen(ENV_PREFIX_STR) + \
				 strlen(SMSG_PREFIX) + 1)
#define ENV_TEXT_STR		"SMSG_TEXT="
#define ENV_TEXT_LEN(msg)	(strlen(ENV_TEXT_STR) + strlen((msg)) + 1)

/* z/VM user ID which is permitted to send SMSGs
 * If the value is undefined or empty (""), special messages are
 * accepted from any z/VM user ID. */
static char *sender;
module_param(sender, charp, 0400);
MODULE_PARM_DESC(sender, "z/VM user ID from which CP SMSGs are accepted");

/* SMSG device representation */
static struct device *smsg_app_dev;

/* list element for queuing received messages for delivery */
struct smsg_app_event {
	struct list_head list;
	char *buf;
	char *envp[4];
};

/* queue for outgoing uevents */
static LIST_HEAD(smsg_event_queue);
static DEFINE_SPINLOCK(smsg_event_queue_lock);

static void smsg_app_event_free(struct smsg_app_event *ev)
{
	kfree(ev->buf);
	kfree(ev);
}

static struct smsg_app_event *smsg_app_event_alloc(const char *from,
						   const char *msg)
{
	struct smsg_app_event *ev;

	ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
	if (!ev)
		return NULL;

	ev->buf = kzalloc(ENV_SENDER_LEN + ENV_PREFIX_LEN +
			  ENV_TEXT_LEN(msg), GFP_ATOMIC);
	if (!ev->buf) {
		kfree(ev);
		return NULL;
	}

	/* setting up environment pointers into buf */
	ev->envp[0] = ev->buf;
	ev->envp[1] = ev->envp[0] + ENV_SENDER_LEN;
	ev->envp[2] = ev->envp[1] + ENV_PREFIX_LEN;
	ev->envp[3] = NULL;

	/* setting up environment: sender, prefix name, and message text */
	snprintf(ev->envp[0], ENV_SENDER_LEN, ENV_SENDER_STR "%s", from);
	snprintf(ev->envp[1], ENV_PREFIX_LEN, ENV_PREFIX_STR "%s", SMSG_PREFIX);
	snprintf(ev->envp[2], ENV_TEXT_LEN(msg), ENV_TEXT_STR "%s", msg);

	return ev;
}

static void smsg_event_work_fn(struct work_struct *work)
{
	LIST_HEAD(event_queue);
	struct smsg_app_event *p, *n;
	struct device *dev;

	dev = get_device(smsg_app_dev);
	if (!dev)
		return;

	spin_lock_bh(&smsg_event_queue_lock);
	list_splice_init(&smsg_event_queue, &event_queue);
	spin_unlock_bh(&smsg_event_queue_lock);

	list_for_each_entry_safe(p, n, &event_queue, list) {
		list_del(&p->list);
		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, p->envp);
		smsg_app_event_free(p);
	}

	put_device(dev);
}
static DECLARE_WORK(smsg_event_work, smsg_event_work_fn);

static void smsg_app_callback(const char *from, char *msg)
{
	struct smsg_app_event *se;

	/* check if the originating z/VM user ID matches
	 * the configured sender. */
	if (sender && strlen(sender) > 0 && strcmp(from, sender) != 0)
		return;

	/* get start of message text (skip prefix and leading blanks) */
	msg += strlen(SMSG_PREFIX);
	while (*msg && isspace(*msg))
		msg++;
	if (*msg == '\0')
		return;

	/* allocate event list element and its environment */
	se = smsg_app_event_alloc(from, msg);
	if (!se)
		return;

	/* queue event and schedule work function */
	spin_lock(&smsg_event_queue_lock);
	list_add_tail(&se->list, &smsg_event_queue);
	spin_unlock(&smsg_event_queue_lock);

	schedule_work(&smsg_event_work);
	return;
}

static int __init smsgiucv_app_init(void)
{
	struct device_driver *smsgiucv_drv;
	int rc;

	if (!MACHINE_IS_VM)
		return -ENODEV;

	smsg_app_dev = kzalloc(sizeof(*smsg_app_dev), GFP_KERNEL);
	if (!smsg_app_dev)
		return -ENOMEM;

	smsgiucv_drv = driver_find(SMSGIUCV_DRV_NAME, &iucv_bus);
	if (!smsgiucv_drv) {
		kfree(smsg_app_dev);
		return -ENODEV;
	}

	rc = dev_set_name(smsg_app_dev, KMSG_COMPONENT);
	if (rc) {
		kfree(smsg_app_dev);
		goto fail;
	}
	smsg_app_dev->bus = &iucv_bus;
	smsg_app_dev->parent = iucv_root;
	smsg_app_dev->release = (void (*)(struct device *)) kfree;
	smsg_app_dev->driver = smsgiucv_drv;
	rc = device_register(smsg_app_dev);
	if (rc) {
		put_device(smsg_app_dev);
		goto fail;
	}

	/* convert sender to uppercase characters */
	if (sender) {
		int len = strlen(sender);
		while (len--)
			sender[len] = toupper(sender[len]);
	}

	/* register with the smsgiucv device driver */
	rc = smsg_register_callback(SMSG_PREFIX, smsg_app_callback);
	if (rc) {
		device_unregister(smsg_app_dev);
		goto fail;
	}

	rc = 0;
fail:
	return rc;
}
module_init(smsgiucv_app_init);

static void __exit smsgiucv_app_exit(void)
{
	/* unregister callback */
	smsg_unregister_callback(SMSG_PREFIX, smsg_app_callback);

	/* cancel pending work and flush any queued event work */
	cancel_work_sync(&smsg_event_work);
	smsg_event_work_fn(&smsg_event_work);

	device_unregister(smsg_app_dev);
}
module_exit(smsgiucv_app_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Deliver z/VM CP SMSG as uevents");
MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");