32 Commits
1.2 ... 1.5

Author SHA1 Message Date
Job Snijders
6484a9a40c fix typo 2022-07-25 14:15:29 +00:00
Jeremiah Millay
3ec83e255a Bump version and change log for 1.5 release (#61) 2022-07-25 16:11:44 +02:00
Job Snijders
c01ebfc3cb cleanup 2022-07-25 09:18:33 +00:00
Job Snijders
8dfcfb3173 add RPKI source (available via most irrd4 instances) 2022-07-25 09:11:23 +00:00
Fiona Weber
c2126343cf Add AS-List Feature for JunOS (#57)
Co-authored-by: Fiona Weber <fiona.weber@wobcom.de>
2022-07-07 09:07:24 +02:00
Massimiliano Stucchi
96079b8901 Improve readability of the manpage (#59) 2022-06-07 11:20:16 -04:00
Job Snijders
58521eb687 Remove a bashism (thanks Marco D'Itri!) 2022-02-11 14:47:48 +00:00
Robert Scheck
8b804f83fd Update matrixbuild to current Linux distributions (#55)
Previously, at the time of #50, broken Fedora Rawhide, 35, CentOS
Stream 9 (development) have been fixed. Switch Alpine 3.14 -> 3.15.
2022-02-06 09:30:05 +01:00
Job Snijders
f26a04e8dd Remove unused header includes.
Thanks Gavin Henry (@ghenry) for reporting.

This fixes #53
2021-10-04 09:50:14 +00:00
Job Snijders
392a44a536 update docs 2021-09-21 20:41:30 +00:00
Stefan Marti
2aed3f9e67 Huawei XPL format support (#45)
Add Huawei XPL format support
2021-09-02 20:45:45 +00:00
Robert Scheck
d285db3c81 Update matrixbuild to current Linux distributions (#50)
Debian 12, 11 and 10, Ubuntu 20.04 and 16.04, Fedora 34 and 33,
CentOS Stream 8 and CentOS Linux 7, Rocky Linux 8, Alpine Edge and
3.14.

Note: Especially CentOS containers are better maintained on Quay.io
rather on Docker Hub (likely because it's a Red Hat service).

CentOS Linux 8 reaches EOL on 2021-12-31 and is superseeded by Rocky
Linux 8 as alternative bug-for-bug compatible RHEL 8 rebuild.

Fedora Rawhide, 35 and CentOS Stream 9 (development) are currently
broken: https://bugzilla.redhat.com/show_bug.cgi?id=1988199#c13
2021-08-30 11:09:29 +00:00
Job Snijders
5d27a24659 reformat main getopt routine 2021-08-22 23:00:51 +00:00
Vincent Bernat
97d1f2acda Add missing .Ed to manual page (#49) 2021-08-20 19:59:07 +02:00
Job Snijders
61a7bcf671 Pretify bird aspath output 2021-08-20 11:19:41 +00:00
Job Snijders
e920d74007 Slightly tweak whitespaces in extended cisco output 2021-08-20 11:14:42 +00:00
Job Snijders
b1074667e8 Add missing closure in bird aspath output 2021-08-20 10:58:07 +00:00
Job Snijders
bdc455d72e Update information on SOURCES 2021-08-20 10:22:19 +00:00
Job Snijders
8cc7ba2751 Bump version 2021-08-20 10:08:59 +00:00
Job Snijders
06b430b420 align bgpq4_print_cisco_aspath and bgpq4_print_cisco_oaspath 2021-08-20 03:51:51 +00:00
Job Snijders
8c51669da9 Clarify what options are mutually exclusive
This fixes #20
2021-08-20 03:06:26 +00:00
Job Snijders
dc1270f10f Improve mikrotik output
This fixes #32
2021-08-20 02:44:37 +00:00
Job Snijders
362f747f0f Let OS decide on buffer size 2021-08-20 02:10:31 +00:00
Job Snijders
6c8c6cfec1 Reduce memory usage 2021-08-20 01:58:38 +00:00
Job Snijders
fb29cd54e1 make local functions static 2021-08-20 01:12:43 +00:00
Job Snijders
903704ef3b Update CHANGES 2021-08-20 01:03:18 +00:00
Job Snijders
b481111cf6 Replace ASN storage facility with Red Black tree macros (RB_) 2021-08-20 00:50:56 +00:00
Job Snijders
80b7dcf4de KNF & renaming 2021-08-19 20:21:25 +00:00
Job Snijders
5ce7d7bfea Untangle function and KNF 2021-08-19 19:59:21 +00:00
Job Snijders
8ec260f03f chop off first and last line 2021-08-19 16:08:37 +00:00
Job Snijders
675155356d Make note of Ben's extra aggregation trick 2021-08-19 16:02:03 +00:00
Job Snijders
da750ed48f KNF 2021-08-19 15:39:19 +00:00
32 changed files with 1306 additions and 1296 deletions

17
.github/images/alpine.Dockerfile vendored Normal file
View File

@@ -0,0 +1,17 @@
ARG image=alpine:latest
FROM $image
# Install dependencies
RUN apk upgrade
RUN apk add autoconf automake file gcc gzip libtool make musl-dev
# Add source code
ADD . /src
WORKDIR /src
# Run steps
RUN ./bootstrap
RUN ./configure
RUN make
RUN make check
RUN make distcheck

1
.github/images/alpine:3.15.Dockerfile vendored Symbolic link
View File

@@ -0,0 +1 @@
alpine.Dockerfile

1
.github/images/alpine:edge.Dockerfile vendored Symbolic link
View File

@@ -0,0 +1 @@
alpine.Dockerfile

View File

@@ -1,10 +1,9 @@
ARG image=centos:8
FROM $image
ARG image=centos/centos:latest
FROM quay.io/$image
# Install dependencies
RUN yum update -y
RUN yum groupinstall -y 'Development Tools'
RUN yum install -y autoconf automake findutils libtool
RUN yum install -y autoconf automake gcc libtool make diffutils file
# Add source code
ADD . /src
@@ -16,4 +15,3 @@ RUN ./configure
RUN make
RUN make check
RUN make distcheck

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -1 +0,0 @@
centos.Dockerfile

View File

@@ -1 +0,0 @@
centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

View File

@@ -1 +0,0 @@
centos.Dockerfile

View File

@@ -1 +0,0 @@
centos.Dockerfile

View File

@@ -0,0 +1 @@
../centos.Dockerfile

1
.github/images/ubuntu:focal.Dockerfile vendored Symbolic link
View File

@@ -0,0 +1 @@
debian.Dockerfile

View File

@@ -9,14 +9,20 @@ jobs:
fail-fast: false
matrix:
dockerenv:
- debian:bookworm
- debian:bullseye
- debian:buster
- debian:stretch
- ubuntu:focal
- ubuntu:bionic
- ubuntu:xenial
- centos:8
- centos:7
- fedora:31
- fedora:30
- fedora/fedora:36
- fedora/fedora:35
- fedora/fedora:34
- centos/centos:stream9
- centos/centos:stream8
- centos/centos:7
- rockylinux/rockylinux:8
- alpine:edge
- alpine:3.15
steps:
- uses: actions/checkout@v1
- name: Run build on ${{matrix.dockerenv}}

15
CHANGES
View File

@@ -1,3 +1,18 @@
1.5 (2022-07-25)
- Add support for the new Junos as-path-origins feature
1.4 (2021-08-20)
- Fix BIRD aspath output
1.3 (2021-08-20)
- Change versioning from X.Y.Z to Y.Z
- The repository file hierachy has been reorganized (compat/ include/)
- Man page has been extended
- Large portions of code have been reformatted to adhere to OpenBSD's source
file style guide to improve readability.
- Refactor: replace two-dimensional array for ASN storage with Red-Black tree
- Reduced memory usage
0.0.9 (2021-08-18)
- Fix various memory errors

29
IDEAS Normal file
View File

@@ -0,0 +1,29 @@
Ben Maddison taught me another aggregation trick:
route-set: AS37271:RS-EXAMPLE
mp-members: 192.0.2.0/27
mp-members: 192.0.2.32/27
mp-members: 192.0.2.64/27
mp-members: 192.0.2.96/27
mp-members: 192.0.2.128/26
mp-members: 192.0.2.128/27
mp-members: 192.0.2.160/27
mp-members: 192.0.2.192/27
mp-members: 192.0.2.224/27
descr: Example route-set
mnt-by: MAINT-AS37271
changed: benm@workonline.africa 20210819
source: RADB
BGPQ4 produces the following:
$ bgpq4 -A AS37271:RS-EXAMPLE
no ip prefix-list NN
ip prefix-list NN permit 192.0.2.0/25 ge 27 le 27
ip prefix-list NN permit 192.0.2.128/26 le 27
ip prefix-list NN permit 192.0.2.192/26 ge 27 le 27
But the following aggregation also is valid, and shorter:
ip prefix-list NN permit 192.0.2.0/24 ge 27 le 27
ip prefix-list NN permit 192.0.2.128/26

View File

@@ -1,5 +1,3 @@
BGPQ4(8) - System Manager's Manual
# NAME
**bgpq4** - bgp filtering automation tool
@@ -13,6 +11,7 @@ BGPQ4(8) - System Manager's Manual
\[**-f**&nbsp;*asn*&nbsp;|
**-F**&nbsp;*fmt*&nbsp;|
**-G**&nbsp;*asn*
**-H**&nbsp;*asn*
**-t**]
\[**-46ABbDdJjNnsXU**]
\[**-a**&nbsp;*asn*]
@@ -29,7 +28,7 @@ BGPQ4(8) - System Manager's Manual
The
**bgpq4**
utility used to generate configurations (prefix-lists, extended
access-lists, policy-statement terms and as-path lists) based on RADB data.
access-lists, policy-statement terms and as-path lists) based on IRR data.
The options are as follows:
@@ -84,6 +83,10 @@ The options are as follows:
> generate output as-path access-list.
**-H** *number*
> generate output as-list for JunOS 21.3R1+ `as-path-origin` filter (JunOS only)
**-h** *host\[:port]*
> host running IRRD database (default: rr.ntt.net).
@@ -147,7 +150,7 @@ The options are as follows:
**-S** *sources*
> use specified sources only (recommended: RADB,RIPE,APNIC).
> use specified sources only (recommended: RPKI,AFRINIC,ARIN,APNIC,LACNIC,RIPE).
**-t**
@@ -165,6 +168,10 @@ The options are as follows:
> generate config for Huawei devices (Cisco IOS by default)
**-u**
> generate output in Huawei XPL format.
**-X**
> generate config for Cisco IOS XR devices (plain IOS by default).
@@ -222,7 +229,7 @@ more compact:
ip prefix-list eltel permit 89.112.64.0/19
ip prefix-list eltel permit 217.170.64.0/19 ge 20 le 20
\- you see, prefixes 89.112.0.0/19 and 89.112.32.0/19 now aggregated
Prefixes 89.112.0.0/19 and 89.112.32.0/19 now aggregated
into single entry 89.112.0.0/18 ge 19 le 19.
Well, for Juniper we can generate even more interesting policy-options,
@@ -333,6 +340,46 @@ be in one line (sometimes it makes sense):
$ bgpq4 -6F "%n/%l; " as-eltel
2001:1b00::/32; 2620:4f:8000::/48; 2a04:bac0::/29; 2a05:3a80::/48;
# NOTES ON SOURCES
By default
*bgpq4*
trusts to data from all databases mirrored into NTT's IRR service.
Unfortunately, not all these databases are equal in how much can we
trust their data.
RIR maintained databases (AFRINIC, ARIN, APNIC, LACNIC and RIPE)
shall be trusted more than the others because they are indeed have the
knowledge about which address space allocated to this or that ASn,
other databases lack this knowledge and can (and, actually, do) contain
some stale data: noone but RIRs care to remove outdated route-objects
when address space revoked from one ASn and allocated to another.
In order to keep their filters both compact and actual,
*bgpq4 users*
are encouraged to use '-S' flag to limit database sources to only
ones they trust.
General recommendations:
Use minimal set of RIR databases (only those in which you and your
customers have registered route-objects).
Avoid using ARIN-NONAUTH and RIPE-NONAUTH as trusted source: these records
were created in database but for address space allocated to different RIR,
so the NONAUTH databases have no chance to confirm validity of this route
object.
$ bgpq4 -S RIPE,RADB as-space
no ip prefix-list NN
ip prefix-list NN permit 195.190.32.0/19
$ bgpq4 -S RADB,RIPE as-space
no ip prefix-list NN
ip prefix-list NN permit 45.4.4.0/22
ip prefix-list NN permit 45.4.132.0/22
ip prefix-list NN permit 45.6.128.0/22
ip prefix-list NN permit 45.65.184.0/22
[...]
# PERFORMANCE
To improve \`bgpq4\` performance when expanding extra-large AS-SETs you
@@ -406,5 +453,3 @@ Users and interested parties can subscribe to the BGPQ4 mailing list bgpq4@tcp0.
# PROJECT MAINTAINER
Job Snijders &lt;job@sobornost.net&gt;
OpenBSD 7.0 - December 23, 2020

View File

@@ -1 +1 @@
1.2
1.5

59
bgpq4.8
View File

@@ -37,6 +37,7 @@
.Fl f Ar asn |
.Fl F Ar fmt |
.Fl G Ar asn
.Fl H Ar asn
.Fl t
.Oc
.Op Fl 46ABbDdJjNnsXU
@@ -52,7 +53,7 @@
The
.Nm
utility used to generate configurations (prefix-lists, extended
access-lists, policy-statement terms and as-path lists) based on RADB data.
access-lists, policy-statement terms and as-path lists) based on IRR data.
.Pp
The options are as follows:
.Bl -tag -width Ds
@@ -83,6 +84,10 @@ generate input as-path access-list.
generate output in user-defined format.
.It Fl G Ar number
generate output as-path access-list.
.It Fl H Ar number
generate output as-list for
.Em as-path-origin
filter (JunOS 21.3R1+)
.It Fl h Ar host[:port]
host running IRRD database (default: rr.ntt.net).
.It Fl J
@@ -115,7 +120,7 @@ allow more specific routes up to specified masklen too.
.It Fl s
generate sequence numbers in IOS-style prefix-lists.
.It Fl S Ar sources
use specified sources only (recommended: RADB,RIPE,APNIC).
use specified sources only (recommended: RPKI,AFRINIC,APNIC,ARIN,LACNIC,RIPE).
.It Fl t
generate as-sets for OpenBGPd, BIRD and JSON formats.
.It Fl T
@@ -124,6 +129,8 @@ disable pipelining (not recommended).
generate as-path strings of no more than len items (use 0 for inifinity).
.It Fl U
generate config for Huawei devices (Cisco IOS by default)
.It Fl u
generate config for Huawei devices in XPL format (Cisco IOS by default)
.It Fl X
generate config for Cisco IOS XR devices (plain IOS by default).
.It Fl z
@@ -178,7 +185,8 @@ ip prefix-list eltel permit 89.112.64.0/19
ip prefix-list eltel permit 217.170.64.0/19 ge 20 le 20
.Ed
.fi
- you see, prefixes 89.112.0.0/19 and 89.112.32.0/19 now aggregated
.Pp
Prefixes 89.112.0.0/19 and 89.112.32.0/19 now aggregated
into single entry 89.112.0.0/18 ge 19 le 19.
.Pp
Well, for Juniper we can generate even more interesting policy-options,
@@ -274,15 +282,54 @@ new line
tabulation
.El
.Pp
Please note that no new lines inserted automatically after each sentence,
you have to add them into format string manually, elsewhere output will
be in one line (sometimes it makes sense):
Please note that no new lines are inserted automatically after each sentence.
You have to add them into format string manually, otherwise the output will
be in one single line (sometimes it makes sense):
.nf
.Bd -literal
$ bgpq4 -6F "%n/%l; " as-eltel
2001:1b00::/32; 2620:4f:8000::/48; 2a04:bac0::/29; 2a05:3a80::/48;
.Ed
.fi
.Sh NOTES ON SOURCES
By default
.Em bgpq4
trusts data from all the databases mirrored into NTT's IRR service.
Unfortunately, not all these databases are equal in how much we can
trust their data.
RIR maintained databases (AFRINIC, ARIN, APNIC, LACNIC and RIPE)
shall be trusted more than the others because they have the
knowledge about who the rightful holders of resources are, while
other databases lack this knowledge and can (and, actually do) contain
stale data: no one but the RIRs care to remove outdated route-objects
when address space is de-allocated or transferred.
In order to keep their filters both compact and actual,
.Em bgpq4 users
are encouraged to use '-S' flag to limit database sources to only
the ones they trust.
.Pp
General recommendations:
.Pp
Use a minimal set of RIR databases (only those in which you and your
customers have registered route-objects).
.Pp
Avoid using ARIN-NONAUTH and RIPE-NONAUTH as trusted sources: these records
were created in the database, but for address space allocated to different RIRs,
so the NONAUTH databases have no chance to confirm validity of the route
objects they contain.
.Bd -literal
$ bgpq4 -S RIPE,RADB as-space
no ip prefix-list NN
ip prefix-list NN permit 195.190.32.0/19
$ bgpq4 -S RADB,RIPE as-space
no ip prefix-list NN
ip prefix-list NN permit 45.4.4.0/22
ip prefix-list NN permit 45.4.132.0/22
ip prefix-list NN permit 45.6.128.0/22
ip prefix-list NN permit 45.65.184.0/22
[...]
.Ed
.Sh PERFORMANCE
To improve `bgpq4` performance when expanding extra-large AS-SETs you
shall tune OS settings to enlarge TCP send buffer.

View File

@@ -5,7 +5,7 @@
bsd_environment() {
# Based on https://github.com/rvm/rvm/blob/59fe3b39f0fb5ae01ed5b9aa187201080815ac16/scripts/functions/build_config_system#L123
if [[ -z "${AUTOCONF_VERSION:-}" ]]
if [ -z "${AUTOCONF_VERSION}" ]
then
export AUTOCONF_VERSION
AUTOCONF_VERSION="$(
@@ -17,7 +17,7 @@ bsd_environment() {
echo "Using autoconf version: $AUTOCONF_VERSION"
fi
if [[ -z "${AUTOMAKE_VERSION:-}" ]]
if [ -z "${AUTOMAKE_VERSION}" ]
then
export AUTOMAKE_VERSION
# FreeBSD might have automake-wrapper

View File

@@ -58,7 +58,15 @@ tentry_cmp(struct sx_tentry *a, struct sx_tentry *b)
return strcasecmp(a->text, b->text);
}
RB_GENERATE_STATIC(tentree, sx_tentry, entries, tentry_cmp);
RB_GENERATE_STATIC(tentree, sx_tentry, entry, tentry_cmp);
int
asn_cmp(struct asn_entry *a, struct asn_entry *b)
{
return (a->asn < b->asn ? -1 : a->asn > b->asn);
}
RB_GENERATE(asn_tree, asn_entry, entry, asn_cmp);
int
bgpq_expander_init(struct bgpq_expander *b, int af)
@@ -80,22 +88,12 @@ bgpq_expander_init(struct bgpq_expander *b, int af)
b->sources = "";
b->name = "NN";
b->aswidth = 8;
b->asn32s[0] = malloc(8192);
if (!b->asn32s[0]) {
sx_report(SX_FATAL,"Unable to allocate 8192 bytes: %s\n",
strerror(errno));
exit(1);
}
memset(b->asn32s[0], 0, 8192);
b->identify = 1;
b->server = "rr.ntt.net";
b->port = "43";
// b->wq = STAILQ_HEAD_INITIALZIER(b->wq);
// b->rq = STAILQ_HEAD_INITIALZIER(b->rq);
// b->rsets = STAILQ_HEAD_INITIALZIER(b->rsets);
// b->macroses = STAILQ_HEAD_INITIALZIER(b->macroses);
RB_INIT(&b->asnlist);
STAILQ_INIT(&b->wq);
STAILQ_INIT(&b->rq);
STAILQ_INIT(&b->rsets);
@@ -116,14 +114,14 @@ fixups:
int
bgpq_expander_add_asset(struct bgpq_expander *b, char *as)
{
struct sx_slentry *le;
struct slentry *le;
if (!b || !as)
return 0;
le = sx_slentry_new(as);
STAILQ_INSERT_TAIL(&b->macroses, le, entries);
STAILQ_INSERT_TAIL(&b->macroses, le, entry);
return 1;
}
@@ -131,7 +129,7 @@ bgpq_expander_add_asset(struct bgpq_expander *b, char *as)
int
bgpq_expander_add_rset(struct bgpq_expander *b, char *rs)
{
struct sx_slentry *le;
struct slentry *le;
if (!b || !rs)
return 0;
@@ -141,7 +139,7 @@ bgpq_expander_add_rset(struct bgpq_expander *b, char *rs)
if (!le)
return 0;
STAILQ_INSERT_TAIL(&b->rsets, le, entries);
STAILQ_INSERT_TAIL(&b->rsets, le, entry);
return 1;
}
@@ -183,46 +181,29 @@ bgpq_expander_add_stop(struct bgpq_expander *b, char *rs)
int
bgpq_expander_add_as(struct bgpq_expander *b, char *as)
{
char *eoa;
uint32_t asn1 = 0, asn2 = 0;
uint32_t asno = 0;
char *eoa;
uint32_t asno = 0;
struct asn_entry *asne;
if (!b || !as)
return 0;
asno = strtoul(as + 2, &eoa, 10);
if (eoa && *eoa != 0) {
sx_report(SX_ERROR,"Invalid symbol in AS number: '%c' in %s\n",
*eoa, as);
return 0;
}
if (asno > 65535) {
asn1 = asno / 65536;
asn2 = asno % 65536;
} else
asn1 = asno;
if (!expand_special_asn &&
((asno >= 4200000000ul) || (asno >= 64496 && asno <= 65551)))
return 0;
if (!b->asn32s[asn1]) {
b->asn32s[asn1] = malloc(8192);
if (!b->asn32s[asn1]) {
sx_report(SX_FATAL, "Unable to allocate 8192 "
"bytes: %s. Unable to add asn32 %s to "
" future expansion\n", strerror(errno), as);
return 0;
}
memset(b->asn32s[asn1], 0, 8192);
}
if ((asne = malloc(sizeof(struct asn_entry))) == NULL)
err(1, NULL);
if (asno > 65535)
b->asn32s[asn1][asn2 / 8] |= (0x80 >> (asn2 % 8));
else
b->asn32s[0][asn1 / 8] |= (0x80 >> (asn1 % 8));
asne->asn = asno;
RB_INSERT(asn_tree, &b->asnlist, asne);
return 1;
}
@@ -261,24 +242,24 @@ bgpq_expander_add_prefix_range(struct bgpq_expander *b, char *prefix)
static int
bgpq_expanded_macro(char *as, struct bgpq_expander *ex,
struct bgpq_request *req)
struct request *req)
{
bgpq_expander_add_as(ex, as);
return 1;
}
struct bgpq_request *bgpq_pipeline(struct bgpq_expander *b,
int (*callback)(char *, struct bgpq_expander *b, struct bgpq_request *req),
struct request *bgpq_pipeline(struct bgpq_expander *b,
int (*callback)(char *, struct bgpq_expander *b, struct request *req),
void *udata, char *fmt, ...);
int bgpq_expand_irrd(struct bgpq_expander *b,
int (*callback)(char*, struct bgpq_expander *b, struct bgpq_request *req),
int (*callback)(char*, struct bgpq_expander *b, struct request *req),
void *udata, char *fmt, ...);
static int
bgpq_expanded_macro_limit(char *as, struct bgpq_expander *b,
struct bgpq_request *req)
struct request *req)
{
if (!strncasecmp(as, "AS-", 3) || strchr(as, '-') || strchr(as, ':')) {
struct sx_tentry tkey = { .text = as };
@@ -300,7 +281,7 @@ bgpq_expanded_macro_limit(char *as, struct bgpq_expander *b,
req->depth + 1 < b->maxdepth)) {
bgpq_expander_add_already(b, as);
if (pipelining) {
struct bgpq_request *req1 = bgpq_pipeline(b,
struct request *req1 = bgpq_pipeline(b,
bgpq_expanded_macro_limit, NULL, "!i%s\n",
as);
req1->depth = req->depth + 1;
@@ -343,7 +324,7 @@ bgpq_expanded_macro_limit(char *as, struct bgpq_expander *b,
static int
bgpq_expanded_prefix(char *as, struct bgpq_expander *ex,
struct bgpq_request *req __attribute__((unused)))
struct request *req __attribute__((unused)))
{
char *d = strchr(as, '^');
@@ -357,7 +338,7 @@ bgpq_expanded_prefix(char *as, struct bgpq_expander *ex,
static int
bgpq_expanded_v6prefix(char *prefix, struct bgpq_expander *ex,
struct bgpq_request *req)
struct request *req)
{
char *d = strchr(prefix, '^');
@@ -371,16 +352,16 @@ bgpq_expanded_v6prefix(char *prefix, struct bgpq_expander *ex,
int bgpq_pipeline_dequeue(int fd, struct bgpq_expander *b);
static struct bgpq_request *
bgpq_request_alloc(char *request, int (*callback)(char *, struct bgpq_expander *,
struct bgpq_request *), void *udata)
static struct request *
request_alloc(char *request, int (*callback)(char *, struct bgpq_expander *,
struct request *), void *udata)
{
struct bgpq_request *bp = malloc(sizeof(struct bgpq_request));
struct request *bp = malloc(sizeof(struct request));
if (!bp)
return NULL;
memset(bp, 0, sizeof(struct bgpq_request));
memset(bp, 0, sizeof(struct request));
bp->request = strdup(request);
bp->offset = 0;
bp->size = strlen(bp->request);
@@ -391,7 +372,7 @@ bgpq_request_alloc(char *request, int (*callback)(char *, struct bgpq_expander *
}
static void
bgpq_request_free(struct bgpq_request *req)
request_free(struct request *req)
{
if (req->request)
free(req->request);
@@ -399,14 +380,14 @@ bgpq_request_free(struct bgpq_request *req)
free(req);
}
struct bgpq_request *
struct request *
bgpq_pipeline(struct bgpq_expander *b,
int (*callback)(char *, struct bgpq_expander *, struct bgpq_request *),
int (*callback)(char *, struct bgpq_expander *, struct request *),
void *udata, char *fmt, ...)
{
char request[128];
int ret;
struct bgpq_request *bp = NULL;
struct request *bp = NULL;
va_list ap;
va_start(ap, fmt);
@@ -415,11 +396,11 @@ bgpq_pipeline(struct bgpq_expander *b,
SX_DEBUG(debug_expander,"expander: sending %s", request);
bp = bgpq_request_alloc(request, callback, udata);
bp = request_alloc(request, callback, udata);
if (!bp) {
sx_report(SX_FATAL,"Unable to allocate %lu bytes: %s\n",
(unsigned long)sizeof(struct bgpq_request),
(unsigned long)sizeof(struct request),
strerror(errno));
exit(1);
}
@@ -449,23 +430,35 @@ bgpq_pipeline(struct bgpq_expander *b,
static void
bgpq_expander_invalidate_asn(struct bgpq_expander *b, const char *q)
{
char *eptr;
unsigned long asn = 0;
struct asn_entry find, *res;
if (!strncmp(q, "!gas", 4) || !strncmp(q, "!6as", 4)) {
char *eptr;
unsigned long asn = strtoul(q+4, &eptr, 10), asn0, asn1 = 0;
if (!asn || asn == ULONG_MAX || asn >= 4294967295
|| (eptr && *eptr != '\n')) {
sx_report(SX_ERROR, "some problem invalidating asn"
" %s\n", q);
errno = 0;
asn = strtoul(q + 4, &eptr, 10);
if (*eptr != '\n') {
sx_report(SX_ERROR, "not a number: %s\n", q);
return;
}
asn1 = asn % 65536;
asn0 = asn / 65536;
if (!b->asn32s[asn0] ||
!(b->asn32s[asn0][asn1/8] & (0x80 >> (asn1 % 8)))) {
sx_report(SX_NOTICE, "strange, invalidating inactive "
"asn %lu(%s)\n", asn, q);
} else {
b->asn32s[asn0][asn1 / 8] &= ~(0x80 >> (asn1 % 8));
if (errno == ERANGE && asn == ULONG_MAX) {
sx_report(SX_ERROR, "overflow: %s\n", q);
return;
}
if (asn == 0 || asn >= 4294967295) {
sx_report(SX_ERROR, "out of range: %s\n", q);
return;
}
if (eptr && *eptr != '\n') {
sx_report(SX_ERROR, "no number? %s\n", q);
return;
}
find.asn = asn;
if ((res = RB_FIND(asn_tree, &b->asnlist, &find)) == NULL) {
RB_REMOVE(asn_tree, &b->asnlist, res);
free(res);
}
}
}
@@ -474,7 +467,7 @@ static void
bgpq_write(struct bgpq_expander *b)
{
while(!STAILQ_EMPTY(&b->wq)) {
struct bgpq_request *req = STAILQ_FIRST(&b->wq);
struct request *req = STAILQ_FIRST(&b->wq);
int ret = write(b->fd, req->request + req->offset,
req->size-req->offset);
@@ -500,8 +493,8 @@ bgpq_write(struct bgpq_expander *b)
static int
bgpq_selread(struct bgpq_expander *b, char *buffer, int size)
{
fd_set rfd, wfd;
int ret;
fd_set rfd, wfd;
int ret;
repeat:
FD_ZERO(&rfd);
@@ -542,7 +535,7 @@ bgpq_read(struct bgpq_expander *b)
while(!STAILQ_EMPTY(&b->rq)) {
int ret = 0;
char *cres;
struct bgpq_request *req = STAILQ_FIRST(&b->rq);
struct request *req = STAILQ_FIRST(&b->rq);
SX_DEBUG(debug_expander > 2, "waiting for answer to %s,"
"init %i '%.*s'\n", req->request, off, off, response);
@@ -602,7 +595,7 @@ have:
/* response is not yet fully buffered */
memcpy(recvbuffer, eon + 1,
off - ((eon + 1) - response));
offset = off - ((eon+1) - response);
offset = off - ((eon + 1) - response);
memset(response, 0, sizeof(response));
off = 0;
}
@@ -674,7 +667,7 @@ have3:
if (c[0] == 0)
break;
req->callback(c, b, req);
c += spn+1;
c += spn + 1;
}
assert(c == recvbuffer + togot);
memset(recvbuffer, 0, togot + 2);
@@ -704,7 +697,7 @@ have3:
}
memmove(response, cres + 1, off - ((cres + 1) - response));
off -= (cres+1) - response;
off -= (cres + 1) - response;
memset(response + off, 0, sizeof(response) - off);
SX_DEBUG(debug_expander > 5,
"fixed response of %i, %.*s\n", off, off, response);
@@ -712,7 +705,7 @@ have3:
STAILQ_REMOVE_HEAD(&b->rq, next);
b->piped--;
bgpq_request_free(req);
request_free(req);
}
return 0;
@@ -720,20 +713,20 @@ have3:
int
bgpq_expand_irrd(struct bgpq_expander *b,
int (*callback)(char *, struct bgpq_expander *, struct bgpq_request *),
int (*callback)(char *, struct bgpq_expander *, struct request *),
void *udata, char *fmt, ...)
{
char request[128], response[128];
va_list ap;
ssize_t ret;
int off = 0;
struct bgpq_request *req;
struct request *req;
va_start(ap, fmt);
vsnprintf(request, sizeof(request), fmt, ap);
va_end(ap);
req = bgpq_request_alloc(request, callback, udata);
req = request_alloc(request, callback, udata);
SX_DEBUG(debug_expander, "expander sending: %s", request);
@@ -763,7 +756,7 @@ repeat:
if (response[0] == 'A') {
char *eon, *c;
long togot = strtoul(response+1, &eon, 10);
long togot = strtoul(response + 1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
int offset = 0;
@@ -859,7 +852,7 @@ have3:
sx_report(SX_ERROR,"Wrong reply: %s\n", response);
exit(0);
}
bgpq_request_free(req);
request_free(req);
return 0;
}
@@ -868,9 +861,10 @@ int
bgpq_expand(struct bgpq_expander *b)
{
int fd = -1, err, ret, aquery = 0;
struct sx_slentry *mc;
struct slentry *mc;
struct addrinfo hints, *res = NULL, *rp;
struct linger sl;
struct asn_entry *asne;
sl.l_onoff = 1;
sl.l_linger = 5;
@@ -1023,7 +1017,7 @@ bgpq_expand(struct bgpq_expander *b)
if (pipelining)
fcntl(fd, F_SETFL, O_NONBLOCK|(fcntl(fd, F_GETFL)));
STAILQ_FOREACH(mc, &b->macroses, entries) {
STAILQ_FOREACH(mc, &b->macroses, entry) {
if (!b->maxdepth && RB_EMPTY(&b->stoplist)) {
if (aquery)
bgpq_expand_irrd(b, bgpq_expanded_prefix, b,
@@ -1052,8 +1046,7 @@ bgpq_expand(struct bgpq_expander *b)
}
if (b->generation >= T_PREFIXLIST || b->validate_asns) {
uint32_t i, j, k;
STAILQ_FOREACH(mc, &b->rsets, entries) {
STAILQ_FOREACH(mc, &b->rsets, entry) {
if (b->family == AF_INET)
bgpq_expand_irrd(b, bgpq_expanded_prefix,
NULL, "!i%s,1\n", mc->text);
@@ -1061,33 +1054,27 @@ bgpq_expand(struct bgpq_expander *b)
bgpq_expand_irrd(b, bgpq_expanded_v6prefix,
NULL, "!i%s,1\n", mc->text);
}
for (k=0; k < sizeof(b->asn32s) / sizeof(unsigned char *); k++) {
if (!b->asn32s[k])
continue;
for (i=0; i<8192; i++) {
for (j=0; j<8; j++) {
if (b->asn32s[k][i] & (0x80 >> j)) {
if (b->family == AF_INET6) {
if (!pipelining) {
bgpq_expand_irrd(b, bgpq_expanded_v6prefix,
NULL, "!6as%" PRIu32 "\n", ( k << 16) + i * 8 + j);
} else {
bgpq_pipeline(b, bgpq_expanded_v6prefix,
NULL, "!6as%" PRIu32 "\n", (k << 16) + i * 8 + j);
}
} else {
if (!pipelining) {
bgpq_expand_irrd(b, bgpq_expanded_prefix,
NULL, "!gas%" PRIu32 "\n", (k << 16) + i * 8 + j);
} else {
bgpq_pipeline(b, bgpq_expanded_prefix,
NULL, "!gas%" PRIu32 "\n", ( k<< 16) + i* 8 + j);
}
}
}
RB_FOREACH(asne, asn_tree, &b->asnlist) {
if (b->family == AF_INET6) {
if (!pipelining) {
bgpq_expand_irrd(b, bgpq_expanded_v6prefix,
NULL, "!6as%" PRIu32 "\n", asne->asn);
} else {
bgpq_pipeline(b, bgpq_expanded_v6prefix,
NULL, "!6as%" PRIu32 "\n", asne->asn);
}
} else {
if (!pipelining) {
bgpq_expand_irrd(b, bgpq_expanded_prefix,
NULL, "!gas%" PRIu32 "\n", asne->asn);
} else {
bgpq_pipeline(b, bgpq_expanded_prefix,
NULL, "!gas%" PRIu32 "\n", asne->asn);
}
}
}
if (pipelining) {
if (!STAILQ_EMPTY(&b->wq))
bgpq_write(b);
@@ -1147,17 +1134,18 @@ bgpq_prequest_freeall(struct bgpq_prequest *bpr) {
void
expander_freeall(struct bgpq_expander *expander) {
struct sx_tentry *var, *nxt;
struct asn_entry *asne, *asne_next;
while (!STAILQ_EMPTY(&expander->macroses)) {
struct sx_slentry *n1 = STAILQ_FIRST(&expander->macroses);
STAILQ_REMOVE_HEAD(&expander->macroses, entries);
struct slentry *n1 = STAILQ_FIRST(&expander->macroses);
STAILQ_REMOVE_HEAD(&expander->macroses, entry);
free(n1->text);
free(n1);
}
while (!STAILQ_EMPTY(&expander->rsets)) {
struct sx_slentry *n1 = STAILQ_FIRST(&expander->rsets);
STAILQ_REMOVE_HEAD(&expander->rsets, entries);
struct slentry *n1 = STAILQ_FIRST(&expander->rsets);
STAILQ_REMOVE_HEAD(&expander->rsets, entry);
free(n1->text);
free(n1);
}
@@ -1176,10 +1164,11 @@ expander_freeall(struct bgpq_expander *expander) {
free(var);
}
for (int i = 0; i < 65536; i++) {
if (expander->asn32s[i] != NULL) {
free(expander->asn32s[i]);
}
for (asne = RB_MIN(asn_tree, &expander->asnlist);
asne != NULL; asne = asne_next) {
asne_next = RB_NEXT(asn_tree, &expander->asnlist, asne);
RB_REMOVE(asn_tree, &expander->asnlist, asne);
free(asne);
}
sx_radix_tree_freeall(expander->tree);

105
extern.h
View File

@@ -29,19 +29,24 @@
#include "sx_prefix.h"
struct sx_slentry {
STAILQ_ENTRY(sx_slentry) entries;
char* text;
struct slentry {
STAILQ_ENTRY(slentry) entry;
char *text;
};
struct sx_slentry* sx_slentry_new(char* text);
struct slentry *sx_slentry_new(char *text);
struct sx_tentry {
RB_ENTRY(sx_tentry) entries;
char* text;
RB_ENTRY(sx_tentry) entry;
char *text;
};
struct sx_tentry* sx_tentry_new(char* text);
struct sx_tentry *sx_tentry_new(char *text);
struct asn_entry {
RB_ENTRY(asn_entry) entry;
uint32_t asn;
};
typedef enum {
V_CISCO = 0,
@@ -53,6 +58,7 @@ typedef enum {
V_FORMAT,
V_NOKIA,
V_HUAWEI,
V_HUAWEI_XPL,
V_MIKROTIK,
V_NOKIA_MD,
V_ARISTA
@@ -62,6 +68,7 @@ typedef enum {
T_NONE = 0,
T_ASPATH,
T_OASPATH,
T_ASLIST,
T_ASSET,
T_PREFIXLIST,
T_EACL,
@@ -70,44 +77,47 @@ typedef enum {
struct bgpq_expander;
struct bgpq_request {
STAILQ_ENTRY(bgpq_request) next;
char *request;
int size, offset;
void *udata;
unsigned int depth;
int (*callback)(char *, struct bgpq_expander *,
struct bgpq_request *);
struct request {
STAILQ_ENTRY(request) next;
char *request;
int size, offset;
void *udata;
unsigned int depth;
int (*callback)(char *, struct bgpq_expander *,
struct request *);
};
struct bgpq_expander {
struct sx_radix_tree *tree;
STAILQ_HEAD(sx_slentries, sx_slentry) macroses, rsets;
RB_HEAD(tentree, sx_tentry) already, stoplist;
int family;
char *sources;
uint32_t asnumber;
int aswidth;
char *name;
bgpq_vendor_t vendor;
bgpq_gen_t generation;
int identify;
int sequence;
unsigned int maxdepth;
unsigned int cdepth;
int validate_asns;
unsigned char *asn32s[65536];
struct bgpq_prequest *firstpipe, *lastpipe;
int piped;
char *match;
char *server;
char *port;
char *format;
unsigned int maxlen;
STAILQ_HEAD(bgpq_requests, bgpq_request) wq, rq;
int fd;
struct sx_radix_tree *tree;
int family;
char *sources;
uint32_t asnumber;
int aswidth;
char *name;
bgpq_vendor_t vendor;
bgpq_gen_t generation;
int identify;
int sequence;
unsigned int maxdepth;
unsigned int cdepth;
int validate_asns;
struct bgpq_prequest *firstpipe, *lastpipe;
int piped;
char *match;
char *server;
char *port;
char *format;
unsigned int maxlen;
int fd;
RB_HEAD(asn_tree, asn_entry) asnlist;
STAILQ_HEAD(requests, request) wq, rq;
STAILQ_HEAD(slentries, slentry) macroses, rsets;
RB_HEAD(tentree, sx_tentry) already, stoplist;
};
int asn_cmp(struct asn_entry *, struct asn_entry *);
RB_PROTOTYPE(asn_tree, asn_entry, entry, asn_cmp);
int bgpq_expander_init(struct bgpq_expander *b, int af);
int bgpq_expander_add_asset(struct bgpq_expander *b, char *set);
int bgpq_expander_add_rset(struct bgpq_expander *b, char *set);
@@ -118,12 +128,13 @@ int bgpq_expander_add_stop(struct bgpq_expander *b, char *object);
int bgpq_expand(struct bgpq_expander *b);
int bgpq4_print_prefixlist(FILE *f, struct bgpq_expander *b);
int bgpq4_print_eacl(FILE *f, struct bgpq_expander *b);
int bgpq4_print_aspath(FILE *f, struct bgpq_expander *b);
int bgpq4_print_asset(FILE *f, struct bgpq_expander *b);
int bgpq4_print_oaspath(FILE *f, struct bgpq_expander *b);
int bgpq4_print_route_filter_list(FILE *f, struct bgpq_expander *b);
void bgpq4_print_prefixlist(FILE *f, struct bgpq_expander *b);
void bgpq4_print_eacl(FILE *f, struct bgpq_expander *b);
void bgpq4_print_aspath(FILE *f, struct bgpq_expander *b);
void bgpq4_print_asset(FILE *f, struct bgpq_expander *b);
void bgpq4_print_oaspath(FILE *f, struct bgpq_expander *b);
void bgpq4_print_aslist(FILE *f, struct bgpq_expander *b);
void bgpq4_print_route_filter_list(FILE *f, struct bgpq_expander *b);
void sx_radix_node_freeall(struct sx_radix_node *n);
void sx_radix_tree_freeall(struct sx_radix_tree *t);
@@ -134,5 +145,5 @@ void expander_freeall(struct bgpq_expander *expander);
int sx_maxsockbuf(int s, int dir);
#ifndef HAVE_STRLCPY
size_t strlcpy(char* dst, const char* src, size_t size);
size_t strlcpy(char *dst, const char *src, size_t size);
#endif

461
main.c
View File

@@ -31,8 +31,6 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdio.h>
@@ -52,13 +50,14 @@ extern int expand_special_asn;
static int
usage(int ecode)
{
printf("\nUsage: bgpq4 [-h host[:port]] [-S sources] [-E|G <num>"
printf("\nUsage: bgpq4 [-h host[:port]] [-S sources] [-E|G|H <num>"
"|f <num>|t] [-46ABbdJjKNnwXz] [-R len] <OBJECTS> ... "
"[EXCEPT <OBJECTS> ...]\n");
printf("\nVendor targets:\n");
printf(" no option : Cisco IOS Classic (default)\n");
printf(" -X : Cisco IOS XR\n");
printf(" -U : Huawei\n");
printf(" -u : Huawei XPL\n");
printf(" -j : JSON\n");
printf(" -J : Juniper Junos\n");
printf(" -K : MikroTik RouterOS\n");
@@ -88,6 +87,7 @@ usage(int ecode)
"(OpenBGPD)\n");
printf(" -f number : generate input as-path access-list\n");
printf(" -G number : generate output as-path access-list\n");
printf(" -H number : generate origin as-lists (JunOS only)\n");
printf(" -M match : extra match conditions for JunOS route-filters\n");
printf(" -l name : use specified name for generated access/prefix/.."
" list\n");
@@ -97,7 +97,7 @@ usage(int ecode)
printf(" -t : generate as-sets for OpenBGPD (OpenBGPD 6.4+), BIRD "
"and JSON formats\n");
printf(" -z : generate route-filter-list (Junos only)\n");
printf(" -W len : specify max-entries on as-path line (use 0 for "
printf(" -W len : specify max-entries on as-path/as-list line (use 0 for "
"infinity)\n");
printf("\nUtility operations:\n");
@@ -124,8 +124,8 @@ version(void)
static void
exclusive(void)
{
fprintf(stderr,"-E, -f <asnum>, -G <asnum>, and -t are mutually "
"exclusive\n");
fprintf(stderr,"-E, -F, -K , -f <asnum>, -G <asnum>, and -t are mutually"
" exclusive\n");
exit(1);
}
@@ -134,8 +134,8 @@ vendor_exclusive(void)
{
fprintf(stderr, "-b (BIRD), -B (OpenBGPD), -F (formatted), -J (Junos),"
" -j (JSON), -N (Nokia SR OS Classic), -n (Nokia SR OS MD-CLI),"
" -U (Huawei), -e (Arista) and -X (IOS XR) options are mutually"
" exclusive\n");
" -U (Huawei), -u (Huawei XPL), -e (Arista) and -X (IOS XR) options "
" are mutually exclusive\n");
exit(1);
}
@@ -197,154 +197,154 @@ main(int argc, char* argv[])
expander.sources=getenv("IRRD_SOURCES");
while ((c = getopt(argc, argv,
"346a:AbBdDEeF:S:jJKf:l:L:m:M:NnW:pr:R:G:tTh:UwXsvz"))
!=EOF) {
"46a:AbBdDEeF:S:jJKf:l:L:m:M:NnW:pr:R:G:H:tTh:UuwXsvz")) != EOF) {
switch (c) {
case '3':
/*
* No-op, left for backwards compatibility with bgpq3
*/
break;
case '4':
/* do nothing, expander already configured for IPv4 */
if (expander.family == AF_INET6) {
sx_report(SX_FATAL, "-4 and -6 are mutually "
"exclusive\n");
exit(1);
case '4':
/* do nothing, expander already configured for IPv4 */
if (expander.family == AF_INET6) {
sx_report(SX_FATAL, "-4 and -6 are mutually "
"exclusive\n");
exit(1);
}
selectedipv4 = 1;
break;
case '6':
if (selectedipv4) {
sx_report(SX_FATAL, "-4 and -6 are mutually "
"exclusive\n");
exit(1);
}
af = AF_INET6;
expander.family = AF_INET6;
expander.tree->family = AF_INET6;
break;
case 'a':
parseasnumber(&expander, optarg);
break;
case 'A':
if (aggregate)
debug_aggregation++;
aggregate = 1;
break;
case 'b':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_BIRD;
break;
case 'B':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_OPENBGPD;
break;
case 'd':
debug_expander++;
break;
case 'E':
if (expander.generation)
exclusive();
expander.generation = T_EACL;
break;
case 'e':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_ARISTA;
expander.sequence = 1;
break;
case 'F':
if (expander.vendor)
exclusive();
expander.vendor = V_FORMAT;
expander.format = optarg;
break;
case 'f':
if (expander.generation)
exclusive();
expander.generation = T_ASPATH;
parseasnumber(&expander, optarg);
break;
case 'G':
if (expander.generation)
exclusive();
expander.generation = T_OASPATH;
parseasnumber(&expander, optarg);
break;
case 'H':
if (expander.generation)
exclusive();
expander.generation = T_ASLIST;
parseasnumber(&expander, optarg);
break;
case 'h':
{
char *d = strchr(optarg, ':');
expander.server = optarg;
if (d) {
*d = 0;
expander.port = d + 1;
}
selectedipv4=1;
break;
case '6':
if (selectedipv4) {
sx_report(SX_FATAL, "-4 and -6 are mutually "
"exclusive\n");
exit(1);
}
af = AF_INET6;
expander.family = AF_INET6;
expander.tree->family = AF_INET6;
break;
case 'a':
parseasnumber(&expander, optarg);
break;
case 'A':
if (aggregate)
debug_aggregation++;
aggregate = 1;
break;
case 'b':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_BIRD;
break;
case 'B':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_OPENBGPD;
break;
case 'd':
debug_expander++;
break;
case 'E':
if (expander.generation)
exclusive();
expander.generation = T_EACL;
break;
case 'e':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_ARISTA;
expander.sequence = 1;
break;
case 'F':
if (expander.vendor)
exclusive();
expander.vendor = V_FORMAT;
expander.format = optarg;
break;
case 'f':
if (expander.generation)
exclusive();
expander.generation = T_ASPATH;
parseasnumber(&expander, optarg);
break;
case 'G':
if (expander.generation)
exclusive();
expander.generation = T_OASPATH;
parseasnumber(&expander, optarg);
break;
case 'h':
{
char* d = strchr(optarg, ':');
expander.server = optarg;
if (d) {
*d = 0;
expander.port = d + 1;
}
}
break;
case 'J':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_JUNIPER;
break;
case 'j':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_JSON;
break;
case 'K':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_MIKROTIK;
break;
case 'p':
expand_special_asn = 1;
break;
case 'r':
refineLow = strtoul(optarg, NULL, 10);
if (!refineLow) {
sx_report(SX_FATAL, "Invalid refineLow value:"
" %s\n", optarg);
exit(1);
}
break;
case 'R':
refine = strtoul(optarg, NULL, 10);
if (!refine) {
sx_report(SX_FATAL,"Invalid refine length:"
" %s\n", optarg);
exit(1);
}
break;
case 'l':
expander.name = optarg;
break;
case 'L':
expander.maxdepth = strtol(optarg, NULL, 10);
if (expander.maxdepth < 1) {
sx_report(SX_FATAL, "Invalid maximum recursion"
" (-L): %s\n", optarg);
exit(1);
}
break;
case 'm':
maxlen=strtoul(optarg, NULL, 10);
if (!maxlen) {
sx_report(SX_FATAL, "Invalid maxlen (-m): %s\n",
optarg);
exit(1);
}
break;
case 'M':
{
char *mc, *md;
}
break;
case 'J':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_JUNIPER;
break;
case 'j':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_JSON;
break;
case 'K':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_MIKROTIK;
break;
case 'p':
expand_special_asn = 1;
break;
case 'r':
refineLow = strtoul(optarg, NULL, 10);
if (!refineLow) {
sx_report(SX_FATAL, "Invalid refineLow value:"
" %s\n", optarg);
exit(1);
}
break;
case 'R':
refine = strtoul(optarg, NULL, 10);
if (!refine) {
sx_report(SX_FATAL,"Invalid refine length:"
" %s\n", optarg);
exit(1);
}
break;
case 'l':
expander.name = optarg;
break;
case 'L':
expander.maxdepth = strtol(optarg, NULL, 10);
if (expander.maxdepth < 1) {
sx_report(SX_FATAL, "Invalid maximum recursion"
" (-L): %s\n", optarg);
exit(1);
}
break;
case 'm':
maxlen=strtoul(optarg, NULL, 10);
if (!maxlen) {
sx_report(SX_FATAL, "Invalid maxlen (-m): %s\n",
optarg);
exit(1);
}
break;
case 'M':
{
char *mc, *md;
expander.match = strdup(optarg);
mc = md = expander.match;
while (*mc) {
if (*mc == '\\') {
if (*(mc+1) == '\n') {
if (*(mc + 1) == '\n') {
*md = '\n';
md++;
mc += 2;
@@ -377,64 +377,68 @@ main(int argc, char* argv[])
}
}
*md = 0;
}
break;
case 'N':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_NOKIA;
break;
case 'n':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_NOKIA_MD;
break;
case 't':
if (expander.generation)
exclusive();
expander.generation = T_ASSET;
break;
case 'T':
pipelining = 0;
break;
case 's':
expander.sequence = 1;
break;
case 'S':
expander.sources = optarg;
break;
case 'U':
if (expander.vendor)
exclusive();
expander.vendor = V_HUAWEI;
break;
case 'W':
expander.aswidth = atoi(optarg);
if (expander.aswidth < 0) {
sx_report(SX_FATAL,"Invalid as-width: %s\n",
optarg);
exit(1);
}
widthSet = 1;
break;
case 'w':
expander.validate_asns = 1;
break;
case 'X':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_CISCO_XR;
break;
case 'v':
version();
break;
case 'z':
if (expander.generation)
exclusive();
expander.generation = T_ROUTE_FILTER_LIST;
break;
default:
usage(1);
}
break;
case 'N':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_NOKIA;
break;
case 'n':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_NOKIA_MD;
break;
case 't':
if (expander.generation)
exclusive();
expander.generation = T_ASSET;
break;
case 'T':
pipelining = 0;
break;
case 's':
expander.sequence = 1;
break;
case 'S':
expander.sources = optarg;
break;
case 'U':
if (expander.vendor)
exclusive();
expander.vendor = V_HUAWEI;
break;
case 'u':
if (expander.vendor)
exclusive();
expander.vendor = V_HUAWEI_XPL;
break;
case 'W':
expander.aswidth = atoi(optarg);
if (expander.aswidth < 0) {
sx_report(SX_FATAL,"Invalid as-width: %s\n", optarg);
exit(1);
}
widthSet = 1;
break;
case 'w':
expander.validate_asns = 1;
break;
case 'X':
if (expander.vendor)
vendor_exclusive();
expander.vendor = V_CISCO_XR;
break;
case 'v':
version();
break;
case 'z':
if (expander.generation)
exclusive();
expander.generation = T_ROUTE_FILTER_LIST;
break;
default:
usage(1);
}
}
@@ -449,14 +453,18 @@ main(int argc, char* argv[])
case V_CISCO:
case V_MIKROTIK:
expander.aswidth = 4;
break;
case V_CISCO_XR:
expander.aswidth = 6;
break;
case V_JUNIPER:
case V_NOKIA:
case V_NOKIA_MD:
expander.aswidth = 8;
break;
case V_BIRD:
expander.aswidth = 10;
break;
}
} else if (expander.generation == T_OASPATH) {
int vendor = expander.vendor;
@@ -464,12 +472,22 @@ main(int argc, char* argv[])
case V_ARISTA:
case V_CISCO:
expander.aswidth = 5;
break;
case V_CISCO_XR:
expander.aswidth = 7;
break;
case V_JUNIPER:
case V_NOKIA:
case V_NOKIA_MD:
expander.aswidth = 8;
break;
}
} else if (expander.generation == T_ASLIST) {
int vendor = expander.vendor;
switch (vendor) {
case V_JUNIPER:
expander.aswidth = 8;
break;
}
}
}
@@ -656,14 +674,18 @@ main(int argc, char* argv[])
"only with Juniper route-filters\n");
}
if ((expander.generation == T_ASPATH || expander.generation == T_OASPATH)
if ((expander.generation == T_ASPATH
|| expander.generation == T_OASPATH
|| expander.generation == T_ASLIST)
&& af != AF_INET && !expander.validate_asns) {
sx_report(SX_FATAL, "Sorry, -6 makes no sense with as-path (-f/-G) "
sx_report(SX_FATAL, "Sorry, -6 makes no sense with as-path (-f/-G) or as-list (-H) "
"generation\n");
}
if (expander.validate_asns && expander.generation != T_ASPATH
&& expander.generation != T_OASPATH) {
if (expander.validate_asns
&& expander.generation != T_ASPATH
&& expander.generation != T_OASPATH
&& expander.generation != T_ASLIST) {
sx_report(SX_FATAL, "Sorry, -w makes sense only for as-path "
"(-f/-G) generation\n");
}
@@ -726,22 +748,25 @@ main(int argc, char* argv[])
switch (expander.generation) {
case T_NONE:
sx_report(SX_FATAL,"Unreachable point... call snar\n");
sx_report(SX_FATAL,"Unreachable point");
exit(1);
case T_ASPATH:
bgpq4_print_aspath(stdout,&expander);
bgpq4_print_aspath(stdout, &expander);
break;
case T_OASPATH:
bgpq4_print_oaspath(stdout,&expander);
bgpq4_print_oaspath(stdout, &expander);
break;
case T_ASLIST:
bgpq4_print_aslist(stdout, &expander);
break;
case T_ASSET:
bgpq4_print_asset(stdout,&expander);
bgpq4_print_asset(stdout, &expander);
break;
case T_PREFIXLIST:
bgpq4_print_prefixlist(stdout,&expander);
bgpq4_print_prefixlist(stdout, &expander);
break;
case T_EACL:
bgpq4_print_eacl(stdout,&expander);
bgpq4_print_eacl(stdout, &expander);
break;
case T_ROUTE_FILTER_LIST:
bgpq4_print_route_filter_list(stdout, &expander);

1508
printer.c

File diff suppressed because it is too large Load Diff

View File

@@ -39,19 +39,15 @@
#include "extern.h"
#include "sx_report.h"
#ifndef SX_MAXSOCKBUF_MAX
#define SX_MAXSOCKBUF_MAX (2 * 1024 * 1024)
#endif
int
sx_maxsockbuf(int s, int dir)
{
{
int optval = 0, voptval;
int hiconf = -1, loconf = -1;
unsigned int voptlen;
int phase = 0, iterations = 0;
if (s < 0) {
if (s < 0) {
sx_report(SX_FATAL,"Unable to maximize sockbuf on invalid "
"socket %i\n", s);
exit(1);
@@ -65,30 +61,27 @@ sx_maxsockbuf(int s, int dir)
return -1;
}
for (;;) {
for (;;) {
iterations++;
if (phase == 0)
optval<<=1;
else {
optval <<= 1;
else {
if (optval == (hiconf + loconf) / 2)
break;
optval = (hiconf + loconf) / 2;
}
if (optval > SX_MAXSOCKBUF_MAX && phase == 0)
break;
if (setsockopt(s, SOL_SOCKET, dir, (void*)&optval,
sizeof(optval)) == -1) {
if (phase == 0)
phase = 1;
phase = 1;
hiconf = optval;
hiconf = optval;
continue;
} else {
} else {
loconf = optval;
}
@@ -99,39 +92,26 @@ sx_maxsockbuf(int s, int dir)
sx_report(SX_ERROR,"getsockopt failed: %s\n",
strerror(errno));
return -1;
} else if (voptval < optval) {
if (phase == 0) {
} else if (voptval < optval) {
if (phase == 0) {
phase = 1;
optval >>= 1;
continue;
} else if (phase == 1) {
} else if (phase == 1) {
phase = 2;
optval -= 2048;
continue;
} else
break;
} else if (voptval >= SX_MAXSOCKBUF_MAX) {
/*
* ... and getsockopt not failed and voptval>=optval.
* Do not allow to increase sockbuf too much even in
* case OS permits it
*/
break;
}
}
voptlen = sizeof(voptval);
if (getsockopt(s, SOL_SOCKET, dir, (void*)&voptval,
&voptlen) == -1) {
sx_report(SX_ERROR,"getsockopt(final stage) failed: %s\n",
sx_report(SX_ERROR,"getsockopt(final stage) failed: %s\n",
strerror(errno));
return -1;
} else {
/*
printf("Finally got %i bytes of recvspace in %i interations\n",
voptval, iterations);
*/
}
return voptval;
} else
return voptval;
}

View File

@@ -133,7 +133,7 @@ sx_prefix_parse(struct sx_prefix *p, int af, char *text)
{
char *c = NULL;
int masklen, ret;
char mtext[INET6_ADDRSTRLEN+5];
char mtext[INET6_ADDRSTRLEN + 5];
strlcpy(mtext, text, sizeof(mtext));
@@ -142,7 +142,7 @@ sx_prefix_parse(struct sx_prefix *p, int af, char *text)
if (c) {
char* eod;
*c = 0;
masklen = strtol(c+1, &eod, 10);
masklen = strtol(c + 1, &eod, 10);
if (eod && eod[0] && !isspace(eod[0])) {
*c = '/';
sx_report(SX_ERROR, "Invalid masklen in prefix %s\n",
@@ -297,7 +297,7 @@ sx_prefix_range_parse(struct sx_radix_tree *tree, int af, unsigned int maxlen,
if (!sx_prefix_parse(p, 0, text)) {
sx_report(SX_ERROR, "Unable to parse prefix %s^%s\n", text,
d+1);
d + 1);
return 0;
}
@@ -323,7 +323,7 @@ sx_prefix_range_parse(struct sx_radix_tree *tree, int af, unsigned int maxlen,
max = maxlen;
} else if (isdigit(d[1])) {
char *dm = NULL;
min = strtoul(d+1, &dm, 10);
min = strtoul(d + 1, &dm, 10);
if (dm && *dm == '-' && isdigit(dm[1])) {
max = strtoul(dm + 1, NULL, 10);
} else if (dm && *dm) {
@@ -483,7 +483,7 @@ sx_prefix_snprintf_fmt(struct sx_prefix *p, FILE *f,
}
c += 2;
} else if (*c == '\\') {
switch(*(c+1)) {
switch(*(c + 1)) {
case 'n':
fprintf(f, "\n");
break;
@@ -925,7 +925,7 @@ sx_radix_node_aggregate(struct sx_radix_node *node)
if (!node->r->isAggregate && !node->l->isAggregate
&& !node->r->isGlue && !node->l->isGlue
&& node->r->prefix->masklen == node->l->prefix->masklen) {
if (node->r->prefix->masklen == node->prefix->masklen+1) {
if (node->r->prefix->masklen == node->prefix->masklen + 1) {
node->isAggregate = 1;
node->r->isGlue = 1;
node->l->isGlue = 1;
@@ -1019,7 +1019,7 @@ sx_radix_node_aggregate(struct sx_radix_node *node)
&& node->l->aggregateHi == node->r->son->aggregateHi
&& node->l->aggregateLow == node->r->son->aggregateLow) {
if (node->l->prefix->masklen == node->prefix->masklen + 1
&& node->r->prefix->masklen == node->prefix->masklen+1) {
&& node->r->prefix->masklen == node->prefix->masklen + 1) {
if (node->isGlue) {
node->l->isGlue = 1;
node->r->son->isGlue = 1;

View File

@@ -31,15 +31,15 @@
#include "extern.h"
struct sx_slentry *
struct slentry *
sx_slentry_new(char *t)
{
struct sx_slentry *e = malloc(sizeof(struct sx_slentry));
struct slentry *e = malloc(sizeof(struct slentry));
if (!e)
return NULL;
memset(e, 0, sizeof(struct sx_slentry));
memset(e, 0, sizeof(struct slentry));
e->text = strdup(t);