summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_open_state.c
blob: 9ca9617e58f2d41d6a46c0731c65f2caed5c9aec (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* 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"

/*==============================================================================
 * 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.
 *
 */
extern 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 ;
}

/*------------------------------------------------------------------------------
 * Free bgp_open_state structure (if any)
 *
 * Returns NULL.
 */
extern bgp_open_state
bgp_open_state_free(bgp_open_state state)
{
  bgp_cap_unknown   unknown ;
  bgp_cap_afi_safi  afi_safi ;

  if (state != NULL)
    {
      while ((unknown = vector_ream_keep(&state->unknowns)) != NULL)
        XFREE(MTYPE_TMP, unknown) ;

      while ((afi_safi = vector_ream_keep(&state->afi_safi)) != NULL)
        XFREE(MTYPE_TMP, afi_safi) ;

      XFREE(MTYPE_BGP_OPEN_STATE, state) ;
    } ;

  return NULL ;
}

/*------------------------------------------------------------------------------
 * Unset pointer to open_state structure and free structure (if any).
 */
extern void
bgp_open_state_unset(bgp_open_state* p_state)
{
  bgp_open_state_free(*p_state) ;
  *p_state = NULL ;
} ;

/*------------------------------------------------------------------------------
 * Set pointer to open_state and unset source pointer
 *
 * Frees any existing open_state at the destination.
 *
 * NB: responsibility for the open_state structure passes to the destination.
 */
extern void
bgp_open_state_set_mov(bgp_open_state* p_dst, bgp_open_state* p_src)
{
  bgp_open_state_free(*p_dst) ;
  *p_dst = *p_src ;
  *p_src = 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 ;

  /* Whether to send capability or not                  */
  state->can_capability = ! CHECK_FLAG(peer->flags, PEER_FLAG_DONT_CAPABILITY) ;

  /* Announce self as AS4 speaker always                */
  if (!bm->as2_speaker)
    SET_FLAG(peer->cap, PEER_CAP_AS4_ADV) ;
  state->can_as4 = CHECK_FLAG(peer->cap, PEER_CAP_AS4_ADV) ? 1 : 0 ;

  state->my_as2 = (state->my_as > BGP_AS2_MAX ) ? BGP_ASN_TRANS
                                                : state->my_as ;

  /* 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 -- always                            */
  SET_FLAG(peer->cap, PEER_CAP_REFRESH_ADV) ;
  state->can_r_refresh = CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)
                                        ? (bgp_form_pre | bgp_form_rfc)
                                        : bgp_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_form_pre | bgp_form_rfc)
                                        : bgp_form_none  ;

  /* Dynamic Capabilities       TODO: check requirement */
  state->can_dynamic = ( CHECK_FLAG(peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)
                                                                        != 0 ) ;
  if (state->can_dynamic)
    SET_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV) ;

  /* Graceful restart capability                                            */
  if (bgp_flag_check(peer->bgp, BGP_FLAG_GRACEFUL_RESTART))
    {
      SET_FLAG(peer->cap, PEER_CAP_RESTART_ADV) ;
      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 has restarted and not preserving forwarding state (?)  */
  state->can_preserve    = 0 ;        /* cannot preserve forwarding     */
  state->has_preserved   = 0 ;        /* has not preserved forwarding   */
  state->has_restarted   = 0 ;        /* has not restarted              */

  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) ;
} ;

/*==============================================================================
 * Generic afi/safi capabilities handling.
 *
 */

/*------------------------------------------------------------------------------
 * Add given afi/safi capability and its value to the given open_state.
 */
extern bgp_cap_afi_safi
bgp_open_state_afi_safi_add(bgp_open_state state, iAFI_t afi, iSAFI_t safi,
                                                   bool known, uint8_t cap_code)
{
  bgp_cap_afi_safi afi_safi ;

  afi_safi = XCALLOC(MTYPE_TMP, sizeof(struct bgp_cap_afi_safi)) ;

  afi_safi->known_afi_safi   = known ;
  afi_safi->afi              = afi ;
  afi_safi->safi             = safi ;
  afi_safi->cap_code         = cap_code ;

  vector_push_item(&state->afi_safi, afi_safi) ;

  return afi_safi ;
} ;

/*------------------------------------------------------------------------------
 * Get count of number of afi/safi capabilities in given open_state.
 */
extern int
bgp_open_state_afi_safi_count(bgp_open_state state)
{
  return vector_end(&state->afi_safi) ;
} ;

