2 # Copyright (C) 2016 Ian Kelling
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
25 usage: ${0##*/} [-h] [-t 2|3|test|client] [-m WIRELESS_MAC]
26 setup my router in general: dhcp, dns, etc.
28 Type 2 or 3 is for setting up a backup device, there are two kinds so
29 that you can switch the main device to a backup, then a backup to the
30 main. Type test is for setting up a testing device.
32 Passing an empty string for WIRELESS_MAC, or if none is defined in the
33 secrets file, will cause the device's native mac to be used.
45 if [[ -e /root
/router-secrets
]]; then
47 source /root
/router-secrets
49 rmac
=$
(cat /sys
/class
/net
/eth
0/address
)
51 hostname
=${rhost[$rmac]}
57 firewall_restart
=false
63 while getopts hm
:t
: opt
; do
70 libremanage_host
=$hostname
86 *) echo "$0: unexpected arg to -t: $*" >&2; usage
1 ;;
90 *) echo "$0: Internal error! unexpected args: $*" >&2 ; usage
1 ;;
93 shift "$((OPTIND-1))" # Discard the options and sentinel --
95 if [[ ! $mac ]] && ! $test && $secrets; then
96 # if we wanted to increment it
97 #mac=${mac:0: -1}$((${mac: -1} + 2))
101 if (( $# != 0 )); then
115 # background: upgrading all packages is not recommended because it
116 # doesn't go into the firmware. build new firmware if you want
117 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
118 f
=(/var
/opkg-lists
/*)
119 if ! (( $
(date -r $f +%s
) + 60*60*24 > $
(date +%s
) )); then
120 if ! opkg update
; then
121 echo "$0: warning: opkg update failed" >&2
132 if [[ ! $
(opkg list-installed
"$pname") ]]; then
137 if [[ $to_install ]]; then
138 opkg
install ${to_install[@]}
147 ######### uci example:#######
148 # # https://wiki.openwrt.org/doc/uci
149 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
150 # wan="firewall.@zone[$wan_index]"
151 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
153 # v uci set firewall.@forwarding[0].dest=$forward_dest
154 # uci commit firewall
155 # firewall_restart=true
157 ####### end uci example #####
160 printf "+ uset %s\n" "$*"
163 local service
="${key%%.*}"
164 restart_var
=${service}_restart
165 if [[ ! ${!restart_var} ]]; then
166 eval $restart_var=false
168 if [[ $
(uci get
"$key") != "$val" ]]; then
169 v uci
set "$key"="$val"
171 eval $restart_var=true
176 printf "+ udel %s\n" "$*"
179 local service
="${key%%.*}"
180 restart_var
=${service}_restart
181 if [[ ! ${!restart_var} ]]; then
182 eval $restart_var=false
184 if uci get
"$key" &>/dev
/null
; then
185 v uci
set "$key"="$val"
187 eval $restart_var=true
198 elif [[ $hostname == cmc
]]; then
216 : ${key:=pictionary49}
222 passwd
-l root ||
: #already locked fails
224 sed -ibak '/^root:/d' /etc
/shadow
225 # /root/router created by manually running passwd then copying the resulting
226 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
227 cat /root
/router
>>/etc
/shadow
228 # otherwise, serial console gets root login with no password
229 uset system.@system
[0].ttylogin
1
233 cat >/usr
/bin
/archlike-pxe-mount
<<'EOFOUTER'
235 # symlinks are collapsed for nfs mount points, so use a bind mount.
236 # tried putting this in /etc/config/fstab,
237 # then doing block mount, it didn't work. This doesn't persist across reboots,
238 # todo: figure that out
240 for d in /run/{arch,parabola}iso/bootmnt; do
241 cat >>/etc/fstab <<EOF
242 /mnt/usb/tftpboot $d none bind 0 0
244 mount | grep $d &>/dev/null || mount $d
246 /etc/init.d/nfsd restart
248 chmod +x
/usr
/bin
/archlike-pxe-mount
250 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc
/passwd
254 uset dropbear.@dropbear
[0].PasswordAuth
0
255 uset dropbear.@dropbear
[0].RootPasswordAuth
0
256 uset dropbear.@dropbear
[0].Port
2220
257 if ! cmp -s /root
/dropbear_rsa_host_key
/etc
/dropbear
/dropbear_rsa_host_key
; then
258 cp /root
/dropbear_rsa_host_key
/etc
/dropbear
/dropbear_rsa_host_key
259 dropbear_restart
=true
262 if $dropbear_restart; then
263 v
/etc
/init.d
/dropbear restart
267 uset network.lan.ipaddr
$l.
$lanip
268 uset network.lan.netmask
$mask
269 if $dev2 ||
$client; then
271 uset network.lan.gateway
$l.1
272 uset network.wan.proto none
273 uset network.wan6.proto none
275 /etc
/init.d
/dnsmasq stop
276 /etc
/init.d
/dnsmasq disable
277 /etc
/init.d
/odhcpd stop
278 /etc
/init.d
/odhcpd disable
279 rm -f /etc
/resolv.conf
280 cat >/etc
/resolv.conf
<<'EOF'
285 # things i tried to keep dnsmasq running but not enabled except local dns,
286 # but it didnt work right and i dont need it anyways.
287 # uset dhcp.wan.ignore $dev2 # default is false
288 # uset dhcp.lan.ignore $dev2 # default is false
289 # uset dhcp.@dnsmasq[0].interface lo
290 # uset dhcp.@dnsmasq[0].localuse 0
291 # uset dhcp.@dnsmasq[0].resolvfile /etc/dnsmasq.conf
292 # uset dhcp.@dnsmasq[0].noresolv 1
293 # todo: populate /etc/resolv.conf with a static value
296 # these are the defaults
297 uset network.lan.gateway
''
298 uset network.wan.proto dhcp
299 uset network.wan6.proto dhcpv6
300 /etc
/init.d
/dnsmasq start
301 # todo: figure out why this returns 1
302 /etc
/init.d
/dnsmasq
enable ||
:
303 /etc
/init.d
/odhcpd start
304 /etc
/init.d
/odhcpd
enable
307 wireless_restart
=false
310 uset wireless.default_radio0.network
'wwan'
311 uset wireless.default_radio0.ssid
${rclientssid[$rmac]}
312 uset wireless.default_radio0.encryption
'psk2'
313 uset wireless.default_radio0.device
'radio0'
314 uset wireless.default_radio0.mode
'sta'
315 uset wireless.default_radio0.bssid
${rclientbssid[$rmac]}
316 # todo: look into whether 5g network is available.
317 uset wireless.default_radio0.key
${rclientkey[$rmac]}
318 uset wireless.radio0.disabled false
319 uset wireless.radio1.disabled true
321 # defaults, just reseting in case client config ran
322 uset wireless.default_radio0.mode ap
324 uset wireless.default_radio
$x.ssid
"$ssid"
325 uset wireless.default_radio
$x.key
$key
326 uset wireless.default_radio
$x.encryption psk2
328 uset wireless.default_radio
$x.macaddr
$macpre$
((macsuf
+ 2*x
))
330 # secondary device has wireless disabled
331 uset wireless.radio
$x.disabled
$dev2
339 # usb, screen, relay are for libremanage
342 # relay package temporarily disabled
343 # /root/relay_1.0-1_mips_24kc.ipk
344 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
345 tcpdump openvpn-openssl adblock libusb-compat \
346 screen kmod-usb-serial-cp210x kmod-usb-serial-ftdi rsync
348 cat >/etc
/libremanage.conf
<<EOF
349 ${libremanage_host}_type=switch
350 ${libremanage_host}_channel=1
355 v
/etc
/init.d
/fstab
enable ||
:
357 # rebooting makes mounting work, but comparing lsmod,
358 # i'm guessing this will too. todo, test it.
359 # 255 == module already loaded
360 for mod
in scsi_mod sd_mod
; do v modprobe
$mod ||
[[ $?
== 255 ]]; done
362 # for archlike pxe. The default settings in the installer expect to find
363 # the NFS at one of these dirs
364 mkdir
-p /run
/archiso
/bootmnt
365 mkdir
-p /run
/parabolaiso
/bootmnt
367 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
368 # that is the case after running this or rebooting.
369 # wiki says safe to do in case of fstab changes:
371 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
372 # echo | cedit /etc/config/fstab ||:
373 v cedit
/etc
/config
/fstab
<<EOF || { v block umount; v block mount; }
374 config global automount
379 # /overlay is an / overlay mount for installing extra packages, etc.
380 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
381 option target /mnt/usb
382 # option target /overlay
383 option device /dev/sda1
385 option options rw,async,noatime,nodiratime
390 # ian: disabled because afaik I don't need it, no benefit.
391 # config global autoswap
392 # option from_fstab 1
396 # option device /dev/sda1
402 # exportfs -ra wont cut it when its the same path, but now a bind mount
403 # todo: restart nfs when nfs is enabled?
404 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
405 cedit
/etc
/exports
<<EOF ||:
406 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
408 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
409 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
413 # todo: enable nfs when we need it only.
414 # v /etc/init.d/portmap start
415 # v /etc/init.d/nfsd start
416 # v /etc/init.d/portmap enable
417 # v /etc/init.d/nfsd enable
426 ########## openvpn exampl
427 ########## missing firewall settings for routing lan
429 # v /etc/init.d/openvpn start
430 # v /etc/init.d/openvpn enable
432 # # from https://wiki.openwrt.org/doc/uci/firewall
433 # # todo: not sure if /etc/init.d/network needs restarting.
434 # # I did, and I had to restart the vpn afterwards.
435 # # This maps a uci interface to a real interface which is
436 # # managed outside of uci.
437 # v cedit /etc/config/network <<'EOF' ||:
438 # config interface 'tun0'
439 # option ifname 'tun0'
440 # option proto 'none'
442 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
443 # config openvpn my_client_config
445 # option config /etc/openvpn/client.conf
452 network_restart
=false
454 v cedit wific
/etc
/config
/network
<<EOF || network_restart=true
455 # https://openwrt.org/docs/guide-user/network/wifi/connect_client_wifi
456 config interface 'wwan'
461 v cedit
/etc
/config
/network
<<EOF || network_restart=true
462 config 'route' 'transmission'
463 option 'interface' 'lan'
464 option 'target' '10.173.0.0'
465 option 'netmask' '255.255.0.0'
466 option 'gateway' '$l.3'
468 option interface 'wg0'
469 option proto 'wireguard'
470 option private_key '$(cat /root/wg.key)'
471 option listen_port $wgport
472 list addresses '10.3.0.1/24'
473 list addresses 'fdfd::1/64'
476 config wireguard_wg0 'wgclient'
477 option public_key '3q+WJGrm85r59NgeXOIvppxoW4ux/+koSw6Fee1c1TI='
478 option preshared_key '$(cat /root/wg.psk)'
479 list allowed_ips '10.3.0.2/24'
480 list allowed_ips 'fdfd::2/64'
483 if $network_restart; then
484 v
/etc
/init.d
/network reload
490 v cedit wific
/etc
/config
/firewall
<<EOF
495 option forward REJECT
504 v cedit
host /etc
/config
/firewall
<<EOF
514 v cedit
host /etc
/config
/firewall
<<EOF
525 v cedit
/etc
/config
/firewall
<<EOF
534 option src_dport 2202
541 option dest_port 2202
546 option src_dport 2205
553 option dest_port 2205
558 option src_dport 2207
565 option dest_port 2207
570 option src_dport 2208
577 option dest_port 2208
582 option src_dport 2209
589 option dest_port 2209
594 option src_dport 8000
595 option dest_port 8000
601 option dest_port 8000
607 option dest_port 2220
613 option dest_port $wgport
644 option name syncthing
646 option src_dport 22001
652 option dest_port 22001
659 # note, using mac transform, we could allow all traffic to a host like this,
660 # replacing 1 as appropriate
661 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
667 option name http-ipv6
675 option name https-ipv6
683 option name node-exporter
686 option dest_port 9101
691 option name mail587-ipv6
700 firewall-cedit || firewall_restart
=true
702 # not using wireguard for now
703 # if ! uci get firewall.@zone[1].network | grep wg0 &>/dev/null; then
704 # # cant mix cedit plus uci
705 # echo | cedit /etc/config/firewall ||:
706 # uci add_list firewall.@zone[1].network=wg0
707 # uci commit firewall
709 # firewall_restart=true
714 v cedit
/etc
/hosts
<<EOF || dnsmasq_restart=true
717 # not sure this case statement is needed
720 v cedit
host /etc
/hosts
<<EOF || dnsmasq_restart=true
727 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
728 # if [[ $mail_host ]]; then
729 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
733 uset dhcp.@dnsmasq
[0].domain b8.nz
734 uset system.@system
[0].hostname
$hostname
735 uset dhcp.@dnsmasq
[0].
local
737 # uci doesnt seem to have a way to set an empty value,
738 # if you delete it, it goes back to the default. this seems
739 # to be a decent workaround.
740 # todo: setup /etc/resolv.conf to point to 127.0.0.1
741 uset dhcp.@dnsmasq
[0].resolvfile
=/dev
/null
743 # disabled for now. i want to selectively enable it
744 # for specific hosts.
745 if [[ $
(uci get adblock.global.adb_enabled
) != 0 ]]; then
746 v uci
set adblock.global.adb_enabled
=0
748 /etc
/init.d
/adblock restart
750 # https://github.com/openwrt/packages/tree/master/net/adblock/files
751 cat >/etc
/crontabs
/root
<<'EOF'
752 0 06 * * * /etc/init.d/adblock reload
756 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
758 # sometimes /mnt/usb fails, cuz it's just a flash drive,
759 # so make sure we have this dir or else dnsmasq will fail
761 mkdir
-p /mnt
/usb
/tftpboot
762 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
764 ptr-record=1.0.2.10.in-addr.arpa.,cmc.b8.nz
765 ptr-record=2.0.2.10.in-addr.arpa.,kd.b8.nz
766 ptr-record=4.0.2.10.in-addr.arpa.,wrt2.b8.nz
767 ptr-record=5.0.2.10.in-addr.arpa.,x2.b8.nz
768 ptr-record=6.0.2.10.in-addr.arpa.,xw2.b8.nz
769 ptr-record=7.0.2.10.in-addr.arpa.,rp.b8.nz
770 ptr-record=8.0.2.10.in-addr.arpa.,tp.b8.nz
771 ptr-record=9.0.2.10.in-addr.arpa.,bb8.b8.nz
772 ptr-record=12.0.2.10.in-addr.arpa.,demohost.b8.nz
773 ptr-record=14.0.2.10.in-addr.arpa.,wrt3.b8.nz
774 ptr-record=19.0.2.10.in-addr.arpa.,brother.b8.nz
775 ptr-record=25.0.2.10.in-addr.arpa.,hp.b8.nz
776 ptr-record=.0.2.10.in-addr.arpa.,transmission.b8.nz
778 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
780 rebind-domain-ok=b8.nz
782 # this says the ip of default gateway and dns server,
783 # but I think they are unneded and default
789 # results from googling around dnsmasq optimizations
790 # about 50k in memory. router has 62 megs.
791 # in a browsing session, I probably won't ever do 5000 lookups
792 # before the ttl expiration or whatever does expiration.
795 # ask all servers, use the one which responds first.
796 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
799 # namebench benchmarks dns servers. google's dns was only
800 # slightly less fast than some others, and I trust it more
801 # to give accurate results, stay relatively fast, and
802 # not do anythin too malicious, so just use that.
803 # download namebench and run it like this:
804 # for x in all regional isp global preferred nearby; do ./namebench.py -s \$x -c US -i firefox -m weighted -J 10 -w; echo \$x; hr; done
808 server=2001:4860:4860::8888
809 server=2001:4860:4860::8844
812 # to fixup existin ips, on the client you can do
813 # sudo dhclient -r; sudo dhclient <interface-name>
815 # default dhcp range is 100-150
816 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
817 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
818 # top port, iPXE (PCI 04:00.0) in seabios boot menu
819 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
820 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
821 # 4 is reserved for a staticly configured host wrt2
822 # old x2 with bad fan
823 #dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
824 dhcp-host=f0:de:f1:81:ec:88,set:x2,$l.5,x2
825 dhcp-host=c4:8e:8f:44:f5:63,set:x2w,$l.6,x2w
826 # This is so fai can have an explicit name to use for testing,
827 # or else any random machine which did a pxe boot would get
828 # reformatted. The mac is from doing a virt-install, cancelling it,
829 # and copying the generated mac, so it should be randomish.
830 dhcp-host=fa:08:f8:4c:14:1c,set:tp,$l.7,rp
831 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
832 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.12,demohost
833 dhcp-host=62:03:cb:a8:3e:a3,set:trp,$1.13,trp
834 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.18,x3
835 # BRN001BA98CA823 in dhcp logs
836 dhcp-host=00:1b:a9:8c:a8:23,set:tp,$l.19,brother
837 dhcp-host=38:63:bb:07:5a:f9,set:tp,$l.25,hp
838 dhcp-host=00:26:b6:f6:0f:e9,set:frodow,$l.28,frodow
842 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
844 # This is the ip it picks by default if dhcp fails,
845 # so might as well use it.
846 # hostname is the name it uses according to telnet
847 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
852 # Just leave the tftp server up even if we aren't doing pxe boot.
853 # It has no sensitive info.
855 tftp-root=/mnt/usb/tftpboot
856 dhcp-optsfile=/etc/dnsmasq-dhcpopts.conf
862 if $dnsmasq_restart && ! $dev2; then
863 # todo: can our ptr records be put in /etc/hosts?
864 # eg: user normal /etc/hosts records, and they wont be used for A resolution
865 # due to the other settings, but will be used for ptr? then maybe
866 # we dont have to restart dnsmasq for a dns update?
868 # todo: according to this
869 # https://www.redpill-linpro.com/techblog/2019/08/27/evaluating-local-dnssec-validators.html#toggling-dnssec-validation-1
870 # we should turn on dnssec validation when wrt gets version > 2.80. currently at 2.80.
871 # todo: download https://downloads.openwrt.org/snapshots/packages/mipsel_24kc/base/dnsmasq_2.84~~test3-1_mipsel_24kc.ipk
872 # and install it. then we can turn off dnssec in systemd-resolved
874 # Also, reload of dnsmasq seems to break things, wifi
875 # clients were not getting internet connectivity.
876 v
/etc
/init.d
/dnsmasq restart
879 if $firewall_restart; then
880 v
/etc
/init.d
/firewall restart
883 # this may just restart the network and take care of the network_restart below.
884 if $wireless_restart; then
888 # todo: we should catch errors and still run this if needed
889 if $network_restart; then