br_ioctl.c 9.0 KB

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