History log of /openbsd-current/usr.sbin/bgpd/mrt.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.117 22-May-2024 claudio

Convert bgpid, remote_bgpid and clusterid to host byte order.

Before the RDE used host byte order for remote_bgpid but all the other
code used network byte order. The reason for that was that bgpid was
initially an IPv4 address but since RFC 6286 in 2011 this is much more
relaxed and so it makes more sense to just treat them as numbers and
so host byte order.

OK tb@


Revision tags: OPENBSD_7_4_BASE OPENBSD_7_5_BASE
# 1.116 14-Jul-2023 claudio

Cleanup mrt message handling. Remove the DUMP_XYZ() macros and replace
them with direct calls to for example ibuf_add_n16(). Further cleanup
the error handling and use goto fail in most places. Remove many of the
error messages and combine all the possible ibuf errors in one place.
For this remove most warnings from internal functions (also mark all
internal helper functions with static to make that more obvious).
There are still some cases where an error will result in to warnings but
those errors are unreachable in normal operations.
OK tb@


# 1.115 12-Jul-2023 claudio

Update OpenBGPD to use new ibuf API.

This replaces the old way of using a static buffer and a len to build
UPDATEs with a pure ibuf solution. The result is much cleaner and a lot
of almost duplicate code can be removed because often a version for ibufs
and one for this static buffer was implemented (e.g. for mrt or bgpctl).
With and OK tb@


# 1.114 19-Apr-2023 claudio

Add minimal support for flowspec in the table-mp MRT format.
In general people should use table-v2 which handles flowspec just fine.
OK tb@


# 1.113 28-Mar-2023 claudio

Instead of exracting the prefix into a bgpd_addr and passing that to
prefix_write() rename prefix_write() to pt_write() and pass a pt_entry to
the function. Removes an extra conversion step.
OK tb@


# 1.112 28-Mar-2023 claudio

Refactor mrt_dump_entry_v2() to use a dynamic buffer to hold the prefix.
Especially flowspec "prefixes" are variable size and so using a static
buffer there is not going to work in the long run.
OK tb@


Revision tags: OPENBSD_7_3_BASE
# 1.111 28-Dec-2022 jmc

spelling fixes; from paul tagliamonte
any parts of his diff not taken are noted on tech


Revision tags: OPENBSD_7_2_BASE
# 1.110 01-Sep-2022 claudio

Switch the rde_peer hashtable and peer list to a single RB tree.
Only the RDE used a hashtable for lookups while the session engine
switched from a list to RB tree some time ago.
Use peer_foreach() in the mrt code instead of passing the peer list
as an argument.
OK benno@ tb@


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.116 14-Jul-2023 claudio

Cleanup mrt message handling. Remove the DUMP_XYZ() macros and replace
them with direct calls to for example ibuf_add_n16(). Further cleanup
the error handling and use goto fail in most places. Remove many of the
error messages and combine all the possible ibuf errors in one place.
For this remove most warnings from internal functions (also mark all
internal helper functions with static to make that more obvious).
There are still some cases where an error will result in to warnings but
those errors are unreachable in normal operations.
OK tb@


# 1.115 12-Jul-2023 claudio

Update OpenBGPD to use new ibuf API.

This replaces the old way of using a static buffer and a len to build
UPDATEs with a pure ibuf solution. The result is much cleaner and a lot
of almost duplicate code can be removed because often a version for ibufs
and one for this static buffer was implemented (e.g. for mrt or bgpctl).
With and OK tb@


# 1.114 19-Apr-2023 claudio

Add minimal support for flowspec in the table-mp MRT format.
In general people should use table-v2 which handles flowspec just fine.
OK tb@


# 1.113 28-Mar-2023 claudio

Instead of exracting the prefix into a bgpd_addr and passing that to
prefix_write() rename prefix_write() to pt_write() and pass a pt_entry to
the function. Removes an extra conversion step.
OK tb@


# 1.112 28-Mar-2023 claudio

Refactor mrt_dump_entry_v2() to use a dynamic buffer to hold the prefix.
Especially flowspec "prefixes" are variable size and so using a static
buffer there is not going to work in the long run.
OK tb@


