Skip to content

Commit

Permalink
Merge pull request FRRouting#5954 from ton31337/feature/rfc7607
Browse files Browse the repository at this point in the history
 bgpd: Proscribe the use of AS 0 (zero)
  • Loading branch information
riw777 authored Mar 17, 2020
2 parents 987ae12 + 33d022b commit 047315d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 3 deletions.
16 changes: 16 additions & 0 deletions bgpd/bgp_aspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ bool aspath_check_as_sets(struct aspath *aspath)
return false;
}

/* Check if aspath has BGP_AS_ZERO */
bool aspath_check_as_zero(struct aspath *aspath)
{
struct assegment *seg = aspath->segments;
unsigned int i;

while (seg) {
for (i = 0; i < seg->length; i++)
if (seg->as[i] == BGP_AS_ZERO)
return true;
seg = seg->next;
}

return false;
}

/* Estimate size aspath /might/ take if encoded into an
* ASPATH attribute.
*
Expand Down
2 changes: 2 additions & 0 deletions bgpd/bgp_aspath.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define BGP_PRIVATE_AS4_MAX 4294967294U

/* we leave BGP_AS_MAX as the 16bit AS MAX number. */
#define BGP_AS_ZERO 0
#define BGP_AS_MAX 65535U
#define BGP_AS4_MAX 4294967295U
/* Transition 16Bit AS as defined by IANA */
Expand Down Expand Up @@ -121,6 +122,7 @@ extern bool aspath_left_confed_check(struct aspath *);
extern unsigned long aspath_count(void);
extern unsigned int aspath_count_hops(const struct aspath *);
extern bool aspath_check_as_sets(struct aspath *aspath);
extern bool aspath_check_as_zero(struct aspath *aspath);
extern unsigned int aspath_count_confeds(struct aspath *);
extern unsigned int aspath_size(struct aspath *);
extern as_t aspath_highest(struct aspath *);
Expand Down
45 changes: 42 additions & 3 deletions bgpd/bgp_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,15 @@ static int bgp_attr_aspath(struct bgp_attr_parser_args *args)
0);
}

/* Codification of AS 0 Processing */
if (aspath_check_as_zero(attr->aspath)) {
flog_err(EC_BGP_ATTR_MAL_AS_PATH,
"Malformed AS path, contains BGP_AS_ZERO(0) from %s",
peer->host);
return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
0);
}

/* Set aspath attribute flag. */
attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS_PATH);

Expand Down Expand Up @@ -1469,6 +1478,15 @@ static int bgp_attr_as4_path(struct bgp_attr_parser_args *args,
0);
}

/* Codification of AS 0 Processing */
if (aspath_check_as_zero(*as4_path)) {
flog_err(EC_BGP_ATTR_MAL_AS_PATH,
"Malformed AS4 path, contains BGP_AS_ZERO(0) from %s",
peer->host);
return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
0);
}

/* Set aspath attribute flag. */
attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH);

Expand Down Expand Up @@ -1615,6 +1633,7 @@ static int bgp_attr_aggregator(struct bgp_attr_parser_args *args)
struct peer *const peer = args->peer;
struct attr *const attr = args->attr;
const bgp_size_t length = args->length;
as_t aggregator_as;

int wantedlen = 6;

Expand All @@ -1632,9 +1651,19 @@ static int bgp_attr_aggregator(struct bgp_attr_parser_args *args)
}

if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV))
attr->aggregator_as = stream_getl(peer->curr);
aggregator_as = stream_getl(peer->curr);
else
attr->aggregator_as = stream_getw(peer->curr);
aggregator_as = stream_getw(peer->curr);

/* Codification of AS 0 Processing */
if (aggregator_as == BGP_AS_ZERO) {
flog_err(EC_BGP_ATTR_LEN,
"AGGREGATOR attribute is BGP_AS_ZERO(0)");
return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
args->total);
}

attr->aggregator_as = aggregator_as;
attr->aggregator_addr.s_addr = stream_get_ipv4(peer->curr);

