PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/net/core/dev_ioctl.c

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