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