Rejigger BGP timeouts
[dnsseed-rust] / src / bgp_client.rs
1 use std::sync::{Arc, Mutex};
2 use std::sync::atomic::{AtomicBool, Ordering};
3 use std::cmp;
4 use std::ops::Bound::Included;
5 use std::collections::BTreeMap;
6 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
7 use std::time::{Duration, Instant};
8
9 use bgp_rs::{AFI, SAFI, AddPathDirection, Open, OpenCapability, OpenParameter, NLRIEncoding, PathAttribute};
10 use bgp_rs::Capabilities;
11 use bgp_rs::Segment;
12 use bgp_rs::Message;
13 use bgp_rs::Reader;
14
15 use tokio::prelude::*;
16 use tokio::codec;
17 use tokio::codec::Framed;
18 use tokio::net::TcpStream;
19 use tokio::timer::Delay;
20
21 use futures::sync::mpsc;
22
23 use crate::printer::{Printer, Stat};
24 use crate::timeout_stream::TimeoutStream;
25
26 struct Route {
27         path: Vec<u32>,
28         pref: u32,
29         med: u32,
30 }
31
32 struct RoutingTable {
33         v4_table: BTreeMap<(Ipv4Addr, u8, u32), Arc<Route>>,
34         v6_table: BTreeMap<(Ipv6Addr, u8, u32), Arc<Route>>,
35 }
36
37 impl RoutingTable {
38         fn new() -> Self {
39                 Self {
40                         v4_table: BTreeMap::new(),
41                         v6_table: BTreeMap::new(),
42                 }
43         }
44
45         fn get_route_attrs(&self, ip: IpAddr) -> Vec<Arc<Route>> {
46                 macro_rules! lookup_res {
47                         ($addrty: ty, $addr: expr, $table: expr, $addr_bits: expr) => { {
48                                 let mut res = Vec::new();
49                                 //TODO: Optimize this!
50                                 for i in (0..$addr_bits).rev() {
51                                         let mut lookup = $addr.octets();
52                                         for b in 0..(i / 8) {
53                                                 lookup[lookup.len() - b - 1] = 0;
54                                         }
55                                         lookup[lookup.len() - (i/8) - 1] &= !(((1u16 << (i % 8)) - 1) as u8);
56                                         let lookup_addr = <$addrty>::from(lookup);
57                                         for attrs in $table.range((Included((lookup_addr, $addr_bits - i as u8, 0)), Included((lookup_addr, $addr_bits - i as u8, std::u32::MAX)))) {
58                                                 res.push(Arc::clone(&attrs.1));
59                                         }
60                                         if !res.is_empty() { break; }
61                                 }
62                                 res
63                         } }
64                 }
65                 match ip {
66                         IpAddr::V4(v4a) => lookup_res!(Ipv4Addr, v4a, self.v4_table, 32),
67                         IpAddr::V6(v6a) => lookup_res!(Ipv6Addr, v6a, self.v6_table, 128)
68                 }
69         }
70
71         fn withdraw(&mut self, route: NLRIEncoding) {
72                 match route {
73                         NLRIEncoding::IP(p) => {
74                                 let (ip, len) = <(IpAddr, u8)>::from(&p);
75                                 match ip {
76                                         IpAddr::V4(v4a) => self.v4_table.remove(&(v4a, len, 0)),
77                                         IpAddr::V6(v6a) => self.v6_table.remove(&(v6a, len, 0)),
78                                 }
79                         },
80                         NLRIEncoding::IP_WITH_PATH_ID((p, id)) => {
81                                 let (ip, len) = <(IpAddr, u8)>::from(&p);
82                                 match ip {
83                                         IpAddr::V4(v4a) => self.v4_table.remove(&(v4a, len, id)),
84                                         IpAddr::V6(v6a) => self.v6_table.remove(&(v6a, len, id)),
85                                 }
86                         },
87                         NLRIEncoding::IP_MPLS(_) => None,
88                 };
89         }
90
91         fn announce(&mut self, prefix: NLRIEncoding, route: Arc<Route>) {
92                 match prefix {
93                         NLRIEncoding::IP(p) => {
94                                 let (ip, len) = <(IpAddr, u8)>::from(&p);
95                                 match ip {
96                                         IpAddr::V4(v4a) => self.v4_table.insert((v4a, len, 0), route),
97                                         IpAddr::V6(v6a) => self.v6_table.insert((v6a, len, 0), route),
98                                 }
99                         },
100                         NLRIEncoding::IP_WITH_PATH_ID((p, id)) => {
101                                 let (ip, len) = <(IpAddr, u8)>::from(&p);
102                                 match ip {
103                                         IpAddr::V4(v4a) => self.v4_table.insert((v4a, len, id), route),
104                                         IpAddr::V6(v6a) => self.v6_table.insert((v6a, len, id), route),
105                                 }
106                         },
107                         NLRIEncoding::IP_MPLS(_) => None,
108                 };
109         }
110 }
111
112 struct BytesCoder<'a>(&'a mut bytes::BytesMut);
113 impl<'a> std::io::Write for BytesCoder<'a> {
114         fn write(&mut self, b: &[u8]) -> Result<usize, std::io::Error> {
115                 self.0.extend_from_slice(&b);
116                 Ok(b.len())
117         }
118         fn flush(&mut self) -> Result<(), std::io::Error> {
119                 Ok(())
120         }
121 }
122 struct BytesDecoder<'a> {
123         buf: &'a mut bytes::BytesMut,
124         pos: usize,
125 }
126 impl<'a> std::io::Read for BytesDecoder<'a> {
127         fn read(&mut self, b: &mut [u8]) -> Result<usize, std::io::Error> {
128                 let copy_len = cmp::min(b.len(), self.buf.len() - self.pos);
129                 b[..copy_len].copy_from_slice(&self.buf[self.pos..self.pos + copy_len]);
130                 self.pos += copy_len;
131                 Ok(copy_len)
132         }
133 }
134
135 struct MsgCoder<'a>(&'a Printer);
136 impl<'a> codec::Decoder for MsgCoder<'a> {
137         type Item = Message;
138         type Error = std::io::Error;
139
140         fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<Message>, std::io::Error> {
141                 let mut decoder = BytesDecoder {
142                         buf: bytes,
143                         pos: 0
144                 };
145                 match (Reader {
146                         stream: &mut decoder,
147                         capabilities: Capabilities {
148                                 FOUR_OCTET_ASN_SUPPORT: true,
149                                 EXTENDED_PATH_NLRI_SUPPORT: true,
150                         }
151                 }).read() {
152                         Ok((_header, msg)) => {
153                                 decoder.buf.advance(decoder.pos);
154                                 Ok(Some(msg))
155                         },
156                         Err(e) => match e.kind() {
157                                 std::io::ErrorKind::UnexpectedEof => Ok(None),
158                                 _ => Err(e),
159                         },
160                 }
161         }
162 }
163 impl<'a> codec::Encoder for MsgCoder<'a> {
164         type Item = Message;
165         type Error = std::io::Error;
166
167         fn encode(&mut self, msg: Message, res: &mut bytes::BytesMut) -> Result<(), std::io::Error> {
168                 msg.write(&mut BytesCoder(res))?;
169                 Ok(())
170         }
171 }
172
173 pub struct BGPClient {
174         routes: Mutex<RoutingTable>,
175         shutdown: AtomicBool,
176 }
177 impl BGPClient {
178         pub fn get_asn(&self, addr: IpAddr) -> u32 {
179                 let mut path_vecs = self.routes.lock().unwrap().get_route_attrs(addr).clone();
180                 if path_vecs.is_empty() { return 0; }
181
182                 path_vecs.sort_unstable_by(|path_a, path_b| {
183                         path_a.pref.cmp(&path_b.pref)
184                                 .then(path_b.path.len().cmp(&path_a.path.len()))
185                                 .then(path_b.med.cmp(&path_a.med))
186                 });
187
188                 let primary_route = path_vecs.pop().unwrap();
189                 'asn_candidates: for asn in primary_route.path.iter().rev() {
190                         for secondary_route in path_vecs.iter() {
191                                 if !secondary_route.path.contains(asn) {
192                                         continue 'asn_candidates;
193                                 }
194                         }
195                         return *asn;
196                 }
197                 *primary_route.path.last().unwrap_or(&0)
198         }
199
200         pub fn disconnect(&self) {
201                 self.shutdown.store(true, Ordering::Relaxed);
202         }
203
204         fn map_attrs(mut attrs: Vec<PathAttribute>) -> Option<Route> {
205                 let mut as4_path = None;
206                 let mut as_path = None;
207                 let mut pref = 100;
208                 let mut med = 0;
209                 for attr in attrs.drain(..) {
210                         match attr {
211                                 PathAttribute::AS4_PATH(path) => as4_path = Some(path),
212                                 PathAttribute::AS_PATH(path) => as_path = Some(path),
213                                 PathAttribute::LOCAL_PREF(p) => pref = p,
214                                 PathAttribute::MULTI_EXIT_DISC(m) => med = m,
215                                 _ => {},
216                         }
217                 }
218                 if let Some(mut aspath) = as4_path.or(as_path) {
219                         let mut path = Vec::new();
220                         for seg in aspath.segments.drain(..) {
221                                 match seg {
222                                         Segment::AS_SEQUENCE(mut asn) => path.append(&mut asn),
223                                         Segment::AS_SET(_) => {}, // Ignore sets for now, they're not that common anyway
224                                 }
225                         }
226                         return Some(Route {
227                                 path: path.clone(),
228                                 pref,
229                                 med,
230                         })
231                 } else { None }
232         }
233
234         fn connect_given_client(addr: SocketAddr, timeout: Duration, printer: &'static Printer, client: Arc<BGPClient>) {
235                 tokio::spawn(Delay::new(Instant::now() + timeout / 4).then(move |_| {
236                         let connect_timeout = Delay::new(Instant::now() + timeout.clone()).then(|_| {
237                                 future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
238                         });
239                         let client_reconn = Arc::clone(&client);
240                         TcpStream::connect(&addr).select(connect_timeout)
241                                 .or_else(move |_| {
242                                         Delay::new(Instant::now() + timeout / 2).then(|_| {
243                                                 future::err(())
244                                         })
245                                 }).and_then(move |stream| {
246                                         let (write, read) = Framed::new(stream.0, MsgCoder(printer)).split();
247                                         let (mut sender, receiver) = mpsc::channel(10); // We never really should send more than 10 messages unless they're dumb
248                                         tokio::spawn(write.sink_map_err(|_| { () }).send_all(receiver)
249                                                 .then(|_| {
250                                                         future::err(())
251                                                 }));
252                                         let _ = sender.try_send(Message::Open(Open {
253                                                 version: 4,
254                                                 peer_asn: 23456,
255                                                 hold_timer: timeout.as_secs() as u16,
256                                                 identifier: 0x453b1215, // 69.59.18.21
257                                                 parameters: vec![OpenParameter::Capabilities(vec![
258                                                         OpenCapability::MultiProtocol((AFI::IPV4, SAFI::Unicast)),
259                                                         OpenCapability::MultiProtocol((AFI::IPV6, SAFI::Unicast)),
260                                                         OpenCapability::FourByteASN(397444),
261                                                         OpenCapability::RouteRefresh,
262                                                         OpenCapability::AddPath(vec![
263                                                                 (AFI::IPV4, SAFI::Unicast, AddPathDirection::ReceivePaths),
264                                                                 (AFI::IPV6, SAFI::Unicast, AddPathDirection::ReceivePaths)]),
265                                                 ])]
266                                         }));
267                                         TimeoutStream::new_persistent(read, timeout).for_each(move |bgp_msg| {
268                                                 if client.shutdown.load(Ordering::Relaxed) {
269                                                         return future::err(std::io::Error::new(std::io::ErrorKind::Other, "Shutting Down"));
270                                                 }
271                                                 match bgp_msg {
272                                                         Message::Open(_) => {
273                                                                 client.routes.lock().unwrap().v4_table.clear();
274                                                                 client.routes.lock().unwrap().v6_table.clear();
275                                                                 printer.add_line("Connected to BGP route provider".to_string(), false);
276                                                         },
277                                                         Message::KeepAlive => {
278                                                                 let _ = sender.try_send(Message::KeepAlive);
279                                                         },
280                                                         Message::Update(mut upd) => {
281                                                                 upd.normalize();
282                                                                 let mut route_table = client.routes.lock().unwrap();
283                                                                 for r in upd.withdrawn_routes {
284                                                                         route_table.withdraw(r);
285                                                                 }
286                                                                 if let Some(path) = Self::map_attrs(upd.attributes) {
287                                                                         let path_arc = Arc::new(path);
288                                                                         for r in upd.announced_routes {
289                                                                                 route_table.announce(r, Arc::clone(&path_arc));
290                                                                         }
291                                                                 }
292                                                                 printer.set_stat(Stat::V4RoutingTableSize(route_table.v4_table.len()));
293                                                                 printer.set_stat(Stat::V6RoutingTableSize(route_table.v6_table.len()));
294                                                         },
295                                                         _ => {}
296                                                 }
297                                                 future::ok(())
298                                         }).or_else(move |e| {
299                                                 printer.add_line(format!("Got error from BGP stream: {:?}", e), true);
300                                                 future::ok(())
301                                         })
302                                 }).then(move |_| {
303                                         if !client_reconn.shutdown.load(Ordering::Relaxed) {
304                                                 BGPClient::connect_given_client(addr, timeout, printer, client_reconn);
305                                         }
306                                         future::ok(())
307                                 })
308                         })
309                 );
310         }
311
312         pub fn new(addr: SocketAddr, timeout: Duration, printer: &'static Printer) -> Arc<BGPClient> {
313                 let client = Arc::new(BGPClient {
314                         routes: Mutex::new(RoutingTable::new()),
315                         shutdown: AtomicBool::new(false),
316                 });
317                 BGPClient::connect_given_client(addr, timeout, printer, Arc::clone(&client));
318                 client
319         }
320 }