1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 local uci = require "luci.model.uci".cursor()
6 local bit = require "nixio".bit
8 -------------------- Init --------------------
11 -- Find link-local address
13 LL_PREFIX = luci.ip.IPv6("fe80::/64")
15 for _, r in ipairs(luci.sys.net.routes6()) do
16 if LL_PREFIX:contains(r.dest) and r.dest:higher(LL_PREFIX) then
17 return r.dest:sub(LL_PREFIX)
20 return luci.ip.IPv6("::")
26 local ula_prefix = uci:get("siit", "ipv6", "ula_prefix") or "fd00::"
27 local ula_global = uci:get("siit", "ipv6", "ula_global") or "00ca:ffee:babe::" -- = Freifunk
28 local ula_subnet = uci:get("siit", "ipv6", "ula_subnet") or "0000:0000:0000:4223::" -- = Berlin
29 local siit_prefix = uci:get("siit", "ipv6", "siit_prefix") or "::ffff:0000:0000"
30 local ipv4_pool = uci:get("siit", "ipv4", "pool") or "172.16.0.0/12"
31 local ipv4_netsz = uci:get("siit", "ipv4", "netsize") or "24"
34 -- Find IPv4 allocation pool
36 local gv4_net = luci.ip.IPv4(ipv4_pool)
41 local ula = luci.ip.IPv6("::/64")
43 for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
44 ula = ula:add(luci.ip.IPv6(prefix))
47 ula = ula:add(find_ll())
50 -------------------- View --------------------
51 f = SimpleForm("siitwizward", "SIIT-Wizzard",
52 "This wizzard helps to setup SIIT (IPv4-over-IPv6) translation according to RFC2765.")
54 f:field(DummyValue, "info_ula", "Mesh ULA address").value = ula:string()
56 f:field(DummyValue, "ipv4_pool", "IPv4 allocation pool").value =
57 "%s (%i hosts)" %{ gv4_net:string(), 2 ^ ( 32 - gv4_net:prefix() ) - 2 }
59 f:field(DummyValue, "ipv4_size", "IPv4 LAN network prefix").value =
60 "%i bit (%i hosts)" %{ ipv4_netsz, 2 ^ ( 32 - ipv4_netsz ) - 2 }
62 mode = f:field(ListValue, "mode", "Operation mode")
63 mode:value("client", "Client")
64 mode:value("gateway", "Gateway")
66 dev = f:field(ListValue, "device", "Wireless device")
67 uci:foreach("wireless", "wifi-device",
69 dev:value(section[".name"])
72 lanip = f:field(Value, "ipaddr", "LAN IPv4 subnet")
73 function lanip.formvalue(self, section)
74 local val = self.map:formvalue(self:cbid(section))
75 local net = luci.ip.IPv4("%s/%i" %{ val, ipv4_netsz })
78 if gv4_net:contains(net) then
79 if not net:minhost():equal(net:host()) then
80 self.error = { [section] = true }
81 f.errmessage = "IPv4 address is not the first host of " ..
82 "subnet, expected " .. net:minhost():string()
85 self.error = { [section] = true }
86 f.errmessage = "IPv4 address is not within the allocation pool"
89 self.error = { [section] = true }
90 f.errmessage = "Invalid IPv4 address given"
96 dns = f:field(Value, "dns", "DNS server for LAN clients")
97 dns.value = "141.1.1.1"
99 -------------------- Control --------------------
100 function f.handle(self, state, data)
101 if state == FORM_VALID then
102 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
108 function mode.write(self, section, value)
111 -- Find LAN IPv4 range
113 local lan_net = luci.ip.IPv4(
114 ( lanip:formvalue(section) or "172.16.0.1" ) .. "/" .. ipv4_netsz
117 if not lan_net then return end
120 -- Find wifi interface, dns server and hostname
122 local device = dev:formvalue(section)
123 local dns_server = dns:formvalue(section) or "141.1.1.1"
124 local hostname = "siit-" .. lan_net:host():string():gsub("%.","-")
127 -- Configure wifi device
129 local wifi_device = dev:formvalue(section)
130 local wifi_essid = uci:get("siit", "wifi", "essid") or "6mesh.freifunk.net"
131 local wifi_bssid = uci:get("siit", "wifi", "bssid") or "02:ca:ff:ee:ba:be"
132 local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
134 -- nuke old device definition
135 uci:delete_all("wireless", "wifi-iface",
136 function(s) return s.device == wifi_device end )
138 uci:delete_all("network", "interface",
139 function(s) return s['.name'] == wifi_device end )
141 -- create wifi device definition
142 uci:tset("wireless", wifi_device, {
144 channel = wifi_channel,
150 uci:section("wireless", "wifi-iface", nil, {
155 network = wifi_device,
156 device = wifi_device,
164 -- * wan port is dhcp, lan port is 172.23.1.1/24
165 -- * siit0 gets a dummy address: 169.254.42.42
166 -- * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
167 -- * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
168 -- * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
169 -- * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
171 if value == "gateway" then
174 uci:set("network", "wan", "mtu", 1240)
177 uci:tset("network", "lan", {
179 ipaddr = lan_net:host():string(),
180 netmask = lan_net:mask():string(),
184 -- use full siit subnet
185 siit_route = luci.ip.IPv6(siit_prefix .. "/96")
188 uci:delete_all("network", "route",
189 function(s) return s.interface == "siit0" end)
191 uci:section("network", "route", nil, {
193 target = gv4_net:network():string(),
194 netmask = gv4_net:mask():string()
200 -- * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
201 -- * 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.
202 -- * same route as HNA6 announcement to catch the traffic out of the mesh.
203 -- * Also, MTU on LAN reduced to 1400.
208 uci:tset("network", "lan", {
210 ipaddr = lan_net:host():string(),
211 netmask = lan_net:mask():string()
214 -- derive siit subnet from lan config
215 siit_route = luci.ip.IPv6(
216 siit_prefix .. "/" .. (96 + lan_net:prefix())
219 -- ipv4 <-> siit route
220 uci:delete_all("network", "route",
221 function(s) return s.interface == "siit0" end)
223 -- XXX: kind of a catch all, gv4_net would be better
224 -- but does not cover non-local v4 space
225 uci:section("network", "route", nil, {
232 -- setup the firewall
233 uci:delete_all("firewall", "zone",
235 s['.name'] == "siit0" or s.name == "siit0" or
236 s.network == "siit0" or s['.name'] == wifi_device or
237 s.name == wifi_device or s.network == wifi_device
240 uci:delete_all("firewall", "forwarding",
242 s.src == wifi_device and s.dest == "siit0" or
243 s.dest == wifi_device and s.src == "siit0" or
244 s.src == "lan" and s.dest == "siit0" or
245 s.dest == "lan" and s.src == "siit0"
248 uci:section("firewall", "zone", "siit0", {
256 uci:section("firewall", "zone", wifi_device, {
258 network = wifi_device,
264 uci:section("firewall", "forwarding", nil, {
269 uci:section("firewall", "forwarding", nil, {
274 uci:section("firewall", "forwarding", nil, {
279 uci:section("firewall", "forwarding", nil, {
285 uci:delete_all("firewall", "include",
286 function(s) return s.path == "/etc/firewall.user" end)
288 uci:section("firewall", "include", nil, {
289 path = "/etc/firewall.user"
294 uci:delete_all("network", "interface",
295 function(s) return ( s.ifname == "siit0" ) end)
297 uci:section("network", "interface", "siit0", {
303 uci:delete_all("network", "route6",
304 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
306 uci:section("network", "route6", nil, {
308 target = siit_route:string()
311 -- create wifi network interface
312 uci:section("network", "interface", wifi_device, {
315 ip6addr = ula:string()
318 -- nuke old olsrd interfaces
319 uci:delete_all("olsrd", "Interface",
320 function(s) return s.interface == wifi_device end)
322 -- configure olsrd interface
323 uci:foreach("olsrd", "olsrd",
324 function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
326 uci:section("olsrd", "Interface", nil, {
328 interface = wifi_device,
329 Ip6AddrType = "unique-local"
333 uci:delete_all("olsrd", "Hna6",
334 function(s) return true end)
336 uci:section("olsrd", "Hna6", nil, {
337 netaddr = siit_route:host():string(),
338 prefix = siit_route:prefix()
341 -- txtinfo v6 & olsrd nameservice
342 uci:foreach("olsrd", "LoadPlugin",
344 if s.library == "olsrd_txtinfo.so.0.1" then
345 uci:set("olsrd", s['.name'], "accept", "::1")
346 elseif s.library == "olsrd_nameservice.so.0.3" then
347 uci:set("olsrd", s['.name'], "name", hostname)
352 uci:tset("dhcp", "lan", {
353 dhcp_option = "6," .. dns_server,
354 start = bit.band(lan_net:minhost():add(1)[2][2], 0xFF),
355 limit = ( 2 ^ ( 32 - lan_net:prefix() ) ) - 3
359 uci:foreach("system", "system",
361 uci:set("system", s['.name'], "hostname", hostname)