1 /**
2 * @file
3 * lwIP netif implementing an FDB for IEEE 802.1D MAC Bridge
4 */
5
6 /*
7 * Copyright (c) 2017 Simon Goldschmidt.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Simon Goldschmidt <goldsimon@gmx.de>
35 *
36 */
37
38 /**
39 * @defgroup bridgeif_fdb FDB example code
40 * @ingroup bridgeif
41 * This file implements an example for an FDB (Forwarding DataBase)
42 */
43
44 #include "netif/bridgeif.h"
45 #include "lwip/sys.h"
46 #include "lwip/mem.h"
47 #include "lwip/timeouts.h"
48 #include <string.h>
49
50 #define BRIDGEIF_AGE_TIMER_MS 1000
51
52 #define BR_FDB_TIMEOUT_SEC (60*5) /* 5 minutes FDB timeout */
53
54 typedef struct bridgeif_dfdb_entry_s {
55 u8_t used;
56 u8_t port;
57 u32_t ts;
58 struct eth_addr addr;
59 } bridgeif_dfdb_entry_t;
60
61 typedef struct bridgeif_dfdb_s {
62 u16_t max_fdb_entries;
63 bridgeif_dfdb_entry_t *fdb;
64 } bridgeif_dfdb_t;
65
66 /**
67 * @ingroup bridgeif_fdb
68 * A real simple and slow implementation of an auto-learning forwarding database that
69 * remembers known src mac addresses to know which port to send frames destined for that
70 * mac address.
71 *
72 * ATTENTION: This is meant as an example only, in real-world use, you should
73 * provide a better implementation :-)
74 */
75 void
bridgeif_fdb_update_src(void * fdb_ptr,struct eth_addr * src_addr,u8_t port_idx)76 bridgeif_fdb_update_src(void *fdb_ptr, struct eth_addr *src_addr, u8_t port_idx)
77 {
78 int i;
79 bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)fdb_ptr;
80 BRIDGEIF_DECL_PROTECT(lev);
81 BRIDGEIF_READ_PROTECT(lev);
82 for (i = 0; i < fdb->max_fdb_entries; i++) {
83 bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
84 if (e->used && e->ts) {
85 if (!memcmp(&e->addr, src_addr, sizeof(struct eth_addr))) {
86 LWIP_DEBUGF(BRIDGEIF_FDB_DEBUG, ("br: update src %02x:%02x:%02x:%02x:%02x:%02x (from %d) @ idx %d\n",
87 src_addr->addr[0], src_addr->addr[1], src_addr->addr[2], src_addr->addr[3], src_addr->addr[4], src_addr->addr[5],
88 port_idx, i));
89 BRIDGEIF_WRITE_PROTECT(lev);
90 e->ts = BR_FDB_TIMEOUT_SEC;
91 e->port = port_idx;
92 BRIDGEIF_WRITE_UNPROTECT(lev);
93 BRIDGEIF_READ_UNPROTECT(lev);
94 return;
95 }
96 }
97 }
98 /* not found, allocate new entry from free */
99 for (i = 0; i < fdb->max_fdb_entries; i++) {
100 bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
101 if (!e->used || !e->ts) {
102 BRIDGEIF_WRITE_PROTECT(lev);
103 /* check again when protected */
104 if (!e->used || !e->ts) {
105 LWIP_DEBUGF(BRIDGEIF_FDB_DEBUG, ("br: create src %02x:%02x:%02x:%02x:%02x:%02x (from %d) @ idx %d\n",
106 src_addr->addr[0], src_addr->addr[1], src_addr->addr[2], src_addr->addr[3], src_addr->addr[4], src_addr->addr[5],
107 port_idx, i));
108 memcpy(&e->addr, src_addr, sizeof(struct eth_addr));
109 e->ts = BR_FDB_TIMEOUT_SEC;
110 e->port = port_idx;
111 e->used = 1;
112 BRIDGEIF_WRITE_UNPROTECT(lev);
113 BRIDGEIF_READ_UNPROTECT(lev);
114 return;
115 }
116 BRIDGEIF_WRITE_UNPROTECT(lev);
117 }
118 }
119 BRIDGEIF_READ_UNPROTECT(lev);
120 /* not found, no free entry -> flood */
121 }
122
123 /**
124 * @ingroup bridgeif_fdb
125 * Walk our list of auto-learnt fdb entries and return a port to forward or BR_FLOOD if unknown
126 */
127 bridgeif_portmask_t
bridgeif_fdb_get_dst_ports(void * fdb_ptr,struct eth_addr * dst_addr)128 bridgeif_fdb_get_dst_ports(void *fdb_ptr, struct eth_addr *dst_addr)
129 {
130 int i;
131 bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)fdb_ptr;
132 BRIDGEIF_DECL_PROTECT(lev);
133 BRIDGEIF_READ_PROTECT(lev);
134 for (i = 0; i < fdb->max_fdb_entries; i++) {
135 bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
136 if (e->used && e->ts) {
137 if (!memcmp(&e->addr, dst_addr, sizeof(struct eth_addr))) {
138 bridgeif_portmask_t ret = (bridgeif_portmask_t)(1 << e->port);
139 BRIDGEIF_READ_UNPROTECT(lev);
140 return ret;
141 }
142 }
143 }
144 BRIDGEIF_READ_UNPROTECT(lev);
145 return BR_FLOOD;
146 }
147
148 /**
149 * @ingroup bridgeif_fdb
150 * Aging implementation of our simple fdb
151 */
152 static void
bridgeif_fdb_age_one_second(void * fdb_ptr)153 bridgeif_fdb_age_one_second(void *fdb_ptr)
154 {
155 int i;
156 bridgeif_dfdb_t *fdb;
157 BRIDGEIF_DECL_PROTECT(lev);
158
159 fdb = (bridgeif_dfdb_t *)fdb_ptr;
160 BRIDGEIF_READ_PROTECT(lev);
161
162 for (i = 0; i < fdb->max_fdb_entries; i++) {
163 bridgeif_dfdb_entry_t *e = &fdb->fdb[i];
164 if (e->used && e->ts) {
165 BRIDGEIF_WRITE_PROTECT(lev);
166 /* check again when protected */
167 if (e->used && e->ts) {
168 if (--e->ts == 0) {
169 e->used = 0;
170 }
171 }
172 BRIDGEIF_WRITE_UNPROTECT(lev);
173 }
174 }
175 BRIDGEIF_READ_UNPROTECT(lev);
176 }
177
178 /** Timer callback for fdb aging, called once per second */
179 static void
bridgeif_age_tmr(void * arg)180 bridgeif_age_tmr(void *arg)
181 {
182 bridgeif_dfdb_t *fdb = (bridgeif_dfdb_t *)arg;
183
184 LWIP_ASSERT("invalid arg", arg != NULL);
185
186 bridgeif_fdb_age_one_second(fdb);
187 sys_timeout(BRIDGEIF_AGE_TIMER_MS, bridgeif_age_tmr, arg);
188 }
189
190 /**
191 * @ingroup bridgeif_fdb
192 * Init our simple fdb list
193 */
194 void *
bridgeif_fdb_init(u16_t max_fdb_entries)195 bridgeif_fdb_init(u16_t max_fdb_entries)
196 {
197 bridgeif_dfdb_t *fdb;
198 size_t alloc_len_sizet = sizeof(bridgeif_dfdb_t) + (max_fdb_entries * sizeof(bridgeif_dfdb_entry_t));
199 mem_size_t alloc_len = (mem_size_t)alloc_len_sizet;
200 LWIP_ASSERT("alloc_len == alloc_len_sizet", alloc_len == alloc_len_sizet);
201 LWIP_DEBUGF(BRIDGEIF_DEBUG, ("bridgeif_fdb_init: allocating %d bytes for private FDB data\n", (int)alloc_len));
202 fdb = (bridgeif_dfdb_t *)mem_calloc(1, alloc_len);
203 if (fdb == NULL) {
204 return NULL;
205 }
206 fdb->max_fdb_entries = max_fdb_entries;
207 fdb->fdb = (bridgeif_dfdb_entry_t *)(fdb + 1);
208
209 sys_timeout(BRIDGEIF_AGE_TIMER_MS, bridgeif_age_tmr, fdb);
210
211 return fdb;
212 }
213