dev_ioctl.c 19 KB

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