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 | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 37 | #include "mgcp_ratectr.h" |
| 38 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 39 | #define RTP_PORT_DEFAULT_RANGE_START 16002 |
| 40 | #define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Handling of MGCP Endpoints and the MGCP Config |
| 44 | */ |
| 45 | struct mgcp_endpoint; |
| 46 | struct mgcp_config; |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 47 | struct mgcp_trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 48 | struct mgcp_rtp_end; |
| 49 | |
| 50 | #define MGCP_ENDP_CRCX 1 |
| 51 | #define MGCP_ENDP_DLCX 2 |
| 52 | #define MGCP_ENDP_MDCX 3 |
| 53 | |
| 54 | /* |
| 55 | * what to do with the msg? |
| 56 | * - continue as usual? |
| 57 | * - reject and send a failure code? |
| 58 | * - defer? do not send anything |
| 59 | */ |
| 60 | #define MGCP_POLICY_CONT 4 |
| 61 | #define MGCP_POLICY_REJECT 5 |
| 62 | #define MGCP_POLICY_DEFER 6 |
| 63 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 64 | typedef int (*mgcp_change)(struct mgcp_endpoint *endp, int state); |
| 65 | typedef int (*mgcp_policy)(struct mgcp_endpoint *endp, int state, const char *transaction_id); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 66 | typedef int (*mgcp_reset)(struct mgcp_trunk *cfg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 67 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
| 68 | |
| 69 | /** |
| 70 | * Return: |
| 71 | * < 0 in case no audio was processed |
| 72 | * >= 0 in case audio was processed. The remaining payload |
| 73 | * length will be returned. |
| 74 | */ |
| 75 | typedef int (*mgcp_processing)(struct mgcp_endpoint *endp, |
| 76 | struct mgcp_rtp_end *dst_end, |
| 77 | char *data, int *len, int buf_size); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 78 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 79 | struct mgcp_conn_rtp; |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 80 | |
| 81 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 82 | struct mgcp_conn_rtp *conn_dst, |
| 83 | struct mgcp_conn_rtp *conn_src); |
| 84 | |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 85 | struct mgcp_rtp_codec; |
| 86 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 87 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 88 | const struct mgcp_rtp_codec **codec, |
| 89 | const char **fmtp_extra, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 90 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * This holds information on how to allocate ports |
| 94 | */ |
| 95 | struct mgcp_port_range { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 96 | /* addr or NULL to fall-back to default */ |
| 97 | char *bind_addr; |
| 98 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 99 | /* dynamically allocated */ |
| 100 | int range_start; |
| 101 | int range_end; |
| 102 | int last_port; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 103 | |
| 104 | /* set to true to enable automatic probing |
| 105 | * of the local bind IP-Address, bind_addr |
| 106 | * (or its fall back) is used when automatic |
| 107 | * probing fails */ |
| 108 | bool bind_addr_probe; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 111 | /* 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] | 112 | * sent. The behaviour is controlled via the keepalive_interval member of the |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 113 | * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy- |
| 114 | * packet is sent at all and the timer that sends regular dummy packets |
| 115 | * is no longer scheduled. If the keepalive_interval is set to -1, only |
| 116 | * 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] | 117 | * 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] | 118 | * value is used as interval. See also mgcp_keepalive_timer_cb(), |
| 119 | * handle_modify_con(), and handle_create_con() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 120 | #define MGCP_KEEPALIVE_ONCE (-1) |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 121 | #define MGCP_KEEPALIVE_NEVER 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 122 | |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 123 | /* Global MCGP general rate counters */ |
| 124 | enum { |
| 125 | MGCP_GENERAL_RX_MSGS_TOTAL, |
| 126 | MGCP_GENERAL_RX_MSGS_RETRANSMITTED, |
| 127 | MGCP_GENERAL_RX_MSGS_HANDLED, |
| 128 | MGCP_GENERAL_RX_MSGS_UNHANDLED, |
| 129 | MGCP_GENERAL_RX_FAIL_MSG_PARSE, |
| 130 | MGCP_GENERAL_RX_FAIL_NO_ENDPOINT, |
| 131 | }; |
| 132 | |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 133 | /* Global MCGP CRCX related rate counters */ |
| 134 | enum { |
| 135 | MGCP_CRCX_SUCCESS, |
| 136 | MGCP_CRCX_FAIL_BAD_ACTION, |
| 137 | MGCP_CRCX_FAIL_UNHANDLED_PARAM, |
| 138 | MGCP_CRCX_FAIL_MISSING_CALLID, |
| 139 | MGCP_CRCX_FAIL_INVALID_MODE, |
| 140 | MGCP_CRCX_FAIL_LIMIT_EXCEEDED, |
| 141 | MGCP_CRCX_FAIL_UNKNOWN_CALLID, |
| 142 | MGCP_CRCX_FAIL_ALLOC_CONN, |
| 143 | MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC, |
| 144 | MGCP_CRCX_FAIL_START_RTP, |
| 145 | MGCP_CRCX_FAIL_REJECTED_BY_POLICY, |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 146 | MGCP_CRCX_FAIL_NO_OSMUX, |
| 147 | MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS, |
| 148 | MGCP_CRCX_FAIL_CODEC_NEGOTIATION, |
| 149 | MGCP_CRCX_FAIL_BIND_PORT, |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 152 | /* Global MCGP MDCX related rate counters */ |
| 153 | enum { |
| 154 | MGCP_MDCX_SUCCESS, |
| 155 | MGCP_MDCX_FAIL_WILDCARD, |
| 156 | MGCP_MDCX_FAIL_NO_CONN, |
| 157 | MGCP_MDCX_FAIL_INVALID_CALLID, |
| 158 | MGCP_MDCX_FAIL_INVALID_CONNID, |
| 159 | MGCP_MDCX_FAIL_UNHANDLED_PARAM, |
| 160 | MGCP_MDCX_FAIL_NO_CONNID, |
| 161 | MGCP_MDCX_FAIL_CONN_NOT_FOUND, |
| 162 | MGCP_MDCX_FAIL_INVALID_MODE, |
| 163 | MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS, |
| 164 | MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC, |
| 165 | MGCP_MDCX_FAIL_START_RTP, |
| 166 | MGCP_MDCX_FAIL_REJECTED_BY_POLICY, |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 167 | MGCP_MDCX_DEFERRED_BY_POLICY |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 170 | /* Global MCGP DLCX related rate counters */ |
| 171 | enum { |
| 172 | MGCP_DLCX_SUCCESS, |
| 173 | MGCP_DLCX_FAIL_WILDCARD, |
| 174 | MGCP_DLCX_FAIL_NO_CONN, |
| 175 | MGCP_DLCX_FAIL_INVALID_CALLID, |
| 176 | MGCP_DLCX_FAIL_INVALID_CONNID, |
| 177 | MGCP_DLCX_FAIL_UNHANDLED_PARAM, |
| 178 | MGCP_DLCX_FAIL_REJECTED_BY_POLICY, |
| 179 | MGCP_DLCX_DEFERRED_BY_POLICY, |
| 180 | }; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 181 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 182 | enum mgcp_role { |
| 183 | MGCP_BSC = 0, |
| 184 | MGCP_BSC_NAT, |
| 185 | }; |
| 186 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 187 | struct mgcp_config { |
| 188 | int source_port; |
| 189 | char *local_ip; |
| 190 | char *source_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 191 | char *call_agent_addr; |
| 192 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 193 | /* RTP processing */ |
| 194 | mgcp_processing rtp_processing_cb; |
| 195 | mgcp_processing_setup setup_rtp_processing_cb; |
| 196 | |
| 197 | mgcp_get_format get_net_downlink_format_cb; |
| 198 | |
| 199 | struct osmo_wqueue gw_fd; |
| 200 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 201 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 202 | int endp_dscp; |
| 203 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 204 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | |
| 206 | mgcp_change change_cb; |
| 207 | mgcp_policy policy_cb; |
| 208 | mgcp_reset reset_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 209 | mgcp_rqnt rqnt_cb; |
| 210 | void *data; |
| 211 | |
| 212 | uint32_t last_call_id; |
| 213 | |
| 214 | /* trunk handling */ |
Harald Welte | c39b1bf | 2020-03-08 11:29:39 +0100 | [diff] [blame] | 215 | |
| 216 | /* virtual trunk for RTP - RTP endpoints */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 217 | struct mgcp_trunk *virt_trunk; |
Harald Welte | c39b1bf | 2020-03-08 11:29:39 +0100 | [diff] [blame] | 218 | /* physical trunks with underlying E1 endpoints */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 219 | struct llist_head trunks; |
| 220 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 221 | enum mgcp_role role; |
| 222 | |
| 223 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 224 | int osmux; |
| 225 | /* addr to bind the server to */ |
| 226 | char *osmux_addr; |
| 227 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 228 | * the osmux socket is already initialized. |
| 229 | */ |
| 230 | int osmux_init; |
| 231 | /* osmux batch factor: from 1 to 4 maximum */ |
| 232 | int osmux_batch; |
| 233 | /* osmux batch size (in bytes) */ |
| 234 | int osmux_batch_size; |
| 235 | /* osmux port */ |
| 236 | uint16_t osmux_port; |
| 237 | /* Pad circuit with dummy messages until we see the first voice |
| 238 | * message. |
| 239 | */ |
| 240 | uint16_t osmux_dummy; |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 241 | /* domain name of the media gateway */ |
| 242 | char domain[255+1]; |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 243 | |
| 244 | /* time after which inactive connections (CIs) get closed */ |
| 245 | int conn_timeout; |
Harald Welte | 9852e22 | 2020-03-08 10:22:14 +0100 | [diff] [blame] | 246 | |
| 247 | /* osmocom CTRL interface */ |
| 248 | struct ctrl_handle *ctrl; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 249 | |
| 250 | /* global rate counters to measure the MGWs overall performance and |
| 251 | * health */ |
| 252 | struct mgcp_ratectr_global ratectr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | /* config management */ |
| 256 | struct mgcp_config *mgcp_config_alloc(void); |
| 257 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 258 | enum mgcp_role role); |
| 259 | int mgcp_vty_init(void); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 260 | void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | * format helper functions |
| 264 | */ |
| 265 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 266 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 267 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 268 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 269 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 270 | |
| 271 | |
| 272 | 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] | 273 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |