Skip to content

Files

Latest commit

bbcf8e7 · Mar 5, 2025

History

History

packet-solutions

Tutorial: Packet - solutions

This directory contains solutions to all the assignments in the packet01, packet02, and packet03 lessons.

Table of Contents

Solutions

Packet01: packet parsing

Assignment 1: Fix the bounds checking error

See the parse_ethhdr function from the parsing_helpers.h file.

Assignment 2: Parsing the IP header

See the parse_ip6hdr function from the parsing_helpers.h file.

Assignment 3: Parsing the ICMPv6 header and reacting to it

See the parse_icmp6hdr function from the parsing_helpers.h file. The sequence number should be accessed as bpf_ntohs(icmp6h->icmp6_sequence) as it is a 2-byte value in the network order.

Assignment 4: Adding VLAN support

See the parse_ethhdr function from the parsing_helpers.h file.

Assignment 5: Adding IPv4 support

See the parse_iphdr and parse_icmphdr functions from the parsing_helpers.h file.

Packet02: packet rewriting

Assignment 1: Rewrite port numbers

An example XDP program can be found in the xdp_patch_ports section in the xdp_prog_kern_02.c file. The program will decrease by one destination port number in any TCP or UDP packet.

fix_port_egress section in the tc_reply_kern_02.c file will increase by one source port number in reply packet.

Steps to do before generate traffic: tc qdisc add dev eth0 clsact tc filter add dev eth0 egress bpf da obj tc_reply_kern_02.o sec tc tc filter show dev eth0 egress

Clean qdisc clsact: tc qdisc del dev eth0 clsact

Assignment 2: Remove the outermost VLAN tag

See the vlan_tag_pop function from the rewrite_helpers.h file. An example XDP program can be found in the xdp_vlan_swap section in the xdp_prog_kern_02.c file.

Assignment 3: Add back a missing VLAN tag

See the vlan_tag_push function from the rewrite_helpers.h file. An example XDP program can be found in the xdp_vlan_swap section in the xdp_prog_kern_02.c file.

Packet03: redirecting packets

Assignment 1: Send packets back where they came from

See the xdp_icmp_echo program in the xdp_prog_kern_03.c file.

Assignment 2: Redirect packets between two interfaces

See the xdp_redirect program in the xdp_prog_kern_03.c file.

Assignment 3: Extend to a bidirectional router

See the xdp_redirect_map program in the xdp_prog_kern_03.c file. User space part of the assignment is implemented in the xdp_prog_user.c file.

Assignment 4: Use the BPF helper for routing

See the xdp_router program in the xdp_prog_kern_03.c file. User space part of the assignment is implemented in the xdp_prog_user.c file.