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> |
| 32 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 33 | #include <arpa/inet.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/in.h> |
| 37 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 38 | #include "mgcp_ratectr.h" |
| 39 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 40 | #define RTP_PORT_DEFAULT_RANGE_START 16002 |
| 41 | #define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 42 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 43 | /* |
| 44 | * Handling of MGCP Endpoints and the MGCP Config |
| 45 | */ |
| 46 | struct mgcp_endpoint; |
| 47 | struct mgcp_config; |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 48 | struct mgcp_trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 49 | struct mgcp_rtp_end; |
| 50 | |
| 51 | #define MGCP_ENDP_CRCX 1 |
| 52 | #define MGCP_ENDP_DLCX 2 |
| 53 | #define MGCP_ENDP_MDCX 3 |
| 54 | |
| 55 | /* |
| 56 | * what to do with the msg? |
| 57 | * - continue as usual? |
| 58 | * - reject and send a failure code? |
| 59 | * - defer? do not send anything |
| 60 | */ |
| 61 | #define MGCP_POLICY_CONT 4 |
| 62 | #define MGCP_POLICY_REJECT 5 |
| 63 | #define MGCP_POLICY_DEFER 6 |
| 64 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 65 | typedef int (*mgcp_reset)(struct mgcp_trunk *cfg); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 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 | |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 84 | struct mgcp_rtp_codec; |
| 85 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 86 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
Philipp Maier | 5812825 | 2019-03-06 11:28:18 +0100 | [diff] [blame] | 87 | const struct mgcp_rtp_codec **codec, |
| 88 | const char **fmtp_extra, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 89 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * This holds information on how to allocate ports |
| 93 | */ |
| 94 | struct mgcp_port_range { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 95 | /* addr or NULL to fall-back to default */ |
Pau Espin Pedrol | 8a2a1b2 | 2020-09-02 20:46:24 +0200 | [diff] [blame] | 96 | char *bind_addr_v4; |
| 97 | char *bind_addr_v6; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 123 | enum mgcp_role { |
| 124 | MGCP_BSC = 0, |
| 125 | MGCP_BSC_NAT, |
| 126 | }; |
| 127 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 128 | struct mgcp_config { |
| 129 | int source_port; |
| 130 | char *local_ip; |
| 131 | char *source_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 132 | char *call_agent_addr; |
| 133 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 134 | /* RTP processing */ |
| 135 | mgcp_processing rtp_processing_cb; |
| 136 | mgcp_processing_setup setup_rtp_processing_cb; |
| 137 | |
| 138 | mgcp_get_format get_net_downlink_format_cb; |
| 139 | |
| 140 | struct osmo_wqueue gw_fd; |
| 141 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 142 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 143 | int endp_dscp; |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 144 | int endp_priority; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 145 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 146 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 148 | mgcp_reset reset_cb; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 149 | mgcp_rqnt rqnt_cb; |
| 150 | void *data; |
| 151 | |
| 152 | uint32_t last_call_id; |
| 153 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 154 | /* list holding the trunks */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 155 | struct llist_head trunks; |
| 156 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 157 | enum mgcp_role role; |
| 158 | |
| 159 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 160 | int osmux; |
| 161 | /* addr to bind the server to */ |
| 162 | char *osmux_addr; |
| 163 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 164 | * the osmux socket is already initialized. |
| 165 | */ |
| 166 | int osmux_init; |
| 167 | /* osmux batch factor: from 1 to 4 maximum */ |
| 168 | int osmux_batch; |
| 169 | /* osmux batch size (in bytes) */ |
| 170 | int osmux_batch_size; |
| 171 | /* osmux port */ |
| 172 | uint16_t osmux_port; |
| 173 | /* Pad circuit with dummy messages until we see the first voice |
| 174 | * message. |
| 175 | */ |
| 176 | uint16_t osmux_dummy; |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 177 | /* domain name of the media gateway */ |
| 178 | char domain[255+1]; |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 179 | |
| 180 | /* time after which inactive connections (CIs) get closed */ |
| 181 | int conn_timeout; |
Harald Welte | 9852e22 | 2020-03-08 10:22:14 +0100 | [diff] [blame] | 182 | |
| 183 | /* osmocom CTRL interface */ |
| 184 | struct ctrl_handle *ctrl; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 185 | |
| 186 | /* global rate counters to measure the MGWs overall performance and |
| 187 | * health */ |
| 188 | struct mgcp_ratectr_global ratectr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | /* config management */ |
| 192 | struct mgcp_config *mgcp_config_alloc(void); |
| 193 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 194 | enum mgcp_role role); |
| 195 | int mgcp_vty_init(void); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 196 | void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * format helper functions |
| 200 | */ |
| 201 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 202 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 203 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 204 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 206 | |
| 207 | |
Harald Welte | 55a9229 | 2021-04-28 19:06:34 +0200 | [diff] [blame] | 208 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port, uint8_t dscp, |
| 209 | uint8_t prio); |
Philipp Maier | 776846a | 2021-05-20 14:15:46 +0200 | [diff] [blame] | 210 | int mgcp_udp_send(int fd, struct osmo_sockaddr *addr, int port, const char *buf, int len); |