1 /* SIP extension for IP connection tracking.
3 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
4 * based on RR's ip_conntrack_ftp.c and other modules.
5 * (C) 2007 United Security Providers
6 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/ctype.h>
17 #include <linux/skbuff.h>
18 #include <linux/inet.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>
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>
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
35 MODULE_DESCRIPTION("SIP connection tracking helper");
36 MODULE_ALIAS("ip_conntrack_sip");
37 MODULE_ALIAS_NFCT_HELPER("sip");
40 static unsigned short ports
[MAX_PORTS
];
41 static unsigned int ports_c
;
42 module_param_array(ports
, ushort
, &ports_c
, 0400);
43 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
45 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
46 module_param(sip_timeout
, uint
, 0600);
47 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
49 static int sip_direct_signalling __read_mostly
= 1;
50 module_param(sip_direct_signalling
, int, 0600);
51 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
54 static int sip_direct_media __read_mostly
= 1;
55 module_param(sip_direct_media
, int, 0600);
56 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
57 "endpoints only (default 1)");
59 static int sip_external_media __read_mostly
= 0;
60 module_param(sip_external_media
, int, 0600);
61 MODULE_PARM_DESC(sip_external_media
, "Expect Media streams between external "
62 "endpoints (default 0)");
64 const struct nf_nat_sip_hooks
*nf_nat_sip_hooks
;
65 EXPORT_SYMBOL_GPL(nf_nat_sip_hooks
);
67 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
68 const char *limit
, int *shift
)
72 while (dptr
< limit
&& isalpha(*dptr
)) {
79 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
80 const char *limit
, int *shift
)
83 while (dptr
< limit
&& isdigit(*dptr
)) {
90 static int iswordc(const char c
)
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
<= '/') ||
101 static int word_len(const char *dptr
, const char *limit
)
104 while (dptr
< limit
&& iswordc(*dptr
)) {
111 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
112 const char *limit
, int *shift
)
116 len
= word_len(dptr
, limit
);
118 if (!len
|| dptr
== limit
|| *dptr
!= '@')
123 domain_len
= word_len(dptr
, limit
);
126 return len
+ domain_len
;
129 /* get media type + port length */
130 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
131 const char *limit
, int *shift
)
133 int len
= string_len(ct
, dptr
, limit
, shift
);
136 if (dptr
>= limit
|| *dptr
!= ' ')
141 return len
+ digits_len(ct
, dptr
, limit
, shift
);
144 static 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
)
154 memset(addr
, 0, sizeof(*addr
));
155 switch (nf_ct_l3num(ct
)) {
157 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
162 if (cp
< limit
&& *cp
== '[')
167 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
171 if (end
< limit
&& *end
== ']')
185 /* skip ip address. returns its length. */
186 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
187 const char *limit
, int *shift
)
189 union nf_inet_addr addr
;
190 const char *aux
= dptr
;
192 if (!sip_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
, true)) {
193 pr_debug("ip: %s parse failed.!\n", dptr
);
200 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
205 /* get address length, skiping user info. */
206 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
207 const char *limit
, int *shift
)
209 const char *start
= dptr
;
212 /* Search for @, but stop at the end of the line.
213 * We are inside a sip: URI, so we don't need to worry about
214 * continuation lines. */
215 while (dptr
< limit
&&
216 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
221 if (dptr
< limit
&& *dptr
== '@') {
229 return epaddr_len(ct
, dptr
, limit
, shift
);
232 /* Parse a SIP request line of the form:
234 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
236 * and return the offset and length of the address contained in the Request-URI.
238 int 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
)
243 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
248 /* Skip method and following whitespace */
249 mlen
= string_len(ct
, dptr
, limit
, NULL
);
257 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
258 if (*dptr
== '\r' || *dptr
== '\n')
260 if (strncasecmp(dptr
, "sip:", strlen("sip:")) == 0) {
261 dptr
+= strlen("sip:");
265 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
269 if (!sip_parse_addr(ct
, dptr
, &end
, addr
, limit
, true))
271 if (end
< limit
&& *end
== ':') {
273 p
= simple_strtoul(end
, (char **)&end
, 10);
274 if (p
< 1024 || p
> 65535)
278 *port
= htons(SIP_PORT
);
282 *matchoff
= dptr
- start
;
283 *matchlen
= end
- dptr
;
286 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
288 /* SIP header parsing: SIP headers are located at the beginning of a line, but
289 * may span several lines, in which case the continuation lines begin with a
290 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
291 * CRLF, RFC 3261 allows only CRLF, we support both.
293 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
294 * whitespace and the values. Whitespace in this context means any amount of
295 * tabs, spaces and continuation lines, which are treated as a single whitespace
298 * Some headers may appear multiple times. A comma separated list of values is
299 * equivalent to multiple headers.
301 static 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
),
313 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
315 /* Walk past newline */
319 /* Skip '\n' in CR LF */
320 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
325 /* Continuation line? */
326 if (*dptr
!= ' ' && *dptr
!= '\t')
329 /* skip leading whitespace */
330 for (; dptr
< limit
; dptr
++) {
331 if (*dptr
!= ' ' && *dptr
!= '\t')
337 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
339 for (; dptr
< limit
; dptr
++) {
340 if (*dptr
== ' ' || *dptr
== '\t')
342 if (*dptr
!= '\r' && *dptr
!= '\n')
344 dptr
= sip_follow_continuation(dptr
, limit
);
350 /* Search within a SIP header value, dealing with continuation lines */
351 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
352 const char *needle
, unsigned int len
)
354 for (limit
-= len
; dptr
< limit
; dptr
++) {
355 if (*dptr
== '\r' || *dptr
== '\n') {
356 dptr
= sip_follow_continuation(dptr
, limit
);
362 if (strncasecmp(dptr
, needle
, len
) == 0)
368 int 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
)
373 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
374 const char *start
= dptr
, *limit
= dptr
+ datalen
;
377 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
378 /* Find beginning of line */
379 if (*dptr
!= '\r' && *dptr
!= '\n')
383 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
388 /* Skip continuation lines */
389 if (*dptr
== ' ' || *dptr
== '\t')
392 /* Find header. Compact headers must be followed by a
393 * non-alphabetic character to avoid mismatches. */
394 if (limit
- dptr
>= hdr
->len
&&
395 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
397 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
398 strncasecmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
399 !isalpha(*(dptr
+ hdr
->clen
)))
404 /* Find and skip colon */
405 dptr
= sip_skip_whitespace(dptr
, limit
);
408 if (*dptr
!= ':' || ++dptr
>= limit
)
411 /* Skip whitespace after colon */
412 dptr
= sip_skip_whitespace(dptr
, limit
);
416 *matchoff
= dptr
- start
;
418 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
425 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
428 *matchoff
= dptr
- start
+ shift
;
433 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
435 /* Get next header field in a list of comma separated values */
436 static 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
)
441 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
442 const char *start
= dptr
, *limit
= dptr
+ datalen
;
447 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
451 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
456 *matchoff
= dptr
- start
;
457 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
464 /* Walk through headers until a parsable one is found or no header of the
465 * given type is left. */
466 static 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
)
473 if (in_header
&& *in_header
) {
475 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
476 type
, matchoff
, matchlen
);
481 dataoff
+= *matchoff
;
487 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
488 type
, matchoff
, matchlen
);
493 dataoff
+= *matchoff
;
501 /* Locate a SIP header, parse the URI and return the offset and length of
502 * the address as well as the address and port themselves. A stream of
503 * headers can be parsed by handing in a non-NULL datalen and in_header
506 int 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
)
512 const char *c
, *limit
= dptr
+ datalen
;
516 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
517 type
, in_header
, matchoff
, matchlen
);
522 if (!sip_parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
, true))
526 p
= simple_strtoul(c
, (char **)&c
, 10);
527 if (p
< 1024 || p
> 65535)
531 *port
= htons(SIP_PORT
);
537 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
539 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
540 unsigned int dataoff
, unsigned int datalen
,
542 unsigned int *matchoff
, unsigned int *matchlen
)
544 const char *limit
= dptr
+ datalen
;
548 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
550 limit
= dptr
+ datalen
;
552 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
555 start
+= strlen(name
);
557 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
561 *matchoff
= start
- dptr
;
562 *matchlen
= end
- start
;
566 /* Parse address from header parameter and return address, offset and length */
567 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
568 unsigned int dataoff
, unsigned int datalen
,
570 unsigned int *matchoff
, unsigned int *matchlen
,
571 union nf_inet_addr
*addr
, bool delim
)
573 const char *limit
= dptr
+ datalen
;
574 const char *start
, *end
;
576 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
578 limit
= dptr
+ datalen
;
580 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
584 start
+= strlen(name
);
585 if (!sip_parse_addr(ct
, start
, &end
, addr
, limit
, delim
))
587 *matchoff
= start
- dptr
;
588 *matchlen
= end
- start
;
591 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
593 /* Parse numerical header parameter and return value, offset and length */
594 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
595 unsigned int dataoff
, unsigned int datalen
,
597 unsigned int *matchoff
, unsigned int *matchlen
,
600 const char *limit
= dptr
+ datalen
;
604 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
606 limit
= dptr
+ datalen
;
608 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
612 start
+= strlen(name
);
613 *val
= simple_strtoul(start
, &end
, 0);
616 if (matchoff
&& matchlen
) {
617 *matchoff
= start
- dptr
;
618 *matchlen
= end
- start
;
622 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
624 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
625 unsigned int dataoff
, unsigned int datalen
,
628 unsigned int matchoff
, matchlen
;
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
;
639 if (*proto
!= nf_ct_protonum(ct
))
642 *proto
= nf_ct_protonum(ct
);
647 static int sdp_parse_addr(const struct nf_conn
*ct
, const char *cp
,
648 const char **endp
, union nf_inet_addr
*addr
,
654 memset(addr
, 0, sizeof(*addr
));
655 switch (nf_ct_l3num(ct
)) {
657 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
660 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
673 /* skip ip address. returns its length. */
674 static int sdp_addr_len(const struct nf_conn
*ct
, const char *dptr
,
675 const char *limit
, int *shift
)
677 union nf_inet_addr addr
;
678 const char *aux
= dptr
;
680 if (!sdp_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
681 pr_debug("ip: %s parse failed.!\n", dptr
);
688 /* SDP header parsing: a SDP session description contains an ordered set of
689 * headers, starting with a section containing general session parameters,
690 * optionally followed by multiple media descriptions.
692 * SDP headers always start at the beginning of a line. According to RFC 2327:
693 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
694 * be tolerant and also accept records terminated with a single newline
695 * character". We handle both cases.
697 static 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
),
704 static 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
),
711 /* Linear string search within SDP header values */
712 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
713 const char *needle
, unsigned int len
)
715 for (limit
-= len
; dptr
< limit
; dptr
++) {
716 if (*dptr
== '\r' || *dptr
== '\n')
718 if (strncmp(dptr
, needle
, len
) == 0)
724 /* Locate a SDP header (optionally a substring within the header value),
725 * optionally stopping at the first occurrence of the term header, parse
726 * it and return the offset and length of the data we're interested in.
728 int 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
)
734 const struct sip_header
*hdrs
, *hdr
, *thdr
;
735 const char *start
= dptr
, *limit
= dptr
+ datalen
;
738 hdrs
= nf_ct_l3num(ct
) == NFPROTO_IPV4
? ct_sdp_hdrs_v4
: ct_sdp_hdrs_v6
;
742 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
743 /* Find beginning of line */
744 if (*dptr
!= '\r' && *dptr
!= '\n')
748 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
753 if (term
!= SDP_HDR_UNSPEC
&&
754 limit
- dptr
>= thdr
->len
&&
755 strncasecmp(dptr
, thdr
->name
, thdr
->len
) == 0)
757 else if (limit
- dptr
>= hdr
->len
&&
758 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
763 *matchoff
= dptr
- start
;
765 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
772 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
775 *matchoff
= dptr
- start
+ shift
;
780 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
782 static 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
)
791 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
796 if (!sdp_parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
797 dptr
+ *matchoff
+ *matchlen
))
802 static int refresh_signalling_expectation(struct nf_conn
*ct
,
803 union nf_inet_addr
*addr
,
804 u8 proto
, __be16 port
,
805 unsigned int expires
)
807 struct nf_conn_help
*help
= nfct_help(ct
);
808 struct nf_conntrack_expect
*exp
;
809 struct hlist_node
*next
;
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
)
819 if (mod_timer_pending(&exp
->timeout
, jiffies
+ expires
* HZ
)) {
820 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
825 spin_unlock_bh(&nf_conntrack_expect_lock
);
829 static void flush_expectations(struct nf_conn
*ct
, bool media
)
831 struct nf_conn_help
*help
= nfct_help(ct
);
832 struct nf_conntrack_expect
*exp
;
833 struct hlist_node
*next
;
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
)
839 if (!nf_ct_remove_expect(exp
))
844 spin_unlock_bh(&nf_conntrack_expect_lock
);
847 static 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
)
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
;
863 __be16 rtp_port
, rtcp_port
;
864 const struct nf_nat_sip_hooks
*hooks
;
867 if (sip_direct_media
) {
868 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
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
);
875 struct dst_entry
*dst
= NULL
;
877 memset(&fl
, 0, sizeof(fl
));
879 switch (nf_ct_l3num(ct
)) {
881 fl
.u
.ip4
.daddr
= daddr
->ip
;
882 nf_ip_route(net
, &dst
, &fl
, false);
886 fl
.u
.ip6
.daddr
= daddr
->in6
;
887 nf_ip6_route(net
, &dst
, &fl
, false);
891 /* Don't predict any conntracks when media endpoint is reachable
892 * through the same interface as the signalling peer.
895 bool external_media
= (dst
->dev
== dev
);
903 /* We need to check whether the registration exists before attempting
904 * to register it since we can see the same media description multiple
905 * times on different connections in case multiple endpoints receive
908 * RTP optimization: if we find a matching media channel expectation
909 * and both the expectation and this connection are SNATed, we assume
910 * both sides can reach each other directly and use the final
911 * destination address from the expectation. We still need to keep
912 * the NATed expectations for media that might arrive from the
913 * outside, and additionally need to expect the direct RTP stream
914 * in case it passes through us even without NAT.
916 memset(&tuple
, 0, sizeof(tuple
));
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
;
925 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
927 if (!exp
|| exp
->master
== ct
||
928 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
931 #ifdef CONFIG_NF_NAT_NEEDED
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
;
943 } while (!skip_expect
);
945 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
946 rtp_port
= htons(base_port
);
947 rtcp_port
= htons(base_port
+ 1);
950 hooks
= rcu_dereference(nf_nat_sip_hooks
);
952 !hooks
->sdp_port(skb
, protoff
, dataoff
, dptr
, datalen
,
953 mediaoff
, medialen
, ntohs(rtp_port
)))
960 rtp_exp
= nf_ct_expect_alloc(ct
);
963 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
964 IPPROTO_UDP
, NULL
, &rtp_port
);
966 rtcp_exp
= nf_ct_expect_alloc(ct
);
967 if (rtcp_exp
== NULL
)
969 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
970 IPPROTO_UDP
, NULL
, &rtcp_port
);
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
);
978 /* -EALREADY handling works around end-points that send
979 * SDP messages with identical port but different media type,
980 * we pretend expectation was set up.
982 int errp
= nf_ct_expect_related(rtp_exp
);
984 if (errp
== 0 || errp
== -EALREADY
) {
985 int errcp
= nf_ct_expect_related(rtcp_exp
);
987 if (errcp
== 0 || errcp
== -EALREADY
)
990 nf_ct_unexpect_related(rtp_exp
);
993 nf_ct_expect_put(rtcp_exp
);
995 nf_ct_expect_put(rtp_exp
);
1000 static 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
),
1006 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
1007 unsigned int matchoff
,
1008 unsigned int matchlen
)
1010 const struct sdp_media_type
*t
;
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
))
1023 static int process_sdp(struct sk_buff
*skb
, unsigned int protoff
,
1024 unsigned int dataoff
,
1025 const char **dptr
, unsigned int *datalen
,
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
;
1035 union nf_inet_addr caddr
, maddr
, rtp_addr
;
1036 const struct nf_nat_sip_hooks
*hooks
;
1038 const struct sdp_media_type
*t
;
1039 int ret
= NF_ACCEPT
;
1041 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1043 /* Find beginning of session description */
1044 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
1045 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
1046 &matchoff
, &matchlen
) <= 0)
1050 /* The connection information is contained in the session description
1051 * and/or once per media description. The first media description marks
1052 * the end of the session description. */
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
;
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)
1066 /* Get media type and port number. A media port value of zero
1067 * indicates an inactive stream. */
1068 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1070 mediaoff
+= medialen
;
1076 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1079 if (port
< 1024 || port
> 65535) {
1080 nf_ct_helper_log(skb
, ct
, "wrong port %u", port
);
1084 /* The media description overrides the session description. */
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
));
1094 nf_ct_helper_log(skb
, ct
, "cannot parse SDP message");
1098 ret
= set_expected_rtp_rtcp(skb
, protoff
, dataoff
,
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");
1108 /* Update media connection address if present */
1109 if (maddr_len
&& hooks
&& ct
->status
& IPS_NAT_MASK
) {
1110 ret
= hooks
->sdp_addr(skb
, protoff
, dataoff
,
1111 dptr
, datalen
, mediaoff
,
1115 if (ret
!= NF_ACCEPT
) {
1116 nf_ct_helper_log(skb
, ct
, "cannot mangle SDP");
1123 /* Update session connection and owner addresses */
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
,
1132 static 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
)
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
);
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);
1149 static 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
)
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
);
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);
1166 static 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
)
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
);
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);
1183 static int process_invite_request(struct sk_buff
*skb
, unsigned int protoff
,
1184 unsigned int dataoff
,
1185 const char **dptr
, unsigned int *datalen
,
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
);
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
;
1200 static int process_bye_request(struct sk_buff
*skb
, unsigned int protoff
,
1201 unsigned int dataoff
,
1202 const char **dptr
, unsigned int *datalen
,
1205 enum ip_conntrack_info ctinfo
;
1206 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1208 flush_expectations(ct
, true);
1212 /* Parse a REGISTER request and create a permanent expectation for incoming
1213 * signalling connections. The expectation is marked inactive and is activated
1214 * when receiving a response indicating success from the registrar.
1216 static int process_register_request(struct sk_buff
*skb
, unsigned int protoff
,
1217 unsigned int dataoff
,
1218 const char **dptr
, unsigned int *datalen
,
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
;
1231 unsigned int expires
= 0;
1234 /* Expected connections can not register again. */
1235 if (ct
->status
& IPS_EXPECTED
)
1238 /* We must check the expiration time: a value of zero signals the
1239 * registrar to release the binding. We'll remove our expectation
1240 * when receiving the new bindings in the response, but we don't
1241 * want to create new ones.
1243 * The expiration time may be contained in Expires: header, the
1244 * Contact: header parameters or the URI parameters.
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);
1250 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1251 SIP_HDR_CONTACT
, NULL
,
1252 &matchoff
, &matchlen
, &daddr
, &port
);
1254 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1256 } else if (ret
== 0)
1259 /* We don't support third-party registrations */
1260 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1263 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
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");
1279 exp
= nf_ct_expect_alloc(ct
);
1281 nf_ct_helper_log(skb
, ct
, "cannot alloc expectation");
1286 if (sip_direct_signalling
)
1287 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
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
;
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
);
1300 if (nf_ct_expect_related(exp
) != 0) {
1301 nf_ct_helper_log(skb
, ct
, "cannot add expectation");
1306 nf_ct_expect_put(exp
);
1309 if (ret
== NF_ACCEPT
)
1310 ct_sip_info
->register_cseq
= cseq
;
1314 static 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
)
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
;
1326 unsigned int matchoff
, matchlen
, coff
= 0;
1327 unsigned int expires
= 0;
1328 int in_contact
= 0, ret
;
1330 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1331 * they have received a final response from the registrar for the
1332 * previous one or the previous REGISTER request has timed out".
1334 * However, some servers fail to detect retransmissions and send late
1335 * responses, so we store the sequence number of the last valid
1336 * request and compare it here.
1338 if (ct_sip_info
->register_cseq
!= cseq
)
1341 if (code
>= 100 && code
<= 199)
1343 if (code
< 200 || code
> 299)
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);
1351 unsigned int c_expires
= expires
;
1353 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1354 SIP_HDR_CONTACT
, &in_contact
,
1355 &matchoff
, &matchlen
,
1358 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1360 } else if (ret
== 0)
1363 /* We don't support third-party registrations */
1364 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1367 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1368 *datalen
, &proto
) == 0)
1371 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1372 matchoff
+ matchlen
,
1373 *datalen
, "expires=",
1374 NULL
, NULL
, &c_expires
);
1376 nf_ct_helper_log(skb
, ct
, "cannot parse expires");
1381 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1387 flush_expectations(ct
, false);
1391 static 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
),
1400 static int process_sip_response(struct sk_buff
*skb
, unsigned int protoff
,
1401 unsigned int dataoff
,
1402 const char **dptr
, unsigned int *datalen
)
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
;
1409 if (*datalen
< strlen("SIP/2.0 200"))
1411 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1413 nf_ct_helper_log(skb
, ct
, "cannot get code");
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");
1422 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1423 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1424 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1427 matchend
= matchoff
+ matchlen
+ 1;
1429 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1430 const struct sip_handler
*handler
;
1432 handler
= &sip_handlers
[i
];
1433 if (handler
->response
== NULL
)
1435 if (*datalen
< matchend
+ handler
->len
||
1436 strncasecmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1438 return handler
->response(skb
, protoff
, dataoff
, dptr
, datalen
,
1444 static int process_sip_request(struct sk_buff
*skb
, unsigned int protoff
,
1445 unsigned int dataoff
,
1446 const char **dptr
, unsigned int *datalen
)
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
;
1457 /* Many Cisco IP phones use a high source port for SIP requests, but
1458 * listen for the response on port 5060. If we are the local
1459 * router for one of these phones, save the port number from the
1460 * Via: header so that nf_nat_sip can redirect the responses to
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
;
1470 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1471 const struct sip_handler
*handler
;
1473 handler
= &sip_handlers
[i
];
1474 if (handler
->request
== NULL
)
1476 if (*datalen
< handler
->len
+ 2 ||
1477 strncasecmp(*dptr
, handler
->method
, handler
->len
))
1479 if ((*dptr
)[handler
->len
] != ' ' ||
1480 !isalpha((*dptr
)[handler
->len
+1]))
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");
1488 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1489 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1490 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1494 return handler
->request(skb
, protoff
, dataoff
, dptr
, datalen
,
1500 static 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
)
1504 const struct nf_nat_sip_hooks
*hooks
;
1507 if (strncasecmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1508 ret
= process_sip_request(skb
, protoff
, dataoff
, dptr
, datalen
);
1510 ret
= process_sip_response(skb
, protoff
, dataoff
, dptr
, datalen
);
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
,
1516 nf_ct_helper_log(skb
, ct
, "cannot NAT SIP message");
1524 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1525 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
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
;
1536 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1537 ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1541 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1544 dataoff
= protoff
+ th
->doff
* 4;
1545 if (dataoff
>= skb
->len
)
1548 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1550 if (unlikely(skb_linearize(skb
)))
1553 dptr
= skb
->data
+ dataoff
;
1554 datalen
= skb
->len
- dataoff
;
1555 if (datalen
< strlen("SIP/2.0 200"))
1559 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1560 SIP_HDR_CONTENT_LENGTH
,
1561 &matchoff
, &matchlen
) <= 0)
1564 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1565 if (dptr
+ matchoff
== end
)
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') {
1578 end
+= strlen("\r\n\r\n") + clen
;
1580 msglen
= origlen
= end
- dptr
;
1581 if (msglen
> datalen
)
1584 ret
= process_sip_msg(skb
, ct
, protoff
, dataoff
,
1586 /* process_sip_* functions report why this packet is dropped */
1587 if (ret
!= NF_ACCEPT
)
1589 diff
= msglen
- origlen
;
1594 datalen
= datalen
+ diff
- msglen
;
1597 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1598 const struct nf_nat_sip_hooks
*hooks
;
1600 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1602 hooks
->seq_adjust(skb
, protoff
, tdiff
);
1608 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1609 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1611 unsigned int dataoff
, datalen
;
1615 dataoff
= protoff
+ sizeof(struct udphdr
);
1616 if (dataoff
>= skb
->len
)
1619 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1621 if (unlikely(skb_linearize(skb
)))
1624 dptr
= skb
->data
+ dataoff
;
1625 datalen
= skb
->len
- dataoff
;
1626 if (datalen
< strlen("SIP/2.0 200"))
1629 return process_sip_msg(skb
, ct
, protoff
, dataoff
, &dptr
, &datalen
);
1632 static struct nf_conntrack_helper sip
[MAX_PORTS
* 4] __read_mostly
;
1634 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1635 [SIP_EXPECT_SIGNALLING
] = {
1636 .name
= "signalling",
1640 [SIP_EXPECT_AUDIO
] = {
1642 .max_expected
= 2 * IP_CT_DIR_MAX
,
1645 [SIP_EXPECT_VIDEO
] = {
1647 .max_expected
= 2 * IP_CT_DIR_MAX
,
1650 [SIP_EXPECT_IMAGE
] = {
1652 .max_expected
= IP_CT_DIR_MAX
,
1657 static void __exit
nf_conntrack_sip_fini(void)
1659 nf_conntrack_helpers_unregister(sip
, ports_c
* 4);
1662 static int __init
nf_conntrack_sip_init(void)
1666 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sip_master
));
1669 ports
[ports_c
++] = SIP_PORT
;
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
,
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
,
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
,
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
,
1690 ret
= nf_conntrack_helpers_register(sip
, ports_c
* 4);
1692 pr_err("failed to register helpers\n");
1698 module_init(nf_conntrack_sip_init
);
1699 module_exit(nf_conntrack_sip_fini
);