This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / kernel / vserver / legacy.c
1 /*
2  *  linux/kernel/vserver/legacy.c
3  *
4  *  Virtual Server: Legacy Funtions
5  *
6  *  Copyright (C) 2001-2003  Jacques Gelinas
7  *  Copyright (C) 2003-2004  Herbert Pƶtzl
8  *
9  *  V0.01  broken out from vcontext.c V0.05
10  *
11  */
12
13 #include <linux/config.h>
14 #include <linux/vserver/legacy.h>
15 #include <linux/vserver/context.h>
16 #include <linux/vserver/namespace.h>
17 #include <linux/vserver.h>
18 #include <linux/sched.h>
19 #include <linux/namespace.h>
20
21 #include <asm/errno.h>
22 #include <asm/uaccess.h>
23
24
25
26 static int vx_set_initpid(struct vx_info *vxi, int pid)
27 {
28         if (vxi->vx_initpid)
29                 return -EPERM;
30
31         vxi->vx_initpid = pid;
32         return 0;
33 }
34
35 int vc_new_s_context(uint32_t ctx, void __user *data)
36 {
37         int ret = -ENOMEM;
38         struct vcmd_new_s_context_v1 vc_data;
39         struct vx_info *new_vxi;
40
41         if (copy_from_user(&vc_data, data, sizeof(vc_data)))
42                 return -EFAULT;
43
44         /* legacy hack, will be removed soon */
45         if (ctx == -2) {
46                 /* assign flags and initpid */
47                 if (!current->vx_info)
48                         return -EINVAL;
49                 ret = 0;
50                 if (vc_data.flags & VX_INFO_INIT)
51                         ret = vx_set_initpid(current->vx_info, current->tgid);
52                 if (ret == 0) {
53                         /* We keep the same vx_id, but lower the capabilities */
54                         current->vx_info->vx_bcaps &= (~vc_data.remove_cap);
55                         // current->cap_bset &= (~vc_data.remove_cap);
56                         ret = vx_current_xid();
57                         current->vx_info->vx_flags |= vc_data.flags;
58                 }
59                 return ret;
60         }
61         
62         if (!vx_check(0, VX_ADMIN) ||
63                 !capable(CAP_SYS_ADMIN) || vx_flags(VX_INFO_LOCK, 0))
64                 return -EPERM;
65
66         /* ugly hack for Spectator */
67         if (ctx == 1) {
68                 current->xid = 1;
69                 return 0;
70         }
71
72         if (((ctx > MAX_S_CONTEXT) && (ctx != VX_DYNAMIC_ID)) ||
73                 (ctx == 0))
74                 return -EINVAL;
75                 
76         if ((ctx == VX_DYNAMIC_ID) || (ctx < MIN_D_CONTEXT))
77                 new_vxi = find_or_create_vx_info(ctx);
78         else
79                 new_vxi = find_vx_info(ctx);
80
81         if (!new_vxi)
82                 return -EINVAL;
83         new_vxi->vx_flags &= ~(VXF_STATE_SETUP|VXF_STATE_INIT);
84         
85         ret = vx_migrate_task(current, new_vxi);
86         if (ret == 0) {
87                 current->vx_info->vx_bcaps &= (~vc_data.remove_cap);
88                 // current->cap_bset &= (~vc_data.remove_cap);
89                 new_vxi->vx_flags |= vc_data.flags;
90                 if (vc_data.flags & VX_INFO_INIT)
91                         vx_set_initpid(new_vxi, current->tgid);
92                 if (vc_data.flags & VX_INFO_NAMESPACE)
93                         vx_set_namespace(new_vxi,
94                                 current->namespace, current->fs);
95                 if (vc_data.flags & VX_INFO_NPROC)
96                         new_vxi->limit.rlim[RLIMIT_NPROC] =
97                                 current->rlim[RLIMIT_NPROC].rlim_max;
98                 ret = new_vxi->vx_id;
99         }
100         put_vx_info(new_vxi);
101         return ret;
102 }
103
104
105
106 /*  set ipv4 root (syscall) */
107
108 int vc_set_ipv4root(uint32_t nbip, void __user *data)
109 {
110         int i, err = -EPERM;
111         struct vcmd_set_ipv4root_v3 vc_data;
112         struct nx_info *new_nxi, *nxi = current->nx_info;
113
114         if (nbip < 0 || nbip > NB_IPV4ROOT)
115                 return -EINVAL;
116         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
117                 return -EFAULT;
118
119         if (!nxi || nxi->ipv4[0] == 0 || capable(CAP_NET_ADMIN))
120                 // We are allowed to change everything
121                 err = 0;
122         else if (nxi) {
123                 int found = 0;
124                 
125                 // We are allowed to select a subset of the currently
126                 // installed IP numbers. No new one allowed
127                 // We can't change the broadcast address though
128                 for (i=0; i<nbip; i++) {
129                         int j;
130                         __u32 nxip = vc_data.nx_mask_pair[i].ip;
131                         for (j=0; j<nxi->nbipv4; j++) {
132                                 if (nxip == nxi->ipv4[j]) {
133                                         found++;
134                                         break;
135                                 }
136                         }
137                 }
138                 if ((found == nbip) &&
139                         (vc_data.broadcast == nxi->v4_bcast))
140                         err = 0;
141         }
142         if (err)
143                 return err;
144
145         new_nxi = create_nx_info();
146         if (!new_nxi)
147                 return -EINVAL;
148
149         new_nxi->nbipv4 = nbip;
150         for (i=0; i<nbip; i++) {
151                 new_nxi->ipv4[i] = vc_data.nx_mask_pair[i].ip;
152                 new_nxi->mask[i] = vc_data.nx_mask_pair[i].mask;
153         }
154         new_nxi->v4_bcast = vc_data.broadcast;
155         current->nx_info = new_nxi;
156         current->nid = new_nxi->nx_id;
157         put_nx_info(nxi);
158         return 0;
159 }
160
161