blob: dc87f8d9d4502bdc8c2374a1ce3dccfced1f598c [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include "fdcore-internal.h"
37
38/* This file contains code to handle Device Watchdog messages (DWR and DWA) */
39
40/* Check the value of Origin-State-Id is consistent in a DWR or DWA -- we return an error otherwise */
41static int check_state_id(struct msg * msg, struct fd_peer * peer)
42{
43 struct avp * osi;
44
45 /* Check if the request contains the Origin-State-Id */
46 CHECK_FCT( fd_msg_search_avp ( msg, fd_dict_avp_OSI, &osi ) );
47 if (osi) {
48 /* Check the value is consistent with the saved one */
49 struct avp_hdr * hdr;
50 CHECK_FCT( fd_msg_avp_hdr( osi, &hdr ) );
51 if (hdr->avp_value == NULL) {
52 /* This is a sanity check */
53 LOG_F("Ignored an Origin-State-Id AVP with unset value in DWR/DWA");
54 ASSERT(0); /* To check if this really happens, and understand why... */
55 }
56
57 if (! peer->p_hdr.info.runtime.pir_orstate) {
58 /* It was not already received in CER/CEA, save it now */
59 peer->p_hdr.info.runtime.pir_orstate = hdr->avp_value->u32;
60 }
61
62 if (peer->p_hdr.info.runtime.pir_orstate != hdr->avp_value->u32) {
63 TRACE_DEBUG(INFO, "Received a new Origin-State-Id from peer '%s'! (%x -> %x); resetting the connection.",
64 peer->p_hdr.info.pi_diamid,
65 peer->p_hdr.info.runtime.pir_orstate,
66 hdr->avp_value->u32 );
67 return EINVAL;
68 }
69 }
70 return 0;
71}
72
73/* Create and send a DWR */
74static int send_DWR(struct fd_peer * peer)
75{
76 struct msg * msg = NULL;
77
78 /* Create a new DWR instance */
79 CHECK_FCT( fd_msg_new ( fd_dict_cmd_DWR, MSGFL_ALLOC_ETEID, &msg ) );
80
81 /* Add the content of the message (only the origin) */
82 CHECK_FCT( fd_msg_add_origin ( msg, 1 ) );
83
84 /* Now send this message */
85 CHECK_FCT( fd_out_send(&msg, NULL, peer, 0) );
86
87 /* And mark the pending DW */
88 peer->p_flags.pf_dw_pending = 1;
89
90 return 0;
91}
92
93/* Handle an incoming message */
94int fd_p_dw_handle(struct msg ** msg, int req, struct fd_peer * peer)
95{
96 int reset_tmr = 0;
97
98 TRACE_ENTRY("%p %d %p", msg, req, peer);
99
100 /* Check the value of OSI for information */
101 CHECK_FCT( check_state_id(*msg, peer) );
102
103 if (req) {
104 /* If we receive a DWR, send back a DWA */
105 CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
106 CHECK_FCT( fd_msg_rescode_set( *msg, "DIAMETER_SUCCESS", NULL, NULL, 0 ) );
107 CHECK_FCT( fd_msg_add_origin ( *msg, 1 ) );
108 CHECK_FCT( fd_out_send( msg, peer->p_cnxctx, peer, 0) );
109
110 } else {
111 /* Discard the DWA */
112 CHECK_FCT_DO( fd_msg_free(*msg), /* continue */ );
113 *msg = NULL;
114
115 /* And clear the pending DW flag */
116 peer->p_flags.pf_dw_pending = 0;
117 }
118
119 /* Now update timeout */
120 if (req) {
121 /* Update timeout only if we did not already send a DWR ourselves */
122 reset_tmr = !peer->p_flags.pf_dw_pending;
123 } else {
124 /* Reset the timer */
125 reset_tmr = 1;
126 }
127 if (reset_tmr) {
128 fd_psm_next_timeout(peer, 1, peer->p_hdr.info.config.pic_twtimer ?: fd_g_config->cnf_timer_tw);
129 }
130
131 /* If we are in REOPEN state, increment the counter */
132 if (fd_peer_getstate(peer) == STATE_REOPEN) {
133 peer->p_flags.pf_reopen_cnt += 1;
134
135 if (peer->p_flags.pf_reopen_cnt) {
136 /* Send a new DWR */
137 CHECK_FCT( send_DWR(peer) );
138 } else {
139 /* Move to OPEN state */
140 CHECK_FCT( fd_psm_change_state(peer, STATE_OPEN) );
141 }
142 }
143
144 return 0;
145}
146
147/* Handle a timeout in the PSM (OPEN or REOPEN state only) */
148int fd_p_dw_timeout(struct fd_peer * peer)
149{
150 TRACE_ENTRY("%p", peer);
151
152 if (peer->p_flags.pf_dw_pending) {
153 /* We have sent a DWR and received no answer during TwTimer */
154 CHECK_FCT( fd_psm_change_state(peer, STATE_SUSPECT) );
155 fd_psm_next_timeout(peer, 0, 2 * (peer->p_hdr.info.config.pic_twtimer ?: fd_g_config->cnf_timer_tw) );
156 } else {
157 /* The timeout has expired, send a DWR */
158 CHECK_FCT( send_DWR(peer) );
159 fd_psm_next_timeout(peer, 0, peer->p_hdr.info.config.pic_twtimer ?: fd_g_config->cnf_timer_tw );
160 }
161
162 return 0;
163}
164
165/* Handle DW exchanges after the peer has come alive again */
166int fd_p_dw_reopen(struct fd_peer * peer)
167{
168 TRACE_ENTRY("%p", peer);
169
170 peer->p_flags.pf_reopen_cnt = 1;
171 peer->p_flags.pf_cnx_pb = 0;
172 CHECK_FCT( send_DWR(peer) );
173
174 return 0;
175}
176
177