--- /dev/null
+From 234e5c8d5a35339fb319affb952581bf5abb48a7 Mon Sep 17 00:00:00 2001
+From: Dinesh G Dutt <ddutt@cumulusnetworks.com>
+Date: Sun, 1 Feb 2015 00:56:12 -0800
+Subject: [PATCH] bgpd: Only use routes from Established peers for best path
+ selection
+
+Ensure that routes from a peer are not considered for best path
+comparison if the peer is not in an Established state. There can
+be a window between a peer being deleted and the background
+thread that actually clears the routes (marks them as "removed")
+runs during which best path may run. If this path selection
+compared two prefixes all the way down to peer IP addresses and
+one of these two peers had just been deleted, that peer would
+not have its sockunion structures, especially su_remote, resulting
+in a BGPD exception.
+
+Signed-off-by: Vivek Venkatraman <vivek@cumulusnetworks.com>
+---
+ bgpd/bgp_route.c | 14 ++++++++++++++
+ 1 file changed, 14 insertions(+)
+
+diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
+index 648dc9c..02c926f 100644
+--- a/bgpd/bgp_route.c
++++ b/bgpd/bgp_route.c
+@@ -1345,6 +1345,9 @@ bgp_best_selection (struct bgp *bgp, struct bgp_node *rn,
+ continue;
+ if (BGP_INFO_HOLDDOWN (ri1))
+ continue;
++ if (ri1->peer && ri1->peer != bgp->peer_self)
++ if (ri1->peer->status != Established)
++ continue;
+
+ new_select = ri1;
+ if (do_mpath)
+@@ -1357,6 +1360,11 @@ bgp_best_selection (struct bgp *bgp, struct bgp_node *rn,
+ continue;
+ if (BGP_INFO_HOLDDOWN (ri2))
+ continue;
++ if (ri2->peer &&
++ ri2->peer != bgp->peer_self &&
++ !CHECK_FLAG (ri2->peer->sflags, PEER_STATUS_NSF_WAIT))
++ if (ri2->peer->status != Established)
++ continue;
+
+ if (aspath_cmp_left (ri1->attr->aspath, ri2->attr->aspath)
+ || aspath_cmp_left_confed (ri1->attr->aspath,
+@@ -1408,6 +1416,12 @@ bgp_best_selection (struct bgp *bgp, struct bgp_node *rn,
+ continue;
+ }
+
++ if (ri->peer &&
++ ri->peer != bgp->peer_self &&
++ !CHECK_FLAG (ri->peer->sflags, PEER_STATUS_NSF_WAIT))
++ if (ri->peer->status != Established)
++ continue;
++
+ if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED)
+ && (! CHECK_FLAG (ri->flags, BGP_INFO_DMED_SELECTED)))
+ {
+--
+2.7.0
+