Iptables101
coscup-2018
COSCUP2018
x
openSUSE.Asia GNOME.Asia
I am Hung-Wei Chiu
Co-organizer of SDNDS-TW
Co-organizer of CNTUUG
I love
Linux Network/Kubernetes/SDN
You can find me at:
blog.hwchiu.com
COSCUP2018
x
openSUSE.Asia GNOME.Asia
How Many People Known Iptables?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Network
Interface Card
PREROUUTING
Network
Interface Card
POSTROUUTING
INPUT OUTPUT
INPUT OUTPUT
FORWARDRouting Routing
LOCAL PROCESS
DNAT
COSCUP2018
x
openSUSE.Asia GNOME.Asia
We Don’t Focus On Those Table/Chain
Today
COSCUP2018
x
openSUSE.Asia GNOME.Asia
User Space
Kernel Space
iptables ebtables application
netlink/system call
Kernel
netfilter system
Network
Interface Card
Network
Interface Card
COSCUP2018
x
openSUSE.Asia GNOME.Asia
iptables, a command-line tool
COSCUP2018
x
openSUSE.Asia GNOME.Asia
iptables
Home:
○ https://www.netfilter.org/downloads.ht
ml
Git
○ git://git.netfilter.org/iptables.git
COSCUP2018
x
openSUSE.Asia GNOME.Asia
We Focus On What Will Happen For
Each Command
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Do You Have Meet The Following
Message?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Another app is currently holding
the xtables lock. Perhaps you
want to use the -w option?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Whathappen
iptables command needs a
communication between user and
kernel space.
It need a lock to make sure the
consistence
iptables will exit if it can’t acquire the
lock by default.
Use the –w option to wait the lock.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Let Read The Source Code
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
v
v
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
So, We Know The Iptables Use The File
Lock
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Do You Meet The Duplicated Rules ?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
How Could We Solve This?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
solution
Custom chain
○ Use the ‘-F’ to flush all rules.
Check before inserting rule
○ Use the ‘-C’ to check.
Modify the iptables to avoid
duplicated rules.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
How Could We Solve This?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, Let We Learn How To Flush The
Rules.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
c
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
First, we need to know how iptables
works with kernel?
COSCUP2018
x
openSUSE.Asia GNOME.Asia
libiptc
COSCUP2018
x
openSUSE.Asia GNOME.Asia
libiptc
Library which manipulates firewall
rules
Use the system call to interact with
kernel
○ GetSocketOpt
○ SetSocketOpt
Maintain a cache for each iptables
command.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
workflows
Initial the libiptc to fetch all current
rules.
Store those rules into a local cache
Operates rules in that cache
Commit the change to the kernel.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
workflows
Initial the libiptc to fetch all current
rules.
In the iptables, we use a handle
(xtc_handle) to represent the cache.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
initlibiptc
Initial the libiptc to fetch all current
rules.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, we have the cache of the current
rules.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Let We Flush Rules
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, We Have Remove Rules From
Cache
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
We Commit The Change After Any
Commands
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, We Have Flush The Rules.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, Let’s See What’s The Extension
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Custom Match Field
–m tcp –dport 1234
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Custom Target Field
–j AUDIT –type accept
COSCUP2018
x
openSUSE.Asia GNOME.Asia
User Space
Kernel Space
iptables
extensions
netlink/system call
Kernel
netfilter system
Network
Interface Card
Network
Interface Card
extensions
extensions
extensions
Kernel module
Kernel module
Kernel module
Kernel module
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Architecture
For each extension, you need to
prepare two things.
User-space library to parse the
command.
Kernel-space module to implement
that function.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
For User-Space, iptables command
should know how to parse arguments.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Howtoread
Function
○ DNAT (upper) -> target
○ tcp (lower) -> match
File naming
Old style
○ libipt_ -> ipv4
○ libip6t -> ipv6
New Style
○ libxt -> ipv4/ipv6
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Now, We Take The Custom Match TCP
as Example
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Architecture
iptables/extensions/libxt_tcp.c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Architecture
iptables/extensions/libxt_tcp.c
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
For Kernel-Space, There’re Some
Kernel Modules In The System.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
c
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
COSCUP2018
x
openSUSE.Asia GNOME.Asia
v
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Demo Time
COSCUP2018
x
openSUSE.Asia GNOME.Asia
summary
The iptables system includes the
user-space tool and kernel-space
system.
We focus on how user-space tools
works today.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
iptables
iptables need a file lock to protect the
rules.
iptables use the library (libiptc) to
control the rules via system call.
You can extend the iptables by
implement the extension
match/target function.
COSCUP2018
x
openSUSE.Asia GNOME.Asia
User Space
Kernel Space
iptables
extensions
netlink/system call
Kernel
netfilter system
Network
Interface Card
Network
Interface Card
extensions
extensions
extensions
Kernel module
Kernel module
Kernel module
Kernel module
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Extenstion
For each iptables extension module,
you should both user-space and
kernel-space.
Please make sure the kernel version
consistent
Use—Space
○ Implement the arguments and store the
data into pre-defined structure.
Kernel-Space
○ Implement the match function
COSCUP2018
x
openSUSE.Asia GNOME.Asia
Thanks!