/* Set atomic aggregate flag. */
Expand All @@ -1652,6 +1681,7 @@ bgp_attr_as4_aggregator(struct bgp_attr_parser_args *args,
struct peer *const peer = args->peer;
struct attr *const attr = args->attr;
const bgp_size_t length = args->length;
as_t aggregator_as;

if (length != 8) {
flog_err(EC_BGP_ATTR_LEN, "New Aggregator length is not 8 [%d]",
Expand All @@ -1660,7 +1690,16 @@ bgp_attr_as4_aggregator(struct bgp_attr_parser_args *args,
0);
}

*as4_aggregator_as = stream_getl(peer->curr);
/* Codification of AS 0 Processing */
aggregator_as = stream_getl(peer->curr);
if (aggregator_as == BGP_AS_ZERO) {
flog_err(EC_BGP_ATTR_LEN,
"AS4_AGGREGATOR attribute is BGP_AS_ZERO(0)");
return bgp_attr_malformed(args, BGP_NOTIFY_UPDATE_MAL_AS_PATH,
0);
}

*as4_aggregator_as = aggregator_as;
as4_aggregator_addr->s_addr = stream_get_ipv4(peer->curr);

attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR);
Expand Down
9 changes: 9 additions & 0 deletions bgpd/bgp_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,15 @@ static int bgp_open_receive(struct peer *peer, bgp_size_t size)
return BGP_Stop;
}

/* Codification of AS 0 Processing */
if (remote_as == BGP_AS_ZERO) {
flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0",
peer->host);
bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS);
return BGP_Stop;
}

if (remote_as == BGP_AS_TRANS) {
/* Take the AS4 from the capability. We must have received the
* capability now! Otherwise we have a asn16 peer who uses
Expand Down
4 changes: 4 additions & 0 deletions bgpd/bgp_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,10 @@ int subgroup_announce_check(struct bgp_node *rn, struct bgp_path_info *pi,
if (aspath_check_as_sets(attr->aspath))
return 0;

/* Codification of AS 0 Processing */
if (aspath_check_as_zero(attr->aspath))
return 0;

if (CHECK_FLAG(bgp->flags, BGP_FLAG_GRACEFUL_SHUTDOWN)) {
if (peer->sort == BGP_PEER_IBGP
|| peer->sort == BGP_PEER_CONFED) {
Expand Down
2 changes: 2 additions & 0 deletions doc/user/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ BGP
:t:`The Resource Public Key Infrastructure (RPKI) to Router Protocol. R. Bush, R. Austein. January 2013.`
- :rfc:`6811`
:t:`BGP Prefix Origin Validation. P. Mohapatra, J. Scudder, D. Ward, R. Bush, R. Austein. January 2013.`
- :rfc:`7607`
:t:`Codification of AS 0 Processing. W. Kumari, R. Bush, H. Schiller, K. Patel. August 2015.`
- :rfc:`7611`
:t:`BGP ACCEPT_OWN Community Attribute. J. Uttaro, P. Mohapatra, D. Smith, R. Raszuk, J. Scudder. August 2015.`
- :rfc:`7999`
Expand Down
29 changes: 29 additions & 0 deletions tests/bgpd/test_aspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,20 @@ static struct test_segment {
14,
{NULL, NULL, 0, 0, 0, 0, 0, 0},
},
{
/* 28 */
"BGP_AS_ZERO",
"seq(8466,3,52737,0,4096)",
{0x2, 0x5,
0x21, 0x12,
0x00, 0x03,
0xce, 0x01,
0x00, 0x00,
0x10, 0x00},
12,
{"8466 3 52737 0 4096", "8466 3 52737 0 4096", 5, 0,
NOT_ALL_PRIVATE, 4096, 4, 8466},
},
{NULL, NULL, {0}, 0, {NULL, 0, 0}}};

#define COMMON_ATTRS \
Expand Down Expand Up @@ -678,6 +692,21 @@ static struct aspath_tests {
COMMON_ATTR_SIZE + 3,
&test_segments[0],
},
/* 13 */
{
"4b AS4_PATH: BGP_AS_ZERO",
&test_segments[28],
"8466 3 52737 0 4096",
AS4_DATA,
-1,
PEER_CAP_AS4_RCV | PEER_CAP_AS4_ADV,
{
COMMON_ATTRS,
BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
BGP_ATTR_AS4_PATH, 22,
},
COMMON_ATTR_SIZE + 3,
},
{NULL, NULL, NULL, 0, 0, 0, {0}, 0},
};

Expand Down
2 changes: 2 additions & 0 deletions tests/bgpd/test_aspath.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def _attrtest(self, line):
TestAspath.parsertest("zero-size overflow")
TestAspath.parsertest("zero-size overflow + valid segment")
TestAspath.parsertest("invalid segment type")
TestAspath.parsertest("BGP_AS_ZERO")

for i in range(10):
TestAspath.okfail("prepend test %d" % i)
Expand All @@ -77,3 +78,4 @@ def _attrtest(self, line):
TestAspath.attrtest("4b AS_PATH: bad flags")
TestAspath.attrtest("4b AS4_PATH w/o AS_PATH")
TestAspath.attrtest("4b AS4_PATH: confed")
TestAspath.attrtest("4b AS4_PATH: BGP_AS_ZERO")

0 comments on commit 047315d

Please sign in to comment.