clear cluster_master on election so that slaves will accept a new master
[l2tpns.git] / bgp.h
1 /* BGPv4 (RFC1771) */
2 /* $Id: bgp.h,v 1.4 2004/12/16 08:49:52 bodea Exp $ */
3
4 #ifndef __BGP_H__
5 #define __BGP_H__
6
7 #define BGP_MAX_PACKET_SIZE 4096
8 #define BGP_HOLD_TIME 180 /* seconds before peer times us out */
9 #define BGP_KEEPALIVE_TIME 60 /* seconds between messages */
10 #define BGP_STATE_TIME 60 /* state transition timeout in seconds */
11 #define BGP_MAX_RETRY 42 /* maximum number of times to retry */
12 #define BGP_RETRY_BACKOFF 60 /* number of seconds between retries,
13 cumulative */
14
15 #define BGP_METRIC 1 /* multi_exit_disc */
16 #define BGP_LOCAL_PREF 100 /* local preference value */
17
18 struct bgp_header {
19 char marker[16];
20 uint16_t len;
21 uint8_t type;
22 } __attribute__ ((packed));
23
24 /* bgp_header.type */
25 #define BGP_MSG_OPEN 1
26 #define BGP_MSG_UPDATE 2
27 #define BGP_MSG_NOTIFICATION 3
28 #define BGP_MSG_KEEPALIVE 4
29
30 struct bgp_packet {
31 struct bgp_header header;
32 char data[BGP_MAX_PACKET_SIZE - sizeof(struct bgp_header)]; /* variable */
33 } __attribute__ ((packed));
34
35 struct bgp_data_open {
36 uint8_t version;
37 #define BGP_VERSION 4
38 uint16_t as;
39 uint16_t hold_time;
40 uint32_t identifier;
41 uint8_t opt_len;
42 #define BGP_DATA_OPEN_SIZE 10 /* size of struct excluding opt_params */
43 char opt_params[sizeof(((struct bgp_packet *)0)->data) - BGP_DATA_OPEN_SIZE]; /* variable */
44 } __attribute__ ((packed));
45
46 struct bgp_ip_prefix {
47 uint8_t len;
48 uint32_t prefix; /* variable */
49 } __attribute__ ((packed));
50
51 #define BGP_IP_PREFIX_SIZE(p) (1 + ((p).len / 8) + ((p).len % 8 != 0))
52
53 struct bgp_path_attr {
54 uint8_t flags;
55 uint8_t code;
56 union {
57 struct {
58 uint8_t len;
59 char value[29]; /* semi-random size, adequate for l2tpns */
60 } __attribute__ ((packed)) s; /* short */
61 struct {
62 uint16_t len;
63 char value[28];
64 } __attribute__ ((packed)) e; /* extended */
65 } data; /* variable */
66 } __attribute__ ((packed));
67
68 /* bgp_path_attr.flags (bitfields) */
69 #define BGP_PATH_ATTR_FLAG_OPTIONAL (1 << 7)
70 #define BGP_PATH_ATTR_FLAG_TRANS (1 << 6)
71 #define BGP_PATH_ATTR_FLAG_PARTIAL (1 << 5)
72 #define BGP_PATH_ATTR_FLAG_EXTLEN (1 << 4)
73
74 /* bgp_path_attr.code, ...value */
75 #define BGP_PATH_ATTR_CODE_ORIGIN 1 /* well-known, mandatory */
76 # define BGP_PATH_ATTR_CODE_ORIGIN_IGP 0
77 # define BGP_PATH_ATTR_CODE_ORIGIN_EGP 1
78 # define BGP_PATH_ATTR_CODE_ORIGIN_INCOMPLETE 2
79 #define BGP_PATH_ATTR_CODE_AS_PATH 2 /* well-known, mandatory */
80 # define BGP_PATH_ATTR_CODE_AS_PATH_AS_SET 1
81 # define BGP_PATH_ATTR_CODE_AS_PATH_AS_SEQUENCE 2
82 #define BGP_PATH_ATTR_CODE_NEXT_HOP 3 /* well-known, mandatory */
83 #define BGP_PATH_ATTR_CODE_MULTI_EXIT_DISC 4 /* optional, non-transitive */
84 #define BGP_PATH_ATTR_CODE_LOCAL_PREF 5 /* well-known, discretionary */
85 #define BGP_PATH_ATTR_CODE_ATOMIC_AGGREGATE 6 /* well-known, discretionary */
86 #define BGP_PATH_ATTR_CODE_AGGREGATOR 7 /* optional, transitive */
87 #define BGP_PATH_ATTR_CODE_COMMUNITIES 8 /* optional, transitive (RFC1997) */
88
89 #define BGP_PATH_ATTR_SIZE(p) ((((p).flags & BGP_PATH_ATTR_FLAG_EXTLEN) \
90 ? ((p).data.e.len + 1) : (p).data.s.len) + 3)
91
92 /* well known COMMUNITIES */
93 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01 /* don't advertise outside confederation */
94 #define BGP_COMMUNITY_NO_ADVERTISE 0xffffff02 /* don't advertise to any peer */
95 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03 /* don't advertise to any other AS */
96
97 struct bgp_data_notification {
98 uint8_t error_code;
99 uint8_t error_subcode;
100 char data[sizeof(((struct bgp_packet *)0)->data) - 2]; /* variable */
101 } __attribute__ ((packed));
102
103 /* bgp_data_notification.error_code, .error_subcode */
104 #define BGP_ERR_HEADER 1
105 # define BGP_ERR_HDR_NOT_SYNC 1
106 # define BGP_ERR_HDR_BAD_LEN 2
107 # define BGP_ERR_HDR_BAD_TYPE 3
108 #define BGP_ERR_OPEN 2
109 # define BGP_ERR_OPN_VERSION 1
110 # define BGP_ERR_OPN_BAD_AS 2
111 # define BGP_ERR_OPN_BAD_IDENT 3
112 # define BGP_ERR_OPN_UNSUP_PARAM 4
113 # define BGP_ERR_OPN_AUTH_FAILURE 5
114 # define BGP_ERR_OPN_HOLD_TIME 6
115 #define BGP_ERR_UPDATE 3
116 # define BGP_ERR_UPD_BAD_ATTR_LIST 1
117 # define BGP_ERR_UPD_UNKN_WK_ATTR 2
118 # define BGP_ERR_UPD_MISS_WK_ATTR 3
119 # define BGP_ERR_UPD_BAD_ATTR_FLAG 4
120 # define BGP_ERR_UPD_BAD_ATTR_LEN 5
121 # define BGP_ERR_UPD_BAD_ORIGIN 6
122 # define BGP_ERR_UPD_ROUTING_LOOP 7
123 # define BGP_ERR_UPD_BAD_NEXT_HOP 8
124 # define BGP_ERR_UPD_BAD_OPT_ATTR 9
125 # define BGP_ERR_UPD_BAD_NETWORK 10
126 # define BGP_ERR_UPD_BAD_AS_PATH 11
127 #define BGP_ERR_HOLD_TIMER_EXP 4
128 #define BGP_ERR_FSM 5
129 #define BGP_ERR_CEASE 6
130
131 enum bgp_state {
132 Disabled, /* initial, or failed */
133 Idle, /* trying to connect */
134 Connect, /* connect issued */
135 Active, /* connected, waiting to send OPEN */
136 OpenSent, /* OPEN sent, waiting for peer OPEN */
137 OpenConfirm, /* KEEPALIVE sent, waiting for peer KEEPALIVE */
138 Established, /* established */
139 };
140
141 struct bgp_route_list {
142 struct bgp_ip_prefix dest;
143 struct bgp_route_list *next;
144 };
145
146 struct bgp_buf {
147 struct bgp_packet packet; /* BGP packet */
148 size_t done; /* bytes sent/recvd */
149 };
150
151 /* state */
152 struct bgp_peer {
153 char name[32]; /* peer name */
154 in_addr_t addr; /* peer address */
155 int as; /* AS number */
156 int sock;
157 enum bgp_state state; /* FSM state */
158 enum bgp_state next_state; /* next state after outbuf cleared */
159 time_t state_time; /* time of last state change */
160 time_t keepalive_time; /* time to send next keepalive */
161 time_t retry_time; /* time for connection retry */
162 int retry_count; /* connection retry count */
163 int init_keepalive; /* initial keepalive time */
164 int init_hold; /* initial hold time */
165 int keepalive; /* negotiated keepalive time */
166 int hold; /* negotiated hold time */
167 time_t expire_time; /* time next peer packet expected */
168 int routing; /* propagate routes */
169 int update_routes; /* UPDATE required */
170 struct bgp_route_list *routes; /* routes known by this peer */
171 struct bgp_buf *outbuf; /* pending output */
172 struct bgp_buf *inbuf; /* pending input */
173 int cli_flag; /* updates requested from CLI */
174 char *path_attrs; /* path attrs to send in UPDATE message */
175 int path_attr_len; /* length of path attrs */
176 };
177
178 /* bgp_peer.cli_flag */
179 #define BGP_CLI_SUSPEND 1
180 #define BGP_CLI_ENABLE 2
181 #define BGP_CLI_RESTART 3
182
183 extern struct bgp_peer *bgp_peers;
184 extern int bgp_configured;
185
186 /* actions */
187 int bgp_setup(int as);
188 int bgp_start(struct bgp_peer *peer, char *name, int as, int keepalive,
189 int hold, int enable);
190
191 void bgp_stop(struct bgp_peer *peer);
192 void bgp_halt(struct bgp_peer *peer);
193 int bgp_restart(struct bgp_peer *peer);
194 int bgp_add_route(in_addr_t ip, in_addr_t mask);
195 int bgp_del_route(in_addr_t ip, in_addr_t mask);
196 void bgp_enable_routing(int enable);
197 int bgp_select_state(struct bgp_peer *peer);
198 int bgp_process(struct bgp_peer *peer, int readable, int writable);
199 char const *bgp_state_str(enum bgp_state state);
200
201 extern char const *cvs_id_bgp;
202
203 #endif /* __BGP_H__ */