dev_ioctl.c 13 KB

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