|
@@ -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
|
+};
|