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> |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 26 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 27 | #include <osmocom/core/write_queue.h> |
| 28 | #include <osmocom/core/timer.h> |
| 29 | #include <osmocom/core/logging.h> |
| 30 | |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_common.h> |
Pau Espin Pedrol | 928a20b | 2022-09-23 15:38:24 +0200 | [diff] [blame] | 32 | #include <osmocom/mgcp/osmux.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 33 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 34 | #include <arpa/inet.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netinet/in.h> |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 38 | #include <pthread.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 39 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 40 | #include "mgcp_ratectr.h" |
| 41 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 42 | #define RTP_PORT_DEFAULT_RANGE_START 16002 |
| 43 | #define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 44 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 45 | /* |
| 46 | * Handling of MGCP Endpoints and the MGCP Config |
| 47 | */ |
| 48 | struct mgcp_endpoint; |
| 49 | struct mgcp_config; |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 50 | struct mgcp_trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 51 | struct mgcp_rtp_end; |
| 52 | |
| 53 | #define MGCP_ENDP_CRCX 1 |
| 54 | #define MGCP_ENDP_DLCX 2 |
| 55 | #define MGCP_ENDP_MDCX 3 |
| 56 | |
| 57 | /* |
| 58 | * what to do with the msg? |
| 59 | * - continue as usual? |
| 60 | * - reject and send a failure code? |
| 61 | * - defer? do not send anything |
| 62 | */ |
| 63 | #define MGCP_POLICY_CONT 4 |
| 64 | #define MGCP_POLICY_REJECT 5 |
| 65 | #define MGCP_POLICY_DEFER 6 |
| 66 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 67 | typedef int (*mgcp_reset)(struct mgcp_trunk *cfg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 68 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
| 69 | |
| 70 | /** |
| 71 | * Return: |
| 72 | * < 0 in case no audio was processed |
| 73 | * >= 0 in case audio was processed. The remaining payload |
| 74 | * length will be returned. |
| 75 | */ |
| 76 | typedef int (*mgcp_processing)(struct mgcp_endpoint *endp, |
| 77 | struct mgcp_rtp_end *dst_end, |
| 78 | char *data, int *len, int buf_size); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 79 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 80 | struct mgcp_conn_rtp; |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 81 | |
| 82 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 83 | struct mgcp_conn_rtp *conn_dst, |
| 84 | struct mgcp_conn_rtp *conn_src); |
| 85 | |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 86 | struct mgcp_rtp_codec; |
| 87 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 88 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 89 | const struct mgcp_rtp_codec **codec, |
| 90 | const char **fmtp_extra, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 91 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * This holds information on how to allocate ports |
| 95 | */ |
| 96 | struct mgcp_port_range { |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 97 | pthread_mutex_t lock; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 98 | /* addr or NULL to fall-back to default */ |
Eric | 2764bdb | 2021-08-23 22:11:47 +0200 | [diff] [blame] | 99 | char bind_addr_v4[INET6_ADDRSTRLEN]; |
| 100 | char bind_addr_v6[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 101 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 102 | /* dynamically allocated */ |
| 103 | int range_start; |
| 104 | int range_end; |
| 105 | int last_port; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 106 | |
| 107 | /* set to true to enable automatic probing |
| 108 | * of the local bind IP-Address, bind_addr |
| 109 | * (or its fall back) is used when automatic |
| 110 | * probing fails */ |
| 111 | bool bind_addr_probe; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 114 | /* 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] | 115 | * sent. The behaviour is controlled via the keepalive_interval member of the |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 116 | * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy- |
| 117 | * packet is sent at all and the timer that sends regular dummy packets |
| 118 | * is no longer scheduled. If the keepalive_interval is set to -1, only |
| 119 | * 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] | 120 | * 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] | 121 | * value is used as interval. See also mgcp_keepalive_timer_cb(), |
| 122 | * handle_modify_con(), and handle_create_con() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 123 | #define MGCP_KEEPALIVE_ONCE (-1) |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 124 | #define MGCP_KEEPALIVE_NEVER 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 125 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 126 | enum mgcp_role { |
| 127 | MGCP_BSC = 0, |
| 128 | MGCP_BSC_NAT, |
| 129 | }; |
| 130 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 131 | struct mgcp_config { |
| 132 | int source_port; |
Eric | 2764bdb | 2021-08-23 22:11:47 +0200 | [diff] [blame] | 133 | char local_ip[INET6_ADDRSTRLEN]; |
| 134 | char source_addr[INET6_ADDRSTRLEN]; |
| 135 | char call_agent_addr[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 136 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 137 | /* RTP processing */ |
| 138 | mgcp_processing rtp_processing_cb; |
| 139 | mgcp_processing_setup setup_rtp_processing_cb; |
| 140 | |
| 141 | mgcp_get_format get_net_downlink_format_cb; |
| 142 | |
| 143 | struct osmo_wqueue gw_fd; |
| 144 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 145 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 146 | int endp_dscp; |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 147 | int endp_priority; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 148 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 149 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 150 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 151 | mgcp_reset reset_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 152 | mgcp_rqnt rqnt_cb; |
| 153 | void *data; |
| 154 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 155 | /* list holding the trunks */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | struct llist_head trunks; |
| 157 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 158 | enum mgcp_role role; |
| 159 | |
Pau Espin Pedrol | 928a20b | 2022-09-23 15:38:24 +0200 | [diff] [blame] | 160 | /* Osmux usage policy: */ |
| 161 | enum osmux_usage osmux_use; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 162 | /* addr to bind the server to */ |
Eric | 2764bdb | 2021-08-23 22:11:47 +0200 | [diff] [blame] | 163 | char osmux_addr[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 164 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 165 | * the osmux socket is already initialized. |
| 166 | */ |
Pau Espin Pedrol | b7f52b4 | 2022-09-25 00:14:03 +0200 | [diff] [blame] | 167 | bool osmux_initialized; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 168 | /* osmux batch factor: from 1 to 4 maximum */ |
| 169 | int osmux_batch; |
| 170 | /* osmux batch size (in bytes) */ |
| 171 | int osmux_batch_size; |
| 172 | /* osmux port */ |
| 173 | uint16_t osmux_port; |
| 174 | /* Pad circuit with dummy messages until we see the first voice |
| 175 | * message. |
| 176 | */ |
| 177 | uint16_t osmux_dummy; |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 178 | /* domain name of the media gateway */ |
| 179 | char domain[255+1]; |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 180 | |
| 181 | /* time after which inactive connections (CIs) get closed */ |
| 182 | int conn_timeout; |
Harald Welte | 9852e22 | 2020-03-08 10:22:14 +0100 | [diff] [blame] | 183 | |
| 184 | /* osmocom CTRL interface */ |
| 185 | struct ctrl_handle *ctrl; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 186 | |
| 187 | /* global rate counters to measure the MGWs overall performance and |
| 188 | * health */ |
| 189 | struct mgcp_ratectr_global ratectr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /* config management */ |
| 193 | struct mgcp_config *mgcp_config_alloc(void); |
| 194 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 195 | enum mgcp_role role); |
| 196 | int mgcp_vty_init(void); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 197 | void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * format helper functions |
| 201 | */ |
| 202 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 203 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 204 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 205 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 206 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 207 | |
| 208 | |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 209 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp, |
| 210 | uint8_t prio); |
Philipp Maier | 776846a | 2021-05-20 14:15:46 +0200 | [diff] [blame] | 211 | int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len); |