2 * linux/kernel/vserver/network.c
4 * Virtual Server: Network Support
6 * Copyright (C) 2003-2005 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
12 * V0.05 and back to locking again
16 #include <linux/config.h>
17 #include <linux/slab.h>
18 #include <linux/vserver/network_cmd.h>
19 #include <linux/rcupdate.h>
22 #include <asm/errno.h>
27 * allocate an initialized nx_info struct
28 * doesn't make it visible (hash) */
30 static struct nx_info *__alloc_nx_info(nid_t nid)
32 struct nx_info *new = NULL;
34 vxdprintk(VXD_CBIT(nid, 1), "alloc_nx_info(%d)*", nid);
36 /* would this benefit from a slab cache? */
37 new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
41 memset (new, 0, sizeof(struct nx_info));
43 INIT_HLIST_NODE(&new->nx_hlist);
44 atomic_set(&new->nx_usecnt, 0);
45 atomic_set(&new->nx_tasks, 0);
48 new->nx_flags = NXF_INIT_SET;
50 /* rest of init goes here */
52 vxdprintk(VXD_CBIT(nid, 0),
53 "alloc_nx_info(%d) = %p", nid, new);
57 /* __dealloc_nx_info()
59 * final disposal of nx_info */
61 static void __dealloc_nx_info(struct nx_info *nxi)
63 vxdprintk(VXD_CBIT(nid, 0),
64 "dealloc_nx_info(%p)", nxi);
66 nxi->nx_hlist.next = LIST_POISON1;
69 BUG_ON(atomic_read(&nxi->nx_usecnt));
70 BUG_ON(atomic_read(&nxi->nx_tasks));
72 nxi->nx_state |= NXS_RELEASED;
76 static void __shutdown_nx_info(struct nx_info *nxi)
78 nxi->nx_state |= NXS_SHUTDOWN;
79 vs_net_change(nxi, VSC_NETDOWN);
84 void free_nx_info(struct nx_info *nxi)
86 /* context shutdown is mandatory */
87 BUG_ON(nxi->nx_state != NXS_SHUTDOWN);
89 /* context must not be hashed */
90 BUG_ON(nxi->nx_state & NXS_HASHED);
92 BUG_ON(atomic_read(&nxi->nx_usecnt));
93 BUG_ON(atomic_read(&nxi->nx_tasks));
95 __dealloc_nx_info(nxi);
99 /* hash table for nx_info hash */
101 #define NX_HASH_SIZE 13
103 struct hlist_head nx_info_hash[NX_HASH_SIZE];
105 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
108 static inline unsigned int __hashval(nid_t nid)
110 return (nid % NX_HASH_SIZE);
117 * add the nxi to the global hash table
118 * requires the hash_lock to be held */
120 static inline void __hash_nx_info(struct nx_info *nxi)
122 struct hlist_head *head;
124 vxd_assert_lock(&nx_info_hash_lock);
125 vxdprintk(VXD_CBIT(nid, 4),
126 "__hash_nx_info: %p[#%d]", nxi, nxi->nx_id);
128 /* context must not be hashed */
129 BUG_ON(nx_info_state(nxi, NXS_HASHED));
131 nxi->nx_state |= NXS_HASHED;
132 head = &nx_info_hash[__hashval(nxi->nx_id)];
133 hlist_add_head(&nxi->nx_hlist, head);
136 /* __unhash_nx_info()
138 * remove the nxi from the global hash table
139 * requires the hash_lock to be held */
141 static inline void __unhash_nx_info(struct nx_info *nxi)
143 vxd_assert_lock(&nx_info_hash_lock);
144 vxdprintk(VXD_CBIT(nid, 4),
145 "__unhash_nx_info: %p[#%d]", nxi, nxi->nx_id);
147 /* context must be hashed */
148 BUG_ON(!nx_info_state(nxi, NXS_HASHED));
150 nxi->nx_state &= ~NXS_HASHED;
151 hlist_del(&nxi->nx_hlist);
155 /* __lookup_nx_info()
157 * requires the hash_lock to be held
158 * doesn't increment the nx_refcnt */
160 static inline struct nx_info *__lookup_nx_info(nid_t nid)
162 struct hlist_head *head = &nx_info_hash[__hashval(nid)];
163 struct hlist_node *pos;
166 vxd_assert_lock(&nx_info_hash_lock);
167 hlist_for_each(pos, head) {
168 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
170 if (nxi->nx_id == nid)
175 vxdprintk(VXD_CBIT(nid, 0),
176 "__lookup_nx_info(#%u): %p[#%u]",
177 nid, nxi, nxi?nxi->nx_id:0);
184 * find unused dynamic nid
185 * requires the hash_lock to be held */
187 static inline nid_t __nx_dynamic_id(void)
189 static nid_t seq = MAX_N_CONTEXT;
192 vxd_assert_lock(&nx_info_hash_lock);
194 if (++seq > MAX_N_CONTEXT)
196 if (!__lookup_nx_info(seq)) {
197 vxdprintk(VXD_CBIT(nid, 4),
198 "__nx_dynamic_id: [#%d]", seq);
201 } while (barrier != seq);
205 /* __create_nx_info()
207 * create the requested context
208 * get() and hash it */
210 static struct nx_info * __create_nx_info(int id)
212 struct nx_info *new, *nxi = NULL;
214 vxdprintk(VXD_CBIT(nid, 1), "create_nx_info(%d)*", id);
216 if (!(new = __alloc_nx_info(id)))
217 return ERR_PTR(-ENOMEM);
219 /* required to make dynamic xids unique */
220 spin_lock(&nx_info_hash_lock);
222 /* dynamic context requested */
223 if (id == NX_DYNAMIC_ID) {
224 id = __nx_dynamic_id();
226 printk(KERN_ERR "no dynamic context available.\n");
227 nxi = ERR_PTR(-EAGAIN);
232 /* static context requested */
233 else if ((nxi = __lookup_nx_info(id))) {
234 vxdprintk(VXD_CBIT(nid, 0),
235 "create_nx_info(%d) = %p (already there)", id, nxi);
236 if (nx_info_flags(nxi, NXF_STATE_SETUP, 0))
237 nxi = ERR_PTR(-EBUSY);
239 nxi = ERR_PTR(-EEXIST);
242 /* dynamic nid creation blocker */
243 else if (id >= MIN_D_CONTEXT) {
244 vxdprintk(VXD_CBIT(nid, 0),
245 "create_nx_info(%d) (dynamic rejected)", id);
246 nxi = ERR_PTR(-EINVAL);
251 vxdprintk(VXD_CBIT(nid, 0),
252 "create_nx_info(%d) = %p (new)", id, new);
253 __hash_nx_info(get_nx_info(new));
254 nxi = new, new = NULL;
257 spin_unlock(&nx_info_hash_lock);
259 __dealloc_nx_info(new);
268 void unhash_nx_info(struct nx_info *nxi)
270 __shutdown_nx_info(nxi);
271 spin_lock(&nx_info_hash_lock);
272 __unhash_nx_info(nxi);
273 spin_unlock(&nx_info_hash_lock);
276 #ifdef CONFIG_VSERVER_LEGACYNET
278 struct nx_info *create_nx_info(void)
280 return __create_nx_info(NX_DYNAMIC_ID);
287 * search for a nx_info and get() it
288 * negative id means current */
290 struct nx_info *locate_nx_info(int id)
292 struct nx_info *nxi = NULL;
295 nxi = get_nx_info(current->nx_info);
297 spin_lock(&nx_info_hash_lock);
298 nxi = get_nx_info(__lookup_nx_info(id));
299 spin_unlock(&nx_info_hash_lock);
306 * verify that nid is still hashed */
308 int nid_is_hashed(nid_t nid)
312 spin_lock(&nx_info_hash_lock);
313 hashed = (__lookup_nx_info(nid) != NULL);
314 spin_unlock(&nx_info_hash_lock);
319 #ifdef CONFIG_PROC_FS
321 int get_nid_list(int index, unsigned int *nids, int size)
323 int hindex, nr_nids = 0;
325 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
326 struct hlist_head *head = &nx_info_hash[hindex];
327 struct hlist_node *pos;
329 spin_lock(&nx_info_hash_lock);
330 hlist_for_each(pos, head) {
336 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
337 nids[nr_nids] = nxi->nx_id;
338 if (++nr_nids >= size) {
339 spin_unlock(&nx_info_hash_lock);
343 /* keep the lock time short */
344 spin_unlock(&nx_info_hash_lock);
353 * migrate task to new network
354 * gets nxi, puts old_nxi on change
357 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
359 struct nx_info *old_nxi;
365 vxdprintk(VXD_CBIT(nid, 5),
366 "nx_migrate_task(%p,%p[#%d.%d.%d])",
368 atomic_read(&nxi->nx_usecnt),
369 atomic_read(&nxi->nx_tasks));
371 /* maybe disallow this completely? */
372 old_nxi = task_get_nx_info(p);
378 clr_nx_info(&p->nx_info);
379 claim_nx_info(nxi, p);
380 set_nx_info(&p->nx_info, nxi);
384 vxdprintk(VXD_CBIT(nid, 5),
385 "moved task %p into nxi:%p[#%d]",
389 release_nx_info(old_nxi, p);
391 put_nx_info(old_nxi);
396 #include <linux/netdevice.h>
397 #include <linux/inetdevice.h>
400 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
406 return addr_in_nx_info(nxi, ifa->ifa_address);
409 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
411 struct in_device *in_dev = __in_dev_get(dev);
412 struct in_ifaddr **ifap = NULL;
413 struct in_ifaddr *ifa = NULL;
420 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
421 ifap = &ifa->ifa_next) {
422 if (addr_in_nx_info(nxi, ifa->ifa_address))
429 * check if address is covered by socket
431 * sk: the socket to check against
432 * addr: the address in question (must be != 0)
434 static inline int __addr_in_socket(struct sock *sk, uint32_t addr)
436 struct nx_info *nxi = sk->sk_nx_info;
437 uint32_t saddr = tcp_v4_rcv_saddr(sk);
439 vxdprintk(VXD_CBIT(net, 5),
440 "__addr_in_socket(%p,%d.%d.%d.%d) %p:%d.%d.%d.%d %p;%lx",
441 sk, VXD_QUAD(addr), nxi, VXD_QUAD(saddr), sk->sk_socket,
442 (sk->sk_socket?sk->sk_socket->flags:0));
445 /* direct address match */
446 return (saddr == addr);
448 /* match against nx_info */
449 return addr_in_nx_info(nxi, addr);
451 /* unrestricted any socket */
457 int nx_addr_conflict(struct nx_info *nxi, uint32_t addr, struct sock *sk)
459 vxdprintk(VXD_CBIT(net, 2),
460 "nx_addr_conflict(%p,%p) %d.%d,%d.%d",
461 nxi, sk, VXD_QUAD(addr));
464 /* check real address */
465 return __addr_in_socket(sk, addr);
467 /* check against nx_info */
468 int i, n = nxi->nbipv4;
471 if (__addr_in_socket(sk, nxi->ipv4[i]))
475 /* check against any */
481 /* vserver syscall commands below here */
483 /* taks nid and nx_info functions */
485 #include <asm/uaccess.h>
488 int vc_task_nid(uint32_t id, void __user *data)
493 struct task_struct *tsk;
495 if (!vx_check(0, VX_ADMIN|VX_WATCH))
498 read_lock(&tasklist_lock);
499 tsk = find_task_by_real_pid(id);
500 nid = (tsk) ? tsk->nid : -ESRCH;
501 read_unlock(&tasklist_lock);
509 int vc_nx_info(uint32_t id, void __user *data)
512 struct vcmd_nx_info_v0 vc_data;
514 if (!vx_check(0, VX_ADMIN))
516 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
519 nxi = locate_nx_info(id);
523 vc_data.nid = nxi->nx_id;
526 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
532 /* network functions */
534 int vc_net_create(uint32_t nid, void __user *data)
536 struct vcmd_net_create vc_data = { .flagword = NXF_INIT_SET };
537 struct nx_info *new_nxi;
540 if (!capable(CAP_SYS_ADMIN))
542 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
545 if ((nid > MAX_S_CONTEXT) && (nid != VX_DYNAMIC_ID))
550 new_nxi = __create_nx_info(nid);
552 return PTR_ERR(new_nxi);
555 new_nxi->nx_flags = vc_data.flagword;
557 vs_net_change(new_nxi, VSC_NETUP);
558 ret = new_nxi->nx_id;
559 nx_migrate_task(current, new_nxi);
560 /* if this fails, we might end up with a hashed nx_info */
561 put_nx_info(new_nxi);
566 int vc_net_migrate(uint32_t id, void __user *data)
570 if (!capable(CAP_SYS_ADMIN))
573 nxi = locate_nx_info(id);
576 nx_migrate_task(current, nxi);
581 int vc_net_add(uint32_t nid, void __user *data)
583 struct vcmd_net_addr_v0 vc_data;
585 int index, pos, ret = 0;
587 if (!capable(CAP_SYS_ADMIN))
589 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
592 switch (vc_data.type) {
594 if ((vc_data.count < 1) || (vc_data.count > 4))
602 nxi = locate_nx_info(nid);
606 switch (vc_data.type) {
609 while ((index < vc_data.count) &&
610 ((pos = nxi->nbipv4) < NB_IPV4ROOT)) {
611 nxi->ipv4[pos] = vc_data.ip[index];
612 nxi->mask[pos] = vc_data.mask[index];
619 case NXA_TYPE_IPV4|NXA_MOD_BCAST:
620 nxi->v4_bcast = vc_data.ip[0];
633 int vc_net_remove(uint32_t nid, void __user *data)
635 struct vcmd_net_addr_v0 vc_data;
639 if (!capable(CAP_SYS_ADMIN))
641 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
644 nxi = locate_nx_info(nid);
648 switch (vc_data.type) {
662 int vc_get_nflags(uint32_t id, void __user *data)
665 struct vcmd_net_flags_v0 vc_data;
667 if (!capable(CAP_SYS_ADMIN))
670 nxi = locate_nx_info(id);
674 vc_data.flagword = nxi->nx_flags;
676 /* special STATE flag handling */
677 vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, NXF_ONE_TIME);
681 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
686 int vc_set_nflags(uint32_t id, void __user *data)
689 struct vcmd_net_flags_v0 vc_data;
690 uint64_t mask, trigger;
692 if (!capable(CAP_SYS_ADMIN))
694 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
697 nxi = locate_nx_info(id);
701 /* special STATE flag handling */
702 mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, NXF_ONE_TIME);
703 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
705 nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
706 vc_data.flagword, mask);
711 int vc_get_ncaps(uint32_t id, void __user *data)
714 struct vcmd_net_caps_v0 vc_data;
716 if (!capable(CAP_SYS_ADMIN))
719 nxi = locate_nx_info(id);
723 vc_data.ncaps = nxi->nx_ncaps;
724 vc_data.cmask = ~0UL;
727 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
732 int vc_set_ncaps(uint32_t id, void __user *data)
735 struct vcmd_net_caps_v0 vc_data;
737 if (!capable(CAP_SYS_ADMIN))
739 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
742 nxi = locate_nx_info(id);
746 nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
747 vc_data.ncaps, vc_data.cmask);
753 #include <linux/module.h>
755 EXPORT_SYMBOL_GPL(free_nx_info);
756 EXPORT_SYMBOL_GPL(unhash_nx_info);