Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / luci / modules / luci-base / luasrc / model / cbi / admin_network / proto_static.lua
1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local map, section, net = ...
5 local ifc = net:get_interface()
6
7 local netmask, gateway, broadcast, dns, accept_ra, send_rs, ip6addr, ip6gw
8 local mtu, metric, usecidr, ipaddr_single, ipaddr_multi
9
10
11 local function is_cidr(s)
12         return (type(s) == "string" and luci.ip.IPv4(s) and s:find("/"))
13 end
14
15 usecidr = section:taboption("general", Value, "ipaddr_usecidr")
16 usecidr.forcewrite = true
17
18 usecidr.cfgvalue = function(self, section)
19         local cfgvalue = self.map:get(section, "ipaddr")
20         return (type(cfgvalue) == "table" or is_cidr(cfgvalue)) and "1" or "0"
21 end
22
23 usecidr.render = function(self, section, scope)
24         luci.template.Template(nil, [[
25                 <input type="hidden"<%= attr("id", cbid) .. attr("name", cbid) .. attr("value", value) %> />
26         ]]):render({
27                 cbid = self:cbid(section),
28                 value = self:cfgvalue(section)
29         })
30 end
31
32 usecidr.write = function(self, section)
33         local cfgvalue = self.map:get(section, "ipaddr")
34         local formvalue = (self:formvalue(section) == "1") and ipaddr_multi:formvalue(section) or ipaddr_single:formvalue(section)
35         local equal = (cfgvalue == formvalue)
36
37         if not equal and type(cfgvalue) == "table" and type(formvalue) == "table" and #cfgvalue == #formvalue then
38                 equal = true
39
40                 local _, v
41                 for _, v in ipairs(cfgvalue) do
42                         if v ~= formvalue[_] then
43                                 equal = false
44                                 break
45                         end
46                 end
47         end
48
49         if not equal then
50                 self.map:set(section, "ipaddr", formvalue or "")
51         end
52
53         return not equal
54 end
55
56
57 ipaddr_multi = section:taboption("general", DynamicList, "ipaddrs", translate("IPv4 address"))
58 ipaddr_multi:depends("ipaddr_usecidr", "1")
59 ipaddr_multi.datatype = "or(cidr4,ipnet4)"
60 ipaddr_multi.placeholder = translate("Add IPv4 address…")
61
62 ipaddr_multi.alias = "ipaddr"
63 ipaddr_multi.write = function() end
64 ipaddr_multi.remove = function() end
65 ipaddr_multi.cfgvalue = function(self, section)
66         local addr = self.map:get(section, "ipaddr")
67         local mask = self.map:get(section, "netmask")
68
69         if is_cidr(addr) then
70                 return { addr }
71         elseif type(addr) == "string" and
72                type(mask) == "string" and
73                #addr > 0 and #mask > 0
74         then
75                 return { "%s/%s" %{ addr, mask } }
76         elseif type(addr) == "table" then
77                 return addr
78         else
79                 return {}
80         end
81 end
82
83
84 ipaddr_single = section:taboption("general", Value, "ipaddr", translate("IPv4 address"))
85 ipaddr_single:depends("ipaddr_usecidr", "0")
86 ipaddr_single.datatype = "ip4addr"
87 ipaddr_single.template = "cbi/ipaddr"
88 ipaddr_single.write = function() end
89 ipaddr_single.remove = function() end
90
91
92 netmask = section:taboption("general", Value, "netmask", translate("IPv4 netmask"))
93 netmask:depends("ipaddr_usecidr", "0")
94 netmask.datatype = "ip4addr"
95 netmask:value("255.255.255.0")
96 netmask:value("255.255.0.0")
97 netmask:value("255.0.0.0")
98
99
100 gateway = section:taboption("general", Value, "gateway", translate("IPv4 gateway"))
101 gateway.datatype = "ip4addr"
102
103
104 broadcast = section:taboption("general", Value, "broadcast", translate("IPv4 broadcast"))
105 broadcast.datatype = "ip4addr"
106
107
108 dns = section:taboption("general", DynamicList, "dns",
109         translate("Use custom DNS servers"))
110
111 dns.datatype = "ipaddr"
112 dns.cast     = "string"
113
114
115 if luci.model.network:has_ipv6() then
116
117         local ip6assign = section:taboption("general", Value, "ip6assign", translate("IPv6 assignment length"),
118                 translate("Assign a part of given length of every public IPv6-prefix to this interface"))
119         ip6assign:value("", translate("disabled"))
120         ip6assign:value("64")
121         ip6assign.datatype = "max(64)"
122
123         local ip6hint = section:taboption("general", Value, "ip6hint", translate("IPv6 assignment hint"),
124                 translate("Assign prefix parts using this hexadecimal subprefix ID for this interface."))
125         for i=33,64 do ip6hint:depends("ip6assign", i) end
126
127         ip6addr = section:taboption("general", DynamicList, "ip6addr", translate("IPv6 address"))
128         ip6addr.datatype = "ip6addr"
129         ip6addr.placeholder = translate("Add IPv6 address…")
130         ip6addr:depends("ip6assign", "")
131
132
133         ip6gw = section:taboption("general", Value, "ip6gw", translate("IPv6 gateway"))
134         ip6gw.datatype = "ip6addr"
135         ip6gw:depends("ip6assign", "")
136
137
138         local ip6prefix = s:taboption("general", Value, "ip6prefix", translate("IPv6 routed prefix"),
139                 translate("Public prefix routed to this device for distribution to clients."))
140         ip6prefix.datatype = "ip6addr"
141         ip6prefix:depends("ip6assign", "")
142
143         local ip6ifaceid = s:taboption("general", Value, "ip6ifaceid", translate("IPv6 suffix"),
144                 translate("Optional. Allowed values: 'eui64', 'random', fixed value like '::1' " ..
145                         "or '::1:2'. When IPv6 prefix (like 'a:b:c:d::') is received from a " ..
146                         "delegating server, use the suffix (like '::1') to form the IPv6 address " ..
147                         "('a:b:c:d::1') for the interface."))
148         ip6ifaceid.datatype = "ip6hostid"
149         ip6ifaceid.placeholder = "::1"
150         ip6ifaceid.rmempty = true
151
152 end
153
154
155 luci.tools.proto.opt_macaddr(section, ifc, translate("Override MAC address"))
156
157
158 mtu = section:taboption("advanced", Value, "mtu", translate("Override MTU"))
159 mtu.placeholder = "1500"
160 mtu.datatype    = "max(9200)"
161
162
163 metric = section:taboption("advanced", Value, "metric",
164         translate("Use gateway metric"))
165
166 metric.placeholder = "0"
167 metric.datatype    = "uinteger"