OSDN Git Service

v24
[android-x86/external-wireless-tools.git] / wireless_tools / sample_enc_cs.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 #define MAX_KEY_SIZE    16
7 #define MAX_KEYS        8
8 int     key_on = 0;
9 int     key_open = 1;
10 int     key_current = 0;
11 char    key_table[MAX_KEYS][MAX_KEY_SIZE];
12 int     key_size[MAX_KEYS];
13
14 #if WIRELESS_EXT > 8
15      case SIOCSIWENCODE:
16        /* Basic checking... */
17        if(wrq->u.encoding.pointer != (caddr_t) 0)
18          {
19            int  index = (wrq->u.encoding.flags & IW_ENCODE_INDEX) - 1;
20
21            /* Check the size of the key */
22            if(wrq->u.encoding.length > MAX_KEY_SIZE)
23              {
24                ret = -EINVAL;
25                break;
26              }
27
28            /* Check the index */
29            if((index < 0) || (index >= MAX_KEYS))
30              index = key_current;
31
32            /* Copy the key in the driver */
33            if(copy_from_user(key_table[index], wrq->u.encoding.pointer,
34                              wrq->u.encoding.length))
35              {
36                key_size[index] = 0;
37                ret = -EFAULT;
38                break;
39              }
40            key_size[index] = wrq->u.encoding.length;
41            key_on = 1;
42          }
43        else
44          {
45            int  index = (wrq->u.encoding.flags & IW_ENCODE_INDEX) - 1;
46            /* Do we want to just set the current key ? */
47            if((index >= 0) && (index < MAX_KEYS))
48              {
49                if(key_size[index] > 0)
50                  {
51                    key_current = index;
52                    key_on = 1;
53                  }
54                else
55                  ret = -EINVAL;
56              }
57          }
58
59        /* Read the flags */
60        if(wrq->u.encoding.flags & IW_ENCODE_DISABLED)
61          key_on = 0;    /* disable encryption */
62        if(wrq->u.encoding.flags & IW_ENCODE_RESTRICTED)
63          key_open = 0;  /* disable open mode */
64        if(wrq->u.encoding.flags & IW_ENCODE_OPEN)
65          key_open = 1;  /* enable open mode */
66
67        break;
68
69      case SIOCGIWENCODE:
70        /* only super-user can see encryption key */
71        if(!suser())
72          {
73            ret = -EPERM;
74            break;
75          }
76
77        /* Basic checking... */
78        if(wrq->u.encoding.pointer != (caddr_t) 0)
79          {
80            int  index = (wrq->u.encoding.flags & IW_ENCODE_INDEX) - 1;
81
82            /* Set the flags */
83            wrq->u.encoding.flags = 0;
84            if(key_on == 0)
85               wrq->u.encoding.flags |= IW_ENCODE_DISABLED;
86            if(key_open == 0)
87              wrq->u.encoding.flags |= IW_ENCODE_RESTRICTED;
88            else
89              wrq->u.encoding.flags |= IW_ENCODE_OPEN;
90
91            /* Which key do we want */
92            if((index < 0) || (index >= MAX_KEYS))
93              index = key_current;
94            wrq->u.encoding.flags |= index + 1;
95
96            /* Copy the key to the user buffer */
97            wrq->u.encoding.length = key_size[index];
98            if(copy_to_user(wrq->u.encoding.pointer, key_table[index],
99                            key_size[index]))
100              ret = -EFAULT;
101         }
102        break;
103 #endif  /* WIRELESS_EXT > 8 */
104
105 #if WIRELESS_EXT > 8
106           range.encoding_size[0] = 8;   /* DES = 64 bits key */
107           range.encoding_size[1] = 16;
108           range.num_encoding_sizes = 2;
109           range.max_encoding_tokens = 8;
110 #endif /* WIRELESS_EXT > 8 */