cjdns.defaults 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/sh
  2. # if there is an existing config, our work is already done
  3. uci get cjdns.cjdns.ipv6 >/dev/null 2>&1
  4. if [ $? -ne 0 ]; then
  5. # register commit handler
  6. uci -q batch <<-EOF >/dev/null
  7. delete ucitrack.@cjdns[-1]
  8. add ucitrack cjdns
  9. set ucitrack.@cjdns[-1].init=cjdns
  10. commit ucitrack
  11. EOF
  12. # generate configuration
  13. touch /etc/config/cjdns
  14. cjdroute --genconf | cjdroute --cleanconf | cjdrouteconf set
  15. # make sure config is present (might fail for any reason)
  16. uci get cjdns.cjdns.ipv6 >/dev/null 2>&1
  17. if [ $? -ne 0 ]; then
  18. exit 1
  19. fi
  20. # enable auto-peering on ethernet interface lan, if existing
  21. uci get network.lan | grep interface >/dev/null 2>&1
  22. if [ $? -eq 0 ]; then
  23. uci get network.lan.type | grep bridge >/dev/null 2>&1
  24. if [ $? -eq 0 ]; then
  25. # most routers will set up an ethernet bridge for the lan
  26. ifname="br-lan"
  27. else
  28. # docker containers don't have permission to create bridges by default,
  29. # so we bind to the underlying interface instead (likely eth0)
  30. ifname=`uci get network.lan.ifname`
  31. fi
  32. uci -q batch <<-EOF >/dev/null
  33. add cjdns eth_interface
  34. set cjdns.@eth_interface[-1].beacon=2
  35. set cjdns.@eth_interface[-1].bind=$ifname
  36. EOF
  37. fi
  38. # set the tun interface name
  39. uci set cjdns.cjdns.tun_device=tuncjdns
  40. # create the network interface
  41. uci -q batch <<-EOF >/dev/null
  42. set network.cjdns=interface
  43. set network.cjdns.ifname=tuncjdns
  44. set network.cjdns.proto=none
  45. EOF
  46. # firewall rules by @dangowrt -- thanks <3
  47. # create the firewall zone
  48. uci -q batch <<-EOF >/dev/null
  49. add firewall zone
  50. set firewall.@zone[-1].name=cjdns
  51. add_list firewall.@zone[-1].network=cjdns
  52. set firewall.@zone[-1].input=REJECT
  53. set firewall.@zone[-1].output=ACCEPT
  54. set firewall.@zone[-1].forward=REJECT
  55. set firewall.@zone[-1].conntrack=1
  56. set firewall.@zone[-1].family=ipv6
  57. EOF
  58. # allow ICMP from cjdns zone, e.g. ping6
  59. uci -q batch <<-EOF >/dev/null
  60. add firewall rule
  61. set firewall.@rule[-1].name='Allow-ICMPv6-cjdns'
  62. set firewall.@rule[-1].src=cjdns
  63. set firewall.@rule[-1].proto=icmp
  64. add_list firewall.@rule[-1].icmp_type=echo-request
  65. add_list firewall.@rule[-1].icmp_type=echo-reply
  66. add_list firewall.@rule[-1].icmp_type=destination-unreachable
  67. add_list firewall.@rule[-1].icmp_type=packet-too-big
  68. add_list firewall.@rule[-1].icmp_type=time-exceeded
  69. add_list firewall.@rule[-1].icmp_type=bad-header
  70. add_list firewall.@rule[-1].icmp_type=unknown-header-type
  71. set firewall.@rule[-1].limit='1000/sec'
  72. set firewall.@rule[-1].family=ipv6
  73. set firewall.@rule[-1].target=ACCEPT
  74. EOF
  75. # allow SSH from cjdns zone, needs to be explicitly enabled
  76. uci -q batch <<-EOF >/dev/null
  77. add firewall rule
  78. set firewall.@rule[-1].enabled=0
  79. set firewall.@rule[-1].name='Allow-SSH-cjdns'
  80. set firewall.@rule[-1].src=cjdns
  81. set firewall.@rule[-1].proto=tcp
  82. set firewall.@rule[-1].dest_port=22
  83. set firewall.@rule[-1].target=ACCEPT
  84. EOF
  85. # allow LuCI access from cjdns zone, needs to be explicitly enabled
  86. uci -q batch <<-EOF >/dev/null
  87. add firewall rule
  88. set firewall.@rule[-1].enabled=0
  89. set firewall.@rule[-1].name='Allow-HTTP-cjdns'
  90. set firewall.@rule[-1].src=cjdns
  91. set firewall.@rule[-1].proto=tcp
  92. set firewall.@rule[-1].dest_port=80
  93. set firewall.@rule[-1].target=ACCEPT
  94. EOF
  95. # allow UDP peering from wan zone, if it exists
  96. uci show network.wan >/dev/null 2>&1
  97. if [ $? -eq 0 ]; then
  98. peeringPort=`uci get cjdns.@udp_interface[0].port`
  99. uci -q batch <<-EOF >/dev/null
  100. add firewall rule
  101. set firewall.@rule[-1].name='Allow-cjdns-wan'
  102. set firewall.@rule[-1].src=wan
  103. set firewall.@rule[-1].proto=udp
  104. set firewall.@rule[-1].dest_port=$peeringPort
  105. set firewall.@rule[-1].target=ACCEPT
  106. EOF
  107. fi
  108. uci commit cjdns
  109. uci commit firewall
  110. uci commit network
  111. fi
  112. exit 0