2 * Copyright (C) 1999 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_var.h>
43 #include <netinet/udp.h>
44 #include <netinet/udp_var.h>
50 #include "interface.h"
51 #include "addrtoname.h"
54 u_int8_t bgp_marker
[16];
58 #define BGP_SIZE 19 /* unaligned */
62 #define BGP_NOTIFICATION 3
63 #define BGP_KEEPALIVE 4
66 u_int8_t bgpo_marker
[16];
69 u_int8_t bgpo_version
;
71 u_int16_t bgpo_holdtime
;
74 /* options should follow */
83 struct bgp_notification
{
84 u_int8_t bgpn_marker
[16];
89 /* data should follow */
99 #define bgp_attr_len(p) \
100 (((p)->bgpa_flags & 0x10) ? \
101 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
102 #define bgp_attr_off(p) \
103 (((p)->bgpa_flags & 0x10) ? 4 : 3)
106 #define BGPTYPE_ORIGIN 1
107 #define BGPTYPE_AS_PATH 2
108 #define BGPTYPE_NEXT_HOP 3
109 #define BGPTYPE_MULTI_EXIT_DISC 4
110 #define BGPTYPE_LOCAL_PREF 5
111 #define BGPTYPE_ATOMIC_AGGREGATE 6
112 #define BGPTYPE_AGGREGATOR 7
113 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
114 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
115 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
116 #define BGPTYPE_DPA 11 /* work in progress */
117 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
118 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
119 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
120 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
123 static const char *bgptype
[] = {
124 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
126 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
128 static const char *bgpopt_type
[] = {
129 NULL
, "Authentication Information",
131 #define bgp_opttype(x) \
132 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
134 static const char *bgpnotify_major
[] = {
135 NULL
, "Message Header Error",
136 "OPEN Message Error", "UPDATE Message Error",
137 "Hold Timer Expired", "Finite State Machine Error",
140 #define bgp_notify_major(x) \
141 num_or_str(bgpnotify_major, \
142 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
144 static const char *bgpnotify_minor_1
[] = {
145 NULL
, "Connection Not Synchronized",
146 "Bad Message Length", "Bad Message Type",
149 static const char *bgpnotify_minor_2
[] = {
150 NULL
, "Unsupported Version Number",
151 "Bad Peer AS", "Bad BGP Identifier",
152 "Unsupported Optional Parameter", "Authentication Failure",
153 "Unacceptable Hold Time",
156 static const char *bgpnotify_minor_3
[] = {
157 NULL
, "Malformed Attribute List",
158 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
159 "Attribute Flags Error", "Attribute Length Error",
160 "Invalid ORIGIN Attribute", "AS Routing Loop",
161 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
162 "Invalid Network Field", "Malformed AS_PATH",
165 static const char **bgpnotify_minor
[] = {
166 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
168 static const int bgpnotify_minor_siz
[] = {
169 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
170 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
171 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
174 static const char *bgpattr_origin
[] = {
175 "IGP", "EGP", "INCOMPLETE",
177 #define bgp_attr_origin(x) \
178 num_or_str(bgpattr_origin, \
179 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
181 static const char *bgpattr_type
[] = {
182 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
183 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
184 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
185 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
187 #define bgp_attr_type(x) \
188 num_or_str(bgpattr_type, \
189 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
191 /* Subsequent address family identifier, RFC2283 section 7 */
192 static const char *bgpattr_nlri_safi
[] = {
193 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
195 #define bgp_attr_nlri_safi(x) \
196 num_or_str(bgpattr_nlri_safi, \
197 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
199 /* well-known community */
200 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
201 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
202 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
204 /* RFC1700 address family numbers */
206 #define AFNUM_INET6 2
209 #define AFNUM_BBN1822 5
214 #define AFNUM_X121 10
216 #define AFNUM_ATALK 12
217 #define AFNUM_DECNET 13
218 #define AFNUM_BANYAN 14
219 #define AFNUM_E164NSAP 15
221 static const char *afnumber
[] = {
222 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
223 "BBN 1822", "802", "E.163", "E.164", "F.69",
224 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
225 "E.164 with NSAP subaddress",
228 (((x) == 65535) ? afnumber[0] : \
229 num_or_str(afnumber, \
230 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
234 num_or_str(const char **table
, size_t siz
, int value
)
237 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
238 sprintf(buf
, "#%d", value
);
245 bgp_notify_minor(int major
, int minor
)
247 static const char **table
;
253 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
254 && bgpnotify_minor
[major
]) {
255 table
= bgpnotify_minor
[major
];
256 siz
= bgpnotify_minor_siz
[major
];
257 if (0 <= minor
&& minor
< siz
&& table
[minor
])
264 sprintf(buf
, "#%d", minor
);
271 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
277 if (plen
< 0 || 32 < plen
)
280 memset(&addr
, 0, sizeof(addr
));
281 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
283 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
284 ((0xff00 >> (plen
% 8)) & 0xff);
286 sprintf(buf
, "%s/%d", getname((char *)&addr
), plen
);
287 return 1 + (plen
+ 7) / 8;
292 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
294 struct in6_addr addr
;
298 if (plen
< 0 || 128 < plen
)
301 memset(&addr
, 0, sizeof(addr
));
302 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
304 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
305 ((0xff00 >> (plen
% 8)) & 0xff);
307 sprintf(buf
, "%s/%d", getname6((char *)&addr
), plen
);
308 return 1 + (plen
+ 7) / 8;
313 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
325 switch (attr
->bgpa_type
) {
328 printf(" invalid len");
330 printf(" %s", bgp_attr_origin(p
[0]));
332 case BGPTYPE_AS_PATH
:
334 printf(" invalid len");
337 while (p
< dat
+ len
) {
339 * under RFC1965, p[0] means:
340 * 1: AS_SET 2: AS_SEQUENCE
341 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
344 if (p
[0] == 3 || p
[0] == 4)
346 printf("%s", (p
[0] & 1) ? "{" : "");
347 for (i
= 0; i
< p
[1]; i
+= 2) {
348 printf("%s%u", i
== 0 ? "" : " ",
349 ntohs(*(u_int16_t
*)&p
[2 + i
]));
351 printf("%s", (p
[0] & 1) ? "}" : "");
355 case BGPTYPE_NEXT_HOP
:
357 printf(" invalid len");
359 printf(" %s", getname(p
));
361 case BGPTYPE_MULTI_EXIT_DISC
:
362 case BGPTYPE_LOCAL_PREF
:
364 printf(" invalid len");
366 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
368 case BGPTYPE_ATOMIC_AGGREGATE
:
370 printf(" invalid len");
372 case BGPTYPE_AGGREGATOR
:
374 printf(" invalid len");
377 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
380 case BGPTYPE_COMMUNITIES
:
382 printf(" invalid len");
385 for (i
= 0; i
< len
; i
++) {
387 comm
= (u_int32_t
)ntohl(*(u_int32_t
*)&p
[i
]);
389 case BGP_COMMUNITY_NO_EXPORT
:
390 printf(" NO_EXPORT");
392 case BGP_COMMUNITY_NO_ADVERT
:
393 printf(" NO_ADVERTISE");
395 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
396 printf(" NO_EXPORT_SUBCONFED");
399 printf(" (AS #%d value 0x%04x)",
400 (comm
>> 16) & 0xffff, comm
& 0xfffff);
405 case BGPTYPE_MP_REACH_NLRI
:
406 af
= ntohs(*(u_int16_t
*)p
);
409 printf(" %s vendor specific,", af_name(af
));
411 printf(" %s %s,", af_name(af
),
412 bgp_attr_nlri_safi(safi
));
416 if (af
== AFNUM_INET
)
419 else if (af
== AFNUM_INET6
)
428 if (af
== AFNUM_INET
)
431 else if (af
== AFNUM_INET6
)
435 for (i
= 0; i
< tlen
; i
+= advance
) {
436 if (af
== AFNUM_INET
)
437 printf(" %s", getname(p
+ 1 + i
));
439 else if (af
== AFNUM_INET6
)
440 printf(" %s", getname6(p
+ 1 + i
));
450 printf(" %u snpa", snpa
);
451 for (/*nothing*/; snpa
> 0; snpa
--) {
452 printf("(%d bytes)", p
[0]);
459 while (len
- (p
- dat
) > 0) {
460 if (af
== AFNUM_INET
)
461 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
463 else if (af
== AFNUM_INET6
)
464 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
473 case BGPTYPE_MP_UNREACH_NLRI
:
474 af
= ntohs(*(u_int16_t
*)p
);
477 printf(" %s vendor specific,", af_name(af
));
479 printf(" %s %s,", af_name(af
),
480 bgp_attr_nlri_safi(safi
));
485 while (len
- (p
- dat
) > 0) {
486 if (af
== AFNUM_INET
)
487 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
489 else if (af
== AFNUM_INET6
)
490 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
503 bgp_open_print(const u_char
*dat
, int length
)
505 struct bgp_open bgpo
;
506 struct bgp_opt bgpopt
;
511 memcpy(&bgpo
, dat
, sizeof(bgpo
));
512 hlen
= ntohs(bgpo
.bgpo_len
);
514 printf(": Version %d,", bgpo
.bgpo_version
);
515 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
516 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
517 printf(" ID %s,", getname((char *)&bgpo
.bgpo_id
));
518 printf(" Option length %u", bgpo
.bgpo_optlen
);
521 opt
= &((struct bgp_open
*)dat
)->bgpo_optlen
;
524 for (i
= 0; i
< bgpo
.bgpo_optlen
; i
++) {
525 memcpy(&bgpopt
, &opt
[i
], sizeof(bgpopt
));
526 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
527 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
531 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
533 i
+= sizeof(bgpopt
) + bgpopt
.bgpopt_len
;
538 bgp_update_print(const u_char
*dat
, int length
)
541 struct bgp_attr bgpa
;
548 memcpy(&bgp
, dat
, sizeof(bgp
));
549 hlen
= ntohs(bgp
.bgp_len
);
550 p
= dat
+ BGP_SIZE
; /*XXX*/
553 /* Unfeasible routes */
554 len
= ntohs(*(u_int16_t
*)p
);
556 printf(" (Withdrawn routes: %d bytes)", len
);
560 len
= ntohs(*(u_int16_t
*)p
);
562 /* do something more useful!*/
564 printf(" (Path attributes:"); /* ) */
566 while (i
< 2 + len
) {
569 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
570 alen
= bgp_attr_len(&bgpa
);
571 aoff
= bgp_attr_off(&bgpa
);
573 if (vflag
&& newline
)
578 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
579 if (bgpa
.bgpa_flags
) {
581 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
582 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
583 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
584 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
587 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
601 if (len
&& dat
+ length
> p
)
603 if (dat
+ length
> p
) {
604 printf("(NLRI:"); /* ) */
605 while (dat
+ length
> p
) {
607 i
= decode_prefix4(p
, buf
, sizeof(buf
));
620 bgp_notification_print(const u_char
*dat
, int length
)
622 struct bgp_notification bgpn
;
625 memcpy(&bgpn
, dat
, sizeof(bgpn
));
626 hlen
= ntohs(bgpn
.bgpn_len
);
628 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
629 printf(" subcode %s",
630 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
634 bgp_header_print(const u_char
*dat
, int length
)
638 memcpy(&bgp
, dat
, sizeof(bgp
));
639 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
641 switch (bgp
.bgp_type
) {
643 bgp_open_print(dat
, length
);
646 bgp_update_print(dat
, length
);
648 case BGP_NOTIFICATION
:
649 bgp_notification_print(dat
, length
);
658 bgp_print(const u_char
*dat
, int length
)
663 const u_char marker
[] = {
664 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
665 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
672 if (snapend
< dat
+ length
)
680 while (p
< snapend
) {
681 if (!TTEST2(p
[0], 1))
688 if (!TTEST2(p
[0], sizeof(marker
)))
690 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
695 /* found BGP header */
696 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
697 memcpy(&bgp
, p
, sizeof(bgp
));
702 hlen
= ntohs(bgp
.bgp_len
);
703 if (vflag
&& newline
)
707 if (TTEST2(p
[0], hlen
)) {
708 bgp_header_print(p
, hlen
);
713 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));