Revision tags: OPENBSD_7_3_BASE
# 1.111 28-Dec-2022 jmc

spelling fixes; from paul tagliamonte
any parts of his diff not taken are noted on tech


Revision tags: OPENBSD_7_2_BASE
# 1.110 01-Sep-2022 claudio

Switch the rde_peer hashtable and peer list to a single RB tree.
Only the RDE used a hashtable for lookups while the session engine
switched from a list to RB tree some time ago.
Use peer_foreach() in the mrt code instead of passing the peer list
as an argument.
OK benno@ tb@


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.114 19-Apr-2023 claudio

Add minimal support for flowspec in the table-mp MRT format.
In general people should use table-v2 which handles flowspec just fine.
OK tb@


# 1.113 28-Mar-2023 claudio

Instead of exracting the prefix into a bgpd_addr and passing that to
prefix_write() rename prefix_write() to pt_write() and pass a pt_entry to
the function. Removes an extra conversion step.
OK tb@


# 1.112 28-Mar-2023 claudio

Refactor mrt_dump_entry_v2() to use a dynamic buffer to hold the prefix.
Especially flowspec "prefixes" are variable size and so using a static
buffer there is not going to work in the long run.
OK tb@


Revision tags: OPENBSD_7_3_BASE
# 1.111 28-Dec-2022 jmc

spelling fixes; from paul tagliamonte
any parts of his diff not taken are noted on tech


Revision tags: OPENBSD_7_2_BASE
# 1.110 01-Sep-2022 claudio

Switch the rde_peer hashtable and peer list to a single RB tree.
Only the RDE used a hashtable for lookups while the session engine
switched from a list to RB tree some time ago.
Use peer_foreach() in the mrt code instead of passing the peer list
as an argument.
OK benno@ tb@


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.111 28-Dec-2022 jmc

spelling fixes; from paul tagliamonte
any parts of his diff not taken are noted on tech


Revision tags: OPENBSD_7_2_BASE
# 1.110 01-Sep-2022 claudio

Switch the rde_peer hashtable and peer list to a single RB tree.
Only the RDE used a hashtable for lookups while the session engine
switched from a list to RB tree some time ago.
Use peer_foreach() in the mrt code instead of passing the peer list
as an argument.
OK benno@ tb@


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.110 01-Sep-2022 claudio

Switch the rde_peer hashtable and peer list to a single RB tree.
Only the RDE used a hashtable for lookups while the session engine
switched from a list to RB tree some time ago.
Use peer_foreach() in the mrt code instead of passing the peer list
as an argument.
OK benno@ tb@


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.109 17-Aug-2022 claudio

Convert bzero() to memset(), bcmp() to memcmp() and bcopy() to memcpy().

The memory regions passed to memcpy() can not overlap so no need for memmove().
OK tb@ deraadt@


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.108 28-Jul-2022 deraadt

whitespace found during a read-thru; ok claudio


Revision tags: OPENBSD_7_1_BASE
# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.107 22-Mar-2022 claudio

Switch from a LIST to TAILQ for the structure to store prefixes on a
rib_entry. Mostly mechanical, this simplifies prefix_insert() and
prefix_remove() since the redo queue can now just use TAILQ_INSERT_TAIL().
rde_softreconfig_sync_reeval() needs to use TAILQ_CONCAT() to move
the list of prefixes over to the local TAILQ_HEAD to reapply them later.
OK tb@


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.106 06-Feb-2022 claudio

Switch from u_intX_t types to stdint.h uintX_t. Mostly mechanical with
a few reindents.
OK florian@ tb@


Revision tags: OPENBSD_7_0_BASE
# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.105 03-Sep-2021 claudio

Add add-path support in MRT dumps (RFC8050).

