modules/admin-full: fix cbi dependency of stp flag
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / ifaces.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 require("luci.tools.webadmin")
17 arg[1] = arg[1] or ""
18
19 local has_3g    = luci.fs.mtime("/usr/bin/gcom")
20 local has_pptp  = luci.fs.mtime("/usr/sbin/pptp")
21 local has_pppd  = luci.fs.mtime("/usr/sbin/pppd")
22 local has_pppoe = luci.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")
23 local has_pppoa = luci.fs.glob("/usr/lib/pppd/*/pppoatm.so")
24
25 m = Map("network", translate("interfaces"), translate("a_n_ifaces1"))
26
27 s = m:section(NamedSection, arg[1], "interface")
28 s.addremove = true
29
30 back = s:option(DummyValue, "_overview", translate("overview"))
31 back.value = ""
32 back.titleref = luci.dispatcher.build_url("admin", "network", "network")
33
34 p = s:option(ListValue, "proto", translate("protocol"))
35 p.override_scheme = true
36 p.default = "static"
37 p:value("static", translate("static"))
38 p:value("dhcp", "DHCP")
39 if has_pppd  then p:value("ppp",   "PPP")     end
40 if has_pppoe then p:value("pppoe", "PPPoE")   end
41 if has_pppoa then p:value("pppoa", "PPPoA")   end
42 if has_3g    then p:value("3g",    "UMTS/3G") end
43 if has_pptp  then p:value("pptp",  "PPTP")    end
44 p:value("none", translate("none"))
45
46 if not ( has_pppd and has_pppoe and has_pppoa and has_3g and has_pptp ) then
47         p.description = translate("network_interface_prereq")
48 end
49
50 br = s:option(Flag, "type", translate("a_n_i_bridge"), translate("a_n_i_bridge1"))
51 br.enabled = "bridge"
52 br.rmempty = true
53
54 stp = s:option(Flag, "stp", translate("a_n_i_stp"),
55         translate("a_n_i_stp1", "Enables the Spanning Tree Protocol on this bridge"))
56 stp:depends("type", "1")
57 stp.rmempty = true
58
59 ifname = s:option(Value, "ifname", translate("interface"))
60 ifname.rmempty = true
61 for i,d in ipairs(luci.sys.net.devices()) do
62         if d ~= "lo" then
63                 ifname:value(d)
64         end
65 end
66
67 local zones = luci.tools.webadmin.network_get_zones(arg[1])
68 if zones then
69         if #zones == 0 then
70                 m:chain("firewall")
71
72                 fwzone = s:option(Value, "_fwzone",
73                         translate("network_interface_fwzone"),
74                         translate("network_interface_fwzone_desc"))
75                 fwzone.rmempty = true
76                 fwzone:value("", "- " .. translate("none") .. " -")
77                 fwzone:value(arg[1])
78                 m.uci:load("firewall")
79                 m.uci:foreach("firewall", "zone",
80                         function (section)
81                                 fwzone:value(section.name)
82                         end
83                 )
84
85                 function fwzone.write(self, section, value)
86                         local zone = luci.tools.webadmin.firewall_find_zone(value)
87                         local stat
88
89                         if not zone then
90                                 stat = m.uci:section("firewall", "zone", nil, {
91                                         name = value,
92                                         network = section
93                                 })
94                         else
95                                 local net = m.uci:get("firewall", zone, "network")
96                                 net = (net or value) .. " " .. section
97                                 stat = m.uci:set("firewall", zone, "network", net)
98                         end
99
100                         if stat then
101                                 self.render = function() end
102                         end
103                 end
104         else
105                 fwzone = s:option(DummyValue, "_fwzone", translate("zone"))
106                 fwzone.value = table.concat(zones, ", ")
107         end
108         fwzone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
109         m.uci:unload("firewall")
110 end
111
112 ipaddr = s:option(Value, "ipaddr", translate("ipaddress"))
113 ipaddr.rmempty = true
114 ipaddr:depends("proto", "static")
115
116 nm = s:option(Value, "netmask", translate("netmask"))
117 nm.rmempty = true
118 nm:depends("proto", "static")
119 nm:value("255.255.255.0")
120 nm:value("255.255.0.0")
121 nm:value("255.0.0.0")
122
123 gw = s:option(Value, "gateway", translate("gateway"))
124 gw:depends("proto", "static")
125 gw.rmempty = true
126
127 bcast = s:option(Value, "bcast", translate("broadcast"))
128 bcast:depends("proto", "static")
129 bcast.optional = true
130
131 ip6addr = s:option(Value, "ip6addr", translate("ip6address"), translate("cidr6"))
132 ip6addr.optional = true
133 ip6addr:depends("proto", "static")
134
135 ip6gw = s:option(Value, "ip6gw", translate("gateway6"))
136 ip6gw:depends("proto", "static")
137 ip6gw.optional = true
138
139 dns = s:option(Value, "dns", translate("dnsserver"))
140 dns.optional = true
141
142 mtu = s:option(Value, "mtu", "MTU")
143 mtu.optional = true
144 mtu.isinteger = true
145
146 mac = s:option(Value, "macaddr", translate("macaddress"))
147 mac.optional = true
148
149
150 srv = s:option(Value, "server", translate("network_interface_server"))
151 srv:depends("proto", "pptp")
152 srv.rmempty = true
153
154 if has_3g then
155         service = s:option(ListValue, "service", translate("network_interface_service"))
156         service:value("", translate("cbi_select"))
157         service:value("umts", "UMTS/GPRS")
158         service:value("cdma", "CDMA")
159         service:value("evdo", "EV-DO")
160         service:depends("proto", "3g")
161         service.rmempty = true
162
163         apn = s:option(Value, "apn", translate("network_interface_apn"))
164         apn:depends("proto", "3g")
165
166         pincode = s:option(Value, "pincode",
167          translate("network_interface_pincode"),
168          translate("network_interface_pincode_desc")
169         )
170         pincode:depends("proto", "3g")
171 end
172
173 if has_pppd or has_pppoe or has_pppoa or has_3g or has_pptp then
174         user = s:option(Value, "username", translate("username"))
175         user.rmempty = true
176         user:depends("proto", "pptp")
177         user:depends("proto", "pppoe")
178         user:depends("proto", "pppoa")
179         user:depends("proto", "ppp")
180         user:depends("proto", "3g")
181
182         pass = s:option(Value, "password", translate("password"))
183         pass.rmempty = true
184         pass.password = true
185         pass:depends("proto", "pptp")
186         pass:depends("proto", "pppoe")
187         pass:depends("proto", "pppoa")
188         pass:depends("proto", "ppp")
189         pass:depends("proto", "3g")
190
191         ka = s:option(Value, "keepalive",
192          translate("network_interface_keepalive"),
193          translate("network_interface_keepalive_desc")
194         )
195         ka.optional = true
196         ka:depends("proto", "pptp")
197         ka:depends("proto", "pppoe")
198         ka:depends("proto", "pppoa")
199         ka:depends("proto", "ppp")
200         ka:depends("proto", "3g")
201
202         demand = s:option(Value, "demand",
203          translate("network_interface_demand"),
204          translate("network_interface_demand_desc")
205         )
206         demand.optional = true
207         demand:depends("proto", "pptp")
208         demand:depends("proto", "pppoe")
209         demand:depends("proto", "pppoa")
210         demand:depends("proto", "ppp")
211         demand:depends("proto", "3g")
212 end
213
214 if has_pppoa then
215         encaps = s:option(ListValue, "encaps", translate("network_interface_encaps"))
216         encaps.optional = false
217         encaps:depends("proto", "pppoa")
218         encaps:value("", translate("cbi_select"))
219         encaps:value("vc", "VC")
220         encaps:value("llc", "LLC")
221
222         vpi = s:option(Value, "vpi", "VPI")
223         vpi.optional = false
224         vpi:depends("proto", "pppoa")
225
226         vci = s:option(Value, "vci", "VCI")
227         vci.optional = false
228         vci:depends("proto", "pppoa")
229 end
230
231 if has_pptp or has_pppd or has_pppoe or has_pppoa or has_3g then
232         device = s:option(Value, "device",
233          translate("network_interface_device"),
234          translate("network_interface_device_desc")
235         )
236         device:depends("proto", "ppp")
237         device:depends("proto", "3g")
238
239         defaultroute = s:option(Flag, "defaultroute",
240          translate("network_interface_defaultroute"),
241          translate("network_interface_defaultroute_desc")
242         )
243         defaultroute:depends("proto", "ppp")
244         defaultroute:depends("proto", "pppoa")
245         defaultroute:depends("proto", "pppoe")
246         defaultroute:depends("proto", "pptp")
247         defaultroute:depends("proto", "3g")
248         defaultroute.rmempty = false
249         function defaultroute.cfgvalue(...)
250                 return ( AbstractValue.cfgvalue(...) or '1' )
251         end
252
253         peerdns = s:option(Flag, "peerdns",
254          translate("network_interface_peerdns"),
255          translate("network_interface_peerdns_desc")
256         )
257         peerdns:depends("proto", "ppp")
258         peerdns:depends("proto", "pppoa")
259         peerdns:depends("proto", "pppoe")
260         peerdns:depends("proto", "pptp")
261         peerdns:depends("proto", "3g")
262         peerdns.rmempty = false
263         function peerdns.cfgvalue(...)
264                 return ( AbstractValue.cfgvalue(...) or '1' )
265         end
266
267         ipv6 = s:option(Flag, "ipv6", translate("network_interface_ipv6") )
268         ipv6:depends("proto", "ppp")
269         ipv6:depends("proto", "pppoa")
270         ipv6:depends("proto", "pppoe")
271         ipv6:depends("proto", "pptp")
272         ipv6:depends("proto", "3g")
273
274         connect = s:option(Value, "connect",
275          translate("network_interface_connect"),
276          translate("network_interface_connect_desc")
277         )
278         connect.optional = true
279         connect:depends("proto", "ppp")
280         connect:depends("proto", "pppoe")
281         connect:depends("proto", "pppoa")
282         connect:depends("proto", "pptp")
283         connect:depends("proto", "3g")
284
285         disconnect = s:option(Value, "disconnect",
286          translate("network_interface_disconnect"),
287          translate("network_interface_disconnect_desc")
288         )
289         disconnect.optional = true
290         disconnect:depends("proto", "ppp")
291         disconnect:depends("proto", "pppoe")
292         disconnect:depends("proto", "pppoa")
293         disconnect:depends("proto", "pptp")
294         disconnect:depends("proto", "3g")
295
296         pppd_options = s:option(Value, "pppd_options",
297          translate("network_interface_pppd_options"),
298          translate("network_interface_pppd_options_desc")
299         )
300         pppd_options.optional = true
301         pppd_options:depends("proto", "ppp")
302         pppd_options:depends("proto", "pppoa")
303         pppd_options:depends("proto", "pppoe")
304         pppd_options:depends("proto", "pptp")
305         pppd_options:depends("proto", "3g")
306
307         maxwait = s:option(Value, "maxwait",
308          translate("network_interface_maxwait"),
309          translate("network_interface_maxwait_desc")
310         )
311         maxwait.optional = true
312         maxwait:depends("proto", "3g")
313 end
314
315 s2 = m:section(TypedSection, "alias", translate("aliases"))
316 s2.addremove = true
317
318 s2:depends("interface", arg[1])
319 s2.defaults.interface = arg[1]
320
321
322 s2.defaults.proto = "static"
323
324 ipaddr = s2:option(Value, "ipaddr", translate("ipaddress"))
325 ipaddr.rmempty = true
326
327 nm = s2:option(Value, "netmask", translate("netmask"))
328 nm.rmempty = true
329 nm:value("255.255.255.0")
330 nm:value("255.255.0.0")
331 nm:value("255.0.0.0")
332
333 gw = s2:option(Value, "gateway", translate("gateway"))
334 gw.rmempty = true
335
336 bcast = s2:option(Value, "bcast", translate("broadcast"))
337 bcast.optional = true
338
339 ip6addr = s2:option(Value, "ip6addr", translate("ip6address"), translate("cidr6"))
340 ip6addr.optional = true
341
342 ip6gw = s2:option(Value, "ip6gw", translate("gateway6"))
343 ip6gw.optional = true
344
345 dns = s2:option(Value, "dns", translate("dnsserver"))
346 dns.optional = true
347
348 return m