| 1 |
/********************************************************************************************************* |
| 2 |
* Software License Agreement (BSD License) * |
| 3 |
* Author: Sebastien Decugis <[email protected]> and * |
| 4 |
* Thomas Klausner <[email protected]> * |
| 5 |
* * |
| 6 |
* Copyright (c) 2013, 2014, WIDE Project and NICT * |
| 7 |
* All rights reserved. * |
| 8 |
* * |
| 9 |
* Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 10 |
* permitted provided that the following conditions are met: * |
| 11 |
* * |
| 12 |
* * Redistributions of source code must retain the above * |
| 13 |
* copyright notice, this list of conditions and the * |
| 14 |
* following disclaimer. * |
| 15 |
* * |
| 16 |
* * Redistributions in binary form must reproduce the above * |
| 17 |
* copyright notice, this list of conditions and the * |
| 18 |
* following disclaimer in the documentation and/or other * |
| 19 |
* materials provided with the distribution. * |
| 20 |
* * |
| 21 |
* * Neither the name of the WIDE Project or NICT nor the * |
| 22 |
* names of its contributors may be used to endorse or * |
| 23 |
* promote products derived from this software without * |
| 24 |
* specific prior written permission of WIDE Project and * |
| 25 |
* NICT. * |
| 26 |
* * |
| 27 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 28 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 29 |
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 30 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 31 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 32 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 33 |
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 34 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 35 |
*********************************************************************************************************/ |
| 36 |
|
| 37 |
/* Diameter Redirect management */ |
| 38 |
#include <freeDiameter/extension.h> |
| 39 |
|
| 40 |
#include "uthash.h" |
| 41 |
|
| 42 |
/* There are 2 locks in this module. The priority is established as follow to avoid deadlocks: |
| 43 |
exp_peer mutex > usages rwlock. |
| 44 |
(e.g., the rwlock can be taken while holding the mutex, but not the other way) |
| 45 |
*/ |
| 46 |
|
| 47 |
/* The types of redirects (from Redirect-Host-Usage AVP value) */ |
| 48 |
enum redir_h_u { |
| 49 |
DONT_CACHE = 0, |
| 50 |
ALL_SESSION, |
| 51 |
ALL_REALM, |
| 52 |
REALM_AND_APPLICATION, |
| 53 |
ALL_APPLICATION, |
| 54 |
ALL_HOST, |
| 55 |
ALL_USER |
| 56 |
}; |
| 57 |
#define H_U_MAX ALL_USER |
| 58 |
|
| 59 |
/* Eye catcher */ |
| 60 |
#define REDIR_ENTRY_EYEC 0x43D14E74 |
| 61 |
|
| 62 |
/* Expiration time set for DONT_CACHE tasks, so that the entry is found when the code is called back */ |
| 63 |
#define DEFAULT_EXPIRE_TIME 10 /* seconds */ |
| 64 |
|
| 65 |
/* Structure to store a parsed Redirect-Host */ |
| 66 |
struct redir_host { |
| 67 |
struct fd_list chain; |
| 68 |
|
| 69 |
DiamId_t id; /* malloc'd */ |
| 70 |
size_t len; |
| 71 |
/* We don't use the following yet because we don't support dynamic new connections |
| 72 |
int secure; |
| 73 |
uint16_t port; |
| 74 |
int l4; |
| 75 |
char proto; |
| 76 |
*/ |
| 77 |
}; |
| 78 |
|
| 79 |
/* Rule data that is stored depending on Redirect-Host-Usage value */ |
| 80 |
union matchdata { |
| 81 |
/* DONT_CACHE */ |
| 82 |
struct { |
| 83 |
struct msg * msg; /* The query message for which this rule was created */ |
| 84 |
} message; |
| 85 |
|
| 86 |
/* ALL_SESSION */ |
| 87 |
struct { |
| 88 |
os0_t s; /* sid */ |
| 89 |
size_t l; |
| 90 |
} session; |
| 91 |
|
| 92 |
/* ALL_REALM */ |
| 93 |
struct { |
| 94 |
os0_t s; /* Destination-Realm AVP data */ |
| 95 |
size_t l; |
| 96 |
} realm; |
| 97 |
|
| 98 |
/* REALM_AND_APPLICATION */ |
| 99 |
struct { |
| 100 |
application_id_t a; /* message's application */ |
| 101 |
os0_t s; /* Destination-Realm AVP data */ |
| 102 |
size_t l; |
| 103 |
} realm_app; |
| 104 |
|
| 105 |
/* ALL_APPLICATION */ |
| 106 |
struct { |
| 107 |
application_id_t a; /* message's application */ |
| 108 |
} app; |
| 109 |
|
| 110 |
/* ALL_HOST */ |
| 111 |
struct { |
| 112 |
os0_t s; /* the Origin-Host of the Redirect message (which may be the next hop or not) */ |
| 113 |
size_t l; |
| 114 |
} host; |
| 115 |
|
| 116 |
/* ALL_USER */ |
| 117 |
struct { |
| 118 |
os0_t s; /* User-Name AVP data */ |
| 119 |
size_t l; |
| 120 |
} user; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
/* Structure to store a Redirect indication */ |
| 125 |
struct redir_entry { |
| 126 |
uint32_t eyec; /* must be REDIR_ENTRY_EYEC, used for debug only */ |
| 127 |
|
| 128 |
struct { |
| 129 |
os0_t s; /* alloc'd, must be freed */ |
| 130 |
size_t l; |
| 131 |
} from; /* whom this rule was received from (next hop) ? */ |
| 132 |
|
| 133 |
struct fd_list target_peers_list; /* The list of Redirect-Hosts for this entry */ |
| 134 |
|
| 135 |
struct timespec timeout; /* When does this entry expire? */ |
| 136 |
struct fd_list exp_list; /* chain in the expire_list list, ordered by expiration date, protected by exp_peer_lock */ |
| 137 |
|
| 138 |
enum redir_h_u type; /* Type of this entry */ |
| 139 |
struct fd_list redir_list; /* link in redirects_usages lists. Lists are ordered by the data value. Protected by rw locks */ |
| 140 |
union matchdata data; /* The strings are duplicated & must be freed in this structure */ |
| 141 |
UT_hash_handle hh; /* magic entry for hash table */ |
| 142 |
}; |
| 143 |
|
| 144 |
/* The array where the redir_entries are stored */ |
| 145 |
struct redir_line { |
| 146 |
enum fd_rt_out_score score; |
| 147 |
pthread_rwlock_t lock; /* protect the list */ |
| 148 |
struct fd_list sentinel; /* list of redir_entry, the "o" field of the sentinel points to the redir_line entry */ |
| 149 |
}; |
| 150 |
extern struct redir_line redirects_usages[]; |
| 151 |
/* the hash table where entries are stored for ALL_SESSION and ALL_USER */ |
| 152 |
extern struct redir_entry *redirect_hash_table[]; |
| 153 |
|
| 154 |
/* Accelerator to the line lock */ |
| 155 |
#define RWLOCK_REDIR( _entry ) ( &(redirects_usages[(_entry)->type].lock) ) |
| 156 |
|
| 157 |
/* Lock that must be owned before calling some functions */ |
| 158 |
extern pthread_mutex_t redir_exp_peer_lock; |
| 159 |
|
| 160 |
/* Dictionary cached objects */ |
| 161 |
extern struct dict_object * redir_dict_dr; |
| 162 |
extern struct dict_object * redir_dict_un; |
| 163 |
|
| 164 |
/* Functions on redir_entry */ |
| 165 |
int redir_entry_init(); |
| 166 |
int redir_entry_fini(); |
| 167 |
int redir_entry_new(struct redir_entry ** e, struct fd_list * targets, uint32_t rhu, struct msg * qry, DiamId_t nh, size_t nhlen, os0_t oh, size_t ohlen); |
| 168 |
extern int (*redir_entry_cmp_key[])(union matchdata * , union matchdata *); /* compare functions */ |
| 169 |
int redir_entry_insert(struct redir_entry * e); |
| 170 |
int redir_entry_destroy(struct redir_entry * e); |
| 171 |
|
| 172 |
/* Functions for expiry */ |
| 173 |
void * redir_exp_thr_fct(void * arg); |
| 174 |
int redir_exp_set(struct redir_entry * e, uint32_t duration); |
| 175 |
|
| 176 |
/* Forward cb */ |
| 177 |
int redir_fwd_cb(void * cbdata, struct msg ** msg); |
| 178 |
|
| 179 |
/* Out callback */ |
| 180 |
int redir_out_cb(void * cbdata, struct msg ** pmsg, struct fd_list * candidates); |