This works for table-v2 formats and also for the all and update dumps.
For the latter the code needs to inspect the UPDATE message to guess if
add-path is used in that message or not. Add-path can be enabled per
AFI and alters the NLRI encoding without any way to detect.
For table dumps we decided to dump RIB_GENERIC entires the same way as
gobgp and not use the encoding from the RFC. The RFC specifies that
RIB_GENERIC (non-IPv4/v6) entries in a way that is not only different
but also against the normal codeflow. Only gobgp seems to implement
RIB_GENERIC encoding (neither bird nor frr/quagga do) and so it makes
sense to follow their way.

OK benno@


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.104 24-Jun-2021 claudio

aspath_deflate() did free the passed in data but since the way aspaths
are processed in the Adj-RIB-Out this is no longer needed since the passed
in pointer is still referenced and is not allowed to be freed.
Adjust the mrt code similar to how up_generate_attr() uses aspath_deflate().
OK sthen@


Revision tags: OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.103 09-Jan-2020 claudio

Convert the lastchange of a prefix from time(NULL) to getmonotime() which
uses CLOCK_MONOTONIC. Convert the control messages to return the relative
age of the prefix instead of the absolute age. Adjust the mrt dump code
to stil dump the route age in seconds since epoch as defined in the RFC.
With this all times in bgpd are now based on CLOCK_MONOTONIC.
OK denis@


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.102 08-Jan-2020 deraadt

eye burning whitespace


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.101 31-Dec-2019 claudio

In mrt_dump_hdr_rde() use clock_gettime(CLOCK_REALTIME, ) like it is done
in mrt_dump_hdr_se(). Table dumps don't need time.tv_nsec like used in
the _ET formats like for message and state changes but it keeps to code
similar.


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.100 08-Aug-2019 claudio

Use O_CLOEXEC or SOCK_CLOEXEC on every open or socket call. Even though
some sockets are open for a short time it does not hurt and it ensures
that there is no file descriptor leak.
OK deraadt@ bluhm@


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.99 24-Jul-2019 claudio

Cleanup and fix some obvious shortcomings in the mrt dump code. Also
implement the missing bits to dump MPLS VPN prefixes in the table-mp
case. Missing support noticed by benno@ because of King Bula warning.
OK benno@


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.98 17-Jul-2019 claudio

Change the Adj-RIB-Out to a per peer set of RB trees. The way RIB data
structures are linked does not scale for the Adj-RIB-Out and so inserts
and updates into the Adj-RIB-Out did not scale because of some linear
list traversals in hot paths.

A synthetic test with 4000 peers announcing one prefix each showed that
the initial convergence time dropped from around 1 hout to around 6min.

Note: because the Adj-RIB-Out is now per peer the order in which prefixes
are dumped in 'bgpctl show rib out' changed.

Tested and OK job@, benno@, phessler@


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.97 25-Jun-2019 benno

ibuf_write() returns -1 on error.
ok claudio@


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.96 24-Jun-2019 claudio

mrt dumps lost communities after the community rewrite.
Readd them by dumping them explicitly.
Tested by and OK benno@


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.95 22-Jun-2019 claudio

Add a direct pointer from struct prefix to struct pt_entry.
This changes makes it possible to not use the struct rib_entry pointer
which will be used to optimize the Adj-RIB-Out.
Also adjust pt_ref() and pt_unref() so that the code can be written
a bit more compact. Also prefix_cmp() no longer needs to go via
rib_compare() and calls pt_prefix_cmp() directly.
OK phessler@


Revision tags: OPENBSD_6_5_BASE
# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.94 07-Mar-2019 claudio

Do a better job at cleaning up the config on shutdown. Remove bits that
were missed before (e.g. network related objects). This helps to detect
memory leaks.
Start using new_config() and free_config() in all places where bgpd_config
structure are used. This way the struct is properly initialised and cleaned
up. Introduce copy_config() to only copy the values into the other struct
leaving the pointers as they were.
Looks good to benno@


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.93 27-Feb-2019 claudio

Convert the remote and local addresses in struct peer to be bgpd_addrs
instead of sockaddr_storage. This again helps protability and simplifies
some code. sa2addr now takes an optional pointer to return the port of
the sockaddr.
OK benno@


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.92 14-Feb-2019 claudio

