dev_ioctl.c 12 KB

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