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