patch-2.6.6-vs1.9.1
[linux-2.6.git] / kernel / vserver / network.c
1 /*
2  *  linux/kernel/vserver/network.c
3  *
4  *  Virtual Server: Network Support
5  *
6  *  Copyright (C) 2003-2004  Herbert Pƶtzl
7  *
8  *  V0.01  broken out from vcontext V0.05
9  *  V0.02  cleaned up implementation
10  *  V0.03  added equiv nx commands
11  *  V0.04  switch to RCU based hash
12  *
13  */
14
15 #include <linux/config.h>
16 #include <linux/slab.h>
17 #include <linux/vserver/network.h>
18 #include <linux/ninline.h>
19 #include <linux/rcupdate.h>
20
21 #include <asm/errno.h>
22
23
24 /*      __alloc_nx_info()
25
26         * allocate an initialized nx_info struct
27         * doesn't make it visible (hash)                        */
28
29 static struct nx_info *__alloc_nx_info(nid_t nid)
30 {
31         struct nx_info *new = NULL;
32         
33         nxdprintk("alloc_nx_info()\n");
34
35         /* would this benefit from a slab cache? */
36         new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
37         if (!new)
38                 return 0;
39         
40         memset (new, 0, sizeof(struct nx_info));
41         new->nx_id = nid;
42         INIT_RCU_HEAD(&new->nx_rcu);
43         INIT_HLIST_NODE(&new->nx_hlist);
44         atomic_set(&new->nx_refcnt, 0);
45         atomic_set(&new->nx_usecnt, 0);
46
47         /* rest of init goes here */
48         
49         nxdprintk("alloc_nx_info() = %p\n", new);
50         return new;
51 }
52
53 /*      __dealloc_nx_info()
54
55         * final disposal of nx_info                             */
56
57 static void __dealloc_nx_info(struct nx_info *nxi)
58 {
59         nxdprintk("dealloc_nx_info(%p)\n", nxi);
60
61         nxi->nx_hlist.next = LIST_POISON1;
62         nxi->nx_id = -1;
63         
64         BUG_ON(atomic_read(&nxi->nx_usecnt));
65         BUG_ON(atomic_read(&nxi->nx_refcnt));
66
67         kfree(nxi);
68 }
69
70
71 /*      hash table for nx_info hash */
72
73 #define NX_HASH_SIZE    13
74
75 struct hlist_head nx_info_hash[NX_HASH_SIZE];
76
77 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
78
79
80 static inline unsigned int __hashval(nid_t nid)
81 {
82         return (nid % NX_HASH_SIZE);
83 }
84
85
86
87 /*      __hash_nx_info()
88
89         * add the nxi to the global hash table
90         * requires the hash_lock to be held                     */
91
92 static inline void __hash_nx_info(struct nx_info *nxi)
93 {
94         struct hlist_head *head;
95         
96         nxdprintk("__hash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
97         get_nx_info(nxi);
98         head = &nx_info_hash[__hashval(nxi->nx_id)];
99         hlist_add_head_rcu(&nxi->nx_hlist, head);
100 }
101
102 /*      __unhash_nx_info()
103
104         * remove the nxi from the global hash table
105         * requires the hash_lock to be held                     */
106
107 static inline void __unhash_nx_info(struct nx_info *nxi)
108 {
109         nxdprintk("__unhash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
110         hlist_del_rcu(&nxi->nx_hlist);
111         put_nx_info(nxi);
112 }
113
114
115 /*      __lookup_nx_info()
116
117         * requires the rcu_read_lock()
118         * doesn't increment the nx_refcnt                       */
119
120 static inline struct nx_info *__lookup_nx_info(nid_t nid)
121 {
122         struct hlist_head *head = &nx_info_hash[__hashval(nid)];
123         struct hlist_node *pos;
124
125         hlist_for_each(pos, head) {
126                 struct nx_info *nxi =
127                         hlist_entry(pos, struct nx_info, nx_hlist);
128
129                 if (nxi->nx_id == nid) {
130                         return nxi;
131                 }
132         }
133         return NULL;
134 }
135
136
137 /*      __nx_dynamic_id()
138
139         * find unused dynamic nid
140         * requires the hash_lock to be held                     */
141
142 static inline nid_t __nx_dynamic_id(void)
143 {
144         static nid_t seq = MAX_N_CONTEXT;
145         nid_t barrier = seq;
146         
147         do {
148                 if (++seq > MAX_N_CONTEXT)
149                         seq = MIN_D_CONTEXT;
150                 if (!__lookup_nx_info(seq))
151                         return seq;
152         } while (barrier != seq);
153         return 0;
154 }
155
156 /*      __loc_nx_info()
157
158         * locate or create the requested context
159         * get() it and if new hash it                           */
160
161 static struct nx_info * __loc_nx_info(int id, int *err)
162 {
163         struct nx_info *new, *nxi = NULL;
164         
165         nxdprintk("loc_nx_info(%d)\n", id);
166
167         if (!(new = __alloc_nx_info(id))) {
168                 *err = -ENOMEM;
169                 return NULL;
170         }
171
172         spin_lock(&nx_info_hash_lock);
173
174         /* dynamic context requested */
175         if (id == NX_DYNAMIC_ID) {
176                 id = __nx_dynamic_id();
177                 if (!id) {
178                         printk(KERN_ERR "no dynamic context available.\n");
179                         goto out_unlock;
180                 }
181                 new->nx_id = id;
182         }
183         /* existing context requested */
184         else if ((nxi = __lookup_nx_info(id))) {
185                 /* context in setup is not available */
186                 if (nxi->nx_flags & VXF_STATE_SETUP) {
187                         nxdprintk("loc_nx_info(%d) = %p (not available)\n", id, nxi);
188                         nxi = NULL;
189                         *err = -EBUSY;
190                 } else {
191                         nxdprintk("loc_nx_info(%d) = %p (found)\n", id, nxi);
192                         get_nx_info(nxi);
193                         *err = 0;
194                 }
195                 goto out_unlock;
196         }
197
198         /* new context requested */
199         nxdprintk("loc_nx_info(%d) = %p (new)\n", id, new);
200         __hash_nx_info(get_nx_info(new));
201         nxi = new, new = NULL;
202         *err = 1;
203
204 out_unlock:
205         spin_unlock(&nx_info_hash_lock);
206         if (new)
207                 __dealloc_nx_info(new);
208         return nxi;
209 }
210
211
212
213 /*      exported stuff                                          */
214
215
216
217
218 void rcu_free_nx_info(void *obj)
219 {
220         struct nx_info *nxi = obj;
221         int usecnt, refcnt;
222
223         usecnt = atomic_read(&nxi->nx_usecnt);
224         BUG_ON(usecnt < 0);
225
226         refcnt = atomic_read(&nxi->nx_refcnt);
227         BUG_ON(refcnt < 0);
228
229         if (!usecnt)
230                 __dealloc_nx_info(nxi);
231         else
232                 printk("!!! rcu didn't free\n");
233 }
234
235 void unhash_nx_info(struct nx_info *nxi)
236 {
237         spin_lock(&nx_info_hash_lock);
238         __unhash_nx_info(nxi);
239         spin_unlock(&nx_info_hash_lock);
240 }
241
242 /*      locate_nx_info()
243
244         * search for a nx_info and get() it                     
245         * negative id means current                             */
246
247 struct nx_info *locate_nx_info(int id)
248 {
249         struct nx_info *nxi;
250         
251         if (id < 0) {
252                 nxi = get_nx_info(current->nx_info);
253         } else {
254                 rcu_read_lock();
255                 nxi = get_nx_info(__lookup_nx_info(id));
256                 rcu_read_unlock();
257         }
258         return nxi;
259 }
260
261 /*      nx_info_is_hashed()
262
263         * verify that nid is still hashed                       */
264
265 int nx_info_is_hashed(nid_t nid)
266 {
267         int hashed;
268
269         rcu_read_lock();
270         hashed = (__lookup_nx_info(nid) != NULL);
271         rcu_read_unlock();
272         return hashed;
273 }
274
275 #ifdef  CONFIG_VSERVER_LEGACY
276
277 struct nx_info *locate_or_create_nx_info(int id)
278 {
279         int err;
280
281         return __loc_nx_info(id, &err);
282 }
283
284 struct nx_info *create_nx_info(void)
285 {
286         struct nx_info *new;
287         int err;
288         
289         nxdprintk("create_nx_info()\n");
290         if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
291                 return NULL;
292         return new;
293 }
294
295
296 #endif
297
298 #ifdef  CONFIG_PROC_FS
299
300 #define hlist_for_each_rcu(pos, head) \
301         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
302                 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
303
304 int get_nid_list(int index, unsigned int *nids, int size)
305 {
306         int hindex, nr_nids = 0;
307
308         rcu_read_lock();
309         for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
310                 struct hlist_head *head = &nx_info_hash[hindex];
311                 struct hlist_node *pos;
312
313                 hlist_for_each_rcu(pos, head) {
314                         struct nx_info *nxi;
315
316                         if (--index > 0)
317                                 continue;
318
319                         nxi = hlist_entry(pos, struct nx_info, nx_hlist);
320                         nids[nr_nids] = nxi->nx_id;                     
321                         if (++nr_nids >= size)
322                                 goto out;
323                 }
324         }
325 out:
326         rcu_read_unlock();
327         return nr_nids;
328 }
329 #endif
330
331
332 /*
333  *      migrate task to new network
334  */
335
336 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
337 {
338         struct nx_info *old_nxi = task_get_nx_info(p);
339         int ret = 0;
340         
341         if (!p || !nxi)
342                 BUG();
343
344         nxdprintk("nx_migrate_task(%p,%p[#%d.%d.%d])\n",
345                 p, nxi, nxi->nx_id,
346                 atomic_read(&nxi->nx_usecnt),
347                 atomic_read(&nxi->nx_refcnt));
348         if (old_nxi == nxi)
349                 goto out;
350
351         task_lock(p);
352         /* should be handled in set_nx_info !! */
353         if (old_nxi)
354                 clr_nx_info(&p->nx_info);
355         set_nx_info(&p->nx_info, nxi);
356         p->nid = nxi->nx_id;
357         task_unlock(p);
358
359         // put_nx_info(old_nxi);
360 out:
361         put_nx_info(old_nxi);
362         return ret;
363 }
364
365
366 #include <linux/netdevice.h>
367 #include <linux/inetdevice.h>
368
369 static inline int __addr_in_nx_info(u32 addr, struct nx_info *nxi)
370 {
371         int i, nbip;
372
373         nbip = nxi->nbipv4;
374         for (i=0; i<nbip; i++)
375                 if (nxi->ipv4[i] == addr)
376                         return 1;
377         return 0;
378 }
379
380 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
381 {
382         if (nxi && ifa)
383                 return __addr_in_nx_info(ifa->ifa_address, nxi);
384         return 1;
385 }
386
387 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
388 {
389         struct in_device *in_dev = __in_dev_get(dev);
390         struct in_ifaddr **ifap = NULL;
391         struct in_ifaddr *ifa = NULL;
392
393         if (!nxi)
394                 return 1;
395         if (!in_dev)
396                 return 0;
397
398         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
399                 ifap = &ifa->ifa_next) {
400                 if (__addr_in_nx_info(ifa->ifa_address, nxi))
401                         return 1;
402         }
403         return 0;
404 }
405
406
407
408
409 /* vserver syscall commands below here */
410
411 /* taks nid and nx_info functions */
412
413 #include <asm/uaccess.h>
414
415
416 int vc_task_nid(uint32_t id, void __user *data)
417 {
418         nid_t nid;
419
420         if (id) {
421                 struct task_struct *tsk;
422
423                 if (!vx_check(0, VX_ADMIN|VX_WATCH))
424                         return -EPERM;
425
426                 read_lock(&tasklist_lock);
427                 tsk = find_task_by_pid(id);
428                 nid = (tsk) ? tsk->nid : -ESRCH;
429                 read_unlock(&tasklist_lock);
430         }
431         else
432                 nid = current->nid;
433         return nid;
434 }
435
436
437 int vc_nx_info(uint32_t id, void __user *data)
438 {
439         struct nx_info *nxi;
440         struct vcmd_nx_info_v0 vc_data;
441
442         if (!vx_check(0, VX_ADMIN))
443                 return -ENOSYS;
444         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
445                 return -EPERM;
446
447         nxi = locate_nx_info(id);
448         if (!nxi)
449                 return -ESRCH;
450
451         vc_data.nid = nxi->nx_id;
452         put_nx_info(nxi);
453
454         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
455                 return -EFAULT;
456         return 0;
457 }
458
459
460 /* network functions */
461
462 int vc_net_create(uint32_t nid, void __user *data)
463 {
464         // int ret = -ENOMEM;
465         struct nx_info *new_nxi;
466         int ret;
467
468         if (!capable(CAP_SYS_ADMIN))
469                 return -EPERM;
470
471         if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
472                 return -EINVAL;
473
474         if (nid < 1)
475                 return -EINVAL;
476
477         new_nxi = __loc_nx_info(nid, &ret);
478         if (!new_nxi)
479                 return ret;
480         if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
481                 ret = -EEXIST;
482                 goto out_put;
483         }
484
485         ret = new_nxi->nx_id;
486         nx_migrate_task(current, new_nxi);
487 out_put:
488         put_nx_info(new_nxi);
489         return ret;
490 }
491
492
493 int vc_net_migrate(uint32_t id, void __user *data)
494 {
495         struct nx_info *nxi;
496         
497         if (!capable(CAP_SYS_ADMIN))
498                 return -EPERM;
499
500         nxi = locate_nx_info(id);
501         if (!nxi)
502                 return -ESRCH;
503         nx_migrate_task(current, nxi);
504         put_nx_info(nxi);
505         return 0;
506 }
507
508 int vc_net_add(uint32_t id, void __user *data)
509 {
510         struct nx_info *nxi;
511         struct vcmd_net_nx_v0 vc_data;
512
513         if (!capable(CAP_SYS_ADMIN))
514                 return -EPERM;
515         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
516                 return -EFAULT;
517
518         nxi = locate_nx_info(id);
519         if (!nxi)
520                 return -ESRCH;
521
522         // add ip to net context here
523         put_nx_info(nxi);
524         return 0;
525 }
526
527 int vc_net_remove(uint32_t id, void __user *data)
528 {
529         struct nx_info *nxi;
530         struct vcmd_net_nx_v0 vc_data;
531
532         if (!capable(CAP_SYS_ADMIN))
533                 return -EPERM;
534         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
535                 return -EFAULT;
536
537         nxi = locate_nx_info(id);
538         if (!nxi)
539                 return -ESRCH;
540
541         // rem ip from net context here
542         put_nx_info(nxi);
543         return 0;
544 }
545
546
547
548 int vc_get_nflags(uint32_t id, void __user *data)
549 {
550         struct nx_info *nxi;
551         struct vcmd_net_flags_v0 vc_data;
552
553         if (!capable(CAP_SYS_ADMIN))
554                 return -EPERM;
555
556         nxi = locate_nx_info(id);
557         if (!nxi)
558                 return -ESRCH;
559
560         vc_data.flagword = nxi->nx_flags;
561
562         /* special STATE flag handling */
563         vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
564
565         put_nx_info(nxi);
566
567         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
568                 return -EFAULT;
569         return 0;
570 }
571
572 int vc_set_nflags(uint32_t id, void __user *data)
573 {
574         struct nx_info *nxi;
575         struct vcmd_net_flags_v0 vc_data;
576         uint64_t mask, trigger;
577
578         if (!capable(CAP_SYS_ADMIN))
579                 return -EPERM;
580         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
581                 return -EFAULT;
582
583         nxi = locate_nx_info(id);
584         if (!nxi)
585                 return -ESRCH;
586
587         /* special STATE flag handling */
588         mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
589         trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
590         // if (trigger & IPF_STATE_SETUP)
591
592         nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
593                 vc_data.flagword, mask);
594         put_nx_info(nxi);
595         return 0;
596 }
597
598 int vc_get_ncaps(uint32_t id, void __user *data)
599 {
600         struct nx_info *nxi;
601         struct vcmd_net_caps_v0 vc_data;
602
603         if (!capable(CAP_SYS_ADMIN))
604                 return -EPERM;
605
606         nxi = locate_nx_info(id);
607         if (!nxi)
608                 return -ESRCH;
609
610         vc_data.ncaps = nxi->nx_ncaps;
611         vc_data.cmask = ~0UL;
612         put_nx_info(nxi);
613
614         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
615                 return -EFAULT;
616         return 0;
617 }
618
619 int vc_set_ncaps(uint32_t id, void __user *data)
620 {
621         struct nx_info *nxi;
622         struct vcmd_net_caps_v0 vc_data;
623
624         if (!capable(CAP_SYS_ADMIN))
625                 return -EPERM;
626         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
627                 return -EFAULT;
628
629         nxi = locate_nx_info(id);
630         if (!nxi)
631                 return -ESRCH;
632
633         nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
634                 vc_data.ncaps, vc_data.cmask);
635         put_nx_info(nxi);
636         return 0;
637 }
638
639
640 #include <linux/module.h>
641
642 EXPORT_SYMBOL_GPL(rcu_free_nx_info);
643 EXPORT_SYMBOL_GPL(nx_info_hash_lock);
644