dev_ioctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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/phylib_stubs.h>
  9. #include <linux/ptp_clock_kernel.h>
  10. #include <linux/wireless.h>
  11. #include <linux/if_bridge.h>
  12. #include <net/dsa_stubs.h>
  13. #include <net/netdev_lock.h>
  14. #include <net/wext.h>
  15. #include "dev.h"
  16. /*
  17. * Map an interface index to its name (SIOCGIFNAME)
  18. */
  19. /*
  20. * We need this ioctl for efficient implementation of the
  21. * if_indextoname() function required by the IPv6 API. Without
  22. * it, we would have to search all the interfaces to find a
  23. * match. --pb
  24. */
  25. static int dev_ifname(struct net *net, struct ifreq *ifr)
  26. {
  27. ifr->ifr_name[IFNAMSIZ-1] = 0;
  28. return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
  29. }
  30. /*
  31. * Perform a SIOCGIFCONF call. This structure will change
  32. * size eventually, and there is nothing I can do about it.
  33. * Thus we will need a 'compatibility mode'.
  34. */
  35. int dev_ifconf(struct net *net, struct ifconf __user *uifc)
  36. {
  37. struct net_device *dev;
  38. void __user *pos;
  39. size_t size;
  40. int len, total = 0, done;
  41. /* both the ifconf and the ifreq structures are slightly different */
  42. if (in_compat_syscall()) {
  43. struct compat_ifconf ifc32;
  44. if (copy_from_user(&ifc32, uifc, sizeof(struct compat_ifconf)))
  45. return -EFAULT;
  46. pos = compat_ptr(ifc32.ifcbuf);
  47. len = ifc32.ifc_len;
  48. size = sizeof(struct compat_ifreq);
  49. } else {
  50. struct ifconf ifc;
  51. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  52. return -EFAULT;
  53. pos = ifc.ifc_buf;
  54. len = ifc.ifc_len;
  55. size = sizeof(struct ifreq);
  56. }
  57. /* Loop over the interfaces, and write an info block for each. */
  58. rtnl_net_lock(net);
  59. for_each_netdev(net, dev) {
  60. if (!pos)
  61. done = inet_gifconf(dev, NULL, 0, size);
  62. else
  63. done = inet_gifconf(dev, pos + total,
  64. len - total, size);
  65. if (done < 0) {
  66. rtnl_net_unlock(net);
  67. return -EFAULT;
  68. }
  69. total += done;
  70. }
  71. rtnl_net_unlock(net);
  72. return put_user(total, &uifc->ifc_len);
  73. }
  74. static int dev_getifmap(struct net_device *dev, struct ifreq *ifr)
  75. {
  76. struct ifmap *ifmap = &ifr->ifr_map;
  77. if (in_compat_syscall()) {
  78. struct compat_ifmap *cifmap = (struct compat_ifmap *)ifmap;
  79. cifmap->mem_start = dev->mem_start;
  80. cifmap->mem_end = dev->mem_end;
  81. cifmap->base_addr = dev->base_addr;
  82. cifmap->irq = dev->irq;
  83. cifmap->dma = dev->dma;
  84. cifmap->port = dev->if_port;
  85. return 0;
  86. }
  87. ifmap->mem_start = dev->mem_start;
  88. ifmap->mem_end = dev->mem_end;
  89. ifmap->base_addr = dev->base_addr;
  90. ifmap->irq = dev->irq;
  91. ifmap->dma = dev->dma;
  92. ifmap->port = dev->if_port;
  93. return 0;
  94. }
  95. static int netif_setifmap(struct net_device *dev, struct ifreq *ifr)
  96. {
  97. struct compat_ifmap *cifmap = (struct compat_ifmap *)&ifr->ifr_map;
  98. if (!dev->netdev_ops->ndo_set_config)
  99. return -EOPNOTSUPP;
  100. if (in_compat_syscall()) {
  101. struct ifmap ifmap = {
  102. .mem_start = cifmap->mem_start,
  103. .mem_end = cifmap->mem_end,
  104. .base_addr = cifmap->base_addr,
  105. .irq = cifmap->irq,
  106. .dma = cifmap->dma,
  107. .port = cifmap->port,
  108. };
  109. return dev->netdev_ops->ndo_set_config(dev, &ifmap);
  110. }
  111. return dev->netdev_ops->ndo_set_config(dev, &ifr->ifr_map);
  112. }
  113. /*
  114. * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
  115. */
  116. static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
  117. {
  118. int err;
  119. struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
  120. if (!dev)
  121. return -ENODEV;
  122. switch (cmd) {
  123. case SIOCGIFFLAGS: /* Get interface flags */
  124. ifr->ifr_flags = (short)netif_get_flags(dev);
  125. return 0;
  126. case SIOCGIFMETRIC: /* Get the metric on the interface
  127. (currently unused) */
  128. ifr->ifr_metric = 0;
  129. return 0;
  130. case SIOCGIFMTU: /* Get the MTU of a device */
  131. ifr->ifr_mtu = dev->mtu;
  132. return 0;
  133. case SIOCGIFSLAVE:
  134. err = -EINVAL;
  135. break;
  136. case SIOCGIFMAP:
  137. return dev_getifmap(dev, ifr);
  138. case SIOCGIFINDEX:
  139. ifr->ifr_ifindex = dev->ifindex;
  140. return 0;
  141. case SIOCGIFTXQLEN:
  142. ifr->ifr_qlen = dev->tx_queue_len;
  143. return 0;
  144. default:
  145. /* dev_ioctl() should ensure this case
  146. * is never reached
  147. */
  148. WARN_ON(1);
  149. err = -ENOTTY;
  150. break;
  151. }
  152. return err;
  153. }
  154. int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg)
  155. {
  156. enum hwtstamp_tx_types tx_type;
  157. enum hwtstamp_rx_filters rx_filter;
  158. int tx_type_valid = 0;
  159. int rx_filter_valid = 0;
  160. if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
  161. return -EINVAL;
  162. tx_type = cfg->tx_type;
  163. rx_filter = cfg->rx_filter;
  164. switch (tx_type) {
  165. case HWTSTAMP_TX_OFF:
  166. case HWTSTAMP_TX_ON:
  167. case HWTSTAMP_TX_ONESTEP_SYNC:
  168. case HWTSTAMP_TX_ONESTEP_P2P:
  169. tx_type_valid = 1;
  170. break;
  171. case __HWTSTAMP_TX_CNT:
  172. /* not a real value */
  173. break;
  174. }
  175. switch (rx_filter) {
  176. case HWTSTAMP_FILTER_NONE:
  177. case HWTSTAMP_FILTER_ALL:
  178. case HWTSTAMP_FILTER_SOME:
  179. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  180. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  181. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  182. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  183. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  184. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  185. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  186. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  187. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  188. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  189. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  190. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  191. case HWTSTAMP_FILTER_NTP_ALL:
  192. rx_filter_valid = 1;
  193. break;
  194. case __HWTSTAMP_FILTER_CNT:
  195. /* not a real value */
  196. break;
  197. }
  198. if (!tx_type_valid || !rx_filter_valid)
  199. return -ERANGE;
  200. return 0;
  201. }
  202. /**
  203. * dev_get_hwtstamp_phylib() - Get hardware timestamping settings of NIC
  204. * or of attached phylib PHY
  205. * @dev: Network device
  206. * @cfg: Timestamping configuration structure
  207. *
  208. * Helper for calling the default hardware provider timestamping.
  209. *
  210. * Note: phy_mii_ioctl() only handles SIOCSHWTSTAMP (not SIOCGHWTSTAMP), but
  211. * phydev->mii_ts has both hwtstamp_get() and hwtstamp_set() methods. So this
  212. * will return -EOPNOTSUPP for phylib only if hwtstamp_get() is not
  213. * implemented for now, which is still more accurate than letting the netdev
  214. * handle the GET request.
  215. */
  216. int dev_get_hwtstamp_phylib(struct net_device *dev,
  217. struct kernel_hwtstamp_config *cfg)
  218. {
  219. struct hwtstamp_provider *hwprov;
  220. hwprov = rtnl_dereference(dev->hwprov);
  221. if (hwprov) {
  222. cfg->qualifier = hwprov->desc.qualifier;
  223. if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
  224. hwprov->phydev)
  225. return phy_hwtstamp_get(hwprov->phydev, cfg);
  226. if (hwprov->source == HWTSTAMP_SOURCE_NETDEV)
  227. return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
  228. return -EOPNOTSUPP;
  229. }
  230. if (phy_is_default_hwtstamp(dev->phydev))
  231. return phy_hwtstamp_get(dev->phydev, cfg);
  232. return dev->netdev_ops->ndo_hwtstamp_get(dev, cfg);
  233. }
  234. static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
  235. {
  236. const struct net_device_ops *ops = dev->netdev_ops;
  237. struct kernel_hwtstamp_config kernel_cfg = {};
  238. struct hwtstamp_config cfg;
  239. int err;
  240. if (!ops->ndo_hwtstamp_get)
  241. return -EOPNOTSUPP;
  242. if (!netif_device_present(dev))
  243. return -ENODEV;
  244. kernel_cfg.ifr = ifr;
  245. netdev_lock_ops(dev);
  246. err = dev_get_hwtstamp_phylib(dev, &kernel_cfg);
  247. netdev_unlock_ops(dev);
  248. if (err)
  249. return err;
  250. /* If the request was resolved through an unconverted driver, omit
  251. * the copy_to_user(), since the implementation has already done that
  252. */
  253. if (!kernel_cfg.copied_to_user) {
  254. hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
  255. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  256. return -EFAULT;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * dev_set_hwtstamp_phylib() - Change hardware timestamping of NIC
  262. * or of attached phylib PHY
  263. * @dev: Network device
  264. * @cfg: Timestamping configuration structure
  265. * @extack: Netlink extended ack message structure, for error reporting
  266. *
  267. * Helper for enforcing a common policy that phylib timestamping, if available,
  268. * should take precedence in front of hardware timestamping provided by the
  269. * netdev. If the netdev driver needs to perform specific actions even for PHY
  270. * timestamping to work properly (a switch port must trap the timestamped
  271. * frames and not forward them), it must set dev->see_all_hwtstamp_requests.
  272. */
  273. int dev_set_hwtstamp_phylib(struct net_device *dev,
  274. struct kernel_hwtstamp_config *cfg,
  275. struct netlink_ext_ack *extack)
  276. {
  277. const struct net_device_ops *ops = dev->netdev_ops;
  278. struct kernel_hwtstamp_config old_cfg = {};
  279. struct hwtstamp_provider *hwprov;
  280. struct phy_device *phydev;
  281. bool changed = false;
  282. bool phy_ts;
  283. int err;
  284. hwprov = rtnl_dereference(dev->hwprov);
  285. if (hwprov) {
  286. if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
  287. hwprov->phydev) {
  288. phy_ts = true;
  289. phydev = hwprov->phydev;
  290. } else if (hwprov->source == HWTSTAMP_SOURCE_NETDEV) {
  291. phy_ts = false;
  292. } else {
  293. return -EOPNOTSUPP;
  294. }
  295. cfg->qualifier = hwprov->desc.qualifier;
  296. } else {
  297. phy_ts = phy_is_default_hwtstamp(dev->phydev);
  298. if (phy_ts)
  299. phydev = dev->phydev;
  300. }
  301. cfg->source = phy_ts ? HWTSTAMP_SOURCE_PHYLIB : HWTSTAMP_SOURCE_NETDEV;
  302. if (phy_ts && dev->see_all_hwtstamp_requests) {
  303. err = ops->ndo_hwtstamp_get(dev, &old_cfg);
  304. if (err)
  305. return err;
  306. }
  307. if (!phy_ts || dev->see_all_hwtstamp_requests) {
  308. err = ops->ndo_hwtstamp_set(dev, cfg, extack);
  309. if (err) {
  310. if (extack->_msg)
  311. netdev_err(dev, "%s\n", extack->_msg);
  312. return err;
  313. }
  314. }
  315. if (phy_ts && dev->see_all_hwtstamp_requests)
  316. changed = kernel_hwtstamp_config_changed(&old_cfg, cfg);
  317. if (phy_ts) {
  318. err = phy_hwtstamp_set(phydev, cfg, extack);
  319. if (err) {
  320. if (changed)
  321. ops->ndo_hwtstamp_set(dev, &old_cfg, NULL);
  322. return err;
  323. }
  324. }
  325. return 0;
  326. }
  327. static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
  328. {
  329. const struct net_device_ops *ops = dev->netdev_ops;
  330. struct kernel_hwtstamp_config kernel_cfg = {};
  331. struct netlink_ext_ack extack = {};
  332. struct hwtstamp_config cfg;
  333. int err;
  334. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  335. return -EFAULT;
  336. hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
  337. kernel_cfg.ifr = ifr;
  338. err = net_hwtstamp_validate(&kernel_cfg);
  339. if (err)
  340. return err;
  341. err = dsa_conduit_hwtstamp_validate(dev, &kernel_cfg, &extack);
  342. if (err) {
  343. if (extack._msg)
  344. netdev_err(dev, "%s\n", extack._msg);
  345. return err;
  346. }
  347. if (!ops->ndo_hwtstamp_set)
  348. return -EOPNOTSUPP;
  349. if (!netif_device_present(dev))
  350. return -ENODEV;
  351. netdev_lock_ops(dev);
  352. err = dev_set_hwtstamp_phylib(dev, &kernel_cfg, &extack);
  353. netdev_unlock_ops(dev);
  354. if (err)
  355. return err;
  356. /* The driver may have modified the configuration, so copy the
  357. * updated version of it back to user space
  358. */
  359. if (!kernel_cfg.copied_to_user) {
  360. hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
  361. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  362. return -EFAULT;
  363. }
  364. return 0;
  365. }
  366. int generic_hwtstamp_get_lower(struct net_device *dev,
  367. struct kernel_hwtstamp_config *kernel_cfg)
  368. {
  369. const struct net_device_ops *ops = dev->netdev_ops;
  370. int err;
  371. if (!netif_device_present(dev))
  372. return -ENODEV;
  373. if (!ops->ndo_hwtstamp_get)
  374. return -EOPNOTSUPP;
  375. netdev_lock_ops(dev);
  376. err = dev_get_hwtstamp_phylib(dev, kernel_cfg);
  377. netdev_unlock_ops(dev);
  378. return err;
  379. }
  380. EXPORT_SYMBOL(generic_hwtstamp_get_lower);
  381. int generic_hwtstamp_set_lower(struct net_device *dev,
  382. struct kernel_hwtstamp_config *kernel_cfg,
  383. struct netlink_ext_ack *extack)
  384. {
  385. const struct net_device_ops *ops = dev->netdev_ops;
  386. int err;
  387. if (!netif_device_present(dev))
  388. return -ENODEV;
  389. if (!ops->ndo_hwtstamp_set)
  390. return -EOPNOTSUPP;
  391. netdev_lock_ops(dev);
  392. err = dev_set_hwtstamp_phylib(dev, kernel_cfg, extack);
  393. netdev_unlock_ops(dev);
  394. return err;
  395. }
  396. EXPORT_SYMBOL(generic_hwtstamp_set_lower);
  397. static int dev_siocbond(struct net_device *dev,
  398. struct ifreq *ifr, unsigned int cmd)
  399. {
  400. const struct net_device_ops *ops = dev->netdev_ops;
  401. if (ops->ndo_siocbond) {
  402. int ret = -ENODEV;
  403. netdev_lock_ops(dev);
  404. if (netif_device_present(dev))
  405. ret = ops->ndo_siocbond(dev, ifr, cmd);
  406. netdev_unlock_ops(dev);
  407. return ret;
  408. }
  409. return -EOPNOTSUPP;
  410. }
  411. static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
  412. void __user *data, unsigned int cmd)
  413. {
  414. const struct net_device_ops *ops = dev->netdev_ops;
  415. if (ops->ndo_siocdevprivate) {
  416. int ret = -ENODEV;
  417. netdev_lock_ops(dev);
  418. if (netif_device_present(dev))
  419. ret = ops->ndo_siocdevprivate(dev, ifr, data, cmd);
  420. netdev_unlock_ops(dev);
  421. return ret;
  422. }
  423. return -EOPNOTSUPP;
  424. }
  425. static int dev_siocwandev(struct net_device *dev, struct if_settings *ifs)
  426. {
  427. const struct net_device_ops *ops = dev->netdev_ops;
  428. if (ops->ndo_siocwandev) {
  429. int ret = -ENODEV;
  430. netdev_lock_ops(dev);
  431. if (netif_device_present(dev))
  432. ret = ops->ndo_siocwandev(dev, ifs);
  433. netdev_unlock_ops(dev);
  434. return ret;
  435. }
  436. return -EOPNOTSUPP;
  437. }
  438. /*
  439. * Perform the SIOCxIFxxx calls, inside rtnl_net_lock()
  440. */
  441. static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
  442. unsigned int cmd)
  443. {
  444. int err;
  445. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  446. const struct net_device_ops *ops;
  447. if (!dev)
  448. return -ENODEV;
  449. ops = dev->netdev_ops;
  450. switch (cmd) {
  451. case SIOCSIFFLAGS: /* Set interface flags */
  452. return dev_change_flags(dev, ifr->ifr_flags, NULL);
  453. case SIOCSIFMETRIC: /* Set the metric on the interface
  454. (currently unused) */
  455. return -EOPNOTSUPP;
  456. case SIOCSIFMTU: /* Set the MTU of a device */
  457. return dev_set_mtu(dev, ifr->ifr_mtu);
  458. case SIOCSIFHWADDR:
  459. if (dev->addr_len > sizeof(ifr->ifr_hwaddr))
  460. return -EINVAL;
  461. return dev_set_mac_address_user(dev,
  462. (struct sockaddr_storage *)&ifr->ifr_hwaddr,
  463. NULL);
  464. case SIOCSIFHWBROADCAST:
  465. if (ifr->ifr_hwaddr.sa_family != dev->type)
  466. return -EINVAL;
  467. memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
  468. min(sizeof(ifr->ifr_hwaddr.sa_data),
  469. (size_t)dev->addr_len));
  470. netdev_lock_ops(dev);
  471. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  472. netdev_unlock_ops(dev);
  473. return 0;
  474. case SIOCSIFMAP:
  475. netdev_lock_ops(dev);
  476. err = netif_setifmap(dev, ifr);
  477. netdev_unlock_ops(dev);
  478. return err;
  479. case SIOCADDMULTI:
  480. if (!ops->ndo_set_rx_mode ||
  481. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  482. return -EINVAL;
  483. if (!netif_device_present(dev))
  484. return -ENODEV;
  485. netdev_lock_ops(dev);
  486. err = dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
  487. netdev_unlock_ops(dev);
  488. return err;
  489. case SIOCDELMULTI:
  490. if (!ops->ndo_set_rx_mode ||
  491. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  492. return -EINVAL;
  493. if (!netif_device_present(dev))
  494. return -ENODEV;
  495. netdev_lock_ops(dev);
  496. err = dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
  497. netdev_unlock_ops(dev);
  498. return err;
  499. case SIOCSIFTXQLEN:
  500. if (ifr->ifr_qlen < 0)
  501. return -EINVAL;
  502. return dev_change_tx_queue_len(dev, ifr->ifr_qlen);
  503. case SIOCSIFNAME:
  504. ifr->ifr_newname[IFNAMSIZ-1] = '\0';
  505. return dev_change_name(dev, ifr->ifr_newname);
  506. case SIOCWANDEV:
  507. return dev_siocwandev(dev, &ifr->ifr_settings);
  508. case SIOCDEVPRIVATE ... SIOCDEVPRIVATE + 15:
  509. return dev_siocdevprivate(dev, ifr, data, cmd);
  510. case SIOCSHWTSTAMP:
  511. return dev_set_hwtstamp(dev, ifr);
  512. case SIOCGHWTSTAMP:
  513. return dev_get_hwtstamp(dev, ifr);
  514. case SIOCGMIIPHY:
  515. case SIOCGMIIREG:
  516. case SIOCSMIIREG:
  517. return dev_eth_ioctl(dev, ifr, cmd);
  518. case SIOCBONDENSLAVE:
  519. case SIOCBONDRELEASE:
  520. case SIOCBONDSETHWADDR:
  521. case SIOCBONDSLAVEINFOQUERY:
  522. case SIOCBONDINFOQUERY:
  523. case SIOCBONDCHANGEACTIVE:
  524. return dev_siocbond(dev, ifr, cmd);
  525. /* Unknown ioctl */
  526. default:
  527. err = -EINVAL;
  528. }
  529. return err;
  530. }
  531. /**
  532. * dev_load - load a network module
  533. * @net: the applicable net namespace
  534. * @name: name of interface
  535. *
  536. * If a network interface is not present and the process has suitable
  537. * privileges this function loads the module. If module loading is not
  538. * available in this kernel then it becomes a nop.
  539. */
  540. void dev_load(struct net *net, const char *name)
  541. {
  542. struct net_device *dev;
  543. int no_module;
  544. rcu_read_lock();
  545. dev = dev_get_by_name_rcu(net, name);
  546. rcu_read_unlock();
  547. no_module = !dev;
  548. if (no_module && capable(CAP_NET_ADMIN))
  549. no_module = request_module("netdev-%s", name);
  550. if (no_module && capable(CAP_SYS_MODULE))
  551. request_module("%s", name);
  552. }
  553. EXPORT_SYMBOL(dev_load);
  554. /*
  555. * This function handles all "interface"-type I/O control requests. The actual
  556. * 'doing' part of this is dev_ifsioc above.
  557. */
  558. /**
  559. * dev_ioctl - network device ioctl
  560. * @net: the applicable net namespace
  561. * @cmd: command to issue
  562. * @ifr: pointer to a struct ifreq in user space
  563. * @data: data exchanged with userspace
  564. * @need_copyout: whether or not copy_to_user() should be called
  565. *
  566. * Issue ioctl functions to devices. This is normally called by the
  567. * user space syscall interfaces but can sometimes be useful for
  568. * other purposes. The return value is the return from the syscall if
  569. * positive or a negative errno code on error.
  570. */
  571. int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
  572. void __user *data, bool *need_copyout)
  573. {
  574. int ret;
  575. char *colon;
  576. if (need_copyout)
  577. *need_copyout = true;
  578. if (cmd == SIOCGIFNAME)
  579. return dev_ifname(net, ifr);
  580. ifr->ifr_name[IFNAMSIZ-1] = 0;
  581. colon = strchr(ifr->ifr_name, ':');
  582. if (colon)
  583. *colon = 0;
  584. /*
  585. * See which interface the caller is talking about.
  586. */
  587. switch (cmd) {
  588. case SIOCGIFHWADDR:
  589. dev_load(net, ifr->ifr_name);
  590. ret = netif_get_mac_address(&ifr->ifr_hwaddr, net,
  591. ifr->ifr_name);
  592. if (colon)
  593. *colon = ':';
  594. return ret;
  595. /*
  596. * These ioctl calls:
  597. * - can be done by all.
  598. * - atomic and do not require locking.
  599. * - return a value
  600. */
  601. case SIOCGIFFLAGS:
  602. case SIOCGIFMETRIC:
  603. case SIOCGIFMTU:
  604. case SIOCGIFSLAVE:
  605. case SIOCGIFMAP:
  606. case SIOCGIFINDEX:
  607. case SIOCGIFTXQLEN:
  608. dev_load(net, ifr->ifr_name);
  609. rcu_read_lock();
  610. ret = dev_ifsioc_locked(net, ifr, cmd);
  611. rcu_read_unlock();
  612. if (colon)
  613. *colon = ':';
  614. return ret;
  615. case SIOCETHTOOL:
  616. dev_load(net, ifr->ifr_name);
  617. ret = dev_ethtool(net, ifr, data);
  618. if (colon)
  619. *colon = ':';
  620. return ret;
  621. /*
  622. * These ioctl calls:
  623. * - require superuser power.
  624. * - require strict serialization.
  625. * - return a value
  626. */
  627. case SIOCGMIIPHY:
  628. case SIOCGMIIREG:
  629. case SIOCSIFNAME:
  630. dev_load(net, ifr->ifr_name);
  631. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  632. return -EPERM;
  633. rtnl_net_lock(net);
  634. ret = dev_ifsioc(net, ifr, data, cmd);
  635. rtnl_net_unlock(net);
  636. if (colon)
  637. *colon = ':';
  638. return ret;
  639. /*
  640. * These ioctl calls:
  641. * - require superuser power.
  642. * - require strict serialization.
  643. * - do not return a value
  644. */
  645. case SIOCSIFMAP:
  646. case SIOCSIFTXQLEN:
  647. if (!capable(CAP_NET_ADMIN))
  648. return -EPERM;
  649. fallthrough;
  650. /*
  651. * These ioctl calls:
  652. * - require local superuser power.
  653. * - require strict serialization.
  654. * - do not return a value
  655. */
  656. case SIOCSIFFLAGS:
  657. case SIOCSIFMETRIC:
  658. case SIOCSIFMTU:
  659. case SIOCSIFHWADDR:
  660. case SIOCSIFSLAVE:
  661. case SIOCADDMULTI:
  662. case SIOCDELMULTI:
  663. case SIOCSIFHWBROADCAST:
  664. case SIOCSMIIREG:
  665. case SIOCBONDENSLAVE:
  666. case SIOCBONDRELEASE:
  667. case SIOCBONDSETHWADDR:
  668. case SIOCBONDCHANGEACTIVE:
  669. case SIOCSHWTSTAMP:
  670. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  671. return -EPERM;
  672. fallthrough;
  673. case SIOCBONDSLAVEINFOQUERY:
  674. case SIOCBONDINFOQUERY:
  675. dev_load(net, ifr->ifr_name);
  676. rtnl_net_lock(net);
  677. ret = dev_ifsioc(net, ifr, data, cmd);
  678. rtnl_net_unlock(net);
  679. if (need_copyout)
  680. *need_copyout = false;
  681. return ret;
  682. case SIOCGIFMEM:
  683. /* Get the per device memory space. We can add this but
  684. * currently do not support it */
  685. case SIOCSIFMEM:
  686. /* Set the per device memory buffer space.
  687. * Not applicable in our case */
  688. case SIOCSIFLINK:
  689. return -ENOTTY;
  690. /*
  691. * Unknown or private ioctl.
  692. */
  693. default:
  694. if (cmd == SIOCWANDEV ||
  695. cmd == SIOCGHWTSTAMP ||
  696. (cmd >= SIOCDEVPRIVATE &&
  697. cmd <= SIOCDEVPRIVATE + 15)) {
  698. dev_load(net, ifr->ifr_name);
  699. rtnl_net_lock(net);
  700. ret = dev_ifsioc(net, ifr, data, cmd);
  701. rtnl_net_unlock(net);
  702. return ret;
  703. }
  704. return -ENOTTY;
  705. }
  706. }