bgp4r 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/README.rdoc +1 -10
  2. data/bgp/io.rb +4 -2
  3. data/bgp/messages/capability.rb +176 -0
  4. data/bgp/messages/keepalive.rb +30 -0
  5. data/bgp/messages/markers.rb +10 -0
  6. data/bgp/messages/message.rb +85 -0
  7. data/bgp/messages/messages.rb +17 -0
  8. data/bgp/messages/notification.rb +116 -0
  9. data/bgp/messages/open.rb +117 -0
  10. data/bgp/messages/route_refresh.rb +181 -0
  11. data/bgp/messages/update.rb +148 -0
  12. data/bgp/neighbor.rb +20 -11
  13. data/bgp/nlris/inet.rb +18 -0
  14. data/bgp/{label.rb → nlris/label.rb} +1 -1
  15. data/bgp/nlris/labeled.rb +39 -0
  16. data/bgp/nlris/nlri.rb +154 -0
  17. data/bgp/{attributes.rb → nlris/nlris.rb} +11 -13
  18. data/bgp/nlris/prefix.rb +33 -0
  19. data/bgp/{rd.rb → nlris/rd.rb} +1 -1
  20. data/bgp/nlris/vpn.rb +68 -0
  21. data/bgp/optional_parameters/as4.rb +59 -0
  22. data/bgp/optional_parameters/capabilities.rb +13 -0
  23. data/bgp/optional_parameters/capability.rb +145 -0
  24. data/bgp/optional_parameters/dynamic.rb +37 -0
  25. data/bgp/optional_parameters/graceful_restart.rb +116 -0
  26. data/bgp/optional_parameters/mbgp.rb +97 -0
  27. data/bgp/optional_parameters/optional_parameter.rb +76 -0
  28. data/bgp/optional_parameters/orf.rb +90 -0
  29. data/bgp/optional_parameters/route_refresh.rb +36 -0
  30. data/bgp/{prefix_orf.rb → orfs/prefix_orf.rb} +4 -10
  31. data/bgp/{aggregator.rb → path_attributes/aggregator.rb} +2 -2
  32. data/bgp/{as_path.rb → path_attributes/as_path.rb} +2 -2
  33. data/bgp/{atomic_aggregate.rb → path_attributes/atomic_aggregate.rb} +2 -2
  34. data/bgp/{attribute.rb → path_attributes/attribute.rb} +2 -2
  35. data/bgp/path_attributes/attributes.rb +21 -0
  36. data/bgp/{cluster_list.rb → path_attributes/cluster_list.rb} +2 -2
  37. data/bgp/{communities.rb → path_attributes/communities.rb} +2 -2
  38. data/bgp/{extended_communities.rb → path_attributes/extended_communities.rb} +3 -3
  39. data/bgp/{extended_community.rb → path_attributes/extended_community.rb} +1 -1
  40. data/bgp/{local_pref.rb → path_attributes/local_pref.rb} +2 -2
  41. data/bgp/{mp_reach.rb → path_attributes/mp_reach.rb} +3 -69
  42. data/bgp/path_attributes/mp_unreach.rb +97 -0
  43. data/bgp/{multi_exit_disc.rb → path_attributes/multi_exit_disc.rb} +2 -2
  44. data/bgp/{next_hop.rb → path_attributes/next_hop.rb} +2 -2
  45. data/bgp/{origin.rb → path_attributes/origin.rb} +2 -2
  46. data/bgp/{originator_id.rb → path_attributes/originator_id.rb} +2 -2
  47. data/bgp/{path_attribute.rb → path_attributes/path_attribute.rb} +6 -7
  48. data/bgp4r.gemspec +127 -67
  49. data/bgp4r.rb +5 -25
  50. data/examples/routegen +1 -1
  51. data/examples/routegen.yml +21 -5
  52. data/examples/simple.rb +4 -4
  53. data/examples/unit-testing/malformed_update.rb +1 -1
  54. data/examples/unit-testing/unknown_transitive_attr.rb +18 -10
  55. data/test/messages/capability_test.rb +113 -0
  56. data/test/messages/keepalive_test.rb +34 -0
  57. data/test/messages/markers_test.rb +33 -0
  58. data/test/messages/message_test.rb +43 -0
  59. data/test/messages/notification_test.rb +49 -0
  60. data/test/messages/open_test.rb +98 -0
  61. data/test/messages/route_refresh_test.rb +77 -0
  62. data/test/messages/update_test.rb +133 -0
  63. data/test/neighbor_test.rb +8 -9
  64. data/test/nlris/inet_test.rb +43 -0
  65. data/test/{label_test.rb → nlris/labeled_test.rb} +1 -1
  66. data/test/{nlri_test.rb → nlris/nlri_test.rb} +5 -1
  67. data/test/{rd_test.rb → nlris/rd_test.rb} +1 -2
  68. data/test/optional_parameters/as4_test.rb +34 -0
  69. data/test/optional_parameters/capability_test.rb +69 -0
  70. data/test/optional_parameters/dynamic_test.rb +34 -0
  71. data/test/optional_parameters/graceful_restart_test.rb +23 -0
  72. data/test/optional_parameters/mbgp_test.rb +35 -0
  73. data/test/optional_parameters/optional_parameter_test.rb +51 -0
  74. data/test/optional_parameters/orf_test.rb +42 -0
  75. data/test/optional_parameters/route_refresh_test.rb +32 -0
  76. data/test/{prefix_orf_test.rb → orfs/prefix_orf_test.rb} +2 -1
  77. data/test/{aggregator_test.rb → path_attributes/aggregator_test.rb} +1 -1
  78. data/test/{as_path_test.rb → path_attributes/as_path_test.rb} +1 -1
  79. data/test/{atomic_aggregate_test.rb → path_attributes/atomic_aggregate_test.rb} +1 -1
  80. data/test/{attribute_test.rb → path_attributes/attribute_test.rb} +2 -2
  81. data/test/{cluster_list_test.rb → path_attributes/cluster_list_test.rb} +1 -1
  82. data/test/{communities_test.rb → path_attributes/communities_test.rb} +1 -1
  83. data/test/{extended_communities_test.rb → path_attributes/extended_communities_test.rb} +1 -1
  84. data/test/{extended_community_test.rb → path_attributes/extended_community_test.rb} +1 -1
  85. data/test/{local_pref_test.rb → path_attributes/local_pref_test.rb} +1 -1
  86. data/test/{mp_reach_test.rb → path_attributes/mp_reach_test.rb} +1 -37
  87. data/test/path_attributes/mp_unreach_test.rb +67 -0
  88. data/test/{multi_exit_disc_test.rb → path_attributes/multi_exit_disc_test.rb} +1 -1
  89. data/test/{next_hop_test.rb → path_attributes/next_hop_test.rb} +1 -1
  90. data/test/{origin_test.rb → path_attributes/origin_test.rb} +2 -1
  91. data/test/{originator_id_test.rb → path_attributes/originator_id_test.rb} +1 -1
  92. data/test/{path_attribute_test.rb → path_attributes/path_attribute_test.rb} +2 -2
  93. metadata +122 -67
  94. data/bgp/message.rb +0 -921
  95. data/bgp/nlri.rb +0 -303
  96. data/bgp/version.rb +0 -3
  97. data/examples/unit-testing/keepalive_set_to_zeo.rb +0 -64
  98. data/test/message_test.rb +0 -347
  99. /data/bgp/{orf.rb → orfs/orf.rb} +0 -0
