blob: ee4e2e3cc08655336d8b3b4458c4e4da13e5b82b [file] [log] [blame]
Simon Horman758ff032010-08-22 21:37:55 +09001#define KMSG_COMPONENT "IPVS"
2#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
3
4#include <linux/module.h>
5#include <linux/kernel.h>
6
7#include <net/ip_vs.h>
8#include <net/netfilter/nf_conntrack.h>
9#include <linux/netfilter/nf_conntrack_sip.h>
10
Simon Hormana91fd262010-10-13 21:22:35 +020011#ifdef CONFIG_IP_VS_DEBUG
Simon Horman758ff032010-08-22 21:37:55 +090012static const char *ip_vs_dbg_callid(char *buf, size_t buf_len,
13 const char *callid, size_t callid_len,
14 int *idx)
15{
16 size_t len = min(min(callid_len, (size_t)64), buf_len - *idx - 1);
17 memcpy(buf + *idx, callid, len);
18 buf[*idx+len] = '\0';
19 *idx += len + 1;
20 return buf + *idx - len;
21}
22
23#define IP_VS_DEBUG_CALLID(callid, len) \
24 ip_vs_dbg_callid(ip_vs_dbg_buf, sizeof(ip_vs_dbg_buf), \
25 callid, len, &ip_vs_dbg_idx)
Simon Hormana91fd262010-10-13 21:22:35 +020026#endif
Simon Horman758ff032010-08-22 21:37:55 +090027
28static int get_callid(const char *dptr, unsigned int dataoff,
29 unsigned int datalen,
30 unsigned int *matchoff, unsigned int *matchlen)
31{
32 /* Find callid */
33 while (1) {
34 int ret = ct_sip_get_header(NULL, dptr, dataoff, datalen,
35 SIP_HDR_CALL_ID, matchoff,
36 matchlen);
37 if (ret > 0)
38 break;
39 if (!ret)
40 return 0;
41 dataoff += *matchoff;
42 }
43
44 /* Empty callid is useless */
45 if (!*matchlen)
46 return -EINVAL;
47
48 /* Too large is useless */
49 if (*matchlen > IP_VS_PEDATA_MAXLEN)
50 return -EINVAL;
51
52 /* SIP headers are always followed by a line terminator */
53 if (*matchoff + *matchlen == datalen)
54 return -EINVAL;
55
56 /* RFC 2543 allows lines to be terminated with CR, LF or CRLF,
57 * RFC 3261 allows only CRLF, we support both. */
58 if (*(dptr + *matchoff + *matchlen) != '\r' &&
59 *(dptr + *matchoff + *matchlen) != '\n')
60 return -EINVAL;
61
62 IP_VS_DBG_BUF(9, "SIP callid %s (%d bytes)\n",
63 IP_VS_DEBUG_CALLID(dptr + *matchoff, *matchlen),
64 *matchlen);
65 return 0;
66}
67
68static int
69ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
70{
71 struct ip_vs_iphdr iph;
72 unsigned int dataoff, datalen, matchoff, matchlen;
73 const char *dptr;
Hans Schillstrom37165222010-11-19 14:25:09 +010074 int retc;
Simon Horman758ff032010-08-22 21:37:55 +090075
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +020076 ip_vs_fill_iph_skb(p->af, skb, &iph);
Simon Horman758ff032010-08-22 21:37:55 +090077
78 /* Only useful with UDP */
79 if (iph.protocol != IPPROTO_UDP)
80 return -EINVAL;
81
82 /* No Data ? */
83 dataoff = iph.len + sizeof(struct udphdr);
84 if (dataoff >= skb->len)
85 return -EINVAL;
86
Hans Schillstrom37165222010-11-19 14:25:09 +010087 if ((retc=skb_linearize(skb)) < 0)
88 return retc;
Simon Horman758ff032010-08-22 21:37:55 +090089 dptr = skb->data + dataoff;
90 datalen = skb->len - dataoff;
91
92 if (get_callid(dptr, dataoff, datalen, &matchoff, &matchlen))
93 return -EINVAL;
94
Simon Horman758ff032010-08-22 21:37:55 +090095 /* N.B: pe_data is only set on success,
96 * this allows fallback to the default persistence logic on failure
97 */
Shan Wei6060c742011-03-07 10:11:34 +080098 p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
99 if (!p->pe_data)
100 return -ENOMEM;
101
Simon Horman758ff032010-08-22 21:37:55 +0900102 p->pe_data_len = matchlen;
103
104 return 0;
105}
106
107static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
108 struct ip_vs_conn *ct)
109
110{
Rusty Russell3db1cd52011-12-19 13:56:45 +0000111 bool ret = false;
Simon Horman758ff032010-08-22 21:37:55 +0900112
113 if (ct->af == p->af &&
114 ip_vs_addr_equal(p->af, p->caddr, &ct->caddr) &&
115 /* protocol should only be IPPROTO_IP if
116 * d_addr is a fwmark */
117 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
118 p->vaddr, &ct->vaddr) &&
119 ct->vport == p->vport &&
120 ct->flags & IP_VS_CONN_F_TEMPLATE &&
121 ct->protocol == p->protocol &&
122 ct->pe_data && ct->pe_data_len == p->pe_data_len &&
123 !memcmp(ct->pe_data, p->pe_data, p->pe_data_len))
Rusty Russell3db1cd52011-12-19 13:56:45 +0000124 ret = true;
Simon Horman758ff032010-08-22 21:37:55 +0900125
126 IP_VS_DBG_BUF(9, "SIP template match %s %s->%s:%d %s\n",
127 ip_vs_proto_name(p->protocol),
128 IP_VS_DEBUG_CALLID(p->pe_data, p->pe_data_len),
129 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
130 ret ? "hit" : "not hit");
131
132 return ret;
133}
134
135static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
136 u32 initval, bool inverse)
137{
138 return jhash(p->pe_data, p->pe_data_len, initval);
139}
140
141static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
142{
143 memcpy(buf, cp->pe_data, cp->pe_data_len);
144 return cp->pe_data_len;
145}
146
147static struct ip_vs_pe ip_vs_sip_pe =
148{
149 .name = "sip",
150 .refcnt = ATOMIC_INIT(0),
151 .module = THIS_MODULE,
152 .n_list = LIST_HEAD_INIT(ip_vs_sip_pe.n_list),
153 .fill_param = ip_vs_sip_fill_param,
154 .ct_match = ip_vs_sip_ct_match,
155 .hashkey_raw = ip_vs_sip_hashkey_raw,
156 .show_pe_data = ip_vs_sip_show_pe_data,
157};
158
159static int __init ip_vs_sip_init(void)
160{
161 return register_ip_vs_pe(&ip_vs_sip_pe);
162}
163
164static void __exit ip_vs_sip_cleanup(void)
165{
166 unregister_ip_vs_pe(&ip_vs_sip_pe);
167}
168
169module_init(ip_vs_sip_init);
170module_exit(ip_vs_sip_cleanup);
171MODULE_LICENSE("GPL");