Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +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 | |
| 23 | #ifndef OPENBSC_MGCP_H |
| 24 | #define OPENBSC_MGCP_H |
| 25 | |
| 26 | #include <osmocom/core/msgb.h> |
| 27 | #include <osmocom/core/write_queue.h> |
| 28 | #include <osmocom/core/timer.h> |
| 29 | #include <osmocom/core/logging.h> |
| 30 | |
| 31 | #include <arpa/inet.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <netinet/in.h> |
| 35 | |
| 36 | #define RTP_PORT_DEFAULT 4000 |
| 37 | #define RTP_PORT_NET_DEFAULT 16000 |
| 38 | |
| 39 | /** |
| 40 | * Calculate the RTP audio port for the given multiplex |
| 41 | * and the direction. This allows a semi static endpoint |
| 42 | * to port calculation removing the need for the BSC |
| 43 | * and the MediaGateway to communicate. |
| 44 | * |
| 45 | * Port usage explained: |
| 46 | * base + (multiplex * 2) + 0 == local port to wait for network packets |
| 47 | * base + (multiplex * 2) + 1 == local port for rtcp |
| 48 | * |
| 49 | * The above port will receive packets from the BTS that need |
| 50 | * to be patched and forwarded to the network. |
| 51 | * The above port will receive packets from the network that |
| 52 | * need to be patched and forwarded to the BTS. |
| 53 | * |
| 54 | * We assume to have a static BTS IP address so we can differentiate |
| 55 | * network and BTS. |
| 56 | * |
| 57 | */ |
| 58 | static inline int rtp_calculate_port(int multiplex, int base) |
| 59 | { |
| 60 | return base + (multiplex * 2); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * Handling of MGCP Endpoints and the MGCP Config |
| 66 | */ |
| 67 | struct mgcp_endpoint; |
| 68 | struct mgcp_config; |
| 69 | struct mgcp_trunk_config; |
| 70 | struct mgcp_rtp_end; |
| 71 | |
| 72 | #define MGCP_ENDP_CRCX 1 |
| 73 | #define MGCP_ENDP_DLCX 2 |
| 74 | #define MGCP_ENDP_MDCX 3 |
| 75 | |
| 76 | /* |
| 77 | * what to do with the msg? |
| 78 | * - continue as usual? |
| 79 | * - reject and send a failure code? |
| 80 | * - defer? do not send anything |
| 81 | */ |
| 82 | #define MGCP_POLICY_CONT 4 |
| 83 | #define MGCP_POLICY_REJECT 5 |
| 84 | #define MGCP_POLICY_DEFER 6 |
| 85 | |
| 86 | typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint); |
| 87 | typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state); |
| 88 | typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id); |
| 89 | typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg); |
| 90 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
| 91 | |
| 92 | /** |
| 93 | * Return: |
| 94 | * < 0 in case no audio was processed |
| 95 | * >= 0 in case audio was processed. The remaining payload |
| 96 | * length will be returned. |
| 97 | */ |
| 98 | typedef int (*mgcp_processing)(struct mgcp_endpoint *endp, |
| 99 | struct mgcp_rtp_end *dst_end, |
| 100 | char *data, int *len, int buf_size); |
| 101 | typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp, |
| 102 | struct mgcp_rtp_end *dst_end, |
| 103 | struct mgcp_rtp_end *src_end); |
| 104 | |
| 105 | typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, |
| 106 | int *payload_type, |
| 107 | const char**subtype_name, |
| 108 | const char**fmtp_extra); |
| 109 | |
| 110 | #define PORT_ALLOC_STATIC 0 |
| 111 | #define PORT_ALLOC_DYNAMIC 1 |
| 112 | |
| 113 | /** |
| 114 | * This holds information on how to allocate ports |
| 115 | */ |
| 116 | struct mgcp_port_range { |
| 117 | int mode; |
| 118 | |
| 119 | /* addr or NULL to fall-back to default */ |
| 120 | char *bind_addr; |
| 121 | |
| 122 | /* pre-allocated from a base? */ |
| 123 | int base_port; |
| 124 | |
| 125 | /* dynamically allocated */ |
| 126 | int range_start; |
| 127 | int range_end; |
| 128 | int last_port; |
| 129 | }; |
| 130 | |
| 131 | #define MGCP_KEEPALIVE_ONCE (-1) |
| 132 | |
| 133 | struct mgcp_trunk_config { |
| 134 | struct llist_head entry; |
| 135 | |
| 136 | struct mgcp_config *cfg; |
| 137 | |
| 138 | int trunk_nr; |
| 139 | int trunk_type; |
| 140 | |
| 141 | char *audio_fmtp_extra; |
| 142 | char *audio_name; |
| 143 | int audio_payload; |
| 144 | int audio_send_ptime; |
| 145 | int audio_send_name; |
| 146 | int audio_loop; |
| 147 | |
| 148 | int no_audio_transcoding; |
| 149 | |
| 150 | int omit_rtcp; |
| 151 | int keepalive_interval; |
| 152 | |
| 153 | /* RTP patching */ |
| 154 | int force_constant_ssrc; /* 0: don't, 1: once */ |
| 155 | int force_aligned_timing; |
| 156 | |
| 157 | /* spec handling */ |
| 158 | int force_realloc; |
| 159 | |
| 160 | /* timer */ |
| 161 | struct osmo_timer_list keepalive_timer; |
| 162 | |
| 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 | |
| 172 | enum mgcp_connection_mode { |
| 173 | MGCP_CONN_NONE = 0, |
| 174 | MGCP_CONN_RECV_ONLY = 1, |
| 175 | MGCP_CONN_SEND_ONLY = 2, |
| 176 | MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY, |
| 177 | MGCP_CONN_LOOPBACK = 4 | MGCP_CONN_RECV_SEND, |
| 178 | }; |
| 179 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 180 | struct mgcp_config { |
| 181 | int source_port; |
| 182 | char *local_ip; |
| 183 | char *source_addr; |
| 184 | char *bts_ip; |
| 185 | char *call_agent_addr; |
| 186 | |
| 187 | struct in_addr bts_in; |
| 188 | |
| 189 | /* transcoder handling */ |
| 190 | char *transcoder_ip; |
| 191 | struct in_addr transcoder_in; |
| 192 | int transcoder_remote_base; |
| 193 | |
| 194 | /* RTP processing */ |
| 195 | mgcp_processing rtp_processing_cb; |
| 196 | mgcp_processing_setup setup_rtp_processing_cb; |
| 197 | |
| 198 | mgcp_get_format get_net_downlink_format_cb; |
| 199 | |
| 200 | struct osmo_wqueue gw_fd; |
| 201 | |
| 202 | struct mgcp_port_range bts_ports; |
| 203 | struct mgcp_port_range net_ports; |
| 204 | struct mgcp_port_range transcoder_ports; |
| 205 | int endp_dscp; |
| 206 | |
| 207 | int bts_force_ptime; |
| 208 | |
| 209 | mgcp_change change_cb; |
| 210 | mgcp_policy policy_cb; |
| 211 | mgcp_reset reset_cb; |
| 212 | mgcp_realloc realloc_cb; |
| 213 | mgcp_rqnt rqnt_cb; |
| 214 | void *data; |
| 215 | |
| 216 | uint32_t last_call_id; |
| 217 | |
| 218 | /* trunk handling */ |
| 219 | struct mgcp_trunk_config trunk; |
| 220 | struct llist_head trunks; |
| 221 | |
| 222 | /* only used for start with a static configuration */ |
| 223 | int last_net_port; |
| 224 | int last_bts_port; |
| 225 | |
| 226 | enum mgcp_role role; |
| 227 | |
| 228 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 229 | int osmux; |
| 230 | /* addr to bind the server to */ |
| 231 | char *osmux_addr; |
| 232 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 233 | * the osmux socket is already initialized. |
| 234 | */ |
| 235 | int osmux_init; |
| 236 | /* osmux batch factor: from 1 to 4 maximum */ |
| 237 | int osmux_batch; |
| 238 | /* osmux batch size (in bytes) */ |
| 239 | int osmux_batch_size; |
| 240 | /* osmux port */ |
| 241 | uint16_t osmux_port; |
| 242 | /* Pad circuit with dummy messages until we see the first voice |
| 243 | * message. |
| 244 | */ |
| 245 | uint16_t osmux_dummy; |
| 246 | }; |
| 247 | |
| 248 | /* config management */ |
| 249 | struct mgcp_config *mgcp_config_alloc(void); |
| 250 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 251 | enum mgcp_role role); |
| 252 | int mgcp_vty_init(void); |
| 253 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
| 254 | void mgcp_release_endp(struct mgcp_endpoint *endp); |
| 255 | void mgcp_initialize_endp(struct mgcp_endpoint *endp); |
| 256 | int mgcp_reset_transcoder(struct mgcp_config *cfg); |
| 257 | void mgcp_format_stats(struct mgcp_endpoint *endp, char *stats, size_t size); |
| 258 | int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter); |
| 259 | |
| 260 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval); |
| 261 | |
| 262 | /* |
| 263 | * format helper functions |
| 264 | */ |
| 265 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
| 266 | |
| 267 | /* adc helper */ |
| 268 | static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot) |
| 269 | { |
| 270 | if (timeslot == 0) { |
| 271 | LOGP(DLMGCP, LOGL_ERROR, "Timeslot should not be 0\n"); |
| 272 | timeslot = 255; |
| 273 | } |
| 274 | |
| 275 | return timeslot + (32 * multiplex); |
| 276 | } |
| 277 | |
| 278 | static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot) |
| 279 | { |
| 280 | *multiplex = endpoint / 32; |
| 281 | *timeslot = endpoint % 32; |
| 282 | } |
| 283 | |
| 284 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint); |
| 285 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 286 | |
| 287 | |
| 288 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port); |
| 289 | int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc); |
| 290 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |
| 291 | |
| 292 | #endif |