mrt_timeout should just return -1 when there is no timeout set instead
of some strange maximum. The poll loop in bgpd.c already limits the
maximum wait time so there is no need to double it.
While there switch to using time_t for the calculation.
OK phessler@


# 1.91 14-Feb-2019 claudio

Revert part of last commit, this stuff is unrelated.


# 1.90 14-Feb-2019 claudio

Use -1 instead of the less portable INFTIM for the poll timeout.
Result is the same.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.89 21-Jan-2019 claudio

Use Adj-RIB-Out to push UPDATE messages to peers instead of having another
set of RB trees of prefixes and atribute. Refactor most of the update code
which removes some strange buffer handling. By building the output queue
directly in the Adj-RIB-Out the top memory usage during startup is greatly
reduced which should help busy server.
Tested by phessler@ and myself


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.88 30-Dec-2018 denis

add support for IPv6 VPN routes

The kernel bits are missing as of now. With input from claudio@ and kn@

OK claudio@


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.87 24-Oct-2018 claudio

Major refactoring of the RIB handling code. Mainly change how the RIB is
walked. rib_dump_r() is now an internal function and instead the code gets
an additional callback for throttling the rib_dump code. This removes a
lot of similar code used to make sure the RDE is not walking to fast and
replaces it with simpler callbacks. The other big change is the removal
of struct rib pointers in other data structures. The rib pointers are not
stable because of a realloc() call happening when extending the array so
instead use the RIB ID as a reference.
Tested and OK denis@ and benno@


Revision tags: OPENBSD_6_4_BASE
# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.86 24-Jul-2018 claudio

Use prefix_nexthop() to access the nexthop instead of dereferencing the
field in asp directly. This is a step to move the prefix from rde_aspath
to struct prefix.
OK benno@


# 1.85 17-Jul-2018 claudio

Switch to MSG_PROTOCOL_BGP4MP_ET formats for update and state mrt messages.
The _ET format adds an additional microsecond time field which makes those
message dumps more informative. The various table dumps are not modified
since there the time especially between entries plays a secondary role.
OK benno@


Revision tags: OPENBSD_6_3_BASE
# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


# 1.84 05-Feb-2018 claudio

Change struct prefix a bit. Make the aspath pointer opaque and remove the
direct pointer to the pt_prefix struct. To getter functions prefix_aspath()
and prefix_peer() added to make access of the opaque pointers possible.
Looks good henning@ and benno@


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.


Revision tags: OPENBSD_6_2_BASE
# 1.83 27-May-2017 phessler

this expects 2byte ASN, so use that, instead of an asn that may or may not fit

noticed by and OK claudio@


Revision tags: OPENBSD_6_1_BASE
# 1.82 24-Jan-2017 benno

sync log.c from relayd et al to bgpd.

there is still a little difference regarding handling of the verbosity
value that will be handled later.

ok claudio@ florian@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.81 30-Dec-2015 benno

now that ibuf_free() checks for null, we can remove the check here.
ok mmcc@ millert@ tb@ claudio@


# 1.80 05-Dec-2015 benno

cleanup some log messages with wrong function names etc.
ok henning,claudio


# 1.79 27-Oct-2015 mmcc

calloc -> malloc when the memory is immediately overwritten with memcpy.

ok claudio@


# 1.78 20-Aug-2015 deraadt

stdlib.h is in scope; do not cast malloc/calloc/realloc*
ok millert krw


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.77 19-Apr-2014 claudio

Try to be clear why struct mrt_config nodes are allocated and
assigned to struct mrt pointers. I may need to rethink this later
but lets hope this does not confuse more people and analyzers.


Revision tags: OPENBSD_5_5_BASE
# 1.76 13-Nov-2013 benno

from claudio
"Let msgbuf_write return -1 with errno EAGAIN. The users then must
check if this was the case and readd the event or poll again. The
current handling in the imsg code is wrong for sure."

ok gilles, benno


Revision tags: OPENBSD_5_4_BASE
# 1.75 30-May-2013 florian

When removing "dump (all|updates)" from bgpd.conf and reloading, tell
the session engine to actually stop logging.
Found the hard way by benno@.

