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)
52 local ula = luci.ip.IPv6("::/64")
54 for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
55 ula = ula:add(luci.ip.IPv6(prefix))
58 ula = ula:add(find_ll())
61 -------------------- View --------------------
62 f = SimpleForm("siitwizward", "SIIT-Wizzard",
63 "This wizzard helps to setup SIIT (IPv4-over-IPv6) translation according to RFC2765.")
65 f:field(DummyValue, "info_ula", "Mesh ULA address").value = ula:string()
67 f:field(DummyValue, "ipv4_pool", "IPv4 allocation pool").value =
68 "%s (%i hosts)" %{ gv4_net:string(), 2 ^ gv4_net:prefix() - 2 }
70 f:field(DummyValue, "ipv4_size", "IPv4 LAN network prefix").value =
71 "%i bit (%i hosts)" %{ ipv4_netsz, 2 ^ ( 32 - ipv4_netsz ) - 2 }
73 mode = f:field(ListValue, "mode", "Operation mode")
74 mode:value("client", "Client")
75 mode:value("gateway", "Gateway")
77 dev = f:field(ListValue, "device", "Wireless device")
78 uci:foreach("wireless", "wifi-device",
80 dev:value(section[".name"])
83 lanip = f:field(Value, "ipaddr", "LAN IPv4 subnet")
84 function lanip.formvalue(self, section)
85 local val = self.map:formvalue(self:cbid(section))
86 local net = luci.ip.IPv4("%s/%i" %{ val, ipv4_netsz })
89 if gv4_net:contains(net) then
90 if not net:minhost():equal(net:host()) then
91 self.error = { [section] = true }
92 f.errmessage = "IPv4 address is not the first host of " ..
93 "subnet, expected " .. net:minhost():string()
96 self.error = { [section] = true }
97 f.errmessage = "IPv4 address is not within the allocation pool"
100 self.error = { [section] = true }
101 f.errmessage = "Invalid IPv4 address given"
107 dns = f:field(Value, "dns", "DNS server for LAN clients")
108 dns.value = "141.1.1.1"
110 -------------------- Control --------------------
111 function f.handle(self, state, data)
112 if state == FORM_VALID then
113 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
119 function mode.write(self, section, value)
122 -- Find LAN IPv4 range
124 local lan_net = luci.ip.IPv4(
125 ( lanip:formvalue(section) or "172.16.0.1" ) .. "/" .. ipv4_netsz
128 if not lan_net then return end
131 -- Find wifi interface, dns server and hostname
133 local device = dev:formvalue(section)
134 local dns_server = dns:formvalue(section) or "141.1.1.1"
135 local hostname = "siit-" .. lan_net:host():string():gsub("%.","-")
138 -- Configure wifi device
140 local wifi_device = dev:formvalue(section)
141 local wifi_essid = uci:get("siit", "wifi", "essid") or "6mesh.freifunk.net"
142 local wifi_bssid = uci:get("siit", "wifi", "bssid") or "02:ca:ff:ee:ba:be"
143 local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
145 -- nuke old device definition
146 uci:delete_all("wireless", "wifi-iface",
147 function(s) return s.device == wifi_device end )
149 uci:delete_all("network", "interface",
150 function(s) return s['.name'] == wifi_device end )
152 -- create wifi device definition
153 uci:tset("wireless", wifi_device, {
155 channel = wifi_channel,
161 uci:section("wireless", "wifi-iface", nil, {
166 network = wifi_device,
167 device = wifi_device,
175 -- * wan port is dhcp, lan port is 172.23.1.1/24
176 -- * siit0 gets a dummy address: 169.254.42.42
177 -- * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
178 -- * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
179 -- * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
180 -- * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
182 if value == "gateway" then
185 uci:set("network", "wan", "mtu", 1400)
188 uci:tset("network", "lan", {
190 ipaddr = lan_net:host():string(),
191 netmask = lan_net:mask():string()
194 -- use full siit subnet
195 siit_route = luci.ip.IPv6(siit_prefix .. "/96")
198 uci:delete_all("network", "route",
199 function(s) return s.interface == "siit0" end)
201 uci:section("network", "route", nil, {
203 target = gv4_net:network():string(),
204 netmask = gv4_net:mask():string()
210 -- * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
211 -- * 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.
212 -- * same route as HNA6 announcement to catch the traffic out of the mesh.
213 -- * Also, MTU on LAN reduced to 1400.
218 uci:tset("network", "lan", {
220 ipaddr = lan_net:host():string(),
221 netmask = lan_net:mask():string()
224 -- derive siit subnet from lan config
225 siit_route = luci.ip.IPv6(
226 siit_prefix .. "/" .. (96 + lan_net:prefix())
229 -- ipv4 <-> siit route
230 uci:delete_all("network", "route",
231 function(s) return s.interface == "siit0" end)
233 -- XXX: kind of a catch all, gv4_net would be better
234 -- but does not cover non-local v4 space
235 uci:section("network", "route", nil, {
242 -- setup the firewall
243 uci:delete_all("firewall", "zone",
245 s['.name'] == "siit0" or s.name == "siit0" or
246 s.network == "siit0" or s['.name'] == wifi_device or
247 s.name == wifi_device or s.network == wifi_device
250 uci:delete_all("firewall", "forwarding",
252 s.src == wifi_device and s.dest == "siit0" or
253 s.dest == wifi_device and s.src == "siit0" or
254 s.src == "lan" and s.dest == "siit0" or
255 s.dest == "lan" and s.src == "siit0"
258 uci:section("firewall", "zone", "siit0", {
266 uci:section("firewall", "zone", wifi_device, {
268 network = wifi_device,
274 uci:section("firewall", "forwarding", nil, {
279 uci:section("firewall", "forwarding", nil, {
284 uci:section("firewall", "forwarding", nil, {
289 uci:section("firewall", "forwarding", nil, {
295 uci:delete_all("firewall", "include",
296 function(s) return s.path == "/etc/firewall.user" end)
298 uci:section("firewall", "include", nil, {
299 path = "/etc/firewall.user"
304 uci:delete_all("network", "interface",
305 function(s) return ( s.ifname == "siit0" ) end)
307 uci:section("network", "interface", "siit0", {
313 uci:delete_all("network", "route6",
314 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
316 uci:section("network", "route6", nil, {
318 target = siit_route:string()
321 -- create wifi network interface
322 uci:section("network", "interface", wifi_device, {
325 ip6addr = ula:string()
328 -- nuke old olsrd interfaces
329 uci:delete_all("olsrd", "Interface",
330 function(s) return s.interface == wifi_device end)
332 -- configure olsrd interface
333 uci:foreach("olsrd", "olsrd",
334 function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
336 uci:section("olsrd", "Interface", nil, {
338 interface = wifi_device,
339 Ip6AddrType = "global"
343 uci:delete_all("olsrd", "Hna6",
345 if s.netaddr and s.prefix then
346 return siit_route:contains(luci.ip.IPv6(s.netaddr.."/"..s.prefix))
350 uci:section("olsrd", "Hna6", nil, {
351 netaddr = siit_route:host():string(),
352 prefix = siit_route:prefix()
355 -- txtinfo v6 & olsrd nameservice
356 uci:foreach("olsrd", "LoadPlugin",
358 if s.library == "olsrd_txtinfo.so.0.1" then
359 uci:set("olsrd", s['.name'], "accept", "::1")
360 elseif s.library == "olsrd_nameservice.so.0.3" then
361 uci:set("olsrd", s['.name'], "name", hostname)
366 uci:tset("dhcp", "lan", {
367 dhcp_option = "6," .. dns_server
371 uci:foreach("system", "system",
373 uci:set("system", s['.name'], "hostname", hostname)