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 #include <linux/module.h>
14 #include <linux/ctype.h>
15 #include <linux/skbuff.h>
16 #include <linux/inet.h>
18 #include <linux/udp.h>
19 #include <linux/tcp.h>
20 #include <linux/netfilter.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_expect.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_zones.h>
27 #include <linux/netfilter/nf_conntrack_sip.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
31 MODULE_DESCRIPTION("SIP connection tracking helper");
32 MODULE_ALIAS("ip_conntrack_sip");
33 MODULE_ALIAS_NFCT_HELPER("sip");
36 static unsigned short ports
[MAX_PORTS
];
37 static unsigned int ports_c
;
38 module_param_array(ports
, ushort
, &ports_c
, 0400);
39 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
41 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
42 module_param(sip_timeout
, uint
, 0600);
43 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
45 static int sip_direct_signalling __read_mostly
= 1;
46 module_param(sip_direct_signalling
, int, 0600);
47 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
50 static int sip_direct_media __read_mostly
= 1;
51 module_param(sip_direct_media
, int, 0600);
52 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
53 "endpoints only (default 1)");
55 unsigned int (*nf_nat_sip_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
57 unsigned int *datalen
) __read_mostly
;
58 EXPORT_SYMBOL_GPL(nf_nat_sip_hook
);
60 void (*nf_nat_sip_seq_adjust_hook
)(struct sk_buff
*skb
, s16 off
) __read_mostly
;
61 EXPORT_SYMBOL_GPL(nf_nat_sip_seq_adjust_hook
);
63 unsigned int (*nf_nat_sip_expect_hook
)(struct sk_buff
*skb
,
66 unsigned int *datalen
,
67 struct nf_conntrack_expect
*exp
,
68 unsigned int matchoff
,
69 unsigned int matchlen
) __read_mostly
;
70 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook
);
72 unsigned int (*nf_nat_sdp_addr_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
74 unsigned int *datalen
,
76 enum sdp_header_types type
,
77 enum sdp_header_types term
,
78 const union nf_inet_addr
*addr
)
80 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook
);
82 unsigned int (*nf_nat_sdp_port_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
84 unsigned int *datalen
,
85 unsigned int matchoff
,
86 unsigned int matchlen
,
87 u_int16_t port
) __read_mostly
;
88 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook
);
90 unsigned int (*nf_nat_sdp_session_hook
)(struct sk_buff
*skb
,
93 unsigned int *datalen
,
95 const union nf_inet_addr
*addr
)
97 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook
);
99 unsigned int (*nf_nat_sdp_media_hook
)(struct sk_buff
*skb
, unsigned int dataoff
,
101 unsigned int *datalen
,
102 struct nf_conntrack_expect
*rtp_exp
,
103 struct nf_conntrack_expect
*rtcp_exp
,
104 unsigned int mediaoff
,
105 unsigned int medialen
,
106 union nf_inet_addr
*rtp_addr
)
108 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook
);
110 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
111 const char *limit
, int *shift
)
115 while (dptr
< limit
&& isalpha(*dptr
)) {
122 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
123 const char *limit
, int *shift
)
126 while (dptr
< limit
&& isdigit(*dptr
)) {
133 static int iswordc(const char c
)
135 if (isalnum(c
) || c
== '!' || c
== '"' || c
== '%' ||
136 (c
>= '(' && c
<= '/') || c
== ':' || c
== '<' || c
== '>' ||
137 c
== '?' || (c
>= '[' && c
<= ']') || c
== '_' || c
== '`' ||
138 c
== '{' || c
== '}' || c
== '~')
143 static int word_len(const char *dptr
, const char *limit
)
146 while (dptr
< limit
&& iswordc(*dptr
)) {
153 static int callid_len(const struct nf_conn
*ct
, const char *dptr
,
154 const char *limit
, int *shift
)
158 len
= word_len(dptr
, limit
);
160 if (!len
|| dptr
== limit
|| *dptr
!= '@')
165 domain_len
= word_len(dptr
, limit
);
168 return len
+ domain_len
;
171 /* get media type + port length */
172 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
173 const char *limit
, int *shift
)
175 int len
= string_len(ct
, dptr
, limit
, shift
);
178 if (dptr
>= limit
|| *dptr
!= ' ')
183 return len
+ digits_len(ct
, dptr
, limit
, shift
);
186 static int parse_addr(const struct nf_conn
*ct
, const char *cp
,
187 const char **endp
, union nf_inet_addr
*addr
,
196 memset(addr
, 0, sizeof(*addr
));
197 switch (nf_ct_l3num(ct
)) {
199 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
202 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
208 if (ret
== 0 || end
== cp
)
215 /* skip ip address. returns its length. */
216 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
217 const char *limit
, int *shift
)
219 union nf_inet_addr addr
;
220 const char *aux
= dptr
;
222 if (!parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
223 pr_debug("ip: %s parse failed.!\n", dptr
);
230 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
235 /* get address length, skiping user info. */
236 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
237 const char *limit
, int *shift
)
239 const char *start
= dptr
;
242 /* Search for @, but stop at the end of the line.
243 * We are inside a sip: URI, so we don't need to worry about
244 * continuation lines. */
245 while (dptr
< limit
&&
246 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
251 if (dptr
< limit
&& *dptr
== '@') {
259 return epaddr_len(ct
, dptr
, limit
, shift
);
262 /* Parse a SIP request line of the form:
264 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
266 * and return the offset and length of the address contained in the Request-URI.
268 int ct_sip_parse_request(const struct nf_conn
*ct
,
269 const char *dptr
, unsigned int datalen
,
270 unsigned int *matchoff
, unsigned int *matchlen
,
271 union nf_inet_addr
*addr
, __be16
*port
)
273 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
278 /* Skip method and following whitespace */
279 mlen
= string_len(ct
, dptr
, limit
, NULL
);
287 for (; dptr
< limit
- strlen("sip:"); dptr
++) {
288 if (*dptr
== '\r' || *dptr
== '\n')
290 if (strnicmp(dptr
, "sip:", strlen("sip:")) == 0) {
291 dptr
+= strlen("sip:");
295 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
299 if (!parse_addr(ct
, dptr
, &end
, addr
, limit
))
301 if (end
< limit
&& *end
== ':') {
303 p
= simple_strtoul(end
, (char **)&end
, 10);
304 if (p
< 1024 || p
> 65535)
308 *port
= htons(SIP_PORT
);
312 *matchoff
= dptr
- start
;
313 *matchlen
= end
- dptr
;
316 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
318 /* SIP header parsing: SIP headers are located at the beginning of a line, but
319 * may span several lines, in which case the continuation lines begin with a
320 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
321 * CRLF, RFC 3261 allows only CRLF, we support both.
323 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
324 * whitespace and the values. Whitespace in this context means any amount of
325 * tabs, spaces and continuation lines, which are treated as a single whitespace
328 * Some headers may appear multiple times. A comma separated list of values is
329 * equivalent to multiple headers.
331 static const struct sip_header ct_sip_hdrs
[] = {
332 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
333 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
334 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
335 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
336 [SIP_HDR_VIA_UDP
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
337 [SIP_HDR_VIA_TCP
] = SIP_HDR("Via", "v", "TCP ", epaddr_len
),
338 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
339 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
340 [SIP_HDR_CALL_ID
] = SIP_HDR("Call-Id", "i", NULL
, callid_len
),
343 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
345 /* Walk past newline */
349 /* Skip '\n' in CR LF */
350 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
355 /* Continuation line? */
356 if (*dptr
!= ' ' && *dptr
!= '\t')
359 /* skip leading whitespace */
360 for (; dptr
< limit
; dptr
++) {
361 if (*dptr
!= ' ' && *dptr
!= '\t')
367 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
369 for (; dptr
< limit
; dptr
++) {
372 if (*dptr
!= '\r' && *dptr
!= '\n')
374 dptr
= sip_follow_continuation(dptr
, limit
);
381 /* Search within a SIP header value, dealing with continuation lines */
382 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
383 const char *needle
, unsigned int len
)
385 for (limit
-= len
; dptr
< limit
; dptr
++) {
386 if (*dptr
== '\r' || *dptr
== '\n') {
387 dptr
= sip_follow_continuation(dptr
, limit
);
393 if (strnicmp(dptr
, needle
, len
) == 0)
399 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
400 unsigned int dataoff
, unsigned int datalen
,
401 enum sip_header_types type
,
402 unsigned int *matchoff
, unsigned int *matchlen
)
404 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
405 const char *start
= dptr
, *limit
= dptr
+ datalen
;
408 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
409 /* Find beginning of line */
410 if (*dptr
!= '\r' && *dptr
!= '\n')
414 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
419 /* Skip continuation lines */
420 if (*dptr
== ' ' || *dptr
== '\t')
423 /* Find header. Compact headers must be followed by a
424 * non-alphabetic character to avoid mismatches. */
425 if (limit
- dptr
>= hdr
->len
&&
426 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
428 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
429 strnicmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
430 !isalpha(*(dptr
+ hdr
->clen
)))
435 /* Find and skip colon */
436 dptr
= sip_skip_whitespace(dptr
, limit
);
439 if (*dptr
!= ':' || ++dptr
>= limit
)
442 /* Skip whitespace after colon */
443 dptr
= sip_skip_whitespace(dptr
, limit
);
447 *matchoff
= dptr
- start
;
449 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
456 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
459 *matchoff
= dptr
- start
+ shift
;
464 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
466 /* Get next header field in a list of comma separated values */
467 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
468 unsigned int dataoff
, unsigned int datalen
,
469 enum sip_header_types type
,
470 unsigned int *matchoff
, unsigned int *matchlen
)
472 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
473 const char *start
= dptr
, *limit
= dptr
+ datalen
;
478 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
482 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
487 *matchoff
= dptr
- start
;
488 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
495 /* Walk through headers until a parsable one is found or no header of the
496 * given type is left. */
497 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
498 unsigned int dataoff
, unsigned int datalen
,
499 enum sip_header_types type
, int *in_header
,
500 unsigned int *matchoff
, unsigned int *matchlen
)
504 if (in_header
&& *in_header
) {
506 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
507 type
, matchoff
, matchlen
);
512 dataoff
+= *matchoff
;
518 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
519 type
, matchoff
, matchlen
);
524 dataoff
+= *matchoff
;
532 /* Locate a SIP header, parse the URI and return the offset and length of
533 * the address as well as the address and port themselves. A stream of
534 * headers can be parsed by handing in a non-NULL datalen and in_header
537 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
538 unsigned int *dataoff
, unsigned int datalen
,
539 enum sip_header_types type
, int *in_header
,
540 unsigned int *matchoff
, unsigned int *matchlen
,
541 union nf_inet_addr
*addr
, __be16
*port
)
543 const char *c
, *limit
= dptr
+ datalen
;
547 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
548 type
, in_header
, matchoff
, matchlen
);
553 if (!parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
))
557 p
= simple_strtoul(c
, (char **)&c
, 10);
558 if (p
< 1024 || p
> 65535)
562 *port
= htons(SIP_PORT
);
568 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
570 static int ct_sip_parse_param(const struct nf_conn
*ct
, const char *dptr
,
571 unsigned int dataoff
, unsigned int datalen
,
573 unsigned int *matchoff
, unsigned int *matchlen
)
575 const char *limit
= dptr
+ datalen
;
579 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
581 limit
= dptr
+ datalen
;
583 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
586 start
+= strlen(name
);
588 end
= ct_sip_header_search(start
, limit
, ";", strlen(";"));
592 *matchoff
= start
- dptr
;
593 *matchlen
= end
- start
;
597 /* Parse address from header parameter and return address, offset and length */
598 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
599 unsigned int dataoff
, unsigned int datalen
,
601 unsigned int *matchoff
, unsigned int *matchlen
,
602 union nf_inet_addr
*addr
)
604 const char *limit
= dptr
+ datalen
;
605 const char *start
, *end
;
607 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
609 limit
= dptr
+ datalen
;
611 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
615 start
+= strlen(name
);
616 if (!parse_addr(ct
, start
, &end
, addr
, limit
))
618 *matchoff
= start
- dptr
;
619 *matchlen
= end
- start
;
622 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
624 /* Parse numerical header parameter and return value, offset and length */
625 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
626 unsigned int dataoff
, unsigned int datalen
,
628 unsigned int *matchoff
, unsigned int *matchlen
,
631 const char *limit
= dptr
+ datalen
;
635 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
637 limit
= dptr
+ datalen
;
639 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
643 start
+= strlen(name
);
644 *val
= simple_strtoul(start
, &end
, 0);
647 if (matchoff
&& matchlen
) {
648 *matchoff
= start
- dptr
;
649 *matchlen
= end
- start
;
653 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
655 static int ct_sip_parse_transport(struct nf_conn
*ct
, const char *dptr
,
656 unsigned int dataoff
, unsigned int datalen
,
659 unsigned int matchoff
, matchlen
;
661 if (ct_sip_parse_param(ct
, dptr
, dataoff
, datalen
, "transport=",
662 &matchoff
, &matchlen
)) {
663 if (!strnicmp(dptr
+ matchoff
, "TCP", strlen("TCP")))
664 *proto
= IPPROTO_TCP
;
665 else if (!strnicmp(dptr
+ matchoff
, "UDP", strlen("UDP")))
666 *proto
= IPPROTO_UDP
;
670 if (*proto
!= nf_ct_protonum(ct
))
673 *proto
= nf_ct_protonum(ct
);
678 /* SDP header parsing: a SDP session description contains an ordered set of
679 * headers, starting with a section containing general session parameters,
680 * optionally followed by multiple media descriptions.
682 * SDP headers always start at the beginning of a line. According to RFC 2327:
683 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
684 * be tolerant and also accept records terminated with a single newline
685 * character". We handle both cases.
687 static const struct sip_header ct_sdp_hdrs
[] = {
688 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
689 [SDP_HDR_OWNER_IP4
] = SDP_HDR("o=", "IN IP4 ", epaddr_len
),
690 [SDP_HDR_CONNECTION_IP4
] = SDP_HDR("c=", "IN IP4 ", epaddr_len
),
691 [SDP_HDR_OWNER_IP6
] = SDP_HDR("o=", "IN IP6 ", epaddr_len
),
692 [SDP_HDR_CONNECTION_IP6
] = SDP_HDR("c=", "IN IP6 ", epaddr_len
),
693 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
696 /* Linear string search within SDP header values */
697 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
698 const char *needle
, unsigned int len
)
700 for (limit
-= len
; dptr
< limit
; dptr
++) {
701 if (*dptr
== '\r' || *dptr
== '\n')
703 if (strncmp(dptr
, needle
, len
) == 0)
709 /* Locate a SDP header (optionally a substring within the header value),
710 * optionally stopping at the first occurence of the term header, parse
711 * it and return the offset and length of the data we're interested in.
713 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
714 unsigned int dataoff
, unsigned int datalen
,
715 enum sdp_header_types type
,
716 enum sdp_header_types term
,
717 unsigned int *matchoff
, unsigned int *matchlen
)
719 const struct sip_header
*hdr
= &ct_sdp_hdrs
[type
];
720 const struct sip_header
*thdr
= &ct_sdp_hdrs
[term
];
721 const char *start
= dptr
, *limit
= dptr
+ datalen
;
724 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
725 /* Find beginning of line */
726 if (*dptr
!= '\r' && *dptr
!= '\n')
730 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
735 if (term
!= SDP_HDR_UNSPEC
&&
736 limit
- dptr
>= thdr
->len
&&
737 strnicmp(dptr
, thdr
->name
, thdr
->len
) == 0)
739 else if (limit
- dptr
>= hdr
->len
&&
740 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
745 *matchoff
= dptr
- start
;
747 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
754 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
757 *matchoff
= dptr
- start
+ shift
;
762 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
764 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
765 unsigned int dataoff
, unsigned int datalen
,
766 enum sdp_header_types type
,
767 enum sdp_header_types term
,
768 unsigned int *matchoff
, unsigned int *matchlen
,
769 union nf_inet_addr
*addr
)
773 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
778 if (!parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
779 dptr
+ *matchoff
+ *matchlen
))
784 static int refresh_signalling_expectation(struct nf_conn
*ct
,
785 union nf_inet_addr
*addr
,
786 u8 proto
, __be16 port
,
787 unsigned int expires
)
789 struct nf_conn_help
*help
= nfct_help(ct
);
790 struct nf_conntrack_expect
*exp
;
791 struct hlist_node
*n
, *next
;
794 spin_lock_bh(&nf_conntrack_lock
);
795 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
796 if (exp
->class != SIP_EXPECT_SIGNALLING
||
797 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
798 exp
->tuple
.dst
.protonum
!= proto
||
799 exp
->tuple
.dst
.u
.udp
.port
!= port
)
801 if (!del_timer(&exp
->timeout
))
803 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
804 exp
->timeout
.expires
= jiffies
+ expires
* HZ
;
805 add_timer(&exp
->timeout
);
809 spin_unlock_bh(&nf_conntrack_lock
);
813 static void flush_expectations(struct nf_conn
*ct
, bool media
)
815 struct nf_conn_help
*help
= nfct_help(ct
);
816 struct nf_conntrack_expect
*exp
;
817 struct hlist_node
*n
, *next
;
819 spin_lock_bh(&nf_conntrack_lock
);
820 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
821 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
823 if (!del_timer(&exp
->timeout
))
825 nf_ct_unlink_expect(exp
);
826 nf_ct_expect_put(exp
);
830 spin_unlock_bh(&nf_conntrack_lock
);
833 static int set_expected_rtp_rtcp(struct sk_buff
*skb
, unsigned int dataoff
,
834 const char **dptr
, unsigned int *datalen
,
835 union nf_inet_addr
*daddr
, __be16 port
,
836 enum sip_expectation_classes
class,
837 unsigned int mediaoff
, unsigned int medialen
)
839 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
840 enum ip_conntrack_info ctinfo
;
841 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
842 struct net
*net
= nf_ct_net(ct
);
843 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
844 union nf_inet_addr
*saddr
;
845 struct nf_conntrack_tuple tuple
;
846 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
848 __be16 rtp_port
, rtcp_port
;
849 typeof(nf_nat_sdp_port_hook
) nf_nat_sdp_port
;
850 typeof(nf_nat_sdp_media_hook
) nf_nat_sdp_media
;
853 if (sip_direct_media
) {
854 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
856 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
859 /* We need to check whether the registration exists before attempting
860 * to register it since we can see the same media description multiple
861 * times on different connections in case multiple endpoints receive
864 * RTP optimization: if we find a matching media channel expectation
865 * and both the expectation and this connection are SNATed, we assume
866 * both sides can reach each other directly and use the final
867 * destination address from the expectation. We still need to keep
868 * the NATed expectations for media that might arrive from the
869 * outside, and additionally need to expect the direct RTP stream
870 * in case it passes through us even without NAT.
872 memset(&tuple
, 0, sizeof(tuple
));
874 tuple
.src
.u3
= *saddr
;
875 tuple
.src
.l3num
= nf_ct_l3num(ct
);
876 tuple
.dst
.protonum
= IPPROTO_UDP
;
877 tuple
.dst
.u3
= *daddr
;
878 tuple
.dst
.u
.udp
.port
= port
;
882 exp
= __nf_ct_expect_find(net
, nf_ct_zone(ct
), &tuple
);
884 if (!exp
|| exp
->master
== ct
||
885 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
888 #ifdef CONFIG_NF_NAT_NEEDED
889 if (exp
->tuple
.src
.l3num
== AF_INET
&& !direct_rtp
&&
890 (exp
->saved_ip
!= exp
->tuple
.dst
.u3
.ip
||
891 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
892 ct
->status
& IPS_NAT_MASK
) {
893 daddr
->ip
= exp
->saved_ip
;
894 tuple
.dst
.u3
.ip
= exp
->saved_ip
;
895 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
900 } while (!skip_expect
);
903 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
904 rtp_port
= htons(base_port
);
905 rtcp_port
= htons(base_port
+ 1);
908 nf_nat_sdp_port
= rcu_dereference(nf_nat_sdp_port_hook
);
909 if (nf_nat_sdp_port
&&
910 !nf_nat_sdp_port(skb
, dataoff
, dptr
, datalen
,
911 mediaoff
, medialen
, ntohs(rtp_port
)))
918 rtp_exp
= nf_ct_expect_alloc(ct
);
921 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
922 IPPROTO_UDP
, NULL
, &rtp_port
);
924 rtcp_exp
= nf_ct_expect_alloc(ct
);
925 if (rtcp_exp
== NULL
)
927 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
928 IPPROTO_UDP
, NULL
, &rtcp_port
);
930 nf_nat_sdp_media
= rcu_dereference(nf_nat_sdp_media_hook
);
931 if (nf_nat_sdp_media
&& ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
932 ret
= nf_nat_sdp_media(skb
, dataoff
, dptr
, datalen
,
934 mediaoff
, medialen
, daddr
);
936 if (nf_ct_expect_related(rtp_exp
) == 0) {
937 if (nf_ct_expect_related(rtcp_exp
) != 0)
938 nf_ct_unexpect_related(rtp_exp
);
943 nf_ct_expect_put(rtcp_exp
);
945 nf_ct_expect_put(rtp_exp
);
950 static const struct sdp_media_type sdp_media_types
[] = {
951 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
952 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
953 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE
),
956 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
957 unsigned int matchoff
,
958 unsigned int matchlen
)
960 const struct sdp_media_type
*t
;
963 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
964 t
= &sdp_media_types
[i
];
965 if (matchlen
< t
->len
||
966 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
973 static int process_sdp(struct sk_buff
*skb
, unsigned int dataoff
,
974 const char **dptr
, unsigned int *datalen
,
977 enum ip_conntrack_info ctinfo
;
978 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
979 unsigned int matchoff
, matchlen
;
980 unsigned int mediaoff
, medialen
;
982 unsigned int caddr_len
, maddr_len
;
984 union nf_inet_addr caddr
, maddr
, rtp_addr
;
986 enum sdp_header_types c_hdr
;
987 const struct sdp_media_type
*t
;
989 typeof(nf_nat_sdp_addr_hook
) nf_nat_sdp_addr
;
990 typeof(nf_nat_sdp_session_hook
) nf_nat_sdp_session
;
992 nf_nat_sdp_addr
= rcu_dereference(nf_nat_sdp_addr_hook
);
993 c_hdr
= nf_ct_l3num(ct
) == AF_INET
? SDP_HDR_CONNECTION_IP4
:
994 SDP_HDR_CONNECTION_IP6
;
996 /* Find beginning of session description */
997 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
998 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
999 &matchoff
, &matchlen
) <= 0)
1003 /* The connection information is contained in the session description
1004 * and/or once per media description. The first media description marks
1005 * the end of the session description. */
1007 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
1008 c_hdr
, SDP_HDR_MEDIA
,
1009 &matchoff
, &matchlen
, &caddr
) > 0)
1010 caddr_len
= matchlen
;
1013 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
1014 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
1015 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
1016 &mediaoff
, &medialen
) <= 0)
1019 /* Get media type and port number. A media port value of zero
1020 * indicates an inactive stream. */
1021 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
1023 mediaoff
+= medialen
;
1029 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
1032 if (port
< 1024 || port
> 65535)
1035 /* The media description overrides the session description. */
1037 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
1038 c_hdr
, SDP_HDR_MEDIA
,
1039 &matchoff
, &matchlen
, &maddr
) > 0) {
1040 maddr_len
= matchlen
;
1041 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
1042 } else if (caddr_len
)
1043 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
1047 ret
= set_expected_rtp_rtcp(skb
, dataoff
, dptr
, datalen
,
1048 &rtp_addr
, htons(port
), t
->class,
1049 mediaoff
, medialen
);
1050 if (ret
!= NF_ACCEPT
)
1053 /* Update media connection address if present */
1054 if (maddr_len
&& nf_nat_sdp_addr
&& ct
->status
& IPS_NAT_MASK
) {
1055 ret
= nf_nat_sdp_addr(skb
, dataoff
, dptr
, datalen
,
1056 mediaoff
, c_hdr
, SDP_HDR_MEDIA
,
1058 if (ret
!= NF_ACCEPT
)
1064 /* Update session connection and owner addresses */
1065 nf_nat_sdp_session
= rcu_dereference(nf_nat_sdp_session_hook
);
1066 if (nf_nat_sdp_session
&& ct
->status
& IPS_NAT_MASK
)
1067 ret
= nf_nat_sdp_session(skb
, dataoff
, dptr
, datalen
, sdpoff
,
1072 static int process_invite_response(struct sk_buff
*skb
, unsigned int dataoff
,
1073 const char **dptr
, unsigned int *datalen
,
1074 unsigned int cseq
, unsigned int code
)
1076 enum ip_conntrack_info ctinfo
;
1077 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1078 struct nf_conn_help
*help
= nfct_help(ct
);
1080 if ((code
>= 100 && code
<= 199) ||
1081 (code
>= 200 && code
<= 299))
1082 return process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1083 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
1084 flush_expectations(ct
, true);
1088 static int process_update_response(struct sk_buff
*skb
, unsigned int dataoff
,
1089 const char **dptr
, unsigned int *datalen
,
1090 unsigned int cseq
, unsigned int code
)
1092 enum ip_conntrack_info ctinfo
;
1093 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1094 struct nf_conn_help
*help
= nfct_help(ct
);
1096 if ((code
>= 100 && code
<= 199) ||
1097 (code
>= 200 && code
<= 299))
1098 return process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1099 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
1100 flush_expectations(ct
, true);
1104 static int process_prack_response(struct sk_buff
*skb
, unsigned int dataoff
,
1105 const char **dptr
, unsigned int *datalen
,
1106 unsigned int cseq
, unsigned int code
)
1108 enum ip_conntrack_info ctinfo
;
1109 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1110 struct nf_conn_help
*help
= nfct_help(ct
);
1112 if ((code
>= 100 && code
<= 199) ||
1113 (code
>= 200 && code
<= 299))
1114 return process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1115 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
1116 flush_expectations(ct
, true);
1120 static int process_invite_request(struct sk_buff
*skb
, unsigned int dataoff
,
1121 const char **dptr
, unsigned int *datalen
,
1124 enum ip_conntrack_info ctinfo
;
1125 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1126 struct nf_conn_help
*help
= nfct_help(ct
);
1129 flush_expectations(ct
, true);
1130 ret
= process_sdp(skb
, dataoff
, dptr
, datalen
, cseq
);
1131 if (ret
== NF_ACCEPT
)
1132 help
->help
.ct_sip_info
.invite_cseq
= cseq
;
1136 static int process_bye_request(struct sk_buff
*skb
, unsigned int dataoff
,
1137 const char **dptr
, unsigned int *datalen
,
1140 enum ip_conntrack_info ctinfo
;
1141 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1143 flush_expectations(ct
, true);
1147 /* Parse a REGISTER request and create a permanent expectation for incoming
1148 * signalling connections. The expectation is marked inactive and is activated
1149 * when receiving a response indicating success from the registrar.
1151 static int process_register_request(struct sk_buff
*skb
, unsigned int dataoff
,
1152 const char **dptr
, unsigned int *datalen
,
1155 enum ip_conntrack_info ctinfo
;
1156 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1157 struct nf_conn_help
*help
= nfct_help(ct
);
1158 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1159 unsigned int matchoff
, matchlen
;
1160 struct nf_conntrack_expect
*exp
;
1161 union nf_inet_addr
*saddr
, daddr
;
1164 unsigned int expires
= 0;
1166 typeof(nf_nat_sip_expect_hook
) nf_nat_sip_expect
;
1168 /* Expected connections can not register again. */
1169 if (ct
->status
& IPS_EXPECTED
)
1172 /* We must check the expiration time: a value of zero signals the
1173 * registrar to release the binding. We'll remove our expectation
1174 * when receiving the new bindings in the response, but we don't
1175 * want to create new ones.
1177 * The expiration time may be contained in Expires: header, the
1178 * Contact: header parameters or the URI parameters.
1180 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1181 &matchoff
, &matchlen
) > 0)
1182 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1184 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1185 SIP_HDR_CONTACT
, NULL
,
1186 &matchoff
, &matchlen
, &daddr
, &port
);
1192 /* We don't support third-party registrations */
1193 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1196 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
, *datalen
,
1200 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1201 matchoff
+ matchlen
, *datalen
,
1202 "expires=", NULL
, NULL
, &expires
) < 0)
1210 exp
= nf_ct_expect_alloc(ct
);
1215 if (sip_direct_signalling
)
1216 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1218 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1219 saddr
, &daddr
, proto
, NULL
, &port
);
1220 exp
->timeout
.expires
= sip_timeout
* HZ
;
1221 exp
->helper
= nfct_help(ct
)->helper
;
1222 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1224 nf_nat_sip_expect
= rcu_dereference(nf_nat_sip_expect_hook
);
1225 if (nf_nat_sip_expect
&& ct
->status
& IPS_NAT_MASK
)
1226 ret
= nf_nat_sip_expect(skb
, dataoff
, dptr
, datalen
, exp
,
1227 matchoff
, matchlen
);
1229 if (nf_ct_expect_related(exp
) != 0)
1234 nf_ct_expect_put(exp
);
1237 if (ret
== NF_ACCEPT
)
1238 help
->help
.ct_sip_info
.register_cseq
= cseq
;
1242 static int process_register_response(struct sk_buff
*skb
, unsigned int dataoff
,
1243 const char **dptr
, unsigned int *datalen
,
1244 unsigned int cseq
, unsigned int code
)
1246 enum ip_conntrack_info ctinfo
;
1247 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1248 struct nf_conn_help
*help
= nfct_help(ct
);
1249 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1250 union nf_inet_addr addr
;
1253 unsigned int matchoff
, matchlen
, coff
= 0;
1254 unsigned int expires
= 0;
1255 int in_contact
= 0, ret
;
1257 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1258 * they have received a final response from the registrar for the
1259 * previous one or the previous REGISTER request has timed out".
1261 * However, some servers fail to detect retransmissions and send late
1262 * responses, so we store the sequence number of the last valid
1263 * request and compare it here.
1265 if (help
->help
.ct_sip_info
.register_cseq
!= cseq
)
1268 if (code
>= 100 && code
<= 199)
1270 if (code
< 200 || code
> 299)
1273 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1274 &matchoff
, &matchlen
) > 0)
1275 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1278 unsigned int c_expires
= expires
;
1280 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &coff
, *datalen
,
1281 SIP_HDR_CONTACT
, &in_contact
,
1282 &matchoff
, &matchlen
,
1289 /* We don't support third-party registrations */
1290 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1293 if (ct_sip_parse_transport(ct
, *dptr
, matchoff
+ matchlen
,
1294 *datalen
, &proto
) == 0)
1297 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1298 matchoff
+ matchlen
,
1299 *datalen
, "expires=",
1300 NULL
, NULL
, &c_expires
);
1305 if (refresh_signalling_expectation(ct
, &addr
, proto
, port
,
1311 flush_expectations(ct
, false);
1315 static const struct sip_handler sip_handlers
[] = {
1316 SIP_HANDLER("INVITE", process_invite_request
, process_invite_response
),
1317 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1318 SIP_HANDLER("ACK", process_sdp
, NULL
),
1319 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1320 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1321 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1324 static int process_sip_response(struct sk_buff
*skb
, unsigned int dataoff
,
1325 const char **dptr
, unsigned int *datalen
)
1327 enum ip_conntrack_info ctinfo
;
1328 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1329 unsigned int matchoff
, matchlen
, matchend
;
1330 unsigned int code
, cseq
, i
;
1332 if (*datalen
< strlen("SIP/2.0 200"))
1334 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1338 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1339 &matchoff
, &matchlen
) <= 0)
1341 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1344 matchend
= matchoff
+ matchlen
+ 1;
1346 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1347 const struct sip_handler
*handler
;
1349 handler
= &sip_handlers
[i
];
1350 if (handler
->response
== NULL
)
1352 if (*datalen
< matchend
+ handler
->len
||
1353 strnicmp(*dptr
+ matchend
, handler
->method
, handler
->len
))
1355 return handler
->response(skb
, dataoff
, dptr
, datalen
,
1361 static int process_sip_request(struct sk_buff
*skb
, unsigned int dataoff
,
1362 const char **dptr
, unsigned int *datalen
)
1364 enum ip_conntrack_info ctinfo
;
1365 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1366 unsigned int matchoff
, matchlen
;
1367 unsigned int cseq
, i
;
1369 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1370 const struct sip_handler
*handler
;
1372 handler
= &sip_handlers
[i
];
1373 if (handler
->request
== NULL
)
1375 if (*datalen
< handler
->len
||
1376 strnicmp(*dptr
, handler
->method
, handler
->len
))
1379 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1380 &matchoff
, &matchlen
) <= 0)
1382 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1386 return handler
->request(skb
, dataoff
, dptr
, datalen
, cseq
);
1391 static int process_sip_msg(struct sk_buff
*skb
, struct nf_conn
*ct
,
1392 unsigned int dataoff
, const char **dptr
,
1393 unsigned int *datalen
)
1395 typeof(nf_nat_sip_hook
) nf_nat_sip
;
1398 if (strnicmp(*dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1399 ret
= process_sip_request(skb
, dataoff
, dptr
, datalen
);
1401 ret
= process_sip_response(skb
, dataoff
, dptr
, datalen
);
1403 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1404 nf_nat_sip
= rcu_dereference(nf_nat_sip_hook
);
1405 if (nf_nat_sip
&& !nf_nat_sip(skb
, dataoff
, dptr
, datalen
))
1412 static int sip_help_tcp(struct sk_buff
*skb
, unsigned int protoff
,
1413 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1415 struct tcphdr
*th
, _tcph
;
1416 unsigned int dataoff
, datalen
;
1417 unsigned int matchoff
, matchlen
, clen
;
1418 unsigned int msglen
, origlen
;
1419 const char *dptr
, *end
;
1420 s16 diff
, tdiff
= 0;
1421 int ret
= NF_ACCEPT
;
1422 typeof(nf_nat_sip_seq_adjust_hook
) nf_nat_sip_seq_adjust
;
1424 if (ctinfo
!= IP_CT_ESTABLISHED
&&
1425 ctinfo
!= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
)
1429 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
1432 dataoff
= protoff
+ th
->doff
* 4;
1433 if (dataoff
>= skb
->len
)
1436 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1438 if (unlikely(skb_linearize(skb
)))
1441 dptr
= skb
->data
+ dataoff
;
1442 datalen
= skb
->len
- dataoff
;
1443 if (datalen
< strlen("SIP/2.0 200"))
1447 if (ct_sip_get_header(ct
, dptr
, 0, datalen
,
1448 SIP_HDR_CONTENT_LENGTH
,
1449 &matchoff
, &matchlen
) <= 0)
1452 clen
= simple_strtoul(dptr
+ matchoff
, (char **)&end
, 10);
1453 if (dptr
+ matchoff
== end
)
1456 if (end
+ strlen("\r\n\r\n") > dptr
+ datalen
)
1458 if (end
[0] != '\r' || end
[1] != '\n' ||
1459 end
[2] != '\r' || end
[3] != '\n')
1461 end
+= strlen("\r\n\r\n") + clen
;
1463 msglen
= origlen
= end
- dptr
;
1465 ret
= process_sip_msg(skb
, ct
, dataoff
, &dptr
, &msglen
);
1466 if (ret
!= NF_ACCEPT
)
1468 diff
= msglen
- origlen
;
1473 datalen
= datalen
+ diff
- msglen
;
1476 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1477 nf_nat_sip_seq_adjust
= rcu_dereference(nf_nat_sip_seq_adjust_hook
);
1478 if (nf_nat_sip_seq_adjust
)
1479 nf_nat_sip_seq_adjust(skb
, tdiff
);
1485 static int sip_help_udp(struct sk_buff
*skb
, unsigned int protoff
,
1486 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
1488 unsigned int dataoff
, datalen
;
1492 dataoff
= protoff
+ sizeof(struct udphdr
);
1493 if (dataoff
>= skb
->len
)
1496 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1498 if (unlikely(skb_linearize(skb
)))
1501 dptr
= skb
->data
+ dataoff
;
1502 datalen
= skb
->len
- dataoff
;
1503 if (datalen
< strlen("SIP/2.0 200"))
1506 return process_sip_msg(skb
, ct
, dataoff
, &dptr
, &datalen
);
1509 static struct nf_conntrack_helper sip
[MAX_PORTS
][4] __read_mostly
;
1510 static char sip_names
[MAX_PORTS
][4][sizeof("sip-65535")] __read_mostly
;
1512 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1513 [SIP_EXPECT_SIGNALLING
] = {
1514 .name
= "signalling",
1518 [SIP_EXPECT_AUDIO
] = {
1520 .max_expected
= 2 * IP_CT_DIR_MAX
,
1523 [SIP_EXPECT_VIDEO
] = {
1525 .max_expected
= 2 * IP_CT_DIR_MAX
,
1528 [SIP_EXPECT_IMAGE
] = {
1530 .max_expected
= IP_CT_DIR_MAX
,
1535 static void nf_conntrack_sip_fini(void)
1539 for (i
= 0; i
< ports_c
; i
++) {
1540 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
1541 if (sip
[i
][j
].me
== NULL
)
1543 nf_conntrack_helper_unregister(&sip
[i
][j
]);
1548 static int __init
nf_conntrack_sip_init(void)
1554 ports
[ports_c
++] = SIP_PORT
;
1556 for (i
= 0; i
< ports_c
; i
++) {
1557 memset(&sip
[i
], 0, sizeof(sip
[i
]));
1559 sip
[i
][0].tuple
.src
.l3num
= AF_INET
;
1560 sip
[i
][0].tuple
.dst
.protonum
= IPPROTO_UDP
;
1561 sip
[i
][0].help
= sip_help_udp
;
1562 sip
[i
][1].tuple
.src
.l3num
= AF_INET
;
1563 sip
[i
][1].tuple
.dst
.protonum
= IPPROTO_TCP
;
1564 sip
[i
][1].help
= sip_help_tcp
;
1566 sip
[i
][2].tuple
.src
.l3num
= AF_INET6
;
1567 sip
[i
][2].tuple
.dst
.protonum
= IPPROTO_UDP
;
1568 sip
[i
][2].help
= sip_help_udp
;
1569 sip
[i
][3].tuple
.src
.l3num
= AF_INET6
;
1570 sip
[i
][3].tuple
.dst
.protonum
= IPPROTO_TCP
;
1571 sip
[i
][3].help
= sip_help_tcp
;
1573 for (j
= 0; j
< ARRAY_SIZE(sip
[i
]); j
++) {
1574 sip
[i
][j
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
1575 sip
[i
][j
].expect_policy
= sip_exp_policy
;
1576 sip
[i
][j
].expect_class_max
= SIP_EXPECT_MAX
;
1577 sip
[i
][j
].me
= THIS_MODULE
;
1579 tmpname
= &sip_names
[i
][j
][0];
1580 if (ports
[i
] == SIP_PORT
)
1581 sprintf(tmpname
, "sip");
1583 sprintf(tmpname
, "sip-%u", i
);
1584 sip
[i
][j
].name
= tmpname
;
1586 pr_debug("port #%u: %u\n", i
, ports
[i
]);
1588 ret
= nf_conntrack_helper_register(&sip
[i
][j
]);
1590 printk(KERN_ERR
"nf_ct_sip: failed to register"
1591 " helper for pf: %u port: %u\n",
1592 sip
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
1593 nf_conntrack_sip_fini();
1601 module_init(nf_conntrack_sip_init
);
1602 module_exit(nf_conntrack_sip_fini
);