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