This repository contains two bash scripts designed to simulate MACsec (IEEE 802.1AE) protocol over LAN and WAN topologies using network namespaces in the context of a single Linux environment. Both scripts help to set up a MACsec protected network simulating two different scenarios:
-
MACsec over LAN: virtual Ethernet interfaces are created and bridged to form a virtual LAN, where MACsec-protected communications can take place between a chosen number of host namespaces. This simulation leverages the use of the MACsec Key Agreement (MKA) protocol with the help of wpa_supplicant.
-
MACsec over WAN: multiple namespaces, each representing hosts connected to virtual routers and interconnected within a WAN, are configured to simulate MACsec-protected communication over a Layer 2 GRE tunnel (GRETAP). This setup conceptually secures the traffic between two remote sites, mimicking a site-to-site VPN at Layer 2, with the added security of the MACsec protocol over the WAN link.
Both scripts provide logging with rotation enabled and include options for environment teardown and direct namespace access.
Note: You can find a comprehensive report explaining how MACsec works, along with a step-by-step tutorial to manually reproduce and configure the simulation provided by these automated scripts, in the docs folder. The report is available in the MACsec_Report.pdf file.
Both scripts are intended to be executed on a Linux machine and the following packages have to be installed on your system to work:
- Linux kernel
$\geq$ 4.7.0 - Netlink library libnl
$\geq$ 3.2.29 - wpa supplicant
$\geq$ 2.6 - ip-route2
- logrotate
- tcpdump
- konsole
You must run all the scripts located in the src/ folder with root privileges (either using sudo or running as root).
Inside the src/ folder, you can find two shell scripts:
lan_macsec.sh: This script replicates the LAN simulation.wan_macsec.sh: This script replicates the WAN simulation.
Additionally, there is a python code named benchmark_plot.py useful for perfoming bandwidth tests using iperf3. More details can be found at the beginning of the code.
git clone https://github.com/eferollo/MACsec-Demo.git
cd MACsec-Demo
chmod +x wan_macsec.sh
chmod +x lan_macsec.sh
For example:
sudo ./wan_macsec.sh
or
sudo ./lan_macsec.sh
Note: For the LAN simulation, carefully check the wpa_supplicant.conf file for configuring wpa_supplicant operation mode. A default configuration file is provided but it can be modified according to your needs.
After starting the script, you will be prompted to enter the number of namespaces to create. Input the desired number (e.g., 2 for two namespaces):
Enter the number of network namespaces: 2
he script will then ask if you would like a konsole terminal window for each namespace. This allows direct interaction within each namespace:
Do you want to open a shell in each namespace? (y/n): y
Entering y opens a konsole window for each namespace. Selecting n will skip this, but instructions to manually access each namespace will be provided.
Once the namespaces and interfaces are set up, the script will prompt you to press Enter to initialize the MACsec and MKA protocols:
Simulating MACsec environment, press enter to start MKA and MACsec...
The script includes a menu for managing namespaces and logs. Upon running the script, a log directory is automatically created in the current working directory. This directory stores various logs generated during the setup and operation of the network namespaces and MACsec configuration. Below is a description of each log type you will find:
-
WPA Supplicant Logs (
wpa_supplicant_nsX.log):
These logs are generated bywpa_supplicantfor each namespace (whereXis the namespace number). They provide detailed debug information about the MACsec Key Agreement (MKA) protocol operations, security association setups, and any issues encountered bywpa_supplicant. -
Interface Capture Logs (
eth0_nsX.pcap):
For each namespace, a.pcapfile is generated to capture packets on theeth0interface. These captures include traffic using MACsec encryption protocols, specifically focusing on frames with EtherTypes0x888E(EAPOL frames) and0x88E5(MACsec secured data). You can analyze these with tools such as Wireshark ortcpdump. -
RTNETLINK Log (
rtnetlink.log):
This log tracks changes to network interfaces and addresses in the system usingrtnetlink. It monitors all network events across namespaces, providing insight into link, address, and route changes crucial for network troubleshooting.
The logs are automatically rotated to prevent excessive disk usage. Each log type is limited to a maximum of 10 rotated files, ensuring older logs are archived while keeping recent information readily available.
To view any of these logs, navigate to the lan_logs directory created within the directory where the script is executed:
To list all namespaces and view MACsec information, use the options below:
Select an option: list
Select an option: show
When you’re done, select exit from the menu. This will:
- Stop all running wpa_supplicant processes.
- Remove all network interfaces.
- Delete namespaces and the bridge.
After starting the script, you’ll be prompted to make two key choices:
-
Open a Shell in Each Namespace:
If you select "yes," the script will open a shell for each namespace in separate terminals. This allows you to interact directly with each namespace. -
Enable MACsec Encryption:
You’ll have the option to toggle MACsec encryption. If enabled, MACsec will secure network communications between namespaces using randomly generated keys.
The script creates detailed logs for each network interface within the namespaces. All log files are saved in the wan_logs directory, with specific subdirectories based on the namespace type:
wan_logs/hosts: Logs for host namespaces (ns1andns2), which are created for each veth (virtual Ethernet) interface.wan_logs/routers: Logs for router namespaces (nsr1andnsr2). Each router has logs for multiple interfaces, including veth, GRE, and (if enabled) MACsec.wan_logs/wan: Logs for WAN interfaces (wan1andwan2) in the WAN namespace.
Each subdirectory contains .pcap (packet capture) files for individual interfaces, which can be analyzed using tools like tcpdump or Wireshark.
An example of log locations are:
-
Host Namespace Logs:
wan_logs/hosts/ns1_veth1.pcapfor theveth1interface ofns1wan_logs/hosts/ns2_veth2.pcapfor theveth2interface ofns2
-
Router Namespace Logs:
wan_logs/routers/nsr1_veth1_1.pcapfor theveth1_1interface ofnsr1wan_logs/routers/nsr2_macsec.pcap(if MACsec is enabled)
-
WAN Namespace Logs:
wan_logs/wan/wan1.pcapfor thewan1interface in the WAN namespace
The script includes a log rotation function that prevents the log files from growing indefinitely. The rotation policy retains the last 10 logs for each interface. You can configure log rotation settings in the configuration files generated by the script in each logging directory.
To list all namespaces and view MACsec information, use the options below:
Select an option: list
Select an option: show
When you’re done, select exit from the menu. This will:
- Bring down the GRETAP tunnel and MACsec context.
- Remove all network interfaces.
- Delete namespaces and the bridge.
If you skipped the console option, you can manually access each namespace as follows:
sudo ip netns exec <name> bash

