Bläddra i källkod

[af_packet] Add new AF_PACKET driver for Linux

This code largely inspired by tap.c.  Allows for testing iPXE on real
NICs from within Linux.  For example:

  make bin-x86_64-linux/af_packet.linux
  valgrind ./bin-x86_64-linux/af_packet.linux --net af_packet,if=eth3

Tested as x86_64 and i386 binary.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
David Decotigny 7 år sedan
förälder
incheckning
b6f524388b

+ 39
- 0
src/arch/x86/core/linux/linux_api.c Visa fil

@@ -108,3 +108,42 @@ void * linux_mremap ( void *old_address, __kernel_size_t old_size,
108 108
 int linux_munmap ( void *addr, __kernel_size_t length ) {
109 109
 	return linux_syscall ( __NR_munmap, addr, length );
110 110
 }
111
+
112
+int linux_socket ( int domain, int type_, int protocol ) {
113
+#ifdef __NR_socket
114
+	return linux_syscall ( __NR_socket, domain, type_, protocol );
115
+#else
116
+#ifndef SOCKOP_socket
117
+# define SOCKOP_socket 1
118
+#endif
119
+	unsigned long sc_args[] = { domain, type_, protocol };
120
+	return linux_syscall ( __NR_socketcall, SOCKOP_socket, sc_args );
121
+#endif
122
+}
123
+
124
+int linux_bind ( int fd, const struct sockaddr *addr, socklen_t addrlen ) {
125
+#ifdef __NR_bind
126
+	return linux_syscall ( __NR_bind, fd, addr, addrlen );
127
+#else
128
+#ifndef SOCKOP_bind
129
+# define SOCKOP_bind 2
130
+#endif
131
+	unsigned long sc_args[] = { fd, (unsigned long)addr, addrlen };
132
+	return linux_syscall ( __NR_socketcall, SOCKOP_bind, sc_args );
133
+#endif
134
+}
135
+
136
+ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
137
+		       const struct sockaddr *daddr, socklen_t addrlen ) {
138
+#ifdef __NR_sendto
139
+	return linux_syscall ( __NR_sendto, fd, buf, len, flags,
140
+			       daddr, addrlen );
141
+#else
142
+#ifndef SOCKOP_sendto
143
+# define SOCKOP_sendto 11
144
+#endif
145
+	unsigned long sc_args[] = { fd, (unsigned long)buf, len,
146
+				    flags, (unsigned long)daddr, addrlen };
147
+	return linux_syscall ( __NR_socketcall, SOCKOP_sendto, sc_args );
148
+#endif
149
+}

+ 325
- 0
src/drivers/linux/af_packet.c Visa fil

