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