br_ioctl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Ioctl handler
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * $Id: br_ioctl.c,v 1.4 2000/11/08 05:16:40 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/capability.h>
  16. #include <linux/kernel.h>
  17. #include <linux/if_bridge.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/times.h>
  20. #include <net/net_namespace.h>
  21. #include <asm/uaccess.h>
  22. #include "br_private.h"
  23. /* called with RTNL */
  24. static int get_bridge_ifindices(int *indices, int num)
  25. {
  26. struct net_device *dev;
  27. int i = 0;
  28. for_each_netdev(&init_net, dev) {
  29. if (i >= num)
  30. break;
  31. if (dev->priv_flags & IFF_EBRIDGE)
  32. indices[i++] = dev->ifindex;
  33. }
  34. return i;
  35. }
  36. /* called with RTNL */
  37. static void get_port_ifindices(struct net_bridge *br, int *ifindices, int num)
  38. {
  39. struct net_bridge_port *p;
  40. list_for_each_entry(p, &br->port_list, list) {
  41. if (p->port_no < num)
  42. ifindices[p->port_no] = p->dev->ifindex;
  43. }
  44. }
  45. /*
  46. * Format up to a page worth of forwarding table entries
  47. * userbuf -- where to copy result
  48. * maxnum -- maximum number of entries desired
  49. * (limited to a page for sanity)
  50. * offset -- number of records to skip
  51. */
  52. static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
  53. unsigned long maxnum, unsigned long offset)
  54. {
  55. int num;
  56. void *buf;
  57. size_t size;
  58. /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
  59. if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
  60. maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
  61. size = maxnum * sizeof(struct __fdb_entry);
  62. buf = kmalloc(size, GFP_USER);
  63. if (!buf)
  64. return -ENOMEM;
  65. num = br_fdb_fillbuf(br, buf, maxnum, offset);
  66. if (num > 0) {
  67. if (copy_to_user(userbuf, buf, num*sizeof(struct __fdb_entry)))
  68. num = -EFAULT;
  69. }
  70. kfree(buf);
  71. return num;
  72. }
  73. static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
  74. {
  75. struct net_device *dev;
  76. int ret;
  77. if (!capable(CAP_NET_ADMIN))
  78. return -EPERM;
  79. dev = dev_get_by_index(&init_net, ifindex);
  80. if (dev == NULL)
  81. return -EINVAL;
  82. if (isadd)
  83. ret = br_add_if(br, dev);
  84. else
  85. ret = br_del_if(br, dev);
  86. dev_put(dev);
  87. return ret;
  88. }
  89. /*
  90. * Legacy ioctl's through SIOCDEVPRIVATE
  91. * This interface is deprecated because it was too difficult to
  92. * to do the translation for 32/64bit ioctl compatability.
  93. */
  94. static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  95. {
  96. struct net_bridge *br = netdev_priv(dev);
  97. unsigned long args[4];
  98. if (copy_from_user(args, rq->ifr_data, sizeof(args)))
  99. return -EFAULT;
  100. switch (args[0]) {
  101. case BRCTL_ADD_IF:
  102. case BRCTL_DEL_IF:
  103. return add_del_if(br, args[1], args[0] == BRCTL_ADD_IF);
  104. case BRCTL_GET_BRIDGE_INFO:
  105. {
  106. struct __bridge_info b;
  107. memset(&b, 0, sizeof(struct __bridge_info));
  108. rcu_read_lock();
  109. memcpy(&b.designated_root, &br->designated_root, 8);
  110. memcpy(&b.bridge_id, &br->bridge_id, 8);
  111. b.root_path_cost = br->root_path_cost;
  112. b.max_age = jiffies_to_clock_t(br->max_age);
  113. b.hello_time = jiffies_to_clock_t(br->hello_time);
  114. b.forward_delay = br->forward_delay;
  115. b.bridge_max_age = br->bridge_max_age;
  116. b.bridge_hello_time = br->bridge_hello_time;
  117. b.bridge_forward_delay = jiffies_to_clock_t(br->bridge_forward_delay);
  118. b.topology_change = br->topology_change;
  119. b.topology_change_detected = br->topology_change_detected;
  120. b.root_port = br->root_port;
  121. b.stp_enabled = (br->stp_enabled != BR_NO_STP);
  122. b.ageing_time = jiffies_to_clock_t(br->ageing_time);
  123. b.hello_timer_value = br_timer_value(&br->hello_timer);
  124. b.tcn_timer_value = br_timer_value(&br->tcn_timer);
  125. b.topology_change_timer_value = br_timer_value(&br->topology_change_timer);
  126. b.gc_timer_value = br_timer_value(&br->gc_timer);
  127. rcu_read_unlock();
  128. if (copy_to_user((void __user *)args[1], &b, sizeof(b)))
  129. return -EFAULT;
  130. return 0;
  131. }
  132. case BRCTL_GET_PORT_LIST:
  133. {
  134. int num, *indices;
  135. num = args[2];
  136. if (num < 0)
  137. return -EINVAL;
  138. if (num == 0)
  139. num = 256;
  140. if (num > BR_MAX_PORTS)
  141. num = BR_MAX_PORTS;
  142. indices = kcalloc(num, sizeof(int), GFP_KERNEL);
  143. if (indices == NULL)
  144. return -ENOMEM;
  145. get_port_ifindices(br, indices, num);
  146. if (copy_to_user((void __user *)args[1], indices, num*sizeof(int)))
  147. num = -EFAULT;
  148. kfree(indices);
  149. return num;
  150. }
  151. case BRCTL_SET_BRIDGE_FORWARD_DELAY:
  152. if (!capable(CAP_NET_ADMIN))
  153. return -EPERM;
  154. spin_lock_bh(&br->lock);
  155. br->bridge_forward_delay = clock_t_to_jiffies(args[1]);
  156. if (br_is_root_bridge(br))
  157. br->forward_delay = br->bridge_forward_delay;
  158. spin_unlock_bh(&br->lock);
  159. return 0;
  160. case BRCTL_SET_BRIDGE_HELLO_TIME:
  161. if (!capable(CAP_NET_ADMIN))
  162. return -EPERM;
  163. spin_lock_bh(&br->lock);
  164. br->bridge_hello_time = clock_t_to_jiffies(args[1]);
  165. if (br_is_root_bridge(br))
  166. br->hello_time = br->bridge_hello_time;
  167. spin_unlock_bh(&br->lock);
  168. return 0;
  169. case BRCTL_SET_BRIDGE_MAX_AGE:
  170. if (!capable(CAP_NET_ADMIN))
  171. return -EPERM;
  172. spin_lock_bh(&br->lock);
  173. br->bridge_max_age = clock_t_to_jiffies(args[1]);
  174. if (br_is_root_bridge(br))
  175. br->max_age = br->bridge_max_age;
  176. spin_unlock_bh(&br->lock);
  177. return 0;
  178. case BRCTL_SET_AGEING_TIME:
  179. if (!capable(CAP_NET_ADMIN))
  180. return -EPERM;
  181. br->ageing_time = clock_t_to_jiffies(args[1]);
  182. return 0;
  183. case BRCTL_GET_PORT_INFO:
  184. {
  185. struct __port_info p;
  186. struct net_bridge_port *pt;
  187. rcu_read_lock();
  188. if ((pt = br_get_port(br, args[2])) == NULL) {
  189. rcu_read_unlock();
  190. return -EINVAL;
  191. }
  192. memset(&p, 0, sizeof(struct __port_info));
  193. memcpy(&p.designated_root, &pt->designated_root, 8);
  194. memcpy(&p.designated_bridge, &pt->designated_bridge, 8);
  195. p.port_id = pt->port_id;
  196. p.designated_port = pt->designated_port;
  197. p.path_cost = pt->path_cost;
  198. p.designated_cost = pt->designated_cost;
  199. p.state = pt->state;
  200. p.top_change_ack = pt->topology_change_ack;
  201. p.config_pending = pt->config_pending;
  202. p.message_age_timer_value = br_timer_value(&pt->message_age_timer);
  203. p.forward_delay_timer_value = br_timer_value(&pt->forward_delay_timer);
  204. p.hold_timer_value = br_timer_value(&pt->hold_timer);
  205. rcu_read_unlock();
  206. if (copy_to_user((void __user *)args[1], &p, sizeof(p)))
  207. return -EFAULT;
  208. return 0;
  209. }
  210. case BRCTL_SET_BRIDGE_STP_STATE:
  211. if (!capable(CAP_NET_ADMIN))
  212. return -EPERM;
  213. br_stp_set_enabled(br, args[1]);
  214. return 0;
  215. case BRCTL_SET_BRIDGE_PRIORITY:
  216. if (!capable(CAP_NET_ADMIN))
  217. return -EPERM;
  218. spin_lock_bh(&br->lock);
  219. br_stp_set_bridge_priority(br, args[1]);
  220. spin_unlock_bh(&br->lock);
  221. return 0;
  222. case BRCTL_SET_PORT_PRIORITY:
  223. {
  224. struct net_bridge_port *p;
  225. int ret = 0;
  226. if (!capable(CAP_NET_ADMIN))
  227. return -EPERM;
  228. if (args[2] >= (1<<(16-BR_PORT_BITS)))
  229. return -ERANGE;
  230. spin_lock_bh(&br->lock);
  231. if ((p = br_get_port(br, args[1])) == NULL)
  232. ret = -EINVAL;
  233. else
  234. br_stp_set_port_priority(p, args[2]);
  235. spin_unlock_bh(&br->lock);
  236. return ret;
  237. }
  238. case BRCTL_SET_PATH_COST:
  239. {
  240. struct net_bridge_port *p;
  241. int ret = 0;
  242. if (!capable(CAP_NET_ADMIN))
  243. return -EPERM;
  244. if ((p = br_get_port(br, args[1])) == NULL)
  245. ret = -EINVAL;
  246. else
  247. br_stp_set_path_cost(p, args[2]);
  248. return ret;
  249. }
  250. case BRCTL_GET_FDB_ENTRIES:
  251. return get_fdb_entries(br, (void __user *)args[1],
  252. args[2], args[3]);
  253. }
  254. return -EOPNOTSUPP;
  255. }
  256. static int old_deviceless(void __user *uarg)
  257. {
  258. unsigned long args[3];
  259. if (copy_from_user(args, uarg, sizeof(args)))
  260. return -EFAULT;
  261. switch (args[0]) {
  262. case BRCTL_GET_VERSION:
  263. return BRCTL_VERSION;
  264. case BRCTL_GET_BRIDGES:
  265. {
  266. int *indices;
  267. int ret = 0;
  268. if (args[2] >= 2048)
  269. return -ENOMEM;
  270. indices = kcalloc(args[2], sizeof(int), GFP_KERNEL);
  271. if (indices == NULL)
  272. return -ENOMEM;
  273. args[2] = get_bridge_ifindices(indices, args[2]);
  274. ret = copy_to_user((void __user *)args[1], indices, args[2]*sizeof(int))
  275. ? -EFAULT : args[2];
  276. kfree(indices);
  277. return ret;
  278. }
  279. case BRCTL_ADD_BRIDGE:
  280. case BRCTL_DEL_BRIDGE:
  281. {
  282. char buf[IFNAMSIZ];
  283. if (!capable(CAP_NET_ADMIN))
  284. return -EPERM;
  285. if (copy_from_user(buf, (void __user *)args[1], IFNAMSIZ))
  286. return -EFAULT;
  287. buf[IFNAMSIZ-1] = 0;
  288. if (args[0] == BRCTL_ADD_BRIDGE)
  289. return br_add_bridge(buf);
  290. return br_del_bridge(buf);
  291. }
  292. }
  293. return -EOPNOTSUPP;
  294. }
  295. int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
  296. {
  297. switch (cmd) {
  298. case SIOCGIFBR:
  299. case SIOCSIFBR:
  300. return old_deviceless(uarg);
  301. case SIOCBRADDBR:
  302. case SIOCBRDELBR:
  303. {
  304. char buf[IFNAMSIZ];
  305. if (!capable(CAP_NET_ADMIN))
  306. return -EPERM;
  307. if (copy_from_user(buf, uarg, IFNAMSIZ))
  308. return -EFAULT;
  309. buf[IFNAMSIZ-1] = 0;
  310. if (cmd == SIOCBRADDBR)
  311. return br_add_bridge(buf);
  312. return br_del_bridge(buf);
  313. }
  314. }
  315. return -EOPNOTSUPP;
  316. }
  317. int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  318. {
  319. struct net_bridge *br = netdev_priv(dev);
  320. switch(cmd) {
  321. case SIOCDEVPRIVATE:
  322. return old_dev_ioctl(dev, rq, cmd);
  323. case SIOCBRADDIF:
  324. case SIOCBRDELIF:
  325. return add_del_if(br, rq->ifr_ifindex, cmd == SIOCBRADDIF);
  326. }
  327. pr_debug("Bridge does not support ioctl 0x%x\n", cmd);
  328. return -EOPNOTSUPP;
  329. }