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