aboutsummaryrefslogtreecommitdiffstats
path: root/xen/common/network.c
blob: cd726621f163a9b57766f24c2f5cd1d581f48608 (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
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
/* network.c
 *
 * Network virtualization for Xen.  Lower-level network interactions are in 
 * net/dev.c and in the drivers.  This file contains routines to interact 
 * with the virtual interfaces (vifs) and the virtual firewall/router through
 * the use of rules.
 *
 * Copyright (c) 2002, A K Warfield and K A Fraser
 */

#include <hypervisor-ifs/network.h>
#include <xeno/sched.h>
#include <xeno/errno.h>
#include <xeno/init.h>
#include <xeno/slab.h>
#include <xeno/spinlock.h>
#include <xeno/if_ether.h>
#include <linux/skbuff.h>
#include <xeno/netdevice.h>
#include <xeno/in.h>
#include <asm/domain_page.h>
#include <asm/io.h>

/* vif globals 
 * sys_vif_list is a lookup table for vifs, used in packet forwarding.
 * it will be replaced later by something a little more flexible.
 */

int sys_vif_count;                                  /* global vif count */
net_vif_t *sys_vif_list[MAX_SYSTEM_VIFS];           /* global vif array */
net_rule_ent_t *net_rule_list;                      /* global list of rules */
kmem_cache_t *net_vif_cache;                        
kmem_cache_t *net_rule_cache;
static rwlock_t net_rule_lock = RW_LOCK_UNLOCKED;   /* rule mutex */
static rwlock_t sys_vif_lock = RW_LOCK_UNLOCKED;    /* vif mutex */

void print_net_rule_list();


/* ----[ VIF Functions ]----------------------------------------------------*/

/* create_net_vif - Create a new vif and append it to the specified domain.
 * 
 * the domain is examined to determine how many vifs currently are allocated
 * and the newly allocated vif is appended.  The vif is also added to the
 * global list.
 * 
 */

net_vif_t *create_net_vif(int domain)
{
    net_vif_t *new_vif;
    net_ring_t *new_ring;
    net_shadow_ring_t *shadow_ring;
    struct task_struct *dom_task;
    
    if ( !(dom_task = find_domain_by_id(domain)) )
        return NULL;
    
    if ( (new_vif = kmem_cache_alloc(net_vif_cache, GFP_KERNEL)) == NULL )
        return NULL;
    
    new_ring = dom_task->net_ring_base + dom_task->num_net_vifs;
    memset(new_ring, 0, sizeof(net_ring_t));

    shadow_ring = kmalloc(sizeof(net_shadow_ring_t), GFP_KERNEL);
    if ( shadow_ring == NULL ) goto fail;
    
    shadow_ring->rx_ring = kmalloc(RX_RING_SIZE
                    * sizeof(rx_shadow_entry_t), GFP_KERNEL);
    shadow_ring->tx_ring = kmalloc(TX_RING_SIZE
                    * sizeof(tx_shadow_entry_t), GFP_KERNEL);
    if ( (shadow_ring->rx_ring == NULL) || (shadow_ring->tx_ring == NULL) )
            goto fail;

    shadow_ring->rx_prod = shadow_ring->rx_cons = shadow_ring->rx_idx = 0;
    shadow_ring->tx_prod = shadow_ring->tx_cons = shadow_ring->tx_idx = 0;
    
    /* Fill in the new vif struct. */
    
    new_vif->net_ring = new_ring;
    new_vif->shadow_ring = shadow_ring;
    
    new_vif->domain = dom_task;

    new_vif->list.next = NULL;
    
    write_lock(&sys_vif_lock);
    new_vif->id = sys_vif_count;
    sys_vif_list[sys_vif_count++] = new_vif;
    write_unlock(&sys_vif_lock);

    dom_task->net_vif_list[dom_task->num_net_vifs] = new_vif;
    dom_task->num_net_vifs++;
    
    free_task_struct(dom_task);
    return new_vif;
    
fail:
    kmem_cache_free(net_vif_cache, new_vif);
    if ( shadow_ring != NULL )
    {
        if ( shadow_ring->rx_ring ) kfree(shadow_ring->rx_ring);
        if ( shadow_ring->tx_ring ) kfree(shadow_ring->tx_ring);
        kfree(shadow_ring);
    }

    free_task_struct(dom_task);
    return NULL;
}

/* delete_net_vif - Delete the last vif in the given domain. 
 *
 * There doesn't seem to be any reason (yet) to be able to axe an arbitrary 
 * vif, by vif id. 
 */

void destroy_net_vif(struct task_struct *p)
{
    int i;

    if ( p->num_net_vifs <= 0 ) return; // nothing to do.
    
    i = --p->num_net_vifs;
    
    write_lock(&sys_vif_lock);
    sys_vif_list[p->net_vif_list[i]->id] = NULL; // system vif list not gc'ed
    write_unlock(&sys_vif_lock);        
   
    kfree(p->net_vif_list[i]->shadow_ring->tx_ring);
    kfree(p->net_vif_list[i]->shadow_ring->rx_ring);
    kfree(p->net_vif_list[i]->shadow_ring);
    kmem_cache_free(net_vif_cache, p->net_vif_list[i]);
}

/* vif_query - Call from the proc file system to get a list of vifs 
 * assigned to a particular domain.
 */

void vif_query(vif_query_t *vq)
{
    struct task_struct *dom_task;
    char buf[128];
    int i;

    if ( !(dom_task = find_domain_by_id(vq->domain)) ) return;

    *buf = '\0';

    for ( i = 0; i < dom_task->num_net_vifs; i++ )
        sprintf(buf + strlen(buf), "%d\n", dom_task->net_vif_list[i]->id);

    copy_to_user(vq->buf, buf, strlen(buf) + 1);
    
    free_task_struct(dom_task);
}
        

/* print_vif_list - Print the contents of the global vif table.
 */

void print_vif_list()
{
    int i;
    net_vif_t *v;

    printk("Currently, there are %d VIFs.\n", sys_vif_count);
    for ( i = 0; i<sys_vif_count; i++ )
    {
        v = sys_vif_list[i];
        printk("] VIF Entry %d(%d):\n", i, v->id);
        printk("   > net_ring*:  %p\n", v->net_ring);
        printk("   > domain   :  %u\n", v->domain->domain);
    }
}

/* ----[ Net Rule Functions ]-----------------------------------------------*/

/* add_net_rule - Add a new network filter rule.
 */

int add_net_rule(net_rule_t *rule)
{
    net_rule_ent_t *new_ent;
    
    if ( (new_ent = kmem_cache_alloc(net_rule_cache, GFP_KERNEL)) == NULL )
    {
        return -ENOMEM;
    }

    memcpy(&new_ent->r, rule, sizeof(net_rule_t));

    write_lock(&net_rule_lock);
    new_ent->next = net_rule_list;
    net_rule_list = new_ent;
    write_unlock(&net_rule_lock);

    return 0;
}

/* delete_net_rule - Delete an existing network rule.
 */

int delete_net_rule(net_rule_t *rule)
{
    net_rule_ent_t *ent = net_rule_list, *prev = NULL;
    while ( (ent) && ((memcmp(rule, &ent->r, sizeof(net_rule_t))) != 0) )
    {
        prev = ent;
        ent = ent->next;
    }

    if (ent != NULL)
    {
        write_lock(&net_rule_lock);
        if (prev != NULL)
        {
            prev->next = ent->next;
        }
        else
        {
            net_rule_list = ent->next;
        }
        kmem_cache_free(net_rule_cache, ent);
        write_unlock(&net_rule_lock);
    }
    return 0;
}
 
/* add_default_net_rule - Set up default network path (ie for dom0).
 * 
 * this is a utility function to route all traffic with the specified
 * ip address to the specified vif.  It's used to set up domain zero.
 */

void add_default_net_rule(int vif_id, u32 ipaddr)
{
    net_rule_t new_rule;

    //outbound rule.
    memset(&new_rule, 0, sizeof(net_rule_t));
    new_rule.src_addr = ipaddr;
    new_rule.src_addr_mask = 0xffffffff;
    new_rule.src_interface = vif_id;
    new_rule.dst_interface = VIF_PHYSICAL_INTERFACE;
    new_rule.action = NETWORK_ACTION_ACCEPT;
    new_rule.proto = NETWORK_PROTO_ANY;
    add_net_rule(&new_rule);

    //inbound rule;
    memset(&new_rule, 0, sizeof(net_rule_t));
    new_rule.dst_addr = ipaddr;
    new_rule.dst_addr_mask = 0xffffffff;
    new_rule.src_interface = VIF_PHYSICAL_INTERFACE;
    new_rule.dst_interface = vif_id;
    new_rule.action = NETWORK_ACTION_ACCEPT;
    new_rule.proto = NETWORK_PROTO_ANY;
    add_net_rule(&new_rule);

}

/* print_net_rule - Print a single net rule.
 */

void print_net_rule(net_rule_t *r)
{
    printk("===] NET RULE:\n");
    printk("=] src_addr         : %lu\n", (unsigned long) r->src_addr);
    printk("=] src_addr_mask    : %lu\n", (unsigned long) r->src_addr_mask);   
    printk("=] dst_addr         : %lu\n", (unsigned long) r->dst_addr);
    printk("=] dst_addr_mask    : %lu\n", (unsigned long) r->dst_addr_mask);
    printk("=] src_port         : %u\n", r->src_port);
    printk("=] src_port_mask    : %u\n", r->src_port_mask);
    printk("=] dst_port         : %u\n", r->dst_port);
    printk("=] dst_port_mask    : %u\n", r->dst_port_mask);
    printk("=] dst_proto        : %u\n", r->proto);
    printk("=] src_interface    : %d\n", r->src_interface);
    printk("=] dst_interface    : %d\n", r->dst_interface);
    printk("=] action           : %u\n", r->action);
}

/* print_net_rule_list - Print the global rule table.
 */

void print_net_rule_list()
{
    net_rule_ent_t *ent;
    int count = 0;
    
    read_lock(&net_rule_lock);

    ent = net_rule_list;
    
    while (ent) 
    {
        print_net_rule(&ent->r);
        ent = ent->next;
        count++;
    }
    printk("\nTotal of %d rules.\n", count);

    read_unlock(&net_rule_lock);
}

/* net_find_rule - Find the destination vif according to the current rules.
 *
 * Apply the rules to this skbuff and return the vif id that it is bound for.
 * If there is no match, VIF_DROP is returned.
 */

int net_find_rule(u8 nproto, u8 tproto, u32 src_addr, u32 dst_addr, u16 src_port, u16 dst_port, 
                  int src_vif)
{
    net_rule_ent_t *ent;
    int dest = VIF_DROP;
    
    read_lock(&net_rule_lock);
    
    ent = net_rule_list;
    
    while (ent)
    {
        if ( ((ent->r.src_interface == src_vif)
              || (ent->r.src_interface == VIF_ANY_INTERFACE)) &&

             (!((ent->r.src_addr ^ src_addr) & ent->r.src_addr_mask )) &&
             (!((ent->r.dst_addr ^ dst_addr) & ent->r.dst_addr_mask )) &&
             (!((ent->r.src_port ^ src_port) & ent->r.src_port_mask )) &&
             (!((ent->r.dst_port ^ dst_port) & ent->r.dst_port_mask )) &&
             
             ((ent->r.proto == NETWORK_PROTO_ANY) ||
              ((ent->r.proto == NETWORK_PROTO_IP)  &&
               (nproto == (u8)ETH_P_IP)) ||
              ((ent->r.proto == NETWORK_PROTO_ARP) &&
               (nproto == (u8)ETH_P_ARP)) ||
              ((ent->r.proto == NETWORK_PROTO_TCP) &&
               (tproto == IPPROTO_TCP)) ||
              ((ent->r.proto == NETWORK_PROTO_UDP) &&
               (tproto == IPPROTO_UDP)))
           )
        {
            break;
        }
        ent = ent->next;
    }

    if (ent) (dest = ent->r.dst_interface);
    read_unlock(&net_rule_lock);
    return dest;
}

/* net_get_target_vif - Find the vif that the given sk_buff is bound for.
 *
 * This is intended to be the main interface to the VFR rules, where 
 * net_find_rule (above) is a private aspect of the current matching 
 * implementation.  All in-hypervisor routing should use this function only
 * to ensure that this can be rewritten later.
 *
 * Currently, network rules are stored in a global linked list.  New rules are
 * added to the front of this list, and (at present) the first matching rule
 * determines the vif that a packet is sent to.  This is obviously not ideal,
 * it might be more advisable to have chains, or at lest most-specific 
 * matching, and moreover routing latency increases linearly (for old rules)
 * as new rules are added.  
 *
 * net_get_target_vif examines the sk_buff and pulls out the relevant fields
 * based on the packet type.  it then calls net_find_rule to scan the rule 
 * list.
 */

#define net_get_target_vif(skb) __net_get_target_vif(skb->data, skb->len, skb->src_vif)

int __net_get_target_vif(u8 *data, unsigned int len, int src_vif)
{
    int target = VIF_DROP;
    u8 *h_raw, *nh_raw;
    
    if ( len < ETH_HLEN ) goto drop;

    nh_raw = data + ETH_HLEN;
    switch ( ntohs(*(unsigned short *)(data + 12)) )
    {
    case ETH_P_ARP:
        if ( len < (ETH_HLEN + 28) ) goto drop;
        target = net_find_rule((u8)ETH_P_ARP, 0, ntohl(*(u32 *)(nh_raw + 14)),
                               ntohl(*(u32 *)(nh_raw + 24)), 0, 0, 
                               src_vif);
        break;

    case ETH_P_IP:
        if ( len < (ETH_HLEN + 20) ) goto drop;
        h_raw =  data + ((*(unsigned char *)(nh_raw)) & 0x0f) * 4;
        
        /* XXX For now, we ignore ports. */
#if 0
        target = net_find_rule((u8)ETH_P_IP,  *(u8 *)(nh_raw + 9),
                               ntohl(*(u32 *)(nh_raw + 12)),
                               ntohl(*(u32 *)(nh_raw + 16)),
                               ntohs(*(u16 *)(h_raw)),
                               ntohs(*(u16 *)(h_raw + 2)), 
                               src_vif);
#else
        target = net_find_rule((u8)ETH_P_IP,  *(u8 *)(data + 9),
                               ntohl(*(u32 *)(nh_raw + 12)),
                               ntohl(*(u32 *)(nh_raw + 16)),
                               0,
                               0, 
                               src_vif);
#endif
    }
    return target;
    
 drop:
    return VIF_DROP;
}

/* ----[ Syscall Interface ]------------------------------------------------*/

/* 
 * This is the hook function to handle guest-invoked traps requesting 
 * changes to the network system.
 */

long do_network_op(network_op_t *u_network_op)
{
    long ret=0;
    network_op_t op;
    
    if ( current->domain != 0 )
        return -EPERM;

    if ( copy_from_user(&op, u_network_op, sizeof(op)) )
        return -EFAULT;
    switch ( op.cmd )
    {

    case NETWORK_OP_ADDRULE:
    {
        add_net_rule(&op.u.net_rule);
    }
    break;

    case NETWORK_OP_DELETERULE:
    {
        delete_net_rule(&op.u.net_rule);
    }
    break;

    case NETWORK_OP_GETRULELIST:
    {
        // This should eventually ship a rule list up to the VM
        // to be printed in its procfs.  For now, we just print the rules.
        
        print_net_rule_list();
    }
    break;

    case NETWORK_OP_VIFQUERY:
    {
        vif_query(&op.u.vif_query);
    }
    
    default:
        ret = -ENOSYS;
    }

    return ret;
}

void __init net_init (void)
{
    sys_vif_count = 0;
    memset(sys_vif_list, 0, sizeof(sys_vif_list));
    net_rule_list = NULL;
    net_vif_cache = kmem_cache_create("net_vif_cache", sizeof(net_vif_t),
                                    0, SLAB_HWCACHE_ALIGN, NULL, NULL);
    net_rule_cache = kmem_cache_create("net_rule_cache", sizeof(net_rule_ent_t),
                                    0, SLAB_HWCACHE_ALIGN, NULL, NULL);
}