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 -------------------- View --------------------
20 f = SimpleForm("siitwizward", "4over6-Assistent",
21 "Dieser Assistent unterstüzt bei der Einrichtung von IPv4-over-IPv6 Translation.")
23 mode = f:field(ListValue, "mode", "Betriebsmodus")
24 mode:value("client", "Client")
25 mode:value("gateway", "Gateway")
27 dev = f:field(ListValue, "device", "WLAN-Gerät")
28 uci:foreach("wireless", "wifi-device",
30 dev:value(section[".name"])
33 lanip = f:field(Value, "ipaddr", "LAN IP Adresse")
34 lanip.value = "172.23.1.1"
36 lanmsk = f:field(Value, "netmask", "Lokale LAN Netzmaske")
37 lanmsk.value = "255.255.255.0"
39 gv4msk = f:field(Value, "netmask", "Globale LAN Netzmaske")
40 gv4msk.value = "255.255.0.0"
43 -------------------- Control --------------------
44 LL_PREFIX = luci.ip.IPv6("fe80::/64")
47 -- find link-local address
50 for _, r in ipairs(luci.sys.net.routes6()) do
51 if LL_PREFIX:contains(r.dest) and r.dest:higher(LL_PREFIX) then
52 return r.dest:sub(LL_PREFIX)
55 return luci.ip.IPv6("::")
60 function f.handle(self, state, data)
61 if state == FORM_VALID then
62 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
64 elseif state == FORM_INVALID then
65 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
70 function mode.write(self, section, value)
73 local lan_net = luci.ip.IPv4(
74 lanip:formvalue(section) or "192.168.1.1",
75 lanmsk:formvalue(section) or "255.255.255.0"
78 local gv4_net = luci.ip.IPv4(
79 lanip:formvalue(section) or "192.168.1.1",
80 gv4msk:formvalue(section) or "255.255.0.0"
84 -- Configure wifi device
86 local wifi_device = dev:formvalue(section)
87 local wifi_essid = uci:get("siit", "wifi", "essid") or "6mesh.freifunk.net"
88 local wifi_bssid = uci:get("siit", "wifi", "bssid") or "02:ca:ff:ee:ba:be"
89 local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
91 -- nuke old device definition
92 uci:delete_all("wireless", "wifi-iface",
93 function(s) return s.device == wifi_device end )
95 uci:delete_all("network", "interface",
96 function(s) return s['.name'] == wifi_device end )
98 -- create wifi device definition
99 uci:tset("wireless", wifi_device, {
101 channel = wifi_channel,
107 uci:section("wireless", "wifi-iface", nil, {
110 network = wifi_device,
111 device = wifi_device,
118 -- Determine defaults
120 local ula_prefix = uci:get("siit", "ipv6", "ula_prefix") or "fd00::"
121 local ula_global = uci:get("siit", "ipv6", "ula_global") or "00ca:ffee:babe::" -- = Freifunk
122 local ula_subnet = uci:get("siit", "ipv6", "ula_subnet") or "0000:0000:0000:4223::" -- = Berlin
123 local siit_prefix = uci:get("siit", "ipv6", "siit_prefix") or "::ffff:0000:0000"
125 -- Find wifi interface
126 local device = dev:formvalue(section)
131 local ula = luci.ip.IPv6("::/64")
133 for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
134 ula = ula:add(luci.ip.IPv6(prefix))
137 ula = ula:add(find_ll())
143 -- * wan port is dhcp, lan port is 172.23.1.1/24
144 -- * siit0 gets a dummy address: 169.254.42.42
145 -- * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
146 -- * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
147 -- * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
148 -- * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
150 if value == "gateway" then
154 uci:set("network", "wan", "mtu", 1400)
157 uci:tset("network", "lan", {
159 ipaddr = lan_net:host():string(),
160 netmask = lan_net:mask():string()
163 -- use full siit subnet
164 siit_route = luci.ip.IPv6(siit_prefix .. "/96")
167 uci:delete_all("network", "route",
168 function(s) return s.interface == "siit0" end)
170 uci:section("network", "route", nil, {
172 target = gv4_net:host():string(),
173 netmask = gv4_net:mask():string()
179 -- * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
180 -- * 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.
181 -- * same route as HNA6 announcement to catch the traffic out of the mesh.
182 -- * Also, MTU on LAN reduced to 1400.
187 uci:tset("network", "lan", {
189 ipaddr = lan_net:host():string(),
190 netmask = lan_net:mask():string()
193 -- derive siit subnet from lan config
194 siit_route = luci.ip.IPv6(
195 siit_prefix .. "/" .. (96 + lan_net:prefix())
198 -- ipv4 <-> siit route
199 uci:delete_all("network", "route",
200 function(s) return s.interface == "siit0" end)
202 -- XXX: kind of a catch all, gv4_net would be better
203 -- but does not cover non-local v4 space
204 uci:section("network", "route", nil, {
211 -- setup the firewall
212 uci:delete_all("firewall", "zone",
214 s['.name'] == "siit0" or s.name == "siit0" or
215 s.network == "siit0" or s['.name'] == wifi_device or
216 s.name == wifi_device or s.network == wifi_device
219 uci:delete_all("firewall", "forwarding",
221 s.src == wifi_device and s.dest == "siit0" or
222 s.dest == wifi_device and s.src == "siit0" or
223 s.src == "lan" and s.dest == "siit0" or
224 s.dest == "lan" and s.src == "siit0"
227 uci:section("firewall", "zone", "siit0", {
235 uci:section("firewall", "zone", wifi_device, {
237 network = wifi_device,
243 uci:section("firewall", "forwarding", nil, {
248 uci:section("firewall", "forwarding", nil, {
253 uci:section("firewall", "forwarding", nil, {
258 uci:section("firewall", "forwarding", nil, {
264 uci:delete_all("network", "interface",
265 function(s) return ( s.ifname == "siit0" ) end)
267 uci:section("network", "interface", "siit0", {
270 ipaddr = "169.254.42.42",
271 netmask = "255.255.255.0"
275 uci:delete_all("network", "route6",
276 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
278 uci:section("network", "route6", nil, {
280 target = siit_route:string()
283 -- create wifi network interface
284 uci:section("network", "interface", wifi_device, {
287 ip6addr = ula:string()
290 -- nuke old olsrd interfaces
291 uci:delete_all("olsrd", "Interface",
292 function(s) return s.interface == wifi_device end)
294 -- configure olsrd interface
295 uci:foreach("olsrd", "olsrd",
296 function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
298 uci:section("olsrd", "Interface", nil, {
300 interface = wifi_device,
301 Ip6AddrType = "global"
305 uci:delete_all("olsrd", "Hna6",
307 if s.netaddr and s.prefix then
308 return siit_route:contains(luci.ip.IPv6(s.netaddr.."/"..s.prefix))
312 uci:section("olsrd", "Hna6", nil, {
313 netaddr = siit_route:host():string(),
314 prefix = siit_route:prefix()