1 | 7 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | 10 | simandl | # release date : 26/01/2006 |
4 | 7 | simandl | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 10 | simandl | sl_version="0.0.4pre1" |
9 | 7 | simandl | |
10 | | | PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
11 | | | |
12 | | | sl_nmcnf="sedlo.conf" |
13 | | | sl_sedlocnf="/etc/$sl_nmcnf" |
14 | | | sl_sedlocache="/var/cache/sedlo" |
15 | | | |
16 | | | sl_rttab="/etc/iproute2/rt_tables" |
17 | | | sl_rtnmin=110 |
18 | | | sl_rtnmax=200 |
19 | | | sl_ipnodef="10.0.0.0/8" |
20 | | | |
21 | | | slm_unknown="Nezname parametry : " |
22 | | | |
23 | | | sl_ipcmd=`which ip` |
24 | | | sl_trcmd=`which tr` |
25 | | | sl_wgetcmd=`which wget` |
26 | | | sl_hnmcmd=`which hostname` |
27 | | | sl_awkcmd=`which awk` |
28 | | | sl_catcmd=`which cat` |
29 | | | sl_grepcmd=`which grep` |
30 | 10 | simandl | sl_diffcmd=`which diff` |
31 | 7 | simandl | |
32 | | | if [ -e $sl_sedlocnf ] |
33 | | | then |
34 | | | sl_nop=1 |
35 | | | else |
36 | | | echo "$sl_sedlocnf not found" |
37 | | | exit 1 |
38 | | | fi |
39 | | | |
40 | | | if [ -e $sl_rttab ] |
41 | | | then |
42 | | | sl_nop=1 |
43 | | | else |
44 | | | echo "$sl_rttab not found" |
45 | | | exit 1 |
46 | | | fi |
47 | | | |
48 | 9 | simandl | sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2" "$3" "$4}'` |
49 | 7 | simandl | |
50 | | | ###################################################################### |
51 | 8 | simandl | s_maru() |
52 | 7 | simandl | { |
53 | | | if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi |
54 | | | |
55 | | | sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \ |
56 | | | $sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq` |
57 | | | |
58 | | | sl_merged=`echo $sl_rules $sl_ips | sort | uniq` |
59 | | | |
60 | | | echo $sl_merged |
61 | | | |
62 | 8 | simandl | } # s_maru |
63 | 7 | simandl | ###################################################################### |
64 | | | s_flru() |
65 | | | { |
66 | | | if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi |
67 | | | |
68 | | | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'` |
69 | | | |
70 | | | for sl_rule in $sl_rules |
71 | | | do |
72 | | | # sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'` |
73 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
74 | | | $sl_ipcmd ru del $sl_ipgws |
75 | | | done |
76 | | | |
77 | 8 | simandl | #this should make faster applying of new routing tables |
78 | | | $sl_ipcmd ro flush cache |
79 | | | |
80 | 7 | simandl | } # s_flru |
81 | 8 | simandl | |
82 | 7 | simandl | ###################################################################### |
83 | 10 | simandl | # here we get each ip and we create a rule to send this ip to a |
84 | | | # certain table |
85 | | | # this routine can be skipped when the number of ips and ip directions |
86 | | | # are still the same = old and new configs are the same |
87 | 7 | simandl | s_fillrules() |
88 | | | { |
89 | 8 | simandl | |
90 | | | #flush old rules before filling new ones |
91 | | | #not so nice solution - it is planned to change just what's necessary |
92 | | | #by s_maru |
93 | 7 | simandl | s_flru |
94 | | | |
95 | 10 | simandl | if [ $scm_info -gt 0 ]; then echo "Creating rules for ips" ; fi |
96 | 7 | simandl | |
97 | | | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
98 | | | |
99 | | | for sl_ip in $sl_ips |
100 | | | do |
101 | | | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
102 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
103 | | | sl_ok="no" |
104 | | | for sl_ipgw in $sl_ipgws |
105 | | | do |
106 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
107 | | | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
108 | | | then |
109 | | | if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
110 | | | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
111 | | | sl_ok="yes" |
112 | | | else |
113 | | | if [ "$sl_ok" = "no" ] |
114 | | | then |
115 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
116 | | | else |
117 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
118 | | | fi |
119 | | | fi |
120 | | | done |
121 | | | done |
122 | | | |
123 | | | $sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main |
124 | | | |
125 | 8 | simandl | #this should make faster applying of new routing tables |
126 | | | $sl_ipcmd ro flush cache |
127 | | | |
128 | 7 | simandl | } # s_fillrules |
129 | 8 | simandl | |
130 | 7 | simandl | ###################################################################### |
131 | 10 | simandl | # here we look into the main routing table for path to our iGWs |
132 | | | # and we fill these tables with two halves default nets that |
133 | | | # point to appropriate direction |
134 | | | # this routine can be skipped when the routing table is the same |
135 | 7 | simandl | s_filltables() |
136 | | | { |
137 | 10 | simandl | if [ $scm_info -gt 0 ]; then echo "Checking main routing table" ; fi |
138 | | | |
139 | 7 | simandl | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
140 | | | |
141 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3"*"$2"*"$1}'` |
142 | 7 | simandl | |
143 | | | for sl_igw in $sl_igws |
144 | | | do |
145 | | | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
146 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
147 | 9 | simandl | sl_igwtype=`echo $sl_igw | $sl_awkcmd -F '*' '{print $3}'` |
148 | 7 | simandl | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
149 | | | |
150 | 9 | simandl | # equal cost multipath detection - just first IP is taken as way to igw |
151 | 7 | simandl | if [ "$sl_igwgt x" = "zebra x" ] |
152 | | | then |
153 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'` |
154 | | | fi |
155 | | | |
156 | 9 | simandl | #if myigw then fill table for local gateway with single ip from config |
157 | | | if [ "$sl_igwtype x" = "myigw x" ] |
158 | | | then |
159 | | | sl_igwgt=$sl_igwip |
160 | | | fi |
161 | | | |
162 | 7 | simandl | if [ "$sl_igwgt x" = " x" ] |
163 | | | then |
164 | | | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi |
165 | | | else |
166 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
167 | | | if [ "$sl_tbl x" != " x" ] |
168 | | | then |
169 | | | $sl_ipcmd ro fl ta $sl_igwn |
170 | | | fi |
171 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
172 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
173 | 9 | simandl | if [ "$sl_igwtype x" = "myigw x" ] |
174 | | | then |
175 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for myigw $sl_igwn" ; fi |
176 | | | else |
177 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
178 | | | fi |
179 | 7 | simandl | fi |
180 | | | done |
181 | | | |
182 | | | } # s_filltables |
183 | 8 | simandl | |
184 | 7 | simandl | ###################################################################### |
185 | 10 | simandl | # filling rttab with tables from config |
186 | | | # only new tables are created with a new uniq number that is not important because |
187 | | | # usually we handle tables just by their names |
188 | | | # this routine acts only when a new iGW appears - only adding a table is supported |
189 | | | # no deleting is implemented because it seems to be not necessary to delete an old table |
190 | | | # because there is space enough and after reboot table will not be created |
191 | 7 | simandl | s_mktables() |
192 | | | { |
193 | 10 | simandl | if [ $scm_info -gt 0 ]; then echo "Checking tables" ; fi |
194 | | | |
195 | | | if [ $scm_info -gt 0 ]; then echo "Creating tables" ; fi |
196 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'` |
197 | 7 | simandl | for sl_igw in $sl_igws |
198 | | | do |
199 | | | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
200 | | | if [ "$sl_igwrttb x" = " x" ] |
201 | | | then |
202 | | | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
203 | | | sl_cnt="$sl_rtnmax" |
204 | | | sl_ok="no" |
205 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
206 | | | do |
207 | | | #space is used to recognized two and three digit numbers |
208 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
209 | | | if [ "$sl_igwrttb x" = " x" ] |
210 | | | then |
211 | | | sl_ok="yes" |
212 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
213 | | | fi |
214 | | | sl_cnt=$(($sl_cnt - 1 )) |
215 | | | done |
216 | | | else |
217 | | | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
218 | | | fi |
219 | 9 | simandl | done |
220 | 7 | simandl | |
221 | | | } # s_mktables |
222 | 8 | simandl | |
223 | 7 | simandl | ###################################################################### |
224 | | | s_getcfg() |
225 | | | { |
226 | | | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
227 | | | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
228 | | | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
229 | | | |
230 | 10 | simandl | rm -f "$sl_sedlocache/$sl_nmcnf.main.tmp" |
231 | 7 | simandl | |
232 | 10 | simandl | $sl_wgetcmd -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" |
233 | | | |
234 | 7 | simandl | if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] |
235 | | | then |
236 | | | date > $sl_sedlocache/last_getcnf.txt |
237 | | | cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main |
238 | 10 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config downloaded and accepted" ; fi |
239 | 7 | simandl | else |
240 | 8 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi |
241 | | | echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt |
242 | 7 | simandl | date >> $sl_sedlocache/last_getcnf.txt |
243 | | | fi |
244 | | | |
245 | 10 | simandl | # before generating a new cached config we store the old one for |
246 | | | # comparison with the new one |
247 | | | rm -f "$sl_sedlocache/$sl_nmcnf.old" |
248 | | | if [ -s $sl_sedlocache/$sl_nmcnf ] |
249 | | | then |
250 | | | cp $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old |
251 | | | else |
252 | | | touch $sl_sedlocache/$sl_nmcnf.old |
253 | | | fi |
254 | | | |
255 | 7 | simandl | # preparing cached config from local and main |
256 | | | # the local config should be processed as the second to have |
257 | | | # higher priority for rules from local config |
258 | | | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
259 | | | for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf` |
260 | | | do |
261 | | | cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
262 | | | cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
263 | | | cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
264 | | | done |
265 | 8 | simandl | |
266 | | | #local gateways taken from local config |
267 | | | cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
268 | | | |
269 | 7 | simandl | #cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
270 | | | #mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
271 | | | |
272 | 10 | simandl | sl_diffcfg=`diff $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old | grep -c .` |
273 | | | if [ $sl_diffcfg -gt 0 ] |
274 | | | then |
275 | | | if [ $scm_info -gt 0 ]; then echo "New config is different than the old one" ; fi |
276 | | | else |
277 | | | if [ $scm_info -gt 0 ]; then echo "New config is the same as the old one" ; fi |
278 | | | fi |
279 | | | |
280 | | | # to detect first start after reboot or rule flushig we compare config with rule number |
281 | | | # when we have less rules than ips in config then |
282 | | | sl_numru=`ip ru ls | grep -c lookup` |
283 | | | sl_numip=`grep -c ^ip $sl_sedlocache/$sl_nmcnf` |
284 | | | sl_numru=$(($sl_numru - 3 )) |
285 | | | if [ $sl_numip -gt $sl_numru ] |
286 | | | then |
287 | | | if [ $scm_info -gt 0 ]; then echo "We have less rules ($sl_numru) than new config has ips ($sl_numip)" ; fi |
288 | | | sl_diffcfg="1" |
289 | | | else |
290 | | | if [ $scm_info -gt 0 ]; then echo "We have $sl_numru rules and $sl_numip ips" ; fi |
291 | | | fi |
292 | | | |
293 | 7 | simandl | } |
294 | 8 | simandl | |
295 | 7 | simandl | ###################################################################### |
296 | | | s_version() |
297 | | | { |
298 | | | echo sedlo $sl_version |
299 | | | } # s_version |
300 | 8 | simandl | |
301 | 7 | simandl | ###################################################################### |
302 | | | s_report() |
303 | | | { |
304 | 9 | simandl | echo Content-type: text/html |
305 | | | echo |
306 | | | echo "Sedlo na routeru `hostname`" |
307 | | | echo "<pre>" |
308 | 7 | simandl | echo "##### SEDLO #####" |
309 | | | echo "date : `date`" |
310 | | | echo "version : $sl_version" |
311 | | | echo "local_config : $sl_sedlocnf" |
312 | 9 | simandl | echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>" |
313 | 7 | simandl | echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" |
314 | | | echo "##### TABLES #####" |
315 | | | cat $sl_rttab |
316 | 9 | simandl | echo ; echo "##### DEFAULT ROUTES IN TABLES #####" |
317 | | | $sl_ipcmd ro ls ta all | $sl_grepcmd table | $sl_grepcmd -v local | $sl_trcmd " " "\t" |
318 | | | echo ; echo "##### RULES FOR IPS #####" |
319 | | | $sl_ipcmd ru ls | $sl_trcmd " " "\t" |
320 | | | echo "</pre>" |
321 | 7 | simandl | } # s_report |
322 | 8 | simandl | |
323 | 9 | simandl | |
324 | | | |
325 | 7 | simandl | ###################################################################### |
326 | | | s_help() |
327 | | | { |
328 | | | echo Pouziti: sedlo [param] |
329 | | | echo param: |
330 | | | echo -V vypise verzi |
331 | | | echo -help vypise napovedu |
332 | | | echo -v malo upovidany |
333 | | | echo -vv hodne upovidany |
334 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
335 | | | echo -report vypise prehled pravidel a tabulek |
336 | | | echo -flru odstrani vsechny pravidla |
337 | | | } # s_help |
338 | 8 | simandl | |
339 | 7 | simandl | ###################################################################### |
340 | | | ###################################################################### |
341 | | | |
342 | | | sl_unknown="" |
343 | | | scm_nogetcfg=0 |
344 | | | scm_flru=0 |
345 | | | scm_info=0 |
346 | | | |
347 | | | # parsing input parameters |
348 | | | while [ "a$1" != "a" ] |
349 | | | do |
350 | | | case $1 in |
351 | | | -V) |
352 | | | s_version |
353 | | | exit 0 |
354 | | | ;; |
355 | | | -h) |
356 | | | s_help |
357 | | | exit 0 |
358 | | | ;; |
359 | | | -report) |
360 | | | s_report |
361 | | | exit 0 |
362 | | | ;; |
363 | | | -flru) |
364 | | | scm_flru=1 |
365 | | | shift |
366 | | | ;; |
367 | | | -help) |
368 | | | s_help |
369 | | | exit 0 |
370 | | | ;; |
371 | | | -nogetcfg) |
372 | | | scm_nogetcfg=1 |
373 | | | shift |
374 | | | ;; |
375 | | | -v) |
376 | | | scm_info=1 |
377 | | | shift |
378 | | | ;; |
379 | | | -vv) |
380 | | | scm_info=2 |
381 | | | shift |
382 | | | ;; |
383 | | | *) |
384 | | | sl_unknown="$sl_unknown$1 " |
385 | | | shift |
386 | | | esac |
387 | | | done |
388 | | | |
389 | | | # printing the list of bad parameters (if there are some) |
390 | | | if [ "a$sl_unknown" != "a" ] |
391 | | | then |
392 | | | echo "$slm_unknown $sl_unknown" |
393 | | | s_help |
394 | | | exit 0 |
395 | | | fi |
396 | | | |
397 | | | if [ $scm_flru -eq 1 ] |
398 | | | then |
399 | | | s_flru |
400 | | | exit 0 |
401 | | | fi |
402 | | | |
403 | | | if [ $scm_nogetcfg -eq 0 ] |
404 | | | then |
405 | | | s_getcfg |
406 | | | fi |
407 | | | |
408 | 10 | simandl | |
409 | 7 | simandl | s_mktables |
410 | | | s_filltables |
411 | | | |
412 | 10 | simandl | if [ $sl_diffcfg -gt 0 ] |
413 | | | then |
414 | | | s_fillrules |
415 | | | fi |
416 | | | |
417 | 7 | simandl | exit 0 |
418 | | | |
419 | | | |