Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 09-01-2009, 04:10 AM   #1
LQ Newbie
Registered: Jul 2009
Posts: 11

Rep: Reputation: 0
ioctl() and changing network interface flags

I've recently made an empty-ish system that contains just grub, a kernel, my binary which bridges between my two network interfaces and my own init (which is responsible for raising the two network interfaces and (re)spawning my binary).

Until now I've done it by running execv on ifconfig but since I removed that from my system I've had to implement it more intrinsically, using the bit from ifconfig that raises the interface. Looking at "ifconfig XXX up", it does this:

void setifflags(const char *vname, int value)
	struct ifreq ifreq;

	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
 	if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
 	flags = ifreq.ifr_flags;

	if (value < 0) {
		value = -value;
		flags &= ~value;
	} else
		flags |= value;
	ifreq.ifr_flags = flags;
	if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
Where 'value' is the flag, "IFF_UP". Pretend they used "vname" instead of "name" to simplify things (they use globals a lot). In the calls to ioctl, they give three parameters, and while the last two are obvious - the first's purpose (s, which is an int holding a socket handle from what I could tell) is unclear to me.

Why do I need to give the ioctl command some socket in order to change an interface's flags? I couldn't find a specific ioctl implementation anywhere, so I'm not sure what it does. If I give it NULL instead of s, it fails to set the interface's flags.

The socket I use with this ioctl call is initialized by socket(AF_PACKET, SOCK_RAW, 0) which refers to an address family, socket type, and protocol, and certainly nothing tying it to a specific network interface. I can successfully raise both interfaces - eth0, eth1 - with this same socket.

So, what is that socket used for in the ioctl call? Alternatively, where can I find an implementation of ioctl or an explanation about the mechanism, or how can I set the interface's IFF_UP flag in a more direct way (that does not require voodoo-like objects to be passed as parameters)?

Old 09-02-2009, 12:16 AM   #2
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,125
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
ioctl is a call into the kernel, the first parameter is a file descriptor, this provides the information on who is going to handle the ioctl, in this case the network stack, the second parameter is what you want access to for that ioctl, and the third parameter is a long or pointer that gives additional information to the function that is handling the ioctl, the third parameter isn't always used.

So what is happening here is the first ioctl is fetching the flags for the interface with the given name and the second ioctl is setting the flags for that interface (G vs S). There probably isn't a more direct way to set your IFF_UP, dunno for sure.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inappropriate ioctl for device while reading flags w1k0 Slackware 3 06-01-2009 08:45 PM
Cannot get interface flags: Invalid argument nasim751 Linux - Software 1 04-21-2008 08:20 PM
Cannot get interface flags: Invalid argument nasim751 Linux - Software 1 04-21-2008 08:18 PM
Cannot get interface flags: Invalid argument nasim751 Programming 1 04-21-2008 01:22 PM
Changing the default network interface at startup frandres Linux - Newbie 1 01-16-2005 07:32 PM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration