]>
Commit | Line | Data |
---|---|---|
cb965348 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 IPFire Network Development Team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | # | |
22 | # Functions for static routing. | |
23 | # | |
24 | ||
68e62bea | 25 | cli_route() { |
2212045f | 26 | if cli_help_requested "$@"; then |
68e62bea MT |
27 | cli_show_man network-route |
28 | exit ${EXIT_OK} | |
29 | fi | |
30 | ||
31 | local action=${1} | |
32 | shift | |
33 | ||
34 | case "${action}" in | |
35 | static) | |
2212045f | 36 | cli_route_static "$@" |
68e62bea MT |
37 | ;; |
38 | *) | |
39 | error "Unrecognized action: ${action}" | |
40 | cli_run_help network route | |
41 | ||
42 | exit ${EXIT_ERROR} | |
43 | ;; | |
44 | esac | |
45 | ||
46 | exit ${EXIT_OK} | |
47 | } | |
48 | ||
49 | cli_route_static() { | |
2212045f | 50 | if cli_help_requested "$@"; then |
68e62bea MT |
51 | cli_show_man network-route-static |
52 | exit ${EXIT_OK} | |
53 | fi | |
54 | ||
55 | local action=${1} | |
56 | shift | |
57 | ||
58 | case "${action}" in | |
59 | # Add a new route. | |
60 | add) | |
2212045f | 61 | route_add "$@" |
68e62bea MT |
62 | ;; |
63 | # Remove an existing route. | |
64 | remove) | |
2212045f | 65 | route_remove "$@" |
68e62bea MT |
66 | ;; |
67 | # List all routes. | |
68 | list) | |
2212045f | 69 | route_list "$@" |
68e62bea MT |
70 | return ${EXIT_OK} |
71 | ;; | |
72 | # Reload all routes | |
73 | reload) | |
2212045f | 74 | route_apply "$@" |
68e62bea MT |
75 | ;; |
76 | *) | |
77 | error "Unrecognized action: ${action}" | |
78 | cli_run_help network route | |
79 | ||
80 | exit ${EXIT_ERROR} | |
81 | ;; | |
82 | esac | |
83 | ||
84 | # Applying all routes. | |
85 | route_apply | |
86 | ||
87 | exit ${EXIT_OK} | |
88 | } | |
89 | ||
1c6a4e30 | 90 | route_add() { |
cb965348 MT |
91 | local ${NETWORK_CONFIG_ROUTES_PARAMS} |
92 | ||
93 | while [ $# -gt 0 ]; do | |
94 | case "${1}" in | |
95 | --gateway=*) | |
2212045f | 96 | gateway=$(cli_get_val "${1}") |
cb965348 MT |
97 | ;; |
98 | --unreachable) | |
99 | unreachable="true" | |
100 | ;; | |
55ea0266 MT |
101 | --prohibit) |
102 | prohibit="true" | |
103 | ;; | |
104 | --blackhole) | |
105 | blackhole="true" | |
106 | ;; | |
478de6f9 | 107 | --mtu=*) |
2212045f | 108 | mtu=$(cli_get_val "${1}") |
478de6f9 | 109 | ;; |
cb965348 | 110 | *) |
47fe7766 JS |
111 | if isset network; then |
112 | error "Bad number of arguments. Network passed twice or more" | |
113 | return ${EXIT_ERROR} | |
114 | else | |
115 | network=${1} | |
116 | fi | |
cb965348 MT |
117 | ;; |
118 | esac | |
119 | shift | |
120 | done | |
121 | ||
122 | assert isset network | |
123 | ||
8b0958b2 | 124 | if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then |
cb965348 MT |
125 | error "The given network is invalid: ${network}" |
126 | return ${EXIT_ERROR} | |
127 | fi | |
128 | ||
129 | if route_find_duplicate ${network}; then | |
130 | error "A route to ${network} does already exist." | |
131 | return ${EXIT_ERROR} | |
132 | fi | |
133 | ||
134 | # Check if gateway and unreachable are both enabled. | |
cb965348 | 135 | if isset gateway; then |
55ea0266 MT |
136 | if enabled unreachable; then |
137 | error "You cannot use both, --gateway=${gateway} and --unreachable at the same time." | |
138 | return ${EXIT_ERROR} | |
139 | fi | |
140 | ||
141 | if enabled prohibit; then | |
142 | error "You cannot use both, --gateway=${gateway} and --prohibit at the same time." | |
143 | return ${EXIT_ERROR} | |
144 | fi | |
145 | ||
146 | if enabled blackhole; then | |
147 | error "You cannot use both, --gateway=${gateway} and --blackhole at the same time." | |
148 | return ${EXIT_ERROR} | |
149 | fi | |
150 | ||
151 | # Check if network and gateway IP protocol version match. | |
d2021e87 MT |
152 | if ! ip_is_valid ${gateway}; then |
153 | error "--gateway= is not a valid IP address." | |
154 | return ${EXIT_ERROR} | |
155 | fi | |
156 | ||
4a7c3c02 MT |
157 | # Check if the gateway is part of the statically routed network |
158 | if ip_network_is_subset_of ${gateway} ${network}; then | |
159 | error "The gateway is in the routed network" | |
160 | return ${EXIT_ERROR} | |
161 | fi | |
162 | ||
cb965348 | 163 | local network_proto=$(ip_detect_protocol ${network}) |
b449504e JS |
164 | assert isset network_proto |
165 | ||
cb965348 | 166 | local gateway_proto=$(ip_detect_protocol ${gateway}) |
b449504e | 167 | assert isset gateway_proto |
cb965348 MT |
168 | |
169 | if [ "${network_proto}" != "${gateway_proto}" ]; then | |
170 | error "The IP protocol version of the given network and gateway did not match." | |
171 | return ${EXIT_ERROR} | |
172 | fi | |
55ea0266 MT |
173 | |
174 | else | |
175 | local counter=$(list_count true ${unreachable} ${prohibit} ${blackhole}) | |
176 | if [ ${counter} -gt 1 ]; then | |
177 | error "You can only use one of --unreachable, --prohibit or --blackhole." | |
178 | return ${EXIT_ERROR} | |
179 | fi | |
cb965348 MT |
180 | fi |
181 | ||
478de6f9 MT |
182 | if isset mtu && ! isinteger mtu; then |
183 | error "MTU must be an integer number: ${mtu}" | |
184 | return ${EXIT_ERROR} | |
185 | fi | |
186 | ||
cb965348 MT |
187 | local line |
188 | list_append line "network=\"${network}\"" | |
189 | ||
190 | # Add gateway to configuration entry when it is set. | |
191 | if isset gateway; then | |
192 | list_append line "gateway=\"${gateway}\"" | |
193 | fi | |
194 | ||
195 | # Add unreachable to configuration entry when it is set. | |
55ea0266 MT |
196 | local arg |
197 | for arg in unreachable prohibit blackhole; do | |
198 | if enabled ${arg}; then | |
199 | list_append line "${arg}=\"true\"" | |
200 | break | |
201 | fi | |
202 | done | |
cb965348 | 203 | |
478de6f9 MT |
204 | # Add MTU (if set). |
205 | if isset mtu; then | |
206 | list_append line "mtu=\"${mtu}\"" | |
207 | fi | |
208 | ||
cb965348 MT |
209 | # Write line to file. |
210 | print "${line}" >> ${NETWORK_CONFIG_ROUTES} | |
211 | ||
212 | log INFO "New route to network '${network}' has been added." | |
213 | return ${EXIT_OK} | |
214 | } | |
215 | ||
1c6a4e30 | 216 | route_remove() { |
e618256e JS |
217 | local _network |
218 | local error=${EXIT_OK} | |
219 | ||
2212045f | 220 | for _network in "$@"; do |
e618256e | 221 | # Validate input |
8b0958b2 | 222 | if ! ip_net_is_valid ${_network} && ! ip_is_valid ${_network}; then |
e618256e JS |
223 | error "Invalid IP address or network: ${_network}" |
224 | error=${EXIT_ERROR} | |
f04e7377 | 225 | continue |
cb965348 MT |
226 | fi |
227 | ||
e618256e JS |
228 | local found="false" |
229 | ||
230 | local ${NETWORK_CONFIG_ROUTES_PARAMS} | |
231 | local line | |
232 | while read line; do | |
233 | route_parse_line ${line} | |
234 | [ $? -eq ${EXIT_OK} ] || continue | |
235 | ||
236 | # Skip the rule, we want to delete. | |
237 | if [ "${network}" = "${_network}" ]; then | |
238 | found="true" | |
239 | continue | |
240 | fi | |
241 | ||
242 | print "${line}" | |
243 | done < ${NETWORK_CONFIG_ROUTES} > ${NETWORK_CONFIG_ROUTES}.tmp | |
244 | mv ${NETWORK_CONFIG_ROUTES}{.tmp,} | |
245 | ||
246 | if enabled found; then | |
247 | log INFO "Route to network '${_network}' has been removed." | |
248 | else | |
249 | error "No route to network '${_network}' was found." | |
250 | error=${EXIT_ERROR} | |
251 | fi | |
252 | done | |
cb965348 | 253 | |
e618256e | 254 | return ${error} |
cb965348 MT |
255 | } |
256 | ||
1c6a4e30 | 257 | route_list() { |
cb965348 MT |
258 | local protocol |
259 | ||
260 | while [ $# -gt 0 ]; do | |
261 | case "${1}" in | |
262 | --protocol=*) | |
2212045f | 263 | protocol=$(cli_get_val "${1}") |
cb965348 MT |
264 | ;; |
265 | *) | |
266 | warning "Unrecognized argument: ${1}" | |
267 | ;; | |
268 | esac | |
269 | shift | |
270 | done | |
271 | ||
272 | if [ ! -r "${NETWORK_CONFIG_ROUTES}" ]; then | |
273 | print "No static routes defined." | |
274 | return ${EXIT_OK} | |
275 | fi | |
276 | ||
478de6f9 MT |
277 | local format="%-40s %-20s %-4s" |
278 | print "${format}" "NETWORK/HOST" "GATEWAY" "MTU" | |
cb965348 MT |
279 | |
280 | local ${NETWORK_CONFIG_ROUTES_PARAMS} | |
281 | local line | |
282 | while read line; do | |
283 | route_parse_line ${line} | |
284 | [ $? -eq ${EXIT_OK} ] || continue | |
285 | ||
55ea0266 MT |
286 | local arg |
287 | for arg in unreachable prohibit blackhole; do | |
288 | if enabled ${arg}; then | |
289 | gateway="<${arg}>" | |
290 | break | |
291 | fi | |
292 | done | |
cb965348 MT |
293 | |
294 | # Filter all entries with a wrong protocol. | |
295 | if isset protocol; then | |
296 | local proto=$(ip_detect_protocol ${network}) | |
297 | [ "${protocol}" = "${proto}" ] || continue | |
298 | fi | |
299 | ||
478de6f9 MT |
300 | # Print something when no MTU was set. |
301 | if ! isset mtu; then | |
302 | mtu="-" | |
303 | fi | |
304 | ||
305 | print "${format}" "${network}" "${gateway}" "${mtu}" | |
cb965348 MT |
306 | done < ${NETWORK_CONFIG_ROUTES} |
307 | } | |
308 | ||
1c6a4e30 | 309 | route_find_duplicate() { |
cb965348 MT |
310 | local _network=${1} |
311 | ||
312 | [ -r "${NETWORK_CONFIG_ROUTES}" ] || return ${EXIT_FALSE} | |
313 | ||
314 | local ${NETWORK_CONFIG_ROUTES_PARAMS} | |
315 | local line | |
316 | while read line; do | |
317 | route_parse_line ${line} | |
d2021e87 | 318 | [ $? -eq ${EXIT_OK} ] || continue |
cb965348 MT |
319 | |
320 | # Check if the network is already in use. | |
321 | [ "${network}" = "${_network}" ] && return ${EXIT_TRUE} | |
322 | done < ${NETWORK_CONFIG_ROUTES} | |
323 | ||
324 | return ${EXIT_FALSE} | |
325 | } | |
326 | ||
1c6a4e30 | 327 | route_parse_line() { |
cb965348 MT |
328 | local arg |
329 | ||
330 | # Reset all possible settings. | |
331 | for arg in ${NETWORK_CONFIG_ROUTES_PARAMS}; do | |
332 | printf -v ${arg} "%s" "" | |
333 | done | |
334 | ||
335 | while read arg; do | |
336 | case "${arg}" in | |
337 | network=*) | |
2212045f | 338 | network=$(cli_get_val "${arg}") |
cb965348 MT |
339 | ;; |
340 | gateway=*) | |
2212045f | 341 | gateway=$(cli_get_val "${arg}") |
cb965348 MT |
342 | ;; |
343 | unreachable=*) | |
2212045f | 344 | unreachable=$(cli_get_val "${arg}") |
cb965348 | 345 | ;; |
55ea0266 | 346 | prohibit=*) |
2212045f | 347 | prohibit=$(cli_get_val "${arg}") |
55ea0266 MT |
348 | ;; |
349 | blackhole=*) | |
2212045f | 350 | blackhole=$(cli_get_val "${arg}") |
55ea0266 | 351 | ;; |
478de6f9 | 352 | mtu=*) |
2212045f | 353 | mtu=$(cli_get_val "${arg}") |
478de6f9 | 354 | ;; |
cb965348 | 355 | esac |
2212045f | 356 | done <<< "$(args "$@")" |
cb965348 MT |
357 | |
358 | ### Check if all values are correctly set. | |
359 | ||
360 | # network must be set. | |
361 | isset network || return ${EXIT_ERROR} | |
362 | ||
146b2b81 | 363 | # Is network or IP valid? |
8b0958b2 | 364 | if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then |
146b2b81 JS |
365 | error "The given network is invalid: ${network}" |
366 | return ${EXIT_ERROR} | |
367 | fi | |
cb965348 MT |
368 | |
369 | # Check gateway settings. | |
370 | if isset gateway; then | |
371 | # When gateway is set, unreachable cannot be set. | |
372 | isset unreachable && return ${EXIT_ERROR} | |
373 | ||
374 | # Must be a valid IP address. | |
375 | ip_is_valid ${gateway} || return ${EXIT_ERROR} | |
4a7c3c02 MT |
376 | |
377 | # Check if the gateway is part of the statically routed network | |
378 | if ip_network_is_subset_of ${gateway} ${network}; then | |
379 | return ${EXIT_ERROR} | |
380 | fi | |
cb965348 | 381 | else |
55ea0266 MT |
382 | # Check if exactly one of unreachable, prohibit or blackhole is set. |
383 | local counter=$(list_count true ${unreachable} ${prohibit} ${blackhole}) | |
384 | [ ${counter} -eq 1 ] || return ${EXIT_ERROR} | |
cb965348 MT |
385 | fi |
386 | ||
478de6f9 MT |
387 | # mtu must be an integer number. |
388 | if isset mtu; then | |
389 | isinteger mtu || return ${EXIT_ERROR} | |
390 | fi | |
391 | ||
cb965348 MT |
392 | return ${EXIT_OK} |
393 | } | |
d2021e87 | 394 | |
1c6a4e30 | 395 | route_apply() { |
39beacd0 MT |
396 | # Update bird |
397 | bird_update | |
d2021e87 MT |
398 | } |
399 | ||
1c6a4e30 | 400 | route_entry_add() { |
d2021e87 MT |
401 | local gateway |
402 | local network | |
403 | local proto | |
404 | local table | |
405 | local type="unicast" | |
478de6f9 | 406 | local mtu |
d2021e87 MT |
407 | |
408 | local command | |
409 | ||
410 | while [ $# -gt 0 ]; do | |
411 | case "${1}" in | |
412 | --gateway=*) | |
2212045f | 413 | gateway=$(cli_get_val "${1}") |
d2021e87 MT |
414 | ;; |
415 | --table=*) | |
2212045f | 416 | table=$(cli_get_val "${1}") |
d2021e87 MT |
417 | ;; |
418 | --type=*) | |
2212045f | 419 | type=$(cli_get_val "${1}") |
d2021e87 MT |
420 | ;; |
421 | --proto=*) | |
2212045f | 422 | proto=$(cli_get_val "${1}") |
d2021e87 | 423 | ;; |
478de6f9 | 424 | --mtu=*) |
2212045f | 425 | mtu=$(cli_get_val "${1}") |
478de6f9 | 426 | ;; |
d2021e87 MT |
427 | *) |
428 | if isset network; then | |
429 | warning "Unrecognized argument: ${1}" | |
430 | else | |
431 | network=${1} | |
432 | fi | |
433 | ;; | |
434 | esac | |
435 | shift | |
436 | done | |
437 | ||
438 | # Validate input. | |
439 | assert isoneof type unicast broadcast unreachable prohibit blackhole | |
146b2b81 JS |
440 | |
441 | assert isset network | |
442 | ||
8b0958b2 | 443 | if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then |
146b2b81 JS |
444 | error "The given network is invalid: ${network}" |
445 | return ${EXIT_ERROR} | |
446 | fi | |
447 | ||
478de6f9 MT |
448 | if isset mtu; then |
449 | assert isinteger mtu | |
450 | fi | |
d2021e87 MT |
451 | |
452 | # Detect the protocol of the given network. | |
453 | local protocol=$(ip_detect_protocol ${network}) | |
146b2b81 | 454 | |
d2021e87 MT |
455 | case "${protocol}" in |
456 | ipv6) | |
457 | command="ip -6 route add" | |
458 | ;; | |
459 | ipv4) | |
460 | command="ip route add" | |
461 | ;; | |
1008bbb9 MT |
462 | *) |
463 | log ERROR "Could not detect protocol for ${network}" | |
464 | return ${EXIT_ERROR} | |
465 | ;; | |
d2021e87 | 466 | esac |
d2021e87 MT |
467 | |
468 | # Add type. | |
469 | list_append command "${type}" | |
470 | ||
471 | # Add network/prefix. | |
472 | list_append command "${network}" | |
473 | ||
474 | if [ "${type}" = "unicast" ]; then | |
475 | assert isset gateway | |
476 | assert ip_is_valid ${gateway} | |
477 | ||
478 | list_append command "via ${gateway}" | |
479 | fi | |
480 | ||
481 | # Add table (if any). | |
482 | if isset table; then | |
483 | # Create routing table, if it does not exist, yet. | |
484 | route_table_create ${table} | |
485 | ||
486 | list_append command "table ${table}" | |
487 | fi | |
488 | ||
489 | # Add proto. | |
490 | if isset proto; then | |
491 | list_append command "proto ${proto}" | |
492 | fi | |
493 | ||
478de6f9 MT |
494 | # Add MTU. |
495 | if isset mtu; then | |
496 | list_append command "mtu ${mtu}" | |
497 | fi | |
498 | ||
a6c3c15c | 499 | cmd "${command}" |
d2021e87 MT |
500 | } |
501 | ||
1c6a4e30 | 502 | route_table_create() { |
d2021e87 MT |
503 | local table=${1} |
504 | assert isset table | |
505 | ||
506 | if route_table_exists ${table}; then | |
507 | return ${EXIT_OK} | |
508 | fi | |
509 | ||
510 | # Get the next free id. | |
511 | local id=$(_route_table_next_id) | |
512 | assert isset id | |
513 | ||
514 | # Write everything to file. | |
515 | print "%d\t%s" "${id}" "${table}" >> /etc/iproute2/rt_tables | |
516 | ||
517 | log DEBUG "Created routing table '${table}'." | |
518 | ||
519 | return ${EXIT_OK} | |
520 | } | |
521 | ||
1c6a4e30 | 522 | _route_table_next_id() { |
d2021e87 MT |
523 | # The Linux kernel is able to manage 255 routing tables (1-255). |
524 | # This function returns the next free id, starting from 255. | |
525 | local next_id | |
526 | ||
527 | for next_id in {255..1}; do | |
528 | if ! route_table_exists --id="${next_id}"; then | |
529 | print "${next_id}" | |
530 | return ${EXIT_OK} | |
531 | fi | |
532 | done | |
533 | ||
534 | return ${EXIT_FALSE} | |
535 | } | |
536 | ||
1c6a4e30 | 537 | route_table_flush() { |
d2021e87 MT |
538 | local protocol |
539 | local table | |
540 | ||
541 | while [ $# -gt 0 ]; do | |
542 | case "${1}" in | |
543 | --protocol=*) | |
2212045f | 544 | protocol=$(cli_get_val "${1}") |
d2021e87 MT |
545 | ;; |
546 | *) | |
547 | table="${1}" | |
548 | ;; | |
549 | esac | |
550 | shift | |
551 | done | |
552 | ||
553 | # If the table does not exists, there is nothing to | |
554 | # flush. | |
555 | route_table_exists ${table} || return ${EXIT_OK} | |
556 | ||
557 | local command | |
558 | local proto | |
559 | for proto in ${IP_SUPPORTED_PROTOCOLS}; do | |
560 | # Skip unwanted protocols. | |
561 | if isset protocol; then | |
562 | [ "${protocol}" = "${proto}" ] || continue | |
563 | fi | |
564 | ||
565 | command="" | |
566 | case "${proto}" in | |
567 | ipv6) | |
568 | command="ip -6 route flush" | |
569 | ;; | |
570 | ipv4) | |
571 | command="ip route flush" | |
572 | ;; | |
573 | esac | |
574 | assert isset command | |
575 | ||
576 | list_append command "table ${table}" | |
577 | ||
578 | # Execute command. | |
579 | cmd "${command}" | |
580 | done | |
581 | ||
582 | return ${EXIT_OK} | |
583 | } | |
584 | ||
1c6a4e30 | 585 | route_table_exists() { |
d2021e87 MT |
586 | local _id _table |
587 | ||
588 | while [ $# -gt 0 ]; do | |
589 | case "${1}" in | |
590 | --id=*) | |
2212045f | 591 | _id=$(cli_get_val "${1}") |
d2021e87 MT |
592 | ;; |
593 | *) | |
594 | _table=${1} | |
595 | break | |
596 | ;; | |
597 | esac | |
598 | shift | |
599 | done | |
600 | ||
601 | local id table | |
602 | while read -r id table; do | |
603 | # Skip all comments. | |
604 | [ "${id:0:1}" = "#" ] && continue | |
605 | ||
606 | if [ "${_table}" = "${table}" ] || [ "${_id}" = "${id}" ]; then | |
607 | # Found a match. | |
608 | return ${EXIT_TRUE} | |
609 | fi | |
610 | done < /etc/iproute2/rt_tables | |
611 | ||
612 | return ${EXIT_FALSE} | |
613 | } | |
614 | ||
1c6a4e30 | 615 | route_rule_add() { |
d2021e87 MT |
616 | local priority |
617 | local protocols=${IP_SUPPORTED_PROTOCOLS} | |
618 | local lookup | |
619 | ||
620 | while [ $# -gt 0 ]; do | |
621 | case "${1}" in | |
622 | --lookup=*) | |
2212045f | 623 | lookup=$(cli_get_val "${1}") |
d2021e87 MT |
624 | ;; |
625 | --priority=*) | |
2212045f | 626 | priority=$(cli_get_val "${1}") |
d2021e87 MT |
627 | ;; |
628 | --protocol=*) | |
2212045f | 629 | protocols=$(cli_get_val "${1}") |
d2021e87 MT |
630 | |
631 | assert isoneof protocols ${IP_SUPPORTED_PROTOCOLS} | |
632 | ;; | |
633 | *) | |
634 | warning "Unhandled argument: ${1}" | |
635 | ;; | |
636 | esac | |
637 | shift | |
638 | done | |
639 | ||
640 | local command options | |
641 | ||
642 | if isset lookup; then | |
643 | route_table_create ${lookup} | |
644 | ||
645 | list_append options "lookup ${lookup}" | |
646 | fi | |
647 | ||
648 | if isset priority; then | |
649 | assert isinteger priority | |
650 | ||
651 | list_append options "prio ${priority}" | |
652 | fi | |
653 | ||
654 | local proto | |
655 | for proto in ${protocols}; do | |
656 | command= | |
657 | case "${proto}" in | |
658 | ipv6) | |
659 | command="ip -6 rule add ${options}" | |
660 | ;; | |
661 | ipv4) | |
662 | command="ip rule add ${options}" | |
663 | ;; | |
664 | esac | |
665 | assert isset command | |
666 | ||
667 | # Skip, if the rule does already exist. | |
668 | route_rule_exists \ | |
669 | --protocol=${proto} \ | |
670 | --lookup=${lookup} \ | |
671 | --priority=${priority} \ | |
672 | && continue | |
673 | ||
674 | cmd "${command}" | |
675 | done | |
676 | } | |
677 | ||
1c6a4e30 | 678 | route_rule_exists() { |
d2021e87 MT |
679 | local from |
680 | local lookup | |
681 | local proto | |
682 | local prio | |
683 | ||
684 | while [ $# -gt 0 ]; do | |
685 | case "${1}" in | |
686 | --from=*) | |
2212045f | 687 | from=$(cli_get_val "${1}") |
d2021e87 MT |
688 | ;; |
689 | --lookup=*) | |
2212045f | 690 | lookup=$(cli_get_val "${1}") |
d2021e87 MT |
691 | ;; |
692 | --priority=*) | |
2212045f | 693 | prio=$(cli_get_val "${1}") |
d2021e87 MT |
694 | ;; |
695 | --protocol=*) | |
2212045f | 696 | proto=$(cli_get_val "${1}") |
d2021e87 MT |
697 | ;; |
698 | *) | |
699 | warning "Unrecognized argument: ${1}" | |
700 | ;; | |
701 | esac | |
702 | shift | |
703 | done | |
704 | ||
705 | local command | |
706 | case "${proto}" in | |
707 | ipv6) | |
708 | command="ip -6 rule show" | |
709 | ;; | |
710 | ipv4) | |
711 | command="ip rule show" | |
712 | ;; | |
713 | esac | |
714 | assert isset command | |
715 | ||
716 | local _lookup _from _prio | |
717 | local line | |
718 | while read -r line; do | |
719 | _route_rule_exists_parse ${line} | |
720 | ||
721 | if isset from; then | |
722 | [ "${from}" = "${_from}" ] || continue | |
723 | fi | |
724 | ||
725 | if isset prio; then | |
726 | [ "${prio}" = "${_prio}" ] || continue | |
727 | fi | |
728 | ||
729 | if isset lookup; then | |
730 | [ "${lookup}" = "${_lookup}" ] || continue | |
731 | fi | |
732 | ||
733 | return ${EXIT_TRUE} | |
734 | done <<< "$(${command})" | |
735 | ||
736 | return ${EXIT_FALSE} | |
737 | } | |
738 | ||
1c6a4e30 | 739 | _route_rule_exists_parse() { |
d2021e87 MT |
740 | # Reset all variables. |
741 | _lookup= | |
742 | _from= | |
743 | _prio= | |
744 | ||
745 | while [ $# -gt 0 ]; do | |
746 | case "${1}" in | |
747 | lookup) | |
748 | _lookup=${2} | |
749 | shift 2 | |
750 | ;; | |
751 | from) | |
752 | _from=${2} | |
753 | shift 2 | |
754 | ;; | |
755 | *:) | |
756 | _prio=${1//:/} | |
757 | shift | |
758 | ;; | |
759 | *) | |
760 | # Skip unknown arguments. | |
761 | shift | |
762 | ;; | |
763 | esac | |
764 | done | |
765 | } |