1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/module.h>
16#include <linux/ctype.h>
17#include <linux/skbuff.h>
18#include <linux/inet.h>
19#include <linux/in.h>
20#include <linux/udp.h>
21#include <linux/tcp.h>
22#include <linux/netfilter.h>
23#include <linux/netfilter_ipv4.h>
24#include <linux/netfilter_ipv6.h>
25
26#include <net/netfilter/nf_conntrack.h>
27#include <net/netfilter/nf_conntrack_core.h>
28#include <net/netfilter/nf_conntrack_expect.h>
29#include <net/netfilter/nf_conntrack_helper.h>
30#include <net/netfilter/nf_conntrack_zones.h>
31#include <linux/netfilter/nf_conntrack_sip.h>
32
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
35MODULE_DESCRIPTION("SIP connection tracking helper");
36MODULE_ALIAS("ip_conntrack_sip");
37MODULE_ALIAS_NFCT_HELPER("sip");
38
39#define MAX_PORTS 8
40static unsigned short ports[MAX_PORTS];
41static unsigned int ports_c;
42module_param_array(ports, ushort, &ports_c, 0400);
43MODULE_PARM_DESC(ports, "port numbers of SIP servers");
44
45static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
46module_param(sip_timeout, uint, 0600);
47MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
48
49static int sip_direct_signalling __read_mostly = 1;
50module_param(sip_direct_signalling, int, 0600);
51MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
52 "only (default 1)");
53
54static int sip_direct_media __read_mostly = 1;
55module_param(sip_direct_media, int, 0600);
56MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
57 "endpoints only (default 1)");
58
59static int sip_external_media __read_mostly = 0;
60module_param(sip_external_media, int, 0600);
61MODULE_PARM_DESC(sip_external_media, "Expect Media streams between external "
62 "endpoints (default 0)");
63
64const struct nf_nat_sip_hooks *nf_nat_sip_hooks;
65EXPORT_SYMBOL_GPL(nf_nat_sip_hooks);
66
67static int string_len(const struct nf_conn *ct, const char *dptr,
68 const char *limit, int *shift)
69{
70 int len = 0;
71
72 while (dptr < limit && isalpha(*dptr)) {
73 dptr++;
74 len++;
75 }
76 return len;
77}
78
79static int digits_len(const struct nf_conn *ct, const char *dptr,
80 const char *limit, int *shift)
81{
82 int len = 0;
83 while (dptr < limit && isdigit(*dptr)) {
84 dptr++;
85 len++;
86 }
87 return len;
88}
89
90static int iswordc(const char c)
91{
92 if (isalnum(c) || c == '!' || c == '"' || c == '%' ||
93 (c >= '(' && c <= '+') || c == ':' || c == '<' || c == '>' ||
94 c == '?' || (c >= '[' && c <= ']') || c == '_' || c == '`' ||
95 c == '{' || c == '}' || c == '~' || (c >= '-' && c <= '/') ||
96 c == '\'')
97 return 1;
98 return 0;
99}
100
101static int word_len(const char *dptr, const char *limit)
102{
103 int len = 0;
104 while (dptr < limit && iswordc(*dptr)) {
105 dptr++;
106 len++;
107 }
108 return len;
109}
110
111static int callid_len(const struct nf_conn *ct, const char *dptr,
112 const char *limit, int *shift)
113{
114 int len, domain_len;
115
116 len = word_len(dptr, limit);
117 dptr += len;
118 if (!len || dptr == limit || *dptr != '@')
119 return len;
120 dptr++;
121 len++;
122
123 domain_len = word_len(dptr, limit);
124 if (!domain_len)
125 return 0;
126 return len + domain_len;
127}
128
129
130static int media_len(const struct nf_conn *ct, const char *dptr,
131 const char *limit, int *shift)
132{
133 int len = string_len(ct, dptr, limit, shift);
134
135 dptr += len;
136 if (dptr >= limit || *dptr != ' ')
137 return 0;
138 len++;
139 dptr++;
140
141 return len + digits_len(ct, dptr, limit, shift);
142}
143
144static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
145 const char **endp, union nf_inet_addr *addr,
146 const char *limit, bool delim)
147{
148 const char *end;
149 int ret;
150
151 if (!ct)
152 return 0;
153
154 memset(addr, 0, sizeof(*addr));
155 switch (nf_ct_l3num(ct)) {
156 case AF_INET:
157 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
158 if (ret == 0)
159 return 0;
160 break;
161 case AF_INET6:
162 if (cp < limit && *cp == '[')
163 cp++;
164 else if (delim)
165 return 0;
166
167 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
168 if (ret == 0)
169 return 0;
170
171 if (end < limit && *end == ']')
172 end++;
173 else if (delim)
174 return 0;
175 break;
176 default:
177 BUG();
178 }
179
180 if (endp)
181 *endp = end;
182 return 1;
183}
184
185
186static int epaddr_len(const struct nf_conn *ct, const char *dptr,
187 const char *limit, int *shift)
188{
189 union nf_inet_addr addr;
190 const char *aux = dptr;
191
192 if (!sip_parse_addr(ct, dptr, &dptr, &addr, limit, true)) {
193 pr_debug("ip: %s parse failed.!\n", dptr);
194 return 0;
195 }
196
197
198 if (*dptr == ':') {
199 dptr++;
200 dptr += digits_len(ct, dptr, limit, shift);
201 }
202 return dptr - aux;
203}
204
205
206static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
207 const char *limit, int *shift)
208{
209 const char *start = dptr;
210 int s = *shift;
211
212
213
214
215 while (dptr < limit &&
216 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
217 (*shift)++;
218 dptr++;
219 }
220
221 if (dptr < limit && *dptr == '@') {
222 dptr++;
223 (*shift)++;
224 } else {
225 dptr = start;
226 *shift = s;
227 }
228
229 return epaddr_len(ct, dptr, limit, shift);
230}
231
232
233
234
235
236
237
238int ct_sip_parse_request(const struct nf_conn *ct,
239 const char *dptr, unsigned int datalen,
240 unsigned int *matchoff, unsigned int *matchlen,
241 union nf_inet_addr *addr, __be16 *port)
242{
243 const char *start = dptr, *limit = dptr + datalen, *end;
244 unsigned int mlen;
245 unsigned int p;
246 int shift = 0;
247
248
249 mlen = string_len(ct, dptr, limit, NULL);
250 if (!mlen)
251 return 0;
252 dptr += mlen;
253 if (++dptr >= limit)
254 return 0;
255
256
257 for (; dptr < limit - strlen("sip:"); dptr++) {
258 if (*dptr == '\r' || *dptr == '\n')
259 return -1;
260 if (strncasecmp(dptr, "sip:", strlen("sip:")) == 0) {
261 dptr += strlen("sip:");
262 break;
263 }
264 }
265 if (!skp_epaddr_len(ct, dptr, limit, &shift))
266 return 0;
267 dptr += shift;
268
269 if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
270 return -1;
271 if (end < limit && *end == ':') {
272 end++;
273 p = simple_strtoul(end, (char **)&end, 10);
274 if (p < 1024 || p > 65535)
275 return -1;
276 *port = htons(p);
277 } else
278 *port = htons(SIP_PORT);
279
280 if (end == dptr)
281 return 0;
282 *matchoff = dptr - start;
283 *matchlen = end - dptr;
284 return 1;
285}
286EXPORT_SYMBOL_GPL(ct_sip_parse_request);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301static const struct sip_header ct_sip_hdrs[] = {
302 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
303 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
304 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
305 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
306 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
307 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
308 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
309 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
310 [SIP_HDR_CALL_ID] = SIP_HDR("Call-Id", "i", NULL, callid_len),
311};
312
313static const char *sip_follow_continuation(const char *dptr, const char *limit)
314{
315
316 if (++dptr >= limit)
317 return NULL;
318
319
320 if (*(dptr - 1) == '\r' && *dptr == '\n') {
321 if (++dptr >= limit)
322 return NULL;
323 }
324
325
326 if (*dptr != ' ' && *dptr != '\t')
327 return NULL;
328
329
330 for (; dptr < limit; dptr++) {
331 if (*dptr != ' ' && *dptr != '\t')
332 break;
333 }
334 return dptr;
335}
336
337static const char *sip_skip_whitespace(const char *dptr, const char *limit)
338{
339 for (; dptr < limit; dptr++) {
340 if (*dptr == ' ' || *dptr == '\t')
341 continue;
342 if (*dptr != '\r' && *dptr != '\n')
343 break;
344 dptr = sip_follow_continuation(dptr, limit);
345 break;
346 }
347 return dptr;
348}
349
350
351static const char *ct_sip_header_search(const char *dptr, const char *limit,
352 const char *needle, unsigned int len)
353{
354 for (limit -= len; dptr < limit; dptr++) {
355 if (*dptr == '\r' || *dptr == '\n') {
356 dptr = sip_follow_continuation(dptr, limit);
357 if (dptr == NULL)
358 break;
359 continue;
360 }
361
362 if (strncasecmp(dptr, needle, len) == 0)
363 return dptr;
364 }
365 return NULL;
366}
367
368int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
369 unsigned int dataoff, unsigned int datalen,
370 enum sip_header_types type,
371 unsigned int *matchoff, unsigned int *matchlen)
372{
373 const struct sip_header *hdr = &ct_sip_hdrs[type];
374 const char *start = dptr, *limit = dptr + datalen;
375 int shift = 0;
376
377 for (dptr += dataoff; dptr < limit; dptr++) {
378
379 if (*dptr != '\r' && *dptr != '\n')
380 continue;
381 if (++dptr >= limit)
382 break;
383 if (*(dptr - 1) == '\r' && *dptr == '\n') {
384 if (++dptr >= limit)
385 break;
386 }
387
388
389 if (*dptr == ' ' || *dptr == '\t')
390 continue;
391
392
393
394 if (limit - dptr >= hdr->len &&
395 strncasecmp(dptr, hdr->name, hdr->len) == 0)
396 dptr += hdr->len;
397 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
398 strncasecmp(dptr, hdr->cname, hdr->clen) == 0 &&
399 !isalpha(*(dptr + hdr->clen)))
400 dptr += hdr->clen;
401 else
402 continue;
403
404
405 dptr = sip_skip_whitespace(dptr, limit);
406 if (dptr == NULL)
407 break;
408 if (*dptr != ':' || ++dptr >= limit)
409 break;
410
411
412 dptr = sip_skip_whitespace(dptr, limit);
413 if (dptr == NULL)
414 break;
415
416 *matchoff = dptr - start;
417 if (hdr->search) {
418 dptr = ct_sip_header_search(dptr, limit, hdr->search,
419 hdr->slen);
420 if (!dptr)
421 return -1;
422 dptr += hdr->slen;
423 }
424
425 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
426 if (!*matchlen)
427 return -1;
428 *matchoff = dptr - start + shift;
429 return 1;
430 }
431 return 0;
432}
433EXPORT_SYMBOL_GPL(ct_sip_get_header);
434
435
436static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
437 unsigned int dataoff, unsigned int datalen,
438 enum sip_header_types type,
439 unsigned int *matchoff, unsigned int *matchlen)
440{
441 const struct sip_header *hdr = &ct_sip_hdrs[type];
442 const char *start = dptr, *limit = dptr + datalen;
443 int shift = 0;
444
445 dptr += dataoff;
446
447 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
448 if (!dptr)
449 return 0;
450
451 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
452 if (!dptr)
453 return 0;
454 dptr += hdr->slen;
455
456 *matchoff = dptr - start;
457 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
458 if (!*matchlen)
459 return -1;
460 *matchoff += shift;
461 return 1;
462}
463
464
465
466static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
467 unsigned int dataoff, unsigned int datalen,
468 enum sip_header_types type, int *in_header,
469 unsigned int *matchoff, unsigned int *matchlen)
470{
471 int ret;
472
473 if (in_header && *in_header) {
474 while (1) {
475 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
476 type, matchoff, matchlen);
477 if (ret > 0)
478 return ret;
479 if (ret == 0)
480 break;
481 dataoff += *matchoff;
482 }
483 *in_header = 0;
484 }
485
486 while (1) {
487 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
488 type, matchoff, matchlen);
489 if (ret > 0)
490 break;
491 if (ret == 0)
492 return ret;
493 dataoff += *matchoff;
494 }
495
496 if (in_header)
497 *in_header = 1;
498 return 1;
499}
500
501
502
503
504
505
506int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
507 unsigned int *dataoff, unsigned int datalen,
508 enum sip_header_types type, int *in_header,
509 unsigned int *matchoff, unsigned int *matchlen,
510 union nf_inet_addr *addr, __be16 *port)
511{
512 const char *c, *limit = dptr + datalen;
513 unsigned int p;
514 int ret;
515
516 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
517 type, in_header, matchoff, matchlen);
518 WARN_ON(ret < 0);
519 if (ret == 0)
520 return ret;
521
522 if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
523 return -1;
524 if (*c == ':') {
525 c++;
526 p = simple_strtoul(c, (char **)&c, 10);
527 if (p < 1024 || p > 65535)
528 return -1;
529 *port = htons(p);
530 } else
531 *port = htons(SIP_PORT);
532
533 if (dataoff)
534 *dataoff = c - dptr;
535 return 1;
536}
537EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
538
539static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
540 unsigned int dataoff, unsigned int datalen,
541 const char *name,
542 unsigned int *matchoff, unsigned int *matchlen)
543{
544 const char *limit = dptr + datalen;
545 const char *start;
546 const char *end;
547
548 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
549 if (!limit)
550 limit = dptr + datalen;
551
552 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
553 if (!start)
554 return 0;
555 start += strlen(name);
556
557 end = ct_sip_header_search(start, limit, ";", strlen(";"));
558 if (!end)
559 end = limit;
560
561 *matchoff = start - dptr;
562 *matchlen = end - start;
563 return 1;
564}
565
566
567int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
568 unsigned int dataoff, unsigned int datalen,
569 const char *name,
570 unsigned int *matchoff, unsigned int *matchlen,
571 union nf_inet_addr *addr, bool delim)
572{
573 const char *limit = dptr + datalen;
574 const char *start, *end;
575
576 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
577 if (!limit)
578 limit = dptr + datalen;
579
580 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
581 if (!start)
582 return 0;
583
584 start += strlen(name);
585 if (!sip_parse_addr(ct, start, &end, addr, limit, delim))
586 return 0;
587 *matchoff = start - dptr;
588 *matchlen = end - start;
589 return 1;
590}
591EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
592
593
594int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
595 unsigned int dataoff, unsigned int datalen,
596 const char *name,
597 unsigned int *matchoff, unsigned int *matchlen,
598 unsigned int *val)
599{
600 const char *limit = dptr + datalen;
601 const char *start;
602 char *end;
603
604 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
605 if (!limit)
606 limit = dptr + datalen;
607
608 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
609 if (!start)
610 return 0;
611
612 start += strlen(name);
613 *val = simple_strtoul(start, &end, 0);
614 if (start == end)
615 return 0;
616 if (matchoff && matchlen) {
617 *matchoff = start - dptr;
618 *matchlen = end - start;
619 }
620 return 1;
621}
622EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
623
624static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
625 unsigned int dataoff, unsigned int datalen,
626 u8 *proto)
627{
628 unsigned int matchoff, matchlen;
629
630 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
631 &matchoff, &matchlen)) {
632 if (!strncasecmp(dptr + matchoff, "TCP", strlen("TCP")))
633 *proto = IPPROTO_TCP;
634 else if (!strncasecmp(dptr + matchoff, "UDP", strlen("UDP")))
635 *proto = IPPROTO_UDP;
636 else
637 return 0;
638
639 if (*proto != nf_ct_protonum(ct))
640 return 0;
641 } else
642 *proto = nf_ct_protonum(ct);
643
644 return 1;
645}
646
647static int sdp_parse_addr(const struct nf_conn *ct, const char *cp,
648 const char **endp, union nf_inet_addr *addr,
649 const char *limit)
650{
651 const char *end;
652 int ret;
653
654 memset(addr, 0, sizeof(*addr));
655 switch (nf_ct_l3num(ct)) {
656 case AF_INET:
657 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
658 break;
659 case AF_INET6:
660 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
661 break;
662 default:
663 BUG();
664 }
665
666 if (ret == 0)
667 return 0;
668 if (endp)
669 *endp = end;
670 return 1;
671}
672
673
674static int sdp_addr_len(const struct nf_conn *ct, const char *dptr,
675 const char *limit, int *shift)
676{
677 union nf_inet_addr addr;
678 const char *aux = dptr;
679
680 if (!sdp_parse_addr(ct, dptr, &dptr, &addr, limit)) {
681 pr_debug("ip: %s parse failed.!\n", dptr);
682 return 0;
683 }
684
685 return dptr - aux;
686}
687
688
689
690
691
692
693
694
695
696
697static const struct sip_header ct_sdp_hdrs_v4[] = {
698 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
699 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len),
700 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len),
701 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
702};
703
704static const struct sip_header ct_sdp_hdrs_v6[] = {
705 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
706 [SDP_HDR_OWNER] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len),
707 [SDP_HDR_CONNECTION] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len),
708 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
709};
710
711
712static const char *ct_sdp_header_search(const char *dptr, const char *limit,
713 const char *needle, unsigned int len)
714{
715 for (limit -= len; dptr < limit; dptr++) {
716 if (*dptr == '\r' || *dptr == '\n')
717 break;
718 if (strncmp(dptr, needle, len) == 0)
719 return dptr;
720 }
721 return NULL;
722}
723
724
725
726
727
728int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
729 unsigned int dataoff, unsigned int datalen,
730 enum sdp_header_types type,
731 enum sdp_header_types term,
732 unsigned int *matchoff, unsigned int *matchlen)
733{
734 const struct sip_header *hdrs, *hdr, *thdr;
735 const char *start = dptr, *limit = dptr + datalen;
736 int shift = 0;
737
738 hdrs = nf_ct_l3num(ct) == NFPROTO_IPV4 ? ct_sdp_hdrs_v4 : ct_sdp_hdrs_v6;
739 hdr = &hdrs[type];
740 thdr = &hdrs[term];
741
742 for (dptr += dataoff; dptr < limit; dptr++) {
743
744 if (*dptr != '\r' && *dptr != '\n')
745 continue;
746 if (++dptr >= limit)
747 break;
748 if (*(dptr - 1) == '\r' && *dptr == '\n') {
749 if (++dptr >= limit)
750 break;
751 }
752
753 if (term != SDP_HDR_UNSPEC &&
754 limit - dptr >= thdr->len &&
755 strncasecmp(dptr, thdr->name, thdr->len) == 0)
756 break;
757 else if (limit - dptr >= hdr->len &&
758 strncasecmp(dptr, hdr->name, hdr->len) == 0)
759 dptr += hdr->len;
760 else
761 continue;
762
763 *matchoff = dptr - start;
764 if (hdr->search) {
765 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
766 hdr->slen);
767 if (!dptr)
768 return -1;
769 dptr += hdr->slen;
770 }
771
772 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
773 if (!*matchlen)
774 return -1;
775 *matchoff = dptr - start + shift;
776 return 1;
777 }
778 return 0;
779}
780EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
781
782static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
783 unsigned int dataoff, unsigned int datalen,
784 enum sdp_header_types type,
785 enum sdp_header_types term,
786 unsigned int *matchoff, unsigned int *matchlen,
787 union nf_inet_addr *addr)
788{
789 int ret;
790
791 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
792 matchoff, matchlen);
793 if (ret <= 0)
794 return ret;
795
796 if (!sdp_parse_addr(ct, dptr + *matchoff, NULL, addr,
797 dptr + *matchoff + *matchlen))
798 return -1;
799 return 1;
800}
801
802static int refresh_signalling_expectation(struct nf_conn *ct,
803 union nf_inet_addr *addr,
804 u8 proto, __be16 port,
805 unsigned int expires)
806{
807 struct nf_conn_help *help = nfct_help(ct);
808 struct nf_conntrack_expect *exp;
809 struct hlist_node *next;
810 int found = 0;
811
812 spin_lock_bh(&nf_conntrack_expect_lock);
813 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
814 if (exp->class != SIP_EXPECT_SIGNALLING ||
815 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
816 exp->tuple.dst.protonum != proto ||
817 exp->tuple.dst.u.udp.port != port)
818 continue;
819 if (mod_timer_pending(&exp->timeout, jiffies + expires * HZ)) {
820 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
821 found = 1;
822 break;
823 }
824 }
825 spin_unlock_bh(&nf_conntrack_expect_lock);
826 return found;
827}
828
829static void flush_expectations(struct nf_conn *ct, bool media)
830{
831 struct nf_conn_help *help = nfct_help(ct);
832 struct nf_conntrack_expect *exp;
833 struct hlist_node *next;
834
835 spin_lock_bh(&nf_conntrack_expect_lock);
836 hlist_for_each_entry_safe(exp, next, &help->expectations, lnode) {
837 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
838 continue;
839 if (!nf_ct_remove_expect(exp))
840 continue;
841 if (!media)
842 break;
843 }
844 spin_unlock_bh(&nf_conntrack_expect_lock);
845}
846
847static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
848 unsigned int dataoff,
849 const char **dptr, unsigned int *datalen,
850 union nf_inet_addr *daddr, __be16 port,
851 enum sip_expectation_classes class,
852 unsigned int mediaoff, unsigned int medialen)
853{
854 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
855 enum ip_conntrack_info ctinfo;
856 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
857 struct net *net = nf_ct_net(ct);
858 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
859 union nf_inet_addr *saddr;
860 struct nf_conntrack_tuple tuple;
861 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
862 u_int16_t base_port;
863 __be16 rtp_port, rtcp_port;
864 const struct nf_nat_sip_hooks *hooks;
865
866 saddr = NULL;
867 if (sip_direct_media) {
868 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
869 return NF_ACCEPT;
870 saddr = &ct->tuplehash[!dir].tuple.src.u3;
871 } else if (sip_external_media) {
872 struct net_device *dev = skb_dst(skb)->dev;
873 struct net *net = dev_net(dev);
874 struct flowi fl;
875 struct dst_entry *dst = NULL;
876
877 memset(&fl, 0, sizeof(fl));
878
879 switch (nf_ct_l3num(ct)) {
880 case NFPROTO_IPV4:
881 fl.u.ip4.daddr = daddr->ip;
882 nf_ip_route(net, &dst, &fl, false);
883 break;
884
885 case NFPROTO_IPV6:
886 fl.u.ip6.daddr = daddr->in6;
887 nf_ip6_route(net, &dst, &fl, false);
888 break;
889 }
890
891
892
893
894 if (dst) {
895 bool external_media = (dst->dev == dev);
896
897 dst_release(dst);
898 if (external_media)
899 return NF_ACCEPT;
900 }
901 }
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916 memset(&tuple, 0, sizeof(tuple));
917 if (saddr)
918 tuple.src.u3 = *saddr;
919 tuple.src.l3num = nf_ct_l3num(ct);
920 tuple.dst.protonum = IPPROTO_UDP;
921 tuple.dst.u3 = *daddr;
922 tuple.dst.u.udp.port = port;
923
924 do {
925 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
926
927 if (!exp || exp->master == ct ||
928 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
929 exp->class != class)
930 break;
931#ifdef CONFIG_NF_NAT_NEEDED
932 if (!direct_rtp &&
933 (!nf_inet_addr_cmp(&exp->saved_addr, &exp->tuple.dst.u3) ||
934 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
935 ct->status & IPS_NAT_MASK) {
936 *daddr = exp->saved_addr;
937 tuple.dst.u3 = exp->saved_addr;
938 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
939 direct_rtp = 1;
940 } else
941#endif
942 skip_expect = 1;
943 } while (!skip_expect);
944
945 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
946 rtp_port = htons(base_port);
947 rtcp_port = htons(base_port + 1);
948
949 if (direct_rtp) {
950 hooks = rcu_dereference(nf_nat_sip_hooks);
951 if (hooks &&
952 !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
953 mediaoff, medialen, ntohs(rtp_port)))
954 goto err1;
955 }
956
957 if (skip_expect)
958 return NF_ACCEPT;
959
960 rtp_exp = nf_ct_expect_alloc(ct);
961 if (rtp_exp == NULL)
962 goto err1;
963 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
964 IPPROTO_UDP, NULL, &rtp_port);
965
966 rtcp_exp = nf_ct_expect_alloc(ct);
967 if (rtcp_exp == NULL)
968 goto err2;
969 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
970 IPPROTO_UDP, NULL, &rtcp_port);
971
972 hooks = rcu_dereference(nf_nat_sip_hooks);
973 if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
974 ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
975 datalen, rtp_exp, rtcp_exp,
976 mediaoff, medialen, daddr);
977 else {
978
979
980
981
982 int errp = nf_ct_expect_related(rtp_exp);
983
984 if (errp == 0 || errp == -EALREADY) {
985 int errcp = nf_ct_expect_related(rtcp_exp);
986
987 if (errcp == 0 || errcp == -EALREADY)
988 ret = NF_ACCEPT;
989 else if (errp == 0)
990 nf_ct_unexpect_related(rtp_exp);
991 }
992 }
993 nf_ct_expect_put(rtcp_exp);
994err2:
995 nf_ct_expect_put(rtp_exp);
996err1:
997 return ret;
998}
999
1000static const struct sdp_media_type sdp_media_types[] = {
1001 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
1002 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
1003 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
1004};
1005
1006static const struct sdp_media_type *sdp_media_type(const char *dptr,
1007 unsigned int matchoff,
1008 unsigned int matchlen)
1009{
1010 const struct sdp_media_type *t;
1011 unsigned int i;
1012
1013 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
1014 t = &sdp_media_types[i];
1015 if (matchlen < t->len ||
1016 strncmp(dptr + matchoff, t->name, t->len))
1017 continue;
1018 return t;
1019 }
1020 return NULL;
1021}
1022
1023static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1024 unsigned int dataoff,
1025 const char **dptr, unsigned int *datalen,
1026 unsigned int cseq)
1027{
1028 enum ip_conntrack_info ctinfo;
1029 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1030 unsigned int matchoff, matchlen;
1031 unsigned int mediaoff, medialen;
1032 unsigned int sdpoff;
1033 unsigned int caddr_len, maddr_len;
1034 unsigned int i;
1035 union nf_inet_addr caddr, maddr, rtp_addr;
1036 const struct nf_nat_sip_hooks *hooks;
1037 unsigned int port;
1038 const struct sdp_media_type *t;
1039 int ret = NF_ACCEPT;
1040
1041 hooks = rcu_dereference(nf_nat_sip_hooks);
1042
1043
1044 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
1045 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
1046 &matchoff, &matchlen) <= 0)
1047 return NF_ACCEPT;
1048 sdpoff = matchoff;
1049
1050
1051
1052
1053 caddr_len = 0;
1054 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
1055 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1056 &matchoff, &matchlen, &caddr) > 0)
1057 caddr_len = matchlen;
1058
1059 mediaoff = sdpoff;
1060 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1061 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
1062 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
1063 &mediaoff, &medialen) <= 0)
1064 break;
1065
1066
1067
1068 t = sdp_media_type(*dptr, mediaoff, medialen);
1069 if (!t) {
1070 mediaoff += medialen;
1071 continue;
1072 }
1073 mediaoff += t->len;
1074 medialen -= t->len;
1075
1076 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1077 if (port == 0)
1078 continue;
1079 if (port < 1024 || port > 65535) {
1080 nf_ct_helper_log(skb, ct, "wrong port %u", port);
1081 return NF_DROP;
1082 }
1083
1084
1085 maddr_len = 0;
1086 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
1087 SDP_HDR_CONNECTION, SDP_HDR_MEDIA,
1088 &matchoff, &matchlen, &maddr) > 0) {
1089 maddr_len = matchlen;
1090 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1091 } else if (caddr_len)
1092 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
1093 else {
1094 nf_ct_helper_log(skb, ct, "cannot parse SDP message");
1095 return NF_DROP;
1096 }
1097
1098 ret = set_expected_rtp_rtcp(skb, protoff, dataoff,
1099 dptr, datalen,
1100 &rtp_addr, htons(port), t->class,
1101 mediaoff, medialen);
1102 if (ret != NF_ACCEPT) {
1103 nf_ct_helper_log(skb, ct,
1104 "cannot add expectation for voice");
1105 return ret;
1106 }
1107
1108
1109 if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1110 ret = hooks->sdp_addr(skb, protoff, dataoff,
1111 dptr, datalen, mediaoff,
1112 SDP_HDR_CONNECTION,
1113 SDP_HDR_MEDIA,
1114 &rtp_addr);
1115 if (ret != NF_ACCEPT) {
1116 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
1117 return ret;
1118 }
1119 }
1120 i++;
1121 }
1122
1123
1124 hooks = rcu_dereference(nf_nat_sip_hooks);
1125 if (hooks && ct->status & IPS_NAT_MASK)
1126 ret = hooks->sdp_session(skb, protoff, dataoff,
1127 dptr, datalen, sdpoff,
1128 &rtp_addr);
1129
1130 return ret;
1131}
1132static int process_invite_response(struct sk_buff *skb, unsigned int protoff,
1133 unsigned int dataoff,
1134 const char **dptr, unsigned int *datalen,
1135 unsigned int cseq, unsigned int code)
1136{
1137 enum ip_conntrack_info ctinfo;
1138 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1139 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1140
1141 if ((code >= 100 && code <= 199) ||
1142 (code >= 200 && code <= 299))
1143 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1144 else if (ct_sip_info->invite_cseq == cseq)
1145 flush_expectations(ct, true);
1146 return NF_ACCEPT;
1147}
1148
1149static int process_update_response(struct sk_buff *skb, unsigned int protoff,
1150 unsigned int dataoff,
1151 const char **dptr, unsigned int *datalen,
1152 unsigned int cseq, unsigned int code)
1153{
1154 enum ip_conntrack_info ctinfo;
1155 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1156 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1157
1158 if ((code >= 100 && code <= 199) ||
1159 (code >= 200 && code <= 299))
1160 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1161 else if (ct_sip_info->invite_cseq == cseq)
1162 flush_expectations(ct, true);
1163 return NF_ACCEPT;
1164}
1165
1166static int process_prack_response(struct sk_buff *skb, unsigned int protoff,
1167 unsigned int dataoff,
1168 const char **dptr, unsigned int *datalen,
1169 unsigned int cseq, unsigned int code)
1170{
1171 enum ip_conntrack_info ctinfo;
1172 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1173 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1174
1175 if ((code >= 100 && code <= 199) ||
1176 (code >= 200 && code <= 299))
1177 return process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1178 else if (ct_sip_info->invite_cseq == cseq)
1179 flush_expectations(ct, true);
1180 return NF_ACCEPT;
1181}
1182
1183static int process_invite_request(struct sk_buff *skb, unsigned int protoff,
1184 unsigned int dataoff,
1185 const char **dptr, unsigned int *datalen,
1186 unsigned int cseq)
1187{
1188 enum ip_conntrack_info ctinfo;
1189 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1190 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1191 unsigned int ret;
1192
1193 flush_expectations(ct, true);
1194 ret = process_sdp(skb, protoff, dataoff, dptr, datalen, cseq);
1195 if (ret == NF_ACCEPT)
1196 ct_sip_info->invite_cseq = cseq;
1197 return ret;
1198}
1199
1200static int process_bye_request(struct sk_buff *skb, unsigned int protoff,
1201 unsigned int dataoff,
1202 const char **dptr, unsigned int *datalen,
1203 unsigned int cseq)
1204{
1205 enum ip_conntrack_info ctinfo;
1206 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1207
1208 flush_expectations(ct, true);
1209 return NF_ACCEPT;
1210}
1211
1212
1213
1214
1215
1216static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1217 unsigned int dataoff,
1218 const char **dptr, unsigned int *datalen,
1219 unsigned int cseq)
1220{
1221 enum ip_conntrack_info ctinfo;
1222 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1223 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1224 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1225 unsigned int matchoff, matchlen;
1226 struct nf_conntrack_expect *exp;
1227 union nf_inet_addr *saddr, daddr;
1228 const struct nf_nat_sip_hooks *hooks;
1229 __be16 port;
1230 u8 proto;
1231 unsigned int expires = 0;
1232 int ret;
1233
1234
1235 if (ct->status & IPS_EXPECTED)
1236 return NF_ACCEPT;
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1247 &matchoff, &matchlen) > 0)
1248 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1249
1250 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1251 SIP_HDR_CONTACT, NULL,
1252 &matchoff, &matchlen, &daddr, &port);
1253 if (ret < 0) {
1254 nf_ct_helper_log(skb, ct, "cannot parse contact");
1255 return NF_DROP;
1256 } else if (ret == 0)
1257 return NF_ACCEPT;
1258
1259
1260 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1261 return NF_ACCEPT;
1262
1263 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1264 &proto) == 0)
1265 return NF_ACCEPT;
1266
1267 if (ct_sip_parse_numerical_param(ct, *dptr,
1268 matchoff + matchlen, *datalen,
1269 "expires=", NULL, NULL, &expires) < 0) {
1270 nf_ct_helper_log(skb, ct, "cannot parse expires");
1271 return NF_DROP;
1272 }
1273
1274 if (expires == 0) {
1275 ret = NF_ACCEPT;
1276 goto store_cseq;
1277 }
1278
1279 exp = nf_ct_expect_alloc(ct);
1280 if (!exp) {
1281 nf_ct_helper_log(skb, ct, "cannot alloc expectation");
1282 return NF_DROP;
1283 }
1284
1285 saddr = NULL;
1286 if (sip_direct_signalling)
1287 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1288
1289 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1290 saddr, &daddr, proto, NULL, &port);
1291 exp->timeout.expires = sip_timeout * HZ;
1292 exp->helper = nfct_help(ct)->helper;
1293 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1294
1295 hooks = rcu_dereference(nf_nat_sip_hooks);
1296 if (hooks && ct->status & IPS_NAT_MASK)
1297 ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1298 exp, matchoff, matchlen);
1299 else {
1300 if (nf_ct_expect_related(exp) != 0) {
1301 nf_ct_helper_log(skb, ct, "cannot add expectation");
1302 ret = NF_DROP;
1303 } else
1304 ret = NF_ACCEPT;
1305 }
1306 nf_ct_expect_put(exp);
1307
1308store_cseq:
1309 if (ret == NF_ACCEPT)
1310 ct_sip_info->register_cseq = cseq;
1311 return ret;
1312}
1313
1314static int process_register_response(struct sk_buff *skb, unsigned int protoff,
1315 unsigned int dataoff,
1316 const char **dptr, unsigned int *datalen,
1317 unsigned int cseq, unsigned int code)
1318{
1319 enum ip_conntrack_info ctinfo;
1320 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1321 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1322 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1323 union nf_inet_addr addr;
1324 __be16 port;
1325 u8 proto;
1326 unsigned int matchoff, matchlen, coff = 0;
1327 unsigned int expires = 0;
1328 int in_contact = 0, ret;
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338 if (ct_sip_info->register_cseq != cseq)
1339 return NF_ACCEPT;
1340
1341 if (code >= 100 && code <= 199)
1342 return NF_ACCEPT;
1343 if (code < 200 || code > 299)
1344 goto flush;
1345
1346 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1347 &matchoff, &matchlen) > 0)
1348 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1349
1350 while (1) {
1351 unsigned int c_expires = expires;
1352
1353 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
1354 SIP_HDR_CONTACT, &in_contact,
1355 &matchoff, &matchlen,
1356 &addr, &port);
1357 if (ret < 0) {
1358 nf_ct_helper_log(skb, ct, "cannot parse contact");
1359 return NF_DROP;
1360 } else if (ret == 0)
1361 break;
1362
1363
1364 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1365 continue;
1366
1367 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1368 *datalen, &proto) == 0)
1369 continue;
1370
1371 ret = ct_sip_parse_numerical_param(ct, *dptr,
1372 matchoff + matchlen,
1373 *datalen, "expires=",
1374 NULL, NULL, &c_expires);
1375 if (ret < 0) {
1376 nf_ct_helper_log(skb, ct, "cannot parse expires");
1377 return NF_DROP;
1378 }
1379 if (c_expires == 0)
1380 break;
1381 if (refresh_signalling_expectation(ct, &addr, proto, port,
1382 c_expires))
1383 return NF_ACCEPT;
1384 }
1385
1386flush:
1387 flush_expectations(ct, false);
1388 return NF_ACCEPT;
1389}
1390
1391static const struct sip_handler sip_handlers[] = {
1392 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
1393 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1394 SIP_HANDLER("ACK", process_sdp, NULL),
1395 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1396 SIP_HANDLER("BYE", process_bye_request, NULL),
1397 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1398};
1399
1400static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
1401 unsigned int dataoff,
1402 const char **dptr, unsigned int *datalen)
1403{
1404 enum ip_conntrack_info ctinfo;
1405 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1406 unsigned int matchoff, matchlen, matchend;
1407 unsigned int code, cseq, i;
1408
1409 if (*datalen < strlen("SIP/2.0 200"))
1410 return NF_ACCEPT;
1411 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1412 if (!code) {
1413 nf_ct_helper_log(skb, ct, "cannot get code");
1414 return NF_DROP;
1415 }
1416
1417 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1418 &matchoff, &matchlen) <= 0) {
1419 nf_ct_helper_log(skb, ct, "cannot parse cseq");
1420 return NF_DROP;
1421 }
1422 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1423 if (!cseq && *(*dptr + matchoff) != '0') {
1424 nf_ct_helper_log(skb, ct, "cannot get cseq");
1425 return NF_DROP;
1426 }
1427 matchend = matchoff + matchlen + 1;
1428
1429 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1430 const struct sip_handler *handler;
1431
1432 handler = &sip_handlers[i];
1433 if (handler->response == NULL)
1434 continue;
1435 if (*datalen < matchend + handler->len ||
1436 strncasecmp(*dptr + matchend, handler->method, handler->len))
1437 continue;
1438 return handler->response(skb, protoff, dataoff, dptr, datalen,
1439 cseq, code);
1440 }
1441 return NF_ACCEPT;
1442}
1443
1444static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
1445 unsigned int dataoff,
1446 const char **dptr, unsigned int *datalen)
1447{
1448 enum ip_conntrack_info ctinfo;
1449 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1450 struct nf_ct_sip_master *ct_sip_info = nfct_help_data(ct);
1451 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1452 unsigned int matchoff, matchlen;
1453 unsigned int cseq, i;
1454 union nf_inet_addr addr;
1455 __be16 port;
1456
1457
1458
1459
1460
1461
1462
1463 if (ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1464 SIP_HDR_VIA_UDP, NULL, &matchoff,
1465 &matchlen, &addr, &port) > 0 &&
1466 port != ct->tuplehash[dir].tuple.src.u.udp.port &&
1467 nf_inet_addr_cmp(&addr, &ct->tuplehash[dir].tuple.src.u3))
1468 ct_sip_info->forced_dport = port;
1469
1470 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1471 const struct sip_handler *handler;
1472
1473 handler = &sip_handlers[i];
1474 if (handler->request == NULL)
1475 continue;
1476 if (*datalen < handler->len + 2 ||
1477 strncasecmp(*dptr, handler->method, handler->len))
1478 continue;
1479 if ((*dptr)[handler->len] != ' ' ||
1480 !isalpha((*dptr)[handler->len+1]))
1481 continue;
1482
1483 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1484 &matchoff, &matchlen) <= 0) {
1485 nf_ct_helper_log(skb, ct, "cannot parse cseq");
1486 return NF_DROP;
1487 }
1488 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1489 if (!cseq && *(*dptr + matchoff) != '0') {
1490 nf_ct_helper_log(skb, ct, "cannot get cseq");
1491 return NF_DROP;
1492 }
1493
1494 return handler->request(skb, protoff, dataoff, dptr, datalen,
1495 cseq);
1496 }
1497 return NF_ACCEPT;
1498}
1499
1500static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1501 unsigned int protoff, unsigned int dataoff,
1502 const char **dptr, unsigned int *datalen)
1503{
1504 const struct nf_nat_sip_hooks *hooks;
1505 int ret;
1506
1507 if (strncasecmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1508 ret = process_sip_request(skb, protoff, dataoff, dptr, datalen);
1509 else
1510 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
1511
1512 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1513 hooks = rcu_dereference(nf_nat_sip_hooks);
1514 if (hooks && !hooks->msg(skb, protoff, dataoff,
1515 dptr, datalen)) {
1516 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
1517 ret = NF_DROP;
1518 }
1519 }
1520
1521 return ret;
1522}
1523
1524static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1525 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1526{
1527 struct tcphdr *th, _tcph;
1528 unsigned int dataoff, datalen;
1529 unsigned int matchoff, matchlen, clen;
1530 unsigned int msglen, origlen;
1531 const char *dptr, *end;
1532 s16 diff, tdiff = 0;
1533 int ret = NF_ACCEPT;
1534 bool term;
1535
1536 if (ctinfo != IP_CT_ESTABLISHED &&
1537 ctinfo != IP_CT_ESTABLISHED_REPLY)
1538 return NF_ACCEPT;
1539
1540
1541 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1542 if (th == NULL)
1543 return NF_ACCEPT;
1544 dataoff = protoff + th->doff * 4;
1545 if (dataoff >= skb->len)
1546 return NF_ACCEPT;
1547
1548 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1549
1550 if (unlikely(skb_linearize(skb)))
1551 return NF_DROP;
1552
1553 dptr = skb->data + dataoff;
1554 datalen = skb->len - dataoff;
1555 if (datalen < strlen("SIP/2.0 200"))
1556 return NF_ACCEPT;
1557
1558 while (1) {
1559 if (ct_sip_get_header(ct, dptr, 0, datalen,
1560 SIP_HDR_CONTENT_LENGTH,
1561 &matchoff, &matchlen) <= 0)
1562 break;
1563
1564 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1565 if (dptr + matchoff == end)
1566 break;
1567
1568 term = false;
1569 for (; end + strlen("\r\n\r\n") <= dptr + datalen; end++) {
1570 if (end[0] == '\r' && end[1] == '\n' &&
1571 end[2] == '\r' && end[3] == '\n') {
1572 term = true;
1573 break;
1574 }
1575 }
1576 if (!term)
1577 break;
1578 end += strlen("\r\n\r\n") + clen;
1579
1580 msglen = origlen = end - dptr;
1581 if (msglen > datalen)
1582 return NF_ACCEPT;
1583
1584 ret = process_sip_msg(skb, ct, protoff, dataoff,
1585 &dptr, &msglen);
1586
1587 if (ret != NF_ACCEPT)
1588 break;
1589 diff = msglen - origlen;
1590 tdiff += diff;
1591
1592 dataoff += msglen;
1593 dptr += msglen;
1594 datalen = datalen + diff - msglen;
1595 }
1596
1597 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1598 const struct nf_nat_sip_hooks *hooks;
1599
1600 hooks = rcu_dereference(nf_nat_sip_hooks);
1601 if (hooks)
1602 hooks->seq_adjust(skb, protoff, tdiff);
1603 }
1604
1605 return ret;
1606}
1607
1608static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1609 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1610{
1611 unsigned int dataoff, datalen;
1612 const char *dptr;
1613
1614
1615 dataoff = protoff + sizeof(struct udphdr);
1616 if (dataoff >= skb->len)
1617 return NF_ACCEPT;
1618
1619 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1620
1621 if (unlikely(skb_linearize(skb)))
1622 return NF_DROP;
1623
1624 dptr = skb->data + dataoff;
1625 datalen = skb->len - dataoff;
1626 if (datalen < strlen("SIP/2.0 200"))
1627 return NF_ACCEPT;
1628
1629 return process_sip_msg(skb, ct, protoff, dataoff, &dptr, &datalen);
1630}
1631
1632static struct nf_conntrack_helper sip[MAX_PORTS * 4] __read_mostly;
1633
1634static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1635 [SIP_EXPECT_SIGNALLING] = {
1636 .name = "signalling",
1637 .max_expected = 1,
1638 .timeout = 3 * 60,
1639 },
1640 [SIP_EXPECT_AUDIO] = {
1641 .name = "audio",
1642 .max_expected = 2 * IP_CT_DIR_MAX,
1643 .timeout = 3 * 60,
1644 },
1645 [SIP_EXPECT_VIDEO] = {
1646 .name = "video",
1647 .max_expected = 2 * IP_CT_DIR_MAX,
1648 .timeout = 3 * 60,
1649 },
1650 [SIP_EXPECT_IMAGE] = {
1651 .name = "image",
1652 .max_expected = IP_CT_DIR_MAX,
1653 .timeout = 3 * 60,
1654 },
1655};
1656
1657static void __exit nf_conntrack_sip_fini(void)
1658{
1659 nf_conntrack_helpers_unregister(sip, ports_c * 4);
1660}
1661
1662static int __init nf_conntrack_sip_init(void)
1663{
1664 int i, ret;
1665
1666 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sip_master));
1667
1668 if (ports_c == 0)
1669 ports[ports_c++] = SIP_PORT;
1670
1671 for (i = 0; i < ports_c; i++) {
1672 nf_ct_helper_init(&sip[4 * i], AF_INET, IPPROTO_UDP, "sip",
1673 SIP_PORT, ports[i], i, sip_exp_policy,
1674 SIP_EXPECT_MAX, sip_help_udp,
1675 NULL, THIS_MODULE);
1676 nf_ct_helper_init(&sip[4 * i + 1], AF_INET, IPPROTO_TCP, "sip",
1677 SIP_PORT, ports[i], i, sip_exp_policy,
1678 SIP_EXPECT_MAX, sip_help_tcp,
1679 NULL, THIS_MODULE);
1680 nf_ct_helper_init(&sip[4 * i + 2], AF_INET6, IPPROTO_UDP, "sip",
1681 SIP_PORT, ports[i], i, sip_exp_policy,
1682 SIP_EXPECT_MAX, sip_help_udp,
1683 NULL, THIS_MODULE);
1684 nf_ct_helper_init(&sip[4 * i + 3], AF_INET6, IPPROTO_TCP, "sip",
1685 SIP_PORT, ports[i], i, sip_exp_policy,
1686 SIP_EXPECT_MAX, sip_help_tcp,
1687 NULL, THIS_MODULE);
1688 }
1689
1690 ret = nf_conntrack_helpers_register(sip, ports_c * 4);
1691 if (ret < 0) {
1692 pr_err("failed to register helpers\n");
1693 return ret;
1694 }
1695 return 0;
1696}
1697
1698module_init(nf_conntrack_sip_init);
1699module_exit(nf_conntrack_sip_fini);
1700