From: tedu Date: Thu, 18 Dec 2014 19:28:44 +0000 (+0000) Subject: two more uses of siphash. better hash for ipv4. maybe not needed for rbtree X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=f0cb2ce51a956b74b352c657aa0148b5218389a2;p=openbsd two more uses of siphash. better hash for ipv4. maybe not needed for rbtree hint, but still pretty. ok deraadt --- diff --git a/usr.sbin/bgpd/rde_rib.c b/usr.sbin/bgpd/rde_rib.c index 8844802e2ba..45e1ce4e304 100644 --- a/usr.sbin/bgpd/rde_rib.c +++ b/usr.sbin/bgpd/rde_rib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_rib.c,v 1.140 2014/12/12 18:15:51 tedu Exp $ */ +/* $OpenBSD: rde_rib.c,v 1.141 2014/12/18 19:28:44 tedu Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -1065,6 +1064,8 @@ struct nexthop_table { u_int32_t nexthop_hashmask; } nexthoptable; +SIPHASH_KEY nexthoptablekey; + void nexthop_init(u_int32_t hashsize) { @@ -1078,6 +1079,7 @@ nexthop_init(u_int32_t hashsize) for (i = 0; i < hs; i++) LIST_INIT(&nexthoptable.nexthop_hashtbl[i]); + arc4random_buf(&nexthoptablekey, sizeof(nexthoptablekey)); nexthoptable.nexthop_hashmask = hs - 1; } @@ -1308,17 +1310,16 @@ nexthop_hash(struct bgpd_addr *nexthop) switch (nexthop->aid) { case AID_INET: - h = (AF_INET ^ ntohl(nexthop->v4.s_addr) ^ - ntohl(nexthop->v4.s_addr) >> 13) & - nexthoptable.nexthop_hashmask; + h = SipHash24(&nexthoptablekey, &nexthop->v4.s_addr, + sizeof(nexthop->v4.s_addr)); break; case AID_INET6: - h = hash32_buf(&nexthop->v6, sizeof(struct in6_addr), - HASHINIT) & nexthoptable.nexthop_hashmask; + h = SipHash24(&nexthoptablekey, &nexthop->v6, + sizeof(struct in6_addr)); break; default: fatalx("nexthop_hash: unsupported AF"); } - return (&nexthoptable.nexthop_hashtbl[h]); + return (&nexthoptable.nexthop_hashtbl[h & nexthoptable.nexthop_hashmask]); } diff --git a/usr.sbin/bgpd/rde_update.c b/usr.sbin/bgpd/rde_update.c index 911d08180a5..b215f987fce 100644 --- a/usr.sbin/bgpd/rde_update.c +++ b/usr.sbin/bgpd/rde_update.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_update.c,v 1.81 2013/08/14 20:34:27 claudio Exp $ */ +/* $OpenBSD: rde_update.c,v 1.82 2014/12/18 19:28:44 tedu Exp $ */ /* * Copyright (c) 2004 Claudio Jeker @@ -17,11 +17,11 @@ */ #include #include -#include #include #include #include +#include #include "bgpd.h" #include "rde.h" @@ -63,6 +63,8 @@ RB_GENERATE(uptree_prefix, update_prefix, entry, up_prefix_cmp) RB_PROTOTYPE(uptree_attr, update_attr, entry, up_attr_cmp) RB_GENERATE(uptree_attr, update_attr, entry, up_attr_cmp) +SIPHASH_KEY uptree_key; + void up_init(struct rde_peer *peer) { @@ -78,6 +80,7 @@ up_init(struct rde_peer *peer) peer->up_acnt = 0; peer->up_nlricnt = 0; peer->up_wcnt = 0; + arc4random_buf(&uptree_key, sizeof(uptree_key)); } void @@ -363,6 +366,7 @@ up_generate(struct rde_peer *peer, struct rde_aspath *asp, { struct update_attr *ua = NULL; struct update_prefix *up; + SIPHASH_CTX ctx; if (asp) { ua = calloc(1, sizeof(struct update_attr)); @@ -378,10 +382,11 @@ up_generate(struct rde_peer *peer, struct rde_aspath *asp, * use aspath_hash as attr_hash, this may be unoptimal * but currently I don't care. */ - ua->attr_hash = hash32_buf(ua->attr, ua->attr_len, HASHINIT); + SipHash24_Init(&ctx, &uptree_key); + SipHash24_Update(&ctx, ua->attr, ua->attr_len); if (ua->mpattr) - ua->attr_hash = hash32_buf(ua->mpattr, ua->mpattr_len, - ua->attr_hash); + SipHash24_Update(&ctx, ua->mpattr, ua->mpattr_len); + ua->attr_hash = SipHash24_End(&ctx); } up = calloc(1, sizeof(struct update_prefix));