| 1 | /********************************************************************************************************* |
|---|
| 2 | * Software License Agreement (BSD License) * |
|---|
| 3 | * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
|---|
| 4 | * * |
|---|
| 5 | * Copyright (c) 2010, 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 "fD.h" |
|---|
| 37 | |
|---|
| 38 | /* This file contains code to handle Disconnect Peer messages (DPR and DPA) */ |
|---|
| 39 | |
|---|
| 40 | /* Handle a received message */ |
|---|
| 41 | int fd_p_dp_handle(struct msg ** msg, int req, struct fd_peer * peer) |
|---|
| 42 | { |
|---|
| 43 | TRACE_ENTRY("%p %d %p", msg, req, peer); |
|---|
| 44 | |
|---|
| 45 | if (req) { |
|---|
| 46 | /* We received a DPR, save the Disconnect-Cause and terminate the connection */ |
|---|
| 47 | struct avp * dc; |
|---|
| 48 | int delay = peer->p_hdr.info.config.pic_tctimer ?: fd_g_config->cnf_timer_tc; |
|---|
| 49 | |
|---|
| 50 | CHECK_FCT( fd_msg_search_avp ( *msg, fd_dict_avp_DC, &dc )); |
|---|
| 51 | if (dc) { |
|---|
| 52 | /* Check the value is consistent with the saved one */ |
|---|
| 53 | struct avp_hdr * hdr; |
|---|
| 54 | CHECK_FCT( fd_msg_avp_hdr( dc, &hdr ) ); |
|---|
| 55 | if (hdr->avp_value == NULL) { |
|---|
| 56 | /* This is a sanity check */ |
|---|
| 57 | TRACE_DEBUG(NONE, "BUG: Unset value in Disconnect-Cause in DPR"); |
|---|
| 58 | fd_msg_dump_one(NONE, dc); |
|---|
| 59 | ASSERT(0); /* To check if this really happens, and understand why... */ |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | peer->p_hdr.info.runtime.pir_lastDC = hdr->avp_value->u32; |
|---|
| 63 | |
|---|
| 64 | switch (hdr->avp_value->u32) { |
|---|
| 65 | case ACV_DC_REBOOTING: |
|---|
| 66 | default: |
|---|
| 67 | /* We use TcTimer to attempt reconnection */ |
|---|
| 68 | break; |
|---|
| 69 | case ACV_DC_BUSY: |
|---|
| 70 | /* No need to hammer the overloaded peer */ |
|---|
| 71 | delay *= 10; |
|---|
| 72 | break; |
|---|
| 73 | case ACV_DC_NOT_FRIEND: |
|---|
| 74 | /* He does not want to speak to us... let's retry a lot later maybe */ |
|---|
| 75 | delay *= 200; |
|---|
| 76 | break; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | if (TRACE_BOOL(INFO)) { |
|---|
| 80 | if (dc) { |
|---|
| 81 | struct dict_object * dictobj = NULL; |
|---|
| 82 | struct dict_enumval_request er; |
|---|
| 83 | memset(&er, 0, sizeof(er)); |
|---|
| 84 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, fd_dict_avp_DC, &er.type_obj, ENOENT ) ); |
|---|
| 85 | er.search.enum_value.u32 = peer->p_hdr.info.runtime.pir_lastDC; |
|---|
| 86 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &er, &dictobj, 0 ) ); |
|---|
| 87 | if (dictobj) { |
|---|
| 88 | CHECK_FCT( fd_dict_getval( dictobj, &er.search ) ); |
|---|
| 89 | TRACE_DEBUG(INFO, "Peer '%s' sent a DPR with cause: %s\n", peer->p_hdr.info.pi_diamid, er.search.enum_name); |
|---|
| 90 | } else { |
|---|
| 91 | TRACE_DEBUG(INFO, "Peer '%s' sent a DPR with unknown cause: %u\n", peer->p_hdr.info.pi_diamid, peer->p_hdr.info.runtime.pir_lastDC); |
|---|
| 92 | } |
|---|
| 93 | } else { |
|---|
| 94 | TRACE_DEBUG(INFO, "Peer '%s' sent a DPR without Disconnect-Cause AVP\n", peer->p_hdr.info.pi_diamid); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /* Now reply with a DPA */ |
|---|
| 99 | CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) ); |
|---|
| 100 | CHECK_FCT( fd_msg_rescode_set( *msg, "DIAMETER_SUCCESS", NULL, NULL, 1 ) ); |
|---|
| 101 | |
|---|
| 102 | /* Move to CLOSING state to failover outgoing messages (and avoid failing the DPA...) */ |
|---|
| 103 | CHECK_FCT( fd_psm_change_state(peer, STATE_CLOSING) ); |
|---|
| 104 | |
|---|
| 105 | /* Now send the DPA */ |
|---|
| 106 | CHECK_FCT( fd_out_send( msg, NULL, peer, FD_CNX_ORDERED) ); |
|---|
| 107 | |
|---|
| 108 | /* Move to CLOSED state */ |
|---|
| 109 | fd_psm_cleanup(peer, 0); |
|---|
| 110 | |
|---|
| 111 | /* Reset the timer for next connection attempt -- we'll retry sooner or later depending on the disconnection cause */ |
|---|
| 112 | fd_psm_next_timeout(peer, 1, delay); |
|---|
| 113 | |
|---|
| 114 | } else { |
|---|
| 115 | /* We received a DPA */ |
|---|
| 116 | fd_cpu_flush_cache(); |
|---|
| 117 | if (peer->p_hdr.info.runtime.pir_state != STATE_CLOSING) { |
|---|
| 118 | TRACE_DEBUG(INFO, "Ignoring DPA received in state %s", STATE_STR(peer->p_hdr.info.runtime.pir_state)); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /* In theory, we should control the Result-Code AVP. But since we will not go back to OPEN state here anyway, let's skip it */ |
|---|
| 122 | CHECK_FCT_DO( fd_msg_free( *msg ), /* continue */ ); |
|---|
| 123 | *msg = NULL; |
|---|
| 124 | |
|---|
| 125 | /* The calling function handles cleaning the PSM and terminating the peer since we return in CLOSING state */ |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | return 0; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /* Start disconnection of a peer: send DPR */ |
|---|
| 132 | int fd_p_dp_initiate(struct fd_peer * peer, char * reason) |
|---|
| 133 | { |
|---|
| 134 | struct msg * msg = NULL; |
|---|
| 135 | struct dict_object * dictobj = NULL; |
|---|
| 136 | struct avp * avp = NULL; |
|---|
| 137 | struct dict_enumval_request er; |
|---|
| 138 | union avp_value val; |
|---|
| 139 | |
|---|
| 140 | TRACE_ENTRY("%p %p", peer, reason); |
|---|
| 141 | |
|---|
| 142 | /* Create a new DWR instance */ |
|---|
| 143 | CHECK_FCT( fd_msg_new ( fd_dict_cmd_DPR, MSGFL_ALLOC_ETEID, &msg ) ); |
|---|
| 144 | |
|---|
| 145 | /* Add the Origin information */ |
|---|
| 146 | CHECK_FCT( fd_msg_add_origin ( msg, 0 ) ); |
|---|
| 147 | |
|---|
| 148 | /* Add the Disconnect-Cause */ |
|---|
| 149 | CHECK_FCT( fd_msg_avp_new ( fd_dict_avp_DC, 0, &avp ) ); |
|---|
| 150 | |
|---|
| 151 | /* Search the value in the dictionary */ |
|---|
| 152 | memset(&er, 0, sizeof(er)); |
|---|
| 153 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, fd_dict_avp_DC, &er.type_obj, ENOENT ) ); |
|---|
| 154 | er.search.enum_name = reason ?: "REBOOTING"; |
|---|
| 155 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &er, &dictobj, ENOENT ) ); |
|---|
| 156 | CHECK_FCT( fd_dict_getval( dictobj, &er.search ) ); |
|---|
| 157 | |
|---|
| 158 | /* Set the value in the AVP */ |
|---|
| 159 | val.u32 = er.search.enum_value.u32; |
|---|
| 160 | CHECK_FCT( fd_msg_avp_setvalue( avp, &val ) ); |
|---|
| 161 | CHECK_FCT( fd_msg_avp_add( msg, MSG_BRW_LAST_CHILD, avp ) ); |
|---|
| 162 | |
|---|
| 163 | /* Save the value also in the peer */ |
|---|
| 164 | peer->p_hdr.info.runtime.pir_lastDC = val.u32; |
|---|
| 165 | |
|---|
| 166 | /* Update the peer state and timer */ |
|---|
| 167 | CHECK_FCT( fd_psm_change_state(peer, STATE_CLOSING) ); |
|---|
| 168 | fd_psm_next_timeout(peer, 0, DPR_TIMEOUT); |
|---|
| 169 | |
|---|
| 170 | /* Now send the DPR message */ |
|---|
| 171 | CHECK_FCT_DO( fd_out_send(&msg, NULL, peer, FD_CNX_ORDERED), /* ignore since we are on timeout anyway */ ); |
|---|
| 172 | |
|---|
| 173 | return 0; |
|---|
| 174 | } |
|---|