bridge.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include "netifd.h"
  20. #include "device.h"
  21. #include "interface.h"
  22. #include "system.h"
  23. #include "ubus.h"
  24. enum {
  25. BRIDGE_ATTR_PORTS,
  26. BRIDGE_ATTR_STP,
  27. BRIDGE_ATTR_FORWARD_DELAY,
  28. BRIDGE_ATTR_PRIORITY,
  29. BRIDGE_ATTR_IGMP_SNOOP,
  30. BRIDGE_ATTR_AGEING_TIME,
  31. BRIDGE_ATTR_HELLO_TIME,
  32. BRIDGE_ATTR_MAX_AGE,
  33. BRIDGE_ATTR_BRIDGE_EMPTY,
  34. BRIDGE_ATTR_MULTICAST_QUERIER,
  35. BRIDGE_ATTR_HASH_MAX,
  36. BRIDGE_ATTR_ROBUSTNESS,
  37. BRIDGE_ATTR_QUERY_INTERVAL,
  38. BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL,
  39. BRIDGE_ATTR_LAST_MEMBER_INTERVAL,
  40. BRIDGE_ATTR_VLAN_FILTERING,
  41. BRIDGE_ATTR_HAS_VLANS,
  42. BRIDGE_ATTR_STP_KERNEL,
  43. BRIDGE_ATTR_STP_PROTO,
  44. __BRIDGE_ATTR_MAX
  45. };
  46. static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
  47. [BRIDGE_ATTR_PORTS] = { "ports", BLOBMSG_TYPE_ARRAY },
  48. [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
  49. [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
  50. [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
  51. [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
  52. [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
  53. [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
  54. [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
  55. [BRIDGE_ATTR_BRIDGE_EMPTY] = { "bridge_empty", BLOBMSG_TYPE_BOOL },
  56. [BRIDGE_ATTR_MULTICAST_QUERIER] = { "multicast_querier", BLOBMSG_TYPE_BOOL },
  57. [BRIDGE_ATTR_HASH_MAX] = { "hash_max", BLOBMSG_TYPE_INT32 },
  58. [BRIDGE_ATTR_ROBUSTNESS] = { "robustness", BLOBMSG_TYPE_INT32 },
  59. [BRIDGE_ATTR_QUERY_INTERVAL] = { "query_interval", BLOBMSG_TYPE_INT32 },
  60. [BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL] = { "query_response_interval", BLOBMSG_TYPE_INT32 },
  61. [BRIDGE_ATTR_LAST_MEMBER_INTERVAL] = { "last_member_interval", BLOBMSG_TYPE_INT32 },
  62. [BRIDGE_ATTR_VLAN_FILTERING] = { "vlan_filtering", BLOBMSG_TYPE_BOOL },
  63. [BRIDGE_ATTR_HAS_VLANS] = { "__has_vlans", BLOBMSG_TYPE_BOOL }, /* internal */
  64. [BRIDGE_ATTR_STP_KERNEL] = { "stp_kernel", BLOBMSG_TYPE_BOOL },
  65. [BRIDGE_ATTR_STP_PROTO] = { "stp_proto", BLOBMSG_TYPE_STRING },
  66. };
  67. static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
  68. [BRIDGE_ATTR_PORTS] = { .type = BLOBMSG_TYPE_STRING },
  69. };
  70. static const struct uci_blob_param_list bridge_attr_list = {
  71. .n_params = __BRIDGE_ATTR_MAX,
  72. .params = bridge_attrs,
  73. .info = bridge_attr_info,
  74. .n_next = 1,
  75. .next = { &device_attr_list },
  76. };
  77. static struct blob_buf b;
  78. static struct device *bridge_create(const char *name, struct device_type *devtype,
  79. struct blob_attr *attr);
  80. static void bridge_config_init(struct device *dev);
  81. static void bridge_dev_vlan_update(struct device *dev);
  82. static void bridge_free(struct device *dev);
  83. static void bridge_stp_init(struct device *dev);
  84. static void bridge_dump_info(struct device *dev, struct blob_buf *b);
  85. static enum dev_change_type
  86. bridge_reload(struct device *dev, struct blob_attr *attr);
  87. static struct device_type bridge_device_type = {
  88. .name = "bridge",
  89. .config_params = &bridge_attr_list,
  90. .bridge_capability = true,
  91. .name_prefix = "br",
  92. .create = bridge_create,
  93. .config_init = bridge_config_init,
  94. .vlan_update = bridge_dev_vlan_update,
  95. .reload = bridge_reload,
  96. .free = bridge_free,
  97. .dump_info = bridge_dump_info,
  98. .stp_init = bridge_stp_init,
  99. };
  100. struct bridge_state {
  101. struct device dev;
  102. device_state_cb set_state;
  103. struct blob_attr *config_data;
  104. struct bridge_config config;
  105. struct blob_attr *ports;
  106. bool active;
  107. bool force_active;
  108. bool has_vlans;
  109. struct uloop_timeout retry;
  110. struct bridge_member *primary_port;
  111. struct vlist_tree members;
  112. int n_present;
  113. int n_failed;
  114. };
  115. struct bridge_member {
  116. struct vlist_node node;
  117. struct bridge_state *bst;
  118. struct device_user dev;
  119. struct uloop_timeout check_timer;
  120. uint16_t pvid;
  121. bool present;
  122. bool active;
  123. char name[];
  124. };
  125. static void
  126. bridge_reset_primary(struct bridge_state *bst)
  127. {
  128. struct bridge_member *bm;
  129. if (!bst->primary_port &&
  130. (bst->dev.settings.flags & DEV_OPT_MACADDR))
  131. return;
  132. bst->primary_port = NULL;
  133. bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
  134. vlist_for_each_element(&bst->members, bm, node) {
  135. uint8_t *macaddr;
  136. if (!bm->present)
  137. continue;
  138. bst->primary_port = bm;
  139. if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
  140. macaddr = bm->dev.dev->settings.macaddr;
  141. else
  142. macaddr = bm->dev.dev->orig_settings.macaddr;
  143. memcpy(bst->dev.settings.macaddr, macaddr, 6);
  144. bst->dev.settings.flags |= DEV_OPT_MACADDR;
  145. return;
  146. }
  147. }
  148. static struct bridge_vlan_port *
  149. bridge_find_vlan_member_port(struct bridge_member *bm, struct bridge_vlan *vlan)
  150. {
  151. struct bridge_vlan_hotplug_port *port;
  152. const char *ifname = bm->dev.dev->ifname;
  153. int i;
  154. for (i = 0; i < vlan->n_ports; i++) {
  155. if (strcmp(vlan->ports[i].ifname, ifname) != 0)
  156. continue;
  157. return &vlan->ports[i];
  158. }
  159. list_for_each_entry(port, &vlan->hotplug_ports, list) {
  160. if (strcmp(port->port.ifname, ifname) != 0)
  161. continue;
  162. return &port->port;
  163. }
  164. return NULL;
  165. }
  166. static bool
  167. bridge_member_vlan_is_pvid(struct bridge_member *bm, struct bridge_vlan_port *port)
  168. {
  169. return (!bm->pvid && (port->flags & BRVLAN_F_UNTAGGED)) ||
  170. (port->flags & BRVLAN_F_PVID);
  171. }
  172. static void
  173. __bridge_set_member_vlan(struct bridge_member *bm, struct bridge_vlan *vlan,
  174. struct bridge_vlan_port *port, bool add)
  175. {
  176. uint16_t flags;
  177. flags = port->flags;
  178. if (bm->pvid == vlan->vid)
  179. flags |= BRVLAN_F_PVID;
  180. system_bridge_vlan(port->ifname, vlan->vid, add, flags);
  181. }
  182. static void
  183. bridge_set_member_vlan(struct bridge_member *bm, struct bridge_vlan *vlan, bool add)
  184. {
  185. struct bridge_vlan_port *port;
  186. if (!bm->present)
  187. return;
  188. port = bridge_find_vlan_member_port(bm, vlan);
  189. if (!port)
  190. return;
  191. if (bridge_member_vlan_is_pvid(bm, port))
  192. bm->pvid = vlan->vid;
  193. __bridge_set_member_vlan(bm, vlan, port, add);
  194. }
  195. static void
  196. bridge_set_local_vlan(struct bridge_state *bst, struct bridge_vlan *vlan, bool add)
  197. {
  198. if (!vlan->local && add)
  199. return;
  200. system_bridge_vlan(bst->dev.ifname, vlan->vid, add, BRVLAN_F_SELF);
  201. }
  202. static void
  203. bridge_set_local_vlans(struct bridge_state *bst, bool add)
  204. {
  205. struct bridge_vlan *vlan;
  206. vlist_for_each_element(&bst->dev.vlans, vlan, node)
  207. bridge_set_local_vlan(bst, vlan, add);
  208. }
  209. static struct bridge_vlan *
  210. bridge_recalc_member_pvid(struct bridge_member *bm)
  211. {
  212. struct bridge_state *bst = bm->bst;
  213. struct bridge_vlan_port *port;
  214. struct bridge_vlan *vlan, *ret = NULL;
  215. vlist_for_each_element(&bst->dev.vlans, vlan, node) {
  216. port = bridge_find_vlan_member_port(bm, vlan);
  217. if (!port)
  218. continue;
  219. if (!bridge_member_vlan_is_pvid(bm, port))
  220. continue;
  221. ret = vlan;
  222. if (port->flags & BRVLAN_F_PVID)
  223. break;
  224. }
  225. return ret;
  226. }
  227. static void
  228. bridge_set_vlan_state(struct bridge_state *bst, struct bridge_vlan *vlan, bool add)
  229. {
  230. struct bridge_member *bm;
  231. struct bridge_vlan *vlan2;
  232. bridge_set_local_vlan(bst, vlan, add);
  233. vlist_for_each_element(&bst->members, bm, node) {
  234. struct bridge_vlan_port *port;
  235. int new_pvid = -1;
  236. port = bridge_find_vlan_member_port(bm, vlan);
  237. if (!port)
  238. continue;
  239. if (add) {
  240. if (bridge_member_vlan_is_pvid(bm, port))
  241. bm->pvid = vlan->vid;
  242. } else if (bm->pvid == vlan->vid) {
  243. vlan2 = bridge_recalc_member_pvid(bm);
  244. if (vlan2 && vlan2->vid != vlan->vid) {
  245. bridge_set_member_vlan(bm, vlan2, false);
  246. bridge_set_member_vlan(bm, vlan2, true);
  247. }
  248. new_pvid = vlan2 ? vlan2->vid : 0;
  249. }
  250. if (!bm->present)
  251. continue;
  252. __bridge_set_member_vlan(bm, vlan, port, add);
  253. if (new_pvid >= 0)
  254. bm->pvid = new_pvid;
  255. }
  256. }
  257. static int
  258. bridge_disable_member(struct bridge_member *bm, bool keep_dev)
  259. {
  260. struct bridge_state *bst = bm->bst;
  261. struct bridge_vlan *vlan;
  262. if (!bm->present || !bm->active)
  263. return 0;
  264. bm->active = false;
  265. vlist_for_each_element(&bst->dev.vlans, vlan, node)
  266. bridge_set_member_vlan(bm, vlan, false);
  267. system_bridge_delif(&bst->dev, bm->dev.dev);
  268. if (!keep_dev)
  269. device_release(&bm->dev);
  270. device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
  271. return 0;
  272. }
  273. static void bridge_stp_notify(struct bridge_state *bst)
  274. {
  275. struct bridge_config *cfg = &bst->config;
  276. if (!cfg->stp || cfg->stp_kernel)
  277. return;
  278. blob_buf_init(&b, 0);
  279. blobmsg_add_string(&b, "name", bst->dev.ifname);
  280. if (cfg->stp_proto)
  281. blobmsg_add_string(&b, "proto", cfg->stp_proto);
  282. blobmsg_add_u32(&b, "forward_delay", cfg->forward_delay);
  283. blobmsg_add_u32(&b, "hello_time", cfg->hello_time);
  284. blobmsg_add_u32(&b, "max_age", cfg->max_age);
  285. if (cfg->flags & BRIDGE_OPT_AGEING_TIME)
  286. blobmsg_add_u32(&b, "ageing_time", cfg->ageing_time);
  287. netifd_ubus_device_notify("stp_init", b.head, 1000);
  288. }
  289. static int
  290. bridge_enable_interface(struct bridge_state *bst)
  291. {
  292. int ret;
  293. if (bst->active)
  294. return 0;
  295. bridge_stp_notify(bst);
  296. ret = system_bridge_addbr(&bst->dev, &bst->config);
  297. if (ret < 0)
  298. return ret;
  299. if (bst->has_vlans) {
  300. /* delete default VLAN 1 */
  301. system_bridge_vlan(bst->dev.ifname, 1, false, BRVLAN_F_SELF);
  302. bridge_set_local_vlans(bst, true);
  303. }
  304. bst->active = true;
  305. return 0;
  306. }
  307. static void
  308. bridge_stp_init(struct device *dev)
  309. {
  310. struct bridge_state *bst;
  311. bst = container_of(dev, struct bridge_state, dev);
  312. if (!bst->config.stp || !bst->active)
  313. return;
  314. bridge_stp_notify(bst);
  315. system_bridge_set_stp_state(&bst->dev, false);
  316. system_bridge_set_stp_state(&bst->dev, true);
  317. }
  318. static void
  319. bridge_disable_interface(struct bridge_state *bst)
  320. {
  321. if (!bst->active)
  322. return;
  323. system_bridge_delbr(&bst->dev);
  324. bst->active = false;
  325. }
  326. static int
  327. bridge_enable_member(struct bridge_member *bm)
  328. {
  329. struct bridge_state *bst = bm->bst;
  330. struct bridge_vlan *vlan;
  331. struct device *dev;
  332. int ret;
  333. if (!bm->present)
  334. return 0;
  335. ret = bridge_enable_interface(bst);
  336. if (ret)
  337. goto error;
  338. /* Disable IPv6 for bridge members */
  339. if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
  340. bm->dev.dev->settings.ipv6 = 0;
  341. bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
  342. }
  343. ret = device_claim(&bm->dev);
  344. if (ret < 0)
  345. goto error;
  346. dev = bm->dev.dev;
  347. if (dev->settings.auth && !dev->auth_status)
  348. return -1;
  349. if (bm->active)
  350. return 0;
  351. ret = system_bridge_addif(&bst->dev, bm->dev.dev);
  352. if (ret < 0) {
  353. D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
  354. goto error;
  355. }
  356. bm->active = true;
  357. if (bst->has_vlans) {
  358. /* delete default VLAN 1 */
  359. system_bridge_vlan(bm->dev.dev->ifname, 1, false, 0);
  360. vlist_for_each_element(&bst->dev.vlans, vlan, node)
  361. bridge_set_member_vlan(bm, vlan, true);
  362. }
  363. device_set_present(&bst->dev, true);
  364. device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
  365. return 0;
  366. error:
  367. bst->n_failed++;
  368. bm->present = false;
  369. bst->n_present--;
  370. device_release(&bm->dev);
  371. return ret;
  372. }
  373. static void
  374. bridge_remove_member(struct bridge_member *bm)
  375. {
  376. struct bridge_state *bst = bm->bst;
  377. if (!bm->present)
  378. return;
  379. if (bst->dev.active)
  380. bridge_disable_member(bm, false);
  381. bm->present = false;
  382. bm->bst->n_present--;
  383. if (bm == bst->primary_port)
  384. bridge_reset_primary(bst);
  385. if (bst->config.bridge_empty)
  386. return;
  387. bst->force_active = false;
  388. if (bst->n_present == 0)
  389. device_set_present(&bst->dev, false);
  390. }
  391. static void
  392. bridge_free_member(struct bridge_member *bm)
  393. {
  394. struct bridge_state *bst = bm->bst;
  395. struct device *dev = bm->dev.dev;
  396. const char *ifname = dev->ifname;
  397. struct bridge_vlan *vlan;
  398. bridge_remove_member(bm);
  399. restart:
  400. vlist_for_each_element(&bst->dev.vlans, vlan, node) {
  401. struct bridge_vlan_hotplug_port *port, *tmp;
  402. bool free_port = false;
  403. list_for_each_entry_safe(port, tmp, &vlan->hotplug_ports, list) {
  404. if (strcmp(port->port.ifname, ifname) != 0)
  405. continue;
  406. list_del(&port->list);
  407. free(port);
  408. free_port = true;
  409. }
  410. if (!free_port || !list_empty(&vlan->hotplug_ports) ||
  411. vlan->n_ports || vlan->node.version != -1)
  412. continue;
  413. vlist_delete(&bst->dev.vlans, &vlan->node);
  414. goto restart;
  415. }
  416. device_remove_user(&bm->dev);
  417. uloop_timeout_cancel(&bm->check_timer);
  418. /*
  419. * When reloading the config and moving a device from one bridge to
  420. * another, the other bridge may have tried to claim this device
  421. * before it was removed here.
  422. * Ensure that claiming the device is retried by toggling its present
  423. * state
  424. */
  425. if (dev->present) {
  426. device_set_present(dev, false);
  427. device_set_present(dev, true);
  428. }
  429. free(bm);
  430. }
  431. static void
  432. bridge_check_retry(struct bridge_state *bst)
  433. {
  434. if (!bst->n_failed)
  435. return;
  436. uloop_timeout_set(&bst->retry, 100);
  437. }
  438. static void
  439. bridge_member_check_cb(struct uloop_timeout *t)
  440. {
  441. struct bridge_member *bm;
  442. struct bridge_state *bst;
  443. bm = container_of(t, struct bridge_member, check_timer);
  444. bst = bm->bst;
  445. if (system_bridge_vlan_check(&bst->dev, bm->dev.dev->ifname) <= 0)
  446. return;
  447. bridge_disable_member(bm, true);
  448. bridge_enable_member(bm);
  449. }
  450. static void
  451. bridge_member_cb(struct device_user *dep, enum device_event ev)
  452. {
  453. struct bridge_member *bm = container_of(dep, struct bridge_member, dev);
  454. struct bridge_state *bst = bm->bst;
  455. struct device *dev = dep->dev;
  456. switch (ev) {
  457. case DEV_EVENT_ADD:
  458. assert(!bm->present);
  459. bm->present = true;
  460. bst->n_present++;
  461. if (bst->n_present == 1)
  462. device_set_present(&bst->dev, true);
  463. fallthrough;
  464. case DEV_EVENT_AUTH_UP:
  465. if (!bst->dev.active)
  466. break;
  467. if (bridge_enable_member(bm))
  468. break;
  469. /*
  470. * Adding a bridge member can overwrite the bridge mtu
  471. * in the kernel, apply the bridge settings in case the
  472. * bridge mtu is set
  473. */
  474. system_if_apply_settings(&bst->dev, &bst->dev.settings,
  475. DEV_OPT_MTU | DEV_OPT_MTU6);
  476. break;
  477. case DEV_EVENT_LINK_UP:
  478. if (bst->has_vlans)
  479. uloop_timeout_set(&bm->check_timer, 1000);
  480. break;
  481. case DEV_EVENT_LINK_DOWN:
  482. if (!dev->settings.auth)
  483. break;
  484. bridge_disable_member(bm, true);
  485. break;
  486. case DEV_EVENT_REMOVE:
  487. if (dep->hotplug) {
  488. vlist_delete(&bst->members, &bm->node);
  489. return;
  490. }
  491. if (bm->present)
  492. bridge_remove_member(bm);
  493. break;
  494. default:
  495. return;
  496. }
  497. }
  498. static int
  499. bridge_set_down(struct bridge_state *bst)
  500. {
  501. struct bridge_member *bm;
  502. bst->set_state(&bst->dev, false);
  503. vlist_for_each_element(&bst->members, bm, node)
  504. bridge_disable_member(bm, false);
  505. bridge_disable_interface(bst);
  506. return 0;
  507. }
  508. static int
  509. bridge_set_up(struct bridge_state *bst)
  510. {
  511. struct bridge_member *bm;
  512. int ret;
  513. bst->has_vlans = !avl_is_empty(&bst->dev.vlans.avl);
  514. if (!bst->n_present) {
  515. if (!bst->force_active)
  516. return -ENOENT;
  517. ret = bridge_enable_interface(bst);
  518. if (ret)
  519. return ret;
  520. }
  521. bst->n_failed = 0;
  522. vlist_for_each_element(&bst->members, bm, node)
  523. bridge_enable_member(bm);
  524. bridge_check_retry(bst);
  525. if (!bst->force_active && !bst->n_present) {
  526. /* initialization of all member interfaces failed */
  527. bridge_disable_interface(bst);
  528. device_set_present(&bst->dev, false);
  529. return -ENOENT;
  530. }
  531. bridge_reset_primary(bst);
  532. ret = bst->set_state(&bst->dev, true);
  533. if (ret < 0)
  534. bridge_set_down(bst);
  535. return ret;
  536. }
  537. static int
  538. bridge_set_state(struct device *dev, bool up)
  539. {
  540. struct bridge_state *bst;
  541. bst = container_of(dev, struct bridge_state, dev);
  542. if (up)
  543. return bridge_set_up(bst);
  544. else
  545. return bridge_set_down(bst);
  546. }
  547. static struct bridge_member *
  548. bridge_create_member(struct bridge_state *bst, const char *name,
  549. struct device *dev, bool hotplug)
  550. {
  551. struct bridge_member *bm;
  552. bm = calloc(1, sizeof(*bm) + strlen(name) + 1);
  553. if (!bm)
  554. return NULL;
  555. bm->bst = bst;
  556. bm->dev.cb = bridge_member_cb;
  557. bm->dev.hotplug = hotplug;
  558. bm->check_timer.cb = bridge_member_check_cb;
  559. strcpy(bm->name, name);
  560. bm->dev.dev = dev;
  561. vlist_add(&bst->members, &bm->node, bm->name);
  562. /*
  563. * Need to look up the bridge member again as the above
  564. * created pointer will be freed in case the bridge member
  565. * already existed
  566. */
  567. bm = vlist_find(&bst->members, name, bm, node);
  568. if (hotplug && bm)
  569. bm->node.version = -1;
  570. return bm;
  571. }
  572. static void
  573. bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
  574. struct vlist_node *node_old)
  575. {
  576. struct bridge_member *bm;
  577. struct device *dev;
  578. if (node_new) {
  579. bm = container_of(node_new, struct bridge_member, node);
  580. if (node_old) {
  581. free(bm);
  582. return;
  583. }
  584. dev = bm->dev.dev;
  585. bm->dev.dev = NULL;
  586. device_add_user(&bm->dev, dev);
  587. }
  588. if (node_old) {
  589. bm = container_of(node_old, struct bridge_member, node);
  590. bridge_free_member(bm);
  591. }
  592. }
  593. static void
  594. bridge_add_member(struct bridge_state *bst, const char *name)
  595. {
  596. struct device *dev;
  597. dev = device_get(name, true);
  598. if (!dev)
  599. return;
  600. bridge_create_member(bst, name, dev, false);
  601. }
  602. static struct bridge_vlan *
  603. bridge_hotplug_get_vlan(struct bridge_state *bst, uint16_t vid)
  604. {
  605. struct bridge_vlan *vlan;
  606. vlan = vlist_find(&bst->dev.vlans, &vid, vlan, node);
  607. if (vlan)
  608. return vlan;
  609. vlan = calloc(1, sizeof(*vlan));
  610. vlan->vid = vid;
  611. vlan->local = true;
  612. INIT_LIST_HEAD(&vlan->hotplug_ports);
  613. vlist_add(&bst->dev.vlans, &vlan->node, &vlan->vid);
  614. vlan->node.version = -1;
  615. return vlan;
  616. }
  617. static struct bridge_vlan_hotplug_port *
  618. bridge_hotplug_get_vlan_port(struct bridge_vlan *vlan, const char *ifname)
  619. {
  620. struct bridge_vlan_hotplug_port *port;
  621. list_for_each_entry(port, &vlan->hotplug_ports, list)
  622. if (!strcmp(port->port.ifname, ifname))
  623. return port;
  624. return NULL;
  625. }
  626. static void
  627. bridge_hotplug_set_member_vlans(struct bridge_state *bst, struct blob_attr *vlans,
  628. const char *ifname, struct bridge_member *bm, bool add)
  629. {
  630. struct bridge_vlan *vlan;
  631. struct blob_attr *cur;
  632. int rem;
  633. if (!vlans)
  634. return;
  635. blobmsg_for_each_attr(cur, vlans, rem) {
  636. struct bridge_vlan_hotplug_port *port;
  637. uint16_t flags = BRVLAN_F_UNTAGGED;
  638. char *name_buf;
  639. unsigned int vid;
  640. char *end;
  641. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  642. continue;
  643. vid = strtoul(blobmsg_get_string(cur), &end, 0);
  644. if (!vid || vid > 4095)
  645. continue;
  646. vlan = bridge_hotplug_get_vlan(bst, vid);
  647. if (!vlan)
  648. continue;
  649. if (vlan->pending) {
  650. vlan->pending = false;
  651. bridge_set_vlan_state(bst, vlan, true);
  652. }
  653. if (end && *end) {
  654. if (*end != ':')
  655. continue;
  656. for (end++; *end; end++) {
  657. switch (*end) {
  658. case 't':
  659. flags &= ~BRVLAN_F_UNTAGGED;
  660. break;
  661. case '*':
  662. flags |= BRVLAN_F_PVID;
  663. break;
  664. }
  665. }
  666. }
  667. port = bridge_hotplug_get_vlan_port(vlan, ifname);
  668. if (!add) {
  669. if (!port)
  670. continue;
  671. __bridge_set_member_vlan(bm, vlan, &port->port, false);
  672. list_del(&port->list);
  673. free(port);
  674. continue;
  675. }
  676. if (port) {
  677. if (port->port.flags == flags)
  678. continue;
  679. __bridge_set_member_vlan(bm, vlan, &port->port, false);
  680. port->port.flags = flags;
  681. __bridge_set_member_vlan(bm, vlan, &port->port, true);
  682. continue;
  683. }
  684. port = calloc_a(sizeof(*port), &name_buf, strlen(ifname) + 1);
  685. if (!port)
  686. continue;
  687. port->port.flags = flags;
  688. port->port.ifname = strcpy(name_buf, ifname);
  689. list_add_tail(&port->list, &vlan->hotplug_ports);
  690. if (!bm)
  691. continue;
  692. __bridge_set_member_vlan(bm, vlan, &port->port, true);
  693. }
  694. }
  695. static int
  696. bridge_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
  697. {
  698. struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
  699. struct bridge_member *bm;
  700. bm = vlist_find(&bst->members, member->ifname, bm, node);
  701. bridge_hotplug_set_member_vlans(bst, vlan, member->ifname, bm, true);
  702. if (!bm)
  703. bridge_create_member(bst, member->ifname, member, true);
  704. return 0;
  705. }
  706. static int
  707. bridge_hotplug_del(struct device *dev, struct device *member, struct blob_attr *vlan)
  708. {
  709. struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
  710. struct bridge_member *bm;
  711. bm = vlist_find(&bst->members, member->ifname, bm, node);
  712. if (!bm)
  713. return UBUS_STATUS_NOT_FOUND;
  714. bridge_hotplug_set_member_vlans(bst, vlan, member->ifname, bm, false);
  715. if (!bm->dev.hotplug)
  716. return 0;
  717. vlist_delete(&bst->members, &bm->node);
  718. return 0;
  719. }
  720. static int
  721. bridge_hotplug_prepare(struct device *dev, struct device **bridge_dev)
  722. {
  723. struct bridge_state *bst;
  724. if (bridge_dev)
  725. *bridge_dev = dev;
  726. bst = container_of(dev, struct bridge_state, dev);
  727. bst->force_active = true;
  728. device_set_present(&bst->dev, true);
  729. return 0;
  730. }
  731. static const struct device_hotplug_ops bridge_ops = {
  732. .prepare = bridge_hotplug_prepare,
  733. .add = bridge_hotplug_add,
  734. .del = bridge_hotplug_del
  735. };
  736. static void
  737. bridge_free(struct device *dev)
  738. {
  739. struct bridge_state *bst;
  740. bst = container_of(dev, struct bridge_state, dev);
  741. vlist_flush_all(&bst->members);
  742. vlist_flush_all(&dev->vlans);
  743. kvlist_free(&dev->vlan_aliases);
  744. free(bst->config_data);
  745. free(bst);
  746. }
  747. static void
  748. bridge_dump_port(struct blob_buf *b, struct bridge_vlan_port *port)
  749. {
  750. bool tagged = !(port->flags & BRVLAN_F_UNTAGGED);
  751. bool pvid = (port->flags & BRVLAN_F_PVID);
  752. blobmsg_printf(b, "%s%s%s%s\n", port->ifname,
  753. tagged || pvid ? ":" : "",
  754. tagged ? "t" : "",
  755. pvid ? "*" : "");
  756. }
  757. static void
  758. bridge_dump_vlan(struct blob_buf *b, struct bridge_vlan *vlan)
  759. {
  760. struct bridge_vlan_hotplug_port *port;
  761. void *c, *p;
  762. int i;
  763. c = blobmsg_open_table(b, NULL);
  764. blobmsg_add_u32(b, "id", vlan->vid);
  765. blobmsg_add_u8(b, "local", vlan->local);
  766. p = blobmsg_open_array(b, "ports");
  767. for (i = 0; i < vlan->n_ports; i++)
  768. bridge_dump_port(b, &vlan->ports[i]);
  769. list_for_each_entry(port, &vlan->hotplug_ports, list)
  770. bridge_dump_port(b, &port->port);
  771. blobmsg_close_array(b, p);
  772. blobmsg_close_table(b, c);
  773. }
  774. static void
  775. bridge_dump_info(struct device *dev, struct blob_buf *b)
  776. {
  777. struct bridge_state *bst;
  778. struct bridge_member *bm;
  779. struct bridge_vlan *vlan;
  780. void *list;
  781. bst = container_of(dev, struct bridge_state, dev);
  782. system_if_dump_info(dev, b);
  783. list = blobmsg_open_array(b, "bridge-members");
  784. vlist_for_each_element(&bst->members, bm, node) {
  785. if (bm->dev.dev->hidden)
  786. continue;
  787. blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
  788. }
  789. blobmsg_close_array(b, list);
  790. if (avl_is_empty(&dev->vlans.avl))
  791. return;
  792. list = blobmsg_open_array(b, "bridge-vlans");
  793. vlist_for_each_element(&bst->dev.vlans, vlan, node)
  794. bridge_dump_vlan(b, vlan);
  795. blobmsg_close_array(b, list);
  796. }
  797. static void
  798. bridge_config_init(struct device *dev)
  799. {
  800. struct bridge_state *bst;
  801. struct bridge_vlan *vlan;
  802. struct blob_attr *cur;
  803. int i, rem;
  804. bst = container_of(dev, struct bridge_state, dev);
  805. if (bst->config.bridge_empty) {
  806. bst->force_active = true;
  807. device_set_present(&bst->dev, true);
  808. }
  809. bst->n_failed = 0;
  810. vlist_update(&bst->members);
  811. if (bst->ports) {
  812. blobmsg_for_each_attr(cur, bst->ports, rem) {
  813. bridge_add_member(bst, blobmsg_data(cur));
  814. }
  815. }
  816. vlist_for_each_element(&bst->dev.vlans, vlan, node)
  817. for (i = 0; i < vlan->n_ports; i++)
  818. bridge_add_member(bst, vlan->ports[i].ifname);
  819. vlist_flush(&bst->members);
  820. bridge_check_retry(bst);
  821. }
  822. static void
  823. bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
  824. {
  825. struct bridge_config *cfg = &bst->config;
  826. struct blob_attr *cur;
  827. /* defaults */
  828. memset(cfg, 0, sizeof(*cfg));
  829. cfg->stp = false;
  830. cfg->stp_kernel = false;
  831. cfg->robustness = 2;
  832. cfg->igmp_snoop = false;
  833. cfg->multicast_querier = false;
  834. cfg->query_interval = 12500;
  835. cfg->query_response_interval = 1000;
  836. cfg->last_member_interval = 100;
  837. cfg->hash_max = 512;
  838. cfg->bridge_empty = false;
  839. cfg->priority = 0x7FFF;
  840. cfg->vlan_filtering = false;
  841. cfg->forward_delay = 8;
  842. cfg->max_age = 10;
  843. cfg->hello_time = 1;
  844. if ((cur = tb[BRIDGE_ATTR_STP]))
  845. cfg->stp = blobmsg_get_bool(cur);
  846. if ((cur = tb[BRIDGE_ATTR_STP_KERNEL]))
  847. cfg->stp = blobmsg_get_bool(cur);
  848. if ((cur = tb[BRIDGE_ATTR_STP_PROTO]))
  849. cfg->stp_proto = blobmsg_get_string(cur);
  850. if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
  851. cfg->forward_delay = blobmsg_get_u32(cur);
  852. if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
  853. cfg->priority = blobmsg_get_u32(cur);
  854. if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
  855. cfg->multicast_querier = cfg->igmp_snoop = blobmsg_get_bool(cur);
  856. if ((cur = tb[BRIDGE_ATTR_MULTICAST_QUERIER]))
  857. cfg->multicast_querier = blobmsg_get_bool(cur);
  858. if ((cur = tb[BRIDGE_ATTR_HASH_MAX]))
  859. cfg->hash_max = blobmsg_get_u32(cur);
  860. if ((cur = tb[BRIDGE_ATTR_ROBUSTNESS])) {
  861. cfg->robustness = blobmsg_get_u32(cur);
  862. cfg->flags |= BRIDGE_OPT_ROBUSTNESS;
  863. }
  864. if ((cur = tb[BRIDGE_ATTR_QUERY_INTERVAL])) {
  865. cfg->query_interval = blobmsg_get_u32(cur);
  866. cfg->flags |= BRIDGE_OPT_QUERY_INTERVAL;
  867. }
  868. if ((cur = tb[BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL])) {
  869. cfg->query_response_interval = blobmsg_get_u32(cur);
  870. cfg->flags |= BRIDGE_OPT_QUERY_RESPONSE_INTERVAL;
  871. }
  872. if ((cur = tb[BRIDGE_ATTR_LAST_MEMBER_INTERVAL])) {
  873. cfg->last_member_interval = blobmsg_get_u32(cur);
  874. cfg->flags |= BRIDGE_OPT_LAST_MEMBER_INTERVAL;
  875. }
  876. if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
  877. cfg->ageing_time = blobmsg_get_u32(cur);
  878. cfg->flags |= BRIDGE_OPT_AGEING_TIME;
  879. }
  880. if ((cur = tb[BRIDGE_ATTR_HELLO_TIME]))
  881. cfg->hello_time = blobmsg_get_u32(cur);
  882. if ((cur = tb[BRIDGE_ATTR_MAX_AGE]))
  883. cfg->max_age = blobmsg_get_u32(cur);
  884. if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
  885. cfg->bridge_empty = blobmsg_get_bool(cur);
  886. if ((cur = tb[BRIDGE_ATTR_VLAN_FILTERING]))
  887. cfg->vlan_filtering = blobmsg_get_bool(cur);
  888. }
  889. static enum dev_change_type
  890. bridge_reload(struct device *dev, struct blob_attr *attr)
  891. {
  892. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  893. struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
  894. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  895. unsigned long diff;
  896. struct bridge_state *bst;
  897. BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
  898. BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
  899. bst = container_of(dev, struct bridge_state, dev);
  900. attr = blob_memdup(attr);
  901. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  902. blob_data(attr), blob_len(attr));
  903. blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
  904. blob_data(attr), blob_len(attr));
  905. if (tb_dev[DEV_ATTR_MACADDR])
  906. bst->primary_port = NULL;
  907. bst->ports = tb_br[BRIDGE_ATTR_PORTS];
  908. device_init_settings(dev, tb_dev);
  909. bridge_apply_settings(bst, tb_br);
  910. if (bst->config_data) {
  911. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  912. struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
  913. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  914. blob_data(bst->config_data), blob_len(bst->config_data));
  915. diff = 0;
  916. uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
  917. if (diff)
  918. ret = DEV_CONFIG_RESTART;
  919. blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
  920. blob_data(bst->config_data), blob_len(bst->config_data));
  921. diff = 0;
  922. uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
  923. if (diff & ~(1 << BRIDGE_ATTR_PORTS))
  924. ret = DEV_CONFIG_RESTART;
  925. bridge_config_init(dev);
  926. }
  927. free(bst->config_data);
  928. bst->config_data = attr;
  929. return ret;
  930. }
  931. static void
  932. bridge_retry_members(struct uloop_timeout *timeout)
  933. {
  934. struct bridge_state *bst = container_of(timeout, struct bridge_state, retry);
  935. struct bridge_member *bm;
  936. bst->n_failed = 0;
  937. vlist_for_each_element(&bst->members, bm, node) {
  938. if (bm->present)
  939. continue;
  940. if (!bm->dev.dev->present)
  941. continue;
  942. bm->present = true;
  943. bst->n_present++;
  944. bridge_enable_member(bm);
  945. }
  946. }
  947. static int bridge_avl_cmp_u16(const void *k1, const void *k2, void *ptr)
  948. {
  949. const uint16_t *i1 = k1, *i2 = k2;
  950. return *i1 - *i2;
  951. }
  952. static bool
  953. bridge_vlan_equal(struct bridge_vlan *v1, struct bridge_vlan *v2)
  954. {
  955. int i;
  956. if (v1->n_ports != v2->n_ports)
  957. return false;
  958. for (i = 0; i < v1->n_ports; i++)
  959. if (v1->ports[i].flags != v2->ports[i].flags ||
  960. strcmp(v1->ports[i].ifname, v2->ports[i].ifname) != 0)
  961. return false;
  962. return true;
  963. }
  964. static void
  965. bridge_vlan_free(struct bridge_vlan *vlan)
  966. {
  967. struct bridge_vlan_hotplug_port *port, *tmp;
  968. if (!vlan)
  969. return;
  970. list_for_each_entry_safe(port, tmp, &vlan->hotplug_ports, list)
  971. free(port);
  972. free(vlan);
  973. }
  974. static void
  975. bridge_vlan_update(struct vlist_tree *tree, struct vlist_node *node_new,
  976. struct vlist_node *node_old)
  977. {
  978. struct bridge_state *bst = container_of(tree, struct bridge_state, dev.vlans);
  979. struct bridge_vlan *vlan_new = NULL, *vlan_old = NULL;
  980. if (!bst->has_vlans || !bst->active)
  981. goto out;
  982. if (node_old)
  983. vlan_old = container_of(node_old, struct bridge_vlan, node);
  984. if (node_new)
  985. vlan_new = container_of(node_new, struct bridge_vlan, node);
  986. if (node_new && node_old && bridge_vlan_equal(vlan_old, vlan_new)) {
  987. list_splice_init(&vlan_old->hotplug_ports, &vlan_new->hotplug_ports);
  988. goto out;
  989. }
  990. if (node_old)
  991. bridge_set_vlan_state(bst, vlan_old, false);
  992. if (node_old && node_new)
  993. list_splice_init(&vlan_old->hotplug_ports, &vlan_new->hotplug_ports);
  994. if (node_new)
  995. vlan_new->pending = true;
  996. bst->dev.config_pending = true;
  997. out:
  998. bridge_vlan_free(vlan_old);
  999. }
  1000. static void
  1001. bridge_dev_vlan_update(struct device *dev)
  1002. {
  1003. struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
  1004. struct bridge_vlan *vlan;
  1005. vlist_for_each_element(&dev->vlans, vlan, node) {
  1006. if (!vlan->pending)
  1007. continue;
  1008. vlan->pending = false;
  1009. bridge_set_vlan_state(bst, vlan, true);
  1010. }
  1011. }
  1012. static struct device *
  1013. bridge_create(const char *name, struct device_type *devtype,
  1014. struct blob_attr *attr)
  1015. {
  1016. struct bridge_state *bst;
  1017. struct device *dev = NULL;
  1018. bst = calloc(1, sizeof(*bst));
  1019. if (!bst)
  1020. return NULL;
  1021. dev = &bst->dev;
  1022. if (device_init(dev, devtype, name) < 0) {
  1023. device_cleanup(dev);
  1024. free(bst);
  1025. return NULL;
  1026. }
  1027. dev->config_pending = true;
  1028. bst->retry.cb = bridge_retry_members;
  1029. bst->set_state = dev->set_state;
  1030. dev->set_state = bridge_set_state;
  1031. dev->hotplug_ops = &bridge_ops;
  1032. vlist_init(&bst->members, avl_strcmp, bridge_member_update);
  1033. bst->members.keep_old = true;
  1034. vlist_init(&dev->vlans, bridge_avl_cmp_u16, bridge_vlan_update);
  1035. bridge_reload(dev, attr);
  1036. return dev;
  1037. }
  1038. static void __init bridge_device_type_init(void)
  1039. {
  1040. device_type_add(&bridge_device_type);
  1041. }