dev_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. 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. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  351. return -EFAULT;
  352. ifr.ifr_name[IFNAMSIZ-1] = 0;
  353. colon = strchr(ifr.ifr_name, ':');
  354. if (colon)
  355. *colon = 0;
  356. /*
  357. * See which interface the caller is talking about.
  358. */
  359. switch (cmd) {
  360. /*
  361. * These ioctl calls:
  362. * - can be done by all.
  363. * - atomic and do not require locking.
  364. * - return a value
  365. */
  366. case SIOCGIFFLAGS:
  367. case SIOCGIFMETRIC:
  368. case SIOCGIFMTU:
  369. case SIOCGIFHWADDR:
  370. case SIOCGIFSLAVE:
  371. case SIOCGIFMAP:
  372. case SIOCGIFINDEX:
  373. case SIOCGIFTXQLEN:
  374. dev_load(net, ifr.ifr_name);
  375. rcu_read_lock();
  376. ret = dev_ifsioc_locked(net, &ifr, cmd);
  377. rcu_read_unlock();
  378. if (!ret) {
  379. if (colon)
  380. *colon = ':';
  381. if (copy_to_user(arg, &ifr,
  382. sizeof(struct ifreq)))
  383. ret = -EFAULT;
  384. }
  385. return ret;
  386. case SIOCETHTOOL:
  387. dev_load(net, ifr.ifr_name);
  388. rtnl_lock();
  389. ret = dev_ethtool(net, &ifr);
  390. rtnl_unlock();
  391. if (!ret) {
  392. if (colon)
  393. *colon = ':';
  394. if (copy_to_user(arg, &ifr,
  395. sizeof(struct ifreq)))
  396. ret = -EFAULT;
  397. }
  398. return ret;
  399. /*
  400. * These ioctl calls:
  401. * - require superuser power.
  402. * - require strict serialization.
  403. * - return a value
  404. */
  405. case SIOCGMIIPHY:
  406. case SIOCGMIIREG:
  407. case SIOCSIFNAME:
  408. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  409. return -EPERM;
  410. dev_load(net, ifr.ifr_name);
  411. rtnl_lock();
  412. ret = dev_ifsioc(net, &ifr, cmd);
  413. rtnl_unlock();
  414. if (!ret) {
  415. if (colon)
  416. *colon = ':';
  417. if (copy_to_user(arg, &ifr,
  418. sizeof(struct ifreq)))
  419. ret = -EFAULT;
  420. }
  421. return ret;
  422. /*
  423. * These ioctl calls:
  424. * - require superuser power.
  425. * - require strict serialization.
  426. * - do not return a value
  427. */
  428. case SIOCSIFMAP:
  429. case SIOCSIFTXQLEN:
  430. if (!capable(CAP_NET_ADMIN))
  431. return -EPERM;
  432. /* fall through */
  433. /*
  434. * These ioctl calls:
  435. * - require local superuser power.
  436. * - require strict serialization.
  437. * - do not return a value
  438. */
  439. case SIOCSIFFLAGS:
  440. case SIOCSIFMETRIC:
  441. case SIOCSIFMTU:
  442. case SIOCSIFHWADDR:
  443. case SIOCSIFSLAVE:
  444. case SIOCADDMULTI:
  445. case SIOCDELMULTI:
  446. case SIOCSIFHWBROADCAST:
  447. case SIOCSMIIREG:
  448. case SIOCBONDENSLAVE:
  449. case SIOCBONDRELEASE:
  450. case SIOCBONDSETHWADDR:
  451. case SIOCBONDCHANGEACTIVE:
  452. case SIOCBRADDIF:
  453. case SIOCBRDELIF:
  454. case SIOCSHWTSTAMP:
  455. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  456. return -EPERM;
  457. /* fall through */
  458. case SIOCBONDSLAVEINFOQUERY:
  459. case SIOCBONDINFOQUERY:
  460. dev_load(net, ifr.ifr_name);
  461. rtnl_lock();
  462. ret = dev_ifsioc(net, &ifr, cmd);
  463. rtnl_unlock();
  464. return ret;
  465. case SIOCGIFMEM:
  466. /* Get the per device memory space. We can add this but
  467. * currently do not support it */
  468. case SIOCSIFMEM:
  469. /* Set the per device memory buffer space.
  470. * Not applicable in our case */
  471. case SIOCSIFLINK:
  472. return -ENOTTY;
  473. /*
  474. * Unknown or private ioctl.
  475. */
  476. default:
  477. if (cmd == SIOCWANDEV ||
  478. cmd == SIOCGHWTSTAMP ||
  479. (cmd >= SIOCDEVPRIVATE &&
  480. cmd <= SIOCDEVPRIVATE + 15)) {
  481. dev_load(net, ifr.ifr_name);
  482. rtnl_lock();
  483. ret = dev_ifsioc(net, &ifr, cmd);
  484. rtnl_unlock();
  485. if (!ret && copy_to_user(arg, &ifr,
  486. sizeof(struct ifreq)))
  487. ret = -EFAULT;
  488. return ret;
  489. }
  490. /* Take care of Wireless Extensions */
  491. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
  492. return wext_handle_ioctl(net, &ifr, cmd, arg);
  493. return -ENOTTY;
  494. }
  495. }