1 // SPDX-License-Identifier: GPL-2.0-only
2 #define KMSG_COMPONENT "IPVS"
3 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
5 #include <linux/module.h>
6 #include <linux/kernel.h>
9 #include <net/netfilter/nf_conntrack.h>
10 #include <linux/netfilter/nf_conntrack_sip.h>
12 #ifdef CONFIG_IP_VS_DEBUG
13 static const char *ip_vs_dbg_callid(char *buf, size_t buf_len,
14 const char *callid, size_t callid_len,
18 size_t len = min3(max_len, callid_len, buf_len - *idx - 1);
19 memcpy(buf + *idx, callid, len);
22 return buf + *idx - len;
25 #define IP_VS_DEBUG_CALLID(callid, len) \
26 ip_vs_dbg_callid(ip_vs_dbg_buf, sizeof(ip_vs_dbg_buf), \
27 callid, len, &ip_vs_dbg_idx)
30 static int get_callid(const char *dptr, unsigned int dataoff,
32 unsigned int *matchoff, unsigned int *matchlen)
36 int ret = ct_sip_get_header(NULL, dptr, dataoff, datalen,
37 SIP_HDR_CALL_ID, matchoff,
46 /* Too large is useless */
47 if (*matchlen > IP_VS_PEDATA_MAXLEN)
50 /* SIP headers are always followed by a line terminator */
51 if (*matchoff + *matchlen == datalen)
54 /* RFC 2543 allows lines to be terminated with CR, LF or CRLF,
55 * RFC 3261 allows only CRLF, we support both. */
56 if (*(dptr + *matchoff + *matchlen) != '\r' &&
57 *(dptr + *matchoff + *matchlen) != '\n')
60 IP_VS_DBG_BUF(9, "SIP callid %s (%d bytes)\n",
61 IP_VS_DEBUG_CALLID(dptr + *matchoff, *matchlen),
67 ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
69 struct ip_vs_iphdr iph;
70 unsigned int dataoff, datalen, matchoff, matchlen;
74 retc = ip_vs_fill_iph_skb(p->af, skb, false, &iph);
76 /* Only useful with UDP */
77 if (!retc || iph.protocol != IPPROTO_UDP)
79 /* todo: IPv6 fragments:
80 * I think this only should be done for the first fragment. /HS
82 dataoff = iph.len + sizeof(struct udphdr);
84 if (dataoff >= skb->len)
86 retc = skb_linearize(skb);
89 dptr = skb->data + dataoff;
90 datalen = skb->len - dataoff;
92 if (get_callid(dptr, 0, datalen, &matchoff, &matchlen))
95 /* N.B: pe_data is only set on success,
96 * this allows fallback to the default persistence logic on failure
98 p->pe_data = kmemdup(dptr + matchoff, matchlen, GFP_ATOMIC);
102 p->pe_data_len = matchlen;
107 static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p,
108 struct ip_vs_conn *ct)
113 if (ct->af == p->af &&
114 ip_vs_addr_equal(p->af, p->caddr, &ct->caddr) &&
115 /* protocol should only be IPPROTO_IP if
116 * d_addr is a fwmark */
117 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
118 p->vaddr, &ct->vaddr) &&
119 ct->vport == p->vport &&
120 ct->flags & IP_VS_CONN_F_TEMPLATE &&
121 ct->protocol == p->protocol &&
122 ct->pe_data && ct->pe_data_len == p->pe_data_len &&
123 !memcmp(ct->pe_data, p->pe_data, p->pe_data_len))
126 IP_VS_DBG_BUF(9, "SIP template match %s %s->%s:%d %s\n",
127 ip_vs_proto_name(p->protocol),
128 IP_VS_DEBUG_CALLID(p->pe_data, p->pe_data_len),
129 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
130 ret ? "hit" : "not hit");
135 static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p,
136 u32 initval, bool inverse)
138 return jhash(p->pe_data, p->pe_data_len, initval);
141 static int ip_vs_sip_show_pe_data(const struct ip_vs_conn *cp, char *buf)
143 memcpy(buf, cp->pe_data, cp->pe_data_len);
144 return cp->pe_data_len;
147 static struct ip_vs_conn *
148 ip_vs_sip_conn_out(struct ip_vs_service *svc,
149 struct ip_vs_dest *dest,
151 const struct ip_vs_iphdr *iph,
155 if (likely(iph->protocol == IPPROTO_UDP))
156 return ip_vs_new_conn_out(svc, dest, skb, iph, dport, cport);
157 /* currently no need to handle other than UDP */
161 static struct ip_vs_pe ip_vs_sip_pe =
164 .refcnt = ATOMIC_INIT(0),
165 .module = THIS_MODULE,
166 .n_list = LIST_HEAD_INIT(ip_vs_sip_pe.n_list),
167 .fill_param = ip_vs_sip_fill_param,
168 .ct_match = ip_vs_sip_ct_match,
169 .hashkey_raw = ip_vs_sip_hashkey_raw,
170 .show_pe_data = ip_vs_sip_show_pe_data,
171 .conn_out = ip_vs_sip_conn_out,
174 static int __init ip_vs_sip_init(void)
176 return register_ip_vs_pe(&ip_vs_sip_pe);
179 static void __exit ip_vs_sip_cleanup(void)
181 unregister_ip_vs_pe(&ip_vs_sip_pe);
185 module_init(ip_vs_sip_init);
186 module_exit(ip_vs_sip_cleanup);
187 MODULE_LICENSE("GPL");