Revision 72a6ef11

View differences:

proto/bgp/Makefile
1
source=bgp.c attrs.c
1
source=bgp.c attrs.c packets.c
2 2
root-rel=../../
3 3
dir-name=proto/bgp
4 4

  
proto/bgp/bgp.c
78 78
  tm_start(t, value);
79 79
}
80 80

  
81
static int
82
bgp_rx(sock *sk, int size)
83
{
84
  DBG("BGP: Got %d bytes\n", size);
85

  
86
  return 1;				/* Start from the beginning */
87
}
88

  
89 81
static void
90 82
bgp_send_open(struct bgp_conn *conn)
91 83
{
92 84
  DBG("BGP: Sending open\n");
93 85
  conn->sk->rx_hook = bgp_rx;
94 86
  tm_stop(conn->connect_retry_timer);
95
  /* FIXME */
87
  bgp_schedule_packet(conn, PKT_OPEN);
96 88
  conn->state = BS_OPENSENT;
97 89
}
98 90

  
......
174 166
  s->data = conn;
175 167
  s->ttl = p->cf->multihop ? : 1;
176 168
  s->rbsize = BGP_RX_BUFFER_SIZE;
177
#if 0
169
  s->tbsize = BGP_TX_BUFFER_SIZE;
178 170
  s->tx_hook = bgp_tx;
179
#endif
180 171
  s->err_hook = bgp_err;
181 172
  s->tos = IP_PREC_INTERNET_CONTROL;
182 173

  
183 174
  conn->bgp = p;
184 175
  conn->sk = s;
176
  conn->packets_to_send = 0;
185 177

  
186 178
  t = conn->connect_retry_timer = tm_new(p->p.pool);
187 179
  t->hook = bgp_connect_timeout;
proto/bgp/bgp.h
22 22
struct bgp_conn {
23 23
  struct bgp_proto *bgp;
24 24
  struct birdsock *sk;
25
  int state;				/* State of connection state machine */
25
  unsigned int state;			/* State of connection state machine */
26 26
  struct timer *connect_retry_timer;
27 27
  struct timer *hold_timer;
28 28
  struct timer *keepalive_timer;
29
  unsigned int packets_to_send;		/* Bitmap of packet types to be sent */
30
  unsigned int notify_code, notify_subcode, notify_arg, notify_arg_size;
31
  unsigned int error_flag;		/* Error state, ignore all input */
29 32
};
30 33

  
31 34
struct bgp_proto {
......
42 45
  struct object_lock *lock;		/* Lock for neighbor connection */
43 46
};
44 47

  
45
#define BGP_PORT 179
46
#define BGP_RX_BUFFER_SIZE 4096
48
#define BGP_PORT		179
49
#define BGP_VERSION		4
50
#define BGP_HEADER_LENGTH	19
51
#define BGP_MAX_PACKET_LENGTH	4096
52
#define BGP_RX_BUFFER_SIZE	4096
53
#define BGP_TX_BUFFER_SIZE	BGP_MAX_PACKET_LENGTH
47 54

  
48 55
void bgp_check(struct bgp_config *c);
49 56

  
......
51 58

  
52 59
/* packets.c */
53 60

  
61
void bgp_schedule_packet(struct bgp_conn *conn, int type);
62
void bgp_tx(struct birdsock *sk);
63
int bgp_rx(struct birdsock *sk, int size);
64

  
54 65
/* Packet types */
55 66

  
56 67
#define PKT_OPEN		0x01
57 68
#define PKT_UPDATE		0x02
58 69
#define PKT_NOTIFICATION	0x03
59 70
#define PKT_KEEPALIVE		0x04
71
#define PKT_SCHEDULE_CLOSE	0x1f	/* Used internally to schedule socket close */
60 72

  
61 73
/* Attributes */
62 74

  
proto/bgp/packets.c
6 6
 *	Can be freely distributed and used under the terms of the GNU GPL.
7 7
 */
