blob: e5920fb7ad01ac0d0927f6d7e1daffe85e597856 [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)
Hans Schillstromf7a1dd62013-04-27 20:06:14 +020040 return -EINVAL;
Simon Horman758ff032010-08-22 21:37:55 +090041 dataoff += *matchoff;
42 }
43
Simon Horman758ff032010-08-22 21:37:55 +090044 /* Too large is useless */
45 if (*matchlen > IP_VS_PEDATA_MAXLEN)
46 return -EINVAL;
47
48 /* SIP headers are always followed by a line terminator */
49 if (*matchoff + *matchlen == datalen)
50 return -EINVAL;
51
52 /* RFC 2543 allows lines to be terminated with CR, LF or CRLF,
53 * RFC 3261 allows only CRLF, we support both. */
54 if (*(dptr + *matchoff + *matchlen) != '\r' &&
55 *(dptr + *matchoff + *matchlen) != '\n')
56 return -EINVAL;
57
58 IP_VS_DBG_BUF(9, "SIP callid %s (%d bytes)\n",
59 IP_VS_DEBUG_CALLID(dptr + *matchoff, *matchlen),
60 *matchlen);
61 return 0;
62}
63
64static int
65ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
66{
Jesper Dangaard Brouer92eec782012-09-26 14:07:33 +020067 struct sk_buff *reasm = skb_nfct_reasm(skb);
Simon Horman758ff032010-08-22 21:37:55 +090068 struct ip_vs_iphdr iph;
69 unsigned int dataoff, datalen, matchoff, matchlen;
70 const char *dptr;
Hans Schillstrom37165222010-11-19 14:25:09 +010071 int retc;
Simon Horman758ff032010-08-22 21:37:55 +090072
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +020073 ip_vs_fill_iph_skb(p->af, skb, &iph);
Simon Horman758ff032010-08-22 21:37:55 +090074
75 /* Only useful with UDP */
76 if (iph.protocol != IPPROTO_UDP)
77 return -EINVAL;
Jesper Dangaard Brouer92eec782012-09-26 14:07:33 +020078 /* todo: IPv6 fragments:
79 * I think this only should be done for the first fragment. /HS
80 */
81 if (reasm) {
82 skb = reasm;
83 dataoff = iph.thoff_reasm + sizeof(struct udphdr);
84 } else
85 dataoff = iph.len + sizeof(struct udphdr);
Simon Horman758ff032010-08-22 21:37:55 +090086
Simon Horman758ff032010-08-22 21:37:55 +090087 if (dataoff >= skb->len)
88 return -EINVAL;
Jesper Dangaard Brouer92eec782012-09-26 14:07:33 +020089 /* todo: Check if this will mess-up the reasm skb !!! /HS */
90 retc = skb_linearize(skb);
91 if (retc < 0)
Hans Schillstrom37165222010-11-19 14:25:09 +010092 return retc;
Simon Horman758ff032010-08-22 21:37:55 +090093 dptr = skb->data + dataoff;
94 datalen = skb->len - dataoff;
95
96 if (get_callid(dptr, dataoff, datalen, &matchoff, &matchlen))
97 return -EINVAL;
98
Simon Horman758ff032010-08-22 21:37:55 +090099 /* N.B: pe_data is only set on success,
100 * this allows fallback to the default persistence logic on failure
101 */
Shan Wei6060c742011-03-07 10:11:34 +0800102 p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
103 if (!p->pe_data)
104 return -ENOMEM;
105
Simon Horman758ff032010-08-22 21:37:55 +0900106 p->pe_data_len = matchlen;
107
108 return 0;
109}
110
111static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
112 struct ip_vs_conn *ct)
113
114{
Rusty Russell3db1cd52011-12-19 13:56:45 +0000115 bool ret = false;
Simon Horman758ff032010-08-22 21:37:55 +0900116
117 if (ct->af == p->af &&
118 ip_vs_addr_equal(p->af, p->caddr, &ct->caddr) &&
119 /* protocol should only be IPPROTO_IP if
120 * d_addr is a fwmark */
121 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
122 p->vaddr, &ct->vaddr) &&
123 ct->vport == p->vport &&
124 ct->flags & IP_VS_CONN_F_TEMPLATE &&
125 ct->protocol == p->protocol &&
126 ct->pe_data && ct->pe_data_len == p->pe_data_len &&
127 !memcmp(ct->pe_data, p->pe_data, p->pe_data_len))
Rusty Russell3db1cd52011-12-19 13:56:45 +0000128 ret = true;
Simon Horman758ff032010-08-22 21:37:55 +0900129
130 IP_VS_DBG_BUF(9, "SIP template match %s %s->%s:%d %s\n",
131 ip_vs_proto_name(p->protocol),
132 IP_VS_DEBUG_CALLID(p->pe_data, p->pe_data_len),
133 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
134 ret ? "hit" : "not hit");
135
136 return ret;
137}
138
139static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
140 u32 initval, bool inverse)
141{
142 return jhash(p->pe_data, p->pe_data_len, initval);
143}
144
145static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
146{
147 memcpy(buf, cp->pe_data, cp->pe_data_len);
148 return cp->pe_data_len;
149}
150
151static struct ip_vs_pe ip_vs_sip_pe =
152{
153 .name = "sip",
154 .refcnt = ATOMIC_INIT(0),
155 .module = THIS_MODULE,
156 .n_list = LIST_HEAD_INIT(ip_vs_sip_pe.n_list),
157 .fill_param = ip_vs_sip_fill_param,
158 .ct_match = ip_vs_sip_ct_match,
159 .hashkey_raw = ip_vs_sip_hashkey_raw,
160 .show_pe_data = ip_vs_sip_show_pe_data,
161};
162
163static int __init ip_vs_sip_init(void)
164{
165 return register_ip_vs_pe(&ip_vs_sip_pe);
166}
167
168static void __exit ip_vs_sip_cleanup(void)
169{
170 unregister_ip_vs_pe(&ip_vs_sip_pe);
171}
172
173module_init(ip_vs_sip_init);
174module_exit(ip_vs_sip_cleanup);
175MODULE_LICENSE("GPL");