File: tickle_tcp.c

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (379 lines) | stat: -rw-r--r-- 9,222 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 
   Tickle TCP connections tool

   Author:	Jiaju Zhang
   Based on the code in CTDB http://ctdb.samba.org/ written by
   Andrew Tridgell and Ronnie Sahlberg

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>

#define discard_const(ptr) ((void *)((intptr_t)(ptr)))

typedef union {
	struct sockaddr     sa;
	struct sockaddr_in  ip;
	struct sockaddr_in6 ip6;
} sock_addr;

uint32_t uint16_checksum(uint16_t *data, size_t n);
void set_nonblocking(int fd);
void set_close_on_exec(int fd);
static int parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
static int parse_ipv6(const char *s, const char *iface, unsigned port, sock_addr *saddr);
int parse_ip(const char *addr, const char *iface, unsigned port, sock_addr *saddr);
int parse_ip_port(const char *addr, sock_addr *saddr);
int send_tickle_ack(const sock_addr *dst, 
		    const sock_addr *src, 
		    uint32_t seq, uint32_t ack, int rst);
static void usage(void);

uint32_t uint16_checksum(uint16_t *data, size_t n)
{
	uint32_t sum=0;
	while (n >= 2) {
		sum += (uint32_t)ntohs(*data);
		data++;        
		n -= 2;
	}                      
	if (n == 1) {
		sum += (uint32_t)ntohs(*(uint8_t *)data);
	}
	return sum;
}       

static uint16_t tcp_checksum(uint16_t *data, size_t n, struct iphdr *ip)
{
	uint32_t sum = uint16_checksum(data, n);
	uint16_t sum2;
	sum += uint16_checksum((uint16_t *)(void *)&ip->saddr,
				sizeof(ip->saddr));
	sum += uint16_checksum((uint16_t *)(void *)&ip->daddr,
				sizeof(ip->daddr));
	sum += ip->protocol + n;
	sum = (sum & 0xFFFF) + (sum >> 16);
	sum = (sum & 0xFFFF) + (sum >> 16);
	sum2 = htons(sum);
	sum2 = ~sum2;
	if (sum2 == 0) {
		return 0xFFFF;
	}
	return sum2;
}

static uint16_t tcp_checksum6(uint16_t *data, size_t n, struct ip6_hdr *ip6)
{
	uint32_t phdr[2];
	uint32_t sum = 0;
	uint16_t sum2;

	sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_src, 16);
	sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_dst, 16);

	phdr[0] = htonl(n);
	phdr[1] = htonl(ip6->ip6_nxt);
	sum += uint16_checksum((uint16_t *)phdr, 8);

	sum += uint16_checksum(data, n);

	sum = (sum & 0xFFFF) + (sum >> 16);
	sum = (sum & 0xFFFF) + (sum >> 16);
	sum2 = htons(sum);
	sum2 = ~sum2;
	if (sum2 == 0) {
		return 0xFFFF;
	}
	return sum2;
}

void set_nonblocking(int fd)
{
	unsigned v;
	v = fcntl(fd, F_GETFL, 0);
	fcntl(fd, F_SETFL, v | O_NONBLOCK);
}

void set_close_on_exec(int fd) 
{               
	unsigned v;
	v = fcntl(fd, F_GETFD, 0);
	fcntl(fd, F_SETFD, v | FD_CLOEXEC);
}

static int parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
{
	sin->sin_family = AF_INET;
	sin->sin_port   = htons(port);

	if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
		fprintf(stderr, "Failed to translate %s into sin_addr\n", s);
		return -1;
	}

	return 0;
}

static int parse_ipv6(const char *s, const char *iface, unsigned port, sock_addr *saddr)
{
	saddr->ip6.sin6_family   = AF_INET6;
	saddr->ip6.sin6_port     = htons(port);
	saddr->ip6.sin6_flowinfo = 0;
	saddr->ip6.sin6_scope_id = 0;

	if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
		fprintf(stderr, "Failed to translate %s into sin6_addr\n", s);
		return -1;
	}

	if (iface && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
		saddr->ip6.sin6_scope_id = if_nametoindex(iface);
	}

        return 0;
}

