Trying to think about this from a hardware design perspective, if I had to design an FPGA or a uC or something to manage a single port being the gateway port and one or more switched ports for routing, with the guarantee that one port == one neighbor and thus one MAC address to route, then it'd be as simple as storing a single MAC address for each port in the firmware's memory and switching/dropping packets based on which MACs I know of.
However, how does it work when there is an arbitrary number of 'neighbors' downstream from a port, i.e. when a port is connected to another layer 2 switch? Would I theoretically have to keep a whole list of known neighbors at that port? That means some arbitrary limit of downstream nodes I could keep track of. Since MACs are not prefixed-based (like IP addresses are), doing the typical CIDR masking wouldn't be sufficient.
Further, if it was just a catch-all "forward packets destined for any unknown MAC to the next hop", then it'd mean potentially flooding all ports with any unknown packets, wouldn't it?
How does this work in reality? Is there something obvious I'm missing with how layer 2 switching works in such a topology?