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/netfilter.h>
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_expect.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <linux/netfilter/nf_conntrack_sip.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
29 MODULE_DESCRIPTION("SIP connection tracking helper");
30 MODULE_ALIAS("ip_conntrack_sip");
33 static unsigned short ports
[MAX_PORTS
];
34 static unsigned int ports_c
;
35 module_param_array(ports
, ushort
, &ports_c
, 0400);
36 MODULE_PARM_DESC(ports
, "port numbers of SIP servers");
38 static unsigned int sip_timeout __read_mostly
= SIP_TIMEOUT
;
39 module_param(sip_timeout
, uint
, 0600);
40 MODULE_PARM_DESC(sip_timeout
, "timeout for the master SIP session");
42 static int sip_direct_signalling __read_mostly
= 1;
43 module_param(sip_direct_signalling
, int, 0600);
44 MODULE_PARM_DESC(sip_direct_signalling
, "expect incoming calls from registrar "
47 static int sip_direct_media __read_mostly
= 1;
48 module_param(sip_direct_media
, int, 0600);
49 MODULE_PARM_DESC(sip_direct_media
, "Expect Media streams between signalling "
50 "endpoints only (default 1)");
52 unsigned int (*nf_nat_sip_hook
)(struct sk_buff
*skb
,
54 unsigned int *datalen
) __read_mostly
;
55 EXPORT_SYMBOL_GPL(nf_nat_sip_hook
);
57 unsigned int (*nf_nat_sip_expect_hook
)(struct sk_buff
*skb
,
59 unsigned int *datalen
,
60 struct nf_conntrack_expect
*exp
,
61 unsigned int matchoff
,
62 unsigned int matchlen
) __read_mostly
;
63 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook
);
65 unsigned int (*nf_nat_sdp_addr_hook
)(struct sk_buff
*skb
,
68 unsigned int *datalen
,
69 enum sdp_header_types type
,
70 enum sdp_header_types term
,
71 const union nf_inet_addr
*addr
)
73 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook
);
75 unsigned int (*nf_nat_sdp_port_hook
)(struct sk_buff
*skb
,
77 unsigned int *datalen
,
78 unsigned int matchoff
,
79 unsigned int matchlen
,
80 u_int16_t port
) __read_mostly
;
81 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook
);
83 unsigned int (*nf_nat_sdp_session_hook
)(struct sk_buff
*skb
,
86 unsigned int *datalen
,
87 const union nf_inet_addr
*addr
)
89 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook
);
91 unsigned int (*nf_nat_sdp_media_hook
)(struct sk_buff
*skb
,
93 unsigned int *datalen
,
94 struct nf_conntrack_expect
*rtp_exp
,
95 struct nf_conntrack_expect
*rtcp_exp
,
96 unsigned int mediaoff
,
97 unsigned int medialen
,
98 union nf_inet_addr
*rtp_addr
)
100 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook
);
102 static int string_len(const struct nf_conn
*ct
, const char *dptr
,
103 const char *limit
, int *shift
)
107 while (dptr
< limit
&& isalpha(*dptr
)) {
114 static int digits_len(const struct nf_conn
*ct
, const char *dptr
,
115 const char *limit
, int *shift
)
118 while (dptr
< limit
&& isdigit(*dptr
)) {
125 /* get media type + port length */
126 static int media_len(const struct nf_conn
*ct
, const char *dptr
,
127 const char *limit
, int *shift
)
129 int len
= string_len(ct
, dptr
, limit
, shift
);
132 if (dptr
>= limit
|| *dptr
!= ' ')
137 return len
+ digits_len(ct
, dptr
, limit
, shift
);
140 static int parse_addr(const struct nf_conn
*ct
, const char *cp
,
141 const char **endp
, union nf_inet_addr
*addr
,
147 memset(addr
, 0, sizeof(*addr
));
148 switch (nf_ct_l3num(ct
)) {
150 ret
= in4_pton(cp
, limit
- cp
, (u8
*)&addr
->ip
, -1, &end
);
153 ret
= in6_pton(cp
, limit
- cp
, (u8
*)&addr
->ip6
, -1, &end
);
159 if (ret
== 0 || end
== cp
)
166 /* skip ip address. returns its length. */
167 static int epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
168 const char *limit
, int *shift
)
170 union nf_inet_addr addr
;
171 const char *aux
= dptr
;
173 if (!parse_addr(ct
, dptr
, &dptr
, &addr
, limit
)) {
174 pr_debug("ip: %s parse failed.!\n", dptr
);
181 dptr
+= digits_len(ct
, dptr
, limit
, shift
);
186 /* get address length, skiping user info. */
187 static int skp_epaddr_len(const struct nf_conn
*ct
, const char *dptr
,
188 const char *limit
, int *shift
)
190 const char *start
= dptr
;
193 /* Search for @, but stop at the end of the line.
194 * We are inside a sip: URI, so we don't need to worry about
195 * continuation lines. */
196 while (dptr
< limit
&&
197 *dptr
!= '@' && *dptr
!= '\r' && *dptr
!= '\n') {
202 if (dptr
< limit
&& *dptr
== '@') {
210 return epaddr_len(ct
, dptr
, limit
, shift
);
213 /* Parse a SIP request line of the form:
215 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
217 * and return the offset and length of the address contained in the Request-URI.
219 int ct_sip_parse_request(const struct nf_conn
*ct
,
220 const char *dptr
, unsigned int datalen
,
221 unsigned int *matchoff
, unsigned int *matchlen
,
222 union nf_inet_addr
*addr
, __be16
*port
)
224 const char *start
= dptr
, *limit
= dptr
+ datalen
, *end
;
229 /* Skip method and following whitespace */
230 mlen
= string_len(ct
, dptr
, limit
, NULL
);
238 limit
-= strlen("sip:");
239 for (; dptr
< limit
; dptr
++) {
240 if (*dptr
== '\r' || *dptr
== '\n')
242 if (strnicmp(dptr
, "sip:", strlen("sip:")) == 0)
245 if (!skp_epaddr_len(ct
, dptr
, limit
, &shift
))
249 if (!parse_addr(ct
, dptr
, &end
, addr
, limit
))
251 if (end
< limit
&& *end
== ':') {
253 p
= simple_strtoul(end
, (char **)&end
, 10);
254 if (p
< 1024 || p
> 65535)
258 *port
= htons(SIP_PORT
);
262 *matchoff
= dptr
- start
;
263 *matchlen
= end
- dptr
;
266 EXPORT_SYMBOL_GPL(ct_sip_parse_request
);
268 /* SIP header parsing: SIP headers are located at the beginning of a line, but
269 * may span several lines, in which case the continuation lines begin with a
270 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
271 * CRLF, RFC 3261 allows only CRLF, we support both.
273 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
274 * whitespace and the values. Whitespace in this context means any amount of
275 * tabs, spaces and continuation lines, which are treated as a single whitespace
278 * Some headers may appear multiple times. A comma seperated list of values is
279 * equivalent to multiple headers.
281 static const struct sip_header ct_sip_hdrs
[] = {
282 [SIP_HDR_CSEQ
] = SIP_HDR("CSeq", NULL
, NULL
, digits_len
),
283 [SIP_HDR_FROM
] = SIP_HDR("From", "f", "sip:", skp_epaddr_len
),
284 [SIP_HDR_TO
] = SIP_HDR("To", "t", "sip:", skp_epaddr_len
),
285 [SIP_HDR_CONTACT
] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len
),
286 [SIP_HDR_VIA
] = SIP_HDR("Via", "v", "UDP ", epaddr_len
),
287 [SIP_HDR_EXPIRES
] = SIP_HDR("Expires", NULL
, NULL
, digits_len
),
288 [SIP_HDR_CONTENT_LENGTH
] = SIP_HDR("Content-Length", "l", NULL
, digits_len
),
291 static const char *sip_follow_continuation(const char *dptr
, const char *limit
)
293 /* Walk past newline */
297 /* Skip '\n' in CR LF */
298 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
303 /* Continuation line? */
304 if (*dptr
!= ' ' && *dptr
!= '\t')
307 /* skip leading whitespace */
308 for (; dptr
< limit
; dptr
++) {
309 if (*dptr
!= ' ' && *dptr
!= '\t')
315 static const char *sip_skip_whitespace(const char *dptr
, const char *limit
)
317 for (; dptr
< limit
; dptr
++) {
320 if (*dptr
!= '\r' && *dptr
!= '\n')
322 dptr
= sip_follow_continuation(dptr
, limit
);
329 /* Search within a SIP header value, dealing with continuation lines */
330 static const char *ct_sip_header_search(const char *dptr
, const char *limit
,
331 const char *needle
, unsigned int len
)
333 for (limit
-= len
; dptr
< limit
; dptr
++) {
334 if (*dptr
== '\r' || *dptr
== '\n') {
335 dptr
= sip_follow_continuation(dptr
, limit
);
341 if (strnicmp(dptr
, needle
, len
) == 0)
347 int ct_sip_get_header(const struct nf_conn
*ct
, const char *dptr
,
348 unsigned int dataoff
, unsigned int datalen
,
349 enum sip_header_types type
,
350 unsigned int *matchoff
, unsigned int *matchlen
)
352 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
353 const char *start
= dptr
, *limit
= dptr
+ datalen
;
356 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
357 /* Find beginning of line */
358 if (*dptr
!= '\r' && *dptr
!= '\n')
362 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
367 /* Skip continuation lines */
368 if (*dptr
== ' ' || *dptr
== '\t')
371 /* Find header. Compact headers must be followed by a
372 * non-alphabetic character to avoid mismatches. */
373 if (limit
- dptr
>= hdr
->len
&&
374 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
376 else if (hdr
->cname
&& limit
- dptr
>= hdr
->clen
+ 1 &&
377 strnicmp(dptr
, hdr
->cname
, hdr
->clen
) == 0 &&
378 !isalpha(*(dptr
+ hdr
->clen
+ 1)))
383 /* Find and skip colon */
384 dptr
= sip_skip_whitespace(dptr
, limit
);
387 if (*dptr
!= ':' || ++dptr
>= limit
)
390 /* Skip whitespace after colon */
391 dptr
= sip_skip_whitespace(dptr
, limit
);
395 *matchoff
= dptr
- start
;
397 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
,
404 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
407 *matchoff
= dptr
- start
+ shift
;
412 EXPORT_SYMBOL_GPL(ct_sip_get_header
);
414 /* Get next header field in a list of comma seperated values */
415 static int ct_sip_next_header(const struct nf_conn
*ct
, const char *dptr
,
416 unsigned int dataoff
, unsigned int datalen
,
417 enum sip_header_types type
,
418 unsigned int *matchoff
, unsigned int *matchlen
)
420 const struct sip_header
*hdr
= &ct_sip_hdrs
[type
];
421 const char *start
= dptr
, *limit
= dptr
+ datalen
;
426 dptr
= ct_sip_header_search(dptr
, limit
, ",", strlen(","));
430 dptr
= ct_sip_header_search(dptr
, limit
, hdr
->search
, hdr
->slen
);
435 *matchoff
= dptr
- start
;
436 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
443 /* Walk through headers until a parsable one is found or no header of the
444 * given type is left. */
445 static int ct_sip_walk_headers(const struct nf_conn
*ct
, const char *dptr
,
446 unsigned int dataoff
, unsigned int datalen
,
447 enum sip_header_types type
, int *in_header
,
448 unsigned int *matchoff
, unsigned int *matchlen
)
452 if (in_header
&& *in_header
) {
454 ret
= ct_sip_next_header(ct
, dptr
, dataoff
, datalen
,
455 type
, matchoff
, matchlen
);
460 dataoff
+= *matchoff
;
466 ret
= ct_sip_get_header(ct
, dptr
, dataoff
, datalen
,
467 type
, matchoff
, matchlen
);
472 dataoff
+= *matchoff
;
480 /* Locate a SIP header, parse the URI and return the offset and length of
481 * the address as well as the address and port themselves. A stream of
482 * headers can be parsed by handing in a non-NULL datalen and in_header
485 int ct_sip_parse_header_uri(const struct nf_conn
*ct
, const char *dptr
,
486 unsigned int *dataoff
, unsigned int datalen
,
487 enum sip_header_types type
, int *in_header
,
488 unsigned int *matchoff
, unsigned int *matchlen
,
489 union nf_inet_addr
*addr
, __be16
*port
)
491 const char *c
, *limit
= dptr
+ datalen
;
495 ret
= ct_sip_walk_headers(ct
, dptr
, dataoff
? *dataoff
: 0, datalen
,
496 type
, in_header
, matchoff
, matchlen
);
501 if (!parse_addr(ct
, dptr
+ *matchoff
, &c
, addr
, limit
))
505 p
= simple_strtoul(c
, (char **)&c
, 10);
506 if (p
< 1024 || p
> 65535)
510 *port
= htons(SIP_PORT
);
516 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri
);
518 /* Parse address from header parameter and return address, offset and length */
519 int ct_sip_parse_address_param(const struct nf_conn
*ct
, const char *dptr
,
520 unsigned int dataoff
, unsigned int datalen
,
522 unsigned int *matchoff
, unsigned int *matchlen
,
523 union nf_inet_addr
*addr
)
525 const char *limit
= dptr
+ datalen
;
526 const char *start
, *end
;
528 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
530 limit
= dptr
+ datalen
;
532 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
536 start
+= strlen(name
);
537 if (!parse_addr(ct
, start
, &end
, addr
, limit
))
539 *matchoff
= start
- dptr
;
540 *matchlen
= end
- start
;
543 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param
);
545 /* Parse numerical header parameter and return value, offset and length */
546 int ct_sip_parse_numerical_param(const struct nf_conn
*ct
, const char *dptr
,
547 unsigned int dataoff
, unsigned int datalen
,
549 unsigned int *matchoff
, unsigned int *matchlen
,
552 const char *limit
= dptr
+ datalen
;
556 limit
= ct_sip_header_search(dptr
+ dataoff
, limit
, ",", strlen(","));
558 limit
= dptr
+ datalen
;
560 start
= ct_sip_header_search(dptr
+ dataoff
, limit
, name
, strlen(name
));
564 start
+= strlen(name
);
565 *val
= simple_strtoul(start
, &end
, 0);
568 if (matchoff
&& matchlen
) {
569 *matchoff
= start
- dptr
;
570 *matchlen
= end
- start
;
574 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param
);
576 /* SDP header parsing: a SDP session description contains an ordered set of
577 * headers, starting with a section containing general session parameters,
578 * optionally followed by multiple media descriptions.
580 * SDP headers always start at the beginning of a line. According to RFC 2327:
581 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
582 * be tolerant and also accept records terminated with a single newline
583 * character". We handle both cases.
585 static const struct sip_header ct_sdp_hdrs
[] = {
586 [SDP_HDR_VERSION
] = SDP_HDR("v=", NULL
, digits_len
),
587 [SDP_HDR_OWNER_IP4
] = SDP_HDR("o=", "IN IP4 ", epaddr_len
),
588 [SDP_HDR_CONNECTION_IP4
] = SDP_HDR("c=", "IN IP4 ", epaddr_len
),
589 [SDP_HDR_OWNER_IP6
] = SDP_HDR("o=", "IN IP6 ", epaddr_len
),
590 [SDP_HDR_CONNECTION_IP6
] = SDP_HDR("c=", "IN IP6 ", epaddr_len
),
591 [SDP_HDR_MEDIA
] = SDP_HDR("m=", NULL
, media_len
),
594 /* Linear string search within SDP header values */
595 static const char *ct_sdp_header_search(const char *dptr
, const char *limit
,
596 const char *needle
, unsigned int len
)
598 for (limit
-= len
; dptr
< limit
; dptr
++) {
599 if (*dptr
== '\r' || *dptr
== '\n')
601 if (strncmp(dptr
, needle
, len
) == 0)
607 /* Locate a SDP header (optionally a substring within the header value),
608 * optionally stopping at the first occurence of the term header, parse
609 * it and return the offset and length of the data we're interested in.
611 int ct_sip_get_sdp_header(const struct nf_conn
*ct
, const char *dptr
,
612 unsigned int dataoff
, unsigned int datalen
,
613 enum sdp_header_types type
,
614 enum sdp_header_types term
,
615 unsigned int *matchoff
, unsigned int *matchlen
)
617 const struct sip_header
*hdr
= &ct_sdp_hdrs
[type
];
618 const struct sip_header
*thdr
= &ct_sdp_hdrs
[term
];
619 const char *start
= dptr
, *limit
= dptr
+ datalen
;
622 for (dptr
+= dataoff
; dptr
< limit
; dptr
++) {
623 /* Find beginning of line */
624 if (*dptr
!= '\r' && *dptr
!= '\n')
628 if (*(dptr
- 1) == '\r' && *dptr
== '\n') {
633 if (term
!= SDP_HDR_UNSPEC
&&
634 limit
- dptr
>= thdr
->len
&&
635 strnicmp(dptr
, thdr
->name
, thdr
->len
) == 0)
637 else if (limit
- dptr
>= hdr
->len
&&
638 strnicmp(dptr
, hdr
->name
, hdr
->len
) == 0)
643 *matchoff
= dptr
- start
;
645 dptr
= ct_sdp_header_search(dptr
, limit
, hdr
->search
,
652 *matchlen
= hdr
->match_len(ct
, dptr
, limit
, &shift
);
655 *matchoff
= dptr
- start
+ shift
;
660 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header
);
662 static int ct_sip_parse_sdp_addr(const struct nf_conn
*ct
, const char *dptr
,
663 unsigned int dataoff
, unsigned int datalen
,
664 enum sdp_header_types type
,
665 enum sdp_header_types term
,
666 unsigned int *matchoff
, unsigned int *matchlen
,
667 union nf_inet_addr
*addr
)
671 ret
= ct_sip_get_sdp_header(ct
, dptr
, dataoff
, datalen
, type
, term
,
676 if (!parse_addr(ct
, dptr
+ *matchoff
, NULL
, addr
,
677 dptr
+ *matchoff
+ *matchlen
))
682 static int refresh_signalling_expectation(struct nf_conn
*ct
,
683 union nf_inet_addr
*addr
,
685 unsigned int expires
)
687 struct nf_conn_help
*help
= nfct_help(ct
);
688 struct nf_conntrack_expect
*exp
;
689 struct hlist_node
*n
, *next
;
692 spin_lock_bh(&nf_conntrack_lock
);
693 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
694 if (exp
->class != SIP_EXPECT_SIGNALLING
||
695 !nf_inet_addr_cmp(&exp
->tuple
.dst
.u3
, addr
) ||
696 exp
->tuple
.dst
.u
.udp
.port
!= port
)
698 if (!del_timer(&exp
->timeout
))
700 exp
->flags
&= ~NF_CT_EXPECT_INACTIVE
;
701 exp
->timeout
.expires
= jiffies
+ expires
* HZ
;
702 add_timer(&exp
->timeout
);
706 spin_unlock_bh(&nf_conntrack_lock
);
710 static void flush_expectations(struct nf_conn
*ct
, bool media
)
712 struct nf_conn_help
*help
= nfct_help(ct
);
713 struct nf_conntrack_expect
*exp
;
714 struct hlist_node
*n
, *next
;
716 spin_lock_bh(&nf_conntrack_lock
);
717 hlist_for_each_entry_safe(exp
, n
, next
, &help
->expectations
, lnode
) {
718 if ((exp
->class != SIP_EXPECT_SIGNALLING
) ^ media
)
720 if (!del_timer(&exp
->timeout
))
722 nf_ct_unlink_expect(exp
);
723 nf_ct_expect_put(exp
);
727 spin_unlock_bh(&nf_conntrack_lock
);
730 static int set_expected_rtp_rtcp(struct sk_buff
*skb
,
731 const char **dptr
, unsigned int *datalen
,
732 union nf_inet_addr
*daddr
, __be16 port
,
733 enum sip_expectation_classes
class,
734 unsigned int mediaoff
, unsigned int medialen
)
736 struct nf_conntrack_expect
*exp
, *rtp_exp
, *rtcp_exp
;
737 enum ip_conntrack_info ctinfo
;
738 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
739 struct net
*net
= nf_ct_net(ct
);
740 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
741 union nf_inet_addr
*saddr
;
742 struct nf_conntrack_tuple tuple
;
743 int direct_rtp
= 0, skip_expect
= 0, ret
= NF_DROP
;
745 __be16 rtp_port
, rtcp_port
;
746 typeof(nf_nat_sdp_port_hook
) nf_nat_sdp_port
;
747 typeof(nf_nat_sdp_media_hook
) nf_nat_sdp_media
;
750 if (sip_direct_media
) {
751 if (!nf_inet_addr_cmp(daddr
, &ct
->tuplehash
[dir
].tuple
.src
.u3
))
753 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
756 /* We need to check whether the registration exists before attempting
757 * to register it since we can see the same media description multiple
758 * times on different connections in case multiple endpoints receive
761 * RTP optimization: if we find a matching media channel expectation
762 * and both the expectation and this connection are SNATed, we assume
763 * both sides can reach each other directly and use the final
764 * destination address from the expectation. We still need to keep
765 * the NATed expectations for media that might arrive from the
766 * outside, and additionally need to expect the direct RTP stream
767 * in case it passes through us even without NAT.
769 memset(&tuple
, 0, sizeof(tuple
));
771 tuple
.src
.u3
= *saddr
;
772 tuple
.src
.l3num
= nf_ct_l3num(ct
);
773 tuple
.dst
.protonum
= IPPROTO_UDP
;
774 tuple
.dst
.u3
= *daddr
;
775 tuple
.dst
.u
.udp
.port
= port
;
779 exp
= __nf_ct_expect_find(net
, &tuple
);
781 if (!exp
|| exp
->master
== ct
||
782 nfct_help(exp
->master
)->helper
!= nfct_help(ct
)->helper
||
785 #ifdef CONFIG_NF_NAT_NEEDED
786 if (exp
->tuple
.src
.l3num
== AF_INET
&& !direct_rtp
&&
787 (exp
->saved_ip
!= exp
->tuple
.dst
.u3
.ip
||
788 exp
->saved_proto
.udp
.port
!= exp
->tuple
.dst
.u
.udp
.port
) &&
789 ct
->status
& IPS_NAT_MASK
) {
790 daddr
->ip
= exp
->saved_ip
;
791 tuple
.dst
.u3
.ip
= exp
->saved_ip
;
792 tuple
.dst
.u
.udp
.port
= exp
->saved_proto
.udp
.port
;
797 } while (!skip_expect
);
800 base_port
= ntohs(tuple
.dst
.u
.udp
.port
) & ~1;
801 rtp_port
= htons(base_port
);
802 rtcp_port
= htons(base_port
+ 1);
805 nf_nat_sdp_port
= rcu_dereference(nf_nat_sdp_port_hook
);
806 if (nf_nat_sdp_port
&&
807 !nf_nat_sdp_port(skb
, dptr
, datalen
,
808 mediaoff
, medialen
, ntohs(rtp_port
)))
815 rtp_exp
= nf_ct_expect_alloc(ct
);
818 nf_ct_expect_init(rtp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
819 IPPROTO_UDP
, NULL
, &rtp_port
);
821 rtcp_exp
= nf_ct_expect_alloc(ct
);
822 if (rtcp_exp
== NULL
)
824 nf_ct_expect_init(rtcp_exp
, class, nf_ct_l3num(ct
), saddr
, daddr
,
825 IPPROTO_UDP
, NULL
, &rtcp_port
);
827 nf_nat_sdp_media
= rcu_dereference(nf_nat_sdp_media_hook
);
828 if (nf_nat_sdp_media
&& ct
->status
& IPS_NAT_MASK
&& !direct_rtp
)
829 ret
= nf_nat_sdp_media(skb
, dptr
, datalen
, rtp_exp
, rtcp_exp
,
830 mediaoff
, medialen
, daddr
);
832 if (nf_ct_expect_related(rtp_exp
) == 0) {
833 if (nf_ct_expect_related(rtcp_exp
) != 0)
834 nf_ct_unexpect_related(rtp_exp
);
839 nf_ct_expect_put(rtcp_exp
);
841 nf_ct_expect_put(rtp_exp
);
846 static const struct sdp_media_type sdp_media_types
[] = {
847 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO
),
848 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO
),
851 static const struct sdp_media_type
*sdp_media_type(const char *dptr
,
852 unsigned int matchoff
,
853 unsigned int matchlen
)
855 const struct sdp_media_type
*t
;
858 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); i
++) {
859 t
= &sdp_media_types
[i
];
860 if (matchlen
< t
->len
||
861 strncmp(dptr
+ matchoff
, t
->name
, t
->len
))
868 static int process_sdp(struct sk_buff
*skb
,
869 const char **dptr
, unsigned int *datalen
,
872 enum ip_conntrack_info ctinfo
;
873 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
874 struct nf_conn_help
*help
= nfct_help(ct
);
875 unsigned int matchoff
, matchlen
;
876 unsigned int mediaoff
, medialen
;
878 unsigned int caddr_len
, maddr_len
;
880 union nf_inet_addr caddr
, maddr
, rtp_addr
;
882 enum sdp_header_types c_hdr
;
883 const struct sdp_media_type
*t
;
885 typeof(nf_nat_sdp_addr_hook
) nf_nat_sdp_addr
;
886 typeof(nf_nat_sdp_session_hook
) nf_nat_sdp_session
;
888 nf_nat_sdp_addr
= rcu_dereference(nf_nat_sdp_addr_hook
);
889 c_hdr
= nf_ct_l3num(ct
) == AF_INET
? SDP_HDR_CONNECTION_IP4
:
890 SDP_HDR_CONNECTION_IP6
;
892 /* Find beginning of session description */
893 if (ct_sip_get_sdp_header(ct
, *dptr
, 0, *datalen
,
894 SDP_HDR_VERSION
, SDP_HDR_UNSPEC
,
895 &matchoff
, &matchlen
) <= 0)
899 /* The connection information is contained in the session description
900 * and/or once per media description. The first media description marks
901 * the end of the session description. */
903 if (ct_sip_parse_sdp_addr(ct
, *dptr
, sdpoff
, *datalen
,
904 c_hdr
, SDP_HDR_MEDIA
,
905 &matchoff
, &matchlen
, &caddr
) > 0)
906 caddr_len
= matchlen
;
909 for (i
= 0; i
< ARRAY_SIZE(sdp_media_types
); ) {
910 if (ct_sip_get_sdp_header(ct
, *dptr
, mediaoff
, *datalen
,
911 SDP_HDR_MEDIA
, SDP_HDR_UNSPEC
,
912 &mediaoff
, &medialen
) <= 0)
915 /* Get media type and port number. A media port value of zero
916 * indicates an inactive stream. */
917 t
= sdp_media_type(*dptr
, mediaoff
, medialen
);
919 mediaoff
+= medialen
;
925 port
= simple_strtoul(*dptr
+ mediaoff
, NULL
, 10);
928 if (port
< 1024 || port
> 65535)
931 /* The media description overrides the session description. */
933 if (ct_sip_parse_sdp_addr(ct
, *dptr
, mediaoff
, *datalen
,
934 c_hdr
, SDP_HDR_MEDIA
,
935 &matchoff
, &matchlen
, &maddr
) > 0) {
936 maddr_len
= matchlen
;
937 memcpy(&rtp_addr
, &maddr
, sizeof(rtp_addr
));
938 } else if (caddr_len
)
939 memcpy(&rtp_addr
, &caddr
, sizeof(rtp_addr
));
943 ret
= set_expected_rtp_rtcp(skb
, dptr
, datalen
,
944 &rtp_addr
, htons(port
), t
->class,
946 if (ret
!= NF_ACCEPT
)
949 /* Update media connection address if present */
950 if (maddr_len
&& nf_nat_sdp_addr
&& ct
->status
& IPS_NAT_MASK
) {
951 ret
= nf_nat_sdp_addr(skb
, dptr
, mediaoff
, datalen
,
952 c_hdr
, SDP_HDR_MEDIA
, &rtp_addr
);
953 if (ret
!= NF_ACCEPT
)
959 /* Update session connection and owner addresses */
960 nf_nat_sdp_session
= rcu_dereference(nf_nat_sdp_session_hook
);
961 if (nf_nat_sdp_session
&& ct
->status
& IPS_NAT_MASK
)
962 ret
= nf_nat_sdp_session(skb
, dptr
, sdpoff
, datalen
, &rtp_addr
);
964 if (ret
== NF_ACCEPT
&& i
> 0)
965 help
->help
.ct_sip_info
.invite_cseq
= cseq
;
969 static int process_invite_response(struct sk_buff
*skb
,
970 const char **dptr
, unsigned int *datalen
,
971 unsigned int cseq
, unsigned int code
)
973 enum ip_conntrack_info ctinfo
;
974 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
975 struct nf_conn_help
*help
= nfct_help(ct
);
977 if ((code
>= 100 && code
<= 199) ||
978 (code
>= 200 && code
<= 299))
979 return process_sdp(skb
, dptr
, datalen
, cseq
);
980 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
981 flush_expectations(ct
, true);
985 static int process_update_response(struct sk_buff
*skb
,
986 const char **dptr
, unsigned int *datalen
,
987 unsigned int cseq
, unsigned int code
)
989 enum ip_conntrack_info ctinfo
;
990 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
991 struct nf_conn_help
*help
= nfct_help(ct
);
993 if ((code
>= 100 && code
<= 199) ||
994 (code
>= 200 && code
<= 299))
995 return process_sdp(skb
, dptr
, datalen
, cseq
);
996 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
997 flush_expectations(ct
, true);
1001 static int process_prack_response(struct sk_buff
*skb
,
1002 const char **dptr
, unsigned int *datalen
,
1003 unsigned int cseq
, unsigned int code
)
1005 enum ip_conntrack_info ctinfo
;
1006 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1007 struct nf_conn_help
*help
= nfct_help(ct
);
1009 if ((code
>= 100 && code
<= 199) ||
1010 (code
>= 200 && code
<= 299))
1011 return process_sdp(skb
, dptr
, datalen
, cseq
);
1012 else if (help
->help
.ct_sip_info
.invite_cseq
== cseq
)
1013 flush_expectations(ct
, true);
1017 static int process_bye_request(struct sk_buff
*skb
,
1018 const char **dptr
, unsigned int *datalen
,
1021 enum ip_conntrack_info ctinfo
;
1022 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1024 flush_expectations(ct
, true);
1028 /* Parse a REGISTER request and create a permanent expectation for incoming
1029 * signalling connections. The expectation is marked inactive and is activated
1030 * when receiving a response indicating success from the registrar.
1032 static int process_register_request(struct sk_buff
*skb
,
1033 const char **dptr
, unsigned int *datalen
,
1036 enum ip_conntrack_info ctinfo
;
1037 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1038 struct nf_conn_help
*help
= nfct_help(ct
);
1039 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1040 unsigned int matchoff
, matchlen
;
1041 struct nf_conntrack_expect
*exp
;
1042 union nf_inet_addr
*saddr
, daddr
;
1044 unsigned int expires
= 0;
1046 typeof(nf_nat_sip_expect_hook
) nf_nat_sip_expect
;
1048 /* Expected connections can not register again. */
1049 if (ct
->status
& IPS_EXPECTED
)
1052 /* We must check the expiration time: a value of zero signals the
1053 * registrar to release the binding. We'll remove our expectation
1054 * when receiving the new bindings in the response, but we don't
1055 * want to create new ones.
1057 * The expiration time may be contained in Expires: header, the
1058 * Contact: header parameters or the URI parameters.
1060 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1061 &matchoff
, &matchlen
) > 0)
1062 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1064 ret
= ct_sip_parse_header_uri(ct
, *dptr
, NULL
, *datalen
,
1065 SIP_HDR_CONTACT
, NULL
,
1066 &matchoff
, &matchlen
, &daddr
, &port
);
1072 /* We don't support third-party registrations */
1073 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.src
.u3
, &daddr
))
1076 if (ct_sip_parse_numerical_param(ct
, *dptr
,
1077 matchoff
+ matchlen
, *datalen
,
1078 "expires=", NULL
, NULL
, &expires
) < 0)
1086 exp
= nf_ct_expect_alloc(ct
);
1091 if (sip_direct_signalling
)
1092 saddr
= &ct
->tuplehash
[!dir
].tuple
.src
.u3
;
1094 nf_ct_expect_init(exp
, SIP_EXPECT_SIGNALLING
, nf_ct_l3num(ct
),
1095 saddr
, &daddr
, IPPROTO_UDP
, NULL
, &port
);
1096 exp
->timeout
.expires
= sip_timeout
* HZ
;
1097 exp
->helper
= nfct_help(ct
)->helper
;
1098 exp
->flags
= NF_CT_EXPECT_PERMANENT
| NF_CT_EXPECT_INACTIVE
;
1100 nf_nat_sip_expect
= rcu_dereference(nf_nat_sip_expect_hook
);
1101 if (nf_nat_sip_expect
&& ct
->status
& IPS_NAT_MASK
)
1102 ret
= nf_nat_sip_expect(skb
, dptr
, datalen
, exp
,
1103 matchoff
, matchlen
);
1105 if (nf_ct_expect_related(exp
) != 0)
1110 nf_ct_expect_put(exp
);
1113 if (ret
== NF_ACCEPT
)
1114 help
->help
.ct_sip_info
.register_cseq
= cseq
;
1118 static int process_register_response(struct sk_buff
*skb
,
1119 const char **dptr
, unsigned int *datalen
,
1120 unsigned int cseq
, unsigned int code
)
1122 enum ip_conntrack_info ctinfo
;
1123 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1124 struct nf_conn_help
*help
= nfct_help(ct
);
1125 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
1126 union nf_inet_addr addr
;
1128 unsigned int matchoff
, matchlen
, dataoff
= 0;
1129 unsigned int expires
= 0;
1130 int in_contact
= 0, ret
;
1132 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1133 * they have received a final response from the registrar for the
1134 * previous one or the previous REGISTER request has timed out".
1136 * However, some servers fail to detect retransmissions and send late
1137 * responses, so we store the sequence number of the last valid
1138 * request and compare it here.
1140 if (help
->help
.ct_sip_info
.register_cseq
!= cseq
)
1143 if (code
>= 100 && code
<= 199)
1145 if (code
< 200 || code
> 299)
1148 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_EXPIRES
,
1149 &matchoff
, &matchlen
) > 0)
1150 expires
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1153 unsigned int c_expires
= expires
;
1155 ret
= ct_sip_parse_header_uri(ct
, *dptr
, &dataoff
, *datalen
,
1156 SIP_HDR_CONTACT
, &in_contact
,
1157 &matchoff
, &matchlen
,
1164 /* We don't support third-party registrations */
1165 if (!nf_inet_addr_cmp(&ct
->tuplehash
[dir
].tuple
.dst
.u3
, &addr
))
1168 ret
= ct_sip_parse_numerical_param(ct
, *dptr
,
1169 matchoff
+ matchlen
,
1170 *datalen
, "expires=",
1171 NULL
, NULL
, &c_expires
);
1176 if (refresh_signalling_expectation(ct
, &addr
, port
, c_expires
))
1181 flush_expectations(ct
, false);
1185 static const struct sip_handler sip_handlers
[] = {
1186 SIP_HANDLER("INVITE", process_sdp
, process_invite_response
),
1187 SIP_HANDLER("UPDATE", process_sdp
, process_update_response
),
1188 SIP_HANDLER("ACK", process_sdp
, NULL
),
1189 SIP_HANDLER("PRACK", process_sdp
, process_prack_response
),
1190 SIP_HANDLER("BYE", process_bye_request
, NULL
),
1191 SIP_HANDLER("REGISTER", process_register_request
, process_register_response
),
1194 static int process_sip_response(struct sk_buff
*skb
,
1195 const char **dptr
, unsigned int *datalen
)
1197 enum ip_conntrack_info ctinfo
;
1198 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1199 unsigned int matchoff
, matchlen
;
1200 unsigned int code
, cseq
, dataoff
, i
;
1202 if (*datalen
< strlen("SIP/2.0 200"))
1204 code
= simple_strtoul(*dptr
+ strlen("SIP/2.0 "), NULL
, 10);
1208 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1209 &matchoff
, &matchlen
) <= 0)
1211 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1214 dataoff
= matchoff
+ matchlen
+ 1;
1216 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1217 const struct sip_handler
*handler
;
1219 handler
= &sip_handlers
[i
];
1220 if (handler
->response
== NULL
)
1222 if (*datalen
< dataoff
+ handler
->len
||
1223 strnicmp(*dptr
+ dataoff
, handler
->method
, handler
->len
))
1225 return handler
->response(skb
, dptr
, datalen
, cseq
, code
);
1230 static int process_sip_request(struct sk_buff
*skb
,
1231 const char **dptr
, unsigned int *datalen
)
1233 enum ip_conntrack_info ctinfo
;
1234 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1235 unsigned int matchoff
, matchlen
;
1236 unsigned int cseq
, i
;
1238 for (i
= 0; i
< ARRAY_SIZE(sip_handlers
); i
++) {
1239 const struct sip_handler
*handler
;
1241 handler
= &sip_handlers
[i
];
1242 if (handler
->request
== NULL
)
1244 if (*datalen
< handler
->len
||
1245 strnicmp(*dptr
, handler
->method
, handler
->len
))
1248 if (ct_sip_get_header(ct
, *dptr
, 0, *datalen
, SIP_HDR_CSEQ
,
1249 &matchoff
, &matchlen
) <= 0)
1251 cseq
= simple_strtoul(*dptr
+ matchoff
, NULL
, 10);
1255 return handler
->request(skb
, dptr
, datalen
, cseq
);
1260 static int sip_help(struct sk_buff
*skb
,
1261 unsigned int protoff
,
1263 enum ip_conntrack_info ctinfo
)
1265 unsigned int dataoff
, datalen
;
1268 typeof(nf_nat_sip_hook
) nf_nat_sip
;
1271 dataoff
= protoff
+ sizeof(struct udphdr
);
1272 if (dataoff
>= skb
->len
)
1275 nf_ct_refresh(ct
, skb
, sip_timeout
* HZ
);
1277 if (!skb_is_nonlinear(skb
))
1278 dptr
= skb
->data
+ dataoff
;
1280 pr_debug("Copy of skbuff not supported yet.\n");
1284 datalen
= skb
->len
- dataoff
;
1285 if (datalen
< strlen("SIP/2.0 200"))
1288 if (strnicmp(dptr
, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1289 ret
= process_sip_request(skb
, &dptr
, &datalen
);
1291 ret
= process_sip_response(skb
, &dptr
, &datalen
);
1293 if (ret
== NF_ACCEPT
&& ct
->status
& IPS_NAT_MASK
) {
1294 nf_nat_sip
= rcu_dereference(nf_nat_sip_hook
);
1295 if (nf_nat_sip
&& !nf_nat_sip(skb
, &dptr
, &datalen
))
1302 static struct nf_conntrack_helper sip
[MAX_PORTS
][2] __read_mostly
;
1303 static char sip_names
[MAX_PORTS
][2][sizeof("sip-65535")] __read_mostly
;
1305 static const struct nf_conntrack_expect_policy sip_exp_policy
[SIP_EXPECT_MAX
+ 1] = {
1306 [SIP_EXPECT_SIGNALLING
] = {
1310 [SIP_EXPECT_AUDIO
] = {
1311 .max_expected
= 2 * IP_CT_DIR_MAX
,
1314 [SIP_EXPECT_VIDEO
] = {
1315 .max_expected
= 2 * IP_CT_DIR_MAX
,
1320 static void nf_conntrack_sip_fini(void)
1324 for (i
= 0; i
< ports_c
; i
++) {
1325 for (j
= 0; j
< 2; j
++) {
1326 if (sip
[i
][j
].me
== NULL
)
1328 nf_conntrack_helper_unregister(&sip
[i
][j
]);
1333 static int __init
nf_conntrack_sip_init(void)
1339 ports
[ports_c
++] = SIP_PORT
;
1341 for (i
= 0; i
< ports_c
; i
++) {
1342 memset(&sip
[i
], 0, sizeof(sip
[i
]));
1344 sip
[i
][0].tuple
.src
.l3num
= AF_INET
;
1345 sip
[i
][1].tuple
.src
.l3num
= AF_INET6
;
1346 for (j
= 0; j
< 2; j
++) {
1347 sip
[i
][j
].tuple
.dst
.protonum
= IPPROTO_UDP
;
1348 sip
[i
][j
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
1349 sip
[i
][j
].expect_policy
= sip_exp_policy
;
1350 sip
[i
][j
].expect_class_max
= SIP_EXPECT_MAX
;
1351 sip
[i
][j
].me
= THIS_MODULE
;
1352 sip
[i
][j
].help
= sip_help
;
1354 tmpname
= &sip_names
[i
][j
][0];
1355 if (ports
[i
] == SIP_PORT
)
1356 sprintf(tmpname
, "sip");
1358 sprintf(tmpname
, "sip-%u", i
);
1359 sip
[i
][j
].name
= tmpname
;
1361 pr_debug("port #%u: %u\n", i
, ports
[i
]);
1363 ret
= nf_conntrack_helper_register(&sip
[i
][j
]);
1365 printk("nf_ct_sip: failed to register helper "
1366 "for pf: %u port: %u\n",
1367 sip
[i
][j
].tuple
.src
.l3num
, ports
[i
]);
1368 nf_conntrack_sip_fini();
1376 module_init(nf_conntrack_sip_init
);
1377 module_exit(nf_conntrack_sip_fini
);