1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
#! /usr/bin/perl
# Description: Quagga to Vyatta configuration converter
# Date: May 2011
# Version: 0.1
# **** MIT License ****
# Copyright (c) 2011 Daniil Baturin <daniil@baturin.org>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# **** End License ****
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
# Show help
sub usage
{
print "Usage:".$0." --config=/path/to/quagga/config\n";
print "Options:\n";
print " --rule-step=n Step between rule numbers in policy objects (e.g. access-lists)\n";
print " --first-rule=n First rule number in policy objects\n";
exit(0);
}
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Print help if not one command line argument is given
if( @ARGV == 0 ) {
usage();
}
# Get options
my $file;
my $rule_step = 10; # Default rule step
my $first_rule = 10; # Default first rule number
GetOptions(
"config=s" => \$file,
"rule-step=s" => \$rule_step,
"first-rule=s" => \$first_rule
);
# Read configuration file to array
open(CONFIG, $file) || die("Can not open file: ".$file);
my @quagga_config = <CONFIG>;
close(CONFIG);
# Parameters that should be set if found in any line
my $ip_forwarding = 0;
my $ipv6_forwarding = 0;
# Rule numbers for access-list processing
my $acl = 0;
my $acl_rule = 0;
# Route-map beginning string
my $rm_begin = '';
my $pl = '';
foreach(@quagga_config)
{
# Search for "ip route <subnet/mask> <iface|gateway>" statement
if( $_ =~ /^ip route/ )
{
my @words = split( / /, $_ );
my $route_command = '';
if( $words[3] =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/ )
{
# It's an ordinary route
$route_command = "set protocols static route ".$words[2]." next-hop ".$words[3];
}
elsif( $words[3] =~ /Null0/ )
{
# It's a blackhole route
$route_command = "set protocols static route ".$words[2]." blackhole\n";
}
else
{
# It's an interface route
$route_command = "set protocols static interface-route ".$words[2]." next-hop-interface ".$words[3];
}
if( $words[4] )
{
# Route has distance
$route_command .= " distance ".$words[4];
}
print $route_command;
}
# Search for "ipv6 route <subnet/mask> <iface|gateway>" statement
if( $_ =~ /^ipv6 route/ )
{
my @words = split( / /, $_ );
my $route_command = '';
if( $words[3] =~ /:/ )
{
# Interface name can not contain ":", thus it's an IPv6 gateway address
$route_command = "set protocols static route6 ".$words[2]." next-hop ".$words[3];
}
elsif( $words[3] =~ /blackhole/ )
{
# It's a blackhole route
$route_command = "set protocols static route6 ".$words[2]." blackhole\n";
}
else
{
# Otherwise it's an interface route
$route_command = "set protocols static interface-route6 ".$words[2]." next-hop-interface ".$words[3];
}
if( $words[4] )
{
# Route has distance
$route_command .= " distance ".$words[4];
}
print $route_command;
}
# Search for "ip forwarding" statement
if( $_ =~ /^ip forwarding/ )
{
$ip_forwarding = 1;
}
# Search for "ipv6 forwarding" statement
if( $_ =~ /^ipv6 forwarding/ )
{
$ipv6_forwarding = 1;
}
# Search for "ip prefix-list" statement
if( $_ =~ /^ip prefix-list/ )
{
my @words = split( / /, $_ );
my $pl_begin = "set policy prefix-list ".$words[2]." rule ".$words[4];
print $pl_begin." action ".$words[5]."\n";
print $pl_begin." prefix ".$words[6]."\n";
if( $words[7] )
{
# It has "le" or "ge" statement
print $pl_begin." ".$words[7]." ".$words[8];
}
}
# Search for "ipv6 prefix-list" statement
if( $_ =~ /^ipv6 prefix-list/ )
{
my @words = split( / /, $_ );
$acl_rule += $rule_step;
if( $pl ne $words[2] )
{
# Previous access list has ended, reset rule counter
$acl_rule = $first_rule;
$pl = $words[2];
}
if( $acl_rule > 65535 )
{
print "Error: access-list rule number exceeded allowed value!\n";
print "Try decreasing rule step by using --rule-step=n option";
exit(1);
}
my $pl_begin = "set policy prefix-list6 ".$words[2]." rule ".$acl_rule;
print $pl_begin." action ".$words[3]."\n";
print $pl_begin." prefix ".$words[4]."\n";
if( $words[6] )
{
# It has "le" or "ge" statement
print $pl_begin." ".$words[5]." ".$words[6];
}
}
# Search for "access-list" statement
if( $_ =~ /^access-list/ )
{
my @words = split( / /, $_ );
if( $acl ne $words[1] )
{
# Previous access list has ended, reset rule counter
$acl_rule = $first_rule;
}
if( $acl_rule > 65535 )
{
print "Error: access-list rule number exceeded allowed value!\n";
print "Try decreasing rule step by using --rule-step=n option";
exit(1);
}
$acl = $words[1];
my $acl_begin = "set policy access-list ".$acl." rule ";
print $acl_begin.$acl_rule." action ".$words[2]."\n";
if( $words[3] ne "ip" )
{
# It's a standard ACL
if( $words[3] =~ /any/ )
{
print $acl_begin.$acl_rule." source ".$words[3]."\n";
}
elsif( $words[4] )
{
# ACL has network and inverse mask
print $acl_begin.$acl_rule." source network ".$words[3]."\n";
print $acl_begin.$acl_rule." source inverse-mask ".$words[4]."\n";
}
else
{
# ACL has host only
print $acl_begin.$acl_rule." source host ".$words[3]."\n";
}
}
else
{
# It's an extended ACL
my $next_word = 0;
# Get source
if( $words[4] =~ /any/ )
{
print $acl_begin.$acl_rule." source any\n";
$next_word = 5;
}
elsif( $words[4] =~ /host/ )
{
print $acl_begin.$acl_rule." source host ".$words[5]."\n";
$next_word = 6;
}
else
{
print $acl_begin.$acl_rule." source network ".$words[4]."\n";
print $acl_begin.$acl_rule." source inverse-mask ".$words[5]."\n";
$next_word = 6;
}
# Get destination
if( $words[$next_word] =~ /any/ )
{
print $acl_begin.$acl_rule." destination any\n";
}
elsif( $words[$next_word] =~ /host/ )
{
print $acl_begin.$acl_rule." destination host ".$words[$next_word+1];
}
else
{
print $acl_begin.$acl_rule." destination network ".$words[$next_word]."\n";
print $acl_begin.$acl_rule." destination inverse-mask ".$words[$next_word+1]."\n";
}
}
$acl_rule += $rule_step;
}
# Search for "ipv6 access-list" statement
if( $_ =~ /^ipv6 access-list/ )
{
my @words = split( / /, $_ );
if( $acl ne $words[2] )
{
# Previous ACL has ended, reset rule counter
$acl_rule = $first_rule;
}
if( $acl_rule > 65535 )
{
print "Error: access-list6 rule number exceeded allowed value!\n";
print "Try decreasing rule step by using --rule-step=n option";
exit(1);
}
$acl = $words[2];
my $acl6_begin = "ipv6 access-list ".$acl." rule ";
print $acl6_begin.$acl_rule." action ".$words[3]."\n";
if( $words[4] =~ /any/ )
{
print $acl6_begin.$acl_rule." source any\n";
}
else
{
print $acl6_begin.$acl_rule." source network ".$words[4]."\n";
}
if( $words[5] )
{
print $acl6_begin.$acl_rule." source exact-match\n";
}
$acl_rule += $rule_step;
}
# Search for "ip as-path access-list" statement
if( $_ =~ /^ip as-path access-list/ )
{
my @words = split( / /, $_ );
if( $acl ne $words[3] )
{
# Previous ACL has ended, reset rule counter
$acl_rule = $first_rule;
}
if( $acl_rule > 65535 )
{
print "Error: as-path-list rule number exceeded allowed value!\n";
print "Try decreasing rule step by using --rule-step=n option";
exit(1);
}
$acl = $words[3];
my $as_path_begin = "set policy as-path-list ".$acl." rule ";
print $as_path_begin.$acl_rule." action ".$words[4]."\n";
print $as_path_begin.$acl_rule." regex \"".trim($words[5])."\"\n";
$acl_rule += $rule_step;
}
# Search for "ip community-list" statement
if( $_ =~ /^ip community-list/ )
{
my @words = split( / /, $_ );
if( $acl ne $words[2] )
{
# Previous ACL has ended, reset rule counter
$acl_rule = $first_rule;
}
if( $acl_rule > 65535 )
{
print "Error: community-list rule number exceeded allowed value!\n";
print "Try decreasing rule step by using --rule-step=n option";
exit(1);
}
$acl = $words[2];
my $comm_begin = "set policy community-list ".$acl." rule ";
print $comm_begin.$acl_rule." action ".$words[3]."\n";
print $comm_begin.$acl_rule." regex \"".trim($words[4])."\"\n";
$acl_rule += $rule_step;
}
# Search for "route-map" statement
if( $_ =~ /^route-map/ )
{
my @words = split( / /, $_ );
$rm_begin = "set policy route-map ".$words[1]." rule ".trim($words[3])." ";
print $rm_begin."action ".$words[2]."\n";
}
if( trim($_) =~ /^match|on-match|set|call/ )
{
# It's a line from route-map
if( $_ =~ /next-hop/ )
{
my @words = split( / /, trim($_) );
print $rm_begin."set ip-next-hop ".$words[3]."\n";
}
else
{
print $rm_begin.$_;
}
}
}
# If forwarding statements weren't found, forwarding should be disabled
# (enabled by default in Vyatta)
if( !$ip_forwarding )
{
print "set system ip disable-forwarding";
}
if( !$ipv6_forwarding )
{
print "set system ipv6 disable-forwarding";
}
|