/*------------------------------------------------------------------------------
 * Get n'th afi_safi capability -- if exists.
 */
extern bgp_cap_afi_safi
bgp_open_state_afi_safi_cap(bgp_open_state state, unsigned index)
{
  return vector_get_item(&state->afi_safi, index) ;
} ;

/*==============================================================================
 *
 */

/* Received an open, update the peer's state
 *
 * Takes the peer->session->open_recv and fills in:
 *
 *   peer->v_holdtime       ) per negotiated values
 *   peer->v_keepalive      )
 *
 *   peer->remote_id.s_addr
 *
 *   peer->cap              ) updated per open_recv -- assumes all recv flags
 *   peer->af_cap           ) have been cleared.
 *
 *   peer->v_gr_restart       set to value received (if any)
 *
 *   peer->afc_recv           set/cleared according to what is advertised
 *                            BUT if !open_recv->can_capability or
 *                                     neighbor override-capability, then
 *                                                        all flags are cleared.
 *
 *   peer->afc_nego           set/cleared according to what is advertised and
 *                            what is activated.
 *                            BUT if !open_recv->can_capability or
 *                                     neighbor override-capability, then
 *                                      set everything which has been activated.
 *
 *
 *
 * NB: for safety, best to have the session locked -- though won't, in fact,
 *     change any of this information after the session is established.
 */
void
bgp_peer_open_state_receive(bgp_peer peer)
{
  bgp_session    session   = peer->session;
  bgp_open_state open_recv = session->open_recv;
  qAFI_t  afi;
  qSAFI_t safi;
  qafx_bit_t qbs ;
  int recv ;

  /* Check neighbor as number. */
  assert(open_recv->my_as == peer->as);

  /* If had to suppress sending of capabilities, note that              */
  if (session->cap_suppress)
    SET_FLAG (peer->cap, PEER_CAP_SUPPRESSED) ;

  /* 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. */

  /* The BGP Engine sets the session's HoldTimer and KeepaliveTimer intervals
   * to the values negotiated when the OPEN messages were exchanged.
   *
   * Take copies of that information.
   */
  peer->v_holdtime  = session->hold_timer_interval ;
  peer->v_keepalive = session->keepalive_timer_interval ;

  /* 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 -- as received, or assumed or overridden                  */

  if (!open_recv->can_capability || session->cap_override)
    {
      /* There were no capabilities, or are OVERRIDING AFI/SAFI, so force
       * not having received any AFI/SAFI, but apply all known.
       */
      recv = 0 ;
      qbs  = qafx_known_bits ;
    }
  else
    {
      /* Use the AFI/SAFI received, if any.                             */
      recv = 1 ;
      qbs  = open_recv->can_mp_ext ;
    }

  for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
    for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
      {
        qafx_bit_t qb = qafx_bit_from_qAFI_qSAFI(afi, safi) ;
        if (qb & qbs)
          {
            peer->afc_recv[afi][safi] = recv ;
            peer->afc_nego[afi][safi] = peer->afc[afi][safi] ;
          }
        else
          {
            peer->afc_recv[afi][safi] = 0 ;
            peer->afc_nego[afi][safi] = 0 ;
          } ;
      } ;

  /* Route refresh. */
  if (open_recv->can_r_refresh & bgp_form_pre)
    SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
  else if (open_recv->can_r_refresh & bgp_form_rfc)
    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_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_form_pre)
        SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_SM_OLD_RCV);
      else if (open_recv->can_orf_prefix & bgp_form_rfc)
        SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_SM_RCV);
    }
  if (open_recv->can_orf_prefix_recv)
    {
      if (open_recv->can_orf_prefix & bgp_form_pre)
        SET_FLAG (peer->cap, PEER_CAP_ORF_PREFIX_RM_OLD_RCV);
      else if (open_recv->can_orf_prefix & bgp_form_rfc)
        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
   *
   * NB: appear not to care about open_recv->has_restarted !
   */
  if (open_recv->can_g_restart)
    SET_FLAG (peer->cap, PEER_CAP_RESTART_RCV) ;

  for (afi = qAFI_min ; afi <= qAFI_max ; ++afi)
     for (safi = qSAFI_min ; safi <= qSAFI_max ; ++safi)
       {
         qafx_bit_t qb = qafx_bit_from_qAFI_qSAFI(afi, safi);
         if (peer->afc[afi][safi] && (qb & open_recv->can_preserve))
           {
             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
}