peapod  0.1.0
EAPOL Proxy Daemon
All Data Structures Files Functions Variables Macros Pages
Functions | Variables
iface.c File Reference

Network interface and socket setup. More...

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <linux/filter.h>
#include <linux/if_packet.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include "iface.h"
#include "log.h"
Include dependency graph for iface.c:

Functions

static int validate (struct iface_t *iface)
 Check that a network interface is up and get its MTU. More...
 
static int epoll_register (int epfd, struct iface_t *iface)
 Register the skt field of iface with an epoll instance. More...
 
static u_char * get_mac (struct iface_t *iface)
 Query the kernel for the MAC address of a network interface. More...
 
static int sockopt (struct iface_t *iface)
 Set socket options for the skt field of a struct iface_t. More...
 
int iface_init (struct iface_t *ifaces, int epfd)
 Create raw sockets for interfaces in a list and add them to an epoll instance. More...
 
int iface_count (struct iface_t *ifaces)
 Count number of items in a list of struct iface_t. More...
 
int iface_set_mac (struct iface_t *iface, u_char *src_mac)
 Set the MAC address of a network interface. More...
 
char * iface_strmac (u_char *mac)
 Convert a MAC address to a string. More...
 

Variables

static const u_char eapol_grp_mac [3][ETH_ALEN]
 EAPOL multicast group MAC addresses. More...
 
BPF filter for EAPOL packets

Scenario - Create a socket with ETH_P_PAE as the protocol. Set the PACKET_AUXDATA option on the socket. Receive tpacket_auxdata structures with recvmsg(2) that contain 802.1Q tag info.

Just kidding! ETH_P_PAE means no tpacket_auxdata structures. Thanks, Linux!

Providing our own bpf filter, however, works fine.

Note
The filter checks bytes 12:13 - after Linux strips out the tag. That's actually convenient.
See also
socket(7), "Socket options"
bpf(2)
static struct sock_filter eapol_sock_filter []
 A simple bpf filter for EAPOL packets. More...
 
static const struct sock_fprog eapol_fprog
 The complete bpf filter program provided to setsockopt(3) More...
 

Detailed Description

Network interface and socket setup.

Function Documentation

static int validate ( struct iface_t iface)
static

Check that a network interface is up and get its MTU.

Also sets the mtu field of iface to the interface's current MTU.

Parameters
ifacePointer to a struct iface_t representing an interface
Returns
0 if successful, or -1 if unsuccessful
static int epoll_register ( int  epfd,
struct iface_t iface 
)
static

Register the skt field of iface with an epoll instance.

Provide iface itself as the event data, so we know on which interface an EPOLLIN event occurred.

Parameters
epfdFile descriptor for an epoll instance
ifacePointer to a struct iface_t representing an interface
Returns
0 if successful, or -1 if unsuccessful
static u_char * get_mac ( struct iface_t iface)
static

Query the kernel for the MAC address of a network interface.

Parameters
ifacePointer to a struct iface_t representing an interface
Returns
Pointer to a static buffer containing ETH_ALEN bytes if successful, or NULL if unsuccessful
Note
For this to succeed, the interface must be an Ethernet interface
static int sockopt ( struct iface_t iface)
static

Set socket options for the skt field of a struct iface_t.

Attaches a bpf filter for the 802.1X EtherType, sets multicast or promiscuous mode, and requests a PACKET_AUXDATA cmsg from the kernel.

Parameters
ifacePointer to a struct iface_t representing an interface
Returns
0 if successful, or -1 if unsuccessful
See also
cmsg(3)
int iface_init ( struct iface_t ifaces,
int  epfd 
)

Create raw sockets for interfaces in a list and add them to an epoll instance.

Also set interface MAC if set-mac was specified in the config file.

Parameters
ifacesPointer to a list of struct iface_t structures representing network interfaces
epfdFile descriptor for an epoll instance
Returns
The number of interfaces added to epoll
int iface_count ( struct iface_t ifaces)

Count number of items in a list of struct iface_t.

Parameters
ifacesPointer to a list of struct iface_t structures representing network interfaces
Returns
The length of the list
int iface_set_mac ( struct iface_t iface,
u_char *  src_mac 
)

Set the MAC address of a network interface.

Parameters
ifacePointer to a struct iface_t representing an interface
src_macPointer to ETH_ALEN bytes containing a MAC address
Returns
0 if successful, or -1 if unsuccessful
Note
Brings the interface down and back up, invalidating all sockets on it
char* iface_strmac ( u_char *  mac)

Convert a MAC address to a string.

Parameters
macPointer to ETH_ALEN bytes containing a MAC address
Returns
Static buffer containing mac converted to a human-readable, colon-delimited MAC address
Note
Like ether_ntoa(3)
See also
ether_ntoa(3)

Variable Documentation

const u_char eapol_grp_mac[3][ETH_ALEN]
static
Initial value:
= {
{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 },
{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }
}

EAPOL multicast group MAC addresses.

See also
IEEE Std 802.1X-2010 §11.1.1
struct sock_filter eapol_sock_filter[]
static
Initial value:
= {
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 1, 0x0000888e },
{ 0x6, 0, 0, 0xbef001ed },
{ 0x6, 0, 0, 0x00000000 }
}

A simple bpf filter for EAPOL packets.

The tcpdump-style bpf assembly equivalent is:

1 (000) ldh [12]
2 (001) jeq #0x888e jt 2 jf 3
3 (002) ret #<decently big nonzero>
4 (003) ret #0
const struct sock_fprog eapol_fprog
static
Initial value:
= {
.len = 4,
}
static struct sock_filter eapol_sock_filter[]
A simple bpf filter for EAPOL packets.
Definition: iface.c:62

The complete bpf filter program provided to setsockopt(3)