1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ip_vs_proto.c: transport protocol load balancing support for IPVS
5 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
6 * Julian Anastasov <ja@ssi.bg>
11 #define KMSG_COMPONENT "IPVS"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/skbuff.h>
17 #include <linux/gfp.h>
20 #include <net/protocol.h>
23 #include <linux/stat.h>
24 #include <linux/proc_fs.h>
26 #include <net/ip_vs.h>
30 * IPVS protocols can only be registered/unregistered when the ipvs
31 * module is loaded/unloaded, so no lock is needed in accessing the
32 * ipvs protocol table.
35 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
36 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
38 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
40 /* States for conn templates: NONE or words separated with ",", max 15 chars */
41 static const char *ip_vs_ctpl_state_name_table[IP_VS_CTPL_S_LAST] = {
42 [IP_VS_CTPL_S_NONE] = "NONE",
43 [IP_VS_CTPL_S_ASSURED] = "ASSURED",
47 * register an ipvs protocol
49 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
51 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
53 pp->next = ip_vs_proto_table[hash];
54 ip_vs_proto_table[hash] = pp;
63 * register an ipvs protocols netns related data
66 register_ip_vs_proto_netns(struct netns_ipvs *ipvs, struct ip_vs_protocol *pp)
68 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
69 struct ip_vs_proto_data *pd =
70 kzalloc(sizeof(struct ip_vs_proto_data), GFP_KERNEL);
75 pd->pp = pp; /* For speed issues */
76 pd->next = ipvs->proto_data_table[hash];
77 ipvs->proto_data_table[hash] = pd;
78 atomic_set(&pd->appcnt, 0); /* Init app counter */
80 if (pp->init_netns != NULL) {
81 int ret = pp->init_netns(ipvs, pd);
83 /* unlink an free proto data */
84 ipvs->proto_data_table[hash] = pd->next;
94 * unregister an ipvs protocol
96 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
98 struct ip_vs_protocol **pp_p;
99 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
101 pp_p = &ip_vs_proto_table[hash];
102 for (; *pp_p; pp_p = &(*pp_p)->next) {
105 if (pp->exit != NULL)
115 * unregister an ipvs protocols netns data
118 unregister_ip_vs_proto_netns(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
120 struct ip_vs_proto_data **pd_p;
121 unsigned int hash = IP_VS_PROTO_HASH(pd->pp->protocol);
123 pd_p = &ipvs->proto_data_table[hash];
124 for (; *pd_p; pd_p = &(*pd_p)->next) {
127 if (pd->pp->exit_netns != NULL)
128 pd->pp->exit_netns(ipvs, pd);
138 * get ip_vs_protocol object by its proto.
140 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
142 struct ip_vs_protocol *pp;
143 unsigned int hash = IP_VS_PROTO_HASH(proto);
145 for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
146 if (pp->protocol == proto)
152 EXPORT_SYMBOL(ip_vs_proto_get);
155 * get ip_vs_protocol object data by netns and proto
157 struct ip_vs_proto_data *
158 ip_vs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
160 struct ip_vs_proto_data *pd;
161 unsigned int hash = IP_VS_PROTO_HASH(proto);
163 for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) {
164 if (pd->pp->protocol == proto)
170 EXPORT_SYMBOL(ip_vs_proto_data_get);
173 * Propagate event for state change to all protocols
175 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
177 struct ip_vs_proto_data *pd;
180 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
181 for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
182 if (pd->pp->timeout_change)
183 pd->pp->timeout_change(pd, flags);
190 ip_vs_create_timeout_table(int *table, int size)
192 return kmemdup(table, size, GFP_KERNEL);
196 const char *ip_vs_state_name(const struct ip_vs_conn *cp)
198 unsigned int state = cp->state;
199 struct ip_vs_protocol *pp;
201 if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
203 if (state >= IP_VS_CTPL_S_LAST)
205 return ip_vs_ctpl_state_name_table[state] ? : "?";
207 pp = ip_vs_proto_get(cp->protocol);
208 if (pp == NULL || pp->state_name == NULL)
209 return (cp->protocol == IPPROTO_IP) ? "NONE" : "ERR!";
210 return pp->state_name(state);
215 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
216 const struct sk_buff *skb,
221 struct iphdr _iph, *ih;
223 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
225 sprintf(buf, "TRUNCATED");
226 else if (ih->frag_off & htons(IP_OFFSET))
227 sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
229 __be16 _ports[2], *pptr;
231 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
232 sizeof(_ports), _ports);
234 sprintf(buf, "TRUNCATED %pI4->%pI4",
235 &ih->saddr, &ih->daddr);
237 sprintf(buf, "%pI4:%u->%pI4:%u",
238 &ih->saddr, ntohs(pptr[0]),
239 &ih->daddr, ntohs(pptr[1]));
242 pr_debug("%s: %s %s\n", msg, pp->name, buf);
245 #ifdef CONFIG_IP_VS_IPV6
247 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
248 const struct sk_buff *skb,
253 struct ipv6hdr _iph, *ih;
255 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
257 sprintf(buf, "TRUNCATED");
258 else if (ih->nexthdr == IPPROTO_FRAGMENT)
259 sprintf(buf, "%pI6c->%pI6c frag", &ih->saddr, &ih->daddr);
261 __be16 _ports[2], *pptr;
263 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
264 sizeof(_ports), _ports);
266 sprintf(buf, "TRUNCATED %pI6c->%pI6c",
267 &ih->saddr, &ih->daddr);
269 sprintf(buf, "%pI6c:%u->%pI6c:%u",
270 &ih->saddr, ntohs(pptr[0]),
271 &ih->daddr, ntohs(pptr[1]));
274 pr_debug("%s: %s %s\n", msg, pp->name, buf);
280 ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
281 const struct sk_buff *skb,
285 #ifdef CONFIG_IP_VS_IPV6
287 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
290 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
294 * per network name-space init
296 int __net_init ip_vs_protocol_net_init(struct netns_ipvs *ipvs)
299 static struct ip_vs_protocol *protos[] = {
300 #ifdef CONFIG_IP_VS_PROTO_TCP
303 #ifdef CONFIG_IP_VS_PROTO_UDP
306 #ifdef CONFIG_IP_VS_PROTO_SCTP
307 &ip_vs_protocol_sctp,
309 #ifdef CONFIG_IP_VS_PROTO_AH
312 #ifdef CONFIG_IP_VS_PROTO_ESP
317 for (i = 0; i < ARRAY_SIZE(protos); i++) {
318 ret = register_ip_vs_proto_netns(ipvs, protos[i]);
325 ip_vs_protocol_net_cleanup(ipvs);
329 void __net_exit ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs)
331 struct ip_vs_proto_data *pd;
334 /* unregister all the ipvs proto data for this netns */
335 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
336 while ((pd = ipvs->proto_data_table[i]) != NULL)
337 unregister_ip_vs_proto_netns(ipvs, pd);
341 int __init ip_vs_protocol_init(void)
344 #define REGISTER_PROTOCOL(p) \
346 register_ip_vs_protocol(p); \
347 strcat(protocols, ", "); \
348 strcat(protocols, (p)->name); \
353 #ifdef CONFIG_IP_VS_PROTO_TCP
354 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
356 #ifdef CONFIG_IP_VS_PROTO_UDP
357 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
359 #ifdef CONFIG_IP_VS_PROTO_SCTP
360 REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
362 #ifdef CONFIG_IP_VS_PROTO_AH
363 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
365 #ifdef CONFIG_IP_VS_PROTO_ESP
366 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
368 pr_info("Registered protocols (%s)\n", &protocols[2]);
374 void ip_vs_protocol_cleanup(void)
376 struct ip_vs_protocol *pp;
379 /* unregister all the ipvs protocols */
380 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
381 while ((pp = ip_vs_proto_table[i]) != NULL)
382 unregister_ip_vs_protocol(pp);