OSDN Git Service

Add Android.mk
[android-x86/external-wireless-tools.git] / wireless_tools / sample_enc.c
1 /* Note : this particular snipset of code is available under
2  * the LGPL, MPL or BSD license (at your choice).
3  * Jean II
4  */
5
6 /* --------------------------- INCLUDE --------------------------- */
7
8 #define MAX_KEY_SIZE    16
9 #define MAX_KEYS        8
10 int     key_on = 0;
11 int     key_open = 1;
12 int     key_current = 0;
13 char    key_table[MAX_KEYS][MAX_KEY_SIZE];
14 int     key_size[MAX_KEYS];
15
16 /* --------------------------- HANDLERS --------------------------- */
17
18 static int ioctl_set_encode(struct net_device *dev,
19                             struct iw_request_info *info,
20                             struct iw_point *erq,
21                             char *key)
22 {
23   int   index = (erq->flags & IW_ENCODE_INDEX) - 1;
24
25   if (erq->length > 0)
26     {
27       /* Check the size of the key */
28       if(erq->length > MAX_KEY_SIZE)
29         return(-EINVAL);
30
31       /* Check the index */
32       if((index < 0) || (index >= MAX_KEYS))
33         index = key_current;
34
35       /* Copy the key in the driver */
36       memcpy(key_table[index], key, erq->length);
37       key_size[index] = erq->length;
38       key_on = 1;
39     }
40   else
41     {
42       /* Do we want to just set the current key ? */
43       if((index >= 0) && (index < MAX_KEYS))
44         {
45           if(key_size[index] > 0)
46             {
47               key_current = index;
48               key_on = 1;
49             }
50           else
51             return(-EINVAL);
52         }
53     }
54
55   /* Read the flags */
56   if(erq->flags & IW_ENCODE_DISABLED)
57     key_on = 0;         /* disable encryption */
58   if(erq->flags & IW_ENCODE_RESTRICTED)
59     key_open = 0;       /* disable open mode */
60   if(erq->flags & IW_ENCODE_OPEN)
61     key_open = 1;       /* enable open mode */
62
63   return(0);
64 }
65
66 static int ioctl_get_encode(struct net_device *dev,
67                             struct iw_request_info *info,
68                             struct iw_point *erq,
69                             char *key)
70 {
71   int   index = (erq->flags & IW_ENCODE_INDEX) - 1;
72
73   /* Set the flags */
74   erq->flags = 0;
75   if(key_on == 0)
76     erq->flags |= IW_ENCODE_DISABLED;
77   if(key_open == 0)
78     erq->flags |= IW_ENCODE_RESTRICTED;
79   else
80     erq->flags |= IW_ENCODE_OPEN;
81
82   /* Which key do we want */
83   if((index < 0) || (index >= MAX_KEYS))
84     index = key_current;
85   erq->flags |= index + 1;
86
87   /* Copy the key to the user buffer */
88   erq->length = key_size[index];
89   memcpy(key, key_table[index], key_size[index]);
90
91   return(0);
92 }
93
94 static int ioctl_get_range(struct net_device *dev,
95                            struct iw_request_info *info,
96                            struct iw_point *rrq,
97                            char *extra)
98 {
99   struct iw_range *range = (struct iw_range *) extra;
100
101   rrq->length = sizeof(struct iw_range);
102
103   memset(range, 0, sizeof(struct iw_range));
104
105 #if WIRELESS_EXT > 10
106   /* Version we are compiled with */
107   range->we_version_compiled = WIRELESS_EXT;
108   /* Minimum version we recommend */
109   range->we_version_source = 8;
110 #endif /* WIRELESS_EXT > 10 */
111
112 #if WIRELESS_EXT > 8
113   range->encoding_size[0] = 8;  /* DES = 64 bits key */
114   range->encoding_size[1] = 16;
115   range->num_encoding_sizes = 2;
116   range->max_encoding_tokens = 8;
117 #endif /* WIRELESS_EXT > 8 */
118   return(0);
119 }
120
121 /* --------------------------- BINDING --------------------------- */
122
123 #if WIRELESS_EXT > 12
124 static const iw_handler         handler_table[] =
125 {
126         ...
127         (iw_handler) ioctl_set_encode,          /* SIOCSIWENCODE */
128         (iw_handler) ioctl_get_encode,          /* SIOCGIWENCODE */
129 };
130 #else   /* WIRELESS_EXT < 12 */
131 static int
132 do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
133 {
134   struct iwreq *wrq = (struct iwreq *) ifr;
135   int           err = 0;
136
137   switch (cmd)
138     {
139 #if WIRELESS_EXT > 8
140     case SIOCSIWENCODE:
141       {
142         char keybuf[MAX_KEY_SIZE];
143         if(wrq->u.encoding.pointer)
144           {
145             /* We actually have a key to set */
146             if(wrq->u.encoding.length > MAX_KEY_SIZE)
147               {
148                 err = -E2BIG;
149                 break;
150               }
151             if(copy_from_user(keybuf, wrq->u.encoding.pointer,
152                               wrq->u.encoding.length))
153               {
154                 err = -EFAULT;
155                 break;
156               }
157           }
158         else
159           if(wrq->u.encoding.length != 0)
160             {
161               err = -EINVAL;
162               break;
163             }
164         err = ioctl_set_encode(dev, NULL, &(wrq->u.encoding), keybuf);
165       }
166       break;
167
168     case SIOCGIWENCODE:
169       /* only super-user can see encryption key */
170       if(! capable(CAP_NET_ADMIN))
171         {
172           err = -EPERM;
173           break;
174         }
175       {
176         char keybuf[MAX_KEY_SIZE];
177         err = ioctl_get_encode(dev, NULL, &(wrq->u.encoding), keybuf);
178         if(wrq->u.encoding.pointer)
179           {
180             if (copy_to_user(wrq->u.encoding.pointer, keybuf,
181                              wrq->u.encoding.length))
182               err= -EFAULT;
183           }
184       }
185       break;
186 #endif  /* WIRELESS_EXT > 8 */
187     }
188   return(err);
189 }
190 #endif  /* WIRELESS_EXT < 12 */
191