Skip to content
Advertisement

Linux programmatically up/down an interface kernel

What is the programmatic way of enabling or disabling an interface in kernel space? What should be done?

Advertisement

Answer

…by using IOCTL’s…

ioctl(skfd, SIOCSIFFLAGS, &ifr);

… with the IFF_UP bit set or unset depending on whether you want bring the interface up or down accordingly, i.e.:

static int set_if_up(char *ifname, short flags)
{
    return set_if_flags(ifname, flags | IFF_UP);
}

static int set_if_down(char *ifname, short flags)
{
    return set_if_flags(ifname, flags & ~IFF_UP);
}

Code copy-pasted from Linux networking documentation.

Advertisement