4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
28 #include "qemu/ctype.h"
29 #include "qemu/cutils.h"
31 #include <sys/ethernet.h>
32 #include <sys/sockio.h>
33 #include <netinet/arp.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_icmp.h> // must come after ip.h
38 #include <netinet/udp.h>
39 #include <netinet/tcp.h>
42 #include "qemu/error-report.h"
44 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
50 sbuf
.buf
= (char *)buf
;
52 return getmsg(tapfd
, NULL
, &sbuf
, &f
) >= 0 ? sbuf
.len
: -1;
55 #define TUNNEWPPA (('T'<<16) | 0x0001)
57 * Allocate TAP device, returns opened fd.
58 * Stores dev name in the first arg(must be large enough).
60 static int tap_alloc(char *dev
, size_t dev_size
, Error
**errp
)
62 /* FIXME leaks like a sieve on error paths */
63 /* FIXME suspicious: many errors are reported, then ignored */
64 int tap_fd
, if_fd
, ppa
= -1;
68 static int arp_fd
= 0;
69 int ip_muxid
, arp_muxid
;
70 struct strioctl strioc_if
, strioc_ppa
;
71 int link_type
= I_PLINK
;
73 char actual_name
[32] = "";
75 memset(&ifr
, 0x0, sizeof(ifr
));
79 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
83 /* Check if IP device was opened */
87 ip_fd
= RETRY_ON_EINTR(open("/dev/udp", O_RDWR
, 0));
89 error_setg(errp
, "Can't open /dev/ip (actually /dev/udp)");
93 tap_fd
= RETRY_ON_EINTR(open("/dev/tap", O_RDWR
, 0));
95 error_setg(errp
, "Can't open /dev/tap");
99 /* Assign a new PPA and get its unit number. */
100 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
101 strioc_ppa
.ic_timout
= 0;
102 strioc_ppa
.ic_len
= sizeof(ppa
);
103 strioc_ppa
.ic_dp
= (char *)&ppa
;
104 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
105 error_report("Can't assign new interface");
107 if_fd
= RETRY_ON_EINTR(open("/dev/tap", O_RDWR
, 0));
109 error_setg(errp
, "Can't open /dev/tap (2)");
112 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
113 error_setg(errp
, "Can't push IP module");
117 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
118 error_report("Can't get flags");
120 snprintf (actual_name
, 32, "tap%d", ppa
);
121 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
124 /* Assign ppa according to the unit number returned by tun device */
126 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
127 error_report("Can't set PPA %d", ppa
);
128 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
129 error_report("Can't get flags");
130 /* Push arp module to if_fd */
131 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
132 error_report("Can't push ARP module (2)");
134 /* Push arp module to ip_fd */
135 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
136 error_report("I_POP failed");
137 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
138 error_report("Can't push ARP module (3)");
140 arp_fd
= RETRY_ON_EINTR(open("/dev/tap", O_RDWR
, 0));
142 error_report("Can't open %s", "/dev/tap");
144 /* Set ifname to arp */
145 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
146 strioc_if
.ic_timout
= 0;
147 strioc_if
.ic_len
= sizeof(ifr
);
148 strioc_if
.ic_dp
= (char *)&ifr
;
149 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
150 error_report("Can't set ifname to arp");
153 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
154 error_setg(errp
, "Can't link TAP device to IP");
158 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
159 error_report("Can't link TAP device to ARP");
163 memset(&ifr
, 0x0, sizeof(ifr
));
164 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
165 ifr
.lifr_ip_muxid
= ip_muxid
;
166 ifr
.lifr_arp_muxid
= arp_muxid
;
168 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
170 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
171 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
172 error_report("Can't set multiplexor id");
175 snprintf(dev
, dev_size
, "tap%d", ppa
);
179 int tap_open(char *ifname
, int ifname_size
, int *vnet_hdr
,
180 int vnet_hdr_required
, int mq_required
, Error
**errp
)
185 fd
= tap_alloc(dev
, sizeof(dev
), errp
);
189 pstrcpy(ifname
, ifname_size
, dev
);
191 /* Solaris doesn't have IFF_VNET_HDR */
194 if (vnet_hdr_required
&& !*vnet_hdr
) {
195 error_setg(errp
, "vnet_hdr=1 requested, but no kernel "
196 "support for IFF_VNET_HDR available");
201 g_unix_set_fd_nonblocking(fd
, true, NULL
);
205 void tap_set_sndbuf(int fd
, const NetdevTapOptions
*tap
, Error
**errp
)
209 int tap_probe_vnet_hdr(int fd
, Error
**errp
)
214 int tap_probe_has_ufo(int fd
)
219 int tap_probe_has_uso(int fd
)
224 int tap_probe_vnet_hdr_len(int fd
, int len
)
229 void tap_fd_set_vnet_hdr_len(int fd
, int len
)
233 int tap_fd_set_vnet_le(int fd
, int is_le
)
238 int tap_fd_set_vnet_be(int fd
, int is_be
)
243 void tap_fd_set_offload(int fd
, int csum
, int tso4
,
244 int tso6
, int ecn
, int ufo
, int uso4
, int uso6
)
248 int tap_fd_enable(int fd
)
253 int tap_fd_disable(int fd
)
258 int tap_fd_get_ifname(int fd
, char *ifname
)
263 int tap_fd_set_steering_ebpf(int fd
, int prog_fd
)