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> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 29 | #include <osmocom/mgcp/mgcp_ep.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 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 | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 38 | #define RTP_PORT_DEFAULT_RANGE_START 16002 |
| 39 | #define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64 |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Calculate the RTP audio port for the given multiplex |
| 43 | * and the direction. This allows a semi static endpoint |
| 44 | * to port calculation removing the need for the BSC |
| 45 | * and the MediaGateway to communicate. |
| 46 | * |
| 47 | * Port usage explained: |
| 48 | * base + (multiplex * 2) + 0 == local port to wait for network packets |
| 49 | * base + (multiplex * 2) + 1 == local port for rtcp |
| 50 | * |
| 51 | * The above port will receive packets from the BTS that need |
| 52 | * to be patched and forwarded to the network. |
| 53 | * The above port will receive packets from the network that |
| 54 | * need to be patched and forwarded to the BTS. |
| 55 | * |
| 56 | * We assume to have a static BTS IP address so we can differentiate |
| 57 | * network and BTS. |
| 58 | * |
| 59 | */ |
| 60 | static inline int rtp_calculate_port(int multiplex, int base) |
| 61 | { |
| 62 | return base + (multiplex * 2); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * Handling of MGCP Endpoints and the MGCP Config |
| 68 | */ |
| 69 | struct mgcp_endpoint; |
| 70 | struct mgcp_config; |
| 71 | struct mgcp_trunk_config; |
| 72 | struct mgcp_rtp_end; |
| 73 | |
| 74 | #define MGCP_ENDP_CRCX 1 |
| 75 | #define MGCP_ENDP_DLCX 2 |
| 76 | #define MGCP_ENDP_MDCX 3 |
| 77 | |
| 78 | /* |
| 79 | * what to do with the msg? |
| 80 | * - continue as usual? |
| 81 | * - reject and send a failure code? |
| 82 | * - defer? do not send anything |
| 83 | */ |
| 84 | #define MGCP_POLICY_CONT 4 |
| 85 | #define MGCP_POLICY_REJECT 5 |
| 86 | #define MGCP_POLICY_DEFER 6 |
| 87 | |
| 88 | typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint); |
| 89 | typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state); |
| 90 | typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id); |
| 91 | typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg); |
| 92 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
| 93 | |
| 94 | /** |
| 95 | * Return: |
| 96 | * < 0 in case no audio was processed |
| 97 | * >= 0 in case audio was processed. The remaining payload |
| 98 | * length will be returned. |
| 99 | */ |
| 100 | typedef int (*mgcp_processing)(struct mgcp_endpoint *endp, |
| 101 | struct mgcp_rtp_end *dst_end, |
| 102 | char *data, int *len, int buf_size); |
| 103 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 104 | struct mgcp_rtp_end *dst_end, |
| 105 | struct mgcp_rtp_end *src_end); |
| 106 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 107 | struct mgcp_conn_rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 108 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
| 109 | int *payload_type, |
| 110 | const char**subtype_name, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 111 | const char**fmtp_extra, |
| 112 | struct mgcp_conn_rtp *conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * This holds information on how to allocate ports |
| 116 | */ |
| 117 | struct mgcp_port_range { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 118 | /* addr or NULL to fall-back to default */ |
| 119 | char *bind_addr; |
| 120 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 121 | /* dynamically allocated */ |
| 122 | int range_start; |
| 123 | int range_end; |
| 124 | int last_port; |
| 125 | }; |
| 126 | |
| 127 | #define MGCP_KEEPALIVE_ONCE (-1) |
| 128 | |
| 129 | struct mgcp_trunk_config { |
| 130 | struct llist_head entry; |
| 131 | |
| 132 | struct mgcp_config *cfg; |
| 133 | |
| 134 | int trunk_nr; |
| 135 | int trunk_type; |
| 136 | |
| 137 | char *audio_fmtp_extra; |
| 138 | char *audio_name; |
| 139 | int audio_payload; |
| 140 | int audio_send_ptime; |
| 141 | int audio_send_name; |
| 142 | int audio_loop; |
| 143 | |
| 144 | int no_audio_transcoding; |
| 145 | |
| 146 | int omit_rtcp; |
| 147 | int keepalive_interval; |
| 148 | |
| 149 | /* RTP patching */ |
| 150 | int force_constant_ssrc; /* 0: don't, 1: once */ |
| 151 | int force_aligned_timing; |
| 152 | |
| 153 | /* spec handling */ |
| 154 | int force_realloc; |
| 155 | |
| 156 | /* timer */ |
| 157 | struct osmo_timer_list keepalive_timer; |
| 158 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 159 | /* When set, incoming RTP packets are not filtered |
| 160 | * when ports and ip-address do not match (debug) */ |
| 161 | int rtp_accept_all; |
| 162 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 163 | unsigned int number_endpoints; |
| 164 | struct mgcp_endpoint *endpoints; |
| 165 | }; |
| 166 | |
| 167 | enum mgcp_role { |
| 168 | MGCP_BSC = 0, |
| 169 | MGCP_BSC_NAT, |
| 170 | }; |
| 171 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 172 | struct mgcp_config { |
| 173 | int source_port; |
| 174 | char *local_ip; |
| 175 | char *source_addr; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 176 | char *call_agent_addr; |
| 177 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 178 | /* RTP processing */ |
| 179 | mgcp_processing rtp_processing_cb; |
| 180 | mgcp_processing_setup setup_rtp_processing_cb; |
| 181 | |
| 182 | mgcp_get_format get_net_downlink_format_cb; |
| 183 | |
| 184 | struct osmo_wqueue gw_fd; |
| 185 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 186 | struct mgcp_port_range net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 187 | int endp_dscp; |
| 188 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 189 | int force_ptime; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 190 | |
| 191 | mgcp_change change_cb; |
| 192 | mgcp_policy policy_cb; |
| 193 | mgcp_reset reset_cb; |
| 194 | mgcp_realloc realloc_cb; |
| 195 | mgcp_rqnt rqnt_cb; |
| 196 | void *data; |
| 197 | |
| 198 | uint32_t last_call_id; |
| 199 | |
| 200 | /* trunk handling */ |
| 201 | struct mgcp_trunk_config trunk; |
| 202 | struct llist_head trunks; |
| 203 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 204 | enum mgcp_role role; |
| 205 | |
| 206 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 207 | int osmux; |
| 208 | /* addr to bind the server to */ |
| 209 | char *osmux_addr; |
| 210 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 211 | * the osmux socket is already initialized. |
| 212 | */ |
| 213 | int osmux_init; |
| 214 | /* osmux batch factor: from 1 to 4 maximum */ |
| 215 | int osmux_batch; |
| 216 | /* osmux batch size (in bytes) */ |
| 217 | int osmux_batch_size; |
| 218 | /* osmux port */ |
| 219 | uint16_t osmux_port; |
| 220 | /* Pad circuit with dummy messages until we see the first voice |
| 221 | * message. |
| 222 | */ |
| 223 | uint16_t osmux_dummy; |
| 224 | }; |
| 225 | |
| 226 | /* config management */ |
| 227 | struct mgcp_config *mgcp_config_alloc(void); |
| 228 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 229 | enum mgcp_role role); |
| 230 | int mgcp_vty_init(void); |
| 231 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
| 232 | void mgcp_release_endp(struct mgcp_endpoint *endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 233 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval); |
| 234 | |
| 235 | /* |
| 236 | * format helper functions |
| 237 | */ |
| 238 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 239 | |
| 240 | /* adc helper */ |
| 241 | static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot) |
| 242 | { |
| 243 | if (timeslot == 0) { |
| 244 | LOGP(DLMGCP, LOGL_ERROR, "Timeslot should not be 0\n"); |
| 245 | timeslot = 255; |
| 246 | } |
| 247 | |
| 248 | return timeslot + (32 * multiplex); |
| 249 | } |
| 250 | |
| 251 | static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot) |
| 252 | { |
| 253 | *multiplex = endpoint / 32; |
| 254 | *timeslot = endpoint % 32; |
| 255 | } |
| 256 | |
| 257 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint); |
| 258 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 259 | |
| 260 | |
| 261 | 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] | 262 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |