Tuesday, February 22, 2022

[SOLVED] Linux programmatically up/down an interface kernel

Issue

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


id='dv4'>

Solution

...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.



Answered By - Andrejs Cainikovs
Answer Checked By - Robin (WPSolving Admin)