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 /* get media type + port length */
134 static int media_len(const struct nf_conn *ct, const char *dptr,
135 const char *limit, int *shift)
137 int len = string_len(ct, dptr, limit, shift);
140 if (dptr >= limit || *dptr != ' ')
145 return len + digits_len(ct, dptr, limit, shift);
148 static int parse_addr(const struct nf_conn *ct, const char *cp,
149 const char **endp, union nf_inet_addr *addr,
158 memset(addr, 0, sizeof(*addr));
159 switch (nf_ct_l3num(ct)) {
161 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
164 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
170 if (ret == 0 || end == cp)
177 /* skip ip address. returns its length. */
178 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
179 const char *limit, int *shift)
181 union nf_inet_addr addr;
182 const char *aux = dptr;
184 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
185 pr_debug("ip: %s parse failed.!\n", dptr);
192 dptr += digits_len(ct, dptr, limit, shift);
197 /* get address length, skiping user info. */
198 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
199 const char *limit, int *shift)
201 const char *start = dptr;
204 /* Search for @, but stop at the end of the line.
205 * We are inside a sip: URI, so we don't need to worry about
206 * continuation lines. */
207 while (dptr < limit &&
208 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
213 if (dptr < limit && *dptr == '@') {
221 return epaddr_len(ct, dptr, limit, shift);
224 /* Parse a SIP request line of the form:
226 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
228 * and return the offset and length of the address contained in the Request-URI.
230 int ct_sip_parse_request(const struct nf_conn *ct,
231 const char *dptr, unsigned int datalen,
232 unsigned int *matchoff, unsigned int *matchlen,
233 union nf_inet_addr *addr, __be16 *port)
235 const char *start = dptr, *limit = dptr + datalen, *end;
240 /* Skip method and following whitespace */
241 mlen = string_len(ct, dptr, limit, NULL);
249 for (; dptr < limit - strlen("sip:"); dptr++) {
250 if (*dptr == '\r' || *dptr == '\n')
252 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0) {
253 dptr += strlen("sip:");
257 if (!skp_epaddr_len(ct, dptr, limit, &shift))
261 if (!parse_addr(ct, dptr, &end, addr, limit))
263 if (end < limit && *end == ':') {
265 p = simple_strtoul(end, (char **)&end, 10);
266 if (p < 1024 || p > 65535)
270 *port = htons(SIP_PORT);
274 *matchoff = dptr - start;
275 *matchlen = end - dptr;
278 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
280 /* SIP header parsing: SIP headers are located at the beginning of a line, but
281 * may span several lines, in which case the continuation lines begin with a
282 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
283 * CRLF, RFC 3261 allows only CRLF, we support both.
285 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
286 * whitespace and the values. Whitespace in this context means any amount of
287 * tabs, spaces and continuation lines, which are treated as a single whitespace
290 * Some headers may appear multiple times. A comma separated list of values is
291 * equivalent to multiple headers.
293 static const struct sip_header ct_sip_hdrs[] = {
294 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
295 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
296 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
297 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
298 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
299 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
300 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
301 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
304 static const char *sip_follow_continuation(const char *dptr, const char *limit)
306 /* Walk past newline */
310 /* Skip '\n' in CR LF */
311 if (*(dptr - 1) == '\r' && *dptr == '\n') {
316 /* Continuation line? */
317 if (*dptr != ' ' && *dptr != '\t')
320 /* skip leading whitespace */
321 for (; dptr < limit; dptr++) {
322 if (*dptr != ' ' && *dptr != '\t')
328 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
330 for (; dptr < limit; dptr++) {
333 if (*dptr != '\r' && *dptr != '\n')
335 dptr = sip_follow_continuation(dptr, limit);
342 /* Search within a SIP header value, dealing with continuation lines */
343 static const char *ct_sip_header_search(const char *dptr, const char *limit,
344 const char *needle, unsigned int len)
346 for (limit -= len; dptr < limit; dptr++) {
347 if (*dptr == '\r' || *dptr == '\n') {
348 dptr = sip_follow_continuation(dptr, limit);
354 if (strnicmp(dptr, needle, len) == 0)
360 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
361 unsigned int dataoff, unsigned int datalen,
362 enum sip_header_types type,
363 unsigned int *matchoff, unsigned int *matchlen)
365 const struct sip_header *hdr = &ct_sip_hdrs[type];
366 const char *start = dptr, *limit = dptr + datalen;
369 for (dptr += dataoff; dptr < limit; dptr++) {
370 /* Find beginning of line */
371 if (*dptr != '\r' && *dptr != '\n')
375 if (*(dptr - 1) == '\r' && *dptr == '\n') {
380 /* Skip continuation lines */
381 if (*dptr == ' ' || *dptr == '\t')
384 /* Find header. Compact headers must be followed by a
385 * non-alphabetic character to avoid mismatches. */
386 if (limit - dptr >= hdr->len &&
387 strnicmp(dptr, hdr->name, hdr->len) == 0)
389 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
390 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
391 !isalpha(*(dptr + hdr->clen)))
396 /* Find and skip colon */
397 dptr = sip_skip_whitespace(dptr, limit);
400 if (*dptr != ':' || ++dptr >= limit)
403 /* Skip whitespace after colon */
404 dptr = sip_skip_whitespace(dptr, limit);
408 *matchoff = dptr - start;
410 dptr = ct_sip_header_search(dptr, limit, hdr->search,
417 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
420 *matchoff = dptr - start + shift;
425 EXPORT_SYMBOL_GPL(ct_sip_get_header);
427 /* Get next header field in a list of comma separated values */
428 static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
429 unsigned int dataoff, unsigned int datalen,
430 enum sip_header_types type,
431 unsigned int *matchoff, unsigned int *matchlen)
433 const struct sip_header *hdr = &ct_sip_hdrs[type];
434 const char *start = dptr, *limit = dptr + datalen;
439 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
443 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
448 *matchoff = dptr - start;
449 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
456 /* Walk through headers until a parsable one is found or no header of the
457 * given type is left. */
458 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
459 unsigned int dataoff, unsigned int datalen,
460 enum sip_header_types type, int *in_header,
461 unsigned int *matchoff, unsigned int *matchlen)
465 if (in_header && *in_header) {
467 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
468 type, matchoff, matchlen);
473 dataoff += *matchoff;
479 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
480 type, matchoff, matchlen);
485 dataoff += *matchoff;
493 /* Locate a SIP header, parse the URI and return the offset and length of
494 * the address as well as the address and port themselves. A stream of
495 * headers can be parsed by handing in a non-NULL datalen and in_header
498 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
499 unsigned int *dataoff, unsigned int datalen,
500 enum sip_header_types type, int *in_header,
501 unsigned int *matchoff, unsigned int *matchlen,
502 union nf_inet_addr *addr, __be16 *port)
504 const char *c, *limit = dptr + datalen;
508 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
509 type, in_header, matchoff, matchlen);
514 if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit))
518 p = simple_strtoul(c, (char **)&c, 10);
519 if (p < 1024 || p > 65535)
523 *port = htons(SIP_PORT);
529 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
531 static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
532 unsigned int dataoff, unsigned int datalen,
534 unsigned int *matchoff, unsigned int *matchlen)
536 const char *limit = dptr + datalen;
540 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
542 limit = dptr + datalen;
544 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
547 start += strlen(name);
549 end = ct_sip_header_search(start, limit, ";", strlen(";"));
553 *matchoff = start - dptr;
554 *matchlen = end - start;
558 /* Parse address from header parameter and return address, offset and length */
559 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
560 unsigned int dataoff, unsigned int datalen,
562 unsigned int *matchoff, unsigned int *matchlen,
563 union nf_inet_addr *addr)
565 const char *limit = dptr + datalen;
566 const char *start, *end;
568 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
570 limit = dptr + datalen;
572 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
576 start += strlen(name);
577 if (!parse_addr(ct, start, &end, addr, limit))
579 *matchoff = start - dptr;
580 *matchlen = end - start;
583 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
585 /* Parse numerical header parameter and return value, offset and length */
586 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
587 unsigned int dataoff, unsigned int datalen,
589 unsigned int *matchoff, unsigned int *matchlen,
592 const char *limit = dptr + datalen;
596 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
598 limit = dptr + datalen;
600 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
604 start += strlen(name);
605 *val = simple_strtoul(start, &end, 0);
608 if (matchoff && matchlen) {
609 *matchoff = start - dptr;
610 *matchlen = end - start;
614 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
616 static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
617 unsigned int dataoff, unsigned int datalen,
620 unsigned int matchoff, matchlen;
622 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
623 &matchoff, &matchlen)) {
624 if (!strnicmp(dptr + matchoff, "TCP", strlen("TCP")))
625 *proto = IPPROTO_TCP;
626 else if (!strnicmp(dptr + matchoff, "UDP", strlen("UDP")))
627 *proto = IPPROTO_UDP;
631 if (*proto != nf_ct_protonum(ct))
634 *proto = nf_ct_protonum(ct);
639 /* SDP header parsing: a SDP session description contains an ordered set of
640 * headers, starting with a section containing general session parameters,
641 * optionally followed by multiple media descriptions.
643 * SDP headers always start at the beginning of a line. According to RFC 2327:
644 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
645 * be tolerant and also accept records terminated with a single newline
646 * character". We handle both cases.
648 static const struct sip_header ct_sdp_hdrs[] = {
649 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
650 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
651 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
652 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
653 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
654 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
657 /* Linear string search within SDP header values */
658 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
659 const char *needle, unsigned int len)
661 for (limit -= len; dptr < limit; dptr++) {
662 if (*dptr == '\r' || *dptr == '\n')
664 if (strncmp(dptr, needle, len) == 0)
670 /* Locate a SDP header (optionally a substring within the header value),
671 * optionally stopping at the first occurence of the term header, parse
672 * it and return the offset and length of the data we're interested in.
674 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
675 unsigned int dataoff, unsigned int datalen,
676 enum sdp_header_types type,
677 enum sdp_header_types term,
678 unsigned int *matchoff, unsigned int *matchlen)
680 const struct sip_header *hdr = &ct_sdp_hdrs[type];
681 const struct sip_header *thdr = &ct_sdp_hdrs[term];
682 const char *start = dptr, *limit = dptr + datalen;
685 for (dptr += dataoff; dptr < limit; dptr++) {
686 /* Find beginning of line */
687 if (*dptr != '\r' && *dptr != '\n')
691 if (*(dptr - 1) == '\r' && *dptr == '\n') {
696 if (term != SDP_HDR_UNSPEC &&
697 limit - dptr >= thdr->len &&
698 strnicmp(dptr, thdr->name, thdr->len) == 0)
700 else if (limit - dptr >= hdr->len &&
701 strnicmp(dptr, hdr->name, hdr->len) == 0)
706 *matchoff = dptr - start;
708 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
715 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
718 *matchoff = dptr - start + shift;
723 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
725 static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
726 unsigned int dataoff, unsigned int datalen,
727 enum sdp_header_types type,
728 enum sdp_header_types term,
729 unsigned int *matchoff, unsigned int *matchlen,
730 union nf_inet_addr *addr)
734 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
739 if (!parse_addr(ct, dptr + *matchoff, NULL, addr,
740 dptr + *matchoff + *matchlen))
745 static int refresh_signalling_expectation(struct nf_conn *ct,
746 union nf_inet_addr *addr,
747 u8 proto, __be16 port,
748 unsigned int expires)
750 struct nf_conn_help *help = nfct_help(ct);
751 struct nf_conntrack_expect *exp;
752 struct hlist_node *n, *next;
755 spin_lock_bh(&nf_conntrack_lock);
756 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
757 if (exp->class != SIP_EXPECT_SIGNALLING ||
758 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
759 exp->tuple.dst.protonum != proto ||
760 exp->tuple.dst.u.udp.port != port)
762 if (!del_timer(&exp->timeout))
764 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
765 exp->timeout.expires = jiffies + expires * HZ;
766 add_timer(&exp->timeout);
770 spin_unlock_bh(&nf_conntrack_lock);
774 static void flush_expectations(struct nf_conn *ct, bool media)
776 struct nf_conn_help *help = nfct_help(ct);
777 struct nf_conntrack_expect *exp;
778 struct hlist_node *n, *next;
780 spin_lock_bh(&nf_conntrack_lock);
781 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
782 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
784 if (!del_timer(&exp->timeout))
786 nf_ct_unlink_expect(exp);
787 nf_ct_expect_put(exp);
791 spin_unlock_bh(&nf_conntrack_lock);
794 static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int dataoff,
795 const char **dptr, unsigned int *datalen,
796 union nf_inet_addr *daddr, __be16 port,
797 enum sip_expectation_classes class,
798 unsigned int mediaoff, unsigned int medialen)
800 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
801 enum ip_conntrack_info ctinfo;
802 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
803 struct net *net = nf_ct_net(ct);
804 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
805 union nf_inet_addr *saddr;
806 struct nf_conntrack_tuple tuple;
807 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
809 __be16 rtp_port, rtcp_port;
810 typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port;
811 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
814 if (sip_direct_media) {
815 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
817 saddr = &ct->tuplehash[!dir].tuple.src.u3;
820 /* We need to check whether the registration exists before attempting
821 * to register it since we can see the same media description multiple
822 * times on different connections in case multiple endpoints receive
825 * RTP optimization: if we find a matching media channel expectation
826 * and both the expectation and this connection are SNATed, we assume
827 * both sides can reach each other directly and use the final
828 * destination address from the expectation. We still need to keep
829 * the NATed expectations for media that might arrive from the
830 * outside, and additionally need to expect the direct RTP stream
831 * in case it passes through us even without NAT.
833 memset(&tuple, 0, sizeof(tuple));
835 tuple.src.u3 = *saddr;
836 tuple.src.l3num = nf_ct_l3num(ct);
837 tuple.dst.protonum = IPPROTO_UDP;
838 tuple.dst.u3 = *daddr;
839 tuple.dst.u.udp.port = port;
843 exp = __nf_ct_expect_find(net, nf_ct_zone(ct), &tuple);
845 if (!exp || exp->master == ct ||
846 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
849 #ifdef CONFIG_NF_NAT_NEEDED
850 if (exp->tuple.src.l3num == AF_INET && !direct_rtp &&
851 (exp->saved_ip != exp->tuple.dst.u3.ip ||
852 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
853 ct->status & IPS_NAT_MASK) {
854 daddr->ip = exp->saved_ip;
855 tuple.dst.u3.ip = exp->saved_ip;
856 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
861 } while (!skip_expect);
864 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
865 rtp_port = htons(base_port);
866 rtcp_port = htons(base_port + 1);
869 nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook);
870 if (nf_nat_sdp_port &&
871 !nf_nat_sdp_port(skb, dataoff, dptr, datalen,
872 mediaoff, medialen, ntohs(rtp_port)))
879 rtp_exp = nf_ct_expect_alloc(ct);
882 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
883 IPPROTO_UDP, NULL, &rtp_port);
885 rtcp_exp = nf_ct_expect_alloc(ct);
886 if (rtcp_exp == NULL)
888 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
889 IPPROTO_UDP, NULL, &rtcp_port);
891 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook);
892 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp)
893 ret = nf_nat_sdp_media(skb, dataoff, dptr, datalen,
895 mediaoff, medialen, daddr);
897 if (nf_ct_expect_related(rtp_exp) == 0) {
898 if (nf_ct_expect_related(rtcp_exp) != 0)
899 nf_ct_unexpect_related(rtp_exp);
904 nf_ct_expect_put(rtcp_exp);
906 nf_ct_expect_put(rtp_exp);
911 static const struct sdp_media_type sdp_media_types[] = {
912 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
913 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
914 SDP_MEDIA_TYPE("image ", SIP_EXPECT_IMAGE),
917 static const struct sdp_media_type *sdp_media_type(const char *dptr,
918 unsigned int matchoff,
919 unsigned int matchlen)
921 const struct sdp_media_type *t;
924 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
925 t = &sdp_media_types[i];
926 if (matchlen < t->len ||
927 strncmp(dptr + matchoff, t->name, t->len))
934 static int process_sdp(struct sk_buff *skb, unsigned int dataoff,
935 const char **dptr, unsigned int *datalen,
938 enum ip_conntrack_info ctinfo;
939 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
940 unsigned int matchoff, matchlen;
941 unsigned int mediaoff, medialen;
943 unsigned int caddr_len, maddr_len;
945 union nf_inet_addr caddr, maddr, rtp_addr;
947 enum sdp_header_types c_hdr;
948 const struct sdp_media_type *t;
950 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
951 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
953 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook);
954 c_hdr = nf_ct_l3num(ct) == AF_INET ? SDP_HDR_CONNECTION_IP4 :
955 SDP_HDR_CONNECTION_IP6;
957 /* Find beginning of session description */
958 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
959 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
960 &matchoff, &matchlen) <= 0)
964 /* The connection information is contained in the session description
965 * and/or once per media description. The first media description marks
966 * the end of the session description. */
968 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
969 c_hdr, SDP_HDR_MEDIA,
970 &matchoff, &matchlen, &caddr) > 0)
971 caddr_len = matchlen;
974 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
975 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
976 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
977 &mediaoff, &medialen) <= 0)
980 /* Get media type and port number. A media port value of zero
981 * indicates an inactive stream. */
982 t = sdp_media_type(*dptr, mediaoff, medialen);
984 mediaoff += medialen;
990 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
993 if (port < 1024 || port > 65535)
996 /* The media description overrides the session description. */
998 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
999 c_hdr, SDP_HDR_MEDIA,
1000 &matchoff, &matchlen, &maddr) > 0) {
1001 maddr_len = matchlen;
1002 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
1003 } else if (caddr_len)
1004 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
1008 ret = set_expected_rtp_rtcp(skb, dataoff, dptr, datalen,
1009 &rtp_addr, htons(port), t->class,
1010 mediaoff, medialen);
1011 if (ret != NF_ACCEPT)
1014 /* Update media connection address if present */
1015 if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) {
1016 ret = nf_nat_sdp_addr(skb, dataoff, dptr, datalen,
1017 mediaoff, c_hdr, SDP_HDR_MEDIA,
1019 if (ret != NF_ACCEPT)
1025 /* Update session connection and owner addresses */
1026 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook);
1027 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK)
1028 ret = nf_nat_sdp_session(skb, dataoff, dptr, datalen, sdpoff,
1033 static int process_invite_response(struct sk_buff *skb, unsigned int dataoff,
1034 const char **dptr, unsigned int *datalen,
1035 unsigned int cseq, unsigned int code)
1037 enum ip_conntrack_info ctinfo;
1038 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1039 struct nf_conn_help *help = nfct_help(ct);
1041 if ((code >= 100 && code <= 199) ||
1042 (code >= 200 && code <= 299))
1043 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1044 else if (help->help.ct_sip_info.invite_cseq == cseq)
1045 flush_expectations(ct, true);
1049 static int process_update_response(struct sk_buff *skb, unsigned int dataoff,
1050 const char **dptr, unsigned int *datalen,
1051 unsigned int cseq, unsigned int code)
1053 enum ip_conntrack_info ctinfo;
1054 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1055 struct nf_conn_help *help = nfct_help(ct);
1057 if ((code >= 100 && code <= 199) ||
1058 (code >= 200 && code <= 299))
1059 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1060 else if (help->help.ct_sip_info.invite_cseq == cseq)
1061 flush_expectations(ct, true);
1065 static int process_prack_response(struct sk_buff *skb, unsigned int dataoff,
1066 const char **dptr, unsigned int *datalen,
1067 unsigned int cseq, unsigned int code)
1069 enum ip_conntrack_info ctinfo;
1070 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1071 struct nf_conn_help *help = nfct_help(ct);
1073 if ((code >= 100 && code <= 199) ||
1074 (code >= 200 && code <= 299))
1075 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1076 else if (help->help.ct_sip_info.invite_cseq == cseq)
1077 flush_expectations(ct, true);
1081 static int process_invite_request(struct sk_buff *skb, unsigned int dataoff,
1082 const char **dptr, unsigned int *datalen,
1085 enum ip_conntrack_info ctinfo;
1086 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1087 struct nf_conn_help *help = nfct_help(ct);
1090 flush_expectations(ct, true);
1091 ret = process_sdp(skb, dataoff, dptr, datalen, cseq);
1092 if (ret == NF_ACCEPT)
1093 help->help.ct_sip_info.invite_cseq = cseq;
1097 static int process_bye_request(struct sk_buff *skb, unsigned int dataoff,
1098 const char **dptr, unsigned int *datalen,
1101 enum ip_conntrack_info ctinfo;
1102 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1104 flush_expectations(ct, true);
1108 /* Parse a REGISTER request and create a permanent expectation for incoming
1109 * signalling connections. The expectation is marked inactive and is activated
1110 * when receiving a response indicating success from the registrar.
1112 static int process_register_request(struct sk_buff *skb, unsigned int dataoff,
1113 const char **dptr, unsigned int *datalen,
1116 enum ip_conntrack_info ctinfo;
1117 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1118 struct nf_conn_help *help = nfct_help(ct);
1119 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1120 unsigned int matchoff, matchlen;
1121 struct nf_conntrack_expect *exp;
1122 union nf_inet_addr *saddr, daddr;
1125 unsigned int expires = 0;
1127 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
1129 /* Expected connections can not register again. */
1130 if (ct->status & IPS_EXPECTED)
1133 /* We must check the expiration time: a value of zero signals the
1134 * registrar to release the binding. We'll remove our expectation
1135 * when receiving the new bindings in the response, but we don't
1136 * want to create new ones.
1138 * The expiration time may be contained in Expires: header, the
1139 * Contact: header parameters or the URI parameters.
1141 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1142 &matchoff, &matchlen) > 0)
1143 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1145 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1146 SIP_HDR_CONTACT, NULL,
1147 &matchoff, &matchlen, &daddr, &port);
1153 /* We don't support third-party registrations */
1154 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1157 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1161 if (ct_sip_parse_numerical_param(ct, *dptr,
1162 matchoff + matchlen, *datalen,
1163 "expires=", NULL, NULL, &expires) < 0)
1171 exp = nf_ct_expect_alloc(ct);
1176 if (sip_direct_signalling)
1177 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1179 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1180 saddr, &daddr, proto, NULL, &port);
1181 exp->timeout.expires = sip_timeout * HZ;
1182 exp->helper = nfct_help(ct)->helper;
1183 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1185 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook);
1186 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK)
1187 ret = nf_nat_sip_expect(skb, dataoff, dptr, datalen, exp,
1188 matchoff, matchlen);
1190 if (nf_ct_expect_related(exp) != 0)
1195 nf_ct_expect_put(exp);
1198 if (ret == NF_ACCEPT)
1199 help->help.ct_sip_info.register_cseq = cseq;
1203 static int process_register_response(struct sk_buff *skb, unsigned int dataoff,
1204 const char **dptr, unsigned int *datalen,
1205 unsigned int cseq, unsigned int code)
1207 enum ip_conntrack_info ctinfo;
1208 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1209 struct nf_conn_help *help = nfct_help(ct);
1210 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1211 union nf_inet_addr addr;
1214 unsigned int matchoff, matchlen, coff = 0;
1215 unsigned int expires = 0;
1216 int in_contact = 0, ret;
1218 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1219 * they have received a final response from the registrar for the
1220 * previous one or the previous REGISTER request has timed out".
1222 * However, some servers fail to detect retransmissions and send late
1223 * responses, so we store the sequence number of the last valid
1224 * request and compare it here.
1226 if (help->help.ct_sip_info.register_cseq != cseq)
1229 if (code >= 100 && code <= 199)
1231 if (code < 200 || code > 299)
1234 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1235 &matchoff, &matchlen) > 0)
1236 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1239 unsigned int c_expires = expires;
1241 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
1242 SIP_HDR_CONTACT, &in_contact,
1243 &matchoff, &matchlen,
1250 /* We don't support third-party registrations */
1251 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1254 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1255 *datalen, &proto) == 0)
1258 ret = ct_sip_parse_numerical_param(ct, *dptr,
1259 matchoff + matchlen,
1260 *datalen, "expires=",
1261 NULL, NULL, &c_expires);
1266 if (refresh_signalling_expectation(ct, &addr, proto, port,
1272 flush_expectations(ct, false);
1276 static const struct sip_handler sip_handlers[] = {
1277 SIP_HANDLER("INVITE", process_invite_request, process_invite_response),
1278 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1279 SIP_HANDLER("ACK", process_sdp, NULL),
1280 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1281 SIP_HANDLER("BYE", process_bye_request, NULL),
1282 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1285 static int process_sip_response(struct sk_buff *skb, unsigned int dataoff,
1286 const char **dptr, unsigned int *datalen)
1288 enum ip_conntrack_info ctinfo;
1289 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1290 unsigned int matchoff, matchlen, matchend;
1291 unsigned int code, cseq, i;
1293 if (*datalen < strlen("SIP/2.0 200"))
1295 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1299 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1300 &matchoff, &matchlen) <= 0)
1302 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1305 matchend = matchoff + matchlen + 1;
1307 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1308 const struct sip_handler *handler;
1310 handler = &sip_handlers[i];
1311 if (handler->response == NULL)
1313 if (*datalen < matchend + handler->len ||
1314 strnicmp(*dptr + matchend, handler->method, handler->len))
1316 return handler->response(skb, dataoff, dptr, datalen,
1322 static int process_sip_request(struct sk_buff *skb, unsigned int dataoff,
1323 const char **dptr, unsigned int *datalen)
1325 enum ip_conntrack_info ctinfo;
1326 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1327 unsigned int matchoff, matchlen;
1328 unsigned int cseq, i;
1330 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1331 const struct sip_handler *handler;
1333 handler = &sip_handlers[i];
1334 if (handler->request == NULL)
1336 if (*datalen < handler->len ||
1337 strnicmp(*dptr, handler->method, handler->len))
1340 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1341 &matchoff, &matchlen) <= 0)
1343 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1347 return handler->request(skb, dataoff, dptr, datalen, cseq);
1352 static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1353 unsigned int dataoff, const char **dptr,
1354 unsigned int *datalen)
1356 typeof(nf_nat_sip_hook) nf_nat_sip;
1359 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1360 ret = process_sip_request(skb, dataoff, dptr, datalen);
1362 ret = process_sip_response(skb, dataoff, dptr, datalen);
1364 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1365 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
1366 if (nf_nat_sip && !nf_nat_sip(skb, dataoff, dptr, datalen))
1373 static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1374 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1376 struct tcphdr *th, _tcph;
1377 unsigned int dataoff, datalen;
1378 unsigned int matchoff, matchlen, clen;
1379 unsigned int msglen, origlen;
1380 const char *dptr, *end;
1381 s16 diff, tdiff = 0;
1383 typeof(nf_nat_sip_seq_adjust_hook) nf_nat_sip_seq_adjust;
1385 if (ctinfo != IP_CT_ESTABLISHED &&
1386 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
1390 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1393 dataoff = protoff + th->doff * 4;
1394 if (dataoff >= skb->len)
1397 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1399 if (unlikely(skb_linearize(skb)))
1402 dptr = skb->data + dataoff;
1403 datalen = skb->len - dataoff;
1404 if (datalen < strlen("SIP/2.0 200"))
1408 if (ct_sip_get_header(ct, dptr, 0, datalen,
1409 SIP_HDR_CONTENT_LENGTH,
1410 &matchoff, &matchlen) <= 0)
1413 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1414 if (dptr + matchoff == end)
1417 if (end + strlen("\r\n\r\n") > dptr + datalen)
1419 if (end[0] != '\r' || end[1] != '\n' ||
1420 end[2] != '\r' || end[3] != '\n')
1422 end += strlen("\r\n\r\n") + clen;
1424 msglen = origlen = end - dptr;
1426 ret = process_sip_msg(skb, ct, dataoff, &dptr, &msglen);
1427 if (ret != NF_ACCEPT)
1429 diff = msglen - origlen;
1434 datalen = datalen + diff - msglen;
1437 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1438 nf_nat_sip_seq_adjust = rcu_dereference(nf_nat_sip_seq_adjust_hook);
1439 if (nf_nat_sip_seq_adjust)
1440 nf_nat_sip_seq_adjust(skb, tdiff);
1446 static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1447 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1449 unsigned int dataoff, datalen;
1453 dataoff = protoff + sizeof(struct udphdr);
1454 if (dataoff >= skb->len)
1457 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1459 if (unlikely(skb_linearize(skb)))
1462 dptr = skb->data + dataoff;
1463 datalen = skb->len - dataoff;
1464 if (datalen < strlen("SIP/2.0 200"))
1467 return process_sip_msg(skb, ct, dataoff, &dptr, &datalen);
1470 static struct nf_conntrack_helper sip[MAX_PORTS][4] __read_mostly;
1471 static char sip_names[MAX_PORTS][4][sizeof("sip-65535")] __read_mostly;
1473 static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1474 [SIP_EXPECT_SIGNALLING] = {
1475 .name = "signalling",
1479 [SIP_EXPECT_AUDIO] = {
1481 .max_expected = 2 * IP_CT_DIR_MAX,
1484 [SIP_EXPECT_VIDEO] = {
1486 .max_expected = 2 * IP_CT_DIR_MAX,
1489 [SIP_EXPECT_IMAGE] = {
1491 .max_expected = IP_CT_DIR_MAX,
1496 static void nf_conntrack_sip_fini(void)
1500 for (i = 0; i < ports_c; i++) {
1501 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1502 if (sip[i][j].me == NULL)
1504 nf_conntrack_helper_unregister(&sip[i][j]);
1509 static int __init nf_conntrack_sip_init(void)
1515 ports[ports_c++] = SIP_PORT;
1517 for (i = 0; i < ports_c; i++) {
1518 memset(&sip[i], 0, sizeof(sip[i]));
1520 sip[i][0].tuple.src.l3num = AF_INET;
1521 sip[i][0].tuple.dst.protonum = IPPROTO_UDP;
1522 sip[i][0].help = sip_help_udp;
1523 sip[i][1].tuple.src.l3num = AF_INET;
1524 sip[i][1].tuple.dst.protonum = IPPROTO_TCP;
1525 sip[i][1].help = sip_help_tcp;
1527 sip[i][2].tuple.src.l3num = AF_INET6;
1528 sip[i][2].tuple.dst.protonum = IPPROTO_UDP;
1529 sip[i][2].help = sip_help_udp;
1530 sip[i][3].tuple.src.l3num = AF_INET6;
1531 sip[i][3].tuple.dst.protonum = IPPROTO_TCP;
1532 sip[i][3].help = sip_help_tcp;
1534 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1535 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
1536 sip[i][j].expect_policy = sip_exp_policy;
1537 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
1538 sip[i][j].me = THIS_MODULE;
1540 tmpname = &sip_names[i][j][0];
1541 if (ports[i] == SIP_PORT)
1542 sprintf(tmpname, "sip");
1544 sprintf(tmpname, "sip-%u", i);
1545 sip[i][j].name = tmpname;
1547 pr_debug("port #%u: %u\n", i, ports[i]);
1549 ret = nf_conntrack_helper_register(&sip[i][j]);
1551 printk(KERN_ERR "nf_ct_sip: failed to register"
1552 " helper for pf: %u port: %u\n",
1553 sip[i][j].tuple.src.l3num, ports[i]);
1554 nf_conntrack_sip_fini();
1562 module_init(nf_conntrack_sip_init);
1563 module_exit(nf_conntrack_sip_fini);