dev_ioctl.c 13 KB

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