summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_connection.h
blob: 4df1fbca411a5386561866755f6dd4ebc11e05a0 (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
/* BGP Connection Handling -- header
 * 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.
 */

#ifndef _QUAGGA_BGP_CONNECTION_H
#define _QUAGGA_BGP_CONNECTION_H

#include "lib/misc.h"

#include "lib/mqueue.h"
#include "lib/qpthreads.h"
#include "lib/qtimers.h"
#include "lib/qpselect.h"

#include "lib/sockunion.h"
#include "lib/stream.h"

//#include "bgpd/bgp.h"

#include "bgpd/bgp_common.h"
#include "bgpd/bgp_session.h"
#include "bgpd/bgp_open_state.h"
#include "bgpd/bgp_notification.h"
#include "bgpd/bgp_msg_read.h"

/*==============================================================================
 * The BGP Finite State Machine: states and events
 *
 */

typedef enum bgp_fsm_states bgp_fsm_state_t ;
enum bgp_fsm_states
{
  bgp_fsm_first_state     = 0,

  bgp_fsm_sInitial        = 0,  /* extra: connection initialised            */

  bgp_fsm_sIdle           = 1,  /* waiting for Idle Hold time               */
  bgp_fsm_sConnect        = 2,  /* waiting for connect (may be listening)   */
  bgp_fsm_sActive         = 3,  /* listening only                           */
  bgp_fsm_sOpenSent       = 4,  /* sent Open -- awaits Open                 */
  bgp_fsm_sOpenConfirm    = 5,  /* sent & received Open -- awaits keepalive */
  bgp_fsm_sEstablished    = 6,  /* running connection                       */

  bgp_fsm_sStopping       = 7,  /* extra: connection shutting down          */

  bgp_fsm_last_state      = 7,
} ;

typedef enum bgp_fsm_events bgp_fsm_event_t ;
enum bgp_fsm_events
{
  bgp_fsm_null_event                      =  0,

  bgp_fsm_eBGP_Start                      =  1,
  bgp_fsm_eBGP_Stop                       =  2,
  bgp_fsm_eTCP_connection_open            =  3,
  bgp_fsm_eTCP_connection_closed          =  4,
  bgp_fsm_eTCP_connection_open_failed     =  5,
  bgp_fsm_eTCP_fatal_error                =  6,
  bgp_fsm_eConnectRetry_timer_expired     =  7,
  bgp_fsm_eHold_Timer_expired             =  8,
  bgp_fsm_eKeepAlive_timer_expired        =  9,
  bgp_fsm_eReceive_OPEN_message           = 10,
  bgp_fsm_eReceive_KEEPALIVE_message      = 11,
  bgp_fsm_eReceive_UPDATE_message         = 12,
  bgp_fsm_eReceive_NOTIFICATION_message   = 13,
  bgp_fsm_eSent_NOTIFICATION_message      = 14,

  bgp_fsm_last_event                      = 15,
} ;

/*==============================================================================
 * BGP Connection Structures
 *
 *------------------------------------------------------------------------------
 * Write buffer for connection.
 *
 *  NB: when connection is initialised all the pointers are set NULL.
 *
 *      The buffer is not allocated until the TCP connection comes up.
 *
 *  NB: p_out == p_in => buffer is empty
 *
 *      BUT: p_out == limit => buffer is not writable.
 *
 *      When connection is first initialised all pointers are NULL, so the
 *      buffer is "empty but not writable".
 *
 *      When connection is opened, closed or fails, buffer is set into this
 *      "empty but not writable" state.
 */
typedef struct bgp_wbuffer* bgp_wbuffer ;
struct bgp_wbuffer
{
  uint8_t*    p_out ;
  uint8_t*    p_in ;

  uint8_t*    base ;
  uint8_t*    limit ;
} ;

/* Buffer is allocated for a number of maximum size BGP messages.       */
enum { bgp_wbuff_size = BGP_MSG_MAX_L * 10 } ;

/*==============================================================================
 * BGP Connection Structure
 *
 * The BGP Connection is the main data structure for the BGP Engine.
 *
 * When a session terminates, or a connection is shut it may have a short
 * independent life, if a NOTIFICATION message is pending.
 */
struct bgp_connection
{
  struct dl_list_pair(bgp_connection) exist ;
                                        /* list of existing connections   */

  bgp_session       session ;           /* session connection belongs to  */
                                        /* NULL if connection stopping    */
  qpt_mutex         p_mutex ;           /* session mutex*                 */
                                        /* (avoids incomplete type issue) */
  unsigned          lock_count ;        /* session mutex lock count       */

  bgp_connection_ord_t ordinal ;        /* primary/secondary connection   */
  bool              accepted ;          /* came via accept()              */

  bgp_fsm_state_t   state ;             /* FSM state of connection        */
  bool              comatose ;          /* Idle and no timer set          */
  bool              half_open ;         /* Idle but accepted connection   */

  bgp_connection    next ;              /* for the connection queue       */
  bgp_connection    prev ;              /* NULL <=> not on the queue      */

  int               fsm_active ;        /* active in FSM counter          */
  bgp_fsm_event_t   follow_on ;         /* event raised within FSM        */

  bgp_session_event_t exception;        /* exception posted here          */
  bgp_notify        notification ;      /* if any sent/received           */
  int               err ;               /* erno, if any                   */

  bool              cap_suppress ;      /* capability send suppress
                                           always set false when connection
                                           initialised.  Set if get
                                           NOTIFICATION that other end does
                                           not do capabilities.  Copied to
                                           session when established.      */

  bgp_open_state    open_recv ;         /* the open received.             */

  qps_file          qf ;                /* qpselect file structure        */
  bool              gtsm ;              /* minttl has been set            */

  union sockunion*  su_local ;          /* address of the near end        */
  union sockunion*  su_remote ;         /* address of the far end         */

  char*             host ;              /* peer "name" + Connect/Listen   */
  struct zlog*      log ;               /* where to log to                */

  unsigned  hold_timer_interval ;       /* subject to negotiation         */
  unsigned  keepalive_timer_interval ;  /* subject to negotiation         */

  bool              as4 ;               /* subject to negotiation         */
  bgp_form_t        route_refresh ;     /* subject to negotiation         */
  bgp_form_t        orf_prefix ;        /* subject to negotiation         */

  qtimer            hold_timer ;
  qtimer            keepalive_timer ;

  struct stream*    ibuf ;              /* a single input "stream"        */
  unsigned          read_pending ;      /* how much input waiting for     */

  bool              read_header ;       /* reading message header         */
  uint8_t           msg_type ;          /* copy of message type           */
  bgp_size_t        msg_body_size ;     /* size of message *body*         */
  bgp_msg_handler*  msg_func ;          /* function to handle message     */

  struct stream*    obuf ;              /* a single output "stream"       */

  int          notification_pending ;   /* waiting to write NOTIFICATION  */

  struct mqueue_local_queue
                    pending_queue ;     /* pending write messages         */

  struct bgp_wbuffer wbuff ;            /* write buffer                   */
} ;

/*==============================================================================
 * The functions
 */

extern bgp_connection bgp_connection_init_new(bgp_connection connection,
                            bgp_session session, bgp_connection_ord_t ordinal) ;
extern void bgp_connection_open(bgp_connection connection, int sock_fd) ;
extern void bgp_connection_start(bgp_connection connection, sockunion su_local,
                                                          sockunion su_remote) ;
extern void bgp_connection_enable_accept(bgp_connection connection) ;
extern void bgp_connection_disable_accept(bgp_connection connection) ;
extern bgp_connection bgp_connection_query_accept(bgp_session session) ;
extern bgp_connection bgp_connection_get_sibling(bgp_connection connection) ;
extern void bgp_connection_make_primary(bgp_connection connection) ;
extern void bgp_connection_close(bgp_connection connection, bool keep_timers) ;
extern bool bgp_connection_part_close(bgp_connection connection) ;
extern void bgp_connection_exit(bgp_connection connection) ;
extern void bgp_connection_read_enable(bgp_connection connection) ;
extern int bgp_connection_write(bgp_connection connection, struct stream* s) ;
extern void bgp_connection_queue_add(bgp_connection connection) ;
extern void bgp_connection_queue_del(bgp_connection connection) ;
extern int bgp_connection_queue_process(void) ;

Inline bool
bgp_connection_no_pending(bgp_connection connection, bgp_connection* is_pending)
{
  return (   (mqueue_local_head(&connection->pending_queue) == NULL)
          || (*is_pending != NULL) ) ;
} ;

extern void bgp_connection_add_pending(bgp_connection connection,
                                 mqueue_block mqb, bgp_connection* is_pending) ;

/*------------------------------------------------------------------------------
 * Set buffer *unwritable* (buffer appears full, but nothing pending).
 */
Inline void
bgp_write_buffer_unwritable(bgp_wbuffer wb)
{
  wb->p_in = wb->p_out = wb->limit ;
} ;

/*------------------------------------------------------------------------------
 * If allocated:   set buffer empty
 * If unallocated: buffer remains *unwritable*
 */
Inline void
bgp_write_buffer_reset(bgp_wbuffer wb)
{
  wb->p_in = wb->p_out = wb->base ;
} ;

/*------------------------------------------------------------------------------
 * See if do NOT have enough room for what want to write PLUS 1.
 *
 * NB: there is never any room in an unallocated buffer.
 */
Inline bool
bgp_write_buffer_cannot(bgp_wbuffer wb, size_t want)
{
  return ((size_t)(wb->limit - wb->p_in) <= want) ;
} ;

/*------------------------------------------------------------------------------
 * Full if NOT enough room for a maximum size BGP message + 1
 *
 * NB: there is never any room in an unallocated buffer.
 */
enum { bgp_write_buffer_full_threshold = BGP_MSG_MAX_L + 1 } ;

Inline bool
bgp_write_buffer_cannot_max(bgp_wbuffer wb)
{
  return bgp_write_buffer_cannot(wb, BGP_MSG_MAX_L) ;
} ;

/*------------------------------------------------------------------------------
 * See if buffer has anything in it.
 *
 * If empty, ensures that the buffer has been allocated, and sets the pointers
 * to the start of the buffer -- so all set to go.
 */
Inline bool
bgp_write_buffer_empty(bgp_wbuffer wb)
{
  if (wb->p_out < wb->p_in)
    return false ;              /* not empty => has buffer      */

  dassert(wb->p_out == wb->p_in) ;

  passert(wb->base != NULL) ;   /* must have buffer             */

  bgp_write_buffer_reset(wb) ;  /* pointers to start of buffer  */

  return true ;                 /* empty and all ready to go    */
} ;

/*------------------------------------------------------------------------------
 * Return how much the write buffer still has to write.
 *
 * NB: if returns 0, may not yet have been allocated.
 *
 *     > 0 => allocated.
 */
Inline int
bgp_write_buffer_has(bgp_wbuffer wb)
{
  dassert(wb->p_out <= wb->p_in) ;
  return (wb->p_in - wb->p_out) ;
} ;

/*------------------------------------------------------------------------------
 * As above, for connection
 */
Inline bool
bgp_connection_write_cannot_max(bgp_connection connection)
{
  return bgp_write_buffer_cannot_max(&connection->wbuff) ;
} ;

/*------------------------------------------------------------------------------
 * As above, for connection
 */
Inline bool
bgp_connection_write_empty(bgp_connection connection)
{
  return bgp_write_buffer_empty(&connection->wbuff) ;
} ;

/*==============================================================================
 * Locking the session associated with the connection.
 *
 * This is slightly complicated by the fact that when the connection is in
 * sStopping, it is no longer attached to the session.
 *
 * To facilitate that, the connection maintains its own "recursive" count, so
 * that when the connection is cut loose from the session, the session's mutex
 * can be released.
 *
 * Further -- when the connection is cut loose, a big number is added to the
 * count, so when the session is "unlocked" nothing will happen !
 *
 * Also -- this mechanism means that the session lock can be called even after
 * the connection has been cut loose, without requiring any other tests.
 */

Inline void
BGP_CONNECTION_SESSION_LOCK(bgp_connection connection)
{
  if (connection->lock_count++ == 0)
    qpt_mutex_lock(connection->p_mutex) ;
} ;

Inline void
BGP_CONNECTION_SESSION_UNLOCK(bgp_connection connection)
{
  if (--connection->lock_count == 0)
    qpt_mutex_unlock(connection->p_mutex) ;
} ;

extern void
BGP_CONNECTION_SESSION_CUT_LOOSE(bgp_connection connection) ;

#endif /* QUAGGA_BGP_CONNECTION_H */