More Related Content

PDF
Basics of firewall, ebtables, arptables and iptables
PDF
Overview of kubernetes network functions
PDF
Container Network Interface: Network Plugins for Kubernetes and beyond
PDF
How to Get Started With NGINX
PDF
Learn O11y from Grafana ecosystem.
PDF
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
PPTX
Introduction to CNI (Container Network Interface)
PPTX
Deep Dive into Building a Secure & Multi-tenant SaaS Solution with NATS
Basics of firewall, ebtables, arptables and iptables
Overview of kubernetes network functions
Container Network Interface: Network Plugins for Kubernetes and beyond
How to Get Started With NGINX
Learn O11y from Grafana ecosystem.
[MeetUp][2nd] 오리뎅이의_쿠버네티스_네트워킹_v1.2
Introduction to CNI (Container Network Interface)
Deep Dive into Building a Secure & Multi-tenant SaaS Solution with NATS

What's hot (20)

PDF
Simple and Scalable Microservices: Using NATS with Docker Compose and Swarm
PDF
Knative로 서버리스 워크로드 구현
PPTX
Tutorial: Using GoBGP as an IXP connecting router
PPTX
Intro to git and git hub
PDF
OpenShift Virtualization - VM and OS Image Lifecycle
PDF
Linux Networking Explained
PDF
Gitlab ci-cd
PDF
Container Networking Deep Dive
PDF
Kubernetes networking
PDF
Intro into Rook and Ceph on Kubernetes
PPTX
OpenvSwitch Deep Dive
PDF
LinuxCon 2015 Linux Kernel Networking Walkthrough
PDF
Demystifying EVPN in the data center: Part 1 in 2 episode series
PDF
Logs/Metrics Gathering With OpenShift EFK Stack
PDF
Monitoring Kubernetes with Prometheus
PDF
Networking in Java with NIO and Netty
PPTX
Git basics to advance with diagrams
PDF
[OpenInfra Days Korea 2018] (Track 4) - Grafana를 이용한 OpenStack 클라우드 성능 모니터링
PPTX
Service-mesh options with Linkerd, Consul, Istio and AWS AppMesh
PPTX
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Simple and Scalable Microservices: Using NATS with Docker Compose and Swarm
Knative로 서버리스 워크로드 구현
Tutorial: Using GoBGP as an IXP connecting router
Intro to git and git hub
OpenShift Virtualization - VM and OS Image Lifecycle
Linux Networking Explained
Gitlab ci-cd
Container Networking Deep Dive
Kubernetes networking
Intro into Rook and Ceph on Kubernetes
OpenvSwitch Deep Dive
LinuxCon 2015 Linux Kernel Networking Walkthrough
Demystifying EVPN in the data center: Part 1 in 2 episode series
Logs/Metrics Gathering With OpenShift EFK Stack
Monitoring Kubernetes with Prometheus
Networking in Java with NIO and Netty
Git basics to advance with diagrams
[OpenInfra Days Korea 2018] (Track 4) - Grafana를 이용한 OpenStack 클라우드 성능 모니터링
Service-mesh options with Linkerd, Consul, Istio and AWS AppMesh
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Ad

Similar to Understand the iptables step by step (20)