8 8

  
9
#define LOCAL_DEBUG
10

  
9 11
#include "nest/bird.h"
10 12
#include "nest/iface.h"
11 13
#include "nest/protocol.h"
12 14
#include "nest/route.h"
13 15
#include "conf/conf.h"
16
#include "lib/unaligned.h"
17
#include "lib/socket.h"
14 18

  
15 19
#include "bgp.h"
20

  
21
static byte *
22
bgp_create_notification(struct bgp_conn *conn, byte *buf)
23
{
24
  DBG("BGP: Sending notification: code=%d, sub=%d, arg=%d:%d\n", conn->notify_code, conn->notify_subcode, conn->notify_arg, conn->notify_arg_size);
25
  buf[0] = conn->notify_code;
26
  buf[1] = conn->notify_subcode;
27
  switch (conn->notify_arg_size)
28
    {
29
    case 1:
30
      buf[2] = conn->notify_arg; return buf+3;
31
    case 2:
32
      put_u16(buf+2, conn->notify_arg); return buf+4;
33
    case 4:
34
      put_u32(buf+2, conn->notify_arg); return buf+6;
35
    default:
36
      bug("bgp_create_notification: unknown error code size");
37
    }
38
}
39

  
40
static byte *
41
bgp_create_open(struct bgp_conn *conn, byte *buf)
42
{
43
  DBG("BGP: Sending open\n");
44
  buf[0] = BGP_VERSION;
45
  put_u16(buf+1, conn->bgp->local_as);
46
  put_u16(buf+3, conn->bgp->cf->hold_time);
47
  put_u32(buf+5, conn->bgp->local_id);
48
  buf[9] = 0;				/* No optional parameters */
49
  return buf+10;
50
}
51

  
52
static byte *
53
bgp_create_update(struct bgp_conn *conn, byte *buf)
54
{
55
  DBG("BGP: Sending update\n");
56
  bug("Don't know how to create updates");
57
}
58

  
59
static void
60
bgp_create_header(byte *buf, unsigned int len, unsigned int type)
61
{
62
  memset(buf, 0xff, 16);		/* Marker */
63
  put_u16(buf+16, len);
64
  buf[18] = type;
65
}
66

  
67
static void
68
bgp_fire_tx(struct bgp_conn *conn)
69
{
70
  unsigned int s = conn->packets_to_send;
71
  sock *sk = conn->sk;
72
  byte *buf = sk->tbuf;
73
  byte *pkt = buf + BGP_HEADER_LENGTH;
74
  byte *end;
75
  int type;
76

  
77
  if (s & (1 << PKT_SCHEDULE_CLOSE))
78
    {
79
      conn->packets_to_send = 0;
80
      bug("Scheduled close");		/* FIXME */
81
    }
82
  if (s & (1 << PKT_NOTIFICATION))
83
    {
84
      s = 1 << PKT_SCHEDULE_CLOSE;
85
      type = PKT_NOTIFICATION;
86
      end = bgp_create_notification(conn, pkt);
87
    }
88
  else if (s & (1 << PKT_KEEPALIVE))
89
    {
90
      s &= ~(1 << PKT_KEEPALIVE);
91
      type = PKT_KEEPALIVE;
92
      end = pkt;			/* Keepalives carry no data */
93
      DBG("BGP: Sending keepalive\n");
94
    }
95
  else if (s & (1 << PKT_OPEN))
96
    {
97
      s &= ~(1 << PKT_OPEN);
98
      type = PKT_OPEN;
99
      end = bgp_create_open(conn, pkt);
100
    }
101
  else if (s & (1 << PKT_UPDATE))
102
    {
103
      end = bgp_create_update(conn, pkt);
104
      type = PKT_UPDATE;
105
      if (!end)
106
	{
107
	  conn->packets_to_send = 0;
108
	  return;
109
	}
110
    }
111
  else
112
    return;
113
  conn->packets_to_send = s;
114
  bgp_create_header(buf, end - buf, type);
115
  sk_send(sk, end - buf);
116
}
117

  
118
void
119
bgp_schedule_packet(struct bgp_conn *conn, int type)
120
{
121
  DBG("BGP: Scheduling packet type %d\n", type);
122
  conn->packets_to_send |= 1 << type;
123
  if (conn->sk->tpos != conn->sk->tbuf)
124
    bgp_fire_tx(conn);
125
}
126

  
127
void
128
bgp_tx(sock *sk)
129
{
130
  struct bgp_conn *conn = sk->data;
131

  
132
  DBG("BGP: TX hook\n");
133
  bgp_fire_tx(conn);
134
}
135

  
136
int
137
bgp_rx(sock *sk, int size)
138
{
139
  struct bgp_conn *conn = sk->data;
140
  byte *pkt_start = sk->rbuf;
141
  byte *end = pkt_start + size;
142

  
143
  DBG("BGP: RX hook: Got %d bytes\n", size);
144
  while (end >= pkt_start + BGP_HEADER_LENGTH)
145
    {
146
      if (conn->error_flag)
147
	return 1;
148
      bug("Incoming packets not handled"); /* FIXME */
149
    }
150
  if (pkt_start != sk->rbuf)
151
    {
152
      memmove(sk->rbuf, pkt_start, end - pkt_start);
153
      sk->rpos = sk->rbuf + (end - pkt_start);
154
    }
155
  return 0;
156
}

Also available in: Unified diff