@@ -0,0 +1,325 @@
1
+/*
2
+ * Copyright (C) 2016 David Decotigny <ddecotig@gmail.com>
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
+ */
18
+
19
+#include <errno.h>
20
+#include <string.h>
21
+#include <stdio.h>
22
+#include <linux_api.h>
23
+#include <ipxe/list.h>
24
+#include <ipxe/linux.h>
25
+#include <ipxe/malloc.h>
26
+#include <ipxe/device.h>
27
+#include <ipxe/netdevice.h>
28
+#include <ipxe/iobuf.h>
29
+#include <ipxe/ethernet.h>
30
+#include <ipxe/settings.h>
31
+#include <ipxe/socket.h>
32
+
33
+/* This hack prevents pre-2.6.32 headers from redefining struct sockaddr */
34
+#define __GLIBC__ 2
35
+#include <linux/socket.h>
36
+#include <linux/if.h>
37
+#include <linux/if_ether.h>
38
+#include <linux/if_packet.h>
39
+#undef __GLIBC__
40
+#include <byteswap.h>
41
+
42
+/* linux-specifc syscall params */
43
+#define LINUX_AF_PACKET 17
44
+#define LINUX_SOCK_RAW 3
45
+#define LINUX_SIOCGIFINDEX 0x8933
46
+#define LINUX_SIOCGIFHWADDR 0x8927
47
+
48
+#define RX_BUF_SIZE 1536
49
+
50
+/** @file
51
+ *
52
+ * The AF_PACKET driver.
53
+ *
54
+ * Bind to an existing linux network interface.
55
+ */
56
+
57
+struct af_packet_nic {
58
+	/** Linux network interface name */
59
+	char * ifname;
60
+	/** Packet socket descriptor */
61
+	int fd;
62
+	/** ifindex */
63
+	int ifindex;
64
+};
65
+
66
+/** Open the linux interface */
67
+static int af_packet_nic_open ( struct net_device * netdev )
68
+{
69
+	struct af_packet_nic * nic = netdev->priv;
70
+	struct sockaddr_ll socket_address;
71
+	struct ifreq if_data;
72
+	int ret;
73
+
74
+	nic->fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW,
75
+			       htons(ETH_P_ALL));
76
+	if (nic->fd < 0) {
77
+		DBGC(nic, "af_packet %p socket(AF_PACKET) = %d (%s)\n",
78
+		     nic, nic->fd, linux_strerror(linux_errno));
79
+		return nic->fd;
80
+	}
81
+
82
+	/* resolve ifindex of ifname */
83
+	memset(&if_data, 0, sizeof(if_data));
84
+	strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
85
+	ret = linux_ioctl(nic->fd, LINUX_SIOCGIFINDEX, &if_data);
86
+	if (ret < 0) {
87
+		DBGC(nic, "af_packet %p ioctl(SIOCGIFINDEX) = %d (%s)\n",
88
+		     nic, ret, linux_strerror(linux_errno));
89
+		linux_close(nic->fd);
90
+		return ret;
91
+	}
92
+
93
+	nic->ifindex = if_data.ifr_ifindex;
94
+
95
+	/* bind to interface */
96
+	memset(&socket_address, 0, sizeof(socket_address));
97
+	socket_address.sll_family = LINUX_AF_PACKET;
98
+	socket_address.sll_ifindex = nic->ifindex;
99
+	socket_address.sll_protocol = htons(ETH_P_ALL);
100
+	ret = linux_bind(nic->fd, (void *) &socket_address,
101
+			 sizeof(socket_address));
102
+	if (ret == -1) {
103
+		DBGC(nic, "af_packet %p bind() = %d (%s)\n",
104
+		     nic, ret, linux_strerror(linux_errno));
105
+		linux_close(nic->fd);
106
+		return ret;
107
+	}
108
+
109
+	/* Set nonblocking mode to make af_packet_nic_poll() easier */
110
+	ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
111
+	if (ret != 0) {
112
+		DBGC(nic, "af_packet %p fcntl(%d, ...) = %d (%s)\n",
113
+		     nic, nic->fd, ret, linux_strerror(linux_errno));
114
+		linux_close(nic->fd);
115
+		return ret;
116
+	}
117
+
118
+	return 0;
119
+}
120
+
121
+/** Close the packet socket */
122
+static void af_packet_nic_close ( struct net_device *netdev )
123
+{
124
+	struct af_packet_nic * nic = netdev->priv;
125
+	linux_close(nic->fd);
126
+}
127
+
128
+/**
129
+ * Transmit an ethernet packet.
130
+ *
131
+ * The packet can be written to the socket and marked as complete immediately.
132
+ */
133
+static int af_packet_nic_transmit ( struct net_device *netdev,
134
+				    struct io_buffer *iobuf )
135
+{
136
+	struct af_packet_nic * nic = netdev->priv;
137
+	struct sockaddr_ll socket_address;
138
+	const struct ethhdr * eh;
139
+	int rc;
140
+
141
+	memset(&socket_address, 0, sizeof(socket_address));
142
+	socket_address.sll_family = LINUX_AF_PACKET;
143
+	socket_address.sll_ifindex = nic->ifindex;
144
+	socket_address.sll_halen = ETH_ALEN;
145
+
146
+	eh = iobuf->data;
147
+	memcpy(socket_address.sll_addr, eh->h_dest, ETH_ALEN);
148
+
149
+	rc = linux_sendto(nic->fd, iobuf->data, iobuf->tail - iobuf->data,
150
+			  0, (struct sockaddr *)&socket_address,
151
+			  sizeof(socket_address));
152
+
153
+	DBGC2(nic, "af_packet %p wrote %d bytes\n", nic, rc);
154
+	netdev_tx_complete(netdev, iobuf);
155
+
156
+	return 0;
157
+}
158
+
159
+/** Poll for new packets */
160
+static void af_packet_nic_poll ( struct net_device *netdev )
161
+{
162
+	struct af_packet_nic * nic = netdev->priv;
163
+	struct pollfd pfd;
164
+	struct io_buffer * iobuf;
165
+	int r;
166
+
167
+	pfd.fd = nic->fd;
168
+	pfd.events = POLLIN;
169
+	if (linux_poll(&pfd, 1, 0) == -1) {
170
+		DBGC(nic, "af_packet %p poll failed (%s)\n",
171
+		     nic, linux_strerror(linux_errno));
172
+		return;
173
+	}
174
+	if ((pfd.revents & POLLIN) == 0)
175
+		return;
176
+
177
+	/* At this point we know there is at least one new packet to be read */
178
+
179
+	iobuf = alloc_iob(RX_BUF_SIZE);
180
+	if (! iobuf)
181
+		goto allocfail;
182
+
183
+	while ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0) {
184
+		DBGC2(nic, "af_packet %p read %d bytes\n", nic, r);
185
+
186
+		iob_put(iobuf, r);
187
+		netdev_rx(netdev, iobuf);
188
+
189
+		iobuf = alloc_iob(RX_BUF_SIZE);
190
+		if (! iobuf)
191
+			goto allocfail;
192
+	}
193
+
194
+	free_iob(iobuf);
195
+	return;
196
+
197
+allocfail:
198
+	DBGC(nic, "af_packet %p alloc_iob failed\n", nic);
199
+}
200
+
201
+/**
202
+ * Set irq.
203
+ *
204
+ * Not used on linux, provide a dummy implementation.
205
+ */
206
+static void af_packet_nic_irq ( struct net_device *netdev, int enable )
207
+{
208
+	struct af_packet_nic *nic = netdev->priv;
209
+
210
+	DBGC(nic, "af_packet %p irq enable = %d\n", nic, enable);
211
+}
212
+
213
+
214
+static int af_packet_update_properties ( struct net_device *netdev )
215
+{
216
+	struct af_packet_nic *nic = netdev->priv;
217
+	struct ifreq if_data;
218
+	int ret;
219
+
220
+	/* retrieve default MAC address */
221
+	int fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW, 0);
222
+	if (fd < 0) {
223
+		DBGC(nic, "af_packet %p cannot create raw socket (%s)\n",
224
+		     nic, linux_strerror(linux_errno));
225
+		return fd;
226
+	}
227
+
228
+	/* retrieve host's MAC address */
229
+	memset(&if_data, 0, sizeof(if_data));
230
+	strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
231
+	ret = linux_ioctl(fd, LINUX_SIOCGIFHWADDR, &if_data);
232
+	if (ret < 0) {
233
+		DBGC(nic, "af_packet %p cannot get mac addr (%s)\n",
234
+		     nic, linux_strerror(linux_errno));
235
+		linux_close(fd);
236
+		return ret;
237
+	}
238
+
239
+	linux_close(fd);
240
+	/* struct sockaddr = { u16 family, u8 pad[14] (equiv. sa_data) }; */
241
+	memcpy(netdev->ll_addr, if_data.ifr_hwaddr.pad, ETH_ALEN);
242
+	return 0;
243
+}
244
+
245
+/** AF_PACKET operations */
246
+static struct net_device_operations af_packet_nic_operations = {
247
+	.open		= af_packet_nic_open,
248
+	.close		= af_packet_nic_close,
249
+	.transmit	= af_packet_nic_transmit,
250
+	.poll		= af_packet_nic_poll,
251
+	.irq		= af_packet_nic_irq,
252
+};
253
+
254
+/** Handle a device request for the af_packet driver */
255
+static int af_packet_nic_probe ( struct linux_device *device,
256
+				 struct linux_device_request *request )
257
+{
258
+	struct linux_setting *if_setting;
259
+	struct net_device *netdev;
260
+	struct af_packet_nic *nic;
261
+	int rc;
262
+
263
+	netdev = alloc_etherdev(sizeof(*nic));
264
+	if (! netdev)
265
+		return -ENOMEM;
266
+
267
+	netdev_init(netdev, &af_packet_nic_operations);
268
+	nic = netdev->priv;
269
+	linux_set_drvdata(device, netdev);
270
+	netdev->dev = &device->dev;
271
+
272
+	memset(nic, 0, sizeof(*nic));
273
+
274
+	/* Look for the mandatory if setting */
275
+	if_setting = linux_find_setting("if", &request->settings);
276
+
277
+	/* No if setting */
278
+	if (! if_setting) {
279
+		printf("af_packet missing a mandatory if setting\n");
280
+		rc = -EINVAL;
281
+		goto err_settings;
282
+	}
283
+
284
+	nic->ifname = if_setting->value;
285
+	snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
286
+		   nic->ifname );
287
+	device->dev.desc.bus_type = BUS_TYPE_TAP;
288
+	af_packet_update_properties(netdev);
289
+	if_setting->applied = 1;
290
+
291
+	/* Apply rest of the settings */
292
+	linux_apply_settings(&request->settings, &netdev->settings.settings);
293
+
294
+	/* Register network device */
295
+	if ((rc = register_netdev(netdev)) != 0)
296
+		goto err_register;
297
+
298
+	netdev_link_up(netdev);
299
+
300
+	return 0;
301
+
302
+err_settings:
303
+	unregister_netdev(netdev);
304
+err_register:
305
+	netdev_nullify(netdev);
306
+	netdev_put(netdev);
307
+	return rc;
308
+}
309
+
310
+/** Remove the device */
311
+static void af_packet_nic_remove ( struct linux_device *device )
312
+{
313
+	struct net_device *netdev = linux_get_drvdata(device);
314
+	unregister_netdev(netdev);
315
+	netdev_nullify(netdev);
316
+	netdev_put(netdev);
317
+}
318
+
319
+/** AF_PACKET linux_driver */
320
+struct linux_driver af_packet_nic_driver __linux_driver = {
321
+	.name = "af_packet",
322
+	.probe = af_packet_nic_probe,
323
+	.remove = af_packet_nic_remove,
324
+	.can_probe = 1,
325
+};

