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