move assertion to top of function. Found by PVS-Studio
[freeradius.git] / src / main / process.c
index e33c336..7b4196c 100644 (file)
@@ -285,6 +285,12 @@ static bool we_are_master(void)
 #  define ASSERT_MASTER
 #endif
 
+/*
+ *     Make state transitions simpler.
+ */
+#define FINAL_STATE(_x) NO_CHILD_THREAD; request->component = "<" #_x ">"; request->module = ""; request->child_state = _x
+
+
 static int event_new_fd(rad_listen_t *this);
 
 /*
@@ -344,7 +350,7 @@ void radius_update_listener(rad_listen_t *this)
 static int request_num_counter = 1;
 #ifdef WITH_PROXY
 static int request_will_proxy(REQUEST *request) CC_HINT(nonnull);
-static int request_proxy(REQUEST *request, int retransmit) CC_HINT(nonnull);
+static int request_proxy(REQUEST *request) CC_HINT(nonnull);
 STATE_MACHINE_DECL(request_ping) CC_HINT(nonnull);
 
 STATE_MACHINE_DECL(request_response_delay) CC_HINT(nonnull);
@@ -409,6 +415,14 @@ static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, bool received)
        if (!packet) return;
        if (!RDEBUG_ENABLED) return;
 
+#ifdef WITH_DETAIL
+       /*
+        *      Don't print IP addresses for detail files.
+        */
+       if (request->listener &&
+           (request->listener->type == RAD_LISTEN_DETAIL)) return;
+
+#endif
        /*
         *      Client-specific debugging re-prints the input
         *      packet into the client log.
@@ -416,31 +430,39 @@ static void debug_packet(REQUEST *request, RADIUS_PACKET *packet, bool received)
         *      This really belongs in a utility library
         */
        if (is_radius_code(packet->code)) {
-               RDEBUG("%s %s Id %i from %s:%i to %s:%i length %zu",
+               RDEBUG("%s %s Id %i from %s%s%s:%i to %s%s%s:%i length %zu",
                       received ? "Received" : "Sent",
                       fr_packet_codes[packet->code],
                       packet->id,
+                      packet->src_ipaddr.af == AF_INET6 ? "[" : "",
                       inet_ntop(packet->src_ipaddr.af,
                                 &packet->src_ipaddr.ipaddr,
                                 src_ipaddr, sizeof(src_ipaddr)),
+                      packet->src_ipaddr.af == AF_INET6 ? "]" : "",
                       packet->src_port,
+                      packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
                       inet_ntop(packet->dst_ipaddr.af,
                                 &packet->dst_ipaddr.ipaddr,
                                 dst_ipaddr, sizeof(dst_ipaddr)),
+                      packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
                       packet->dst_port,
                       packet->data_len);
        } else {
-               RDEBUG("%s code %i Id %i from %s:%i to %s:%i length %zu",
+               RDEBUG("%s code %u Id %i from %s%s%s:%i to %s%s%s:%i length %zu\n",
                       received ? "Received" : "Sent",
                       packet->code,
                       packet->id,
+                      packet->src_ipaddr.af == AF_INET6 ? "[" : "",
                       inet_ntop(packet->src_ipaddr.af,
                                 &packet->src_ipaddr.ipaddr,
                                 src_ipaddr, sizeof(src_ipaddr)),
+                      packet->src_ipaddr.af == AF_INET6 ? "]" : "",
                       packet->src_port,
+                      packet->dst_ipaddr.af == AF_INET6 ? "[" : "",
                       inet_ntop(packet->dst_ipaddr.af,
                                 &packet->dst_ipaddr.ipaddr,
                                 dst_ipaddr, sizeof(dst_ipaddr)),
+                      packet->dst_ipaddr.af == AF_INET6 ? "]" : "",
                       packet->dst_port,
                       packet->data_len);
        }
@@ -463,6 +485,8 @@ static struct timeval *request_response_window(REQUEST *request)
 {
        VERIFY_REQUEST(request);
 
+       rad_assert(request->home_server != NULL);
+
        if (request->client) {
                /*
                 *      The client hasn't set the response window.  Return
@@ -478,7 +502,6 @@ static struct timeval *request_response_window(REQUEST *request)
                }
        }
 
-       rad_assert(request->home_server != NULL);
        return &request->home_server->response_window;
 }
 
@@ -605,8 +628,7 @@ static void request_done(REQUEST *request, int action)
         *      and wait for the master thread timer to clean us up.
         */
        if (!we_are_master()) {
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_DONE;
+               FINAL_STATE(REQUEST_DONE);
                return;
        }
 #endif
@@ -805,12 +827,23 @@ static void request_cleanup_delay_init(REQUEST *request)
         *      client.  Everything else just gets cleaned up
         *      immediately.
         */
-       if (!(request->packet->code == PW_CODE_ACCESS_REQUEST)
-#ifdef WITH_COA
-           || (request->packet->code == PW_CODE_COA_REQUEST)
-           || (request->packet->code == PW_CODE_DISCONNECT_REQUEST)
+       if (request->packet->dst_port == 0) goto done;
+
+       /*
+        *      Accounting packets shouldn't be retransmitted.  They
+        *      should always be updated with Acct-Delay-Time.
+        */
+#ifdef WITH_ACCOUNTING
+       if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) goto done;
+#endif
+
+#ifdef WITH_DHCP
+       if (request->listener->type == RAD_LISTEN_DHCP) goto done;
+#endif
+
+#ifdef WITH_VMPS
+       if (request->listener->type == RAD_LISTEN_VQP) goto done;
 #endif
-               ) goto done;
 
        if (!request->root->cleanup_delay) goto done;
 
@@ -830,16 +863,20 @@ static void request_cleanup_delay_init(REQUEST *request)
                if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
 #endif
                request->process = request_cleanup_delay;
-               request->child_state = REQUEST_CLEANUP_DELAY;
+
+               if (!we_are_master()) {
+                       FINAL_STATE(REQUEST_CLEANUP_DELAY);
+                       return;
+               }
 
                /*
                 *      Update this if we can, otherwise let the timers pick it up.
                 */
-               if (we_are_master()) {
-                       STATE_MACHINE_TIMER(FR_ACTION_TIMER);
-               } else {
-                       NO_CHILD_THREAD;
-               }
+               request->child_state = REQUEST_CLEANUP_DELAY;
+#ifdef HAVE_PTHREAD_H
+               rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
+#endif
+               STATE_MACHINE_TIMER(FR_ACTION_TIMER);
                return;
        }
 
@@ -854,7 +891,7 @@ done:
 /*
  *     Enforce max_request_time.
  */
-static void request_max_time(REQUEST *request)
+static bool request_max_time(REQUEST *request)
 {
        struct timeval now, when;
        rad_assert(request->magic == REQUEST_MAGIC);
@@ -877,7 +914,7 @@ static void request_max_time(REQUEST *request)
        if (request->child_state == REQUEST_DONE) {
        done:
                request_done(request, FR_ACTION_DONE);
-               return;
+               return true;
        }
 
        /*
@@ -920,6 +957,7 @@ static void request_max_time(REQUEST *request)
        tv_add(&when, request->delay);
        request->delay += request->delay >> 1;
        STATE_MACHINE_TIMER(FR_ACTION_TIMER);
+       return false;
 }
 
 static void request_queue_or_run(REQUEST *request,
@@ -1083,7 +1121,7 @@ static void request_cleanup_delay(REQUEST *request, int action)
                        return;
                } /* else it's time to clean up */
 
-               request_done(request, REQUEST_DONE);
+               request_done(request, FR_ACTION_DONE);
                break;
 
        default:
@@ -1125,7 +1163,7 @@ static void request_response_delay(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_DUP:
-               ERROR("(%u) Discarding duplicate request from "
+               RDEBUG("(%u) Discarding duplicate request from "
                      "client %s port %d - ID: %u due to delayed response",
                      request->number, request->client->shortname,
                      request->packet->src_port,request->packet->id);
@@ -1140,8 +1178,6 @@ static void request_response_delay(REQUEST *request, int action)
        case FR_ACTION_TIMER:
                fr_event_now(el, &now);
 
-               rad_assert(request->response_delay.tv_sec > 0);
-
                /*
                 *      See if it's time to send the reply.  If not,
                 *      we wait some more.
@@ -1192,8 +1228,8 @@ static int request_pre_handler(REQUEST *request, UNUSED int action)
         *      process it.
         */
        if (request->packet->dst_port == 0) {
-               request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
-               request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
+               request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+               request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
                return 1;
        }
 
@@ -1224,7 +1260,7 @@ static int request_pre_handler(REQUEST *request, UNUSED int action)
        }
 
        if (!request->username) {
-               request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+               request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
        }
 
        return 1;
@@ -1259,7 +1295,7 @@ static void request_finish(REQUEST *request, int action)
        /*
         *      Override the response code if a control:Response-Packet-Type attribute is present.
         */
-       vp = pairfind(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
        if (vp) {
                if (vp->vp_integer == 256) {
                        RDEBUG2("Not responding to request");
@@ -1273,7 +1309,7 @@ static void request_finish(REQUEST *request, int action)
         */
        else if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
                if (request->reply->code == 0) {
-                       vp = pairfind(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
+                       vp = fr_pair_find_by_num(request->config, PW_AUTH_TYPE, 0, TAG_ANY);
                        if (!vp || (vp->vp_integer != 5)) {
                                RDEBUG2("There was no response configured: "
                                        "rejecting request");
@@ -1286,9 +1322,9 @@ static void request_finish(REQUEST *request, int action)
        /*
         *      Copy Proxy-State from the request to the reply.
         */
-       vp = paircopy_by_num(request->reply, request->packet->vps,
+       vp = fr_pair_list_copy_by_num(request->reply, request->packet->vps,
                       PW_PROXY_STATE, 0, TAG_ANY);
-       if (vp) pairadd(&request->reply->vps, vp);
+       if (vp) fr_pair_add(&request->reply->vps, vp);
 
        /*
         *      Call Post-Auth for Access-Request packets.
@@ -1318,8 +1354,7 @@ static void request_finish(REQUEST *request, int action)
         */
        if (request->packet->dst_port == 0) {
                RDEBUG("Finished internally proxied request.");
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_DONE;
+               FINAL_STATE(REQUEST_DONE);
                return;
        }
 
@@ -1382,6 +1417,27 @@ static void request_finish(REQUEST *request, int action)
            (request->root->reject_delay.tv_sec > 0)) {
                request->response_delay = request->root->reject_delay;
 
+               vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY, 0, TAG_ANY);
+               if (vp) {
+                       if (vp->vp_integer <= 10) {
+                               request->response_delay.tv_sec = vp->vp_integer;
+                       } else {
+                               request->response_delay.tv_sec = 10;
+                       }
+                       request->response_delay.tv_usec = 0;
+               } else {
+                       vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY_USEC, 0, TAG_ANY);
+                       if (vp) {
+                               if (vp->vp_integer <= 10 * USEC) {
+                                       request->response_delay.tv_sec = vp->vp_integer / USEC;
+                                       request->response_delay.tv_usec = vp->vp_integer % USEC;
+                               } else {
+                                       request->response_delay.tv_sec = 10;
+                                       request->response_delay.tv_usec = 0;
+                               }
+                       }
+               }
+
 #ifdef WITH_PROXY
                /*
                 *      If we timed out a proxy packet, don't delay
@@ -1397,22 +1453,26 @@ static void request_finish(REQUEST *request, int action)
        /*
         *      Send the reply.
         */
-       if (request->response_delay.tv_sec == 0) {
-               rad_assert(request->response_delay.tv_usec == 0);
+       if ((request->response_delay.tv_sec == 0) &&
+           (request->response_delay.tv_usec == 0)) {
 
                /*
                 *      Don't print a reply if there's none to send.
                 */
                if (request->reply->code != 0) {
+                       if (rad_debug_lvl && request->state &&
+                           (request->reply->code == PW_CODE_ACCESS_ACCEPT)) {
+                               if (!fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY)) {
+                                       RWDEBUG2("Unused attributes found in &session-state:");
+                               }
+                       }
+
                        debug_packet(request, request->reply, false);
                        request->listener->send(request->listener, request);
                }
 
        done:
                RDEBUG2("Finished request");
-               request->component = "<core>";
-               request->module = "<done>";
-
                request_cleanup_delay_init(request);
 
        } else {
@@ -1424,11 +1484,9 @@ static void request_finish(REQUEST *request, int action)
                RDEBUG2("Delaying response for %d.%06d seconds",
                        (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
                request->listener->encode(request->listener, request);
-               request->component = "<core>";
-               request->module = "<delay>";
                request->process = request_response_delay;
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_RESPONSE_DELAY;
+
+               FINAL_STATE(REQUEST_RESPONSE_DELAY);
        }
 }
 
@@ -1455,7 +1513,7 @@ static void request_running(REQUEST *request, int action)
        switch (action) {
        case FR_ACTION_TIMER:
                COA_SEPARATE;
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_DUP:
@@ -1470,9 +1528,7 @@ static void request_running(REQUEST *request, int action)
                                               child_state_names[request->child_state],
                                               child_state_names[REQUEST_DONE]);
 #endif
-
-                       NO_CHILD_THREAD;
-                       request->child_state = REQUEST_DONE;
+                       FINAL_STATE(REQUEST_DONE);
                        break;
                }
 
@@ -1494,7 +1550,7 @@ static void request_running(REQUEST *request, int action)
                         *      up the post proxy fail
                         *      handler.
                         */
-                       if (request_proxy(request, 0) < 0) goto req_finished;
+                       if (request_proxy(request) < 0) goto req_finished;
                } else
 #endif
                {
@@ -1536,10 +1592,15 @@ int request_receive(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PACKET *pack
 #ifdef WITH_ACCOUNTING
        if (listener->type != RAD_LISTEN_DETAIL)
 #endif
+
+#ifdef WITH_TCP
        {
                sock = listener->data;
                sock->last_packet = now.tv_sec;
+
+               packet->proto = sock->proto;
        }
+#endif
 
        /*
         *      Skip everything if required.
@@ -1706,8 +1767,8 @@ skip_dup:
 #endif
 
                request->listener->decode(request->listener, request);
-               request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
-               request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
+               request->username = fr_pair_find_by_num(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
+               request->password = fr_pair_find_by_num(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
 
                fun(request);
 
@@ -1747,7 +1808,7 @@ static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PA
                ERROR("No memory");
                return NULL;
        }
-       request->reply = rad_alloc(request, false);
+       request->reply = rad_alloc_reply(request, packet);
        if (!request->reply) {
                ERROR("No memory");
                talloc_free(request);
@@ -1767,9 +1828,6 @@ static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PA
                               child_state_names[request->child_state],
                               child_state_names[REQUEST_RUNNING]);
 #endif
-#ifdef HAVE_PTHREAD_H
-       request->child_pid = NO_SUCH_CHILD_PID;
-#endif
        request->handle = fun;
        NO_CHILD_THREAD;
 
@@ -1958,26 +2016,6 @@ static void tcp_socket_timer(void *ctx)
 
 #ifdef WITH_PROXY
 /*
- *     Add +/- 2s of jitter, as suggested in RFC 3539
- *     and in RFC 5080.
- */
-static void add_jitter(struct timeval *when)
-{
-       uint32_t jitter;
-
-       when->tv_sec -= 2;
-
-       jitter = fr_rand();
-       jitter ^= (jitter >> 10);
-       jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
-
-       /*
-        *      Add in ~ (4 * USEC) of jitter.
-        */
-       tv_add(when, jitter);
-}
-
-/*
  *     Called by socket_del to remove requests with this socket
  */
 static int eol_proxy_listener(void *ctx, void *data)
@@ -2085,8 +2123,9 @@ static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
        }
 
 #ifdef WITH_TCP
-       rad_assert(request->proxy_listener != NULL);
-       request->proxy_listener->count--;
+       if (request->proxy_listener) {
+               request->proxy_listener->count--;
+       }
 #endif
        request->proxy_listener = NULL;
 
@@ -2095,7 +2134,6 @@ static void remove_from_proxy_hash_nl(REQUEST *request, bool yank)
         *      the mutex.  This guarantees that when another thread
         *      grabs the mutex, the "not in hash" flag is correct.
         */
-       RDEBUG3("proxy: request is no longer in proxy hash");
 }
 
 static void remove_from_proxy_hash(REQUEST *request)
@@ -2255,13 +2293,13 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
         *      Delete any reply we had accumulated until now.
         */
        RDEBUG2("Clearing existing &reply: attributes");
-       pairfree(&request->reply->vps);
+       fr_pair_list_free(&request->reply->vps);
 
        /*
         *      Run the packet through the post-proxy stage,
         *      BEFORE playing games with the attributes.
         */
-       vp = pairfind(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
        if (vp) {
                post_proxy_type = vp->vp_integer;
        /*
@@ -2298,7 +2336,7 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
                 *      Create config:Post-Proxy-Type
                 */
                if (dval) {
-                       vp = radius_paircreate(request, &request->config, PW_POST_PROXY_TYPE, 0);
+                       vp = radius_pair_create(request, &request->config, PW_POST_PROXY_TYPE, 0);
                        vp->vp_integer = dval->value;
                }
        }
@@ -2348,7 +2386,7 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
        }
 
 #ifdef WITH_COA
-       if (request->packet->code == request->proxy->code)
+       if (request->packet->code == request->proxy->code) {
          /*
           *    Don't run the next bit if we originated a CoA
           *    packet, after receiving an Access-Request or
@@ -2356,21 +2394,30 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
           */
 #endif
 
-       /*
-        *      There may NOT be a proxy reply, as we may be
-        *      running Post-Proxy-Type = Fail.
-        */
-       if (reply) {
-               pairadd(&request->reply->vps, paircopy(request->reply, reply->vps));
-
                /*
-                *      Delete the Proxy-State Attributes from
-                *      the reply.  These include Proxy-State
-                *      attributes from us and remote server.
+                *      There may NOT be a proxy reply, as we may be
+                *      running Post-Proxy-Type = Fail.
                 */
-               pairdelete(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
-       }
+               if (reply) {
+                       fr_pair_add(&request->reply->vps, fr_pair_list_copy(request->reply, reply->vps));
 
+                       /*
+                        *      Delete the Proxy-State Attributes from
+                        *      the reply.  These include Proxy-State
+                        *      attributes from us and remote server.
+                        */
+                       fr_pair_delete_by_num(&request->reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
+
+               } else {
+                       vp = fr_pair_find_by_num(request->config, PW_RESPONSE_PACKET_TYPE, 0, TAG_ANY);
+                       if (vp && (vp->vp_integer != 256)) {
+                               request->proxy_reply = rad_alloc_reply(request, request->proxy);
+                               request->proxy_reply->code = vp->vp_integer;
+                       }
+               }
+#ifdef WITH_COA
+       }
+#endif
        switch (rcode) {
        default:  /* Don't do anything */
                break;
@@ -2384,6 +2431,28 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
        return 1;
 }
 
+static void mark_home_server_alive(REQUEST *request, home_server_t *home)
+{
+       char buffer[128];
+
+       home->state = HOME_STATE_ALIVE;
+       home->response_timeouts = 0;
+       exec_trigger(request, home->cs, "home_server.alive", false);
+       home->currently_outstanding = 0;
+       home->num_sent_pings = 0;
+       home->num_received_pings = 0;
+       gettimeofday(&home->revive_time, NULL);
+
+       fr_event_delete(el, &home->ev);
+
+       RPROXY("Marking home server %s port %d alive",
+              inet_ntop(request->proxy->dst_ipaddr.af,
+                        &request->proxy->dst_ipaddr.ipaddr,
+                        buffer, sizeof(buffer)),
+              request->proxy->dst_port);
+}
+
+
 int request_proxy_reply(RADIUS_PACKET *packet)
 {
        RADIUS_PACKET **proxy_p;
@@ -2398,7 +2467,8 @@ int request_proxy_reply(RADIUS_PACKET *packet)
 
        if (!proxy_p) {
                PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
-               PROXY("No outstanding request was found for reply from host %s port %d - ID %u",
+               PROXY("No outstanding request was found for %s packet from host %s port %d - ID %u",
+                      fr_packet_codes[packet->code],
                       inet_ntop(packet->src_ipaddr.af,
                                 &packet->src_ipaddr.ipaddr,
                                 buffer, sizeof(buffer)),
@@ -2407,7 +2477,6 @@ int request_proxy_reply(RADIUS_PACKET *packet)
        }
 
        request = fr_packet2myptr(REQUEST, proxy, proxy_p);
-       request->num_proxied_responses++; /* needs to be protected by lock */
 
        PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
 
@@ -2442,12 +2511,16 @@ int request_proxy_reply(RADIUS_PACKET *packet)
         *      Status-Server packets don't count as real packets.
         */
        if (request->proxy->code != PW_CODE_STATUS_SERVER) {
+#ifdef WITH_TCP
                listen_socket_t *sock = request->proxy_listener->data;
 
-               request->home_server->last_packet_recv = now.tv_sec;
                sock->last_packet = now.tv_sec;
+#endif
+               request->home_server->last_packet_recv = now.tv_sec;
        }
 
+       request->num_proxied_responses++;
+
        /*
         *      If we have previously seen a reply, ignore the
         *      duplicate.
@@ -2498,6 +2571,8 @@ int request_proxy_reply(RADIUS_PACKET *packet)
 
 #ifdef WITH_ACCOUNTING
        case PW_CODE_ACCOUNTING_REQUEST:
+               proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
+
                request->proxy_listener->stats.total_responses++;
                proxy_acct_stats.last_packet = packet->timestamp.tv_sec;
                break;
@@ -2522,12 +2597,13 @@ int request_proxy_reply(RADIUS_PACKET *packet)
 #endif
 
        /*
-        *      We've received a reply.  If we hadn't been sending it
-        *      packets for a while, just mark it alive.
+        *      If we hadn't been sending the home server packets for
+        *      a while, just mark it alive.  Or, if it was zombie,
+        *      it's now responded, and is therefore alive.
         */
-       if (request->home_server->state == HOME_STATE_UNKNOWN) {
-               request->home_server->state = HOME_STATE_ALIVE;
-               request->home_server->response_timeouts = 0;
+       if ((request->home_server->state == HOME_STATE_UNKNOWN) ||
+           (request->home_server->state == HOME_STATE_ZOMBIE)) {
+               mark_home_server_alive(request, request->home_server);
        }
 
        /*
@@ -2572,12 +2648,12 @@ static int setup_post_proxy_fail(REQUEST *request)
        if (!dval) dval = dict_valbyname(PW_POST_PROXY_TYPE, 0, "Fail");
 
        if (!dval) {
-               pairdelete(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
+               fr_pair_delete_by_num(&request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
                return 0;
        }
 
-       vp = pairfind(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
-       if (!vp) vp = radius_paircreate(request, &request->config,
+       vp = fr_pair_find_by_num(request->config, PW_POST_PROXY_TYPE, 0, TAG_ANY);
+       if (!vp) vp = radius_pair_create(request, &request->config,
                                        PW_POST_PROXY_TYPE, 0);
        vp->vp_integer = dval->value;
 
@@ -2614,7 +2690,7 @@ static void proxy_no_reply(REQUEST *request, int action)
                break;
 
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_PROXY_REPLY:
@@ -2663,7 +2739,7 @@ static void proxy_running(REQUEST *request, int action)
                break;
 
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_RUN:
@@ -2728,7 +2804,7 @@ static int request_will_proxy(REQUEST *request)
         */
        if (request->reply->code != 0) return 0;
 
-       vp = pairfind(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
        if (vp) {
                realm = realm_find2(vp->vp_strvalue);
                if (!realm) {
@@ -2760,7 +2836,7 @@ static int request_will_proxy(REQUEST *request)
                        return 0;
                }
 
-       } else if ((vp = pairfind(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
+       } else if ((vp = fr_pair_find_by_num(request->config, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
                int pool_type;
 
                switch (request->packet->code) {
@@ -2790,9 +2866,8 @@ static int request_will_proxy(REQUEST *request)
                /*
                 *      Send it directly to a home server (i.e. NAS)
                 */
-       } else if (((vp = pairfind(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
-                  ((vp = pairfind(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
-               VALUE_PAIR *port;
+       } else if (((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY)) != NULL) ||
+                  ((vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL)) {
                uint16_t dst_port;
                fr_ipaddr_t dst_ipaddr;
 
@@ -2808,8 +2883,8 @@ static int request_will_proxy(REQUEST *request)
                        dst_ipaddr.prefix = 128;
                }
 
-               port = pairfind(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
-               if (!port) {
+               vp = fr_pair_find_by_num(request->config, PW_PACKET_DST_PORT, 0, TAG_ANY);
+               if (!vp) {
                        if (request->packet->code == PW_CODE_ACCESS_REQUEST) {
                                dst_port = PW_AUTH_UDP_PORT;
 
@@ -2895,7 +2970,7 @@ do_home:
        /*
         *      Remember that we sent the request to a Realm.
         */
-       if (realmname) pairmake_packet("Realm", realmname, T_OP_EQ);
+       if (realmname) pair_make_request("Realm", realmname, T_OP_EQ);
 
        /*
         *      Strip the name, if told to.
@@ -2904,7 +2979,7 @@ do_home:
         *      requests.
         */
        if (realm && (realm->strip_realm == true) &&
-          (strippedname = pairfind(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
+          (strippedname = fr_pair_find_by_num(request->proxy->vps, PW_STRIPPED_USER_NAME, 0, TAG_ANY)) != NULL) {
                /*
                 *      If there's a Stripped-User-Name attribute in
                 *      the request, then use THAT as the User-Name
@@ -2918,10 +2993,10 @@ do_home:
                 *      from the vps list, and making the new
                 *      User-Name the head of the vps list.
                 */
-               vp = pairfind(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
+               vp = fr_pair_find_by_num(request->proxy->vps, PW_USER_NAME, 0, TAG_ANY);
                if (!vp) {
                        vp_cursor_t cursor;
-                       vp = radius_paircreate(NULL, NULL,
+                       vp = radius_pair_create(NULL, NULL,
                                               PW_USER_NAME, 0);
                        rad_assert(vp != NULL); /* handled by above function */
                        /* Insert at the START of the list */
@@ -2930,7 +3005,7 @@ do_home:
                        fr_cursor_merge(&cursor, request->proxy->vps);
                        request->proxy->vps = vp;
                }
-               pairstrcpy(vp, strippedname->vp_strvalue);
+               fr_pair_value_strcpy(vp, strippedname->vp_strvalue);
 
                /*
                 *      Do NOT delete Stripped-User-Name.
@@ -2944,18 +3019,18 @@ do_home:
         *      anymore - we changed it.
         */
        if ((request->packet->code == PW_CODE_ACCESS_REQUEST) &&
-           pairfind(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
-           pairfind(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
-               vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
-               pairmemcpy(vp, request->packet->vector, sizeof(request->packet->vector));
+           fr_pair_find_by_num(request->proxy->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) &&
+           fr_pair_find_by_num(request->proxy->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL) {
+               vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_CHAP_CHALLENGE, 0);
+               fr_pair_value_memcpy(vp, request->packet->vector, sizeof(request->packet->vector));
        }
 
        /*
         *      The RFC's say we have to do this, but FreeRADIUS
         *      doesn't need it.
         */
-       vp = radius_paircreate(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
-       pairsprintf(vp, "%u", request->packet->id);
+       vp = radius_pair_create(request->proxy, &request->proxy->vps, PW_PROXY_STATE, 0);
+       fr_pair_value_sprintf(vp, "%u", request->packet->id);
 
        /*
         *      Should be done BEFORE inserting into proxy hash, as
@@ -2966,7 +3041,7 @@ do_home:
        /*
         *      Call the pre-proxy routines.
         */
-       vp = pairfind(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
        if (vp) {
                DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
                /* Must be a validation issue */
@@ -2992,6 +3067,14 @@ do_home:
 
                request->server = old_server;
        } else {
+               char buffer[128];
+
+               RDEBUG2("Starting proxy to home server %s port %d",
+                       inet_ntop(request->proxy->dst_ipaddr.af,
+                                 &request->proxy->dst_ipaddr.ipaddr,
+                                 buffer, sizeof(buffer)),
+                       request->proxy->dst_port);
+
                rcode = process_pre_proxy(pre_proxy_type, request);
        }
 
@@ -3040,7 +3123,7 @@ static int proxy_to_virtual_server(REQUEST *request)
 
        fake = request_alloc_fake(request);
 
-       fake->packet->vps = paircopy(fake->packet, request->packet->vps);
+       fake->packet->vps = fr_pair_list_copy(fake->packet, request->packet->vps);
        talloc_free(request->proxy);
 
        fake->server = request->home_server->server;
@@ -3079,7 +3162,7 @@ static int proxy_to_virtual_server(REQUEST *request)
 }
 
 
-static int request_proxy(REQUEST *request, int retransmit)
+static int request_proxy(REQUEST *request)
 {
        char buffer[128];
 
@@ -3145,11 +3228,8 @@ static int request_proxy(REQUEST *request, int retransmit)
 
        }
 
-       gettimeofday(&request->proxy_retransmit, NULL);
-       if (!retransmit) {
-               request->proxy->timestamp = request->proxy_retransmit;
-       }
-       request->home_server->last_packet_sent = request->proxy_retransmit.tv_sec;
+       gettimeofday(&request->proxy->timestamp, NULL);
+       request->home_server->last_packet_sent = request->proxy->timestamp.tv_sec;
 
        /*
         *      Encode the packet before we do anything else.
@@ -3160,9 +3240,15 @@ static int request_proxy(REQUEST *request, int retransmit)
        /*
         *      Set the state function, then the state, no child, and
         *      send the packet.
+        *
+        *      The order here is different from other state changes
+        *      due to race conditions with replies from the home
+        *      server.
         */
        request->process = proxy_wait_for_reply;
        request->child_state = REQUEST_PROXIED;
+       request->component = "<REQUEST_PROXIED>";
+       request->module = "";
        NO_CHILD_THREAD;
 
        /*
@@ -3210,20 +3296,21 @@ static int request_proxy_anew(REQUEST *request)
 
 #ifdef WITH_ACCOUNTING
        /*
-        *      Update the Acct-Delay-Time attribute.
+        *      Update the Acct-Delay-Time attribute, since the LAST
+        *      time we tried to retransmit this packet.
         */
        if (request->packet->code == PW_CODE_ACCOUNTING_REQUEST) {
                VALUE_PAIR *vp;
 
-               vp = pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
-               if (!vp) vp = radius_paircreate(request->proxy,
+               vp = fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
+               if (!vp) vp = radius_pair_create(request->proxy,
                                                &request->proxy->vps,
                                                PW_ACCT_DELAY_TIME, 0);
                if (vp) {
                        struct timeval now;
 
                        gettimeofday(&now, NULL);
-                       vp->vp_integer += now.tv_sec - request->proxy_retransmit.tv_sec;
+                       vp->vp_integer += now.tv_sec - request->proxy->timestamp.tv_sec;
                }
        }
 #endif
@@ -3255,7 +3342,7 @@ static int request_proxy_anew(REQUEST *request)
        request->proxy->data = NULL;
        request->proxy->data_len = 0;
 
-       if (request_proxy(request, 1) != 1) goto post_proxy_fail;
+       if (request_proxy(request) != 1) goto post_proxy_fail;
 
        return 1;
 }
@@ -3276,20 +3363,22 @@ static void request_ping(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_TIMER:
-               ERROR("No response to status check %d for home server %s port %d",
+               ERROR("No response to status check %d ID %u for home server %s port %d",
                       request->number,
+                      request->proxy->id,
                       inet_ntop(request->proxy->dst_ipaddr.af,
                                 &request->proxy->dst_ipaddr.ipaddr,
                                 buffer, sizeof(buffer)),
                       request->proxy->dst_port);
+               remove_from_proxy_hash(request);
                break;
 
        case FR_ACTION_PROXY_REPLY:
                rad_assert(request->in_proxy_hash);
 
                request->home_server->num_received_pings++;
-               RPROXY("Received response to status check %d (%d in current sequence)",
-                      request->number, home->num_received_pings);
+               RPROXY("Received response to status check %d ID %u (%d in current sequence)",
+                      request->number, request->proxy->id, home->num_received_pings);
 
                /*
                 *      Remove the request from any hashes
@@ -3320,21 +3409,7 @@ static void request_ping(REQUEST *request, int action)
                 *      Mark it alive and delete any outstanding
                 *      pings.
                 */
-               home->state = HOME_STATE_ALIVE;
-               home->response_timeouts = 0;
-               exec_trigger(request, home->cs, "home_server.alive", false);
-               home->currently_outstanding = 0;
-               home->num_sent_pings = 0;
-               home->num_received_pings = 0;
-               gettimeofday(&home->revive_time, NULL);
-
-               fr_event_delete(el, &home->ev);
-
-               RPROXY("Marking home server %s port %d alive",
-                      inet_ntop(request->proxy->dst_ipaddr.af,
-                                &request->proxy->dst_ipaddr.ipaddr,
-                                buffer, sizeof(buffer)),
-                      request->proxy->dst_port);
+               mark_home_server_alive(request, home);
                break;
 
        default:
@@ -3343,12 +3418,33 @@ static void request_ping(REQUEST *request, int action)
        }
 
        rad_assert(!request->in_request_hash);
+       rad_assert(!request->in_proxy_hash);
        rad_assert(request->ev == NULL);
        NO_CHILD_THREAD;
        request_done(request, FR_ACTION_DONE);
 }
 
 /*
+ *     Add +/- 2s of jitter, as suggested in RFC 3539
+ *     and in RFC 5080.
+ */
+static void add_jitter(struct timeval *when)
+{
+       uint32_t jitter;
+
+       when->tv_sec -= 2;
+
+       jitter = fr_rand();
+       jitter ^= (jitter >> 10);
+       jitter &= ((1 << 22) - 1); /* 22 bits of 1 */
+
+       /*
+        *      Add in ~ (4 * USEC) of jitter.
+        */
+       tv_add(when, jitter);
+}
+
+/*
  *     Called from start of zombie period, OR after control socket
  *     marks the home server dead.
  */
@@ -3360,9 +3456,6 @@ static void ping_home_server(void *ctx)
        struct timeval when, now;
 
        if ((home->state == HOME_STATE_ALIVE) ||
-#ifdef WITH_TCP
-           (home->proto == IPPROTO_TCP) ||
-#endif
            (home->ev != NULL)) {
                return;
        }
@@ -3413,46 +3506,55 @@ static void ping_home_server(void *ctx)
        if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
                request->proxy->code = PW_CODE_STATUS_SERVER;
 
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "Message-Authenticator", "0x00", T_OP_SET);
 
-       } else if (home->type == HOME_TYPE_AUTH) {
+       } else if ((home->type == HOME_TYPE_AUTH) ||
+                  (home->type == HOME_TYPE_AUTH_ACCT)) {
                request->proxy->code = PW_CODE_ACCESS_REQUEST;
 
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "User-Name", home->ping_user_name, T_OP_SET);
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "User-Password", home->ping_user_password, T_OP_SET);
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "Service-Type", "Authenticate-Only", T_OP_SET);
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "Message-Authenticator", "0x00", T_OP_SET);
 
-       } else {
 #ifdef WITH_ACCOUNTING
+       } else if (home->type == HOME_TYPE_ACCT) {
                request->proxy->code = PW_CODE_ACCOUNTING_REQUEST;
 
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "User-Name", home->ping_user_name, T_OP_SET);
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "Acct-Status-Type", "Stop", T_OP_SET);
-               pairmake(request->proxy, &request->proxy->vps,
+               fr_pair_make(request->proxy, &request->proxy->vps,
                         "Acct-Session-Id", "00000000", T_OP_SET);
-               vp = pairmake(request->proxy, &request->proxy->vps,
+               vp = fr_pair_make(request->proxy, &request->proxy->vps,
                              "Event-Timestamp", "0", T_OP_SET);
                vp->vp_date = now.tv_sec;
-#else
-               rad_assert("Internal sanity check failed");
 #endif
+
+       } else {
+               /*
+                *      Unkown home server type.
+                */
+               talloc_free(request);
+               return;
        }
 
-       vp = pairmake(request->proxy, &request->proxy->vps,
+       vp = fr_pair_make(request->proxy, &request->proxy->vps,
                      "NAS-Identifier", "", T_OP_SET);
        if (vp) {
-               pairsprintf(vp, "Status Check %u. Are you alive?",
+               fr_pair_value_sprintf(vp, "Status Check %u. Are you alive?",
                            home->num_sent_pings);
        }
 
+#ifdef WITH_TCP
+       request->proxy->proto = home->proto;
+#endif
        request->proxy->src_ipaddr = home->src_ipaddr;
        request->proxy->dst_ipaddr = home->ipaddr;
        request->proxy->dst_port = home->port;
@@ -3495,6 +3597,7 @@ static void ping_home_server(void *ctx)
        home->num_sent_pings++;
 
        rad_assert(request->proxy_listener != NULL);
+       debug_packet(request, request->proxy, false);
        request->proxy_listener->send(request->proxy_listener,
                                      request);
 
@@ -3536,13 +3639,6 @@ static void mark_home_server_zombie(home_server_t *home, struct timeval *now, st
        rad_assert((home->state == HOME_STATE_ALIVE) ||
                   (home->state == HOME_STATE_UNKNOWN));
 
-#ifdef WITH_TCP
-       if (home->proto == IPPROTO_TCP) {
-               WARN("Not marking TCP server %s zombie", home->log_name);
-               return;
-       }
-#endif
-
        /*
         *      We've received a real packet recently.  Don't mark the
         *      server as zombie until we've received NO packets for a
@@ -3554,7 +3650,7 @@ static void mark_home_server_zombie(home_server_t *home, struct timeval *now, st
         */
        start = now->tv_sec - ((home->zombie_period + 3) / 4);
        if (home->last_packet_recv >= start) {
-               DEBUG("Recieved reply from home server %d seconds ago.  Might not be zombie.",
+               DEBUG("Received reply from home server %d seconds ago.  Might not be zombie.",
                      (int) (now->tv_sec - home->last_packet_recv));
                return;
        }
@@ -3588,10 +3684,6 @@ void revive_home_server(void *ctx)
        home_server_t *home = talloc_get_type_abort(ctx, home_server_t);
        char buffer[128];
 
-#ifdef WITH_TCP
-       rad_assert(home->proto != IPPROTO_TCP);
-#endif
-
        home->state = HOME_STATE_ALIVE;
        home->response_timeouts = 0;
        home_trigger(home, "home_server.alive");
@@ -3615,13 +3707,6 @@ void mark_home_server_dead(home_server_t *home, struct timeval *when)
        int previous_state = home->state;
        char buffer[128];
 
-#ifdef WITH_TCP
-       if (home->proto == IPPROTO_TCP) {
-               WARN("Not marking TCP server dead");
-               return;
-       }
-#endif
-
        PROXY( "Marking home server %s port %d as dead.",
               inet_ntop(home->ipaddr.af, &home->ipaddr.ipaddr,
                         buffer, sizeof(buffer)),
@@ -3741,7 +3826,7 @@ static void proxy_wait_for_reply(REQUEST *request, int action)
                 *      More than one retransmit a second is stupid,
                 *      and should be suppressed by the proxy.
                 */
-               when = request->proxy_retransmit;
+               when = request->proxy->timestamp;
                when.tv_sec++;
 
                if (timercmp(&now, &when, <)) {
@@ -3760,7 +3845,7 @@ static void proxy_wait_for_reply(REQUEST *request, int action)
                 *      get a new ID.
                 */
                if ((request->packet->code == PW_CODE_ACCOUNTING_REQUEST) &&
-                   pairfind(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
+                   fr_pair_find_by_num(request->proxy->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY)) {
                        request_proxy_anew(request);
                        return;
                }
@@ -3777,7 +3862,7 @@ static void proxy_wait_for_reply(REQUEST *request, int action)
                rad_assert(request->proxy_listener != NULL);
                FR_STATS_TYPE_INC(home->stats.total_requests);
                home->last_packet_sent = now.tv_sec;
-               request->proxy_retransmit = now;
+               request->proxy->timestamp = now;
                debug_packet(request, request->proxy, false);
                request->proxy_listener->send(request->proxy_listener, request);
                break;
@@ -3834,18 +3919,11 @@ static void proxy_wait_for_reply(REQUEST *request, int action)
                 *      "response_window", then mark the home server
                 *      as zombie.
                 *
-                *      If the connection is TCP, then another
-                *      "watchdog timer" function takes care of pings,
-                *      etc.  So we don't need to do it here.
-                *
                 *      This check should really be part of a home
                 *      server state machine.
                 */
                if (((home->state == HOME_STATE_ALIVE) ||
                     (home->state == HOME_STATE_UNKNOWN))
-#ifdef WITH_TCP
-                   && (home->proto != IPPROTO_TCP)
-#endif
                        ) {
                        home->response_timeouts++;
                        if (home->response_timeouts >= home->max_response_timeouts)
@@ -3863,6 +3941,17 @@ static void proxy_wait_for_reply(REQUEST *request, int action)
                        FR_STATS_TYPE_INC(proxy_acct_stats.total_timeouts);
                }
 #endif
+#ifdef WITH_COA
+               else if (home->type == HOME_TYPE_COA) {
+                       if (request->proxy_listener) FR_STATS_TYPE_INC(request->proxy_listener->stats.total_timeouts);
+
+                       if (request->packet->code == PW_CODE_COA_REQUEST) {
+                               FR_STATS_TYPE_INC(proxy_coa_stats.total_timeouts);
+                       } else {
+                               FR_STATS_TYPE_INC(proxy_dsc_stats.total_timeouts);
+                       }
+               }
+#endif
 
                /*
                 *      There was no response within the window.  Stop
@@ -3946,9 +4035,9 @@ static void request_coa_originate(REQUEST *request)
        /*
         *      Check whether we want to originate one, or cancel one.
         */
-       vp = pairfind(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_SEND_COA_REQUEST, 0, TAG_ANY);
        if (!vp) {
-               vp = pairfind(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
+               vp = fr_pair_find_by_num(request->coa->proxy->vps, PW_SEND_COA_REQUEST, 0, TAG_ANY);
        }
 
        if (vp) {
@@ -3959,22 +4048,28 @@ static void request_coa_originate(REQUEST *request)
                }
        }
 
+       if (!main_config.proxy_requests) {
+               RWDEBUG("Cannot originate CoA packets unless 'proxy_requests = yes'");
+                       TALLOC_FREE(request->coa);
+               return;
+       }
+
        coa = request->coa;
 
        /*
         *      src_ipaddr will be set up in proxy_encode.
         */
        memset(&ipaddr, 0, sizeof(ipaddr));
-       vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
        if (vp) {
                ipaddr.af = AF_INET;
                ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
                ipaddr.prefix = 32;
-       } else if ((vp = pairfind(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
+       } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY)) != NULL) {
                ipaddr.af = AF_INET6;
                ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
                ipaddr.prefix = 128;
-       } else if ((vp = pairfind(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
+       } else if ((vp = fr_pair_find_by_num(coa->proxy->vps, PW_HOME_SERVER_POOL, 0, TAG_ANY)) != NULL) {
                coa->home_pool = home_pool_byname(vp->vp_strvalue,
                                                  HOME_TYPE_COA);
                if (!coa->home_pool) {
@@ -4014,7 +4109,7 @@ static void request_coa_originate(REQUEST *request)
        } else if (!coa->home_server) {
                uint16_t port = PW_COA_UDP_PORT;
 
-               vp = pairfind(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
+               vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_DST_PORT, 0, TAG_ANY);
                if (vp) port = vp->vp_integer;
 
                coa->home_server = home_server_find(&ipaddr, port, IPPROTO_UDP);
@@ -4026,7 +4121,7 @@ static void request_coa_originate(REQUEST *request)
                }
        }
 
-       vp = pairfind(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(coa->proxy->vps, PW_PACKET_TYPE, 0, TAG_ANY);
        if (vp) {
                switch (vp->vp_integer) {
                case PW_CODE_COA_REQUEST:
@@ -4054,7 +4149,7 @@ static void request_coa_originate(REQUEST *request)
        coa->packet = rad_copy_packet(coa, request->packet);
        coa->reply = rad_copy_packet(coa, request->reply);
 
-       coa->config = paircopy(coa, request->config);
+       coa->config = fr_pair_list_copy(coa, request->config);
        coa->num_coa_requests = 0;
        coa->handle = null_handler;
        coa->number = request->number; /* it's associated with the same request */
@@ -4062,7 +4157,7 @@ static void request_coa_originate(REQUEST *request)
        /*
         *      Call the pre-proxy routines.
         */
-       vp = pairfind(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_PRE_PROXY_TYPE, 0, TAG_ANY);
        if (vp) {
                DICT_VALUE const *dval = dict_valbyattr(vp->da->attr, vp->da->vendor, vp->vp_integer);
                /* Must be a validation issue */
@@ -4311,18 +4406,27 @@ static void coa_wait_for_reply(REQUEST *request, int action)
        ASSERT_MASTER;
        CHECK_FOR_STOP;
 
+       if (request->parent) coa_separate(request);
+
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               if (request_max_time(request)) break;
 
-               if (request->parent) coa_separate(request);
+               /*
+                *      Don't do fail-over.  This is a 3.1 feature.
+                */
+               if (!request->home_server ||
+                   (request->home_server->state == HOME_STATE_IS_DEAD) ||
+                   !request->proxy_listener ||
+                   (request->proxy_listener->status >= RAD_LISTEN_STATUS_EOL)) {
+                       request_done(request, FR_ACTION_DONE);
+                       break;
+               }
 
                coa_retransmit(request);
                break;
 
        case FR_ACTION_PROXY_REPLY:
-               if (request->parent) coa_separate(request);
-
                request_queue_or_run(request, coa_running);
                break;
 
@@ -4388,7 +4492,7 @@ static void coa_no_reply(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_PROXY_REPLY: /* too late! */
@@ -4437,7 +4541,7 @@ static void coa_running(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_RUN:
@@ -4606,7 +4710,6 @@ static void listener_free_cb(void *ctx)
        rad_assert(this->next == NULL);
        talloc_free(this);
 }
-#endif
 
 #ifdef WITH_PROXY
 static int proxy_eol_cb(void *ctx, void *data)
@@ -4646,7 +4749,8 @@ static int proxy_eol_cb(void *ctx, void *data)
         */
        return 0;
 }
-#endif
+#endif /* WITH_PROXY */
+#endif /* WITH_TCP */
 
 static int event_new_fd(rad_listen_t *this)
 {
@@ -4664,11 +4768,14 @@ static int event_new_fd(rad_listen_t *this)
                rad_assert(sock != NULL);
                if (just_started) {
                        DEBUG("Listening on %s", buffer);
+               } else {
+                       INFO(" ... adding new socket %s", buffer);
+               }
 
 #ifdef WITH_PROXY
-               } else if (this->type == RAD_LISTEN_PROXY) {
+               if (!just_started && (this->type == RAD_LISTEN_PROXY)) {
                        home_server_t *home;
-
+                       
                        home = sock->home;
                        if (!home || !home->limit.max_connections) {
                                INFO(" ... adding new socket %s", buffer);
@@ -4678,8 +4785,6 @@ static int event_new_fd(rad_listen_t *this)
                        }
 
 #endif
-               } else {
-                       INFO(" ... adding new socket %s", buffer);
                }
 
                switch (this->type) {
@@ -4728,7 +4833,7 @@ static int event_new_fd(rad_listen_t *this)
                                        rad_panic("Failed to insert event");
                                }
                        }
-#endif
+#endif /* WITH_TCP */
                        break;
 #endif /* WITH_PROXY */
 
@@ -4755,7 +4860,7 @@ static int event_new_fd(rad_listen_t *this)
                                        fr_exit(1);
                                }
                        }
-#endif
+#endif /* WITH_TCP */
                        break;
                } /* switch over listener types */
 
@@ -4832,7 +4937,7 @@ static int event_new_fd(rad_listen_t *this)
                        }
                        PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
                }
-#endif
+#endif /* WITH_PROXY */
 
                /*
                 *      Requests are still using the socket.  Wait for
@@ -4864,7 +4969,7 @@ static int event_new_fd(rad_listen_t *this)
                 */
                this->status = RAD_LISTEN_STATUS_REMOVE_NOW;
        } /* socket is at EOL */
-#endif
+#endif   /* WITH_TCP */
 
        /*
         *      Nuke the socket.
@@ -4873,8 +4978,8 @@ static int event_new_fd(rad_listen_t *this)
                int devnull;
 #ifdef WITH_TCP
                listen_socket_t *sock = this->data;
-#endif
                struct timeval when;
+#endif
 
                /*
                 *      Re-open the socket, pointing it to /dev/null.
@@ -4935,7 +5040,7 @@ static int event_new_fd(rad_listen_t *this)
                        }
                        PTHREAD_MUTEX_UNLOCK(&proxy_mutex);
                } else
-#endif
+#endif /* WITH_PROXY */
                {
                        INFO(" ... shutting down socket %s", buffer);
 
@@ -4966,8 +5071,8 @@ static int event_new_fd(rad_listen_t *this)
                                     &(sock->ev))) {
                        rad_panic("Failed to insert event");
                }
-       }
 #endif /* WITH_TCP */
+       }
 
        return 1;
 }
@@ -5071,6 +5176,10 @@ static void handle_signal_self(int flag)
 #ifndef HAVE_PTHREAD_H
 void radius_signal_self(int flag)
 {
+       if (flag == RADIUS_SIGNAL_SELF_TERM) {
+               main_config.exiting = true;
+       }
+
        return handle_signal_self(flag);
 }
 
@@ -5085,6 +5194,10 @@ void radius_signal_self(int flag)
        ssize_t rcode;
        uint8_t buffer[16];
 
+       if (flag == RADIUS_SIGNAL_SELF_TERM) {
+               main_config.exiting = true;
+       }
+
        /*
         *      The read MUST be non-blocking for this to work.
         */
@@ -5149,6 +5262,119 @@ static int packet_entry_cmp(void const *one, void const *two)
        return fr_packet_cmp(*a, *b);
 }
 
+#ifdef WITH_PROXY
+/*
+ *     They haven't defined a proxy listener.  Automatically
+ *     add one for them, with the correct address family.
+ */
+static void create_default_proxy_listener(int af)
+{
+       uint16_t        port = 0;
+       home_server_t   home;
+       listen_socket_t *sock;
+       rad_listen_t    *this;
+
+       memset(&home, 0, sizeof(home));
+
+       /*
+        *      Open a default UDP port
+        */
+       home.proto = IPPROTO_UDP;
+       port = 0;
+
+       /*
+        *      Set the address family.
+        */
+       home.src_ipaddr.af = af;
+       home.ipaddr.af = af;
+
+       /*
+        *      Get the correct listener.
+        */
+       this = proxy_new_listener(proxy_ctx, &home, port);
+       if (!this) {
+               fr_exit_now(1);
+       }
+
+       sock = this->data;
+       if (!fr_packet_list_socket_add(proxy_list, this->fd,
+                                      sock->proto,
+                                      &sock->other_ipaddr, sock->other_port,
+                                      this)) {
+               ERROR("Failed adding proxy socket");
+               fr_exit_now(1);
+       }
+
+       /*
+        *      Insert the FD into list of FDs to listen on.
+        */
+       radius_update_listener(this);
+}
+
+/*
+ *     See if we automatically need to open a proxy socket.
+ */
+static void check_proxy(rad_listen_t *head)
+{
+       bool            defined_proxy;
+       bool            has_v4, has_v6;
+       rad_listen_t    *this;
+
+       if (check_config) return;
+       if (!main_config.proxy_requests) return;
+       if (!head) return;
+       if (!home_servers_udp) return;
+
+       /*
+        *      We passed "-i" on the command line.  Use that address
+        *      family for the proxy socket.
+        */
+       if (main_config.myip.af != AF_UNSPEC) {
+               create_default_proxy_listener(main_config.myip.af);
+               return;
+       }
+
+       defined_proxy = has_v4 = has_v6 = false;
+
+       /*
+        *      Figure out if we need to open a proxy socket, and if
+        *      so, which one.
+        */
+       for (this = head; this != NULL; this = this->next) {
+               listen_socket_t *sock;
+
+               switch (this->type) {
+               case RAD_LISTEN_PROXY:
+                       defined_proxy = true;
+                       break;
+
+               case RAD_LISTEN_AUTH:
+#ifdef WITH_ACCT
+               case RAD_LISTEN_ACCT:
+#endif
+#ifdef WITH_COA
+               case RAD_LISTEN_COA:
+#endif
+                       sock = this->data;
+                       if (sock->my_ipaddr.af == AF_INET) has_v4 = true;
+                       if (sock->my_ipaddr.af == AF_INET6) has_v6 = true;
+                       break;
+                       
+               default:
+                       break;
+               }
+       }
+
+       /*
+        *      Assume they know what they're doing.
+        */
+       if (defined_proxy) return;
+
+       if (has_v4) create_default_proxy_listener(AF_INET);
+
+       if (has_v6) create_default_proxy_listener(AF_INET6);
+}
+#endif
 
 int radius_event_start(CONF_SECTION *cs, bool have_children)
 {
@@ -5275,10 +5501,14 @@ int radius_event_start(CONF_SECTION *cs, bool have_children)
 
        main_config.listen = head;
 
+#ifdef WITH_PROXY
+       check_proxy(head);
+#endif
+
        /*
-             At this point, no one has any business *ever* going
-             back to root uid.
-       */
+        *      At this point, no one has any business *ever* going
+        *      back to root uid.
+        */
        rad_suid_down_permanent();
 
        return 1;