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