blob: 6d6731ffe596981d2ea97d88b5156e4af73f5ae1 [file] [log] [blame]
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001/* SIP extension for IP connection tracking.
2 *
Mohammed Javidf4530ef2017-11-20 20:15:46 +05303 * Copyright (c) 2015,2017, 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
395static const struct nf_queue_handler nf_sip_qh = {
396 .outfn = &nf_sip_enqueue_packet,
397};
398
399static
400int proc_sip_segment(struct ctl_table *ctl, int write,
401 void __user *buffer, size_t *lenp, loff_t *ppos)
402{
403 int ret;
404
405 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
406 if (nf_ct_enable_sip_segmentation) {
407 pr_debug("registering queue handler\n");
408 nf_register_queue_handler(&init_net, &nf_sip_qh);
409 } else {
410 pr_debug("de-registering queue handler\n");
411 nf_unregister_queue_handler(&init_net);
412 }
413 return ret;
414}
415
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800416static int digits_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800417 const char *limit, int *shift)
418{
419 int len = 0;
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700420 while (dptr < limit && isdigit(*dptr)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800421 dptr++;
422 len++;
423 }
424 return len;
425}
426
Simon Horman001985b2010-08-22 21:37:51 +0900427static int iswordc(const char c)
428{
429 if (isalnum(c) || c == '!' || c == '"' || c == '%' ||
Marco Angaronif0608ce2016-08-30 18:48:24 +0200430 (c >= '(' && c <= '+') || c == ':' || c == '<' || c == '>' ||
Simon Horman001985b2010-08-22 21:37:51 +0900431 c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' ||
Marco Angaronif0608ce2016-08-30 18:48:24 +0200432 c == '{' || c == '}' || c == '~' || (c >= '-' && c <= '/') ||
433 c == '\'')
Simon Horman001985b2010-08-22 21:37:51 +0900434 return 1;
435 return 0;
436}
437
438static int word_len(const char *dptr, const char *limit)
439{
440 int len = 0;
441 while (dptr < limit && iswordc(*dptr)) {
442 dptr++;
443 len++;
444 }
445 return len;
446}
447
448static int callid_len(const struct nf_conn *ct, const char *dptr,
449 const char *limit, int *shift)
450{
451 int len, domain_len;
452
453 len = word_len(dptr, limit);
454 dptr += len;
455 if (!len || dptr == limit || *dptr != '@')
456 return len;
457 dptr++;
458 len++;
459
460 domain_len = word_len(dptr, limit);
461 if (!domain_len)
462 return 0;
463 return len + domain_len;
464}
465
Patrick McHardy0d0ab032008-03-25 20:26:24 -0700466/* get media type + port length */
467static int media_len(const struct nf_conn *ct, const char *dptr,
468 const char *limit, int *shift)
469{
470 int len = string_len(ct, dptr, limit, shift);
471
472 dptr += len;
473 if (dptr >= limit || *dptr != ' ')
474 return 0;
475 len++;
476 dptr++;
477
478 return len + digits_len(ct, dptr, limit, shift);
479}
480
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000481static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
482 const char **endp, union nf_inet_addr *addr,
483 const char *limit, bool delim)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800484{
485 const char *end;
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000486 int ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800487
Simon Horman5adbb9f2010-08-22 21:37:51 +0900488 if (!ct)
489 return 0;
490
Patrick McHardyfa913dd2008-04-14 11:15:45 +0200491 memset(addr, 0, sizeof(*addr));
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200492 switch (nf_ct_l3num(ct)) {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800493 case AF_INET:
494 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000495 if (ret == 0)
496 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800497 break;
498 case AF_INET6:
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000499 if (cp < limit && *cp == '[')
500 cp++;
501 else if (delim)
502 return 0;
503
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800504 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000505 if (ret == 0)
506 return 0;
507
508 if (end < limit && *end == ']')
509 end++;
510 else if (delim)
511 return 0;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800512 break;
513 default:
514 BUG();
515 }
516
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800517 if (endp)
518 *endp = end;
519 return 1;
520}
521
522/* skip ip address. returns its length. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800523static int epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800524 const char *limit, int *shift)
525{
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800526 union nf_inet_addr addr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800527 const char *aux = dptr;
528
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000529 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700530 pr_debug("ip: %s parse failed.!\n", dptr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800531 return 0;
532 }
533
534 /* Port number */
535 if (*dptr == ':') {
536 dptr++;
537 dptr += digits_len(ct, dptr, limit, shift);
538 }
539 return dptr - aux;
540}
541
542/* get address length, skiping user info. */
Jan Engelhardt13f7d632008-01-31 04:50:25 -0800543static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800544 const char *limit, int *shift)
545{
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700546 const char *start = dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800547 int s = *shift;
548
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800549 /* Search for @, but stop at the end of the line.
550 * We are inside a sip: URI, so we don't need to worry about
551 * continuation lines. */
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700552 while (dptr < limit &&
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800553 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800554 (*shift)++;
Lars Immisch7da5bfb2007-01-30 14:24:57 -0800555 dptr++;
556 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800557
Patrick McHardyb1ec4882008-03-25 20:10:11 -0700558 if (dptr < limit && *dptr == '@') {
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800559 dptr++;
560 (*shift)++;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700561 } else {
562 dptr = start;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800563 *shift = s;
Patrick McHardyaa584ed2007-08-14 13:14:35 -0700564 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800565
566 return epaddr_len(ct, dptr, limit, shift);
567}
568
Patrick McHardyac367742008-03-25 20:18:40 -0700569/* Parse a SIP request line of the form:
570 *
571 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
572 *
573 * and return the offset and length of the address contained in the Request-URI.
574 */
575int ct_sip_parse_request(const struct nf_conn *ct,
576 const char *dptr, unsigned int datalen,
Patrick McHardy624f8b72008-03-25 20:19:30 -0700577 unsigned int *matchoff, unsigned int *matchlen,
578 union nf_inet_addr *addr, __be16 *port)
Patrick McHardyac367742008-03-25 20:18:40 -0700579{
Patrick McHardy624f8b72008-03-25 20:19:30 -0700580 const char *start = dptr, *limit = dptr + datalen, *end;
Patrick McHardyac367742008-03-25 20:18:40 -0700581 unsigned int mlen;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700582 unsigned int p;
Patrick McHardyac367742008-03-25 20:18:40 -0700583 int shift = 0;
584
585 /* Skip method and following whitespace */
586 mlen = string_len(ct, dptr, limit, NULL);
587 if (!mlen)
588 return 0;
589 dptr += mlen;
590 if (++dptr >= limit)
591 return 0;
592
593 /* Find SIP URI */
Patrick McHardy54101f42010-02-11 12:23:12 +0100594 for (; dptr < limit - strlen("sip:"); dptr++) {
Patrick McHardyac367742008-03-25 20:18:40 -0700595 if (*dptr == '\r' || *dptr == '\n')
596 return -1;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700597 if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) {
Patrick McHardy54101f42010-02-11 12:23:12 +0100598 dptr += strlen("sip:");
Patrick McHardyac367742008-03-25 20:18:40 -0700599 break;
Patrick McHardy54101f42010-02-11 12:23:12 +0100600 }
Patrick McHardyac367742008-03-25 20:18:40 -0700601 }
Patrick McHardy624f8b72008-03-25 20:19:30 -0700602 if (!skp_epaddr_len(ct, dptr, limit, &shift))
Patrick McHardyac367742008-03-25 20:18:40 -0700603 return 0;
Patrick McHardy624f8b72008-03-25 20:19:30 -0700604 dptr += shift;
605
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000606 if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
Patrick McHardy624f8b72008-03-25 20:19:30 -0700607 return -1;
608 if (end < limit && *end == ':') {
609 end++;
610 p = simple_strtoul(end, (char **)&end, 10);
611 if (p < 1024 || p > 65535)
612 return -1;
613 *port = htons(p);
614 } else
615 *port = htons(SIP_PORT);
616
617 if (end == dptr)
618 return 0;
619 *matchoff = dptr - start;
620 *matchlen = end - dptr;
Patrick McHardyac367742008-03-25 20:18:40 -0700621 return 1;
622}
623EXPORT_SYMBOL_GPL(ct_sip_parse_request);
624
Patrick McHardyea45f122008-03-25 20:18:57 -0700625/* SIP header parsing: SIP headers are located at the beginning of a line, but
626 * may span several lines, in which case the continuation lines begin with a
627 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
628 * CRLF, RFC 3261 allows only CRLF, we support both.
629 *
630 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
631 * whitespace and the values. Whitespace in this context means any amount of
632 * tabs, spaces and continuation lines, which are treated as a single whitespace
633 * character.
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700634 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800635 * Some headers may appear multiple times. A comma separated list of values is
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700636 * equivalent to multiple headers.
Patrick McHardyea45f122008-03-25 20:18:57 -0700637 */
638static const struct sip_header ct_sip_hdrs[] = {
Patrick McHardy30f33e62008-03-25 20:22:20 -0700639 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700640 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
641 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
642 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100643 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
644 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
Patrick McHardy0f32a402008-03-25 20:25:13 -0700645 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700646 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
Simon Horman001985b2010-08-22 21:37:51 +0900647 [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len),
Patrick McHardyea45f122008-03-25 20:18:57 -0700648};
649
650static const char *sip_follow_continuation(const char *dptr, const char *limit)
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800651{
Patrick McHardyea45f122008-03-25 20:18:57 -0700652 /* Walk past newline */
653 if (++dptr >= limit)
654 return NULL;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800655
Patrick McHardyea45f122008-03-25 20:18:57 -0700656 /* Skip '\n' in CR LF */
657 if (*(dptr - 1) == '\r' && *dptr == '\n') {
658 if (++dptr >= limit)
659 return NULL;
660 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800661
Patrick McHardyea45f122008-03-25 20:18:57 -0700662 /* Continuation line? */
663 if (*dptr != ' ' && *dptr != '\t')
664 return NULL;
665
666 /* skip leading whitespace */
667 for (; dptr < limit; dptr++) {
668 if (*dptr != ' ' && *dptr != '\t')
669 break;
670 }
671 return dptr;
672}
673
674static const char *sip_skip_whitespace(const char *dptr, const char *limit)
675{
676 for (; dptr < limit; dptr++) {
Marco Angaroni1bcabc82016-08-30 18:52:22 +0200677 if (*dptr == ' ' || *dptr == '\t')
Patrick McHardyea45f122008-03-25 20:18:57 -0700678 continue;
679 if (*dptr != '\r' && *dptr != '\n')
680 break;
681 dptr = sip_follow_continuation(dptr, limit);
Marco Angaroni68cb9fe2016-08-30 18:48:19 +0200682 break;
Patrick McHardyea45f122008-03-25 20:18:57 -0700683 }
684 return dptr;
685}
686
687/* Search within a SIP header value, dealing with continuation lines */
688static const char *ct_sip_header_search(const char *dptr, const char *limit,
689 const char *needle, unsigned int len)
690{
691 for (limit -= len; dptr < limit; dptr++) {
692 if (*dptr == '\r' || *dptr == '\n') {
693 dptr = sip_follow_continuation(dptr, limit);
694 if (dptr == NULL)
695 break;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800696 continue;
697 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800698
Rasmus Villemoes18082742014-10-13 15:54:31 -0700699 if (strncasecmp(dptr, needle, len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700700 return dptr;
701 }
702 return NULL;
703}
704
705int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
706 unsigned int dataoff, unsigned int datalen,
707 enum sip_header_types type,
708 unsigned int *matchoff, unsigned int *matchlen)
709{
710 const struct sip_header *hdr = &ct_sip_hdrs[type];
711 const char *start = dptr, *limit = dptr + datalen;
712 int shift = 0;
713
714 for (dptr += dataoff; dptr < limit; dptr++) {
715 /* Find beginning of line */
716 if (*dptr != '\r' && *dptr != '\n')
717 continue;
718 if (++dptr >= limit)
719 break;
720 if (*(dptr - 1) == '\r' && *dptr == '\n') {
721 if (++dptr >= limit)
722 break;
723 }
724
725 /* Skip continuation lines */
726 if (*dptr == ' ' || *dptr == '\t')
727 continue;
728
729 /* Find header. Compact headers must be followed by a
730 * non-alphabetic character to avoid mismatches. */
731 if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700732 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardyea45f122008-03-25 20:18:57 -0700733 dptr += hdr->len;
734 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
Rasmus Villemoes18082742014-10-13 15:54:31 -0700735 strncasecmp(dptr, hdr->cname, hdr->clen) == 0 &&
Patrick McHardy135d0182010-01-19 19:06:59 +0100736 !isalpha(*(dptr + hdr->clen)))
Patrick McHardyea45f122008-03-25 20:18:57 -0700737 dptr += hdr->clen;
738 else
739 continue;
740
741 /* Find and skip colon */
742 dptr = sip_skip_whitespace(dptr, limit);
743 if (dptr == NULL)
744 break;
745 if (*dptr != ':' || ++dptr >= limit)
746 break;
747
748 /* Skip whitespace after colon */
749 dptr = sip_skip_whitespace(dptr, limit);
750 if (dptr == NULL)
751 break;
752
753 *matchoff = dptr - start;
754 if (hdr->search) {
755 dptr = ct_sip_header_search(dptr, limit, hdr->search,
756 hdr->slen);
757 if (!dptr)
758 return -1;
759 dptr += hdr->slen;
760 }
761
762 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800763 if (!*matchlen)
764 return -1;
Patrick McHardyea45f122008-03-25 20:18:57 -0700765 *matchoff = dptr - start + shift;
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800766 return 1;
767 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800768 return 0;
769}
Patrick McHardyea45f122008-03-25 20:18:57 -0700770EXPORT_SYMBOL_GPL(ct_sip_get_header);
Patrick McHardy9fafcd72006-12-02 22:09:57 -0800771
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800772/* Get next header field in a list of comma separated values */
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700773static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
774 unsigned int dataoff, unsigned int datalen,
775 enum sip_header_types type,
776 unsigned int *matchoff, unsigned int *matchlen)
777{
778 const struct sip_header *hdr = &ct_sip_hdrs[type];
779 const char *start = dptr, *limit = dptr + datalen;
780 int shift = 0;
781
782 dptr += dataoff;
783
784 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
785 if (!dptr)
786 return 0;
787
788 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
789 if (!dptr)
790 return 0;
791 dptr += hdr->slen;
792
793 *matchoff = dptr - start;
794 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
795 if (!*matchlen)
796 return -1;
797 *matchoff += shift;
798 return 1;
799}
800
801/* Walk through headers until a parsable one is found or no header of the
802 * given type is left. */
803static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
804 unsigned int dataoff, unsigned int datalen,
805 enum sip_header_types type, int *in_header,
806 unsigned int *matchoff, unsigned int *matchlen)
807{
808 int ret;
809
810 if (in_header && *in_header) {
811 while (1) {
812 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
813 type, matchoff, matchlen);
814 if (ret > 0)
815 return ret;
816 if (ret == 0)
817 break;
818 dataoff += *matchoff;
819 }
820 *in_header = 0;
821 }
822
823 while (1) {
824 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
825 type, matchoff, matchlen);
826 if (ret > 0)
827 break;
828 if (ret == 0)
829 return ret;
830 dataoff += *matchoff;
831 }
832
833 if (in_header)
834 *in_header = 1;
835 return 1;
836}
837
838/* Locate a SIP header, parse the URI and return the offset and length of
839 * the address as well as the address and port themselves. A stream of
840 * headers can be parsed by handing in a non-NULL datalen and in_header
841 * pointer.
842 */
843int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
844 unsigned int *dataoff, unsigned int datalen,
845 enum sip_header_types type, int *in_header,
846 unsigned int *matchoff, unsigned int *matchlen,
847 union nf_inet_addr *addr, __be16 *port)
848{
849 const char *c, *limit = dptr + datalen;
850 unsigned int p;
851 int ret;
852
853 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
854 type, in_header, matchoff, matchlen);
855 WARN_ON(ret < 0);
856 if (ret == 0)
857 return ret;
858
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000859 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
Patrick McHardy05e3ced2008-03-25 20:19:13 -0700860 return -1;
861 if (*c == ':') {
862 c++;
863 p = simple_strtoul(c, (char **)&c, 10);
864 if (p < 1024 || p > 65535)
865 return -1;
866 *port = htons(p);
867 } else
868 *port = htons(SIP_PORT);
869
870 if (dataoff)
871 *dataoff = c - dptr;
872 return 1;
873}
874EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
875
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100876static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
877 unsigned int dataoff, unsigned int datalen,
878 const char *name,
879 unsigned int *matchoff, unsigned int *matchlen)
880{
881 const char *limit = dptr + datalen;
882 const char *start;
883 const char *end;
884
885 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
886 if (!limit)
887 limit = dptr + datalen;
888
889 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
890 if (!start)
891 return 0;
892 start += strlen(name);
893
894 end = ct_sip_header_search(start, limit, ";", strlen(";"));
895 if (!end)
896 end = limit;
897
898 *matchoff = start - dptr;
899 *matchlen = end - start;
900 return 1;
901}
902
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700903/* Parse address from header parameter and return address, offset and length */
904int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
905 unsigned int dataoff, unsigned int datalen,
906 const char *name,
907 unsigned int *matchoff, unsigned int *matchlen,
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000908 union nf_inet_addr *addr, bool delim)
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700909{
910 const char *limit = dptr + datalen;
911 const char *start, *end;
912
913 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
914 if (!limit)
915 limit = dptr + datalen;
916
917 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
918 if (!start)
919 return 0;
920
921 start += strlen(name);
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000922 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
Patrick McHardy2bbb2112008-03-25 20:24:24 -0700923 return 0;
924 *matchoff = start - dptr;
925 *matchlen = end - start;
926 return 1;
927}
928EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
929
930/* Parse numerical header parameter and return value, offset and length */
931int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
932 unsigned int dataoff, unsigned int datalen,
933 const char *name,
934 unsigned int *matchoff, unsigned int *matchlen,
935 unsigned int *val)
936{
937 const char *limit = dptr + datalen;
938 const char *start;
939 char *end;
940
941 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
942 if (!limit)
943 limit = dptr + datalen;
944
945 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
946 if (!start)
947 return 0;
948
949 start += strlen(name);
950 *val = simple_strtoul(start, &end, 0);
951 if (start == end)
952 return 0;
953 if (matchoff && matchlen) {
954 *matchoff = start - dptr;
955 *matchlen = end - start;
956 }
957 return 1;
958}
959EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
960
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100961static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
962 unsigned int dataoff, unsigned int datalen,
963 u8 *proto)
964{
965 unsigned int matchoff, matchlen;
966
967 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
968 &matchoff, &matchlen)) {
Rasmus Villemoes18082742014-10-13 15:54:31 -0700969 if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100970 *proto = IPPROTO_TCP;
Rasmus Villemoes18082742014-10-13 15:54:31 -0700971 else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP")))
Patrick McHardyf5b321b2010-02-11 12:26:19 +0100972 *proto = IPPROTO_UDP;
973 else
974 return 0;
975
976 if (*proto != nf_ct_protonum(ct))
977 return 0;
978 } else
979 *proto = nf_ct_protonum(ct);
980
981 return 1;
982}
983
Patrick McHardy02b69cb2012-08-09 10:08:46 +0000984static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
985 const char **endp, union nf_inet_addr *addr,
986 const char *limit)
987{
988 const char *end;
989 int ret;
990
991 memset(addr, 0, sizeof(*addr));
992 switch (nf_ct_l3num(ct)) {
993 case AF_INET:
994 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
995 break;
996 case AF_INET6:
997 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
998 break;
999 default:
1000 BUG();
1001 }
1002
1003 if (ret == 0)
1004 return 0;
1005 if (endp)
1006 *endp = end;
1007 return 1;
1008}
1009
1010/* skip ip address. returns its length. */
1011static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
1012 const char *limit, int *shift)
1013{
1014 union nf_inet_addr addr;
1015 const char *aux = dptr;
1016
1017 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
1018 pr_debug("ip: %s parse failed.!\n", dptr);
1019 return 0;
1020 }
1021
1022 return dptr - aux;
1023}
1024
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001025/* SDP header parsing: a SDP session description contains an ordered set of
1026 * headers, starting with a section containing general session parameters,
1027 * optionally followed by multiple media descriptions.
1028 *
1029 * SDP headers always start at the beginning of a line. According to RFC 2327:
1030 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
1031 * be tolerant and also accept records terminated with a single newline
1032 * character". We handle both cases.
1033 */
Patrick McHardy9a664822012-08-26 19:14:25 +02001034static const struct sip_header ct_sdp_hdrs_v4[] = {
1035 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
1036 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
1037 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
1038 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
1039};
1040
1041static const struct sip_header ct_sdp_hdrs_v6[] = {
1042 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
1043 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
1044 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
1045 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001046};
1047
1048/* Linear string search within SDP header values */
1049static const char *ct_sdp_header_search(const char *dptr, const char *limit,
1050 const char *needle, unsigned int len)
1051{
1052 for (limit -= len; dptr < limit; dptr++) {
1053 if (*dptr == '\r' || *dptr == '\n')
1054 break;
1055 if (strncmp(dptr, needle, len) == 0)
1056 return dptr;
1057 }
1058 return NULL;
1059}
1060
1061/* Locate a SDP header (optionally a substring within the header value),
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001062 * optionally stopping at the first occurrence of the term header, parse
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001063 * it and return the offset and length of the data we're interested in.
1064 */
1065int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
1066 unsigned int dataoff, unsigned int datalen,
1067 enum sdp_header_types type,
1068 enum sdp_header_types term,
1069 unsigned int *matchoff, unsigned int *matchlen)
1070{
Patrick McHardy9a664822012-08-26 19:14:25 +02001071 const struct sip_header *hdrs, *hdr, *thdr;
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001072 const char *start = dptr, *limit = dptr + datalen;
1073 int shift = 0;
1074
Patrick McHardy9a664822012-08-26 19:14:25 +02001075 hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
1076 hdr = &hdrs[type];
1077 thdr = &hdrs[term];
1078
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001079 for (dptr += dataoff; dptr < limit; dptr++) {
1080 /* Find beginning of line */
1081 if (*dptr != '\r' && *dptr != '\n')
1082 continue;
1083 if (++dptr >= limit)
1084 break;
1085 if (*(dptr - 1) == '\r' && *dptr == '\n') {
1086 if (++dptr >= limit)
1087 break;
1088 }
1089
1090 if (term != SDP_HDR_UNSPEC &&
1091 limit - dptr >= thdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -07001092 strncasecmp(dptr, thdr->name, thdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001093 break;
1094 else if (limit - dptr >= hdr->len &&
Rasmus Villemoes18082742014-10-13 15:54:31 -07001095 strncasecmp(dptr, hdr->name, hdr->len) == 0)
Patrick McHardy3e9b4600b2008-03-25 20:17:55 -07001096 dptr += hdr->len;
1097 else
1098 continue;
1099
1100 *matchoff = dptr - start;
1101 if (hdr->search) {
1102 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
1103 hdr->slen);
1104 if (!dptr)
1105 return -1;
1106 dptr += hdr->slen;
1107 }
1108
1109 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
1110 if (!*matchlen)
1111 return -1;
1112 *matchoff = dptr - start + shift;
1113 return 1;
1114 }
1115 return 0;
1116}
1117EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
1118
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001119static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
1120 unsigned int dataoff, unsigned int datalen,
1121 enum sdp_header_types type,
1122 enum sdp_header_types term,
1123 unsigned int *matchoff, unsigned int *matchlen,
1124 union nf_inet_addr *addr)
1125{
1126 int ret;
1127
1128 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
1129 matchoff, matchlen);
1130 if (ret <= 0)
1131 return ret;
1132
Patrick McHardy02b69cb2012-08-09 10:08:46 +00001133 if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
1134 dptr + *matchoff + *matchlen))
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001135 return -1;
1136 return 1;
1137}
1138
Patrick McHardy0f32a402008-03-25 20:25:13 -07001139static int refresh_signalling_expectation(struct nf_conn *ct,
1140 union nf_inet_addr *addr,
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001141 u8 proto, __be16 port,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001142 unsigned int expires)
1143{
1144 struct nf_conn_help *help = nfct_help(ct);
1145 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001146 struct hlist_node *next;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001147 int found = 0;
1148
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001149 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001150 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -07001151 if (exp->class != SIP_EXPECT_SIGNALLING ||
1152 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001153 exp->tuple.dst.protonum != proto ||
Patrick McHardy0f32a402008-03-25 20:25:13 -07001154 exp->tuple.dst.u.udp.port != port)
1155 continue;
1156 if (!del_timer(&exp->timeout))
1157 continue;
1158 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
1159 exp->timeout.expires = jiffies + expires * HZ;
1160 add_timer(&exp->timeout);
1161 found = 1;
1162 break;
1163 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001164 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001165 return found;
1166}
1167
1168static void flush_expectations(struct nf_conn *ct, bool media)
Patrick McHardy9467ee32008-03-25 20:24:04 -07001169{
1170 struct nf_conn_help *help = nfct_help(ct);
1171 struct nf_conntrack_expect *exp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001172 struct hlist_node *next;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001173
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001174 spin_lock_bh(&nf_conntrack_expect_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001175 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
Patrick McHardy0f32a402008-03-25 20:25:13 -07001176 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
1177 continue;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001178 if (!del_timer(&exp->timeout))
1179 continue;
1180 nf_ct_unlink_expect(exp);
1181 nf_ct_expect_put(exp);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001182 if (!media)
1183 break;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001184 }
Jesper Dangaard Brouerca7433d2014-03-03 14:46:01 +01001185 spin_unlock_bh(&nf_conntrack_expect_lock);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001186}
1187
Patrick McHardy051966c2012-08-26 19:14:04 +02001188static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
1189 unsigned int dataoff,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001190 const char **dptr, unsigned int *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001191 union nf_inet_addr *daddr, __be16 port,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001192 enum sip_expectation_classes class,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001193 unsigned int mediaoff, unsigned int medialen)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001194{
Patrick McHardya9c1d352008-03-25 20:25:49 -07001195 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
Patrick McHardy212440a2008-03-25 20:17:13 -07001196 enum ip_conntrack_info ctinfo;
1197 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Alexey Dobriyana5c3a802008-10-08 11:35:09 +02001198 struct net *net = nf_ct_net(ct);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001199 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardyd901a932008-03-25 20:25:32 -07001200 union nf_inet_addr *saddr;
1201 struct nf_conntrack_tuple tuple;
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001202 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
Patrick McHardya9c1d352008-03-25 20:25:49 -07001203 u_int16_t base_port;
1204 __be16 rtp_port, rtcp_port;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001205 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001206
Patrick McHardyd901a932008-03-25 20:25:32 -07001207 saddr = NULL;
1208 if (sip_direct_media) {
1209 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
1210 return NF_ACCEPT;
1211 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1212 }
1213
1214 /* We need to check whether the registration exists before attempting
1215 * to register it since we can see the same media description multiple
1216 * times on different connections in case multiple endpoints receive
1217 * the same call.
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001218 *
1219 * RTP optimization: if we find a matching media channel expectation
1220 * and both the expectation and this connection are SNATed, we assume
1221 * both sides can reach each other directly and use the final
1222 * destination address from the expectation. We still need to keep
1223 * the NATed expectations for media that might arrive from the
1224 * outside, and additionally need to expect the direct RTP stream
1225 * in case it passes through us even without NAT.
Patrick McHardyd901a932008-03-25 20:25:32 -07001226 */
1227 memset(&tuple, 0, sizeof(tuple));
1228 if (saddr)
1229 tuple.src.u3 = *saddr;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001230 tuple.src.l3num = nf_ct_l3num(ct);
Patrick McHardyd901a932008-03-25 20:25:32 -07001231 tuple.dst.protonum = IPPROTO_UDP;
1232 tuple.dst.u3 = *daddr;
1233 tuple.dst.u.udp.port = port;
1234
1235 rcu_read_lock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001236 do {
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +01001237 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
Patrick McHardyd901a932008-03-25 20:25:32 -07001238
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001239 if (!exp || exp->master == ct ||
1240 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
1241 exp->class != class)
1242 break;
Patrick McHardye1f9a462008-04-19 17:53:52 -07001243#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardy9a664822012-08-26 19:14:25 +02001244 if (!direct_rtp &&
1245 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001246 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
1247 ct->status & IPS_NAT_MASK) {
Patrick McHardy9a664822012-08-26 19:14:25 +02001248 *daddr = exp->saved_addr;
1249 tuple.dst.u3 = exp->saved_addr;
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001250 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
1251 direct_rtp = 1;
1252 } else
Patrick McHardye1f9a462008-04-19 17:53:52 -07001253#endif
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001254 skip_expect = 1;
1255 } while (!skip_expect);
Patrick McHardyd901a932008-03-25 20:25:32 -07001256
Patrick McHardya9c1d352008-03-25 20:25:49 -07001257 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
1258 rtp_port = htons(base_port);
1259 rtcp_port = htons(base_port + 1);
1260
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001261 if (direct_rtp) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001262 hooks = rcu_dereference(nf_nat_sip_hooks);
1263 if (hooks &&
1264 !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001265 mediaoff, medialen, ntohs(rtp_port)))
1266 goto err1;
1267 }
1268
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001269 if (skip_expect) {
1270 rcu_read_unlock();
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001271 return NF_ACCEPT;
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001272 }
Patrick McHardyc7f485a2008-03-25 20:26:43 -07001273
Patrick McHardya9c1d352008-03-25 20:25:49 -07001274 rtp_exp = nf_ct_expect_alloc(ct);
1275 if (rtp_exp == NULL)
1276 goto err1;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001277 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001278 IPPROTO_UDP, NULL, &rtp_port);
1279
1280 rtcp_exp = nf_ct_expect_alloc(ct);
1281 if (rtcp_exp == NULL)
1282 goto err2;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001283 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
Patrick McHardya9c1d352008-03-25 20:25:49 -07001284 IPPROTO_UDP, NULL, &rtcp_port);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001285
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001286 hooks = rcu_dereference(nf_nat_sip_hooks);
1287 if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
1288 ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
1289 datalen, rtp_exp, rtcp_exp,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001290 mediaoff, medialen, daddr);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001291 else {
Patrick McHardya9c1d352008-03-25 20:25:49 -07001292 if (nf_ct_expect_related(rtp_exp) == 0) {
1293 if (nf_ct_expect_related(rtcp_exp) != 0)
1294 nf_ct_unexpect_related(rtp_exp);
1295 else
1296 ret = NF_ACCEPT;
1297 }
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001298 }
Patrick McHardya9c1d352008-03-25 20:25:49 -07001299 nf_ct_expect_put(rtcp_exp);
1300err2:
1301 nf_ct_expect_put(rtp_exp);
1302err1:
holger@eitzenberger.orgb21613a2013-09-20 22:43:04 +02001303 rcu_read_unlock();
Patrick McHardy9fafcd72006-12-02 22:09:57 -08001304 return ret;
1305}
1306
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001307static const struct sdp_media_type sdp_media_types[] = {
1308 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
1309 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
Patrick McHardy9d288df2010-02-11 12:30:21 +01001310 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001311};
1312
1313static const struct sdp_media_type *sdp_media_type(const char *dptr,
1314 unsigned int matchoff,
1315 unsigned int matchlen)
1316{
1317 const struct sdp_media_type *t;
1318 unsigned int i;
1319
1320 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
1321 t = &sdp_media_types[i];
1322 if (matchlen < t->len ||
1323 strncmp(dptr + matchoff, t->name, t->len))
1324 continue;
1325 return t;
1326 }
1327 return NULL;
1328}
1329
Patrick McHardy051966c2012-08-26 19:14:04 +02001330static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1331 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001332 const char **dptr, unsigned int *datalen,
1333 unsigned int cseq)
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001334{
1335 enum ip_conntrack_info ctinfo;
1336 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001337 unsigned int matchoff, matchlen;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001338 unsigned int mediaoff, medialen;
1339 unsigned int sdpoff;
1340 unsigned int caddr_len, maddr_len;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001341 unsigned int i;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001342 union nf_inet_addr caddr, maddr, rtp_addr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001343 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001344 unsigned int port;
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001345 const struct sdp_media_type *t;
1346 int ret = NF_ACCEPT;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001347
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001348 hooks = rcu_dereference(nf_nat_sip_hooks);
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001349
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001350 /* Find beginning of session description */
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001351 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001352 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001353 &matchoff, &matchlen) <= 0)
1354 return NF_ACCEPT;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001355 sdpoff = matchoff;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001356
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001357 /* The connection information is contained in the session description
1358 * and/or once per media description. The first media description marks
1359 * the end of the session description. */
1360 caddr_len = 0;
1361 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001362 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001363 &matchoff, &matchlen, &caddr) > 0)
1364 caddr_len = matchlen;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001365
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001366 mediaoff = sdpoff;
1367 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1368 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1369 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1370 &mediaoff, &medialen) <= 0)
1371 break;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001372
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001373 /* Get media type and port number. A media port value of zero
1374 * indicates an inactive stream. */
1375 t = sdp_media_type(*dptr, mediaoff, medialen);
1376 if (!t) {
1377 mediaoff += medialen;
1378 continue;
1379 }
1380 mediaoff += t->len;
1381 medialen -= t->len;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001382
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001383 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1384 if (port == 0)
1385 continue;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001386 if (port < 1024 || port > 65535) {
1387 nf_ct_helper_log(skb, ct, "wrong port %u", port);
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001388 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001389 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001390
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001391 /* The media description overrides the session description. */
1392 maddr_len = 0;
1393 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
Patrick McHardy9a664822012-08-26 19:14:25 +02001394 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001395 &matchoff, &matchlen, &maddr) > 0) {
1396 maddr_len = matchlen;
1397 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1398 } else if (caddr_len)
1399 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001400 else {
1401 nf_ct_helper_log(skb, ct, "cannot parse SDP message");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001402 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001403 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001404
Patrick McHardy051966c2012-08-26 19:14:04 +02001405 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1406 dptr, datalen,
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001407 &rtp_addr, htons(port), t->class,
1408 mediaoff, medialen);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001409 if (ret != NF_ACCEPT) {
1410 nf_ct_helper_log(skb, ct,
1411 "cannot add expectation for voice");
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001412 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001413 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001414
1415 /* Update media connection address if present */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001416 if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1417 ret = hooks->sdp_addr(skb, protoff, dataoff,
Patrick McHardy9a664822012-08-26 19:14:25 +02001418 dptr, datalen, mediaoff,
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001419 SDP_HDR_CONNECTION,
1420 SDP_HDR_MEDIA,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001421 &rtp_addr);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001422 if (ret != NF_ACCEPT) {
1423 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001424 return ret;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001425 }
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001426 }
Patrick McHardy0d0ab032008-03-25 20:26:24 -07001427 i++;
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001428 }
1429
1430 /* Update session connection and owner addresses */
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001431 hooks = rcu_dereference(nf_nat_sip_hooks);
1432 if (hooks && ct->status & IPS_NAT_MASK)
1433 ret = hooks->sdp_session(skb, protoff, dataoff,
1434 dptr, datalen, sdpoff,
1435 &rtp_addr);
Patrick McHardy4ab9e642008-03-25 20:26:08 -07001436
1437 return ret;
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001438}
Patrick McHardy051966c2012-08-26 19:14:04 +02001439static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1440 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001441 const char **dptr, unsigned int *datalen,
1442 unsigned int cseq, unsigned int code)
1443{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001444 enum ip_conntrack_info ctinfo;
1445 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001446 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001447
Patrick McHardy30f33e62008-03-25 20:22:20 -07001448 if ((code >= 100 && code <= 199) ||
1449 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001450 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001451 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001452 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001453 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001454}
1455
Patrick McHardy051966c2012-08-26 19:14:04 +02001456static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1457 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001458 const char **dptr, unsigned int *datalen,
1459 unsigned int cseq, unsigned int code)
1460{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001461 enum ip_conntrack_info ctinfo;
1462 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001463 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001464
Patrick McHardy30f33e62008-03-25 20:22:20 -07001465 if ((code >= 100 && code <= 199) ||
1466 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001467 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001468 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001469 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001470 return NF_ACCEPT;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001471}
1472
Patrick McHardy051966c2012-08-26 19:14:04 +02001473static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1474 unsigned int dataoff,
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001475 const char **dptr, unsigned int *datalen,
1476 unsigned int cseq, unsigned int code)
1477{
Patrick McHardy9467ee32008-03-25 20:24:04 -07001478 enum ip_conntrack_info ctinfo;
1479 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001480 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9467ee32008-03-25 20:24:04 -07001481
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001482 if ((code >= 100 && code <= 199) ||
1483 (code >= 200 && code <= 299))
Patrick McHardy051966c2012-08-26 19:14:04 +02001484 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001485 else if (ct_sip_info->invite_cseq == cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001486 flush_expectations(ct, true);
Patrick McHardyef75d492008-05-08 01:15:21 -07001487 return NF_ACCEPT;
Patrick McHardy9467ee32008-03-25 20:24:04 -07001488}
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001489
Patrick McHardy051966c2012-08-26 19:14:04 +02001490static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1491 unsigned int dataoff,
Patrick McHardy9d288df2010-02-11 12:30:21 +01001492 const char **dptr, unsigned int *datalen,
1493 unsigned int cseq)
1494{
1495 enum ip_conntrack_info ctinfo;
1496 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001497 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001498 unsigned int ret;
1499
1500 flush_expectations(ct, true);
Patrick McHardy051966c2012-08-26 19:14:04 +02001501 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
Patrick McHardy9d288df2010-02-11 12:30:21 +01001502 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001503 ct_sip_info->invite_cseq = cseq;
Patrick McHardy9d288df2010-02-11 12:30:21 +01001504 return ret;
1505}
1506
Patrick McHardy051966c2012-08-26 19:14:04 +02001507static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1508 unsigned int dataoff,
Patrick McHardy9467ee32008-03-25 20:24:04 -07001509 const char **dptr, unsigned int *datalen,
1510 unsigned int cseq)
1511{
1512 enum ip_conntrack_info ctinfo;
1513 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1514
Patrick McHardy0f32a402008-03-25 20:25:13 -07001515 flush_expectations(ct, true);
1516 return NF_ACCEPT;
1517}
1518
1519/* Parse a REGISTER request and create a permanent expectation for incoming
1520 * signalling connections. The expectation is marked inactive and is activated
1521 * when receiving a response indicating success from the registrar.
1522 */
Patrick McHardy051966c2012-08-26 19:14:04 +02001523static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1524 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001525 const char **dptr, unsigned int *datalen,
1526 unsigned int cseq)
1527{
1528 enum ip_conntrack_info ctinfo;
1529 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001530 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001531 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001532 unsigned int matchoff, matchlen;
1533 struct nf_conntrack_expect *exp;
1534 union nf_inet_addr *saddr, daddr;
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001535 const struct nf_nat_sip_hooks *hooks;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001536 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001537 u8 proto;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001538 unsigned int expires = 0;
1539 int ret;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001540
1541 /* Expected connections can not register again. */
1542 if (ct->status & IPS_EXPECTED)
1543 return NF_ACCEPT;
1544
1545 /* We must check the expiration time: a value of zero signals the
1546 * registrar to release the binding. We'll remove our expectation
1547 * when receiving the new bindings in the response, but we don't
1548 * want to create new ones.
1549 *
1550 * The expiration time may be contained in Expires: header, the
1551 * Contact: header parameters or the URI parameters.
1552 */
1553 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1554 &matchoff, &matchlen) > 0)
1555 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1556
1557 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1558 SIP_HDR_CONTACT, NULL,
1559 &matchoff, &matchlen, &daddr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001560 if (ret < 0) {
1561 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001562 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001563 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001564 return NF_ACCEPT;
1565
1566 /* We don't support third-party registrations */
1567 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1568 return NF_ACCEPT;
1569
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001570 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1571 &proto) == 0)
1572 return NF_ACCEPT;
1573
Patrick McHardy0f32a402008-03-25 20:25:13 -07001574 if (ct_sip_parse_numerical_param(ct, *dptr,
1575 matchoff + matchlen, *datalen,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001576 "expires=", NULL, NULL, &expires) < 0) {
1577 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001578 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001579 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001580
1581 if (expires == 0) {
1582 ret = NF_ACCEPT;
1583 goto store_cseq;
1584 }
1585
1586 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001587 if (!exp) {
1588 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001589 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001590 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001591
1592 saddr = NULL;
1593 if (sip_direct_signalling)
1594 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1595
Patrick McHardy5e8fbe22008-04-14 11:15:52 +02001596 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001597 saddr, &daddr, proto, NULL, &port);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001598 exp->timeout.expires = sip_timeout * HZ;
1599 exp->helper = nfct_help(ct)->helper;
1600 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1601
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001602 hooks = rcu_dereference(nf_nat_sip_hooks);
1603 if (hooks && ct->status & IPS_NAT_MASK)
1604 ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1605 exp, matchoff, matchlen);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001606 else {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001607 if (nf_ct_expect_related(exp) != 0) {
1608 nf_ct_helper_log(skb, ct, "cannot add expectation");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001609 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001610 } else
Patrick McHardy0f32a402008-03-25 20:25:13 -07001611 ret = NF_ACCEPT;
1612 }
1613 nf_ct_expect_put(exp);
1614
1615store_cseq:
1616 if (ret == NF_ACCEPT)
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001617 ct_sip_info->register_cseq = cseq;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001618 return ret;
1619}
1620
Patrick McHardy051966c2012-08-26 19:14:04 +02001621static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1622 unsigned int dataoff,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001623 const char **dptr, unsigned int *datalen,
1624 unsigned int cseq, unsigned int code)
1625{
1626 enum ip_conntrack_info ctinfo;
1627 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001628 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
Patrick McHardy0f32a402008-03-25 20:25:13 -07001629 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1630 union nf_inet_addr addr;
1631 __be16 port;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001632 u8 proto;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001633 unsigned int matchoff, matchlen, coff = 0;
Patrick McHardy0f32a402008-03-25 20:25:13 -07001634 unsigned int expires = 0;
1635 int in_contact = 0, ret;
1636
1637 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1638 * they have received a final response from the registrar for the
1639 * previous one or the previous REGISTER request has timed out".
1640 *
1641 * However, some servers fail to detect retransmissions and send late
1642 * responses, so we store the sequence number of the last valid
1643 * request and compare it here.
1644 */
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +02001645 if (ct_sip_info->register_cseq != cseq)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001646 return NF_ACCEPT;
1647
1648 if (code >= 100 && code <= 199)
1649 return NF_ACCEPT;
1650 if (code < 200 || code > 299)
1651 goto flush;
1652
1653 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1654 &matchoff, &matchlen) > 0)
1655 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1656
1657 while (1) {
1658 unsigned int c_expires = expires;
1659
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001660 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
Patrick McHardy0f32a402008-03-25 20:25:13 -07001661 SIP_HDR_CONTACT, &in_contact,
1662 &matchoff, &matchlen,
1663 &addr, &port);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001664 if (ret < 0) {
1665 nf_ct_helper_log(skb, ct, "cannot parse contact");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001666 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001667 } else if (ret == 0)
Patrick McHardy0f32a402008-03-25 20:25:13 -07001668 break;
1669
1670 /* We don't support third-party registrations */
1671 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1672 continue;
1673
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001674 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1675 *datalen, &proto) == 0)
1676 continue;
1677
Patrick McHardy0f32a402008-03-25 20:25:13 -07001678 ret = ct_sip_parse_numerical_param(ct, *dptr,
1679 matchoff + matchlen,
1680 *datalen, "expires=",
1681 NULL, NULL, &c_expires);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001682 if (ret < 0) {
1683 nf_ct_helper_log(skb, ct, "cannot parse expires");
Patrick McHardy0f32a402008-03-25 20:25:13 -07001684 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001685 }
Patrick McHardy0f32a402008-03-25 20:25:13 -07001686 if (c_expires == 0)
1687 break;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001688 if (refresh_signalling_expectation(ct, &addr, proto, port,
1689 c_expires))
Patrick McHardy0f32a402008-03-25 20:25:13 -07001690 return NF_ACCEPT;
1691 }
1692
1693flush:
1694 flush_expectations(ct, false);
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001695 return NF_ACCEPT;
1696}
1697
Patrick McHardy30f33e62008-03-25 20:22:20 -07001698static const struct sip_handler sip_handlers[] = {
Patrick McHardy9d288df2010-02-11 12:30:21 +01001699 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001700 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
Patrick McHardy595a8ec2008-03-25 20:22:53 -07001701 SIP_HANDLER("ACK", process_sdp, NULL),
1702 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
Patrick McHardy9467ee32008-03-25 20:24:04 -07001703 SIP_HANDLER("BYE", process_bye_request, NULL),
Patrick McHardy0f32a402008-03-25 20:25:13 -07001704 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
Patrick McHardy30f33e62008-03-25 20:22:20 -07001705};
1706
Patrick McHardy051966c2012-08-26 19:14:04 +02001707static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1708 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001709 const char **dptr, unsigned int *datalen)
1710{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001711 enum ip_conntrack_info ctinfo;
1712 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001713 unsigned int matchoff, matchlen, matchend;
1714 unsigned int code, cseq, i;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001715
1716 if (*datalen < strlen("SIP/2.0 200"))
1717 return NF_ACCEPT;
1718 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001719 if (!code) {
1720 nf_ct_helper_log(skb, ct, "cannot get code");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001721 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001722 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001723
1724 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001725 &matchoff, &matchlen) <= 0) {
1726 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001727 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001728 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001729 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001730 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001731 nf_ct_helper_log(skb, ct, "cannot get cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001732 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001733 }
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001734 matchend = matchoff + matchlen + 1;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001735
1736 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001737 const struct sip_handler *handler;
1738
Patrick McHardy30f33e62008-03-25 20:22:20 -07001739 handler = &sip_handlers[i];
1740 if (handler->response == NULL)
1741 continue;
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001742 if (*datalen < matchend + handler->len ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001743 strncasecmp(*dptr + matchend, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001744 continue;
Patrick McHardy051966c2012-08-26 19:14:04 +02001745 return handler->response(skb, protoff, dataoff, dptr, datalen,
Patrick McHardy3b6b9fa2010-02-11 12:23:53 +01001746 cseq, code);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001747 }
1748 return NF_ACCEPT;
1749}
1750
Patrick McHardy051966c2012-08-26 19:14:04 +02001751static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1752 unsigned int dataoff,
Patrick McHardy30f33e62008-03-25 20:22:20 -07001753 const char **dptr, unsigned int *datalen)
1754{
Patrick McHardy30f33e62008-03-25 20:22:20 -07001755 enum ip_conntrack_info ctinfo;
1756 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Kevin Cernekee72665072012-12-17 18:33:58 +00001757 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1758 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001759 unsigned int matchoff, matchlen;
1760 unsigned int cseq, i;
Kevin Cernekee72665072012-12-17 18:33:58 +00001761 union nf_inet_addr addr;
1762 __be16 port;
1763
1764 /* Many Cisco IP phones use a high source port for SIP requests, but
1765 * listen for the response on port 5060. If we are the local
1766 * router for one of these phones, save the port number from the
1767 * Via: header so that nf_nat_sip can redirect the responses to
1768 * the correct port.
1769 */
1770 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1771 SIP_HDR_VIA_UDP, NULL, &matchoff,
1772 &matchlen, &addr, &port) > 0 &&
1773 port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1774 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1775 ct_sip_info->forced_dport = port;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001776
1777 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
Alexey Dobriyan66bf7912008-09-07 18:19:25 -07001778 const struct sip_handler *handler;
1779
Patrick McHardy30f33e62008-03-25 20:22:20 -07001780 handler = &sip_handlers[i];
1781 if (handler->request == NULL)
1782 continue;
Ulrich Weber444f9012016-10-24 18:07:23 +02001783 if (*datalen < handler->len + 2 ||
Rasmus Villemoes18082742014-10-13 15:54:31 -07001784 strncasecmp(*dptr, handler->method, handler->len))
Patrick McHardy30f33e62008-03-25 20:22:20 -07001785 continue;
Ulrich Weber444f9012016-10-24 18:07:23 +02001786 if ((*dptr)[handler->len] != ' ' ||
1787 !isalpha((*dptr)[handler->len+1]))
1788 continue;
Patrick McHardy30f33e62008-03-25 20:22:20 -07001789
1790 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001791 &matchoff, &matchlen) <= 0) {
1792 nf_ct_helper_log(skb, ct, "cannot parse cseq");
Patrick McHardy30f33e62008-03-25 20:22:20 -07001793 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001794 }
Patrick McHardy30f33e62008-03-25 20:22:20 -07001795 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
Christophe Leroy0d35d0812016-08-03 12:41:40 +02001796 if (!cseq && *(*dptr + matchoff) != '0') {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001797 nf_ct_helper_log(skb, ct, "cannot get 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
Patrick McHardy051966c2012-08-26 19:14:04 +02001801 return handler->request(skb, protoff, dataoff, dptr, datalen,
1802 cseq);
Patrick McHardy30f33e62008-03-25 20:22:20 -07001803 }
1804 return NF_ACCEPT;
1805}
Patrick McHardy7d3dd042008-03-25 20:19:46 -07001806
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001807static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
Patrick McHardy051966c2012-08-26 19:14:04 +02001808 unsigned int protoff, unsigned int dataoff,
1809 const char **dptr, unsigned int *datalen)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001810{
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001811 const struct nf_nat_sip_hooks *hooks;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001812 int ret;
1813
Tyler Weard2aeec02013-09-20 12:12:07 -07001814 if (nf_ct_disable_sip_alg)
1815 return NF_ACCEPT;
Rasmus Villemoes18082742014-10-13 15:54:31 -07001816 if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
Patrick McHardy051966c2012-08-26 19:14:04 +02001817 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001818 else
Patrick McHardy051966c2012-08-26 19:14:04 +02001819 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001820
Patrick McHardy9a664822012-08-26 19:14:25 +02001821 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02001822 hooks = rcu_dereference(nf_nat_sip_hooks);
1823 if (hooks && !hooks->msg(skb, protoff, dataoff,
1824 dptr, datalen)) {
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001825 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001826 ret = NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001827 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001828 }
1829
1830 return ret;
1831}
1832
1833static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1834 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1835{
1836 struct tcphdr *th, _tcph;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301837 unsigned int dataoff;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001838 unsigned int matchoff, matchlen, clen;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001839 const char *dptr, *end;
1840 s16 diff, tdiff = 0;
Simon Horman78748962010-09-21 21:17:30 +00001841 int ret = NF_ACCEPT;
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001842 bool term;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301843 unsigned int datalen = 0, msglen = 0, origlen = 0;
1844 unsigned int dataoff_orig = 0;
1845 unsigned int splitlen, oldlen, oldlen1;
1846 struct sip_list *sip_entry = NULL;
1847 bool skip_sip_process = false;
1848 bool do_not_process = false;
1849 bool skip = false;
1850 bool skb_is_combined = false;
1851 enum ip_conntrack_dir dir = IP_CT_DIR_MAX;
1852 struct sk_buff *combined_skb = NULL;
1853 bool content_len_exists = 1;
1854
1855 packet_count++;
1856 pr_debug("packet count %d\n", packet_count);
1857
1858 if (nf_ct_disable_sip_alg)
1859 return NF_ACCEPT;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001860
1861 if (ctinfo != IP_CT_ESTABLISHED &&
Eric Dumazetfb048832011-05-19 15:44:27 +02001862 ctinfo != IP_CT_ESTABLISHED_REPLY)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001863 return NF_ACCEPT;
1864
1865 /* No Data ? */
1866 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1867 if (th == NULL)
1868 return NF_ACCEPT;
1869 dataoff = protoff + th->doff * 4;
1870 if (dataoff >= skb->len)
1871 return NF_ACCEPT;
1872
1873 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1874
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02001875 if (unlikely(skb_linearize(skb)))
1876 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001877
1878 dptr = skb->data + dataoff;
1879 datalen = skb->len - dataoff;
1880 if (datalen < strlen("SIP/2.0 200"))
1881 return NF_ACCEPT;
1882
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301883 /* here we save the original datalength and data offset of the skb, this
1884 * is needed later to split combined skbs
1885 */
1886 oldlen1 = skb->len - protoff;
1887 dataoff_orig = dataoff;
1888
1889 if (!ct)
1890 return NF_DROP;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001891 while (1) {
1892 if (ct_sip_get_header(ct, dptr, 0, datalen,
1893 SIP_HDR_CONTENT_LENGTH,
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301894 &matchoff, &matchlen) <= 0){
1895 if (nf_ct_enable_sip_segmentation) {
1896 do_not_process = true;
1897 content_len_exists = 0;
1898 goto destination;
1899 } else {
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001900 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301901 }
1902 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001903
1904 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1905 if (dptr + matchoff == end)
1906 break;
1907
Patrick McHardye6e4d9e2011-05-16 14:45:39 +02001908 term = false;
1909 for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1910 if (end[0] == '\r' && end[1] == '\n' &&
1911 end[2] == '\r' && end[3] == '\n') {
1912 term = true;
1913 break;
1914 }
1915 }
1916 if (!term)
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001917 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301918
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001919 end += strlen("\r\n\r\n") + clen;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301920destination:
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001921
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301922 if (content_len_exists == 0) {
1923 origlen = datalen;
1924 msglen = origlen;
1925 } else {
1926 origlen = end - dptr;
1927 msglen = origlen;
1928 }
1929 pr_debug("mslgen %d datalen %d\n", msglen, datalen);
1930 dir = CTINFO2DIR(ctinfo);
1931 combined_skb = skb;
1932 if (nf_ct_enable_sip_segmentation) {
1933 /* Segmented Packet */
1934 if (msglen > datalen) {
1935 skip = sip_save_segment_info(ct, skb, msglen,
1936 datalen, dptr,
1937 ctinfo);
1938 if (!skip)
1939 return NF_QUEUE;
1940 }
1941 /* Traverse list to find prev segment */
1942 /*Traverse the list if list non empty */
1943 if (((&ct->sip_segment_list)->next) !=
1944 (&ct->sip_segment_list)) {
1945 /* Combine segments if they are fragments of
1946 * the same message.
1947 */
1948 sip_entry = sip_coalesce_segments(ct, &skb,
1949 dataoff,
1950 &combined_skb,
1951 &skip_sip_process,
1952 do_not_process,
1953 ctinfo,
1954 &skb_is_combined);
1955 sip_update_params(dir, &msglen, &origlen, &dptr,
1956 &datalen,
1957 skb_is_combined, ct);
1958
1959 if (skip_sip_process)
1960 goto here;
1961 } else if (do_not_process) {
1962 goto here;
1963 }
1964 } else if (msglen > datalen) {
Patrick McHardy3a7b21e2013-04-05 08:13:30 +00001965 return NF_ACCEPT;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301966 }
1967 /* process the combined skb having the complete SIP message */
1968 ret = process_sip_msg(combined_skb, ct, protoff, dataoff,
Patrick McHardy051966c2012-08-26 19:14:04 +02001969 &dptr, &msglen);
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301970
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +01001971 /* process_sip_* functions report why this packet is dropped */
Patrick McHardyf5b321b2010-02-11 12:26:19 +01001972 if (ret != NF_ACCEPT)
1973 break;
Mohammed Javidf4530ef2017-11-20 20:15:46 +05301974 sip_calculate_parameters(&diff, &tdiff, &dataoff, &dptr,
1975 &datalen, msglen, origlen);
1976 if (nf_ct_enable_sip_segmentation && skb_is_combined)
1977 break;
1978 }
1979 if (skb_is_combined) {
1980 /* once combined skb is processed, split the skbs again The
1981 * length to split at is the same as length of first skb. Any
1982 * changes in the combined skb length because of SIP processing
1983 * will reflect in the second fragment
1984 */
1985 splitlen = (dir == IP_CT_DIR_ORIGINAL) ?
1986 ct->segment.skb_len[0] : ct->segment.skb_len[1];
1987 oldlen = combined_skb->len - protoff;
1988 skb_split(combined_skb, skb, splitlen);
1989 /* Headers need to be recalculated since during SIP processing
1990 * headers are calculated based on the change in length of the
1991 * combined message
1992 */
1993 recalc_header(combined_skb, splitlen, oldlen, protoff);
1994 /* Reinject the first skb now that the processing is complete */
1995 if (sip_entry) {
1996 nf_reinject(sip_entry->entry, NF_ACCEPT);
1997 kfree(sip_entry);
1998 }
1999 skb->len = (oldlen1 + protoff) + tdiff - dataoff_orig;
2000 /* After splitting, push the headers back to the first skb which
2001 * were removed before combining the skbs.This moves the skb
2002 * begin pointer back to the beginning of its headers
2003 */
2004 skb_push(skb, dataoff_orig);
2005 /* Since the length of this second segment willbe affected
2006 * because of SIP processing,we need to recalculate its header
2007 * as well.
2008 */
2009 recalc_header(skb, skb->len, oldlen1, protoff);
2010 /* Now that the processing is done and the first skb reinjected.
2011 * We allow addition of fragmented skbs to the list for this
2012 * direction
2013 */
2014 if (dir == IP_CT_DIR_ORIGINAL)
2015 ct->sip_original_dir = 0;
2016 else
2017 ct->sip_reply_dir = 0;
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002018 }
2019
Mohammed Javidf4530ef2017-11-20 20:15:46 +05302020here:
2021
2022 if (ret == NF_ACCEPT && ct && ct->status & IPS_NAT_MASK) {
holger@eitzenberger.org180cf722013-09-30 17:07:28 +02002023 const struct nf_nat_sip_hooks *hooks;
2024
2025 hooks = rcu_dereference(nf_nat_sip_hooks);
2026 if (hooks)
2027 hooks->seq_adjust(skb, protoff, tdiff);
Patrick McHardy48f8ac22010-02-11 12:29:38 +01002028 }
2029
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002030 return ret;
2031}
2032
2033static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
2034 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002035{
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002036 unsigned int dataoff, datalen;
2037 const char *dptr;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002038
2039 /* No Data ? */
2040 dataoff = protoff + sizeof(struct udphdr);
Herbert Xu3db05fe2007-10-15 00:53:15 -07002041 if (dataoff >= skb->len)
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002042 return NF_ACCEPT;
2043
Herbert Xu3db05fe2007-10-15 00:53:15 -07002044 nf_ct_refresh(ct, skb, sip_timeout * HZ);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002045
Patrick McHardya1d7c1b2010-05-14 21:18:17 +02002046 if (unlikely(skb_linearize(skb)))
2047 return NF_DROP;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002048
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002049 dptr = skb->data + dataoff;
Herbert Xu3db05fe2007-10-15 00:53:15 -07002050 datalen = skb->len - dataoff;
Patrick McHardy779382e2008-03-25 20:17:36 -07002051 if (datalen < strlen("SIP/2.0 200"))
Patrick McHardy7d3dd042008-03-25 20:19:46 -07002052 return NF_ACCEPT;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002053
Patrick McHardy051966c2012-08-26 19:14:04 +02002054 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002055}
2056
Gao Feng82de0be2016-07-18 11:39:23 +08002057static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002058
Patrick McHardy0f32a402008-03-25 20:25:13 -07002059static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
2060 [SIP_EXPECT_SIGNALLING] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002061 .name = "signalling",
Patrick McHardy0f32a402008-03-25 20:25:13 -07002062 .max_expected = 1,
2063 .timeout = 3 * 60,
2064 },
2065 [SIP_EXPECT_AUDIO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002066 .name = "audio",
Patrick McHardya9c1d352008-03-25 20:25:49 -07002067 .max_expected = 2 * IP_CT_DIR_MAX,
Patrick McHardy0f32a402008-03-25 20:25:13 -07002068 .timeout = 3 * 60,
2069 },
Patrick McHardy0d0ab032008-03-25 20:26:24 -07002070 [SIP_EXPECT_VIDEO] = {
Patrick McHardyb87921b2010-02-11 12:22:48 +01002071 .name = "video",
Patrick McHardy0d0ab032008-03-25 20:26:24 -07002072 .max_expected = 2 * IP_CT_DIR_MAX,
2073 .timeout = 3 * 60,
2074 },
Patrick McHardy9d288df2010-02-11 12:30:21 +01002075 [SIP_EXPECT_IMAGE] = {
2076 .name = "image",
2077 .max_expected = IP_CT_DIR_MAX,
2078 .timeout = 3 * 60,
2079 },
Patrick McHardy6002f2662008-03-25 20:09:15 -07002080};
2081
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002082static void nf_conntrack_sip_fini(void)
2083{
Gao Feng82de0be2016-07-18 11:39:23 +08002084 nf_conntrack_helpers_unregister(sip, ports_c * 4);
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002085}
2086
2087static int __init nf_conntrack_sip_init(void)
2088{
Gao Feng82de0be2016-07-18 11:39:23 +08002089 int i, ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002090
Tyler Wear2e6eecd2014-05-16 11:58:23 -07002091 sip_sysctl_header = register_net_sysctl(&init_net, "net/netfilter",
2092 sip_sysctl_tbl);
Tyler Weard2aeec02013-09-20 12:12:07 -07002093 if (!sip_sysctl_header)
2094 pr_debug("nf_ct_sip:Unable to register SIP systbl\n");
2095
2096 if (nf_ct_disable_sip_alg)
2097 pr_debug("nf_ct_sip: SIP ALG disabled\n");
2098 else
2099 pr_debug("nf_ct_sip: SIP ALG enabled\n");
2100
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002101 if (ports_c == 0)
2102 ports[ports_c++] = SIP_PORT;
2103
2104 for (i = 0; i < ports_c; i++) {
Gao Feng82de0be2016-07-18 11:39:23 +08002105 nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP, "sip",
2106 SIP_PORT, ports[i], i, sip_exp_policy,
2107 SIP_EXPECT_MAX,
2108 sizeof(struct nf_ct_sip_master), sip_help_udp,
2109 NULL, THIS_MODULE);
2110 nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP, "sip",
2111 SIP_PORT, ports[i], i, sip_exp_policy,
2112 SIP_EXPECT_MAX,
2113 sizeof(struct nf_ct_sip_master), sip_help_tcp,
2114 NULL, THIS_MODULE);
2115 nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP, "sip",
2116 SIP_PORT, ports[i], i, sip_exp_policy,
2117 SIP_EXPECT_MAX,
2118 sizeof(struct nf_ct_sip_master), sip_help_udp,
2119 NULL, THIS_MODULE);
2120 nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP, "sip",
2121 SIP_PORT, ports[i], i, sip_exp_policy,
2122 SIP_EXPECT_MAX,
2123 sizeof(struct nf_ct_sip_master), sip_help_tcp,
2124 NULL, THIS_MODULE);
2125 }
Patrick McHardyf5b321b2010-02-11 12:26:19 +01002126
Gao Feng82de0be2016-07-18 11:39:23 +08002127 ret = nf_conntrack_helpers_register(sip, ports_c * 4);
2128 if (ret < 0) {
2129 pr_err("failed to register helpers\n");
2130 return ret;
Patrick McHardy9fafcd72006-12-02 22:09:57 -08002131 }
2132 return 0;
2133}
2134
2135module_init(nf_conntrack_sip_init);
2136module_exit(nf_conntrack_sip_fini);