]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/netfilter/ipvs/ip_vs_proto.c
2 * ip_vs_proto.c: transport protocol load balancing support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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.
16 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/skbuff.h>
22 #include <linux/gfp.h>
25 #include <net/protocol.h>
28 #include <linux/stat.h>
29 #include <linux/proc_fs.h>
31 #include <net/ip_vs.h>
35 * IPVS protocols can only be registered/unregistered when the ipvs
36 * module is loaded/unloaded, so no lock is needed in accessing the
37 * ipvs protocol table.
40 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
41 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
43 static struct ip_vs_protocol
*ip_vs_proto_table
[IP_VS_PROTO_TAB_SIZE
];
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 net
*net
, struct ip_vs_protocol
*pp
)
68 struct netns_ipvs
*ipvs
= net_ipvs(net
);
69 unsigned int hash
= IP_VS_PROTO_HASH(pp
->protocol
);
70 struct ip_vs_proto_data
*pd
=
71 kzalloc(sizeof(struct ip_vs_proto_data
), GFP_KERNEL
);
76 pd
->pp
= pp
; /* For speed issues */
77 pd
->next
= ipvs
->proto_data_table
[hash
];
78 ipvs
->proto_data_table
[hash
] = pd
;
79 atomic_set(&pd
->appcnt
, 0); /* Init app counter */
81 if (pp
->init_netns
!= NULL
) {
82 int ret
= pp
->init_netns(net
, pd
);
84 /* unlink an free proto data */
85 ipvs
->proto_data_table
[hash
] = pd
->next
;
95 * unregister an ipvs protocol
97 static int unregister_ip_vs_protocol(struct ip_vs_protocol
*pp
)
99 struct ip_vs_protocol
**pp_p
;
100 unsigned int hash
= IP_VS_PROTO_HASH(pp
->protocol
);
102 pp_p
= &ip_vs_proto_table
[hash
];
103 for (; *pp_p
; pp_p
= &(*pp_p
)->next
) {
106 if (pp
->exit
!= NULL
)
116 * unregister an ipvs protocols netns data
119 unregister_ip_vs_proto_netns(struct net
*net
, struct ip_vs_proto_data
*pd
)
121 struct netns_ipvs
*ipvs
= net_ipvs(net
);
122 struct ip_vs_proto_data
**pd_p
;
123 unsigned int hash
= IP_VS_PROTO_HASH(pd
->pp
->protocol
);
125 pd_p
= &ipvs
->proto_data_table
[hash
];
126 for (; *pd_p
; pd_p
= &(*pd_p
)->next
) {
129 if (pd
->pp
->exit_netns
!= NULL
)
130 pd
->pp
->exit_netns(net
, pd
);
140 * get ip_vs_protocol object by its proto.
142 struct ip_vs_protocol
* ip_vs_proto_get(unsigned short proto
)
144 struct ip_vs_protocol
*pp
;
145 unsigned int hash
= IP_VS_PROTO_HASH(proto
);
147 for (pp
= ip_vs_proto_table
[hash
]; pp
; pp
= pp
->next
) {
148 if (pp
->protocol
== proto
)
154 EXPORT_SYMBOL(ip_vs_proto_get
);
157 * get ip_vs_protocol object data by netns and proto
159 struct ip_vs_proto_data
*
160 __ipvs_proto_data_get(struct netns_ipvs
*ipvs
, unsigned short proto
)
162 struct ip_vs_proto_data
*pd
;
163 unsigned int hash
= IP_VS_PROTO_HASH(proto
);
165 for (pd
= ipvs
->proto_data_table
[hash
]; pd
; pd
= pd
->next
) {
166 if (pd
->pp
->protocol
== proto
)
173 struct ip_vs_proto_data
*
174 ip_vs_proto_data_get(struct net
*net
, unsigned short proto
)
176 struct netns_ipvs
*ipvs
= net_ipvs(net
);
178 return __ipvs_proto_data_get(ipvs
, proto
);
180 EXPORT_SYMBOL(ip_vs_proto_data_get
);
183 * Propagate event for state change to all protocols
185 void ip_vs_protocol_timeout_change(struct netns_ipvs
*ipvs
, int flags
)
187 struct ip_vs_proto_data
*pd
;
190 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
191 for (pd
= ipvs
->proto_data_table
[i
]; pd
; pd
= pd
->next
) {
192 if (pd
->pp
->timeout_change
)
193 pd
->pp
->timeout_change(pd
, flags
);
200 ip_vs_create_timeout_table(int *table
, int size
)
202 return kmemdup(table
, size
, GFP_KERNEL
);
207 * Set timeout value for state specified by name
210 ip_vs_set_state_timeout(int *table
, int num
, const char *const *names
,
211 const char *name
, int to
)
215 if (!table
|| !name
|| !to
)
218 for (i
= 0; i
< num
; i
++) {
219 if (strcmp(names
[i
], name
))
228 const char * ip_vs_state_name(__u16 proto
, int state
)
230 struct ip_vs_protocol
*pp
= ip_vs_proto_get(proto
);
232 if (pp
== NULL
|| pp
->state_name
== NULL
)
233 return (IPPROTO_IP
== proto
) ? "NONE" : "ERR!";
234 return pp
->state_name(state
);
239 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol
*pp
,
240 const struct sk_buff
*skb
,
245 struct iphdr _iph
, *ih
;
247 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
249 sprintf(buf
, "TRUNCATED");
250 else if (ih
->frag_off
& htons(IP_OFFSET
))
251 sprintf(buf
, "%pI4->%pI4 frag", &ih
->saddr
, &ih
->daddr
);
253 __be16 _ports
[2], *pptr
;
255 pptr
= skb_header_pointer(skb
, offset
+ ih
->ihl
*4,
256 sizeof(_ports
), _ports
);
258 sprintf(buf
, "TRUNCATED %pI4->%pI4",
259 &ih
->saddr
, &ih
->daddr
);
261 sprintf(buf
, "%pI4:%u->%pI4:%u",
262 &ih
->saddr
, ntohs(pptr
[0]),
263 &ih
->daddr
, ntohs(pptr
[1]));
266 pr_debug("%s: %s %s\n", msg
, pp
->name
, buf
);
269 #ifdef CONFIG_IP_VS_IPV6
271 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol
*pp
,
272 const struct sk_buff
*skb
,
277 struct ipv6hdr _iph
, *ih
;
279 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
281 sprintf(buf
, "TRUNCATED");
282 else if (ih
->nexthdr
== IPPROTO_FRAGMENT
)
283 sprintf(buf
, "%pI6->%pI6 frag", &ih
->saddr
, &ih
->daddr
);
285 __be16 _ports
[2], *pptr
;
287 pptr
= skb_header_pointer(skb
, offset
+ sizeof(struct ipv6hdr
),
288 sizeof(_ports
), _ports
);
290 sprintf(buf
, "TRUNCATED %pI6->%pI6",
291 &ih
->saddr
, &ih
->daddr
);
293 sprintf(buf
, "%pI6:%u->%pI6:%u",
294 &ih
->saddr
, ntohs(pptr
[0]),
295 &ih
->daddr
, ntohs(pptr
[1]));
298 pr_debug("%s: %s %s\n", msg
, pp
->name
, buf
);
304 ip_vs_tcpudp_debug_packet(int af
, struct ip_vs_protocol
*pp
,
305 const struct sk_buff
*skb
,
309 #ifdef CONFIG_IP_VS_IPV6
311 ip_vs_tcpudp_debug_packet_v6(pp
, skb
, offset
, msg
);
314 ip_vs_tcpudp_debug_packet_v4(pp
, skb
, offset
, msg
);
318 * per network name-space init
320 int __net_init
ip_vs_protocol_net_init(struct net
*net
)
323 static struct ip_vs_protocol
*protos
[] = {
324 #ifdef CONFIG_IP_VS_PROTO_TCP
327 #ifdef CONFIG_IP_VS_PROTO_UDP
330 #ifdef CONFIG_IP_VS_PROTO_SCTP
331 &ip_vs_protocol_sctp
,
333 #ifdef CONFIG_IP_VS_PROTO_AH
336 #ifdef CONFIG_IP_VS_PROTO_ESP
341 for (i
= 0; i
< ARRAY_SIZE(protos
); i
++) {
342 ret
= register_ip_vs_proto_netns(net
, protos
[i
]);
349 ip_vs_protocol_net_cleanup(net
);
353 void __net_exit
ip_vs_protocol_net_cleanup(struct net
*net
)
355 struct netns_ipvs
*ipvs
= net_ipvs(net
);
356 struct ip_vs_proto_data
*pd
;
359 /* unregister all the ipvs proto data for this netns */
360 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
361 while ((pd
= ipvs
->proto_data_table
[i
]) != NULL
)
362 unregister_ip_vs_proto_netns(net
, pd
);
366 int __init
ip_vs_protocol_init(void)
369 #define REGISTER_PROTOCOL(p) \
371 register_ip_vs_protocol(p); \
372 strcat(protocols, ", "); \
373 strcat(protocols, (p)->name); \
378 #ifdef CONFIG_IP_VS_PROTO_TCP
379 REGISTER_PROTOCOL(&ip_vs_protocol_tcp
);
381 #ifdef CONFIG_IP_VS_PROTO_UDP
382 REGISTER_PROTOCOL(&ip_vs_protocol_udp
);
384 #ifdef CONFIG_IP_VS_PROTO_SCTP
385 REGISTER_PROTOCOL(&ip_vs_protocol_sctp
);
387 #ifdef CONFIG_IP_VS_PROTO_AH
388 REGISTER_PROTOCOL(&ip_vs_protocol_ah
);
390 #ifdef CONFIG_IP_VS_PROTO_ESP
391 REGISTER_PROTOCOL(&ip_vs_protocol_esp
);
393 pr_info("Registered protocols (%s)\n", &protocols
[2]);
399 void ip_vs_protocol_cleanup(void)
401 struct ip_vs_protocol
*pp
;
404 /* unregister all the ipvs protocols */
405 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
406 while ((pp
= ip_vs_proto_table
[i
]) != NULL
)
407 unregister_ip_vs_protocol(pp
);