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.