Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2012 by On-Waves |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program 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 Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 23 | #pragma once |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/write_queue.h> |
| 27 | #include <osmocom/core/timer.h> |
| 28 | #include <osmocom/core/logging.h> |
| 29 | |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_common.h> |
| 31 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 32 | #include <arpa/inet.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | #define RTP_PORT_DEFAULT_RANGE_START 16002 |
| 38 | #define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 39 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 40 | /* |
| 41 | * Handling of MGCP Endpoints and the MGCP Config |
| 42 | */ |
| 43 | struct mgcp_endpoint; |
| 44 | struct mgcp_config; |
| 45 | struct mgcp_trunk_config; |
| 46 | struct mgcp_rtp_end; |
| 47 | |
| 48 | #define MGCP_ENDP_CRCX 1 |
| 49 | #define MGCP_ENDP_DLCX 2 |
| 50 | #define MGCP_ENDP_MDCX 3 |
| 51 | |
| 52 | /* |
| 53 | * what to do with the msg? |
| 54 | * - continue as usual? |
| 55 | * - reject and send a failure code? |
| 56 | * - defer? do not send anything |
| 57 | */ |
| 58 | #define MGCP_POLICY_CONT 4 |
| 59 | #define MGCP_POLICY_REJECT 5 |
| 60 | #define MGCP_POLICY_DEFER 6 |
| 61 | |
| 62 | typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint); |
| 63 | typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state); |
| 64 | typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id); |
| 65 | typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg); |
| 66 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
| 67 | |
| 68 | /** |
| 69 | * Return: |
| 70 | * < 0 in case no audio was processed |
| 71 | * >= 0 in case audio was processed. The remaining payload |
| 72 | * length will be returned. |
| 73 | */ |
| 74 | typedef int (*mgcp_processing)(struct mgcp_endpoint *endp, |
| 75 | struct mgcp_rtp_end *dst_end, |
| 76 | char *data, int *len, int buf_size); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 77 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 78 | struct mgcp_conn_rtp; |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 79 | |
| 80 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 81 | struct mgcp_conn_rtp *conn_dst, |
| 82 | struct mgcp_conn_rtp *conn_src); |
| 83 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 84 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
| 85 | int *payload_type, |
| 86 | const char**subtype_name, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 87 | const char**fmtp_extra, |
| 88 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * This holds information on how to allocate ports |
| 92 | */ |
| 93 | struct mgcp_port_range { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 94 | /* addr or NULL to fall-back to default */ |
| 95 | char *bind_addr; |
| 96 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 97 | /* dynamically allocated */ |
| 98 | int range_start; |
| 99 | int range_end; |
| 100 | int last_port; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 101 | |
| 102 | /* set to true to enable automatic probing |
| 103 | * of the local bind IP-Address, bind_addr |
| 104 | * (or its fall back) is used when automatic |
| 105 | * probing fails */ |
| 106 | bool bind_addr_probe; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 109 | /* There are up to three modes in which the keep-alive dummy packet can be |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 110 | * sent. The behaviour is controlled via the keepalive_interval member of the |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 111 | * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy- |
| 112 | * packet is sent at all and the timer that sends regular dummy packets |
| 113 | * is no longer scheduled. If the keepalive_interval is set to -1, only |
| 114 | * one dummy packet is sent when an CRCX or an MDCX is performed. No timer |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 115 | * is scheduled. For all vales greater 0, the timer is scheduled and the |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 116 | * value is used as interval. See also mgcp_keepalive_timer_cb(), |
| 117 | * handle_modify_con(), and handle_create_con() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 118 | #define MGCP_KEEPALIVE_ONCE (-1) |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 119 | #define MGCP_KEEPALIVE_NEVER 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 120 | |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 121 | /* Global MCGP CRCX related rate counters */ |
| 122 | enum { |
| 123 | MGCP_CRCX_SUCCESS, |
| 124 | MGCP_CRCX_FAIL_BAD_ACTION, |
| 125 | MGCP_CRCX_FAIL_UNHANDLED_PARAM, |
| 126 | MGCP_CRCX_FAIL_MISSING_CALLID, |
| 127 | MGCP_CRCX_FAIL_INVALID_MODE, |
| 128 | MGCP_CRCX_FAIL_LIMIT_EXCEEDED, |
| 129 | MGCP_CRCX_FAIL_UNKNOWN_CALLID, |
| 130 | MGCP_CRCX_FAIL_ALLOC_CONN, |
| 131 | MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC, |
| 132 | MGCP_CRCX_FAIL_START_RTP, |
| 133 | MGCP_CRCX_FAIL_REJECTED_BY_POLICY, |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 134 | MGCP_CRCX_FAIL_NO_OSMUX, |
| 135 | MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS, |
| 136 | MGCP_CRCX_FAIL_CODEC_NEGOTIATION, |
| 137 | MGCP_CRCX_FAIL_BIND_PORT, |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 140 | /* Global MCGP MDCX related rate counters */ |
| 141 | enum { |
| 142 | MGCP_MDCX_SUCCESS, |
| 143 | MGCP_MDCX_FAIL_WILDCARD, |
| 144 | MGCP_MDCX_FAIL_NO_CONN, |
| 145 | MGCP_MDCX_FAIL_INVALID_CALLID, |
| 146 | MGCP_MDCX_FAIL_INVALID_CONNID, |
| 147 | MGCP_MDCX_FAIL_UNHANDLED_PARAM, |
| 148 | MGCP_MDCX_FAIL_NO_CONNID, |
| 149 | MGCP_MDCX_FAIL_CONN_NOT_FOUND, |
| 150 | MGCP_MDCX_FAIL_INVALID_MODE, |
| 151 | MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS, |
| 152 | MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC, |
| 153 | MGCP_MDCX_FAIL_START_RTP, |
| 154 | MGCP_MDCX_FAIL_REJECTED_BY_POLICY, |
| 155 | MGCP_MDCX_FAIL_DEFERRED_BY_POLICY |
| 156 | }; |
| 157 | |
| 158 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 159 | struct mgcp_trunk_config { |
| 160 | struct llist_head entry; |
| 161 | |
| 162 | struct mgcp_config *cfg; |
| 163 | |
| 164 | int trunk_nr; |
| 165 | int trunk_type; |
| 166 | |
| 167 | char *audio_fmtp_extra; |
| 168 | char *audio_name; |
| 169 | int audio_payload; |
| 170 | int audio_send_ptime; |
| 171 | int audio_send_name; |
| 172 | int audio_loop; |
| 173 | |
| 174 | int no_audio_transcoding; |
| 175 | |
| 176 | int omit_rtcp; |
| 177 | int keepalive_interval; |
| 178 | |
| 179 | /* RTP patching */ |
| 180 | int force_constant_ssrc; /* 0: don't, 1: once */ |
| 181 | int force_aligned_timing; |
| 182 | |
| 183 | /* spec handling */ |
| 184 | int force_realloc; |
| 185 | |
| 186 | /* timer */ |
| 187 | struct osmo_timer_list keepalive_timer; |
| 188 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 189 | /* When set, incoming RTP packets are not filtered |
| 190 | * when ports and ip-address do not match (debug) */ |
| 191 | int rtp_accept_all; |
| 192 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 193 | unsigned int number_endpoints; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 194 | int vty_number_endpoints; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 195 | struct mgcp_endpoint *endpoints; |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 196 | |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 197 | /* Rate counter group which contains stats for processed CRCX commands. */ |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 198 | struct rate_ctr_group *mgcp_crcx_ctr_group; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 199 | /* Rate counter group which contains stats for processed MDCX commands. */ |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 200 | struct rate_ctr_group *mgcp_mdcx_ctr_group; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 201 | /* Rate counter group which aggregates stats of individual RTP connections. */ |
| 202 | struct rate_ctr_group *all_rtp_conn_stats; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | enum mgcp_role { |
| 206 | MGCP_BSC = 0, |
| 207 | MGCP_BSC_NAT, |
| 208 | }; |
| 209 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 210 | struct mgcp_config { |
| 211 | int source_port; |
| 212 | char *local_ip; |
| 213 | char *source_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 214 | char *call_agent_addr; |
| 215 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 216 | /* RTP processing */ |
| 217 | mgcp_processing rtp_processing_cb; |
| 218 | mgcp_processing_setup setup_rtp_processing_cb; |
| 219 | |
| 220 | mgcp_get_format get_net_downlink_format_cb; |
| 221 | |
| 222 | struct osmo_wqueue gw_fd; |
| 223 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 225 | int endp_dscp; |
| 226 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 227 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 228 | |
| 229 | mgcp_change change_cb; |
| 230 | mgcp_policy policy_cb; |
| 231 | mgcp_reset reset_cb; |
| 232 | mgcp_realloc realloc_cb; |
| 233 | mgcp_rqnt rqnt_cb; |
| 234 | void *data; |
| 235 | |
| 236 | uint32_t last_call_id; |
| 237 | |
| 238 | /* trunk handling */ |
| 239 | struct mgcp_trunk_config trunk; |
| 240 | struct llist_head trunks; |
| 241 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 242 | enum mgcp_role role; |
| 243 | |
| 244 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 245 | int osmux; |
| 246 | /* addr to bind the server to */ |
| 247 | char *osmux_addr; |
| 248 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 249 | * the osmux socket is already initialized. |
| 250 | */ |
| 251 | int osmux_init; |
| 252 | /* osmux batch factor: from 1 to 4 maximum */ |
| 253 | int osmux_batch; |
| 254 | /* osmux batch size (in bytes) */ |
| 255 | int osmux_batch_size; |
| 256 | /* osmux port */ |
| 257 | uint16_t osmux_port; |
| 258 | /* Pad circuit with dummy messages until we see the first voice |
| 259 | * message. |
| 260 | */ |
| 261 | uint16_t osmux_dummy; |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 262 | /* domain name of the media gateway */ |
| 263 | char domain[255+1]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | /* config management */ |
| 267 | struct mgcp_config *mgcp_config_alloc(void); |
| 268 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 269 | enum mgcp_role role); |
| 270 | int mgcp_vty_init(void); |
| 271 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 272 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval); |
| 273 | |
| 274 | /* |
| 275 | * format helper functions |
| 276 | */ |
| 277 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 278 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 279 | |
| 280 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint); |
| 281 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 282 | |
| 283 | |
| 284 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 285 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |