summaryrefslogtreecommitdiff
path: root/release/src/router/rc/listen.c
blob: c00cd93205830abbaf7a11b5e722c0db8edf2710 (plain)
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
/*
	listen.c -- Listen for any packet through an interface
	Copyright 2003, CyberTAN  Inc.  All Rights Reserved

	This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
	the contents of this file may not be disclosed to third parties,
	copied or duplicated in any form without the prior written
	permission of CyberTAN Inc.

	This software should be used as a reference only, and it not
	intended for production use!

	THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
	KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE.  CYBERTAN
	SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
	FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
*/

#include <rc.h>

#include <sys/ioctl.h>
#include <arpa/inet.h>

// for PF_PACKET
#include <features.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif


//	#define DEBUG

#ifdef DEBUG
#	define LOG(fmt, args...) cprintf(fmt, ##args);
#else
#	define LOG(fmt, args...) do { } while (0);
#endif


enum {L_FAIL, L_ERROR, L_UPGRADE, L_ESTABLISHED, L_SUCCESS};

struct iphdr {
	u_int8_t version;
	u_int8_t tos;
	u_int16_t tot_len;
	u_int16_t id;
	u_int16_t frag_off;
	u_int8_t ttl;
	u_int8_t protocol;
	u_int16_t check;
	u_int8_t saddr[4];
	u_int8_t daddr[4];
};

struct EthPacket {
	u_int8_t dst_mac[6];
	u_int8_t src_mac[6];
	u_int8_t type[2];
	//struct iphdr ip;	// size = 20
	u_int8_t version;
	u_int8_t tos;
	u_int16_t tot_len;
	u_int16_t id;
	u_int16_t frag_off;
	u_int8_t ttl;
	u_int8_t protocol;
	u_int16_t check;
	u_int8_t saddr[4];
	u_int8_t daddr[4];
	u_int8_t data[1500 - 20];
};


static int read_interface(const char *interface, int *ifindex, unsigned char *mac)
{
	int fd;
	struct ifreq ifr;
	int r;

	memset(&ifr, 0, sizeof(struct ifreq));
	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
		LOG("socket failed!: \n");
		return -1;
	}

	r = -1;
	ifr.ifr_addr.sa_family = AF_INET;
	strcpy(ifr.ifr_name, interface);

	if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
		*ifindex = ifr.ifr_ifindex;
		LOG("adapter index %d \n", ifr.ifr_ifindex);

		if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
			memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
			LOG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
			r = 0;
		}
		else {
			LOG("SIOCGIFHWADDR failed!\n");
		}
	}
	else {
		LOG("SIOCGIFINDEX failed!\n");
	}
	close(fd);
	return r;
}

static int raw_socket(int ifindex)
{
	int fd;
	struct sockaddr_ll sock;

	LOG("Opening raw socket on ifindex %d\n", ifindex);
	if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
		LOG("socket call failed: \n");
		return -1;
	}

	sock.sll_family = AF_PACKET;
	sock.sll_protocol = htons(ETH_P_IP);
	sock.sll_ifindex = ifindex;
	if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
		LOG("bind call failed: \n");
		close(fd);
		return -1;
	}

	return fd;
}

static u_int16_t checksum(void *addr, int count)
{
	// Compute Internet Checksum for "count" bytes beginning at location "addr".
	register int32_t sum = 0;
	u_int16_t *source = (u_int16_t *) addr;

	while (count > 1)  {
		/*  This is the inner loop */
		sum += *source++;
		count -= 2;
	}

	/*  Add left-over byte, if any */
	if (count > 0) {
		/* Make sure that the left-over byte is added correctly both with little and big endian hosts */
		u_int16_t tmp = 0;
		*(unsigned char *)(&tmp) = *(unsigned char *)source;
		sum += tmp;
	}

	/*  Fold 32-bit sum to 16 bits */
	while (sum >> 16)
		sum = (sum & 0xffff) + (sum >> 16);
	return ~sum;
}

