blob: 77f04e71100fc7de6c508a1ece2d375d16390331 [file] [log] [blame]
Cong Wang96b45cb2013-02-15 22:20:46 +00001#include <linux/kmod.h>
2#include <linux/netdevice.h>
3#include <linux/etherdevice.h>
4#include <linux/rtnetlink.h>
5#include <linux/net_tstamp.h>
6#include <linux/wireless.h>
7#include <net/wext.h>
8
9/*
10 * Map an interface index to its name (SIOCGIFNAME)
11 */
12
13/*
14 * We need this ioctl for efficient implementation of the
15 * if_indextoname() function required by the IPv6 API. Without
16 * it, we would have to search all the interfaces to find a
17 * match. --pb
18 */
19
20static int dev_ifname(struct net *net, struct ifreq __user *arg)
21{
Cong Wang96b45cb2013-02-15 22:20:46 +000022 struct ifreq ifr;
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +020023 int error;
Cong Wang96b45cb2013-02-15 22:20:46 +000024
25 /*
26 * Fetch the caller's info block.
27 */
28
29 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
30 return -EFAULT;
31
Nicolas Schichan5dbe7c12013-06-26 17:23:42 +020032 error = netdev_get_name(net, ifr.ifr_name, ifr.ifr_ifindex);
33 if (error)
34 return error;
Cong Wang96b45cb2013-02-15 22:20:46 +000035
36 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
37 return -EFAULT;
38 return 0;
39}
40
41static gifconf_func_t *gifconf_list[NPROTO];
42
43/**
44 * register_gifconf - register a SIOCGIF handler
45 * @family: Address family
46 * @gifconf: Function handler
47 *
48 * Register protocol dependent address dumping routines. The handler
49 * that is passed must not be freed or reused until it has been replaced
50 * by another handler.
51 */
52int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
53{
54 if (family >= NPROTO)
55 return -EINVAL;
56 gifconf_list[family] = gifconf;
57 return 0;
58}
59EXPORT_SYMBOL(register_gifconf);
60
61/*
62 * Perform a SIOCGIFCONF call. This structure will change
63 * size eventually, and there is nothing I can do about it.
64 * Thus we will need a 'compatibility mode'.
65 */
66
67static int dev_ifconf(struct net *net, char __user *arg)
68{
69 struct ifconf ifc;
70 struct net_device *dev;
71 char __user *pos;
72 int len;
73 int total;
74 int i;
75
76 /*
77 * Fetch the caller's info block.
78 */
79
80 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
81 return -EFAULT;
82
83 pos = ifc.ifc_buf;
84 len = ifc.ifc_len;
85
86 /*
87 * Loop over the interfaces, and write an info block for each.
88 */
89
90 total = 0;
91 for_each_netdev(net, dev) {
92 for (i = 0; i < NPROTO; i++) {
93 if (gifconf_list[i]) {
94 int done;
95 if (!pos)
96 done = gifconf_list[i](dev, NULL, 0);
97 else
98 done = gifconf_list[i](dev, pos + total,
99 len - total);
100 if (done < 0)
101 return -EFAULT;
102 total += done;
103 }
104 }
105 }
106
107 /*
108 * All done. Write the updated control block back to the caller.
109 */
110 ifc.ifc_len = total;
111
112 /*
113 * Both BSD and Solaris return 0 here, so we do too.
114 */
115 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
116}
117
118/*
119 * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
120 */
121static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
122{
123 int err;
124 struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
125
126 if (!dev)
127 return -ENODEV;
128
129 switch (cmd) {
130 case SIOCGIFFLAGS: /* Get interface flags */
131 ifr->ifr_flags = (short) dev_get_flags(dev);
132 return 0;
133
134 case SIOCGIFMETRIC: /* Get the metric on the interface
135 (currently unused) */
136 ifr->ifr_metric = 0;
137 return 0;
138
139 case SIOCGIFMTU: /* Get the MTU of a device */
140 ifr->ifr_mtu = dev->mtu;
141 return 0;
142
143 case SIOCGIFHWADDR:
144 if (!dev->addr_len)
Fabian Frederick54aeba72014-11-17 22:23:17 +0100145 memset(ifr->ifr_hwaddr.sa_data, 0,
146 sizeof(ifr->ifr_hwaddr.sa_data));
Cong Wang96b45cb2013-02-15 22:20:46 +0000147 else
148 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
Fabian Frederick54aeba72014-11-17 22:23:17 +0100149 min(sizeof(ifr->ifr_hwaddr.sa_data),
150 (size_t)dev->addr_len));
Cong Wang96b45cb2013-02-15 22:20:46 +0000151 ifr->ifr_hwaddr.sa_family = dev->type;
152 return 0;
153
154 case SIOCGIFSLAVE:
155 err = -EINVAL;
156 break;
157
158 case SIOCGIFMAP:
159 ifr->ifr_map.mem_start = dev->mem_start;
160 ifr->ifr_map.mem_end = dev->mem_end;
161 ifr->ifr_map.base_addr = dev->base_addr;
162 ifr->ifr_map.irq = dev->irq;
163 ifr->ifr_map.dma = dev->dma;
164 ifr->ifr_map.port = dev->if_port;
165 return 0;
166
167 case SIOCGIFINDEX:
168 ifr->ifr_ifindex = dev->ifindex;
169 return 0;
170
171 case SIOCGIFTXQLEN:
172 ifr->ifr_qlen = dev->tx_queue_len;
173 return 0;
174
175 default:
176 /* dev_ioctl() should ensure this case
177 * is never reached
178 */
179 WARN_ON(1);
180 err = -ENOTTY;
181 break;
182
183 }
184 return err;
185}
186
187static int net_hwtstamp_validate(struct ifreq *ifr)
188{
189 struct hwtstamp_config cfg;
190 enum hwtstamp_tx_types tx_type;
191 enum hwtstamp_rx_filters rx_filter;
192 int tx_type_valid = 0;
193 int rx_filter_valid = 0;
194
195 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
196 return -EFAULT;
197
198 if (cfg.flags) /* reserved for future extensions */
199 return -EINVAL;
200
201 tx_type = cfg.tx_type;
202 rx_filter = cfg.rx_filter;
203
204 switch (tx_type) {
205 case HWTSTAMP_TX_OFF:
206 case HWTSTAMP_TX_ON:
207 case HWTSTAMP_TX_ONESTEP_SYNC:
208 tx_type_valid = 1;
209 break;
210 }
211
212 switch (rx_filter) {
213 case HWTSTAMP_FILTER_NONE:
214 case HWTSTAMP_FILTER_ALL:
215 case HWTSTAMP_FILTER_SOME:
216 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
217 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
218 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
219 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
220 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
221 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
222 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
223 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
224 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
225 case HWTSTAMP_FILTER_PTP_V2_EVENT:
226 case HWTSTAMP_FILTER_PTP_V2_SYNC:
227 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvarb8210a92017-05-19 17:52:35 +0200228 case HWTSTAMP_FILTER_NTP_ALL:
Miroslav Lichvare3412572017-05-19 17:52:36 +0200229 rx_filter_valid = 1;
Miroslav Lichvarb8210a92017-05-19 17:52:35 +0200230 break;
Cong Wang96b45cb2013-02-15 22:20:46 +0000231 }
232
233 if (!tx_type_valid || !rx_filter_valid)
234 return -ERANGE;
235
236 return 0;
237}
238
239/*
240 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
241 */
242static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
243{
244 int err;
245 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
246 const struct net_device_ops *ops;
247
248 if (!dev)
249 return -ENODEV;
250
251 ops = dev->netdev_ops;
252
253 switch (cmd) {
254 case SIOCSIFFLAGS: /* Set interface flags */
255 return dev_change_flags(dev, ifr->ifr_flags);
256
257 case SIOCSIFMETRIC: /* Set the metric on the interface
258 (currently unused) */
259 return -EOPNOTSUPP;
260
261 case SIOCSIFMTU: /* Set the MTU of a device */
262 return dev_set_mtu(dev, ifr->ifr_mtu);
263
264 case SIOCSIFHWADDR:
265 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
266
267 case SIOCSIFHWBROADCAST:
268 if (ifr->ifr_hwaddr.sa_family != dev->type)
269 return -EINVAL;
270 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
Fabian Frederick54aeba72014-11-17 22:23:17 +0100271 min(sizeof(ifr->ifr_hwaddr.sa_data),
272 (size_t)dev->addr_len));
Cong Wang96b45cb2013-02-15 22:20:46 +0000273 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
274 return 0;
275
276 case SIOCSIFMAP:
277 if (ops->ndo_set_config) {
278 if (!netif_device_present(dev))
279 return -ENODEV;
280 return ops->ndo_set_config(dev, &ifr->ifr_map);
281 }
282 return -EOPNOTSUPP;
283
284 case SIOCADDMULTI:
285 if (!ops->ndo_set_rx_mode ||
286 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
287 return -EINVAL;
288 if (!netif_device_present(dev))
289 return -ENODEV;
290 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
291
292 case SIOCDELMULTI:
293 if (!ops->ndo_set_rx_mode ||
294 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
295 return -EINVAL;
296 if (!netif_device_present(dev))
297 return -ENODEV;
298 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
299
300 case SIOCSIFTXQLEN:
301 if (ifr->ifr_qlen < 0)
302 return -EINVAL;
303 dev->tx_queue_len = ifr->ifr_qlen;
304 return 0;
305
306 case SIOCSIFNAME:
307 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
308 return dev_change_name(dev, ifr->ifr_newname);
309
310 case SIOCSHWTSTAMP:
311 err = net_hwtstamp_validate(ifr);
312 if (err)
313 return err;
314 /* fall through */
315
316 /*
317 * Unknown or private ioctl
318 */
319 default:
320 if ((cmd >= SIOCDEVPRIVATE &&
321 cmd <= SIOCDEVPRIVATE + 15) ||
322 cmd == SIOCBONDENSLAVE ||
323 cmd == SIOCBONDRELEASE ||
324 cmd == SIOCBONDSETHWADDR ||
325 cmd == SIOCBONDSLAVEINFOQUERY ||
326 cmd == SIOCBONDINFOQUERY ||
327 cmd == SIOCBONDCHANGEACTIVE ||
328 cmd == SIOCGMIIPHY ||
329 cmd == SIOCGMIIREG ||
330 cmd == SIOCSMIIREG ||
331 cmd == SIOCBRADDIF ||
332 cmd == SIOCBRDELIF ||
333 cmd == SIOCSHWTSTAMP ||
Ben Hutchingsfd468c72013-11-14 01:19:29 +0000334 cmd == SIOCGHWTSTAMP ||
Cong Wang96b45cb2013-02-15 22:20:46 +0000335 cmd == SIOCWANDEV) {
336 err = -EOPNOTSUPP;
337 if (ops->ndo_do_ioctl) {
338 if (netif_device_present(dev))
339 err = ops->ndo_do_ioctl(dev, ifr, cmd);
340 else
341 err = -ENODEV;
342 }
343 } else
344 err = -EINVAL;
345
346 }
347 return err;
348}
349
350/**
351 * dev_load - load a network module
352 * @net: the applicable net namespace
353 * @name: name of interface
354 *
355 * If a network interface is not present and the process has suitable
356 * privileges this function loads the module. If module loading is not
357 * available in this kernel then it becomes a nop.
358 */
359
360void dev_load(struct net *net, const char *name)
361{
362 struct net_device *dev;
363 int no_module;
364
365 rcu_read_lock();
366 dev = dev_get_by_name_rcu(net, name);
367 rcu_read_unlock();
368
369 no_module = !dev;
370 if (no_module && capable(CAP_NET_ADMIN))
371 no_module = request_module("netdev-%s", name);
Daniel Borkmanne0208362014-09-02 23:30:05 +0200372 if (no_module && capable(CAP_SYS_MODULE))
373 request_module("%s", name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000374}
375EXPORT_SYMBOL(dev_load);
376
377/*
378 * This function handles all "interface"-type I/O control requests. The actual
379 * 'doing' part of this is dev_ifsioc above.
380 */
381
382/**
383 * dev_ioctl - network device ioctl
384 * @net: the applicable net namespace
385 * @cmd: command to issue
386 * @arg: pointer to a struct ifreq in user space
387 *
388 * Issue ioctl functions to devices. This is normally called by the
389 * user space syscall interfaces but can sometimes be useful for
390 * other purposes. The return value is the return from the syscall if
391 * positive or a negative errno code on error.
392 */
393
394int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
395{
396 struct ifreq ifr;
397 int ret;
398 char *colon;
399
400 /* One special case: SIOCGIFCONF takes ifconf argument
401 and requires shared lock, because it sleeps writing
402 to user space.
403 */
404
405 if (cmd == SIOCGIFCONF) {
406 rtnl_lock();
407 ret = dev_ifconf(net, (char __user *) arg);
408 rtnl_unlock();
409 return ret;
410 }
411 if (cmd == SIOCGIFNAME)
412 return dev_ifname(net, (struct ifreq __user *)arg);
413
414 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
415 return -EFAULT;
416
417 ifr.ifr_name[IFNAMSIZ-1] = 0;
418
419 colon = strchr(ifr.ifr_name, ':');
420 if (colon)
421 *colon = 0;
422
423 /*
424 * See which interface the caller is talking about.
425 */
426
427 switch (cmd) {
428 /*
429 * These ioctl calls:
430 * - can be done by all.
431 * - atomic and do not require locking.
432 * - return a value
433 */
434 case SIOCGIFFLAGS:
435 case SIOCGIFMETRIC:
436 case SIOCGIFMTU:
437 case SIOCGIFHWADDR:
438 case SIOCGIFSLAVE:
439 case SIOCGIFMAP:
440 case SIOCGIFINDEX:
441 case SIOCGIFTXQLEN:
442 dev_load(net, ifr.ifr_name);
443 rcu_read_lock();
444 ret = dev_ifsioc_locked(net, &ifr, cmd);
445 rcu_read_unlock();
446 if (!ret) {
447 if (colon)
448 *colon = ':';
449 if (copy_to_user(arg, &ifr,
450 sizeof(struct ifreq)))
451 ret = -EFAULT;
452 }
453 return ret;
454
455 case SIOCETHTOOL:
456 dev_load(net, ifr.ifr_name);
457 rtnl_lock();
458 ret = dev_ethtool(net, &ifr);
459 rtnl_unlock();
460 if (!ret) {
461 if (colon)
462 *colon = ':';
463 if (copy_to_user(arg, &ifr,
464 sizeof(struct ifreq)))
465 ret = -EFAULT;
466 }
467 return ret;
468
469 /*
470 * These ioctl calls:
471 * - require superuser power.
472 * - require strict serialization.
473 * - return a value
474 */
475 case SIOCGMIIPHY:
476 case SIOCGMIIREG:
477 case SIOCSIFNAME:
478 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
479 return -EPERM;
480 dev_load(net, ifr.ifr_name);
481 rtnl_lock();
482 ret = dev_ifsioc(net, &ifr, cmd);
483 rtnl_unlock();
484 if (!ret) {
485 if (colon)
486 *colon = ':';
487 if (copy_to_user(arg, &ifr,
488 sizeof(struct ifreq)))
489 ret = -EFAULT;
490 }
491 return ret;
492
493 /*
494 * These ioctl calls:
495 * - require superuser power.
496 * - require strict serialization.
497 * - do not return a value
498 */
499 case SIOCSIFMAP:
500 case SIOCSIFTXQLEN:
501 if (!capable(CAP_NET_ADMIN))
502 return -EPERM;
503 /* fall through */
504 /*
505 * These ioctl calls:
506 * - require local superuser power.
507 * - require strict serialization.
508 * - do not return a value
509 */
510 case SIOCSIFFLAGS:
511 case SIOCSIFMETRIC:
512 case SIOCSIFMTU:
513 case SIOCSIFHWADDR:
514 case SIOCSIFSLAVE:
515 case SIOCADDMULTI:
516 case SIOCDELMULTI:
517 case SIOCSIFHWBROADCAST:
518 case SIOCSMIIREG:
519 case SIOCBONDENSLAVE:
520 case SIOCBONDRELEASE:
521 case SIOCBONDSETHWADDR:
522 case SIOCBONDCHANGEACTIVE:
523 case SIOCBRADDIF:
524 case SIOCBRDELIF:
525 case SIOCSHWTSTAMP:
526 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
527 return -EPERM;
528 /* fall through */
529 case SIOCBONDSLAVEINFOQUERY:
530 case SIOCBONDINFOQUERY:
531 dev_load(net, ifr.ifr_name);
532 rtnl_lock();
533 ret = dev_ifsioc(net, &ifr, cmd);
534 rtnl_unlock();
535 return ret;
536
537 case SIOCGIFMEM:
538 /* Get the per device memory space. We can add this but
539 * currently do not support it */
540 case SIOCSIFMEM:
541 /* Set the per device memory buffer space.
542 * Not applicable in our case */
543 case SIOCSIFLINK:
544 return -ENOTTY;
545
546 /*
547 * Unknown or private ioctl.
548 */
549 default:
550 if (cmd == SIOCWANDEV ||
Ben Hutchingsfd468c72013-11-14 01:19:29 +0000551 cmd == SIOCGHWTSTAMP ||
Cong Wang96b45cb2013-02-15 22:20:46 +0000552 (cmd >= SIOCDEVPRIVATE &&
553 cmd <= SIOCDEVPRIVATE + 15)) {
554 dev_load(net, ifr.ifr_name);
555 rtnl_lock();
556 ret = dev_ifsioc(net, &ifr, cmd);
557 rtnl_unlock();
558 if (!ret && copy_to_user(arg, &ifr,
559 sizeof(struct ifreq)))
560 ret = -EFAULT;
561 return ret;
562 }
563 /* Take care of Wireless Extensions */
564 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
565 return wext_handle_ioctl(net, &ifr, cmd, arg);
566 return -ENOTTY;
567 }
568}