dev_ioctl.c 13 KB

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