applications/siitwizard: remove leftover variable definition
[project/luci.git] / applications / luci-siitwizard / luasrc / model / cbi / siitwizard.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 local uci = require "luci.model.uci".cursor()
18
19 -------------------- Init --------------------
20
21 --
22 -- Find link-local address
23 --
24 LL_PREFIX = luci.ip.IPv6("fe80::/64")
25 function find_ll()
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)
29                 end
30         end
31         return luci.ip.IPv6("::")
32 end
33
34 --
35 -- Determine defaults
36 --
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"
43
44 --
45 -- Find IPv4 allocation pool
46 --
47 local gv4_net = luci.ip.IPv4(ipv4_pool)
48
49 --
50 -- Generate ULA
51 --
52 local ula = luci.ip.IPv6("::/64")
53
54 for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
55         ula = ula:add(luci.ip.IPv6(prefix))
56 end
57
58 ula = ula:add(find_ll())
59
60
61 -------------------- View --------------------
62 f = SimpleForm("siitwizward", "SIIT-Wizzard",
63  "This wizzard helps to setup SIIT (IPv4-over-IPv6) translation according to RFC2765.")
64
65 f:field(DummyValue, "info_ula", "Mesh ULA address").value = ula:string()
66
67 f:field(DummyValue, "ipv4_pool", "IPv4 allocation pool").value =
68         "%s (%i hosts)" %{ gv4_net:string(), 2 ^ gv4_net:prefix() - 2 }
69
70 f:field(DummyValue, "ipv4_size", "IPv4 LAN network prefix").value =
71         "%i bit (%i hosts)" %{ ipv4_netsz, 2 ^ ( 32 - ipv4_netsz ) - 2 }
72
73 mode = f:field(ListValue, "mode", "Operation mode")
74 mode:value("client", "Client")
75 mode:value("gateway", "Gateway")
76
77 dev = f:field(ListValue, "device", "Wireless device")
78 uci:foreach("wireless", "wifi-device",
79         function(section)
80                 dev:value(section[".name"])
81         end)
82
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 })
87
88         if net then
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()
94                         end
95                 else
96                         self.error = { [section] = true }
97                         f.errmessage = "IPv4 address is not within the allocation pool"
98                 end
99         else
100                 self.error = { [section] = true }
101                 f.errmessage = "Invalid IPv4 address given"
102         end
103
104         return val
105 end
106
107 dns = f:field(Value, "dns", "DNS server for LAN clients")
108 dns.value = "141.1.1.1"
109
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"))
114                 return false
115         end
116         return true
117 end
118
119 function mode.write(self, section, value)
120
121         --
122         -- Find LAN IPv4 range
123         --
124         local lan_net = luci.ip.IPv4(
125                 ( lanip:formvalue(section) or "172.16.0.1" ) .. "/" .. ipv4_netsz
126         )
127
128         if not lan_net then return end
129
130         --
131         -- Find wifi interface, dns server and hostname
132         --
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("%.","-")
136
137         --
138         -- Configure wifi device
139         --
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"
144
145         -- nuke old device definition
146         uci:delete_all("wireless", "wifi-iface",
147                 function(s) return s.device == wifi_device end )
148
149         uci:delete_all("network", "interface",
150                 function(s) return s['.name'] == wifi_device end )
151
152         -- create wifi device definition
153         uci:tset("wireless", wifi_device, {
154                 disabled  = 0,
155                 channel   = wifi_channel,
156 --              txantenna = 1,
157 --              rxantenna = 1,
158 --              diversity = 0
159         })
160
161         uci:section("wireless", "wifi-iface", nil, {
162                 encryption = "none",
163                 mode       = "adhoc",
164                 network    = wifi_device,
165                 device     = wifi_device,
166                 ssid       = wifi_essid,
167                 bssid      = wifi_bssid,
168         })
169
170         --
171         -- Gateway mode
172         --
173         --      * wan port is dhcp, lan port is 172.23.1.1/24
174         --      * siit0 gets a dummy address: 169.254.42.42
175         --      * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
176         --      * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
177         --      * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
178         --      * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
179
180         if value == "gateway" then
181
182                 -- wan mtu
183                 uci:set("network", "wan", "mtu", 1400)
184
185                 -- lan settings
186                 uci:tset("network", "lan", {
187                         mtu     = 1400,
188                         ipaddr  = lan_net:host():string(),
189                         netmask = lan_net:mask():string()
190                 })
191
192                 -- use full siit subnet
193                 siit_route = luci.ip.IPv6(siit_prefix .. "/96")
194
195                 -- v4 <-> siit route
196                 uci:delete_all("network", "route",
197                         function(s) return s.interface == "siit0" end)
198
199                 uci:section("network", "route", nil, {
200                         interface = "siit0",
201                         target    = gv4_net:network():string(),
202                         netmask   = gv4_net:mask():string()
203                 })
204
205         --
206         -- Client mode
207         --
208         --      * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
209         --      * 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.
210         --      * same route as HNA6 announcement to catch the traffic out of the mesh.
211         --      * Also, MTU on LAN reduced to 1400.
212
213         else
214
215                 -- lan settings
216                 uci:tset("network", "lan", {
217                         mtu     = 1400,
218                         ipaddr  = lan_net:host():string(),
219                         netmask = lan_net:mask():string()
220                 })
221
222                 -- derive siit subnet from lan config
223                 siit_route = luci.ip.IPv6(
224                         siit_prefix .. "/" .. (96 + lan_net:prefix())
225                 ):add(lan_net[2])
226
227                 -- ipv4 <-> siit route
228                 uci:delete_all("network", "route",
229                         function(s) return s.interface == "siit0" end)
230
231                 -- XXX: kind of a catch all, gv4_net would be better
232                 --      but does not cover non-local v4 space
233                 uci:section("network", "route", nil, {
234                         interface = "siit0",
235                         target    = "0.0.0.0",
236                         netmask   = "0.0.0.0"
237                 })
238         end
239
240         -- setup the firewall
241         uci:delete_all("firewall", "zone",
242                 function(s) return (
243                         s['.name'] == "siit0" or s.name == "siit0" or
244                         s.network == "siit0" or s['.name'] == wifi_device or
245                         s.name == wifi_device or s.network == wifi_device
246                 ) end)
247
248         uci:delete_all("firewall", "forwarding",
249                 function(s) return (
250                         s.src == wifi_device and s.dest == "siit0" or
251                         s.dest == wifi_device and s.src == "siit0" or
252                         s.src == "lan" and s.dest == "siit0" or
253                         s.dest == "lan" and s.src == "siit0"
254                 ) end)
255
256         uci:section("firewall", "zone", "siit0", {
257                 name    = "siit0",
258                 network = "siit0",
259                 input   = "ACCEPT",
260                 output  = "ACCEPT",
261                 forward = "ACCEPT"
262         })
263
264         uci:section("firewall", "zone", wifi_device, {
265                 name    = wifi_device,
266                 network = wifi_device,
267                 input   = "ACCEPT",
268                 output  = "ACCEPT",
269                 forward = "ACCEPT"
270         })
271
272         uci:section("firewall", "forwarding", nil, {
273                 src  = wifi_device,
274                 dest = "siit0"
275         })
276
277         uci:section("firewall", "forwarding", nil, {
278                 src  = "siit0",
279                 dest = wifi_device
280         })
281
282         uci:section("firewall", "forwarding", nil, {
283                 src  = "lan",
284                 dest = "siit0"
285         })
286
287         uci:section("firewall", "forwarding", nil, {
288                 src  = "siit0",
289                 dest = "lan"
290         })
291
292         -- firewall include
293         uci:delete_all("firewall", "include",
294                 function(s) return s.path == "/etc/firewall.user" end)
295
296         uci:section("firewall", "include", nil, {
297                 path = "/etc/firewall.user"
298         })
299
300
301         -- siit0 interface
302         uci:delete_all("network", "interface",
303                 function(s) return ( s.ifname == "siit0" ) end)
304
305         uci:section("network", "interface", "siit0", {
306                 ifname  = "siit0",
307                 proto   = "none"
308         })
309
310         -- siit0 route
311         uci:delete_all("network", "route6",
312                 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
313
314         uci:section("network", "route6", nil, {
315                 interface = "siit0",
316                 target    = siit_route:string()
317         })
318
319         -- create wifi network interface
320         uci:section("network", "interface", wifi_device, {
321                 proto   = "static",
322                 mtu     = 1400,
323                 ip6addr = ula:string()
324         })
325
326         -- nuke old olsrd interfaces
327         uci:delete_all("olsrd", "Interface",
328                 function(s) return s.interface == wifi_device end)
329
330         -- configure olsrd interface
331         uci:foreach("olsrd", "olsrd",
332                 function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
333
334         uci:section("olsrd", "Interface", nil, {
335                 ignore      = 0,
336                 interface   = wifi_device,
337                 Ip6AddrType = "global"
338         })
339
340         -- hna6
341         uci:delete_all("olsrd", "Hna6",
342                 function(s)
343                         if s.netaddr and s.prefix then
344                                 return siit_route:contains(luci.ip.IPv6(s.netaddr.."/"..s.prefix))
345                         end
346                 end)
347
348         uci:section("olsrd", "Hna6", nil, {
349                 netaddr = siit_route:host():string(),
350                 prefix  = siit_route:prefix()
351         })
352
353         -- txtinfo v6 & olsrd nameservice
354         uci:foreach("olsrd", "LoadPlugin",
355                 function(s)
356                         if s.library == "olsrd_txtinfo.so.0.1" then
357                                 uci:set("olsrd", s['.name'], "accept", "::1")
358                         elseif s.library == "olsrd_nameservice.so.0.3" then
359                                 uci:set("olsrd", s['.name'], "name", hostname)
360                         end
361                 end)
362
363         -- lan dns
364         uci:tset("dhcp", "lan", {
365                 dhcp_option = "6," .. dns_server
366         })
367
368         -- hostname
369         uci:foreach("system", "system",
370                 function(s)
371                         uci:set("system", s['.name'], "hostname", hostname)
372                 end)
373
374         uci:save("wireless")
375         uci:save("firewall")
376         uci:save("network")
377         uci:save("system")
378         uci:save("olsrd")
379         uci:save("dhcp")
380 end
381
382 return f