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 <linux/netfilter/nf_conntrack_sip.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Christian Hentschel <chentschel@arnet.com.ar>");
30 MODULE_DESCRIPTION("SIP connection tracking helper");
31 MODULE_ALIAS("ip_conntrack_sip");
32 MODULE_ALIAS_NFCT_HELPER("sip");
35 static unsigned short ports[MAX_PORTS];
36 static unsigned int ports_c;
37 module_param_array(ports, ushort, &ports_c, 0400);
38 MODULE_PARM_DESC(ports, "port numbers of SIP servers");
40 static unsigned int sip_timeout __read_mostly = SIP_TIMEOUT;
41 module_param(sip_timeout, uint, 0600);
42 MODULE_PARM_DESC(sip_timeout, "timeout for the master SIP session");
44 static int sip_direct_signalling __read_mostly = 1;
45 module_param(sip_direct_signalling, int, 0600);
46 MODULE_PARM_DESC(sip_direct_signalling, "expect incoming calls from registrar "
49 static int sip_direct_media __read_mostly = 1;
50 module_param(sip_direct_media, int, 0600);
51 MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
52 "endpoints only (default 1)");
54 unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, unsigned int dataoff,
56 unsigned int *datalen) __read_mostly;
57 EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
59 unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb,
62 unsigned int *datalen,
63 struct nf_conntrack_expect *exp,
64 unsigned int matchoff,
65 unsigned int matchlen) __read_mostly;
66 EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook);
68 unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb, unsigned int dataoff,
70 unsigned int *datalen,
72 enum sdp_header_types type,
73 enum sdp_header_types term,
74 const union nf_inet_addr *addr)
76 EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook);
78 unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb, unsigned int dataoff,
80 unsigned int *datalen,
81 unsigned int matchoff,
82 unsigned int matchlen,
83 u_int16_t port) __read_mostly;
84 EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook);
86 unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb,
89 unsigned int *datalen,
91 const union nf_inet_addr *addr)
93 EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook);
95 unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb, unsigned int dataoff,
97 unsigned int *datalen,
98 struct nf_conntrack_expect *rtp_exp,
99 struct nf_conntrack_expect *rtcp_exp,
100 unsigned int mediaoff,
101 unsigned int medialen,
102 union nf_inet_addr *rtp_addr)
104 EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook);
106 static int string_len(const struct nf_conn *ct, const char *dptr,
107 const char *limit, int *shift)
111 while (dptr < limit && isalpha(*dptr)) {
118 static int digits_len(const struct nf_conn *ct, const char *dptr,
119 const char *limit, int *shift)
122 while (dptr < limit && isdigit(*dptr)) {
129 /* get media type + port length */
130 static int media_len(const struct nf_conn *ct, const char *dptr,
131 const char *limit, int *shift)
133 int len = string_len(ct, dptr, limit, shift);
136 if (dptr >= limit || *dptr != ' ')
141 return len + digits_len(ct, dptr, limit, shift);
144 static int parse_addr(const struct nf_conn *ct, const char *cp,
145 const char **endp, union nf_inet_addr *addr,
151 memset(addr, 0, sizeof(*addr));
152 switch (nf_ct_l3num(ct)) {
154 ret = in4_pton(cp, limit - cp, (u8 *)&addr->ip, -1, &end);
157 ret = in6_pton(cp, limit - cp, (u8 *)&addr->ip6, -1, &end);
163 if (ret == 0 || end == cp)
170 /* skip ip address. returns its length. */
171 static int epaddr_len(const struct nf_conn *ct, const char *dptr,
172 const char *limit, int *shift)
174 union nf_inet_addr addr;
175 const char *aux = dptr;
177 if (!parse_addr(ct, dptr, &dptr, &addr, limit)) {
178 pr_debug("ip: %s parse failed.!\n", dptr);
185 dptr += digits_len(ct, dptr, limit, shift);
190 /* get address length, skiping user info. */
191 static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
192 const char *limit, int *shift)
194 const char *start = dptr;
197 /* Search for @, but stop at the end of the line.
198 * We are inside a sip: URI, so we don't need to worry about
199 * continuation lines. */
200 while (dptr < limit &&
201 *dptr != '@' && *dptr != '\r' && *dptr != '\n') {
206 if (dptr < limit && *dptr == '@') {
214 return epaddr_len(ct, dptr, limit, shift);
217 /* Parse a SIP request line of the form:
219 * Request-Line = Method SP Request-URI SP SIP-Version CRLF
221 * and return the offset and length of the address contained in the Request-URI.
223 int ct_sip_parse_request(const struct nf_conn *ct,
224 const char *dptr, unsigned int datalen,
225 unsigned int *matchoff, unsigned int *matchlen,
226 union nf_inet_addr *addr, __be16 *port)
228 const char *start = dptr, *limit = dptr + datalen, *end;
233 /* Skip method and following whitespace */
234 mlen = string_len(ct, dptr, limit, NULL);
242 for (; dptr < limit - strlen("sip:"); dptr++) {
243 if (*dptr == '\r' || *dptr == '\n')
245 if (strnicmp(dptr, "sip:", strlen("sip:")) == 0) {
246 dptr += strlen("sip:");
250 if (!skp_epaddr_len(ct, dptr, limit, &shift))
254 if (!parse_addr(ct, dptr, &end, addr, limit))
256 if (end < limit && *end == ':') {
258 p = simple_strtoul(end, (char **)&end, 10);
259 if (p < 1024 || p > 65535)
263 *port = htons(SIP_PORT);
267 *matchoff = dptr - start;
268 *matchlen = end - dptr;
271 EXPORT_SYMBOL_GPL(ct_sip_parse_request);
273 /* SIP header parsing: SIP headers are located at the beginning of a line, but
274 * may span several lines, in which case the continuation lines begin with a
275 * whitespace character. RFC 2543 allows lines to be terminated with CR, LF or
276 * CRLF, RFC 3261 allows only CRLF, we support both.
278 * Headers are followed by (optionally) whitespace, a colon, again (optionally)
279 * whitespace and the values. Whitespace in this context means any amount of
280 * tabs, spaces and continuation lines, which are treated as a single whitespace
283 * Some headers may appear multiple times. A comma seperated list of values is
284 * equivalent to multiple headers.
286 static const struct sip_header ct_sip_hdrs[] = {
287 [SIP_HDR_CSEQ] = SIP_HDR("CSeq", NULL, NULL, digits_len),
288 [SIP_HDR_FROM] = SIP_HDR("From", "f", "sip:", skp_epaddr_len),
289 [SIP_HDR_TO] = SIP_HDR("To", "t", "sip:", skp_epaddr_len),
290 [SIP_HDR_CONTACT] = SIP_HDR("Contact", "m", "sip:", skp_epaddr_len),
291 [SIP_HDR_VIA_UDP] = SIP_HDR("Via", "v", "UDP ", epaddr_len),
292 [SIP_HDR_VIA_TCP] = SIP_HDR("Via", "v", "TCP ", epaddr_len),
293 [SIP_HDR_EXPIRES] = SIP_HDR("Expires", NULL, NULL, digits_len),
294 [SIP_HDR_CONTENT_LENGTH] = SIP_HDR("Content-Length", "l", NULL, digits_len),
297 static const char *sip_follow_continuation(const char *dptr, const char *limit)
299 /* Walk past newline */
303 /* Skip '\n' in CR LF */
304 if (*(dptr - 1) == '\r' && *dptr == '\n') {
309 /* Continuation line? */
310 if (*dptr != ' ' && *dptr != '\t')
313 /* skip leading whitespace */
314 for (; dptr < limit; dptr++) {
315 if (*dptr != ' ' && *dptr != '\t')
321 static const char *sip_skip_whitespace(const char *dptr, const char *limit)
323 for (; dptr < limit; dptr++) {
326 if (*dptr != '\r' && *dptr != '\n')
328 dptr = sip_follow_continuation(dptr, limit);
335 /* Search within a SIP header value, dealing with continuation lines */
336 static const char *ct_sip_header_search(const char *dptr, const char *limit,
337 const char *needle, unsigned int len)
339 for (limit -= len; dptr < limit; dptr++) {
340 if (*dptr == '\r' || *dptr == '\n') {
341 dptr = sip_follow_continuation(dptr, limit);
347 if (strnicmp(dptr, needle, len) == 0)
353 int ct_sip_get_header(const struct nf_conn *ct, const char *dptr,
354 unsigned int dataoff, unsigned int datalen,
355 enum sip_header_types type,
356 unsigned int *matchoff, unsigned int *matchlen)
358 const struct sip_header *hdr = &ct_sip_hdrs[type];
359 const char *start = dptr, *limit = dptr + datalen;
362 for (dptr += dataoff; dptr < limit; dptr++) {
363 /* Find beginning of line */
364 if (*dptr != '\r' && *dptr != '\n')
368 if (*(dptr - 1) == '\r' && *dptr == '\n') {
373 /* Skip continuation lines */
374 if (*dptr == ' ' || *dptr == '\t')
377 /* Find header. Compact headers must be followed by a
378 * non-alphabetic character to avoid mismatches. */
379 if (limit - dptr >= hdr->len &&
380 strnicmp(dptr, hdr->name, hdr->len) == 0)
382 else if (hdr->cname && limit - dptr >= hdr->clen + 1 &&
383 strnicmp(dptr, hdr->cname, hdr->clen) == 0 &&
384 !isalpha(*(dptr + hdr->clen)))
389 /* Find and skip colon */
390 dptr = sip_skip_whitespace(dptr, limit);
393 if (*dptr != ':' || ++dptr >= limit)
396 /* Skip whitespace after colon */
397 dptr = sip_skip_whitespace(dptr, limit);
401 *matchoff = dptr - start;
403 dptr = ct_sip_header_search(dptr, limit, hdr->search,
410 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
413 *matchoff = dptr - start + shift;
418 EXPORT_SYMBOL_GPL(ct_sip_get_header);
420 /* Get next header field in a list of comma seperated values */
421 static int ct_sip_next_header(const struct nf_conn *ct, const char *dptr,
422 unsigned int dataoff, unsigned int datalen,
423 enum sip_header_types type,
424 unsigned int *matchoff, unsigned int *matchlen)
426 const struct sip_header *hdr = &ct_sip_hdrs[type];
427 const char *start = dptr, *limit = dptr + datalen;
432 dptr = ct_sip_header_search(dptr, limit, ",", strlen(","));
436 dptr = ct_sip_header_search(dptr, limit, hdr->search, hdr->slen);
441 *matchoff = dptr - start;
442 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
449 /* Walk through headers until a parsable one is found or no header of the
450 * given type is left. */
451 static int ct_sip_walk_headers(const struct nf_conn *ct, const char *dptr,
452 unsigned int dataoff, unsigned int datalen,
453 enum sip_header_types type, int *in_header,
454 unsigned int *matchoff, unsigned int *matchlen)
458 if (in_header && *in_header) {
460 ret = ct_sip_next_header(ct, dptr, dataoff, datalen,
461 type, matchoff, matchlen);
466 dataoff += *matchoff;
472 ret = ct_sip_get_header(ct, dptr, dataoff, datalen,
473 type, matchoff, matchlen);
478 dataoff += *matchoff;
486 /* Locate a SIP header, parse the URI and return the offset and length of
487 * the address as well as the address and port themselves. A stream of
488 * headers can be parsed by handing in a non-NULL datalen and in_header
491 int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
492 unsigned int *dataoff, unsigned int datalen,
493 enum sip_header_types type, int *in_header,
494 unsigned int *matchoff, unsigned int *matchlen,
495 union nf_inet_addr *addr, __be16 *port)
497 const char *c, *limit = dptr + datalen;
501 ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
502 type, in_header, matchoff, matchlen);
507 if (!parse_addr(ct, dptr + *matchoff, &c, addr, limit))
511 p = simple_strtoul(c, (char **)&c, 10);
512 if (p < 1024 || p > 65535)
516 *port = htons(SIP_PORT);
522 EXPORT_SYMBOL_GPL(ct_sip_parse_header_uri);
524 static int ct_sip_parse_param(const struct nf_conn *ct, const char *dptr,
525 unsigned int dataoff, unsigned int datalen,
527 unsigned int *matchoff, unsigned int *matchlen)
529 const char *limit = dptr + datalen;
533 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
535 limit = dptr + datalen;
537 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
540 start += strlen(name);
542 end = ct_sip_header_search(start, limit, ";", strlen(";"));
546 *matchoff = start - dptr;
547 *matchlen = end - start;
551 /* Parse address from header parameter and return address, offset and length */
552 int ct_sip_parse_address_param(const struct nf_conn *ct, const char *dptr,
553 unsigned int dataoff, unsigned int datalen,
555 unsigned int *matchoff, unsigned int *matchlen,
556 union nf_inet_addr *addr)
558 const char *limit = dptr + datalen;
559 const char *start, *end;
561 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
563 limit = dptr + datalen;
565 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
569 start += strlen(name);
570 if (!parse_addr(ct, start, &end, addr, limit))
572 *matchoff = start - dptr;
573 *matchlen = end - start;
576 EXPORT_SYMBOL_GPL(ct_sip_parse_address_param);
578 /* Parse numerical header parameter and return value, offset and length */
579 int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
580 unsigned int dataoff, unsigned int datalen,
582 unsigned int *matchoff, unsigned int *matchlen,
585 const char *limit = dptr + datalen;
589 limit = ct_sip_header_search(dptr + dataoff, limit, ",", strlen(","));
591 limit = dptr + datalen;
593 start = ct_sip_header_search(dptr + dataoff, limit, name, strlen(name));
597 start += strlen(name);
598 *val = simple_strtoul(start, &end, 0);
601 if (matchoff && matchlen) {
602 *matchoff = start - dptr;
603 *matchlen = end - start;
607 EXPORT_SYMBOL_GPL(ct_sip_parse_numerical_param);
609 static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,
610 unsigned int dataoff, unsigned int datalen,
613 unsigned int matchoff, matchlen;
615 if (ct_sip_parse_param(ct, dptr, dataoff, datalen, "transport=",
616 &matchoff, &matchlen)) {
617 if (!strnicmp(dptr + matchoff, "TCP", strlen("TCP")))
618 *proto = IPPROTO_TCP;
619 else if (!strnicmp(dptr + matchoff, "UDP", strlen("UDP")))
620 *proto = IPPROTO_UDP;
624 if (*proto != nf_ct_protonum(ct))
627 *proto = nf_ct_protonum(ct);
632 /* SDP header parsing: a SDP session description contains an ordered set of
633 * headers, starting with a section containing general session parameters,
634 * optionally followed by multiple media descriptions.
636 * SDP headers always start at the beginning of a line. According to RFC 2327:
637 * "The sequence CRLF (0x0d0a) is used to end a record, although parsers should
638 * be tolerant and also accept records terminated with a single newline
639 * character". We handle both cases.
641 static const struct sip_header ct_sdp_hdrs[] = {
642 [SDP_HDR_VERSION] = SDP_HDR("v=", NULL, digits_len),
643 [SDP_HDR_OWNER_IP4] = SDP_HDR("o=", "IN IP4 ", epaddr_len),
644 [SDP_HDR_CONNECTION_IP4] = SDP_HDR("c=", "IN IP4 ", epaddr_len),
645 [SDP_HDR_OWNER_IP6] = SDP_HDR("o=", "IN IP6 ", epaddr_len),
646 [SDP_HDR_CONNECTION_IP6] = SDP_HDR("c=", "IN IP6 ", epaddr_len),
647 [SDP_HDR_MEDIA] = SDP_HDR("m=", NULL, media_len),
650 /* Linear string search within SDP header values */
651 static const char *ct_sdp_header_search(const char *dptr, const char *limit,
652 const char *needle, unsigned int len)
654 for (limit -= len; dptr < limit; dptr++) {
655 if (*dptr == '\r' || *dptr == '\n')
657 if (strncmp(dptr, needle, len) == 0)
663 /* Locate a SDP header (optionally a substring within the header value),
664 * optionally stopping at the first occurence of the term header, parse
665 * it and return the offset and length of the data we're interested in.
667 int ct_sip_get_sdp_header(const struct nf_conn *ct, const char *dptr,
668 unsigned int dataoff, unsigned int datalen,
669 enum sdp_header_types type,
670 enum sdp_header_types term,
671 unsigned int *matchoff, unsigned int *matchlen)
673 const struct sip_header *hdr = &ct_sdp_hdrs[type];
674 const struct sip_header *thdr = &ct_sdp_hdrs[term];
675 const char *start = dptr, *limit = dptr + datalen;
678 for (dptr += dataoff; dptr < limit; dptr++) {
679 /* Find beginning of line */
680 if (*dptr != '\r' && *dptr != '\n')
684 if (*(dptr - 1) == '\r' && *dptr == '\n') {
689 if (term != SDP_HDR_UNSPEC &&
690 limit - dptr >= thdr->len &&
691 strnicmp(dptr, thdr->name, thdr->len) == 0)
693 else if (limit - dptr >= hdr->len &&
694 strnicmp(dptr, hdr->name, hdr->len) == 0)
699 *matchoff = dptr - start;
701 dptr = ct_sdp_header_search(dptr, limit, hdr->search,
708 *matchlen = hdr->match_len(ct, dptr, limit, &shift);
711 *matchoff = dptr - start + shift;
716 EXPORT_SYMBOL_GPL(ct_sip_get_sdp_header);
718 static int ct_sip_parse_sdp_addr(const struct nf_conn *ct, const char *dptr,
719 unsigned int dataoff, unsigned int datalen,
720 enum sdp_header_types type,
721 enum sdp_header_types term,
722 unsigned int *matchoff, unsigned int *matchlen,
723 union nf_inet_addr *addr)
727 ret = ct_sip_get_sdp_header(ct, dptr, dataoff, datalen, type, term,
732 if (!parse_addr(ct, dptr + *matchoff, NULL, addr,
733 dptr + *matchoff + *matchlen))
738 static int refresh_signalling_expectation(struct nf_conn *ct,
739 union nf_inet_addr *addr,
740 u8 proto, __be16 port,
741 unsigned int expires)
743 struct nf_conn_help *help = nfct_help(ct);
744 struct nf_conntrack_expect *exp;
745 struct hlist_node *n, *next;
748 spin_lock_bh(&nf_conntrack_lock);
749 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
750 if (exp->class != SIP_EXPECT_SIGNALLING ||
751 !nf_inet_addr_cmp(&exp->tuple.dst.u3, addr) ||
752 exp->tuple.dst.protonum != proto ||
753 exp->tuple.dst.u.udp.port != port)
755 if (!del_timer(&exp->timeout))
757 exp->flags &= ~NF_CT_EXPECT_INACTIVE;
758 exp->timeout.expires = jiffies + expires * HZ;
759 add_timer(&exp->timeout);
763 spin_unlock_bh(&nf_conntrack_lock);
767 static void flush_expectations(struct nf_conn *ct, bool media)
769 struct nf_conn_help *help = nfct_help(ct);
770 struct nf_conntrack_expect *exp;
771 struct hlist_node *n, *next;
773 spin_lock_bh(&nf_conntrack_lock);
774 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
775 if ((exp->class != SIP_EXPECT_SIGNALLING) ^ media)
777 if (!del_timer(&exp->timeout))
779 nf_ct_unlink_expect(exp);
780 nf_ct_expect_put(exp);
784 spin_unlock_bh(&nf_conntrack_lock);
787 static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int dataoff,
788 const char **dptr, unsigned int *datalen,
789 union nf_inet_addr *daddr, __be16 port,
790 enum sip_expectation_classes class,
791 unsigned int mediaoff, unsigned int medialen)
793 struct nf_conntrack_expect *exp, *rtp_exp, *rtcp_exp;
794 enum ip_conntrack_info ctinfo;
795 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
796 struct net *net = nf_ct_net(ct);
797 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
798 union nf_inet_addr *saddr;
799 struct nf_conntrack_tuple tuple;
800 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
802 __be16 rtp_port, rtcp_port;
803 typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port;
804 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
807 if (sip_direct_media) {
808 if (!nf_inet_addr_cmp(daddr, &ct->tuplehash[dir].tuple.src.u3))
810 saddr = &ct->tuplehash[!dir].tuple.src.u3;
813 /* We need to check whether the registration exists before attempting
814 * to register it since we can see the same media description multiple
815 * times on different connections in case multiple endpoints receive
818 * RTP optimization: if we find a matching media channel expectation
819 * and both the expectation and this connection are SNATed, we assume
820 * both sides can reach each other directly and use the final
821 * destination address from the expectation. We still need to keep
822 * the NATed expectations for media that might arrive from the
823 * outside, and additionally need to expect the direct RTP stream
824 * in case it passes through us even without NAT.
826 memset(&tuple, 0, sizeof(tuple));
828 tuple.src.u3 = *saddr;
829 tuple.src.l3num = nf_ct_l3num(ct);
830 tuple.dst.protonum = IPPROTO_UDP;
831 tuple.dst.u3 = *daddr;
832 tuple.dst.u.udp.port = port;
836 exp = __nf_ct_expect_find(net, &tuple);
838 if (!exp || exp->master == ct ||
839 nfct_help(exp->master)->helper != nfct_help(ct)->helper ||
842 #ifdef CONFIG_NF_NAT_NEEDED
843 if (exp->tuple.src.l3num == AF_INET && !direct_rtp &&
844 (exp->saved_ip != exp->tuple.dst.u3.ip ||
845 exp->saved_proto.udp.port != exp->tuple.dst.u.udp.port) &&
846 ct->status & IPS_NAT_MASK) {
847 daddr->ip = exp->saved_ip;
848 tuple.dst.u3.ip = exp->saved_ip;
849 tuple.dst.u.udp.port = exp->saved_proto.udp.port;
854 } while (!skip_expect);
857 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
858 rtp_port = htons(base_port);
859 rtcp_port = htons(base_port + 1);
862 nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook);
863 if (nf_nat_sdp_port &&
864 !nf_nat_sdp_port(skb, dataoff, dptr, datalen,
865 mediaoff, medialen, ntohs(rtp_port)))
872 rtp_exp = nf_ct_expect_alloc(ct);
875 nf_ct_expect_init(rtp_exp, class, nf_ct_l3num(ct), saddr, daddr,
876 IPPROTO_UDP, NULL, &rtp_port);
878 rtcp_exp = nf_ct_expect_alloc(ct);
879 if (rtcp_exp == NULL)
881 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
882 IPPROTO_UDP, NULL, &rtcp_port);
884 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook);
885 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp)
886 ret = nf_nat_sdp_media(skb, dataoff, dptr, datalen,
888 mediaoff, medialen, daddr);
890 if (nf_ct_expect_related(rtp_exp) == 0) {
891 if (nf_ct_expect_related(rtcp_exp) != 0)
892 nf_ct_unexpect_related(rtp_exp);
897 nf_ct_expect_put(rtcp_exp);
899 nf_ct_expect_put(rtp_exp);
904 static const struct sdp_media_type sdp_media_types[] = {
905 SDP_MEDIA_TYPE("audio ", SIP_EXPECT_AUDIO),
906 SDP_MEDIA_TYPE("video ", SIP_EXPECT_VIDEO),
909 static const struct sdp_media_type *sdp_media_type(const char *dptr,
910 unsigned int matchoff,
911 unsigned int matchlen)
913 const struct sdp_media_type *t;
916 for (i = 0; i < ARRAY_SIZE(sdp_media_types); i++) {
917 t = &sdp_media_types[i];
918 if (matchlen < t->len ||
919 strncmp(dptr + matchoff, t->name, t->len))
926 static int process_sdp(struct sk_buff *skb, unsigned int dataoff,
927 const char **dptr, unsigned int *datalen,
930 enum ip_conntrack_info ctinfo;
931 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
932 struct nf_conn_help *help = nfct_help(ct);
933 unsigned int matchoff, matchlen;
934 unsigned int mediaoff, medialen;
936 unsigned int caddr_len, maddr_len;
938 union nf_inet_addr caddr, maddr, rtp_addr;
940 enum sdp_header_types c_hdr;
941 const struct sdp_media_type *t;
943 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
944 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
946 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook);
947 c_hdr = nf_ct_l3num(ct) == AF_INET ? SDP_HDR_CONNECTION_IP4 :
948 SDP_HDR_CONNECTION_IP6;
950 /* Find beginning of session description */
951 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
952 SDP_HDR_VERSION, SDP_HDR_UNSPEC,
953 &matchoff, &matchlen) <= 0)
957 /* The connection information is contained in the session description
958 * and/or once per media description. The first media description marks
959 * the end of the session description. */
961 if (ct_sip_parse_sdp_addr(ct, *dptr, sdpoff, *datalen,
962 c_hdr, SDP_HDR_MEDIA,
963 &matchoff, &matchlen, &caddr) > 0)
964 caddr_len = matchlen;
967 for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
968 if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
969 SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
970 &mediaoff, &medialen) <= 0)
973 /* Get media type and port number. A media port value of zero
974 * indicates an inactive stream. */
975 t = sdp_media_type(*dptr, mediaoff, medialen);
977 mediaoff += medialen;
983 port = simple_strtoul(*dptr + mediaoff, NULL, 10);
986 if (port < 1024 || port > 65535)
989 /* The media description overrides the session description. */
991 if (ct_sip_parse_sdp_addr(ct, *dptr, mediaoff, *datalen,
992 c_hdr, SDP_HDR_MEDIA,
993 &matchoff, &matchlen, &maddr) > 0) {
994 maddr_len = matchlen;
995 memcpy(&rtp_addr, &maddr, sizeof(rtp_addr));
996 } else if (caddr_len)
997 memcpy(&rtp_addr, &caddr, sizeof(rtp_addr));
1001 ret = set_expected_rtp_rtcp(skb, dataoff, dptr, datalen,
1002 &rtp_addr, htons(port), t->class,
1003 mediaoff, medialen);
1004 if (ret != NF_ACCEPT)
1007 /* Update media connection address if present */
1008 if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) {
1009 ret = nf_nat_sdp_addr(skb, dataoff, dptr, datalen,
1010 mediaoff, c_hdr, SDP_HDR_MEDIA,
1012 if (ret != NF_ACCEPT)
1018 /* Update session connection and owner addresses */
1019 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook);
1020 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK)
1021 ret = nf_nat_sdp_session(skb, dataoff, dptr, datalen, sdpoff,
1024 if (ret == NF_ACCEPT && i > 0)
1025 help->help.ct_sip_info.invite_cseq = cseq;
1029 static int process_invite_response(struct sk_buff *skb, unsigned int dataoff,
1030 const char **dptr, unsigned int *datalen,
1031 unsigned int cseq, unsigned int code)
1033 enum ip_conntrack_info ctinfo;
1034 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1035 struct nf_conn_help *help = nfct_help(ct);
1037 if ((code >= 100 && code <= 199) ||
1038 (code >= 200 && code <= 299))
1039 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1040 else if (help->help.ct_sip_info.invite_cseq == cseq)
1041 flush_expectations(ct, true);
1045 static int process_update_response(struct sk_buff *skb, unsigned int dataoff,
1046 const char **dptr, unsigned int *datalen,
1047 unsigned int cseq, unsigned int code)
1049 enum ip_conntrack_info ctinfo;
1050 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1051 struct nf_conn_help *help = nfct_help(ct);
1053 if ((code >= 100 && code <= 199) ||
1054 (code >= 200 && code <= 299))
1055 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1056 else if (help->help.ct_sip_info.invite_cseq == cseq)
1057 flush_expectations(ct, true);
1061 static int process_prack_response(struct sk_buff *skb, unsigned int dataoff,
1062 const char **dptr, unsigned int *datalen,
1063 unsigned int cseq, unsigned int code)
1065 enum ip_conntrack_info ctinfo;
1066 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1067 struct nf_conn_help *help = nfct_help(ct);
1069 if ((code >= 100 && code <= 199) ||
1070 (code >= 200 && code <= 299))
1071 return process_sdp(skb, dataoff, dptr, datalen, cseq);
1072 else if (help->help.ct_sip_info.invite_cseq == cseq)
1073 flush_expectations(ct, true);
1077 static int process_bye_request(struct sk_buff *skb, unsigned int dataoff,
1078 const char **dptr, unsigned int *datalen,
1081 enum ip_conntrack_info ctinfo;
1082 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1084 flush_expectations(ct, true);
1088 /* Parse a REGISTER request and create a permanent expectation for incoming
1089 * signalling connections. The expectation is marked inactive and is activated
1090 * when receiving a response indicating success from the registrar.
1092 static int process_register_request(struct sk_buff *skb, unsigned int dataoff,
1093 const char **dptr, unsigned int *datalen,
1096 enum ip_conntrack_info ctinfo;
1097 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1098 struct nf_conn_help *help = nfct_help(ct);
1099 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1100 unsigned int matchoff, matchlen;
1101 struct nf_conntrack_expect *exp;
1102 union nf_inet_addr *saddr, daddr;
1105 unsigned int expires = 0;
1107 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
1109 /* Expected connections can not register again. */
1110 if (ct->status & IPS_EXPECTED)
1113 /* We must check the expiration time: a value of zero signals the
1114 * registrar to release the binding. We'll remove our expectation
1115 * when receiving the new bindings in the response, but we don't
1116 * want to create new ones.
1118 * The expiration time may be contained in Expires: header, the
1119 * Contact: header parameters or the URI parameters.
1121 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1122 &matchoff, &matchlen) > 0)
1123 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1125 ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
1126 SIP_HDR_CONTACT, NULL,
1127 &matchoff, &matchlen, &daddr, &port);
1133 /* We don't support third-party registrations */
1134 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &daddr))
1137 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen, *datalen,
1141 if (ct_sip_parse_numerical_param(ct, *dptr,
1142 matchoff + matchlen, *datalen,
1143 "expires=", NULL, NULL, &expires) < 0)
1151 exp = nf_ct_expect_alloc(ct);
1156 if (sip_direct_signalling)
1157 saddr = &ct->tuplehash[!dir].tuple.src.u3;
1159 nf_ct_expect_init(exp, SIP_EXPECT_SIGNALLING, nf_ct_l3num(ct),
1160 saddr, &daddr, proto, NULL, &port);
1161 exp->timeout.expires = sip_timeout * HZ;
1162 exp->helper = nfct_help(ct)->helper;
1163 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1165 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook);
1166 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK)
1167 ret = nf_nat_sip_expect(skb, dataoff, dptr, datalen, exp,
1168 matchoff, matchlen);
1170 if (nf_ct_expect_related(exp) != 0)
1175 nf_ct_expect_put(exp);
1178 if (ret == NF_ACCEPT)
1179 help->help.ct_sip_info.register_cseq = cseq;
1183 static int process_register_response(struct sk_buff *skb, unsigned int dataoff,
1184 const char **dptr, unsigned int *datalen,
1185 unsigned int cseq, unsigned int code)
1187 enum ip_conntrack_info ctinfo;
1188 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1189 struct nf_conn_help *help = nfct_help(ct);
1190 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
1191 union nf_inet_addr addr;
1194 unsigned int matchoff, matchlen, coff = 0;
1195 unsigned int expires = 0;
1196 int in_contact = 0, ret;
1198 /* According to RFC 3261, "UAs MUST NOT send a new registration until
1199 * they have received a final response from the registrar for the
1200 * previous one or the previous REGISTER request has timed out".
1202 * However, some servers fail to detect retransmissions and send late
1203 * responses, so we store the sequence number of the last valid
1204 * request and compare it here.
1206 if (help->help.ct_sip_info.register_cseq != cseq)
1209 if (code >= 100 && code <= 199)
1211 if (code < 200 || code > 299)
1214 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
1215 &matchoff, &matchlen) > 0)
1216 expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1219 unsigned int c_expires = expires;
1221 ret = ct_sip_parse_header_uri(ct, *dptr, &coff, *datalen,
1222 SIP_HDR_CONTACT, &in_contact,
1223 &matchoff, &matchlen,
1230 /* We don't support third-party registrations */
1231 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &addr))
1234 if (ct_sip_parse_transport(ct, *dptr, matchoff + matchlen,
1235 *datalen, &proto) == 0)
1238 ret = ct_sip_parse_numerical_param(ct, *dptr,
1239 matchoff + matchlen,
1240 *datalen, "expires=",
1241 NULL, NULL, &c_expires);
1246 if (refresh_signalling_expectation(ct, &addr, proto, port,
1252 flush_expectations(ct, false);
1256 static const struct sip_handler sip_handlers[] = {
1257 SIP_HANDLER("INVITE", process_sdp, process_invite_response),
1258 SIP_HANDLER("UPDATE", process_sdp, process_update_response),
1259 SIP_HANDLER("ACK", process_sdp, NULL),
1260 SIP_HANDLER("PRACK", process_sdp, process_prack_response),
1261 SIP_HANDLER("BYE", process_bye_request, NULL),
1262 SIP_HANDLER("REGISTER", process_register_request, process_register_response),
1265 static int process_sip_response(struct sk_buff *skb, unsigned int dataoff,
1266 const char **dptr, unsigned int *datalen)
1268 enum ip_conntrack_info ctinfo;
1269 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1270 unsigned int matchoff, matchlen, matchend;
1271 unsigned int code, cseq, i;
1273 if (*datalen < strlen("SIP/2.0 200"))
1275 code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1279 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1280 &matchoff, &matchlen) <= 0)
1282 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1285 matchend = matchoff + matchlen + 1;
1287 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1288 const struct sip_handler *handler;
1290 handler = &sip_handlers[i];
1291 if (handler->response == NULL)
1293 if (*datalen < matchend + handler->len ||
1294 strnicmp(*dptr + matchend, handler->method, handler->len))
1296 return handler->response(skb, dataoff, dptr, datalen,
1302 static int process_sip_request(struct sk_buff *skb, unsigned int dataoff,
1303 const char **dptr, unsigned int *datalen)
1305 enum ip_conntrack_info ctinfo;
1306 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1307 unsigned int matchoff, matchlen;
1308 unsigned int cseq, i;
1310 for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
1311 const struct sip_handler *handler;
1313 handler = &sip_handlers[i];
1314 if (handler->request == NULL)
1316 if (*datalen < handler->len ||
1317 strnicmp(*dptr, handler->method, handler->len))
1320 if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_CSEQ,
1321 &matchoff, &matchlen) <= 0)
1323 cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1327 return handler->request(skb, dataoff, dptr, datalen, cseq);
1332 static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1333 unsigned int dataoff, const char **dptr,
1334 unsigned int *datalen)
1336 typeof(nf_nat_sip_hook) nf_nat_sip;
1339 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
1340 ret = process_sip_request(skb, dataoff, dptr, datalen);
1342 ret = process_sip_response(skb, dataoff, dptr, datalen);
1344 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1345 nf_nat_sip = rcu_dereference(nf_nat_sip_hook);
1346 if (nf_nat_sip && !nf_nat_sip(skb, dataoff, dptr, datalen))
1353 static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1354 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1356 struct tcphdr *th, _tcph;
1357 unsigned int dataoff, datalen;
1358 unsigned int matchoff, matchlen, clen;
1359 unsigned int msglen, origlen;
1360 const char *dptr, *end;
1361 s16 diff, tdiff = 0;
1364 if (ctinfo != IP_CT_ESTABLISHED &&
1365 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
1369 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1372 dataoff = protoff + th->doff * 4;
1373 if (dataoff >= skb->len)
1376 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1378 if (skb_is_nonlinear(skb)) {
1379 pr_debug("Copy of skbuff not supported yet.\n");
1383 dptr = skb->data + dataoff;
1384 datalen = skb->len - dataoff;
1385 if (datalen < strlen("SIP/2.0 200"))
1389 if (ct_sip_get_header(ct, dptr, 0, datalen,
1390 SIP_HDR_CONTENT_LENGTH,
1391 &matchoff, &matchlen) <= 0)
1394 clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1395 if (dptr + matchoff == end)
1398 if (end + strlen("\r\n\r\n") > dptr + datalen)
1400 if (end[0] != '\r' || end[1] != '\n' ||
1401 end[2] != '\r' || end[3] != '\n')
1403 end += strlen("\r\n\r\n") + clen;
1405 msglen = origlen = end - dptr;
1407 ret = process_sip_msg(skb, ct, dataoff, &dptr, &msglen);
1408 if (ret != NF_ACCEPT)
1410 diff = msglen - origlen;
1415 datalen = datalen + diff - msglen;
1421 static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,
1422 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1424 unsigned int dataoff, datalen;
1428 dataoff = protoff + sizeof(struct udphdr);
1429 if (dataoff >= skb->len)
1432 nf_ct_refresh(ct, skb, sip_timeout * HZ);
1434 if (skb_is_nonlinear(skb)) {
1435 pr_debug("Copy of skbuff not supported yet.\n");
1439 dptr = skb->data + dataoff;
1440 datalen = skb->len - dataoff;
1441 if (datalen < strlen("SIP/2.0 200"))
1444 return process_sip_msg(skb, ct, dataoff, &dptr, &datalen);
1447 static struct nf_conntrack_helper sip[MAX_PORTS][4] __read_mostly;
1448 static char sip_names[MAX_PORTS][4][sizeof("sip-65535")] __read_mostly;
1450 static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1] = {
1451 [SIP_EXPECT_SIGNALLING] = {
1452 .name = "signalling",
1456 [SIP_EXPECT_AUDIO] = {
1458 .max_expected = 2 * IP_CT_DIR_MAX,
1461 [SIP_EXPECT_VIDEO] = {
1463 .max_expected = 2 * IP_CT_DIR_MAX,
1468 static void nf_conntrack_sip_fini(void)
1472 for (i = 0; i < ports_c; i++) {
1473 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1474 if (sip[i][j].me == NULL)
1476 nf_conntrack_helper_unregister(&sip[i][j]);
1481 static int __init nf_conntrack_sip_init(void)
1487 ports[ports_c++] = SIP_PORT;
1489 for (i = 0; i < ports_c; i++) {
1490 memset(&sip[i], 0, sizeof(sip[i]));
1492 sip[i][0].tuple.src.l3num = AF_INET;
1493 sip[i][0].tuple.dst.protonum = IPPROTO_UDP;
1494 sip[i][0].help = sip_help_udp;
1495 sip[i][1].tuple.src.l3num = AF_INET;
1496 sip[i][1].tuple.dst.protonum = IPPROTO_TCP;
1497 sip[i][1].help = sip_help_tcp;
1499 sip[i][2].tuple.src.l3num = AF_INET6;
1500 sip[i][2].tuple.dst.protonum = IPPROTO_UDP;
1501 sip[i][2].help = sip_help_udp;
1502 sip[i][3].tuple.src.l3num = AF_INET6;
1503 sip[i][3].tuple.dst.protonum = IPPROTO_TCP;
1504 sip[i][3].help = sip_help_tcp;
1506 for (j = 0; j < ARRAY_SIZE(sip[i]); j++) {
1507 sip[i][j].tuple.src.u.udp.port = htons(ports[i]);
1508 sip[i][j].expect_policy = sip_exp_policy;
1509 sip[i][j].expect_class_max = SIP_EXPECT_MAX;
1510 sip[i][j].me = THIS_MODULE;
1512 tmpname = &sip_names[i][j][0];
1513 if (ports[i] == SIP_PORT)
1514 sprintf(tmpname, "sip");
1516 sprintf(tmpname, "sip-%u", i);
1517 sip[i][j].name = tmpname;
1519 pr_debug("port #%u: %u\n", i, ports[i]);
1521 ret = nf_conntrack_helper_register(&sip[i][j]);
1523 printk("nf_ct_sip: failed to register helper "
1524 "for pf: %u port: %u\n",
1525 sip[i][j].tuple.src.l3num, ports[i]);
1526 nf_conntrack_sip_fini();
1534 module_init(nf_conntrack_sip_init);
1535 module_exit(nf_conntrack_sip_fini);