#include "network.h"
#include "log.h"
#include "memory.h"
+#include "hash.h"
+#include "jhash.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
return 0;
}
+
+/* BGP own address structure */
+struct bgp_addr
+{
+ struct in_addr addr;
+ int refcnt;
+};
+
+static struct hash *bgp_address_hash;
+
+static void *
+bgp_address_hash_alloc (void *p)
+{
+ struct in_addr *val = p;
+ struct bgp_addr *addr;
+
+ addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
+ addr->refcnt = 0;
+ addr->addr.s_addr = val->s_addr;
+
+ return addr;
+}
+
+static unsigned int
+bgp_address_hash_key_make (void *p)
+{
+ const struct bgp_addr *addr = p;
+
+ return jhash_1word(addr->addr.s_addr, 0);
+}
+
+static int
+bgp_address_hash_cmp (const void *p1, const void *p2)
+{
+ const struct bgp_addr *addr1 = p1;
+ const struct bgp_addr *addr2 = p2;
+
+ return addr1->addr.s_addr == addr2->addr.s_addr;
+}
+
+void
+bgp_address_init (void)
+{
+ bgp_address_hash = hash_create (bgp_address_hash_key_make,
+ bgp_address_hash_cmp);
+}
+
+static void
+bgp_address_add (struct prefix *p)
+{
+ struct bgp_addr tmp;
+ struct bgp_addr *addr;
+
+ tmp.addr = p->u.prefix4;
+
+ addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
+ addr->refcnt++;
+}
+
+static void
+bgp_address_del (struct prefix *p)
+{
+ struct bgp_addr tmp;
+ struct bgp_addr *addr;
+
+ tmp.addr = p->u.prefix4;
+
+ addr = hash_lookup (bgp_address_hash, &tmp);
+ addr->refcnt--;
+
+ if (addr->refcnt == 0)
+ {
+ hash_release (bgp_address_hash, addr);
+ XFREE (MTYPE_BGP_ADDR, addr);
+ }
+}
+
\f
struct bgp_connected_ref
{
if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
return;
+ bgp_address_add (addr);
+
rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
if (rn->info)
{
if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
return;
+ bgp_address_del (addr);
+
rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
if (! rn)
return;
}
int
-bgp_nexthop_self (afi_t afi, struct attr *attr)
+bgp_nexthop_self (struct attr *attr)
{
- struct listnode *node;
- struct listnode *node2;
- struct interface *ifp;
- struct connected *ifc;
- struct prefix *p;
+ struct bgp_addr tmp, *addr;
- for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
- {
- for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
- {
- p = ifc->address;
+ tmp.addr = attr->nexthop;
+
+ addr = hash_lookup (bgp_address_hash, &tmp);
+ if (addr)
+ return 1;
- if (p && p->family == AF_INET
- && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
- return 1;
- }
- }
return 0;
}
\f
/* Next hop must not be 0.0.0.0 nor Class D/E address. Next hop
must not be my own address. */
- if (bgp_nexthop_self (afi, &new_attr)
- || new_attr.nexthop.s_addr == 0
- || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
+ if (new_attr.nexthop.s_addr == 0
+ || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr))
+ || bgp_nexthop_self (&new_attr))
{
reason = "martian next-hop;";
goto filtered;