applications/siitwizard:
[project/luci.git] / applications / luci-siitwizard / luasrc / model / cbi / siitwizard.lua
index b6493b3..7ba7f03 100644 (file)
@@ -15,11 +15,6 @@ $Id$
 ]]--
 
 local uci = require "luci.model.uci".cursor()
-local tools = require "luci.tools.ffwizard"
-local util = require "luci.util"
-
-local io = require "io"
-
 
 -------------------- View --------------------
 f = SimpleForm("siitwizward", "4over6-Assistent",
@@ -30,23 +25,27 @@ mode:value("gateway", "Gateway")
 mode:value("client", "Client")
 
 dev = f:field(ListValue, "device", "WLAN-Gerät")
-uci:foreach("network", "interface",
+uci:foreach("wireless", "wifi-device",
        function(section)
-               if section[".name"] ~= "siit0" then
-                       dev:value(section[".name"])
-               end
+               dev:value(section[".name"])
        end)
 
+lanip = f:field(Value, "ipaddr", "LAN IP Adresse")
+lanip.value = "127.23.1.1"
+
+lanmsk = f:field(Value, "netmask", "LAN Netzmaske")
+lanmsk.value = "255.255.0.0"
+
 
 -------------------- Control --------------------
-LL_PREFIX = luci.ip.IPv6("fe80::/16")
+LL_PREFIX = luci.ip.IPv6("fe80::/64")
 
 --
 -- find link-local address
 --
-function find_ll(dev)
+function find_ll()
        for _, r in ipairs(luci.sys.net.routes6()) do
-               if r.device == dev and LL_PREFIX:contains(r.dest) then
+               if LL_PREFIX:contains(r.dest) and r.dest:higher(LL_PREFIX) then
                        return r.dest:sub(LL_PREFIX)
                end
        end
@@ -68,13 +67,46 @@ end
 function mode.write(self, section, value)
 
        --
+       -- Configure wifi device
+       --
+       local wifi_device  = dev:formvalue(section)
+       local wifi_essid   = uci:get("siit", "wifi", "essid")   or "6mesh.freifunk.net"
+       local wifi_bssid   = uci:get("siit", "wifi", "bssid")   or "02:ca:ff:ee:ba:be"
+       local wifi_channel = uci:get("siit", "wifi", "channel") or "1"
+
+       -- nuke old device definition
+       uci:delete_all("wireless", "wifi-iface",
+               function(s) return s.device == wifi_device end )
+
+       uci:delete_all("network", "interface",
+               function(s) return s['.name'] == wifi_device end )
+
+       -- create wifi device definition
+       uci:tset("wireless", wifi_device, {
+               disabled  = 0,
+               channel   = wifi_channel,
+--             txantenna = 1,
+--             rxantenna = 1,
+--             diversity = 0
+       })
+
+       uci:section("wireless", "wifi-iface", nil, {
+               encryption = "none",
+               mode       = "adhoc",
+               network    = wifi_device,
+               device     = wifi_device,
+               ssid       = wifi_essid,
+               bssid      = wifi_bssid,
+       })
+
+
+       --
        -- Determine defaults
        --
-       local ula_prefix  = uci:get("siit", "defaults", "ula_prefix")  or "fd00::"
-       local ula_global  = uci:get("siit", "defaults", "ula_global")  or "00ca:ffee:babe::"            -- = Freifunk
-       local ula_subnet  = uci:get("siit", "defaults", "ula_subnet")  or "0000:0000:0000:4223::"       -- = Berlin
-       local siit_prefix = uci:get("siit", "defaults", "siit_prefix") or "::ffff:ffff:0000:0000"
-       local siit_route  = luci.ip.IPv6(siit_prefix .. "/96")
+       local ula_prefix  = uci:get("siit", "ipv6", "ula_prefix")  or "fd00::"
+       local ula_global  = uci:get("siit", "ipv6", "ula_global")  or "00ca:ffee:babe::"                -- = Freifunk
+       local ula_subnet  = uci:get("siit", "ipv6", "ula_subnet")  or "0000:0000:0000:4223::"   -- = Berlin
+       local siit_prefix = uci:get("siit", "ipv6", "siit_prefix") or "::ffff:0000:0000"
 
        -- Find wifi interface
        local device = dev:formvalue(section)
@@ -82,13 +114,13 @@ function mode.write(self, section, value)
        --
        -- Generate ULA
        --
-       local ula = luci.ip.IPv6("::")
+       local ula = luci.ip.IPv6("::/64")
 
        for _, prefix in ipairs({ ula_prefix, ula_global, ula_subnet }) do
                ula = ula:add(luci.ip.IPv6(prefix))
        end
 
-       ula = ula:add(find_ll(uci:get("network", device, "ifname") or device))
+       ula = ula:add(find_ll())
 
 
        --
@@ -105,6 +137,8 @@ function mode.write(self, section, value)
 
                uci:set("network", "wan", "mtu", 1400)
 
+               -- use full siit subnet
+               siit_route = luci.ip.IPv6(siit_prefix .. "/96")
 
        --
        -- Client mode
@@ -115,14 +149,22 @@ function mode.write(self, section, value)
        --      * Also, MTU on LAN reduced to 1400.
 
        else
-               local lan_ip = luci.ip.IPv4(
-                       uci:get("network", "lan", "ipaddr"),
-                       uci:get("network", "lan", "netmask")
+               -- lan interface
+               local lan_net = luci.ip.IPv4(
+                       lanip:formvalue(section) or "192.168.1.1",
+                       lanmsk:formvalue(section) or "255.255.255.0"
                )
 
+               uci:tset("network", "lan", {
+                       mtu     = 1400,
+                       ipaddr  = lan_net:host():string(),
+                       netmask = lan_net:mask():string()
+               })
+
+               -- derive siit subnet from lan config
                siit_route = luci.ip.IPv6(
-                       siit_prefix .. "/" .. (96 + lan_ip:prefix())
-               ):add(lan_ip[2])
+                       siit_prefix .. "/" .. (96 + lan_net:prefix())
+               ):add(lan_net[2])
 
        end
 
@@ -142,22 +184,30 @@ function mode.write(self, section, value)
                function(s) return siit_route:contains(luci.ip.IPv6(s.target)) end)
 
        uci:section("network", "route6", nil, {
-               interface = device,
+               interface = "siit0",
                target    = siit_route:string()
        })
 
-       -- interface
-       uci:set("network", device, "ip6addr", ula:string())
-       uci:set("network", "lan", "mtu", 1400)
+       -- create wifi network interface
+       uci:section("network", "interface", wifi_device, {
+               proto   = "static",
+               mtu     = 1400,
+               ip6addr = ula:string()
+       })
+
+       -- nuke old olsrd interfaces
+       uci:delete_all("olsrd", "Interface",
+               function(s) return s.interface == wifi_device end)
 
-       uci:set("olsrd", "general", "IpVersion", 6)
-       uci:foreach("olsrd", "Interface",
-               function(s)
-                       if s.interface == device then
-                               uci:set("olsrd", s[".name"], "Ip6AddrType", "global")
-                       end
-                       uci:delete("olsrd", s[".name"], "Ip4Broadcast")
-               end)
+       -- configure olsrd interface
+       uci:foreach("olsrd", "olsrd",
+               function(s) uci:set("olsrd", s['.name'], "IpVersion", 6) end)
+
+       uci:section("olsrd", "Interface", nil, {
+               ignore      = 0,
+               interface   = wifi_device,
+               Ip6AddrType = "global"
+       })
 
        -- hna6
        uci:delete_all("olsrd", "Hna6",
@@ -172,6 +222,7 @@ function mode.write(self, section, value)
                prefix  = siit_route:prefix()
        })
 
+       uci:save("wireless")
        uci:save("network")
        uci:save("olsrd")
 end