Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 4 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 23 | #ifndef OPENBSC_MGCP_H |
| 24 | #define OPENBSC_MGCP_H |
| 25 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 26 | #include <osmocom/core/msgb.h> |
| 27 | #include <osmocom/core/write_queue.h> |
Jacob Erlbeck | 075a9eb | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 28 | #include <osmocom/core/timer.h> |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | e3946f4 | 2010-09-20 01:07:23 +0800 | [diff] [blame] | 30 | #include "debug.h" |
| 31 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 32 | #include <arpa/inet.h> |
| 33 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 34 | #define RTP_PORT_DEFAULT 4000 |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 35 | #define RTP_PORT_NET_DEFAULT 16000 |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Calculate the RTP audio port for the given multiplex |
| 39 | * and the direction. This allows a semi static endpoint |
| 40 | * to port calculation removing the need for the BSC |
| 41 | * and the MediaGateway to communicate. |
| 42 | * |
| 43 | * Port usage explained: |
| 44 | * base + (multiplex * 2) + 0 == local port to wait for network packets |
| 45 | * base + (multiplex * 2) + 1 == local port for rtcp |
| 46 | * |
| 47 | * The above port will receive packets from the BTS that need |
| 48 | * to be patched and forwarded to the network. |
| 49 | * The above port will receive packets from the network that |
| 50 | * need to be patched and forwarded to the BTS. |
| 51 | * |
| 52 | * We assume to have a static BTS IP address so we can differentiate |
| 53 | * network and BTS. |
| 54 | * |
| 55 | */ |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 56 | static inline int rtp_calculate_port(int multiplex, int base) |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 57 | { |
| 58 | return base + (multiplex * 2); |
| 59 | } |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 60 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 61 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 62 | /* |
| 63 | * Handling of MGCP Endpoints and the MGCP Config |
| 64 | */ |
| 65 | struct mgcp_endpoint; |
| 66 | struct mgcp_config; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 67 | struct mgcp_trunk_config; |
Holger Hans Peter Freyther | 77f7afe | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 68 | |
Holger Hans Peter Freyther | 77f7afe | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 69 | #define MGCP_ENDP_CRCX 1 |
| 70 | #define MGCP_ENDP_DLCX 2 |
| 71 | #define MGCP_ENDP_MDCX 3 |
| 72 | |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 73 | /* |
| 74 | * what to do with the msg? |
| 75 | * - continue as usual? |
| 76 | * - reject and send a failure code? |
| 77 | * - defer? do not send anything |
| 78 | */ |
| 79 | #define MGCP_POLICY_CONT 4 |
| 80 | #define MGCP_POLICY_REJECT 5 |
| 81 | #define MGCP_POLICY_DEFER 6 |
| 82 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 83 | typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint); |
| 84 | typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state); |
| 85 | typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id); |
Holger Hans Peter Freyther | 74db774 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 86 | typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg); |
Holger Hans Peter Freyther | ce55361 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 87 | typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 88 | |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 89 | #define PORT_ALLOC_STATIC 0 |
| 90 | #define PORT_ALLOC_DYNAMIC 1 |
| 91 | |
| 92 | /** |
| 93 | * This holds information on how to allocate ports |
| 94 | */ |
| 95 | struct mgcp_port_range { |
| 96 | int mode; |
| 97 | |
| 98 | /* pre-allocated from a base? */ |
| 99 | int base_port; |
Holger Hans Peter Freyther | 1be9f2f | 2010-08-05 07:20:09 +0800 | [diff] [blame] | 100 | |
| 101 | /* dynamically allocated */ |
| 102 | int range_start; |
| 103 | int range_end; |
| 104 | int last_port; |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
Jacob Erlbeck | 075a9eb | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 107 | #define MGCP_KEEPALIVE_ONCE (-1) |
| 108 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 109 | struct mgcp_trunk_config { |
Holger Hans Peter Freyther | 0e939fe | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 110 | struct llist_head entry; |
| 111 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 112 | struct mgcp_config *cfg; |
| 113 | |
| 114 | int trunk_nr; |
| 115 | int trunk_type; |
| 116 | |
Holger Hans Peter Freyther | 5ea1bc7 | 2012-09-03 00:07:39 +0200 | [diff] [blame] | 117 | char *audio_fmtp_extra; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 118 | char *audio_name; |
| 119 | int audio_payload; |
Jacob Erlbeck | 0a1bc56 | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 120 | int audio_send_ptime; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 121 | int audio_loop; |
| 122 | |
Holger Hans Peter Freyther | a8090d5 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 123 | int omit_rtcp; |
Jacob Erlbeck | 075a9eb | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 124 | int keepalive_interval; |
Holger Hans Peter Freyther | a8090d5 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 125 | |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 126 | /* RTP patching */ |
| 127 | int force_constant_ssrc; /* 0: don't, 1: once */ |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 128 | int force_aligned_timing; |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 129 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 130 | /* spec handling */ |
| 131 | int force_realloc; |
| 132 | |
Jacob Erlbeck | 075a9eb | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 133 | /* timer */ |
| 134 | struct osmo_timer_list keepalive_timer; |
| 135 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 136 | unsigned int number_endpoints; |
| 137 | struct mgcp_endpoint *endpoints; |
| 138 | }; |
| 139 | |
Pablo Neira Ayuso | 326b5d8 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 140 | enum mgcp_role { |
| 141 | MGCP_BSC = 0, |
| 142 | MGCP_BSC_NAT, |
| 143 | }; |
| 144 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 145 | struct mgcp_config { |
| 146 | int source_port; |
| 147 | char *local_ip; |
| 148 | char *source_addr; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 149 | char *bts_ip; |
Holger Hans Peter Freyther | b79994c | 2010-03-31 11:46:41 +0200 | [diff] [blame] | 150 | char *call_agent_addr; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 151 | |
| 152 | struct in_addr bts_in; |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 153 | |
Holger Hans Peter Freyther | b98ba72 | 2010-09-19 04:21:39 +0800 | [diff] [blame] | 154 | /* transcoder handling */ |
| 155 | char *transcoder_ip; |
| 156 | struct in_addr transcoder_in; |
| 157 | int transcoder_remote_base; |
| 158 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 159 | struct osmo_wqueue gw_fd; |
Holger Hans Peter Freyther | 6f68010 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 160 | |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 161 | struct mgcp_port_range bts_ports; |
| 162 | struct mgcp_port_range net_ports; |
Holger Hans Peter Freyther | 54aaa0f | 2010-09-17 23:35:53 +0800 | [diff] [blame] | 163 | struct mgcp_port_range transcoder_ports; |
Holger Hans Peter Freyther | d0c3229 | 2010-07-27 20:34:45 +0800 | [diff] [blame] | 164 | int endp_dscp; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 165 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 166 | mgcp_change change_cb; |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 167 | mgcp_policy policy_cb; |
Holger Hans Peter Freyther | 9bdcc9c | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 168 | mgcp_reset reset_cb; |
Holger Hans Peter Freyther | 869e38e | 2010-08-06 17:54:27 +0800 | [diff] [blame] | 169 | mgcp_realloc realloc_cb; |
Holger Hans Peter Freyther | 8d0be25 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 170 | mgcp_rqnt rqnt_cb; |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 171 | void *data; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 172 | |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 173 | uint32_t last_call_id; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 174 | |
| 175 | /* trunk handling */ |
| 176 | struct mgcp_trunk_config trunk; |
Holger Hans Peter Freyther | 0e939fe | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 177 | struct llist_head trunks; |
Holger Hans Peter Freyther | 44016fe | 2011-02-28 14:46:01 +0100 | [diff] [blame] | 178 | |
| 179 | /* only used for start with a static configuration */ |
| 180 | int last_net_port; |
| 181 | int last_bts_port; |
Pablo Neira Ayuso | 326b5d8 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 182 | |
| 183 | enum mgcp_role role; |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame^] | 184 | |
| 185 | /* osmux translator: 0 means disabled, 1 means enabled */ |
| 186 | int osmux; |
| 187 | /* The BSC-NAT may ask for enabling osmux on demand. This tells us if |
| 188 | * the osmux socket is already initialized. |
| 189 | */ |
| 190 | int osmux_init; |
| 191 | /* osmux batch factor: from 1 to 4 maximum */ |
| 192 | int osmux_batch; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | /* config management */ |
| 196 | struct mgcp_config *mgcp_config_alloc(void); |
Pablo Neira Ayuso | 326b5d8 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 197 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg, |
| 198 | enum mgcp_role role); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 199 | int mgcp_vty_init(void); |
Holger Hans Peter Freyther | 1f0c5b4 | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 200 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
Holger Hans Peter Freyther | 154b955 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 201 | void mgcp_free_endp(struct mgcp_endpoint *endp); |
Holger Hans Peter Freyther | f2eedff | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 202 | int mgcp_reset_transcoder(struct mgcp_config *cfg); |
Holger Hans Peter Freyther | 0bf15a8 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 203 | void mgcp_format_stats(struct mgcp_endpoint *endp, char *stats, size_t size); |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 204 | int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 205 | |
Jacob Erlbeck | 075a9eb | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 206 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval); |
| 207 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 208 | /* |
| 209 | * format helper functions |
| 210 | */ |
| 211 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 212 | |
Holger Hans Peter Freyther | 616d222 | 2010-03-31 09:27:04 +0200 | [diff] [blame] | 213 | /* adc helper */ |
| 214 | static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot) |
| 215 | { |
Holger Hans Peter Freyther | e3946f4 | 2010-09-20 01:07:23 +0800 | [diff] [blame] | 216 | if (timeslot == 0) { |
| 217 | LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n"); |
| 218 | timeslot = 255; |
| 219 | } |
| 220 | |
Holger Hans Peter Freyther | cb8c35c | 2010-08-10 20:24:24 +0800 | [diff] [blame] | 221 | return timeslot + (32 * multiplex); |
Holger Hans Peter Freyther | 616d222 | 2010-03-31 09:27:04 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Holger Hans Peter Freyther | 82049d8 | 2010-08-28 17:59:15 +0800 | [diff] [blame] | 224 | static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot) |
| 225 | { |
| 226 | *multiplex = endpoint / 32; |
| 227 | *timeslot = endpoint % 32; |
Holger Hans Peter Freyther | 82049d8 | 2010-08-28 17:59:15 +0800 | [diff] [blame] | 228 | } |
| 229 | |
Harald Welte | 94b499c | 2012-01-27 00:46:49 +0100 | [diff] [blame] | 230 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint); |
| 231 | int mgcp_send_reset_all(struct mgcp_config *cfg); |
| 232 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 233 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame^] | 234 | int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port); |
| 235 | int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc); |
| 236 | int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len); |
| 237 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 238 | #endif |