]> git.ipfire.org Git - thirdparty/bird.git/blame - proto/bgp/bgp.h
Fixes handling of 'next hop self' and 'source address' configuration
[thirdparty/bird.git] / proto / bgp / bgp.h
CommitLineData
2638249d
MM
1/*
2 * BIRD -- The Border Gateway Protocol
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9#ifndef _BIRD_BGP_H_
10#define _BIRD_BGP_H_
11
c2b28c99
MM
12#include "nest/route.h"
13
c00d31be 14struct linpool;
10be74da 15struct eattr;
c00d31be 16
2638249d
MM
17struct bgp_config {
18 struct proto_config c;
11cb6202 19 u32 local_as, remote_as;
2638249d
MM
20 ip_addr remote_ip;
21 int multihop; /* Number of hops if multihop */
c00d31be 22 ip_addr multihop_via; /* Multihop: address to route to */
8b258e4e 23 ip_addr source_addr; /* Source address to use */
48e842cc 24 int next_hop_self; /* Always set next hop to local IP address */
56a2bed4
MM
25 int compare_path_lengths; /* Use path lengths when selecting best route */
26 u32 default_local_pref; /* Default value for LOCAL_PREF attribute */
27 u32 default_med; /* Default value for MULTI_EXIT_DISC attribute */
e8ba557c 28 int capabilities; /* Enable capability handshake [RFC3392] */
ba5ed6f3 29 int enable_as4; /* Enable local support for 4B AS numbers [RFC4893] */
4847a894
OZ
30 u32 rr_cluster_id; /* Route reflector cluster ID, if different from local ID */
31 int rr_client; /* Whether neighbor is RR client of me */
a92fe607 32 int rs_client; /* Whether neighbor is RS client of me */
e3299ab1 33 int advertise_ipv4; /* Whether we should add IPv4 capability advertisement to OPEN message */
3fdbafb6
MM
34 unsigned connect_retry_time;
35 unsigned hold_time, initial_hold_time;
36 unsigned keepalive_time;
6fd766c1
MM
37 unsigned start_delay_time; /* Minimum delay between connects */
38 unsigned error_amnesia_time; /* Errors are forgotten after */
39 unsigned error_delay_time_min; /* Time to wait after an error is detected */
40 unsigned error_delay_time_max;
41 unsigned disable_after_error; /* Disable the protocol when error is detected */
d51aa281 42 char *password; /* Password used for MD5 authentication */
c01e3741
MM
43};
44
45struct bgp_conn {
46 struct bgp_proto *bgp;
47 struct birdsock *sk;
72a6ef11 48 unsigned int state; /* State of connection state machine */
c01e3741
MM
49 struct timer *connect_retry_timer;
50 struct timer *hold_timer;
51 struct timer *keepalive_timer;
11b32d91 52 struct event *tx_ev;
3fdbafb6 53 int packets_to_send; /* Bitmap of packet types to be sent */
efcece2d
MM
54 int notify_code, notify_subcode, notify_size;
55 byte *notify_data;
11cb6202 56 u32 advertised_as; /* Temporary value for AS number received */
165a6227
OZ
57 int start_state; /* protocol start_state snapshot when connection established */
58 int want_as4_support; /* Connection tries to establish AS4 session */
59 int peer_as4_support; /* Peer supports 4B AS numbers [RFC4893] */
3fdbafb6 60 unsigned hold_time, keepalive_time; /* Times calculated from my and neighbor's requirements */
2638249d
MM
61};
62
63struct bgp_proto {
64 struct proto p;
c01e3741 65 struct bgp_config *cf; /* Shortcut to BGP configuration */
11cb6202 66 u32 local_as, remote_as;
11b32d91 67 int start_state; /* Substates that partitions BS_START */
c01e3741 68 int is_internal; /* Internal BGP connection (local_as == remote_as) */
ba5ed6f3 69 int as4_session; /* Session uses 4B AS numbers in AS_PATH (both sides support it) */
c01e3741
MM
70 u32 local_id; /* BGP identifier of this router */
71 u32 remote_id; /* BGP identifier of the neighbor */
4847a894
OZ
72 u32 rr_cluster_id; /* Route reflector cluster ID */
73 int rr_client; /* Whether neighbor is RR client of me */
a92fe607 74 int rs_client; /* Whether neighbor is RS client of me */
b552ecc4
MM
75 struct bgp_conn *conn; /* Connection we have established */
76 struct bgp_conn outgoing_conn; /* Outgoing connection we're working with */
c01e3741
MM
77 struct bgp_conn incoming_conn; /* Incoming connection we have neither accepted nor rejected yet */
78 struct object_lock *lock; /* Lock for neighbor connection */
48e842cc
MM
79 ip_addr next_hop; /* Either the peer or multihop_via */
80 struct neighbor *neigh; /* Neighbor entry corresponding to next_hop */
81 ip_addr local_addr; /* Address of the local end of the link to next_hop */
ad440a57 82 ip_addr source_addr; /* Address used as advertised next hop, usually local_addr */
11b32d91 83 struct event *event; /* Event for respawning and shutting process */
c2b28c99 84 struct bgp_bucket **bucket_hash; /* Hash table of attribute buckets */
ae8f5584 85 unsigned int hash_size, hash_count, hash_limit;
c2b28c99
MM
86 struct fib prefix_fib; /* Prefixes to be sent */
87 list bucket_queue; /* Queue of buckets to send */
88 struct bgp_bucket *withdraw_bucket; /* Withdrawn routes */
6fd766c1 89 unsigned startup_delay; /* Time to delay protocol startup by due to errors */
11b32d91
OZ
90 bird_clock_t last_proto_error; /* Time of last error that leads to protocol stop */
91 u8 last_error_class; /* Error class of last error */
92 u32 last_error_code; /* Error code of last error. BGP protocol errors
93 are encoded as (bgp_err_code << 16 | bgp_err_subcode) */
1c1da87b
MM
94#ifdef IPV6
95 byte *mp_reach_start, *mp_unreach_start; /* Multiprotocol BGP attribute notes */
96 unsigned mp_reach_len, mp_unreach_len;
11d4474c 97 ip_addr local_link; /* Link-level version of local_addr */
1c1da87b 98#endif
c2b28c99
MM
99};
100
101struct bgp_prefix {
102 struct fib_node n; /* Node in prefix fib */
103 node bucket_node; /* Node in per-bucket list */
104};
105
106struct bgp_bucket {
c2b28c99 107 node send_node; /* Node in send queue */
f421cfdd 108 struct bgp_bucket *hash_next, *hash_prev; /* Node in bucket hash table */
c2b28c99
MM
109 unsigned hash; /* Hash over extended attributes */
110 list prefixes; /* Prefixes in this buckets */
111 ea_list eattrs[0]; /* Per-bucket extended attributes */
2638249d
MM
112};
113
72a6ef11
MM
114#define BGP_PORT 179
115#define BGP_VERSION 4
116#define BGP_HEADER_LENGTH 19
117#define BGP_MAX_PACKET_LENGTH 4096
118#define BGP_RX_BUFFER_SIZE 4096
119#define BGP_TX_BUFFER_SIZE BGP_MAX_PACKET_LENGTH
2638249d 120
973399ae
MM
121extern struct linpool *bgp_linpool;
122
11cb6202
OZ
123extern int bgp_as4_support;
124
125
3fdbafb6 126void bgp_start_timer(struct timer *t, int value);
2638249d 127void bgp_check(struct bgp_config *c);
efcece2d 128void bgp_error(struct bgp_conn *c, unsigned code, unsigned subcode, byte *data, int len);
b552ecc4 129void bgp_close_conn(struct bgp_conn *c);
11b32d91
OZ
130void bgp_update_startup_delay(struct bgp_proto *p, struct bgp_conn *conn, unsigned code, unsigned subcode);
131void bgp_conn_enter_established_state(struct bgp_conn *conn);
132void bgp_conn_enter_close_state(struct bgp_conn *conn);
133void bgp_conn_enter_idle_state(struct bgp_conn *conn);
134void bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code);
135
2638249d 136
85368cd4
MM
137#ifdef LOCAL_DEBUG
138#define BGP_FORCE_DEBUG 1
139#else
140#define BGP_FORCE_DEBUG 0
141#endif
142#define BGP_TRACE(flags, msg, args...) do { if ((p->p.debug & flags) || BGP_FORCE_DEBUG) \
143 log(L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
144
cb530392
OZ
145#define BGP_TRACE_RL(rl, flags, msg, args...) do { if ((p->p.debug & flags) || BGP_FORCE_DEBUG) \
146 log_rl(rl, L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
147
148
c01e3741
MM
149/* attrs.c */
150
4847a894
OZ
151void bgp_attach_attr(struct ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val);
152byte *bgp_attach_attr_wa(struct ea_list **to, struct linpool *pool, unsigned attr, unsigned len);
2a9e064d 153struct rta *bgp_decode_attrs(struct bgp_conn *conn, byte *a, unsigned int len, struct linpool *pool, int mandatory);
aebe06b4 154int bgp_get_attr(struct eattr *e, byte *buf, int buflen);
ef2c708d
MM
155int bgp_rte_better(struct rte *, struct rte *);
156void bgp_rt_notify(struct proto *, struct network *, struct rte *, struct rte *, struct ea_list *);
157int bgp_import_control(struct proto *, struct rte **, struct ea_list **, struct linpool *);
ae8f5584 158void bgp_attr_init(struct bgp_proto *);
11cb6202 159unsigned int bgp_encode_attrs(struct bgp_proto *p, byte *w, ea_list *attrs, int remains);
f421cfdd 160void bgp_free_bucket(struct bgp_proto *p, struct bgp_bucket *buck);
5e88d730 161void bgp_get_route_info(struct rte *, byte *buf, struct ea_list *attrs);
c00d31be 162
4847a894
OZ
163inline static void bgp_attach_attr_ip(struct ea_list **to, struct linpool *pool, unsigned attr, ip_addr a)
164{ *(ip_addr *) bgp_attach_attr_wa(to, pool, attr, sizeof(ip_addr)) = a; }
165
c01e3741
MM
166/* packets.c */
167
72a6ef11 168void bgp_schedule_packet(struct bgp_conn *conn, int type);
11b32d91 169void bgp_kick_tx(void *vconn);
72a6ef11
MM
170void bgp_tx(struct birdsock *sk);
171int bgp_rx(struct birdsock *sk, int size);
11b32d91 172const byte * bgp_error_dsc(byte *buff, unsigned code, unsigned subcode);
efcece2d 173void bgp_log_error(struct bgp_proto *p, char *msg, unsigned code, unsigned subcode, byte *data, unsigned len);
72a6ef11 174
c01e3741
MM
175/* Packet types */
176
177#define PKT_OPEN 0x01
178#define PKT_UPDATE 0x02
179#define PKT_NOTIFICATION 0x03
180#define PKT_KEEPALIVE 0x04
72a6ef11 181#define PKT_SCHEDULE_CLOSE 0x1f /* Used internally to schedule socket close */
c01e3741
MM
182
183/* Attributes */
184
185#define BAF_OPTIONAL 0x80
186#define BAF_TRANSITIVE 0x40
187#define BAF_PARTIAL 0x20
188#define BAF_EXT_LEN 0x10
189
190#define BA_ORIGIN 0x01 /* [RFC1771] */ /* WM */
191#define BA_AS_PATH 0x02 /* WM */
192#define BA_NEXT_HOP 0x03 /* WM */
193#define BA_MULTI_EXIT_DISC 0x04 /* ON */
c00d31be 194#define BA_LOCAL_PREF 0x05 /* WD */
c01e3741
MM
195#define BA_ATOMIC_AGGR 0x06 /* WD */
196#define BA_AGGREGATOR 0x07 /* OT */
197#define BA_COMMUNITY 0x08 /* [RFC1997] */ /* OT */
198#define BA_ORIGINATOR_ID 0x09 /* [RFC1966] */ /* ON */
199#define BA_CLUSTER_LIST 0x0a /* ON */
200/* We don't support these: */
201#define BA_DPA 0x0b /* ??? */
202#define BA_ADVERTISER 0x0c /* [RFC1863] */
203#define BA_RCID_PATH 0x0d
204#define BA_MP_REACH_NLRI 0x0e /* [RFC2283] */
205#define BA_MP_UNREACH_NLRI 0x0f
206#define BA_EXTENDED_COMM 0x10 /* draft-ramachandra-bgp-ext-communities */
11cb6202
OZ
207#define BA_AS4_PATH 0x11 /* [RFC4893] */
208#define BA_AS4_AGGREGATOR 0x12
c01e3741 209
11b32d91 210/* BGP connection states */
c01e3741
MM
211
212#define BS_IDLE 0
213#define BS_CONNECT 1 /* Attempting to connect */
214#define BS_ACTIVE 2 /* Waiting for connection retry & listening */
215#define BS_OPENSENT 3
216#define BS_OPENCONFIRM 4
217#define BS_ESTABLISHED 5
11b32d91
OZ
218#define BS_CLOSE 6 /* Used during transition to BS_IDLE */
219
220/* BGP start states
221 *
222 * Used in PS_START for fine-grained specification of starting state.
223 *
224 * When BGP protocol is started by core, it goes to BSS_PREPARE. When BGP protocol
225 * done what is neccessary to start itself (like acquiring the lock), it goes to BSS_CONNECT.
226 * When some connection attempt failed because of option or capability error, it goes to
227 * BSS_CONNECT_NOCAP.
228 */
229
230#define BSS_PREPARE 0 /* Used before ordinary BGP started, i. e. waiting for lock */
231#define BSS_CONNECT 1 /* Ordinary BGP connecting */
232#define BSS_CONNECT_NOCAP 2 /* Legacy BGP connecting (without capabilities) */
233
234/* Error classes */
235
236#define BE_NONE 0
237#define BE_MISC 1 /* Miscellaneous error */
238#define BE_SOCKET 2 /* Socket error */
239#define BE_BGP_RX 3 /* BGP protocol error notification received */
240#define BE_BGP_TX 4 /* BGP protocol error notification sent */
241#define BE_AUTO_DOWN 5 /* Automatic shutdown */
242#define BE_MAN_DOWN 6 /* Manual shutdown */
243
244/* Misc error codes */
245
246#define BEM_NEIGHBOR_LOST 1
247#define BEM_INVALID_NEXT_HOP 2
248#define BEM_INVALID_MD5 3 /* MD5 authentication kernel request failed (possibly not supported */
249
c01e3741 250
bd2d8190
MM
251/* Well-known communities */
252
253#define BGP_COMM_NO_EXPORT 0xffffff01 /* Don't export outside local AS / confed. */
254#define BGP_COMM_NO_ADVERTISE 0xffffff02 /* Don't export at all */
255#define BGP_COMM_NO_EXPORT_SUBCONFED 0xffffff03 /* NO_EXPORT even in local confederation */
256
cea63664
MM
257/* Origins */
258
259#define ORIGIN_IGP 0
260#define ORIGIN_EGP 1
261#define ORIGIN_INCOMPLETE 2
262
1c1da87b
MM
263/* Address families */
264
e3299ab1 265#define BGP_AF_IPV4 1
1c1da87b
MM
266#define BGP_AF_IPV6 2
267
2638249d 268#endif