blob: 33bfe716c32ab583cdc37c939e99d2a87f78728d [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* 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 Maier87bd9be2017-08-22 16:35:41 +020023#pragma once
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020024
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 Hofmeyr67793542017-09-08 04:25:16 +020030#include <osmocom/mgcp/mgcp_common.h>
31
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032#include <arpa/inet.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36
Philipp Maierc66ab2c2020-06-02 20:55:34 +020037#include "mgcp_ratectr.h"
38
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#define RTP_PORT_DEFAULT_RANGE_START 16002
40#define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020041
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020042/*
43 * Handling of MGCP Endpoints and the MGCP Config
44 */
45struct mgcp_endpoint;
46struct mgcp_config;
Philipp Maier14b27a82020-06-02 20:15:30 +020047struct mgcp_trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048struct mgcp_rtp_end;
49
50#define MGCP_ENDP_CRCX 1
51#define MGCP_ENDP_DLCX 2
52#define MGCP_ENDP_MDCX 3
53
54/*
55 * what to do with the msg?
56 * - continue as usual?
57 * - reject and send a failure code?
58 * - defer? do not send anything
59 */
60#define MGCP_POLICY_CONT 4
61#define MGCP_POLICY_REJECT 5
62#define MGCP_POLICY_DEFER 6
63
Philipp Maierc66ab2c2020-06-02 20:55:34 +020064typedef int (*mgcp_change)(struct mgcp_endpoint *endp, int state);
65typedef int (*mgcp_policy)(struct mgcp_endpoint *endp, int state, const char *transaction_id);
Philipp Maier14b27a82020-06-02 20:15:30 +020066typedef int (*mgcp_reset)(struct mgcp_trunk *cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020067typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone);
68
69/**
70 * Return:
71 * < 0 in case no audio was processed
72 * >= 0 in case audio was processed. The remaining payload
73 * length will be returned.
74 */
75typedef int (*mgcp_processing)(struct mgcp_endpoint *endp,
76 struct mgcp_rtp_end *dst_end,
77 char *data, int *len, int buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020078
Philipp Maier87bd9be2017-08-22 16:35:41 +020079struct mgcp_conn_rtp;
Philipp Maieracc10352018-07-19 18:07:57 +020080
81typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp,
82 struct mgcp_conn_rtp *conn_dst,
83 struct mgcp_conn_rtp *conn_src);
84
Philipp Maier58128252019-03-06 11:28:18 +010085struct mgcp_rtp_codec;
86
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020087typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +010088 const struct mgcp_rtp_codec **codec,
89 const char **fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020091
92/**
93 * This holds information on how to allocate ports
94 */
95struct mgcp_port_range {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020096 /* addr or NULL to fall-back to default */
97 char *bind_addr;
98
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099 /* dynamically allocated */
100 int range_start;
101 int range_end;
102 int last_port;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100103
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 Hofmeyrf83ec562017-09-07 19:18:40 +0200109};
110
Philipp Maiere726d4f2017-11-01 10:41:34 +0100111/* There are up to three modes in which the keep-alive dummy packet can be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100112 * sent. The behaviour is controlled via the keepalive_interval member of the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100113 * 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 Welte1d1b98f2017-12-25 10:03:40 +0100117 * is scheduled. For all vales greater 0, the timer is scheduled and the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100118 * value is used as interval. See also mgcp_keepalive_timer_cb(),
119 * handle_modify_con(), and handle_create_con() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200120#define MGCP_KEEPALIVE_ONCE (-1)
Philipp Maiere726d4f2017-11-01 10:41:34 +0100121#define MGCP_KEEPALIVE_NEVER 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200122
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200123enum mgcp_role {
124 MGCP_BSC = 0,
125 MGCP_BSC_NAT,
126};
127
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128struct mgcp_config {
129 int source_port;
130 char *local_ip;
131 char *source_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132 char *call_agent_addr;
133
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 /* 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 Hofmeyrf83ec562017-09-07 19:18:40 +0200142 struct mgcp_port_range net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143 int endp_dscp;
144
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 int force_ptime;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146
147 mgcp_change change_cb;
148 mgcp_policy policy_cb;
149 mgcp_reset reset_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 mgcp_rqnt rqnt_cb;
151 void *data;
152
153 uint32_t last_call_id;
154
Philipp Maierd19de2e2020-06-03 13:55:33 +0200155 /* list holding the trunks */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156 struct llist_head trunks;
157
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200158 enum mgcp_role role;
159
160 /* osmux translator: 0 means disabled, 1 means enabled */
161 int osmux;
162 /* addr to bind the server to */
163 char *osmux_addr;
164 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
165 * the osmux socket is already initialized.
166 */
167 int osmux_init;
168 /* osmux batch factor: from 1 to 4 maximum */
169 int osmux_batch;
170 /* osmux batch size (in bytes) */
171 int osmux_batch_size;
172 /* osmux port */
173 uint16_t osmux_port;
174 /* Pad circuit with dummy messages until we see the first voice
175 * message.
176 */
177 uint16_t osmux_dummy;
Philipp Maier12943ea2018-01-17 15:40:25 +0100178 /* domain name of the media gateway */
179 char domain[255+1];
Oliver Smithe36b7752019-01-22 16:31:36 +0100180
181 /* time after which inactive connections (CIs) get closed */
182 int conn_timeout;
Harald Welte9852e222020-03-08 10:22:14 +0100183
184 /* osmocom CTRL interface */
185 struct ctrl_handle *ctrl;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200186
187 /* global rate counters to measure the MGWs overall performance and
188 * health */
189 struct mgcp_ratectr_global ratectr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190};
191
192/* config management */
193struct mgcp_config *mgcp_config_alloc(void);
194int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
195 enum mgcp_role role);
196int mgcp_vty_init(void);
Philipp Maier14b27a82020-06-02 20:15:30 +0200197void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198
199/*
200 * format helper functions
201 */
202struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
203
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200205int mgcp_send_reset_ep(struct mgcp_endpoint *endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206int mgcp_send_reset_all(struct mgcp_config *cfg);
207
208
209int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);