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