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
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* BGP Open State -- functions
* Copyright (C) 2009 Chris Hall (GMCH), Highwayman
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "zebra.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_peer.h"
#include "bgpd/bgp_session.h"
#include "bgpd/bgp_open_state.h"
#include "lib/memory.h"
#include "lib/memtypes.h"
/*==============================================================================
* BGP Open State.
*
* This structure encapsulates all the information that may be sent/received
* in a BGP OPEN Message.
*
*/
/* Initialise new bgp_open_state structure -- allocate if required.
*
*/
bgp_open_state
bgp_open_state_init_new(bgp_open_state state)
{
if (state == NULL)
state = XCALLOC(MTYPE_BGP_OPEN_STATE, sizeof(struct bgp_open_state)) ;
else
memset(state, 0, sizeof(struct bgp_open_state)) ;
vector_init_new(&state->unknowns, 0) ;
return state ;
}
bgp_open_state
bgp_open_state_free(bgp_open_state state)
{
bgp_cap_unknown unknown ;
if (state != NULL)
{
while ((unknown = vector_ream_keep(&state->unknowns)) != NULL)
XFREE(MTYPE_TMP, unknown) ;
XFREE(MTYPE_BGP_OPEN_STATE, state) ;
} ;
return NULL ;
}
/*==============================================================================
* Construct new bgp_open_state for the given peer -- allocate if required.
*
* Initialises the structure according to the current peer state.
*/
bgp_open_state
bgp_peer_open_state_init_new(bgp_open_state state, bgp_peer peer)
{
safi_t safi ;
afi_t afi ;
state = bgp_open_state_init_new(state) ; /* allocate if req. Zeroise. */
/* Choose the appropriate ASN */
if (peer->change_local_as)
state->my_as = peer->change_local_as ;
else
state->my_as = peer->local_as ;
/* Choose the appropriate hold time */
if (peer->config & PEER_CONFIG_TIMER)
state->holdtime = peer->holdtime ;
else
state->holdtime = peer->bgp->default_holdtime ;
/* Set our bgpd_id */
state->bgp_id = peer->local_id.s_addr ;
/* Do not send capability. */ /* TODO: can_capability? */
state->can_capability =
CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN)
&& (! CHECK_FLAG(peer->flags, PEER_FLAG_DONT_CAPABILITY) ) ;
/* Announce self as AS4 speaker if required */
state->can_as4 = ((peer->cap & PEER_CAP_AS4_ADV) != 0) ;
/* Fill in the supported AFI/SAFI */
for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
if (peer->afc[afi][safi])
state->can_mp_ext |= qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi)) ;
/* Route refresh. */
state->can_r_refresh = (peer->cap & PEER_CAP_REFRESH_ADV)
? (bgp_cap_form_old | bgp_cap_form_new)
: bgp_cap_form_none ;
/* ORF capability. */
for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
{
if (peer->af_flags[afi][safi] & PEER_FLAG_ORF_PREFIX_SM)
state->can_orf_prefix_send |=
qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi)) ;
if (peer->af_flags[afi][safi] & PEER_FLAG_ORF_PREFIX_RM)
state->can_orf_prefix_recv |=
qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi)) ;
} ;
state->can_orf_prefix = (state->can_orf_prefix_send |
state->can_orf_prefix_recv)
? (bgp_cap_form_old | bgp_cap_form_new)
: bgp_cap_form_none ;
/* Dynamic Capabilities TODO: check requirement */
state->can_dynamic = ( CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)
!= 0 ) ;
/* Graceful restart capability */
if (bgp_flag_check(peer->bgp, BGP_FLAG_GRACEFUL_RESTART))
{
state->can_g_restart = 1 ;
state->restart_time = peer->bgp->restart_time ;
}
else
{
state->can_g_restart = 0 ;
state->restart_time = 0 ;
} ;
/* TODO: check not restarting and not preserving forwarding state (?) */
state->can_preserve = 0 ; /* cannot preserve forwarding */
state->has_preserved = 0 ; /* has not preserved forwarding */
state->restarting = 0 ; /* is not restarting */
return state;
}
/*==============================================================================
* Unknown capabilities handling.
*
*/
/*------------------------------------------------------------------------------
* Add given unknown capability and its value to the given open_state.
*/
extern void
bgp_open_state_unknown_add(bgp_open_state state, uint8_t code,
void* value, bgp_size_t length)
{
bgp_cap_unknown unknown ;
unknown = XCALLOC(MTYPE_TMP, sizeof(struct bgp_cap_unknown) + length) ;
unknown->code = code ;
unknown->length = length ;
if (length != 0)
memcpy(unknown->value, value, length) ;
vector_push_item(&state->unknowns, unknown) ;
} ;
/*------------------------------------------------------------------------------
* Get count of number of unknown capabilities in given open_state.
*/
extern int
bgp_open_state_unknown_count(bgp_open_state state)
{
return vector_end(&state->unknowns) ;
} ;
/*------------------------------------------------------------------------------
* Get n'th unknown capability -- if exists.
*/
extern bgp_cap_unknown
bgp_open_state_unknown_cap(bgp_open_state state, unsigned index)
{
return vector_get_item(&state->unknowns, index) ;
} ;
/*==============================================================================
*
*/
/* Received an open, update the peer's state */
void
bgp_peer_open_state_receive(bgp_peer peer)
{
bgp_session session = peer->session;
bgp_open_state open_send = session->open_send;
bgp_open_state open_recv = session->open_recv;
int afi;
int safi;
/* Check neighbor as number. */
assert(open_recv->my_as == peer->as);
/* holdtime */
/* From the rfc: A reasonable maximum time between KEEPALIVE messages
would be one third of the Hold Time interval. KEEPALIVE messages
MUST NOT be sent more frequently than one per second. An
implementation MAY adjust the rate at which it sends KEEPALIVE
messages as a function of the Hold Time interval. */
peer->v_holdtime =
(open_recv->holdtime < open_send->holdtime)
? open_recv->holdtime
: open_send->holdtime;
peer->v_keepalive = peer->v_holdtime / 3;
/* TODO: update session state as well? */
session->hold_timer_interval = peer->v_holdtime ;
session->keepalive_timer_interval = peer->v_keepalive ;
/* Set remote router-id */
peer->remote_id.s_addr = open_recv->bgp_id;
/* AS4 */
if (open_recv->can_as4)
SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
/* AFI/SAFI */
/* Ignore capability when override-capability is set. */
if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
{
for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
{
qafx_bit_t qb = qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi));
if (qb & open_recv->can_mp_ext)
{
peer->afc_recv[afi][safi] = 1;
assert(peer->afc[afi][safi]);
peer->afc_nego[afi][safi] = 1;
}
}
}
/* Route refresh. */
if (open_recv->can_r_refresh & bgp_cap_form_old)
SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
else if (open_recv->can_r_refresh & bgp_cap_form_new)
SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
/* ORF */
for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
{
qafx_bit_t qb = qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi));
if (qb & open_recv->can_orf_prefix_send)
SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV);
if (qb & open_recv->can_orf_prefix_recv)
SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV);
}
/* ORF prefix. */
if (open_recv->can_orf_prefix_send)
{
if (open_recv->can_orf_prefix & bgp_cap_form_old)
SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_SM_OLD_RCV);
else if (open_recv->can_orf_prefix & bgp_cap_form_new)
SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_SM_RCV);
}
if (open_recv->can_orf_prefix_recv)
{
if (open_recv->can_orf_prefix & bgp_cap_form_old)
SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_RM_OLD_RCV);
else if (open_recv->can_orf_prefix & bgp_cap_form_new)
SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_RM_RCV);
}
/* Dynamic Capabilities */
if (open_recv->can_dynamic)
SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV);
/* Graceful restart */
for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
{
qafx_bit_t qb = qafx_bit(qafx_num_from_qAFI_qSAFI(afi, safi));
if (peer->afc[afi][safi] && (qb & open_recv->can_g_restart))
{
SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV);
if (qb & open_recv->has_preserved)
SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV);
}
}
peer->v_gr_restart = open_recv->restart_time;
/* TODO: should we do anything with this? */
#if 0
int restarting ; /* Restart State flag */
#endif
/* Override capability. */
if (!open_recv->can_capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
{
peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
}
}
|