modules/admin-full: only offer hwmodes actually supported by the wiphy
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / iface_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local nw  = require "luci.model.network"
16 local fw  = require "luci.model.firewall"
17 local uci = require "luci.model.uci".cursor()
18
19 m = SimpleForm("network", translate("Create Or Attach Network"),
20         translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
21                 "to the existing interfaces and is covered by the firewall zone of the choosen network.<br />" ..
22                 "Uncheck the attach option to define a new standalone network for this interface."
23         ))
24
25 nw.init(uci)
26 fw.init(uci)
27
28 attachnet = m:field(Flag, "_attach", translate("Attach to existing network"))
29 attachnet.rmempty = false
30 attachnet.default = "1"
31
32 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
33         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
34                 "<code>0-9</code> and <code>_</code>"
35         ))
36
37 newnet:depends("_attach", "")
38 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
39
40 addnet = m:field(Value, "_netname_attach",
41         translate("Network to attach interface to"))
42
43 addnet.template = "cbi/network_netlist"
44 addnet.widget = "radio"
45 addnet.nocreate = true
46 addnet:depends("_attach", "1")
47
48 fwzone = m:field(Value, "_fwzone",
49         translate("Create / Assign firewall-zone"),
50         translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
51
52 fwzone.template = "cbi/firewall_zonelist"
53 addnet.widget = "radio"
54 fwzone:depends("_attach", "")
55 fwzone.default = arg[1] and "zone_" .. arg[1]:gsub("[^%w_]+", "_")
56
57
58 function attachnet.write(self, section, value)
59         local net, zone
60
61         if value == "1" then
62                 net = nw:get_network(addnet:formvalue(section))
63                 if net then
64                         net:type("bridge")
65                 end
66         else
67                 local zval = fwzone:formvalue(section)
68                 
69                 net = nw:add_network(newnet:formvalue(section), { proto  = "none" })
70                 zone = fw:get_zone(zval)
71
72                 if not zone and zval == '-' then
73                         zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
74                         if zval and #zval > 0 then
75                                 zone = fw:add_zone(zval)
76                         else
77                                 fw:del_network(arg[1])
78                         end
79                 end
80         end
81
82         if not net then
83                 self.error = { [section] = "missing" }
84         else
85                 net:add_interface(arg[1])
86
87                 if zone then
88                         fw:del_network(net:name())
89                         zone:add_network(net:name())
90                 end
91
92                 uci:save("wireless")
93                 uci:save("network")
94                 uci:save("firewall")
95                 luci.http.redirect(luci.dispatcher.build_url("admin/network/network", net:name()))
96         end
97 end
98
99 function fwzone.cfgvalue(self, section)
100         self.iface = section
101         local z = fw:get_zone_by_network(section)
102         return z and z:name()
103 end
104
105 return m