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

Notices

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

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


Hello,
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:

Code:
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)
		err(EXIT_FAILURE, "SIOCGIFFLAGS");
 	flags = ifreq.ifr_flags;

	if (value < 0) {
		value = -value;
		flags &= ~value;
	} else
		flags |= value;
	ifreq.ifr_flags = flags;
	if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
		err(EXIT_FAILURE, "SIOCSIFFLAGS");
}
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)?

Thanks.
 
Old 09-01-2009, 11:16 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply


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 07:45 PM
Cannot get interface flags: Invalid argument nasim751 Linux - Software 1 04-21-2008 07:20 PM
Cannot get interface flags: Invalid argument nasim751 Linux - Software 1 04-21-2008 07:18 PM
Cannot get interface flags: Invalid argument nasim751 Programming 1 04-21-2008 12:22 PM
Changing the default network interface at startup frandres Linux - Newbie 1 01-16-2005 06:32 PM


All times are GMT -5. The time now is 05:59 AM.

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