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