This goes about knowing the learning table of the linux bridge (where the well known as "br-lan" is for sure the most popular case). The objective is then to find out on which bridge's port each MAC address from the outside is detected.
Installing the "bridge" package this information appears with the following command:
root@OpenWrt:~# brctl showmacs br-lan
port no mac addr is local? ageing timer
1 2c:db:07:54:47:7d no 29.21
1 44:d4:54:a0:0c:f1 no 7.96
1 60:32:b1:fd:80:37 yes 0.00
1 60:32:b1:fd:80:37 yes 0.00
3 62:32:b1:fd:80:39 yes 0.00
3 62:32:b1:fd:80:39 yes 0.00
2 62:32:b1:fd:80:40 yes 0.00
2 62:32:b1:fd:80:40 yes 0.00
4 62:32:b1:fd:80:41 yes 0.00
4 62:32:b1:fd:80:41 yes 0.00
5 68:ff:7b:3b:53:90 no 8.00
5 6a:ff:7b:3b:53:8e no 31.42
1 d4:3d:7e:bc:ec:cf no 0.40
There is another better way to obtain this information? I mean, a more appropiate inter process communication. For example, if this was available via an ubus object the usage could be more efficient and also it will guarantee a parsing schema (json in that case).
As a software will need this, if I go with the "cli" approach I will need to parse the human readable text as it is, with a risk of a future update where this syntax could be changed and all would stop working.
You can parse the binary file /sys/class/net/br-lan/brforward. It is a packed series of struct __fdb_entry structures.
Each record is 16 bytes. Bytes 0-5 are the mac address, byte 6 is the bridge port number, byte 7 designates the local state, bytes 8-11 the timer value and byte 12 the high port number index in case the value exceeds 255. The remaining three bytes are padding/reserved.
A crude implementation in shell may look like the code below:
Thank you very much. I am exploring these files but I do not find where I can obtain the pairing between the port number and the "ifname" of each interface.