contrib/package: make luci-app-siitwizzard depend on kmod-siit
[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 local tools = require "luci.tools.ffwizard"
19 local util = require "luci.util"
20
21 local io = require "io"
22
23
24 -------------------- View --------------------
25 f = SimpleForm("siitwizward", "4over6-Assistent",
26  "Dieser Assistent unterstüzt bei der Einrichtung von IPv4-over-IPv6 Translation.")
27
28 mode = f:field(ListValue, "mode", "Betriebsmodus")
29 mode:value("gateway", "Gateway")
30 mode:value("client", "Client")
31
32 dev = f:field(ListValue, "device", "WLAN-Gerät")
33 uci:foreach("network", "interface",
34         function(section)
35                 if section[".name"] ~= "siit0" then
36                         dev:value(section[".name"])
37                 end
38         end)
39
40
41 -------------------- Control --------------------
42 LL_PREFIX = luci.ip.IPv6("fe80::/16")
43
44 --
45 -- find link-local address
46 --
47 function find_ll(dev)
48         for _, r in ipairs(luci.sys.net.routes6()) do
49                 if r.device == dev and LL_PREFIX:contains(r.dest) then
50                         return r.dest:sub(LL_PREFIX)
51                 end
52         end
53         return luci.ip.IPv6("::")
54 end
55
56
57
58 function f.handle(self, state, data)
59         if state == FORM_VALID then
60                 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
61                 return false
62         elseif state == FORM_INVALID then
63                 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
64         end
65         return true
66 end
67
68 function mode.write(self, section, value)
69
70         --
71         -- Determine defaults
72         --
73         local ula_prefix  = uci:get("siit", "defaults", "ula_prefix")  or "fd00::"
74         local ula_global  = uci:get("siit", "defaults", "ula_global")  or "00ca:ffee:babe::"            -- = Freifunk
75         local ula_subnet  = uci:get("siit", "defaults", "ula_subnet")  or "0000:0000:0000:4223::"       -- = Berlin
76         local siit_prefix = uci:get("siit", "defaults", "siit_prefix") or "::ffff:ffff:0000:0000"
77         local siit_route  = luci.ip.IPv6(siit_prefix .. "/96")
78
79         -- Find wifi interface
80         local device = dev:formvalue(section)
81
82         --
83         -- Generate ULA
84         --
85         local ula = luci.ip.IPv6("::")
86
87         for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
88                 ula = ula:add(luci.ip.IPv6(prefix))
89         end
90
91         ula = ula:add(find_ll(uci:get("network", device, "ifname") or device))
92
93
94         --
95         -- Gateway mode
96         --
97         --      * wan port is dhcp, lan port is 172.23.1.1/24
98         --      * siit0 gets a dummy address: 169.254.42.42
99         --      * wl0 gets an ipv6 address, in this case the fdca:ffee:babe::1:1/64
100         --      * we do a ::ffff:ffff:0/96 route into siit0, so everything from 6mesh goes into translation.
101         --      * an HNA6 of ::ffff:ffff:0:0/96 announces the mapped 0.0.0.0/0 ipv4 space.
102         --      * MTU on WAN, LAN down to 1400, ipv6 headers are slighly larger.
103
104         if value == "gateway" then
105
106                 uci:set("network", "wan", "mtu", 1400)
107
108
109         --
110         -- Client mode
111         --
112         --      * 172.23.2.1/24 on its lan, fdca:ffee:babe::1:2 on wl0 and the usual dummy address on siit0.
113         --      * 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.
114         --      * same route as HNA6 announcement to catch the traffic out of the mesh.
115         --      * Also, MTU on LAN reduced to 1400.
116
117         else
118                 local lan_ip = luci.ip.IPv4(
119                         uci:get("network", "lan", "ipaddr"),
120                         uci:get("network", "lan", "netmask")
121                 )
122
123                 siit_route = luci.ip.IPv6(
124                         siit_prefix .. "/" .. (96 + lan_ip:prefix())
125                 ):add(lan_ip[2])
126
127         end
128
129         -- siit0 interface
130         uci:delete_all("network", "interface",
131                 function(s) return ( s.ifname == "siit0" ) end)
132
133         uci:section("network", "interface", "siit0", {
134                 ifname  = "siit0",
135                 proto   = "static",
136                 ipaddr  = "169.254.42.42",
137                 netmask = "255.255.255.0"
138         })
139
140         -- siit0 route
141         uci:delete_all("network", "route6",
142                 function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
143
144         uci:section("network", "route6", nil, {
145                 interface = device,
146                 target    = siit_route:string()
147         })
148
149         -- interface
150         uci:set("network", device, "ip6addr", ula:string())
151         uci:set("network", "lan", "mtu", 1400)
152
153         uci:set("olsrd", "general", "IpVersion", 6)
154         uci:foreach("olsrd", "Interface",
155                 function(s)
156                         if s.interface == device then
157                                 uci:set("olsrd", s[".name"], "Ip6AddrType", "global")
158                         end
159                         uci:delete("olsrd", s[".name"], "Ip4Broadcast")
160                 end)
161
162         -- hna6
163         uci:delete_all("olsrd", "Hna6",
164                 function(s)
165                         if s.netaddr and s.prefix then
166                                 return siit_route:contains(luci.ip.IPv6(s.netaddr.."/"..s.prefix))
167                         end
168                 end)
169
170         uci:section("olsrd", "Hna6", nil, {
171                 netaddr = siit_route:host():string(),
172                 prefix  = siit_route:prefix()
173         })
174
175         uci:save("network")
176         uci:save("olsrd")
177 end
178
179 return f