int parse_ip(const char *addr, const char *iface, unsigned port, sock_addr *saddr)
{
	char *p;
	int ret;

	p = index(addr, ':');
	if (!p)
		ret = parse_ipv4(addr, port, &saddr->ip);
	else
		ret = parse_ipv6(addr, iface, port, saddr);

	return ret;
}

int parse_ip_port(const char *addr, sock_addr *saddr)
{
	char *s, *p;
	unsigned port;
	char *endp = NULL;
	int ret;

	s = strdup(addr);
	if (!s) {
		fprintf(stderr, "Failed strdup()\n");
		return -1;
	}

	p = rindex(s, ':');
	if (!p) {
		fprintf(stderr, "This addr: %s does not contain a port number\n", s);
		free(s);
		return -1;
	}
	
	port = strtoul(p+1, &endp, 10);
	if (!endp || *endp != 0) {
		fprintf(stderr, "Trailing garbage after the port in %s\n", s);
		free(s);
		return -1;
	}
	*p = 0;

	ret = parse_ip(s, NULL, port, saddr);
	free(s);
	return ret;
}

int send_tickle_ack(const sock_addr *dst, 
		    const sock_addr *src, 
		    uint32_t seq, uint32_t ack, int rst)
{
	int s;
	int ret;
	uint32_t one = 1;
	uint16_t tmpport;
	sock_addr *tmpdest;
	struct {
		struct iphdr ip;
		struct tcphdr tcp;
	} ip4pkt;
	struct {
		struct ip6_hdr ip6;
		struct tcphdr tcp;
	} ip6pkt;

	switch (src->ip.sin_family) {
	case AF_INET:
		memset(&ip4pkt, 0, sizeof(ip4pkt));
		ip4pkt.ip.version  = 4;
		ip4pkt.ip.ihl      = sizeof(ip4pkt.ip)/4;
		ip4pkt.ip.tot_len  = htons(sizeof(ip4pkt));
		ip4pkt.ip.ttl      = 255;
		ip4pkt.ip.protocol = IPPROTO_TCP;
		ip4pkt.ip.saddr    = src->ip.sin_addr.s_addr;
		ip4pkt.ip.daddr    = dst->ip.sin_addr.s_addr;
		ip4pkt.ip.check    = 0;

		ip4pkt.tcp.source  = src->ip.sin_port;
		ip4pkt.tcp.dest    = dst->ip.sin_port;
		ip4pkt.tcp.seq     = seq;
		ip4pkt.tcp.ack_seq = ack;
		ip4pkt.tcp.ack     = 1;
		if (rst)
			ip4pkt.tcp.rst = 1;
		ip4pkt.tcp.doff    = sizeof(ip4pkt.tcp)/4;
		ip4pkt.tcp.window   = htons(1234);
		ip4pkt.tcp.check    = tcp_checksum((uint16_t *)&ip4pkt.tcp, sizeof(ip4pkt.tcp), &ip4pkt.ip);

		s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
		if (s == -1) {
			fprintf(stderr, "Failed to open raw socket (%s)\n", strerror(errno));
			return -1;
		}

		ret = setsockopt(s, SOL_IP, IP_HDRINCL, &one, sizeof(one));
		if (ret != 0) {
			fprintf(stderr, "Failed to setup IP headers (%s)\n", strerror(errno));
			close(s);
			return -1;
		}

		set_nonblocking(s);
		set_close_on_exec(s);

		ret = sendto(s, &ip4pkt, sizeof(ip4pkt), 0, 
			     (const struct sockaddr *)&dst->ip, sizeof(dst->ip));
		close(s);
		if (ret != sizeof(ip4pkt)) {
			fprintf(stderr, "Failed sendto (%s)\n", strerror(errno));
			return -1;
		}
		break;

        case AF_INET6:
		memset(&ip6pkt, 0, sizeof(ip6pkt));
		ip6pkt.ip6.ip6_vfc  = 0x60;
		ip6pkt.ip6.ip6_plen = htons(20);
		ip6pkt.ip6.ip6_nxt  = IPPROTO_TCP;
		ip6pkt.ip6.ip6_hlim = 64;
		ip6pkt.ip6.ip6_src  = src->ip6.sin6_addr;
		ip6pkt.ip6.ip6_dst  = dst->ip6.sin6_addr;

		ip6pkt.tcp.source   = src->ip6.sin6_port;
		ip6pkt.tcp.dest     = dst->ip6.sin6_port;
		ip6pkt.tcp.seq      = seq;
		ip6pkt.tcp.ack_seq  = ack;
		ip6pkt.tcp.ack      = 1;
		if (rst)
			ip6pkt.tcp.rst      = 1;
		ip6pkt.tcp.doff     = sizeof(ip6pkt.tcp)/4;
		ip6pkt.tcp.window   = htons(1234);
		ip6pkt.tcp.check    = tcp_checksum6((uint16_t *)&ip6pkt.tcp, sizeof(ip6pkt.tcp), &ip6pkt.ip6);

		s = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
		if (s == -1) {
			fprintf(stderr, "Failed to open sending socket\n");
			return -1;
                }

		tmpdest = discard_const(dst);
		tmpport = tmpdest->ip6.sin6_port;

		tmpdest->ip6.sin6_port = 0;
		ret = sendto(s, &ip6pkt, sizeof(ip6pkt), 0, (const struct sockaddr *)&dst->ip6, sizeof(dst->ip6));
		tmpdest->ip6.sin6_port = tmpport;
		close(s);

		if (ret != sizeof(ip6pkt)) {
			fprintf(stderr, "Failed sendto (%s)\n", strerror(errno));
			return -1;
		}
		break;

	default:
		fprintf(stderr, "Not an ipv4/v6 address\n");
		return -1;
	}

	return 0;
}

