blob: b0fa56c2443f5bb05269ab579c3967c4073986f4 [file] [log] [blame]
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001/* SIP extension for IP connection tracking.
2 *
Ravinder Konka4eafdb92016-01-18 19:35:52 +05303 * Copyright (c) 2015,2017-2019 The Linux Foundation. All rights reserved.
Patrick McHardy9fafcd72006-12-02 22:09:57 -08004 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
5 * based on RR's ip_conntrack_ftp.c and other modules.
Patrick McHardyf49e1aa2008-03-25 20:27:05 -07006 * (C) 2007 United Security Providers
7 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
Patrick McHardy9fafcd72006-12-02 22:09:57 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Pablo Neira Ayusoad6d9502016-01-03 22:41:24 +010014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Patrick McHardy9fafcd72006-12-02 22:09:57 -080016#include <linux/module.h>
17#include <linux/ctype.h>
18#include <linux/skbuff.h>
19#include <linux/inet.h>
20#include <linux/in.h>
21#include <linux/udp.h>
Patrick McHardyf5b321b2010-02-11 12:26:19 +010022#include <linux/tcp.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080023#include <linux/netfilter.h>
Mohammed Javidf4530ef2017-11-20 20:15:46 +053024#include <net/tcp.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080025#include <net/netfilter/nf_conntrack.h>
Patrick McHardy9467ee32008-03-25 20:24:04 -070026#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080027#include <net/netfilter/nf_conntrack_expect.h>
28#include <net/netfilter/nf_conntrack_helper.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010029#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardy9fafcd72006-12-02 22:09:57 -080030#include <linux/netfilter/nf_conntrack_sip.h>
Mohammed Javidf4530ef2017-11-20 20:15:46 +053031#include <net/netfilter/nf_nat.h>
32#include <net/netfilter/nf_nat_l3proto.h>
33#include <net/netfilter/nf_nat_l4proto.h>
34#include <net/netfilter/nf_queue.h>
35
Patrick McHardy9fafcd72006-12-02 22:09:57 -080036
Patrick McHardy9fafcd72006-12-02 22:09:57 -080037MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
39MODULE_DESCRIPTION("SIP connection tracking helper");
40MODULE_ALIAS("ip_conntrack_sip");
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +010041MODULE_ALIAS_NFCT_HELPER("sip");
Patrick McHardy9fafcd72006-12-02 22:09:57 -080042
43#define MAX_PORTS 8
44static unsigned short ports[MAX_PORTS];
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -080045static unsigned int ports_c;
Patrick McHardy9fafcd72006-12-02 22:09:57 -080046module_param_array(ports, ushort, &ports_c, 0400);
47MODULE_PARM_DESC(ports, "port numbers of SIP servers");
48
49static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
50module_param(sip_timeout, uint, 0600);
51MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
52
Patrick McHardy0f32a402008-03-25 20:25:13 -070053static int sip_direct_signalling __read_mostly = 1;
54module_param(sip_direct_signalling, int, 0600);
55MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
56 "only (default 1)");
57
holger@eitzenberger.org180cf722013-09-30 17:07:28 +020058const struct nf_nat_sip_hooks *nf_nat_sip_hooks;
59EXPORT_SYMBOL_GPL(nf_nat_sip_hooks);
Tyler Weard2aeec02013-09-20 12:12:07 -070060static struct ctl_table_header *sip_sysctl_header;
61static unsigned int nf_ct_disable_sip_alg;
Tyler Wear6cd63b02014-01-13 12:33:17 -080062static int sip_direct_media = 1;
Mohammed Javidf4530ef2017-11-20 20:15:46 +053063static unsigned int nf_ct_enable_sip_segmentation;
64static int packet_count;
65static
66int proc_sip_segment(struct ctl_table *ctl, int write,
67 void __user *buffer, size_t *lenp, loff_t *ppos);
68
Tyler Weard2aeec02013-09-20 12:12:07 -070069static struct ctl_table sip_sysctl_tbl[] = {
70 {
71 .procname = "nf_conntrack_disable_sip_alg",
72 .data = &nf_ct_disable_sip_alg,
73 .maxlen = sizeof(unsigned int),
74 .mode = 0644,
75 .proc_handler = proc_dointvec,
76 },
Tyler Wear6cd63b02014-01-13 12:33:17 -080077 {
78 .procname = "nf_conntrack_sip_direct_media",
79 .data = &sip_direct_media,
80 .maxlen = sizeof(int),
81 .mode = 0644,
82 .proc_handler = proc_dointvec,
83 },
Mohammed Javidf4530ef2017-11-20 20:15:46 +053084 {
85 .procname = "nf_conntrack_enable_sip_segmentation",
86 .data = &nf_ct_enable_sip_segmentation,
87 .maxlen = sizeof(unsigned int),
88 .mode = 0644,
89 .proc_handler = proc_sip_segment,
90 },
Tyler Weard2aeec02013-09-20 12:12:07 -070091 {}
92};
93
Mohammed Javidf4530ef2017-11-20 20:15:46 +053094static unsigned int (*nf_nat_sip_hook)
95 (struct sk_buff *skb,
96 unsigned int protoff,
97 unsigned int dataoff,
98 const char **dptr,
99 unsigned int *datalen)
100 __read_mostly;
101EXPORT_SYMBOL(nf_nat_sip_hook);
102static void sip_calculate_parameters(s16 *diff, s16 *tdiff,
103 unsigned int *dataoff, const char **dptr,
104 unsigned int *datalen,
105 unsigned int msglen, unsigned int origlen)
106{
107 *diff = msglen - origlen;
108 *tdiff += *diff;
109 *dataoff += msglen;
110 *dptr += msglen;
111 *datalen = *datalen + *diff - msglen;
112}
113
114static void sip_update_params(enum ip_conntrack_dir dir,
115 unsigned int *msglen, unsigned int *origlen,
116 const char **dptr, unsigned int *datalen,
117 bool skb_is_combined, struct nf_conn *ct)
118{
119 if (skb_is_combined) {
120 /* The msglen of first skb has the total msg length of
121 * the two fragments. hence after combining,we update
122 * the msglen to that of the msglen of first skb
123 */
124 *msglen = (dir == IP_CT_DIR_ORIGINAL) ?
125 ct->segment.msg_length[0] : ct->segment.msg_length[1];
126 *origlen = *msglen;
127 *dptr = ct->dptr_prev;
128 *datalen = *msglen;
129 }
130}
131
132/* This function is to save all the information of the first segment
133 * that will be needed for combining the two segments
134 */
135static bool sip_save_segment_info(struct nf_conn *ct, struct sk_buff *skb,
136 unsigned int msglen, unsigned int datalen,
137 const char *dptr,
138 enum ip_conntrack_info ctinfo)
139{
140 enum ip_conntrack_dir dir = IP_CT_DIR_MAX;
141 bool skip = false;
142
143 /* one set of information is saved per direction ,also only one segment
144 * per direction is queued based on the assumption that after the first
145 * complete message leaves the kernel, only then the next fragmented
146 * segment will reach the kernel
147 */
148 dir = CTINFO2DIR(ctinfo);
149 if (dir == IP_CT_DIR_ORIGINAL) {
150 /* here we check if there is already an element queued for this
151 * direction, in that case we do not queue the next element,we
152 * make skip 1.ideally this scenario should never be hit
153 */
154 if (ct->sip_original_dir == 1) {
155 skip = true;
156 } else {
157 ct->segment.msg_length[0] = msglen;
158 ct->segment.data_len[0] = datalen;
159 ct->segment.skb_len[0] = skb->len;
160 ct->dptr_prev = dptr;
161 ct->sip_original_dir = 1;
162 skip = false;
163 }
164 } else {
165 if (ct->sip_reply_dir == 1) {
166 skip = true;
167 } else {
168 if (ct->sip_reply_dir == 1) {
169 skip = true;
170 } else {
171 ct->segment.msg_length[1] = msglen;
172 ct->segment.data_len[1] = datalen;
173 ct->segment.skb_len[1] = skb->len;
174 ct->dptr_prev = dptr;
175 ct->sip_reply_dir = 1;
176 skip = false;
177 }
178 }
179 }
180 return skip;
181}
182
183static struct sip_list *sip_coalesce_segments(struct nf_conn *ct,
184 struct sk_buff **skb_ref,
185 unsigned int dataoff,
186 struct sk_buff **combined_skb_ref,
187 bool *skip_sip_process,
188 bool do_not_process,
189 enum ip_conntrack_info ctinfo,
190 bool *success)
191
192{
193 struct list_head *list_trav_node;
194 struct list_head *list_backup_node;
195 struct nf_conn *ct_list;
196 enum ip_conntrack_info ctinfo_list;
197 enum ip_conntrack_dir dir_list;
198 enum ip_conntrack_dir dir = IP_CT_DIR_MAX;
199 const struct tcphdr *th_old;
200 unsigned int prev_data_len;
201 unsigned int seq_no, seq_old, exp_seq_no;
202 const struct tcphdr *th_new;
203 bool fragstolen = false;
204 int delta_truesize = 0;
205 struct sip_list *sip_entry = NULL;
206
207 th_new = (struct tcphdr *)(skb_network_header(*skb_ref) +
208 ip_hdrlen(*skb_ref));
209 seq_no = ntohl(th_new->seq);
210
211 if (ct) {
212 dir = CTINFO2DIR(ctinfo);
213 /* traverse the list it would have 1 or 2 elements. 1 element
214 * per direction at max
215 */
216 list_for_each_safe(list_trav_node, list_backup_node,
217 &ct->sip_segment_list){
218 sip_entry = list_entry(list_trav_node, struct sip_list,
219 list);
220 ct_list = nf_ct_get(sip_entry->entry->skb,
221 &ctinfo_list);
222 dir_list = CTINFO2DIR(ctinfo_list);
223 /* take an element and check if its direction matches
224 * with the current one
225 */
226 if (dir_list == dir) {
227 /* once we have the two elements to be combined
228 * we do another check. match the next expected
229 * seq no of the packet in the list with the
230 * seq no of the current packet.this is to be
231 * protected against out of order fragments
232 */
233 th_old = ((struct tcphdr *)(skb_network_header
234 (sip_entry->entry->skb) +
235 ip_hdrlen(sip_entry->entry->skb)));
236
237 prev_data_len = (dir == IP_CT_DIR_ORIGINAL) ?
238 ct->segment.data_len[0] :
239 ct->segment.data_len[1];
240 seq_old = (ntohl(th_old->seq));
241 exp_seq_no = seq_old + prev_data_len;
242
243 if (exp_seq_no == seq_no) {
244 /* Found packets to be combined.Pull
245 * header from second skb when
246 * preparing combined skb.This shifts
247 * the second skb start pointer to its
248 * data that was initially at the start
249 * of its headers.This so that the
250 * combined skb has the tcp ip headerof
251 * the first skb followed by the data
252 * of first skb followed by the data
253 * of second skb.
254 */
255 skb_pull(*skb_ref, dataoff);
256 if (skb_try_coalesce(
257 sip_entry->entry->skb,
258 *skb_ref, &fragstolen,
259 &delta_truesize)) {
260 pr_debug(" Combining segments\n");
261 *combined_skb_ref =
262 sip_entry->entry->skb;
263 *success = true;
264 list_del(list_trav_node);
265 } else{
266 skb_push(*skb_ref, dataoff);
267 }
268 }
269 } else if (do_not_process) {
270 *skip_sip_process = true;
271 }
272 }
273 }
274 return sip_entry;
275}
276
277static void recalc_header(struct sk_buff *skb, unsigned int skblen,
278 unsigned int oldlen, unsigned int protoff)
279{
280 unsigned int datalen;
281 struct tcphdr *tcph;
282 const struct nf_nat_l3proto *l3proto;
283
284 /* here we recalculate ip and tcp headers */
285 if (nf_ct_l3num((struct nf_conn *)skb->nfct) == NFPROTO_IPV4) {
286 /* fix IP hdr checksum information */
287 ip_hdr(skb)->tot_len = htons(skblen);
288 ip_send_check(ip_hdr(skb));
289 } else {
290 ipv6_hdr(skb)->payload_len =
291 htons(skblen - sizeof(struct ipv6hdr));
292 }
293 datalen = skb->len - protoff;
294 tcph = (struct tcphdr *)((void *)skb->data + protoff);
295 l3proto = __nf_nat_l3proto_find(nf_ct_l3num
296 ((struct nf_conn *)skb->nfct));
297 l3proto->csum_recalc(skb, IPPROTO_TCP, tcph, &tcph->check,
298 datalen, oldlen);
299}
300
301void (*nf_nat_sip_seq_adjust_hook)
302 (struct sk_buff *skb,
303 unsigned int protoff,
304 s16 off);
305
306static unsigned int (*nf_nat_sip_expect_hook)
307 (struct sk_buff *skb,
308 unsigned int protoff,
309 unsigned int dataoff,
310 const char **dptr,
311 unsigned int *datalen,
312 struct nf_conntrack_expect *exp,
313 unsigned int matchoff,
314 unsigned int matchlen)
315 __read_mostly;
316EXPORT_SYMBOL(nf_nat_sip_expect_hook);
317
318static unsigned int (*nf_nat_sdp_addr_hook)
319 (struct sk_buff *skb,
320 unsigned int protoff,
321 unsigned int dataoff,
322 const char **dptr,
323 unsigned int *datalen,
324 unsigned int sdpoff,
325 enum sdp_header_types type,
326 enum sdp_header_types term,
327 const union nf_inet_addr *addr)
328 __read_mostly;
329EXPORT_SYMBOL(nf_nat_sdp_addr_hook);
330
331static unsigned int (*nf_nat_sdp_port_hook)
332 (struct sk_buff *skb,
333 unsigned int protoff,
334 unsigned int dataoff,
335 const char **dptr,
336 unsigned int *datalen,
337 unsigned int matchoff,
338 unsigned int matchlen,
339 u_int16_t port) __read_mostly;
340EXPORT_SYMBOL(nf_nat_sdp_port_hook);
341
342static unsigned int (*nf_nat_sdp_session_hook)
343 (struct sk_buff *skb,
344 unsigned int protoff,
345 unsigned int dataoff,
346 const char **dptr,
347 unsigned int *datalen,
348 unsigned int sdpoff,
349 const union nf_inet_addr *addr)
350 __read_mostly;
351EXPORT_SYMBOL(nf_nat_sdp_session_hook);
352
353static unsigned int (*nf_nat_sdp_media_hook)
354 (struct sk_buff *skb,
355 unsigned int protoff,
356 unsigned int dataoff,
357 const char **dptr,
358 unsigned int *datalen,
359 struct nf_conntrack_expect *rtp_exp,
360 struct nf_conntrack_expect *rtcp_exp,
361 unsigned int mediaoff,
362 unsigned int medialen,
363 union nf_inet_addr *rtp_addr)
364 __read_mostly;
365EXPORT_SYMBOL(nf_nat_sdp_media_hook);
366
Patrick McHardyac367742008-03-25 20:18:40 -0700367static int string_len(const struct nf_conn *ct, const char *dptr,
368 const char *limit, int *shift)
369{
370 int len = 0;
371
372 while (dptr < limit && isalpha(*dptr)) {
373 dptr++;
374 len++;
375 }
376 return len;
377}
378
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530379static int nf_sip_enqueue_packet(struct nf_queue_entry *entry,
380 unsigned int queuenum)
381{
382 enum ip_conntrack_info ctinfo_list;
383 struct nf_conn *ct_temp;
384 struct sip_list *node = kzalloc(sizeof(*node),
385 GFP_ATOMIC | __GFP_NOWARN);
386 if (!node)
387 return XT_CONTINUE;
388
389 ct_temp = nf_ct_get(entry->skb, &ctinfo_list);
390 node->entry = entry;
391 list_add(&node->list, &ct_temp->sip_segment_list);
392 return 0;
393}
394
Ravinder Konka4eafdb92016-01-18 19:35:52 +0530395static void nf_hook_drop_sip(struct net *net, const struct nf_hook_entry *hook)
396{
397}
398
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530399static const struct nf_queue_handler nf_sip_qh = {
400 .outfn = &nf_sip_enqueue_packet,
Ravinder Konka4eafdb92016-01-18 19:35:52 +0530401 .nf_hook_drop = &nf_hook_drop_sip,
Mohammed Javidf4530ef2017-11-20 20:15:46 +0530402};
403
404static
405int proc_sip_segment(struct ctl_table *ctl, int write,
406 void __user *buffer, size_t *lenp, loff_t *ppos)
407{
408 int ret;
409
410 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
411 if (nf_ct_enable_sip_segmentation) {
412 pr_debug("registering queue handler\n");
413 nf_register_queue_handler(&init_net, &nf_sip_qh);
414 } else {
415 pr_debug("de-registering queue handler\n");
416 nf_unregister_queue_handler(&init_net);
417 }
418 return ret;
419}
420
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800421static int digits_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800422 const char *limit, int *shift)
423{
424 int len = 0;
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700425 while (dptr < limit && isdigit(*dptr)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800426 dptr++;
427 len++;
428 }
429 return len;
430}
431
Simon Horman001985b2010-08-22 21:37:51 +0900432static int iswordc(const char c)
433{
434 if (isalnum(c) || c == '!' || c == '"' || c == '%' ||
Marco Angaronif0608ce2016-08-30 18:48:24 +0200435 (c >= '(' && c <= '+') || c == ':' || c == '<' || c == '>' ||
Simon Horman001985b2010-08-22 21:37:51 +0900436 c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' ||
Marco Angaronif0608ce2016-08-30 18:48:24 +0200437 c == '{' || c == '}' || c == '~' || (c >= '-' && c <= '/') ||
438 c == '\'')
Simon Horman001985b2010-08-22 21:37:51 +0900439 return 1;
440 return 0;
441}
442
443static int word_len(const char *dptr, const char *limit)
444{
445 int len = 0;
446 while (dptr < limit && iswordc(*dptr)) {
447 dptr++;
448 len++;
449 }
450 return len;
451}
452
453static int callid_len(const struct nf_conn *ct, const char *dptr,
454 const char *limit, int *shift)
455{
456 int len, domain_len;
457
458 len = word_len(dptr, limit);
459 dptr += len;
460 if (!len || dptr == limit || *dptr != '@')
461 return len;
462 dptr++;
463 len++;
464
465 domain_len = word_len(dptr, limit);
466 if (!domain_len)
467 return 0;
468 return len + domain_len;
469}
470
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700471/* get media type + port length */
472static int media_len(const struct nf_conn *ct, const char *dptr,
473 const char *limit, int *shift)
474{
475 int len = string_len(ct, dptr, limit, shift);
476
477 dptr += len;
478 if (dptr >= limit || *dptr != ' ')
479 return 0;
480 len++;
481 dptr++;
482
483 return len + digits_len(ct, dptr, limit, shift);
484}
485
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000486static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
487 const char **endp, union nf_inet_addr *addr,
488 const char *limit, bool delim)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800489{
490 const char *end;
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000491 int ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800492
Simon Horman5adbb9f2010-08-22 21:37:51 +0900493 if (!ct)
494 return 0;
495
Patrick McHardyfa913dd2008-04-14 11:15:45 +0200496 memset(addr, 0, sizeof(*addr));
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200497 switch (nf_ct_l3num(ct)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800498 case AF_INET:
499 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000500 if (ret == 0)
501 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800502 break;
503 case AF_INET6:
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000504 if (cp < limit && *cp == '[')
505 cp++;
506 else if (delim)
507 return 0;
508
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800509 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000510 if (ret == 0)
511 return 0;
512
513 if (end < limit && *end == ']')
514 end++;
515 else if (delim)
516 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800517 break;
518 default:
519 BUG();
520 }
521
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800522 if (endp)
523 *endp = end;
524 return 1;
525}
526
527/* skip ip address. returns its length. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800528static int epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800529 const char *limit, int *shift)
530{
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800531 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800532 const char *aux = dptr;
533
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000534 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700535 pr_debug("ip: %s parse failed.!\n", dptr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800536 return 0;
537 }
538
539 /* Port number */
540 if (*dptr == ':') {
541 dptr++;
542 dptr += digits_len(ct, dptr, limit, shift);
543 }
544 return dptr - aux;
545}
546
547/* get address length, skiping user info. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800548static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800549 const char *limit, int *shift)
550{
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700551 const char *start = dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800552 int s = *shift;
553
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800554 /* Search for @, but stop at the end of the line.
555 * We are inside a sip: URI, so we don't need to worry about
556 * continuation lines. */
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700557 while (dptr < limit &&
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800558 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800559 (*shift)++;
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800560 dptr++;
561 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800562
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700563 if (dptr < limit && *dptr == '@') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800564 dptr++;
565 (*shift)++;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700566 } else {
567 dptr = start;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800568 *shift = s;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700569 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800570
571 return epaddr_len(ct, dptr, limit, shift);
572}
573
Patrick McHardyac367742008-03-25 20:18:40 -0700574/* Parse a SIP request line of the form:
575 *
576 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
577 *
578 * and return the offset and length of the address contained in the Request-URI.
579 */
580int ct_sip_parse_request(const struct nf_conn *ct,
581 const char *dptr, unsigned int datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700582 unsigned int *matchoff, unsigned int *matchlen,
583 union nf_inet_addr *addr, __be16 *port)
Patrick McHardyac367742008-03-25 20:18:40 -0700584{
Patrick McHardy624f8b72008-03-25 20:19:30 -0700585 const char *start = dptr, *limit = dptr + datalen, *end;
Patrick McHardyac367742008-03-25 20:18:40 -0700586 unsigned int mlen;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700587 unsigned int p;
Patrick McHardyac367742008-03-25 20:18:40 -0700588 int shift = 0;
589
590 /* Skip method and following whitespace */
591 mlen = string_len(ct, dptr, limit, NULL);
592 if (!mlen)
593 return 0;
594 dptr += mlen;
595 if (++dptr >= limit)
596 return 0;
597
598 /* Find SIP URI */
Patrick McHardy54101f42010-02-11 12:23:12 +0100599 for (; dptr < limit - strlen("sip:"); dptr++) {
Patrick McHardyac367742008-03-25 20:18:40 -0700600 if (*dptr == '\r' || *dptr == '\n')
601 return -1;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700602 if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) {
Patrick McHardy54101f42010-02-11 12:23:12 +0100603 dptr += strlen("sip:");
Patrick McHardyac367742008-03-25 20:18:40 -0700604 break;
Patrick McHardy54101f42010-02-11 12:23:12 +0100605 }
Patrick McHardyac367742008-03-25 20:18:40 -0700606 }
Patrick McHardy624f8b72008-03-25 20:19:30 -0700607 if (!skp_epaddr_len(ct, dptr, limit, &shift))
Patrick McHardyac367742008-03-25 20:18:40 -0700608 return 0;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700609 dptr += shift;
610
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000611 if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
Patrick McHardy624f8b72008-03-25 20:19:30 -0700612 return -1;
613 if (end < limit && *end == ':') {
614 end++;
615 p = simple_strtoul(end, (char **)&end, 10);
616 if (p < 1024 || p > 65535)
617 return -1;
618 *port = htons(p);
619 } else
620 *port = htons(SIP_PORT);
621
622 if (end == dptr)
623 return 0;
624 *matchoff = dptr - start;
625 *matchlen = end - dptr;
Patrick McHardyac367742008-03-25 20:18:40 -0700626 return 1;
627}
628EXPORT_SYMBOL_GPL(ct_sip_parse_request);
629
Patrick McHardyea45f122008-03-25 20:18:57 -0700630/* SIP header parsing: SIP headers are located at the beginning of a line, but
631 * may span several lines, in which case the continuation lines begin with a
632 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
633 * CRLF, RFC 3261 allows only CRLF, we support both.
634 *
635 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
636 * whitespace and the values. Whitespace in this context means any amount of
637 * tabs, spaces and continuation lines, which are treated as a single whitespace
638 * character.
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700639 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800640 * Some headers may appear multiple times. A comma separated list of values is
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700641 * equivalent to multiple headers.
Patrick McHardyea45f122008-03-25 20:18:57 -0700642 */
643static const struct sip_header ct_sip_hdrs[] = {
Patrick McHardy30f33e62008-03-25 20:22:20 -0700644 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700645 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
646 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
647 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100648 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
649 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
Patrick McHardy0f32a402008-03-25 20:25:13 -0700650 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700651 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
Simon Horman001985b2010-08-22 21:37:51 +0900652 [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700653};
654
655static const char *sip_follow_continuation(const char *dptr, const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800656{
Patrick McHardyea45f122008-03-25 20:18:57 -0700657 /* Walk past newline */
658 if (++dptr >= limit)
659 return NULL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800660
Patrick McHardyea45f122008-03-25 20:18:57 -0700661 /* Skip '\n' in CR LF */
662 if (*(dptr - 1) == '\r' && *dptr == '\n') {
663 if (++dptr >= limit)
664 return NULL;
665 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800666
Patrick McHardyea45f122008-03-25 20:18:57 -0700667 /* Continuation line? */
668 if (*dptr != ' ' && *dptr != '\t')
669 return NULL;
670
671 /* skip leading whitespace */
672 for (; dptr < limit; dptr++) {
673 if (*dptr != ' ' && *dptr != '\t')
674 break;
675 }
676 return dptr;
677}
678
679static const char *sip_skip_whitespace(const char *dptr, const char *limit)
680{
681 for (; dptr < limit; dptr++) {
Marco Angaroni1bcabc82016-08-30 18:52:22 +0200682 if (*dptr == ' ' || *dptr == '\t')
Patrick McHardyea45f122008-03-25 20:18:57 -0700683 continue;
684 if (*dptr != '\r' && *dptr != '\n')
685 break;
686 dptr = sip_follow_continuation(dptr, limit);
Marco Angaroni68cb9fe2016-08-30 18:48:19 +0200687 break;
Patrick McHardyea45f122008-03-25 20:18:57 -0700688 }
689 return dptr;
690}
691
692/* Search within a SIP header value, dealing with continuation lines */
693static const char *ct_sip_header_search(const char *dptr, const char *limit,
694 const char *needle, unsigned int len)
695{
696 for (limit -= len; dptr < limit; dptr++) {
697 if (*dptr == '\r' || *dptr == '\n') {
698 dptr = sip_follow_continuation(dptr, limit);
699 if (dptr == NULL)
700 break;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800701 continue;
702 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800703
Rasmus Villemoes18082742014-10-13 15:54:31 -0700704 if (strncasecmp(dptr, needle, len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700705 return dptr;
706 }
707 return NULL;
708}
709
710int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
711 unsigned int dataoff, unsigned int datalen,
712 enum sip_header_types type,
713 unsigned int *matchoff, unsigned int *matchlen)
714{
715 const struct sip_header *hdr = &ct_sip_hdrs[type];
716 const char *start = dptr, *limit = dptr + datalen;
717 int shift = 0;
718
719 for (dptr += dataoff; dptr < limit; dptr++) {
720 /* Find beginning of line */
721 if (*dptr != '\r' && *dptr != '\n')
722 continue;
723 if (++dptr >= limit)
724 break;
725 if (*(dptr - 1) == '\r' && *dptr == '\n') {
726 if (++dptr >= limit)
727 break;
728 }
729
730 /* Skip continuation lines */
731 if (*dptr == ' ' || *dptr == '\t')
732 continue;
733
734 /* Find header. Compact headers must be followed by a
735 * non-alphabetic character to avoid mismatches. */
736 if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700737 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700738 dptr += hdr->len;
739 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700740 strncasecmp(dptr, hdr->cname, hdr->clen) == 0 &&
Patrick McHardy135d0182010-01-19 19:06:59 +0100741 !isalpha(*(dptr + hdr->clen)))
Patrick McHardyea45f122008-03-25 20:18:57 -0700742 dptr += hdr->clen;
743 else
744 continue;
745
746 /* Find and skip colon */
747 dptr = sip_skip_whitespace(dptr, limit);
748 if (dptr == NULL)
749 break;
750 if (*dptr != ':' || ++dptr >= limit)
751 break;
752
753 /* Skip whitespace after colon */
754 dptr = sip_skip_whitespace(dptr, limit);
755 if (dptr == NULL)
756 break;
757
758 *matchoff = dptr - start;
759 if (hdr->search) {
760 dptr = ct_sip_header_search(dptr, limit, hdr->search,
761 hdr->slen);
762 if (!dptr)
763 return -1;
764 dptr += hdr->slen;
765 }
766
767 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800768 if (!*matchlen)
769 return -1;
Patrick McHardyea45f122008-03-25 20:18:57 -0700770 *matchoff = dptr - start + shift;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800771 return 1;
772 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800773 return 0;
774}
Patrick McHardyea45f122008-03-25 20:18:57 -0700775EXPORT_SYMBOL_GPL(ct_sip_get_header);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800776
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800777/* Get next header field in a list of comma separated values */
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700778static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
779 unsigned int dataoff, unsigned int datalen,
780 enum sip_header_types type,
781 unsigned int *matchoff, unsigned int *matchlen)
782{
783 const struct sip_header *hdr = &ct_sip_hdrs[type];
784 const char *start = dptr, *limit = dptr + datalen;
785 int shift = 0;
786
787 dptr += dataoff;
788
789 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
790 if (!dptr)
791 return 0;
792
793 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
794 if (!dptr)
795 return 0;
796 dptr += hdr->slen;
797
798 *matchoff = dptr - start;
799 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
800 if (!*matchlen)
801 return -1;
802 *matchoff += shift;
803 return 1;
804}
805
806/* Walk through headers until a parsable one is found or no header of the
807 * given type is left. */
808static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
809 unsigned int dataoff, unsigned int datalen,
810 enum sip_header_types type, int *in_header,
811 unsigned int *matchoff, unsigned int *matchlen)
812{
813 int ret;
814
815 if (in_header && *in_header) {
816 while (1) {
817 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
818 type, matchoff, matchlen);
819 if (ret > 0)
820 return ret;
821 if (ret == 0)
822 break;
823 dataoff += *matchoff;
824 }
825 *in_header = 0;
826 }
827
828 while (1) {
829 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
830 type, matchoff, matchlen);
831 if (ret > 0)
832 break;
833 if (ret == 0)
834 return ret;
835 dataoff += *matchoff;
836 }
837
838 if (in_header)
839 *in_header = 1;
840 return 1;
841}
842
843/* Locate a SIP header, parse the URI and return the offset and length of
844 * the address as well as the address and port themselves. A stream of
845 * headers can be parsed by handing in a non-NULL datalen and in_header
846 * pointer.
847 */
848int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
849 unsigned int *dataoff, unsigned int datalen,
850 enum sip_header_types type, int *in_header,
851 unsigned int *matchoff, unsigned int *matchlen,
852 union nf_inet_addr *addr, __be16 *port)
853{
854 const char *c, *limit = dptr + datalen;
855 unsigned int p;
856 int ret;
857
858 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
859 type, in_header, matchoff, matchlen);
860 WARN_ON(ret < 0);
861 if (ret == 0)
862 return ret;
863
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000864 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700865 return -1;
866 if (*c == ':') {
867 c++;
868 p = simple_strtoul(c, (char **)&c, 10);
869 if (p < 1024 || p > 65535)
870 return -1;
871 *port = htons(p);
872 } else
873 *port = htons(SIP_PORT);
874
875 if (dataoff)
876 *dataoff = c - dptr;
877 return 1;
878}
879EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
880
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100881static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
882 unsigned int dataoff, unsigned int datalen,
883 const char *name,
884 unsigned int *matchoff, unsigned int *matchlen)
885{
886 const char *limit = dptr + datalen;
887 const char *start;
888 const char *end;
889
890 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
891 if (!limit)
892 limit = dptr + datalen;
893
894 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
895 if (!start)
896 return 0;
897 start += strlen(name);
898
899 end = ct_sip_header_search(start, limit, ";", strlen(";"));
900 if (!end)
901 end = limit;
902
903 *matchoff = start - dptr;
904 *matchlen = end - start;
905 return 1;
906}
907
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700908/* Parse address from header parameter and return address, offset and length */
909int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
910 unsigned int dataoff, unsigned int datalen,
911 const char *name,
912 unsigned int *matchoff, unsigned int *matchlen,
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000913 union nf_inet_addr *addr, bool delim)
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700914{
915 const char *limit = dptr + datalen;
916 const char *start, *end;
917
918 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
919 if (!limit)
920 limit = dptr + datalen;
921
922 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
923 if (!start)
924 return 0;
925
926 start += strlen(name);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000927 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700928 return 0;
929 *matchoff = start - dptr;
930 *matchlen = end - start;
931 return 1;
932}
933EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
934
935/* Parse numerical header parameter and return value, offset and length */
936int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
937 unsigned int dataoff, unsigned int datalen,
938 const char *name,
939 unsigned int *matchoff, unsigned int *matchlen,
940 unsigned int *val)
941{
942 const char *limit = dptr + datalen;
943 const char *start;
944 char *end;
945
946 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
947 if (!limit)
948 limit = dptr + datalen;
949
950 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
951 if (!start)
952 return 0;
953
954 start += strlen(name);
955 *val = simple_strtoul(start, &end, 0);
956 if (start == end)
957 return 0;
958 if (matchoff && matchlen) {
959 *matchoff = start - dptr;
960 *matchlen = end - start;
961 }
962 return 1;
963}
964EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
965
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100966static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
967 unsigned int dataoff, unsigned int datalen,
968 u8 *proto)
969{
970 unsigned int matchoff, matchlen;
971
972 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
973 &matchoff, &matchlen)) {
Rasmus Villemoes18082742014-10-13 15:54:31 -0700974 if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100975 *proto = IPPROTO_TCP;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700976 else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100977 *proto = IPPROTO_UDP;
978 else
979 return 0;
980
981 if (*proto != nf_ct_protonum(ct))
982 return 0;
983 } else
984 *proto = nf_ct_protonum(ct);
985
986 return 1;
987}
988
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000989static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
990 const char **endp, union nf_inet_addr *addr,
991 const char *limit)
992{
993 const char *end;
994 int ret;
995
996 memset(addr, 0, sizeof(*addr));
997 switch (nf_ct_l3num(ct)) {
998 case AF_INET:
999 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
1000 break;
1001 case AF_INET6:
1002 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
1003 break;
1004 default:
1005 BUG();
1006 }
1007
1008 if (ret == 0)
1009 return 0;
1010 if (endp)
1011 *endp = end;
1012 return 1;
1013}
1014
1015/* skip ip address. returns its length. */
1016static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
1017 const char *limit, int *shift)
1018{
1019 union nf_inet_addr addr;
1020 const char *aux = dptr;
1021
1022 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
1023 pr_debug("ip: %s parse failed.!\n", dptr);
1024 return 0;
1025 }
1026
1027 return dptr - aux;
1028}
1029
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001030/* SDP header parsing: a SDP session description contains an ordered set of
1031 * headers, starting with a section containing general session parameters,
1032 * optionally followed by multiple media descriptions.
1033 *
1034 * SDP headers always start at the beginning of a line. According to RFC 2327:
1035 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
1036 * be tolerant and also accept records terminated with a single newline
1037 * character". We handle both cases.
1038 */
Patrick McHardy9a664822012-08-26 19:14:25 +02001039static const struct sip_header ct_sdp_hdrs_v4[] = {
1040 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
1041 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
1042 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
1043 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
1044};
1045
1046static const struct sip_header ct_sdp_hdrs_v6[] = {
1047 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
1048 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
1049 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
1050 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001051};
1052
1053/* Linear string search within SDP header values */
1054static const char *ct_sdp_header_search(const char *dptr, const char *limit,
1055 const char *needle, unsigned int len)
1056{
1057 for (limit -= len; dptr < limit; dptr++) {
1058 if (*dptr == '\r' || *dptr == '\n')
1059 break;
1060 if (strncmp(dptr, needle, len) == 0)
1061 return dptr;
1062 }
1063 return NULL;
1064}
1065
1066/* Locate a SDP header (optionally a substring within the header value),
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001067 * optionally stopping at the first occurrence of the term header, parse
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001068 * it and return the offset and length of the data we're interested in.
1069 */
1070int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
1071 unsigned int dataoff, unsigned int datalen,
1072 enum sdp_header_types type,
1073 enum sdp_header_types term,
1074 unsigned int *matchoff, unsigned int *matchlen)
1075{
Patrick McHardy9a664822012-08-26 19:14:25 +02001076 const struct sip_header *hdrs, *hdr, *thdr;
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001077 const char *start = dptr, *limit = dptr + datalen;
1078 int shift = 0;
1079
Patrick McHardy9a664822012-08-26 19:14:25 +02001080 hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
1081 hdr = &hdrs[type];
1082 thdr = &hdrs[term];
1083
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001084 for (dptr += dataoff; dptr < limit; dptr++) {
1085 /* Find beginning of line */
1086 if (*dptr != '\r' && *dptr != '\n')
1087 continue;
1088 if (++dptr >= limit)
1089 break;
1090 if (*(dptr - 1) == '\r' && *dptr == '\n') {
1091 if (++dptr >= limit)
1092 break;
1093 }
1094
1095 if (term != SDP_HDR_UNSPEC &&
1096 limit - dptr >= thdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -07001097 strncasecmp(dptr, thdr->name, thdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001098 break;
1099 else if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -07001100 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001101 dptr += hdr->len;
1102 else
1103 continue;
1104
1105 *matchoff = dptr - start;
1106 if (hdr->search) {
1107 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
1108 hdr->slen);
1109 if (!dptr)
1110 return -1;
1111 dptr += hdr->slen;
1112 }
1113
1114 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
1115 if (!*matchlen)
1116 return -1;
1117 *matchoff = dptr - start + shift;
1118 return 1;
1119 }
1120 return 0;
1121}
1122EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
1123
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001124static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
1125 unsigned int dataoff, unsigned int datalen,
1126 enum sdp_header_types type,
1127 enum sdp_header_types term,
1128 unsigned int *matchoff, unsigned int *matchlen,
1129 union nf_inet_addr *addr)
1130{
1131 int ret;
1132
1133 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
1134 matchoff, matchlen);
1135 if (ret <= 0)
1136 return ret;
1137
Patrick McHardy02b69cb2012-08-09 10:08:46 +00001138 if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
1139 dptr + *matchoff + *matchlen))
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001140 return -1;
1141 return 1;
1142}
1143
Patrick McHardy0f32a402008-03-25 20:25:13 -07001144static int refresh_signalling_expectation(struct nf_conn *ct,
1145 union nf_inet_addr *addr,
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001146 u8 proto, __be16 port,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001147 unsigned int expires)
1148{
1149 struct nf_conn_help *help = nfct_help(ct);
1150 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001151 struct hlist_node *next;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001152 int found = 0;
1153
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001154 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001155 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -07001156 if (exp->class != SIP_EXPECT_SIGNALLING ||
1157 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001158 exp->tuple.dst.protonum != proto ||
Patrick McHardy0f32a402008-03-25 20:25:13 -07001159 exp->tuple.dst.u.udp.port != port)
1160 continue;
1161 if (!del_timer(&exp->timeout))
1162 continue;
1163 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
1164 exp->timeout.expires = jiffies + expires * HZ;
1165 add_timer(&exp->timeout);
1166 found = 1;
1167 break;
1168 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001169 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001170 return found;
1171}
1172
1173static void flush_expectations(struct nf_conn *ct, bool media)
Patrick McHardy9467ee32008-03-25 20:24:04 -07001174{
1175 struct nf_conn_help *help = nfct_help(ct);
1176 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001177 struct hlist_node *next;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001178
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001179 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001180 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -07001181 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
1182 continue;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001183 if (!del_timer(&exp->timeout))
1184 continue;
1185 nf_ct_unlink_expect(exp);
1186 nf_ct_expect_put(exp);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001187 if (!media)
1188 break;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001189 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001190 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001191}
1192
Patrick McHardy051966c2012-08-26 19:14:04 +02001193static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
1194 unsigned int dataoff,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001195 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001196 union nf_inet_addr *daddr, __be16 port,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001197 enum sip_expectation_classes class,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001198 unsigned int mediaoff, unsigned int medialen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001199{
Patrick McHardya9c1d352008-03-25 20:25:49 -07001200 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
Patrick McHardy212440a2008-03-25 20:17:13 -07001201 enum ip_conntrack_info ctinfo;
1202 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyana5c3a802008-10-08 11:35:09 +02001203 struct net *net = nf_ct_net(ct);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001204 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardyd901a932008-03-25 20:25:32 -07001205 union nf_inet_addr *saddr;
1206 struct nf_conntrack_tuple tuple;
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001207 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
Patrick McHardya9c1d352008-03-25 20:25:49 -07001208 u_int16_t base_port;
1209 __be16 rtp_port, rtcp_port;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001210 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001211
Patrick McHardyd901a932008-03-25 20:25:32 -07001212 saddr = NULL;
1213 if (sip_direct_media) {
1214 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
1215 return NF_ACCEPT;
1216 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1217 }
1218
1219 /* We need to check whether the registration exists before attempting
1220 * to register it since we can see the same media description multiple
1221 * times on different connections in case multiple endpoints receive
1222 * the same call.
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001223 *
1224 * RTP optimization: if we find a matching media channel expectation
1225 * and both the expectation and this connection are SNATed, we assume
1226 * both sides can reach each other directly and use the final
1227 * destination address from the expectation. We still need to keep
1228 * the NATed expectations for media that might arrive from the
1229 * outside, and additionally need to expect the direct RTP stream
1230 * in case it passes through us even without NAT.
Patrick McHardyd901a932008-03-25 20:25:32 -07001231 */
1232 memset(&tuple, 0, sizeof(tuple));
1233 if (saddr)
1234 tuple.src.u3 = *saddr;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001235 tuple.src.l3num = nf_ct_l3num(ct);
Patrick McHardyd901a932008-03-25 20:25:32 -07001236 tuple.dst.protonum = IPPROTO_UDP;
1237 tuple.dst.u3 = *daddr;
1238 tuple.dst.u.udp.port = port;
1239
1240 rcu_read_lock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001241 do {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001242 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
Patrick McHardyd901a932008-03-25 20:25:32 -07001243
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001244 if (!exp || exp->master == ct ||
1245 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
1246 exp->class != class)
1247 break;
Patrick McHardye1f9a462008-04-19 17:53:52 -07001248#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardy9a664822012-08-26 19:14:25 +02001249 if (!direct_rtp &&
1250 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001251 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
1252 ct->status & IPS_NAT_MASK) {
Patrick McHardy9a664822012-08-26 19:14:25 +02001253 *daddr = exp->saved_addr;
1254 tuple.dst.u3 = exp->saved_addr;
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001255 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
1256 direct_rtp = 1;
1257 } else
Patrick McHardye1f9a462008-04-19 17:53:52 -07001258#endif
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001259 skip_expect = 1;
1260 } while (!skip_expect);
Patrick McHardyd901a932008-03-25 20:25:32 -07001261
Patrick McHardya9c1d352008-03-25 20:25:49 -07001262 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
1263 rtp_port = htons(base_port);
1264 rtcp_port = htons(base_port + 1);
1265
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001266 if (direct_rtp) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001267 hooks = rcu_dereference(nf_nat_sip_hooks);
1268 if (hooks &&
1269 !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001270 mediaoff, medialen, ntohs(rtp_port)))
1271 goto err1;
1272 }
1273
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001274 if (skip_expect) {
1275 rcu_read_unlock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001276 return NF_ACCEPT;
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001277 }
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001278
Patrick McHardya9c1d352008-03-25 20:25:49 -07001279 rtp_exp = nf_ct_expect_alloc(ct);
1280 if (rtp_exp == NULL)
1281 goto err1;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001282 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001283 IPPROTO_UDP, NULL, &rtp_port);
1284
1285 rtcp_exp = nf_ct_expect_alloc(ct);
1286 if (rtcp_exp == NULL)
1287 goto err2;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001288 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001289 IPPROTO_UDP, NULL, &rtcp_port);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001290
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001291 hooks = rcu_dereference(nf_nat_sip_hooks);
1292 if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
1293 ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
1294 datalen, rtp_exp, rtcp_exp,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001295 mediaoff, medialen, daddr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001296 else {
Patrick McHardya9c1d352008-03-25 20:25:49 -07001297 if (nf_ct_expect_related(rtp_exp) == 0) {
1298 if (nf_ct_expect_related(rtcp_exp) != 0)
1299 nf_ct_unexpect_related(rtp_exp);
1300 else
1301 ret = NF_ACCEPT;
1302 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001303 }
Patrick McHardya9c1d352008-03-25 20:25:49 -07001304 nf_ct_expect_put(rtcp_exp);
1305err2:
1306 nf_ct_expect_put(rtp_exp);
1307err1:
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001308 rcu_read_unlock();
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001309 return ret;
1310}
1311
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001312static const struct sdp_media_type sdp_media_types[] = {
1313 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
1314 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
Patrick McHardy9d288df2010-02-11 12:30:21 +01001315 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001316};
1317
1318static const struct sdp_media_type *sdp_media_type(const char *dptr,
1319 unsigned int matchoff,
1320 unsigned int matchlen)
1321{
1322 const struct sdp_media_type *t;
1323 unsigned int i;
1324
1325 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
1326 t = &sdp_media_types[i];
1327 if (matchlen < t->len ||
1328 strncmp(dptr + matchoff, t->name, t->len))
1329 continue;
1330 return t;
1331 }
1332 return NULL;
1333}
1334
Patrick McHardy051966c2012-08-26 19:14:04 +02001335static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1336 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001337 const char **dptr, unsigned int *datalen,
1338 unsigned int cseq)
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001339{
1340 enum ip_conntrack_info ctinfo;
1341 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001342 unsigned int matchoff, matchlen;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001343 unsigned int mediaoff, medialen;
1344 unsigned int sdpoff;
1345 unsigned int caddr_len, maddr_len;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001346 unsigned int i;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001347 union nf_inet_addr caddr, maddr, rtp_addr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001348 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001349 unsigned int port;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001350 const struct sdp_media_type *t;
1351 int ret = NF_ACCEPT;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001352
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001353 hooks = rcu_dereference(nf_nat_sip_hooks);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001354
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001355 /* Find beginning of session description */
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001356 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001357 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001358 &matchoff, &matchlen) <= 0)
1359 return NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001360 sdpoff = matchoff;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001361
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001362 /* The connection information is contained in the session description
1363 * and/or once per media description. The first media description marks
1364 * the end of the session description. */
1365 caddr_len = 0;
1366 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001367 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001368 &matchoff, &matchlen, &caddr) > 0)
1369 caddr_len = matchlen;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001370
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001371 mediaoff = sdpoff;
1372 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1373 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1374 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1375 &mediaoff, &medialen) <= 0)
1376 break;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001377
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001378 /* Get media type and port number. A media port value of zero
1379 * indicates an inactive stream. */
1380 t = sdp_media_type(*dptr, mediaoff, medialen);
1381 if (!t) {
1382 mediaoff += medialen;
1383 continue;
1384 }
1385 mediaoff += t->len;
1386 medialen -= t->len;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001387
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001388 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1389 if (port == 0)
1390 continue;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001391 if (port < 1024 || port > 65535) {
1392 nf_ct_helper_log(skb, ct, "wrong port %u", port);
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001393 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001394 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001395
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001396 /* The media description overrides the session description. */
1397 maddr_len = 0;
1398 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001399 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001400 &matchoff, &matchlen, &maddr) > 0) {
1401 maddr_len = matchlen;
1402 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1403 } else if (caddr_len)
1404 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001405 else {
1406 nf_ct_helper_log(skb, ct, "cannot parse SDP message");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001407 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001408 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001409
Patrick McHardy051966c2012-08-26 19:14:04 +02001410 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1411 dptr, datalen,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001412 &rtp_addr, htons(port), t->class,
1413 mediaoff, medialen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001414 if (ret != NF_ACCEPT) {
1415 nf_ct_helper_log(skb, ct,
1416 "cannot add expectation for voice");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001417 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001418 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001419
1420 /* Update media connection address if present */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001421 if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1422 ret = hooks->sdp_addr(skb, protoff, dataoff,
Patrick McHardy9a664822012-08-26 19:14:25 +02001423 dptr, datalen, mediaoff,
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001424 SDP_HDR_CONNECTION,
1425 SDP_HDR_MEDIA,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001426 &rtp_addr);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001427 if (ret != NF_ACCEPT) {
1428 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001429 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001430 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001431 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001432 i++;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001433 }
1434
1435 /* Update session connection and owner addresses */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001436 hooks = rcu_dereference(nf_nat_sip_hooks);
1437 if (hooks && ct->status & IPS_NAT_MASK)
1438 ret = hooks->sdp_session(skb, protoff, dataoff,
1439 dptr, datalen, sdpoff,
1440 &rtp_addr);
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001441
1442 return ret;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001443}
Patrick McHardy051966c2012-08-26 19:14:04 +02001444static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1445 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001446 const char **dptr, unsigned int *datalen,
1447 unsigned int cseq, unsigned int code)
1448{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001449 enum ip_conntrack_info ctinfo;
1450 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001451 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001452
Patrick McHardy30f33e62008-03-25 20:22:20 -07001453 if ((code >= 100 && code <= 199) ||
1454 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001455 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001456 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001457 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001458 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001459}
1460
Patrick McHardy051966c2012-08-26 19:14:04 +02001461static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1462 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001463 const char **dptr, unsigned int *datalen,
1464 unsigned int cseq, unsigned int code)
1465{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001466 enum ip_conntrack_info ctinfo;
1467 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001468 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001469
Patrick McHardy30f33e62008-03-25 20:22:20 -07001470 if ((code >= 100 && code <= 199) ||
1471 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001472 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001473 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001474 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001475 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001476}
1477
Patrick McHardy051966c2012-08-26 19:14:04 +02001478static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1479 unsigned int dataoff,
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001480 const char **dptr, unsigned int *datalen,
1481 unsigned int cseq, unsigned int code)
1482{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001483 enum ip_conntrack_info ctinfo;
1484 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001485 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001486
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001487 if ((code >= 100 && code <= 199) ||
1488 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001489 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001490 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001491 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001492 return NF_ACCEPT;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001493}
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001494
Patrick McHardy051966c2012-08-26 19:14:04 +02001495static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1496 unsigned int dataoff,
Patrick McHardy9d288df2010-02-11 12:30:21 +01001497 const char **dptr, unsigned int *datalen,
1498 unsigned int cseq)
1499{
1500 enum ip_conntrack_info ctinfo;
1501 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001502 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001503 unsigned int ret;
1504
1505 flush_expectations(ct, true);
Patrick McHardy051966c2012-08-26 19:14:04 +02001506 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001507 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001508 ct_sip_info->invite_cseq = cseq;
Patrick McHardy9d288df2010-02-11 12:30:21 +01001509 return ret;
1510}
1511
Patrick McHardy051966c2012-08-26 19:14:04 +02001512static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1513 unsigned int dataoff,
Patrick McHardy9467ee32008-03-25 20:24:04 -07001514 const char **dptr, unsigned int *datalen,
1515 unsigned int cseq)
1516{
1517 enum ip_conntrack_info ctinfo;
1518 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1519
Patrick McHardy0f32a402008-03-25 20:25:13 -07001520 flush_expectations(ct, true);
1521 return NF_ACCEPT;
1522}
1523
1524/* Parse a REGISTER request and create a permanent expectation for incoming
1525 * signalling connections. The expectation is marked inactive and is activated
1526 * when receiving a response indicating success from the registrar.
1527 */
Patrick McHardy051966c2012-08-26 19:14:04 +02001528static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1529 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001530 const char **dptr, unsigned int *datalen,
1531 unsigned int cseq)
1532{
1533 enum ip_conntrack_info ctinfo;
1534 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001535 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001536 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001537 unsigned int matchoff, matchlen;
1538 struct nf_conntrack_expect *exp;
1539 union nf_inet_addr *saddr, daddr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001540 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001541 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001542 u8 proto;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001543 unsigned int expires = 0;
1544 int ret;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001545
1546 /* Expected connections can not register again. */
1547 if (ct->status & IPS_EXPECTED)
1548 return NF_ACCEPT;
1549
1550 /* We must check the expiration time: a value of zero signals the
1551 * registrar to release the binding. We'll remove our expectation
1552 * when receiving the new bindings in the response, but we don't
1553 * want to create new ones.
1554 *
1555 * The expiration time may be contained in Expires: header, the
1556 * Contact: header parameters or the URI parameters.
1557 */
1558 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1559 &matchoff, &matchlen) > 0)
1560 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1561
1562 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1563 SIP_HDR_CONTACT, NULL,
1564 &matchoff, &matchlen, &daddr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001565 if (ret < 0) {
1566 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001567 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001568 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001569 return NF_ACCEPT;
1570
1571 /* We don't support third-party registrations */
1572 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1573 return NF_ACCEPT;
1574
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001575 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1576 &proto) == 0)
1577 return NF_ACCEPT;
1578
Patrick McHardy0f32a402008-03-25 20:25:13 -07001579 if (ct_sip_parse_numerical_param(ct, *dptr,
1580 matchoff + matchlen, *datalen,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001581 "expires=", NULL, NULL, &expires) < 0) {
1582 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001583 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001584 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001585
1586 if (expires == 0) {
1587 ret = NF_ACCEPT;
1588 goto store_cseq;
1589 }
1590
1591 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001592 if (!exp) {
1593 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001594 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001595 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001596
1597 saddr = NULL;
1598 if (sip_direct_signalling)
1599 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1600
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001601 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001602 saddr, &daddr, proto, NULL, &port);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001603 exp->timeout.expires = sip_timeout * HZ;
1604 exp->helper = nfct_help(ct)->helper;
1605 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1606
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001607 hooks = rcu_dereference(nf_nat_sip_hooks);
1608 if (hooks && ct->status & IPS_NAT_MASK)
1609 ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1610 exp, matchoff, matchlen);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001611 else {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001612 if (nf_ct_expect_related(exp) != 0) {
1613 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001614 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001615 } else
Patrick McHardy0f32a402008-03-25 20:25:13 -07001616 ret = NF_ACCEPT;
1617 }
1618 nf_ct_expect_put(exp);
1619
1620store_cseq:
1621 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001622 ct_sip_info->register_cseq = cseq;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001623 return ret;
1624}
1625
Patrick McHardy051966c2012-08-26 19:14:04 +02001626static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1627 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001628 const char **dptr, unsigned int *datalen,
1629 unsigned int cseq, unsigned int code)
1630{
1631 enum ip_conntrack_info ctinfo;
1632 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001633 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001634 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1635 union nf_inet_addr addr;
1636 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001637 u8 proto;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001638 unsigned int matchoff, matchlen, coff = 0;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001639 unsigned int expires = 0;
1640 int in_contact = 0, ret;
1641
1642 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1643 * they have received a final response from the registrar for the
1644 * previous one or the previous REGISTER request has timed out".
1645 *
1646 * However, some servers fail to detect retransmissions and send late
1647 * responses, so we store the sequence number of the last valid
1648 * request and compare it here.
1649 */
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001650 if (ct_sip_info->register_cseq != cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001651 return NF_ACCEPT;
1652
1653 if (code >= 100 && code <= 199)
1654 return NF_ACCEPT;
1655 if (code < 200 || code > 299)
1656 goto flush;
1657
1658 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1659 &matchoff, &matchlen) > 0)
1660 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1661
1662 while (1) {
1663 unsigned int c_expires = expires;
1664
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001665 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001666 SIP_HDR_CONTACT, &in_contact,
1667 &matchoff, &matchlen,
1668 &addr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001669 if (ret < 0) {
1670 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001671 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001672 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001673 break;
1674
1675 /* We don't support third-party registrations */
1676 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1677 continue;
1678
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001679 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1680 *datalen, &proto) == 0)
1681 continue;
1682
Patrick McHardy0f32a402008-03-25 20:25:13 -07001683 ret = ct_sip_parse_numerical_param(ct, *dptr,
1684 matchoff + matchlen,
1685 *datalen, "expires=",
1686 NULL, NULL, &c_expires);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001687 if (ret < 0) {
1688 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001689 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001690 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001691 if (c_expires == 0)
1692 break;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001693 if (refresh_signalling_expectation(ct, &addr, proto, port,
1694 c_expires))
Patrick McHardy0f32a402008-03-25 20:25:13 -07001695 return NF_ACCEPT;
1696 }
1697
1698flush:
1699 flush_expectations(ct, false);
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001700 return NF_ACCEPT;
1701}
1702
Patrick McHardy30f33e62008-03-25 20:22:20 -07001703static const struct sip_handler sip_handlers[] = {
Patrick McHardy9d288df2010-02-11 12:30:21 +01001704 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001705 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001706 SIP_HANDLER("ACK", process_sdp, NULL),
1707 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
Patrick McHardy9467ee32008-03-25 20:24:04 -07001708 SIP_HANDLER("BYE", process_bye_request, NULL),
Patrick McHardy0f32a402008-03-25 20:25:13 -07001709 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001710};
1711
Patrick McHardy051966c2012-08-26 19:14:04 +02001712static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1713 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001714 const char **dptr, unsigned int *datalen)
1715{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001716 enum ip_conntrack_info ctinfo;
1717 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001718 unsigned int matchoff, matchlen, matchend;
1719 unsigned int code, cseq, i;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001720
1721 if (*datalen < strlen("SIP/2.0 200"))
1722 return NF_ACCEPT;
1723 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001724 if (!code) {
1725 nf_ct_helper_log(skb, ct, "cannot get code");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001726 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001727 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001728
1729 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001730 &matchoff, &matchlen) <= 0) {
1731 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001732 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001733 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001734 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001735 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001736 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001737 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001738 }
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001739 matchend = matchoff + matchlen + 1;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001740
1741 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001742 const struct sip_handler *handler;
1743
Patrick McHardy30f33e62008-03-25 20:22:20 -07001744 handler = &sip_handlers[i];
1745 if (handler->response == NULL)
1746 continue;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001747 if (*datalen < matchend + handler->len ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001748 strncasecmp(*dptr + matchend, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001749 continue;
Patrick McHardy051966c2012-08-26 19:14:04 +02001750 return handler->response(skb, protoff, dataoff, dptr, datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001751 cseq, code);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001752 }
1753 return NF_ACCEPT;
1754}
1755
Patrick McHardy051966c2012-08-26 19:14:04 +02001756static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1757 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001758 const char **dptr, unsigned int *datalen)
1759{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001760 enum ip_conntrack_info ctinfo;
1761 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Kevin Cernekee72665072012-12-17 18:33:58 +00001762 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1763 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001764 unsigned int matchoff, matchlen;
1765 unsigned int cseq, i;
Kevin Cernekee72665072012-12-17 18:33:58 +00001766 union nf_inet_addr addr;
1767 __be16 port;
1768
1769 /* Many Cisco IP phones use a high source port for SIP requests, but
1770 * listen for the response on port 5060. If we are the local
1771 * router for one of these phones, save the port number from the
1772 * Via: header so that nf_nat_sip can redirect the responses to
1773 * the correct port.
1774 */
1775 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1776 SIP_HDR_VIA_UDP, NULL, &matchoff,
1777 &matchlen, &addr, &port) > 0 &&
1778 port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1779 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1780 ct_sip_info->forced_dport = port;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001781
1782 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001783 const struct sip_handler *handler;
1784
Patrick McHardy30f33e62008-03-25 20:22:20 -07001785 handler = &sip_handlers[i];
1786 if (handler->request == NULL)
1787 continue;
Ulrich Weber444f9012016-10-24 18:07:23 +02001788 if (*datalen < handler->len + 2 ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001789 strncasecmp(*dptr, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001790 continue;
Ulrich Weber444f9012016-10-24 18:07:23 +02001791 if ((*dptr)[handler->len] != ' ' ||
1792 !isalpha((*dptr)[handler->len+1]))
1793 continue;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001794
1795 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001796 &matchoff, &matchlen) <= 0) {
1797 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001798 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001799 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001800 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001801 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001802 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001803 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001804 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001805
Patrick McHardy051966c2012-08-26 19:14:04 +02001806 return handler->request(skb, protoff, dataoff, dptr, datalen,
1807 cseq);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001808 }
1809 return NF_ACCEPT;
1810}
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001811
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001812static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
Patrick McHardy051966c2012-08-26 19:14:04 +02001813 unsigned int protoff, unsigned int dataoff,
1814 const char **dptr, unsigned int *datalen)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001815{
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001816 const struct nf_nat_sip_hooks *hooks;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001817 int ret;
1818
Tyler Weard2aeec02013-09-20 12:12:07 -07001819 if (nf_ct_disable_sip_alg)
1820 return NF_ACCEPT;
Rasmus Villemoes18082742014-10-13 15:54:31 -07001821 if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
Patrick McHardy051966c2012-08-26 19:14:04 +02001822 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001823 else
Patrick McHardy051966c2012-08-26 19:14:04 +02001824 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001825
Patrick McHardy9a664822012-08-26 19:14:25 +02001826 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001827 hooks = rcu_dereference(nf_nat_sip_hooks);
1828 if (hooks && !hooks->msg(skb, protoff, dataoff,
1829 dptr, datalen)) {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001830 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001831 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001832 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001833 }
1834
1835 return ret;
1836}
1837
1838static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1839 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1840{
1841 struct tcphdr *th, _tcph;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301842 unsigned int dataoff;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001843 unsigned int matchoff, matchlen, clen;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001844 const char *dptr, *end;
1845 s16 diff, tdiff = 0;
Simon Horman78748962010-09-21 21:17:30 +00001846 int ret = NF_ACCEPT;
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001847 bool term;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301848 unsigned int datalen = 0, msglen = 0, origlen = 0;
1849 unsigned int dataoff_orig = 0;
1850 unsigned int splitlen, oldlen, oldlen1;
1851 struct sip_list *sip_entry = NULL;
1852 bool skip_sip_process = false;
1853 bool do_not_process = false;
1854 bool skip = false;
1855 bool skb_is_combined = false;
1856 enum ip_conntrack_dir dir = IP_CT_DIR_MAX;
1857 struct sk_buff *combined_skb = NULL;
1858 bool content_len_exists = 1;
1859
1860 packet_count++;
1861 pr_debug("packet count %d\n", packet_count);
1862
1863 if (nf_ct_disable_sip_alg)
1864 return NF_ACCEPT;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001865
1866 if (ctinfo != IP_CT_ESTABLISHED &&
Eric Dumazetfb048832011-05-19 15:44:27 +02001867 ctinfo != IP_CT_ESTABLISHED_REPLY)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001868 return NF_ACCEPT;
1869
1870 /* No Data ? */
1871 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1872 if (th == NULL)
1873 return NF_ACCEPT;
1874 dataoff = protoff + th->doff * 4;
1875 if (dataoff >= skb->len)
1876 return NF_ACCEPT;
1877
1878 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1879
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001880 if (unlikely(skb_linearize(skb)))
1881 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001882
1883 dptr = skb->data + dataoff;
1884 datalen = skb->len - dataoff;
1885 if (datalen < strlen("SIP/2.0 200"))
1886 return NF_ACCEPT;
1887
Mohammed Javid3cfcd572018-01-15 16:53:18 +05301888 /* Check if the header contains SIP version */
1889 if (!strnstr(dptr, "SIP/2.0", datalen))
1890 return NF_ACCEPT;
1891
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301892 /* here we save the original datalength and data offset of the skb, this
1893 * is needed later to split combined skbs
1894 */
1895 oldlen1 = skb->len - protoff;
1896 dataoff_orig = dataoff;
1897
1898 if (!ct)
1899 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001900 while (1) {
1901 if (ct_sip_get_header(ct, dptr, 0, datalen,
1902 SIP_HDR_CONTENT_LENGTH,
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301903 &matchoff, &matchlen) <= 0){
1904 if (nf_ct_enable_sip_segmentation) {
1905 do_not_process = true;
1906 content_len_exists = 0;
1907 goto destination;
1908 } else {
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001909 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301910 }
1911 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001912
1913 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1914 if (dptr + matchoff == end)
1915 break;
1916
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001917 term = false;
1918 for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1919 if (end[0] == '\r' && end[1] == '\n' &&
1920 end[2] == '\r' && end[3] == '\n') {
1921 term = true;
1922 break;
1923 }
1924 }
1925 if (!term)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001926 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301927
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001928 end += strlen("\r\n\r\n") + clen;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301929destination:
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001930
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301931 if (content_len_exists == 0) {
1932 origlen = datalen;
1933 msglen = origlen;
1934 } else {
1935 origlen = end - dptr;
1936 msglen = origlen;
1937 }
1938 pr_debug("mslgen %d datalen %d\n", msglen, datalen);
1939 dir = CTINFO2DIR(ctinfo);
1940 combined_skb = skb;
1941 if (nf_ct_enable_sip_segmentation) {
1942 /* Segmented Packet */
1943 if (msglen > datalen) {
1944 skip = sip_save_segment_info(ct, skb, msglen,
1945 datalen, dptr,
1946 ctinfo);
1947 if (!skip)
1948 return NF_QUEUE;
1949 }
1950 /* Traverse list to find prev segment */
1951 /*Traverse the list if list non empty */
1952 if (((&ct->sip_segment_list)->next) !=
1953 (&ct->sip_segment_list)) {
1954 /* Combine segments if they are fragments of
1955 * the same message.
1956 */
1957 sip_entry = sip_coalesce_segments(ct, &skb,
1958 dataoff,
1959 &combined_skb,
1960 &skip_sip_process,
1961 do_not_process,
1962 ctinfo,
1963 &skb_is_combined);
1964 sip_update_params(dir, &msglen, &origlen, &dptr,
1965 &datalen,
1966 skb_is_combined, ct);
1967
1968 if (skip_sip_process)
1969 goto here;
1970 } else if (do_not_process) {
1971 goto here;
1972 }
1973 } else if (msglen > datalen) {
Patrick McHardy3a7b21e2013-04-05 08:13:30 +00001974 return NF_ACCEPT;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301975 }
1976 /* process the combined skb having the complete SIP message */
1977 ret = process_sip_msg(combined_skb, ct, protoff, dataoff,
Patrick McHardy051966c2012-08-26 19:14:04 +02001978 &dptr, &msglen);
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301979
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001980 /* process_sip_* functions report why this packet is dropped */
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001981 if (ret != NF_ACCEPT)
1982 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301983 sip_calculate_parameters(&diff, &tdiff, &dataoff, &dptr,
1984 &datalen, msglen, origlen);
1985 if (nf_ct_enable_sip_segmentation && skb_is_combined)
1986 break;
1987 }
1988 if (skb_is_combined) {
1989 /* once combined skb is processed, split the skbs again The
1990 * length to split at is the same as length of first skb. Any
1991 * changes in the combined skb length because of SIP processing
1992 * will reflect in the second fragment
1993 */
1994 splitlen = (dir == IP_CT_DIR_ORIGINAL) ?
1995 ct->segment.skb_len[0] : ct->segment.skb_len[1];
1996 oldlen = combined_skb->len - protoff;
1997 skb_split(combined_skb, skb, splitlen);
1998 /* Headers need to be recalculated since during SIP processing
1999 * headers are calculated based on the change in length of the
2000 * combined message
2001 */
2002 recalc_header(combined_skb, splitlen, oldlen, protoff);
2003 /* Reinject the first skb now that the processing is complete */
2004 if (sip_entry) {
2005 nf_reinject(sip_entry->entry, NF_ACCEPT);
2006 kfree(sip_entry);
2007 }
2008 skb->len = (oldlen1 + protoff) + tdiff - dataoff_orig;
2009 /* After splitting, push the headers back to the first skb which
2010 * were removed before combining the skbs.This moves the skb
2011 * begin pointer back to the beginning of its headers
2012 */
2013 skb_push(skb, dataoff_orig);
2014 /* Since the length of this second segment willbe affected
2015 * because of SIP processing,we need to recalculate its header
2016 * as well.
2017 */
2018 recalc_header(skb, skb->len, oldlen1, protoff);
2019 /* Now that the processing is done and the first skb reinjected.
2020 * We allow addition of fragmented skbs to the list for this
2021 * direction
2022 */
2023 if (dir == IP_CT_DIR_ORIGINAL)
2024 ct->sip_original_dir = 0;
2025 else
2026 ct->sip_reply_dir = 0;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002027 }
2028
Mohammed Javidf4530ef2017-11-20 20:15:46 +05302029here:
2030
2031 if (ret == NF_ACCEPT && ct && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02002032 const struct nf_nat_sip_hooks *hooks;
2033
2034 hooks = rcu_dereference(nf_nat_sip_hooks);
2035 if (hooks)
2036 hooks->seq_adjust(skb, protoff, tdiff);
Patrick McHardy48f8ac22010-02-11 12:29:38 +01002037 }
2038
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002039 return ret;
2040}
2041
2042static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
2043 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002044{
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002045 unsigned int dataoff, datalen;
2046 const char *dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002047
2048 /* No Data ? */
2049 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -07002050 if (dataoff >= skb->len)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002051 return NF_ACCEPT;
2052
Herbert Xu3db05fe2007-10-15 00:53:15 -07002053 nf_ct_refresh(ct, skb, sip_timeout * HZ);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002054
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02002055 if (unlikely(skb_linearize(skb)))
2056 return NF_DROP;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002057
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002058 dptr = skb->data + dataoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -07002059 datalen = skb->len - dataoff;
Patrick McHardy779382e2008-03-25 20:17:36 -07002060 if (datalen < strlen("SIP/2.0 200"))
Patrick McHardy7d3dd042008-03-25 20:19:46 -07002061 return NF_ACCEPT;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002062
Mohammed Javid3cfcd572018-01-15 16:53:18 +05302063 /* Check if the header contains SIP version */
2064 if (!strnstr(dptr, "SIP/2.0", datalen))
2065 return NF_ACCEPT;
2066
Patrick McHardy051966c2012-08-26 19:14:04 +02002067 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002068}
2069
Gao Feng82de0be2016-07-18 11:39:23 +08002070static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002071
Patrick McHardy0f32a402008-03-25 20:25:13 -07002072static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
2073 [SIP_EXPECT_SIGNALLING] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002074 .name = "signalling",
Patrick McHardy0f32a402008-03-25 20:25:13 -07002075 .max_expected = 1,
2076 .timeout = 3 * 60,
2077 },
2078 [SIP_EXPECT_AUDIO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002079 .name = "audio",
Patrick McHardya9c1d352008-03-25 20:25:49 -07002080 .max_expected = 2 * IP_CT_DIR_MAX,
Patrick McHardy0f32a402008-03-25 20:25:13 -07002081 .timeout = 3 * 60,
2082 },
Patrick McHardy0d0ab032008-03-25 20:26:24 -07002083 [SIP_EXPECT_VIDEO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002084 .name = "video",
Patrick McHardy0d0ab032008-03-25 20:26:24 -07002085 .max_expected = 2 * IP_CT_DIR_MAX,
2086 .timeout = 3 * 60,
2087 },
Patrick McHardy9d288df2010-02-11 12:30:21 +01002088 [SIP_EXPECT_IMAGE] = {
2089 .name = "image",
2090 .max_expected = IP_CT_DIR_MAX,
2091 .timeout = 3 * 60,
2092 },
Patrick McHardy6002f2662008-03-25 20:09:15 -07002093};
2094
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002095static void nf_conntrack_sip_fini(void)
2096{
Gao Feng82de0be2016-07-18 11:39:23 +08002097 nf_conntrack_helpers_unregister(sip, ports_c * 4);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002098}
2099
2100static int __init nf_conntrack_sip_init(void)
2101{
Gao Feng82de0be2016-07-18 11:39:23 +08002102 int i, ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002103
Tyler Wear2e6eecd2014-05-16 11:58:23 -07002104 sip_sysctl_header = register_net_sysctl(&init_net, "net/netfilter",
2105 sip_sysctl_tbl);
Tyler Weard2aeec02013-09-20 12:12:07 -07002106 if (!sip_sysctl_header)
2107 pr_debug("nf_ct_sip:Unable to register SIP systbl\n");
2108
2109 if (nf_ct_disable_sip_alg)
2110 pr_debug("nf_ct_sip: SIP ALG disabled\n");
2111 else
2112 pr_debug("nf_ct_sip: SIP ALG enabled\n");
2113
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002114 if (ports_c == 0)
2115 ports[ports_c++] = SIP_PORT;
2116
2117 for (i = 0; i < ports_c; i++) {
Gao Feng82de0be2016-07-18 11:39:23 +08002118 nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP, "sip",
2119 SIP_PORT, ports[i], i, sip_exp_policy,
2120 SIP_EXPECT_MAX,
2121 sizeof(struct nf_ct_sip_master), sip_help_udp,
2122 NULL, THIS_MODULE);
2123 nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP, "sip",
2124 SIP_PORT, ports[i], i, sip_exp_policy,
2125 SIP_EXPECT_MAX,
2126 sizeof(struct nf_ct_sip_master), sip_help_tcp,
2127 NULL, THIS_MODULE);
2128 nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP, "sip",
2129 SIP_PORT, ports[i], i, sip_exp_policy,
2130 SIP_EXPECT_MAX,
2131 sizeof(struct nf_ct_sip_master), sip_help_udp,
2132 NULL, THIS_MODULE);
2133 nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP, "sip",
2134 SIP_PORT, ports[i], i, sip_exp_policy,
2135 SIP_EXPECT_MAX,
2136 sizeof(struct nf_ct_sip_master), sip_help_tcp,
2137 NULL, THIS_MODULE);
2138 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002139
Gao Feng82de0be2016-07-18 11:39:23 +08002140 ret = nf_conntrack_helpers_register(sip, ports_c * 4);
2141 if (ret < 0) {
2142 pr_err("failed to register helpers\n");
2143 return ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002144 }
2145 return 0;
2146}
2147
2148module_init(nf_conntrack_sip_init);
2149module_exit(nf_conntrack_sip_fini);