]>
Commit | Line | Data |
---|---|---|
966ea5ec MM |
1 | /* |
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 | ||
2744d920 | 25 | #include "qemu/osdep.h" |
da34e65c | 26 | #include "qapi/error.h" |
1422e32d | 27 | #include "tap_int.h" |
9c17d615 | 28 | #include "sysemu/sysemu.h" |
966ea5ec | 29 | |
966ea5ec MM |
30 | #include <sys/ethernet.h> |
31 | #include <sys/sockio.h> | |
32 | #include <netinet/arp.h> | |
33 | #include <netinet/in.h> | |
34 | #include <netinet/in_systm.h> | |
35 | #include <netinet/ip.h> | |
36 | #include <netinet/ip_icmp.h> // must come after ip.h | |
37 | #include <netinet/udp.h> | |
38 | #include <netinet/tcp.h> | |
39 | #include <net/if.h> | |
966ea5ec | 40 | #include <stropts.h> |
1de7afc9 | 41 | #include "qemu/error-report.h" |
966ea5ec MM |
42 | |
43 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
44 | { | |
45 | struct strbuf sbuf; | |
46 | int f = 0; | |
47 | ||
48 | sbuf.maxlen = maxlen; | |
49 | sbuf.buf = (char *)buf; | |
50 | ||
51 | return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1; | |
52 | } | |
53 | ||
54 | #define TUNNEWPPA (('T'<<16) | 0x0001) | |
55 | /* | |
56 | * Allocate TAP device, returns opened fd. | |
57 | * Stores dev name in the first arg(must be large enough). | |
58 | */ | |
576c6eb6 | 59 | static int tap_alloc(char *dev, size_t dev_size, Error **errp) |
966ea5ec | 60 | { |
576c6eb6 MA |
61 | /* FIXME leaks like a sieve on error paths */ |
62 | /* FIXME suspicious: many errors are reported, then ignored */ | |
966ea5ec MM |
63 | int tap_fd, if_fd, ppa = -1; |
64 | static int ip_fd = 0; | |
65 | char *ptr; | |
66 | ||
67 | static int arp_fd = 0; | |
68 | int ip_muxid, arp_muxid; | |
69 | struct strioctl strioc_if, strioc_ppa; | |
3a93113a | 70 | int link_type = I_PLINK; |
966ea5ec MM |
71 | struct lifreq ifr; |
72 | char actual_name[32] = ""; | |
73 | ||
74 | memset(&ifr, 0x0, sizeof(ifr)); | |
75 | ||
76 | if( *dev ){ | |
77 | ptr = dev; | |
78 | while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; | |
79 | ppa = atoi(ptr); | |
80 | } | |
81 | ||
82 | /* Check if IP device was opened */ | |
83 | if( ip_fd ) | |
84 | close(ip_fd); | |
85 | ||
86 | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); | |
87 | if (ip_fd < 0) { | |
576c6eb6 MA |
88 | error_setg(errp, "Can't open /dev/ip (actually /dev/udp)"); |
89 | return -1; | |
966ea5ec MM |
90 | } |
91 | ||
92 | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); | |
93 | if (tap_fd < 0) { | |
576c6eb6 MA |
94 | error_setg(errp, "Can't open /dev/tap"); |
95 | return -1; | |
966ea5ec MM |
96 | } |
97 | ||
98 | /* Assign a new PPA and get its unit number. */ | |
99 | strioc_ppa.ic_cmd = TUNNEWPPA; | |
100 | strioc_ppa.ic_timout = 0; | |
101 | strioc_ppa.ic_len = sizeof(ppa); | |
102 | strioc_ppa.ic_dp = (char *)&ppa; | |
103 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) | |
576c6eb6 | 104 | error_report("Can't assign new interface"); |
966ea5ec MM |
105 | |
106 | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); | |
107 | if (if_fd < 0) { | |
576c6eb6 MA |
108 | error_setg(errp, "Can't open /dev/tap (2)"); |
109 | return -1; | |
966ea5ec MM |
110 | } |
111 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ | |
576c6eb6 MA |
112 | error_setg(errp, "Can't push IP module"); |
113 | return -1; | |
966ea5ec MM |
114 | } |
115 | ||
116 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) | |
576c6eb6 | 117 | error_report("Can't get flags"); |
966ea5ec MM |
118 | |
119 | snprintf (actual_name, 32, "tap%d", ppa); | |
120 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
121 | ||
122 | ifr.lifr_ppa = ppa; | |
123 | /* Assign ppa according to the unit number returned by tun device */ | |
124 | ||
125 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) | |
576c6eb6 | 126 | error_report("Can't set PPA %d", ppa); |
966ea5ec | 127 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
576c6eb6 | 128 | error_report("Can't get flags"); |
966ea5ec MM |
129 | /* Push arp module to if_fd */ |
130 | if (ioctl (if_fd, I_PUSH, "arp") < 0) | |
576c6eb6 | 131 | error_report("Can't push ARP module (2)"); |
966ea5ec MM |
132 | |
133 | /* Push arp module to ip_fd */ | |
134 | if (ioctl (ip_fd, I_POP, NULL) < 0) | |
576c6eb6 | 135 | error_report("I_POP failed"); |
966ea5ec | 136 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
576c6eb6 | 137 | error_report("Can't push ARP module (3)"); |
966ea5ec MM |
138 | /* Open arp_fd */ |
139 | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); | |
140 | if (arp_fd < 0) | |
576c6eb6 | 141 | error_report("Can't open %s", "/dev/tap"); |
966ea5ec MM |
142 | |
143 | /* Set ifname to arp */ | |
144 | strioc_if.ic_cmd = SIOCSLIFNAME; | |
145 | strioc_if.ic_timout = 0; | |
146 | strioc_if.ic_len = sizeof(ifr); | |
147 | strioc_if.ic_dp = (char *)𝔦 | |
148 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ | |
576c6eb6 | 149 | error_report("Can't set ifname to arp"); |
966ea5ec MM |
150 | } |
151 | ||
152 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ | |
576c6eb6 MA |
153 | error_setg(errp, "Can't link TAP device to IP"); |
154 | return -1; | |
966ea5ec MM |
155 | } |
156 | ||
157 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) | |
576c6eb6 | 158 | error_report("Can't link TAP device to ARP"); |
966ea5ec MM |
159 | |
160 | close (if_fd); | |
161 | ||
162 | memset(&ifr, 0x0, sizeof(ifr)); | |
163 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
164 | ifr.lifr_ip_muxid = ip_muxid; | |
165 | ifr.lifr_arp_muxid = arp_muxid; | |
166 | ||
167 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) | |
168 | { | |
169 | ioctl (ip_fd, I_PUNLINK , arp_muxid); | |
170 | ioctl (ip_fd, I_PUNLINK, ip_muxid); | |
576c6eb6 | 171 | error_report("Can't set multiplexor id"); |
966ea5ec MM |
172 | } |
173 | ||
174 | snprintf(dev, dev_size, "tap%d", ppa); | |
175 | return tap_fd; | |
176 | } | |
177 | ||
264986e2 | 178 | int tap_open(char *ifname, int ifname_size, int *vnet_hdr, |
468dd824 | 179 | int vnet_hdr_required, int mq_required, Error **errp) |
966ea5ec MM |
180 | { |
181 | char dev[10]=""; | |
182 | int fd; | |
576c6eb6 MA |
183 | |
184 | fd = tap_alloc(dev, sizeof(dev), errp); | |
185 | if (fd < 0) { | |
186 | return -1; | |
966ea5ec MM |
187 | } |
188 | pstrcpy(ifname, ifname_size, dev); | |
f5c5e381 MM |
189 | if (*vnet_hdr) { |
190 | /* Solaris doesn't have IFF_VNET_HDR */ | |
191 | *vnet_hdr = 0; | |
192 | ||
193 | if (vnet_hdr_required && !*vnet_hdr) { | |
576c6eb6 MA |
194 | error_setg(errp, "vnet_hdr=1 requested, but no kernel " |
195 | "support for IFF_VNET_HDR available"); | |
f5c5e381 MM |
196 | close(fd); |
197 | return -1; | |
198 | } | |
199 | } | |
966ea5ec MM |
200 | fcntl(fd, F_SETFL, O_NONBLOCK); |
201 | return fd; | |
202 | } | |
15ac913b | 203 | |
80b832c3 | 204 | void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp) |
15ac913b | 205 | { |
15ac913b | 206 | } |
dc69004c MM |
207 | |
208 | int tap_probe_vnet_hdr(int fd) | |
209 | { | |
210 | return 0; | |
211 | } | |
1faac1f7 | 212 | |
9c282718 MM |
213 | int tap_probe_has_ufo(int fd) |
214 | { | |
215 | return 0; | |
216 | } | |
217 | ||
445d892f MT |
218 | int tap_probe_vnet_hdr_len(int fd, int len) |
219 | { | |
220 | return 0; | |
221 | } | |
222 | ||
223 | void tap_fd_set_vnet_hdr_len(int fd, int len) | |
224 | { | |
225 | } | |
226 | ||
4ee9b43b MT |
227 | int tap_fd_set_vnet_le(int fd, int is_le) |
228 | { | |
229 | return -EINVAL; | |
230 | } | |
231 | ||
232 | int tap_fd_set_vnet_be(int fd, int is_be) | |
233 | { | |
234 | return -EINVAL; | |
235 | } | |
236 | ||
1faac1f7 MM |
237 | void tap_fd_set_offload(int fd, int csum, int tso4, |
238 | int tso6, int ecn, int ufo) | |
239 | { | |
240 | } | |
94fdc6d0 JW |
241 | |
242 | int tap_fd_enable(int fd) | |
243 | { | |
244 | return -1; | |
245 | } | |
246 | ||
247 | int tap_fd_disable(int fd) | |
248 | { | |
249 | return -1; | |
250 | } | |
251 | ||
e5dc0b40 JW |
252 | int tap_fd_get_ifname(int fd, char *ifname) |
253 | { | |
254 | return -1; | |
255 | } |