mirror of
https://github.com/garywill/linux-router.git
synced 2025-12-30 10:48:28 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15a2e0ce53 | ||
|
|
083cd42afd | ||
|
|
7c6113f1d2 | ||
|
|
0ccdcf647a | ||
|
|
61a5af2202 | ||
|
|
d2bc2d9460 | ||
|
|
2468b5a415 | ||
|
|
3b71515e07 | ||
|
|
c2b21bb391 | ||
|
|
e5fc9efe48 | ||
|
|
1e3c5004c3 |
42
NOTICE
42
NOTICE
@@ -1,42 +0,0 @@
|
|||||||
Copyright (c) 2013, oblique
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
* Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
================================================================================
|
|
||||||
|
|
||||||
linux-router
|
|
||||||
Copyright (C) 2018 garywill
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
237
README.md
237
README.md
@@ -1,10 +1,10 @@
|
|||||||
# Linux-router
|
# Linux-router
|
||||||
|
|
||||||
Set Linux as router in one command. Able to Provide Internet, or create Wifi hotspot. Support transparent proxy (redsocks). Also useful for routing VM/containers.
|
Set Linux as router in one command. Able to provide Internet, or create WiFi hotspot. Support transparent proxy (redsocks). Also useful for routing VM/containers.
|
||||||
|
|
||||||
It wraps `iptables`, `dnsmasq` etc. stuff. Use in one command, restore in one command or by `control-c` (or even by closing terminal window).
|
It wraps `iptables`, `dnsmasq` etc. stuff. Use in one command, restore in one command or by `control-c` (or even by closing terminal window).
|
||||||
|
|
||||||
[Buy me a coffee](https://github.com/garywill/receiving/blob/master/receiving_methods.md) :)
|
[More tools and projects 🛠️](https://garywill.github.io) | [🍻 Buy me a coffee ❤️](https://github.com/garywill/receiving/blob/master/receiving_methods.md)
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -12,16 +12,19 @@ Basic features:
|
|||||||
|
|
||||||
- Create a NATed sub-network
|
- Create a NATed sub-network
|
||||||
- Provide Internet
|
- Provide Internet
|
||||||
- DHCP server and RA
|
- DHCP server (and RA)
|
||||||
|
- Specify what DNS the DHCP server assigns to clients
|
||||||
- DNS server
|
- DNS server
|
||||||
|
- Specify upstream DNS (kind of a plain DNS proxy)
|
||||||
- IPv6 (behind NATed LAN, like IPv4)
|
- IPv6 (behind NATed LAN, like IPv4)
|
||||||
- Creating Wifi hotspot:
|
- Creating WiFi hotspot:
|
||||||
- Channel selecting
|
- Channel selecting
|
||||||
- Choose encryptions: WPA2/WPA, WPA2, WPA, No encryption
|
- Choose encryptions: WPA2/WPA, WPA2, WPA, No encryption
|
||||||
- Create AP on the same interface you are getting Internet (require same channel)
|
- Create AP on the same interface you are getting Internet (usually require same channel)
|
||||||
- Transparent proxy (redsocks)
|
- Transparent proxy (redsocks)
|
||||||
- DNS proxy
|
- Transparent DNS proxy (hijack port 53 packets)
|
||||||
- Compatible with NetworkManager (automatically set interface as unmanaged)
|
- Compatible with NetworkManager (automatically set interface as unmanaged)
|
||||||
|
- You can run many instances, to create many different networks. Has instances managing feature.
|
||||||
|
|
||||||
**For many other features, see below [CLI usage](#cli-usage-and-other-features)**
|
**For many other features, see below [CLI usage](#cli-usage-and-other-features)**
|
||||||
|
|
||||||
@@ -35,7 +38,7 @@ Internet----(eth0/wlan0)-Linux-(wlanX)AP
|
|||||||
|
|
||||||
```
|
```
|
||||||
Internet
|
Internet
|
||||||
Wifi AP(no DHCP) |
|
WiFi AP(no DHCP) |
|
||||||
|----(wlan1)-Linux-(eth0/wlan0)------
|
|----(wlan1)-Linux-(eth0/wlan0)------
|
||||||
| (DHCP)
|
| (DHCP)
|
||||||
|--client
|
|--client
|
||||||
@@ -66,21 +69,39 @@ Internet----(eth0/wlan0)-Linux-(virtual interface)-----VM/container
|
|||||||
sudo lnxrouter -i eth1
|
sudo lnxrouter -i eth1
|
||||||
```
|
```
|
||||||
|
|
||||||
### Provide an interface's Internet to another interface
|
no matter which interface (other than `eth1`) you're getting Internet from.
|
||||||
|
|
||||||
```
|
### Create WiFi hotspot
|
||||||
sudo lnxrouter -i eth1 -o vpn0 --dhcp-dns 1.1.1.1 -6 --dhcp-dns6 [2606:4700:4700::1111]
|
|
||||||
```
|
|
||||||
> Read _Notice 1_
|
|
||||||
|
|
||||||
### Create Wifi hotspot
|
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo lnxrouter --ap wlan0 MyAccessPoint -p MyPassPhrase
|
sudo lnxrouter --ap wlan0 MyAccessPoint -p MyPassPhrase
|
||||||
```
|
```
|
||||||
|
|
||||||
|
no matter which interface you're getting Internet from (even from `wlan0`). Will create virtual Interface `x0wlan0` for hotspot.
|
||||||
|
|
||||||
|
### Provide an interface's Internet to another interface
|
||||||
|
|
||||||
|
Clients access Internet through only `isp5`
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo lnxrouter -i eth1 -o isp5 --no-dns --dhcp-dns 1.1.1.1 -6 --dhcp-dns6 [2606:4700:4700::1111]
|
||||||
|
```
|
||||||
|
|
||||||
|
> In this case of usage, it's recommended to:
|
||||||
|
>
|
||||||
|
> 1. Stop serving local DNS
|
||||||
|
> 2. Tell clients which DNS to use (ISP5's DNS. Or, a safe public DNS, like above example)
|
||||||
|
|
||||||
|
> Also, read *Notice 1*
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### LAN without Internet
|
### LAN without Internet
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo lnxrouter -n -i eth1
|
sudo lnxrouter -n -i eth1
|
||||||
sudo lnxrouter -n --ap wlan0 MyAccessPoint -p MyPassPhrase
|
sudo lnxrouter -n --ap wlan0 MyAccessPoint -p MyPassPhrase
|
||||||
@@ -88,9 +109,12 @@ sudo lnxrouter -n --ap wlan0 MyAccessPoint -p MyPassPhrase
|
|||||||
|
|
||||||
> Read _Notice 1_
|
> Read _Notice 1_
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### Internet for LXC
|
### Internet for LXC
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
Create a bridge
|
Create a bridge
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -110,10 +134,16 @@ lxc.network.hwaddr = xx:xx:xx:xx:xx:xx
|
|||||||
sudo lnxrouter -i lxcbr5
|
sudo lnxrouter -i lxcbr5
|
||||||
```
|
```
|
||||||
|
|
||||||
### Transparent proxy with Tor
|
</details>
|
||||||
|
|
||||||
|
### Transparent proxy
|
||||||
|
|
||||||
|
All clients' Internet traffic go through, for example, Tor
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo lnxrouter -i eth1 --tp 9040 --dns 9053 -g 192.168.55.1 --p6 fd00:5:6:7::
|
sudo lnxrouter -i eth1 --tp 9040 --dns 9053 -g 192.168.55.1 -6 --p6 fd00:5:6:7::
|
||||||
```
|
```
|
||||||
|
|
||||||
In `torrc`
|
In `torrc`
|
||||||
@@ -125,9 +155,13 @@ TransPort [fd00:5:6:7::1]:9040
|
|||||||
DNSPort [fd00:5:6:7::1]:9053
|
DNSPort [fd00:5:6:7::1]:9053
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### Clients-in-sandbox network
|
### Clients-in-sandbox network
|
||||||
|
|
||||||
To not give our infomation to clients:
|
To not give our infomation to clients. Clients can still access Internet.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo lnxrouter -i eth1 \
|
sudo lnxrouter -i eth1 \
|
||||||
@@ -137,11 +171,14 @@ sudo lnxrouter -i eth1 \
|
|||||||
--catch-dns --log-dns # optional
|
--catch-dns --log-dns # optional
|
||||||
```
|
```
|
||||||
|
|
||||||
> This script comes with no warrenty, use on your own risk
|
</details>
|
||||||
|
|
||||||
|
> This script comes with no warrenty. Use on your own risk
|
||||||
|
|
||||||
### Use as transparent proxy for LXD
|
### Use as transparent proxy for LXD
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
Create a bridge
|
Create a bridge
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -192,16 +229,24 @@ To remove the customized `eth0` to restore default `eth0`
|
|||||||
lxc config device remove <container> eth0
|
lxc config device remove <container> eth0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### Use as transparent proxy for VirtualBox
|
### Use as transparent proxy for VirtualBox
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
In VirtualBox's global settings, create a host-only network `vboxnet5` with DHCP disabled.
|
In VirtualBox's global settings, create a host-only network `vboxnet5` with DHCP disabled.
|
||||||
|
|
||||||
```
|
```
|
||||||
sudo lnxrouter -i vboxnet5 --tp 9040 --dns 9053
|
sudo lnxrouter -i vboxnet5 --tp 9040 --dns 9053
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### Use as transparent proxy for firejail
|
### Use as transparent proxy for firejail
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
Create a bridge
|
Create a bridge
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -213,8 +258,16 @@ sudo lnxrouter -i firejail5 -g 192.168.55.1 --tp 9040 --dns 9053
|
|||||||
firejail --net=firejail5 --dns=192.168.55.1 --blacklist=/var/run/nscd
|
firejail --net=firejail5 --dns=192.168.55.1 --blacklist=/var/run/nscd
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Firejail's `/etc/resolv.conf` doesn't obtain DNS from DHCP, so we need to assign.
|
||||||
|
|
||||||
|
nscd is domain name cache service, which shouldn't be accessed from in jail here.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### CLI usage and other features
|
### CLI usage and other features
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage: lnxrouter <options>
|
Usage: lnxrouter <options>
|
||||||
|
|
||||||
@@ -224,20 +277,23 @@ Options:
|
|||||||
|
|
||||||
-i <interface> Interface to make NATed sub-network,
|
-i <interface> Interface to make NATed sub-network,
|
||||||
and to provide Internet to
|
and to provide Internet to
|
||||||
(To create Wifi hotspot use '--ap' instead)
|
(To create WiFi hotspot use '--ap' instead)
|
||||||
-o <interface> Specify an inteface to provide Internet from.
|
-o <interface> Specify an inteface to provide Internet from.
|
||||||
|
(See Notice 1)
|
||||||
(Note using this with default DNS option may leak
|
(Note using this with default DNS option may leak
|
||||||
queries to other interfaces)
|
queries to other interfaces)
|
||||||
-n Do not provide Internet (See Notice 1)
|
-n Do not provide Internet (See Notice 1)
|
||||||
--ban-priv Disallow clients to access my private network
|
--ban-priv Disallow clients to access my private network
|
||||||
|
|
||||||
-g <ip> This host's IPv4 address in subnet (mask is /24)
|
-g <ip> This host's IPv4 address in subnet (mask is /24)
|
||||||
|
(example: '192.168.5.1' or '5' shortly)
|
||||||
-6 Enable IPv6 (NAT)
|
-6 Enable IPv6 (NAT)
|
||||||
--no4 Disable IPv4 Internet (not forwarding IPv4)
|
--no4 Disable IPv4 Internet (not forwarding IPv4)
|
||||||
(See Notice 1). Usually used with '-6'
|
(See Notice 1). Usually used with '-6'
|
||||||
|
|
||||||
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
|
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
|
||||||
(example: fd00:1:2:3::) Using this enables '-6'
|
(example: 'fd00:0:0:5::' or '5' shortly)
|
||||||
|
Using this enables '-6'
|
||||||
|
|
||||||
--dns <ip>|<port>|<ip:port>
|
--dns <ip>|<port>|<ip:port>
|
||||||
DNS server's upstream DNS.
|
DNS server's upstream DNS.
|
||||||
@@ -248,13 +304,14 @@ Options:
|
|||||||
--no-dnsmasq Disable dnsmasq server (DHCP, DNS, RA)
|
--no-dnsmasq Disable dnsmasq server (DHCP, DNS, RA)
|
||||||
--catch-dns Transparent DNS proxy, redirect packets(TCP/UDP)
|
--catch-dns Transparent DNS proxy, redirect packets(TCP/UDP)
|
||||||
whose destination port is 53 to this host
|
whose destination port is 53 to this host
|
||||||
--log-dns Show DNS query log
|
--log-dns Show DNS query log (dnsmasq)
|
||||||
--dhcp-dns <IP1[,IP2]>|no
|
--dhcp-dns <IP1[,IP2]>|no
|
||||||
Set IPv4 DNS offered by DHCP (default: this host)
|
Set IPv4 DNS offered by DHCP (default: this host).
|
||||||
--dhcp-dns6 <IP1[,IP2]>|no
|
--dhcp-dns6 <IP1[,IP2]>|no
|
||||||
Set IPv6 DNS offered by DHCP (RA)
|
Set IPv6 DNS offered by DHCP (RA)
|
||||||
(default: this host)
|
(default: this host)
|
||||||
(Note IPv6 addresses need '[]' around)
|
(Note IPv6 addresses need '[]' around)
|
||||||
|
Using both above two will enable '--no-dns'
|
||||||
--hostname <name> DNS server associate this name with this host.
|
--hostname <name> DNS server associate this name with this host.
|
||||||
Use '-' to read name from /etc/hostname
|
Use '-' to read name from /etc/hostname
|
||||||
-d DNS server will take into account /etc/hosts
|
-d DNS server will take into account /etc/hosts
|
||||||
@@ -268,12 +325,12 @@ Options:
|
|||||||
redirect non-LAN TCP and UDP traffic to port.
|
redirect non-LAN TCP and UDP traffic to port.
|
||||||
(usually used with '--dns')
|
(usually used with '--dns')
|
||||||
|
|
||||||
Wifi hotspot options:
|
WiFi hotspot options:
|
||||||
--ap <wifi interface> <SSID>
|
--ap <wifi interface> <SSID>
|
||||||
Create Wifi access point
|
Create WiFi access point
|
||||||
-p, --password <password>
|
-p, --password <password>
|
||||||
Wifi password
|
WiFi password
|
||||||
--qr Show Wifi QR code in terminal
|
--qr Show WiFi QR code in terminal
|
||||||
|
|
||||||
--hidden Hide access point (not broadcast SSID)
|
--hidden Hide access point (not broadcast SSID)
|
||||||
--no-virt Do not create virtual interface
|
--no-virt Do not create virtual interface
|
||||||
@@ -288,8 +345,8 @@ Options:
|
|||||||
(default: 2)
|
(default: 2)
|
||||||
--psk Use 64 hex digits pre-shared-key instead of
|
--psk Use 64 hex digits pre-shared-key instead of
|
||||||
passphrase
|
passphrase
|
||||||
--mac-filter Enable Wifi hotspot MAC address filtering
|
--mac-filter Enable WiFi hotspot MAC address filtering
|
||||||
--mac-filter-accept Location of Wifi hotspot MAC address filter list
|
--mac-filter-accept Location of WiFi hotspot MAC address filter list
|
||||||
(defaults to /etc/hostapd/hostapd.accept)
|
(defaults to /etc/hostapd/hostapd.accept)
|
||||||
--hostapd-debug <level> 1 or 2. Passes -d or -dd to hostapd
|
--hostapd-debug <level> 1 or 2. Passes -d or -dd to hostapd
|
||||||
--isolate-clients Disable wifi communication between clients
|
--isolate-clients Disable wifi communication between clients
|
||||||
@@ -303,28 +360,42 @@ Options:
|
|||||||
|
|
||||||
Instance managing:
|
Instance managing:
|
||||||
--daemon Run in background
|
--daemon Run in background
|
||||||
--list-running Show running instances
|
-l, --list-running Show running instances
|
||||||
--lc, --list-clients <id>
|
--lc, --list-clients <id|interface>
|
||||||
List clients of an instance. Or list neighbors of
|
List clients of an instance. Or list neighbors of
|
||||||
any interface, even if it isn't handled by us
|
an interface, even if it isn't handled by us.
|
||||||
|
(passive mode)
|
||||||
--stop <id> Stop a running instance
|
--stop <id> Stop a running instance
|
||||||
For <id> you can use PID or subnet interface name.
|
For <id> you can use PID or subnet interface name.
|
||||||
You can get them with '--list-running'
|
You can get them with '--list-running'
|
||||||
|
|
||||||
Notice 1: This script assume your host's default policy won't forward
|
|
||||||
packets, so the script won't explictly ban forwarding in any
|
|
||||||
mode. In some unexpected case may cause unwanted packets
|
|
||||||
leakage between 2 networks, which you should be aware of if you
|
|
||||||
want isolated network
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> These changes to system will not be restored by script's cleanup:
|
</details>
|
||||||
> 1. `/proc/sys/net/ipv4/ip_forward = 1` and `/proc/sys/net/ipv6/conf/all/forwarding = 1`
|
|
||||||
> 1. dnsmasq in Apparmor complain mode
|
|
||||||
> 1. Kernel module `nf_nat_pptp` loaded
|
|
||||||
> 1. The wifi device which is used to create hotspot is `rfkill unblock`ed
|
|
||||||
> 1. Wifi country code, if user specified
|
|
||||||
|
|
||||||
|
## Notice
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
```
|
||||||
|
Notice 1: This script assume your host's default policy won't forward
|
||||||
|
packets, so the script won't explictly ban forwarding in any
|
||||||
|
mode. In some unexpected case (eg. mistaken configurations) may
|
||||||
|
cause unwanted packets leakage between 2 networks, which you
|
||||||
|
should be aware of if you want isolated network
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## What changes are done to Linux system
|
||||||
|
|
||||||
|
On exit of a linux-router instance, script **will do cleanup**, i.e. undo most changes to system. Though, **some** changes will **not** be undone, which are:
|
||||||
|
|
||||||
|
1. `/proc/sys/net/ipv4/ip_forward = 1` and `/proc/sys/net/ipv6/conf/all/forwarding = 1`
|
||||||
|
2. dnsmasq (if used) in Apparmor complain mode
|
||||||
|
3. hostapd (if used) in Apparmor complain mode
|
||||||
|
4. Kernel module `nf_nat_pptp` loaded
|
||||||
|
5. The wifi device which is used to create hotspot is `rfkill unblock`ed
|
||||||
|
6. WiFi country code, if user assigns
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
@@ -332,31 +403,93 @@ Options:
|
|||||||
- procps or procps-ng
|
- procps or procps-ng
|
||||||
- iproute2
|
- iproute2
|
||||||
- dnsmasq
|
- dnsmasq
|
||||||
- iptables
|
- iptables (or nftables with `iptables-nft` translation linked)
|
||||||
- WiFi hotspot dependencies
|
- WiFi hotspot dependencies
|
||||||
- hostapd
|
- hostapd
|
||||||
- iw
|
- iw
|
||||||
- iwconfig (you only need this if 'iw' can not recognize your adapter)
|
- iwconfig (you only need this if 'iw' can not recognize your adapter)
|
||||||
- haveged (optional)
|
- haveged (optional)
|
||||||
- qrencode (opional)
|
- qrencode (optional)
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
- Compatibility with firewalld
|
||||||
- WPA3
|
- WPA3
|
||||||
- Global IPv6
|
- Global IPv6
|
||||||
- Explictly ban forwarding if not needed
|
- Explictly ban forwarding if not needed
|
||||||
- Bring bridging method back
|
- Bring bridging method back
|
||||||
|
|
||||||
## Donate
|
</details>
|
||||||
|
|
||||||
[Buy me a coffee](https://github.com/garywill/receiving/blob/master/receiving_methods.md) , this project took me lots of time!
|
## License
|
||||||
|
|
||||||
( ^\_^)o自自o(^_^ )
|
linux-router is LGPL licensed
|
||||||
|
|
||||||
No? Okay, or just give me a star!
|
<details>
|
||||||
|
|
||||||
## For developers
|
```
|
||||||
|
linux-router
|
||||||
|
Copyright (C) 2018 garywill
|
||||||
|
|
||||||
**Many thanks to project [create_ap](https://github.com/oblique/create_ap)**.
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This script was forked from create\_ap. Now it's quite different from it. (See `history` branch for how I modified create_ap)
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
Upstream create_ap was BSD licensed
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
```
|
||||||
|
Copyright (c) 2013, oblique
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## Meet developer(s) and become one of them
|
||||||
|
|
||||||
|
Visit [**my homepage** 🏡](https://garywill.github.io) to see **more tools and projects** 🛠️.
|
||||||
|
|
||||||
|
> [❤️ Buy me a coffee](https://github.com/garywill/receiving/blob/master/receiving_methods.md) , this project took me lots of time! ([❤️ 扫个码打赏一个!](https://github.com/garywill/receiving/blob/master/receiving_methods.md))
|
||||||
|
>
|
||||||
|
> 🥂 ( ^\_^) o自自o (^_^ ) 🍻
|
||||||
|
|
||||||
|
🤝 Bisides, thank [create_ap](https://github.com/oblique/create_ap) by [oblique](https://github.com/oblique). This script was forked from create\_ap. Now they are quite different. (See `history` branch for how I modified create_ap). 🤝 Also thank those who contributed to that project.
|
||||||
|
|
||||||
|
👨💻 You can be contributor, too! 🍃 There're some TO-DOs listed, at both [above](#todo) and [in the code file](https://github.com/garywill/linux-router/search?q=TODO&type=code). 🍃 Also some [unfulfilled enhancements in the Issues](https://github.com/garywill/linux-router/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement). Your name can be here!
|
||||||
|
|||||||
555
lnxrouter
555
lnxrouter
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
VERSION=0.6.0
|
VERSION=0.6.6
|
||||||
PROGNAME="$(basename $0)"
|
PROGNAME="$(basename $0)"
|
||||||
|
|
||||||
export LC_ALL=C
|
export LC_ALL=C
|
||||||
@@ -8,10 +8,16 @@ export LC_ALL=C
|
|||||||
SCRIPT_UMASK=0122
|
SCRIPT_UMASK=0122
|
||||||
umask $SCRIPT_UMASK
|
umask $SCRIPT_UMASK
|
||||||
|
|
||||||
|
phead() {
|
||||||
|
echo "linux-router $VERSION (https://github.com/garywill/linux-router)"
|
||||||
|
}
|
||||||
|
phead2() {
|
||||||
|
echo "Released under LGPL, with no warranty. Use on your own risk."
|
||||||
|
}
|
||||||
usage() {
|
usage() {
|
||||||
|
phead
|
||||||
|
phead2
|
||||||
cat << EOF
|
cat << EOF
|
||||||
linux-router $VERSION (https://github.com/garywill/linux-router)
|
|
||||||
Released under LGPL, with no warranty. Use on your own risk.
|
|
||||||
|
|
||||||
Usage: $PROGNAME <options>
|
Usage: $PROGNAME <options>
|
||||||
|
|
||||||
@@ -21,20 +27,23 @@ Options:
|
|||||||
|
|
||||||
-i <interface> Interface to make NATed sub-network,
|
-i <interface> Interface to make NATed sub-network,
|
||||||
and to provide Internet to
|
and to provide Internet to
|
||||||
(To create Wifi hotspot use '--ap' instead)
|
(To create WiFi hotspot use '--ap' instead)
|
||||||
-o <interface> Specify an inteface to provide Internet from.
|
-o <interface> Specify an inteface to provide Internet from.
|
||||||
|
(See Notice 1)
|
||||||
(Note using this with default DNS option may leak
|
(Note using this with default DNS option may leak
|
||||||
queries to other interfaces)
|
queries to other interfaces)
|
||||||
-n Do not provide Internet (See Notice 1)
|
-n Do not provide Internet (See Notice 1)
|
||||||
--ban-priv Disallow clients to access my private network
|
--ban-priv Disallow clients to access my private network
|
||||||
|
|
||||||
-g <ip> This host's IPv4 address in subnet (mask is /24)
|
-g <ip> This host's IPv4 address in subnet (mask is /24)
|
||||||
|
(example: '192.168.5.1' or '5' shortly)
|
||||||
-6 Enable IPv6 (NAT)
|
-6 Enable IPv6 (NAT)
|
||||||
--no4 Disable IPv4 Internet (not forwarding IPv4)
|
--no4 Disable IPv4 Internet (not forwarding IPv4)
|
||||||
(See Notice 1). Usually used with '-6'
|
(See Notice 1). Usually used with '-6'
|
||||||
|
|
||||||
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
|
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
|
||||||
(example: fd00:1:2:3::) Using this enables '-6'
|
(example: 'fd00:0:0:5::' or '5' shortly)
|
||||||
|
Using this enables '-6'
|
||||||
|
|
||||||
--dns <ip>|<port>|<ip:port>
|
--dns <ip>|<port>|<ip:port>
|
||||||
DNS server's upstream DNS.
|
DNS server's upstream DNS.
|
||||||
@@ -45,13 +54,14 @@ Options:
|
|||||||
--no-dnsmasq Disable dnsmasq server (DHCP, DNS, RA)
|
--no-dnsmasq Disable dnsmasq server (DHCP, DNS, RA)
|
||||||
--catch-dns Transparent DNS proxy, redirect packets(TCP/UDP)
|
--catch-dns Transparent DNS proxy, redirect packets(TCP/UDP)
|
||||||
whose destination port is 53 to this host
|
whose destination port is 53 to this host
|
||||||
--log-dns Show DNS query log
|
--log-dns Show DNS query log (dnsmasq)
|
||||||
--dhcp-dns <IP1[,IP2]>|no
|
--dhcp-dns <IP1[,IP2]>|no
|
||||||
Set IPv4 DNS offered by DHCP (default: this host)
|
Set IPv4 DNS offered by DHCP (default: this host).
|
||||||
--dhcp-dns6 <IP1[,IP2]>|no
|
--dhcp-dns6 <IP1[,IP2]>|no
|
||||||
Set IPv6 DNS offered by DHCP (RA)
|
Set IPv6 DNS offered by DHCP (RA)
|
||||||
(default: this host)
|
(default: this host)
|
||||||
(Note IPv6 addresses need '[]' around)
|
(Note IPv6 addresses need '[]' around)
|
||||||
|
Using both above two will enable '--no-dns'
|
||||||
--hostname <name> DNS server associate this name with this host.
|
--hostname <name> DNS server associate this name with this host.
|
||||||
Use '-' to read name from /etc/hostname
|
Use '-' to read name from /etc/hostname
|
||||||
-d DNS server will take into account /etc/hosts
|
-d DNS server will take into account /etc/hosts
|
||||||
@@ -65,12 +75,12 @@ Options:
|
|||||||
redirect non-LAN TCP and UDP traffic to port.
|
redirect non-LAN TCP and UDP traffic to port.
|
||||||
(usually used with '--dns')
|
(usually used with '--dns')
|
||||||
|
|
||||||
Wifi hotspot options:
|
WiFi hotspot options:
|
||||||
--ap <wifi interface> <SSID>
|
--ap <wifi interface> <SSID>
|
||||||
Create Wifi access point
|
Create WiFi access point
|
||||||
-p, --password <password>
|
-p, --password <password>
|
||||||
Wifi password
|
WiFi password
|
||||||
--qr Show Wifi QR code in terminal
|
--qr Show WiFi QR code in terminal
|
||||||
|
|
||||||
--hidden Hide access point (not broadcast SSID)
|
--hidden Hide access point (not broadcast SSID)
|
||||||
--no-virt Do not create virtual interface
|
--no-virt Do not create virtual interface
|
||||||
@@ -85,8 +95,8 @@ Options:
|
|||||||
(default: 2)
|
(default: 2)
|
||||||
--psk Use 64 hex digits pre-shared-key instead of
|
--psk Use 64 hex digits pre-shared-key instead of
|
||||||
passphrase
|
passphrase
|
||||||
--mac-filter Enable Wifi hotspot MAC address filtering
|
--mac-filter Enable WiFi hotspot MAC address filtering
|
||||||
--mac-filter-accept Location of Wifi hotspot MAC address filter list
|
--mac-filter-accept Location of WiFi hotspot MAC address filter list
|
||||||
(defaults to /etc/hostapd/hostapd.accept)
|
(defaults to /etc/hostapd/hostapd.accept)
|
||||||
--hostapd-debug <level> 1 or 2. Passes -d or -dd to hostapd
|
--hostapd-debug <level> 1 or 2. Passes -d or -dd to hostapd
|
||||||
--isolate-clients Disable wifi communication between clients
|
--isolate-clients Disable wifi communication between clients
|
||||||
@@ -100,25 +110,24 @@ Options:
|
|||||||
|
|
||||||
Instance managing:
|
Instance managing:
|
||||||
--daemon Run in background
|
--daemon Run in background
|
||||||
--list-running Show running instances
|
-l, --list-running Show running instances
|
||||||
--lc, --list-clients <id>
|
--lc, --list-clients <id|interface>
|
||||||
List clients of an instance. Or list neighbors of
|
List clients of an instance. Or list neighbors of
|
||||||
any interface, even if it isn't handled by us
|
an interface, even if it isn't handled by us.
|
||||||
|
(passive mode)
|
||||||
--stop <id> Stop a running instance
|
--stop <id> Stop a running instance
|
||||||
For <id> you can use PID or subnet interface name.
|
For <id> you can use PID or subnet interface name.
|
||||||
You can get them with '--list-running'
|
You can get them with '--list-running'
|
||||||
|
|
||||||
Notice 1: This script assume your host's default policy won't forward
|
Notice 1: This script assume your host's default policy won't forward
|
||||||
packets, so the script won't explictly ban forwarding in any
|
packets, so the script won't explictly ban forwarding in any
|
||||||
mode. In some unexpected case may cause unwanted packets
|
mode. In some unexpected case (eg. mistaken configurations) may
|
||||||
leakage between 2 networks, which you should be aware of if you
|
cause unwanted packets leakage between 2 networks, which you
|
||||||
want isolated network
|
should be aware of if you want isolated network
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
$PROGNAME -i eth1
|
$PROGNAME -i eth1
|
||||||
$PROGNAME --ap wlan0 MyAccessPoint
|
|
||||||
$PROGNAME --ap wlan0 MyAccessPoint -p MyPassPhrase
|
$PROGNAME --ap wlan0 MyAccessPoint -p MyPassPhrase
|
||||||
$PROGNAME -n --ap wlan0 MyAccessPoint -p MyPassPhrase
|
|
||||||
$PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy>
|
$PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy>
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@@ -222,16 +231,10 @@ parse_user_options(){
|
|||||||
shift
|
shift
|
||||||
INTERNET_IFACE="$1"
|
INTERNET_IFACE="$1"
|
||||||
shift
|
shift
|
||||||
echo ""
|
|
||||||
echo "WARN: Since you're using in this mode, make sure you've read Notice 1" >&2
|
|
||||||
echo ""
|
|
||||||
;;
|
;;
|
||||||
-n)
|
-n)
|
||||||
shift
|
shift
|
||||||
SHARE_METHOD=none
|
SHARE_METHOD=none
|
||||||
echo ""
|
|
||||||
echo "WARN: Since you're using in this mode, make sure you've read Notice 1" >&2
|
|
||||||
echo ""
|
|
||||||
;;
|
;;
|
||||||
--ban-priv)
|
--ban-priv)
|
||||||
shift
|
shift
|
||||||
@@ -257,9 +260,6 @@ parse_user_options(){
|
|||||||
--no4)
|
--no4)
|
||||||
shift
|
shift
|
||||||
NO4=1
|
NO4=1
|
||||||
echo ""
|
|
||||||
echo "WARN: Since you're using in this mode, make sure you've read Notice 1" >&2
|
|
||||||
echo ""
|
|
||||||
;;
|
;;
|
||||||
--p6)
|
--p6)
|
||||||
shift
|
shift
|
||||||
@@ -440,7 +440,7 @@ parse_user_options(){
|
|||||||
STOP_ID="$1"
|
STOP_ID="$1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--list-running)
|
-l|--list-running)
|
||||||
shift
|
shift
|
||||||
LIST_RUNNING=1
|
LIST_RUNNING=1
|
||||||
;;
|
;;
|
||||||
@@ -735,7 +735,7 @@ haveged_watchdog() {
|
|||||||
echo "WARN: Low entropy detected. We recommend you to install \`haveged'" 1>&2
|
echo "WARN: Low entropy detected. We recommend you to install \`haveged'" 1>&2
|
||||||
show_warn=0
|
show_warn=0
|
||||||
fi
|
fi
|
||||||
elif ! pidof haveged > /dev/null 2>&1; then
|
elif ! pidof haveged > /dev/null 2>&1; then # TODO judge zombie ?
|
||||||
echo "Low entropy detected, starting haveged" 1>&2
|
echo "Low entropy detected, starting haveged" 1>&2
|
||||||
# boost low-entropy
|
# boost low-entropy
|
||||||
haveged -w 1024 -p $COMMON_CONFDIR/haveged.pid
|
haveged -w 1024 -p $COMMON_CONFDIR/haveged.pid
|
||||||
@@ -744,7 +744,24 @@ haveged_watchdog() {
|
|||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
pid_watchdog() {
|
||||||
|
local PID="$1"
|
||||||
|
local SLEEP="$2"
|
||||||
|
local ERR_MSG="$3"
|
||||||
|
local ST
|
||||||
|
while true
|
||||||
|
do
|
||||||
|
if [[ -e "/proc/$PID" ]]; then
|
||||||
|
ST="$(cat "/proc/$PID/status" | grep "^State:" | awk '{print $2}')"
|
||||||
|
if [[ "$ST" != 'Z' ]]; then
|
||||||
|
sleep $SLEEP
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
die "$ERR_MSG"
|
||||||
|
done
|
||||||
|
|
||||||
|
}
|
||||||
#========
|
#========
|
||||||
|
|
||||||
|
|
||||||
@@ -791,15 +808,96 @@ nm_restore_manage() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
#=========
|
#=========
|
||||||
|
check_iptables()
|
||||||
|
{
|
||||||
|
echo
|
||||||
|
iptables --version
|
||||||
|
|
||||||
iptables_()
|
if which firewall-cmd > /dev/null 2>&1; then
|
||||||
{
|
if [[ "$(firewall-cmd --state 2>&1)" == "running" ]]; then
|
||||||
iptables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
|
echo "firewalld is running ($(firewall-cmd --version))"
|
||||||
return $?
|
echo -e "\nWARN: We haven't completed the compatibility with firewalld.\nWARN: If you see any trouble, try:\nWARN: 1) 'firewall-cmd --zone=trusted --add-interface=<SUBN_IFACE>'\nWARN: 2) disable firewalld\n" >&2
|
||||||
|
# TODO
|
||||||
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
ip6tables_()
|
|
||||||
|
CUSTOM_CHAINS_4_filter=
|
||||||
|
CUSTOM_CHAINS_4_nat=
|
||||||
|
CUSTOM_CHAINS_6_filter=
|
||||||
|
CUSTOM_CHAINS_6_nat=
|
||||||
|
iptb()
|
||||||
{
|
{
|
||||||
ip6tables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
|
local FoS=$1 # 4 | 6
|
||||||
|
shift
|
||||||
|
local Vis=$1 # 'v' | 'n'
|
||||||
|
shift
|
||||||
|
local T=$1 # table
|
||||||
|
shift
|
||||||
|
local ACT=$1 # action: I | A | N . On undo: I or A -> D , N -> F+X
|
||||||
|
shift
|
||||||
|
local CH=$1 # chain
|
||||||
|
shift
|
||||||
|
|
||||||
|
[[ "$IPV6" -ne 1 && "$FoS" == "6" ]] && return
|
||||||
|
|
||||||
|
local CMD_HEAD=""
|
||||||
|
local MOUTH=""
|
||||||
|
local NECK=""
|
||||||
|
local HAND_UN_NC=0
|
||||||
|
local TAIL=""
|
||||||
|
|
||||||
|
local FULL=""
|
||||||
|
local ADD_TO_UNDO=1
|
||||||
|
|
||||||
|
for arr_name in CUSTOM_CHAINS_4_filter CUSTOM_CHAINS_4_nat CUSTOM_CHAINS_6_filter CUSTOM_CHAINS_6_nat
|
||||||
|
do
|
||||||
|
local arr_content
|
||||||
|
eval arr_content=\"\${$arr_name}\"
|
||||||
|
#echo $arr_content
|
||||||
|
|
||||||
|
for w in $arr_content
|
||||||
|
do
|
||||||
|
if [[ "$arr_name" =~ "$FoS" && "$arr_name" =~ "$T" && "$w" == "$CH" ]]; then
|
||||||
|
ADD_TO_UNDO=0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
[[ "$FoS" == "4" ]] && CMD_HEAD="iptables -w "
|
||||||
|
[[ "$FoS" == "6" ]] && CMD_HEAD="ip6tables -w "
|
||||||
|
|
||||||
|
[[ "$Vis" == 'v' ]] && MOUTH="-v"
|
||||||
|
|
||||||
|
NECK="-t ${T}"
|
||||||
|
|
||||||
|
if [[ "$ACT" == "N" ]]; then
|
||||||
|
eval CUSTOM_CHAINS_${FoS}_${T}=\"\${CUSTOM_CHAINS_${FoS}_${T}} ${CH}\"
|
||||||
|
HAND_UN_NC=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[[ ! "$NETFILTER_XT_MATCH_COMMENT" == "0" ]] && TAIL="-m comment --comment lrt${$}${SUBNET_IFACE}"
|
||||||
|
|
||||||
|
if [[ "$ADD_TO_UNDO" -eq 1 ]]; then
|
||||||
|
if [[ "$ACT" == "I" || "$ACT" == "A" ]]; then
|
||||||
|
echo "$CMD_HEAD $NECK -D ${CH} $@ $TAIL" >> $CONFDIR/undo_iptables.sh
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$HAND_UN_NC" -eq 1 ]]; then
|
||||||
|
echo "$CMD_HEAD $NECK -F ${CH} $@ $TAIL" >> $CONFDIR/undo_iptables_2.sh
|
||||||
|
echo "$CMD_HEAD $NECK -X ${CH} $@ $TAIL" >> $CONFDIR/undo_iptables_2.sh
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FULL="$CMD_HEAD $MOUTH $NECK -${ACT} ${CH} $@ $TAIL"
|
||||||
|
#echo $FULL
|
||||||
|
$FULL
|
||||||
return $?
|
return $?
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -814,211 +912,128 @@ start_nat() {
|
|||||||
echo
|
echo
|
||||||
echo "iptables: NAT "
|
echo "iptables: NAT "
|
||||||
if [[ $NO4 -eq 0 ]]; then
|
if [[ $NO4 -eq 0 ]]; then
|
||||||
iptables_ -v -t nat -I POSTROUTING -s ${GATEWAY%.*}.0/24 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${GATEWAY%.*}.0/24 -j MASQUERADE || die
|
iptb 4 v nat I POSTROUTING -s ${GATEWAY%.*}.0/24 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${GATEWAY%.*}.0/24 -j MASQUERADE || die
|
||||||
iptables_ -v -I FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${GATEWAY%.*}.0/24 -j ACCEPT || die
|
iptb 4 v filter I FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${GATEWAY%.*}.0/24 -j ACCEPT || die
|
||||||
iptables_ -v -I FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${GATEWAY%.*}.0/24 -j ACCEPT || die
|
iptb 4 v filter I FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${GATEWAY%.*}.0/24 -j ACCEPT || die
|
||||||
fi
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -v -t nat -I POSTROUTING -s ${PREFIX6}/64 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${PREFIX6}/64 -j MASQUERADE || die
|
|
||||||
ip6tables_ -v -I FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${PREFIX6}/64 -j ACCEPT || die
|
|
||||||
ip6tables_ -v -I FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${PREFIX6}/64 -j ACCEPT || die
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
stop_nat() {
|
|
||||||
echo "iptables: stop NAT"
|
|
||||||
if [[ $NO4 -eq 0 ]]; then
|
|
||||||
iptables_ -t nat -D POSTROUTING -s ${GATEWAY%.*}.0/24 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${GATEWAY%.*}.0/24 -j MASQUERADE
|
|
||||||
iptables_ -D FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${GATEWAY%.*}.0/24 -j ACCEPT
|
|
||||||
iptables_ -D FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${GATEWAY%.*}.0/24 -j ACCEPT
|
|
||||||
fi
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -t nat -D POSTROUTING -s ${PREFIX6}/64 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${PREFIX6}/64 -j MASQUERADE
|
|
||||||
ip6tables_ -D FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${PREFIX6}/64 -j ACCEPT
|
|
||||||
ip6tables_ -D FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${PREFIX6}/64 -j ACCEPT
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
iptb 6 v nat I POSTROUTING -s ${PREFIX6}/64 $IPTABLES_NAT_OUT $MASQUERADE_NOTOUT ! -d ${PREFIX6}/64 -j MASQUERADE || die
|
||||||
|
iptb 6 v filter I FORWARD -i ${SUBNET_IFACE} $IPTABLES_NAT_OUT -s ${PREFIX6}/64 -j ACCEPT || die
|
||||||
|
iptb 6 v filter I FORWARD -o ${SUBNET_IFACE} $IPTABLES_NAT_IN -d ${PREFIX6}/64 -j ACCEPT || die
|
||||||
}
|
}
|
||||||
|
|
||||||
start_ban_lan() {
|
start_ban_lan() {
|
||||||
echo
|
echo
|
||||||
echo "iptables: Disallow clients to access LAN"
|
echo "iptables: Disallow clients to access LAN"
|
||||||
iptables_ -N BANLAN-f-${SUBNET_IFACE} || die
|
iptb 4 n filter N lrt${$}${SUBNET_IFACE}-BLF || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 0.0.0.0/8 -j REJECT || die
|
# TODO: allow '--dhcp-dns(6)' address port 53, which can be something needed, e.g. a VPN's internal private IP
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 10.0.0.0/8 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 0.0.0.0/8 -j REJECT || die # TODO: use array
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 100.64.0.0/10 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 10.0.0.0/8 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 127.0.0.0/8 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 100.64.0.0/10 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 169.254.0.0/16 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 127.0.0.0/8 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 172.16.0.0/12 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 169.254.0.0/16 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 192.168.0.0/16 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 172.16.0.0/12 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 224.0.0.0/4 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 192.168.0.0/16 -j REJECT || die
|
||||||
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 255.255.255.255 -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 224.0.0.0/4 -j REJECT || die
|
||||||
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLF -d 255.255.255.255 -j REJECT || die
|
||||||
|
|
||||||
iptables_ -I FORWARD -i ${SUBNET_IFACE} -j BANLAN-f-${SUBNET_IFACE} || die
|
iptb 4 n filter I FORWARD -i ${SUBNET_IFACE} -j lrt${$}${SUBNET_IFACE}-BLF || die
|
||||||
|
|
||||||
iptables_ -N BANLAN-i-${SUBNET_IFACE}
|
iptb 4 n filter N lrt${$}${SUBNET_IFACE}-BLI || die
|
||||||
#iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} -j REJECT || die
|
iptb 4 v filter I lrt${$}${SUBNET_IFACE}-BLI -i ${SUBNET_IFACE} ! -p icmp -j REJECT || die # ipv6 need icmp to function. TODO: maybe we can block some unneeded icmp to improve security
|
||||||
iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} ! -p icmp -j REJECT || die
|
|
||||||
|
|
||||||
iptables_ -I INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE} || die
|
iptb 4 n filter I INPUT -i ${SUBNET_IFACE} -j lrt${$}${SUBNET_IFACE}-BLI || die
|
||||||
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -N BANLAN-f-${SUBNET_IFACE} || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d fc00::/7 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d fe80::/10 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d ff00::/8 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d ::1 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d ::/128 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d ::ffff:0:0/96 -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-f-${SUBNET_IFACE} -d ::ffff:0:0:0/96 -j REJECT || die
|
|
||||||
|
|
||||||
ip6tables_ -I FORWARD -i ${SUBNET_IFACE} -j BANLAN-f-${SUBNET_IFACE} || die
|
iptb 6 n filter N lrt${$}${SUBNET_IFACE}-BLF || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d fc00::/7 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d fe80::/10 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d ff00::/8 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d ::1 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d ::/128 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d ::ffff:0:0/96 -j REJECT || die
|
||||||
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLF -d ::ffff:0:0:0/96 -j REJECT || die
|
||||||
|
|
||||||
ip6tables_ -N BANLAN-i-${SUBNET_IFACE} || die
|
iptb 6 n filter I FORWARD -i ${SUBNET_IFACE} -j lrt${$}${SUBNET_IFACE}-BLF || die
|
||||||
#ip6tables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} -j REJECT || die
|
|
||||||
ip6tables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} ! -p icmpv6 -j REJECT || die
|
|
||||||
|
|
||||||
ip6tables_ -I INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE} || die
|
iptb 6 n filter N lrt${$}${SUBNET_IFACE}-BLI || die
|
||||||
fi
|
iptb 6 v filter I lrt${$}${SUBNET_IFACE}-BLI -i ${SUBNET_IFACE} ! -p icmpv6 -j REJECT || die
|
||||||
}
|
|
||||||
stop_ban_lan() {
|
|
||||||
echo "iptables: Unban clients' LAN access"
|
|
||||||
|
|
||||||
iptables_ -D FORWARD -i ${SUBNET_IFACE} -j BANLAN-f-${SUBNET_IFACE}
|
iptb 6 n filter I INPUT -i ${SUBNET_IFACE} -j lrt${$}${SUBNET_IFACE}-BLI || die
|
||||||
|
|
||||||
iptables_ -F BANLAN-f-${SUBNET_IFACE}
|
|
||||||
iptables_ -X BANLAN-f-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
iptables_ -F BANLAN-i-${SUBNET_IFACE}
|
|
||||||
iptables_ -X BANLAN-i-${SUBNET_IFACE}
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -D FORWARD -i ${SUBNET_IFACE} -j BANLAN-f-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
ip6tables_ -F BANLAN-f-${SUBNET_IFACE}
|
|
||||||
ip6tables_ -X BANLAN-f-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
ip6tables_ -F BANLAN-i-${SUBNET_IFACE}
|
|
||||||
ip6tables_ -X BANLAN-i-${SUBNET_IFACE}
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allow_dns_port() {
|
allow_dns_port() {
|
||||||
echo
|
echo
|
||||||
echo "iptables: allow DNS"
|
echo "iptables: allow DNS"
|
||||||
iptables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p tcp -m tcp --dport 53 -j ACCEPT || die
|
iptb 4 v filter I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p tcp -m tcp --dport 53 -j ACCEPT || die
|
||||||
iptables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p udp -m udp --dport 53 -j ACCEPT || die
|
iptb 4 v filter I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p udp -m udp --dport 53 -j ACCEPT || die
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
iptb 6 v filter I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j ACCEPT || die
|
||||||
ip6tables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j ACCEPT || die
|
iptb 6 v filter I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p udp -m udp --dport 53 -j ACCEPT || die
|
||||||
ip6tables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p udp -m udp --dport 53 -j ACCEPT || die
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
unallow_dns_port() {
|
|
||||||
echo "iptables: unallow DNS"
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p tcp -m tcp --dport 53 -j ACCEPT
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -d ${GATEWAY} -p udp -m udp --dport 53 -j ACCEPT
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j ACCEPT
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -d ${GATEWAY6} -p udp -m udp --dport 53 -j ACCEPT
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
start_catch_dns() {
|
start_catch_dns() {
|
||||||
echo
|
echo
|
||||||
echo "iptables: redirect all TCP/UDP packet that destination port is 53"
|
echo "iptables: redirect DNS queries to this host"
|
||||||
iptables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53 || die
|
iptb 4 v nat I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53 || die
|
||||||
iptables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53 || die
|
iptb 4 v nat I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53 || die
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53 || die
|
iptb 6 v nat I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53 || die
|
||||||
ip6tables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53 || die
|
iptb 6 v nat I PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53 || die
|
||||||
fi
|
|
||||||
}
|
|
||||||
stop_catch_dns() {
|
|
||||||
echo "iptables: stop redirecting DNS queries"
|
|
||||||
iptables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53
|
|
||||||
iptables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p udp -m udp --dport 53 -j REDIRECT --to-ports 53
|
|
||||||
ip6tables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} ! -d ${GATEWAY6} -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 53
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
allow_dhcp() {
|
allow_dhcp() {
|
||||||
echo
|
echo
|
||||||
echo "iptables: allow dhcp"
|
echo "iptables: allow dhcp"
|
||||||
iptables_ -v -I INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 67 -j ACCEPT || die
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
iptb 4 v filter I INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 67 -j ACCEPT || die
|
||||||
ip6tables_ -v -I INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 547 -j ACCEPT || die
|
iptb 6 v filter I INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 547 -j ACCEPT || die
|
||||||
fi
|
|
||||||
}
|
|
||||||
unallow_dhcp() {
|
|
||||||
echo "iptables: unallow dhcp"
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 67 -j ACCEPT
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -p udp -m udp --dport 547 -j ACCEPT
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: use 'DNAT' instead of '--to-ports' to support other IP
|
||||||
start_redsocks() {
|
start_redsocks() {
|
||||||
echo
|
echo
|
||||||
echo "iptables: transparent proxy non-LAN TCP/UDP traffic to port ${TP_PORT}"
|
echo "iptables: transparent proxy non-LAN TCP/UDP traffic to port ${TP_PORT}"
|
||||||
if [[ $NO4 -eq 0 ]]; then
|
if [[ $NO4 -eq 0 ]]; then
|
||||||
iptables_ -t nat -N REDSOCKS-${SUBNET_IFACE} || die
|
iptb 4 n nat N lrt${$}${SUBNET_IFACE}-TP || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 0.0.0.0/8 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 0.0.0.0/8 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 10.0.0.0/8 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 10.0.0.0/8 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 100.64.0.0/10 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 100.64.0.0/10 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 127.0.0.0/8 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 127.0.0.0/8 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 169.254.0.0/16 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 169.254.0.0/16 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 172.16.0.0/12 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 172.16.0.0/12 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 192.168.0.0/16 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 192.168.0.0/16 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 224.0.0.0/4 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 224.0.0.0/4 -j RETURN || die
|
||||||
iptables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d 255.255.255.255 -j RETURN || die
|
iptb 4 n nat A lrt${$}${SUBNET_IFACE}-TP -d 255.255.255.255 -j RETURN || die
|
||||||
|
|
||||||
iptables_ -v -t nat -A REDSOCKS-${SUBNET_IFACE} -p tcp -j REDIRECT --to-ports ${TP_PORT} || die
|
iptb 4 v nat A lrt${$}${SUBNET_IFACE}-TP -p tcp -j REDIRECT --to-ports ${TP_PORT} || die
|
||||||
iptables_ -v -t nat -A REDSOCKS-${SUBNET_IFACE} -p udp -j REDIRECT --to-ports ${TP_PORT} || die
|
iptb 4 v nat A lrt${$}${SUBNET_IFACE}-TP -p udp -j REDIRECT --to-ports ${TP_PORT} || die
|
||||||
|
|
||||||
iptables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -j REDSOCKS-${SUBNET_IFACE} || die
|
iptb 4 v nat I PREROUTING -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -j lrt${$}${SUBNET_IFACE}-TP || die
|
||||||
|
|
||||||
iptables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT || die
|
iptb 4 v filter I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT || die
|
||||||
iptables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p udp -m udp --dport ${TP_PORT} -j ACCEPT || die
|
iptb 4 v filter I INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p udp -m udp --dport ${TP_PORT} -j ACCEPT || die
|
||||||
fi
|
fi
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -t nat -N REDSOCKS-${SUBNET_IFACE} || die
|
|
||||||
ip6tables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d fc00::/7 -j RETURN || die
|
|
||||||
ip6tables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d fe80::/10 -j RETURN || die
|
|
||||||
ip6tables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d ff00::/8 -j RETURN || die
|
|
||||||
ip6tables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d ::1 -j RETURN || die
|
|
||||||
ip6tables_ -t nat -A REDSOCKS-${SUBNET_IFACE} -d :: -j RETURN || die
|
|
||||||
|
|
||||||
ip6tables_ -v -t nat -A REDSOCKS-${SUBNET_IFACE} -p tcp -j REDIRECT --to-ports ${TP_PORT} || die
|
iptb 6 n nat N lrt${$}${SUBNET_IFACE}-TP || die
|
||||||
ip6tables_ -v -t nat -A REDSOCKS-${SUBNET_IFACE} -p udp -j REDIRECT --to-ports ${TP_PORT} || die
|
iptb 6 n nat A lrt${$}${SUBNET_IFACE}-TP -d fc00::/7 -j RETURN || die
|
||||||
|
iptb 6 n nat A lrt${$}${SUBNET_IFACE}-TP -d fe80::/10 -j RETURN || die
|
||||||
|
iptb 6 n nat A lrt${$}${SUBNET_IFACE}-TP -d ff00::/8 -j RETURN || die
|
||||||
|
iptb 6 n nat A lrt${$}${SUBNET_IFACE}-TP -d ::1 -j RETURN || die
|
||||||
|
iptb 6 n nat A lrt${$}${SUBNET_IFACE}-TP -d :: -j RETURN || die
|
||||||
|
|
||||||
ip6tables_ -v -t nat -I PREROUTING -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -j REDSOCKS-${SUBNET_IFACE} || die
|
iptb 6 v nat A lrt${$}${SUBNET_IFACE}-TP -p tcp -j REDIRECT --to-ports ${TP_PORT} || die
|
||||||
|
iptb 6 v nat A lrt${$}${SUBNET_IFACE}-TP -p udp -j REDIRECT --to-ports ${TP_PORT} || die
|
||||||
|
|
||||||
|
iptb 6 v nat I PREROUTING -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -j lrt${$}${SUBNET_IFACE}-TP || die
|
||||||
|
|
||||||
|
iptb 6 v filter I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT || die
|
||||||
|
iptb 6 v filter I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p udp -m udp --dport ${TP_PORT} -j ACCEPT || die
|
||||||
|
|
||||||
ip6tables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT || die
|
|
||||||
ip6tables_ -v -I INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p udp -m udp --dport ${TP_PORT} -j ACCEPT || die
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
stop_redsocks() {
|
|
||||||
echo "iptables: stop transparent proxy"
|
|
||||||
if [[ $NO4 -eq 0 ]]; then
|
|
||||||
iptables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -j REDSOCKS-${SUBNET_IFACE}
|
|
||||||
iptables_ -t nat -F REDSOCKS-${SUBNET_IFACE}
|
|
||||||
iptables_ -t nat -X REDSOCKS-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT
|
|
||||||
iptables_ -D INPUT -i ${SUBNET_IFACE} -s ${GATEWAY%.*}.0/24 -p udp -m udp --dport ${TP_PORT} -j ACCEPT
|
|
||||||
fi
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
|
||||||
ip6tables_ -t nat -D PREROUTING -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -j REDSOCKS-${SUBNET_IFACE}
|
|
||||||
ip6tables_ -t nat -F REDSOCKS-${SUBNET_IFACE}
|
|
||||||
ip6tables_ -t nat -X REDSOCKS-${SUBNET_IFACE}
|
|
||||||
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p tcp -m tcp --dport ${TP_PORT} -j ACCEPT
|
|
||||||
ip6tables_ -D INPUT -i ${SUBNET_IFACE} -s ${PREFIX6}/64 -p udp -m udp --dport ${TP_PORT} -j ACCEPT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
#---------------------------------------
|
#---------------------------------------
|
||||||
backup_ipv6_bits() {
|
backup_ipv6_bits() {
|
||||||
mkdir "$CONFDIR/sys_6_conf_iface" || die "Failed making dir to save interface IPv6 status"
|
mkdir "$CONFDIR/sys_6_conf_iface" || die "Failed making dir to save interface IPv6 status"
|
||||||
@@ -1026,7 +1041,7 @@ backup_ipv6_bits() {
|
|||||||
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/accept_ra" \
|
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/accept_ra" \
|
||||||
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/use_tempaddr" \
|
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/use_tempaddr" \
|
||||||
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/addr_gen_mode" \
|
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/addr_gen_mode" \
|
||||||
"$CONFDIR/sys_6_conf_iface/" || die "Failed backking up interface ipv6 bits"
|
"$CONFDIR/sys_6_conf_iface/" || die "Failed backing up interface ipv6 bits"
|
||||||
|
|
||||||
if [[ "$SHARE_METHOD" == 'redsocks' ]] ; then
|
if [[ "$SHARE_METHOD" == 'redsocks' ]] ; then
|
||||||
cp "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/forwarding" \
|
cp "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/forwarding" \
|
||||||
@@ -1066,14 +1081,14 @@ backup_interface_status() {
|
|||||||
(ip link show ${SUBNET_IFACE} |grep -q "state UP") && SUBNET_IFACE_ORIGINAL_UP_STATUS=1
|
(ip link show ${SUBNET_IFACE} |grep -q "state UP") && SUBNET_IFACE_ORIGINAL_UP_STATUS=1
|
||||||
|
|
||||||
# save interface old mac
|
# save interface old mac
|
||||||
if [[ -n "$NEW_MACADDR" ]]; then
|
#if [[ -n "$NEW_MACADDR" ]]; then
|
||||||
OLD_MACADDR=$(get_interface_mac $SUBNET_IFACE)
|
OLD_MACADDR=$(get_interface_mac $SUBNET_IFACE)
|
||||||
echo "Saved ${SUBNET_IFACE} old MAC address ${OLD_MACADDR} into RAM"
|
#echo "Saved ${SUBNET_IFACE} old MAC address ${OLD_MACADDR} into RAM"
|
||||||
fi
|
#fi
|
||||||
|
|
||||||
backup_ipv6_bits
|
backup_ipv6_bits
|
||||||
|
|
||||||
# TODO : backup ip and others
|
# TODO : ? backup ip and others???
|
||||||
|
|
||||||
# nm managing status is saved when nm_set_unmanaged()
|
# nm managing status is saved when nm_set_unmanaged()
|
||||||
}
|
}
|
||||||
@@ -1084,9 +1099,9 @@ restore_interface_status() {
|
|||||||
|
|
||||||
restore_ipv6_bits
|
restore_ipv6_bits
|
||||||
|
|
||||||
if [[ -n "$OLD_MACADDR" ]] ; then
|
if [[ -n "$OLD_MACADDR" && "$(get_interface_mac $SUBNET_IFACE)" != "$OLD_MACADDR" ]] ; then
|
||||||
echo "Restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} ..."
|
echo "Restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} ..."
|
||||||
set_interface_mac ${SUBNET_IFACE} ${OLD_MACADDR} && echo "Successfully restored ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR}"
|
set_interface_mac ${SUBNET_IFACE} ${OLD_MACADDR} || echo "Failed restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR}" >&2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
nm_restore_manage
|
nm_restore_manage
|
||||||
@@ -1106,7 +1121,7 @@ kill_processes() { # for this instance
|
|||||||
pn=$( ps -p $pid -o comm= )
|
pn=$( ps -p $pid -o comm= )
|
||||||
#echo "Killing $pid $pn ... "
|
#echo "Killing $pid $pn ... "
|
||||||
pkill -P $pid
|
pkill -P $pid
|
||||||
kill $pid 2>/dev/null && ( echo "Killed $pid $pn" && rm $x ) || echo "Failed to kill $pid $pn, it may have exited"
|
kill $pid 2>/dev/null && ( echo "Killed $(basename $x) $pid $pn" && rm $x ) || echo "Failed to kill $(basename $x) $pid $pn, it may have exited"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
@@ -1143,25 +1158,9 @@ _cleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clean_iptables() {
|
clean_iptables() {
|
||||||
|
bash $CONFDIR/undo_iptables.sh
|
||||||
|
|
||||||
if [[ "$SHARE_METHOD" == "nat" ]]; then
|
[[ -f $CONFDIR/undo_iptables_2.sh ]] && bash $CONFDIR/undo_iptables_2.sh
|
||||||
stop_nat
|
|
||||||
elif [[ "$SHARE_METHOD" == "redsocks" ]]; then
|
|
||||||
stop_redsocks
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$DHCP_DNS" == "gateway" || "$DHCP_DNS6" == "gateway" ]]; then
|
|
||||||
unallow_dns_port
|
|
||||||
fi
|
|
||||||
|
|
||||||
[[ "$CATCH_DNS" -eq 1 ]] && stop_catch_dns
|
|
||||||
|
|
||||||
|
|
||||||
if [[ $NO_DNSMASQ -eq 0 ]]; then
|
|
||||||
unallow_dhcp
|
|
||||||
fi
|
|
||||||
|
|
||||||
[[ "$BANLAN" -eq 1 ]] && stop_ban_lan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
@@ -1170,16 +1169,19 @@ cleanup() {
|
|||||||
echo
|
echo
|
||||||
echo "Doing cleanup.. "
|
echo "Doing cleanup.. "
|
||||||
kill_processes
|
kill_processes
|
||||||
clean_iptables 2> /dev/null
|
echo "Undoing iptables changes .."
|
||||||
|
clean_iptables > /dev/null
|
||||||
_cleanup 2> /dev/null
|
_cleanup 2> /dev/null
|
||||||
|
|
||||||
pgid=$(ps opgid= $$ |awk '{print $1}' )
|
pgid=$(ps opgid= $$ |awk '{print $1}' )
|
||||||
|
echo "Killing PGID $pgid ..."
|
||||||
kill -15 -$pgid
|
kill -15 -$pgid
|
||||||
sleep 1
|
sleep 1
|
||||||
echo "Cleaning up done"
|
echo "Cleaning up done"
|
||||||
#kill -9 -$pgid
|
#kill -9 -$pgid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# NOTE function die() is designed not to be used before init_trap() executed
|
||||||
die() { # SIGUSR2
|
die() { # SIGUSR2
|
||||||
echo "Error occured"
|
echo "Error occured"
|
||||||
[[ -n "$1" ]] && echo -e "\nERROR: $1\n" >&2
|
[[ -n "$1" ]] && echo -e "\nERROR: $1\n" >&2
|
||||||
@@ -1204,7 +1206,7 @@ init_conf_dirs() {
|
|||||||
chmod 755 "$TMPDIR" 2>/dev/null
|
chmod 755 "$TMPDIR" 2>/dev/null
|
||||||
cd "$TMPDIR" || die "Couldn't change directory to linux-router's temporary path"
|
cd "$TMPDIR" || die "Couldn't change directory to linux-router's temporary path"
|
||||||
|
|
||||||
CONFDIR="$(mktemp -d $TMPDIR/lnxrouter.${TARGET_IFACE}.conf.XXX)" || die "Instance couldn't make config dir" # config dir for one instance
|
CONFDIR="$(mktemp -d $TMPDIR/lnxrouter.${TARGET_IFACE}.conf.XXXXXX)" || die "Instance couldn't make config dir" # config dir for one instance
|
||||||
chmod 755 "$CONFDIR"
|
chmod 755 "$CONFDIR"
|
||||||
#echo "Config dir: $CONFDIR"
|
#echo "Config dir: $CONFDIR"
|
||||||
echo $$ > "$CONFDIR/pid"
|
echo $$ > "$CONFDIR/pid"
|
||||||
@@ -1267,18 +1269,19 @@ print_clients_from_leases() { # MAC|IP|HOST|lease
|
|||||||
|
|
||||||
FILEC="$(cat "$LEASE_FILE" | grep -v -E "^duid\b" | sed -r '/^\s*$/d' )"
|
FILEC="$(cat "$LEASE_FILE" | grep -v -E "^duid\b" | sed -r '/^\s*$/d' )"
|
||||||
|
|
||||||
|
# TODO: duid is somewhat related to ipv6. I don't know about it. Not sure excluding it miss some info or not
|
||||||
echo "$FILEC" | while read line
|
echo "$FILEC" | while read line
|
||||||
do
|
do
|
||||||
#echo aa$line
|
#echo aa$line
|
||||||
LEASEstamp="$(echo "$line" | awk '{print $1}')"
|
LEASEstamp="$(echo "$line" | awk '{print $1}')"
|
||||||
MAC="$(echo "$line" | awk '{print $2}')"
|
MAC="$(echo "$line" | awk '{print $2}')"
|
||||||
IP="$(echo "$line" | awk '{print $3}' | sed 's/\[//g' | sed 's/\]//g')"
|
IP="$(echo "$line" | awk '{print $3}' | sed 's/\[//g' | sed 's/\]//g')"
|
||||||
HOST="$(echo "$line" | awk '{print $4}' | sed 's/*/?/g')"
|
HOST="$(echo "$line" | awk '{print $4}' | sed 's/*/?/g' | sed 's/|/_/g' | sed 's/ /_/g' )"
|
||||||
|
|
||||||
if [[ -n "$MAC" ]]; then
|
if [[ -n "$MAC" ]]; then
|
||||||
LEASEstr="$(date -d @${LEASEstamp} +%m-%d_%X)"
|
LEASEstr="$(date -d @${LEASEstamp} +%m-%d_%X)"
|
||||||
|
|
||||||
echo "$MAC|$IP|$HOST|$LEASEstr"
|
echo "$MAC|$IP|$HOST|lease_$LEASEstr"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -1303,7 +1306,7 @@ print_interface_neighbors_via_iproute() { # MAC|IP|_|STATUS
|
|||||||
MAC="?"
|
MAC="?"
|
||||||
STATUS="$(echo $line | awk -F'|' '$1="";$2="";$3="";$4="";{print}' | awk '{$1=$1;print}' | sed 's/ /,/g')"
|
STATUS="$(echo $line | awk -F'|' '$1="";$2="";$3="";$4="";{print}' | awk '{$1=$1;print}' | sed 's/ /,/g')"
|
||||||
fi
|
fi
|
||||||
if [[ -n "$IP" ]]; then
|
if [[ -n "$IP" && ( "$MAC" != "?" || "$STATUS" != "FAILED" ) ]]; then
|
||||||
echo "$MAC|$IP|?|$STATUS"
|
echo "$MAC|$IP|?|$STATUS"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -1315,12 +1318,12 @@ print_interface_neighbors_via_iw() { # MAC|_|_|signal
|
|||||||
do
|
do
|
||||||
if [[ -n "$MAC" ]]; then
|
if [[ -n "$MAC" ]]; then
|
||||||
SIGNAL="$(iw dev $IFACE station get $MAC | grep "signal:" | awk '{print $2}')"
|
SIGNAL="$(iw dev $IFACE station get $MAC | grep "signal:" | awk '{print $2}')"
|
||||||
echo "${MAC}|?|?|${SIGNAL} dBm"
|
echo "${MAC}|?|?|${SIGNAL}_dBm"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
list_clients() {
|
list_clients() { # passive mode. (use 'arp-scan' or 'netdiscover' if want active mode)
|
||||||
local IFACE pid
|
local IFACE pid
|
||||||
local CONFDIR
|
local CONFDIR
|
||||||
|
|
||||||
@@ -1336,7 +1339,7 @@ list_clients() {
|
|||||||
else # non-number given
|
else # non-number given
|
||||||
IFACE="$1"
|
IFACE="$1"
|
||||||
if ( ! is_interface $IFACE ) ; then
|
if ( ! is_interface $IFACE ) ; then
|
||||||
echo "'$IFACE' is not an interface" >&2
|
echo "'$IFACE' is not an interface or PID" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
pid=$(get_pid_from_subn_iface "$IFACE")
|
pid=$(get_pid_from_subn_iface "$IFACE")
|
||||||
@@ -1435,7 +1438,7 @@ check_other_functions(){
|
|||||||
|
|
||||||
##### root test ##### NOTE above don't require root ##########
|
##### root test ##### NOTE above don't require root ##########
|
||||||
if [[ $(id -u) -ne 0 ]]; then
|
if [[ $(id -u) -ne 0 ]]; then
|
||||||
echo "You must run it as root." >&2
|
echo "ERROR: Need root to continue" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
###### NOTE below require root ##########
|
###### NOTE below require root ##########
|
||||||
@@ -1459,6 +1462,19 @@ daemonizing_check(){
|
|||||||
|
|
||||||
#============================
|
#============================
|
||||||
check_wifi_settings() {
|
check_wifi_settings() {
|
||||||
|
|
||||||
|
if ! ( which iw > /dev/null 2>&1 && iw dev $WIFI_IFACE info > /dev/null 2>&1 ); then
|
||||||
|
echo "WARN: Can't use 'iw' to operate interfce '$WIFI_IFACE', trying 'iwconfig' (not as good as 'iw') ..." >&2
|
||||||
|
USE_IWCONFIG=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $USE_IWCONFIG -eq 1 ]]; then
|
||||||
|
if ! (which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1); then
|
||||||
|
echo "ERROR: Can't use 'iwconfig' to operate interfce '$WIFI_IFACE'" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
|
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
|
||||||
echo "ERROR: Invalid frequency band" >&2
|
echo "ERROR: Invalid frequency band" >&2
|
||||||
exit 1
|
exit 1
|
||||||
@@ -1547,8 +1563,8 @@ decide_target_interface() {
|
|||||||
elif [[ "$WIFI_IFACE" ]]; then
|
elif [[ "$WIFI_IFACE" ]]; then
|
||||||
echo "$WIFI_IFACE"
|
echo "$WIFI_IFACE"
|
||||||
else
|
else
|
||||||
die "No target interface specified"
|
echo "No target interface specified" >&2
|
||||||
exit 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1556,11 +1572,15 @@ decide_ip_addresses() {
|
|||||||
if [[ ! -n $GATEWAY ]]; then
|
if [[ ! -n $GATEWAY ]]; then
|
||||||
GATEWAY="$(generate_random_ip4)"
|
GATEWAY="$(generate_random_ip4)"
|
||||||
echo "Use random LAN IPv4 address $GATEWAY"
|
echo "Use random LAN IPv4 address $GATEWAY"
|
||||||
|
elif [[ ! "$GATEWAY" =~ "." ]]; then
|
||||||
|
GATEWAY="192.168.${GATEWAY}.1"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $IPV6 -eq 1 && ! -n $PREFIX6 ]]; then
|
if [[ $IPV6 -eq 1 && ! -n $PREFIX6 ]]; then
|
||||||
PREFIX6="$(generate_random_lan_ip6_prefix)"
|
PREFIX6="$(generate_random_lan_ip6_prefix)"
|
||||||
echo "Use random LAN IPv6 address ${PREFIX6}${IID6}"
|
echo "Use random LAN IPv6 address ${PREFIX6}${IID6}"
|
||||||
|
elif [[ ! "$PREFIX6" =~ ":" ]]; then
|
||||||
|
PREFIX6="fd00:0:0:${PREFIX6}::"
|
||||||
fi
|
fi
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
if [[ $IPV6 -eq 1 ]]; then
|
||||||
GATEWAY6="${PREFIX6}${IID6}"
|
GATEWAY6="${PREFIX6}${IID6}"
|
||||||
@@ -1595,8 +1615,9 @@ prepare_wifi_interface() {
|
|||||||
VWIFI_IFACE=$(alloc_new_vface_name)
|
VWIFI_IFACE=$(alloc_new_vface_name)
|
||||||
if iw dev ${WIFI_IFACE} interface add ${VWIFI_IFACE} type __ap; then
|
if iw dev ${WIFI_IFACE} interface add ${VWIFI_IFACE} type __ap; then
|
||||||
# Successfully created virtual wifi interface
|
# Successfully created virtual wifi interface
|
||||||
sleep 2 # wait for virtual interface MAC may change by system (but could be changed back by other programs)
|
# if NM running, it will give the new virtual interface a random MAC. MAC will go back after setting NM unmanaged
|
||||||
echo "${VWIFI_IFACE} created)"
|
sleep 2
|
||||||
|
echo "${VWIFI_IFACE} created"
|
||||||
else
|
else
|
||||||
VWIFI_IFACE=
|
VWIFI_IFACE=
|
||||||
die "Failed creating virtual WiFi interface. Maybe your WiFi adapter does not fully support virtual interfaces. Try again with '--no-virt'"
|
die "Failed creating virtual WiFi interface. Maybe your WiFi adapter does not fully support virtual interfaces. Try again with '--no-virt'"
|
||||||
@@ -1701,7 +1722,7 @@ write_hostapd_conf() {
|
|||||||
rsn_pairwise=CCMP
|
rsn_pairwise=CCMP
|
||||||
EOF
|
EOF
|
||||||
else
|
else
|
||||||
echo "WARN: Wifi is not protected by password" >&2
|
echo "WARN: WiFi is not protected by password" >&2
|
||||||
fi
|
fi
|
||||||
chmod 600 "$CONFDIR/hostapd.conf"
|
chmod 600 "$CONFDIR/hostapd.conf"
|
||||||
}
|
}
|
||||||
@@ -1712,6 +1733,11 @@ write_dnsmasq_conf() {
|
|||||||
else
|
else
|
||||||
NOBODY_GROUP="nogroup"
|
NOBODY_GROUP="nogroup"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
mkfifo "$CONFDIR/dnsmasq.log" || die "Failed creating pipe file for dnsmasq"
|
||||||
|
chown nobody "$CONFDIR/dnsmasq.log" || die "Failed changing dnsmasq log file owner"
|
||||||
|
cat "$CONFDIR/dnsmasq.log" &
|
||||||
|
|
||||||
cat <<- EOF > "$CONFDIR/dnsmasq.conf"
|
cat <<- EOF > "$CONFDIR/dnsmasq.conf"
|
||||||
user=nobody
|
user=nobody
|
||||||
group=$NOBODY_GROUP
|
group=$NOBODY_GROUP
|
||||||
@@ -1723,7 +1749,7 @@ write_dnsmasq_conf() {
|
|||||||
dhcp-range=${GATEWAY%.*}.10,${GATEWAY%.*}.250,255.255.255.0
|
dhcp-range=${GATEWAY%.*}.10,${GATEWAY%.*}.250,255.255.255.0
|
||||||
dhcp-option-force=option:router,${GATEWAY}
|
dhcp-option-force=option:router,${GATEWAY}
|
||||||
#log-dhcp
|
#log-dhcp
|
||||||
log-facility=/dev/stdout
|
log-facility=$CONFDIR/dnsmasq.log
|
||||||
bogus-priv
|
bogus-priv
|
||||||
domain-needed
|
domain-needed
|
||||||
EOF
|
EOF
|
||||||
@@ -1793,6 +1819,7 @@ run_wifi_ap_processes() {
|
|||||||
haveged_watchdog &
|
haveged_watchdog &
|
||||||
HAVEGED_WATCHDOG_PID=$!
|
HAVEGED_WATCHDOG_PID=$!
|
||||||
echo "$HAVEGED_WATCHDOG_PID" > "$CONFDIR/haveged_watchdog.pid"
|
echo "$HAVEGED_WATCHDOG_PID" > "$CONFDIR/haveged_watchdog.pid"
|
||||||
|
echo
|
||||||
echo "haveged_watchdog PID: $HAVEGED_WATCHDOG_PID"
|
echo "haveged_watchdog PID: $HAVEGED_WATCHDOG_PID"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -1805,6 +1832,11 @@ run_wifi_ap_processes() {
|
|||||||
fi
|
fi
|
||||||
echo
|
echo
|
||||||
echo "Starting hostapd"
|
echo "Starting hostapd"
|
||||||
|
|
||||||
|
if which complain > /dev/null 2>&1; then
|
||||||
|
complain hostapd
|
||||||
|
fi
|
||||||
|
|
||||||
# hostapd '-P' works only when use '-B' (run in background)
|
# hostapd '-P' works only when use '-B' (run in background)
|
||||||
$STDBUF_PATH hostapd $HOSTAPD_DEBUG_ARGS -P "$CONFDIR/hostapd.pid" "$CONFDIR/hostapd.conf" &
|
$STDBUF_PATH hostapd $HOSTAPD_DEBUG_ARGS -P "$CONFDIR/hostapd.pid" "$CONFDIR/hostapd.conf" &
|
||||||
HOSTAPD_PID=$!
|
HOSTAPD_PID=$!
|
||||||
@@ -1814,20 +1846,20 @@ run_wifi_ap_processes() {
|
|||||||
# sleep 1
|
# sleep 1
|
||||||
#done
|
#done
|
||||||
#echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
|
#echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
|
||||||
( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die "hostapd exited" ) &
|
pid_watchdog $HOSTAPD_PID 10 "hostapd failed" &
|
||||||
|
|
||||||
sleep 3
|
sleep 3
|
||||||
}
|
}
|
||||||
|
|
||||||
start_dnsmasq() {
|
start_dnsmasq() {
|
||||||
|
echo
|
||||||
|
echo "Starting dnsmasq"
|
||||||
|
|
||||||
if which complain > /dev/null 2>&1; then
|
if which complain > /dev/null 2>&1; then
|
||||||
# openSUSE's apparmor does not allow dnsmasq to read files.
|
# openSUSE's apparmor does not allow dnsmasq to read files.
|
||||||
# remove restriction.
|
# remove restriction.
|
||||||
complain dnsmasq
|
complain dnsmasq
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo
|
|
||||||
echo "Starting dnsmasq"
|
|
||||||
# Using '-d'(no daemon) dnsmasq will not turn into 'nobody'
|
# Using '-d'(no daemon) dnsmasq will not turn into 'nobody'
|
||||||
# '-x' works only when no '-d'
|
# '-x' works only when no '-d'
|
||||||
dnsmasq -k -C "$CONFDIR/dnsmasq.conf" -x "$CONFDIR/dnsmasq.pid" -l "$CONFDIR/dnsmasq.leases" &
|
dnsmasq -k -C "$CONFDIR/dnsmasq.conf" -x "$CONFDIR/dnsmasq.pid" -l "$CONFDIR/dnsmasq.leases" &
|
||||||
@@ -1838,9 +1870,10 @@ start_dnsmasq() {
|
|||||||
i=$((i + 1))
|
i=$((i + 1))
|
||||||
if [[ $i -gt 10 ]]; then die "Couldn't get dnsmasq PID" ; fi
|
if [[ $i -gt 10 ]]; then die "Couldn't get dnsmasq PID" ; fi
|
||||||
done
|
done
|
||||||
echo -n "dnsmasq PID: " ; cat "$CONFDIR/dnsmasq.pid"
|
DNSMASQ_PID="$(cat "$CONFDIR/dnsmasq.pid" )"
|
||||||
|
echo "dnsmasq PID: $DNSMASQ_PID"
|
||||||
######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child
|
######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child
|
||||||
( while [ -e "/proc/$DNSMASQ_PID" ]; do sleep 10; done ; die "dnsmasq exited" ) &
|
pid_watchdog $DNSMASQ_PID 9 "dnsmasq failed" &
|
||||||
sleep 2
|
sleep 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1888,10 +1921,15 @@ daemonizing_check
|
|||||||
|
|
||||||
## ===== Above don't echo anything if no warning or error====================
|
## ===== Above don't echo anything if no warning or error====================
|
||||||
## ========================================================
|
## ========================================================
|
||||||
|
phead
|
||||||
|
phead2
|
||||||
|
echo
|
||||||
|
|
||||||
echo "PID: $$"
|
echo "PID: $$"
|
||||||
|
|
||||||
TARGET_IFACE="$(decide_target_interface)" # judge wired (-i CONN_IFACE) or wireless hotspot (--ap $WIFI_IFACE)
|
TARGET_IFACE="$(decide_target_interface)" || exit 1 # judge wired (-i CONN_IFACE) or wireless hotspot (--ap $WIFI_IFACE)
|
||||||
echo "Target interface is ${TARGET_IFACE}"
|
echo "Target interface is ${TARGET_IFACE} ($(get_interface_mac $TARGET_IFACE))"
|
||||||
|
# TODO: show interface type, device model and pci/usb id (hwdata pci.ids), current driver
|
||||||
|
|
||||||
if [[ "$MAC_USE_RANDOM" -eq 1 ]] ; then
|
if [[ "$MAC_USE_RANDOM" -eq 1 ]] ; then
|
||||||
NEW_MACADDR="$(generate_random_mac)"
|
NEW_MACADDR="$(generate_random_mac)"
|
||||||
@@ -1907,6 +1945,7 @@ decide_ip_addresses # ip 4 & 6 lan addresses
|
|||||||
#==== begin to do some change on config files and system===
|
#==== begin to do some change on config files and system===
|
||||||
|
|
||||||
init_trap
|
init_trap
|
||||||
|
# NOTE function die() is designed not to be used before init_trap() executed
|
||||||
|
|
||||||
init_conf_dirs # CONFDIR , COMMON_CONFDIR . make dir
|
init_conf_dirs # CONFDIR , COMMON_CONFDIR . make dir
|
||||||
|
|
||||||
@@ -1947,7 +1986,7 @@ ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down
|
|||||||
# flush old IPs of subnet interface
|
# flush old IPs of subnet interface
|
||||||
ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP"
|
ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP"
|
||||||
|
|
||||||
dealwith_mac
|
dealwith_mac # setting MAC should be after setting NM unmanaged
|
||||||
|
|
||||||
[[ $WIFI_IFACE ]] && check_rfkill_unblock_wifi
|
[[ $WIFI_IFACE ]] && check_rfkill_unblock_wifi
|
||||||
|
|
||||||
@@ -1967,25 +2006,35 @@ if [[ $IPV6 -eq 1 ]] ; then
|
|||||||
ip -6 addr add ${GATEWAY6}/64 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv6 address"
|
ip -6 addr add ${GATEWAY6}/64 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv6 address"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
check_iptables
|
||||||
|
|
||||||
|
echo "NOTICE: Not showing all operations done to iptables rules"
|
||||||
|
|
||||||
|
[[ "$NO4" -eq 1 ]] && echo -e "\nWARN: Since you're using in this mode (no IPv4 Internet), make sure you've read Notice 1\n" >&2
|
||||||
|
|
||||||
# enable Internet sharing
|
# enable Internet sharing
|
||||||
if [[ "$SHARE_METHOD" == "none" ]]; then
|
if [[ "$SHARE_METHOD" == "none" ]]; then
|
||||||
|
|
||||||
echo "No Internet sharing"
|
echo "No Internet sharing"
|
||||||
|
|
||||||
|
echo -e "\nWARN: Since you're using in this mode (no Internet share), make sure you've read Notice 1\n" >&2
|
||||||
|
|
||||||
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
||||||
|
|
||||||
elif [[ "$SHARE_METHOD" == "nat" ]]; then
|
elif [[ "$SHARE_METHOD" == "nat" ]]; then
|
||||||
|
|
||||||
[[ "$INTERNET_IFACE" && "$dnsmasq_NO_DNS" -eq 0 ]] && echo -e "\nWARN: You specified Internet interface but this host is providing local DNS, queries may leak to other interfaces!!!\n" >&2
|
[[ "$INTERNET_IFACE" ]] && echo -e "\nWARN: Since you're using in this mode (specify Internet interface), make sure you've read Notice 1\n" >&2
|
||||||
|
|
||||||
|
[[ "$INTERNET_IFACE" && "$dnsmasq_NO_DNS" -eq 0 ]] && echo -e "\nWARN: You specified Internet interface but this host is providing local DNS. In some unexpected case (eg. mistaken configurations), queries may leak to other interfaces, which you should be aware of.\n" >&2
|
||||||
|
|
||||||
start_nat
|
start_nat
|
||||||
|
|
||||||
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
||||||
|
|
||||||
echo 1 > "/proc/sys/net/ipv4/ip_forward" || die "Failed enabling system ipv4 forwarding"
|
echo 1 > "/proc/sys/net/ipv4/ip_forward" || die "Failed enabling system ipv4 forwarding" # TODO maybe uneeded in '--no4' mode
|
||||||
|
|
||||||
if [[ $IPV6 -eq 1 ]]; then
|
if [[ $IPV6 -eq 1 ]]; then
|
||||||
echo 1 > "/proc/sys/net/ipv6/conf/all/forwarding" || die "Failed enabling system ipv6 forwarding"
|
echo 1 > "/proc/sys/net/ipv6/conf/all/forwarding" || die "Failed enabling system ipv6 forwarding" # TODO if '-o' used, set only 2 interfaces' bits
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# to enable clients to establish PPTP connections we must
|
# to enable clients to establish PPTP connections we must
|
||||||
@@ -1998,7 +2047,7 @@ elif [[ "$SHARE_METHOD" == "redsocks" ]]; then
|
|||||||
echo 1 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/forwarding" || die "Failed enabling $SUBNET_IFACE ipv6 forwarding" # to set NA router bit
|
echo 1 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/forwarding" || die "Failed enabling $SUBNET_IFACE ipv6 forwarding" # to set NA router bit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ "$dnsmasq_NO_DNS" -eq 0 && ! $DNS ]] && echo -e "\nWARN: You are using in transparent proxy mode but this host is providing local DNS, this may cause privacy leak !!!\n" >&2
|
[[ "$dnsmasq_NO_DNS" -eq 0 && ! $DNS ]] && echo -e "\nWARN: You are using in transparent proxy mode but this host is providing local DNS. In some unexpected case (eg. mistaken configurations), queries may leak to other interfaces, which you should be aware of.\n" >&2
|
||||||
|
|
||||||
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
[[ "$BANLAN" -eq 1 ]] && start_ban_lan
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user