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.
42 #include <poll.h> // for nfds_t
48 #include <tr_name_internal.h>
49 #include <trp_route.h>
50 #include <trp_internal.h>
51 #include <tr_gss_names.h>
53 #include <trp_ptable.h>
54 #include <trp_rtable.h>
57 #include <tr_socket.h>
59 static int trps_destructor(void *object)
61 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
62 if (trps->rtable!=NULL)
63 trp_rtable_free(trps->rtable);
67 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
69 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
76 trps->update_interval=(struct timeval){0,0};
77 trps->sweep_interval=(struct timeval){0,0};
80 trps->mq=tr_mq_new(trps);
82 /* failed to allocate mq */
88 if (trps_init_rtable(trps) != TRP_SUCCESS) {
89 /* failed to allocate rtable */
94 talloc_set_destructor((void *)trps, trps_destructor);
99 /* create a new route table, first discarding an old one if necessary */
100 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
102 if (trps->rtable != NULL) {
103 trp_rtable_free(trps->rtable);
107 trps->rtable=trp_rtable_new();
108 if (trps->rtable==NULL) {
114 void trps_clear_rtable(TRPS_INSTANCE *trps)
116 trp_rtable_clear(trps->rtable);
119 void trps_free (TRPS_INSTANCE *trps)
125 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
127 return tr_mq_pop(trps->mq, 0);
130 void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
132 tr_mq_add(trps->mq, msg);
135 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
137 return trps->connect_interval.tv_sec;
140 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
142 trps->connect_interval.tv_sec=interval;
143 trps->connect_interval.tv_usec=0;
146 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
148 return trps->update_interval.tv_sec;
151 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
153 trps->update_interval.tv_sec=interval;
154 trps->update_interval.tv_usec=0;
157 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
159 return trps->sweep_interval.tv_sec;
162 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
164 trps->sweep_interval.tv_sec=interval;
165 trps->sweep_interval.tv_usec=0;
168 void trps_set_ctable(TRPS_INSTANCE *trps, TR_COMM_TABLE *comm)
173 void trps_set_ptable(TRPS_INSTANCE *trps, TRP_PTABLE *ptable)
175 if (trps->ptable!=NULL)
176 trp_ptable_free(trps->ptable);
180 void trps_set_peer_status_callback(TRPS_INSTANCE *trps, void (*cb)(TRP_PEER *, void *), void *cookie)
182 TRP_PTABLE_ITER *iter=NULL;
184 if (trps->ptable==NULL)
187 iter=trp_ptable_iter_new(NULL);
188 for (peer=trp_ptable_iter_first(iter, trps->ptable); peer!=NULL; peer=trp_ptable_iter_next(iter))
189 trp_peer_set_conn_status_cb(peer, cb, cookie);
190 trp_ptable_iter_free(iter);
193 /* Get the label peers will know us by - needs to match trp_peer_get_label() output.
194 * There is no get, only dup, because we don't store the label except when requested. */
195 TR_NAME *trps_dup_label(TRPS_INSTANCE *trps)
197 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
199 char *s=talloc_asprintf(tmp_ctx, "%s:%u", trps->hostname, trps->port);
202 label=tr_new_name(s);
205 talloc_free(tmp_ctx);
209 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
211 TRPC_INSTANCE *cur=NULL;
213 TR_NAME *peer_servicename=trp_peer_get_servicename(peer);
215 for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
216 name=trpc_get_gssname(cur);
217 if ((name!=NULL) && (0==tr_name_cmp(peer_servicename, name))) {
224 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
226 if (trps->conn==NULL)
229 trp_connection_append(trps->conn, new);
231 talloc_steal(trps, new);
234 /* ok to call more than once; guarantees connection no longer in the list.
235 * Caller is responsible for freeing the removed element afterwards. */
236 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
238 trps->conn=trp_connection_remove(trps->conn, remove);
241 void trps_add_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
243 if (trps->trpc==NULL)
246 trpc_append(trps->trpc, trpc);
248 talloc_steal(trps, trpc);
251 /* ok to call more than once; guarantees trpc no longer in the list.
252 * Caller is responsible for freeing the removed element afterwards. */
253 void trps_remove_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *remove)
255 trps->trpc=trpc_remove(trps->trpc, remove);
258 TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
260 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
261 TR_MQ_MSG *mq_msg=NULL;
264 TRPC_INSTANCE *trpc=NULL;
266 /* get the connection for this peer */
267 trpc=trps_find_trpc(trps, peer);
268 /* instead, let's let that happen and then clear the queue when an attempt to
271 tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
272 } else if (trpc->shutting_down) {
273 tr_debug("trps_send_msg: skipping message because TRP client is shutting down.");
274 rc = TRP_SUCCESS; /* it's ok that this didn't get sent, the connection will be gone in a moment */
276 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND, TR_MQ_PRIO_NORMAL);
277 msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
278 tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
279 trpc_mq_add(trpc, mq_msg);
282 talloc_free(tmp_ctx);
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(NULL, 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 */
366 /* update provenance if necessary */
367 trp_upd_add_to_provenance(tr_msg_get_trp_upd(*msg), trp_peer_get_label(peer));
371 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
375 tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
376 tr_msg_free_decoded(*msg);
378 return TRP_UNSUPPORTED;
384 int trps_get_listener(TRPS_INSTANCE *trps,
385 TRPS_MSG_FUNC msg_handler,
386 TRP_AUTH_FUNC auth_handler,
387 const char *hostname,
396 n_fd = tr_sock_listen_all(port, fd_out, max_fd);
399 tr_err("trps_get_listener: Error opening port %d.");
401 /* opening port succeeded */
402 tr_info("trps_get_listener: Opened port %d.", port);
404 /* make the sockets non-blocking */
405 for (ii=0; ii<n_fd; ii++) {
406 if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
407 tr_err("trps_get_listener: Error setting O_NONBLOCK.");
408 for (ii=0; ii<n_fd; ii++) {
419 /* store the caller's request handler & cookie */
420 trps->msg_handler = msg_handler;
421 trps->auth_handler = auth_handler;
422 trps->hostname = talloc_strdup(trps, hostname);
424 trps->cookie = cookie;
430 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
432 /* try to establish a GSS context */
433 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
434 tr_notice("trps_authorize_connection: failed to authorize connection");
435 trp_connection_close(conn);
438 tr_notice("trps_authorize_connection: authorized connection");
442 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
447 /* loop as long as the connection exists */
448 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
449 rc=trps_read_message(trps, conn, &msg);
452 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
456 trp_connection_close(conn);
460 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
464 tr_debug("trps_handle_connection: connection closed.");
467 /* TODO: check realm/comm, now part of the update instead of inforec */
468 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
471 tr_notice("trps_validate_update: null TRP update.");
475 if (trp_upd_get_realm(upd)==NULL) {
476 tr_notice("trps_validate_update: received TRP update without realm.");
480 if (trp_upd_get_comm(upd)==NULL) {
481 tr_notice("trps_validate_update: received TRP update without community.");
485 if (trp_upd_get_inforec(upd)==NULL) {
486 tr_notice("trps_validate_update: received TRP update with no info records.");
490 if (trp_upd_get_peer(upd)==NULL) {
491 tr_notice("trps_validate_update: received TRP update without origin peer information.");
499 /* ensure that the update could be accepted if feasible */
500 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
502 switch(trp_inforec_get_type(rec)) {
503 case TRP_INFOREC_TYPE_ROUTE:
504 if ((trp_inforec_get_trust_router(rec)==NULL)
505 || (trp_inforec_get_next_hop(rec)==NULL)) {
506 tr_debug("trps_validate_inforec: missing record info.");
510 /* check for valid metric */
511 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
512 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
516 /* check for valid interval */
517 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
518 tr_debug("trps_validate_inforec: invalid interval.");
523 case TRP_INFOREC_TYPE_COMMUNITY:
524 /* TODO: validate community updates */
528 tr_notice("trps_validate_inforec: unsupported record type.");
529 return TRP_UNSUPPORTED;
535 /* link cost to a peer */
536 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
541 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
543 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
545 return TRP_METRIC_INFINITY;
546 return trp_route_get_metric(entry) + trps_cost(trps, peer);
549 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
551 unsigned int rec_metric=trp_inforec_get_metric(rec);
552 unsigned int new_metric=0;
553 unsigned int current_metric=0;
554 TR_NAME *next_hop=NULL;
556 /* we check these in the validation stage, but just in case... */
557 if (trp_metric_is_invalid(rec_metric))
560 /* retractions (aka infinite metrics) are always feasible */
561 if (trp_metric_is_infinite(rec_metric))
564 /* updates from our current next hop are always feasible*/
565 next_hop=trps_get_next_hop(trps, comm, realm);
567 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
572 /* compare the existing metric we advertise to what we would advertise
573 * if we accept this update */
574 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
575 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
576 if (new_metric <= current_metric)
582 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
583 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
585 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
586 if (0!=clock_gettime(TRP_CLOCK, ts)) {
587 tr_err("trps_compute_expiry: could not read realtime clock.");
591 tr_debug("trps_compute_expiry: tv_sec=%u, interval=%u, small_factor*interval=%u", ts->tv_sec, interval, small_factor*interval);
592 ts->tv_sec += small_factor*interval;
596 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
598 TRP_ROUTE *entry=NULL;
600 entry=trp_rtable_get_entry(trps->rtable,
601 trp_upd_get_comm(upd),
602 trp_upd_get_realm(upd),
603 trp_inforec_get_next_hop(rec));
605 entry=trp_route_new(NULL);
607 tr_err("trps_accept_update: unable to allocate new entry.");
611 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
612 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
613 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
614 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
615 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
616 /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
617 if ((trp_route_get_comm(entry)==NULL)
618 ||(trp_route_get_realm(entry)==NULL)
619 ||(trp_route_get_peer(entry)==NULL)
620 ||(trp_route_get_trust_router(entry)==NULL)
621 ||(trp_route_get_next_hop(entry)==NULL)) {
622 /* at least one field could not be allocated */
623 tr_err("trps_accept_update: unable to allocate all fields for entry.");
624 trp_route_free(entry);
627 trp_rtable_add(trps->rtable, entry);
630 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
631 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
632 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
633 * time unset on a new route entry. */
634 tr_debug("trps_accept_update: accepting route update.");
635 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
636 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
638 /* check whether the trust router has changed */
639 if (0!=tr_name_cmp(trp_route_get_trust_router(entry),
640 trp_inforec_get_trust_router(rec))) {
641 /* The name changed. Set this route as triggered. */
642 tr_debug("trps_accept_update: trust router for route changed.");
643 trp_route_set_triggered(entry, 1);
644 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
646 if (!trps_route_retracted(trps, entry)) {
647 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
648 trp_route_set_expiry(entry, trps_compute_expiry(trps,
649 trp_route_get_interval(entry),
650 trp_route_get_expiry(entry)));
656 static TRP_RC trps_handle_inforec_route(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
658 TRP_ROUTE *route=NULL;
661 /* determine feasibility */
662 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
663 tr_debug("trps_handle_update: record feasibility=%d", feas);
665 /* do we have an existing route? */
666 route=trps_get_route(trps,
667 trp_upd_get_comm(upd),
668 trp_upd_get_realm(upd),
669 trp_upd_get_peer(upd));
671 /* there was a route table entry already */
672 tr_debug("trps_handle_updates: route entry already exists.");
674 /* Update is feasible. Accept it. */
675 trps_accept_update(trps, upd, rec);
677 /* Update is infeasible. Ignore it unless the trust router has changed. */
678 if (0!=tr_name_cmp(trp_route_get_trust_router(route),
679 trp_inforec_get_trust_router(rec))) {
680 /* the trust router associated with the route has changed, treat update as a retraction */
681 trps_retract_route(trps, route);
685 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
686 tr_debug("trps_handle_update: no route entry exists yet.");
687 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
688 trps_accept_update(trps, upd, rec);
694 static int trps_name_in_provenance(TR_NAME *name, json_t *prov)
697 TR_NAME *this_name=NULL;
701 return 0; /* no provenance list, so it has no names in it */
703 /* now check to see if name is in the provenance */
704 for (ii=0; ii<json_array_size(prov); ii++) {
705 s=json_string_value(json_array_get(prov, ii));
707 tr_debug("trps_name_in_provenance: empty entry in provenance list.");
711 this_name=tr_new_name(s);
712 if (this_name==NULL) {
713 tr_debug("trps_name_in_provenance: unable to allocate name.");
716 if (0==tr_name_cmp(name, this_name)) {
717 tr_free_name(this_name);
720 tr_free_name(this_name);
725 static TR_COMM *trps_create_new_comm(TALLOC_CTX *mem_ctx, TR_NAME *comm_id, TRP_INFOREC *rec)
727 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
728 TR_COMM *comm=tr_comm_new(tmp_ctx);
731 tr_debug("trps_create_new_comm: unable to allocate new community.");
734 /* fill in the community with info */
735 tr_comm_set_id(comm, tr_dup_name(comm_id));
736 if (tr_comm_get_id(comm)==NULL) {
737 tr_debug("trps_create_new_comm: unable to allocate community name.");
741 tr_comm_set_type(comm, trp_inforec_get_comm_type(rec));
742 if (trp_inforec_get_apcs(rec)!=NULL) {
743 tr_comm_set_apcs(comm, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
744 if (tr_comm_get_apcs(comm)==NULL) {
745 tr_debug("trps_create_new_comm: unable to allocate APC list.");
750 if (trp_inforec_get_owner_realm(rec)!=NULL) {
751 tr_comm_set_owner_realm(comm, tr_dup_name(trp_inforec_get_owner_realm(rec)));
752 if (tr_comm_get_owner_realm(comm)==NULL) {
753 tr_debug("trps_create_new_comm: unable to allocate owner realm name.");
758 if (trp_inforec_get_owner_contact(rec)!=NULL) {
759 tr_comm_set_owner_contact(comm, tr_dup_name(trp_inforec_get_owner_contact(rec)));
760 if (tr_comm_get_owner_contact(comm)==NULL) {
761 tr_debug("trps_create_new_comm: unable to allocate owner contact.");
766 comm->expiration_interval=trp_inforec_get_exp_interval(rec);
767 talloc_steal(mem_ctx, comm);
770 talloc_free(tmp_ctx);
774 static TR_RP_REALM *trps_create_new_rp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
776 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
777 TR_RP_REALM *rp=tr_rp_realm_new(tmp_ctx);
780 tr_debug("trps_create_new_rp_realm: unable to allocate new realm.");
783 /* fill in the realm */
784 tr_rp_realm_set_id(rp, tr_dup_name(realm_id));
785 if (tr_rp_realm_get_id(rp)==NULL) {
786 tr_debug("trps_create_new_rp_realm: unable to allocate realm name.");
790 talloc_steal(mem_ctx, rp);
793 talloc_free(tmp_ctx);
797 static TR_IDP_REALM *trps_create_new_idp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
799 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
800 TR_IDP_REALM *idp=tr_idp_realm_new(tmp_ctx);
803 tr_debug("trps_create_new_idp_realm: unable to allocate new realm.");
806 /* fill in the realm */
807 tr_idp_realm_set_id(idp, tr_dup_name(realm_id));
808 if (tr_idp_realm_get_id(idp)==NULL) {
809 tr_debug("trps_create_new_idp_realm: unable to allocate realm name.");
813 if (trp_inforec_get_apcs(rec)!=NULL) {
814 tr_idp_realm_set_apcs(idp, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
815 if (tr_idp_realm_get_apcs(idp)==NULL) {
816 tr_debug("trps_create_new_idp_realm: unable to allocate APC list.");
821 idp->origin=TR_REALM_DISCOVERED;
823 talloc_steal(mem_ctx, idp);
826 talloc_free(tmp_ctx);
830 static TRP_RC trps_handle_inforec_comm(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
832 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
833 TR_NAME *comm_id=trp_upd_get_comm(upd);
834 TR_NAME *realm_id=trp_upd_get_realm(upd);
835 TR_NAME *origin_id=NULL;
836 TR_NAME *our_peer_label=NULL;
838 TR_RP_REALM *rp_realm=NULL;
839 TR_IDP_REALM *idp_realm=NULL;
840 struct timespec expiry={0,0};
843 if ((comm_id==NULL) || (realm_id==NULL))
846 origin_id=trp_inforec_dup_origin(rec);
850 /* see whether we want to add this */
851 our_peer_label=trps_dup_label(trps);
852 if (our_peer_label==NULL) {
853 tr_debug("trps_handle_inforec_comm: unable to allocate peer label.");
857 if (trps_name_in_provenance(our_peer_label, trp_inforec_get_provenance(rec)))
858 tr_debug("trps_handle_inforec_comm: rejecting community inforec to avoid provenance loop.");
860 /* no loop occurring, accept the update */
861 comm=tr_comm_table_find_comm(trps->ctable, comm_id);
863 tr_debug("trps_handle_inforec_comm: unknown community %.*s in inforec, creating it.",
864 comm_id->len, comm_id->buf);
865 comm=trps_create_new_comm(tmp_ctx, comm_id, rec);
867 tr_debug("trps_handle_inforec_comm: unable to create new community.");
870 tr_comm_table_add_comm(trps->ctable, comm);
872 /* TODO: see if other comm data match the new inforec and update or complain */
874 trps_compute_expiry(trps, trp_inforec_get_interval(rec), &expiry);
875 if ((expiry.tv_sec==0)&&(expiry.tv_nsec==0))
878 switch (trp_inforec_get_role(rec)) {
880 rp_realm=tr_rp_realm_lookup(trps->ctable->rp_realms, realm_id);
881 if (rp_realm==NULL) {
882 tr_debug("trps_handle_inforec_comm: unknown RP realm %.*s in inforec, creating it.",
883 realm_id->len, realm_id->buf);
884 rp_realm=trps_create_new_rp_realm(tmp_ctx, realm_id, rec);
885 if (rp_realm==NULL) {
886 tr_debug("trps_handle_inforec_comm: unable to create new RP realm.");
887 /* we may leave an unused community in the table, but it will only last until
888 * the next table sweep if it does not get any realms before that happens */
891 tr_comm_table_add_rp_realm(trps->ctable, rp_realm);
893 /* TODO: if realm existed, see if data match the new inforec and update or complain */
894 tr_comm_add_rp_realm(trps->ctable, comm, rp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
895 tr_debug("trps_handle_inforec_comm: added RP realm %.*s to comm %.*s (origin %.*s).",
896 realm_id->len, realm_id->buf,
897 comm_id->len, comm_id->buf,
898 origin_id->len, origin_id->buf);
901 idp_realm=tr_idp_realm_lookup(trps->ctable->idp_realms, realm_id);
902 if (idp_realm==NULL) {
903 tr_debug("trps_handle_inforec_comm: unknown IDP realm %.*s in inforec, creating it.",
904 realm_id->len, realm_id->buf);
905 idp_realm=trps_create_new_idp_realm(tmp_ctx, realm_id, rec);
906 if (idp_realm==NULL) {
907 tr_debug("trps_handle_inforec_comm: unable to create new IDP realm.");
908 /* we may leave an unused community in the table, but it will only last until
909 * the next table sweep if it does not get any realms before that happens */
912 tr_comm_table_add_idp_realm(trps->ctable, idp_realm);
914 /* TODO: if realm existed, see if data match the new inforec and update or complain */
915 tr_comm_add_idp_realm(trps->ctable, comm, idp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
916 tr_debug("trps_handle_inforec_comm: added IDP realm %.*s to comm %.*s (origin %.*s).",
917 realm_id->len, realm_id->buf,
918 comm_id->len, comm_id->buf,
919 origin_id->len, origin_id->buf);
922 tr_debug("trps_handle_inforec_comm: unable to add realm.");
930 if (our_peer_label!=NULL)
931 tr_free_name(our_peer_label);
933 tr_free_name(origin_id);
934 talloc_free(tmp_ctx);
939 * Apply applicable TRP_INBOUND filters to an inforec. Rejects everything if peer has no filters.
941 * @param trps Active TRPS instance
942 * @param upd TRP_UPD that contains the inforec to filter
943 * @param rec Inforec to filter
944 * @return 1 if accepted by the filter, 0 otherwise
946 static int trps_filter_inbound_inforec(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
949 TR_NAME *peer_name=NULL;
950 TR_FILTER_ACTION action=TR_FILTER_ACTION_REJECT;
951 TR_FILTER_TARGET *target=NULL;
954 /* Look up the peer. For inbound messages, the peer is identified by its GSS name */
955 peer_name=trp_upd_get_peer(upd);
956 peer=trps_get_peer_by_gssname(trps, peer_name);
958 tr_err("trps_filter_inbound_inforec: received inforec from unknown peer (%.*s), rejecting.",
964 /* tr_filter_apply() and tr_filter_set_get() handle null filter sets/filters by rejecting */
965 target= tr_filter_target_trp_inforec(NULL, upd, rec);
967 /* TODO: signal that filtering failed. Until then, just filter everything and give an error message. */
968 tr_crit("trps_filter_inbound_inforec: Unable to allocate filter target, cannot apply filter!");
971 || (TR_FILTER_NO_MATCH==tr_filter_apply(target,
972 tr_filter_set_get(peer->filters, TR_FILTER_TYPE_TRP_INBOUND),
975 || (action!=TR_FILTER_ACTION_ACCEPT)) {
976 /* either the filter did not match or it matched a reject rule or allocating the target failed */
981 tr_filter_target_free(target);
983 /* filter matched an accept rule */
988 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
990 TRP_INFOREC *rec=NULL;
992 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
993 tr_notice("trps_handle_update: received invalid TRP update.");
997 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
998 /* validate/sanity check the record update */
999 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
1000 tr_notice("trps_handle_update: invalid inforec in TRP update, discarding entire update.");
1005 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1006 if (!trps_filter_inbound_inforec(trps, upd, rec)) {
1007 tr_debug("trps_handle_update: inforec rejected by filter.");
1008 continue; /* just go on to the next record */
1011 switch (trp_inforec_get_type(rec)) {
1012 case TRP_INFOREC_TYPE_ROUTE:
1013 tr_debug("trps_handle_update: handling route inforec.");
1014 if (TRP_SUCCESS!=trps_handle_inforec_route(trps, upd, rec))
1015 tr_notice("trps_handle_update: error handling route inforec.");
1017 case TRP_INFOREC_TYPE_COMMUNITY:
1018 tr_debug("trps_handle_update: handling community inforec.");
1019 if (TRP_SUCCESS!=trps_handle_inforec_comm(trps, upd, rec))
1020 tr_notice("trps_handle_update: error handling community inforec.");
1024 tr_notice("trps_handle_update: unsupported inforec in TRP update.");
1031 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1034 tr_notice("trps_validate_request: null TRP request.");
1038 if (trp_req_get_comm(req)==NULL) {
1039 tr_notice("trps_validate_request: received TRP request with null community.");
1043 if (trp_req_get_realm(req)==NULL) {
1044 tr_notice("trps_validate_request: received TRP request with null realm.");
1048 if (trp_req_get_peer(req)==NULL) {
1049 tr_notice("trps_validate_request: received TRP request without origin peer information.");
1056 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
1057 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
1060 TR_NAME *exclude_peer)
1062 TRP_ROUTE **entry=NULL;
1063 TRP_ROUTE *best=NULL;
1066 unsigned int kk_min=0;
1067 unsigned int min_metric=TRP_METRIC_INFINITY;
1069 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
1070 for (kk=0; kk<n_entry; kk++) {
1071 if (trp_route_get_metric(entry[kk]) < min_metric) {
1072 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_route_get_peer(entry[kk]),
1075 min_metric=trp_route_get_metric(entry[kk]);
1079 if (trp_metric_is_finite(min_metric))
1086 /* TODO: think this through more carefully. At least ought to add hysteresis
1087 * to avoid flapping between routers or routes. */
1088 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
1090 size_t n_comm=0, ii=0;
1091 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1092 size_t n_realm=0, jj=0;
1093 TR_NAME **realm=NULL;
1094 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
1095 unsigned int best_metric=0, cur_metric=0;
1097 for (ii=0; ii<n_comm; ii++) {
1098 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1099 for (jj=0; jj<n_realm; jj++) {
1100 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
1101 if (best_route==NULL)
1102 best_metric=TRP_METRIC_INFINITY;
1104 best_metric=trp_route_get_metric(best_route);
1106 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
1107 if (cur_route!=NULL) {
1108 cur_metric=trp_route_get_metric(cur_route);
1109 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
1110 /* The new route has a lower metric than the previous, and is finite. Accept. */
1111 trp_route_set_selected(cur_route, 0);
1112 trp_route_set_selected(best_route, 1);
1113 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
1114 trp_route_set_selected(cur_route, 0);
1115 } else if (trp_metric_is_finite(best_metric)) {
1116 trp_route_set_selected(best_route, 1);
1121 realm=NULL; n_realm=0;
1125 comm=NULL; n_comm=0;
1130 /* true if curtime >= expiry */
1131 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
1133 return (tr_cmp_timespec(curtime, expiry) >= 0);
1136 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
1137 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
1138 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
1140 struct timespec sweep_time={0,0};
1141 TRP_ROUTE **entry=NULL;
1145 /* use a single time for the entire sweep */
1146 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1147 tr_err("trps_sweep_routes: could not read realtime clock.");
1148 sweep_time.tv_sec=0;
1149 sweep_time.tv_nsec=0;
1153 entry= trp_rtable_get_entries(NULL, trps->rtable, &n_entry); /* must talloc_free *entry */
1155 /* loop over the entries */
1156 for (ii=0; ii<n_entry; ii++) {
1157 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
1158 tr_debug("trps_sweep_routes: route expired.");
1159 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
1161 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
1162 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
1165 /* set metric to infinity and reset timer */
1166 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
1167 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
1168 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
1169 trp_route_get_interval(entry[ii]),
1170 trp_route_get_expiry(entry[ii])));
1180 /* Sweep for expired communities/realms/memberships. */
1181 TRP_RC trps_sweep_ctable(TRPS_INSTANCE *trps)
1183 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1184 struct timespec sweep_time={0,0};
1185 TR_COMM_MEMB *memb=NULL;
1186 TR_COMM_ITER *iter=NULL;
1187 TRP_RC rc=TRP_ERROR;
1189 /* use a single time for the entire sweep */
1190 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1191 tr_err("trps_sweep_ctable: could not read realtime clock.");
1192 sweep_time.tv_sec=0;
1193 sweep_time.tv_nsec=0;
1197 /* iterate all memberships */
1198 iter=tr_comm_iter_new(tmp_ctx);
1200 tr_err("trps_sweep_ctable: unable to allocate iterator.");
1204 for (memb=tr_comm_memb_iter_all_first(iter, trps->ctable);
1206 memb=tr_comm_memb_iter_all_next(iter)) {
1207 if (tr_comm_memb_get_origin(memb)==NULL)
1208 continue; /* do not expire local entries */
1210 if (tr_comm_memb_is_expired(memb, &sweep_time)) {
1211 if (tr_comm_memb_get_times_expired(memb)>0) {
1212 /* Already expired once; flush. */
1213 tr_debug("trps_sweep_ctable: flushing expired community membership (%.*s in %.*s, origin %.*s, expired %s).",
1214 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1215 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1216 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf,
1217 timespec_to_str(tr_comm_memb_get_expiry(memb)));
1218 tr_comm_table_remove_memb(trps->ctable, memb);
1219 tr_comm_memb_free(memb);
1221 /* This is the first expiration. Note this and reset the expiry time. */
1222 tr_comm_memb_expire(memb);
1223 trps_compute_expiry(trps, tr_comm_memb_get_interval(memb), tr_comm_memb_get_expiry(memb));
1224 tr_debug("trps_sweep_ctable: community membership expired at %s, resetting expiry to %s (%.*s in %.*s, origin %.*s).",
1225 timespec_to_str(&sweep_time),
1226 timespec_to_str(tr_comm_memb_get_expiry(memb)),
1227 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1228 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1229 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf);
1234 /* get rid of any unreferenced realms, etc */
1235 tr_comm_table_sweep(trps->ctable);
1238 talloc_free(tmp_ctx);
1243 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
1245 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
1246 return TRP_METRIC_INVALID;
1248 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
1249 return TRP_METRIC_INFINITY;
1251 if (trp_metric_is_finite(m1+m2))
1254 return TRP_METRIC_INFINITY;
1257 /* convert an rentry into a new trp update info record */
1258 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1260 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
1261 unsigned int linkcost=0;
1264 if (trp_route_is_local(route))
1267 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
1268 trp_route_get_peer(route)));
1271 /* Note that we leave the next hop empty since the recipient fills that in.
1272 * This is where we add the link cost (currently always 1) to the next peer. */
1273 if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
1274 ||(trp_inforec_set_metric(rec,
1275 trps_metric_add(trp_route_get_metric(route),
1276 linkcost)) != TRP_SUCCESS)
1277 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
1278 tr_err("trps_route_to_inforec: error creating route update.");
1286 static TRP_UPD *trps_route_to_upd(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1288 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1289 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1290 TRP_INFOREC *rec=NULL;
1293 tr_err("trps_route_to_upd: could not create update message.");
1296 trp_upd_set_realm(upd, trp_route_dup_realm(route));
1297 if (trp_upd_get_realm(upd)==NULL) {
1298 tr_err("trps_route_to_upd: could not copy realm.");
1299 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1302 trp_upd_set_comm(upd, trp_route_dup_comm(route));
1303 if (trp_upd_get_comm(upd)==NULL) {
1304 tr_err("trps_route_to_upd: could not copy comm.");
1305 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1308 rec=trps_route_to_inforec(tmp_ctx, trps, route);
1310 tr_err("trps_route_to_upd: could not create route info record for realm %.*s in comm %.*s.",
1311 trp_route_get_realm(route)->len, trp_route_get_realm(route)->buf,
1312 trp_route_get_comm(route)->len, trp_route_get_comm(route)->buf);
1313 upd=NULL; /* it's till in tmp_ctx, so it will be freed */
1316 trp_upd_add_inforec(upd, rec);
1319 talloc_steal(mem_ctx, upd);
1322 talloc_free(tmp_ctx);
1326 /* select the correct route to comm/realm to be announced to peer */
1327 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_label)
1329 TRP_ROUTE *route = NULL;
1330 TRP_PEER *route_peer = NULL;
1331 TR_NAME *route_peer_label = NULL;
1333 /* Take the currently selected route unless it is through the peer we're sending the update to.
1334 * I.e., enforce the split horizon rule. Start by looking up the currently selected route. */
1335 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
1337 /* No selected route, this should only happen if the only route has been retracted,
1338 * in which case we do not want to advertise it. */
1342 /* Check whether it's local. */
1343 if (trp_route_is_local(route)) {
1344 /* It is always ok to announce a local route */
1345 tr_debug("trps_select_realm_update: selected route for %.*s/%.*s is local",
1346 realm->len, realm->buf,
1347 comm->len, comm->buf);
1349 /* It's not local. Get the route's peer and check whether it's the same place we
1350 * got the selected route from. Peer should always correspond to an entry in our
1352 tr_debug("trps_select_realm_update: selected route for %.*s/%.*s is not local",
1353 realm->len, realm->buf,
1354 comm->len, comm->buf);
1355 route_peer = trp_ptable_find_gss_name(trps->ptable, trp_route_get_peer(route));
1356 if (route_peer == NULL) {
1357 tr_err("trps_select_realm_update: unknown peer GSS name (%.*s) for selected route for %.*s/%.*s",
1358 trp_route_get_peer(route)->len, trp_route_get_peer(route)->buf,
1359 realm->len, realm->buf,
1360 comm->len, comm->buf);
1363 route_peer_label = trp_peer_get_label(route_peer);
1364 if (route_peer_label == NULL) {
1365 tr_err("trps_select_realm_update: error retrieving peer label for selected route for %.*s/%.*s",
1366 realm->len, realm->buf,
1367 comm->len, comm->buf);
1371 /* see if these match */
1372 tr_debug("trps_select_realm_update: %.*s vs %.*s",
1373 peer_label->len, peer_label->buf,
1374 route_peer_label->len, route_peer_label->buf);
1376 if (0==tr_name_cmp(peer_label, route_peer_label)) {
1377 /* the selected entry goes through the peer we're reporting to, choose an alternate */
1378 tr_debug("trps_select_realm_update: matched, finding alternate route");
1379 route=trps_find_best_route(trps, comm, realm, peer_label);
1380 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route))))
1381 return NULL; /* don't advertise a nonexistent or retracted route */
1387 /* Add TRP_UPD msgs to the updates GPtrArray. Caller needs to arrange for these to be freed. */
1388 static TRP_RC trps_select_route_updates_for_peer(TALLOC_CTX *mem_ctx,
1390 TRPS_INSTANCE *trps,
1391 TR_NAME *peer_label,
1395 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1396 TR_NAME **realm=NULL;
1399 TRP_ROUTE *best=NULL;
1405 for (ii=0; ii<n_comm; ii++) {
1406 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1407 for (jj=0; jj<n_realm; jj++) {
1408 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_label);
1409 /* If we found a route, add it to the list. If triggered!=0, then only
1410 * add triggered routes. */
1411 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best))) {
1412 upd=trps_route_to_upd(mem_ctx, trps, best);
1414 tr_err("trps_select_route_updates_for_peer: unable to create update message.");
1417 g_ptr_array_add(updates, upd);
1433 static TRP_INFOREC *trps_memb_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_COMM_MEMB *memb)
1435 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1436 TRP_INFOREC *rec=NULL;
1442 comm=tr_comm_memb_get_comm(memb);
1443 rec=trp_inforec_new(tmp_ctx, TRP_INFOREC_TYPE_COMMUNITY);
1447 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_get_type(comm))) {
1452 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_comm_memb_get_role(memb))) {
1457 if ((NULL!=tr_comm_get_apcs(comm)) &&
1458 ( (TRP_SUCCESS!=trp_inforec_set_apcs(rec,
1459 tr_apc_dup(rec, tr_comm_get_apcs(comm)))) ||
1460 (NULL==trp_inforec_get_apcs(rec)))) {
1465 if ((NULL!=tr_comm_get_owner_realm(comm)) &&
1466 ( (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_dup_name(tr_comm_get_owner_realm(comm)))) ||
1467 (NULL==trp_inforec_get_owner_realm(rec)))) {
1472 if ((NULL!=tr_comm_get_owner_contact(comm)) &&
1473 ( (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_dup_name(tr_comm_get_owner_contact(comm)))) ||
1474 (NULL==trp_inforec_get_owner_contact(rec)))) {
1479 if ((NULL!=tr_comm_memb_get_provenance(memb)) &&
1480 (TRP_SUCCESS!=trp_inforec_set_provenance(rec, tr_comm_memb_get_provenance(memb)))) {
1485 if (TRP_SUCCESS!=trp_inforec_set_interval(rec, trps_get_update_interval(trps))) {
1491 talloc_steal(mem_ctx, rec);
1494 talloc_free(tmp_ctx);
1498 /* construct an update with all the inforecs for comm/realm/role to be sent to peer */
1499 static TRP_UPD *trps_comm_update(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_NAME *peer_gssname, TR_COMM *comm, TR_REALM *realm)
1501 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1502 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1503 TRP_INFOREC *rec=NULL;
1504 TR_COMM_ITER *iter=NULL;
1505 TR_COMM_MEMB *memb=NULL;
1510 trp_upd_set_comm(upd, tr_comm_dup_id(comm));
1511 trp_upd_set_realm(upd, tr_realm_dup_id(realm));
1512 /* leave peer empty */
1514 iter=tr_comm_iter_new(tmp_ctx);
1516 tr_err("trps_comm_update: unable to allocate iterator.");
1521 /* now add inforecs */
1522 switch (realm->role) {
1524 memb=tr_comm_table_find_idp_memb(trps->ctable,
1525 tr_realm_get_id(realm),
1526 tr_comm_get_id(comm));
1529 memb=tr_comm_table_find_rp_memb(trps->ctable,
1530 tr_realm_get_id(realm),
1531 tr_comm_get_id(comm));
1537 for (memb=tr_comm_memb_iter_first(iter, memb);
1539 memb=tr_comm_memb_iter_next(iter)) {
1540 rec=trps_memb_to_inforec(tmp_ctx, trps, memb);
1542 tr_err("trps_comm_update: unable to allocate inforec.");
1546 trp_upd_add_inforec(upd, rec);
1550 if (trp_upd_get_inforec(upd)==NULL)
1551 upd=NULL; /* no inforecs, no reason to send the update */
1553 talloc_steal(mem_ctx, upd); /* success! */
1556 talloc_free(tmp_ctx);
1560 /* Find all community updates to send to a peer and add these as TR_UPD records
1561 * to the updates GPtrArray. */
1562 static TRP_RC trps_select_comm_updates_for_peer(TALLOC_CTX *mem_ctx,
1564 TRPS_INSTANCE *trps,
1565 TR_NAME *peer_gssname,
1568 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1569 TR_COMM_ITER *comm_iter=NULL;
1571 TR_COMM_ITER *realm_iter=NULL;
1572 TR_REALM *realm=NULL;
1574 TRP_RC rc=TRP_ERROR;
1576 /* currently do not send any communities on triggered updates */
1582 comm_iter=tr_comm_iter_new(tmp_ctx);
1583 realm_iter=tr_comm_iter_new(tmp_ctx);
1584 if ((comm_iter==NULL) || (realm_iter==NULL)) {
1585 tr_err("trps_select_comm_updates_for_peer: unable to allocate iterator.");
1590 /* do every community */
1591 for (comm=tr_comm_table_iter_first(comm_iter, trps->ctable);
1593 comm=tr_comm_table_iter_next(comm_iter)) {
1594 /* do every realm in this community */
1595 tr_debug("trps_select_comm_updates_for_peer: looking through community %.*s",
1596 tr_comm_get_id(comm)->len,
1597 tr_comm_get_id(comm)->buf);
1598 for (realm=tr_realm_iter_first(realm_iter, trps->ctable, tr_comm_get_id(comm));
1600 realm=tr_realm_iter_next(realm_iter)) {
1601 /* get the update for this comm/realm */
1602 tr_debug("trps_select_comm_updates_for_peer: adding realm %.*s",
1603 tr_realm_get_id(realm)->len,
1604 tr_realm_get_id(realm)->buf);
1605 upd=trps_comm_update(mem_ctx, trps, peer_gssname, comm, realm);
1607 g_ptr_array_add(updates, upd);
1612 talloc_free(tmp_ctx);
1617 * Filter the inforecs in a single update
1619 * @param filt The filter to apply
1620 * @param upd The update to filter
1622 static void trps_filter_one_outbound_update(TR_FILTER *filt, TRP_UPD *upd)
1624 TRP_INFOREC *this=NULL, *next=NULL;
1625 TR_FILTER_ACTION action=TR_FILTER_ACTION_REJECT;
1626 TR_FILTER_TARGET *target=NULL;
1628 for(this=trp_upd_get_inforec(upd); this!=NULL; this=next) {
1630 target= tr_filter_target_trp_inforec(NULL, upd, this);
1632 /* TODO: signal that filtering failed. Until then, just filter everything and give an error message. */
1633 tr_crit("trps_filter_one_outbound_update: Unable to allocate filter target, cannot apply filter!");
1636 || (TR_FILTER_NO_MATCH==tr_filter_apply(target, filt, NULL, &action))
1637 || (action!=TR_FILTER_ACTION_ACCEPT)) {
1638 /* Either no filter matched or one matched and rejected this record.
1639 * Also filter out record if we were unable to allocate a target. */
1640 trp_upd_remove_inforec(upd, this); /* "this" is now invalid */
1643 tr_filter_target_free(target);
1648 * May shuffle the update list.
1650 * @param filters The filter set for the relevant TRP peer
1651 * @param updates GPtrArray of updates to filter
1653 static void trps_filter_outbound_updates(TR_FILTER_SET *filters, GPtrArray *updates)
1658 /* Walk backward through the array so we can remove elements. Careful about loop
1659 * termination - remember that ii is unsigned. */
1660 for (ii=updates->len; ii>0; ii--) {
1661 upd=g_ptr_array_index(updates, ii-1);
1662 trps_filter_one_outbound_update(tr_filter_set_get(filters, TR_FILTER_TYPE_TRP_OUTBOUND), upd);
1663 /* see if we removed all the records from this update */
1664 if (trp_upd_num_inforecs(upd)==0)
1665 g_ptr_array_remove_index_fast(updates, ii-1); /* does not preserve order at index ii or higher */
1669 /* helper for trps_update_one_peer. Frees the TRP_UPD pointed to by a GPtrArray element */
1670 static void trps_trp_upd_destroy(gpointer data)
1672 trp_upd_free((TRP_UPD *)data);
1675 /* all routes/communities to a single peer, unless comm/realm are specified (both or neither must be NULL) */
1676 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
1678 TRP_UPDATE_TYPE update_type,
1682 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1683 TR_MSG msg; /* not a pointer! */
1685 TRP_ROUTE *route=NULL;
1688 TRP_RC rc=TRP_ERROR;
1689 TR_NAME *peer_label=trp_peer_get_label(peer);
1690 GPtrArray *updates=g_ptr_array_new_with_free_func(trps_trp_upd_destroy);
1692 if (updates==NULL) {
1693 tr_err("trps_update_one_peer: unable to allocate updates array.");
1698 switch (update_type) {
1699 case TRP_UPDATE_TRIGGERED:
1700 tr_debug("trps_update_one_peer: preparing triggered update for %.*s",
1701 peer_label->len, peer_label->buf);
1703 case TRP_UPDATE_SCHEDULED:
1704 tr_debug("trps_update_one_peer: preparing scheduled update for %.*s",
1705 peer_label->len, peer_label->buf);
1707 case TRP_UPDATE_REQUESTED:
1708 tr_debug("trps_update_one_peer: preparing requested update for %.*s",
1709 peer_label->len, peer_label->buf);
1712 tr_err("trps_update_one_peer: invalid update type requested.");
1717 /* First, gather route updates. */
1718 tr_debug("trps_update_one_peer: selecting route updates for %.*s.", peer_label->len, peer_label->buf);
1719 if ((comm==NULL) && (realm==NULL)) {
1721 rc=trps_select_route_updates_for_peer(tmp_ctx,
1725 update_type==TRP_UPDATE_TRIGGERED);
1726 } else if ((comm!=NULL) && (realm!=NULL)) {
1727 /* a single community/realm was requested */
1728 route=trps_select_realm_update(trps, comm, realm, peer_label);
1730 /* we have no actual update to send back, MUST send a retraction */
1731 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1732 route=trp_route_new(tmp_ctx);
1733 trp_route_set_comm(route, tr_dup_name(comm));
1734 trp_route_set_realm(route, tr_dup_name(realm));
1735 trp_route_set_peer(route, tr_new_name(""));
1736 trp_route_set_metric(route, TRP_METRIC_INFINITY);
1737 trp_route_set_trust_router(route, tr_new_name(""));
1738 trp_route_set_next_hop(route, tr_new_name(""));
1740 upd=trps_route_to_upd(tmp_ctx, trps, route);
1742 tr_err("trps_update_one_peer: unable to allocate route update.");
1746 g_ptr_array_add(updates, upd);
1748 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1753 /* Second, gather community updates */
1754 tr_debug("trps_update_one_peer: selecting community updates for %.*s.", peer_label->len, peer_label->buf);
1755 rc=trps_select_comm_updates_for_peer(tmp_ctx, updates, trps, peer_label, update_type==TRP_UPDATE_TRIGGERED);
1757 /* see if we have anything to send */
1758 if (updates->len<=0)
1759 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1761 /* Apply outbound TRP filters for this peer */
1762 trps_filter_outbound_updates(peer->filters, updates);
1764 if (updates->len<=0)
1765 tr_debug("trps_update_one_peer: no updates for %.*s after filtering.", peer_label->len, peer_label->buf);
1767 tr_debug("trps_update_one_peer: sending %d update messages.", updates->len);
1768 for (ii=0; ii<updates->len; ii++) {
1769 upd = (TRP_UPD *) g_ptr_array_index(updates, ii);
1770 /* now encode the update message */
1771 tr_msg_set_trp_upd(&msg, upd);
1772 encoded = tr_msg_encode(NULL, &msg);
1773 if (encoded == NULL) {
1774 tr_err("trps_update_one_peer: error encoding update.");
1779 tr_debug("trps_update_one_peer: adding message to queue.");
1780 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1781 tr_err("trps_update_one_peer: error queueing update.");
1783 tr_debug("trps_update_one_peer: update queued successfully.");
1785 tr_msg_free_encoded(encoded);
1795 g_ptr_array_free(updates, TRUE); /* frees any TRP_UPD records */
1796 talloc_free(tmp_ctx);
1800 /* all routes/communities to all peers */
1801 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1803 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1804 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1805 TRP_PEER *peer=NULL;
1806 TRP_RC rc=TRP_SUCCESS;
1808 if (trps->ptable==NULL)
1809 return TRP_SUCCESS; /* no peers, nothing to do */
1812 tr_err("trps_update: failed to allocate peer table iterator.");
1813 talloc_free(tmp_ctx);
1817 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1818 (peer!=NULL) && (rc==TRP_SUCCESS);
1819 peer=trp_ptable_iter_next(iter))
1821 if (!trps_peer_connected(trps, peer)) {
1822 TR_NAME *peer_label=trp_peer_get_label(peer);
1823 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1824 peer_label->len, peer_label->buf);
1827 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1830 tr_debug("trps_update: rc=%u after attempting update.", rc);
1831 trp_ptable_iter_free(iter);
1832 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1833 talloc_free(tmp_ctx);
1837 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1839 trp_rtable_add(trps->rtable, route); /* should return status */
1843 /* steals the peer object */
1844 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1846 if (trps->ptable==NULL) {
1847 trps->ptable=trp_ptable_new(trps);
1848 if (trps->ptable==NULL)
1851 return trp_ptable_add(trps->ptable, peer);
1854 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1856 if (trps->ptable==NULL)
1859 return trp_ptable_find_gss_name(trps->ptable, gssname);
1862 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1864 if (trps->ptable==NULL)
1867 return trp_ptable_find_servicename(trps->ptable, servicename);
1870 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1872 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
1876 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
1883 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1886 TR_NAME *realm=NULL;
1888 tr_debug("trps_handle_request: handling TRP request.");
1890 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
1891 tr_notice("trps_handle_request: received invalid TRP request.");
1895 if (!trp_req_is_wildcard(req)) {
1896 comm=trp_req_get_comm(req);
1897 realm=trp_req_get_realm(req);
1898 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
1899 comm->len, comm->buf, realm->len, realm->buf);
1901 tr_debug("trps_handle_request: all routes requested.");
1902 /* leave comm/realm NULL */
1904 return trps_update_one_peer(trps,
1905 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
1906 TRP_UPDATE_REQUESTED,
1912 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
1914 TRP_RC rc=TRP_ERROR;
1916 switch (tr_msg_get_msg_type(tr_msg)) {
1918 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
1919 if (rc==TRP_SUCCESS) {
1920 rc=trps_update_active_routes(trps);
1921 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
1926 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
1930 /* unknown error or one we don't care about (e.g., TID messages) */
1935 /* send wildcard route request to a peer */
1936 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
1938 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1939 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
1940 TR_MSG msg; /* not a pointer */
1941 TRP_REQ *req=trp_req_new(tmp_ctx);
1943 TRP_RC rc=TRP_ERROR;
1946 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
1950 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
1951 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
1956 tr_msg_set_trp_req(&msg, req);
1957 encoded= tr_msg_encode(NULL, &msg);
1958 if (encoded==NULL) {
1959 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
1964 tr_debug("trps_wildcard_route_req: adding message to queue.");
1965 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
1966 tr_err("trps_wildcard_route_req: error queueing request.");
1969 tr_debug("trps_wildcard_route_req: request queued successfully.");
1975 tr_msg_free_encoded(encoded);
1979 talloc_free(tmp_ctx);