bridgeif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /**
  2. * @file
  3. * lwIP netif implementing an IEEE 802.1D MAC Bridge
  4. */
  5. /*
  6. * Copyright (c) 2017 Simon Goldschmidt.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  34. *
  35. */
  36. /**
  37. * @defgroup bridgeif IEEE 802.1D bridge
  38. * @ingroup netifs
  39. * This file implements an IEEE 802.1D bridge by using a multilayer netif approach
  40. * (one hardware-independent netif for the bridge that uses hardware netifs for its ports).
  41. * On transmit, the bridge selects the outgoing port(s).
  42. * On receive, the port netif calls into the bridge (via its netif->input function) and
  43. * the bridge selects the port(s) (and/or its netif->input function) to pass the received pbuf to.
  44. *
  45. * Usage:
  46. * - add the port netifs just like you would when using them as dedicated netif without a bridge
  47. * - only NETIF_FLAG_ETHARP/NETIF_FLAG_ETHERNET netifs are supported as bridge ports
  48. * - add the bridge port netifs without IPv4 addresses (i.e. pass 'NULL, NULL, NULL')
  49. * - don't add IPv6 addresses to the port netifs!
  50. * - set up the bridge configuration in a global variable of type 'bridgeif_initdata_t' that contains
  51. * - the MAC address of the bridge
  52. * - some configuration options controlling the memory consumption (maximum number of ports
  53. * and FDB entries)
  54. * - e.g. for a bridge MAC address 00-01-02-03-04-05, 2 bridge ports, 1024 FDB entries + 16 static MAC entries:
  55. * bridgeif_initdata_t mybridge_initdata = BRIDGEIF_INITDATA1(2, 1024, 16, ETH_ADDR(0, 1, 2, 3, 4, 5));
  56. * - add the bridge netif (with IPv4 config):
  57. * struct netif bridge_netif;
  58. * netif_add(&bridge_netif, &my_ip, &my_netmask, &my_gw, &mybridge_initdata, bridgeif_init, tcpip_input);
  59. * NOTE: the passed 'input' function depends on BRIDGEIF_PORT_NETIFS_OUTPUT_DIRECT setting,
  60. * which controls where the forwarding is done (netif low level input context vs. tcpip_thread)
  61. * - set up all ports netifs and the bridge netif
  62. *
  63. * - When adding a port netif, NETIF_FLAG_ETHARP flag will be removed from a port
  64. * to prevent ETHARP working on that port netif (we only want one IP per bridge not per port).
  65. * - When adding a port netif, its input function is changed to call into the bridge.
  66. *
  67. *
  68. * @todo:
  69. * - compact static FDB entries (instead of walking the whole array)
  70. * - add FDB query/read access
  71. * - add FDB change callback (when learning or dropping auto-learned entries)
  72. * - prefill FDB with MAC classes that should never be forwarded
  73. * - multicast snooping? (and only forward group addresses to interested ports)
  74. * - support removing ports
  75. * - check SNMP integration
  76. * - VLAN handling / trunk ports
  77. * - priority handling? (although that largely depends on TX queue limitations and lwIP doesn't provide tx-done handling)
  78. */
  79. #include "netif/bridgeif.h"
  80. #include "lwip/netif.h"
  81. #include "lwip/sys.h"
  82. #include "lwip/etharp.h"
  83. #include "lwip/ethip6.h"
  84. #include "lwip/snmp.h"
  85. #include "lwip/timeouts.h"
  86. #include <string.h>
  87. #if LWIP_NUM_NETIF_CLIENT_DATA
  88. /* Define those to better describe your network interface. */
  89. #define IFNAME0 'b'
  90. #define IFNAME1 'r'
  91. struct bridgeif_private_s;
  92. typedef struct bridgeif_port_private_s {
  93. struct bridgeif_private_s *bridge;
  94. struct netif *port_netif;
  95. u8_t port_num;
  96. } bridgeif_port_t;
  97. typedef struct bridgeif_fdb_static_entry_s {
  98. u8_t used;
  99. bridgeif_portmask_t dst_ports;
  100. struct eth_addr addr;
  101. } bridgeif_fdb_static_entry_t;
  102. typedef struct bridgeif_private_s {
  103. struct netif *netif;
  104. struct eth_addr ethaddr;
  105. u8_t max_ports;
  106. u8_t num_ports;
  107. bridgeif_port_t *ports;
  108. u16_t max_fdbs_entries;
  109. bridgeif_fdb_static_entry_t *fdbs;
  110. u16_t max_fdbd_entries;
  111. void *fdbd;
  112. } bridgeif_private_t;
  113. /* netif data index to get the bridge on input */
  114. u8_t bridgeif_netif_client_id = 0xff;
  115. /**
  116. * @ingroup bridgeif
  117. * Add a static entry to the forwarding database.
  118. * A static entry marks where frames to a specific eth address (unicast or group address) are
  119. * forwarded.
  120. * bits [0..(BRIDGEIF_MAX_PORTS-1)]: hw ports
  121. * bit [BRIDGEIF_MAX_PORTS]: cpu port
  122. * 0: drop
  123. */
  124. err_t
  125. bridgeif_fdb_add(struct netif *bridgeif, const struct eth_addr *addr, bridgeif_portmask_t ports)
  126. {
  127. int i;
  128. bridgeif_private_t *br;
  129. BRIDGEIF_DECL_PROTECT(lev);
  130. LWIP_ASSERT("invalid netif", bridgeif != NULL);
  131. br = (bridgeif_private_t *)bridgeif->state;
  132. LWIP_ASSERT("invalid state", br != NULL);
  133. BRIDGEIF_READ_PROTECT(lev);
  134. for (i = 0; i < br->max_fdbs_entries; i++) {
  135. if (!br->fdbs[i].used) {
  136. BRIDGEIF_WRITE_PROTECT(lev);
  137. if (!br->fdbs[i].used) {
  138. br->fdbs[i].used = 1;
  139. br->fdbs[i].dst_ports = ports;
  140. memcpy(&br->fdbs[i].addr, addr, sizeof(struct eth_addr));
  141. BRIDGEIF_WRITE_UNPROTECT(lev);
  142. BRIDGEIF_READ_UNPROTECT(lev);
  143. return ERR_OK;
  144. }
  145. BRIDGEIF_WRITE_UNPROTECT(lev);
  146. }
  147. }
  148. BRIDGEIF_READ_UNPROTECT(lev);
  149. return ERR_MEM;
  150. }
  151. /**
  152. * @ingroup bridgeif
  153. * Remove a static entry from the forwarding database
  154. */
  155. err_t
  156. bridgeif_fdb_remove(struct netif *bridgeif, const struct eth_addr *addr)
  157. {
  158. int i;
  159. bridgeif_private_t *br;
  160. BRIDGEIF_DECL_PROTECT(lev);
  161. LWIP_ASSERT("invalid netif", bridgeif != NULL);
  162. br = (bridgeif_private_t *)bridgeif->state;
  163. LWIP_ASSERT("invalid state", br != NULL);
  164. BRIDGEIF_READ_PROTECT(lev);
  165. for (i = 0; i < br->max_fdbs_entries; i++) {
  166. if (br->fdbs[i].used && !memcmp(&br->fdbs[i].addr, addr, sizeof(struct eth_addr))) {
  167. BRIDGEIF_WRITE_PROTECT(lev);
  168. if (br->fdbs[i].used && !memcmp(&br->fdbs[i].addr, addr, sizeof(struct eth_addr))) {
  169. memset(&br->fdbs[i], 0, sizeof(bridgeif_fdb_static_entry_t));
  170. BRIDGEIF_WRITE_UNPROTECT(lev);
  171. BRIDGEIF_READ_UNPROTECT(lev);
  172. return ERR_OK;
  173. }
  174. BRIDGEIF_WRITE_UNPROTECT(lev);
  175. }
  176. }
  177. BRIDGEIF_READ_UNPROTECT(lev);
  178. return ERR_VAL;
  179. }
  180. /** Get the forwarding port(s) (as bit mask) for the specified destination mac address */
  181. static bridgeif_portmask_t
  182. bridgeif_find_dst_ports(bridgeif_private_t *br, struct eth_addr *dst_addr)
  183. {
  184. int i;
  185. BRIDGEIF_DECL_PROTECT(lev);
  186. BRIDGEIF_READ_PROTECT(lev);
  187. /* first check for static entries */
  188. for (i = 0; i < br->max_fdbs_entries; i++) {
  189. if (br->fdbs[i].used) {
  190. if (!memcmp(&br->fdbs[i].addr, dst_addr, sizeof(struct eth_addr))) {
  191. bridgeif_portmask_t ret = br->fdbs[i].dst_ports;
  192. BRIDGEIF_READ_UNPROTECT(lev);
  193. return ret;
  194. }
  195. }
  196. }
  197. if (dst_addr->addr[0] & 1) {
  198. /* no match found: flood remaining group address */
  199. BRIDGEIF_READ_UNPROTECT(lev);
  200. return BR_FLOOD;
  201. }
  202. BRIDGEIF_READ_UNPROTECT(lev);
  203. /* no match found: check dynamic fdb for port or fall back to flooding */
  204. return bridgeif_fdb_get_dst_ports(br->fdbd, dst_addr);
  205. }
  206. /** Helper function to see if a destination mac belongs to the bridge
  207. * (bridge netif or one of the port netifs), in which case the frame
  208. * is sent to the cpu only.
  209. */
  210. static int
  211. bridgeif_is_local_mac(bridgeif_private_t *br, struct eth_addr *addr)
  212. {
  213. int i;
  214. BRIDGEIF_DECL_PROTECT(lev);
  215. if (!memcmp(br->netif->hwaddr, addr, sizeof(struct eth_addr))) {
  216. return 1;
  217. }
  218. BRIDGEIF_READ_PROTECT(lev);
  219. for (i = 0; i < br->num_ports; i++) {
  220. struct netif *portif = br->ports[i].port_netif;
  221. if (portif != NULL) {
  222. if (!memcmp(portif->hwaddr, addr, sizeof(struct eth_addr))) {
  223. BRIDGEIF_READ_UNPROTECT(lev);
  224. return 1;
  225. }
  226. }
  227. }
  228. BRIDGEIF_READ_UNPROTECT(lev);
  229. return 0;
  230. }
  231. /* Output helper function */
  232. static err_t
  233. bridgeif_send_to_port(bridgeif_private_t *br, struct pbuf *p, u8_t dstport_idx)
  234. {
  235. if (dstport_idx < BRIDGEIF_MAX_PORTS) {
  236. /* possibly an external port */
  237. if (dstport_idx < br->max_ports) {
  238. struct netif *portif = br->ports[dstport_idx].port_netif;
  239. if ((portif != NULL) && (portif->linkoutput != NULL)) {
  240. /* prevent sending out to rx port */
  241. if (netif_get_index(portif) != p->if_idx) {
  242. if (netif_is_link_up(portif)) {
  243. LWIP_DEBUGF(BRIDGEIF_FW_DEBUG, ("br -> flood(%p:%d) -> %d\n", (void *)p, p->if_idx, netif_get_index(portif)));
  244. return portif->linkoutput(portif, p);
  245. }
  246. }
  247. }
  248. }
  249. } else {
  250. LWIP_ASSERT("invalid port index", dstport_idx == BRIDGEIF_MAX_PORTS);
  251. }
  252. return ERR_OK;
  253. }
  254. /** Helper function to pass a pbuf to all ports marked in 'dstports'
  255. */
  256. static err_t
  257. bridgeif_send_to_ports(bridgeif_private_t *br, struct pbuf *p, bridgeif_portmask_t dstports)
  258. {
  259. err_t err, ret_err = ERR_OK;
  260. u8_t i;
  261. bridgeif_portmask_t mask = 1;
  262. BRIDGEIF_DECL_PROTECT(lev);
  263. BRIDGEIF_READ_PROTECT(lev);
  264. for (i = 0; i < BRIDGEIF_MAX_PORTS; i++, mask = (bridgeif_portmask_t)(mask << 1)) {
  265. if (dstports & mask) {
  266. err = bridgeif_send_to_port(br, p, i);
  267. if (err != ERR_OK) {
  268. ret_err = err;
  269. }
  270. }
  271. }
  272. BRIDGEIF_READ_UNPROTECT(lev);
  273. return ret_err;
  274. }
  275. /** Output function of the application port of the bridge (the one with an ip address).
  276. * The forwarding port(s) where this pbuf is sent on is/are automatically selected
  277. * from the FDB.
  278. */
  279. static err_t
  280. bridgeif_output(struct netif *netif, struct pbuf *p)
  281. {
  282. err_t err;
  283. bridgeif_private_t *br = (bridgeif_private_t *)netif->state;
  284. struct eth_addr *dst = (struct eth_addr *)(p->payload);
  285. bridgeif_portmask_t dstports = bridgeif_find_dst_ports(br, dst);
  286. err = bridgeif_send_to_ports(br, p, dstports);
  287. MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
  288. if (((u8_t *)p->payload)[0] & 1) {
  289. /* broadcast or multicast packet*/
  290. MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
  291. } else {
  292. /* unicast packet */
  293. MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
  294. }
  295. /* increase ifoutdiscards or ifouterrors on error */
  296. LINK_STATS_INC(link.xmit);
  297. return err;
  298. }
  299. /** The actual bridge input function. Port netif's input is changed to call
  300. * here. This function decides where the frame is forwarded.
  301. */
  302. static err_t
  303. bridgeif_input(struct pbuf *p, struct netif *netif)
  304. {
  305. u8_t rx_idx;
  306. bridgeif_portmask_t dstports;
  307. struct eth_addr *src, *dst;
  308. bridgeif_private_t *br;
  309. bridgeif_port_t *port;
  310. if (p == NULL || netif == NULL) {
  311. return ERR_VAL;
  312. }
  313. port = (bridgeif_port_t *)netif_get_client_data(netif, bridgeif_netif_client_id);
  314. LWIP_ASSERT("port data not set", port != NULL);
  315. if (port == NULL || port->bridge == NULL) {
  316. return ERR_VAL;
  317. }
  318. br = (bridgeif_private_t *)port->bridge;
  319. rx_idx = netif_get_index(netif);
  320. /* store receive index in pbuf */
  321. p->if_idx = rx_idx;
  322. dst = (struct eth_addr *)p->payload;
  323. src = (struct eth_addr *)(((u8_t *)p->payload) + sizeof(struct eth_addr));
  324. if ((src->addr[0] & 1) == 0) {
  325. /* update src for all non-group addresses */
  326. bridgeif_fdb_update_src(br->fdbd, src, port->port_num);
  327. }
  328. if (dst->addr[0] & 1) {
  329. /* group address -> flood + cpu? */
  330. dstports = bridgeif_find_dst_ports(br, dst);
  331. bridgeif_send_to_ports(br, p, dstports);
  332. if (dstports & (1 << BRIDGEIF_MAX_PORTS)) {
  333. /* we pass the reference to ->input or have to free it */
  334. LWIP_DEBUGF(BRIDGEIF_FW_DEBUG, ("br -> input(%p)\n", (void *)p));
  335. if (br->netif->input(p, br->netif) != ERR_OK) {
  336. pbuf_free(p);
  337. }
  338. } else {
  339. /* all references done */
  340. pbuf_free(p);
  341. }
  342. /* always return ERR_OK here to prevent the caller freeing the pbuf */
  343. return ERR_OK;
  344. } else {
  345. /* is this for one of the local ports? */
  346. if (bridgeif_is_local_mac(br, dst)) {
  347. /* yes, send to cpu port only */
  348. LWIP_DEBUGF(BRIDGEIF_FW_DEBUG, ("br -> input(%p)\n", (void *)p));
  349. return br->netif->input(p, br->netif);
  350. }
  351. /* get dst port */
  352. dstports = bridgeif_find_dst_ports(br, dst);
  353. bridgeif_send_to_ports(br, p, dstports);
  354. /* no need to send to cpu, flooding is for external ports only */
  355. /* by this, we consumed the pbuf */
  356. pbuf_free(p);
  357. /* always return ERR_OK here to prevent the caller freeing the pbuf */
  358. return ERR_OK;
  359. }
  360. }
  361. #if !BRIDGEIF_PORT_NETIFS_OUTPUT_DIRECT
  362. /** Input function for port netifs used to synchronize into tcpip_thread.
  363. */
  364. static err_t
  365. bridgeif_tcpip_input(struct pbuf *p, struct netif *netif)
  366. {
  367. return tcpip_inpkt(p, netif, bridgeif_input);
  368. }
  369. #endif /* BRIDGEIF_PORT_NETIFS_OUTPUT_DIRECT */
  370. /**
  371. * @ingroup bridgeif
  372. * Initialization function passed to netif_add().
  373. *
  374. * ATTENTION: A pointer to a @ref bridgeif_initdata_t must be passed as 'state'
  375. * to @ref netif_add when adding the bridge. I supplies MAC address
  376. * and controls memory allocation (number of ports, FDB size).
  377. *
  378. * @param netif the lwip network interface structure for this ethernetif
  379. * @return ERR_OK if the loopif is initialized
  380. * ERR_MEM if private data couldn't be allocated
  381. * any other err_t on error
  382. */
  383. err_t
  384. bridgeif_init(struct netif *netif)
  385. {
  386. bridgeif_initdata_t *init_data;
  387. bridgeif_private_t *br;
  388. size_t alloc_len_sizet;
  389. mem_size_t alloc_len;
  390. LWIP_ASSERT("netif != NULL", (netif != NULL));
  391. LWIP_ASSERT("bridgeif needs an input callback", (netif->input != NULL));
  392. #if !BRIDGEIF_PORT_NETIFS_OUTPUT_DIRECT
  393. if (netif->input == tcpip_input) {
  394. LWIP_DEBUGF(BRIDGEIF_DEBUG | LWIP_DBG_ON, ("bridgeif does not need tcpip_input, use netif_input/ethernet_input instead"));
  395. }
  396. #endif
  397. if (bridgeif_netif_client_id == 0xFF) {
  398. bridgeif_netif_client_id = netif_alloc_client_data_id();
  399. }
  400. init_data = (bridgeif_initdata_t *)netif->state;
  401. LWIP_ASSERT("init_data != NULL", (init_data != NULL));
  402. LWIP_ASSERT("init_data->max_ports <= BRIDGEIF_MAX_PORTS",
  403. init_data->max_ports <= BRIDGEIF_MAX_PORTS);
  404. alloc_len_sizet = sizeof(bridgeif_private_t) + (init_data->max_ports * sizeof(bridgeif_port_t) + (init_data->max_fdb_static_entries * sizeof(bridgeif_fdb_static_entry_t)));
  405. alloc_len = (mem_size_t)alloc_len_sizet;
  406. LWIP_ASSERT("alloc_len == alloc_len_sizet", alloc_len == alloc_len_sizet);
  407. LWIP_DEBUGF(BRIDGEIF_DEBUG, ("bridgeif_init: allocating %d bytes for private data\n", (int)alloc_len));
  408. br = (bridgeif_private_t *)mem_calloc(1, alloc_len);
  409. if (br == NULL) {
  410. LWIP_DEBUGF(NETIF_DEBUG, ("bridgeif_init: out of memory\n"));
  411. return ERR_MEM;
  412. }
  413. memcpy(&br->ethaddr, &init_data->ethaddr, sizeof(br->ethaddr));
  414. br->netif = netif;
  415. br->max_ports = init_data->max_ports;
  416. br->ports = (bridgeif_port_t *)(br + 1);
  417. br->max_fdbs_entries = init_data->max_fdb_static_entries;
  418. br->fdbs = (bridgeif_fdb_static_entry_t *)(((u8_t *)(br + 1)) + (init_data->max_ports * sizeof(bridgeif_port_t)));
  419. br->max_fdbd_entries = init_data->max_fdb_dynamic_entries;
  420. br->fdbd = bridgeif_fdb_init(init_data->max_fdb_dynamic_entries);
  421. if (br->fdbd == NULL) {
  422. LWIP_DEBUGF(NETIF_DEBUG, ("bridgeif_init: out of memory in fdb_init\n"));
  423. mem_free(br);
  424. return ERR_MEM;
  425. }
  426. #if LWIP_NETIF_HOSTNAME
  427. /* Initialize interface hostname */
  428. netif->hostname = "lwip";
  429. #endif /* LWIP_NETIF_HOSTNAME */
  430. /*
  431. * Initialize the snmp variables and counters inside the struct netif.
  432. * The last argument should be replaced with your link speed, in units
  433. * of bits per second.
  434. */
  435. MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 0);
  436. netif->state = br;
  437. netif->name[0] = IFNAME0;
  438. netif->name[1] = IFNAME1;
  439. /* We directly use etharp_output() here to save a function call.
  440. * You can instead declare your own function an call etharp_output()
  441. * from it if you have to do some checks before sending (e.g. if link
  442. * is available...) */
  443. #if LWIP_IPV4
  444. netif->output = etharp_output;
  445. #endif /* LWIP_IPV4 */
  446. #if LWIP_IPV6
  447. netif->output_ip6 = ethip6_output;
  448. #endif /* LWIP_IPV6 */
  449. netif->linkoutput = bridgeif_output;
  450. /* set MAC hardware address length */
  451. netif->hwaddr_len = ETH_HWADDR_LEN;
  452. /* set MAC hardware address */
  453. memcpy(netif->hwaddr, &br->ethaddr, ETH_HWADDR_LEN);
  454. /* maximum transfer unit */
  455. netif->mtu = 1500;
  456. /* device capabilities */
  457. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  458. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_LINK_UP;
  459. #if LWIP_IPV6 && LWIP_IPV6_MLD
  460. /*
  461. * For hardware/netifs that implement MAC filtering.
  462. * All-nodes link-local is handled by default, so we must let the hardware know
  463. * to allow multicast packets in.
  464. * Should set mld_mac_filter previously. */
  465. if (netif->mld_mac_filter != NULL) {
  466. ip6_addr_t ip6_allnodes_ll;
  467. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  468. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  469. }
  470. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  471. return ERR_OK;
  472. }
  473. /**
  474. * @ingroup bridgeif
  475. * Add a port to the bridge
  476. */
  477. err_t
  478. bridgeif_add_port(struct netif *bridgeif, struct netif *portif)
  479. {
  480. bridgeif_private_t *br;
  481. bridgeif_port_t *port;
  482. LWIP_ASSERT("bridgeif != NULL", bridgeif != NULL);
  483. LWIP_ASSERT("bridgeif->state != NULL", bridgeif->state != NULL);
  484. LWIP_ASSERT("portif != NULL", portif != NULL);
  485. if (!(portif->flags & NETIF_FLAG_ETHARP) || !(portif->flags & NETIF_FLAG_ETHERNET)) {
  486. /* can only add ETHERNET/ETHARP interfaces */
  487. return ERR_VAL;
  488. }
  489. br = (bridgeif_private_t *)bridgeif->state;
  490. if (br->num_ports >= br->max_ports) {
  491. return ERR_VAL;
  492. }
  493. port = &br->ports[br->num_ports];
  494. port->port_netif = portif;
  495. port->port_num = br->num_ports;
  496. port->bridge = br;
  497. br->num_ports++;
  498. /* let the port call us on input */
  499. #if BRIDGEIF_PORT_NETIFS_OUTPUT_DIRECT
  500. portif->input = bridgeif_input;
  501. #else
  502. portif->input = bridgeif_tcpip_input;
  503. #endif
  504. /* store pointer to bridge in netif */
  505. netif_set_client_data(portif, bridgeif_netif_client_id, port);
  506. /* remove ETHARP flag to prevent sending report events on netif-up */
  507. netif_clear_flags(portif, NETIF_FLAG_ETHARP);
  508. return ERR_OK;
  509. }
  510. #endif /* LWIP_NUM_NETIF_CLIENT_DATA */