ipvs: correct initial offset of Call-ID header search in SIP persistence engine
[pandora-kernel.git] / net / netfilter / ipvs / ip_vs_pe_sip.c
1 #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
11 #ifdef CONFIG_IP_VS_DEBUG
12 static 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)
26 #endif
27
28 static 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 -EINVAL;
41                 dataoff += *matchoff;
42         }
43
44         /* 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
64 static int
65 ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
66 {
67         struct ip_vs_iphdr iph;
68         unsigned int dataoff, datalen, matchoff, matchlen;
69         const char *dptr;
70         int retc;
71
72         ip_vs_fill_iphdr(p->af, skb_network_header(skb), &iph);
73
74         /* Only useful with UDP */
75         if (iph.protocol != IPPROTO_UDP)
76                 return -EINVAL;
77
78         /* No Data ? */
79         dataoff = iph.len + sizeof(struct udphdr);
80         if (dataoff >= skb->len)
81                 return -EINVAL;
82
83         if ((retc=skb_linearize(skb)) < 0)
84                 return retc;
85         dptr = skb->data + dataoff;
86         datalen = skb->len - dataoff;
87
88         if (get_callid(dptr, 0, datalen, &matchoff, &matchlen))
89                 return -EINVAL;
90
91         /* N.B: pe_data is only set on success,
92          * this allows fallback to the default persistence logic on failure
93          */
94         p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
95         if (!p->pe_data)
96                 return -ENOMEM;
97
98         p->pe_data_len = matchlen;
99
100         return 0;
101 }
102
103 static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
104                                   struct ip_vs_conn *ct)
105
106 {
107         bool ret = 0;
108
109         if (ct->af == p->af &&
110             ip_vs_addr_equal(p->af, p->caddr, &ct->caddr) &&
111             /* protocol should only be IPPROTO_IP if
112              * d_addr is a fwmark */
113             ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
114                              p->vaddr, &ct->vaddr) &&
115             ct->vport == p->vport &&
116             ct->flags & IP_VS_CONN_F_TEMPLATE &&
117             ct->protocol == p->protocol &&
118             ct->pe_data && ct->pe_data_len == p->pe_data_len &&
119             !memcmp(ct->pe_data, p->pe_data, p->pe_data_len))
120                 ret = 1;
121
122         IP_VS_DBG_BUF(9, "SIP template match %s %s->%s:%d %s\n",
123                       ip_vs_proto_name(p->protocol),
124                       IP_VS_DEBUG_CALLID(p->pe_data, p->pe_data_len),
125                       IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
126                       ret ? "hit" : "not hit");
127
128         return ret;
129 }
130
131 static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
132                                  u32 initval, bool inverse)
133 {
134         return jhash(p->pe_data, p->pe_data_len, initval);
135 }
136
137 static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
138 {
139         memcpy(buf, cp->pe_data, cp->pe_data_len);
140         return cp->pe_data_len;
141 }
142
143 static struct ip_vs_pe ip_vs_sip_pe =
144 {
145         .name =                 "sip",
146         .refcnt =               ATOMIC_INIT(0),
147         .module =               THIS_MODULE,
148         .n_list =               LIST_HEAD_INIT(ip_vs_sip_pe.n_list),
149         .fill_param =           ip_vs_sip_fill_param,
150         .ct_match =             ip_vs_sip_ct_match,
151         .hashkey_raw =          ip_vs_sip_hashkey_raw,
152         .show_pe_data =         ip_vs_sip_show_pe_data,
153 };
154
155 static int __init ip_vs_sip_init(void)
156 {
157         return register_ip_vs_pe(&ip_vs_sip_pe);
158 }
159
160 static void __exit ip_vs_sip_cleanup(void)
161 {
162         unregister_ip_vs_pe(&ip_vs_sip_pe);
163 }
164
165 module_init(ip_vs_sip_init);
166 module_exit(ip_vs_sip_cleanup);
167 MODULE_LICENSE("GPL");