File:  [Qemu by Fabrice Bellard] / qemu / net / tap-solaris.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:32:51 2018 UTC (2 years, 2 months ago) by root
Branches: qemu, MAIN
CVS tags: qemu0130, HEAD
qemu 0.13.0

    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: 
   25: #include "net/tap.h"
   26: #include "sysemu.h"
   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>
   39: #include <syslog.h>
   40: #include <stropts.h>
   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:  */
   58: static int tap_alloc(char *dev, size_t dev_size)
   59: {
   60:     int tap_fd, if_fd, ppa = -1;
   61:     static int ip_fd = 0;
   62:     char *ptr;
   63: 
   64:     static int arp_fd = 0;
   65:     int ip_muxid, arp_muxid;
   66:     struct strioctl  strioc_if, strioc_ppa;
   67:     int link_type = I_PLINK;;
   68:     struct lifreq ifr;
   69:     char actual_name[32] = "";
   70: 
   71:     memset(&ifr, 0x0, sizeof(ifr));
   72: 
   73:     if( *dev ){
   74:        ptr = dev;
   75:        while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
   76:        ppa = atoi(ptr);
   77:     }
   78: 
   79:     /* Check if IP device was opened */
   80:     if( ip_fd )
   81:        close(ip_fd);
   82: 
   83:     TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
   84:     if (ip_fd < 0) {
   85:        syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
   86:        return -1;
   87:     }
   88: 
   89:     TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
   90:     if (tap_fd < 0) {
   91:        syslog(LOG_ERR, "Can't open /dev/tap");
   92:        return -1;
   93:     }
   94: 
   95:     /* Assign a new PPA and get its unit number. */
   96:     strioc_ppa.ic_cmd = TUNNEWPPA;
   97:     strioc_ppa.ic_timout = 0;
   98:     strioc_ppa.ic_len = sizeof(ppa);
   99:     strioc_ppa.ic_dp = (char *)&ppa;
  100:     if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
  101:        syslog (LOG_ERR, "Can't assign new interface");
  102: 
  103:     TFR(if_fd = open("/dev/tap", O_RDWR, 0));
  104:     if (if_fd < 0) {
  105:        syslog(LOG_ERR, "Can't open /dev/tap (2)");
  106:        return -1;
  107:     }
  108:     if(ioctl(if_fd, I_PUSH, "ip") < 0){
  109:        syslog(LOG_ERR, "Can't push IP module");
  110:        return -1;
  111:     }
  112: 
  113:     if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
  114: 	syslog(LOG_ERR, "Can't get flags\n");
  115: 
  116:     snprintf (actual_name, 32, "tap%d", ppa);
  117:     pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  118: 
  119:     ifr.lifr_ppa = ppa;
  120:     /* Assign ppa according to the unit number returned by tun device */
  121: 
  122:     if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
  123:         syslog (LOG_ERR, "Can't set PPA %d", ppa);
  124:     if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
  125:         syslog (LOG_ERR, "Can't get flags\n");
  126:     /* Push arp module to if_fd */
  127:     if (ioctl (if_fd, I_PUSH, "arp") < 0)
  128:         syslog (LOG_ERR, "Can't push ARP module (2)");
  129: 
  130:     /* Push arp module to ip_fd */
  131:     if (ioctl (ip_fd, I_POP, NULL) < 0)
  132:         syslog (LOG_ERR, "I_POP failed\n");
  133:     if (ioctl (ip_fd, I_PUSH, "arp") < 0)
  134:         syslog (LOG_ERR, "Can't push ARP module (3)\n");
  135:     /* Open arp_fd */
  136:     TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
  137:     if (arp_fd < 0)
  138:        syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
  139: 
  140:     /* Set ifname to arp */
  141:     strioc_if.ic_cmd = SIOCSLIFNAME;
  142:     strioc_if.ic_timout = 0;
  143:     strioc_if.ic_len = sizeof(ifr);
  144:     strioc_if.ic_dp = (char *)&ifr;
  145:     if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
  146:         syslog (LOG_ERR, "Can't set ifname to arp\n");
  147:     }
  148: 
  149:     if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
  150:        syslog(LOG_ERR, "Can't link TAP device to IP");
  151:        return -1;
  152:     }
  153: 
  154:     if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
  155:         syslog (LOG_ERR, "Can't link TAP device to ARP");
  156: 
  157:     close (if_fd);
  158: 
  159:     memset(&ifr, 0x0, sizeof(ifr));
  160:     pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  161:     ifr.lifr_ip_muxid  = ip_muxid;
  162:     ifr.lifr_arp_muxid = arp_muxid;
  163: 
  164:     if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
  165:     {
  166:       ioctl (ip_fd, I_PUNLINK , arp_muxid);
  167:       ioctl (ip_fd, I_PUNLINK, ip_muxid);
  168:       syslog (LOG_ERR, "Can't set multiplexor id");
  169:     }
  170: 
  171:     snprintf(dev, dev_size, "tap%d", ppa);
  172:     return tap_fd;
  173: }
  174: 
  175: int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required)
  176: {
  177:     char  dev[10]="";
  178:     int fd;
  179:     if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
  180:        fprintf(stderr, "Cannot allocate TAP device\n");
  181:        return -1;
  182:     }
  183:     pstrcpy(ifname, ifname_size, dev);
  184:     if (*vnet_hdr) {
  185:         /* Solaris doesn't have IFF_VNET_HDR */
  186:         *vnet_hdr = 0;
  187: 
  188:         if (vnet_hdr_required && !*vnet_hdr) {
  189:             error_report("vnet_hdr=1 requested, but no kernel "
  190:                          "support for IFF_VNET_HDR available");
  191:             close(fd);
  192:             return -1;
  193:         }
  194:     }
  195:     fcntl(fd, F_SETFL, O_NONBLOCK);
  196:     return fd;
  197: }
  198: 
  199: int tap_set_sndbuf(int fd, QemuOpts *opts)
  200: {
  201:     return 0;
  202: }
  203: 
  204: int tap_probe_vnet_hdr(int fd)
  205: {
  206:     return 0;
  207: }
  208: 
  209: int tap_probe_has_ufo(int fd)
  210: {
  211:     return 0;
  212: }
  213: 
  214: void tap_fd_set_offload(int fd, int csum, int tso4,
  215:                         int tso6, int ecn, int ufo)
  216: {
  217: }

unix.superglobalmegacorp.com