with & ok benno@
input & ok claudio@


# 1.74 20-May-2013 claudio

Call pt_getaddr() a bit earlier so that addr.aid is available when first
accessed. Found by magic clang skill of blambert@


Revision tags: OPENBSD_5_3_BASE
# 1.73 15-Nov-2012 krw

buf_* -> ibuf_* in various log entries to reflect correct function names.

ok claudio@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.72 06-Nov-2011 guenther

Add a #include that's currently picked up via another header that's
violating namespace rules

ok claudio@


# 1.71 17-Sep-2011 claudio

Implement new mrt table dump format as specified in draft-ietf-grow-mrt.
Tested with IP and IPv6 sessions and against the libbgpdump parser.
OK henning@


Revision tags: OPENBSD_4_9_BASE OPENBSD_5_0_BASE
# 1.70 02-Sep-2010 sobrado

remove trailing spaces and tabs from source code; no binary changes
(verified by both sthen@ and me).

ok sthen@; "just commit it" claudio@


Revision tags: OPENBSD_4_8_BASE
# 1.69 04-Jun-2010 claudio

It is actually possible to dump IPv6 tables with "dump table" and not
"dump table-mp" but this is only possible if the session and the prefixes
are from the same address family. Evil little hackery (especially the
IPv6 nexthop encoding) but seems to work according to libbgpdump.


# 1.68 26-May-2010 nicm

Rename some imsg bits to make namespace collisions less likely buf to
ibuf, buf_read to ibuf_read, READ_BUF_SIZE to IBUF_READ_SIZE.

ok henning gilles claudio jacekm deraadt


# 1.67 22-Apr-2010 claudio

Depending on capa.neg.as4byte (bgp session is using 4-byte AS number
extension) we need to switch between _AS4 and non _AS4 types. The
non _AS4 mrt types are totaly confused by 4-byte AS_PATH attributes since
they expect 2-byte ones (and vice versa).
Problem found and fix tested by Xiaoliang Zhao (xzhao at cernet edu cn).


Revision tags: OPENBSD_4_7_BASE
# 1.66 01-Dec-2009 claudio

Use an artificial address family id in struct bgpd_addr and almost everywhere
else. Adds conversion functions to map AFI/SAFI and the Unix AF_ values
from and into AID used in bgpd. This is needed to support things like MPLS
VPN and other upcomming changes that need to play a lot with AFI/SAFI pairs.
Mostly mechanical change, henning@ has no particular issues with this.
Must go in so that I can continue working.


# 1.65 26-Oct-2009 claudio

Better error handling when doing mrt dumps (e.g. when there is no more space
on the disk). Before the SE spinned and the RDE may even crash in these events.
Found by Elisa Jasinska. OK henning@


# 1.64 12-Jul-2009 jsg

Remove a semicolon at the end of an if statement so the
test if a buffer isn't NULL before freeing has effect.

This was harmless as it is in a failure case and free(NULL) is
valid in userland.


Revision tags: OPENBSD_4_6_BASE
# 1.63 29-Jun-2009 claudio

Unfuck mrt table dumps and plug a memory leak while there.


# 1.62 05-Jun-2009 claudio

Change the way we do mrt dumps in the rde (from the retarded only one dump
possible to as many as you like). The mrt dumps are now done with the same
tree walking code as the control messages. Makes the RDE respond a bit faster
and use less memory while dumping.


# 1.61 04-Jun-2009 claudio

Make mrt understand alternate RIB plus remove some other static rib references.
There is still a problem with the mrt dumps because we only allow one in the
RDE. This needs some additional work.


# 1.60 17-May-2009 claudio

Rework most of the RDE to allow multiple RIBs. This is mostly preparation
work by changing the way the RDE DB is built. struct prefix and struct
pt_entry are simplified and extended with a rib_entry where the decision
tree is run on. From now on a prefix can only reside on one particular RIB
which simplifies the code a bit. Currently there are two fixed ribs
(adj-rib-in and the local-rib) which needs to be made more dynamic in
upcomming commits.
This is work in progress, the RDE seems to work for me and sthen@ (no flames
comming out of our testrouters but there is still a lot missing)
Move into the tree to simplify developement -- henning@


