-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
529 lines (474 loc) · 13.4 KB
/
main.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <linux/kernel.h>
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <net/net_namespace.h>
#include "namestacknl.h"
#include "namestack_priv.h"
static DEFINE_MUTEX(nos_mutex);
static struct sock *nls = NULL;
static int daemon_pid;
static atomic_t req_id = ATOMIC_INIT(0);
struct pending_node
{
atomic_t ref;
__u32 seq;
void *cb;
void *data;
struct pending_node *next;
};
static DEFINE_SPINLOCK(pending_queue_lock);
static struct pending_node *pending_queue = NULL;
static inline void lock_pending_queue(void)
{
spin_lock(&pending_queue_lock);
}
static inline void unlock_pending_queue(void)
{
spin_unlock(&pending_queue_lock);
}
/**
* Logically inserts a new node into the pending queue. Must *not* be called
* from an interrupt handler.
* Literally it instead looks for a node with a reference count of 0 first, and
* reclaims it if it finds one. Otherwise it allocates a new node to insert
* into the pending queue. This mechanism allows pending_queue_find_and_remove
* to avoid taking the queue lock, which is important since it's called from
* an irq handler.
*
*/
static int pending_queue_push(struct sk_buff *sk, __u32 seq,
void *cb, void *data)
{
struct pending_node *ptr, *node = NULL;
int err = -ENOMEM, insert = 0;
printk(KERN_INFO "pending queue is %p\n", pending_queue);
lock_pending_queue();
for (ptr = pending_queue, node = NULL; ptr && !node; ptr = ptr->next)
if (ptr->ref.counter == 0)
{
/* Found a node that needs to be freed, claim it */
ptr->ref.counter = 1;
node = ptr;
}
unlock_pending_queue();
if (!node)
{
node = kmalloc(sizeof(struct pending_node), GFP_ATOMIC);
if (node)
{
node->ref.counter = 1;
insert = 1;
}
}
if (node)
{
err = 0;
node->seq = seq;
node->cb = cb;
node->data = data;
if (insert)
{
lock_pending_queue();
node->next = pending_queue;
pending_queue = node;
unlock_pending_queue();
}
}
return err;
}
static struct pending_node *pending_queue_find_and_remove(__u32 seq)
{
struct pending_node *node = NULL;
for (node = pending_queue; node; node = node->next)
if (atomic_read(&node->ref) && node->seq == seq)
break;
if (node)
{
/* "Free" the node by decrementing its reference counter.
* It'll actually get freed later, in pending_queue_free.
*/
atomic_dec(&node->ref);
}
return node;
}
/* Frees any memory allocated by the pending node queue. */
static void pending_queue_free(void)
{
struct pending_node *ptr;
for (ptr = pending_queue; ptr; )
{
struct pending_node *next = ptr->next;
kfree(ptr);
ptr = next;
}
}
static int
namestack_send_message(int pid, int type, const void *payload, int size)
{
struct sk_buff *skb;
int len = NLMSG_SPACE(size);
struct nlmsghdr *nlh;
skb = alloc_skb(len, GFP_ATOMIC);
if (!skb) {
printk(KERN_ERR "Could not allocate skb to send message\n");
return -ENOMEM;
}
nlh = __nlmsg_put(skb, pid, 0, type, (len - sizeof(*nlh)), 0);
nlh->nlmsg_flags = 0;
memcpy(NLMSG_DATA(nlh), payload, size);
return netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
}
/**
* This function deals with messages sent by do_qualify() (in the daemon)
*/
static int
handle_register(const struct sk_buff *skb, const struct nlmsghdr *nlh)
{
printk("received register message from user %d, process %d\n",
NETLINK_CREDS(skb)->uid,
NETLINK_CREDS(skb)->pid);
/* FIXME: should check whether user is root first. Not doing for now
* to simplify testing.
*/
daemon_pid = NETLINK_CREDS(skb)->pid;
return namestack_send_message(daemon_pid, NAME_STACK_REGISTER, NULL, 0);
}
/**
* This function handles messages sent by do_??() in the daemon
* R
*/
static int
handle_name_reply(const struct sk_buff *skb, const struct nlmsghdr *nlh)
{
struct pending_node *node;
printk("received reply message from user %d, process %d\n",
NETLINK_CREDS(skb)->uid,
NETLINK_CREDS(skb)->pid);
node = pending_queue_find_and_remove(nlh->nlmsg_seq);
if (node)
{
int len;
printk(KERN_INFO "found reply on pending queue\n");
if (NLMSG_PAYLOAD(nlh, 0) >= sizeof(len))
{
memcpy(&len, NLMSG_DATA(nlh), sizeof(len));
printk(KERN_INFO "len is %d\n", len);
if (NLMSG_PAYLOAD(nlh, 0) >= len + sizeof(len))
{
query_resolv_cb cb = node->cb;
/**
cb = name_stream_query_resolve( response, length, data)
response = nlmsg_data + integer
len = first int of the nlmsg_data
data = sock
*/
cb(NLMSG_DATA(nlh) + sizeof(len), len,
node->data);
}
else
printk(KERN_WARNING
"invalid payload length in reply\n");
}
else
printk(KERN_WARNING
"invalid payload length in reply\n");
}
else
printk(KERN_WARNING "reply for unknown request\n");
/* Send an empty REPLY as an ack */
return namestack_send_message(NETLINK_CREDS(skb)->pid,
NAME_STACK_NAME_REPLY, NULL, 0);
}
/// Maximum length of a name (including nulltermination?)
#define MAX_NAME_LEN 256
/**
* handles messages from do_register() in the daemon.
*/
static int
handle_register_reply(const struct sk_buff *skb, const struct nlmsghdr *nlh)
{
struct pending_node *node;
printk("received reply message from user %d, process %d\n",
NETLINK_CREDS(skb)->uid,
NETLINK_CREDS(skb)->pid);
node = pending_queue_find_and_remove(nlh->nlmsg_seq);
if (node)
{
int result;
printk(KERN_INFO "found reply on pending queue\n");
if (NLMSG_PAYLOAD(nlh, 0) >= sizeof(result))
{
int name_len;
char name_buf[MAX_NAME_LEN];
register_cb cb = node->cb;
memcpy(&result, NLMSG_DATA(nlh), sizeof(result));
memcpy(&name_len, NLMSG_DATA(nlh) + sizeof(int),
sizeof(int));
if (name_len)
memcpy(name_buf,
NLMSG_DATA(nlh) + 2 * sizeof(int),
name_len);
name_buf[name_len] = 0;
printk(KERN_INFO "result is %d\n", result);
cb(result, name_buf, node->data);
}
else
printk(KERN_WARNING
"invalid payload length in reply\n");
}
else
printk(KERN_WARNING "reply for unknown request\n");
/* Send an empty REPLY as an ack */
return namestack_send_message(NETLINK_CREDS(skb)->pid,
NAME_STACK_NAME_REPLY, NULL, 0);
}
/**
* This function picks out a node from the pending_queue and executes it (cb) with a zero:ed array and node->data (only two args?!)
*/
static int
handle_qualify_reply(const struct sk_buff *skb, const struct nlmsghdr *nlh)
{
struct pending_node *node;
printk("received qualify reply message from user %d, process %d\n",
NETLINK_CREDS(skb)->uid,
NETLINK_CREDS(skb)->pid);
node = pending_queue_find_and_remove(nlh->nlmsg_seq);
if (node)
{
printk(KERN_INFO "found reply on pending queue\n");
if (NLMSG_PAYLOAD(nlh, 0) >= sizeof(int))
{
int name_len;
char name_buf[MAX_NAME_LEN];
qualify_cb cb = node->cb;
memcpy(&name_len, NLMSG_DATA(nlh), sizeof(int));
if (name_len)
memcpy(name_buf, NLMSG_DATA(nlh) + sizeof(int),
name_len);
name_buf[name_len] = 0;
cb(name_buf, node->data);
}
else
printk(KERN_WARNING
"invalid payload length in reply\n");
}
else
printk(KERN_WARNING "reply for unknown request\n");
/* Send an empty REPLY as an ack */
return namestack_send_message(NETLINK_CREDS(skb)->pid,
NAME_STACK_QUALIFY_REPLY, NULL, 0);
}
static int
nos_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
{
int err = 0;
printk(KERN_INFO "got message type %d\n", nlh->nlmsg_type);
switch (nlh->nlmsg_type) {
case NAME_STACK_REGISTER:
err = handle_register(skb, nlh);
break;
case NAME_STACK_NAME_REPLY:
err = handle_name_reply(skb, nlh);
break;
case NAME_STACK_REGISTER_REPLY:
err = handle_register_reply(skb, nlh);
break;
case NAME_STACK_QUALIFY_REPLY:
err = handle_qualify_reply(skb, nlh);
break;
default:
err = -ENOSYS;
}
return err;
}
static void
nos_rcv_skb(struct sk_buff *skb)
{
mutex_lock(&nos_mutex);
while (skb->len >= NLMSG_SPACE(0)) {
int err;
uint32_t rlen;
struct nlmsghdr *nlh;
nlh = nlmsg_hdr(skb);
if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
break;
rlen = NLMSG_ALIGN(nlh->nlmsg_len);
if (rlen > skb->len)
rlen = skb->len;
err = nos_rcv_msg(skb, nlh);
skb_pull(skb, rlen);
}
mutex_unlock(&nos_mutex);
}
static __init int namestack_init(void)
{
int rc;
printk(KERN_INFO "name-oriented stack module loading\n");
nls = netlink_kernel_create(&init_net, NETLINK_NAME_ORIENTED_STACK,
0, nos_rcv_skb, NULL, THIS_MODULE);
if (!nls) {
printk(KERN_ERR "namestackmod: failed to create netlink socket\n");
return -ENOMEM;
}
rc = name_af_init();
if (!rc)
rc = name_cache_init();
return rc;
}
static void __exit namestack_exit(void)
{
name_cache_free();
name_af_exit();
netlink_kernel_release(nls);
/* Only after no new requests can be received is it safe to free the
* pending request queue.
*/
pending_queue_free();
printk(KERN_INFO "name-oriented stack module unloading\n");
}
/**
* Send a message to the daemon (using net-link).
* It creates a sequence number, sends a message to the daemon with that sequence number and puts a task with the same sequence number in the local execution-"queue" (pending_queue_push()). When a net-link message comes back (through nos_recv_msg()) its sequence number is matched to the queue and executed together with it's *cb function.
* \param pid is the PID of the daemon
* \param type is either one of NAME_STACK_* messages defined in name_stack_message_types
* \param cb is the function to be when a reply comes back from the the daemon
*/
static int
namestack_send_message_tracked(int pid, int type, const void *payload, int size,
void *cb, void *data)
{
struct sk_buff *skb;
int len = NLMSG_SPACE(size), seq, err;
struct nlmsghdr *nlh;
skb = alloc_skb(len, GFP_ATOMIC);
if (!skb) {
printk(KERN_ERR "Could not allocate skb to send message\n");
return -ENOMEM;
}
seq = atomic_inc_return(&req_id);
nlh = __nlmsg_put(skb, pid, seq, type, (len - sizeof(*nlh)), 0);
nlh->nlmsg_flags = 0;
memcpy(NLMSG_DATA(nlh), payload, size);
// Enqueue the waiting function
err = pending_queue_push(skb, seq, cb, data);
if (err) {
printk(KERN_ERR "Allocation failure, can't send message\n");
goto out;
}
// Send the message to the daemon
err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
if (err > 0) {
/* A positive return value indicates how many bytes were sent
* successfully, which is equivalent to success since sends
* aren't fragmented in any way.
*/
err = 0;
}
out:
return err;
}
int name_send_query(const char *name, query_resolv_cb cb, void *data)
{
int err;
if (!daemon_pid) {
printk(KERN_WARNING "no resolver daemon, unable to send query\n");
err = -ENOSYS;
}
else {
printk(KERN_INFO "resolving %s\n", name);
/* FIXME: who handles retrying in case of failure? */
err = namestack_send_message_tracked(daemon_pid,
NAME_STACK_NAME_QUERY,
name, strlen(name) + 1,
cb, data);
}
return err;
}
int name_fully_qualify(const char *name, qualify_cb cb, void *data)
{
int err;
if (!daemon_pid) {
printk(KERN_WARNING "no resolver daemon, unable to send query\n");
err = -ENOSYS;
}
else {
printk(KERN_INFO "qualifying %s\n", name);
/* FIXME: who handles retrying in case of failure? */
err = namestack_send_message_tracked(daemon_pid,
NAME_STACK_QUALIFY_QUERY,
name, strlen(name) + 1,
cb, data);
}
return err;
}
/**
* \param num_v6_addresses is a pointer to an int contain the amount of local v6 addresses
* \param v6_addresses is a pointer to an array of struct in6_addr containing the found v6 addresses
* \param num_v4_addresses is a pointer to an array of _be32* v4_addresses containing the local v4 addresses
* pointer to an int. The amount of found v4 addresses
* \param
*/
int name_send_registration(const char *name,
const struct in6_addr *v6_addresses,
int num_v6_addresses,
const __be32 *v4_addresses,
int num_v4_addresses,
register_cb cb, void *data)
{
int err;
if (!daemon_pid) {
printk(KERN_WARNING "no resolver daemon, unable to send query\n");
err = -ENOSYS;
}
else {
char *payload, *ptr;
size_t name_len, len;
printk(KERN_INFO "registering %s\n", name);
name_len = strlen(name) + 1;
len = name_len;
len += sizeof(int) + num_v6_addresses * sizeof(struct in6_addr);
len += sizeof(int) + num_v4_addresses * sizeof(__be32);
err = -ENOMEM;
payload = kmalloc(len, GFP_ATOMIC);
if (!payload)
goto out;
ptr = payload;
memcpy(ptr, name, name_len);
ptr += name_len;
memcpy(ptr, &num_v6_addresses, sizeof(int));
ptr += sizeof(int);
memcpy(ptr, v6_addresses,
num_v6_addresses * sizeof(struct in6_addr));
ptr += num_v6_addresses * sizeof(struct in6_addr);
memcpy(ptr, &num_v4_addresses, sizeof(int));
ptr += sizeof(int);
memcpy(ptr, v4_addresses, num_v4_addresses * sizeof(__be32));
/* FIXME: who handles retrying in case of failure? */
// at this point, payload = name, num_v6_addresses, v6_addresses[], num_v4_addresses, v4_addresses[]
err = namestack_send_message_tracked(daemon_pid,
NAME_STACK_REGISTER_QUERY,
payload, len,
cb, data);
kfree(payload);
}
out:
return err;
}
void name_delete_registration(const char *name)
{
int err;
if (!daemon_pid) {
printk(KERN_WARNING "no resolver daemon, unable to send query\n");
err = -ENOSYS;
}
else {
printk(KERN_INFO "deleting registered name %s\n", name);
/* FIXME: who handles retrying in case of failure? */
err = namestack_send_message(daemon_pid,
NAME_STACK_REGISTER_DELETE,
name, strlen(name) + 1);
}
}
module_init(namestack_init);
module_exit(namestack_exit);