2 * linux/kernel/vserver/network.c
4 * Virtual Server: Network Support
6 * Copyright (C) 2003-2004 Herbert Pƶtzl
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
15 #include <linux/config.h>
16 #include <linux/slab.h>
17 #include <linux/vserver/network_cmd.h>
18 #include <linux/rcupdate.h>
21 #include <asm/errno.h>
26 * allocate an initialized nx_info struct
27 * doesn't make it visible (hash) */
29 static struct nx_info *__alloc_nx_info(nid_t nid)
31 struct nx_info *new = NULL;
33 vxdprintk(VXD_CBIT(nid, 1), "alloc_nx_info(%d)*", nid);
35 /* would this benefit from a slab cache? */
36 new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
40 memset (new, 0, sizeof(struct nx_info));
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);
47 /* rest of init goes here */
49 vxdprintk(VXD_CBIT(nid, 0),
50 "alloc_nx_info() = %p", new);
54 /* __dealloc_nx_info()
56 * final disposal of nx_info */
58 static void __dealloc_nx_info(struct nx_info *nxi)
60 vxdprintk(VXD_CBIT(nid, 0),
61 "dealloc_nx_info(%p)", nxi);
63 nxi->nx_hlist.next = LIST_POISON1;
66 BUG_ON(atomic_read(&nxi->nx_usecnt));
67 BUG_ON(atomic_read(&nxi->nx_refcnt));
72 static inline int __free_nx_info(struct nx_info *nxi)
78 usecnt = atomic_read(&nxi->nx_usecnt);
81 refcnt = atomic_read(&nxi->nx_refcnt);
85 __dealloc_nx_info(nxi);
89 static void __rcu_put_nx_info(struct rcu_head *head)
91 struct nx_info *nxi = container_of(head, struct nx_info, nx_rcu);
93 vxdprintk(VXD_CBIT(nid, 3),
94 "__rcu_put_nx_info(%p[#%d]): %d,%d",
96 atomic_read(&nxi->nx_usecnt),
97 atomic_read(&nxi->nx_refcnt));
102 /* hash table for nx_info hash */
104 #define NX_HASH_SIZE 13
106 struct hlist_head nx_info_hash[NX_HASH_SIZE];
108 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
111 static inline unsigned int __hashval(nid_t nid)
113 return (nid % NX_HASH_SIZE);
120 * add the nxi to the global hash table
121 * requires the hash_lock to be held */
123 static inline void __hash_nx_info(struct nx_info *nxi)
125 struct hlist_head *head;
127 vxdprintk(VXD_CBIT(nid, 4),
128 "__hash_nx_info: %p[#%d]", nxi, nxi->nx_id);
130 head = &nx_info_hash[__hashval(nxi->nx_id)];
131 hlist_add_head_rcu(&nxi->nx_hlist, head);
134 /* __unhash_nx_info()
136 * remove the nxi from the global hash table
137 * requires the hash_lock to be held */
139 static inline void __unhash_nx_info(struct nx_info *nxi)
141 vxdprintk(VXD_CBIT(nid, 4),
142 "__unhash_nx_info: %p[#%d]", nxi, nxi->nx_id);
143 hlist_del_rcu(&nxi->nx_hlist);
144 call_rcu(&nxi->nx_rcu, __rcu_put_nx_info);
148 /* __lookup_nx_info()
150 * requires the rcu_read_lock()
151 * doesn't increment the nx_refcnt */
153 static inline struct nx_info *__lookup_nx_info(nid_t nid)
155 struct hlist_head *head = &nx_info_hash[__hashval(nid)];
156 struct hlist_node *pos;
158 hlist_for_each_rcu(pos, head) {
159 struct nx_info *nxi =
160 hlist_entry(pos, struct nx_info, nx_hlist);
162 if (nxi->nx_id == nid) {
172 * find unused dynamic nid
173 * requires the rcu_read_lock()
174 * requires the hash_lock to be held */
176 static inline nid_t __nx_dynamic_id(void)
178 static nid_t seq = MAX_N_CONTEXT;
182 if (++seq > MAX_N_CONTEXT)
184 if (!__lookup_nx_info(seq)) {
185 vxdprintk(VXD_CBIT(nid, 4),
186 "__nx_dynamic_id: [#%d]", seq);
189 } while (barrier != seq);
195 * locate or create the requested context
196 * get() it and if new hash it */
198 static struct nx_info * __loc_nx_info(int id, int *err)
200 struct nx_info *new, *nxi = NULL;
202 vxdprintk(VXD_CBIT(nid, 1), "loc_nx_info(%d)*", id);
204 if (!(new = __alloc_nx_info(id))) {
209 /* FIXME is this required at all ? */
211 /* required to make dynamic xids unique */
212 spin_lock(&nx_info_hash_lock);
214 /* dynamic context requested */
215 if (id == NX_DYNAMIC_ID) {
216 id = __nx_dynamic_id();
218 printk(KERN_ERR "no dynamic context available.\n");
223 /* existing context requested */
224 else if ((nxi = __lookup_nx_info(id))) {
225 /* context in setup is not available */
226 if (nxi->nx_flags & VXF_STATE_SETUP) {
227 vxdprintk(VXD_CBIT(nid, 0),
228 "loc_nx_info(%d) = %p (not available)", id, nxi);
232 vxdprintk(VXD_CBIT(nid, 0),
233 "loc_nx_info(%d) = %p (found)", id, nxi);
240 /* new context requested */
241 vxdprintk(VXD_CBIT(nid, 0),
242 "loc_nx_info(%d) = %p (new)", id, new);
243 __hash_nx_info(get_nx_info(new));
244 nxi = new, new = NULL;
248 spin_unlock(&nx_info_hash_lock);
251 __dealloc_nx_info(new);
259 void free_nx_info(struct nx_info *nxi)
261 BUG_ON(__free_nx_info(nxi));
264 void unhash_nx_info(struct nx_info *nxi)
266 spin_lock(&nx_info_hash_lock);
267 __unhash_nx_info(nxi);
268 spin_unlock(&nx_info_hash_lock);
273 * search for a nx_info and get() it
274 * negative id means current */
276 struct nx_info *locate_nx_info(int id)
281 nxi = get_nx_info(current->nx_info);
284 nxi = get_nx_info(__lookup_nx_info(id));
290 /* nx_info_is_hashed()
292 * verify that nid is still hashed */
294 int nx_info_is_hashed(nid_t nid)
299 hashed = (__lookup_nx_info(nid) != NULL);
304 #ifdef CONFIG_VSERVER_LEGACY
306 struct nx_info *locate_or_create_nx_info(int id)
310 return __loc_nx_info(id, &err);
313 struct nx_info *create_nx_info(void)
318 vxdprintk(VXD_CBIT(nid, 5), "create_nx_info(%s)", "void");
319 if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
327 #ifdef CONFIG_PROC_FS
329 int get_nid_list(int index, unsigned int *nids, int size)
331 int hindex, nr_nids = 0;
334 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
335 struct hlist_head *head = &nx_info_hash[hindex];
336 struct hlist_node *pos;
338 hlist_for_each_rcu(pos, head) {
344 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
345 nids[nr_nids] = nxi->nx_id;
346 if (++nr_nids >= size)
358 * migrate task to new network
361 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
363 struct nx_info *old_nxi;
369 vxdprintk(VXD_CBIT(nid, 5),
370 "nx_migrate_task(%p,%p[#%d.%d.%d])",
372 atomic_read(&nxi->nx_usecnt),
373 atomic_read(&nxi->nx_refcnt));
375 old_nxi = task_get_nx_info(p);
380 /* should be handled in set_nx_info !! */
382 clr_nx_info(&p->nx_info);
383 set_nx_info(&p->nx_info, nxi);
387 /* obsoleted by clr/set */
388 // put_nx_info(old_nxi);
390 put_nx_info(old_nxi);
395 #include <linux/netdevice.h>
396 #include <linux/inetdevice.h>
399 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
405 return addr_in_nx_info(nxi, ifa->ifa_address);
408 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
410 struct in_device *in_dev = __in_dev_get(dev);
411 struct in_ifaddr **ifap = NULL;
412 struct in_ifaddr *ifa = NULL;
419 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
420 ifap = &ifa->ifa_next) {
421 if (addr_in_nx_info(nxi, ifa->ifa_address))
428 * check if address is covered by socket
430 * sk: the socket to check against
431 * addr: the address in question (must be != 0)
433 static inline int __addr_in_socket(struct sock *sk, uint32_t addr)
435 struct nx_info *nxi = sk->sk_nx_info;
436 uint32_t saddr = tcp_v4_rcv_saddr(sk);
438 vxdprintk(VXD_CBIT(net, 5),
439 "__addr_in_socket(%p,%d.%d.%d.%d) %p:%d.%d.%d.%d %p;%lx",
440 sk, VXD_QUAD(addr), nxi, VXD_QUAD(saddr), sk->sk_socket,
441 (sk->sk_socket?sk->sk_socket->flags:0));
444 /* direct address match */
445 return (saddr == addr);
447 /* match against nx_info */
448 return addr_in_nx_info(nxi, addr);
450 /* unrestricted any socket */
456 int nx_addr_conflict(struct nx_info *nxi, uint32_t addr, struct sock *sk)
458 vxdprintk(VXD_CBIT(net, 2),
459 "nx_addr_conflict(%p,%p) %d.%d,%d.%d",
460 nxi, sk, VXD_QUAD(addr));
463 /* check real address */
464 return __addr_in_socket(sk, addr);
466 /* check against nx_info */
467 int i, n = nxi->nbipv4;
470 if (__addr_in_socket(sk, nxi->ipv4[i]))
474 /* check against any */
480 /* vserver syscall commands below here */
482 /* taks nid and nx_info functions */
484 #include <asm/uaccess.h>
487 int vc_task_nid(uint32_t id, void __user *data)
492 struct task_struct *tsk;
494 if (!vx_check(0, VX_ADMIN|VX_WATCH))
497 read_lock(&tasklist_lock);
498 tsk = find_task_by_real_pid(id);
499 nid = (tsk) ? tsk->nid : -ESRCH;
500 read_unlock(&tasklist_lock);
508 int vc_nx_info(uint32_t id, void __user *data)
511 struct vcmd_nx_info_v0 vc_data;
513 if (!vx_check(0, VX_ADMIN))
515 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
518 nxi = locate_nx_info(id);
522 vc_data.nid = nxi->nx_id;
525 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
531 /* network functions */
533 int vc_net_create(uint32_t nid, void __user *data)
535 // int ret = -ENOMEM;
536 struct nx_info *new_nxi;
539 if (!capable(CAP_SYS_ADMIN))
542 if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
548 new_nxi = __loc_nx_info(nid, &ret);
551 if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
556 ret = new_nxi->nx_id;
557 nx_migrate_task(current, new_nxi);
559 put_nx_info(new_nxi);
564 int vc_net_migrate(uint32_t id, void __user *data)
568 if (!capable(CAP_SYS_ADMIN))
571 nxi = locate_nx_info(id);
574 nx_migrate_task(current, nxi);
579 int vc_net_add(uint32_t id, void __user *data)
582 struct vcmd_net_nx_v0 vc_data;
584 if (!capable(CAP_SYS_ADMIN))
586 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
589 nxi = locate_nx_info(id);
593 // add ip to net context here
598 int vc_net_remove(uint32_t id, void __user *data)
601 struct vcmd_net_nx_v0 vc_data;
603 if (!capable(CAP_SYS_ADMIN))
605 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
608 nxi = locate_nx_info(id);
612 // rem ip from net context here
619 int vc_get_nflags(uint32_t id, void __user *data)
622 struct vcmd_net_flags_v0 vc_data;
624 if (!capable(CAP_SYS_ADMIN))
627 nxi = locate_nx_info(id);
631 vc_data.flagword = nxi->nx_flags;
633 /* special STATE flag handling */
634 vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
638 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
643 int vc_set_nflags(uint32_t id, void __user *data)
646 struct vcmd_net_flags_v0 vc_data;
647 uint64_t mask, trigger;
649 if (!capable(CAP_SYS_ADMIN))
651 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
654 nxi = locate_nx_info(id);
658 /* special STATE flag handling */
659 mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
660 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
661 // if (trigger & IPF_STATE_SETUP)
663 nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
664 vc_data.flagword, mask);
669 int vc_get_ncaps(uint32_t id, void __user *data)
672 struct vcmd_net_caps_v0 vc_data;
674 if (!capable(CAP_SYS_ADMIN))
677 nxi = locate_nx_info(id);
681 vc_data.ncaps = nxi->nx_ncaps;
682 vc_data.cmask = ~0UL;
685 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
690 int vc_set_ncaps(uint32_t id, void __user *data)
693 struct vcmd_net_caps_v0 vc_data;
695 if (!capable(CAP_SYS_ADMIN))
697 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
700 nxi = locate_nx_info(id);
704 nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
705 vc_data.ncaps, vc_data.cmask);
711 #include <linux/module.h>
713 EXPORT_SYMBOL_GPL(free_nx_info);
714 EXPORT_SYMBOL_GPL(unhash_nx_info);