+ 1
- 0
src/include/ipxe/errfile.h Visa fil

@@ -194,6 +194,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
194 194
 #define ERRFILE_pciea		     ( ERRFILE_DRIVER | 0x00c00000 )
195 195
 #define ERRFILE_axge		     ( ERRFILE_DRIVER | 0x00c10000 )
196 196
 #define ERRFILE_thunderx	     ( ERRFILE_DRIVER | 0x00c20000 )
197
+#define ERRFILE_af_packet	     ( ERRFILE_DRIVER | 0x00c30000 )
197 198
 
198 199
 #define ERRFILE_aoe			( ERRFILE_NET | 0x00000000 )
199 200
 #define ERRFILE_arp			( ERRFILE_NET | 0x00010000 )

+ 7
- 0
src/include/linux_api.h Visa fil

@@ -46,6 +46,8 @@ typedef __kernel_loff_t loff_t;
46 46
 #include <linux/poll.h>
47 47
 typedef unsigned long nfds_t;
48 48
 typedef uint32_t useconds_t;
49
+typedef uint32_t socklen_t;
50
+struct sockaddr;
49 51
 #define MAP_FAILED ( ( void * ) -1 )
50 52
 #define SEEK_SET 0
51 53
 
@@ -68,6 +70,11 @@ extern void * linux_mmap ( void *addr, __kernel_size_t length, int prot,
68 70
 extern void * linux_mremap ( void *old_address, __kernel_size_t old_size,
69 71
 			     __kernel_size_t new_size, int flags );
70 72
 extern int linux_munmap ( void *addr, __kernel_size_t length );
73
+extern int linux_socket ( int domain, int type_, int protocol );
74
+extern int linux_bind ( int fd, const struct sockaddr *addr,
75
+			socklen_t addrlen );
76
+extern ssize_t linux_sendto ( int fd, const void *buf, size_t len, int flags,
77
+			      const struct sockaddr *daddr, socklen_t addrlen );
71 78
 
72 79
 extern const char * linux_strerror ( int errnum );
73 80
 

Laddar…
Avbryt
Spara