-
Notifications
You must be signed in to change notification settings - Fork 908
/
diameter_msg.c
620 lines (546 loc) · 17 KB
/
diameter_msg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
* Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
* Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
*
* The initial version of this code was written by Dragos Vingarzan
* (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
* Fruanhofer Institute. It was and still is maintained in a separate
* branch of the original SER. We are therefore migrating it to
* Kamailio/SR and look forward to maintaining it from here on out.
* 2011/2012 Smile Communications, Pty. Ltd.
* ported/maintained/improved by
* Jason Penton (jason(dot)penton(at)smilecoms.com and
* Richard Good (richard(dot)good(at)smilecoms.com) as part of an
* effort to add full IMS support to Kamailio/SR using a new and
* improved architecture
*
* NB: Alot of this code was originally part of OpenIMSCore,
* FhG Fokus.
* Copyright (C) 2004-2006 FhG Fokus
* Thanks for great work! This is an effort to
* break apart the various CSCF functions into logically separate
* components. We hope this will drive wider use. We also feel
* that in this way the architecture is more complete and thereby easier
* to manage in the Kamailio/SR environment
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "diameter.h"
#include "diameter_api.h"
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include "utils.h"
#include "globals.h"
#include "config.h"
#include "peermanager.h"
#include "diameter_epc_code_cmd.h"
extern dp_config *config; /**< Configuration for this diameter peer */
/**
* This function encodes a AAAMessage to its network representation (encoder).
* From a AAAMessage structure, a buffer to be send is built.
* @param msg - the message to encode
* @returns 1 on success, -1 on error
* \note This function is taken from DISC http://developer.berlios.de/projects/disc/
*/
AAAReturnCode AAABuildMsgBuffer( AAAMessage *msg )
{
unsigned char *p;
AAA_AVP *avp;
/* first let's comput the length of the buffer */
msg->buf.len = AAA_MSG_HDR_SIZE; /* AAA message header size */
/* count and add the avps */
for(avp=msg->avpList.head;avp;avp=avp->next) {
msg->buf.len += AVP_HDR_SIZE(avp->flags)+ to_32x_len( avp->data.len );
}
LM_DBG("AAABuildMsgBuffer(): len=%d\n",msg->buf.len);
/* allocate some memory */
msg->buf.s = (char*)shm_malloc( msg->buf.len );
if (!msg->buf.s) {
LM_ERR("AAABuildMsgBuffer: no more free memory!\n");
goto error;
}
memset(msg->buf.s, 0, msg->buf.len);
/* fill in the buffer */
p = (unsigned char*)msg->buf.s;
/* DIAMETER HEADER */
/* message length */
((unsigned int*)p)[0] =htonl(msg->buf.len);
/* Diameter Version */
*p = 1;
p += VER_SIZE + MESSAGE_LENGTH_SIZE;
/* command code */
((unsigned int*)p)[0] = htonl(msg->commandCode);
/* flags */
*p = (unsigned char)msg->flags;
p += FLAGS_SIZE + COMMAND_CODE_SIZE;
/* application-ID */
((unsigned int*)p)[0] = htonl(msg->applicationId);
p += APPLICATION_ID_SIZE;
/* hop by hop id */
((unsigned int*)p)[0] = htonl(msg->hopbyhopId);
p += HOP_BY_HOP_IDENTIFIER_SIZE;
/* end to end id */
((unsigned int*)p)[0] = htonl(msg->endtoendId);
p += END_TO_END_IDENTIFIER_SIZE;
/* AVPS */
for(avp=msg->avpList.head;avp;avp=avp->next) {
/* AVP HEADER */
/* avp code */
set_4bytes(p,avp->code);
p +=4;
/* flags */
(*p++) = (unsigned char)avp->flags;
/* avp length */
set_3bytes(p, (AVP_HDR_SIZE(avp->flags)+avp->data.len) );
p += 3;
/* vendor id */
if ((avp->flags&0x80)!=0) {
set_4bytes(p,avp->vendorId);
p +=4;
}
/* data */
memcpy( p, avp->data.s, avp->data.len);
p += to_32x_len( avp->data.len );
}
if ((char*)p-msg->buf.s!=msg->buf.len) {
LM_ERR("BUG: build_buf_from_msg: mismatch between len and buf!\n");
shm_free( msg->buf.s );
msg->buf.s = 0;
msg->buf.len = 0;
goto error;
}
return 1;
error:
return -1;
}
/**
* Allocates a new AAAMessage.
* @param commandCode - the command code for this message
* @param applicationId - application id to be set
* @param sessionId - session id to be set
* @param request - if you want to create a response, put the request here. If you want a
* request, call with NULL
* @returns the AAAMessage* or NULL on error
* \note This function is taken from DISC http://developer.berlios.de/projects/disc/
*/
AAAMessage *AAANewMessage(
AAACommandCode commandCode,
AAAApplicationId applicationId,
AAASession *session,
AAAMessage *request)
{
AAAMessage *msg;
AAA_AVP *avp;
AAA_AVP *avp_t;
str *sessionId=0;
#if 0
unsigned int code;
#endif
// str dest_host={"?",1};
str dest_realm={"?",1};
msg = 0;
if (!session||!session->id.s) {
if (request){
/* copy old session id from AVP */
if (request->sessionId)
sessionId = &(request->sessionId->data);
}else{
if (commandCode!=Code_DW)
LM_DBG("AAANewMessage: param session received null and it's a request!!\n");
}
}else{
sessionId = &(session->id);
}
/* allocated a new AAAMessage structure and set it to 0 */
msg = (AAAMessage*)shm_malloc(sizeof(AAAMessage));
if (!msg) {
LM_ERR("AAANewMessage: no more free memory!!\n");
goto error;
}
memset(msg,0,sizeof(AAAMessage));
/* command code */
msg->commandCode = commandCode;
/* application ID */
msg->applicationId = applicationId;
/*add session ID */
if (sessionId){
avp = AAACreateAVP( 263, 0, 0, sessionId->s, sessionId->len,
AVP_DUPLICATE_DATA);
if ( !avp || AAAAddAVPToMessage(msg,avp,0)!=AAA_ERR_SUCCESS) {
LM_ERR("AAANewMessage: cannot create/add Session-Id avp\n");
if (avp) AAAFreeAVP( &avp );
goto error;
}
msg->sessionId = avp;
}
/* add origin host AVP */
/* changed by cristian to comply with rfc3588:
* 6.3. Origin-Host AVP
*
* The Origin-Host AVP (AVP Code 264) is of type
* DiameterIdentity... */
avp = AAACreateAVP( 264, 0, 0, config->fqdn.s, config->fqdn.len,
AVP_DUPLICATE_DATA);
if (!avp||AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
LM_ERR("AAANewMessage: cannot create/add Origin-Host avp\n");
if (avp) AAAFreeAVP( &avp );
goto error;
}
msg->orig_host = avp;
/* add origin realm AVP */
avp = AAACreateAVP( 296, 0, 0, config->realm.s, config->realm.len,
AVP_DUPLICATE_DATA);
if (!avp||AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
LM_ERR("AAANewMessage: cannot create/add Origin-Realm avp\n");
if (avp) AAAFreeAVP( &avp );
goto error;
}
msg->orig_realm = avp;
if (!request) {
/* it's a new request -> set the flag */
msg->flags = 0x80;
} else {
/* link the incoming peer to the answer */
msg->in_peer = request->in_peer;
/* set the P flag as in request */
msg->flags |= request->flags&0x40;
/**/
msg->endtoendId = request->endtoendId;
msg->hopbyhopId = request->hopbyhopId;
//TODO: aon:move this information in the AAASession structure, do not add these fields for
if (msg->commandCode==Code_CE||msg->commandCode==Code_DP||msg->commandCode==Code_DW ||
msg->commandCode==Diameter_CCR || msg->commandCode==Diameter_RAR){
// Don't add Destination Host/Realm because some stacks are way to picky and will just refuse it
}else{
/* Mirror the old originhost/realm to destinationhost/realm*/
//avp = AAAFindMatchingAVP(request,0,AVP_Origin_Host,0,0);
//if (avp) dest_host = avp->data;
/* add destination host and destination realm */
//avp = AAACreateAVP(AVP_Destination_Host,AAA_AVP_FLAG_MANDATORY,0,
// dest_host.s,dest_host.len,AVP_DUPLICATE_DATA);
//if (!avp) {
// LM_ERR("ERR:AAANewMessage: Failed creating Destination Host avp\n");
// goto error;
// }
// if (AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
// LM_ERR("ERR:AAANewMessage: Failed adding Destination Host avp to message\n");
// AAAFreeAVP(&avp);
// goto error;
// }
avp = AAAFindMatchingAVP(request,0,AVP_Origin_Realm,0,0);
if (avp) dest_realm = avp->data;
avp = AAACreateAVP(AVP_Destination_Realm,AAA_AVP_FLAG_MANDATORY,0,
dest_realm.s,dest_realm.len,AVP_DUPLICATE_DATA);
if (!avp) {
LM_ERR("ERR:AAANewMessage: Failed creating Destination Realm avp\n");
goto error;
}
if (AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
LM_ERR("ERR:AAANewMessage: Failed adding Destination Realm avp to message\n");
AAAFreeAVP(&avp);
goto error;
}
}
msg->res_code=0;
/* mirror all the proxy-info avp in the same order */
avp_t = request->avpList.head;
while ( (avp_t=AAAFindMatchingAVP
(request,avp_t,284,0,AAA_FORWARD_SEARCH))!=0 ) {
if ( (avp=AAACloneAVP(avp_t,1))==0 || AAAAddAVPToMessage( msg, avp,
msg->avpList.tail)!=AAA_ERR_SUCCESS )
goto error;
}
}
return msg;
error:
LM_ERR("AAANewMessage: failed to create a new AAA message!\n");
AAAFreeMessage(&msg);
return 0;
}
/**
* Create a Diameter Request.
* @param app_id - application id to be set
* @param command_code - the command code for this message
* @param flags - flags to be set
* @param sessId - session id to be set
* @returns the AAAMessage* or NULL on error
*/
AAAMessage *AAACreateRequest(AAAApplicationId app_id,
AAACommandCode command_code,
AAAMsgFlag flags,
AAASession *session)
{
AAAMessage *msg;
AAA_AVP *avp;
msg = AAANewMessage(command_code,app_id,session,0);
if (!msg) return 0;
msg->hopbyhopId = next_hopbyhop();
msg->endtoendId = next_endtoend();
msg->flags |= flags;
if(session){
/* add destination host and destination realm */
/*if(session->dest_host.s){//TODO: check spec about removing this across the board (jason)
avp = AAACreateAVP(AVP_Destination_Host,AAA_AVP_FLAG_MANDATORY,0,
session->dest_host.s,session->dest_host.len,AVP_DUPLICATE_DATA);
if (!avp) {
LM_ERR("ERR:AAACreateRequest: Failed creating Destination Host avp\n");
goto error;
}
if (AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
LM_ERR("ERR:AAACreateRequest: Failed adding Destination Host avp to message\n");
AAAFreeAVP(&avp);
goto error;
}
}*/
if(session->dest_realm.s){
avp = AAACreateAVP(AVP_Destination_Realm,AAA_AVP_FLAG_MANDATORY,0,
session->dest_realm.s,session->dest_realm.len,AVP_DUPLICATE_DATA);
if (!avp) {
LM_ERR("ERR:AAACreateRequest: Failed creating Destination Realm avp\n");
goto error;
}
if (AAAAddAVPToMessage(msg,avp,msg->avpList.tail)!=AAA_ERR_SUCCESS) {
LM_ERR("ERR:AAACreateRequest: Failed adding Destination Realm avp to message\n");
AAAFreeAVP(&avp);
goto error;
}
}
}
return msg;
error:
AAAFreeMessage(&msg);
return NULL;
}
/**
* Create a Diameter Response to a given Request.
* @param request - the request that this response is for
* @returns the AAAMessage* or NULL on error
*/
AAAMessage *AAACreateResponse(AAAMessage *request)
{
AAAMessage *msg;
msg = AAANewMessage(request->commandCode,request->applicationId,0,request);
return msg;
}
/**
* Frees a AVP List and all the members
* @param avpList - list to be freed
* @returns AAA_ERR_SUCCESS
*/
AAAReturnCode AAAFreeAVPList(AAA_AVP_LIST *avpList)
{
AAA_AVP *avp_t;
AAA_AVP *avp;
/* free the avp list */
avp = avpList->head;
while (avp) {
avp_t = avp;
avp = avp->next;
/*free the avp*/
AAAFreeAVP(&avp_t);
}
avpList->head = 0;
avpList->tail = 0;
return AAA_ERR_SUCCESS;
}
/**
* Frees completely a message allocated through AAANewMessage()
* @param msg - pointer to the pointer containing the message.
* @returns AAA_ERR_SUCCESS
*/
AAAReturnCode AAAFreeMessage(AAAMessage **msg)
{
LM_DBG("AAAFreeMessage: Freeing message (%p) %d\n",*msg,(*msg)->commandCode);
/* param check */
if (!msg || !(*msg))
goto done;
/* free the avp list */
AAAFreeAVPList(&((*msg)->avpList));
/* free the buffer (if any) */
if ( (*msg)->buf.s )
shm_free( (*msg)->buf.s );
/* free the AAA msg */
shm_free(*msg);
*msg = 0;
done:
return AAA_ERR_SUCCESS;
}
/**
* Sets the proper result_code into the Result-Code AVP; ths avp must already
* exists into the reply messge.
* @param message - the message to set the Result-Code to
* @param resultCode - code to set as result
* \note This function is taken from DISC http://developer.berlios.de/projects/disc/
*/
AAAReturnCode AAASetMessageResultCode(
AAAMessage *message,
AAAResultCode resultCode)
{
if ( !is_req(message) && message->res_code) {
*((unsigned int*)(message->res_code->data.s)) = htonl(resultCode);
return AAA_ERR_SUCCESS;
}
return AAA_ERR_FAILURE;
}
/**
* This function convert message from the network format to the AAAMessage structure (decoder).
* @param source - the source char buffer
* @param sourceLen - the length of the input buffer
* @param attach_buf - whether to attach the input buffer to the message
* @returns the AAAMessage* or NULL on error
* \note This function is taken from DISC http://developer.berlios.de/projects/disc/
*/
AAAMessage* AAATranslateMessage( unsigned char* source, unsigned int sourceLen,
int attach_buf)
{
unsigned char *ptr;
AAAMessage *msg;
unsigned char version;
unsigned int msg_len;
AAA_AVP *avp;
unsigned int avp_code;
unsigned char avp_flags;
unsigned int avp_len;
unsigned int avp_vendorID;
unsigned int avp_data_len;
/* check the params */
if( !source || !sourceLen || sourceLen<AAA_MSG_HDR_SIZE) {
LM_ERR("AAATranslateMessage: invalid buffered received!\n");
goto error;
}
/* inits */
msg = 0;
avp = 0;
ptr = source;
/* alloc a new message structure */
msg = (AAAMessage*)shm_malloc(sizeof(AAAMessage));
if (!msg) {
LM_ERR("AAATranslateMessage: no more free memory!!\n");
goto error;
}
memset(msg,0,sizeof(AAAMessage));
/* get the version */
version = (unsigned char)*ptr;
ptr += VER_SIZE;
if (version!=1) {
LM_ERR("AAATranslateMessage: invalid version [%d]in "
"AAA msg\n",version);
goto error;
}
/* message length */
msg_len = get_3bytes( ptr );
ptr += MESSAGE_LENGTH_SIZE;
if (msg_len>sourceLen) {
LM_ERR("AAATranslateMessage: AAA message len [%d] bigger then"
" buffer len [%d]\n",msg_len,sourceLen);
goto error;
}
/* command flags */
msg->flags = *ptr;
ptr += FLAGS_SIZE;
/* command code */
msg->commandCode = get_3bytes( ptr );
ptr += COMMAND_CODE_SIZE;
/* application-Id */
msg->applicationId = get_4bytes( ptr );
ptr += APPLICATION_ID_SIZE;
/* Hop-by-Hop-Id */
msg->hopbyhopId = ntohl(*((unsigned int*)ptr));
ptr += HOP_BY_HOP_IDENTIFIER_SIZE;
/* End-to-End-Id */
msg->endtoendId = ntohl(*((unsigned int*)ptr));
ptr += END_TO_END_IDENTIFIER_SIZE;
/* start decoding the AVPS */
while (ptr < source+msg_len) {
if (ptr+AVP_HDR_SIZE(0x80)>source+msg_len){
LM_ERR("AAATranslateMessage: source buffer to short!! "
"Cannot read the whole AVP header!\n");
goto error;
}
/* avp code */
avp_code = get_4bytes( ptr );
ptr += AVP_CODE_SIZE;
/* avp flags */
avp_flags = (unsigned char)*ptr;
ptr += AVP_FLAGS_SIZE;
/* avp length */
avp_len = get_3bytes( ptr );
ptr += AVP_LENGTH_SIZE;
if (avp_len<1) {
LM_ERR("AAATranslateMessage: invalid AVP len [%d]\n",
avp_len);
goto error;
}
/* avp vendor-ID */
avp_vendorID = 0;
if (avp_flags&AAA_AVP_FLAG_VENDOR_SPECIFIC) {
avp_vendorID = get_4bytes( ptr );
ptr += AVP_VENDOR_ID_SIZE;
}
/* data length */
avp_data_len = avp_len-AVP_HDR_SIZE(avp_flags);
/*check the data length */
if ( source+msg_len<ptr+avp_data_len) {
LM_ERR("AAATranslateMessage: source buffer to short!! "
"Cannot read a whole data for AVP!\n");
goto error;
}
/* create the AVP */
avp = AAACreateAVP( avp_code, avp_flags, avp_vendorID, (char*) ptr,
avp_data_len, AVP_DONT_FREE_DATA);
if (!avp)
goto error;
/* link the avp into aaa message to the end */
AAAAddAVPToMessage( msg, avp, msg->avpList.tail);
ptr += to_32x_len( avp_data_len );
}
/* link the buffer to the message */
if (attach_buf) {
msg->buf.s = (char*) source;
msg->buf.len = msg_len;
}
msg->sessionId = AAAFindMatchingAVP(msg,0,AVP_Session_Id,0,0);
//AAAPrintMessage( msg );
return msg;
error:
LM_ERR("AAATranslateMessage: message conversion droped!!\n");
AAAFreeMessage(&msg);
return 0;
}
/**
* print as debug all info contained by an aaa message + AVPs
* @param msg - the AAAMessage to print
* \note This function is taken from DISC http://developer.berlios.de/projects/disc/
*/
void AAAPrintMessage( AAAMessage *msg)
{
char buf[1024];
AAA_AVP *avp;
/* print msg info */
LM_DBG("AAA_MESSAGE - %p\n",msg);
LM_DBG("\tCode = %u\n",msg->commandCode);
LM_DBG("\tFlags = %x\n",msg->flags);
/*print the AVPs */
avp = msg->avpList.head;
while (avp) {
AAAConvertAVPToString(avp,buf,1024);
LM_DBG("\n%s\n",buf);
avp=avp->next;
}
}