summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_netlink.c
blob: 78bd31b0501c44c20352eacf74e64d10fca596e5 (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * kernel/power/tuxonice_netlink.c
 *
 * Copyright (C) 2004-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * This file is released under the GPLv2.
 *
 * Functions for communicating with a userspace helper via netlink.
 */

#include <linux/suspend.h>
#include <linux/sched.h>
#include <linux/kmod.h>
#include "tuxonice_netlink.h"
#include "tuxonice.h"
#include "tuxonice_modules.h"
#include "tuxonice_alloc.h"
#include "tuxonice_builtin.h"

static struct user_helper_data *uhd_list;

/*
 * Refill our pool of SKBs for use in emergencies (eg, when eating memory and
 * none can be allocated).
 */
static void toi_fill_skb_pool(struct user_helper_data *uhd)
{
        while (uhd->pool_level < uhd->pool_limit) {
                struct sk_buff *new_skb =
                        alloc_skb(NLMSG_SPACE(uhd->skb_size), TOI_ATOMIC_GFP);

                if (!new_skb)
                        break;

                new_skb->next = uhd->emerg_skbs;
                uhd->emerg_skbs = new_skb;
                uhd->pool_level++;
        }
}

/*
 * Try to allocate a single skb. If we can't get one, try to use one from
 * our pool.
 */
static struct sk_buff *toi_get_skb(struct user_helper_data *uhd)
{
        struct sk_buff *skb =
                alloc_skb(NLMSG_SPACE(uhd->skb_size), TOI_ATOMIC_GFP);

        if (skb)
                return skb;

        skb = uhd->emerg_skbs;
        if (skb) {
                uhd->pool_level--;
                uhd->emerg_skbs = skb->next;
                skb->next = NULL;
        }

        return skb;
}

void toi_send_netlink_message(struct user_helper_data *uhd,
                int type, void *params, size_t len)
{
        struct sk_buff *skb;
        struct nlmsghdr *nlh;
        void *dest;
        struct task_struct *t;

        if (uhd->pid == -1)
                return;

        if (uhd->debug)
                printk(KERN_ERR "toi_send_netlink_message: Send "
                                "message type %d.\n", type);

        skb = toi_get_skb(uhd);
        if (!skb) {
                printk(KERN_INFO "toi_netlink: Can't allocate skb!\n");
                return;
        }

        nlh = nlmsg_put(skb, 0, uhd->sock_seq, type, len, 0);
        uhd->sock_seq++;

        dest = NLMSG_DATA(nlh);
        if (params && len > 0)
                memcpy(dest, params, len);

        netlink_unicast(uhd->nl, skb, uhd->pid, 0);

        toi_read_lock_tasklist();
        t = find_task_by_pid_ns(uhd->pid, &init_pid_ns);
        if (!t) {
                toi_read_unlock_tasklist();
                if (uhd->pid > -1)
                        printk(KERN_INFO "Hmm. Can't find the userspace task"
                                " %d.\n", uhd->pid);
                return;
        }
        wake_up_process(t);
        toi_read_unlock_tasklist();

        yield();
}

static void send_whether_debugging(struct user_helper_data *uhd)
{
        static u8 is_debugging = 1;

        toi_send_netlink_message(uhd, NETLINK_MSG_IS_DEBUGGING,
                        &is_debugging, sizeof(u8));
}

/*
 * Set the PF_NOFREEZE flag on the given process to ensure it can run whilst we
 * are hibernating.
 */
static int nl_set_nofreeze(struct user_helper_data *uhd, __u32 pid)
{
        struct task_struct *t;

        if (uhd->debug)
                printk(KERN_ERR "nl_set_nofreeze for pid %d.\n", pid);

        toi_read_lock_tasklist();
        t = find_task_by_pid_ns(pid, &init_pid_ns);
        if (!t) {
                toi_read_unlock_tasklist();
                printk(KERN_INFO "Strange. Can't find the userspace task %d.\n",
                                pid);
                return -EINVAL;
        }

        t->flags |= PF_NOFREEZE;

        toi_read_unlock_tasklist();
        uhd->pid = pid;

        toi_send_netlink_message(uhd, NETLINK_MSG_NOFREEZE_ACK, NULL, 0);

        return 0;
}

/*
 * Called when the userspace process has informed us that it's ready to roll.
 */
static int nl_ready(struct user_helper_data *uhd, u32 version)
{
        if (version != uhd->interface_version) {
                printk(KERN_INFO "%s userspace process using invalid interface"
                                " version (%d - kernel wants %d). Trying to "
                                "continue without it.\n",
                                uhd->name, version, uhd->interface_version);
                if (uhd->not_ready)
                        uhd->not_ready();
                return -EINVAL;
        }

        complete(&uhd->wait_for_process);

        return 0;
}

void toi_netlink_close_complete(struct user_helper_data *uhd)
{
        if (uhd->nl) {
                netlink_kernel_release(uhd->nl);
                uhd->nl = NULL;
        }

        while (uhd->emerg_skbs) {
                struct sk_buff *next = uhd->emerg_skbs->next;
                kfree_skb(uhd->emerg_skbs);
                uhd->emerg_skbs = next;
        }

        uhd->pid = -1;
}

static int toi_nl_gen_rcv_msg(struct user_helper_data *uhd,
                struct sk_buff *skb, struct nlmsghdr *nlh)
{
        int type = nlh->nlmsg_type;
        int *data;
        int err;

        if (uhd->debug)
                printk(KERN_ERR "toi_user_rcv_skb: Received message %d.\n",
                                type);

        /* Let the more specific handler go first. It returns
         * 1 for valid messages that it doesn't know. */
        err = uhd->rcv_msg(skb, nlh);
        if (err != 1)
                return err;

        /* Only allow one task to receive NOFREEZE privileges */
        if (type == NETLINK_MSG_NOFREEZE_ME && uhd->pid != -1) {
                printk(KERN_INFO "Received extra nofreeze me requests.\n");
                return -EBUSY;
        }

        data = NLMSG_DATA(nlh);

        switch (type) {
        case NETLINK_MSG_NOFREEZE_ME:
                return nl_set_nofreeze(uhd, nlh->nlmsg_pid);
        case NETLINK_MSG_GET_DEBUGGING:
                send_whether_debugging(uhd);
                return 0;
        case NETLINK_MSG_READY:
                if (nlh->nlmsg_len != NLMSG_LENGTH(sizeof(u32))) {
                        printk(KERN_INFO "Invalid ready mesage.\n");
                        if (uhd->not_ready)
                                uhd->not_ready();
                        return -EINVAL;
                }
                return nl_ready(uhd, (u32) *data);
        case NETLINK_MSG_CLEANUP:
                toi_netlink_close_complete(uhd);
                return 0;
        }

        return -EINVAL;
}

static void toi_user_rcv_skb(struct sk_buff *skb)
{
        int err;
        struct nlmsghdr *nlh;
        struct user_helper_data *uhd = uhd_list;

        while (uhd && uhd->netlink_id != skb->sk->sk_protocol)
                uhd = uhd->next;

        if (!uhd)
                return;

        while (skb->len >= NLMSG_SPACE(0)) {
                u32 rlen;

                nlh = (struct nlmsghdr *) skb->data;
                if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
                        return;

                rlen = NLMSG_ALIGN(nlh->nlmsg_len);
                if (rlen > skb->len)
                        rlen = skb->len;

                err = toi_nl_gen_rcv_msg(uhd, skb, nlh);
                if (err)
                        netlink_ack(skb, nlh, err);
                else if (nlh->nlmsg_flags & NLM_F_ACK)
                        netlink_ack(skb, nlh, 0);
                skb_pull(skb, rlen);
        }
}

static int netlink_prepare(struct user_helper_data *uhd)
{
        struct netlink_kernel_cfg cfg = {
                .groups = 0,
                .input = toi_user_rcv_skb,
        };

        uhd->next = uhd_list;
        uhd_list = uhd;

        uhd->sock_seq = 0x42c0ffee;
        uhd->nl = netlink_kernel_create(&init_net, uhd->netlink_id, &cfg);
        if (!uhd->nl) {
                printk(KERN_INFO "Failed to allocate netlink socket for %s.\n",
                                uhd->name);
                return -ENOMEM;
        }

        toi_fill_skb_pool(uhd);

        return 0;
}

void toi_netlink_close(struct user_helper_data *uhd)
{
        struct task_struct *t;

        toi_read_lock_tasklist();
        t = find_task_by_pid_ns(uhd->pid, &init_pid_ns);
        if (t)
                t->flags &= ~PF_NOFREEZE;
        toi_read_unlock_tasklist();

        toi_send_netlink_message(uhd, NETLINK_MSG_CLEANUP, NULL, 0);
}
int toi_netlink_setup(struct user_helper_data *uhd)
{
        /* In case userui didn't cleanup properly on us */
        toi_netlink_close_complete(uhd);

        if (netlink_prepare(uhd) < 0) {
                printk(KERN_INFO "Netlink prepare failed.\n");
                return 1;
        }

        if (toi_launch_userspace_program(uhd->program, uhd->netlink_id,
                                UMH_WAIT_EXEC, uhd->debug) < 0) {
                printk(KERN_INFO "Launch userspace program failed.\n");
                toi_netlink_close_complete(uhd);
                return 1;
        }

        /* Wait 2 seconds for the userspace process to make contact */
        wait_for_completion_timeout(&uhd->wait_for_process, 2*HZ);

        if (uhd->pid == -1) {
                printk(KERN_INFO "%s: Failed to contact userspace process.\n",
                                uhd->name);
                toi_netlink_close_complete(uhd);
                return 1;
        }

        return 0;
}