PDF
iptables 101- bottom-up
PDF
Advanced Namespaces and cgroups
PPTX
Nagios intro
PDF
Docker and-containers-for-development-and-deployment-scale12x
PDF
Spraykatz installation & basic usage
PDF
LXC Containers and AUFs
ODP
Linux Capabilities - eng - v2.1.5, compact
PDF
Practical virtual network functions with Snabb (SDN Barcelona VI)
PDF
Introducing Ansible
PDF
Spark Summit EU talk by Jorg Schad
PPTX
HPC Examples
PDF
No one puts java in the container
PDF
GNU Parallel și GNU Stow
PDF
Containers with systemd-nspawn
PDF
Nrpe - Nagios Remote Plugin Executor. NRPE plugin for Nagios Core
PDF
NRPE - Nagios Remote Plugin Executor. NRPE plugin for Nagios Core 4 and others.
PDF
Ephemeral DevOps: Adventures in Managing Short-Lived Systems
PDF
Tomcat from a cluster to the cloud on RP3
PDF
GNU Make, Autotools, CMake 簡介
PDF
Go 1.8 Release Party
iptables 101- bottom-up
Advanced Namespaces and cgroups
Nagios intro
Docker and-containers-for-development-and-deployment-scale12x
Spraykatz installation & basic usage
LXC Containers and AUFs
Linux Capabilities - eng - v2.1.5, compact
Practical virtual network functions with Snabb (SDN Barcelona VI)
Introducing Ansible
Spark Summit EU talk by Jorg Schad
HPC Examples
No one puts java in the container
GNU Parallel și GNU Stow
Containers with systemd-nspawn
Nrpe - Nagios Remote Plugin Executor. NRPE plugin for Nagios Core
NRPE - Nagios Remote Plugin Executor. NRPE plugin for Nagios Core 4 and others.
Ephemeral DevOps: Adventures in Managing Short-Lived Systems
Tomcat from a cluster to the cloud on RP3
GNU Make, Autotools, CMake 簡介
Go 1.8 Release Party
Ad

More from HungWei Chiu (20)

PDF
Learned from KIND
PDF
Debug Your Kubernetes Network
PDF
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
PDF
Learning how AWS implement AWS VPC CNI
PDF
Jenkins & IaC
PDF
The relationship between Docker, Kubernetes and CRI
PDF
PDF
Introduction to CRI and OCI
PDF
IP Virtual Server(IPVS) 101
PDF
Opentracing 101
PDF
iptables and Kubernetes
PDF
IPTABLES Introduction
PDF
Open vSwitch Introduction
PDF
Load Balancing 101
PDF
How Networking works with Data Science
PDF
Introduction to CircleCI
PDF
Head First to Container&Kubernetes
PDF
Kubernetes 1001
PDF
Application-Based Routing
PDF
Build Your Own CaaS (Container as a Service)
Learned from KIND
Debug Your Kubernetes Network
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
Learning how AWS implement AWS VPC CNI
Jenkins & IaC
The relationship between Docker, Kubernetes and CRI
Introduction to CRI and OCI
IP Virtual Server(IPVS) 101
Opentracing 101
iptables and Kubernetes
IPTABLES Introduction
Open vSwitch Introduction
Load Balancing 101
How Networking works with Data Science
Introduction to CircleCI
Head First to Container&Kubernetes
Kubernetes 1001
Application-Based Routing
Build Your Own CaaS (Container as a Service)

Recently uploaded (20)

PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
The various Industrial Revolutions .pptx
PDF
STKI Israel Market Study 2025 version august
PDF
Unlock new opportunities with location data.pdf
PPT
What is a Computer? Input Devices /output devices
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Architecture types and enterprise applications.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
CloudStack 4.21: First Look Webinar slides
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Taming the Chaos: How to Turn Unstructured Data into Decisions
Assigned Numbers - 2025 - Bluetooth® Document
A comparative study of natural language inference in Swahili using monolingua...
Chapter 5: Probability Theory and Statistics
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
The various Industrial Revolutions .pptx
STKI Israel Market Study 2025 version august
Unlock new opportunities with location data.pdf
What is a Computer? Input Devices /output devices
Univ-Connecticut-ChatGPT-Presentaion.pdf
Architecture types and enterprise applications.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Enhancing emotion recognition model for a student engagement use case through...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
sustainability-14-14877-v2.pddhzftheheeeee
Benefits of Physical activity for teenagers.pptx
CloudStack 4.21: First Look Webinar slides
NewMind AI Weekly Chronicles – August ’25 Week III
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game

Understand the iptables step by step