Linux API to create logical interface on a physical interface?

Discussion in 'Linux Networking' started by Zarko Coklin, Jul 12, 2004.

  1. Zarko Coklin

    Zarko Coklin Guest

    I am trying to find an API to create a logical interface on a physical
    interface. For people familiar with Solaris I am trying to find
    replacement for:
    ioctl(fd, SIOCLIFADDIF, &if_req,sizeof(struct ifreq)

    Does anybody know how to do that on Linux? Comments/code
    samples/references are highly appreciated.

    Thanks,
    Zarko Coklin
     
    Zarko Coklin, Jul 12, 2004
    #1
    1. Advertisements

  2. Zarko Coklin

    Zarko Coklin Guest

    Let me put it in a different way. How to create and ethernet interface
    using Linux APIs? What is correct function to use? (init_etherdev,
    etrax_ethernet_init or some other less kernel-ish) function?
     
    Zarko Coklin, Jul 13, 2004
    #2
    1. Advertisements

  3. Zarko Coklin

    Zarko Coklin Guest

    Oh, well! I am sorry no-one really replied and saved me some time :-(
    I figured it out myself and am posting it so other people have chance
    to get over it if/when needed. Here is code snippet that will do the
    job.

    Enjoy :)
    Zarko Coklin
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include <stdio.h>
    #include <sys/ioctl.h>
    #include <sys/socket.h>
    #include <net/if.h>
    #include <netinet/in.h>

    int main ()
    {
    struct ifreq if_req = {0};
    int fd;
    struct sockaddr_in sin = {0};

    /* first open socket */
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
    printf("%s:%d Failed to open socket!\n", __func__, __LINE__);
    return -1;
    }

    /* set dummy eth. IF values. Note: Linux does not like IP 0.0.0.0
    * and does not allow IF to be in 'DOWN' state like Solaris. If
    'DOWN'
    * IF is immediately un-plumbed !*/
    strncpy(if_req.ifr_name, "eth0:5", IFNAMSIZ);
    sin.sin_family = AF_INET;
    sin.sin_port = 0;
    if (inet_aton("123.123.123.123", &sin.sin_addr) == 0)
    {
    printf("Failed in inet_aton!\n");
    close (fd);
    return -1;
    }

    /* provision if_req with eth. info and create IF */
    memcpy((char *)&if_req.ifr_addr, (char *)&sin, sizeof(struct
    sockaddr));
    if (ioctl(fd, SIOCSIFADDR, &if_req) < 0)
    {
    printf("%s:%d Failed to open socket!\n", __func__, __LINE__);
    close (fd);
    return -1;
    }

    close (fd);
    printf("%s:%d Got here? Interface created\n", __func__, __LINE__);
    return 0;
    }
     
    Zarko Coklin, Jul 18, 2004
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.