# 1.59 29-Apr-2009 claudio

de-static some functions. Helps handling the monster change I'm working on.


# 1.58 19-Mar-2009 claudio

First big chunk of mrt rewrite. Simplifies code hopefully and fixes table
dumps that were wrong because of the 4byte AS support. Dumps I took seemed
to work so far. "Put it in" henning@


# 1.57 18-Mar-2009 claudio

Correctly check for MRT_TABLE_DUMP_MP and don't send the config to the SE.
Make mrt table-mp dumps actually work. Found on the plane from NRT to ZRH.
OK henning@


# 1.56 13-Mar-2009 claudio

Next step in mrt cleanup. Switch buf_write to a smarter version of itself.
Doing more the stuff msgbuf_write does and switch the only user -- mrt --
over to use this simpler way of writing out stuff.


# 1.55 13-Mar-2009 claudio

Move mrt code over to use the msgbuf way of queuing buffers instead of rolling
an own version. More cleanup to come.


Revision tags: OPENBSD_4_5_BASE
# 1.54 19-Feb-2009 claudio

Use a bob sized header for IPv6 not the default pedro sized one.
Seems like nobody ever tried to dump IPv6 tables. Found while reworking this
code.


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.53 23-Apr-2007 claudio

Make bgpd 4-byte AS compatible. All internal representations of AS numbers
are now 4-byte instead of the old 2-byte numbers. The only exception are
communities because they can not be switched. The RDE will inflate and deflate
the ASPATH and AGGREGATOR attributes on demand and create the NEW_ASPATH and
NEW_AGGREGATOR field whenever needed. Both old and new stile sessions are
supported and can be mixed. Currently new stile sessions with the 4-byte AS
number capability turned on are only enabled if one of the AS numbers involved
is a 4-byte one.
This is based on an initial diff by Geoff Huston gih (at) apnic (dot) net
Cleanup, testing and bug-fixes by myself (via AS 3.10).
Currently mrt table dumps are producing incompatible output this will be fixed
afterwards -- this diff is already big enough.

"get it in if you think it is ready" henning@


Revision tags: OPENBSD_4_1_BASE
# 1.52 12-Feb-2007 claudio

Switch address and AS fields in mrt dump messages for incoming traffic so
that source and destination match the actual traffic flow.
Found and initial diff by Adam Chappell (Adam.Chappell at interoute dot com)


# 1.51 12-Feb-2007 claudio

Fix some log_warn() messages.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.50 08-Feb-2006 claudio

For now dump only stuff from the local-RIB into mrt table dumps.
Issue found by Andre Oppermann.


# 1.49 05-Jan-2006 claudio

Cache optional BGP attributes (mostly communities) and use a simple
pointer plus a ref counter to link the attributes to the path object.
Saves +/- 10M on 11 full feeds. Looks good Henning


# 1.48 29-Nov-2005 claudio

Add a flags field to struct prefix which will be used shortly. Remove the peer
pointer so that the size does not grow. Adding 4 bytes to struct prefix would
result in 64MB more memory usage on one of my systems.


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.47 23-Dec-2004 henning

KNF


# 1.46 11-Nov-2004 claudio

Store the local v4 and v6 address of a session. For IPv4 sessions the first
IPv6 address with global scope of the interface used by the session is used.
In any case the local endpoint of the session is used for one of the two no
matter what scope.
OK henning@


# 1.45 16-Sep-2004 henning

imsg API cleanup:
-kill imsg_compose_pid, imsg_compose_fdpass and imsg_create_pid
-extend the original imsg_compose/_create API to take pid & fd too
-make imsg_compose do imsg_create + imsg_add + imsg_close instead of
duplicating the code
-adjust all callers to the new API
ok claudio


Revision tags: OPENBSD_3_6_BASE
# 1.44 13-Aug-2004 claudio