static int listen_interface(char *interface)
{
	int ifindex;
	fd_set rfds;
	struct EthPacket packet;
	struct timeval tv;
	int retval;
	unsigned char mac[6];
	static int fd;
	int ret = L_SUCCESS;
	int bytes;
	u_int16_t check;
	struct in_addr ipaddr, netmask;


	if (read_interface(interface, &ifindex, mac) < 0) {
		return L_ERROR;
	}

	fd = raw_socket(ifindex);
	if (fd < 0) {
		LOG("FATAL: couldn't listen on socket\n");
		return L_ERROR;
	}

	while (1) {
		if (!wait_action_idle(5)) {	// Don't execute during upgrading
			ret = L_UPGRADE;
			break;
		}
		if (check_wanup()) {
			ret = L_ESTABLISHED;
			break;
		}

		tv.tv_sec = 100000;
		tv.tv_usec = 0;
		FD_ZERO(&rfds);
		FD_SET(fd, &rfds);
		LOG("Waitting for select... \n");
		retval = select(fd + 1, &rfds, NULL, NULL, &tv);

		if (retval == 0) {
			printf("no packet recieved! \n\n");
			continue;
		}

		memset(&packet, 0, sizeof(struct EthPacket));
		bytes = read(fd, &packet, sizeof(struct EthPacket));
		if (bytes < 0) {
			close(fd);
			LOG("couldn't read on raw listening socket -- ignoring\n");
			usleep(500000); // possible down interface, looping condition
			return L_FAIL;
		}

		if (bytes < (int) (sizeof(struct iphdr))) {
			LOG("message too short, ignoring\n");
			ret = L_FAIL;
			goto EXIT;
		}

		if (memcmp(mac, packet.dst_mac, 6) != 0) {
			LOG("dest %02x:%02x:%02x:%02x:%02x:%02x mac not the router\n",
				packet.dst_mac[0], packet.dst_mac[1], packet.dst_mac[2],
				packet.dst_mac[3], packet.dst_mac[4], packet.dst_mac[5]);
			ret = L_FAIL;
			goto EXIT;
		}

		if (inet_addr(nvram_safe_get("lan_ipaddr")) == *(u_int32_t *)packet.daddr) {
			LOG("dest ip equal to lan ipaddr\n");
			ret = L_FAIL;
			goto EXIT;
		}

		LOG("inet_addr=%x, packet.daddr=%x",inet_addr(nvram_safe_get("lan_ipaddr")),*(u_int32_t *)packet.daddr);

		//for (i=0; i<34;i++) {
		//	if (i%16==0) printf("\n");
		//	printf("%02x ",*(((u_int8_t *)packet)+i));
		//}
		//printf ("\n");

		LOG("%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X,%02X%02X\n",
			packet.dst_mac[0], packet.dst_mac[1], packet.dst_mac[2],
			packet.dst_mac[3], packet.dst_mac[4], packet.dst_mac[5],
			packet.src_mac[0], packet.src_mac[1], packet.src_mac[2],
			packet.src_mac[3], packet.src_mac[4], packet.src_mac[5],
			packet.type[0],packet.type[1]);

		LOG("ip.version = %x", packet.version);
		LOG("ip.tos = %x", packet.tos);
		LOG("ip.tot_len = %x", packet.tot_len);
		LOG("ip.id = %x", packet.id);
		LOG("ip.ttl= %x", packet.ttl);
		LOG("ip.protocol= %x", packet.protocol);
		LOG("ip.check=%04x", packet.check);
		LOG("ip.saddr=%08x", *(u_int32_t *)&(packet.saddr));
		LOG("ip.daddr=%08x", *(u_int32_t *)&(packet.daddr));

		if (*(u_int16_t *)packet.type == 0x0800) {
			LOG("not ip protocol");
			ret = L_FAIL;
			goto EXIT;
		}

		/* ignore any extra garbage bytes */
		bytes = ntohs(packet.tot_len);

		/* check IP checksum */
		check = packet.check;
		packet.check = 0;

		if (check != checksum(&(packet.version), sizeof(struct iphdr))) {
			LOG("bad IP header checksum, ignoring\n");
			LOG("check received = %X, should be %X",check, checksum(&(packet.version), sizeof(struct iphdr)));
			ret = L_FAIL;
			goto EXIT;
		}

		LOG("oooooh!!! got some!\n");

		if (nvram_match("wan_proto", "pptp")) {
			inet_aton(nvram_safe_get("pptp_server_ip"), &ipaddr);
		}
		else if (nvram_match("wan_proto", "l2tp")) {
#ifdef TCONFIG_L2TP
			inet_aton(nvram_safe_get("lan_ipaddr"), &ipaddr);	// checkme: why?	zzz
#endif
		}
		else {
			inet_aton(nvram_safe_get("wan_ipaddr"), &ipaddr);
		}
		inet_aton(nvram_safe_get("wan_netmask"), &netmask);
		LOG("gateway=%08x", ipaddr.s_addr);
		LOG("netmask=%08x", netmask.s_addr);

		if ((ipaddr.s_addr & netmask.s_addr) != (*(u_int32_t *)&(packet.daddr) & netmask.s_addr)) {
			if (nvram_match("wan_proto", "l2tp")) {
				ret = L_SUCCESS;
				goto EXIT;
			}
			else {
				ret = L_FAIL;
				goto EXIT;
			}
		}
	}

EXIT:
	if (fd) close(fd);
	return ret;
}

int listen_main(int argc, char *argv[])
{
	char *interface;

	if (argc < 2) {
		usage_exit(argv[0], "<interface>");
	}

	interface = argv[1];
	printf("Starting listen on %s\n", interface);

	if (fork() != 0) return 0;

	while (1) {
		switch (listen_interface(interface)) {
		case L_SUCCESS:
			LOG("\n*** LAN to WAN packet received\n\n");
			force_to_dial();

			if (check_wanup()) return 0;

			// Connect fail, we want to re-connect session
			sleep(3);
			break;
		case L_UPGRADE:
			LOG("listen: nothing to do...\n");
			return 0;
		case L_ESTABLISHED:
			LOG("The link had been established\n");
			return 0;
		case L_ERROR:
			LOG("ERROR\n");
			return 0;
/*		case L_FAIL:
			break;	*/
		}
	}
}