X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/6411163d48434575eaf49463e056e971a2fba16a..1b9a78583fbff3967b91e128dd7f57fcbcdc8db9:/server/peer.c diff --git a/server/peer.c b/server/peer.c index 72287cdd..75f7acba 100644 --- a/server/peer.c +++ b/server/peer.c @@ -188,54 +188,99 @@ static int p_encrypt(peer *p, int ty, buf *bin, buf *bout) static int p_decrypt(peer **pp, addr *a, size_t n, int ty, buf *bin, buf *bout) { - peer *p; - peer_byaddr *pa; + peer *p, *q; + peer_byaddr *pa, *qa; int err = KSERR_DECRYPT; unsigned f; - if (*pp) { - p = *pp; + /* --- If we have a match on the source address then try that first --- */ + + q = *pp; + if (q) { T( trace(T_PEER, "peer: decrypting packet from known peer `%s'", - p_name(p)); ) - err = ksl_decrypt(&p->ks, ty, bin, bout); - } else { + p_name(q)); ) + if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) != KSERR_DECRYPT || + !(q->spec.f & PSF_MOBILE) || nmobile == 1) { + p = q; + goto match; + } + T( trace(T_PEER, "peer: failed to decrypt: try other mobile peers...", + p_name(q)); ) + } else if (nmobile) + T( trace(T_PEER, "peer: unknown source: trying mobile peers..."); ) + else { p = 0; - if (nmobile) { - T( trace(T_PEER, "peer: unknown source: trying mobile peers..."); ) - FOREACH_PEER(q, { - if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) == KSERR_DECRYPT) { - T( trace(T_PEER, "peer: peer `%s' failed to decrypt", - p_name(q)); ) - continue; - } else { - p = *pp = q; - IF_TRACING(T_PEER, { - if (!err) - trace(T_PEER, "peer: peer `%s' reports success", p_name(p)); - else { - trace(T_PEER, "peer: peer `%s' reports decryption error %d", - p_name(p), err); - } - }) - break; + goto searched; + } + + /* --- See whether any mobile peer is interested --- */ + + p = 0; + FOREACH_PEER(qq, { + if (qq == q || !(qq->spec.f & PSF_MOBILE)) continue; + if ((err = ksl_decrypt(&qq->ks, ty, bin, bout)) == KSERR_DECRYPT) { + T( trace(T_PEER, "peer: peer `%s' failed to decrypt", + p_name(qq)); ) + continue; + } else { + p = qq; + IF_TRACING(T_PEER, { + if (!err) + trace(T_PEER, "peer: peer `%s' reports success", p_name(qq)); + else { + trace(T_PEER, "peer: peer `%s' reports decryption error %d", + p_name(qq), err); } - }); + }) + break; } - if (!p) { + }); + + /* --- We've searched the mobile peers --- */ + +searched: + if (!p) { + if (!q) a_warn("PEER", "-", "unexpected-source", "?ADDR", a, A_END); - return (-1); + else { + a_warn("PEER", "?PEER", p, "decrypt-failed", + "error-code", "%d", err, A_END); + p_rxupdstats(q, n); } - if (!err) { + return (-1); + } + + /* --- We found one that accepted, so update the peer's address --- * + * + * If we had an initial guess of which peer this packet came from -- i.e., + * @q@ is not null -- then swap the addresses over. This doesn't leave the + * evicted peer in an especially good state, but it ought to get sorted out + * soon enough. + */ + + if (!err) { + *pp = p; + if (!q) { T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); ) - p_rxupdstats(p, n); pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f); assert(!f); am_remove(&byaddr, p->byaddr); p->byaddr = pa; pa->p = p; p->spec.sa = *a; a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); + } else { + T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'", + p_name(p), p_name(q)); ) + pa = p->byaddr; qa = q->byaddr; + pa->p = q; q->byaddr = pa; q->spec.sa = p->spec.sa; + qa->p = p; p->byaddr = qa; p->spec.sa = *a; + a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); + a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END); } } + +match: + p_rxupdstats(p, n); if (err) { if (p) p->st.n_reject++; a_warn("PEER", "?PEER", p, "decrypt-failed", @@ -320,7 +365,6 @@ static void p_read(int fd, unsigned mode, void *v) /* --- Pick the packet apart --- */ - if (p) p_rxupdstats(p, n); buf_init(&b, buf_i, n); if ((ch = buf_getbyte(&b)) < 0) { a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END); @@ -351,21 +395,25 @@ static void p_read(int fd, unsigned mode, void *v) break; case MSG_KEYEXCH: if (!p) goto unexp; + p_rxupdstats(p, n); kx_message(&p->kx, ch & MSG_TYPEMASK, &b); break; case MSG_MISC: switch (ch & MSG_TYPEMASK) { case MISC_NOP: if (!p) goto unexp; + p_rxupdstats(p, n); T( trace(T_PEER, "peer: received NOP packet"); ) break; case MISC_PING: if (!p) goto unexp; + p_rxupdstats(p, n); buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b)); p_txend(p); break; case MISC_PONG: if (!p) goto unexp; + p_rxupdstats(p, n); p_ponged(p, MISC_PONG, &b); break; case MISC_EPING: @@ -395,7 +443,7 @@ static void p_read(int fd, unsigned mode, void *v) a_warn("PEER", "?PEER", p, "bad-packet", - "unknown-category" "0x%02x", ch, + "unknown-category", "0x%02x", ch, A_END); break; unexp: @@ -772,6 +820,7 @@ unsigned p_port(void) static void p_keepalive(struct timeval *now, void *pv) { peer *p = pv; + p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p); T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); ) p_setkatimer(p); @@ -823,8 +872,8 @@ peer *p_create(peerspec *spec) T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); ) p->spec = *spec; p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname); - if (spec->tag) - p->spec.tag = xstrdup(spec->tag); + if (spec->tag) p->spec.tag = xstrdup(spec->tag); + if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag); p->ks = 0; p->pings = 0; p->ifname = 0; @@ -854,8 +903,7 @@ peer *p_create(peerspec *spec) return (p); tidy_4: - if (spec->t_ka) - sel_rmtimer(&p->tka); + if (spec->t_ka) sel_rmtimer(&p->tka); xfree(p->ifname); p->t->ops->destroy(p->t); tidy_3: @@ -863,6 +911,7 @@ tidy_3: tidy_2: am_remove(&byaddr, p->byaddr); if (p->spec.tag) xfree(p->spec.tag); + if (p->spec.privtag) xfree(p->spec.privtag); tidy_1: sym_remove(&byname, p->byname); tidy_0: @@ -890,6 +939,16 @@ const char *p_name(peer *p) const char *p_tag(peer *p) { return (p->spec.tag ? p->spec.tag : p->spec.name); } +/* --- @p_privtag@ --- * + * + * Arguments: @peer *p@ = pointer to a peer block + * + * Returns: A pointer to the peer's private key tag. + */ + +const char *p_privtag(peer *p) + { return (p->spec.privtag ? p->spec.privtag : tag_priv); } + /* --- @p_spec@ --- * * * Arguments: @peer *p@ = pointer to a peer block @@ -954,15 +1013,12 @@ void p_destroy(peer *p) a_notify("KILL", "%s", p->spec.name, A_END); ksl_free(&p->ks); kx_free(&p->kx); - if (p->spec.f & PSF_MOBILE) - nmobile--; - if (p->ifname) - xfree(p->ifname); - if (p->spec.tag) - xfree(p->spec.tag); + if (p->spec.f & PSF_MOBILE) nmobile--; + if (p->ifname) xfree(p->ifname); + if (p->spec.tag) xfree(p->spec.tag); + if (p->spec.privtag) xfree(p->spec.privtag); p->t->ops->destroy(p->t); - if (p->spec.t_ka) - sel_rmtimer(&p->tka); + if (p->spec.t_ka) sel_rmtimer(&p->tka); for (pg = p->pings; pg; pg = ppg) { ppg = pg->next; p_pingdone(pg, PING_PEERDIED);