2
0

tap-solaris.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "tap_int.h"
  27. #include "qemu/ctype.h"
  28. #include "qemu/cutils.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>
  39. #include <stropts.h>
  40. #include "qemu/error-report.h"
  41. ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
  42. {
  43. struct strbuf sbuf;
  44. int f = 0;
  45. sbuf.maxlen = maxlen;
  46. sbuf.buf = (char *)buf;
  47. return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
  48. }
  49. #define TUNNEWPPA (('T'<<16) | 0x0001)
  50. /*
  51. * Allocate TAP device, returns opened fd.
  52. * Stores dev name in the first arg(must be large enough).
  53. */
  54. static int tap_alloc(char *dev, size_t dev_size, Error **errp)
  55. {
  56. /* FIXME leaks like a sieve on error paths */
  57. /* FIXME suspicious: many errors are reported, then ignored */
  58. int tap_fd, if_fd, ppa = -1;
  59. static int ip_fd = 0;
  60. char *ptr;
  61. static int arp_fd = 0;
  62. int ip_muxid, arp_muxid;
  63. struct strioctl strioc_if, strioc_ppa;
  64. int link_type = I_PLINK;
  65. struct lifreq ifr;
  66. char actual_name[32] = "";
  67. memset(&ifr, 0x0, sizeof(ifr));
  68. if( *dev ){
  69. ptr = dev;
  70. while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
  71. ppa = atoi(ptr);
  72. }
  73. /* Check if IP device was opened */
  74. if( ip_fd )
  75. close(ip_fd);
  76. ip_fd = RETRY_ON_EINTR(open("/dev/udp", O_RDWR, 0));
  77. if (ip_fd < 0) {
  78. error_setg(errp, "Can't open /dev/ip (actually /dev/udp)");
  79. return -1;
  80. }
  81. tap_fd = RETRY_ON_EINTR(open("/dev/tap", O_RDWR, 0));
  82. if (tap_fd < 0) {
  83. error_setg(errp, "Can't open /dev/tap");
  84. return -1;
  85. }
  86. /* Assign a new PPA and get its unit number. */
  87. strioc_ppa.ic_cmd = TUNNEWPPA;
  88. strioc_ppa.ic_timout = 0;
  89. strioc_ppa.ic_len = sizeof(ppa);
  90. strioc_ppa.ic_dp = (char *)&ppa;
  91. if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
  92. error_report("Can't assign new interface");
  93. if_fd = RETRY_ON_EINTR(open("/dev/tap", O_RDWR, 0));
  94. if (if_fd < 0) {
  95. error_setg(errp, "Can't open /dev/tap (2)");
  96. return -1;
  97. }
  98. if(ioctl(if_fd, I_PUSH, "ip") < 0){
  99. error_setg(errp, "Can't push IP module");
  100. return -1;
  101. }
  102. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
  103. error_report("Can't get flags");
  104. snprintf (actual_name, 32, "tap%d", ppa);
  105. pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  106. ifr.lifr_ppa = ppa;
  107. /* Assign ppa according to the unit number returned by tun device */
  108. if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
  109. error_report("Can't set PPA %d", ppa);
  110. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
  111. error_report("Can't get flags");
  112. /* Push arp module to if_fd */
  113. if (ioctl (if_fd, I_PUSH, "arp") < 0)
  114. error_report("Can't push ARP module (2)");
  115. /* Push arp module to ip_fd */
  116. if (ioctl (ip_fd, I_POP, NULL) < 0)
  117. error_report("I_POP failed");
  118. if (ioctl (ip_fd, I_PUSH, "arp") < 0)
  119. error_report("Can't push ARP module (3)");
  120. /* Open arp_fd */
  121. arp_fd = RETRY_ON_EINTR(open("/dev/tap", O_RDWR, 0));
  122. if (arp_fd < 0)
  123. error_report("Can't open %s", "/dev/tap");
  124. /* Set ifname to arp */
  125. strioc_if.ic_cmd = SIOCSLIFNAME;
  126. strioc_if.ic_timout = 0;
  127. strioc_if.ic_len = sizeof(ifr);
  128. strioc_if.ic_dp = (char *)&ifr;
  129. if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
  130. error_report("Can't set ifname to arp");
  131. }
  132. if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
  133. error_setg(errp, "Can't link TAP device to IP");
  134. return -1;
  135. }
  136. if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
  137. error_report("Can't link TAP device to ARP");
  138. close (if_fd);
  139. memset(&ifr, 0x0, sizeof(ifr));
  140. pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  141. ifr.lifr_ip_muxid = ip_muxid;
  142. ifr.lifr_arp_muxid = arp_muxid;
  143. if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
  144. {
  145. ioctl (ip_fd, I_PUNLINK , arp_muxid);
  146. ioctl (ip_fd, I_PUNLINK, ip_muxid);
  147. error_report("Can't set multiplexor id");
  148. }
  149. snprintf(dev, dev_size, "tap%d", ppa);
  150. return tap_fd;
  151. }
  152. int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
  153. int vnet_hdr_required, int mq_required, Error **errp)
  154. {
  155. char dev[10]="";
  156. int fd;
  157. fd = tap_alloc(dev, sizeof(dev), errp);
  158. if (fd < 0) {
  159. return -1;
  160. }
  161. pstrcpy(ifname, ifname_size, dev);
  162. if (*vnet_hdr) {
  163. /* Solaris doesn't have IFF_VNET_HDR */
  164. *vnet_hdr = 0;
  165. if (vnet_hdr_required && !*vnet_hdr) {
  166. error_setg(errp, "vnet_hdr=1 requested, but no kernel "
  167. "support for IFF_VNET_HDR available");
  168. close(fd);
  169. return -1;
  170. }
  171. }
  172. g_unix_set_fd_nonblocking(fd, true, NULL);
  173. return fd;
  174. }
  175. void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
  176. {
  177. }
  178. int tap_probe_vnet_hdr(int fd, Error **errp)
  179. {
  180. return 0;
  181. }
  182. int tap_probe_has_ufo(int fd)
  183. {
  184. return 0;
  185. }
  186. int tap_probe_has_uso(int fd)
  187. {
  188. return 0;
  189. }
  190. void tap_fd_set_vnet_hdr_len(int fd, int len)
  191. {
  192. }
  193. int tap_fd_set_vnet_le(int fd, int is_le)
  194. {
  195. return -EINVAL;
  196. }
  197. int tap_fd_set_vnet_be(int fd, int is_be)
  198. {
  199. return -EINVAL;
  200. }
  201. void tap_fd_set_offload(int fd, int csum, int tso4,
  202. int tso6, int ecn, int ufo, int uso4, int uso6)
  203. {
  204. }
  205. int tap_fd_enable(int fd)
  206. {
  207. return -1;
  208. }
  209. int tap_fd_disable(int fd)
  210. {
  211. return -1;
  212. }
  213. int tap_fd_get_ifname(int fd, char *ifname)
  214. {
  215. return -1;
  216. }
  217. int tap_fd_set_steering_ebpf(int fd, int prog_fd)
  218. {
  219. return -1;
  220. }