/src/rtpproxy/modules/dtls_gw/rtpp_dtls_gw.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022 Sippy Software, Inc., http://www.sippysoft.com |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions |
6 | | * are met: |
7 | | * 1. Redistributions of source code must retain the above copyright |
8 | | * notice, this list of conditions and the following disclaimer. |
9 | | * 2. Redistributions in binary form must reproduce the above copyright |
10 | | * notice, this list of conditions and the following disclaimer in the |
11 | | * documentation and/or other materials provided with the distribution. |
12 | | * |
13 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
14 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
17 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
19 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
20 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
21 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
23 | | * SUCH DAMAGE. |
24 | | * |
25 | | */ |
26 | | |
27 | | #include <sys/types.h> |
28 | | #include <sys/socket.h> |
29 | | #include <arpa/inet.h> |
30 | | #include <stdatomic.h> |
31 | | #include <stdbool.h> |
32 | | #include <stddef.h> |
33 | | #include <stdint.h> |
34 | | #include <stdlib.h> |
35 | | #include <stdio.h> |
36 | | #include <string.h> |
37 | | |
38 | | #include <srtp2/srtp.h> |
39 | | |
40 | | #include "config_pp.h" |
41 | | |
42 | | #include "rtpp_types.h" |
43 | | #include "rtpp_debug.h" |
44 | | #include "rtpp_module.h" |
45 | | #include "rtpp_module_wthr.h" |
46 | | #include "rtpp_module_cplane.h" |
47 | | #include "rtpp_log.h" |
48 | | #include "rtpp_log_obj.h" |
49 | | #include "rtpp_codeptr.h" |
50 | | #include "rtpp_refcnt.h" |
51 | | #include "rtpp_cfg.h" |
52 | | #include "rtpp_wi.h" |
53 | | #include "rtpp_wi_sgnl.h" |
54 | | #include "rtpp_wi_data.h" |
55 | | #include "rtpp_queue.h" |
56 | | #include "rtpp_stream.h" |
57 | | #include "rtp.h" |
58 | | #include "rtpp_time.h" |
59 | | #include "rtp_packet.h" |
60 | | #include "rtpp_packetops.h" |
61 | | #include "rtpp_command.h" |
62 | | #include "rtpp_command_args.h" |
63 | | #include "rtpp_command_sub.h" |
64 | | #include "rtpp_util.h" |
65 | | #include "rtpp_socket.h" |
66 | | #include "rtpp_sessinfo.h" |
67 | | #include "rtpp_session.h" |
68 | | #include "rtpp_stats.h" |
69 | | #include "rtpp_stream.h" |
70 | | #include "rtpp_pipe.h" |
71 | | #include "rtpp_proc_async.h" |
72 | | #include "rtpp_ttl.h" |
73 | | #include "rtpp_util.h" |
74 | | #include "rtpp_pcount.h" |
75 | | #include "rtpp_pcnt_strm.h" |
76 | | #include "rtpp_linker_set.h" |
77 | | #include "rtpp_bindaddr.h" |
78 | | #include "advanced/packet_processor.h" |
79 | | #include "advanced/pproc_manager.h" |
80 | | |
81 | | #include "rtpp_dtls.h" |
82 | | #include "rtpp_dtls_util.h" |
83 | | #include "rtpp_dtls_conn.h" |
84 | | |
85 | | struct rtpp_module_priv { |
86 | | struct rtpp_dtls *dtls_ctx; |
87 | | const struct rtpp_cfg *cfsp; |
88 | | struct rtpp_minfo *mself; |
89 | | }; |
90 | | |
91 | | struct dtls_gw_stream_cfg { |
92 | | struct rtpp_refcnt *rcnt; |
93 | | struct rtpp_dtls_conn *dtls_conn; |
94 | | struct rtpp_minfo *mself; |
95 | | }; |
96 | | |
97 | | enum rtpp_dtls_dir { |
98 | | DTLS_IN, SRTP_IN, RTP_OUT |
99 | | }; |
100 | | |
101 | | struct rtpp_dtls_gw_aux { |
102 | | enum rtpp_dtls_dir direction; |
103 | | struct rtpp_dtls_conn *dtls_conn; |
104 | | struct rtpp_minfo *mself; |
105 | | }; |
106 | | |
107 | | struct wipkt { |
108 | | struct pkt_proc_ctx pktx; |
109 | | struct rtpp_dtls_gw_aux edata; |
110 | | }; |
111 | | |
112 | | static struct rtpp_module_priv *rtpp_dtls_gw_ctor(const struct rtpp_cfg *, |
113 | | struct rtpp_minfo *); |
114 | | static void rtpp_dtls_gw_dtor(struct rtpp_module_priv *); |
115 | | static void rtpp_dtls_gw_worker(const struct rtpp_wthrdata *); |
116 | | static int rtpp_dtls_gw_handle_command(struct rtpp_module_priv *, |
117 | | const struct rtpp_subc_ctx *); |
118 | | static int rtpp_dtls_gw_taste_encrypted(struct pkt_proc_ctx *); |
119 | | static int rtpp_dtls_gw_taste_plain(struct pkt_proc_ctx *); |
120 | | static struct pproc_act rtpp_dtls_gw_enqueue(const struct pkt_proc_ctx *); |
121 | | |
122 | | #ifdef RTPP_CHECK_LEAKS |
123 | | #include "rtpp_memdeb_internal.h" |
124 | | |
125 | | RTPP_MEMDEB_APP_STATIC; |
126 | | #endif |
127 | | |
128 | | const struct rtpp_minfo RTPP_MOD_SELF = { |
129 | | .descr.name = "dtls_gw", |
130 | | .descr.ver = MI_VER_INIT(), |
131 | | .descr.module_id = 4, |
132 | | .proc.ctor = rtpp_dtls_gw_ctor, |
133 | | .proc.dtor = rtpp_dtls_gw_dtor, |
134 | | .wapi = &(const struct rtpp_wthr_handlers){ |
135 | | .main_thread = rtpp_dtls_gw_worker, |
136 | | .queue_size = RTPQ_MEDIUM_CB_LEN, |
137 | | }, |
138 | | .capi = &(const struct rtpp_cplane_handlers){.ul_subc_handle = rtpp_dtls_gw_handle_command}, |
139 | | .fn = &(struct rtpp_minfo_fset){0}, |
140 | | #ifdef RTPP_CHECK_LEAKS |
141 | | .memdeb_p = &MEMDEB_SYM |
142 | | #endif |
143 | | }; |
144 | | #if defined(LIBRTPPROXY) |
145 | | DATA_SET(rtpp_modules, RTPP_MOD_SELF); |
146 | | #endif |
147 | | |
148 | | static void |
149 | | rtpp_dtls_gw_worker(const struct rtpp_wthrdata *wp) |
150 | 6 | { |
151 | 6 | struct rtpp_wi *wi; |
152 | 6 | struct wipkt *wip; |
153 | 6 | struct res_loc res; |
154 | | |
155 | 327k | for (;;) { |
156 | 327k | wi = rtpp_queue_get_item(wp->mod_q, 0); |
157 | 327k | if (wi == wp->sigterm) { |
158 | 6 | RTPP_OBJ_DECREF(wi); |
159 | 6 | break; |
160 | 6 | } |
161 | 327k | wip = rtpp_wi_data_get_ptr(wi, sizeof(*wip), sizeof(*wip)); |
162 | 327k | struct rtpp_dtls_gw_aux *edp = &wip->edata; |
163 | 327k | switch (edp->direction) { |
164 | 43.1k | case DTLS_IN: |
165 | | #if 0 |
166 | | RTPP_LOG(edp->mself->log, RTPP_LOG_DBUG, "Packet from DTLS"); |
167 | | #endif |
168 | 43.1k | CALL_SMETHOD(edp->dtls_conn, dtls_recv, wip->pktx.pktp); |
169 | 43.1k | res = RES_HERE(1); |
170 | 43.1k | break; |
171 | 96.3k | case SRTP_IN: |
172 | | #if 0 |
173 | | RTPP_LOG(RTPP_MOD_SELF.log, RTPP_LOG_DBUG, "DTLS: packet SRTP->RTP"); |
174 | | #endif |
175 | 96.3k | res = CALL_SMETHOD(edp->dtls_conn, srtp_recv, &wip->pktx); |
176 | 96.3k | break; |
177 | 187k | case RTP_OUT: |
178 | | #if 0 |
179 | | RTPP_LOG(RTPP_MOD_SELF.log, RTPP_LOG_DBUG, "DTLS: packet RTP->SRTP"); |
180 | | #endif |
181 | 187k | res = CALL_SMETHOD(edp->dtls_conn, rtp_send, &wip->pktx); |
182 | 187k | break; |
183 | 0 | default: |
184 | 0 | abort(); |
185 | 327k | } |
186 | 327k | if (res.v != 0) { |
187 | 327k | switch (edp->direction) { |
188 | 43.1k | case DTLS_IN: |
189 | 43.1k | break; |
190 | 96.3k | case SRTP_IN: |
191 | 96.3k | CALL_SMETHOD(wip->pktx.strmp_in->pcnt_strm, reg_pktin, wip->pktx.pktp); |
192 | | /* Fallthrouth */ |
193 | 284k | case RTP_OUT: |
194 | 284k | CALL_SMETHOD(wip->pktx.strmp_in->pcount, reg_drop, res.loc); |
195 | 284k | CALL_SMETHOD(wip->pktx.strmp_in->pproc_manager, reg_drop); |
196 | 284k | break; |
197 | 327k | } |
198 | 327k | RTPP_OBJ_DECREF(wip->pktx.pktp); |
199 | 327k | } |
200 | 327k | RTPP_OBJ_DECREF(wi); |
201 | 327k | } |
202 | 6 | } |
203 | | |
204 | | static struct dtls_gw_stream_cfg * |
205 | | dtls_gw_data_ctor(struct rtpp_module_priv *pvt, struct rtpp_stream *dtls_strmp) |
206 | 31.7k | { |
207 | 31.7k | struct dtls_gw_stream_cfg *rtps_c; |
208 | | |
209 | 31.7k | rtps_c = mod_rzmalloc(sizeof(*rtps_c), offsetof(struct dtls_gw_stream_cfg, rcnt)); |
210 | 31.7k | if (rtps_c == NULL) { |
211 | 0 | goto e0; |
212 | 0 | } |
213 | 31.7k | rtps_c->dtls_conn = CALL_METHOD(pvt->dtls_ctx, newconn, dtls_strmp); |
214 | 31.7k | if (rtps_c->dtls_conn == NULL) { |
215 | 0 | goto e1; |
216 | 0 | } |
217 | 31.7k | rtps_c->mself = pvt->mself; |
218 | 31.7k | RC_INCREF(pvt->mself->super_rcnt); |
219 | 31.7k | RTPP_OBJ_DTOR_ATTACH_RC_s(rtps_c, pvt->mself->super_rcnt); |
220 | 31.7k | RTPP_OBJ_DTOR_ATTACH_RC_s(rtps_c, rtps_c->dtls_conn->rcnt); |
221 | 31.7k | RTPP_OBJ_DTOR_ATTACH_s(rtps_c, GET_SMETHOD(rtps_c->dtls_conn, godead), |
222 | 31.7k | rtps_c->dtls_conn); |
223 | 31.7k | rtps_c->mself = pvt->mself; |
224 | 31.7k | return (rtps_c); |
225 | 0 | e1: |
226 | 0 | mod_free(rtps_c); |
227 | 0 | e0: |
228 | 0 | return (NULL); |
229 | 0 | } |
230 | | |
231 | | static int |
232 | | rtpp_dtls_gw_setup_sender(struct rtpp_module_priv *pvt, |
233 | | struct rtpp_session *spa, struct rtpp_stream *dtls_strmp) |
234 | 9.59k | { |
235 | 9.59k | int sidx, lport; |
236 | 9.59k | struct rtpp_socket *fd, *fds[2]; |
237 | | |
238 | 9.59k | fd = CALL_SMETHOD(dtls_strmp, get_skt, HEREVAL); |
239 | 9.59k | if (fd != NULL) { |
240 | 1.04k | RTPP_OBJ_DECREF(fd); |
241 | 1.04k | return (0); |
242 | 1.04k | } |
243 | | |
244 | 8.55k | if (spa->rtp->stream[0] == dtls_strmp) { |
245 | 0 | sidx = 0; |
246 | 8.55k | } else if (spa->rtp->stream[1] == dtls_strmp) { |
247 | 8.55k | sidx = 1; |
248 | 8.55k | } else { |
249 | 0 | abort(); |
250 | 0 | } |
251 | | |
252 | 8.55k | if (rtpp_create_listener(pvt->cfsp, dtls_strmp->laddr->addr, &lport, fds, |
253 | 8.55k | dtls_strmp->tos) == -1) |
254 | 66 | return (-1); |
255 | 8.55k | CALL_SMETHOD(pvt->cfsp->sessinfo, append, spa, sidx, fds); |
256 | 8.49k | CALL_METHOD(pvt->cfsp->rtpp_proc_cf, nudge); |
257 | 8.49k | RTPP_OBJ_DECREF(fds[0]); |
258 | 8.49k | RTPP_OBJ_DECREF(fds[1]); |
259 | 8.49k | dtls_strmp->port = lport; |
260 | 8.49k | spa->rtcp->stream[sidx]->port = lport + 1; |
261 | 8.49k | if (spa->complete == 0) { |
262 | 8.42k | CALL_SMETHOD(pvt->cfsp->rtpp_stats, updatebyname, "nsess_complete", 1); |
263 | 8.42k | CALL_SMETHOD(spa->rtp->stream[0]->ttl, reset_with, |
264 | 8.42k | pvt->cfsp->max_ttl); |
265 | 8.42k | CALL_SMETHOD(spa->rtp->stream[1]->ttl, reset_with, |
266 | 8.42k | pvt->cfsp->max_ttl); |
267 | 8.42k | } |
268 | 8.49k | spa->complete = 1; |
269 | 8.49k | return (0); |
270 | 8.55k | } |
271 | | |
272 | | enum rdg_cmd {RDG_CMD_A, RDG_CMD_P, RDG_CMD_S, RDG_CMD_D, RDG_CMD_U, RDG_CMD_Q}; |
273 | | |
274 | | static int |
275 | | rtpp_dtls_gw_handle_command(struct rtpp_module_priv *pvt, |
276 | | const struct rtpp_subc_ctx *ctxp) |
277 | 50.0k | { |
278 | 50.0k | struct dtls_gw_stream_cfg *rtps_c; |
279 | 50.0k | enum rtpp_dtls_mode my_mode; |
280 | 50.0k | struct rdc_peer_spec rdfs, *rdfsp; |
281 | 50.0k | const rtpp_str_t * argv = rtpp_str_fix(&ctxp->subc_args->v[1]); |
282 | 50.0k | int argc = ctxp->subc_args->c - 1; |
283 | 50.0k | struct rtpp_stream *dtls_strmp; |
284 | 50.0k | int rlen; |
285 | 50.0k | char *rcp; |
286 | 50.0k | enum rdg_cmd rdg_cmd; |
287 | | |
288 | 50.0k | if (argc != 1 && argc != 3 && argc != 4) { |
289 | 434 | RTPP_LOG(pvt->mself->log, RTPP_LOG_ERR, "expected 1, 3 or 4 parameters: %d", |
290 | 434 | argc); |
291 | 434 | return (-1); |
292 | 434 | } |
293 | | |
294 | 49.6k | switch (argv[0].s[0] | argv[0].s[1]) { |
295 | 4.02k | case 'a': |
296 | 14.9k | case 'A': |
297 | 14.9k | if (argc != 3 && argc != 4) |
298 | 80 | goto invalmode; |
299 | 14.9k | rdfs.peer_mode = RTPP_DTLS_ACTIVE; |
300 | 14.9k | rdg_cmd = RDG_CMD_A; |
301 | 14.9k | break; |
302 | | |
303 | 4.15k | case 'p': |
304 | 9.94k | case 'P': |
305 | 9.94k | if (argc != 3 && argc != 4) |
306 | 263 | goto invalmode; |
307 | 9.68k | rdfs.peer_mode = RTPP_DTLS_PASSIVE; |
308 | 9.68k | rdg_cmd = RDG_CMD_P; |
309 | 9.68k | break; |
310 | | |
311 | 73 | case 'q': |
312 | 368 | case 'Q': |
313 | 368 | if (argc != 1) |
314 | 202 | goto invalmode; |
315 | 166 | rdg_cmd = RDG_CMD_Q; |
316 | 166 | break; |
317 | | |
318 | 2.50k | case 's': |
319 | 20.2k | case 'S': |
320 | 20.2k | if (argc != 1) |
321 | 99 | goto invalmode; |
322 | 20.1k | rdg_cmd = RDG_CMD_S; |
323 | 20.1k | break; |
324 | | |
325 | 1.54k | case 'd': |
326 | 1.89k | case 'D': |
327 | 1.89k | if (argc != 1) |
328 | 70 | goto invalmode; |
329 | 1.82k | rdg_cmd = RDG_CMD_D; |
330 | 1.82k | break; |
331 | | |
332 | 1.29k | case 'u': |
333 | 1.62k | case 'U': |
334 | 1.62k | if (argc != 1) |
335 | 262 | goto invalmode; |
336 | 1.36k | rdg_cmd = RDG_CMD_U; |
337 | 1.36k | break; |
338 | | |
339 | 531 | default: |
340 | 531 | goto invalmode; |
341 | 49.6k | } |
342 | | |
343 | 48.1k | switch (rdg_cmd) { |
344 | 14.9k | case RDG_CMD_A: |
345 | 24.5k | case RDG_CMD_P: |
346 | 24.5k | if (argv[1].len > sizeof(rdfs.alg_buf)) |
347 | 89 | goto invalalg; |
348 | 173k | for (int i = 0; i < argv[1].len; i++) { |
349 | 149k | rdfs.alg_buf[i] = argv[1].s[i]; |
350 | 149k | if (rdfs.alg_buf[i] >= 'a') |
351 | 48.6k | rdfs.alg_buf[i] -= ('a' - 'A'); |
352 | 149k | } |
353 | 24.5k | rdfs.algorithm.len = argv[1].len; |
354 | 24.5k | rdfs.algorithm.s = rdfs.alg_buf; |
355 | 24.5k | rdfs.fingerprint = &argv[2]; |
356 | 24.5k | rdfs.ssrc = (argc == 4) ? &argv[3] : NULL; |
357 | 24.5k | rdfsp = &rdfs; |
358 | | /* Fallthrough */ |
359 | 24.6k | case RDG_CMD_Q: |
360 | 26.4k | case RDG_CMD_D: |
361 | 26.4k | dtls_strmp = ctxp->env->strmp_in; |
362 | 26.4k | break; |
363 | | |
364 | 20.1k | case RDG_CMD_S: |
365 | 20.1k | rdfsp = NULL; |
366 | | /* Fallthrough */ |
367 | 21.5k | case RDG_CMD_U: |
368 | 21.5k | dtls_strmp = ctxp->env->strmp_out; |
369 | 21.5k | break; |
370 | 48.1k | } |
371 | | |
372 | 48.0k | struct packet_processor_if dtls_in_poi; |
373 | | |
374 | 48.0k | int lookup_res = CALL_SMETHOD(dtls_strmp->pproc_manager, lookup, pvt, &dtls_in_poi); |
375 | 48.0k | if (lookup_res != 0) { |
376 | 15.8k | rtps_c = dtls_in_poi.arg; |
377 | 15.8k | } |
378 | | |
379 | | /* A query must not allocate a connection or alter its mode/fingerprint. */ |
380 | 48.0k | if (rdg_cmd == RDG_CMD_Q) { |
381 | 166 | strlcpy(ctxp->resp->buf_t, lookup_res == 0 ? "none" : |
382 | 166 | CALL_SMETHOD(rtps_c->dtls_conn, getstate), sizeof(ctxp->resp->buf_t)); |
383 | 166 | if (lookup_res == 0) |
384 | 88 | return (0); |
385 | 78 | goto out; |
386 | 166 | } |
387 | | |
388 | 47.8k | if (rdg_cmd == RDG_CMD_D || rdg_cmd == RDG_CMD_U) { |
389 | 3.19k | if (lookup_res == 0) { |
390 | 397 | return (-1); |
391 | 397 | } |
392 | 3.19k | CALL_SMETHOD(dtls_strmp->pproc_manager, unreg, pvt); |
393 | 2.79k | CALL_SMETHOD(dtls_strmp->pproc_manager->reverse, unreg, pvt + 1); |
394 | 2.79k | goto out; |
395 | 3.19k | } |
396 | | |
397 | 44.6k | if (lookup_res == 0) { |
398 | 31.7k | rtps_c = dtls_gw_data_ctor(pvt, dtls_strmp); |
399 | 31.7k | if (rtps_c == NULL) { |
400 | 0 | return (-1); |
401 | 0 | } |
402 | 31.7k | } |
403 | 44.6k | if (rdfsp != NULL && rdfs.peer_mode == RTPP_DTLS_PASSIVE) { |
404 | 9.59k | if (rtpp_dtls_gw_setup_sender(pvt, ctxp->env->sessp, dtls_strmp) != 0) { |
405 | 66 | goto e0; |
406 | 66 | } |
407 | 9.59k | } |
408 | 44.6k | my_mode = CALL_SMETHOD(rtps_c->dtls_conn, setmode, rdfsp); |
409 | 44.6k | if (my_mode == RTPP_DTLS_MODERR) { |
410 | 5.17k | goto e0; |
411 | 5.17k | } |
412 | 39.4k | if (lookup_res == 0) { |
413 | 26.6k | dtls_in_poi = (struct packet_processor_if){ |
414 | 26.6k | .descr = "dtls (srtp->rtp)", |
415 | 26.6k | .taste = rtpp_dtls_gw_taste_encrypted, |
416 | 26.6k | .enqueue = rtpp_dtls_gw_enqueue, |
417 | 26.6k | .key = pvt, |
418 | 26.6k | .arg = rtps_c, |
419 | 26.6k | .rcnt = rtps_c->rcnt |
420 | 26.6k | }; |
421 | 26.6k | if (CALL_SMETHOD(dtls_strmp->pproc_manager, reg, PPROC_ORD_DECRYPT, &dtls_in_poi) < 0) { |
422 | 0 | goto e0; |
423 | 0 | } |
424 | 26.6k | const struct packet_processor_if dtls_out_poi = { |
425 | 26.6k | .descr = "dtls (rtp->srtp)", |
426 | 26.6k | .taste = rtpp_dtls_gw_taste_plain, |
427 | 26.6k | .enqueue = rtpp_dtls_gw_enqueue, |
428 | 26.6k | .key = pvt + 1, |
429 | 26.6k | .arg = rtps_c, |
430 | 26.6k | .rcnt = rtps_c->rcnt |
431 | 26.6k | }; |
432 | 26.6k | if (CALL_SMETHOD(dtls_strmp->pproc_manager->reverse, reg, PPROC_ORD_ENCRYPT, &dtls_out_poi) < 0) { |
433 | 0 | goto e1; |
434 | 0 | } |
435 | 26.6k | } |
436 | 39.4k | if (rdfsp == NULL) { |
437 | 20.1k | rcp = ctxp->resp->buf_t; |
438 | 20.1k | rlen = sizeof(ctxp->resp->buf_t); |
439 | | |
440 | 20.1k | switch (my_mode) { |
441 | 9.30k | case RTPP_DTLS_ACTPASS: |
442 | 9.30k | strlcpy(rcp, "actpass ", rlen); |
443 | 9.30k | rcp += strlen("actpass "); |
444 | 9.30k | rlen -= strlen("actpass "); |
445 | 9.30k | break; |
446 | | |
447 | 66 | case RTPP_DTLS_ACTIVE: |
448 | 66 | strlcpy(rcp, "active ", rlen); |
449 | 66 | rcp += strlen("active "); |
450 | 66 | rlen -= strlen("active "); |
451 | 66 | break; |
452 | | |
453 | 10.8k | case RTPP_DTLS_PASSIVE: |
454 | 10.8k | strlcpy(rcp, "passive ", rlen); |
455 | 10.8k | rcp += strlen("passive "); |
456 | 10.8k | rlen -= strlen("passive "); |
457 | 10.8k | break; |
458 | | |
459 | 0 | default: |
460 | 0 | abort(); |
461 | 20.1k | } |
462 | | |
463 | 20.1k | strlcpy(rcp, pvt->dtls_ctx->fingerprint, rlen); |
464 | 20.1k | } |
465 | 42.3k | out: |
466 | 42.3k | RTPP_OBJ_DECREF(rtps_c); |
467 | 42.3k | return (0); |
468 | | |
469 | 89 | invalalg: |
470 | 89 | RTPP_LOG(pvt->mself->log, RTPP_LOG_ERR, "invalid algorithm: \"%s\"", |
471 | 89 | argv[1].s); |
472 | 89 | return (-1); |
473 | 1.50k | invalmode: |
474 | 1.50k | RTPP_LOG(pvt->mself->log, RTPP_LOG_ERR, "invalid mode: \"%s\"", |
475 | 1.50k | argv[0].s); |
476 | 1.50k | return (-1); |
477 | 0 | e1: |
478 | 0 | CALL_SMETHOD(dtls_strmp->pproc_manager, unreg, pvt); |
479 | 5.24k | e0: |
480 | 5.24k | RTPP_OBJ_DECREF(rtps_c); |
481 | 5.24k | return (-1); |
482 | 0 | } |
483 | | |
484 | | static int |
485 | | rtpp_dtls_gw_taste_encrypted(struct pkt_proc_ctx *pktxp) |
486 | 150k | { |
487 | 150k | struct dtls_gw_stream_cfg *rtps_c; |
488 | 150k | static __thread struct rtpp_dtls_gw_aux dtls_in = {.direction = DTLS_IN}; |
489 | 150k | static __thread struct rtpp_dtls_gw_aux strp_in = {.direction = SRTP_IN}; |
490 | 150k | struct rtpp_dtls_gw_aux *rdgap; |
491 | | |
492 | 150k | if (!rtpp_is_dtls_tst(pktxp)) |
493 | 101k | rdgap = &strp_in; |
494 | 49.3k | else |
495 | 49.3k | rdgap = &dtls_in; |
496 | 150k | rtps_c = pktxp->pproc->arg; |
497 | 150k | rdgap->dtls_conn = rtps_c->dtls_conn; |
498 | 150k | rdgap->mself = rtps_c->mself; |
499 | 150k | pktxp->auxp = rdgap; |
500 | 150k | return (1); |
501 | 150k | } |
502 | | |
503 | | static int |
504 | | rtpp_dtls_gw_taste_plain(struct pkt_proc_ctx *pktxp) |
505 | 200k | { |
506 | 200k | struct dtls_gw_stream_cfg *rtps_c; |
507 | 200k | static __thread struct rtpp_dtls_gw_aux rtp_out = {.direction = RTP_OUT}; |
508 | | |
509 | 200k | if (pktxp->strmp_out == NULL) |
510 | 0 | return (0); |
511 | 200k | rtps_c = pktxp->pproc->arg; |
512 | 200k | rtp_out.dtls_conn = rtps_c->dtls_conn; |
513 | 200k | rtp_out.mself = rtps_c->mself; |
514 | 200k | pktxp->auxp = &rtp_out; |
515 | 200k | return (1); |
516 | 200k | } |
517 | | |
518 | | static struct pproc_act |
519 | | rtpp_dtls_gw_enqueue(const struct pkt_proc_ctx *pktxp) |
520 | 350k | { |
521 | 350k | struct rtpp_dtls_gw_aux *edata; |
522 | 350k | struct rtpp_wi *wi; |
523 | 350k | struct wipkt *wip; |
524 | | |
525 | 350k | edata = (struct rtpp_dtls_gw_aux *)pktxp->auxp; |
526 | 350k | wi = rtpp_wi_malloc_udata((void **)&wip, sizeof(struct wipkt)); |
527 | 350k | if (wi == NULL) |
528 | 0 | return (PPROC_ACT_DROP); |
529 | 350k | wip->edata = *edata; |
530 | 350k | RTPP_OBJ_BORROW_s(wi, edata->dtls_conn); |
531 | 350k | wip->pktx = *pktxp; |
532 | 350k | wip->pktx.rsp = NULL; |
533 | 350k | RTPP_OBJ_BORROW_s(wi, pktxp->strmp_in); |
534 | 350k | if (pktxp->strmp_out != NULL) |
535 | 350k | RTPP_OBJ_BORROW_s(wi, pktxp->strmp_out); |
536 | 350k | if (rtpp_queue_put_item(wi, edata->mself->wthr.mod_q) != 0) { |
537 | 23.8k | RTPP_OBJ_DECREF(wi); |
538 | 23.8k | return (PPROC_ACT_DROP); |
539 | 23.8k | } |
540 | | |
541 | 327k | return (PPROC_ACT_TAKE); |
542 | 350k | } |
543 | | |
544 | | static struct rtpp_module_priv * |
545 | | rtpp_dtls_gw_ctor(const struct rtpp_cfg *cfsp, struct rtpp_minfo *mself) |
546 | 6 | { |
547 | 6 | struct rtpp_module_priv *pvt; |
548 | 6 | static int srtp_inited = 0; |
549 | | |
550 | 6 | pvt = mod_zmalloc(sizeof(struct rtpp_module_priv)); |
551 | 6 | if (pvt == NULL) { |
552 | 0 | goto e0; |
553 | 0 | } |
554 | 6 | pvt->dtls_ctx = rtpp_dtls_ctor(cfsp, mself); |
555 | 6 | if (pvt->dtls_ctx == NULL) { |
556 | 0 | goto e1; |
557 | 0 | } |
558 | 6 | if (!srtp_inited && srtp_init() != 0) { |
559 | 0 | goto e2; |
560 | 0 | } |
561 | 6 | srtp_inited = 1; |
562 | 6 | pvt->cfsp = cfsp; |
563 | 6 | pvt->mself = mself; |
564 | 6 | return (pvt); |
565 | 0 | e2: |
566 | 0 | RTPP_OBJ_DECREF(pvt->dtls_ctx); |
567 | 0 | e1: |
568 | 0 | mod_free(pvt); |
569 | 0 | e0: |
570 | 0 | return (NULL); |
571 | 0 | } |
572 | | |
573 | | static void |
574 | | rtpp_dtls_gw_dtor(struct rtpp_module_priv *pvt) |
575 | 6 | { |
576 | | |
577 | 6 | srtp_shutdown(); |
578 | 6 | RTPP_OBJ_DECREF(pvt->dtls_ctx); |
579 | 6 | mod_free(pvt); |
580 | 6 | return; |
581 | 6 | } |