Fix minor issues with IPv6 dumps and add a function for dumping the RIB table
protocol independent. This new dump format is not (yet) supported by the
mrtd route_btoa tool. OK henning@


# 1.43 11-Aug-2004 claudio

mrt dumps for cloned sessions did not work because the wrong address was used.
OK henning@


# 1.42 11-Aug-2004 claudio

buf_write() returns -1 on error and -2 on connection closed. Treat both
as errors for mrt dumps.


# 1.41 10-Aug-2004 claudio

Always free message buffer on error.


# 1.40 10-Aug-2004 claudio

Support IPv6 in message and state transition dumps. Table dumps still
unsupported. OK henning@


# 1.39 06-Aug-2004 claudio

Monster diff to get one step closer to IPv6 support.
Cleanup path attribute handling. First of all kill struct attr_flags, all
those infos are now in struct rde_aspath. Second move attribute parser
functions into rde.c, rde_attr.c is shared between bgpd and bgpctl.
Third reimplementation of the nexthop handling. Make it IPv6 ready and
fix some major bug relating to "set nexthop".
henning@ OK if it breaks nothing


# 1.38 05-Aug-2004 claudio

struct prefix has a pointer to the peer so use it everywhere directly instead
of the detour via aspath.


# 1.37 03-Aug-2004 claudio

Make mrt dumps mrt compliant. I already fixed one of those but this one
slipped my attention. Now all bgpd dumps can be read with mrt's route_btoa.


# 1.36 28-Jul-2004 claudio

The hole dance to close a mrt file after fd passing in the parent is not
needed as the fd is closed while beeing passed. looks good henning@


# 1.35 07-Jul-2004 claudio

Stupid typo that may cause a lot of pain. Found by Patrick Latifi. Thanks.


# 1.34 07-Jul-2004 claudio

Bad interpretation of the oh so well documented mrt spec. Found by
Stefan Wahl.


# 1.33 03-Jul-2004 claudio

Switch mrt dumping to fd passing. This gives some speed up when extensive
dumping is done. Acctually mrt dumps were broken because of the fd passing.
The nice side effect is a much cleaner code, especially in the parent process.
OK henning@


# 1.32 22-Jun-2004 claudio

Make the RDE IPv6 ready missing is the message handling. The internal
prefix tree changed form a hash table to a per AF RB tree.
OK henning@ some ideas are from Brent Graveland.


# 1.31 20-Jun-2004 henning

at least somewhat consistently name the TAILQ_ENTRYs... this confused me
more than once


# 1.30 29-Apr-2004 deraadt

sock -> fd; ok henning


# 1.29 28-Apr-2004 deraadt

spacing


Revision tags: OPENBSD_3_5_BASE
# 1.28 27-Feb-2004 claudio

remove unneded peer pointer in struct prefix and change a in_addr_t to
struct in_addr. OK henning@


# 1.27 25-Feb-2004 claudio

Rewrite some parts of the mrt dump handling. It is no longer possible to
dump the filtered updates but therefore it is now possible to dump per
neighbor and also to dump the outgoing messages. OK henning@


# 1.26 19-Feb-2004 claudio

Make the code more portable. Add some missing header files and make the use
of the queue(3) makros more portable. OK henning@ some time ago.


# 1.25 02-Feb-2004 henning

* use macro expansion instead of hardcoding 'buf'.
From: Patrick Latifi <pat@eyeo.org>, claudio ok


# 1.24 27-Jan-2004 henning

use a struct bgpd_addr instead of sockaddr_in for peer_config->local_addr and
->remote_addr for easier multiprotocol support

ok claudio@


# 1.23 22-Jan-2004 henning

use log_warnx and log_info. reclassify a few messages in the process and fix
a few messages.

ok claudio@


# 1.22 11-Jan-2004 claudio

Move all struct in_addr to either struct bgpd_addr or in_addr_t whichever
is more appropriate. The rde uses now in most cases struct bgpd_addr.
OK henning@


# 1.21 11-Jan-2004 claudio

The buffer changes produced some fallout in the mrt code.
Wait until all data has been written out before closing the file and fix
some obvious misstakes. OK henning@


