1544d18f5ecabc48983015bbe0ee21ddfe81d23e
[feed/routing.git] / bird1-openwrt / bird1-ipv4-openwrt / src / model / gen_proto.lua
1 --[[
2 Copyright (C) 2014-2017 - Eloi Carbo
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 --]]
17
18 require("luci.sys")
19 local http = require "luci.http"
20 local uci = luci.model.uci.cursor()
21
22 -- Repeated Strings
23 local common_string = "Valid options are:<br />" .. "1. all (All the routes)<br />" .. "2. none (No routes)<br />" .. "3. filter <b>Your_Filter_Name</b> (Call a specific filter from any of the available in the filters files)"
24 local imp_string = "Set if the protocol must import routes.<br />" .. common_string
25 local exp_string = "Set if the protocol must export routes.<br />" .. common_string
26
27 m=Map("bird4", "Bird4 general protocol's configuration.")
28
29 -- Optional parameters lists
30 local protoptions = {
31 {["name"]="table", ["help"]="Auxiliar table for routing", ["depends"]={"static","kernel"}},
32 {["name"]="import", ["help"]=imp_string, ["depends"]={"kernel"}},
33 {["name"]="export", ["help"]=exp_string, ["depends"]={"kernel"}},
34 {["name"]="scan_time", ["help"]="Time between scans", ["depends"]={"kernel","device"}},
35 {["name"]="kernel_table", ["help"]="Set which table must be used as auxiliar kernel table", ["depends"]={"kernel"}},
36 {["name"]="learn", ["help"]="Learn routes", ["depends"]={"kernel"}},
37 {["name"]="persist", ["help"]="Store routes. After a restart, routes willstill be configured", ["depends"]={"kernel"}}
38 }
39
40 local routeroptions = {
41 {["name"]="prefix",["help"]="",["depends"]={"router","special","iface","multipath","recursive"}},
42 {["name"]="via",["help"]="",["depends"]={"router","multipath"}},
43 {["name"]="attribute",["help"]="",["depends"]={"special"}},
44 {["name"]="iface",["help"]="",["depends"]={"iface"}},
45 {["name"]="ip",["help"]="",["depends"]={"recursive"}}
46 }
47
48
49 --
50 -- KERNEL PROTOCOL
51 --
52 sect_kernel_protos = m:section(TypedSection, "kernel", "Kernel options", "Configuration of the kernel protocols. First Instance MUST be Primary table (no table or kernel_table fields).")
53 sect_kernel_protos.addremove = true
54 sect_kernel_protos.anonymous = false
55
56 -- Default kernel parameters
57 disabled = sect_kernel_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
58 disabled.default=0
59
60 -- Optional parameters
61 for _,o in ipairs(protoptions) do
62 if o.name ~= nil then
63 for _, d in ipairs(o.depends) do
64 if d == "kernel" then
65 if o.name == "learn" or o.name == "persist" then
66 value = sect_kernel_protos:option(Flag, o.name, translate(o.name), translate(o.help))
67 elseif o.name == "table" then
68 value = sect_kernel_protos:option(ListValue, o.name, translate(o.name), translate(o.help))
69 uci:foreach("bird4", "table",
70 function (s)
71 value:value(s.name)
72 end)
73 value:value("")
74 value.default = ""
75 else
76 value = sect_kernel_protos:option(Value, o.name, translate(o.name), translate(o.help))
77 end
78 value.optional = true
79 value.rmempty = true
80 end
81 end
82
83 end
84 end
85
86
87 --
88 -- DEVICE PROTOCOL
89 --
90 sect_device_protos = m:section(TypedSection, "device", "Device options", "Configuration of the device protocols.")
91 sect_device_protos.addremove = true
92 sect_device_protos.anonymous = false
93
94 -- Default kernel parameters
95
96 disabled = sect_device_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
97 disabled.default=0
98
99 -- Optional parameters
100 for _,o in ipairs(protoptions) do
101 if o.name ~= nil then
102 for _, d in ipairs(o.depends) do
103 if d == "device" then
104 value = sect_device_protos:option(Value, o.name, translate(o.name), translate(o.help))
105 value.optional = true
106 value.rmempty = true
107 end
108 end
109 end
110 end
111
112
113 --
114 -- PIPE PROTOCOL
115 --
116 sect_pipe_protos = m:section(TypedSection, "pipe", "Pipe options", "Configuration of the Pipe protocols.")
117 sect_pipe_protos.addremove = true
118 sect_pipe_protos.anonymous = false
119
120 -- Default Pipe parameters
121 disabled = sect_pipe_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured. This protocol will connect the configured 'Table' to the 'Peer Table'.")
122 disabled.default=0
123
124 table = sect_pipe_protos:option(ListValue, "table", "Table", "Select the Primary Table to connect.")
125 table.optional = false
126 uci:foreach("bird4", "table",
127 function (s)
128 table:value(s.name)
129 end)
130 table:value("")
131 table.default = ""
132
133 peer_table = sect_pipe_protos:option(ListValue, "peer_table", "Peer Table", "Select the Secondary Table to connect.")
134 table.optional = false
135 uci:foreach("bird4", "table",
136 function (s)
137 peer_table:value(s.name)
138 end)
139 peer_table:value("")
140 peer_table.default = ""
141
142 mode = sect_pipe_protos:option(ListValue, "mode", "Mode", "Select <b>transparent</b> to retransmit all routes and their attributes<br />Select <b>opaque</b> to retransmit optimal routes (similar to what other protocols do)")
143 mode.optional = false
144 mode:value("transparent")
145 mode:value("opaque")
146 mode.default = "transparent"
147
148 import = sect_pipe_protos:option(Value, "import", "Import",imp_string)
149 import.optional=true
150
151 export = sect_pipe_protos:option(Value, "export", "Export", exp_string)
152 export.optional=true
153
154
155 --
156 -- DIRECT PROTOCOL
157 --
158 sect_direct_protos = m:section(TypedSection, "direct", "Direct options", "Configuration of the Direct protocols.")
159 sect_direct_protos.addremove = true
160 sect_direct_protos.anonymous = false
161
162 -- Default Direct parameters
163 disabled = sect_direct_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured. This protocol will connect the configured 'Table' to the 'Peer Table'.")
164 disabled.optional = false
165 disabled.default = 0
166
167 interface = sect_direct_protos:option(Value, "interface", "Interfaces", "By default Direct will generate device routes for all the interfaces. To restrict this behaviour, select a number of patterns to match your desired interfaces:" .. "<br />" .. "1. All the strings <b>MUST</b> be quoted: \"pattern\"" .. "<br />" .. "2. Use * (star) to match patterns: \"eth*\" (<b>include</b> all eth... interfaces)" .. "<br />" .. "3. You can add \"-\" (minus) to exclude patterns: \"-em*\" (<b>exclude</b> all em... interfaces)." .. "<br />" .. "4. Separate several patterns using , (coma): \"-em*\", \"eth*\" (<b>exclude</b> em... and <b>include</b> all eth... interfaces).")
168 interface.optional = false
169 interface.default = "\"*\""
170
171
172 --
173 -- STATIC PROTOCOL
174 --
175 sect_static_protos = m:section(TypedSection, "static", "Static options", "Configuration of the static protocols.")
176 sect_static_protos.addremove = true
177 sect_static_protos.anonymous = false
178
179 -- Default kernel parameters
180 disabled = sect_static_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
181 disabled.default=0
182
183 -- Optional parameters
184 for _,o in ipairs(protoptions) do
185 if o.name ~= nil then
186 for _, d in ipairs(o.depends) do
187 if d == "static" then
188 if o.name == "table" then
189 value = sect_static_protos:option(ListValue, o.name, translate(o.name), translate(o.help))
190 uci:foreach("bird4", "table",
191 function (s)
192 value:value(s.name)
193 end)
194 value:value("")
195 value.default = ""
196 else
197 value = sect_static_protos:option(Value, o.name, translate(o.name), translate(o.help))
198 end
199 value.optional = true
200 value.rmempty = true
201 end
202 end
203 end
204 end
205
206
207 --
208 -- ROUTES FOR STATIC PROTOCOL
209 --
210 sect_routes = m:section(TypedSection, "route", "Routes configuration", "Configuration of the routes used in static protocols.")
211 sect_routes.addremove = true
212 sect_routes.anonymous = true
213
214 instance = sect_routes:option(ListValue, "instance", "Route instance", "")
215 i = 0
216 uci:foreach("bird4", "static",
217 function (s)
218 instance:value(s[".name"])
219 end)
220
221 prefix = sect_routes:option(Value, "prefix", "Route prefix", "")
222
223 type = sect_routes:option(ListValue, "type", "Type of route", "")
224 type:value("router")
225 type:value("special")
226 type:value("iface")
227 type:value("recursive")
228 type:value("multipath")
229
230 valueVia = sect_routes:option(Value, "via", "Via", "")
231 valueVia.optional = false
232 valueVia:depends("type", "router")
233 valueVia.datatype = "ip4addr"
234
235 listVia = sect_routes:option(DynamicList, "l_via", "Via", "")
236 listVia:depends("type", "multipath")
237 listVia.optional=false
238 listVia.datatype = "ip4addr"
239
240 attribute = sect_routes:option(ListValue, "attribute", "Attribute", "")
241 attribute:depends("type", "special")
242 attribute:value("unreachable")
243 attribute:value("prohibit")
244 attribute:value("blackhole")
245
246 iface = sect_routes:option(ListValue, "iface", "Interface", "")
247 iface:depends("type", "iface")
248 uci:foreach("network", "interface",
249 function(section)
250 if section[".name"] ~= "loopback" then
251 iface:value(section[".name"])
252 end
253 end)
254
255 ip = sect_routes:option(Value, "ip", "IP address", "")
256 ip:depends("type", "ip")
257 ip.datatype = [[ or"ip4addr", "ip6addr" ]]
258
259 function m.on_commit(self,map)
260 luci.sys.exec('/etc/init.d/bird4 restart')
261 end
262
263 return m