Professional Documents
Culture Documents
Network Packet Capture in Linux Kernelspace
Network Packet Capture in Linux Kernelspace
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
1 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Outline
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
2 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Introduction
Sniffers;
Improvements in packet reception;
Linux kernel network subsystem;
Beraldo Leal
3 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Sniffers
Beraldo Leal
4 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
5 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
1 2
1
2
kernel 3.0.0
source: wc, find, cat, etc..
Beraldo Leal
6 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Network stack
L5: Application
http, ftp, ssh, telnet, ... (message)
L4: Transport
tcp, udp, ... (segment)
L3: Network
ipv4, ipv6, ... (datagram/packet)
Beraldo Leal
8 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
net device
include/linux/netdevice.h
sk buff
include/linux/skbuff.h
Beraldo Leal
9 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
10 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
11 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
alloc skb();
dev alloc skb();
kfree skb();
dev kfree skb();
skb clone();
skb network header(skb);
skb transport header(skb);
skb mac header(skb);
Beraldo Leal
12 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
interrupt handler;
This interrupt handler will be called when a frame is received;
Typically in the handler, we allocate sk buff by calling
dev alloc skb();
Copies data from nics buffer to this struct just created;
nic call generic reception routine netif rx();
netif rx() put frame in per cpu queue;
if queue is full, drop!
net rx action() decision based on skb->protocol;
This function basically dequeues the frame and delivery a copy
for every protocol handler;
ptype all and ptype base queues
Beraldo Leal
13 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
ip local delivery())
ip local delivery() defrag fragmented packets, and call
Beraldo Leal
14 / 25
ip_local_deliver_nish()
(net/ipv4/ip_input.c)
nd protocol handler or
send icmp_dst_unreach
NF_IP_LOCAL_IN
NF_IP_PRE_ROUTING
<continue>
NF_IP_FORWARD
Layer 3
Network
ip_local_deliver()
(net/ipv4/ip_input.c)
defrag fragmented
packets
ip_rcv_nish()
(net/ipv4/ip_input.c)
nd route and handle
IP options
ip_forward()
(net/ipv4/ip_forward.c)
handle route alert;
send redirect if
necessary;
decrease TTL;
verify if frag is
possible (mtu)
ip_error()
(net/ipv4/route.c)
routing error, send
icmp pkt
ip_rcv()
packet_rcv()
arp_rcv()
(net/ipv4/ip_input.c)
<tcpdump_process>
(handle arp requests
verify skb, IP headers
<dhcpd process>
and replies)
and IP checksum
<...>
netif_rx()
(net/core/dev.c)
input_queue
[cpu]
Network Drivers
(drivers/net/*)
<...>
net_rx_action()
(net/core/dev.c)
decision based on
skb->protocol eld
Layer 1/2
Physical/Link
Application
userspace
kernelspace
Socket Layer
(net/core/sock.c)
__tcp_v4_lookup()
(net/ipv4/tcp_ipv4.c)
check for socket in
LISTEN, with dst_port
tcp_v4_do_rcv()
(net/ipv4/tcp_ipv4.c)
check for socket state
tcp_v4_rcv()
(net/ipv4/tcp_ipv4.c)
check for tcp headers
udp_rcv()
(net/ipv4/udp.c)
check for udp headers
ip_local_deliver_nish()
(net/ipv4/ip_input.c)
nd protocol handler or
send icmp_dst_unreach
NF_IP_LOCAL_IN
NF_IP_PRE_ROUTING
generate ICMP
error
<...>
Layer 4
Transport
<continue>
NF_IP_FORWARD
Layer 3
Network
ip_local_deliver()
(net/ipv4/ip_input.c)
defrag fragmented
packets
ip_rcv_nish()
(net/ipv4/ip_input.c)
nd route and handle
IP options
ip_forward()
(net/ipv4/ip_forward.c)
handle route alert;
send redirect if
necessary;
decrease TTL;
verify if frag is
possible (mtu)
ip_error()
(net/ipv4/route.c)
routing error, send
icmp pkt
Introduction
Network stack
Packet ingress flow
Methods to capture packets
protocol handler
register a function to handler packets with dev add pack()
netfilter hooks
userspace tools;
socket AF PACKET, libpcap, ...
Beraldo Leal
17 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Beraldo Leal
18 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int my packet rcv(struct sk buff skb, struct net device dev, struct packet type pt, struct net device
orig dev)
{
switch (skb>pkt type) {
case PACKET HOST:
printk(PACKET HOST );
break;
case PACKET BROADCAST:
printk(PACKET BROADCAST );
break;
case PACKET MULTICAST:
printk(PACKET MULTICAST );
break;
case PACKET OTHERHOST:
printk(PACKET OTHERHOST );
break;
case PACKET OUTGOING:
printk(PACKET OUTGOING );
break;
case PACKET LOOPBACK:
printk(PACKET LOOPBACK );
break;
case PACKET FASTROUTE:
printk(PACKET FASTROUTE );
break;
}
printk(%s 0x%.4X 0x%.4X \n, skb>dev>name, ntohs(skb>protocol), ip hdr(skb)>protocol)
kfree skb(skb);
return 0;
}
Beraldo Leal
19 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Netfilter hooks
iptables = userspace;
netfilter = kernelspace;
Netfilter is merely a series of hooks in various points in a
protocol stack;
packet filtering, network address [and port] translation
Beraldo Leal
20 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
References
Beraldo Leal
23 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Thankyou! Question?
Beraldo Leal
24 / 25
Introduction
Network stack
Packet ingress flow
Methods to capture packets
Beraldo Leal
25 / 25