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); |
| 77 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 78 | struct mgcp_rtp_end *dst_end, |
| 79 | struct mgcp_rtp_end *src_end); |
| 80 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 81 | struct mgcp_conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 82 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
| 83 | int *payload_type, |
| 84 | const char**subtype_name, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 85 | const char**fmtp_extra, |
| 86 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * This holds information on how to allocate ports |
| 90 | */ |
| 91 | struct mgcp_port_range { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 92 | /* addr or NULL to fall-back to default */ |
| 93 | char *bind_addr; |
| 94 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 95 | /* dynamically allocated */ |
| 96 | int range_start; |
| 97 | int range_end; |
| 98 | int last_port; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 99 | |
| 100 | /* set to true to enable automatic probing |
| 101 | * of the local bind IP-Address, bind_addr |
| 102 | * (or its fall back) is used when automatic |
| 103 | * probing fails */ |
| 104 | bool bind_addr_probe; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 107 | /* 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] | 108 | * sent. The behaviour is controlled via the keepalive_interval member of the |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 109 | * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy- |
| 110 | * packet is sent at all and the timer that sends regular dummy packets |
| 111 | * is no longer scheduled. If the keepalive_interval is set to -1, only |
| 112 | * 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] | 113 | * 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] | 114 | * value is used as interval. See also mgcp_keepalive_timer_cb(), |
| 115 | * handle_modify_con(), and handle_create_con() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 116 | #define MGCP_KEEPALIVE_ONCE (-1) |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 117 | #define MGCP_KEEPALIVE_NEVER 0 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 118 | |
| 119 | struct mgcp_trunk_config { |
| 120 | struct llist_head entry; |
| 121 | |
| 122 | struct mgcp_config *cfg; |
| 123 | |
| 124 | int trunk_nr; |
| 125 | int trunk_type; |
| 126 | |
| 127 | char *audio_fmtp_extra; |
| 128 | char *audio_name; |
| 129 | int audio_payload; |
| 130 | int audio_send_ptime; |
| 131 | int audio_send_name; |
| 132 | int audio_loop; |
| 133 | |
| 134 | int no_audio_transcoding; |
| 135 | |
| 136 | int omit_rtcp; |
| 137 | int keepalive_interval; |
| 138 | |
| 139 | /* RTP patching */ |
| 140 | int force_constant_ssrc; /* 0: don't, 1: once */ |
| 141 | int force_aligned_timing; |
| 142 | |
| 143 | /* spec handling */ |
| 144 | int force_realloc; |
| 145 | |
| 146 | /* timer */ |
| 147 | struct osmo_timer_list keepalive_timer; |
| 148 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 149 | /* When set, incoming RTP packets are not filtered |
| 150 | * when ports and ip-address do not match (debug) */ |
| 151 | int rtp_accept_all; |
| 152 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 153 | unsigned int number_endpoints; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 154 | int vty_number_endpoints; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 155 | struct mgcp_endpoint *endpoints; |
| 156 | }; |
| 157 | |
| 158 | enum mgcp_role { |
| 159 | MGCP_BSC = 0, |
| 160 | MGCP_BSC_NAT, |
| 161 | }; |
| 162 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 163 | struct mgcp_config { |
| 164 | int source_port; |
| 165 | char *local_ip; |
| 166 | char *source_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 167 | char *call_agent_addr; |
| 168 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 169 | /* RTP processing */ |
| 170 | mgcp_processing rtp_processing_cb; |
| 171 | mgcp_processing_setup setup_rtp_processing_cb; |
| 172 | |
| 173 | mgcp_get_format get_net_downlink_format_cb; |
| 174 | |
| 175 | struct osmo_wqueue gw_fd; |
| 176 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 177 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 178 | int endp_dscp; |
| 179 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 180 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 181 | |
| 182 | mgcp_change change_cb; |
| 183 | mgcp_policy policy_cb; |
| 184 | mgcp_reset reset_cb; |
| 185 | mgcp_realloc realloc_cb; |
| 186 | mgcp_rqnt rqnt_cb; |
| 187 | void *data; |
| 188 | |
| 189 | uint32_t last_call_id; |
| 190 | |
| 191 | /* trunk handling */ |
| 192 | struct mgcp_trunk_config trunk; |
| 193 | struct llist_head trunks; |
| 194 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 195 | enum mgcp_role role; |
| 196 | |
| 197 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 198 | int osmux; |
| 199 | /* addr to bind the server to */ |
| 200 | char *osmux_addr; |
| 201 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 202 | * the osmux socket is already initialized. |
| 203 | */ |
| 204 | int osmux_init; |
| 205 | /* osmux batch factor: from 1 to 4 maximum */ |
| 206 | int osmux_batch; |
| 207 | /* osmux batch size (in bytes) */ |
| 208 | int osmux_batch_size; |
| 209 | /* osmux port */ |
| 210 | uint16_t osmux_port; |
| 211 | /* Pad circuit with dummy messages until we see the first voice |
| 212 | * message. |
| 213 | */ |
| 214 | uint16_t osmux_dummy; |
| 215 | }; |
| 216 | |
| 217 | /* config management */ |
| 218 | struct mgcp_config *mgcp_config_alloc(void); |
| 219 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 220 | enum mgcp_role role); |
| 221 | int mgcp_vty_init(void); |
| 222 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
| 223 | void mgcp_release_endp(struct mgcp_endpoint *endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval); |
| 225 | |
| 226 | /* |
| 227 | * format helper functions |
| 228 | */ |
| 229 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 230 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 231 | |
| 232 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint); |
| 233 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 234 | |
| 235 | |
| 236 | 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] | 237 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |