1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/* $OpenBSD: bgpd.h,v 1.44 2003/12/28 14:34:30 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __BGPD_H__
#define __BGPD_H__
#include <sys/types.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <syslog.h>
#define BGP_VERSION 4
#define BGP_PORT 179
#define CONFFILE "/etc/bgpd.conf"
#define BGPD_USER "_bgpd"
#define PEER_DESCR_LEN 32
#define MAX_PKTSIZE 4096
#define MIN_HOLDTIME 3
#define READ_BUF_SIZE 65535
#define RT_BUF_SIZE 16384
#define BGPD_OPT_VERBOSE 0x0001
#define BGPD_OPT_VERBOSE2 0x0002
#define BGPD_OPT_NOACTION 0x0004
#define BGPD_FLAG_NO_FIB_UPDATE 0x0001
#define BGPD_LOG_UPDATES 0x0001
enum {
PROC_MAIN,
PROC_SE,
PROC_RDE
} bgpd_process;
enum session_state {
STATE_NONE,
STATE_IDLE,
STATE_CONNECT,
STATE_ACTIVE,
STATE_OPENSENT,
STATE_OPENCONFIRM,
STATE_ESTABLISHED
};
enum session_events {
EVNT_NONE,
EVNT_START,
EVNT_STOP,
EVNT_CON_OPEN,
EVNT_CON_CLOSED,
EVNT_CON_OPENFAIL,
EVNT_CON_FATAL,
EVNT_TIMER_CONNRETRY,
EVNT_TIMER_HOLDTIME,
EVNT_TIMER_KEEPALIVE,
EVNT_RCVD_OPEN,
EVNT_RCVD_KEEPALIVE,
EVNT_RCVD_UPDATE,
EVNT_RCVD_NOTIFICATION
};
enum reconf_action {
RECONF_NONE,
RECONF_KEEP,
RECONF_REINIT,
RECONF_DELETE
};
struct buf {
TAILQ_ENTRY(buf) entries;
u_char *buf;
ssize_t size;
ssize_t wpos;
ssize_t rpos;
};
struct msgbuf {
u_int32_t queued;
int sock;
TAILQ_HEAD(bufs, buf) bufs;
};
struct bgpd_config {
int opts;
u_int16_t as;
u_int32_t bgpid;
u_int16_t holdtime;
u_int16_t min_holdtime;
int flags;
int log;
struct sockaddr_in listen_addr;
struct peer *peers;
};
struct buf_read {
u_char buf[READ_BUF_SIZE];
u_char *rptr;
ssize_t wpos;
};
struct peer_config {
u_int32_t id;
char group[PEER_DESCR_LEN];
char descr[PEER_DESCR_LEN];
struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
u_int16_t remote_as;
u_int8_t ebgp; /* 1 = ebgp, 0 = ibgp */
u_int8_t distance; /* 1 = direct, >1 = multihop */
u_int8_t passive;
enum reconf_action reconf_action;
};
struct peer {
struct peer_config conf;
u_int32_t remote_bgpid;
u_int16_t holdtime;
enum session_state state;
time_t ConnectRetryTimer;
time_t KeepaliveTimer;
time_t HoldTimer;
time_t StartTimer;
u_int StartTimerInterval;
int sock;
int events;
struct msgbuf wbuf;
struct buf_read *rbuf;
struct peer *next;
};
#define MRT_FILE_LEN 512
enum mrtdump_type {
MRT_NONE,
MRT_TABLE_DUMP
/*
* MRT_UPDATE_START,
* MRT_SESSION_START,
* MRT_UPDATE_STOP,
* MRT_SESSION_STOP,
*/
};
enum mrtdump_state {
MRT_STATE_OPEN,
MRT_STATE_RUNNING,
MRT_STATE_DONE,
MRT_STATE_CLOSE,
MRT_STATE_REOPEN
};
LIST_HEAD(mrt_config, mrtdump_config);
struct mrtdump_config {
enum mrtdump_type type;
u_int32_t id;
struct msgbuf msgbuf;
char name[MRT_FILE_LEN]; /* base file name */
char file[MRT_FILE_LEN]; /* actual file name */
time_t ReopenTimer;
time_t ReopenTimerInterval;
enum mrtdump_state state;
LIST_ENTRY(mrtdump_config)
list;
};
/* ipc messages */
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
#define MAX_IMSGSIZE 8192
struct imsgbuf {
int sock;
struct buf_read r;
struct msgbuf w;
};
enum imsg_type {
IMSG_NONE,
IMSG_RECONF_CONF,
IMSG_RECONF_PEER,
IMSG_RECONF_DONE,
IMSG_UPDATE,
IMSG_UPDATE_ERR,
IMSG_SESSION_UP,
IMSG_SESSION_DOWN,
IMSG_MRT_REQ,
IMSG_MRT_MSG,
IMSG_MRT_END,
IMSG_KROUTE_CHANGE,
IMSG_KROUTE_DELETE,
IMSG_NEXTHOP_ADD,
IMSG_NEXTHOP_REMOVE,
IMSG_NEXTHOP_UPDATE
};
struct imsg_hdr {
enum imsg_type type;
u_int16_t len;
u_int32_t peerid;
};
struct imsg {
struct imsg_hdr hdr;
void *data;
};
/* error subcode for UPDATE; needed in SE and RDE */
enum suberr_update {
ERR_UPD_ATTRLIST = 1,
ERR_UPD_UNKNWN_WK_ATTR,
ERR_UPD_MISSNG_WK_ATTR,
ERR_UPD_ATTRFLAGS,
ERR_UPD_ATTRLEN,
ERR_UPD_ORIGIN,
ERR_UPD_LOOP,
ERR_UPD_NEXTHOP,
ERR_UPD_OPTATTR,
ERR_UPD_NETWORK,
ERR_UPD_ASPATH
};
struct kroute {
in_addr_t prefix;
u_int8_t prefixlen;
in_addr_t nexthop;
};
struct kroute_nexthop {
in_addr_t nexthop;
u_int8_t valid;
u_int8_t connected;
in_addr_t gateway;
};
/* prototypes */
/* bgpd.c */
void send_nexthop_update(struct kroute_nexthop *);
/* session.c */
int session_main(struct bgpd_config *, int[2], int[2]);
/* buffer.c */
struct buf *buf_open(ssize_t);
int buf_add(struct buf *, void *, ssize_t);
void *buf_reserve(struct buf *, ssize_t);
int buf_close(struct msgbuf *, struct buf *);
void buf_free(struct buf *);
void msgbuf_init(struct msgbuf *);
void msgbuf_clear(struct msgbuf *);
int msgbuf_write(struct msgbuf *);
/* log.c */
void log_init(int);
void logit(int, const char *, ...);
void vlog(int, const char *, va_list);
void log_peer_err(const struct peer *, const char *, ...);
void log_peer_errx(const struct peer *, const char *, ...);
void log_err(const char *, ...);
void fatal(const char *);
void fatalx(const char *);
void fatal_ensure(const char *, int, const char *);
void log_statechange(const struct peer *, enum session_state,
enum session_events);
void log_notification(const struct peer *, u_int8_t, u_int8_t,
u_char *, u_int16_t);
void log_conn_attempt(const struct peer *, struct in_addr);
char *log_ntoa(in_addr_t);
/* parse.y */
int cmdline_symset(char *);
int parse_config(char *, struct bgpd_config *, struct mrt_config *);
/* config.c */
int merge_config(struct bgpd_config *, struct bgpd_config *);
/* imsg.c */
void imsg_init(struct imsgbuf *, int);
int imsg_get(struct imsgbuf *, struct imsg *);
int imsg_compose(struct imsgbuf *, int, u_int32_t, void *, u_int16_t);
void imsg_free(struct imsg *);
/* rde.c */
int rde_main(struct bgpd_config *, int[2], int[2]);
/* mrt.c */
int mrt_mergeconfig(struct mrt_config *, struct mrt_config *);
/* kroute.c */
int kroute_init(int);
int kroute_change(struct kroute *);
int kroute_delete(struct kroute *);
void kroute_shutdown(void);
void kroute_fib_couple(void);
void kroute_fib_decouple(void);
int kroute_dispatch_msg(void);
int kroute_nexthop_add(in_addr_t);
void kroute_nexthop_delete(in_addr_t);
#endif /* __BGPD_H__ */
|