tap-solaris.c 6.7 KB

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