使用Raspberry Pi和Python的家庭网络-网桥

使用Raspberry Pi和Python的家庭网络-网桥,python,networking,raspbian,scapy,forwarding,Python,Networking,Raspbian,Scapy,Forwarding,基本上,我希望能够通过运行我将要编写的无线python桥接软件的Raspberry Pi来增加我家庭网络的范围 设置如下所示: Internet | | 192.168.0.4 PC1 |

基本上,我希望能够通过运行我将要编写的无线python桥接软件的Raspberry Pi来增加我家庭网络的范围

设置如下所示:

                Internet
                    |
                    |                                                           192.168.0.4
PC1                 |                                                             Laptop1                                                  
   |---------------Switch(Gateway)---------------Pi1-------Pi2---------Pi3------------|
PC2                192.168.0.1              192.168.0.2           192.168.0.3     Laptop2                  
                                                                                192.168.0.5
这意味着笔记本电脑1和笔记本电脑2都可以连接到互联网,就像它们连接到交换机一样,就像两台电脑一样(ping google等也可以)

出于对python网络的兴趣和实践,我想自己编写在Pi上运行的桥接代码

由于所有Pi之间的hostapd和特别连接,我目前在Pi3上设置了面向笔记本电脑的接口作为接入点。Pi1的一个接口连接到交换机,就像您通常连接到AP一样

每个Pi有2个wlan接口(wlan0和wlan1)

我一直在使用嗅探从笔记本电脑发送的数据包,然后在修改目标和源MAC地址后使用sendp转发

使用wireshark,我可以看到,如果我从一台笔记本电脑发送一个scapy构造的数据包,它确实会在面向交换机的Pi1接口上发送出去(交换机应接收到so,从而更新其表,即目标IP地址为192.168.0.2或192.168.0.3的任何数据包必须发送到面向交换机的Pi1上接口的MAC地址),但是笔记本电脑无法建立Internet连接

我想知道这是否是因为scapy没有真正重定向/操纵原始数据包,只是创建了一个副本供您使用,因此无法满足我的需求

我从其他帖子上读到,也许这是正确的方法?但也读到他们不再受支持了

同时,我正在静态设置Pi和笔记本电脑的IP地址,并且仅在IPv4数据包上转发(Pi3 AP没有任何DHCP/NAT/DNS服务器运行,只允许笔记本电脑连接到Pi)

这是我第一次尝试这样一个项目,所以问题可能非常简单,所以我可能没有注意到,或者我的理解可能根本不正确。任何帮助或建议都将不胜感激,因为这让我发疯。(代码需要一些重大的重构,但希望先让它工作起来)

谢谢你看

        if packet[Ether].src in interfaceMACs:#If packet sent from 1 of the interfaces ignore
            print "Drop from bridge"
            return

        if packet[Ether].dst in inTable:#If the destination is on the same segment as the recieving interface ignore
            print "Drop same segment"
            return

        if packet[Ether].dst == 'ff:ff:ff:ff:ff:ff':#Ignore broadcasts #TODO: need to support broadcasts
            print "Drop broadcast"
            return




        if IP in packet:
            if packet[IP].dst == myIP:  # When packet is destined for this interfaces
                print "reached destination - this interface"
                return

            if packet[IP].dst == otherIP:  # When packet destined for far side interface
                print "Reached destination - Other interface"
                return

            newDstMac = searchTable(outTable, packet[IP].dst)
            if newDstMac is None:#If final destination is not on this bridges out interface
                if len(outTable) == 1:#Resolve which host is the bridge
                    packet[Ether].dst = outTable.keys()[0]#Send to the only device connected to this interface (Another bridge)
                else:#If more than 1 destination host, would be the gateway and multiple hosts, so find the gateway and send packet to it.
                    gatewayMAC = searchTable(outTable, "192.168.0.1")
                    if gatewayMAC is None:#No gateway, problem
                        print "ERROR!!!!!!!!!!! NO GATEWAY"
                    else:
                        packet[Ether].dst = gatewayMAC



            else:#means the final destination is on this interface
                packet[Ether].dst = newDstMac#Then set the destination MAC to the final destination

            packet[Ether].src = self.outMAC#Set source as the interface sending the packet
            sendPacket(outInterface, packet)
            self.packetCount += 1


        print self.packetCount

欢迎使用Stack Overflow。请阅读有关如何创建.Cheers的内容,编辑后只保留处理数据包的逻辑,因为我可以很好地发送和接收数据包(但前提是我使用scapy在笔记本电脑上构建数据包,然后发送)。因此,不确定问题是代码还是我的方法的根本问题。不,物理桥接与软件桥接不一样!你能扩展一下吗?你是说我需要制作物理桥接才能使这种类型的设置正常工作?谢谢