blob: d85224242b30045dc6b037e84fa0ef18a113483f [file] [log] [blame]
Mark McLoughlin966ea5e2009-10-22 17:49:09 +01001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydell2744d922016-01-29 17:50:00 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020027#include "tap_int.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020028#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020029#include "qemu/cutils.h"
Peter Maydell89615cf2020-07-04 10:23:17 +010030#include "qemu-common.h"
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010031
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010032#include <sys/ethernet.h>
33#include <sys/sockio.h>
34#include <netinet/arp.h>
35#include <netinet/in.h>
36#include <netinet/in_systm.h>
37#include <netinet/ip.h>
38#include <netinet/ip_icmp.h> // must come after ip.h
39#include <netinet/udp.h>
40#include <netinet/tcp.h>
41#include <net/if.h>
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010042#include <stropts.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/error-report.h"
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010044
45ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
46{
47 struct strbuf sbuf;
48 int f = 0;
49
50 sbuf.maxlen = maxlen;
51 sbuf.buf = (char *)buf;
52
53 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
54}
55
56#define TUNNEWPPA (('T'<<16) | 0x0001)
57/*
58 * Allocate TAP device, returns opened fd.
59 * Stores dev name in the first arg(must be large enough).
60 */
Markus Armbruster576c6eb2015-05-15 13:59:01 +020061static int tap_alloc(char *dev, size_t dev_size, Error **errp)
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010062{
Markus Armbruster576c6eb2015-05-15 13:59:01 +020063 /* FIXME leaks like a sieve on error paths */
64 /* FIXME suspicious: many errors are reported, then ignored */
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010065 int tap_fd, if_fd, ppa = -1;
66 static int ip_fd = 0;
67 char *ptr;
68
69 static int arp_fd = 0;
70 int ip_muxid, arp_muxid;
71 struct strioctl strioc_if, strioc_ppa;
Dong Xu Wang3a931132011-11-29 16:52:38 +080072 int link_type = I_PLINK;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010073 struct lifreq ifr;
74 char actual_name[32] = "";
75
76 memset(&ifr, 0x0, sizeof(ifr));
77
78 if( *dev ){
79 ptr = dev;
80 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
81 ppa = atoi(ptr);
82 }
83
84 /* Check if IP device was opened */
85 if( ip_fd )
86 close(ip_fd);
87
88 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
89 if (ip_fd < 0) {
Markus Armbruster576c6eb2015-05-15 13:59:01 +020090 error_setg(errp, "Can't open /dev/ip (actually /dev/udp)");
91 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010092 }
93
94 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
95 if (tap_fd < 0) {
Markus Armbruster576c6eb2015-05-15 13:59:01 +020096 error_setg(errp, "Can't open /dev/tap");
97 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +010098 }
99
100 /* Assign a new PPA and get its unit number. */
101 strioc_ppa.ic_cmd = TUNNEWPPA;
102 strioc_ppa.ic_timout = 0;
103 strioc_ppa.ic_len = sizeof(ppa);
104 strioc_ppa.ic_dp = (char *)&ppa;
105 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200106 error_report("Can't assign new interface");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100107
108 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
109 if (if_fd < 0) {
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200110 error_setg(errp, "Can't open /dev/tap (2)");
111 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100112 }
113 if(ioctl(if_fd, I_PUSH, "ip") < 0){
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200114 error_setg(errp, "Can't push IP module");
115 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100116 }
117
118 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200119 error_report("Can't get flags");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100120
121 snprintf (actual_name, 32, "tap%d", ppa);
122 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
123
124 ifr.lifr_ppa = ppa;
125 /* Assign ppa according to the unit number returned by tun device */
126
127 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200128 error_report("Can't set PPA %d", ppa);
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100129 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200130 error_report("Can't get flags");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100131 /* Push arp module to if_fd */
132 if (ioctl (if_fd, I_PUSH, "arp") < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200133 error_report("Can't push ARP module (2)");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100134
135 /* Push arp module to ip_fd */
136 if (ioctl (ip_fd, I_POP, NULL) < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200137 error_report("I_POP failed");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100138 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200139 error_report("Can't push ARP module (3)");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100140 /* Open arp_fd */
141 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
142 if (arp_fd < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200143 error_report("Can't open %s", "/dev/tap");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100144
145 /* Set ifname to arp */
146 strioc_if.ic_cmd = SIOCSLIFNAME;
147 strioc_if.ic_timout = 0;
148 strioc_if.ic_len = sizeof(ifr);
149 strioc_if.ic_dp = (char *)&ifr;
150 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200151 error_report("Can't set ifname to arp");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100152 }
153
154 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200155 error_setg(errp, "Can't link TAP device to IP");
156 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100157 }
158
159 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200160 error_report("Can't link TAP device to ARP");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100161
162 close (if_fd);
163
164 memset(&ifr, 0x0, sizeof(ifr));
165 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
166 ifr.lifr_ip_muxid = ip_muxid;
167 ifr.lifr_arp_muxid = arp_muxid;
168
169 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
170 {
171 ioctl (ip_fd, I_PUNLINK , arp_muxid);
172 ioctl (ip_fd, I_PUNLINK, ip_muxid);
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200173 error_report("Can't set multiplexor id");
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100174 }
175
176 snprintf(dev, dev_size, "tap%d", ppa);
177 return tap_fd;
178}
179
Jason Wang264986e2013-01-30 19:12:34 +0800180int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
Markus Armbruster468dd822015-05-15 13:58:58 +0200181 int vnet_hdr_required, int mq_required, Error **errp)
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100182{
183 char dev[10]="";
184 int fd;
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200185
186 fd = tap_alloc(dev, sizeof(dev), errp);
187 if (fd < 0) {
188 return -1;
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100189 }
190 pstrcpy(ifname, ifname_size, dev);
Mark McLoughlinf5c5e382009-11-25 18:49:37 +0000191 if (*vnet_hdr) {
192 /* Solaris doesn't have IFF_VNET_HDR */
193 *vnet_hdr = 0;
194
195 if (vnet_hdr_required && !*vnet_hdr) {
Markus Armbruster576c6eb2015-05-15 13:59:01 +0200196 error_setg(errp, "vnet_hdr=1 requested, but no kernel "
197 "support for IFF_VNET_HDR available");
Mark McLoughlinf5c5e382009-11-25 18:49:37 +0000198 close(fd);
199 return -1;
200 }
201 }
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100202 fcntl(fd, F_SETFL, O_NONBLOCK);
203 return fd;
204}
Mark McLoughlin15ac9132009-10-22 17:49:13 +0100205
Markus Armbruster80b832c2015-05-15 13:58:55 +0200206void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
Mark McLoughlin15ac9132009-10-22 17:49:13 +0100207{
Mark McLoughlin15ac9132009-10-22 17:49:13 +0100208}
Mark McLoughlindc690042009-10-22 17:49:14 +0100209
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200210int tap_probe_vnet_hdr(int fd, Error **errp)
Mark McLoughlindc690042009-10-22 17:49:14 +0100211{
212 return 0;
213}
Mark McLoughlin1faac1f2009-10-22 17:49:15 +0100214
Mark McLoughlin9c282712009-10-22 17:49:16 +0100215int tap_probe_has_ufo(int fd)
216{
217 return 0;
218}
219
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300220int tap_probe_vnet_hdr_len(int fd, int len)
221{
222 return 0;
223}
224
225void tap_fd_set_vnet_hdr_len(int fd, int len)
226{
227}
228
Michael S. Tsirkin4ee9b432015-06-18 16:52:23 +0200229int tap_fd_set_vnet_le(int fd, int is_le)
230{
231 return -EINVAL;
232}
233
234int tap_fd_set_vnet_be(int fd, int is_be)
235{
236 return -EINVAL;
237}
238
Mark McLoughlin1faac1f2009-10-22 17:49:15 +0100239void tap_fd_set_offload(int fd, int csum, int tso4,
240 int tso6, int ecn, int ufo)
241{
242}
Jason Wang94fdc6d2013-01-30 19:12:31 +0800243
244int tap_fd_enable(int fd)
245{
246 return -1;
247}
248
249int tap_fd_disable(int fd)
250{
251 return -1;
252}
253
Jason Wange5dc0b42013-01-30 19:12:33 +0800254int tap_fd_get_ifname(int fd, char *ifname)
255{
256 return -1;
257}
Andrew Melnychenko8f364e32021-05-14 14:48:30 +0300258
259int tap_fd_set_steering_ebpf(int fd, int prog_fd)
260{
261 return -1;
262}