blob: 8f036a76b92ef626c185ea203b733a5712b000bb [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:
228 rx_filter_valid = 1;
229 break;
Miroslav Lichvarb8210a92017-05-19 17:52:35 +0200230 case HWTSTAMP_FILTER_NTP_ALL:
231 break;
Cong Wang96b45cb2013-02-15 22:20:46 +0000232 }
233
234 if (!tx_type_valid || !rx_filter_valid)
235 return -ERANGE;
236
237 return 0;
238}
239
240/*
241 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
242 */
243static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
244{
245 int err;
246 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
247 const struct net_device_ops *ops;
248
249 if (!dev)
250 return -ENODEV;
251
252 ops = dev->netdev_ops;
253
254 switch (cmd) {
255 case SIOCSIFFLAGS: /* Set interface flags */
256 return dev_change_flags(dev, ifr->ifr_flags);
257
258 case SIOCSIFMETRIC: /* Set the metric on the interface
259 (currently unused) */
260 return -EOPNOTSUPP;
261
262 case SIOCSIFMTU: /* Set the MTU of a device */
263 return dev_set_mtu(dev, ifr->ifr_mtu);
264
265 case SIOCSIFHWADDR:
266 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
267
268 case SIOCSIFHWBROADCAST:
269 if (ifr->ifr_hwaddr.sa_family != dev->type)
270 return -EINVAL;
271 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
Fabian Frederick54aeba72014-11-17 22:23:17 +0100272 min(sizeof(ifr->ifr_hwaddr.sa_data),
273 (size_t)dev->addr_len));
Cong Wang96b45cb2013-02-15 22:20:46 +0000274 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
275 return 0;
276
277 case SIOCSIFMAP:
278 if (ops->ndo_set_config) {
279 if (!netif_device_present(dev))
280 return -ENODEV;
281 return ops->ndo_set_config(dev, &ifr->ifr_map);
282 }
283 return -EOPNOTSUPP;
284
285 case SIOCADDMULTI:
286 if (!ops->ndo_set_rx_mode ||
287 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
288 return -EINVAL;
289 if (!netif_device_present(dev))
290 return -ENODEV;
291 return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
292
293 case SIOCDELMULTI:
294 if (!ops->ndo_set_rx_mode ||
295 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
296 return -EINVAL;
297 if (!netif_device_present(dev))
298 return -ENODEV;
299 return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
300
301 case SIOCSIFTXQLEN:
302 if (ifr->ifr_qlen < 0)
303 return -EINVAL;
304 dev->tx_queue_len = ifr->ifr_qlen;
305 return 0;
306
307 case SIOCSIFNAME:
308 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
309 return dev_change_name(dev, ifr->ifr_newname);
310
311 case SIOCSHWTSTAMP:
312 err = net_hwtstamp_validate(ifr);
313 if (err)
314 return err;
315 /* fall through */
316
317 /*
318 * Unknown or private ioctl
319 */
320 default:
321 if ((cmd >= SIOCDEVPRIVATE &&
322 cmd <= SIOCDEVPRIVATE + 15) ||
323 cmd == SIOCBONDENSLAVE ||
324 cmd == SIOCBONDRELEASE ||
325 cmd == SIOCBONDSETHWADDR ||
326 cmd == SIOCBONDSLAVEINFOQUERY ||
327 cmd == SIOCBONDINFOQUERY ||
328 cmd == SIOCBONDCHANGEACTIVE ||
329 cmd == SIOCGMIIPHY ||
330 cmd == SIOCGMIIREG ||
331 cmd == SIOCSMIIREG ||
332 cmd == SIOCBRADDIF ||
333 cmd == SIOCBRDELIF ||
334 cmd == SIOCSHWTSTAMP ||
Ben Hutchingsfd468c72013-11-14 01:19:29 +0000335 cmd == SIOCGHWTSTAMP ||
Cong Wang96b45cb2013-02-15 22:20:46 +0000336 cmd == SIOCWANDEV) {
337 err = -EOPNOTSUPP;
338 if (ops->ndo_do_ioctl) {
339 if (netif_device_present(dev))
340 err = ops->ndo_do_ioctl(dev, ifr, cmd);
341 else
342 err = -ENODEV;
343 }
344 } else
345 err = -EINVAL;
346
347 }
348 return err;
349}
350
351/**
352 * dev_load - load a network module
353 * @net: the applicable net namespace
354 * @name: name of interface
355 *
356 * If a network interface is not present and the process has suitable
357 * privileges this function loads the module. If module loading is not
358 * available in this kernel then it becomes a nop.
359 */
360
361void dev_load(struct net *net, const char *name)
362{
363 struct net_device *dev;
364 int no_module;
365
366 rcu_read_lock();
367 dev = dev_get_by_name_rcu(net, name);
368 rcu_read_unlock();
369
370 no_module = !dev;
371 if (no_module && capable(CAP_NET_ADMIN))
372 no_module = request_module("netdev-%s", name);
Daniel Borkmanne0208362014-09-02 23:30:05 +0200373 if (no_module && capable(CAP_SYS_MODULE))
374 request_module("%s", name);
Cong Wang96b45cb2013-02-15 22:20:46 +0000375}
376EXPORT_SYMBOL(dev_load);
377
378/*
379 * This function handles all "interface"-type I/O control requests. The actual
380 * 'doing' part of this is dev_ifsioc above.
381 */
382
383/**
384 * dev_ioctl - network device ioctl
385 * @net: the applicable net namespace
386 * @cmd: command to issue
387 * @arg: pointer to a struct ifreq in user space
388 *
389 * Issue ioctl functions to devices. This is normally called by the
390 * user space syscall interfaces but can sometimes be useful for
391 * other purposes. The return value is the return from the syscall if
392 * positive or a negative errno code on error.
393 */
394
395int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
396{
397 struct ifreq ifr;
398 int ret;
399 char *colon;
400
401 /* One special case: SIOCGIFCONF takes ifconf argument
402 and requires shared lock, because it sleeps writing
403 to user space.
404 */
405
406 if (cmd == SIOCGIFCONF) {
407 rtnl_lock();
408 ret = dev_ifconf(net, (char __user *) arg);
409 rtnl_unlock();
410 return ret;
411 }
412 if (cmd == SIOCGIFNAME)
413 return dev_ifname(net, (struct ifreq __user *)arg);
414
415 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
416 return -EFAULT;
417
418 ifr.ifr_name[IFNAMSIZ-1] = 0;
419
420 colon = strchr(ifr.ifr_name, ':');
421 if (colon)
422 *colon = 0;
423
424 /*
425 * See which interface the caller is talking about.
426 */
427
428 switch (cmd) {
429 /*
430 * These ioctl calls:
431 * - can be done by all.
432 * - atomic and do not require locking.
433 * - return a value
434 */
435 case SIOCGIFFLAGS:
436 case SIOCGIFMETRIC:
437 case SIOCGIFMTU:
438 case SIOCGIFHWADDR:
439 case SIOCGIFSLAVE:
440 case SIOCGIFMAP:
441 case SIOCGIFINDEX:
442 case SIOCGIFTXQLEN:
443 dev_load(net, ifr.ifr_name);
444 rcu_read_lock();
445 ret = dev_ifsioc_locked(net, &ifr, cmd);
446 rcu_read_unlock();
447 if (!ret) {
448 if (colon)
449 *colon = ':';
450 if (copy_to_user(arg, &ifr,
451 sizeof(struct ifreq)))
452 ret = -EFAULT;
453 }
454 return ret;
455
456 case SIOCETHTOOL:
457 dev_load(net, ifr.ifr_name);
458 rtnl_lock();
459 ret = dev_ethtool(net, &ifr);
460 rtnl_unlock();
461 if (!ret) {
462 if (colon)
463 *colon = ':';
464 if (copy_to_user(arg, &ifr,
465 sizeof(struct ifreq)))
466 ret = -EFAULT;
467 }
468 return ret;
469
470 /*
471 * These ioctl calls:
472 * - require superuser power.
473 * - require strict serialization.
474 * - return a value
475 */
476 case SIOCGMIIPHY:
477 case SIOCGMIIREG:
478 case SIOCSIFNAME:
479 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
480 return -EPERM;
481 dev_load(net, ifr.ifr_name);
482 rtnl_lock();
483 ret = dev_ifsioc(net, &ifr, cmd);
484 rtnl_unlock();
485 if (!ret) {
486 if (colon)
487 *colon = ':';
488 if (copy_to_user(arg, &ifr,
489 sizeof(struct ifreq)))
490 ret = -EFAULT;
491 }
492 return ret;
493
494 /*
495 * These ioctl calls:
496 * - require superuser power.
497 * - require strict serialization.
498 * - do not return a value
499 */
500 case SIOCSIFMAP:
501 case SIOCSIFTXQLEN:
502 if (!capable(CAP_NET_ADMIN))
503 return -EPERM;
504 /* fall through */
505 /*
506 * These ioctl calls:
507 * - require local superuser power.
508 * - require strict serialization.
509 * - do not return a value
510 */
511 case SIOCSIFFLAGS:
512 case SIOCSIFMETRIC:
513 case SIOCSIFMTU:
514 case SIOCSIFHWADDR:
515 case SIOCSIFSLAVE:
516 case SIOCADDMULTI:
517 case SIOCDELMULTI:
518 case SIOCSIFHWBROADCAST:
519 case SIOCSMIIREG:
520 case SIOCBONDENSLAVE:
521 case SIOCBONDRELEASE:
522 case SIOCBONDSETHWADDR:
523 case SIOCBONDCHANGEACTIVE:
524 case SIOCBRADDIF:
525 case SIOCBRDELIF:
526 case SIOCSHWTSTAMP:
527 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
528 return -EPERM;
529 /* fall through */
530 case SIOCBONDSLAVEINFOQUERY:
531 case SIOCBONDINFOQUERY:
532 dev_load(net, ifr.ifr_name);
533 rtnl_lock();
534 ret = dev_ifsioc(net, &ifr, cmd);
535 rtnl_unlock();
536 return ret;
537
538 case SIOCGIFMEM:
539 /* Get the per device memory space. We can add this but
540 * currently do not support it */
541 case SIOCSIFMEM:
542 /* Set the per device memory buffer space.
543 * Not applicable in our case */
544 case SIOCSIFLINK:
545 return -ENOTTY;
546
547 /*
548 * Unknown or private ioctl.
549 */
550 default:
551 if (cmd == SIOCWANDEV ||
Ben Hutchingsfd468c72013-11-14 01:19:29 +0000552 cmd == SIOCGHWTSTAMP ||
Cong Wang96b45cb2013-02-15 22:20:46 +0000553 (cmd >= SIOCDEVPRIVATE &&
554 cmd <= SIOCDEVPRIVATE + 15)) {
555 dev_load(net, ifr.ifr_name);
556 rtnl_lock();
557 ret = dev_ifsioc(net, &ifr, cmd);
558 rtnl_unlock();
559 if (!ret && copy_to_user(arg, &ifr,
560 sizeof(struct ifreq)))
561 ret = -EFAULT;
562 return ret;
563 }
564 /* Take care of Wireless Extensions */
565 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
566 return wext_handle_ioctl(net, &ifr, cmd, arg);
567 return -ENOTTY;
568 }
569}