File: sample_enc.c

package info (click to toggle)
wireless-tools 30~pre9-12
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,632 kB
  • ctags: 4,575
  • sloc: ansic: 13,804; sh: 209; makefile: 154
file content (191 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Note : this particular snipset of code is available under
 * the LGPL, MPL or BSD license (at your choice).
 * Jean II
 */

/* --------------------------- INCLUDE --------------------------- */

#define MAX_KEY_SIZE	16
#define	MAX_KEYS	8
int	key_on = 0;
int	key_open = 1;
int	key_current = 0;
char	key_table[MAX_KEYS][MAX_KEY_SIZE];
int	key_size[MAX_KEYS];

/* --------------------------- HANDLERS --------------------------- */

static int ioctl_set_encode(struct net_device *dev,
			    struct iw_request_info *info,
			    struct iw_point *erq,
			    char *key)
{
  int	index = (erq->flags & IW_ENCODE_INDEX) - 1;

  if (erq->length > 0)
    {
      /* Check the size of the key */
      if(erq->length > MAX_KEY_SIZE)
	return(-EINVAL);

      /* Check the index */
      if((index < 0) || (index >= MAX_KEYS))
	index = key_current;

      /* Copy the key in the driver */
      memcpy(key_table[index], key, erq->length);
      key_size[index] = erq->length;
      key_on = 1;
    }
  else
    {
      /* Do we want to just set the current key ? */
      if((index >= 0) && (index < MAX_KEYS))
	{
	  if(key_size[index] > 0)
	    {
	      key_current = index;
	      key_on = 1;
	    }
	  else
	    return(-EINVAL);
	}
    }

  /* Read the flags */
  if(erq->flags & IW_ENCODE_DISABLED)
    key_on = 0;		/* disable encryption */
  if(erq->flags & IW_ENCODE_RESTRICTED)
    key_open = 0;	/* disable open mode */
  if(erq->flags & IW_ENCODE_OPEN)
    key_open = 1;	/* enable open mode */

  return(0);
}

static int ioctl_get_encode(struct net_device *dev,
			    struct iw_request_info *info,
			    struct iw_point *erq,
			    char *key)
{
  int	index = (erq->flags & IW_ENCODE_INDEX) - 1;

  /* Set the flags */
  erq->flags = 0;
  if(key_on == 0)
    erq->flags |= IW_ENCODE_DISABLED;
  if(key_open == 0)
    erq->flags |= IW_ENCODE_RESTRICTED;
  else
    erq->flags |= IW_ENCODE_OPEN;

  /* Which key do we want */
  if((index < 0) || (index >= MAX_KEYS))
    index = key_current;
  erq->flags |= index + 1;

  /* Copy the key to the user buffer */
  erq->length = key_size[index];
  memcpy(key, key_table[index], key_size[index]);

  return(0);
}

static int ioctl_get_range(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *rrq,
			   char *extra)
{
  struct iw_range *range = (struct iw_range *) extra;

  rrq->length = sizeof(struct iw_range);

  memset(range, 0, sizeof(struct iw_range));

#if WIRELESS_EXT > 10
  /* Version we are compiled with */
  range->we_version_compiled = WIRELESS_EXT;
  /* Minimum version we recommend */
  range->we_version_source = 8;
#endif /* WIRELESS_EXT > 10 */

#if WIRELESS_EXT > 8
  range->encoding_size[0] = 8;	/* DES = 64 bits key */
  range->encoding_size[1] = 16;
  range->num_encoding_sizes = 2;
  range->max_encoding_tokens = 8;
#endif /* WIRELESS_EXT > 8 */
  return(0);
}

/* --------------------------- BINDING --------------------------- */

#if WIRELESS_EXT > 12
static const iw_handler		handler_table[] =
{
	...
	(iw_handler) ioctl_set_encode,		/* SIOCSIWENCODE */
	(iw_handler) ioctl_get_encode,		/* SIOCGIWENCODE */
};
#else	/* WIRELESS_EXT < 12 */
static int
do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
  struct iwreq *wrq = (struct iwreq *) ifr;
  int		err = 0;

  switch (cmd)
    {
#if WIRELESS_EXT > 8
    case SIOCSIWENCODE:
      {
	char keybuf[MAX_KEY_SIZE];
	if(wrq->u.encoding.pointer)
	  {
	    /* We actually have a key to set */
	    if(wrq->u.encoding.length > MAX_KEY_SIZE)
	      {
		err = -E2BIG;
		break;
	      }
	    if(copy_from_user(keybuf, wrq->u.encoding.pointer,
			      wrq->u.encoding.length))
	      {
		err = -EFAULT;
		break;
	      }
	  }
	else
	  if(wrq->u.encoding.length != 0)
	    {
	      err = -EINVAL;
	      break;
	    }
	err = ioctl_set_encode(dev, NULL, &(wrq->u.encoding), keybuf);
      }
      break;

    case SIOCGIWENCODE:
      /* only super-user can see encryption key */
      if(! capable(CAP_NET_ADMIN))
	{
	  err = -EPERM;
	  break;
	}
      {
	char keybuf[MAX_KEY_SIZE];
	err = ioctl_get_encode(dev, NULL, &(wrq->u.encoding), keybuf);
	if(wrq->u.encoding.pointer)
	  {
	    if (copy_to_user(wrq->u.encoding.pointer, keybuf,
			     wrq->u.encoding.length))
	      err= -EFAULT;
	  }
      }
      break;
#endif	/* WIRELESS_EXT > 8 */
    }
  return(err);
}
#endif	/* WIRELESS_EXT < 12 */