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

时间:2017-03-10 11:04:43

标签: python networking raspbian scapy forwarding

基本上我希望能够通过运行一些我将要编写的无线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都可以连接到互联网,就好像它们连接到交换机一样,如2台电脑(所以可以ping谷歌等)。

我想根据自己的兴趣和python网络练习编写我自己在Pi上运行的桥接代码。

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

每个Pi都有2个wlan接口(wlan0& wlan1)。

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

使用wireshark我可以看到,如果我从其中一台笔记本电脑发送一个scapy构造的数据包,它会在面向交换机的Pi1接口上发送出去(因此应该由交换机接收,因此更新其表以说明任何数据包目的IP地址为192.168.0.2或192.168.0.3必须发送到Pi1上面向Switch的接口的MAC地址,但是笔记本电脑不能建立Internet连接。

我想知道这是不是因为scapy并没有真正重定向/操纵原始数据包而只是创建了一个副本供你玩,所以不适用于我想要的东西?

我从其他帖子中读到,或许Divert Sockets可能是正确的做法?但是还读过它们不再受支持了吗?

另外,我正在静态设置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

0 个答案:

没有答案
相关问题