1 /*
2  * ip_vs_proto.c: transport protocol load balancing support for IPVS
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *              Julian Anastasov <ja@ssi.bg>
6  *
7  *              This program is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  * Changes:
13  *
14  */
15 
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/skbuff.h>
22 #include <linux/gfp.h>
23 #include <linux/in.h>
24 #include <linux/ip.h>
25 #include <net/protocol.h>
26 #include <net/tcp.h>
27 #include <net/udp.h>
28 #include <asm/system.h>
29 #include <linux/stat.h>
30 #include <linux/proc_fs.h>
31 
32 #include <net/ip_vs.h>
33 
34 
35 /*
36  * IPVS protocols can only be registered/unregistered when the ipvs
37  * module is loaded/unloaded, so no lock is needed in accessing the
38  * ipvs protocol table.
39  */
40 
41 #define IP_VS_PROTO_TAB_SIZE		32	/* must be power of 2 */
42 #define IP_VS_PROTO_HASH(proto)		((proto) & (IP_VS_PROTO_TAB_SIZE-1))
43 
44 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
45 
46 
47 /*
48  *	register an ipvs protocol
49  */
register_ip_vs_protocol(struct ip_vs_protocol * pp)50 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
51 {
52 	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
53 
54 	pp->next = ip_vs_proto_table[hash];
55 	ip_vs_proto_table[hash] = pp;
56 
57 	if (pp->init != NULL)
58 		pp->init(pp);
59 
60 	return 0;
61 }
62 
63 #if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP) || \
64     defined(CONFIG_IP_VS_PROTO_SCTP) || defined(CONFIG_IP_VS_PROTO_AH) || \
65     defined(CONFIG_IP_VS_PROTO_ESP)
66 /*
67  *	register an ipvs protocols netns related data
68  */
69 static int
register_ip_vs_proto_netns(struct net * net,struct ip_vs_protocol * pp)70 register_ip_vs_proto_netns(struct net *net, struct ip_vs_protocol *pp)
71 {
72 	struct netns_ipvs *ipvs = net_ipvs(net);
73 	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
74 	struct ip_vs_proto_data *pd =
75 			kzalloc(sizeof(struct ip_vs_proto_data), GFP_ATOMIC);
76 
77 	if (!pd)
78 		return -ENOMEM;
79 
80 	pd->pp = pp;	/* For speed issues */
81 	pd->next = ipvs->proto_data_table[hash];
82 	ipvs->proto_data_table[hash] = pd;
83 	atomic_set(&pd->appcnt, 0);	/* Init app counter */
84 
85 	if (pp->init_netns != NULL)
86 		pp->init_netns(net, pd);
87 
88 	return 0;
89 }
90 #endif
91 
92 /*
93  *	unregister an ipvs protocol
94  */
unregister_ip_vs_protocol(struct ip_vs_protocol * pp)95 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
96 {
97 	struct ip_vs_protocol **pp_p;
98 	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
99 
100 	pp_p = &ip_vs_proto_table[hash];
101 	for (; *pp_p; pp_p = &(*pp_p)->next) {
102 		if (*pp_p == pp) {
103 			*pp_p = pp->next;
104 			if (pp->exit != NULL)
105 				pp->exit(pp);
106 			return 0;
107 		}
108 	}
109 
110 	return -ESRCH;
111 }
112 
113 /*
114  *	unregister an ipvs protocols netns data
115  */
116 static int
unregister_ip_vs_proto_netns(struct net * net,struct ip_vs_proto_data * pd)117 unregister_ip_vs_proto_netns(struct net *net, struct ip_vs_proto_data *pd)
118 {
119 	struct netns_ipvs *ipvs = net_ipvs(net);
120 	struct ip_vs_proto_data **pd_p;
121 	unsigned hash = IP_VS_PROTO_HASH(pd->pp->protocol);
122 
123 	pd_p = &ipvs->proto_data_table[hash];
124 	for (; *pd_p; pd_p = &(*pd_p)->next) {
125 		if (*pd_p == pd) {
126 			*pd_p = pd->next;
127 			if (pd->pp->exit_netns != NULL)
128 				pd->pp->exit_netns(net, pd);
129 			kfree(pd);
130 			return 0;
131 		}
132 	}
133 
134 	return -ESRCH;
135 }
136 
137 /*
138  *	get ip_vs_protocol object by its proto.
139  */
ip_vs_proto_get(unsigned short proto)140 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
141 {
142 	struct ip_vs_protocol *pp;
143 	unsigned hash = IP_VS_PROTO_HASH(proto);
144 
145 	for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
146 		if (pp->protocol == proto)
147 			return pp;
148 	}
149 
150 	return NULL;
151 }
152 EXPORT_SYMBOL(ip_vs_proto_get);
153 
154 /*
155  *	get ip_vs_protocol object data by netns and proto
156  */
157 struct ip_vs_proto_data *
__ipvs_proto_data_get(struct netns_ipvs * ipvs,unsigned short proto)158 __ipvs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
159 {
160 	struct ip_vs_proto_data *pd;
161 	unsigned hash = IP_VS_PROTO_HASH(proto);
162 
163 	for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) {
164 		if (pd->pp->protocol == proto)
165 			return pd;
166 	}
167 
168 	return NULL;
169 }
170 
171 struct ip_vs_proto_data *
ip_vs_proto_data_get(struct net * net,unsigned short proto)172 ip_vs_proto_data_get(struct net *net, unsigned short proto)
173 {
174 	struct netns_ipvs *ipvs = net_ipvs(net);
175 
176 	return __ipvs_proto_data_get(ipvs, proto);
177 }
178 EXPORT_SYMBOL(ip_vs_proto_data_get);
179 
180 /*
181  *	Propagate event for state change to all protocols
182  */
ip_vs_protocol_timeout_change(struct netns_ipvs * ipvs,int flags)183 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
184 {
185 	struct ip_vs_proto_data *pd;
186 	int i;
187 
188 	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
189 		for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
190 			if (pd->pp->timeout_change)
191 				pd->pp->timeout_change(pd, flags);
192 		}
193 	}
194 }
195 
196 
197 int *
ip_vs_create_timeout_table(int * table,int size)198 ip_vs_create_timeout_table(int *table, int size)
199 {
200 	return kmemdup(table, size, GFP_ATOMIC);
201 }
202 
203 
204 /*
205  *	Set timeout value for state specified by name
206  */
207 int
ip_vs_set_state_timeout(int * table,int num,const char * const * names,const char * name,int to)208 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
209 			const char *name, int to)
210 {
211 	int i;
212 
213 	if (!table || !name || !to)
214 		return -EINVAL;
215 
216 	for (i = 0; i < num; i++) {
217 		if (strcmp(names[i], name))
218 			continue;
219 		table[i] = to * HZ;
220 		return 0;
221 	}
222 	return -ENOENT;
223 }
224 
225 
ip_vs_state_name(__u16 proto,int state)226 const char * ip_vs_state_name(__u16 proto, int state)
227 {
228 	struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
229 
230 	if (pp == NULL || pp->state_name == NULL)
231 		return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
232 	return pp->state_name(state);
233 }
234 
235 
236 static void
ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol * pp,const struct sk_buff * skb,int offset,const char * msg)237 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
238 			     const struct sk_buff *skb,
239 			     int offset,
240 			     const char *msg)
241 {
242 	char buf[128];
243 	struct iphdr _iph, *ih;
244 
245 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
246 	if (ih == NULL)
247 		sprintf(buf, "TRUNCATED");
248 	else if (ih->frag_off & htons(IP_OFFSET))
249 		sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
250 	else {
251 		__be16 _ports[2], *pptr;
252 
253 		pptr = skb_header_pointer(skb, offset + ih->ihl*4,
254 					  sizeof(_ports), _ports);
255 		if (pptr == NULL)
256 			sprintf(buf, "TRUNCATED %pI4->%pI4",
257 				&ih->saddr, &ih->daddr);
258 		else
259 			sprintf(buf, "%pI4:%u->%pI4:%u",
260 				&ih->saddr, ntohs(pptr[0]),
261 				&ih->daddr, ntohs(pptr[1]));
262 	}
263 
264 	pr_debug("%s: %s %s\n", msg, pp->name, buf);
265 }
266 
267 #ifdef CONFIG_IP_VS_IPV6
268 static void
ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol * pp,const struct sk_buff * skb,int offset,const char * msg)269 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
270 			     const struct sk_buff *skb,
271 			     int offset,
272 			     const char *msg)
273 {
274 	char buf[192];
275 	struct ipv6hdr _iph, *ih;
276 
277 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
278 	if (ih == NULL)
279 		sprintf(buf, "TRUNCATED");
280 	else if (ih->nexthdr == IPPROTO_FRAGMENT)
281 		sprintf(buf, "%pI6->%pI6 frag",	&ih->saddr, &ih->daddr);
282 	else {
283 		__be16 _ports[2], *pptr;
284 
285 		pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
286 					  sizeof(_ports), _ports);
287 		if (pptr == NULL)
288 			sprintf(buf, "TRUNCATED %pI6->%pI6",
289 				&ih->saddr, &ih->daddr);
290 		else
291 			sprintf(buf, "%pI6:%u->%pI6:%u",
292 				&ih->saddr, ntohs(pptr[0]),
293 				&ih->daddr, ntohs(pptr[1]));
294 	}
295 
296 	pr_debug("%s: %s %s\n", msg, pp->name, buf);
297 }
298 #endif
299 
300 
301 void
ip_vs_tcpudp_debug_packet(int af,struct ip_vs_protocol * pp,const struct sk_buff * skb,int offset,const char * msg)302 ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
303 			  const struct sk_buff *skb,
304 			  int offset,
305 			  const char *msg)
306 {
307 #ifdef CONFIG_IP_VS_IPV6
308 	if (af == AF_INET6)
309 		ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
310 	else
311 #endif
312 		ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
313 }
314 
315 /*
316  * per network name-space init
317  */
ip_vs_protocol_net_init(struct net * net)318 int __net_init ip_vs_protocol_net_init(struct net *net)
319 {
320 #ifdef CONFIG_IP_VS_PROTO_TCP
321 	register_ip_vs_proto_netns(net, &ip_vs_protocol_tcp);
322 #endif
323 #ifdef CONFIG_IP_VS_PROTO_UDP
324 	register_ip_vs_proto_netns(net, &ip_vs_protocol_udp);
325 #endif
326 #ifdef CONFIG_IP_VS_PROTO_SCTP
327 	register_ip_vs_proto_netns(net, &ip_vs_protocol_sctp);
328 #endif
329 #ifdef CONFIG_IP_VS_PROTO_AH
330 	register_ip_vs_proto_netns(net, &ip_vs_protocol_ah);
331 #endif
332 #ifdef CONFIG_IP_VS_PROTO_ESP
333 	register_ip_vs_proto_netns(net, &ip_vs_protocol_esp);
334 #endif
335 	return 0;
336 }
337 
ip_vs_protocol_net_cleanup(struct net * net)338 void __net_exit ip_vs_protocol_net_cleanup(struct net *net)
339 {
340 	struct netns_ipvs *ipvs = net_ipvs(net);
341 	struct ip_vs_proto_data *pd;
342 	int i;
343 
344 	/* unregister all the ipvs proto data for this netns */
345 	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
346 		while ((pd = ipvs->proto_data_table[i]) != NULL)
347 			unregister_ip_vs_proto_netns(net, pd);
348 	}
349 }
350 
ip_vs_protocol_init(void)351 int __init ip_vs_protocol_init(void)
352 {
353 	char protocols[64];
354 #define REGISTER_PROTOCOL(p)			\
355 	do {					\
356 		register_ip_vs_protocol(p);	\
357 		strcat(protocols, ", ");	\
358 		strcat(protocols, (p)->name);	\
359 	} while (0)
360 
361 	protocols[0] = '\0';
362 	protocols[2] = '\0';
363 #ifdef CONFIG_IP_VS_PROTO_TCP
364 	REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
365 #endif
366 #ifdef CONFIG_IP_VS_PROTO_UDP
367 	REGISTER_PROTOCOL(&ip_vs_protocol_udp);
368 #endif
369 #ifdef CONFIG_IP_VS_PROTO_SCTP
370 	REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
371 #endif
372 #ifdef CONFIG_IP_VS_PROTO_AH
373 	REGISTER_PROTOCOL(&ip_vs_protocol_ah);
374 #endif
375 #ifdef CONFIG_IP_VS_PROTO_ESP
376 	REGISTER_PROTOCOL(&ip_vs_protocol_esp);
377 #endif
378 	pr_info("Registered protocols (%s)\n", &protocols[2]);
379 
380 	return 0;
381 }
382 
383 
ip_vs_protocol_cleanup(void)384 void ip_vs_protocol_cleanup(void)
385 {
386 	struct ip_vs_protocol *pp;
387 	int i;
388 
389 	/* unregister all the ipvs protocols */
390 	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
391 		while ((pp = ip_vs_proto_table[i]) != NULL)
392 			unregister_ip_vs_protocol(pp);
393 	}
394 }
395