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