static void usage(void)
{
	printf("Usage: /usr/lib/heartbeat/tickle_tcp [ -n num ]\n");
	printf("Please note that this program need to read the list of\n");
	printf("{local_ip:port remote_ip:port} from stdin.\n");
	exit(1);
}

#define OPTION_STRING "n:h"

int main(int argc, char *argv[])
{
	int optchar, i, num = 1, cont = 1;
	sock_addr src, dst;
	char addrline[128], addr1[64], addr2[64];

	while(cont) {
		optchar = getopt(argc, argv, OPTION_STRING);
		switch(optchar) {
		case 'n':
			num = atoi(optarg);
			break;
		case 'h':
			usage();
			exit(EXIT_SUCCESS);
			break;
		case EOF:
			cont = 0;
			break;
		default:
			fprintf(stderr, "unknown option, please use '-h' for usage.\n");
			exit(EXIT_FAILURE);
			break;
		};
	}

	while(fgets(addrline, sizeof(addrline), stdin)) {
		sscanf(addrline, "%s %s", addr1, addr2);

		if (parse_ip_port(addr1, &src)) {
			fprintf(stderr, "Bad IP:port '%s'\n", addr1);
			return -1;
		}
		if (parse_ip_port(addr2, &dst)) {
			fprintf(stderr, "Bad IP:port '%s'\n", addr2);
			return -1;
		}
	
		for (i = 1; i <= num; i++) {
			if (send_tickle_ack(&dst, &src, 0, 0, 0)) {
				fprintf(stderr, "Error while sending tickle ack from '%s' to '%s'\n",
					addr1, addr2);
				return -1;
			}
		}

	}
	return 0;
}