2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 local uci = require "luci.model.uci".cursor()
19 -------------------- Init --------------------
22 -- Find link-local address
24 LL_PREFIX = luci.ip.IPv6("fe80::/64")
26 for _, r in ipairs(luci.sys.net.routes6()) do
27 if LL_PREFIX:contains(r.dest) and r.dest:higher(LL_PREFIX) then
28 return r.dest:sub(LL_PREFIX)
31 return luci.ip.IPv6("::")
37 local ula_prefix = uci:get("siit", "ipv6", "ula_prefix") or "fd00::"
38 local ula_global = uci:get("siit", "ipv6", "ula_global") or "00ca:ffee:babe::" -- = Freifunk
39 local ula_subnet = uci:get("siit", "ipv6", "ula_subnet") or "0000:0000:0000:4223::" -- = Berlin
40 local siit_prefix = uci:get("siit", "ipv6", "siit_prefix") or "::ffff:0000:0000"
41 local ipv4_pool = uci:get("siit", "ipv4", "pool") or "172.16.0.0/12"
42 local ipv4_netsz = uci:get("siit", "ipv4", "netsize") or "24"
45 -- Find IPv4 allocation pool
47 local gv4_net = luci.ip.IPv4(ipv4_pool)
53 local ula = luci.ip.IPv6("::/64")
55 for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
56 ula = ula:add(luci.ip.IPv6(prefix))
59 ula = ula:add(find_ll())
62 -------------------- View --------------------
63 f = SimpleForm("siitwizward", "SIIT-Wizzard",
64 "This wizzard helps to setup SIIT (IPv4-over-IPv6) translation according to RFC2765.")
66 f:field(DummyValue, "info_ula", "Mesh ULA address").value = ula:string()
68 f:field(DummyValue, "ipv4_pool", "IPv4 allocation pool").value =
69 "%s (%i hosts)" %{ gv4_net:string(), 2 ^ gv4_net:prefix() - 2 }
71 f:field(DummyValue, "ipv4_size", "IPv4 LAN network prefix").value =
72 "%i bit (%i hosts)" %{ ipv4_netsz, 2 ^ ( 32 - ipv4_netsz ) - 2 }
74 mode = f:field(ListValue, "mode", "Operation mode")
75 mode:value("client", "Client")
76 mode:value("gateway", "Gateway")
78 dev = f:field(ListValue, "device", "Wireless device")
79 uci:foreach("wireless", "wifi-device",
81 dev:value(section[".name"])
84 lanip = f:field(Value, "ipaddr", "LAN IPv4 subnet")
85 function lanip.formvalue(self, section)
86 local val = self.map:formvalue(self:cbid(section))
87 local net = luci.ip.IPv4("%s/%i" %{ val, ipv4_netsz })
90 if gv4_net:contains(net) then
91 if not net:minhost():equal(net:host()) then
92 self.error = { [section] = true }
93 f.errmessage = "IPv4 address is not the first host of " ..
94 "subnet, expected " .. net:minhost():string()
97 self.error = { [section] = true }
98 f.errmessage = "IPv4 address is not within the allocation pool"
101 self.error = { [section] = true }
102 f.errmessage = "Invalid IPv4 address given"
108 dns = f:field(Value, "dns", "DNS server for LAN clients")
109 dns.value = "141.1.1.1"
111 -------------------- Control --------------------
112 function f.handle(self, state, data)
113 if state == FORM_VALID then
114 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
120 function mode.write(self, section, value)
123 -- Find LAN IPv4 range
125 local lan_net = luci.ip.IPv4(
126 ( lanip:formvalue(section) or "172.16.0.1" ) .. "/" .. ipv4_netsz
129 if not lan_net then return end
132 -- Find wifi interface, dns server and hostname
134 local device = dev:formvalue(section)
135 local dns_server = dns:formvalue(section) or "141.1.1.1"
136 local hostname = "siit-" .. lan_net:host():string():gsub("%.","-")
139 -- Configure wifi device
141 local wifi_device = dev:formvalue(section)
142 local wifi_essid = uci:get("siit", "wifi", "essid") or "6mesh.freifunk.net"
143 local wifi_bssid = uci:get("siit", "wifi", "bssid") or "02:ca:ff:ee:ba:be"
144 local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
146 -- nuke old device definition
147 uci:delete_all("wireless", "wifi-iface",
148 function(s) return s.device == wifi_device end )
150 uci:delete_all("network", "interface",
151 function(s) return s['.name'] == wifi_device end )
153 -- create wifi device definition
154 uci:tset("wireless", wifi_device, {
156 channel = wifi_channel,
162 uci:section("wireless", "wifi-iface", nil, {
165 network = wifi_device,
166 device = wifi_device,
174 -- * wan port is dhcp, lan port is 172.23.1.1/24
175 -- * siit0 gets a dummy address: 169.254.42.42
176 -- * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
177 -- * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
178 -- * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
179 -- * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
181 if value == "gateway" then
184 uci:set("network", "wan", "mtu", 1400)
187 uci:tset("network", "lan", {
189 ipaddr = lan_net:host():string(),
190 netmask = lan_net:mask():string()
193 -- use full siit subnet
194 siit_route = luci.ip.IPv6(siit_prefix .. "/96")
197 uci:delete_all("network", "route",
198 function(s) return s.interface == "siit0" end)
200 uci:section("network", "route", nil, {
202 target = gv4_net:network():string(),
203 netmask = gv4_net:mask():string()
209 -- * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
210 -- * 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.
211 -- * same route as HNA6 announcement to catch the traffic out of the mesh.
212 -- * Also, MTU on LAN reduced to 1400.
217 uci:tset("network", "lan", {
219 ipaddr = lan_net:host():string(),
220 netmask = lan_net:mask():string()
223 -- derive siit subnet from lan config
224 siit_route = luci.ip.IPv6(
225 siit_prefix .. "/" .. (96 + lan_net:prefix())
228 -- ipv4 <-> siit route
229 uci:delete_all("network", "route",
230 function(s) return s.interface == "siit0" end)
232 -- XXX: kind of a catch all, gv4_net would be better
233 -- but does not cover non-local v4 space
234 uci:section("network", "route", nil, {
241 -- setup the firewall
242 uci:delete_all("firewall", "zone",
244 s['.name'] == "siit0" or s.name == "siit0" or
245 s.network == "siit0" or s['.name'] == wifi_device or
246 s.name == wifi_device or s.network == wifi_device
249 uci:delete_all("firewall", "forwarding",
251 s.src == wifi_device and s.dest == "siit0" or
252 s.dest == wifi_device and s.src == "siit0" or
253 s.src == "lan" and s.dest == "siit0" or
254 s.dest == "lan" and s.src == "siit0"
257 uci:section("firewall", "zone", "siit0", {
265 uci:section("firewall", "zone", wifi_device, {
267 network = wifi_device,
273 uci:section("firewall", "forwarding", nil, {
278 uci:section("firewall", "forwarding", nil, {
283 uci:section("firewall", "forwarding", nil, {
288 uci:section("firewall", "forwarding", nil, {
294 uci:delete_all("firewall", "include",
295 function(s) return s.path == "/etc/firewall.user" end)
297 uci:section("firewall", "include", nil, {
298 path = "/etc/firewall.user"
303 uci:delete_all("network", "interface",
304 function(s) return ( s.ifname == "siit0" ) end)
306 uci:section("network", "interface", "siit0", {
312 uci:delete_all("network", "route6",
313 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
315 uci:section("network", "route6", nil, {
317 target = siit_route:string()
320 -- create wifi network interface
321 uci:section("network", "interface", wifi_device, {
324 ip6addr = ula:string()
327 -- nuke old olsrd interfaces
328 uci:delete_all("olsrd", "Interface",
329 function(s) return s.interface == wifi_device end)
331 -- configure olsrd interface
332 uci:foreach("olsrd", "olsrd",
333 function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
335 uci:section("olsrd", "Interface", nil, {
337 interface = wifi_device,
338 Ip6AddrType = "global"
342 uci:delete_all("olsrd", "Hna6",
344 if s.netaddr and s.prefix then
345 return siit_route:contains(luci.ip.IPv6(s.netaddr.."/"..s.prefix))
349 uci:section("olsrd", "Hna6", nil, {
350 netaddr = siit_route:host():string(),
351 prefix = siit_route:prefix()
354 -- txtinfo v6 & olsrd nameservice
355 uci:foreach("olsrd", "LoadPlugin",
357 if s.library == "olsrd_txtinfo.so.0.1" then
358 uci:set("olsrd", s['.name'], "accept", "::1")
359 elseif s.library == "olsrd_nameservice.so.0.3" then
360 uci:set("olsrd", s['.name'], "name", hostname)
365 uci:tset("dhcp", "lan", {
366 dhcp_option = "6," .. dns_server
370 uci:foreach("system", "system",
372 uci:set("system", s['.name'], "hostname", hostname)