1 /*
2 * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@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 _SYS_SOCKET_H
35 #define __GLIBC__ 2
36 #include <linux/socket.h>
37 #undef __GLIBC__
38 #include <linux/if.h>
39 #include <linux/if_ether.h>
40 #include <linux/if_tun.h>
41
42 #define RX_BUF_SIZE 1536
43
44 /** @file
45 *
46 * The TAP driver.
47 *
48 * The TAP is a Virtual Ethernet network device.
49 */
50
51 struct tap_nic {
52 /** Tap interface name */
53 char * interface;
54 /** File descriptor of the opened tap device */
55 int fd;
56 };
57
58 /** Open the TAP device */
tap_open(struct net_device * netdev)59 static int tap_open(struct net_device * netdev)
60 {
61 struct tap_nic * nic = netdev->priv;
62 struct ifreq ifr;
63 int ret;
64
65 nic->fd = linux_open("/dev/net/tun", O_RDWR);
66 if (nic->fd < 0) {
67 DBGC(nic, "tap %p open('/dev/net/tun') = %d (%s)\n", nic, nic->fd, linux_strerror(linux_errno));
68 return nic->fd;
69 }
70
71 memset(&ifr, 0, sizeof(ifr));
72 /* IFF_NO_PI for no extra packet information */
73 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
74 strncpy(ifr.ifr_name, nic->interface, IFNAMSIZ);
75 DBGC(nic, "tap %p interface = '%s'\n", nic, nic->interface);
76
77 ret = linux_ioctl(nic->fd, TUNSETIFF, &ifr);
78
79 if (ret != 0) {
80 DBGC(nic, "tap %p ioctl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
81 linux_close(nic->fd);
82 return ret;
83 }
84
85 /* Set nonblocking mode to make tap_poll easier */
86 ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
87
88 if (ret != 0) {
89 DBGC(nic, "tap %p fcntl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
90 linux_close(nic->fd);
91 return ret;
92 }
93
94 return 0;
95 }
96
97 /** Close the TAP device */
tap_close(struct net_device * netdev)98 static void tap_close(struct net_device *netdev)
99 {
100 struct tap_nic * nic = netdev->priv;
101 linux_close(nic->fd);
102 }
103
104 /**
105 * Transmit an ethernet packet.
106 *
107 * The packet can be written to the TAP device and marked as complete immediately.
108 */
tap_transmit(struct net_device * netdev,struct io_buffer * iobuf)109 static int tap_transmit(struct net_device *netdev, struct io_buffer *iobuf)
110 {
111 struct tap_nic * nic = netdev->priv;
112 int rc;
113
114 /* Pad and align packet */
115 iob_pad(iobuf, ETH_ZLEN);
116
117 rc = linux_write(nic->fd, iobuf->data, iobuf->tail - iobuf->data);
118 DBGC2(nic, "tap %p wrote %d bytes\n", nic, rc);
119 netdev_tx_complete(netdev, iobuf);
120
121 return 0;
122 }
123
124 /** Poll for new packets */
tap_poll(struct net_device * netdev)125 static void tap_poll(struct net_device *netdev)
126 {
127 struct tap_nic * nic = netdev->priv;
128 struct pollfd pfd;
129 struct io_buffer * iobuf;
130 int r;
131
132 pfd.fd = nic->fd;
133 pfd.events = POLLIN;
134 if (linux_poll(&pfd, 1, 0) == -1) {
135 DBGC(nic, "tap %p poll failed (%s)\n", nic, linux_strerror(linux_errno));
136 return;
137 }
138 if ((pfd.revents & POLLIN) == 0)
139 return;
140
141 /* At this point we know there is at least one new packet to be read */
142
143 iobuf = alloc_iob(RX_BUF_SIZE);
144 if (! iobuf)
145 goto allocfail;
146
147 while ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0) {
148 DBGC2(nic, "tap %p read %d bytes\n", nic, r);
149
150 iob_put(iobuf, r);
151 netdev_rx(netdev, iobuf);
152
153 iobuf = alloc_iob(RX_BUF_SIZE);
154 if (! iobuf)
155 goto allocfail;
156 }
157
158 free_iob(iobuf);
159 return;
160
161 allocfail:
162 DBGC(nic, "tap %p alloc_iob failed\n", nic);
163 }
164
165 /**
166 * Set irq.
167 *
168 * Not used on linux, provide a dummy implementation.
169 */
tap_irq(struct net_device * netdev,int enable)170 static void tap_irq(struct net_device *netdev, int enable)
171 {
172 struct tap_nic *nic = netdev->priv;
173
174 DBGC(nic, "tap %p irq enable = %d\n", nic, enable);
175 }
176
177 /** Tap operations */
178 static struct net_device_operations tap_operations = {
179 .open = tap_open,
180 .close = tap_close,
181 .transmit = tap_transmit,
182 .poll = tap_poll,
183 .irq = tap_irq,
184 };
185
186 /** Handle a device request for the tap driver */
tap_probe(struct linux_device * device,struct linux_device_request * request)187 static int tap_probe(struct linux_device *device, struct linux_device_request *request)
188 {
189 struct linux_setting *if_setting;
190 struct net_device *netdev;
191 struct tap_nic *nic;
192 int rc;
193
194 netdev = alloc_etherdev(sizeof(*nic));
195 if (! netdev)
196 return -ENOMEM;
197
198 netdev_init(netdev, &tap_operations);
199 nic = netdev->priv;
200 linux_set_drvdata(device, netdev);
201 netdev->dev = &device->dev;
202 memset(nic, 0, sizeof(*nic));
203
204 /* Look for the mandatory if setting */
205 if_setting = linux_find_setting("if", &request->settings);
206
207 /* No if setting */
208 if (! if_setting) {
209 printf("tap missing a mandatory if setting\n");
210 rc = -EINVAL;
211 goto err_settings;
212 }
213
214 nic->interface = if_setting->value;
215 snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
216 nic->interface );
217 device->dev.desc.bus_type = BUS_TYPE_TAP;
218 if_setting->applied = 1;
219
220 /* Apply rest of the settings */
221 linux_apply_settings(&request->settings, &netdev->settings.settings);
222
223 /* Register network device */
224 if ((rc = register_netdev(netdev)) != 0)
225 goto err_register;
226
227 netdev_link_up(netdev);
228
229 return 0;
230
231 err_settings:
232 unregister_netdev(netdev);
233 err_register:
234 netdev_nullify(netdev);
235 netdev_put(netdev);
236 return rc;
237 }
238
239 /** Remove the device */
tap_remove(struct linux_device * device)240 static void tap_remove(struct linux_device *device)
241 {
242 struct net_device *netdev = linux_get_drvdata(device);
243 unregister_netdev(netdev);
244 netdev_nullify(netdev);
245 netdev_put(netdev);
246 }
247
248 /** Tap linux_driver */
249 struct linux_driver tap_driver __linux_driver = {
250 .name = "tap",
251 .probe = tap_probe,
252 .remove = tap_remove,
253 .can_probe = 1,
254 };
255