2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <trust_router/tr_name.h>
46 #include <trp_internal.h>
48 #include <trp_ptable.h>
49 #include <trp_rtable.h>
53 static int trps_destructor(void *object)
55 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
56 if (trps->rtable!=NULL)
57 trp_rtable_free(trps->rtable);
61 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
63 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
70 trps->update_interval=(struct timeval){0,0};
71 trps->sweep_interval=(struct timeval){0,0};
74 trps->mq=tr_mq_new(trps);
76 /* failed to allocate mq */
82 if (trps_init_rtable(trps) != TRP_SUCCESS) {
83 /* failed to allocate rtable */
88 talloc_set_destructor((void *)trps, trps_destructor);
93 /* create a new route table, first discarding an old one if necessary */
94 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
96 if (trps->rtable != NULL) {
97 trp_rtable_free(trps->rtable);
101 trps->rtable=trp_rtable_new();
102 if (trps->rtable==NULL) {
108 void trps_clear_rtable(TRPS_INSTANCE *trps)
110 trp_rtable_clear(trps->rtable);
113 void trps_free (TRPS_INSTANCE *trps)
119 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
121 return tr_mq_pop(trps->mq);
124 void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
126 tr_mq_add(trps->mq, msg);
129 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
131 return trps->connect_interval.tv_sec;
134 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
136 trps->connect_interval.tv_sec=interval;
137 trps->connect_interval.tv_usec=0;
140 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
142 return trps->update_interval.tv_sec;
145 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
147 trps->update_interval.tv_sec=interval;
148 trps->update_interval.tv_usec=0;
151 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
153 return trps->sweep_interval.tv_sec;
156 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
158 trps->sweep_interval.tv_sec=interval;
159 trps->sweep_interval.tv_usec=0;
162 void trps_set_ctable(TRPS_INSTANCE *trps, TR_COMM_TABLE *comm)
164 if (trps->ctable!=NULL)
165 tr_comm_table_free(trps->ctable);
169 void trps_set_ptable(TRPS_INSTANCE *trps, TRP_PTABLE *ptable)
171 if (trps->ptable!=NULL)
172 trp_ptable_free(trps->ptable);
176 void trps_set_peer_status_callback(TRPS_INSTANCE *trps, void (*cb)(TRP_PEER *, void *), void *cookie)
178 TRP_PTABLE_ITER *iter=NULL;
180 if (trps->ptable==NULL)
183 iter=trp_ptable_iter_new(NULL);
184 for (peer=trp_ptable_iter_first(iter, trps->ptable); peer!=NULL; peer=trp_ptable_iter_next(iter))
185 trp_peer_set_conn_status_cb(peer, cb, cookie);
186 trp_ptable_iter_free(iter);
189 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
191 TRPC_INSTANCE *cur=NULL;
193 TR_NAME *peer_servicename=trp_peer_get_servicename(peer);
195 for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
196 name=trpc_get_gssname(cur);
197 if ((name!=NULL) && (0==tr_name_cmp(peer_servicename, name))) {
204 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
206 if (trps->conn==NULL)
209 trp_connection_append(trps->conn, new);
211 talloc_steal(trps, new);
214 /* ok to call more than once; guarantees connection no longer in the list.
215 * Caller is responsible for freeing the removed element afterwards. */
216 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
218 trps->conn=trp_connection_remove(trps->conn, remove);
221 void trps_add_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
223 if (trps->trpc==NULL)
226 trpc_append(trps->trpc, trpc);
228 talloc_steal(trps, trpc);
231 /* ok to call more than once; guarantees trpc no longer in the list.
232 * Caller is responsible for freeing the removed element afterwards. */
233 void trps_remove_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *remove)
235 trps->trpc=trpc_remove(trps->trpc, remove);
238 TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
240 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
241 TR_MQ_MSG *mq_msg=NULL;
244 TRPC_INSTANCE *trpc=NULL;
246 /* get the connection for this peer */
247 trpc=trps_find_trpc(trps, peer);
248 /* instead, let's let that happen and then clear the queue when an attempt to
251 tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
253 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND, TR_MQ_PRIO_NORMAL);
254 msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
255 tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
256 trpc_mq_add(trpc, mq_msg);
259 talloc_free(tmp_ctx);
263 static int trps_listen (TRPS_INSTANCE *trps, int port)
270 struct sockaddr_storage storage;
271 struct sockaddr_in in4;
274 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
276 saddr->sin_port = htons (port);
277 saddr->sin_family = AF_INET;
278 saddr->sin_addr.s_addr = INADDR_ANY;
280 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
283 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
285 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
288 if (0 > (rc = listen(conn, 512)))
291 tr_debug("trps_listen: TRP Server listening on port %d", port);
295 /* get the currently selected route if available */
296 TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
298 return trp_rtable_get_entry(trps->rtable, comm, realm, peer);
301 TRP_ROUTE *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
303 tr_debug("trps_get_selected_route: entered. trps=%p, comm=%p, realm=%p", trps, comm, realm);
304 return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
307 /* copy the result if you want to keep it */
308 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
310 TRP_ROUTE *route=trps_get_selected_route(trps, comm, realm);
314 return trp_route_get_next_hop(route);
318 /* mark a route as retracted */
319 static void trps_retract_route(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
321 trp_route_set_metric(entry, TRP_METRIC_INFINITY);
322 trp_route_set_triggered(entry, 1);
325 /* is this route retracted? */
326 static int trps_route_retracted(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
328 return (trp_metric_is_infinite(trp_route_get_metric(entry)));
331 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
336 TRP_PEER *peer=NULL; /* entry in the peer table */
337 TR_NAME *conn_peer=NULL; /* name from the TRP_CONN, which comes from the gss context */
339 tr_debug("trps_read_message: started");
340 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
341 *(trp_connection_get_gssctx(conn)),
344 tr_debug("trps_read_message: error");
350 tr_debug("trps_read_message: message received, %u bytes.", (unsigned) buflen);
351 tr_debug("trps_read_message: %.*s", buflen, buf);
353 *msg=tr_msg_decode(buf, buflen);
358 conn_peer=trp_connection_get_peer(conn);
359 if (conn_peer==NULL) {
360 tr_err("trps_read_message: connection has no peer name");
364 peer=trps_get_peer_by_gssname(trps, conn_peer);
366 tr_err("trps_read_message: could not find peer with gssname=%s", trp_connection_get_gssname(conn));
370 /* verify we received a message we support, otherwise drop it now */
371 switch (tr_msg_get_msg_type(*msg)) {
373 trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(conn_peer));
374 trp_upd_set_next_hop(tr_msg_get_trp_upd(*msg), trp_peer_get_server(peer), 0); /* TODO: 0 should be the configured TID port */
378 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
382 tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
383 tr_msg_free_decoded(*msg);
385 return TRP_UNSUPPORTED;
391 int trps_get_listener(TRPS_INSTANCE *trps,
392 TRPS_MSG_FUNC msg_handler,
393 TRP_AUTH_FUNC auth_handler,
394 const char *hostname,
400 if (0 > (listen = trps_listen(trps, port))) {
402 if (0 == strerror_r(errno, errbuf, 256)) {
403 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
405 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
410 /* opening port succeeded */
411 tr_debug("trps_get_listener: Opened port %d.", port);
413 /* make this socket non-blocking */
414 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
415 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
422 /* store the caller's request handler & cookie */
423 trps->msg_handler = msg_handler;
424 trps->auth_handler = auth_handler;
425 trps->hostname = talloc_strdup(trps, hostname);
427 trps->cookie = cookie;
433 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
435 /* try to establish a GSS context */
436 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
437 tr_notice("trps_authorize_connection: failed to authorize connection");
438 trp_connection_close(conn);
441 tr_notice("trps_authorize_connection: authorized connection");
445 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
450 /* loop as long as the connection exists */
451 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
452 rc=trps_read_message(trps, conn, &msg);
455 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
459 trp_connection_close(conn);
463 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
467 tr_debug("trps_handle_connection: connection closed.");
470 /* TODO: check realm/comm, now part of the update instead of inforec */
471 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
474 tr_notice("trps_validate_update: null TRP update.");
478 if (trp_upd_get_realm(upd)==NULL) {
479 tr_notice("trps_validate_update: received TRP update without realm.");
483 if (trp_upd_get_comm(upd)==NULL) {
484 tr_notice("trps_validate_update: received TRP update without community.");
488 if (trp_upd_get_inforec(upd)==NULL) {
489 tr_notice("trps_validate_update: received TRP update with no info records.");
493 if (trp_upd_get_peer(upd)==NULL) {
494 tr_notice("trps_validate_update: received TRP update without origin peer information.");
502 /* ensure that the update could be accepted if feasible */
503 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
505 switch(trp_inforec_get_type(rec)) {
506 case TRP_INFOREC_TYPE_ROUTE:
507 if ((trp_inforec_get_trust_router(rec)==NULL)
508 || (trp_inforec_get_next_hop(rec)==NULL)) {
509 tr_debug("trps_validate_inforec: missing record info.");
513 /* check for valid metric */
514 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
515 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
519 /* check for valid interval */
520 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
521 tr_debug("trps_validate_inforec: invalid interval.");
527 tr_notice("trps_validate_inforec: unsupported record type.");
528 return TRP_UNSUPPORTED;
534 /* link cost to a peer */
535 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
540 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
542 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
544 return TRP_METRIC_INFINITY;
545 return trp_route_get_metric(entry) + trps_cost(trps, peer);
548 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
550 unsigned int rec_metric=trp_inforec_get_metric(rec);
551 unsigned int new_metric=0;
552 unsigned int current_metric=0;
553 TR_NAME *next_hop=NULL;
555 /* we check these in the validation stage, but just in case... */
556 if (trp_metric_is_invalid(rec_metric))
559 /* retractions (aka infinite metrics) are always feasible */
560 if (trp_metric_is_infinite(rec_metric))
563 /* updates from our current next hop are always feasible*/
564 next_hop=trps_get_next_hop(trps, comm, realm);
566 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
571 /* compare the existing metric we advertise to what we would advertise
572 * if we accept this update */
573 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
574 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
575 if (new_metric <= current_metric)
581 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
582 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
584 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
585 if (0!=clock_gettime(CLOCK_REALTIME, ts)) {
586 tr_err("trps_compute_expiry: could not read realtime clock.");
590 ts->tv_sec += small_factor*interval;
594 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
596 TRP_ROUTE *entry=NULL;
598 entry=trp_rtable_get_entry(trps->rtable,
599 trp_upd_get_comm(upd),
600 trp_upd_get_realm(upd),
601 trp_inforec_get_next_hop(rec));
603 entry=trp_route_new(NULL);
605 tr_err("trps_accept_update: unable to allocate new entry.");
609 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
610 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
611 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
612 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
613 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
614 /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
615 if ((trp_route_get_comm(entry)==NULL)
616 ||(trp_route_get_realm(entry)==NULL)
617 ||(trp_route_get_peer(entry)==NULL)
618 ||(trp_route_get_trust_router(entry)==NULL)
619 ||(trp_route_get_next_hop(entry)==NULL)) {
620 /* at least one field could not be allocated */
621 tr_err("trps_accept_update: unable to allocate all fields for entry.");
622 trp_route_free(entry);
625 trp_rtable_add(trps->rtable, entry);
628 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
629 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
630 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
631 * time unset on a new route entry. */
632 tr_debug("trps_accept_update: accepting route update.");
633 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
634 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
636 /* check whether the trust router has changed */
637 if (0!=tr_name_cmp(trp_route_get_trust_router(entry),
638 trp_inforec_get_trust_router(rec))) {
639 /* The name changed. Set this route as triggered. */
640 tr_debug("trps_accept_update: trust router for route changed.");
641 trp_route_set_triggered(entry, 1);
642 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
644 if (!trps_route_retracted(trps, entry)) {
645 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
646 trp_route_set_expiry(entry, trps_compute_expiry(trps,
647 trp_route_get_interval(entry),
648 trp_route_get_expiry(entry)));
653 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
656 TRP_INFOREC *rec=NULL;
657 TRP_ROUTE *route=NULL;
659 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
660 tr_notice("trps_handle_update: received invalid TRP update.");
664 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
665 /* validate/sanity check the record update */
666 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
667 tr_notice("trps_handle_update: invalid record in TRP update, discarding entire update.");
672 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
673 /* determine feasibility */
674 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
675 tr_debug("trps_handle_update: record feasibility=%d", feas);
677 /* do we have an existing route? */
678 route=trps_get_route(trps,
679 trp_upd_get_comm(upd),
680 trp_upd_get_realm(upd),
681 trp_upd_get_peer(upd));
683 /* there was a route table entry already */
684 tr_debug("trps_handle_updates: route entry already exists.");
686 /* Update is feasible. Accept it. */
687 trps_accept_update(trps, upd, rec);
689 /* Update is infeasible. Ignore it unless the trust router has changed. */
690 if (0!=tr_name_cmp(trp_route_get_trust_router(route),
691 trp_inforec_get_trust_router(rec))) {
692 /* the trust router associated with the route has changed, treat update as a retraction */
693 trps_retract_route(trps, route);
697 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
698 tr_debug("trps_handle_update: no route entry exists yet.");
699 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
700 trps_accept_update(trps, upd, rec);
706 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
709 tr_notice("trps_validate_request: null TRP request.");
713 if (trp_req_get_comm(req)==NULL) {
714 tr_notice("trps_validate_request: received TRP request with null community.");
718 if (trp_req_get_realm(req)==NULL) {
719 tr_notice("trps_validate_request: received TRP request with null realm.");
723 if (trp_req_get_peer(req)==NULL) {
724 tr_notice("trps_validate_request: received TRP request without origin peer information.");
731 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
732 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
735 TR_NAME *exclude_peer)
737 TRP_ROUTE **entry=NULL;
738 TRP_ROUTE *best=NULL;
741 unsigned int kk_min=0;
742 unsigned int min_metric=TRP_METRIC_INFINITY;
744 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
745 for (kk=0; kk<n_entry; kk++) {
746 if (trp_route_get_metric(entry[kk]) < min_metric) {
747 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_route_get_peer(entry[kk]),
750 min_metric=trp_route_get_metric(entry[kk]);
754 if (trp_metric_is_finite(min_metric))
761 /* TODO: think this through more carefully. At least ought to add hysteresis
762 * to avoid flapping between routers or routes. */
763 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
765 size_t n_comm=0, ii=0;
766 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
767 size_t n_realm=0, jj=0;
768 TR_NAME **realm=NULL;
769 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
770 unsigned int best_metric=0, cur_metric=0;
772 for (ii=0; ii<n_comm; ii++) {
773 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
774 for (jj=0; jj<n_realm; jj++) {
775 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
776 if (best_route==NULL)
777 best_metric=TRP_METRIC_INFINITY;
779 best_metric=trp_route_get_metric(best_route);
781 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
782 if (cur_route!=NULL) {
783 cur_metric=trp_route_get_metric(cur_route);
784 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
785 /* The new route has a lower metric than the previous, and is finite. Accept. */
786 trp_route_set_selected(cur_route, 0);
787 trp_route_set_selected(best_route, 1);
788 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
789 trp_route_set_selected(cur_route, 0);
790 } else if (trp_metric_is_finite(best_metric)) {
791 trp_route_set_selected(best_route, 1);
796 realm=NULL; n_realm=0;
805 /* true if curtime >= expiry */
806 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
808 return ((curtime->tv_sec > expiry->tv_sec)
809 || ((curtime->tv_sec == expiry->tv_sec)
810 &&(curtime->tv_nsec >= expiry->tv_nsec)));
813 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
814 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
815 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
817 struct timespec sweep_time={0,0};
818 TRP_ROUTE **entry=NULL;
822 /* use a single time for the entire sweep */
823 if (0!=clock_gettime(CLOCK_REALTIME, &sweep_time)) {
824 tr_err("trps_sweep_routes: could not read realtime clock.");
826 sweep_time.tv_nsec=0;
830 entry=trp_rtable_get_entries(trps->rtable, &n_entry); /* must talloc_free *entry */
832 /* loop over the entries */
833 for (ii=0; ii<n_entry; ii++) {
834 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
835 tr_debug("trps_sweep_routes: route expired.");
836 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
838 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
839 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
842 /* set metric to infinity and reset timer */
843 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
844 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
845 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
846 trp_route_get_interval(entry[ii]),
847 trp_route_get_expiry(entry[ii])));
856 /* select the correct route to comm/realm to be announced to peer */
857 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_gssname)
861 /* Take the currently selected route unless it is through the peer we're sending the update to.
862 * I.e., enforce the split horizon rule. */
863 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
865 /* No selected route, this should only happen if the only route has been retracted,
866 * in which case we do not want to advertise it. */
869 tr_debug("trps_select_realm_update: %s vs %s", peer_gssname->buf,
870 trp_route_get_peer(route)->buf);
871 if (0==tr_name_cmp(peer_gssname, trp_route_get_peer(route))) {
872 tr_debug("trps_select_realm_update: matched, finding alternate route");
873 /* the selected entry goes through the peer we're reporting to, choose an alternate */
874 route=trps_find_best_route(trps, comm, realm, peer_gssname);
875 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route))))
876 return NULL; /* don't advertise a nonexistent or retracted route */
881 /* Adds inforecs for route updates to the updates Garray. If it fails, it may leave
882 * some inforecs in the list. Caller needs to arrange for these to be freed. */
883 static TRP_RC trps_select_route_updates_for_peer(TALLOC_CTX *mem_ctx,
886 TR_NAME *peer_gssname,
890 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
891 TR_NAME **realm=NULL;
894 TRP_ROUTE *best=NULL;
899 for (ii=0; ii<n_comm; ii++) {
900 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
901 for (jj=0; jj<n_realm; jj++) {
902 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_gssname);
903 /* If we found a route, add it to the list. If triggered!=0, then only
904 * add triggered routes. */
905 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best)))
906 g_array_append_val(updates, best);
921 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
923 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
924 return TRP_METRIC_INVALID;
926 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
927 return TRP_METRIC_INFINITY;
929 if (trp_metric_is_finite(m1+m2))
932 return TRP_METRIC_INFINITY;
935 /* Every realm has a community, so always returns at least one record except on error. */
936 static TRP_INFOREC *trps_comm_inforecs_for_realm(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_NAME *comm_name, TR_NAME *realm)
938 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
939 /* TRP_INFOREC *results=NULL;*/
940 /* TRP_INFOREC *rec=NULL;*/
941 TR_COMM *this_comm=NULL;
942 TR_COMM_ITER *comm_iter=NULL;
944 comm_iter=tr_comm_iter_new(tmp_ctx);
945 for (this_comm=tr_comm_iter_first(comm_iter, trps->ctable, comm_name);
947 this_comm=tr_comm_iter_next(comm_iter)) {
951 talloc_free(tmp_ctx);
955 /* convert an rentry into a new trp update info record */
956 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
958 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
959 unsigned int linkcost=0;
962 if (trp_route_is_local(route))
965 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
966 trp_route_get_peer(route)));
969 /* Note that we leave the next hop empty since the recipient fills that in.
970 * This is where we add the link cost (currently always 1) to the next peer. */
971 if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
972 ||(trp_inforec_set_metric(rec,
973 trps_metric_add(trp_route_get_metric(route),
974 linkcost)) != TRP_SUCCESS)
975 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
976 tr_err("trps_route_to_inforec: error creating route update.");
984 /* all routes to a single peer, unless comm/realm are specified (both or neither must be NULL) */
985 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
987 TRP_UPDATE_TYPE update_type,
991 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
992 TR_MSG msg; /* not a pointer! */
994 TRP_INFOREC *rec=NULL;
995 TRP_ROUTE *route=NULL;
999 TR_NAME *peer_label=trp_peer_get_label(peer);
1000 GArray *updates=NULL;
1002 switch (update_type) {
1003 case TRP_UPDATE_TRIGGERED:
1004 tr_debug("trps_update_one_peer: preparing triggered update for %.*s",
1005 peer_label->len, peer_label->buf);
1007 case TRP_UPDATE_SCHEDULED:
1008 tr_debug("trps_update_one_peer: preparing scheduled update for %.*s",
1009 peer_label->len, peer_label->buf);
1011 case TRP_UPDATE_REQUESTED:
1012 tr_debug("trps_update_one_peer: preparing requested update for %.*s",
1013 peer_label->len, peer_label->buf);
1016 /* allocate updates array */
1017 updates=g_array_new(TRUE, FALSE, sizeof(TRP_ROUTE *));
1018 /* not setting the array to free entries when we free it, we will
1019 * have to do that ourselves */
1021 /* do not fill in peer, recipient does that */
1022 if ((comm==NULL) && (realm==NULL)) {
1024 rc=trps_select_route_updates_for_peer(tmp_ctx,
1028 update_type==TRP_UPDATE_TRIGGERED);
1029 } else if ((comm!=NULL) && (realm!=NULL)) {
1030 /* a single community/realm was requested */
1031 route=trps_select_realm_update(trps, comm, realm, peer_label);
1033 g_array_append_val(updates, route);
1035 /* we have no actual update to send back, MUST send a retraction */
1036 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1037 route=trp_route_new(tmp_ctx);
1038 trp_route_set_comm(route, tr_dup_name(comm));
1039 trp_route_set_realm(route, tr_dup_name(realm));
1040 trp_route_set_peer(route, tr_new_name(""));
1041 trp_route_set_metric(route, TRP_METRIC_INFINITY);
1042 trp_route_set_trust_router(route, tr_new_name(""));
1043 trp_route_set_next_hop(route, tr_new_name(""));
1044 g_array_append_val(updates, route);
1047 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1052 /* see if we have anything to send */
1053 if (updates->len<=0)
1054 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1056 tr_debug("trps_update_one_peer: sending %d update messages.", updates->len);
1057 for (ii=0; NULL!=(route=g_array_index(updates, TRP_ROUTE *, ii)); ii++) {
1058 upd=trp_upd_new(tmp_ctx);
1060 tr_err("trps_update_one_peer: could not create update message.");
1064 trp_upd_set_realm(upd, trp_route_dup_realm(route));
1065 if (trp_upd_get_realm(upd)==NULL) {
1066 tr_err("trps_update_one_peer: could not copy realm.");
1070 trp_upd_set_comm(upd, trp_route_dup_comm(route));
1071 if (trp_upd_get_comm(upd)==NULL) {
1072 tr_err("trps_update_one_peer: could not copy comm.");
1076 rec=trps_route_to_inforec(tmp_ctx, trps, route);
1078 tr_err("trps_update_one_peer: could not create route info record for realm %.*s in comm %.*s.",
1079 realm->len, realm->buf,
1080 comm->len, comm->buf);
1084 trp_upd_add_inforec(upd, rec);
1086 /* now add community info records */
1087 rec=trps_comm_inforecs_for_realm(tmp_ctx,
1089 trp_route_get_comm(route),
1090 trp_route_get_realm(route));
1092 tr_err("trps_update_one_peer: could not create all update records.");
1096 trp_upd_add_inforec(upd, rec);
1098 /* now encode the update message */
1099 tr_msg_set_trp_upd(&msg, upd);
1100 encoded=tr_msg_encode(&msg);
1101 if (encoded==NULL) {
1102 tr_err("trps_update_one_peer: error encoding update.");
1107 tr_debug("trps_update_one_peer: adding message to queue.");
1108 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1109 tr_err("trps_update_one_peer: error queueing update.");
1111 tr_debug("trps_update_one_peer: update queued successfully.");
1113 tr_msg_free_encoded(encoded);
1118 g_array_free(updates, TRUE);
1124 talloc_free(tmp_ctx);
1128 /* all routes/communities to all peers */
1129 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1131 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1132 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1133 TRP_PEER *peer=NULL;
1134 TRP_RC rc=TRP_SUCCESS;
1136 if (trps->ptable==NULL)
1137 return TRP_SUCCESS; /* no peers, nothing to do */
1140 tr_err("trps_update: failed to allocate peer table iterator.");
1141 talloc_free(tmp_ctx);
1145 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1146 peer!=NULL && rc==TRP_SUCCESS;
1147 peer=trp_ptable_iter_next(iter))
1149 if (!trps_peer_connected(trps, peer)) {
1150 TR_NAME *peer_label=trp_peer_get_label(peer);
1151 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1152 peer_label->len, peer_label->buf);
1155 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1158 tr_debug("trps_update: rc=%u after attempting update.", rc);
1159 trp_ptable_iter_free(iter);
1160 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1161 talloc_free(tmp_ctx);
1165 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1167 trp_rtable_add(trps->rtable, route); /* should return status */
1171 /* steals the peer object */
1172 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1174 if (trps->ptable==NULL) {
1175 trps->ptable=trp_ptable_new(trps);
1176 if (trps->ptable==NULL)
1179 return trp_ptable_add(trps->ptable, peer);
1182 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1184 if (trps->ptable==NULL)
1187 return trp_ptable_find_gss_name(trps->ptable, gssname);
1190 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1192 if (trps->ptable==NULL)
1195 return trp_ptable_find_servicename(trps->ptable, servicename);
1198 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1200 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
1204 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
1211 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1214 TR_NAME *realm=NULL;
1216 tr_debug("trps_handle_request: handling TRP request.");
1218 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
1219 tr_notice("trps_handle_request: received invalid TRP request.");
1223 if (!trp_req_is_wildcard(req)) {
1224 comm=trp_req_get_comm(req);
1225 realm=trp_req_get_realm(req);
1226 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
1227 comm->len, comm->buf, realm->len, realm->buf);
1229 tr_debug("trps_handle_request: all routes requested.");
1230 /* leave comm/realm NULL */
1232 return trps_update_one_peer(trps,
1233 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
1234 TRP_UPDATE_REQUESTED,
1240 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
1242 TRP_RC rc=TRP_ERROR;
1244 switch (tr_msg_get_msg_type(tr_msg)) {
1246 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
1247 if (rc==TRP_SUCCESS) {
1248 rc=trps_update_active_routes(trps);
1249 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
1254 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
1258 /* unknown error or one we don't care about (e.g., TID messages) */
1263 /* send wildcard route request to a peer */
1264 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
1266 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1267 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
1268 TR_MSG msg; /* not a pointer */
1269 TRP_REQ *req=trp_req_new(tmp_ctx);
1271 TRP_RC rc=TRP_ERROR;
1274 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
1278 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
1279 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
1284 tr_msg_set_trp_req(&msg, req);
1285 encoded=tr_msg_encode(&msg);
1286 if (encoded==NULL) {
1287 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
1292 tr_debug("trps_wildcard_route_req: adding message to queue.");
1293 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
1294 tr_err("trps_wildcard_route_req: error queueing request.");
1297 tr_debug("trps_wildcard_route_req: request queued successfully.");
1303 tr_msg_free_encoded(encoded);
1307 talloc_free(tmp_ctx);