Code: Select all
#!/bin/bash
NAM=`basename "$0"`
S="sudo"
HOSTAPD="hostapd"
WLAN="wlan0"
ENI="/etc/network/interfaces"
fcp_usage ()
{
cat<<EOF
${NAM}: [ --help | -h ]
EOF
}
fcp_q ()
{
$S systemctl show "$HOSTAPD" --property ActiveState
brctl show
}
fcp_1 ()
{
$S -i ifdown "$WLAN"
$S ln -sf /usr/local/sd/etc/interfaces.ap "$ENI"
$S systemctl enable "$HOSTAPD"
$S systemctl start "$HOSTAPD"
$S -i ifup "$WLAN"
}
fcp_0 ()
{
$S -i ifdown "$WLAN"
$S ln -sf /usr/local/sd/etc/interfaces.lt "$ENI"
$S systemctl stop "$HOSTAPD"
$S systemctl disable "$HOSTAPD"
$S -i ifup "$WLAN"
}
case "$1" in
--help | -h)
fcp_usage
exit 0
;;
--query | -q)
fcp_q
;;
start | 1)
fcp_1
;;
stop | 0)
fcp_0
;;
*)
fcp_usage
exit 1
;;
esac
Code: Select all
[email protected]:/usr/local $ cat /usr/local/sd/etc/interfaces.ap | egrep -v "^(#|$)"
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
iface wlan0 inet manual
auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0
Code: Select all
[email protected]:/usr/local $ cat /usr/local/sd/etc/interfaces.lt | egrep -v "^(#|$)"
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
auto br0
iface br0 inet dhcp
bridge_ports eth0
Code: Select all
[email protected]:/usr/local $ brctl show && netstat -anr
bridge name bridge id STP enabled interfaces
br0 8000.dca63207642a no eth0
wlan0
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 br0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.192 U 0 0 0 br0
192.168.1.0 0.0.0.0 255.255.255.192 U 0 0 0 eth0
Code: Select all
[email protected]:/usr/local $ cat /usr/local/sd/etc/svr-fw.dat
# Generated by iptables-save v1.6.0 on Wed Mar 21 22:57:43 2018
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [88:11004]
:FWF - [0:0]
:FWI - [0:0]
:FWO - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -i br0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 4000 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i br0 -p icmp -m icmp --icmp-type 8 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 8 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i wlan0 -p icmp -m icmp --icmp-type 8 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wlan0 -o eth0 -j ACCEPT
COMMIT
# Completed on Wed Mar 21 22:57:43 2018
# Generated by iptables-save v1.6.0 on Wed Mar 21 22:57:43 2018
*mangle
:PREROUTING ACCEPT [1603:319675]
:INPUT ACCEPT [1357:227372]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1053:87636]
:POSTROUTING ACCEPT [1053:87636]
COMMIT
# Completed on Wed Mar 21 22:57:43 2018
# Generated by iptables-save v1.6.0 on Wed Mar 21 22:57:43 2018
*nat
:PREROUTING ACCEPT [517:142771]
:INPUT ACCEPT [1:60]
:OUTPUT ACCEPT [234:15931]
:POSTROUTING ACCEPT [8:527]
-A POSTROUTING -o eth0 -j MASQUERADE
-A POSTROUTING -o br0 -j MASQUERADE
COMMIT
# Completed on Wed Mar 21 22:57:43 2018
Code: Select all
[email protected]:/usr/local $ ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.13 netmask 255.255.255.192 broadcast 192.168.1.63
inet6 fe80::dea6:32ff:fe07:642a prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:07:64:2a txqueuelen 1000 (Ethernet)
RX packets 4927 bytes 676835 (660.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3496 bytes 534988 (522.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.13 netmask 255.255.255.192 broadcast 192.168.1.63
ether dc:a6:32:07:64:2a txqueuelen 1000 (Ethernet)
RX packets 5075 bytes 776363 (758.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3933 bytes 630014 (615.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 287 bytes 24169 (23.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 287 bytes 24169 (23.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether dc:a6:32:07:64:2b txqueuelen 1000 (Ethernet)
RX packets 121 bytes 12122 (11.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2392 bytes 592942 (579.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
As far as I know It is not possible to bridge a WireGuard interface.TheEngineer2019 wrote: ↑Thu Sep 26, 2019 2:50 pmhey swampdog,
thanks for your reply,
your instructions seems a bit confusing in regards to applying it to my local instance,
i already set up my wlan0 interface to act as an access point and that is working fine.
Let me explain better what is the current scenario,
i have 2 wireless ethernet on my raspberry pi Zero, one the built in, and one a usb dongle,
wlan0 is the AP where i set it to be able to transmit wireless and clients connect to it,
wlan1 is configured as a wireless client to connect to my phone's hotspot.
wireguard software is installed to connect to the internet from wlan1 all the way to reach my wireguard server sitting somewhere else.
i also applied all the crazy iprouting rules between the wireguard and wlan0 (2 way communication), which works fine.
when connecting my laptop to wlan0, i am able to ping and reach only the wlan0 ip addresses, but not the remote internal LAN subnet where the wireguard server is.
the way that i am doing it now is creating an ip route rule on my laptop to redirect all my internal subnet to the wlan0 interface.
This works fine, but as i said i don't want other client have to add a rule on their laptop, just connect to the AP and be able to connect to my internal lan subnet, as easy as that. This is why i was thinking of the tool bridge-utils so i can bridge the wg0 with wlan0 because that is the only way you can reach your internet network, but it doesnt seem the tools like a virtual interface.
If you can think of a way on how to bridge the wlan0 with wg0 so makes it easier, help would be really appreciated.
Thanks,
Why doesn't packet forwarding from one subnet to the other work?TheEngineer2019 wrote: ↑Thu Sep 26, 2019 6:58 pmhi ejolson,
yes, this was the original conversation that it is not possible to achieve with wireguard,
but i am still doing my search hopefully i can come up with something someone else might have tried a similar situation and worked.
But anyone feel free to share their thoughts about it, maybe we can come up with a solution, which might be helpful for everyone who thinks this is helpful for them.
The routing will only work if the wlan0, wg0 and the remote network are each on different subnets.TheEngineer2019 wrote: ↑Thu Sep 26, 2019 7:30 pmhi ejolson,
this is what i did already by routing traffic between them.
i tried all these below:
sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE
sudo iptables -A FORWARD -i wg0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o wg0 -j ACCEPT
all works only if you create a static route on your pc, but if not, then it won't
and yes, to answer your question, the pc or laptop does have dual interface (ethernets), each one is connected to a different network, but when you try to reach an ip address that is not picked up by the dhcp then the pc won't know how to route those packets, which interface. So you need to tell the static route on pc if it tries to reach that destination where to find that network.
I was thinking of having the dhcp provide an ip from the local network this way no routing is required, but i don't think that works too.
Code: Select all
[email protected] ~ $ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
[email protected] ~ $ cat /usr/local/sd/etc/svr-fw.dat | grep eth
-A POSTROUTING -o eth0 -j MASQUERADE
-A INPUT -i eth1 -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth1 -o eth0 -j ACCEPT
[email protected] ~ $ netstat -anr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 81.109.74.1 0.0.0.0 UG 0 0 0 eth0
81.109.74.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.192 U 0 0 0 eth1
Code: Select all
[email protected] ~ $ cat /usr/local/sd/etc/svr-fw.dat | grep eth | sed -e 's/eth0/wlan1/' -e 's/eth1/wlan0/'
-A POSTROUTING -o wlan1 -j MASQUERADE
-A INPUT -i wlan0 -j ACCEPT
-A INPUT -i wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wlan0 -o wlan1 -j ACCEPT
No, you can not bridge a WireGuard network with a physical one. First they are at different levels in the Linux network stack; second bridging could have security implications regarding the cryptographic authentication of packets. Since the Zero is acting as the access point, then it is already the default route for all the computers on wireless. All you need is the correct routing and forwarding rules on the Pi Zero.swampdog wrote: ↑Fri Sep 27, 2019 9:33 amYou've mentioned wg0 is on 10.0.0.3. Your internal network is on 192.168.1.0 which means wg0 has nowhere to go. I'm assuming wlan0 is bridged to wg0 ('brctl show')? so sidestep that problem by assigning a 192.168.1.x address to wg0 instead of 10.0.0.3 (if they're on the same subnet no routing required).
Ah. Thanks for mentioning that about WireGuard. I guess it was inevitable it couldn't be "ignored". C'est la vie!ejolson wrote: ↑Fri Sep 27, 2019 3:03 pmNo, you can not bridge a WireGuard network with a physical one. First they are at different levels in the Linux network stack; second bridging could have security implications regarding the cryptographic authentication of packets. Since the Zero is acting as the access point, then it is already the default route for all the computers on wireless. All you need is the correct routing and forwarding rules on the Pi Zero.swampdog wrote: ↑Fri Sep 27, 2019 9:33 amYou've mentioned wg0 is on 10.0.0.3. Your internal network is on 192.168.1.0 which means wg0 has nowhere to go. I'm assuming wlan0 is bridged to wg0 ('brctl show')? so sidestep that problem by assigning a 192.168.1.x address to wg0 instead of 10.0.0.3 (if they're on the same subnet no routing required).
I'll be setting up a Zero as an access point this weekend to deal with some some smart switches. Upstream will be through a wired dongle but otherwise much the same. I'll try to post a working configuration soon.
This is a quick description of how to setup a WireGuard VPN tunnel using two Raspberry Pi Computers to securely route traffic between two private subnets. We assume each of the Raspberry Pi computers have two network interfaces: one should be connected to the private subnet and the other should be connected to the Internet and have a public IP addresses.
Code: Select all
# Downstream wireguard.conf
[Interface]
ListenPort = 50176
PrivateKey = ???????????????????????????????????????????=
[Peer]
PrivateKey = ???????????????????????????????????????????=
Endpoint = upstream.public.ip:50176
AllowedIPs = 192.168.0.0/16
Code: Select all
# Upstream wireguard.conf
[Interface]
ListenPort = 50176
PrivateKey = ???????????????????????????????????????????=
[Peer]
PrivateKey = ???????????????????????????????????????????=
Endpoint = downstream.public.ip:50176
AllowedIPs = 192.168.0.0/16
Code: Select all
#!/bin/bash
# Downstream wireguard initialization
case $1 in
down)
ip route del to 192.168.17.0/24 via 192.168.16.2
ip link set down dev wg0
ip address del dev wg0 192.168.16.1/24
ip link del dev wg0 type wireguard
;;
*)
ip link add dev wg0 type wireguard
ip address add dev wg0 192.168.16.1/24
wg setconf wg0 wireguard.conf
ip link set up dev wg0
ip route add to 192.168.17.0/24 via 192.168.16.2
esac
Code: Select all
#!/bin/bash
# Upstream wireguard initialization
case $1 in
down)
ip route del to 192.168.15.0/24 via 192.168.16.1
ip link set down dev wg0
ip address del dev wg0 192.168.16.2/24
ip link del dev wg0 type wireguard
;;
*)
ip link add dev wg0 type wireguard
ip address add dev wg0 192.168.16.2/24
wg setconf wg0 wireguard.conf
ip link set up dev wg0
ip route add to 192.168.15.0/24 via 192.168.16.1
esac
Code: Select all
#!/bin/bash
# Turn off forwarding
echo 0 > /proc/sys/net/ipv4/ip_forward
# Flush the rulesets
iptables -F
iptables -t nat -F
# Make reject the forwarding policy
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# Forward from the LAN and tunnels
iptables -A FORWARD -i wlan0 -j ACCEPT
iptables -A FORWARD -i wg0 -j ACCEPT
# Forward established masquerade connections
iptables -A FORWARD -i eth0 -o wlan0 -m state \
--state ESTABLISHED,RELATED -j ACCEPT
# IP Masquerade to upstream
iptables -t nat -A POSTROUTING ! -d 192.168.0.0/16 -o eth0 -j MASQUERADE
# Turn on forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward