dev_ioctl.c 13 KB

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