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