File: network_bridge.c

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (266 lines) | stat: -rw-r--r-- 5,240 bytes parent folder | download
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
/*
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#define DBG_DUMP	0

#include "config.h"

#if defined(HAVE_LINUX_IF_TUN_H) || defined(HAVE_NET_IF_TUN_H)

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "glue.h"

#define INCLUDE
#include "arch_tap.c"
#undef INCLUDE

#include "network_bridge.h"

struct cpssp {
	struct sig_eth *port_eth;
#define STATE
#define NAME		tuntap
#include "arch_tap.c"
#undef NAME
#undef STATE
};

/* ------------------------------------------------------------------ */
/* tun/tap
 */

/*forward*/ static void
bridge_real_to_virt(struct cpssp * cpssp, unsigned char *buf, unsigned int bufsize);

#define BEHAVIOR
#define NAME		tuntap
#define NAME_(x)	tuntap_ ## x
#include "arch_tap.c"
#undef NAME_
#undef NAME
#undef BEHAVIOR

/* ------------------------------------------------------------------ */
/* debugging
 */

static int
dump(const char *string, const unsigned char *buf, int bufsize)
{
#if DBG_DUMP
	int i;
	char arp=0 ;

#if 0
	/* FIXME - won't work with CIM/bridge */
	static unsigned char broadcast[6] = {
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	};

	if (memcmp(buf, broadcast, 6) != 0
	 && memcmp(buf + 6, broadcast, 6) != 0) {
		for (i = 0; ; i++) {
			if (i == 16) {
				/* foreign packet => don't dump */
				/* fprintf(stderr, "foreign packet!\n") ; */
				return 0;
			}
			if (host[i].addr == 0
			 || host[i].port == 0) {
				continue;
			}
			if (memcmp(buf, host[i].mac, 6) == 0
			 || memcmp(buf + 6, host[i].mac, 6) == 0) {
				break;
			}
		}
	}
#endif

	if (((*((const char*)(&buf[12]))) == (char)0x08) &&
		((*((const char*)(&buf[13]))) == (char)0x06)) {
		arp = 1 ;
	}
	fprintf(stderr, "%s len(%d):", string, bufsize);
	for (i = 0; i < bufsize && i < (160 - strlen(string)) / 3; i++) {
		fprintf(stderr, " %02x", buf[i]);
		switch(i) {
		case 5:
		case 11:
			fprintf(stderr, " |") ; break ;
		case 13:
			fprintf(stderr, " ||\n\t") ; break ;
		case 15:
		case 17:
		case 18:
		case 19:
		case 21:
		case 27:
		case 31:
		case 37:
		case 41:
			if (arp) { fprintf(stderr, " |") ; } break ;
		default:
			break ;
		}
	}
	fprintf(stderr, "\n");
	return 1 ;
#endif
#if !DBG_DUMP
	return 0 ;
#endif
}

/* ------------------------------------------------------------------ */
/* called from tun/tap
 */

static void
bridge_real_to_virt(
	struct cpssp * cpssp,
	unsigned char *buf,
	unsigned int bufsize
)
{
	int dumped;

	dumped = dump("#### phys -> virt ### start\np->v", buf, bufsize);

	/* fix packet by padding with zeros */
	/* Linux bridging code seems to truncate padding ??! */
	while (bufsize < 60) {
		buf[bufsize++] = 0;
	}
	sig_eth_send(cpssp->port_eth, &cpssp->port_eth, buf, bufsize);

	if (dumped) { fprintf(stderr, "#### phys -> virt ### end\n") ;}
}

/* ------------------------------------------------------------------ */
/* callback for ethernet port
 */

static void
bridge_virt_to_real(void *_cpssp, const void *buf, unsigned int bufsize)
{
	struct cpssp * cpssp = (struct cpssp *) _cpssp;
	int dumped;
	int ret;

	dumped = dump("#### virt -> phys ### start\nv->p", buf, bufsize);

	ret = tuntap_send(cpssp, buf, bufsize);
	if (ret < 0) {
		fprintf(stderr, "%s: phys_send: %s.\n",
				progname,
				strerror(errno));
	}

	if (dumped) { fprintf(stderr, "#### virt -> phys ### end\n") ; }
}

/* ------------------------------------------------------------------ */
/* interface of module
 */

void *
network_bridge_create(
	const char *name,
	const char *interface,
	struct sig_manage *port_manage,
	struct sig_eth *port_eth
)
{
	static const struct sig_eth_funcs eth_funcs = {
		.recv = bridge_virt_to_real,
	};
	struct cpssp *cpssp;

	cpssp = shm_alloc(sizeof(*cpssp));
	assert(cpssp);
	memset(cpssp, 0, sizeof(*cpssp));

	assert(interface);

	tuntap_create(cpssp, interface);

	cpssp->port_eth = port_eth;
	sig_eth_connect(cpssp->port_eth, cpssp, &eth_funcs);

	return cpssp;
}

void
network_bridge_destroy(void *_cpssp)
{
	struct cpssp * cpssp = (struct cpssp *) _cpssp;

	tuntap_destroy(cpssp);

	shm_free(cpssp);
}

void
network_bridge_suspend(void *_cpssp, FILE *fComp)
{
	struct cpssp *cpssp = _cpssp;

	generic_suspend(cpssp, sizeof(*cpssp), fComp);
}

void
network_bridge_resume(void *_cpssp, FILE *fComp)
{
	struct cpssp *cpssp = _cpssp;

	generic_resume(cpssp, sizeof(*cpssp), fComp);
}

#else /* defined(HAVE_LINUX_IF_TUN_H) || defined(HAVE_NET_IF_TUN_H) */

#include <stdio.h>
#include "network_bridge.h"

void *
network_bridge_create(
	const char *name,
	const char *interface,
	struct sig_manage *port_manage,
	struct sig_eth *port_eth
)
{
	return NULL;
}

void
network_bridge_destroy(void *_cpssp)
{
}

void
network_bridge_suspend(void *_cpssp, FILE *fComp)
{
	struct cpssp *cpssp = _cpssp;

	generic_suspend(cpssp, sizeof(*cpssp), fComp);
}

void
network_bridge_resume(void *_cpssp, FILE *fComp)
{
	struct cpssp *cpssp = _cpssp;

	generic_resume(cpssp, sizeof(*cpssp), fComp);
}

#endif /* defined(HAVE_LINUX_IF_TUN_H) || defined(HAVE_NET_IF_TUN_H) */