summaryrefslogtreecommitdiff
path: root/lwip/iioctl-ops.c
blob: 041da106d34b36ae8e823ebbc95d85cc02dbd69e (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
   Copyright (C) 2000, 2007, 2017 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Ioctls for network device configuration */

#include <lwip_iioctl_S.h>

#include <lwip/sockets.h>
#include <lwip/inet.h>
#include <device/device.h>
#include <device/net_status.h>

#include <lwip-hurd.h>
#include <lwip-util.h>
#include <netif/ifcommon.h>

/* Get the interface from its name */
static struct netif *
get_if (char *name)
{
  char ifname[IFNAMSIZ];
  struct netif *netif;

  memcpy (ifname, name, IFNAMSIZ - 1);
  ifname[IFNAMSIZ - 1] = 0;

  for (netif = netif_list; netif != 0; netif = netif->next)
    {
      if (strcmp (netif_get_state (netif)->devname, ifname) == 0)
	break;
    }

  return netif;
}

enum siocgif_type
{
  ADDR,
  NETMASK,
  DSTADDR,
  BRDADDR
};

#define SIOCGIF(name, type)						\
  kern_return_t								\
  lwip_S_iioctl_siocgif##name (struct sock_user *user,                       \
        ifname_t ifnam,				\
        sockaddr_t *addr)				\
  {									\
    return siocgifXaddr (user, ifnam, addr, type);			\
  }

/* Get some sockaddr type of info.  */
static kern_return_t
siocgifXaddr (struct sock_user *user,
	      ifname_t ifnam, sockaddr_t * addr, enum siocgif_type type)
{
  error_t err = 0;
  struct sockaddr_in *sin = (struct sockaddr_in *) addr;
  size_t buflen = sizeof (struct sockaddr);
  struct netif *netif;
  uint32_t addrs[4];

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (ifnam);
  if (!netif)
    return ENODEV;

  if (type == DSTADDR)
    return EOPNOTSUPP;

  /* We're only interested in geting the address family */
  err = lwip_getsockname (user->sock->sockno, addr, (socklen_t *) & buflen);
  if (err)
    return err;

  if (sin->sin_family != AF_INET)
    err = EINVAL;
  else
    {
      inquire_device (netif, &addrs[0], &addrs[1], &addrs[2], &addrs[3], 0, 0,
		      0);
      sin->sin_addr.s_addr = addrs[type];
    }

  return err;
}

#define SIOCSIF(name, type)						\
  kern_return_t								\
  lwip_S_iioctl_siocsif##name (struct sock_user *user,                       \
			  const ifname_t ifnam,				\
			  sockaddr_t addr)				\
  {									\
    return siocsifXaddr (user, ifnam, &addr, type);			\
  }

/* Set some sockaddr type of info.  */
static kern_return_t
siocsifXaddr (struct sock_user *user,
	      const ifname_t ifnam, sockaddr_t * addr, enum siocgif_type type)
{
  error_t err = 0;
  struct sockaddr_in sin;
  size_t buflen = sizeof (struct sockaddr_in);
  struct netif *netif;
  uint32_t ipv4_addrs[5];

  if (!user)
    return EOPNOTSUPP;

  if (!user->isroot)
    return EPERM;

  netif = get_if (ifnam);

  if (!netif)
    return ENODEV;

  if (type == DSTADDR || type == BRDADDR)
    return EOPNOTSUPP;

  err = lwip_getsockname (user->sock->sockno,
			  (sockaddr_t *) & sin, (socklen_t *) & buflen);
  if (err)
    return err;

  if (sin.sin_family != AF_INET)
    err = EINVAL;
  else
    {
      inquire_device (netif, &ipv4_addrs[0], &ipv4_addrs[1],
		      &ipv4_addrs[2], &ipv4_addrs[3], &ipv4_addrs[4], 0, 0);

      ipv4_addrs[type] = ((struct sockaddr_in *) addr)->sin_addr.s_addr;

      err = configure_device (netif, ipv4_addrs[0], ipv4_addrs[1],
			      ipv4_addrs[2], ipv4_addrs[3], ipv4_addrs[4], 0,
			      0);
    }

  return err;
}

/* 12 SIOCSIFADDR -- Set address of a network interface.  */
SIOCSIF (addr, ADDR);

/* 14 SIOCSIFDSTADDR -- Set point-to-point (peer) address of a network interface.  */
SIOCSIF (dstaddr, DSTADDR);

/* 16 SIOCSIFFLAGS -- Set flags of a network interface.  */
kern_return_t
lwip_S_iioctl_siocsifflags (struct sock_user * user,
			    const ifname_t ifnam,
			    short flags)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (ifnam);

  if (!user->isroot)
    err = EPERM;
  else if (!netif)
    err = ENODEV;
  else
    err = if_change_flags (netif, flags);

  return err;
}

