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