1
1
#! /bin/bash
2
2
3
- # TODO: showing version or git commit on running
3
+ # TODO: showing version ( or git commit) on running
4
4
VERSION=0.6.0
5
5
PROGNAME=" $( basename $0 ) "
6
6
@@ -30,12 +30,14 @@ Options:
30
30
--ban-priv Disallow clients to access my private network
31
31
32
32
-g <ip> This host's IPv4 address in subnet (mask is /24)
33
+ (example: '192.168.5.1' or '5' shortly)
33
34
-6 Enable IPv6 (NAT)
34
35
--no4 Disable IPv4 Internet (not forwarding IPv4)
35
36
(See Notice 1). Usually used with '-6'
36
37
37
38
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
38
- (example: fd00:1:2:3::) Using this enables '-6'
39
+ (example: 'fd00:0:0:5::' or '5' shortly)
40
+ Using this enables '-6'
39
41
40
42
--dns <ip>|<port>|<ip:port>
41
43
DNS server's upstream DNS.
@@ -201,6 +203,7 @@ define_global_variables(){
201
203
CONFDIR=
202
204
NM_RUNNING=0
203
205
NM_UNM_LIST= # it's called "list" but for now one interface
206
+ XT_COMMENT=1
204
207
}
205
208
206
209
parse_user_options (){
@@ -736,7 +739,7 @@ haveged_watchdog() {
736
739
echo " WARN: Low entropy detected. We recommend you to install \` haveged'" 1>&2
737
740
show_warn=0
738
741
fi
739
- elif ! pidof haveged > /dev/null 2>&1 ; then
742
+ elif ! pidof haveged > /dev/null 2>&1 ; then # TODO judge zombie ?
740
743
echo " Low entropy detected, starting haveged" 1>&2
741
744
# boost low-entropy
742
745
haveged -w 1024 -p $COMMON_CONFDIR /haveged.pid
@@ -745,7 +748,24 @@ haveged_watchdog() {
745
748
sleep 2
746
749
done
747
750
}
748
-
751
+ pid_watchdog () {
752
+ local PID=" $1 "
753
+ local SLEEP=" $2 "
754
+ local ERR_MSG=" $3 "
755
+ local ST
756
+ while true
757
+ do
758
+ if [[ -e " /proc/$PID " ]]; then
759
+ ST=" $( cat " /proc/$PID /status" | grep " ^State:" | awk ' {print $2}' ) "
760
+ if [[ " $ST " != ' Z' ]]; then
761
+ sleep $SLEEP
762
+ continue
763
+ fi
764
+ fi
765
+ die " $ERR_MSG "
766
+ done
767
+
768
+ }
749
769
# ========
750
770
751
771
@@ -792,15 +812,22 @@ nm_restore_manage() {
792
812
fi
793
813
}
794
814
# =========
795
-
796
815
iptables_ ()
797
816
{
798
- iptables -w $@ -m comment --comment " lnxrouter-$$ -$SUBNET_IFACE "
817
+ if [[ $XT_COMMENT -eq 1 ]]; then
818
+ iptables -w $@ -m comment --comment " lnxrouter-$$ -$SUBNET_IFACE "
819
+ else
820
+ iptables -w $@
821
+ fi
799
822
return $?
800
823
}
801
824
ip6tables_ ()
802
825
{
803
- ip6tables -w $@ -m comment --comment " lnxrouter-$$ -$SUBNET_IFACE "
826
+ if [[ $XT_COMMENT -eq 1 ]]; then
827
+ ip6tables -w $@ -m comment --comment " lnxrouter-$$ -$SUBNET_IFACE "
828
+ else
829
+ ip6tables -w $@
830
+ fi
804
831
return $?
805
832
}
806
833
@@ -843,7 +870,7 @@ start_ban_lan() {
843
870
echo
844
871
echo " iptables: Disallow clients to access LAN"
845
872
iptables_ -N BANLAN-f-${SUBNET_IFACE} || die
846
- iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 0.0.0.0/8 -j REJECT || die
873
+ iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 0.0.0.0/8 -j REJECT || die # TODO: use array
847
874
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 10.0.0.0/8 -j REJECT || die
848
875
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 100.64.0.0/10 -j REJECT || die
849
876
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 127.0.0.0/8 -j REJECT || die
@@ -858,6 +885,7 @@ start_ban_lan() {
858
885
iptables_ -N BANLAN-i-${SUBNET_IFACE}
859
886
# iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} -j REJECT || die
860
887
iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} ! -p icmp -j REJECT || die
888
+ # TODO: ipv6 need icmp to function. maybe we can block some unneeded icmp to improve security
861
889
862
890
iptables_ -I INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE} || die
863
891
@@ -1465,12 +1493,13 @@ daemonizing_check(){
1465
1493
check_wifi_settings () {
1466
1494
1467
1495
if ! ( which iw > /dev/null 2>&1 && iw dev $WIFI_IFACE info > /dev/null 2>&1 ); then
1468
- echo " WARN: Can't use 'iw' to operation this WiFi interface, trying 'iwconfig' ..." >&2
1469
- if which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1 ; then
1470
- USE_IWCONFIG=1
1471
- echo " WARN: Using 'iwconfig', not as good as 'iw'" >&2
1472
- else
1473
- echo " ERROR: Can't use 'iwconfig' to operation this WiFi interface neither" >&2
1496
+ echo " WARN: Can't use 'iw' to operate interfce '$WIFI_IFACE ', trying 'iwconfig' (not as good as 'iw') ..." >&2
1497
+ USE_IWCONFIG=1
1498
+ fi
1499
+
1500
+ if [[ $USE_IWCONFIG -eq 1 ]]; then
1501
+ if ! (which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1 ); then
1502
+ echo " ERROR: Can't use 'iwconfig' to operate interfce '$WIFI_IFACE '" >&2
1474
1503
exit 1
1475
1504
fi
1476
1505
fi
@@ -1572,11 +1601,15 @@ decide_ip_addresses() {
1572
1601
if [[ ! -n $GATEWAY ]]; then
1573
1602
GATEWAY=" $( generate_random_ip4) "
1574
1603
echo " Use random LAN IPv4 address $GATEWAY "
1604
+ elif [[ ! " $GATEWAY " =~ " ." ]]; then
1605
+ GATEWAY=" 192.168.${GATEWAY} .1"
1575
1606
fi
1576
1607
1577
1608
if [[ $IPV6 -eq 1 && ! -n $PREFIX6 ]]; then
1578
1609
PREFIX6=" $( generate_random_lan_ip6_prefix) "
1579
1610
echo " Use random LAN IPv6 address ${PREFIX6}${IID6} "
1611
+ elif [[ ! " $PREFIX6 " =~ " :" ]]; then
1612
+ PREFIX6=" fd00:0:0:${PREFIX6} ::"
1580
1613
fi
1581
1614
if [[ $IPV6 -eq 1 ]]; then
1582
1615
GATEWAY6=" ${PREFIX6}${IID6} "
@@ -1729,6 +1762,11 @@ write_dnsmasq_conf() {
1729
1762
else
1730
1763
NOBODY_GROUP=" nogroup"
1731
1764
fi
1765
+
1766
+ mkfifo " $CONFDIR /dnsmasq.log" || die " Failed creating pipe file for dnsmasq"
1767
+ chown nobody " $CONFDIR /dnsmasq.log" || die " Failed changing dnsmasq log file owner"
1768
+ cat " $CONFDIR /dnsmasq.log" &
1769
+
1732
1770
cat << - EOF > "$CONFDIR /dnsmasq.conf"
1733
1771
user=nobody
1734
1772
group=$NOBODY_GROUP
@@ -1740,7 +1778,7 @@ write_dnsmasq_conf() {
1740
1778
dhcp-range=${GATEWAY% .* } .10,${GATEWAY% .* } .250,255.255.255.0
1741
1779
dhcp-option-force=option:router,${GATEWAY}
1742
1780
#log-dhcp
1743
- log-facility=/dev/stdout
1781
+ log-facility=$CONFDIR /dnsmasq.log
1744
1782
bogus-priv
1745
1783
domain-needed
1746
1784
EOF
@@ -1831,8 +1869,7 @@ run_wifi_ap_processes() {
1831
1869
# sleep 1
1832
1870
# done
1833
1871
# echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
1834
- ( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die " hostapd exited" ) &
1835
-
1872
+ pid_watchdog $HOSTAPD_PID 10 " hostapd failed" &
1836
1873
sleep 3
1837
1874
}
1838
1875
@@ -1855,9 +1892,10 @@ start_dnsmasq() {
1855
1892
i=$(( i + 1 ))
1856
1893
if [[ $i -gt 10 ]]; then die " Couldn't get dnsmasq PID" ; fi
1857
1894
done
1858
- echo -n " dnsmasq PID: " ; cat " $CONFDIR /dnsmasq.pid"
1895
+ DNSMASQ_PID=" $( cat " $CONFDIR /dnsmasq.pid" ) "
1896
+ echo " dnsmasq PID: $DNSMASQ_PID "
1859
1897
# #####(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child
1860
- ( while [ -e " /proc/ $DNSMASQ_PID " ] ; do sleep 10 ; done ; die " dnsmasq exited " ) &
1898
+ pid_watchdog $DNSMASQ_PID 9 " dnsmasq failed " &
1861
1899
sleep 2
1862
1900
}
1863
1901
@@ -1986,6 +2024,18 @@ if [[ $IPV6 -eq 1 ]] ; then
1986
2024
ip -6 addr add ${GATEWAY6} /64 dev ${SUBNET_IFACE} || die " Failed setting ${SUBNET_IFACE} IPv6 address"
1987
2025
fi
1988
2026
2027
+ function check_iptables() {
2028
+ if iptables --version | grep " nf_tables" > /dev/null 2>&1 ; then
2029
+ echo -e " \nWARN: Your system is using nftables. This script is tested with iptables legacy only. If you encounter problems, visit following URL for infomation:\n https://github.com/oblique/create_ap/issues/373\n https://github.com/oblique/create_ap/issues/433\n https://github.com/garywill/linux-router/issues/18\n" >&2
2030
+ fi
2031
+
2032
+ if ! lsmod | grep -E " \bxt_comment\b" > /dev/null 2>&1 ; then
2033
+ XT_COMMENT=0
2034
+ fi
2035
+ }
2036
+
2037
+ check_iptables
2038
+
1989
2039
# enable Internet sharing
1990
2040
if [[ " $SHARE_METHOD " == " none" ]]; then
1991
2041
0 commit comments