Linux programmatically up / down the kernel interface

What is the programmatic way to enable or disable an interface in kernel space? What to do?

+8
c linux linux-kernel network-programming
source share
2 answers

... using IOCTL's ...

ioctl(skfd, SIOCSIFFLAGS, &ifr); 

... with the IFF_UP bit set or not set, depending on whether you want to connect the interface up or down, respectively, that is:

 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 copied from Linux Network Documentation .

+12
source share

Code to bring eth0 up:

 int sockfd; struct ifreq ifr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) return; memset(&ifr, 0, sizeof ifr); strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); ifr.ifr_flags |= IFF_UP; ioctl(sockfd, SIOCSIFFLAGS, &ifr); 
+8
source share

All Articles