tap-solaris.c 6.6 KB

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