How Wake-On-LAN works (2020) (blog.xaner.dev)

by swq115 33 comments 92 points
Read article View on HN

33 comments

[−] drob518 29d ago
Wow, that brings back memories. In 1995, I sat right next to one of the guys who invented what we called “Magic Packet” inside AMD and what became known as Wake On LAN. I was working on what later became WiFi. Those were fun times; lots of innovation in the networking space. We worked with HP and Microsoft on this since it needed OS support from Windows, too. Yea, it’s not sophisticated at all since the hardware needed to decode it before generating an interrupt and passing anything to the driver. It just needed to be something statistically unlikely to occur in a real network to avoid false positives. And it needs to be unique per NIC, because you don’t want a single packet waking up every machine. So, it just repeats the MAC address a few times. The original use case was for doing things like network backups late at night. The file server needed a way to wake up a machine that had gone to sleep. Almost everything in the computer can be asleep excelpt for the NIC and whatever power-on circuitry is necessary to power everything back up and resume. In other words, it can be a deep sleep.
[−] Fwirt 29d ago
Not wanting to install an OS package to do something as simple as sending some bytes a couple years back, I wrote a shell script to send WoL packets. Its only dependencies are netcat and bash so if you have busybox it should run almost anywhere. It just takes the mac address and interface as an argument and sends a WoL packet on that interface

  #!/bin/bash
  hex="\xFF\xFF\xFF\xFF\xFF\xFF"
  mac_hex="\\x`printf "$1" | sed 's/:/\\\\x/g'`"
  wol_string="$hex"
  for i in {1..16}
  do
      wol_string+="$mac_hex"
  done
  printf "$wol_string" | nc -u -b -w 1 "$2" 9
It took me a while to find an explanation of something so simple, I can't figure out why everyone relies on huge binary packages and libraries to do it. I just needed something on my router so that I could wake my machines from outside the house. I ended up just writing a couple shell scripts that called it and triggering them with nginx via FastCGI so I could click on a link to wake up my machines.
[−] LittleLily 29d ago
Here's an equivalent shell script that only uses bash builtins, so no other software required:

  #!/bin/bash 
  mac="\\x${1//:/\\x}" 
  wol="\xFF\xFF\xFF\xFF\xFF\xFF" 
  for i in {1..16}; do wol+="$mac"; done 
  printf "$wol" > "/dev/udp/$2/9"
[−] ysleepy 30d ago
I was kinda hoping to get the nitty gritty of how the NIC does the packet matching, how, it wakes up the system via PCIe and how switches route the frames to the port which has/had the client.

Nothing against the article though, but maybe someone knows a good writeup.

[−] jonah-archive 29d ago
The original paper proposing the technology is actually very good (and surprisingly still online!): https://www.amd.com/content/dam/amd/en/documents/archived-te...
[−] Animats 29d ago
That's more useful. A big question is how much is really turned off in a computer waiting for the wake-up packet. "The power to the Ethernet controller must be maintained at all times, allowing the Ethernet controller to scan all incoming packets for the Magic Packet frame". So the full network controller is still alive. There's not some tiny Magic Packet detector hardware running off a rechargable coin cell or something, with the main power supply turned off. At least not in the original design.

A lot of sleep modes leave more running than you'd expect.

[−] MrBuddyCasino 29d ago
Didn‘t know a whitepaper is allowed to be this readable.
[−] elevation 29d ago
I was distracted by the poor typesetting in parts of the page. The meaning of the text is overwhelmed by the distracting spacing used to justify the text:

> . I n o t h e r w o r d s , s i l i c o n - o r g a t e - l evel

[−] toast0 29d ago

> how the NIC does the packet matching

This part, I don't know, but default magic packet has 6 bytes of 0xff, followed by the mac address sixteen times in a row, so it's a fairly simple state machine as the packet comes in. The AMD whitepaper others linked might have details?

> how, it wakes up the system via PCIe

Pci-e pin 11-B is wake#. PCI 2.2 added PME# on 19A which does the same job for PCI. Pull it high (I think) to wakeup the host. I don't think there's a pin for this on ISA, so you'd need some system specific connector to wakeup from an ISA nic.

> how switches route the frames to the port which has/had the client.

Ethernet switching is a whole different thing. You can send a broadcast frame and those should get flooded to all ports. If you send a unicast frame, the switch looks up the destination mac in its address table, if present, it sends only to the port where that address was seen, otherwise it floods to all ports.

[−] rubatuga 29d ago
I believe WOL brings it down to 10Mbit - at which point it'll process any received packets use a low power processor. Packets wouldn't be routed to it unless specifically addressed
[−] ErroneousBosh 29d ago
Shift registers. It's all done with shift registers.
[−] Terr_ 29d ago
Ditto, I clicked and was disappointed.

"How to send a magic packet in $LANG" isn't very interesting to me. There are plenty of guides for it, and I remember actually doing it 20+ years ago with a short PHP script.

Even at the time, the task didn't seem like "enough" for a show-the-world blog post. A dramatically shortened version (no validation, error handling, logging, etc.) for your amusement:

    // Given $macAddress and $addr and $port
    $macAddress = str_replace(":","",$macAddress);
    $macAddress = str_replace("-","",$macAddress);

    $header = pack('H12','FFFFFFFFFFFF');
    $payload = pack("H12",$macAddress);
    $packet = $header . str_repeat($payload,16);

    $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_set_option($sock, 1, 6, TRUE);
    socket_sendto($sock, $payload, strlen($payload), 0, $addr, $port);
    socket_close($sock);
[−] lights0123 29d ago

> The computer that you are trying to wake up also needs to be connect with an ethernet cable as it is not possible to send a magic packet over wifi.

While WiFi adapters may not support waking up the computer from a WiFi signal, you absolutely can send magic packets over WiFi as they're normally just UDP broadcast frames. Convenient for waking up a desktop from a laptop!

[−] dang 29d ago
[stub for offtopicness]