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->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 /* The peer connection (trpc) usually exists even if the connection is down.
269 * We will queue messages even if the connection is down. To prevent this from
270 * endlessly increasing the size of the queue, the trpc handler needs to clear
271 * its queue periodically, even if it is unable to send the messages
274 tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
276 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND);
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 /* update provenance if necessary */
365 trp_upd_add_to_provenance(tr_msg_get_trp_upd(*msg), trp_peer_get_label(peer));
366 /* fall through to next case */
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,
394 n_fd = tr_sock_listen_all(port, fd_out, max_fd);
397 tr_err("trps_get_listener: Error opening port %d.");
399 /* opening port succeeded */
400 tr_info("trps_get_listener: Opened port %d.", port);
402 /* make the sockets non-blocking */
403 for (ii=0; ii<n_fd; ii++) {
404 if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
405 tr_err("trps_get_listener: Error setting O_NONBLOCK.");
406 for (ii=0; ii<n_fd; ii++) {
417 /* store the caller's request handler & cookie */
418 trps->msg_handler = msg_handler;
419 trps->auth_handler = auth_handler;
420 trps->hostname = talloc_strdup(trps, hostname);
421 trps->trps_port = port;
422 trps->cookie = cookie;
428 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
430 /* try to establish a GSS context */
431 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
432 tr_notice("trps_authorize_connection: failed to authorize connection");
433 trp_connection_close(conn);
436 tr_notice("trps_authorize_connection: authorized connection");
440 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
445 /* loop as long as the connection exists */
446 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
447 rc=trps_read_message(trps, conn, &msg);
450 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
454 trp_connection_close(conn);
458 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
462 tr_debug("trps_handle_connection: connection closed.");
465 /* TODO: check realm/comm, now part of the update instead of inforec */
466 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
469 tr_notice("trps_validate_update: null TRP update.");
473 if (trp_upd_get_realm(upd)==NULL) {
474 tr_notice("trps_validate_update: received TRP update without realm.");
478 if (trp_upd_get_comm(upd)==NULL) {
479 tr_notice("trps_validate_update: received TRP update without community.");
483 if (trp_upd_get_inforec(upd)==NULL) {
484 tr_notice("trps_validate_update: received TRP update with no info records.");
488 if (trp_upd_get_peer(upd)==NULL) {
489 tr_notice("trps_validate_update: received TRP update without origin peer information.");
497 /* ensure that the update could be accepted if feasible */
498 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
500 switch(trp_inforec_get_type(rec)) {
501 case TRP_INFOREC_TYPE_ROUTE:
502 if ((trp_inforec_get_trust_router(rec)==NULL)
503 || (trp_inforec_get_next_hop(rec)==NULL)) {
504 tr_debug("trps_validate_inforec: missing record info.");
508 /* check for valid ports */
509 if ((trp_inforec_get_trust_router_port(rec) <= 0)
510 || (trp_inforec_get_trust_router_port(rec) > 65535)) {
511 tr_debug("trps_validate_inforec: invalid trust router port (%d)",
512 trp_inforec_get_trust_router_port(rec));
516 if ((trp_inforec_get_next_hop_port(rec) <= 0)
517 || (trp_inforec_get_next_hop_port(rec) > 65535)) {
518 tr_debug("trps_validate_inforec: invalid next hop port (%d)",
519 trp_inforec_get_next_hop_port(rec));
523 /* check for valid metric */
524 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
525 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
529 /* check for valid interval */
530 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
531 tr_debug("trps_validate_inforec: invalid interval.");
536 case TRP_INFOREC_TYPE_COMMUNITY:
537 /* TODO: validate community updates */
541 tr_notice("trps_validate_inforec: unsupported record type.");
542 return TRP_UNSUPPORTED;
548 /* link cost to a peer */
549 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
554 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
556 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
558 return TRP_METRIC_INFINITY;
559 return trp_route_get_metric(entry) + trps_cost(trps, peer);
562 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
564 unsigned int rec_metric=trp_inforec_get_metric(rec);
565 unsigned int new_metric=0;
566 unsigned int current_metric=0;
567 TR_NAME *next_hop=NULL;
569 /* we check these in the validation stage, but just in case... */
570 if (trp_metric_is_invalid(rec_metric))
573 /* retractions (aka infinite metrics) are always feasible */
574 if (trp_metric_is_infinite(rec_metric))
577 /* updates from our current next hop are always feasible*/
578 next_hop=trps_get_next_hop(trps, comm, realm);
580 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
585 /* compare the existing metric we advertise to what we would advertise
586 * if we accept this update */
587 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
588 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
589 if (new_metric <= current_metric)
595 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
596 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
598 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
599 if (0!=clock_gettime(TRP_CLOCK, ts)) {
600 tr_err("trps_compute_expiry: could not read realtime clock.");
604 tr_debug("trps_compute_expiry: tv_sec=%u, interval=%u, small_factor*interval=%u", ts->tv_sec, interval, small_factor*interval);
605 ts->tv_sec += small_factor*interval;
610 /* compare hostname/port of the trust router, return 0 if they match */
611 static int trust_router_changed(TRP_ROUTE *route, TRP_INFOREC *rec)
613 if (trp_route_get_trust_router_port(route) != trp_inforec_get_trust_router_port(rec))
616 return tr_name_cmp(trp_route_get_trust_router(route),
617 trp_inforec_get_trust_router(rec));
620 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
622 TRP_ROUTE *entry=NULL;
624 entry=trp_rtable_get_entry(trps->rtable,
625 trp_upd_get_comm(upd),
626 trp_upd_get_realm(upd),
627 trp_inforec_get_next_hop(rec));
629 entry=trp_route_new(NULL);
631 tr_err("trps_accept_update: unable to allocate new entry.");
635 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
636 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
637 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
638 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
639 trp_route_set_trust_router_port(entry, trp_inforec_get_trust_router_port(rec));
640 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
641 trp_route_set_next_hop_port(entry, trp_inforec_get_next_hop_port(rec));
642 if ((trp_route_get_comm(entry)==NULL)
643 ||(trp_route_get_realm(entry)==NULL)
644 ||(trp_route_get_peer(entry)==NULL)
645 ||(trp_route_get_trust_router(entry)==NULL)
646 ||(trp_route_get_next_hop(entry)==NULL)) {
647 /* at least one field could not be allocated */
648 tr_err("trps_accept_update: unable to allocate all fields for entry.");
649 trp_route_free(entry);
652 trp_rtable_add(trps->rtable, entry);
655 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
656 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
657 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
658 * time unset on a new route entry. */
659 tr_debug("trps_accept_update: accepting route update.");
660 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
661 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
663 /* check whether the trust router has changed (either name or port) */
664 if (trust_router_changed(entry, rec)) {
665 /* The name changed. Set this route as triggered. */
666 tr_debug("trps_accept_update: trust router for route changed.");
667 trp_route_set_triggered(entry, 1);
668 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
669 trp_route_set_trust_router_port(entry, trp_inforec_get_trust_router_port(rec));
671 if (!trps_route_retracted(trps, entry)) {
672 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
673 trp_route_set_expiry(entry, trps_compute_expiry(trps,
674 trp_route_get_interval(entry),
675 trp_route_get_expiry(entry)));
681 static TRP_RC trps_handle_inforec_route(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
683 TRP_ROUTE *route=NULL;
684 TR_COMM *comm = NULL;
687 /* determine feasibility */
688 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
689 tr_debug("trps_handle_update: record feasibility=%d", feas);
691 /* verify that the community is an APC */
692 comm = tr_comm_table_find_comm(trps->ctable, trp_upd_get_comm(upd));
694 /* We don't know this community. Reject the route. */
695 tr_debug("trps_handle_updates: community %.*s unknown, ignoring route for %.*s",
696 trp_upd_get_comm(upd)->len, trp_upd_get_comm(upd)->buf,
697 trp_upd_get_realm(upd)->len, trp_upd_get_realm(upd)->buf);
698 } else if (tr_comm_get_type(comm) != TR_COMM_APC) {
699 /* The community in a route request *must* be an APC. This was not - ignore it. */
700 tr_debug("trps_handle_updates: community %.*s is not an APC, ignoring route for %.*s",
701 trp_upd_get_comm(upd)->len, trp_upd_get_comm(upd)->buf,
702 trp_upd_get_realm(upd)->len, trp_upd_get_realm(upd)->buf);
704 /* do we have an existing route? */
705 route=trps_get_route(trps,
706 trp_upd_get_comm(upd),
707 trp_upd_get_realm(upd),
708 trp_upd_get_peer(upd));
710 /* there was a route table entry already */
711 tr_debug("trps_handle_updates: route entry already exists.");
713 /* Update is feasible. Accept it. */
714 trps_accept_update(trps, upd, rec);
716 /* Update is infeasible. Ignore it unless the trust router has changed. */
717 if (trust_router_changed(route, rec)) {
718 /* the trust router associated with the route has changed, treat update as a retraction */
719 trps_retract_route(trps, route);
723 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
724 tr_debug("trps_handle_update: no route entry exists yet.");
725 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
726 trps_accept_update(trps, upd, rec);
733 static int trps_name_in_provenance(TR_NAME *name, json_t *prov)
736 TR_NAME *this_name=NULL;
740 return 0; /* no provenance list, so it has no names in it */
742 /* now check to see if name is in the provenance */
743 for (ii=0; ii<json_array_size(prov); ii++) {
744 s=json_string_value(json_array_get(prov, ii));
746 tr_debug("trps_name_in_provenance: empty entry in provenance list.");
750 this_name=tr_new_name(s);
751 if (this_name==NULL) {
752 tr_debug("trps_name_in_provenance: unable to allocate name.");
755 if (0==tr_name_cmp(name, this_name)) {
756 tr_free_name(this_name);
759 tr_free_name(this_name);
764 static TR_COMM *trps_create_new_comm(TALLOC_CTX *mem_ctx, TR_NAME *comm_id, TRP_INFOREC *rec)
766 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
767 TR_COMM *comm=tr_comm_new(tmp_ctx);
770 tr_debug("trps_create_new_comm: unable to allocate new community.");
773 /* fill in the community with info */
774 tr_comm_set_id(comm, tr_dup_name(comm_id));
775 if (tr_comm_get_id(comm)==NULL) {
776 tr_debug("trps_create_new_comm: unable to allocate community name.");
780 tr_comm_set_type(comm, trp_inforec_get_comm_type(rec));
781 if (trp_inforec_get_apcs(rec)!=NULL) {
782 tr_comm_set_apcs(comm, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
783 if (tr_comm_get_apcs(comm)==NULL) {
784 tr_debug("trps_create_new_comm: unable to allocate APC list.");
789 if (trp_inforec_get_owner_realm(rec)!=NULL) {
790 tr_comm_set_owner_realm(comm, tr_dup_name(trp_inforec_get_owner_realm(rec)));
791 if (tr_comm_get_owner_realm(comm)==NULL) {
792 tr_debug("trps_create_new_comm: unable to allocate owner realm name.");
797 if (trp_inforec_get_owner_contact(rec)!=NULL) {
798 tr_comm_set_owner_contact(comm, tr_dup_name(trp_inforec_get_owner_contact(rec)));
799 if (tr_comm_get_owner_contact(comm)==NULL) {
800 tr_debug("trps_create_new_comm: unable to allocate owner contact.");
805 comm->expiration_interval=trp_inforec_get_exp_interval(rec);
806 talloc_steal(mem_ctx, comm);
809 talloc_free(tmp_ctx);
813 static TR_RP_REALM *trps_create_new_rp_realm(TALLOC_CTX *mem_ctx, TR_NAME *comm, TR_NAME *realm_id, TRP_INFOREC *rec)
815 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
816 TR_RP_REALM *rp=tr_rp_realm_new(tmp_ctx);
819 tr_debug("trps_create_new_rp_realm: unable to allocate new realm.");
822 /* fill in the realm */
823 tr_rp_realm_set_id(rp, tr_dup_name(realm_id));
824 if (tr_rp_realm_get_id(rp)==NULL) {
825 tr_debug("trps_create_new_rp_realm: unable to allocate realm name.");
829 talloc_steal(mem_ctx, rp);
832 talloc_free(tmp_ctx);
836 static TR_IDP_REALM *trps_create_new_idp_realm(TALLOC_CTX *mem_ctx,
841 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
842 TR_IDP_REALM *idp=tr_idp_realm_new(tmp_ctx);
843 TR_APC *realm_apcs = NULL;
846 tr_debug("trps_create_new_idp_realm: unable to allocate new realm.");
849 /* fill in the realm */
850 tr_idp_realm_set_id(idp, tr_dup_name(realm_id));
851 if (tr_idp_realm_get_id(idp)==NULL) {
852 tr_debug("trps_create_new_idp_realm: unable to allocate realm name.");
857 /* Set the APCs. If the community is a CoI, copy its APCs. If it is an APC, then
858 * that community itself is the APC for the realm. */
859 if (trp_inforec_get_comm_type(rec) == TR_COMM_APC) {
860 /* the community is an APC for this realm */
861 realm_apcs = tr_apc_new(tmp_ctx);
862 if (realm_apcs == NULL) {
863 tr_debug("trps_create_new_idp_realm: unable to allocate new APC list.");
868 tr_apc_set_id(realm_apcs, tr_dup_name(comm_id));
869 if (tr_apc_get_id(realm_apcs) == NULL) {
870 tr_debug("trps_create_new_idp_realm: unable to allocate new APC name.");
875 /* the community is not an APC for this realm */
876 realm_apcs = trp_inforec_get_apcs(rec);
877 if (realm_apcs == NULL) {
878 tr_debug("trps_create_new_idp_realm: no APCs for realm %.*s/%.*s, cannot add.",
879 realm_id->len, realm_id->buf,
880 comm_id->len, comm_id->buf);
885 /* we have APCs, make our own copy */
886 realm_apcs = tr_apc_dup(tmp_ctx, realm_apcs);
887 if (realm_apcs == NULL) {
888 tr_debug("trps_create_new_idp_realm: unable to duplicate APC list.");
894 /* Whether the community is an APC or CoI, the APCs for the realm are in realm_apcs */
895 tr_idp_realm_set_apcs(idp, realm_apcs); /* takes realm_apcs out of tmp_ctx on success */
896 if (tr_idp_realm_get_apcs(idp) == NULL) {
897 tr_debug("trps_create_new_idp_realm: unable to set APC list for new realm.");
902 idp->origin=TR_REALM_DISCOVERED;
904 talloc_steal(mem_ctx, idp);
907 talloc_free(tmp_ctx);
911 static TRP_RC trps_handle_inforec_comm(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
913 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
914 TR_NAME *comm_id=trp_upd_get_comm(upd);
915 TR_NAME *realm_id=trp_upd_get_realm(upd);
916 TR_NAME *origin_id=NULL;
917 TR_NAME *our_peer_label=NULL;
919 TR_RP_REALM *rp_realm=NULL;
920 TR_IDP_REALM *idp_realm=NULL;
921 struct timespec expiry={0,0};
924 if ((comm_id==NULL) || (realm_id==NULL))
927 origin_id=trp_inforec_dup_origin(rec);
931 /* see whether we want to add this */
932 our_peer_label=trps_dup_label(trps);
933 if (our_peer_label==NULL) {
934 tr_debug("trps_handle_inforec_comm: unable to allocate peer label.");
938 if (trps_name_in_provenance(our_peer_label, trp_inforec_get_provenance(rec)))
939 tr_debug("trps_handle_inforec_comm: rejecting community inforec to avoid provenance loop.");
941 /* no loop occurring, accept the update */
942 comm=tr_comm_table_find_comm(trps->ctable, comm_id);
944 tr_debug("trps_handle_inforec_comm: unknown community %.*s in inforec, creating it.",
945 comm_id->len, comm_id->buf);
946 comm=trps_create_new_comm(tmp_ctx, comm_id, rec);
948 tr_debug("trps_handle_inforec_comm: unable to create new community.");
951 if (tr_comm_table_add_comm(trps->ctable, comm) != 0)
953 tr_debug("trps_handle_inforec_comm: unable to add community to community table.");
957 /* TODO: see if other comm data match the new inforec and update or complain */
959 trps_compute_expiry(trps, trp_inforec_get_interval(rec), &expiry);
960 if ((expiry.tv_sec==0)&&(expiry.tv_nsec==0))
963 switch (trp_inforec_get_role(rec)) {
965 rp_realm=tr_rp_realm_lookup(trps->ctable->rp_realms, realm_id);
966 if (rp_realm==NULL) {
967 tr_debug("trps_handle_inforec_comm: unknown RP realm %.*s in inforec, creating it.",
968 realm_id->len, realm_id->buf);
969 rp_realm= trps_create_new_rp_realm(tmp_ctx, tr_comm_get_id(comm), realm_id, rec);
970 if (rp_realm==NULL) {
971 tr_debug("trps_handle_inforec_comm: unable to create new RP realm.");
972 /* we may leave an unused community in the table, but it will only last until
973 * the next table sweep if it does not get any realms before that happens */
976 tr_comm_table_add_rp_realm(trps->ctable, rp_realm);
978 /* TODO: if realm existed, see if data match the new inforec and update or complain */
979 tr_comm_add_rp_realm(trps->ctable, comm, rp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
980 tr_debug("trps_handle_inforec_comm: added RP realm %.*s to comm %.*s (origin %.*s).",
981 realm_id->len, realm_id->buf,
982 comm_id->len, comm_id->buf,
983 origin_id->len, origin_id->buf);
986 idp_realm=tr_idp_realm_lookup(trps->ctable->idp_realms, realm_id);
987 if (idp_realm==NULL) {
988 tr_debug("trps_handle_inforec_comm: unknown IDP realm %.*s in inforec, creating it.",
989 realm_id->len, realm_id->buf);
990 idp_realm= trps_create_new_idp_realm(tmp_ctx, tr_comm_get_id(comm), realm_id, rec);
991 if (idp_realm==NULL) {
992 tr_debug("trps_handle_inforec_comm: unable to create new IDP realm.");
993 /* we may leave an unused community in the table, but it will only last until
994 * the next table sweep if it does not get any realms before that happens */
997 tr_comm_table_add_idp_realm(trps->ctable, idp_realm);
999 /* TODO: if realm existed, see if data match the new inforec and update or complain */
1000 tr_comm_add_idp_realm(trps->ctable, comm, idp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
1001 tr_debug("trps_handle_inforec_comm: added IDP realm %.*s to comm %.*s (origin %.*s).",
1002 realm_id->len, realm_id->buf,
1003 comm_id->len, comm_id->buf,
1004 origin_id->len, origin_id->buf);
1007 tr_debug("trps_handle_inforec_comm: unable to add realm.");
1015 if (our_peer_label!=NULL)
1016 tr_free_name(our_peer_label);
1017 if (origin_id!=NULL)
1018 tr_free_name(origin_id);
1019 talloc_free(tmp_ctx);
1024 * Apply applicable TRP_INBOUND filters to an inforec. Rejects everything if peer has no filters.
1026 * @param trps Active TRPS instance
1027 * @param upd TRP_UPD that contains the inforec to filter
1028 * @param rec Inforec to filter
1029 * @return 1 if accepted by the filter, 0 otherwise
1031 static int trps_filter_inbound_inforec(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
1033 TRP_PEER *peer=NULL;
1034 TR_NAME *peer_name=NULL;
1035 TR_FILTER_ACTION action=TR_FILTER_ACTION_REJECT;
1036 TR_FILTER_TARGET *target=NULL;
1039 /* Look up the peer. For inbound messages, the peer is identified by its GSS name */
1040 peer_name=trp_upd_get_peer(upd);
1041 peer=trps_get_peer_by_gssname(trps, peer_name);
1043 tr_err("trps_filter_inbound_inforec: received inforec from unknown peer (%.*s), rejecting.",
1049 /* tr_filter_apply() and tr_filter_set_get() handle null filter sets/filters by rejecting */
1050 target= tr_filter_target_trp_inforec(NULL, upd, rec);
1052 /* TODO: signal that filtering failed. Until then, just filter everything and give an error message. */
1053 tr_crit("trps_filter_inbound_inforec: Unable to allocate filter target, cannot apply filter!");
1056 || (TR_FILTER_NO_MATCH==tr_filter_apply(target,
1057 tr_filter_set_get(peer->filters, TR_FILTER_TYPE_TRP_INBOUND),
1060 || (action!=TR_FILTER_ACTION_ACCEPT)) {
1061 /* either the filter did not match or it matched a reject rule or allocating the target failed */
1066 tr_filter_target_free(target);
1068 /* filter matched an accept rule */
1073 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
1075 TRP_INFOREC *rec=NULL;
1077 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
1078 tr_notice("trps_handle_update: received invalid TRP update.");
1082 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1083 /* validate/sanity check the record update */
1084 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
1085 tr_notice("trps_handle_update: invalid inforec in TRP update, discarding entire update.");
1090 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1091 if (!trps_filter_inbound_inforec(trps, upd, rec)) {
1092 tr_debug("trps_handle_update: inforec rejected by filter.");
1093 continue; /* just go on to the next record */
1096 switch (trp_inforec_get_type(rec)) {
1097 case TRP_INFOREC_TYPE_ROUTE:
1098 tr_debug("trps_handle_update: handling route inforec.");
1099 if (TRP_SUCCESS!=trps_handle_inforec_route(trps, upd, rec))
1100 tr_notice("trps_handle_update: error handling route inforec.");
1102 case TRP_INFOREC_TYPE_COMMUNITY:
1103 tr_debug("trps_handle_update: handling community inforec.");
1104 if (TRP_SUCCESS!=trps_handle_inforec_comm(trps, upd, rec))
1105 tr_notice("trps_handle_update: error handling community inforec.");
1109 tr_notice("trps_handle_update: unsupported inforec in TRP update.");
1116 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1119 tr_notice("trps_validate_request: null TRP request.");
1123 if (trp_req_get_comm(req)==NULL) {
1124 tr_notice("trps_validate_request: received TRP request with null community.");
1128 if (trp_req_get_realm(req)==NULL) {
1129 tr_notice("trps_validate_request: received TRP request with null realm.");
1133 if (trp_req_get_peer(req)==NULL) {
1134 tr_notice("trps_validate_request: received TRP request without origin peer information.");
1141 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
1142 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
1145 TR_NAME *exclude_peer_label)
1147 TRP_ROUTE **entry=NULL;
1148 TRP_ROUTE *best=NULL;
1149 TRP_PEER *route_peer = NULL;
1152 unsigned int kk_min=0;
1153 unsigned int min_metric=TRP_METRIC_INFINITY;
1155 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
1156 for (kk=0; kk<n_entry; kk++) {
1157 if (trp_route_get_metric(entry[kk]) < min_metric) {
1158 if (exclude_peer_label != NULL) {
1159 if (!trp_route_is_local(entry[kk])) {
1160 /* route is not local, check the peer label */
1161 route_peer = trp_ptable_find_gss_name(trps->ptable,
1162 trp_route_get_peer(entry[kk]));
1163 if (route_peer == NULL) {
1164 tr_err("trps_find_best_route: unknown peer GSS name (%.*s) for route %d to %.*s/%.*s",
1165 trp_route_get_peer(entry[kk])->len, trp_route_get_peer(entry[kk])->buf,
1167 realm->len, realm->buf,
1168 comm->len, comm->buf);
1169 continue; /* unknown peer, skip the route */
1171 if (0 == tr_name_cmp(exclude_peer_label, trp_peer_get_label(route_peer))) {
1172 /* we're excluding this peer - skip the route */
1177 /* if we get here, we're not excluding the route */
1179 min_metric = trp_route_get_metric(entry[kk]);
1183 if (trp_metric_is_finite(min_metric))
1190 /* TODO: think this through more carefully. At least ought to add hysteresis
1191 * to avoid flapping between routers or routes. */
1192 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
1194 size_t n_comm=0, ii=0;
1195 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1196 size_t n_realm=0, jj=0;
1197 TR_NAME **realm=NULL;
1198 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
1199 unsigned int best_metric=0, cur_metric=0;
1201 for (ii=0; ii<n_comm; ii++) {
1202 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1203 for (jj=0; jj<n_realm; jj++) {
1204 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
1205 if (best_route==NULL)
1206 best_metric=TRP_METRIC_INFINITY;
1208 best_metric=trp_route_get_metric(best_route);
1210 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
1211 if (cur_route!=NULL) {
1212 cur_metric=trp_route_get_metric(cur_route);
1213 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
1214 /* The new route has a lower metric than the previous, and is finite. Accept. */
1215 trp_route_set_selected(cur_route, 0);
1216 trp_route_set_selected(best_route, 1);
1217 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
1218 trp_route_set_selected(cur_route, 0);
1219 } else if (trp_metric_is_finite(best_metric)) {
1220 trp_route_set_selected(best_route, 1);
1225 realm=NULL; n_realm=0;
1229 comm=NULL; n_comm=0;
1234 /* true if curtime >= expiry */
1235 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
1237 return (tr_cmp_timespec(curtime, expiry) >= 0);
1240 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
1241 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
1242 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
1244 struct timespec sweep_time={0,0};
1245 TRP_ROUTE **entry=NULL;
1249 /* use a single time for the entire sweep */
1250 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1251 tr_err("trps_sweep_routes: could not read realtime clock.");
1252 sweep_time.tv_sec=0;
1253 sweep_time.tv_nsec=0;
1257 entry= trp_rtable_get_entries(NULL, trps->rtable, &n_entry); /* must talloc_free *entry */
1259 /* loop over the entries */
1260 for (ii=0; ii<n_entry; ii++) {
1261 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
1262 tr_debug("trps_sweep_routes: route expired.");
1263 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
1265 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
1266 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
1269 /* set metric to infinity and reset timer */
1270 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
1271 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
1272 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
1273 trp_route_get_interval(entry[ii]),
1274 trp_route_get_expiry(entry[ii])));
1284 /* Sweep for expired communities/realms/memberships. */
1285 TRP_RC trps_sweep_ctable(TRPS_INSTANCE *trps)
1287 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1288 struct timespec sweep_time={0,0};
1289 struct timespec tmp = {0};
1290 TR_COMM_MEMB *memb=NULL;
1291 TR_COMM_ITER *iter=NULL;
1292 TRP_RC rc=TRP_ERROR;
1294 /* use a single time for the entire sweep */
1295 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1296 tr_err("trps_sweep_ctable: could not read realtime clock.");
1297 sweep_time.tv_sec=0;
1298 sweep_time.tv_nsec=0;
1302 /* iterate all memberships */
1303 iter=tr_comm_iter_new(tmp_ctx);
1305 tr_err("trps_sweep_ctable: unable to allocate iterator.");
1309 for (memb=tr_comm_memb_iter_all_first(iter, trps->ctable);
1311 memb=tr_comm_memb_iter_all_next(iter)) {
1312 if (tr_comm_memb_get_origin(memb)==NULL)
1313 continue; /* do not expire local entries */
1315 if (tr_comm_memb_is_expired(memb, &sweep_time)) {
1316 if (tr_comm_memb_get_times_expired(memb)>0) {
1317 /* Already expired once; flush. */
1318 tr_debug("trps_sweep_ctable: flushing expired community membership (%.*s in %.*s, origin %.*s, expired %s).",
1319 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1320 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1321 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf,
1322 timespec_to_str(tr_comm_memb_get_expiry_realtime(memb, &tmp)));
1323 tr_comm_table_remove_memb(trps->ctable, memb);
1324 tr_comm_memb_free(memb);
1326 /* This is the first expiration. Note this and reset the expiry time. */
1327 tr_comm_memb_expire(memb);
1328 trps_compute_expiry(trps, tr_comm_memb_get_interval(memb), tr_comm_memb_get_expiry(memb));
1329 tr_debug("trps_sweep_ctable: community membership expired at %s, resetting expiry to %s (%.*s in %.*s, origin %.*s).",
1330 timespec_to_str(tr_clock_convert(TRP_CLOCK, &sweep_time, CLOCK_REALTIME, &tmp)),
1331 timespec_to_str(tr_comm_memb_get_expiry_realtime(memb, &tmp)),
1332 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1333 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1334 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf);
1339 /* get rid of any unreferenced realms, etc */
1340 tr_comm_table_sweep(trps->ctable);
1343 talloc_free(tmp_ctx);
1348 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
1350 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
1351 return TRP_METRIC_INVALID;
1353 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
1354 return TRP_METRIC_INFINITY;
1356 if (trp_metric_is_finite(m1+m2))
1359 return TRP_METRIC_INFINITY;
1362 /* convert an rentry into a new trp update info record */
1363 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1365 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
1366 unsigned int linkcost=0;
1369 if (trp_route_is_local(route))
1372 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
1373 trp_route_get_peer(route)));
1377 * This is where we add the link cost (currently always 1) to the next peer.
1379 * Here, set next_hop to our TID address/port rather than passing along our own
1380 * next_hop. That is the one *we* use to forward requests. We are advertising
1381 * ourselves as a hop for our peers.
1383 if ((TRP_SUCCESS != trp_inforec_set_trust_router(rec,
1384 trp_route_dup_trust_router(route),
1385 trp_route_get_trust_router_port(route)))
1386 ||(TRP_SUCCESS != trp_inforec_set_next_hop(rec,
1387 tr_new_name(trps->hostname),
1389 ||(TRP_SUCCESS != trp_inforec_set_metric(rec,
1390 trps_metric_add(trp_route_get_metric(route),
1392 ||(TRP_SUCCESS != trp_inforec_set_interval(rec, trps_get_update_interval(trps)))) {
1393 tr_err("trps_route_to_inforec: error creating route update.");
1401 static TRP_UPD *trps_route_to_upd(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1403 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1404 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1405 TRP_INFOREC *rec=NULL;
1408 tr_err("trps_route_to_upd: could not create update message.");
1411 trp_upd_set_realm(upd, trp_route_dup_realm(route));
1412 if (trp_upd_get_realm(upd)==NULL) {
1413 tr_err("trps_route_to_upd: could not copy realm.");
1414 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1417 trp_upd_set_comm(upd, trp_route_dup_comm(route));
1418 if (trp_upd_get_comm(upd)==NULL) {
1419 tr_err("trps_route_to_upd: could not copy comm.");
1420 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1423 rec=trps_route_to_inforec(tmp_ctx, trps, route);
1425 tr_err("trps_route_to_upd: could not create route info record for realm %.*s in comm %.*s.",
1426 trp_route_get_realm(route)->len, trp_route_get_realm(route)->buf,
1427 trp_route_get_comm(route)->len, trp_route_get_comm(route)->buf);
1428 upd=NULL; /* it's till in tmp_ctx, so it will be freed */
1431 trp_upd_add_inforec(upd, rec);
1434 talloc_steal(mem_ctx, upd);
1437 talloc_free(tmp_ctx);
1441 /* select the correct route to comm/realm to be announced to peer */
1442 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_label)
1444 TRP_ROUTE *route = NULL;
1445 TRP_PEER *route_peer = NULL;
1446 TR_NAME *route_peer_label = NULL;
1448 /* Take the currently selected route unless it is through the peer we're sending the update to.
1449 * I.e., enforce the split horizon rule. Start by looking up the currently selected route. */
1450 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
1452 /* No selected route, this should only happen if the only route has been retracted,
1453 * in which case we do not want to advertise it. */
1457 /* Check whether it's local. */
1458 if (trp_route_is_local(route)) {
1459 /* It is always ok to announce a local route */
1460 tr_debug("trps_select_realm_update: selected route for %.*s/%.*s is local",
1461 realm->len, realm->buf,
1462 comm->len, comm->buf);
1464 /* It's not local. Get the route's peer and check whether it's the same place we
1465 * got the selected route from. Peer should always correspond to an entry in our
1467 tr_debug("trps_select_realm_update: selected route for %.*s/%.*s is not local",
1468 realm->len, realm->buf,
1469 comm->len, comm->buf);
1470 route_peer = trp_ptable_find_gss_name(trps->ptable, trp_route_get_peer(route));
1471 if (route_peer == NULL) {
1472 tr_err("trps_select_realm_update: unknown peer GSS name (%.*s) for selected route for %.*s/%.*s",
1473 trp_route_get_peer(route)->len, trp_route_get_peer(route)->buf,
1474 realm->len, realm->buf,
1475 comm->len, comm->buf);
1478 route_peer_label = trp_peer_get_label(route_peer);
1479 if (route_peer_label == NULL) {
1480 tr_err("trps_select_realm_update: error retrieving peer label for selected route for %.*s/%.*s",
1481 realm->len, realm->buf,
1482 comm->len, comm->buf);
1486 /* see if these match */
1487 tr_debug("trps_select_realm_update: %.*s vs %.*s",
1488 peer_label->len, peer_label->buf,
1489 route_peer_label->len, route_peer_label->buf);
1491 if (0==tr_name_cmp(peer_label, route_peer_label)) {
1492 /* the selected entry goes through the peer we're reporting to, choose an alternate */
1493 tr_debug("trps_select_realm_update: matched, finding alternate route");
1494 route=trps_find_best_route(trps, comm, realm, peer_label);
1495 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route)))) {
1496 tr_debug("trps_select_realm_update: no route to %.*s/%.*s suitable to advertise to %.*s",
1497 realm->len, realm->buf,
1498 comm->len, comm->buf,
1499 peer_label->len, peer_label->buf);
1500 return NULL; /* don't advertise a nonexistent or retracted route */
1507 /* Add TRP_UPD msgs to the updates GPtrArray. Caller needs to arrange for these to be freed. */
1508 static TRP_RC trps_select_route_updates_for_peer(TALLOC_CTX *mem_ctx,
1510 TRPS_INSTANCE *trps,
1511 TR_NAME *peer_label,
1515 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1516 TR_NAME **realm=NULL;
1519 TRP_ROUTE *best=NULL;
1525 for (ii=0; ii<n_comm; ii++) {
1526 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1527 for (jj=0; jj<n_realm; jj++) {
1528 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_label);
1529 /* If we found a route, add it to the list. If triggered!=0, then only
1530 * add triggered routes. */
1531 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best))) {
1532 upd=trps_route_to_upd(mem_ctx, trps, best);
1534 tr_err("trps_select_route_updates_for_peer: unable to create update message.");
1537 g_ptr_array_add(updates, upd);
1553 static TRP_INFOREC *trps_memb_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_COMM_MEMB *memb)
1555 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1556 TRP_INFOREC *rec=NULL;
1562 comm=tr_comm_memb_get_comm(memb);
1563 rec=trp_inforec_new(tmp_ctx, TRP_INFOREC_TYPE_COMMUNITY);
1567 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_get_type(comm))) {
1572 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_comm_memb_get_role(memb))) {
1577 if ((NULL!=tr_comm_get_apcs(comm)) &&
1578 ( (TRP_SUCCESS!=trp_inforec_set_apcs(rec,
1579 tr_apc_dup(rec, tr_comm_get_apcs(comm)))) ||
1580 (NULL==trp_inforec_get_apcs(rec)))) {
1585 if ((NULL!=tr_comm_get_owner_realm(comm)) &&
1586 ( (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_dup_name(tr_comm_get_owner_realm(comm)))) ||
1587 (NULL==trp_inforec_get_owner_realm(rec)))) {
1592 if ((NULL!=tr_comm_get_owner_contact(comm)) &&
1593 ( (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_dup_name(tr_comm_get_owner_contact(comm)))) ||
1594 (NULL==trp_inforec_get_owner_contact(rec)))) {
1599 if ((NULL!=tr_comm_memb_get_provenance(memb)) &&
1600 (TRP_SUCCESS!=trp_inforec_set_provenance(rec, tr_comm_memb_get_provenance(memb)))) {
1605 if (TRP_SUCCESS!=trp_inforec_set_interval(rec, trps_get_update_interval(trps))) {
1611 talloc_steal(mem_ctx, rec);
1614 talloc_free(tmp_ctx);
1618 /* construct an update with all the inforecs for comm/realm/role to be sent to peer */
1619 static TRP_UPD *trps_comm_update(TALLOC_CTX *mem_ctx,
1620 TRPS_INSTANCE *trps,
1621 TR_NAME *peer_label,
1625 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1626 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1627 TRP_INFOREC *rec=NULL;
1628 TR_COMM_ITER *iter=NULL;
1629 TR_COMM_MEMB *memb=NULL;
1634 trp_upd_set_comm(upd, tr_comm_dup_id(comm));
1635 trp_upd_set_realm(upd, tr_realm_dup_id(realm));
1636 /* leave peer empty */
1638 iter=tr_comm_iter_new(tmp_ctx);
1640 tr_err("trps_comm_update: unable to allocate iterator.");
1645 /* now add inforecs */
1646 switch (realm->role) {
1648 memb=tr_comm_table_find_idp_memb(trps->ctable,
1649 tr_realm_get_id(realm),
1650 tr_comm_get_id(comm));
1653 memb=tr_comm_table_find_rp_memb(trps->ctable,
1654 tr_realm_get_id(realm),
1655 tr_comm_get_id(comm));
1661 for (memb=tr_comm_memb_iter_first(iter, memb);
1663 memb=tr_comm_memb_iter_next(iter)) {
1664 rec=trps_memb_to_inforec(tmp_ctx, trps, memb);
1666 tr_err("trps_comm_update: unable to allocate inforec.");
1670 trp_upd_add_inforec(upd, rec);
1674 if (trp_upd_get_inforec(upd)==NULL)
1675 upd=NULL; /* no inforecs, no reason to send the update */
1677 talloc_steal(mem_ctx, upd); /* success! */
1680 talloc_free(tmp_ctx);
1684 /* Find all community updates to send to a peer and add these as TR_UPD records
1685 * to the updates GPtrArray. */
1686 static TRP_RC trps_select_comm_updates_for_peer(TALLOC_CTX *mem_ctx,
1688 TRPS_INSTANCE *trps,
1689 TR_NAME *peer_label,
1692 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1693 TR_COMM_ITER *comm_iter=NULL;
1695 TR_COMM_ITER *realm_iter=NULL;
1696 TR_REALM *realm=NULL;
1698 TRP_RC rc=TRP_ERROR;
1700 /* currently do not send any communities on triggered updates */
1706 comm_iter=tr_comm_iter_new(tmp_ctx);
1707 realm_iter=tr_comm_iter_new(tmp_ctx);
1708 if ((comm_iter==NULL) || (realm_iter==NULL)) {
1709 tr_err("trps_select_comm_updates_for_peer: unable to allocate iterator.");
1714 /* do every community */
1715 for (comm=tr_comm_table_iter_first(comm_iter, trps->ctable);
1717 comm=tr_comm_table_iter_next(comm_iter)) {
1718 /* do every realm in this community */
1719 tr_debug("trps_select_comm_updates_for_peer: looking through community %.*s",
1720 tr_comm_get_id(comm)->len,
1721 tr_comm_get_id(comm)->buf);
1722 for (realm=tr_realm_iter_first(realm_iter, trps->ctable, tr_comm_get_id(comm));
1724 realm=tr_realm_iter_next(realm_iter)) {
1725 /* get the update for this comm/realm */
1726 tr_debug("trps_select_comm_updates_for_peer: adding realm %.*s",
1727 tr_realm_get_id(realm)->len,
1728 tr_realm_get_id(realm)->buf);
1729 upd=trps_comm_update(mem_ctx, trps, peer_label, comm, realm);
1731 g_ptr_array_add(updates, upd);
1736 talloc_free(tmp_ctx);
1741 * Filter the inforecs in a single update
1743 * @param filt The filter to apply
1744 * @param upd The update to filter
1746 static void trps_filter_one_outbound_update(TR_FILTER *filt, TRP_UPD *upd)
1748 TRP_INFOREC *this=NULL, *next=NULL;
1749 TR_FILTER_ACTION action=TR_FILTER_ACTION_REJECT;
1750 TR_FILTER_TARGET *target=NULL;
1752 for(this=trp_upd_get_inforec(upd); this!=NULL; this=next) {
1754 target= tr_filter_target_trp_inforec(NULL, upd, this);
1756 /* TODO: signal that filtering failed. Until then, just filter everything and give an error message. */
1757 tr_crit("trps_filter_one_outbound_update: Unable to allocate filter target, cannot apply filter!");
1760 || (TR_FILTER_NO_MATCH==tr_filter_apply(target, filt, NULL, &action))
1761 || (action!=TR_FILTER_ACTION_ACCEPT)) {
1762 /* Either no filter matched or one matched and rejected this record.
1763 * Also filter out record if we were unable to allocate a target. */
1764 trp_upd_remove_inforec(upd, this); /* "this" is now invalid */
1767 tr_filter_target_free(target);
1772 * May shuffle the update list.
1774 * @param filters The filter set for the relevant TRP peer
1775 * @param updates GPtrArray of updates to filter
1777 static void trps_filter_outbound_updates(TR_FILTER_SET *filters, GPtrArray *updates)
1782 /* Walk backward through the array so we can remove elements. Careful about loop
1783 * termination - remember that ii is unsigned. */
1784 for (ii=updates->len; ii>0; ii--) {
1785 upd=g_ptr_array_index(updates, ii-1);
1786 trps_filter_one_outbound_update(tr_filter_set_get(filters, TR_FILTER_TYPE_TRP_OUTBOUND), upd);
1787 /* see if we removed all the records from this update */
1788 if (trp_upd_num_inforecs(upd)==0)
1789 g_ptr_array_remove_index_fast(updates, ii-1); /* does not preserve order at index ii or higher */
1793 /* helper for trps_update_one_peer. Frees the TRP_UPD pointed to by a GPtrArray element */
1794 static void trps_trp_upd_destroy(gpointer data)
1796 trp_upd_free((TRP_UPD *)data);
1799 /* all routes/communities to a single peer, unless comm/realm are specified (both or neither must be NULL) */
1800 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
1802 TRP_UPDATE_TYPE update_type,
1806 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1807 TR_MSG msg; /* not a pointer! */
1809 TRP_ROUTE *route=NULL;
1812 TRP_RC rc=TRP_ERROR;
1813 TR_NAME *peer_label=trp_peer_get_label(peer);
1814 GPtrArray *updates=g_ptr_array_new_with_free_func(trps_trp_upd_destroy);
1816 if (updates==NULL) {
1817 tr_err("trps_update_one_peer: unable to allocate updates array.");
1822 switch (update_type) {
1823 case TRP_UPDATE_TRIGGERED:
1824 tr_debug("trps_update_one_peer: preparing triggered update for %.*s",
1825 peer_label->len, peer_label->buf);
1827 case TRP_UPDATE_SCHEDULED:
1828 tr_debug("trps_update_one_peer: preparing scheduled update for %.*s",
1829 peer_label->len, peer_label->buf);
1831 case TRP_UPDATE_REQUESTED:
1832 tr_debug("trps_update_one_peer: preparing requested update for %.*s",
1833 peer_label->len, peer_label->buf);
1836 tr_err("trps_update_one_peer: invalid update type requested.");
1841 /* First, gather route updates. */
1842 tr_debug("trps_update_one_peer: selecting route updates for %.*s.", peer_label->len, peer_label->buf);
1843 if ((comm==NULL) && (realm==NULL)) {
1845 rc=trps_select_route_updates_for_peer(tmp_ctx,
1849 update_type==TRP_UPDATE_TRIGGERED);
1850 } else if ((comm!=NULL) && (realm!=NULL)) {
1851 /* a single community/realm was requested */
1852 route=trps_select_realm_update(trps, comm, realm, peer_label);
1854 /* we have no actual update to send back, MUST send a retraction */
1855 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1856 route=trp_route_new(tmp_ctx);
1857 trp_route_set_comm(route, tr_dup_name(comm));
1858 trp_route_set_realm(route, tr_dup_name(realm));
1859 trp_route_set_peer(route, tr_new_name(""));
1860 trp_route_set_metric(route, TRP_METRIC_INFINITY);
1861 trp_route_set_trust_router(route, tr_new_name(""));
1862 trp_route_set_next_hop(route, tr_new_name(""));
1864 upd=trps_route_to_upd(tmp_ctx, trps, route);
1866 tr_err("trps_update_one_peer: unable to allocate route update.");
1870 g_ptr_array_add(updates, upd);
1872 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1877 /* Second, gather community updates */
1878 tr_debug("trps_update_one_peer: selecting community updates for %.*s.", peer_label->len, peer_label->buf);
1879 rc=trps_select_comm_updates_for_peer(tmp_ctx, updates, trps, peer_label, update_type==TRP_UPDATE_TRIGGERED);
1881 /* see if we have anything to send */
1882 if (updates->len<=0)
1883 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1885 /* Apply outbound TRP filters for this peer */
1886 trps_filter_outbound_updates(peer->filters, updates);
1888 if (updates->len<=0)
1889 tr_debug("trps_update_one_peer: no updates for %.*s after filtering.", peer_label->len, peer_label->buf);
1891 tr_debug("trps_update_one_peer: sending %d update messages.", updates->len);
1892 for (ii=0; ii<updates->len; ii++) {
1893 upd = (TRP_UPD *) g_ptr_array_index(updates, ii);
1894 /* now encode the update message */
1895 tr_msg_set_trp_upd(&msg, upd);
1896 encoded = tr_msg_encode(NULL, &msg);
1897 if (encoded == NULL) {
1898 tr_err("trps_update_one_peer: error encoding update.");
1903 tr_debug("trps_update_one_peer: adding message to queue.");
1904 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1905 tr_err("trps_update_one_peer: error queueing update.");
1907 tr_debug("trps_update_one_peer: update queued successfully.");
1909 tr_msg_free_encoded(encoded);
1919 g_ptr_array_free(updates, TRUE); /* frees any TRP_UPD records */
1920 talloc_free(tmp_ctx);
1924 /* all routes/communities to all peers */
1925 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1927 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1928 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1929 TRP_PEER *peer=NULL;
1930 TRP_RC rc=TRP_SUCCESS;
1932 if (trps->ptable==NULL)
1933 return TRP_SUCCESS; /* no peers, nothing to do */
1936 tr_err("trps_update: failed to allocate peer table iterator.");
1937 talloc_free(tmp_ctx);
1941 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1942 (peer!=NULL) && (rc==TRP_SUCCESS);
1943 peer=trp_ptable_iter_next(iter))
1945 if (!trps_peer_connected(trps, peer)) {
1946 TR_NAME *peer_label=trp_peer_get_label(peer);
1947 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1948 peer_label->len, peer_label->buf);
1951 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1954 tr_debug("trps_update: rc=%u after attempting update.", rc);
1955 trp_ptable_iter_free(iter);
1956 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1957 talloc_free(tmp_ctx);
1961 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1963 trp_rtable_add(trps->rtable, route); /* should return status */
1967 /* steals the peer object */
1968 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1970 if (trps->ptable==NULL) {
1971 trps->ptable=trp_ptable_new(trps);
1972 if (trps->ptable==NULL)
1975 return trp_ptable_add(trps->ptable, peer);
1978 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1980 if (trps->ptable==NULL)
1983 return trp_ptable_find_gss_name(trps->ptable, gssname);
1986 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1988 if (trps->ptable==NULL)
1991 return trp_ptable_find_servicename(trps->ptable, servicename);
1994 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1996 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
2000 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
2007 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
2010 TR_NAME *realm=NULL;
2012 tr_debug("trps_handle_request: handling TRP request.");
2014 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
2015 tr_notice("trps_handle_request: received invalid TRP request.");
2019 if (!trp_req_is_wildcard(req)) {
2020 comm=trp_req_get_comm(req);
2021 realm=trp_req_get_realm(req);
2022 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
2023 comm->len, comm->buf, realm->len, realm->buf);
2025 tr_debug("trps_handle_request: all routes requested.");
2026 /* leave comm/realm NULL */
2028 return trps_update_one_peer(trps,
2029 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
2030 TRP_UPDATE_REQUESTED,
2036 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
2038 TRP_RC rc=TRP_ERROR;
2040 switch (tr_msg_get_msg_type(tr_msg)) {
2042 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
2043 if (rc==TRP_SUCCESS) {
2044 rc=trps_update_active_routes(trps);
2045 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
2050 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
2054 /* unknown error or one we don't care about (e.g., TID messages) */
2059 /* send wildcard route request to a peer */
2060 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
2062 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
2063 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
2064 TR_MSG msg; /* not a pointer */
2065 TRP_REQ *req=trp_req_new(tmp_ctx);
2067 TRP_RC rc=TRP_ERROR;
2070 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
2074 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
2075 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
2080 tr_msg_set_trp_req(&msg, req);
2081 encoded= tr_msg_encode(NULL, &msg);
2082 if (encoded==NULL) {
2083 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
2088 tr_debug("trps_wildcard_route_req: adding message to queue.");
2089 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
2090 tr_err("trps_wildcard_route_req: error queueing request.");
2093 tr_debug("trps_wildcard_route_req: request queued successfully.");
2099 tr_msg_free_encoded(encoded);
2103 talloc_free(tmp_ctx);