Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bgp / packets.c @ 72a6ef11

History | View | Annotate | Download (3.43 KB)

1
/*
2
 *        BIRD -- BGP Packet Processing
3
 *
4
 *        (c) 2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#define LOCAL_DEBUG
10

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

    
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
}