dev_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. rx_filter_valid = 1;
  193. break;
  194. }
  195. if (!tx_type_valid || !rx_filter_valid)
  196. return -ERANGE;
  197. return 0;
  198. }
  199. /*
  200. * Perform the SIOCxIFxxx calls, inside rtnl_lock()
  201. */
  202. static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
  203. {
  204. int err;
  205. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  206. const struct net_device_ops *ops;
  207. if (!dev)
  208. return -ENODEV;
  209. ops = dev->netdev_ops;
  210. switch (cmd) {
  211. case SIOCSIFFLAGS: /* Set interface flags */
  212. return dev_change_flags(dev, ifr->ifr_flags);
  213. case SIOCSIFMETRIC: /* Set the metric on the interface
  214. (currently unused) */
  215. return -EOPNOTSUPP;
  216. case SIOCSIFMTU: /* Set the MTU of a device */
  217. return dev_set_mtu(dev, ifr->ifr_mtu);
  218. case SIOCSIFHWADDR:
  219. return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
  220. case SIOCSIFHWBROADCAST:
  221. if (ifr->ifr_hwaddr.sa_family != dev->type)
  222. return -EINVAL;
  223. memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
  224. min(sizeof(ifr->ifr_hwaddr.sa_data),
  225. (size_t)dev->addr_len));
  226. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  227. return 0;
  228. case SIOCSIFMAP:
  229. if (ops->ndo_set_config) {
  230. if (!netif_device_present(dev))
  231. return -ENODEV;
  232. return ops->ndo_set_config(dev, &ifr->ifr_map);
  233. }
  234. return -EOPNOTSUPP;
  235. case SIOCADDMULTI:
  236. if (!ops->ndo_set_rx_mode ||
  237. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  238. return -EINVAL;
  239. if (!netif_device_present(dev))
  240. return -ENODEV;
  241. return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
  242. case SIOCDELMULTI:
  243. if (!ops->ndo_set_rx_mode ||
  244. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  245. return -EINVAL;
  246. if (!netif_device_present(dev))
  247. return -ENODEV;
  248. return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
  249. case SIOCSIFTXQLEN:
  250. if (ifr->ifr_qlen < 0)
  251. return -EINVAL;
  252. dev->tx_queue_len = ifr->ifr_qlen;
  253. return 0;
  254. case SIOCSIFNAME:
  255. ifr->ifr_newname[IFNAMSIZ-1] = '\0';
  256. return dev_change_name(dev, ifr->ifr_newname);
  257. case SIOCSHWTSTAMP:
  258. err = net_hwtstamp_validate(ifr);
  259. if (err)
  260. return err;
  261. /* fall through */
  262. /*
  263. * Unknown or private ioctl
  264. */
  265. default:
  266. if ((cmd >= SIOCDEVPRIVATE &&
  267. cmd <= SIOCDEVPRIVATE + 15) ||
  268. cmd == SIOCBONDENSLAVE ||
  269. cmd == SIOCBONDRELEASE ||
  270. cmd == SIOCBONDSETHWADDR ||
  271. cmd == SIOCBONDSLAVEINFOQUERY ||
  272. cmd == SIOCBONDINFOQUERY ||
  273. cmd == SIOCBONDCHANGEACTIVE ||
  274. cmd == SIOCGMIIPHY ||
  275. cmd == SIOCGMIIREG ||
  276. cmd == SIOCSMIIREG ||
  277. cmd == SIOCBRADDIF ||
  278. cmd == SIOCBRDELIF ||
  279. cmd == SIOCSHWTSTAMP ||
  280. cmd == SIOCGHWTSTAMP ||
  281. cmd == SIOCWANDEV) {
  282. err = -EOPNOTSUPP;
  283. if (ops->ndo_do_ioctl) {
  284. if (netif_device_present(dev))
  285. err = ops->ndo_do_ioctl(dev, ifr, cmd);
  286. else
  287. err = -ENODEV;
  288. }
  289. } else
  290. err = -EINVAL;
  291. }
  292. return err;
  293. }
  294. /**
  295. * dev_load - load a network module
  296. * @net: the applicable net namespace
  297. * @name: name of interface
  298. *
  299. * If a network interface is not present and the process has suitable
  300. * privileges this function loads the module. If module loading is not
  301. * available in this kernel then it becomes a nop.
  302. */
  303. void dev_load(struct net *net, const char *name)
  304. {
  305. struct net_device *dev;
  306. int no_module;
  307. rcu_read_lock();
  308. dev = dev_get_by_name_rcu(net, name);
  309. rcu_read_unlock();
  310. no_module = !dev;
  311. if (no_module && capable(CAP_NET_ADMIN))
  312. no_module = request_module("netdev-%s", name);
  313. if (no_module && capable(CAP_SYS_MODULE))
  314. request_module("%s", name);
  315. }
  316. EXPORT_SYMBOL(dev_load);
  317. /*
  318. * This function handles all "interface"-type I/O control requests. The actual
  319. * 'doing' part of this is dev_ifsioc above.
  320. */
  321. /**
  322. * dev_ioctl - network device ioctl
  323. * @net: the applicable net namespace
  324. * @cmd: command to issue
  325. * @arg: pointer to a struct ifreq in user space
  326. *
  327. * Issue ioctl functions to devices. This is normally called by the
  328. * user space syscall interfaces but can sometimes be useful for
  329. * other purposes. The return value is the return from the syscall if
  330. * positive or a negative errno code on error.
  331. */
  332. int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  333. {
  334. struct ifreq ifr;
  335. int ret;
  336. char *colon;
  337. /* One special case: SIOCGIFCONF takes ifconf argument
  338. and requires shared lock, because it sleeps writing
  339. to user space.
  340. */
  341. if (cmd == SIOCGIFCONF) {
  342. rtnl_lock();
  343. ret = dev_ifconf(net, (char __user *) arg);
  344. rtnl_unlock();
  345. return ret;
  346. }
  347. if (cmd == SIOCGIFNAME)
  348. return dev_ifname(net, (struct ifreq __user *)arg);
  349. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  350. return -EFAULT;
  351. ifr.ifr_name[IFNAMSIZ-1] = 0;
  352. colon = strchr(ifr.ifr_name, ':');
  353. if (colon)
  354. *colon = 0;
  355. /*
  356. * See which interface the caller is talking about.
  357. */
  358. switch (cmd) {
  359. /*
  360. * These ioctl calls:
  361. * - can be done by all.
  362. * - atomic and do not require locking.
  363. * - return a value
  364. */
  365. case SIOCGIFFLAGS:
  366. case SIOCGIFMETRIC:
  367. case SIOCGIFMTU:
  368. case SIOCGIFHWADDR:
  369. case SIOCGIFSLAVE:
  370. case SIOCGIFMAP:
  371. case SIOCGIFINDEX:
  372. case SIOCGIFTXQLEN:
  373. dev_load(net, ifr.ifr_name);
  374. rcu_read_lock();
  375. ret = dev_ifsioc_locked(net, &ifr, cmd);
  376. rcu_read_unlock();
  377. if (!ret) {
  378. if (colon)
  379. *colon = ':';
  380. if (copy_to_user(arg, &ifr,
  381. sizeof(struct ifreq)))
  382. ret = -EFAULT;
  383. }
  384. return ret;
  385. case SIOCETHTOOL:
  386. dev_load(net, ifr.ifr_name);
  387. rtnl_lock();
  388. ret = dev_ethtool(net, &ifr);
  389. rtnl_unlock();
  390. if (!ret) {
  391. if (colon)
  392. *colon = ':';
  393. if (copy_to_user(arg, &ifr,
  394. sizeof(struct ifreq)))
  395. ret = -EFAULT;
  396. }
  397. return ret;
  398. /*
  399. * These ioctl calls:
  400. * - require superuser power.
  401. * - require strict serialization.
  402. * - return a value
  403. */
  404. case SIOCGMIIPHY:
  405. case SIOCGMIIREG:
  406. case SIOCSIFNAME:
  407. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  408. return -EPERM;
  409. dev_load(net, ifr.ifr_name);
  410. rtnl_lock();
  411. ret = dev_ifsioc(net, &ifr, cmd);
  412. rtnl_unlock();
  413. if (!ret) {
  414. if (colon)
  415. *colon = ':';
  416. if (copy_to_user(arg, &ifr,
  417. sizeof(struct ifreq)))
  418. ret = -EFAULT;
  419. }
  420. return ret;
  421. /*
  422. * These ioctl calls:
  423. * - require superuser power.
  424. * - require strict serialization.
  425. * - do not return a value
  426. */
  427. case SIOCSIFMAP:
  428. case SIOCSIFTXQLEN:
  429. if (!capable(CAP_NET_ADMIN))
  430. return -EPERM;
  431. /* fall through */
  432. /*
  433. * These ioctl calls:
  434. * - require local superuser power.
  435. * - require strict serialization.
  436. * - do not return a value
  437. */
  438. case SIOCSIFFLAGS:
  439. case SIOCSIFMETRIC:
  440. case SIOCSIFMTU:
  441. case SIOCSIFHWADDR:
  442. case SIOCSIFSLAVE:
  443. case SIOCADDMULTI:
  444. case SIOCDELMULTI:
  445. case SIOCSIFHWBROADCAST:
  446. case SIOCSMIIREG:
  447. case SIOCBONDENSLAVE:
  448. case SIOCBONDRELEASE:
  449. case SIOCBONDSETHWADDR:
  450. case SIOCBONDCHANGEACTIVE:
  451. case SIOCBRADDIF:
  452. case SIOCBRDELIF:
  453. case SIOCSHWTSTAMP:
  454. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  455. return -EPERM;
  456. /* fall through */
  457. case SIOCBONDSLAVEINFOQUERY:
  458. case SIOCBONDINFOQUERY:
  459. dev_load(net, ifr.ifr_name);
  460. rtnl_lock();
  461. ret = dev_ifsioc(net, &ifr, cmd);
  462. rtnl_unlock();
  463. return ret;
  464. case SIOCGIFMEM:
  465. /* Get the per device memory space. We can add this but
  466. * currently do not support it */
  467. case SIOCSIFMEM:
  468. /* Set the per device memory buffer space.
  469. * Not applicable in our case */
  470. case SIOCSIFLINK:
  471. return -ENOTTY;
  472. /*
  473. * Unknown or private ioctl.
  474. */
  475. default:
  476. if (cmd == SIOCWANDEV ||
  477. cmd == SIOCGHWTSTAMP ||
  478. (cmd >= SIOCDEVPRIVATE &&
  479. cmd <= SIOCDEVPRIVATE + 15)) {
  480. dev_load(net, ifr.ifr_name);
  481. rtnl_lock();
  482. ret = dev_ifsioc(net, &ifr, cmd);
  483. rtnl_unlock();
  484. if (!ret && copy_to_user(arg, &ifr,
  485. sizeof(struct ifreq)))
  486. ret = -EFAULT;
  487. return ret;
  488. }
  489. /* Take care of Wireless Extensions */
  490. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
  491. return wext_handle_ioctl(net, &ifr, cmd, arg);
  492. return -ENOTTY;
  493. }
  494. }