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.
43 #include <trust_router/tr_name.h>
44 #include <trp_internal.h>
46 #include <trp_ptable.h>
47 #include <trp_rtable.h>
51 static int trps_destructor(void *object)
53 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
54 if (trps->rtable!=NULL)
55 trp_rtable_free(trps->rtable);
59 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
61 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
68 trps->update_interval=(struct timeval){0,0};
69 trps->sweep_interval=(struct timeval){0,0};
72 trps->mq=tr_mq_new(trps);
74 /* failed to allocate mq */
80 if (trps_init_rtable(trps) != TRP_SUCCESS) {
81 /* failed to allocate rtable */
86 talloc_set_destructor((void *)trps, trps_destructor);
91 /* create a new route table, first discarding an old one if necessary */
92 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
94 if (trps->rtable != NULL) {
95 trp_rtable_free(trps->rtable);
99 trps->rtable=trp_rtable_new();
100 if (trps->rtable==NULL) {
106 void trps_clear_rtable(TRPS_INSTANCE *trps)
108 trp_rtable_clear(trps->rtable);
111 void trps_free (TRPS_INSTANCE *trps)
117 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
119 return tr_mq_pop(trps->mq);
122 void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
124 tr_mq_add(trps->mq, msg);
127 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
129 return trps->connect_interval.tv_sec;
132 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
134 trps->connect_interval.tv_sec=interval;
135 trps->connect_interval.tv_usec=0;
138 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
140 return trps->update_interval.tv_sec;
143 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
145 trps->update_interval.tv_sec=interval;
146 trps->update_interval.tv_usec=0;
149 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
151 return trps->sweep_interval.tv_sec;
154 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
156 trps->sweep_interval.tv_sec=interval;
157 trps->sweep_interval.tv_usec=0;
160 void trps_set_ptable(TRPS_INSTANCE *trps, TRP_PTABLE *ptable)
162 if (trps->ptable!=NULL)
163 trp_ptable_free(trps->ptable);
167 void trps_set_peer_status_callback(TRPS_INSTANCE *trps, void (*cb)(TRP_PEER *, void *), void *cookie)
169 TRP_PTABLE_ITER *iter=NULL;
171 if (trps->ptable==NULL)
174 iter=trp_ptable_iter_new(NULL);
175 for (peer=trp_ptable_iter_first(iter, trps->ptable); peer!=NULL; peer=trp_ptable_iter_next(iter))
176 trp_peer_set_conn_status_cb(peer, cb, cookie);
177 trp_ptable_iter_free(iter);
180 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
182 TRPC_INSTANCE *cur=NULL;
184 TR_NAME *peer_servicename=trp_peer_get_servicename(peer);
186 for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
187 name=trpc_get_gssname(cur);
188 if ((name!=NULL) && (0==tr_name_cmp(peer_servicename, name))) {
195 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
197 if (trps->conn==NULL)
200 trp_connection_append(trps->conn, new);
202 talloc_steal(trps, new);
205 /* ok to call more than once; guarantees connection no longer in the list.
206 * Caller is responsible for freeing the removed element afterwards. */
207 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
209 trps->conn=trp_connection_remove(trps->conn, remove);
212 void trps_add_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
214 if (trps->trpc==NULL)
217 trpc_append(trps->trpc, trpc);
219 talloc_steal(trps, trpc);
222 /* ok to call more than once; guarantees trpc no longer in the list.
223 * Caller is responsible for freeing the removed element afterwards. */
224 void trps_remove_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *remove)
226 trps->trpc=trpc_remove(trps->trpc, remove);
229 TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
231 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
232 TR_MQ_MSG *mq_msg=NULL;
235 TRPC_INSTANCE *trpc=NULL;
237 /* get the connection for this peer */
238 trpc=trps_find_trpc(trps, peer);
239 /* instead, let's let that happen and then clear the queue when an attempt to
242 tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
244 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND, TR_MQ_PRIO_NORMAL);
245 msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
246 tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
247 trpc_mq_add(trpc, mq_msg);
250 talloc_free(tmp_ctx);
254 static int trps_listen (TRPS_INSTANCE *trps, int port)
261 struct sockaddr_storage storage;
262 struct sockaddr_in in4;
265 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
267 saddr->sin_port = htons (port);
268 saddr->sin_family = AF_INET;
269 saddr->sin_addr.s_addr = INADDR_ANY;
271 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
274 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
276 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
279 if (0 > (rc = listen(conn, 512)))
282 tr_debug("trps_listen: TRP Server listening on port %d", port);
286 /* get the currently selected route if available */
287 TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
289 return trp_rtable_get_entry(trps->rtable, comm, realm, peer);
292 TRP_ROUTE *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
294 tr_debug("trps_get_selected_route: entered. trps=%p, comm=%p, realm=%p", trps, comm, realm);
295 return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
298 /* copy the result if you want to keep it */
299 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
301 TRP_ROUTE *route=trps_get_selected_route(trps, comm, realm);
305 return trp_route_get_next_hop(route);
309 /* mark a route as retracted */
310 static void trps_retract_route(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
312 trp_route_set_metric(entry, TRP_METRIC_INFINITY);
313 trp_route_set_triggered(entry, 1);
316 /* is this route retracted? */
317 static int trps_route_retracted(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
319 return (trp_metric_is_infinite(trp_route_get_metric(entry)));
322 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
327 TRP_PEER *peer=NULL; /* entry in the peer table */
328 TR_NAME *conn_peer=NULL; /* name from the TRP_CONN, which comes from the gss context */
330 tr_debug("trps_read_message: started");
331 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
332 *(trp_connection_get_gssctx(conn)),
335 tr_debug("trps_read_message: error");
341 tr_debug("trps_read_message: message received, %u bytes.", (unsigned) buflen);
342 tr_debug("trps_read_message: %.*s", buflen, buf);
344 *msg=tr_msg_decode(buf, buflen);
349 conn_peer=trp_connection_get_peer(conn);
350 if (conn_peer==NULL) {
351 tr_err("trps_read_message: connection has no peer name");
355 peer=trps_get_peer_by_gssname(trps, conn_peer);
357 tr_err("trps_read_message: could not find peer with gssname=%s", trp_connection_get_gssname(conn));
361 /* verify we received a message we support, otherwise drop it now */
362 switch (tr_msg_get_msg_type(*msg)) {
364 trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(conn_peer));
365 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 */
369 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
373 tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
374 tr_msg_free_decoded(*msg);
376 return TRP_UNSUPPORTED;
382 int trps_get_listener(TRPS_INSTANCE *trps,
383 TRPS_MSG_FUNC msg_handler,
384 TRP_AUTH_FUNC auth_handler,
385 const char *hostname,
391 if (0 > (listen = trps_listen(trps, port))) {
393 if (0 == strerror_r(errno, errbuf, 256)) {
394 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
396 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
401 /* opening port succeeded */
402 tr_debug("trps_get_listener: Opened port %d.", port);
404 /* make this socket non-blocking */
405 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
406 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
413 /* store the caller's request handler & cookie */
414 trps->msg_handler = msg_handler;
415 trps->auth_handler = auth_handler;
416 trps->hostname = talloc_strdup(trps, hostname);
418 trps->cookie = cookie;
424 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
426 /* try to establish a GSS context */
427 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
428 tr_notice("trps_authorize_connection: failed to authorize connection");
429 trp_connection_close(conn);
432 tr_notice("trps_authorize_connection: authorized connection");
436 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
441 /* loop as long as the connection exists */
442 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
443 rc=trps_read_message(trps, conn, &msg);
446 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
450 trp_connection_close(conn);
454 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
458 tr_debug("trps_handle_connection: connection closed.");
461 /* TODO: check realm/comm, now part of the update instead of inforec */
462 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
465 tr_notice("trps_validate_update: null TRP update.");
469 if (trp_upd_get_realm(upd)==NULL) {
470 tr_notice("trps_validate_update: received TRP update without realm.");
474 if (trp_upd_get_comm(upd)==NULL) {
475 tr_notice("trps_validate_update: received TRP update without community.");
479 if (trp_upd_get_inforec(upd)==NULL) {
480 tr_notice("trps_validate_update: received TRP update with no info records.");
484 if (trp_upd_get_peer(upd)==NULL) {
485 tr_notice("trps_validate_update: received TRP update without origin peer information.");
493 /* ensure that the update could be accepted if feasible */
494 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
496 switch(trp_inforec_get_type(rec)) {
497 case TRP_INFOREC_TYPE_ROUTE:
498 if ((trp_inforec_get_trust_router(rec)==NULL)
499 || (trp_inforec_get_next_hop(rec)==NULL)) {
500 tr_debug("trps_validate_inforec: missing record info.");
504 /* check for valid metric */
505 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
506 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
510 /* check for valid interval */
511 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
512 tr_debug("trps_validate_inforec: invalid interval.");
518 tr_notice("trps_validate_inforec: unsupported record type.");
519 return TRP_UNSUPPORTED;
525 /* link cost to a peer */
526 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
531 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
533 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
535 return TRP_METRIC_INFINITY;
536 return trp_route_get_metric(entry) + trps_cost(trps, peer);
539 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
541 unsigned int rec_metric=trp_inforec_get_metric(rec);
542 unsigned int new_metric=0;
543 unsigned int current_metric=0;
544 TR_NAME *next_hop=NULL;
546 /* we check these in the validation stage, but just in case... */
547 if (trp_metric_is_invalid(rec_metric))
550 /* retractions (aka infinite metrics) are always feasible */
551 if (trp_metric_is_infinite(rec_metric))
554 /* updates from our current next hop are always feasible*/
555 next_hop=trps_get_next_hop(trps, comm, realm);
557 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
562 /* compare the existing metric we advertise to what we would advertise
563 * if we accept this update */
564 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
565 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
566 if (new_metric <= current_metric)
572 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
573 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
575 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
576 if (0!=clock_gettime(CLOCK_REALTIME, ts)) {
577 tr_err("trps_compute_expiry: could not read realtime clock.");
581 ts->tv_sec += small_factor*interval;
585 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
587 TRP_ROUTE *entry=NULL;
589 entry=trp_rtable_get_entry(trps->rtable,
590 trp_upd_get_comm(upd),
591 trp_upd_get_realm(upd),
592 trp_inforec_get_next_hop(rec));
594 entry=trp_route_new(NULL);
596 tr_err("trps_accept_update: unable to allocate new entry.");
600 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
601 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
602 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
603 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
604 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
605 /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
606 if ((trp_route_get_comm(entry)==NULL)
607 ||(trp_route_get_realm(entry)==NULL)
608 ||(trp_route_get_peer(entry)==NULL)
609 ||(trp_route_get_trust_router(entry)==NULL)
610 ||(trp_route_get_next_hop(entry)==NULL)) {
611 /* at least one field could not be allocated */
612 tr_err("trps_accept_update: unable to allocate all fields for entry.");
613 trp_route_free(entry);
616 trp_rtable_add(trps->rtable, entry);
619 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
620 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
621 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
622 * time unset on a new route entry. */
623 tr_debug("trps_accept_update: accepting route update.");
624 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
625 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
627 /* check whether the trust router has changed */
628 if (0!=tr_name_cmp(trp_route_get_trust_router(entry),
629 trp_inforec_get_trust_router(rec))) {
630 /* The name changed. Set this route as triggered. */
631 tr_debug("trps_accept_update: trust router for route changed.");
632 trp_route_set_triggered(entry, 1);
633 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
635 if (!trps_route_retracted(trps, entry)) {
636 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
637 trp_route_set_expiry(entry, trps_compute_expiry(trps,
638 trp_route_get_interval(entry),
639 trp_route_get_expiry(entry)));
644 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
647 TRP_INFOREC *rec=NULL;
648 TRP_ROUTE *route=NULL;
650 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
651 tr_notice("trps_handle_update: received invalid TRP update.");
655 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
656 /* validate/sanity check the record update */
657 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
658 tr_notice("trps_handle_update: invalid record in TRP update, discarding entire update.");
663 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
664 /* determine feasibility */
665 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
666 tr_debug("trps_handle_update: record feasibility=%d", feas);
668 /* do we have an existing route? */
669 route=trps_get_route(trps,
670 trp_upd_get_comm(upd),
671 trp_upd_get_realm(upd),
672 trp_upd_get_peer(upd));
674 /* there was a route table entry already */
675 tr_debug("trps_handle_updates: route entry already exists.");
677 /* Update is feasible. Accept it. */
678 trps_accept_update(trps, upd, rec);
680 /* Update is infeasible. Ignore it unless the trust router has changed. */
681 if (0!=tr_name_cmp(trp_route_get_trust_router(route),
682 trp_inforec_get_trust_router(rec))) {
683 /* the trust router associated with the route has changed, treat update as a retraction */
684 trps_retract_route(trps, route);
688 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
689 tr_debug("trps_handle_update: no route entry exists yet.");
690 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
691 trps_accept_update(trps, upd, rec);
697 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
700 tr_notice("trps_validate_request: null TRP request.");
704 if (trp_req_get_comm(req)==NULL) {
705 tr_notice("trps_validate_request: received TRP request with null community.");
709 if (trp_req_get_realm(req)==NULL) {
710 tr_notice("trps_validate_request: received TRP request with null realm.");
714 if (trp_req_get_peer(req)==NULL) {
715 tr_notice("trps_validate_request: received TRP request without origin peer information.");
722 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
723 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
726 TR_NAME *exclude_peer)
728 TRP_ROUTE **entry=NULL;
729 TRP_ROUTE *best=NULL;
732 unsigned int kk_min=0;
733 unsigned int min_metric=TRP_METRIC_INFINITY;
735 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
736 for (kk=0; kk<n_entry; kk++) {
737 if (trp_route_get_metric(entry[kk]) < min_metric) {
738 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_route_get_peer(entry[kk]),
741 min_metric=trp_route_get_metric(entry[kk]);
745 if (trp_metric_is_finite(min_metric))
752 /* TODO: think this through more carefully. At least ought to add hysteresis
753 * to avoid flapping between routers or routes. */
754 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
756 size_t n_comm=0, ii=0;
757 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
758 size_t n_realm=0, jj=0;
759 TR_NAME **realm=NULL;
760 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
761 unsigned int best_metric=0, cur_metric=0;
763 for (ii=0; ii<n_comm; ii++) {
764 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
765 for (jj=0; jj<n_realm; jj++) {
766 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
767 if (best_route==NULL)
768 best_metric=TRP_METRIC_INFINITY;
770 best_metric=trp_route_get_metric(best_route);
772 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
773 if (cur_route!=NULL) {
774 cur_metric=trp_route_get_metric(cur_route);
775 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
776 /* The new route has a lower metric than the previous, and is finite. Accept. */
777 trp_route_set_selected(cur_route, 0);
778 trp_route_set_selected(best_route, 1);
779 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
780 trp_route_set_selected(cur_route, 0);
781 } else if (trp_metric_is_finite(best_metric)) {
782 trp_route_set_selected(best_route, 1);
787 realm=NULL; n_realm=0;
796 /* true if curtime >= expiry */
797 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
799 return ((curtime->tv_sec > expiry->tv_sec)
800 || ((curtime->tv_sec == expiry->tv_sec)
801 &&(curtime->tv_nsec > expiry->tv_nsec)));
804 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
805 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
806 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
808 struct timespec sweep_time={0,0};
809 TRP_ROUTE **entry=NULL;
813 /* use a single time for the entire sweep */
814 if (0!=clock_gettime(CLOCK_REALTIME, &sweep_time)) {
815 tr_err("trps_sweep_routes: could not read realtime clock.");
817 sweep_time.tv_nsec=0;
821 entry=trp_rtable_get_entries(trps->rtable, &n_entry); /* must talloc_free *entry */
823 /* loop over the entries */
824 for (ii=0; ii<n_entry; ii++) {
825 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
826 tr_debug("trps_sweep_routes: route expired.");
827 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
829 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
830 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
833 /* set metric to infinity and reset timer */
834 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
835 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
836 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
837 trp_route_get_interval(entry[ii]),
838 trp_route_get_expiry(entry[ii])));
847 /* select the correct route to comm/realm to be announced to peer */
848 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_gssname)
852 /* Take the currently selected route unless it is through the peer we're sending the update to.
853 * I.e., enforce the split horizon rule. */
854 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
856 /* No selected route, this should only happen if the only route has been retracted,
857 * in which case we do not want to advertise it. */
860 tr_debug("trps_select_realm_update: %s vs %s", peer_gssname->buf,
861 trp_route_get_peer(route)->buf);
862 if (0==tr_name_cmp(peer_gssname, trp_route_get_peer(route))) {
863 tr_debug("trps_select_realm_update: matched, finding alternate route");
864 /* the selected entry goes through the peer we're reporting to, choose an alternate */
865 route=trps_find_best_route(trps, comm, realm, peer_gssname);
866 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route))))
867 return NULL; /* don't advertise a nonexistent or retracted route */
872 /* returns an array of pointers to updates (*not* an array of updates). Returns number of entries
873 * via n_update parameter. (The allocated space will generally be larger than required, see note in
874 * the code.) If triggered is set, sends only triggered updates. */
875 static TRP_ROUTE **trps_select_updates_for_peer(TALLOC_CTX *memctx,
877 TR_NAME *peer_gssname,
882 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
883 TR_NAME **realm=NULL;
886 TRP_ROUTE *best=NULL;
887 TRP_ROUTE **result=NULL;
890 /* Need to allocate space for the results. For simplicity, we just allocate a block
891 * with space for every route table entry to be returned. This is guaranteed to be large
892 * enough. If the routing table gets very large, this may be wasteful, but that seems
893 * unlikely to be significant in the near future. */
894 result=talloc_array(memctx, TRP_ROUTE *, trp_rtable_size(trps->rtable));
901 for (ii=0; ii<n_comm; ii++) {
902 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
903 for (jj=0; jj<n_realm; jj++) {
904 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_gssname);
905 /* If we found a route, add it to the list. If triggered!=0, then only
906 * add triggered routes. */
907 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best)))
908 result[n_used++]=best;
923 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
925 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
926 return TRP_METRIC_INVALID;
928 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
929 return TRP_METRIC_INFINITY;
931 if (trp_metric_is_finite(m1+m2))
934 return TRP_METRIC_INFINITY;
937 /* convert an rentry into a new trp update info record */
938 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
940 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
941 unsigned int linkcost=0;
944 if (trp_route_is_local(route))
947 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
948 trp_route_get_peer(route)));
951 /* Note that we leave the next hop empty since the recipient fills that in.
952 * This is where we add the link cost (currently always 1) to the next peer. */
953 if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
954 ||(trp_inforec_set_metric(rec,
955 trps_metric_add(trp_route_get_metric(route),
956 linkcost)) != TRP_SUCCESS)
957 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
958 tr_err("trps_route_to_inforec: error creating route update.");
966 /* all routes to a single peer, unless comm/realm are specified (both or neither must be NULL) */
967 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
969 TRP_UPDATE_TYPE update_type,
973 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
974 TR_MSG msg; /* not a pointer! */
976 TRP_ROUTE **update_list=NULL;
977 TRP_INFOREC *rec=NULL;
978 size_t n_updates=0, ii=0;
981 TR_NAME *peer_label=trp_peer_get_label(peer);
983 switch (update_type) {
984 case TRP_UPDATE_TRIGGERED:
985 tr_debug("trps_update_one_peer: preparing triggered route update for %.*s",
986 peer_label->len, peer_label->buf);
988 case TRP_UPDATE_SCHEDULED:
989 tr_debug("trps_update_one_peer: preparing scheduled route update for %.*s",
990 peer_label->len, peer_label->buf);
992 case TRP_UPDATE_REQUESTED:
993 tr_debug("trps_update_one_peer: preparing requested route update for %.*s",
994 peer_label->len, peer_label->buf);
997 /* do not fill in peer, recipient does that */
998 if ((comm==NULL) && (realm==NULL)) {
1000 update_list=trps_select_updates_for_peer(tmp_ctx,
1003 update_type==TRP_UPDATE_TRIGGERED,
1005 } else if ((comm!=NULL) && (realm!=NULL)) {
1006 /* a single community/realm was requested */
1007 update_list=talloc(tmp_ctx, TRP_ROUTE *);
1008 if (update_list==NULL) {
1009 tr_err("trps_update_one_peer: could not allocate update_list.");
1013 *update_list=trps_select_realm_update(trps, comm, realm, peer_label);
1014 if (*update_list==NULL) {
1015 /* we have no actual update to send back, MUST send a retraction */
1016 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1017 *update_list=trp_route_new(update_list);
1018 trp_route_set_comm(*update_list, tr_dup_name(comm));
1019 trp_route_set_realm(*update_list, tr_dup_name(realm));
1020 trp_route_set_peer(*update_list, tr_new_name(""));
1021 trp_route_set_metric(*update_list, TRP_METRIC_INFINITY);
1022 trp_route_set_trust_router(*update_list, tr_new_name(""));
1023 trp_route_set_next_hop(*update_list, tr_new_name(""));
1027 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1031 if ((n_updates>0) && (update_list!=NULL)) {
1032 tr_debug("trps_update_one_peer: sending %u update records.", (unsigned int)n_updates);
1033 for (ii=0; ii<n_updates; ii++) {
1034 upd=trp_upd_new(tmp_ctx);
1036 tr_err("trps_update_one_peer: could not create update message.");
1040 trp_upd_set_realm(upd, trp_route_dup_realm(update_list[ii]));
1041 if (trp_upd_get_realm(upd)==NULL) {
1042 tr_err("trps_update_one_peer: could not copy realm.");
1046 trp_upd_set_comm(upd, trp_route_dup_comm(update_list[ii]));
1047 if (trp_upd_get_comm(upd)==NULL) {
1048 tr_err("trps_update_one_peer: could not copy comm.");
1052 rec=trps_route_to_inforec(tmp_ctx, trps, update_list[ii]);
1054 tr_err("trps_update_one_peer: could not create all update records.");
1058 trp_upd_add_inforec(upd, rec);
1060 /* now encode the update message */
1061 tr_msg_set_trp_upd(&msg, upd);
1062 encoded=tr_msg_encode(&msg);
1063 if (encoded==NULL) {
1064 tr_err("trps_update_one_peer: error encoding update.");
1069 tr_debug("trps_update_one_peer: adding message to queue.");
1070 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1071 tr_err("trps_update_one_peer: error queueing update.");
1073 tr_debug("trps_update_one_peer: update queued successfully.");
1075 tr_msg_free_encoded(encoded);
1080 talloc_free(update_list);
1083 } else if (n_updates==0)
1084 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1089 talloc_free(tmp_ctx);
1093 /* all routes to all peers */
1094 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1096 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1097 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1098 TRP_PEER *peer=NULL;
1099 TRP_RC rc=TRP_SUCCESS;
1101 if (trps->ptable==NULL)
1102 return TRP_SUCCESS; /* no peers, nothing to do */
1105 tr_err("trps_update: failed to allocate peer table iterator.");
1106 talloc_free(tmp_ctx);
1110 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1111 peer!=NULL && rc==TRP_SUCCESS;
1112 peer=trp_ptable_iter_next(iter))
1114 if (!trps_peer_connected(trps, peer)) {
1115 TR_NAME *peer_label=trp_peer_get_label(peer);
1116 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1117 peer_label->len, peer_label->buf);
1120 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1123 tr_debug("trps_update: rc=%u after attempting update.", rc);
1124 trp_ptable_iter_free(iter);
1125 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1126 talloc_free(tmp_ctx);
1130 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1132 trp_rtable_add(trps->rtable, route); /* should return status */
1136 /* steals the peer object */
1137 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1139 if (trps->ptable==NULL) {
1140 trps->ptable=trp_ptable_new(trps);
1141 if (trps->ptable==NULL)
1144 return trp_ptable_add(trps->ptable, peer);
1147 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1149 if (trps->ptable==NULL)
1152 return trp_ptable_find_gss_name(trps->ptable, gssname);
1155 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1157 if (trps->ptable==NULL)
1160 return trp_ptable_find_servicename(trps->ptable, servicename);
1163 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1165 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
1169 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
1176 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1179 TR_NAME *realm=NULL;
1181 tr_debug("trps_handle_request: handling TRP request.");
1183 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
1184 tr_notice("trps_handle_request: received invalid TRP request.");
1188 if (!trp_req_is_wildcard(req)) {
1189 comm=trp_req_get_comm(req);
1190 realm=trp_req_get_realm(req);
1191 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
1192 comm->len, comm->buf, realm->len, realm->buf);
1194 tr_debug("trps_handle_request: all routes requested.");
1195 /* leave comm/realm NULL */
1197 return trps_update_one_peer(trps,
1198 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
1199 TRP_UPDATE_REQUESTED,
1205 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
1207 TRP_RC rc=TRP_ERROR;
1209 switch (tr_msg_get_msg_type(tr_msg)) {
1211 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
1212 if (rc==TRP_SUCCESS) {
1213 rc=trps_update_active_routes(trps);
1214 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
1219 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
1223 /* unknown error or one we don't care about (e.g., TID messages) */
1228 /* send wildcard route request to a peer */
1229 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
1231 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1232 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
1233 TR_MSG msg; /* not a pointer */
1234 TRP_REQ *req=trp_req_new(tmp_ctx);
1236 TRP_RC rc=TRP_ERROR;
1239 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
1243 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
1244 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
1249 tr_msg_set_trp_req(&msg, req);
1250 encoded=tr_msg_encode(&msg);
1251 if (encoded==NULL) {
1252 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
1257 tr_debug("trps_wildcard_route_req: adding message to queue.");
1258 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
1259 tr_err("trps_wildcard_route_req: error queueing request.");
1262 tr_debug("trps_wildcard_route_req: request queued successfully.");
1268 tr_msg_free_encoded(encoded);
1272 talloc_free(tmp_ctx);