data/bgp/message.rb DELETED
@@ -1,921 +0,0 @@
1
- #--
2
- # Copyright 2008, 2009 Jean-Michel Esnault.
3
- # All rights reserved.
4
- # See LICENSE.txt for permissions.
5
- #
6
- #
7
- # This file is part of BGP4R.
8
- #
9
- # BGP4R is free software: you can redistribute it and/or modify
10
- # it under the terms of the GNU General Public License as published by
11
- # the Free Software Foundation, either version 3 of the License, or
12
- # (at your option) any later version.
13
- #
14
- # BGP4R is distributed in the hope that it will be useful,
15
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- # GNU General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU General Public License
20
- # along with BGP4R. If not, see <http://www.gnu.org/licenses/>.
21
- #++
22
-
23
- require 'bgp/path_attribute'
24
- require 'bgp/nlri'
25
- require 'timeout'
26
-
27
- module BGP
28
-
29
- class UnknownBgpCapability < RuntimeError
30
- end
31
-
32
- class UnknownBgpMessage < RuntimeError
33
- end
34
-
35
- module OPT_PARM
36
-
37
- CAPABILITY = 2
38
-
39
- #TODO module CAP
40
- #TODO module CAP::ORF
41
-
42
- CAP_MBGP = 1
43
- CAP_ROUTE_REFRESH = 2
44
- CAP_ORF = 3
45
- CAP_AS4 = 65
46
- CAP_ROUTE_REFRESH_CISCO = 128
47
- CAP_ORF_CISCO = 130
48
-
49
- ORF_NLRI = 1
50
- ORF_COMMUNITIES = 2
51
- ORF_EXTENDED_COMMUNITIES = 3
52
- ORF_PREFIX_LIST = 129
53
-
54
- ##########################################################
55
- # BGP OPEN OPTION PARAMETERS #
56
- ##########################################################
57
-
58
- class Optional_parameter
59
-
60
- def initialize(parm_type)
61
- @parm_type=parm_type
62
- end
63
-
64
- def encode(value)
65
- [@parm_type, value.size, value].pack('CCa*')
66
- end
67
-
68
- def parse(s)
69
- @parm_type, len = s.slice!(0,2).unpack('CC')
70
- s.slice!(0,len).is_packed
71
- end
72
-
73
- def self.factory(s)
74
- parm_type, len = s.unpack('CC')
75
- opt_parm = s.slice!(0,len+2).is_packed
76
- case parm_type
77
- when CAPABILITY
78
- Capability.factory(opt_parm)
79
- else
80
- raise RuntimeError, "Optional parameter type '#{parm_type}' not implemented"
81
- end
82
- end
83
-
84
- end
85
- end
86
-
87
-
88
- class Capability < OPT_PARM::Optional_parameter
89
- include OPT_PARM
90
- def initialize(code)
91
- super(OPT_PARM::CAPABILITY)
92
- @code=code
93
- end
94
- def encode(value='')
95
- super([@code,value.size, value].pack('CCa*'))
96
- end
97
- def parse(_s)
98
- s = super(_s)
99
- @code, len = s.slice!(0,2).unpack('CC')
100
- s.slice!(0,len).is_packed
101
- end
102
- def to_s
103
- "Option Capabilities Advertisement (#{@parm_type}): [#{to_shex}]"
104
- end
105
- def self.factory(s)
106
- code = s.slice(2,1).unpack('C')[0]
107
- case code
108
- when CAP_AS4
109
- As4_cap.new(s)
110
- when CAP_MBGP
111
- Mbgp_cap.new(s)
112
- when CAP_ROUTE_REFRESH, CAP_ROUTE_REFRESH_CISCO
113
- Route_refresh_cap.new(code)
114
- when CAP_ORF,CAP_ORF_CISCO
115
- Orf_cap.new(s)
116
- else
117
- raise UnknownBgpCapability, "Capability (#{code}), length: #{s.size} not implemented: [#{s.unpack('H*')[0]}]"
118
- end
119
- end
120
- def to_hash(h={})
121
- if h.empty?
122
- {:code=> @code}
123
- else
124
- {:code=> @code, :capability=> h}
125
- end
126
- end
127
- end
128
-
129
- class Mbgp_cap < Capability
130
-
131
- def self.ipv4_unicast
132
- Mbgp_cap.new(1,1)
133
- end
134
-
135
- def self.ipv4_multicast
136
- Mbgp_cap.new(1,2)
137
- end
138
-
139
- def self.ipv6_unicast
140
- Mbgp_cap.new(2,2)
141
- end
142
-
143
- def self.ipv6_multicast
144
- Mbgp_cap.new(2,2)
145
- end
146
-
147
- def initialize(afi,safi=nil)
148
- if safi.nil? and afi.is_a?(String) and afi.is_packed?
149
- parse(afi)
150
- else
151
- super(OPT_PARM::CAP_MBGP)
152
- self.safi, self.afi = safi, afi
153
- end
154
- end
155
-
156
- def afi=(val)
157
- @afi = if val.is_a?(Fixnum)
158
- val
159
- elsif val == :ipv4
160
- 1
161
- elsif val == :ipv6
162
- 2
163
- end
164
- end
165
-
166
- def safi=(val)
167
- @safi = if val.is_a?(Fixnum)
168
- val
169
- elsif val == :unicast
170
- 1
171
- elsif val == :multicast
172
- 2
173
- end
174
- end
175
-
176
- def encode
177
- super([@afi,0,@safi].pack('nCC'))
178
- end
179
-
180
- def parse(s)
181
- @afi, ignore, @safi = super(s).unpack('nCC')
182
- end
183
-
184
- def to_s
185
- super + "\n Multiprotocol Extensions (#{CAP_MBGP}), length: 4" +
186
- "\n AFI #{IANA.afi(@afi)} (#{@afi}), SAFI #{IANA.safi(@safi)} (#{@safi})"
187
- end
188
-
189
- def to_hash
190
- super({:afi => @afi, :safi => @safi, })
191
- end
192
- end
193
-
194
- class As4_cap < Capability
195
- def initialize(s)
196
- if s.is_a?(String) and s.is_packed?
197
- parse(s)
198
- else
199
- super(OPT_PARM::CAP_AS4)
200
- @as=s
201
- end
202
- end
203
-
204
- def encode
205
- super([@as].pack('N'))
206
- end
207
-
208
- def parse(s)
209
- @as = super(s).unpack('N')[0]
210
- end
211
-
212
- def to_s
213
- "Capability(#{CAP_AS4}): 4-octet AS number: " + @as.to_s
214
- end
215
-
216
- def to_hash
217
- super({:as => @as})
218
- end
219
- end
220
-
221
- class Route_refresh_cap < Capability
222
- def initialize(code=OPT_PARM::CAP_ROUTE_REFRESH)
223
- super(code)
224
- end
225
-
226
- def encode
227
- super()
228
- end
229
-
230
- def to_s
231
- super + "\n Route Refresh #{@code==128 ? "(Cisco) " : ""}(#{@code}), length: 2"
232
- end
233
-
234
- def to_hash
235
- super()
236
- end
237
- end
238
-
239
- class Orf_cap < Capability
240
-
241
- class Entry
242
-
243
- def initialize(*args)
244
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
245
- parse(args[0])
246
- elsif args[0].is_a?(Hash)
247
- else
248
- @afi, @safi, *@types = *args
249
- end
250
- end
251
-
252
- def encode
253
- [@afi, 0, @safi, @types.size,@types.collect { |e| e.pack('CC')}.join].pack('nCCCa*')
254
- end
255
-
256
- def parse(s)
257
- @afi, __, @safi, n = s.slice!(0,5).unpack('nCCC')
258
- @types=[]
259
- types = s.slice!(0, 2*n)
260
- while types.size>0
261
- @types<< types. slice!(0,2).unpack('CC')
262
- end
263
- end
264
-
265
- def to_s
266
- "AFI #{IANA.afi(@afi)} (#{@afi}), SAFI #{IANA.safi(@safi)} (#{@safi}): #{@types.inspect}"
267
- end
268
- end
269
-
270
- def initialize(*args)
271
- @entries=[]
272
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
273
- parse(args[0])
274
- else
275
- super(OPT_PARM::CAP_ORF)
276
- end
277
- end
278
-
279
- def add(entry)
280
- @entries << entry
281
- end
282
-
283
- def encode
284
- super(@entries.collect { |e| e.encode }.join)
285
- end
286
-
287
- def parse(s)
288
- entries = super(s)
289
- while entries.size>0
290
- @entries << Entry.new(entries)
291
- end
292
- end
293
-
294
- def to_s
295
- super + "\n Outbound Route Filtering (#{@code}), length: #{encode.size}" +
296
- (['']+@entries.collect { |e| e.to_s }).join("\n ")
297
- end
298
-
299
- end
300
-
301
- # http://www.iana.org/assignments/bgp-parameters/bgp-parameters.xhtml#bgp-parameters-1
302
- module MESSAGE
303
-
304
- OPEN = 1
305
- UPDATE = 2
306
- NOTIFICATION = 3
307
- KEEPALIVE = 4
308
- ROUTE_REFRESH = 5
309
-
310
- def encode(message='')
311
- len = message.size+19
312
- [[255]*16,len,@msg_type,message].flatten.pack('C16nCa*')
313
- end
314
-
315
- def parse(s)
316
- len, @msg_type, message = s[16..-1].unpack('nCa*')
317
- message.is_packed
318
- end
319
-
320
- end
321
-
322
- class Message
323
- include MESSAGE
324
- def self.factory(_s, as4byte=false)
325
- s = [_s].pack('a*')
326
- s.slice(18,1).unpack('C')[0]
327
- case s.slice(18,1).unpack('C')[0]
328
- when OPEN
329
- Open.new(s)
330
- when UPDATE
331
- Update.new(s, as4byte)
332
- when KEEPALIVE
333
- Keepalive.new
334
- when NOTIFICATION
335
- Notification.new(s)
336
- when ROUTE_REFRESH
337
- if s.size > 23
338
- Orf_route_refresh.new(s)
339
- else
340
- Route_refresh.new(s)
341
- end
342
- else
343
- puts "don't know what kind of bgp messgage this is #{s.slice(18,1).unpack('C')}"
344
- end
345
- end
346
- def self.keepalive
347
- Keepalive.new.encode
348
- end
349
- def self.route_refresh(afi,safi)
350
- Route_refresh.new(afi,safi).encode
351
- end
352
- end
353
-
354
- # KEEPALIVE Message Format
355
- #
356
- # BGP does not use any TCP-based, keep-alive mechanism to determine if
357
- # peers are reachable. Instead, KEEPALIVE messages are exchanged
358
- # between peers often enough not to cause the Hold Timer to expire. A
359
- # reasonable maximum time between KEEPALIVE messages would be one third
360
- # of the Hold Time interval. KEEPALIVE messages MUST NOT be sent more
361
- # frequently than one per second. An implementation MAY adjust the
362
- # rate at which it sends KEEPALIVE messages as a function of the Hold
363
- # Time interval.
364
- #
365
- # If the negotiated Hold Time interval is zero, then periodic KEEPALIVE
366
- # messages MUST NOT be sent.
367
- #
368
- # A KEEPALIVE message consists of only the message header and has a
369
- # length of 19 octets.
370
- #
371
-
372
- class Keepalive < Message
373
- def initialize
374
- @msg_type=KEEPALIVE
375
- end
376
-
377
- def to_s
378
- "Keepalive Message (#{MESSAGE::KEEPALIVE}), length: 19" + ", [#{self.to_shex[32..-1]}]"
379
- end
380
- end
381
-
382
- # UPDATE Message Format
383
- #
384
- # UPDATE messages are used to transfer routing information between BGP
385
- # peers. The information in the UPDATE message can be used to
386
- # construct a graph that describes the relationships of the various
387
- # Autonomous Systems. By applying rules to be discussed, routing
388
- #
389
- #
390
- #
391
- # Rekhter, et al. Standards Track [Page 14]
392
- #
393
- # RFC 4271 BGP-4 January 2006
394
- #
395
- #
396
- # information loops and some other anomalies may be detected and
397
- # removed from inter-AS routing.
398
- #
399
- # An UPDATE message is used to advertise feasible routes that share
400
- # common path Copyright 2008, 2009 to a peer, or to withdraw multiple unfeasible
401
- # routes from service (see 3.1). An UPDATE message MAY simultaneously
402
- # advertise a feasible route and withdraw multiple unfeasible routes
403
- # from service. The UPDATE message always includes the fixed-size BGP
404
- # header, and also includes the other fields, as shown below (note,
405
- # some of the shown fields may not be present in every UPDATE message):
406
- #
407
- # +-----------------------------------------------------+
408
- # | Withdrawn Routes Length (2 octets) |
409
- # +-----------------------------------------------------+
410
- # | Withdrawn Routes (variable) |
411
- # +-----------------------------------------------------+
412
- # | Total Path Attribute Length (2 octets) |
413
- # +-----------------------------------------------------+
414
- # | Path Attributes (variable) |
415
- # +-----------------------------------------------------+
416
- # | Network Layer Reachability Information (variable) |
417
- # +-----------------------------------------------------+
418
- #
419
-
420
-
421
- class Update < Message
422
- def as4byte?
423
- @as4byte ||= false
424
- end
425
- def initialize(*args)
426
- if args[0].is_a?(String) and args[0].is_packed?
427
- @as4byte=false
428
- parse(*args)
429
- elsif args[0].is_a?(self.class)
430
- parse(args[0].encode, *args[1..-1])
431
- else
432
- @msg_type=UPDATE
433
- set(*args)
434
- end
435
- end
436
-
437
- def set(*args)
438
- args.each { |arg|
439
- if arg.is_a?(Withdrawn)
440
- self.withdrawn=arg
441
- elsif arg.is_a?(Path_attribute)
442
- self.path_attribute = arg
443
- elsif arg.is_a?(Nlri)
444
- self.nlri = arg
445
- end
446
- }
447
- end
448
-
449
- def withdrawn=(val)
450
- @withdrawn=val if val.is_a?(Withdrawn)
451
- end
452
-
453
- def nlri=(val)
454
- @nlri=val if val.is_a?(Nlri)
455
- end
456
-
457
- def path_attribute=(val)
458
- @path_attribute=val if val.is_a?(Path_attribute)
459
- end
460
-
461
- #TODO refactor out passing an argument to encode...
462
- def encode(as4byte=@as4byte)
463
- withdrawn, path_attribute, nlri = '', '', ''
464
- withdrawn = @withdrawn.encode(false) if defined? @withdrawn and @withdrawn
465
- path_attribute = @path_attribute.encode(as4byte) if defined?(@path_attribute) and @path_attribute
466
- nlri = @nlri.encode if defined? @nlri and @nlri
467
- super([withdrawn.size, withdrawn, path_attribute.size, path_attribute, nlri].pack('na*na*a*'))
468
- end
469
-
470
- def encode4
471
- encode(true)
472
- end
473
-
474
- attr_reader :path_attribute, :nlri, :withdrawn
475
-
476
- # CHANGED ME: NO DEFAULT HERE, the factory calling us has to tell what it is giving us.
477
- def parse(s, as4byte=false)
478
- @as4byte=as4byte
479
- update = super(s)
480
- len = update.slice!(0,2).unpack('n')[0]
481
- self.withdrawn=Withdrawn.new(update.slice!(0,len).is_packed) if len>0
482
- len = update.slice!(0,2).unpack('n')[0]
483
- enc_path_attribute = update.slice!(0,len).is_packed
484
- self.path_attribute=Path_attribute.new(enc_path_attribute, as4byte) if len>0
485
- self.nlri = Nlri.new(update) if update.size>0
486
- end
487
-
488
- def <<(val)
489
- if val.is_a?(Attr)
490
- @path_attribute ||= Path_attribute.new
491
- @path_attribute << val
492
- elsif val.is_a?(String)
493
- begin
494
- Nlri.new(val)
495
- @nlri ||=Nlri.new
496
- @nlri << val
497
- rescue => e
498
- end
499
- elsif val.is_a?(Nlri)
500
- val.to_s.split.each { |n| self << n }
501
- end
502
- end
503
-
504
- def to_s(as4byte=@as4byte, fmt=:tcpdump)
505
- msg = encode(as4byte)
506
- # if as4byte
507
- # msg = self.encode(true)
508
- # else
509
- # msg = self.encode
510
- # end
511
- s = []
512
- s << @withdrawn.to_s if defined?(@withdrawn) and @withdrawn
513
- s << @path_attribute.to_s(fmt, as4byte) if defined?(@path_attribute) and @path_attribute
514
- s << @nlri.to_s if defined?(@nlri) and @nlri
515
- "Update Message (#{MESSAGE::UPDATE}), #{as4byte ? "4 bytes AS, " : ''}length: #{msg.size}\n " + s.join("\n") + "\n" + msg.hexlify.join("\n") + "\n"
516
- end
517
-
518
- def self.withdrawn(u)
519
- if u.nlri and u.nlri.size>0
520
- Update.new(Withdrawn.new(*(u.nlri.nlris.collect { |n| n.to_s})))
521
- elsif u.path_attribute.has?(Mp_reach)
522
- pa = Path_attribute.new
523
- pa << u.path_attribute[ATTR::MP_REACH].new_unreach
524
- Update.new(pa)
525
- end
526
- end
527
- end
528
-
529
-
530
- # OPEN Message Format
531
- #
532
- # After a TCP connection is established, the first message sent by each
533
- # side is an OPEN message. If the OPEN message is acceptable, a
534
- # KEEPALIVE message confirming the OPEN is sent back.
535
- #
536
- # In addition to the fixed-size BGP header, the OPEN message contains
537
- # the following fields:
538
- #
539
- # 0 1 2 3
540
- # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
541
- # +-+-+-+-+-+-+-+-+
542
- # | Version |
543
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544
- # | My Autonomous System |
545
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546
- # | Hold Time |
547
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
548
- # | BGP Identifier |
549
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
550
- # | Opt Parm Len |
551
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552
- # | |
553
- # | Optional Parameters (variable) |
554
- # | |
555
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556
- #
557
- #
558
-
559
- class Open < Message
560
-
561
- include OPT_PARM
562
-
563
- attr_reader :version, :local_as, :holdtime, :opt_parms
564
-
565
- def initialize(*args)
566
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
567
- @opt_parms=[] # FIMXE: should not have ot init here
568
- parse(args[0])
569
- elsif args[0].is_a?(self.class)
570
- parse(args[0].encode, *args[1..-1])
571
- else
572
- @msg_type=OPEN
573
- @version, @local_as, @holdtime, bgp_id, *@opt_parms=args
574
- @bgp_id = IPAddr.new(bgp_id)
575
- end
576
- end
577
-
578
- def <<(arg)
579
- raise ArgumentError, "arg is not an Optional_parameter" unless arg.is_a?(Optional_parameter)
580
- @opt_parms << arg
581
- end
582
-
583
- def encode
584
- opt_parms = @opt_parms.flatten.compact.collect { |cap| cap.encode }.join
585
- super([@version, @local_as, @holdtime, @bgp_id.hton, opt_parms.size, opt_parms].pack('Cnna4Ca*'))
586
- end
587
-
588
- def parse(s)
589
- @version, @local_as, @holdtime, bgp_id, opt_parm_len, opt_parms = super(s).unpack('Cnna4Ca*')
590
- while opt_parms.size>0
591
- begin
592
- @opt_parms << Optional_parameter.factory(opt_parms)
593
- rescue UnknownBGPCapability => e
594
- puts "#{e}"
595
- end
596
- end
597
- @bgp_id = IPAddr.new_ntoh(bgp_id)
598
- end
599
-
600
- def bgp_id
601
- @bgp_id.to_s
602
- end
603
-
604
- def to_s
605
- msg = self.encode
606
- "Open Message (#{MESSAGE::OPEN}), length: #{msg.size}\n" +
607
- " Version #{@version}, my AS #{@local_as}, Holdtime #{@holdtime}s, ID #{@bgp_id}" +
608
- ([""] + @opt_parms.compact.collect { |cap| cap.to_s } + [""]).join("\n ") +
609
- msg.hexlify.join("\n") + "\n"
610
- end
611
-
612
- def find(klass)
613
- @opt_parms.find { |a| a.is_a?(klass) }
614
- end
615
-
616
- def has?(klass)
617
- @opt_parms.find { |a| a.is_a?(klass) }.nil? ? false : true
618
- end
619
-
620
- def to_hash
621
- h = {:version => @version, :my_as => @local_as, :holdtime => @holdtime, :bgp_id => bgp_id }
622
- unless @opt_parms.empty?
623
- h[:capabilities] = @opt_parms.collect { |opt| opt.to_hash }
624
- end
625
- h
626
- end
627
- end
628
-
629
- # Route-REFRESH Message
630
- #
631
- # The ROUTE-REFRESH message is a new BGP message type defined as
632
- # follows:
633
- #
634
- # Type: 5 - ROUTE-REFRESH
635
- #
636
- # Message Format: One <AFI, SAFI> encoded as
637
- #
638
- # 0 7 15 23 31
639
- # +-------+-------+-------+-------+
640
- # | AFI | Res. | SAFI |
641
- # +-------+-------+-------+-------+
642
- #
643
-
644
- class Route_refresh < Message
645
- attr_reader :afi, :safi
646
- def initialize(*args)
647
- @msg_type=ROUTE_REFRESH
648
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
649
- parse(args[0])
650
- elsif args[0].is_a?(self.class)
651
- parse(args[0].encode, *args[1..-1])
652
- elsif args.size==1 and args[0].is_a?(Hash)
653
- self.afi = args[0][:afi] if args[0][:afi]
654
- self.safi = args[0][:safi] if args[0][:safi]
655
- else
656
- @afi, @safi=args
657
- end
658
- end
659
-
660
- def afi=(val)
661
- raise ArgumentError, "invalid argument" unless val.is_a?(Fixnum) and (0..0xffff) === val
662
- @afi=val
663
- end
664
-
665
- def safi=(val)
666
- raise ArgumentError, "invalid argument" unless val.is_a?(Fixnum) and (0..0xff) === val
667
- @safi=val
668
- end
669
-
670
- def encode
671
- # default to ipv4 and unicast when not set
672
- @afi ||=1
673
- @safi ||=1
674
- super([@afi, 0, @safi].pack('nCC'))
675
- end
676
-
677
- def parse(s)
678
- @afi, reserved, @safi= super(s).unpack('nCC')
679
- end
680
-
681
- def to_hash
682
- {:afi=> @afi, :safi=> @safi}
683
- end
684
-
685
- def to_s
686
- msg = self.encode
687
- "Route Refresh (#{MESSAGE::ROUTE_REFRESH}), length: #{msg.size}\n" + "AFI #{IANA.afi(@afi)} (#{@afi}), SAFI #{IANA.safi(@safi)} (#{@safi})"
688
- end
689
- end
690
-
691
- #
692
- # Carrying ORF Entries in BGP
693
- #
694
- # ORF entries are carried in the BGP ROUTE-REFRESH message [BGP-RR].
695
- #
696
- # A BGP speaker can distinguish an incoming ROUTE-REFRESH message that
697
- # carries one or more ORF entries from an incoming plain ROUTE-REFRESH
698
- # message by using the Message Length field in the BGP message header.
699
- #
700
-
701
- class Orf
702
- def self.factory(s)
703
- type, len = s.unpack('Cn')
704
- case type
705
- when ORF::PREFIX
706
- Prefix_orf.new(s.slice!(0,len+3).is_packed)
707
- else
708
- raise RuntimeError, "orf type #{type} not implemented"
709
- end
710
- end
711
- end
712
-
713
- #FIXME: Unit-test
714
- class Orf_route_refresh < Message
715
-
716
- attr_accessor :orfs
717
-
718
- def initialize(*args)
719
- @msg_type=ROUTE_REFRESH
720
- @orfs=[]
721
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
722
- parse(args[0])
723
- elsif args[0].is_a?(self.class)
724
- parse(args[0].encode, *args[1..-1])
725
- elsif args.size==1 and args[0].is_a?(Hash)
726
- self.afi = args[0][:afi] if args[0][:afi]
727
- self.safi = args[0][:safi] if args[0][:safi]
728
- else
729
- @afi, @safi=args
730
- end
731
- end
732
-
733
- def afi_to_i
734
- @afi
735
- end
736
-
737
- def safi_to_i
738
- @safi
739
- end
740
-
741
- def afi
742
- IANA.afi(@afi)
743
- end
744
-
745
- def safi
746
- IANA.safi(@safi)
747
- end
748
-
749
- def afi=(val)
750
- raise ArgumentError, "invalid argument" unless val.is_a?(Fixnum) and (0..0xffff) === val
751
- @afi=val
752
- end
753
-
754
- def safi=(val)
755
- raise ArgumentError, "invalid argument" unless val.is_a?(Fixnum) and (0..0xff) === val
756
- @safi=val
757
- end
758
-
759
- def when_to_s
760
- case @when
761
- when 1 ; 'defer (1)'
762
- when 2 ; 'immediate (2)'
763
- else
764
- "bogus (#{@when})"
765
- end
766
- end
767
-
768
- def encode
769
- super([@afi, 0, @safi, @orfs.collect { |o| o.encode }.join].pack('nCCa*'))
770
- end
771
-
772
- def parse(s)
773
- @afi, reserved, @safi, orfs= super(s).unpack('nCCa*')
774
- while orfs.size>0
775
- #puts "orfs before factory: #{orfs.unpack('H*')}"
776
- @orfs << Orf.factory(orfs.is_packed)
777
- #puts "orfs after factory: #{orfs.unpack('H*')}"
778
- end
779
- end
780
-
781
- def to_hash
782
- {:afi=> @afi, :safi=> @safi}
783
- end
784
-
785
- def to_s
786
- msg = self.encode
787
- "ORF Route Refresh (#{MESSAGE::ROUTE_REFRESH}), length: #{msg.size}\n" +
788
- "AFI #{IANA.afi(@afi)} (#{@afi}), SAFI #{IANA.safi(@safi)} (#{@safi}):\n" +
789
- @orfs.collect { |orf| orf.to_s}.join("\n")
790
- end
791
-
792
- def add(*args)
793
- @orfs ||=[]
794
- args.each { |arg| @orfs << arg if arg.is_a?(Orf) }
795
- end
796
- alias << add
797
-
798
- def communities
799
- @communities.collect { |comm| comm.to_s }.join(' ')
800
- end
801
-
802
- end
803
-
804
- # NOTIFICATION Message Format
805
- #
806
- # A NOTIFICATION message is sent when an error condition is detected.
807
- # The BGP connection is closed immediately after it is sent.
808
- #
809
- #
810
- #
811
- #
812
- # Rekhter, et al. Standards Track [Page 21]
813
- #
814
- # RFC 4271 BGP-4 January 2006
815
- #
816
- #
817
- # In addition to the fixed-size BGP header, the NOTIFICATION message
818
- # contains the following fields:
819
- #
820
- # 0 1 2 3
821
- # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
822
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
823
- # | Error code | Error subcode | Data (variable) |
824
- # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
825
- #
826
-
827
-
828
- class Notification < Message
829
-
830
- def self.code_to_s(*args)
831
- @code_to_s[args]
832
- end
833
-
834
- @code_to_s=Hash.new("Undefined")
835
- @code_to_s[[1]] = "Header Error"
836
- @code_to_s[[2]] = "OPEN msg error"
837
- @code_to_s[[3]] = "UPDATE msg error"
838
- @code_to_s[[4]] = "Hold Timer Expired"
839
- @code_to_s[[5]] = "Finite State Machine Error"
840
- @code_to_s[[6]] = "Cease"
841
- @code_to_s[[1,1]] = "Connection Not Synchronized"
842
- @code_to_s[[1,2]] = "Bad Message Length"
843
- @code_to_s[[1,3]] = "Bad Message Type"
844
- @code_to_s[[2,1]] = "Unsupported Version Number"
845
- @code_to_s[[2,2]] = "Bad Peer AS"
846
- @code_to_s[[2,3]] = "Bad BGP Identifier"
847
- @code_to_s[[2,4]] = "Unsupported Optional Parameter"
848
- @code_to_s[[2,5]] = "[Deprecated]"
849
- @code_to_s[[2,6]] = "Unacceptable Hold Time"
850
- @code_to_s[[2,7]] = "Unsupported Optional Parameter"
851
- @code_to_s[[3,1]] = "Malformed Attribute List"
852
- @code_to_s[[3,2]] = "Unrecognized Well-known Attribute"
853
- @code_to_s[[3,3]] = "Missing Well-known Attribute"
854
- @code_to_s[[3,4]] = "Attribute Flags Error"
855
- @code_to_s[[3,5]] = "Attribute Length Error"
856
- @code_to_s[[3,6]] = "Invalid ORIGIN Attribute"
857
- @code_to_s[[3,7]] = "Deprecated"
858
- @code_to_s[[3,8]] = "Invalid NEXT_HOP Attribute"
859
- @code_to_s[[3,9]] = "Optional Attribute Error"
860
- @code_to_s[[3,10]] = "Invalid Network Field"
861
- @code_to_s[[3,11]] = "Malformed AS_PATH"
862
- @code_to_s[[6,1]] = "Maximum Number of Prefixes Reached"
863
- @code_to_s[[6,2]] = "Administrative Shutdown"
864
- @code_to_s[[6,3]] = "Peer De-configured"
865
- @code_to_s[[6,4]] = "Administrative Reset"
866
- @code_to_s[[6,5]] = "Connection Rejected"
867
- @code_to_s[[6,6]] = "Other Configuration Change"
868
- @code_to_s[[6,7]] = "Connection Collision Resolution"
869
- @code_to_s[[6,8]] = "Out of Resources"
870
-
871
- def initialize(*args)
872
- @msg_type=NOTIFICATION
873
- if args.size==1 and args[0].is_a?(String) and args[0].is_packed?
874
- parse(args[0])
875
- elsif args[0].is_a?(self.class)
876
- parse(args[0].encode, *args[1..-1])
877
- elsif args.size==1 and args[0].is_a?(Notification) and args[0].respond_to?(:encode)
878
- parse(args[0].encode)
879
- else
880
- @code, @subcode, @data=args
881
- end
882
- end
883
-
884
- def encode
885
- # default to ipv4 and unicast when not set
886
- super([@code,@subcode, @data].pack('CCa*'))
887
- end
888
-
889
- def parse(s)
890
- @code, @subcode, @data= super(s).unpack('CCa*')
891
- end
892
-
893
- def to_hash
894
- {:code=> @code, :subcode=> @subcode, :data=>@data}
895
- end
896
-
897
- def to_string
898
- Notification.code_to_s(@code, @subcode)
899
- end
900
-
901
- def to_s
902
- msg = self.encode
903
- s = "Notification (#{MESSAGE::NOTIFICATION}), length: #{msg.size}: "
904
- s += "#{Notification.code_to_s(@code)} (#{@code}), #{Notification.code_to_s(@code, @subcode)} (#{@subcode}) "
905
- s += "\ndata: [#{@data}]" if @data.size>0
906
- s
907
- end
908
-
909
- end
910
-
911
- # s = 'ffffffffffffffffffffffffffffffff0050020000002f40010101400304c0a80105800404000000644005040000006440020402010064c0080c051f00010137003b0af50040200a0a0a0a2020202020'
912
- # m = Message.factory([s].pack('H*'))
913
- # puts m.to_s(true, :tcpdump)
914
- # puts m.to_s(true, :default)
915
- # puts m.to_s(false, :default)
916
-
917
- end
918
-
919
- load "../test/#{ File.basename($0.gsub(/.rb/,'_test.rb'))}" if __FILE__ == $0
920
-
921
-