1
2
3
4
5
6
7
8
9
10
11
12
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/in.h>
23#include <linux/ip.h>
24#include <net/protocol.h>
25#include <net/tcp.h>
26#include <net/udp.h>
27#include <asm/system.h>
28#include <linux/stat.h>
29#include <linux/proc_fs.h>
30
31#include <net/ip_vs.h>
32
33
34
35
36
37
38
39
40#define IP_VS_PROTO_TAB_SIZE 32
41#define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
42
43static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
44
45
46
47
48
49static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
50{
51 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
52
53 pp->next = ip_vs_proto_table[hash];
54 ip_vs_proto_table[hash] = pp;
55
56 if (pp->init != NULL)
57 pp->init(pp);
58
59 return 0;
60}
61
62
63
64
65
66static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
67{
68 struct ip_vs_protocol **pp_p;
69 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
70
71 pp_p = &ip_vs_proto_table[hash];
72 for (; *pp_p; pp_p = &(*pp_p)->next) {
73 if (*pp_p == pp) {
74 *pp_p = pp->next;
75 if (pp->exit != NULL)
76 pp->exit(pp);
77 return 0;
78 }
79 }
80
81 return -ESRCH;
82}
83
84
85
86
87
88struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
89{
90 struct ip_vs_protocol *pp;
91 unsigned hash = IP_VS_PROTO_HASH(proto);
92
93 for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
94 if (pp->protocol == proto)
95 return pp;
96 }
97
98 return NULL;
99}
100
101
102
103
104
105void ip_vs_protocol_timeout_change(int flags)
106{
107 struct ip_vs_protocol *pp;
108 int i;
109
110 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
111 for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
112 if (pp->timeout_change)
113 pp->timeout_change(pp, flags);
114 }
115 }
116}
117
118
119int *
120ip_vs_create_timeout_table(int *table, int size)
121{
122 return kmemdup(table, size, GFP_ATOMIC);
123}
124
125
126
127
128
129int
130ip_vs_set_state_timeout(int *table, int num, const char *const *names,
131 const char *name, int to)
132{
133 int i;
134
135 if (!table || !name || !to)
136 return -EINVAL;
137
138 for (i = 0; i < num; i++) {
139 if (strcmp(names[i], name))
140 continue;
141 table[i] = to * HZ;
142 return 0;
143 }
144 return -ENOENT;
145}
146
147
148const char * ip_vs_state_name(__u16 proto, int state)
149{
150 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
151
152 if (pp == NULL || pp->state_name == NULL)
153 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
154 return pp->state_name(state);
155}
156
157
158static void
159ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
160 const struct sk_buff *skb,
161 int offset,
162 const char *msg)
163{
164 char buf[128];
165 struct iphdr _iph, *ih;
166
167 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
168 if (ih == NULL)
169 sprintf(buf, "%s TRUNCATED", pp->name);
170 else if (ih->frag_off & htons(IP_OFFSET))
171 sprintf(buf, "%s %pI4->%pI4 frag",
172 pp->name, &ih->saddr, &ih->daddr);
173 else {
174 __be16 _ports[2], *pptr
175;
176 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
177 sizeof(_ports), _ports);
178 if (pptr == NULL)
179 sprintf(buf, "%s TRUNCATED %pI4->%pI4",
180 pp->name, &ih->saddr, &ih->daddr);
181 else
182 sprintf(buf, "%s %pI4:%u->%pI4:%u",
183 pp->name,
184 &ih->saddr, ntohs(pptr[0]),
185 &ih->daddr, ntohs(pptr[1]));
186 }
187
188 pr_debug("%s: %s\n", msg, buf);
189}
190
191#ifdef CONFIG_IP_VS_IPV6
192static void
193ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
194 const struct sk_buff *skb,
195 int offset,
196 const char *msg)
197{
198 char buf[192];
199 struct ipv6hdr _iph, *ih;
200
201 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
202 if (ih == NULL)
203 sprintf(buf, "%s TRUNCATED", pp->name);
204 else if (ih->nexthdr == IPPROTO_FRAGMENT)
205 sprintf(buf, "%s %pI6->%pI6 frag",
206 pp->name, &ih->saddr, &ih->daddr);
207 else {
208 __be16 _ports[2], *pptr;
209
210 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
211 sizeof(_ports), _ports);
212 if (pptr == NULL)
213 sprintf(buf, "%s TRUNCATED %pI6->%pI6",
214 pp->name, &ih->saddr, &ih->daddr);
215 else
216 sprintf(buf, "%s %pI6:%u->%pI6:%u",
217 pp->name,
218 &ih->saddr, ntohs(pptr[0]),
219 &ih->daddr, ntohs(pptr[1]));
220 }
221
222 pr_debug("%s: %s\n", msg, buf);
223}
224#endif
225
226
227void
228ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
229 const struct sk_buff *skb,
230 int offset,
231 const char *msg)
232{
233#ifdef CONFIG_IP_VS_IPV6
234 if (skb->protocol == htons(ETH_P_IPV6))
235 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
236 else
237#endif
238 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
239}
240
241
242int __init ip_vs_protocol_init(void)
243{
244 char protocols[64];
245#define REGISTER_PROTOCOL(p) \
246 do { \
247 register_ip_vs_protocol(p); \
248 strcat(protocols, ", "); \
249 strcat(protocols, (p)->name); \
250 } while (0)
251
252 protocols[0] = '\0';
253 protocols[2] = '\0';
254#ifdef CONFIG_IP_VS_PROTO_TCP
255 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
256#endif
257#ifdef CONFIG_IP_VS_PROTO_UDP
258 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
259#endif
260#ifdef CONFIG_IP_VS_PROTO_AH
261 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
262#endif
263#ifdef CONFIG_IP_VS_PROTO_ESP
264 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
265#endif
266 pr_info("Registered protocols (%s)\n", &protocols[2]);
267
268 return 0;
269}
270
271
272void ip_vs_protocol_cleanup(void)
273{
274 struct ip_vs_protocol *pp;
275 int i;
276
277
278 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
279 while ((pp = ip_vs_proto_table[i]) != NULL)
280 unregister_ip_vs_protocol(pp);
281 }
282}
283