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