br_ioctl.c 8.6 KB

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