1 /*
2  * Portions Copyright (C) 2013 Crocodile RCS Ltd
3  *
4  * Based on "ser_stun.h". Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 /*!
24  * \file
25  * \brief STUN :: Configuration
26  * \ingroup stun
27  */
28 
29 #ifndef _kam_stun_h
30 #define _kam_stun_h
31 
32 #include "../../core/str.h"
33 #include "../../core/tcp_conn.h"
34 #include "../../core/ip_addr.h"
35 #include "../../core/stun.h"
36 
37 /* STUN message types supported by Kamailio */
38 #define BINDING_REQUEST		0x0001
39 #define BINDING_RESPONSE	0x0101
40 #define BINDING_ERROR_RESPONSE	0x0111
41 
42 /* common STUN attributes */
43 #define MAPPED_ADDRESS_ATTR	0x0001
44 #define USERNAME_ATTR		0x0006
45 #define MESSAGE_INTEGRITY_ATTR	0x0008
46 #define ERROR_CODE_ATTR		0x0009
47 #define UNKNOWN_ATTRIBUTES_ATTR	0x000A
48 
49 /* STUN attributes defined by rfc5389 */
50 #define REALM_ATTR		0x0014
51 #define NONCE_ATTR		0x0015
52 #define XOR_MAPPED_ADDRESS_ATTR	0x0020
53 #define FINGERPRINT_ATTR	0x8028
54 #define SOFTWARE_ATTR		0x8022
55 #define ALTERNATE_SERVER_ATTR	0x8023
56 
57 /* STUN attributes defined by rfc3489 */
58 #define RESPONSE_ADDRESS_ATTR	0x0002
59 #define CHANGE_REQUEST_ATTR	0x0003
60 #define SOURCE_ADDRESS_ATTR	0x0004
61 #define CHANGED_ADDRESS_ATTR	0x0005
62 #define REFLECTED_FROM_ATTR	0x000b
63 
64 /* STUN error codes supported by Kamailio */
65 #define RESPONSE_OK		200
66 #define TRY_ALTERNATE_ERR	300
67 #define BAD_REQUEST_ERR		400
68 #define UNAUTHORIZED_ERR	401
69 #define UNKNOWN_ATTRIBUTE_ERR	420
70 #define STALE_CREDENTIALS_ERR	430
71 #define INTEGRITY_CHECK_ERR	431
72 #define MISSING_USERNAME_ERR	432
73 #define USE_TLS_ERR		433
74 #define MISSING_REALM_ERR	434
75 #define MISSING_NONCE_ERR	435
76 #define UNKNOWN_USERNAME_ERR	436
77 #define STALE_NONCE_ERR		438
78 #define SERVER_ERROR_ERR	500
79 #define GLOBAL_FAILURE_ERR	600
80 
81 #define TRY_ALTERNATE_TXT      "Try Alternate"
82 #define BAD_REQUEST_TXT        "Bad Request"
83 #define UNAUTHORIZED_TXT       "Unauthorized"
84 #define UNKNOWN_ATTRIBUTE_TXT  "Unknown Attribute"
85 #define STALE_CREDENTIALS_TXT  "Stale Credentials"
86 #define INTEGRITY_CHECK_TXT    "Integrity Check Failure"
87 #define MISSING_USERNAME_TXT   "Missing Username"
88 #define USE_TLS_TXT            "Use TLS"
89 #define MISSING_REALM_TXT      "Missing Realm"
90 #define MISSING_NONCE_TXT      "Missing Nonce"
91 #define UNKNOWN_USERNAME_TXT   "Unknown Username"
92 #define STALE_NONCE_TXT        "Stale Nonce"
93 #define SERVER_ERROR_TXT       "Server Error"
94 #define GLOBAL_FAILURE_TXT     "Global Failure"
95 
96 /* other stuff */
97 #define MAGIC_COOKIE_2B		0x2112	/* because of XOR for port */
98 #define MANDATORY_ATTR		0x7fff
99 #define PAD4			4
100 #define PAD64			64
101 #define STUN_MSG_LEN		516
102 #define IPV4_LEN		4
103 #define IPV6_LEN		16
104 #define IPV4_FAMILY		0x0001
105 #define IPV6_FAMILY		0x0002
106 #define	FATAL_ERROR		-1
107 #define IP_ADDR			4
108 #define XOR			1
109 
110 #ifndef SHA_DIGEST_LENGTH
111 #define SHA_DIGEST_LENGTH	20
112 #endif
113 
114 /** padd len to a multiple of sz.
115  *  sz must be a power of the form 2^k (e.g. 2, 4, 8, 16 ...)
116  */
117 #define PADD_TO(len, sz)	(((len) + (sz)-1) & (~((sz) - 1)))
118 
119 #define PADDED_TO_FOUR(len)	PADD_TO(len, 4)
120 #define PADDED_TO_SIXTYFOUR(len) PADD_TO(len, 64)
121 
122 struct stun_ip_addr {
123 	USHORT_T family; /* 0x01: IPv4; 0x02: IPv6 */
124 	USHORT_T port;
125 	UINT_T ip[IP_ADDR];
126 };
127 
128 struct stun_buffer {
129 	str buf;
130 	USHORT_T empty;	/* number of free bytes in buf before it'll be necessary
131 			   to realloc the buf */
132 };
133 
134 struct stun_unknown_att {
135 	USHORT_T type;
136 	struct stun_unknown_att* next;
137 };
138 
139 struct stun_msg {
140 	struct stun_hdr hdr;
141 	struct stun_ip_addr ip_addr; /* XOR values for rfc3489bis, normal values
142 					for rfc3489 */
143 	struct stun_buffer msg;
144 	UCHAR_T old; /* true: the format of message is in accordance with
145 			rfc3489 */
146 };
147 
148 int process_stun_msg(char* buf, unsigned len, struct receive_info* ri);
149 
150 #endif  /* _kam_stun_h */
151