1 #define KMSG_COMPONENT "IPVS"
2 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
4 #include <linux/module.h>
5 #include <linux/kernel.h>
8 #include <net/netfilter/nf_conntrack.h>
9 #include <linux/netfilter/nf_conntrack_sip.h>
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,
17 size_t len = min3(max_len, callid_len, buf_len - *idx - 1);
18 memcpy(buf + *idx, callid, len);
21 return buf + *idx - len;
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)
29 static int get_callid(const char *dptr, unsigned int dataoff,
31 unsigned int *matchoff, unsigned int *matchlen)
35 int ret = ct_sip_get_header(NULL, dptr, dataoff, datalen,
36 SIP_HDR_CALL_ID, matchoff,
45 /* Too large is useless */
46 if (*matchlen > IP_VS_PEDATA_MAXLEN)
49 /* SIP headers are always followed by a line terminator */
50 if (*matchoff + *matchlen == datalen)
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')
59 IP_VS_DBG_BUF(9, "SIP callid %s (%d bytes)\n",
60 IP_VS_DEBUG_CALLID(dptr + *matchoff, *matchlen),
66 ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
68 struct sk_buff *reasm = skb_nfct_reasm(skb);
69 struct ip_vs_iphdr iph;
70 unsigned int dataoff, datalen, matchoff, matchlen;
74 ip_vs_fill_iph_skb(p->af, skb, &iph);
76 /* Only useful with UDP */
77 if (iph.protocol != IPPROTO_UDP)
79 /* todo: IPv6 fragments:
80 * I think this only should be done for the first fragment. /HS
84 dataoff = iph.thoff_reasm + sizeof(struct udphdr);
86 dataoff = iph.len + sizeof(struct udphdr);
88 if (dataoff >= skb->len)
90 /* todo: Check if this will mess-up the reasm skb !!! /HS */
91 retc = skb_linearize(skb);
94 dptr = skb->data + dataoff;
95 datalen = skb->len - dataoff;
97 if (get_callid(dptr, dataoff, datalen, &matchoff, &matchlen))
100 /* N.B: pe_data is only set on success,
101 * this allows fallback to the default persistence logic on failure
103 p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
107 p->pe_data_len = matchlen;
112 static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
113 struct ip_vs_conn *ct)
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))
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");
140 static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
141 u32 initval, bool inverse)
143 return jhash(p->pe_data, p->pe_data_len, initval);
146 static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
148 memcpy(buf, cp->pe_data, cp->pe_data_len);
149 return cp->pe_data_len;
152 static struct ip_vs_pe ip_vs_sip_pe =
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,
164 static int __init ip_vs_sip_init(void)
166 return register_ip_vs_pe(&ip_vs_sip_pe);
169 static void __exit ip_vs_sip_cleanup(void)
171 unregister_ip_vs_pe(&ip_vs_sip_pe);
175 module_init(ip_vs_sip_init);
176 module_exit(ip_vs_sip_cleanup);
177 MODULE_LICENSE("GPL");