siitwizard.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local uci = require "luci.model.uci".cursor()
  5. local bit = require "nixio".bit
  6. local ip = require "luci.ip"
  7. -------------------- Init --------------------
  8. --
  9. -- Find link-local address
  10. --
  11. function find_ll()
  12. local _, r
  13. for _, r in ipairs(ip.routes({ family = 6, dest = "fe80::/64" })) do
  14. if r.dest:higher("fe80:0:0:0:ff:fe00:0:0") then
  15. return (r.dest - "fe80::")
  16. end
  17. end
  18. return ip.IPv6("::")
  19. end
  20. --
  21. -- Determine defaults
  22. --
  23. local ula_prefix = uci:get("siit", "ipv6", "ula_prefix") or "fd00::"
  24. local ula_global = uci:get("siit", "ipv6", "ula_global") or "00ca:ffee:babe::" -- = Freifunk
  25. local ula_subnet = uci:get("siit", "ipv6", "ula_subnet") or "0000:0000:0000:4223::" -- = Berlin
  26. local siit_prefix = uci:get("siit", "ipv6", "siit_prefix") or "::ffff:0000:0000"
  27. local ipv4_pool = uci:get("siit", "ipv4", "pool") or "172.16.0.0/12"
  28. local ipv4_netsz = uci:get("siit", "ipv4", "netsize") or "24"
  29. --
  30. -- Find IPv4 allocation pool
  31. --
  32. local gv4_net = ip.IPv4(ipv4_pool)
  33. --
  34. -- Generate ULA
  35. --
  36. local ula = ip.IPv6("::/64")
  37. for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
  38. ula = ula:add(ip.IPv6(prefix))
  39. end
  40. ula = ula:add(find_ll())
  41. -------------------- View --------------------
  42. f = SimpleForm("siitwizward", "SIIT-Wizzard",
  43. "This wizzard helps to setup SIIT (IPv4-over-IPv6) translation according to RFC2765.")
  44. f:field(DummyValue, "info_ula", "Mesh ULA address").value = ula:string()
  45. f:field(DummyValue, "ipv4_pool", "IPv4 allocation pool").value =
  46. "%s (%i hosts)" %{ gv4_net:string(), 2 ^ ( 32 - gv4_net:prefix() ) - 2 }
  47. f:field(DummyValue, "ipv4_size", "IPv4 LAN network prefix").value =
  48. "%i bit (%i hosts)" %{ ipv4_netsz, 2 ^ ( 32 - ipv4_netsz ) - 2 }
  49. mode = f:field(ListValue, "mode", "Operation mode")
  50. mode:value("client", "Client")
  51. mode:value("gateway", "Gateway")
  52. dev = f:field(ListValue, "device", "Wireless device")
  53. uci:foreach("wireless", "wifi-device",
  54. function(section)
  55. dev:value(section[".name"])
  56. end)
  57. lanip = f:field(Value, "ipaddr", "LAN IPv4 subnet")
  58. function lanip.formvalue(self, section)
  59. local val = self.map:formvalue(self:cbid(section))
  60. local net = ip.IPv4("%s/%i" %{ val, ipv4_netsz })
  61. if net then
  62. if gv4_net:contains(net) then
  63. if not net:minhost():equal(net:host()) then
  64. self.error = { [section] = true }
  65. f.errmessage = "IPv4 address is not the first host of " ..
  66. "subnet, expected " .. net:minhost():string()
  67. end
  68. else
  69. self.error = { [section] = true }
  70. f.errmessage = "IPv4 address is not within the allocation pool"
  71. end
  72. else
  73. self.error = { [section] = true }
  74. f.errmessage = "Invalid IPv4 address given"
  75. end
  76. return val
  77. end
  78. dns = f:field(Value, "dns", "DNS server for LAN clients")
  79. dns.value = "141.1.1.1"
  80. -------------------- Control --------------------
  81. function f.handle(self, state, data)
  82. if state == FORM_VALID then
  83. luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
  84. return false
  85. end
  86. return true
  87. end
  88. function mode.write(self, section, value)
  89. --
  90. -- Find LAN IPv4 range
  91. --
  92. local lan_net = ip.IPv4(
  93. ( lanip:formvalue(section) or "172.16.0.1" ) .. "/" .. ipv4_netsz
  94. )
  95. if not lan_net then return end
  96. --
  97. -- Find wifi interface, dns server and hostname
  98. --
  99. local device = dev:formvalue(section)
  100. local dns_server = dns:formvalue(section) or "141.1.1.1"
  101. local hostname = "siit-" .. lan_net:host():string():gsub("%.","-")
  102. --
  103. -- Configure wifi device
  104. --
  105. local wifi_device = dev:formvalue(section)
  106. local wifi_essid = uci:get("siit", "wifi", "essid") or "6mesh.freifunk.net"
  107. local wifi_bssid = uci:get("siit", "wifi", "bssid") or "02:ca:ff:ee:ba:be"
  108. local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
  109. -- nuke old device definition
  110. uci:delete_all("wireless", "wifi-iface",
  111. function(s) return s.device == wifi_device end )
  112. uci:delete_all("network", "interface",
  113. function(s) return s['.name'] == wifi_device end )
  114. -- create wifi device definition
  115. uci:tset("wireless", wifi_device, {
  116. disabled = 0,
  117. channel = wifi_channel,
  118. -- txantenna = 1,
  119. -- rxantenna = 1,
  120. -- diversity = 0
  121. })
  122. uci:section("wireless", "wifi-iface", nil, {
  123. encryption = "none",
  124. mode = "adhoc",
  125. txpower = 10,
  126. sw_merge = 1,
  127. network = wifi_device,
  128. device = wifi_device,
  129. ssid = wifi_essid,
  130. bssid = wifi_bssid,
  131. })
  132. --
  133. -- Gateway mode
  134. --
  135. -- * wan port is dhcp, lan port is 172.23.1.1/24
  136. -- * siit0 gets a dummy address: 169.254.42.42
  137. -- * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
  138. -- * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
  139. -- * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
  140. -- * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
  141. if value == "gateway" then
  142. -- wan mtu
  143. uci:set("network", "wan", "mtu", 1240)
  144. -- lan settings
  145. uci:tset("network", "lan", {
  146. mtu = 1240,
  147. ipaddr = lan_net:host():string(),
  148. netmask = lan_net:mask():string(),
  149. proto = "static"
  150. })
  151. -- use full siit subnet
  152. siit_route = ip.IPv6(siit_prefix .. "/96")
  153. -- v4 <-> siit route
  154. uci:delete_all("network", "route",
  155. function(s) return s.interface == "siit0" end)
  156. uci:section("network", "route", nil, {
  157. interface = "siit0",
  158. target = gv4_net:network():string(),
  159. netmask = gv4_net:mask():string()
  160. })
  161. --
  162. -- Client mode
  163. --
  164. -- * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
  165. -- * we do a ::ffff:ffff:172.13.2.0/120 to siit0, because in this case, only traffic directed to clients needs to go into translation.
  166. -- * same route as HNA6 announcement to catch the traffic out of the mesh.
  167. -- * Also, MTU on LAN reduced to 1400.
  168. else
  169. -- lan settings
  170. uci:tset("network", "lan", {
  171. mtu = 1240,
  172. ipaddr = lan_net:host():string(),
  173. netmask = lan_net:mask():string()
  174. })
  175. -- derive siit subnet from lan config
  176. siit_route = ip.IPv6(
  177. siit_prefix .. "/" .. (96 + lan_net:prefix())
  178. ):add(lan_net[2])
  179. -- ipv4 <-> siit route
  180. uci:delete_all("network", "route",
  181. function(s) return s.interface == "siit0" end)
  182. -- XXX: kind of a catch all, gv4_net would be better
  183. -- but does not cover non-local v4 space
  184. uci:section("network", "route", nil, {
  185. interface = "siit0",
  186. target = "0.0.0.0",
  187. netmask = "0.0.0.0"
  188. })
  189. end
  190. -- setup the firewall
  191. uci:delete_all("firewall", "zone",
  192. function(s) return (
  193. s['.name'] == "siit0" or s.name == "siit0" or
  194. s.network == "siit0" or s['.name'] == wifi_device or
  195. s.name == wifi_device or s.network == wifi_device
  196. ) end)
  197. uci:delete_all("firewall", "forwarding",
  198. function(s) return (
  199. s.src == wifi_device and s.dest == "siit0" or
  200. s.dest == wifi_device and s.src == "siit0" or
  201. s.src == "lan" and s.dest == "siit0" or
  202. s.dest == "lan" and s.src == "siit0"
  203. ) end)
  204. uci:section("firewall", "zone", "siit0", {
  205. name = "siit0",
  206. network = "siit0",
  207. input = "ACCEPT",
  208. output = "ACCEPT",
  209. forward = "ACCEPT"
  210. })
  211. uci:section("firewall", "zone", wifi_device, {
  212. name = wifi_device,
  213. network = wifi_device,
  214. input = "ACCEPT",
  215. output = "ACCEPT",
  216. forward = "ACCEPT"
  217. })
  218. uci:section("firewall", "forwarding", nil, {
  219. src = wifi_device,
  220. dest = "siit0"
  221. })
  222. uci:section("firewall", "forwarding", nil, {
  223. src = "siit0",
  224. dest = wifi_device
  225. })
  226. uci:section("firewall", "forwarding", nil, {
  227. src = "lan",
  228. dest = "siit0"
  229. })
  230. uci:section("firewall", "forwarding", nil, {
  231. src = "siit0",
  232. dest = "lan"
  233. })
  234. -- firewall include
  235. uci:delete_all("firewall", "include",
  236. function(s) return s.path == "/etc/firewall.user" end)
  237. uci:section("firewall", "include", nil, {
  238. path = "/etc/firewall.user"
  239. })
  240. -- siit0 interface
  241. uci:delete_all("network", "interface",
  242. function(s) return ( s.ifname == "siit0" ) end)
  243. uci:section("network", "interface", "siit0", {
  244. ifname = "siit0",
  245. proto = "none"
  246. })
  247. -- siit0 route
  248. uci:delete_all("network", "route6",
  249. function(s) return siit_route:contains(ip.IPv6(s.target)) end)
  250. uci:section("network", "route6", nil, {
  251. interface = "siit0",
  252. target = siit_route:string()
  253. })
  254. -- create wifi network interface
  255. uci:section("network", "interface", wifi_device, {
  256. proto = "static",
  257. mtu = 1400,
  258. ip6addr = ula:string()
  259. })
  260. -- nuke old olsrd interfaces
  261. uci:delete_all("olsrd", "Interface",
  262. function(s) return s.interface == wifi_device end)
  263. -- configure olsrd interface
  264. uci:foreach("olsrd", "olsrd",
  265. function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
  266. uci:section("olsrd", "Interface", nil, {
  267. ignore = 0,
  268. interface = wifi_device,
  269. Ip6AddrType = "unique-local"
  270. })
  271. -- hna6
  272. uci:delete_all("olsrd", "Hna6",
  273. function(s) return true end)
  274. uci:section("olsrd", "Hna6", nil, {
  275. netaddr = siit_route:host():string(),
  276. prefix = siit_route:prefix()
  277. })
  278. -- txtinfo v6 & olsrd nameservice
  279. uci:foreach("olsrd", "LoadPlugin",
  280. function(s)
  281. if s.library == "olsrd_txtinfo.so.0.1" then
  282. uci:set("olsrd", s['.name'], "accept", "::1")
  283. elseif s.library == "olsrd_nameservice.so.0.3" then
  284. uci:set("olsrd", s['.name'], "name", hostname)
  285. end
  286. end)
  287. -- lan dns
  288. uci:tset("dhcp", "lan", {
  289. dhcp_option = "6," .. dns_server,
  290. start = bit.band(lan_net:minhost():add(1)[2][2], 0xFF),
  291. limit = ( 2 ^ ( 32 - lan_net:prefix() ) ) - 3
  292. })
  293. -- hostname
  294. uci:foreach("system", "system",
  295. function(s)
  296. uci:set("system", s['.name'], "hostname", hostname)
  297. end)
  298. uci:save("wireless")
  299. uci:save("firewall")
  300. uci:save("network")
  301. uci:save("system")
  302. uci:save("olsrd")
  303. uci:save("dhcp")
  304. end
  305. return f