dev_ioctl.c 13 KB

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