br_ioctl.c 9.0 KB

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