# 1.20 10-Jan-2004 claudio

RDE update generation. First we queue all updates and withdraws on a per
peer basis. A queue runner will dequeue and package those messages to valid
bgp UPDATE messages and send them to the SE.
Not yet done is per peer type attribute handling (like aspath prepends and
nexthop modifications) and the queue runner could be a tad smarter. All in
all this gives us a good starting point for the missing parts.
OK henning@


# 1.19 07-Jan-2004 claudio

Duh. Fixing stupid braino. OK henning@


# 1.18 06-Jan-2004 claudio

Free buf on error and correct of by one in strlcpy.
From Patrick Latifi <pat at eyeo.org> OK henning@


# 1.17 06-Jan-2004 claudio

2004 OK henning@


# 1.16 06-Jan-2004 claudio

Fix reopening of dump files.
OK henning@


# 1.15 05-Jan-2004 claudio

Big overhaul of the mrt code.
Dumping of incomming bgp messages is now possible and dumping the (not yet)
filtered updates works too. Per neighbor dumps are still missing.
OK henning@


# 1.14 01-Jan-2004 henning

conf -> mconf
causes a conflicht with upcoming changes


# 1.13 26-Dec-2003 henning

when this project started and i added the fatal() function, I made it take
the error number as parameter instead of accessing errno, because in one
place the error number was not in errno but fetched from a socket.
now, of course it makes much more sense to just set errno to the error number
just fecthed in this one place instead of having hundreds of fatal() calls
all transfer the errno round and round and round...
fix this, and also provide a fatalx, which does not care for errno and doesn't
invoke strerror.
oh, btw, in the place where we fetch the err # from the socket, we don't
call fatal anymore anyway...


# 1.12 26-Dec-2003 claudio

In the mrt state machine one state was forgotten. This caused the parent
process to quit it a fatal buf_close error message.
Noticed and reported by jakob@
OK henning@


# 1.11 25-Dec-2003 henning

free where it was allocated; much easier to verify


# 1.10 24-Dec-2003 henning

handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.


# 1.9 23-Dec-2003 deraadt

spacing


# 1.8 21-Dec-2003 henning

wrap read & write buffers for imsgs into a struct.
finally gives us read buffers per pipe instead of per process, eleminating
a possible race.
also gets us a real imsg_init() that does all the boring init work


# 1.7 21-Dec-2003 henning

overhaul the write buffering code.
introduce msgbuf API and bundle all info needed for the write buffers in a
struct msgbuf.
also switch to a write queue per handled connection (each bgp session, each
pipe) instead of one big one.
fixes some subtle problems and is overall nicer.

ok claudio@


# 1.6 21-Dec-2003 claudio

yet more from the castathon; most aspath functions where accessing non-
aligned memory (u_int16_t) therefor crashed the RDE on my sparc64. All
buffer specific functions use now void * instead of u_char * so most cast
are now history. Tested on sparc64 and i386. OK henning@


# 1.5 20-Dec-2003 claudio

knf. OK henning@


# 1.4 20-Dec-2003 henning

let buf_add take void *data instead of u_char *data and save a zillion
evil casts


# 1.3 20-Dec-2003 henning

few missing break; in default: cases in switch; one noticed by tedu@


# 1.2 19-Dec-2003 deraadt

knf & 64-bit cleanup; henning ok


# 1.1 17-Dec-2003 henning

welcome, bgpd
started by me some time ago with moral support from theo, the proceeded up to
the point where the session engine worked correctly. claudio jeker joined
then and did a lot of work in the RDE.
it is not particulary usefull as application right now as parts are still
missing but is imported to enable more people to work on it.
status:
BGP sessions get established fine, OPEN messages and then KEEPALIVEs
exchanged etc. session FSM works fine; NOTIFICATIONs are handled fine, and
all connection drops etc I provoked get handled fine.
Incoming UPDATE messgages are parsed well and the data entered to the RIB,
the decision process is not yet there, neither is outgoing UPDATEs or sync
to the kernel routing table.

not connected to the builds yet.