]> git.karo-electronics.de Git - linux-beck.git/blob - net/netfilter/ipvs/ip_vs_pe_sip.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux-beck.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 max_len = 64;
17         size_t len = min3(max_len, callid_len, buf_len - *idx - 1);
18         memcpy(buf + *idx, callid, len);
19         buf[*idx+len] = '\0';
20         *idx += len + 1;
21         return buf + *idx - len;
22 }
23
24 #define IP_VS_DEBUG_CALLID(callid, len)                                 \
25         ip_vs_dbg_callid(ip_vs_dbg_buf, sizeof(ip_vs_dbg_buf),          \
26                          callid, len, &ip_vs_dbg_idx)
27 #endif
28
29 static int get_callid(const char *dptr, unsigned int dataoff,
30                       unsigned int datalen,
31                       unsigned int *matchoff, unsigned int *matchlen)
32 {
33         /* Find callid */
34         while (1) {
35                 int ret = ct_sip_get_header(NULL, dptr, dataoff, datalen,
36                                             SIP_HDR_CALL_ID, matchoff,
37                                             matchlen);
38                 if (ret > 0)
39                         break;
40                 if (!ret)
41                         return 0;
42                 dataoff += *matchoff;
43         }
44
45         /* Empty callid is useless */
46         if (!*matchlen)
47                 return -EINVAL;
48
49         /* Too large is useless */
50         if (*matchlen > IP_VS_PEDATA_MAXLEN)
51                 return -EINVAL;
52
53         /* SIP headers are always followed by a line terminator */
54         if (*matchoff + *matchlen == datalen)
55                 return -EINVAL;
56
57         /* RFC 2543 allows lines to be terminated with CR, LF or CRLF,
58          * RFC 3261 allows only CRLF, we support both. */
59         if (*(dptr + *matchoff + *matchlen) != '\r' &&
60             *(dptr + *matchoff + *matchlen) != '\n')
61                 return -EINVAL;
62
63         IP_VS_DBG_BUF(9, "SIP callid %s (%d bytes)\n",
64                       IP_VS_DEBUG_CALLID(dptr + *matchoff, *matchlen),
65                       *matchlen);
66         return 0;
67 }
68
69 static int
70 ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
71 {
72         struct sk_buff *reasm = skb_nfct_reasm(skb);
73         struct ip_vs_iphdr iph;
74         unsigned int dataoff, datalen, matchoff, matchlen;
75         const char *dptr;
76         int retc;
77
78         ip_vs_fill_iph_skb(p->af, skb, &iph);
79
80         /* Only useful with UDP */
81         if (iph.protocol != IPPROTO_UDP)
82                 return -EINVAL;
83         /* todo: IPv6 fragments:
84          *       I think this only should be done for the first fragment. /HS
85          */
86         if (reasm) {
87                 skb = reasm;
88                 dataoff = iph.thoff_reasm + sizeof(struct udphdr);
89         } else
90                 dataoff = iph.len + sizeof(struct udphdr);
91
92         if (dataoff >= skb->len)
93                 return -EINVAL;
94         /* todo: Check if this will mess-up the reasm skb !!! /HS */
95         retc = skb_linearize(skb);
96         if (retc < 0)
97                 return retc;
98         dptr = skb->data + dataoff;
99         datalen = skb->len - dataoff;
100
101         if (get_callid(dptr, dataoff, datalen, &matchoff, &matchlen))
102                 return -EINVAL;
103
104         /* N.B: pe_data is only set on success,
105          * this allows fallback to the default persistence logic on failure
106          */
107         p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
108         if (!p->pe_data)
109                 return -ENOMEM;
110
111         p->pe_data_len = matchlen;
112
113         return 0;
114 }
115
116 static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
117                                   struct ip_vs_conn *ct)
118
119 {
120         bool ret = false;
121
122         if (ct->af == p->af &&
123             ip_vs_addr_equal(p->af, p->caddr, &ct->caddr) &&
124             /* protocol should only be IPPROTO_IP if
125              * d_addr is a fwmark */
126             ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
127                              p->vaddr, &ct->vaddr) &&
128             ct->vport == p->vport &&
129             ct->flags & IP_VS_CONN_F_TEMPLATE &&
130             ct->protocol == p->protocol &&
131             ct->pe_data && ct->pe_data_len == p->pe_data_len &&
132             !memcmp(ct->pe_data, p->pe_data, p->pe_data_len))
133                 ret = true;
134
135         IP_VS_DBG_BUF(9, "SIP template match %s %s->%s:%d %s\n",
136                       ip_vs_proto_name(p->protocol),
137                       IP_VS_DEBUG_CALLID(p->pe_data, p->pe_data_len),
138                       IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
139                       ret ? "hit" : "not hit");
140
141         return ret;
142 }
143
144 static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
145                                  u32 initval, bool inverse)
146 {
147         return jhash(p->pe_data, p->pe_data_len, initval);
148 }
149
150 static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
151 {
152         memcpy(buf, cp->pe_data, cp->pe_data_len);
153         return cp->pe_data_len;
154 }
155
156 static struct ip_vs_pe ip_vs_sip_pe =
157 {
158         .name =                 "sip",
159         .refcnt =               ATOMIC_INIT(0),
160         .module =               THIS_MODULE,
161         .n_list =               LIST_HEAD_INIT(ip_vs_sip_pe.n_list),
162         .fill_param =           ip_vs_sip_fill_param,
163         .ct_match =             ip_vs_sip_ct_match,
164         .hashkey_raw =          ip_vs_sip_hashkey_raw,
165         .show_pe_data =         ip_vs_sip_show_pe_data,
166 };
167
168 static int __init ip_vs_sip_init(void)
169 {
170         return register_ip_vs_pe(&ip_vs_sip_pe);
171 }
172
173 static void __exit ip_vs_sip_cleanup(void)
174 {
175         unregister_ip_vs_pe(&ip_vs_sip_pe);
176         synchronize_rcu();
177 }
178
179 module_init(ip_vs_sip_init);
180 module_exit(ip_vs_sip_cleanup);
181 MODULE_LICENSE("GPL");