1 // SPDX-License-Identifier: GPL-2.0-only
2 /* SIP extension for IP connection tracking.
4 * (C) 2005 by Christian Hentschel <chentschel@arnet.com.ar>
5 * based on RR's ip_conntrack_ftp.c and other modules.
6 * (C) 2007 United Security Providers
7 * (C) 2007, 2008 Patrick McHardy <kaber@trash.net>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/ctype.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet.h>
17 #include <linux/udp.h>
18 #include <linux/tcp.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_zones.h>
28 #include <linux/netfilter/nf_conntrack_sip.h>
30 #define HELPER_NAME "sip"
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
34 MODULE_DESCRIPTION("SIP connection tracking helper");
35 MODULE_ALIAS("ip_conntrack_sip");
36 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME
);
39 static unsigned short ports
[MAX_PORTS
];
40 static unsigned int ports_c
;
41 module_param_array(ports
, ushort
, &ports_c
, 0400);
42 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
44 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
45 module_param(sip_timeout
, uint
, 0600);
46 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
48 static int sip_direct_signalling __read_mostly
= 1;
49 module_param(sip_direct_signalling
, int, 0600);
50 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
53 static int sip_direct_media __read_mostly
= 1;
54 module_param(sip_direct_media
, int, 0600);
55 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
56 "endpoints only (default 1)");
58 static int sip_external_media __read_mostly
= 0;
59 module_param(sip_external_media
, int, 0600);
60 MODULE_PARM_DESC(sip_external_media
, "Expect Media streams between external "
61 "endpoints (default 0)");
63 const struct nf_nat_sip_hooks __rcu
*nf_nat_sip_hooks
;
64 EXPORT_SYMBOL_GPL(nf_nat_sip_hooks
);
66 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
67 const char *limit
, int *shift
)
71 while (dptr
< limit
&& isalpha(*dptr
)) {
78 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
79 const char *limit
, int *shift
)
82 while (dptr
< limit
&& isdigit(*dptr
)) {
89 static int iswordc(const char c
)
91 if (isalnum(c
) || c
== '!' || c
== '"' || c
== '%' ||
92 (c
>= '(' && c
<= '+') || c
== ':' || c
== '<' || c
== '>' ||
93 c
== '?' || (c
>= '[' && c
<= ']') || c
== '_' || c
== '`' ||
94 c
== '{' || c
== '}' || c
== '~' || (c
>= '-' && c
<= '/') ||
100 static int word_len(const char *dptr
, const char *limit
)
103 while (dptr
< limit
&& iswordc(*dptr
)) {
110 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
111 const char *limit
, int *shift
)
115 len
= word_len(dptr
, limit
);
117 if (!len
|| dptr
== limit
|| *dptr
!= '@')
122 domain_len
= word_len(dptr
, limit
);
125 return len
+ domain_len
;
128 /* get media type + port length */
129 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
130 const char *limit
, int *shift
)
132 int len
= string_len(ct
, dptr
, limit
, shift
);
135 if (dptr
>= limit
|| *dptr
!= ' ')
140 return len
+ digits_len(ct
, dptr
, limit
, shift
);
143 static int sip_parse_addr(const struct nf_conn
*ct
, const char *cp
,
144 const char **endp
, union nf_inet_addr
*addr
,
145 const char *limit
, bool delim
)
153 memset(addr
, 0, sizeof(*addr
));
154 switch (nf_ct_l3num(ct
)) {
156 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
161 if (cp
< limit
&& *cp
== '[')
166 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
170 if (end
< limit
&& *end
== ']')
184 /* skip ip address. returns its length. */
185 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
186 const char *limit
, int *shift
)
188 union nf_inet_addr addr
;
189 const char *aux
= dptr
;
191 if (!sip_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
, true)) {
192 pr_debug("ip: %s parse failed.!\n", dptr
);
199 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
204 /* get address length, skiping user info. */
205 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
206 const char *limit
, int *shift
)
208 const char *start
= dptr
;
211 /* Search for @, but stop at the end of the line.
212 * We are inside a sip: URI, so we don't need to worry about
213 * continuation lines. */
214 while (dptr
< limit
&&
215 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
220 if (dptr
< limit
&& *dptr
== '@') {
228 return epaddr_len(ct
, dptr
, limit
, shift
);
231 /* Parse a SIP request line of the form:
233 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
235 * and return the offset and length of the address contained in the Request-URI.
237 int ct_sip_parse_request(const struct nf_conn
*ct
,
238 const char *dptr
, unsigned int datalen
,
239 unsigned int *matchoff
, unsigned int *matchlen
,
240 union nf_inet_addr
*addr
, __be16
*port
)
242 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
247 /* Skip method and following whitespace */
248 mlen
= string_len(ct
, dptr
, limit
, NULL
);
256 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
257 if (*dptr
== '\r' || *dptr
== '\n')
259 if (strncasecmp(dptr
, "sip:", strlen("sip:")) == 0) {
260 dptr
+= strlen("sip:");
264 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
268 if (!sip_parse_addr(ct
, dptr
, &end
, addr
, limit
, true))
270 if (end
< limit
&& *end
== ':') {
272 p
= simple_strtoul(end
, (char **)&end
, 10);
273 if (p
< 1024 || p
> 65535)
277 *port
= htons(SIP_PORT
);
281 *matchoff
= dptr
- start
;
282 *matchlen
= end
- dptr
;
285 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
287 /* SIP header parsing: SIP headers are located at the beginning of a line, but
288 * may span several lines, in which case the continuation lines begin with a
289 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
290 * CRLF, RFC 3261 allows only CRLF, we support both.
292 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
293 * whitespace and the values. Whitespace in this context means any amount of
294 * tabs, spaces and continuation lines, which are treated as a single whitespace
297 * Some headers may appear multiple times. A comma separated list of values is
298 * equivalent to multiple headers.
300 static const struct sip_header ct_sip_hdrs
[] = {
301 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
302 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
303 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
304 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
305 [SIP_HDR_VIA_UDP
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
306 [SIP_HDR_VIA_TCP
] = SIP_HDR("Via", "v", "TCP ", epaddr_len
),
307 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
308 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
309 [SIP_HDR_CALL_ID
] = SIP_HDR("Call-Id", "i", NULL
, callid_len
),
312 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
314 /* Walk past newline */
318 /* Skip '\n' in CR LF */
319 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
324 /* Continuation line? */
325 if (*dptr
!= ' ' && *dptr
!= '\t')
328 /* skip leading whitespace */
329 for (; dptr
< limit
; dptr
++) {
330 if (*dptr
!= ' ' && *dptr
!= '\t')
336 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
338 for (; dptr
< limit
; dptr
++) {
339 if (*dptr
== ' ' || *dptr
== '\t')
341 if (*dptr
!= '\r' && *dptr
!= '\n')
343 dptr
= sip_follow_continuation(dptr
, limit
);
349 /* Search within a SIP header value, dealing with continuation lines */
350 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
351 const char *needle
, unsigned int len
)
353 for (limit
-= len
; dptr
< limit
; dptr
++) {
354 if (*dptr
== '\r' || *dptr
== '\n') {
355 dptr
= sip_follow_continuation(dptr
, limit
);
361 if (strncasecmp(dptr
, needle
, len
) == 0)
367 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
368 unsigned int dataoff
, unsigned int datalen
,
369 enum sip_header_types type
,
370 unsigned int *matchoff
, unsigned int *matchlen
)
372 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
373 const char *start
= dptr
, *limit
= dptr
+ datalen
;
376 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
377 /* Find beginning of line */
378 if (*dptr
!= '\r' && *dptr
!= '\n')
382 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
387 /* Skip continuation lines */
388 if (*dptr
== ' ' || *dptr
== '\t')
391 /* Find header. Compact headers must be followed by a
392 * non-alphabetic character to avoid mismatches. */
393 if (limit
- dptr
>= hdr
->len
&&
394 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
396 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
397 strncasecmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
398 !isalpha(*(dptr
+ hdr
->clen
)))
403 /* Find and skip colon */
404 dptr
= sip_skip_whitespace(dptr
, limit
);
407 if (*dptr
!= ':' || ++dptr
>= limit
)
410 /* Skip whitespace after colon */
411 dptr
= sip_skip_whitespace(dptr
, limit
);
415 *matchoff
= dptr
- start
;
417 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
424 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
427 *matchoff
= dptr
- start
+ shift
;
432 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
434 /* Get next header field in a list of comma separated values */
435 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
436 unsigned int dataoff
, unsigned int datalen
,
437 enum sip_header_types type
,
438 unsigned int *matchoff
, unsigned int *matchlen
)
440 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
441 const char *start
= dptr
, *limit
= dptr
+ datalen
;
446 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
450 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
455 *matchoff
= dptr
- start
;
456 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
463 /* Walk through headers until a parsable one is found or no header of the
464 * given type is left. */
465 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
466 unsigned int dataoff
, unsigned int datalen
,
467 enum sip_header_types type
, int *in_header
,
468 unsigned int *matchoff
, unsigned int *matchlen
)
472 if (in_header
&& *in_header
) {
474 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
475 type
, matchoff
, matchlen
);
486 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
487 type
, matchoff
, matchlen
);
500 /* Locate a SIP header, parse the URI and return the offset and length of
501 * the address as well as the address and port themselves. A stream of
502 * headers can be parsed by handing in a non-NULL datalen and in_header
505 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
506 unsigned int *dataoff
, unsigned int datalen
,
507 enum sip_header_types type
, int *in_header
,
508 unsigned int *matchoff
, unsigned int *matchlen
,
509 union nf_inet_addr
*addr
, __be16
*port
)
511 const char *c
, *limit
= dptr
+ datalen
;
515 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
516 type
, in_header
, matchoff
, matchlen
);
521 if (!sip_parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
, true))
525 p
= simple_strtoul(c
, (char **)&c
, 10);
526 if (p
< 1024 || p
> 65535)
530 *port
= htons(SIP_PORT
);
536 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
538 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
539 unsigned int dataoff
, unsigned int datalen
,
541 unsigned int *matchoff
, unsigned int *matchlen
)
543 const char *limit
= dptr
+ datalen
;
547 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
549 limit
= dptr
+ datalen
;
551 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
554 start
+= strlen(name
);
556 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
560 *matchoff
= start
- dptr
;
561 *matchlen
= end
- start
;
565 /* Parse address from header parameter and return address, offset and length */
566 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
567 unsigned int dataoff
, unsigned int datalen
,
569 unsigned int *matchoff
, unsigned int *matchlen
,
570 union nf_inet_addr
*addr
, bool delim
)
572 const char *limit
= dptr
+ datalen
;
573 const char *start
, *end
;
575 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
577 limit
= dptr
+ datalen
;
579 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
583 start
+= strlen(name
);
584 if (!sip_parse_addr(ct
, start
, &end
, addr
, limit
, delim
))
586 *matchoff
= start
- dptr
;
587 *matchlen
= end
- start
;
590 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
592 /* Parse numerical header parameter and return value, offset and length */
593 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
594 unsigned int dataoff
, unsigned int datalen
,
596 unsigned int *matchoff
, unsigned int *matchlen
,
599 const char *limit
= dptr
+ datalen
;
603 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
605 limit
= dptr
+ datalen
;
607 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
611 start
+= strlen(name
);
612 *val
= simple_strtoul(start
, &end
, 0);
615 if (matchoff
&& matchlen
) {
616 *matchoff
= start
- dptr
;
617 *matchlen
= end
- start
;
621 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
623 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
624 unsigned int dataoff
, unsigned int datalen
,
627 unsigned int matchoff
, matchlen
;
629 if (ct_sip_parse_param(ct
, dptr
, dataoff
, datalen
, "transport=",
630 &matchoff
, &matchlen
)) {
631 if (!strncasecmp(dptr
+ matchoff
, "TCP", strlen("TCP")))
632 *proto
= IPPROTO_TCP
;
633 else if (!strncasecmp(dptr
+ matchoff
, "UDP", strlen("UDP")))
634 *proto
= IPPROTO_UDP
;
638 if (*proto
!= nf_ct_protonum(ct
))
641 *proto
= nf_ct_protonum(ct
);
646 static int sdp_parse_addr(const struct nf_conn
*ct
, const char *cp
,
647 const char **endp
, union nf_inet_addr
*addr
,
653 memset(addr
, 0, sizeof(*addr
));
654 switch (nf_ct_l3num(ct
)) {
656 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
659 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
672 /* skip ip address. returns its length. */
673 static int sdp_addr_len(const struct nf_conn
*ct
, const char *dptr
,
674 const char *limit
, int *shift
)
676 union nf_inet_addr addr
;
677 const char *aux
= dptr
;
679 if (!sdp_parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
680 pr_debug("ip: %s parse failed.!\n", dptr
);
687 /* SDP header parsing: a SDP session description contains an ordered set of
688 * headers, starting with a section containing general session parameters,
689 * optionally followed by multiple media descriptions.
691 * SDP headers always start at the beginning of a line. According to RFC 2327:
692 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
693 * be tolerant and also accept records terminated with a single newline
694 * character". We handle both cases.
696 static const struct sip_header ct_sdp_hdrs_v4
[] = {
697 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
698 [SDP_HDR_OWNER
] = SDP_HDR("o=", "IN IP4 ", sdp_addr_len
),
699 [SDP_HDR_CONNECTION
] = SDP_HDR("c=", "IN IP4 ", sdp_addr_len
),
700 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
703 static const struct sip_header ct_sdp_hdrs_v6
[] = {
704 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
705 [SDP_HDR_OWNER
] = SDP_HDR("o=", "IN IP6 ", sdp_addr_len
),
706 [SDP_HDR_CONNECTION
] = SDP_HDR("c=", "IN IP6 ", sdp_addr_len
),
707 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
710 /* Linear string search within SDP header values */
711 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
712 const char *needle
, unsigned int len
)
714 for (limit
-= len
; dptr
< limit
; dptr
++) {
715 if (*dptr
== '\r' || *dptr
== '\n')
717 if (strncmp(dptr
, needle
, len
) == 0)
723 /* Locate a SDP header (optionally a substring within the header value),
724 * optionally stopping at the first occurrence of the term header, parse
725 * it and return the offset and length of the data we're interested in.
727 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
728 unsigned int dataoff
, unsigned int datalen
,
729 enum sdp_header_types type
,
730 enum sdp_header_types term
,
731 unsigned int *matchoff
, unsigned int *matchlen
)
733 const struct sip_header
*hdrs
, *hdr
, *thdr
;
734 const char *start
= dptr
, *limit
= dptr
+ datalen
;
737 hdrs
= nf_ct_l3num(ct
) == NFPROTO_IPV4
? ct_sdp_hdrs_v4
: ct_sdp_hdrs_v6
;
741 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
742 /* Find beginning of line */
743 if (*dptr
!= '\r' && *dptr
!= '\n')
747 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
752 if (term
!= SDP_HDR_UNSPEC
&&
753 limit
- dptr
>= thdr
->len
&&
754 strncasecmp(dptr
, thdr
->name
, thdr
->len
) == 0)
756 else if (limit
- dptr
>= hdr
->len
&&
757 strncasecmp(dptr
, hdr
->name
, hdr
->len
) == 0)
762 *matchoff
= dptr
- start
;
764 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
771 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
774 *matchoff
= dptr
- start
+ shift
;
779 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
781 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
782 unsigned int dataoff
, unsigned int datalen
,
783 enum sdp_header_types type
,
784 enum sdp_header_types term
,
785 unsigned int *matchoff
, unsigned int *matchlen
,
786 union nf_inet_addr
*addr
)
790 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
795 if (!sdp_parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
796 dptr
+ *matchoff
+ *matchlen
))
801 static int refresh_signalling_expectation(struct nf_conn
*ct
,
802 union nf_inet_addr
*addr
,
803 u8 proto
, __be16 port
,
804 unsigned int expires
)
806 struct nf_conn_help
*help
= nfct_help(ct
);
807 struct nf_conntrack_expect
*exp
;
808 struct hlist_node
*next
;
811 spin_lock_bh(&nf_conntrack_expect_lock
);
812 hlist_for_each_entry_safe(exp
, next
, &help
->expectations
, lnode
) {
813 if (exp
->class != SIP_EXPECT_SIGNALLING
||
814 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
815 exp
->tuple
.dst
.protonum
!= proto
||
816 exp
->tuple
.dst
.u
.udp
.port
!= port
)
818 if (mod_timer_pending(&exp
->timeout
, jiffies
+ expires
* HZ
)) {
819 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
824 spin_unlock_bh(&nf_conntrack_expect_lock
);
828 static void flush_expectations(struct nf_conn
*ct
, bool media
)
830 struct nf_conn_help
*help
= nfct_help(ct
);
831 struct nf_conntrack_expect
*exp
;
832 struct hlist_node
*next
;
834 spin_lock_bh(&nf_conntrack_expect_lock
);
835 hlist_for_each_entry_safe(exp
, next
, &help
->expectations
, lnode
) {
836 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
838 if (!nf_ct_remove_expect(exp
))
843 spin_unlock_bh(&nf_conntrack_expect_lock
);
846 static int set_expected_rtp_rtcp(struct sk_buff
*skb
, unsigned int protoff
,
847 unsigned int dataoff
,
848 const char **dptr
, unsigned int *datalen
,
849 union nf_inet_addr
*daddr
, __be16 port
,
850 enum sip_expectation_classes
class,
851 unsigned int mediaoff
, unsigned int medialen
)
853 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
854 enum ip_conntrack_info ctinfo
;
855 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
856 struct net
*net
= nf_ct_net(ct
);
857 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
858 union nf_inet_addr
*saddr
;
859 struct nf_conntrack_tuple tuple
;
860 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
862 __be16 rtp_port
, rtcp_port
;
863 const struct nf_nat_sip_hooks
*hooks
;
866 if (sip_direct_media
) {
867 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
869 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
870 } else if (sip_external_media
) {
871 struct net_device
*dev
= skb_dst(skb
)->dev
;
872 struct net
*net
= dev_net(dev
);
874 struct dst_entry
*dst
= NULL
;
876 memset(&fl
, 0, sizeof(fl
));
878 switch (nf_ct_l3num(ct
)) {
880 fl
.u
.ip4
.daddr
= daddr
->ip
;
881 nf_ip_route(net
, &dst
, &fl
, false);
885 fl
.u
.ip6
.daddr
= daddr
->in6
;
886 nf_ip6_route(net
, &dst
, &fl
, false);
890 /* Don't predict any conntracks when media endpoint is reachable
891 * through the same interface as the signalling peer.
894 bool external_media
= (dst
->dev
== dev
);
902 /* We need to check whether the registration exists before attempting
903 * to register it since we can see the same media description multiple
904 * times on different connections in case multiple endpoints receive
907 * RTP optimization: if we find a matching media channel expectation
908 * and both the expectation and this connection are SNATed, we assume
909 * both sides can reach each other directly and use the final
910 * destination address from the expectation. We still need to keep
911 * the NATed expectations for media that might arrive from the
912 * outside, and additionally need to expect the direct RTP stream
913 * in case it passes through us even without NAT.
915 memset(&tuple
, 0, sizeof(tuple
));
917 tuple
.src
.u3
= *saddr
;
918 tuple
.src
.l3num
= nf_ct_l3num(ct
);
919 tuple
.dst
.protonum
= IPPROTO_UDP
;
920 tuple
.dst
.u3
= *daddr
;
921 tuple
.dst
.u
.udp
.port
= port
;
924 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
926 if (!exp
|| exp
->master
== ct
||
927 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
930 #if IS_ENABLED(CONFIG_NF_NAT)
932 (!nf_inet_addr_cmp(&exp
->saved_addr
, &exp
->tuple
.dst
.u3
) ||
933 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
934 ct
->status
& IPS_NAT_MASK
) {
935 *daddr
= exp
->saved_addr
;
936 tuple
.dst
.u3
= exp
->saved_addr
;
937 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
942 } while (!skip_expect
);
944 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
945 rtp_port
= htons(base_port
);
946 rtcp_port
= htons(base_port
+ 1);
949 hooks
= rcu_dereference(nf_nat_sip_hooks
);
951 !hooks
->sdp_port(skb
, protoff
, dataoff
, dptr
, datalen
,
952 mediaoff
, medialen
, ntohs(rtp_port
)))
959 rtp_exp
= nf_ct_expect_alloc(ct
);
962 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
963 IPPROTO_UDP
, NULL
, &rtp_port
);
965 rtcp_exp
= nf_ct_expect_alloc(ct
);
966 if (rtcp_exp
== NULL
)
968 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
969 IPPROTO_UDP
, NULL
, &rtcp_port
);
971 hooks
= rcu_dereference(nf_nat_sip_hooks
);
972 if (hooks
&& ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
973 ret
= hooks
->sdp_media(skb
, protoff
, dataoff
, dptr
,
974 datalen
, rtp_exp
, rtcp_exp
,
975 mediaoff
, medialen
, daddr
);
977 /* -EALREADY handling works around end-points that send
978 * SDP messages with identical port but different media type,
979 * we pretend expectation was set up.
980 * It also works in the case that SDP messages are sent with
981 * identical expect tuples but for different master conntracks.
983 int errp
= nf_ct_expect_related(rtp_exp
,
984 NF_CT_EXP_F_SKIP_MASTER
);
986 if (errp
== 0 || errp
== -EALREADY
) {
987 int errcp
= nf_ct_expect_related(rtcp_exp
,
988 NF_CT_EXP_F_SKIP_MASTER
);
990 if (errcp
== 0 || errcp
== -EALREADY
)
993 nf_ct_unexpect_related(rtp_exp
);
996 nf_ct_expect_put(rtcp_exp
);
998 nf_ct_expect_put(rtp_exp
);
1003 static const struct sdp_media_type sdp_media_types
[] = {
1004 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
1005 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
1006 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE
),
1009 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
1010 unsigned int matchoff
,
1011 unsigned int matchlen
)
1013 const struct sdp_media_type
*t
;
1016 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
1017 t
= &sdp_media_types
[i
];
1018 if (matchlen
< t
->len
||
1019 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
1026 static int process_sdp(struct sk_buff
*skb
, unsigned int protoff
,
1027 unsigned int dataoff
,
1028 const char **dptr
, unsigned int *datalen
,
1031 enum ip_conntrack_info ctinfo
;
1032 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1033 unsigned int matchoff
, matchlen
;
1034 unsigned int mediaoff
, medialen
;
1035 unsigned int sdpoff
;
1036 unsigned int caddr_len
, maddr_len
;
1038 union nf_inet_addr caddr
, maddr
, rtp_addr
;
1039 const struct nf_nat_sip_hooks
*hooks
;
1041 const struct sdp_media_type
*t
;
1042 int ret
= NF_ACCEPT
;
1044 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1046 /* Find beginning of session description */
1047 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
1048 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
1049 &matchoff
, &matchlen
) <= 0)
1053 /* The connection information is contained in the session description
1054 * and/or once per media description. The first media description marks
1055 * the end of the session description. */
1057 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
1058 SDP_HDR_CONNECTION
, SDP_HDR_MEDIA
,
1059 &matchoff
, &matchlen
, &caddr
) > 0)
1060 caddr_len
= matchlen
;
1063 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
1064 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
1065 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
1066 &mediaoff
, &medialen
) <= 0)
1069 /* Get media type and port number. A media port value of zero
1070 * indicates an inactive stream. */
1071 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1073 mediaoff
+= medialen
;
1079 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1082 if (port
< 1024 || port
> 65535) {
1083 nf_ct_helper_log(skb
, ct
, "wrong port %u", port
);
1087 /* The media description overrides the session description. */
1089 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
1090 SDP_HDR_CONNECTION
, SDP_HDR_MEDIA
,
1091 &matchoff
, &matchlen
, &maddr
) > 0) {
1092 maddr_len
= matchlen
;
1093 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
1094 } else if (caddr_len
)
1095 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
1097 nf_ct_helper_log(skb
, ct
, "cannot parse SDP message");
1101 ret
= set_expected_rtp_rtcp(skb
, protoff
, dataoff
,
1103 &rtp_addr
, htons(port
), t
->class,
1104 mediaoff
, medialen
);
1105 if (ret
!= NF_ACCEPT
) {
1106 nf_ct_helper_log(skb
, ct
,
1107 "cannot add expectation for voice");
1111 /* Update media connection address if present */
1112 if (maddr_len
&& hooks
&& ct
->status
& IPS_NAT_MASK
) {
1113 ret
= hooks
->sdp_addr(skb
, protoff
, dataoff
,
1114 dptr
, datalen
, mediaoff
,
1118 if (ret
!= NF_ACCEPT
) {
1119 nf_ct_helper_log(skb
, ct
, "cannot mangle SDP");
1126 /* Update session connection and owner addresses */
1127 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1128 if (hooks
&& ct
->status
& IPS_NAT_MASK
)
1129 ret
= hooks
->sdp_session(skb
, protoff
, dataoff
,
1130 dptr
, datalen
, sdpoff
,
1135 static int process_invite_response(struct sk_buff
*skb
, unsigned int protoff
,
1136 unsigned int dataoff
,
1137 const char **dptr
, unsigned int *datalen
,
1138 unsigned int cseq
, unsigned int code
)
1140 enum ip_conntrack_info ctinfo
;
1141 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1142 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1144 if ((code
>= 100 && code
<= 199) ||
1145 (code
>= 200 && code
<= 299))
1146 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1147 else if (ct_sip_info
->invite_cseq
== cseq
)
1148 flush_expectations(ct
, true);
1152 static int process_update_response(struct sk_buff
*skb
, unsigned int protoff
,
1153 unsigned int dataoff
,
1154 const char **dptr
, unsigned int *datalen
,
1155 unsigned int cseq
, unsigned int code
)
1157 enum ip_conntrack_info ctinfo
;
1158 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1159 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1161 if ((code
>= 100 && code
<= 199) ||
1162 (code
>= 200 && code
<= 299))
1163 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1164 else if (ct_sip_info
->invite_cseq
== cseq
)
1165 flush_expectations(ct
, true);
1169 static int process_prack_response(struct sk_buff
*skb
, unsigned int protoff
,
1170 unsigned int dataoff
,
1171 const char **dptr
, unsigned int *datalen
,
1172 unsigned int cseq
, unsigned int code
)
1174 enum ip_conntrack_info ctinfo
;
1175 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1176 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1178 if ((code
>= 100 && code
<= 199) ||
1179 (code
>= 200 && code
<= 299))
1180 return process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1181 else if (ct_sip_info
->invite_cseq
== cseq
)
1182 flush_expectations(ct
, true);
1186 static int process_invite_request(struct sk_buff
*skb
, unsigned int protoff
,
1187 unsigned int dataoff
,
1188 const char **dptr
, unsigned int *datalen
,
1191 enum ip_conntrack_info ctinfo
;
1192 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1193 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1196 flush_expectations(ct
, true);
1197 ret
= process_sdp(skb
, protoff
, dataoff
, dptr
, datalen
, cseq
);
1198 if (ret
== NF_ACCEPT
)
1199 ct_sip_info
->invite_cseq
= cseq
;
1203 static int process_bye_request(struct sk_buff
*skb
, unsigned int protoff
,
1204 unsigned int dataoff
,
1205 const char **dptr
, unsigned int *datalen
,
1208 enum ip_conntrack_info ctinfo
;
1209 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1211 flush_expectations(ct
, true);
1215 /* Parse a REGISTER request and create a permanent expectation for incoming
1216 * signalling connections. The expectation is marked inactive and is activated
1217 * when receiving a response indicating success from the registrar.
1219 static int process_register_request(struct sk_buff
*skb
, unsigned int protoff
,
1220 unsigned int dataoff
,
1221 const char **dptr
, unsigned int *datalen
,
1224 enum ip_conntrack_info ctinfo
;
1225 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1226 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1227 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1228 unsigned int matchoff
, matchlen
;
1229 struct nf_conntrack_expect
*exp
;
1230 union nf_inet_addr
*saddr
, daddr
;
1231 const struct nf_nat_sip_hooks
*hooks
;
1232 struct nf_conntrack_helper
*helper
;
1235 unsigned int expires
= 0;
1238 /* Expected connections can not register again. */
1239 if (ct
->status
& IPS_EXPECTED
)
1242 /* We must check the expiration time: a value of zero signals the
1243 * registrar to release the binding. We'll remove our expectation
1244 * when receiving the new bindings in the response, but we don't
1245 * want to create new ones.
1247 * The expiration time may be contained in Expires: header, the
1248 * Contact: header parameters or the URI parameters.
1250 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1251 &matchoff
, &matchlen
) > 0)
1252 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1254 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1255 SIP_HDR_CONTACT
, NULL
,
1256 &matchoff
, &matchlen
, &daddr
, &port
);
1258 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1260 } else if (ret
== 0)
1263 /* We don't support third-party registrations */
1264 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1267 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
1271 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1272 matchoff
+ matchlen
, *datalen
,
1273 "expires=", NULL
, NULL
, &expires
) < 0) {
1274 nf_ct_helper_log(skb
, ct
, "cannot parse expires");
1283 exp
= nf_ct_expect_alloc(ct
);
1285 nf_ct_helper_log(skb
, ct
, "cannot alloc expectation");
1290 if (sip_direct_signalling
)
1291 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1293 helper
= rcu_dereference(nfct_help(ct
)->helper
);
1297 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1298 saddr
, &daddr
, proto
, NULL
, &port
);
1299 exp
->timeout
.expires
= sip_timeout
* HZ
;
1300 exp
->helper
= helper
;
1301 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1303 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1304 if (hooks
&& ct
->status
& IPS_NAT_MASK
)
1305 ret
= hooks
->expect(skb
, protoff
, dataoff
, dptr
, datalen
,
1306 exp
, matchoff
, matchlen
);
1308 if (nf_ct_expect_related(exp
, 0) != 0) {
1309 nf_ct_helper_log(skb
, ct
, "cannot add expectation");
1314 nf_ct_expect_put(exp
);
1317 if (ret
== NF_ACCEPT
)
1318 ct_sip_info
->register_cseq
= cseq
;
1322 static int process_register_response(struct sk_buff
*skb
, unsigned int protoff
,
1323 unsigned int dataoff
,
1324 const char **dptr
, unsigned int *datalen
,
1325 unsigned int cseq
, unsigned int code
)
1327 enum ip_conntrack_info ctinfo
;
1328 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1329 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1330 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1331 union nf_inet_addr addr
;
1334 unsigned int matchoff
, matchlen
, coff
= 0;
1335 unsigned int expires
= 0;
1336 int in_contact
= 0, ret
;
1338 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1339 * they have received a final response from the registrar for the
1340 * previous one or the previous REGISTER request has timed out".
1342 * However, some servers fail to detect retransmissions and send late
1343 * responses, so we store the sequence number of the last valid
1344 * request and compare it here.
1346 if (ct_sip_info
->register_cseq
!= cseq
)
1349 if (code
>= 100 && code
<= 199)
1351 if (code
< 200 || code
> 299)
1354 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1355 &matchoff
, &matchlen
) > 0)
1356 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1359 unsigned int c_expires
= expires
;
1361 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1362 SIP_HDR_CONTACT
, &in_contact
,
1363 &matchoff
, &matchlen
,
1366 nf_ct_helper_log(skb
, ct
, "cannot parse contact");
1368 } else if (ret
== 0)
1371 /* We don't support third-party registrations */
1372 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1375 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1376 *datalen
, &proto
) == 0)
1379 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1380 matchoff
+ matchlen
,
1381 *datalen
, "expires=",
1382 NULL
, NULL
, &c_expires
);
1384 nf_ct_helper_log(skb
, ct
, "cannot parse expires");
1389 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1395 flush_expectations(ct
, false);
1399 static const struct sip_handler sip_handlers
[] = {
1400 SIP_HANDLER("INVITE", process_invite_request
, process_invite_response
),
1401 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1402 SIP_HANDLER("ACK", process_sdp
, NULL
),
1403 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1404 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1405 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1408 static int process_sip_response(struct sk_buff
*skb
, unsigned int protoff
,
1409 unsigned int dataoff
,
1410 const char **dptr
, unsigned int *datalen
)
1412 enum ip_conntrack_info ctinfo
;
1413 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1414 unsigned int matchoff
, matchlen
, matchend
;
1415 unsigned int code
, cseq
, i
;
1417 if (*datalen
< strlen("SIP/2.0 200"))
1419 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1421 nf_ct_helper_log(skb
, ct
, "cannot get code");
1425 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1426 &matchoff
, &matchlen
) <= 0) {
1427 nf_ct_helper_log(skb
, ct
, "cannot parse cseq");
1430 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1431 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1432 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1435 matchend
= matchoff
+ matchlen
+ 1;
1437 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1438 const struct sip_handler
*handler
;
1440 handler
= &sip_handlers
[i
];
1441 if (handler
->response
== NULL
)
1443 if (*datalen
< matchend
+ handler
->len
||
1444 strncasecmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1446 return handler
->response(skb
, protoff
, dataoff
, dptr
, datalen
,
1452 static int process_sip_request(struct sk_buff
*skb
, unsigned int protoff
,
1453 unsigned int dataoff
,
1454 const char **dptr
, unsigned int *datalen
)
1456 enum ip_conntrack_info ctinfo
;
1457 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1458 struct nf_ct_sip_master
*ct_sip_info
= nfct_help_data(ct
);
1459 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1460 unsigned int matchoff
, matchlen
;
1461 unsigned int cseq
, i
;
1462 union nf_inet_addr addr
;
1465 /* Many Cisco IP phones use a high source port for SIP requests, but
1466 * listen for the response on port 5060. If we are the local
1467 * router for one of these phones, save the port number from the
1468 * Via: header so that nf_nat_sip can redirect the responses to
1471 if (ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1472 SIP_HDR_VIA_UDP
, NULL
, &matchoff
,
1473 &matchlen
, &addr
, &port
) > 0 &&
1474 port
!= ct
->tuplehash
[dir
].tuple
.src
.u
.udp
.port
&&
1475 nf_inet_addr_cmp(&addr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
1476 ct_sip_info
->forced_dport
= port
;
1478 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1479 const struct sip_handler
*handler
;
1481 handler
= &sip_handlers
[i
];
1482 if (handler
->request
== NULL
)
1484 if (*datalen
< handler
->len
+ 2 ||
1485 strncasecmp(*dptr
, handler
->method
, handler
->len
))
1487 if ((*dptr
)[handler
->len
] != ' ' ||
1488 !isalpha((*dptr
)[handler
->len
+1]))
1491 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1492 &matchoff
, &matchlen
) <= 0) {
1493 nf_ct_helper_log(skb
, ct
, "cannot parse cseq");
1496 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1497 if (!cseq
&& *(*dptr
+ matchoff
) != '0') {
1498 nf_ct_helper_log(skb
, ct
, "cannot get cseq");
1502 return handler
->request(skb
, protoff
, dataoff
, dptr
, datalen
,
1508 static int process_sip_msg(struct sk_buff
*skb
, struct nf_conn
*ct
,
1509 unsigned int protoff
, unsigned int dataoff
,
1510 const char **dptr
, unsigned int *datalen
)
1512 const struct nf_nat_sip_hooks
*hooks
;
1515 if (strncasecmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1516 ret
= process_sip_request(skb
, protoff
, dataoff
, dptr
, datalen
);
1518 ret
= process_sip_response(skb
, protoff
, dataoff
, dptr
, datalen
);
1520 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1521 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1522 if (hooks
&& !hooks
->msg(skb
, protoff
, dataoff
,
1524 nf_ct_helper_log(skb
, ct
, "cannot NAT SIP message");
1532 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1533 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1535 struct tcphdr
*th
, _tcph
;
1536 unsigned int dataoff
, datalen
;
1537 unsigned int matchoff
, matchlen
, clen
;
1538 unsigned int msglen
, origlen
;
1539 const char *dptr
, *end
;
1540 s16 diff
, tdiff
= 0;
1541 int ret
= NF_ACCEPT
;
1544 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1545 ctinfo
!= IP_CT_ESTABLISHED_REPLY
)
1549 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1552 dataoff
= protoff
+ th
->doff
* 4;
1553 if (dataoff
>= skb
->len
)
1556 nf_ct_refresh(ct
, sip_timeout
* HZ
);
1558 if (unlikely(skb_linearize(skb
)))
1561 dptr
= skb
->data
+ dataoff
;
1562 datalen
= skb
->len
- dataoff
;
1563 if (datalen
< strlen("SIP/2.0 200"))
1567 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1568 SIP_HDR_CONTENT_LENGTH
,
1569 &matchoff
, &matchlen
) <= 0)
1572 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1573 if (dptr
+ matchoff
== end
)
1577 for (; end
+ strlen("\r\n\r\n") <= dptr
+ datalen
; end
++) {
1578 if (end
[0] == '\r' && end
[1] == '\n' &&
1579 end
[2] == '\r' && end
[3] == '\n') {
1586 end
+= strlen("\r\n\r\n") + clen
;
1588 msglen
= origlen
= end
- dptr
;
1589 if (msglen
> datalen
)
1592 ret
= process_sip_msg(skb
, ct
, protoff
, dataoff
,
1594 /* process_sip_* functions report why this packet is dropped */
1595 if (ret
!= NF_ACCEPT
)
1597 diff
= msglen
- origlen
;
1602 datalen
= datalen
+ diff
- msglen
;
1605 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1606 const struct nf_nat_sip_hooks
*hooks
;
1608 hooks
= rcu_dereference(nf_nat_sip_hooks
);
1610 hooks
->seq_adjust(skb
, protoff
, tdiff
);
1616 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1617 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1619 unsigned int dataoff
, datalen
;
1623 dataoff
= protoff
+ sizeof(struct udphdr
);
1624 if (dataoff
>= skb
->len
)
1627 nf_ct_refresh(ct
, sip_timeout
* HZ
);
1629 if (unlikely(skb_linearize(skb
)))
1632 dptr
= skb
->data
+ dataoff
;
1633 datalen
= skb
->len
- dataoff
;
1634 if (datalen
< strlen("SIP/2.0 200"))
1637 return process_sip_msg(skb
, ct
, protoff
, dataoff
, &dptr
, &datalen
);
1640 static struct nf_conntrack_helper sip
[MAX_PORTS
* 4] __read_mostly
;
1642 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1643 [SIP_EXPECT_SIGNALLING
] = {
1644 .name
= "signalling",
1648 [SIP_EXPECT_AUDIO
] = {
1650 .max_expected
= 2 * IP_CT_DIR_MAX
,
1653 [SIP_EXPECT_VIDEO
] = {
1655 .max_expected
= 2 * IP_CT_DIR_MAX
,
1658 [SIP_EXPECT_IMAGE
] = {
1660 .max_expected
= IP_CT_DIR_MAX
,
1665 static void __exit
nf_conntrack_sip_fini(void)
1667 nf_conntrack_helpers_unregister(sip
, ports_c
* 4);
1670 static int __init
nf_conntrack_sip_init(void)
1674 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_sip_master
));
1677 ports
[ports_c
++] = SIP_PORT
;
1679 for (i
= 0; i
< ports_c
; i
++) {
1680 nf_ct_helper_init(&sip
[4 * i
], AF_INET
, IPPROTO_UDP
,
1681 HELPER_NAME
, SIP_PORT
, ports
[i
], i
,
1682 sip_exp_policy
, SIP_EXPECT_MAX
, sip_help_udp
,
1684 nf_ct_helper_init(&sip
[4 * i
+ 1], AF_INET
, IPPROTO_TCP
,
1685 HELPER_NAME
, SIP_PORT
, ports
[i
], i
,
1686 sip_exp_policy
, SIP_EXPECT_MAX
, sip_help_tcp
,
1688 nf_ct_helper_init(&sip
[4 * i
+ 2], AF_INET6
, IPPROTO_UDP
,
1689 HELPER_NAME
, SIP_PORT
, ports
[i
], i
,
1690 sip_exp_policy
, SIP_EXPECT_MAX
, sip_help_udp
,
1692 nf_ct_helper_init(&sip
[4 * i
+ 3], AF_INET6
, IPPROTO_TCP
,
1693 HELPER_NAME
, SIP_PORT
, ports
[i
], i
,
1694 sip_exp_policy
, SIP_EXPECT_MAX
, sip_help_tcp
,
1698 ret
= nf_conntrack_helpers_register(sip
, ports_c
* 4);
1700 pr_err("failed to register helpers\n");
1706 module_init(nf_conntrack_sip_init
);
1707 module_exit(nf_conntrack_sip_fini
);