Skip to content

Commit

Permalink
src/netdev.c: bring up network interface for Ski before use
Browse files Browse the repository at this point in the history
* otherwise Ski crashes on first console input (w/PF_INET) or
* the selected interface cannot be used inside Ski (w/AF_PACKET).
  • Loading branch information
johnny-mnemonic committed Feb 12, 2025
1 parent c754a11 commit c19b7f5
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,47 @@ find_eth (int fd)
return NULL;
}

/*
* taken and adapted from
* https://github.com/torvalds/linux/blob/v3.10/Documentation/networking/ifenslave.c
*/
static
int
set_if_up (char *ifname)
{
int skfd = -1; /* AF_INET socket for ioctl() calls.*/
int saved_errno;

struct ifreq ifr;
int res = 0;

ifr.ifr_flags = IFF_UP;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);

/* Open a basic socket */
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("set_if_up: socket() failed\n");
res = 1;
goto out;
}

res = ioctl(skfd, SIOCSIFFLAGS, &ifr);
if (res < 0) {
saved_errno = errno;
cmdwPrint("Host interface '%s': Error: SIOCSIFFLAGS failed: %s\n",
ifname, strerror(saved_errno));
} else {
cmdwPrint("Host interface '%s': flags set to %04X.\n", ifname, IFF_UP);
}

out:
if (skfd >= 0) {
close(skfd);
}

return res;
}

int
netdev_open (char *name, unsigned char *macaddr)
{
Expand Down Expand Up @@ -183,6 +224,11 @@ netdev_open (char *name, unsigned char *macaddr)
return -1;
}

/*
* Make sure the selected interface is up, before using it.
*/
set_if_up (name);

r = bind (fd, (struct sockaddr *)&sa_ll, sizeof (sa_ll));
if (r) {
cmdwPrint("Cannot bind (disabling network): make sure %s exists\n", name);
Expand Down

0 comments on commit c19b7f5

Please sign in to comment.