/* 17 SIOCGIFFLAGS -- Get flags of a network interface.  */
kern_return_t
lwip_S_iioctl_siocgifflags (struct sock_user * user, ifname_t name, short *flags)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (name);
  if (!netif)
    err = ENODEV;
  else
    {
      *flags = netif_get_state (netif)->flags;
    }

  return err;
}

/* 19 SIOCSIFBRDADDR -- Set broadcast address of a network interface.  */
SIOCSIF (brdaddr, BRDADDR);

/* 22 SIOCSIFNETMASK -- Set netmask of a network interface.  */
SIOCSIF (netmask, NETMASK);

/* 23 SIOCGIFMETRIC -- Get metric of a network interface.  */
kern_return_t
lwip_S_iioctl_siocgifmetric (struct sock_user * user,
			     ifname_t ifnam,
			     int *metric)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (ifnam);
  if (!netif)
    err = ENODEV;
  else
    {
      *metric = 0;		/* Not supported.  */
    }

  return err;
}

/* 24 SIOCSIFMETRIC -- Set metric of a network interface.  */
kern_return_t
lwip_S_iioctl_siocsifmetric (struct sock_user * user,
			     const ifname_t ifnam,
			     int metric)
{
  return EOPNOTSUPP;
}

/* 25 SIOCDIFADDR -- Delete interface address.  */
kern_return_t
lwip_S_iioctl_siocdifaddr (struct sock_user * user,
			   const ifname_t ifnam,
			   sockaddr_t addr)
{
  return EOPNOTSUPP;
}

/* 33 SIOCGIFADDR -- Get address of a network interface.  */
SIOCGIF (addr, ADDR);

/* 34 SIOCGIFDSTADDR -- Get point-to-point address of a network interface.  */
SIOCGIF (dstaddr, DSTADDR);

/* 35 SIOCGIFBRDADDR -- Get broadcast address of a network interface.  */
SIOCGIF (brdaddr, BRDADDR);

/* 37 SIOCGIFNETMASK -- Get netmask of a network interface.  */
SIOCGIF (netmask, NETMASK);

/* 39 SIOCGIFHWADDR -- Get the hardware address of a network interface.  */
error_t
lwip_S_iioctl_siocgifhwaddr (struct sock_user * user,
			     ifname_t ifname,
			     sockaddr_t * addr)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (ifname);
  if (!netif)
    err = ENODEV;
  else
    {
      memcpy (addr->sa_data, netif->hwaddr, netif->hwaddr_len);
      addr->sa_family = netif_get_state (netif)->type;
    }

  return err;
}

/* 51 SIOCGIFMTU -- Get mtu of a network interface.  */
error_t
lwip_S_iioctl_siocgifmtu (struct sock_user * user, ifname_t ifnam, int *mtu)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  netif = get_if (ifnam);
  if (!netif)
    err = ENODEV;
  else
    {
      *mtu = netif->mtu;
    }

  return err;
}

/* 51 SIOCSIFMTU -- Set mtu of a network interface.  */
error_t
lwip_S_iioctl_siocsifmtu (struct sock_user * user, const ifname_t ifnam, int mtu)
{
  error_t err = 0;
  struct netif *netif;

  if (!user)
    return EOPNOTSUPP;

  if (!user->isroot)
    return EPERM;

  if (mtu <= 0)
    return EINVAL;

  netif = get_if (ifnam);
  if (!netif)
    err = ENODEV;
  else
    {
      err = netif_get_state (netif)->update_mtu (netif, mtu);
    }

  return err;
}

/* 100 SIOCGIFINDEX -- Get index number of a network interface.  */
error_t
lwip_S_iioctl_siocgifindex (struct sock_user * user,
			    ifname_t ifnam,
			    int *index)
{
  error_t err = 0;
  struct netif *netif;
  int i;

  if (!user)
    return EOPNOTSUPP;

  i = 1;			/* The first index must be 1 */
  for (netif = netif_list; netif != 0; netif = netif->next)
    {
      if (strcmp (netif_get_state (netif)->devname, ifnam) == 0)
	{
	  *index = i;
	  break;
	}

      i++;
    }

  if (!netif)
    err = ENODEV;

  return err;
}

/* 101 SIOCGIFNAME -- Get name of a network interface from index number.  */
error_t
lwip_S_iioctl_siocgifname (struct sock_user * user,
			   ifname_t ifnam,
			   int *index)
{
  error_t err = 0;
  struct netif *netif;
  int i;

  if (!user)
    return EOPNOTSUPP;

  if (*index < 0)
    return EINVAL;

  i = 1;			/* The first index is 1 */
  for (netif = netif_list; netif != 0; netif = netif->next)
    {
      if (i == *index)
	break;

      i++;
    }

  if (!netif)
    err = ENODEV;
  else
    {
      strncpy (ifnam, netif_get_state (netif)->devname, IFNAMSIZ);
      ifnam[IFNAMSIZ - 1] = '\0';
    }

  return err;
}