1 Netfilter in Linux Bing Qi Department of Computer Science and Engineering Auburn university.

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Computer System Laboratory
COMS W6998 Spring 2010 Erich Nahum
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
Ipchains and Iptables Linux operating system natively supports packet-filtering rules: Kernel versions 2.2 and earlier support the ipchains command. Kernel.
Lecture 1.2: Linux and networking Roei Ben-Harush 2015.
CSCI 530 Lab Firewalls. Overview Firewalls Capabilities Limitations What are we limiting with a firewall? General Network Security Strategies Packet Filtering.
Linux Netfilter Code Trace Part1: Iptable 周世中嚴長青.
ITINERANT: TCP Socket Migration Titus Winters Dan Berger CS 202: Spring ‘03.
1 Network Address Translation (NAT) Relates to Lab 7. Module about private networks and NAT.
Page: 1 Director 1.0 TECHNION Department of Computer Science The Computer Communication Lab (236340) Summer 2002 Submitted by: David Schwartz Idan Zak.
Packet Mangling for Fun & Profit A Brief Intro to Netfilter, User Mode Linux, and the Linux TCP/IP Stack.
Firewall Lab Zutao Zhu 02/05/2010. Outline Preliminaries getopt LKM /proc filesystem Netfilter.
Tutorial and Demos on Linux Virtual Machine
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
Operating System Program 5 I/O System DMA Device Driver.
Small Form Computing A bump in the wire. The questions ● What can we do with an inexpensive small computer? ● Can we make it a part of a seamless wireless.
NetFilter – IPtables Firewall –Series of rules to govern what Kind of access to allow on your system –Packet filtering –Drop or Accept packets NAT –Network.
07/11/ L10/1/63 COM342 Networks and Data Communications Ian McCrumRoom 5B18 Tel: voice.
For OS Experiments. What Do We Need? A Computer &
LWIP TCP/IP Stack 김백규.
NETWORK SECURITY USING IPTABLES. TOPICS OF DISCUSSION NETWORK TRAFFIC IN PRESENT SCENARIO !! WHY WE NEED SECURITY ? T TYPE OF ATTACKS & WAYS TO TACKLE.
Firewalling With Netfilter/Iptables. What Is Netfilter/Iptables? Improved successor to ipchains available in linux kernel 2.4/2.6. Netfilter is a set.
IPtables Objectives Contents Practicals Summary
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
Firewall Tutorial Hyukjae Jang Nc lab, CS dept, Kaist.
Application Block Diagram III. SOFTWARE PLATFORM Figure above shows a network protocol stack for a computer that connects to an Ethernet network and.
IP1 The Underlying Technologies. What is inside the Internet? Or What are the key underlying technologies that make it work so successfully? –Packet Switching.
An initial study on Multi Path Routing Over Multiple Devices in Linux 2.4.x kernel Towards CS522 term project By Syama Sundar Kosuri.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Netfilter.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Linux Kernel Backdoors And Their Detection Joanna Rutkowska ITUnderground Conference, October 12 th -13 th 2004, Warsaw.
LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate.
Firewall C. Edward Chow CS691 – Chapter 26.3 of Matt Bishop Linux Iptables Tutorial by Oskar Andreasson.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
Introduction to Linux Firewall
Network Router Security Packeting Filtering. OSI Model 1.It is the most commonly refrenced protocol model. It provides common ground when describing any.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
LINUX® Netfilter The Linux Firewall Engine. Overview LINUX® Netfilter is a firewall engine built into the Linux kernel Sometimes called “iptables” for.
Chapter 4: server services. The Complete Guide to Linux System Administration2 Objectives Configure network interfaces using command- line and graphical.
Virtual Memory Mohammad H. Mofrad February 23, 2016
Linux Firewall Iptables.
Session 25: Netlink Sockets 문준혁. Index What are netlink sockets? Opening a netlink socket –Family Netlink messages –nlmsg_type –nlmsg_flags Lab.
Lecture 3: Stateless Packet Filtering. 2 Agenda 1 1 Linux file system - networking sk_buff 2 2 Stateless packet filtering 3 3 About next assignment 4.
Netfilter Framework Jimit Mahadevia Nishit Shah This work is licensed under a Creative Commons Attribution-Share.
Conntrack + NAT Helpers Jimit Mahadevia Nishit Shah This work is licensed under a Creative Commons Attribution-Share.
Lecture 1.1: Introduction. 2 Agenda 1 1 What is Firewall Types of Firewall 2 2 Firewall in linux kernel 3 3.
Network Address Translation (NAT)
Zero-copy Receive Path in Virtio
COMP2322 Lab 5 IP Steven Lee March 22, 2017.
Network Address Translation (NAT)
Jimit Mahadevia Nishit Shah This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
Computer System Laboratory
Network Address Translation (NAT)
Want to play a game? – Linux Kernel Modules
Network and System Security Risk Assessment
Chapter 6: Network Layer
Net 323: NETWORK Protocols
IS3440 Linux Security Unit 6 Using Layered Security for Access Control
FSU CIS 5930 Internet Protocols
Setting Up Firewall using Netfilter and Iptables
CS 6560 Operating System Design
Lab 4 Kernel Module Operating System Lab.
CS 6560 Operating System Design Kernel Loadable Modules
From ACCEPT to MASQUERADE Tim(othy) Clark (eclipse)
Firewalls.
Presentation transcript:

1 Netfilter in Linux Bing Qi Department of Computer Science and Engineering Auburn university

2 Netfilter  A framework for packet mangling in kernel  Built in Linux 2.4 kernel or higher version  Independent of network protocol stack  Provide an easy way to do firewall setting or packet filtering, network address translation and packet mangling

3 Netfilter II—how it works  Defines a set of hooks Hooks are well defined point in the path of packets when these packets pass through network stack The protocol code will jump into netfilter framework when it hits the hook point.  Registers Kernel functions to these hooks: Called when a packet reaches at hook point Can decide the fate of the packets After the functions, the packet could continue its journey

4 Netfilter Hooks  Each protocol family can have different hooks IPv4 defines 5 hooks Upper layer Route IP_input Network driver device Route IP_output NF_PRE_ROUTING NF_IP_POSTROUTING NF_IP_LOCAL_INNF_IP_LOCAL_OUT NF_IP_FORWARD

5 Netfilter hook points  NF_IP_PRE_ROUTING: After sanity checks, before routing decision  NF_IP_LOCAL_IN: After routing decisions, if the packet destines for this host  NF_IP_FORWARD: Packets are not destined for this host but need to be forwarded to another interface  NF_IP_LOCAL_OUT: All packets created from local host would come here before it is been sent out  NF_IP_POST_ROUTING: All packets have been routed and ready to hit the wire

6 Netfilter hook functions  Multiple functions could register to the same hook point Need to set priority The corresponding packet will path the hook points Each function registered for that hook point is called in the order of priority and is free to manipulate the packet  What to do in hook functions The function could do anything it wants But need to tell netfilter to return one of the five values:  NF_ACCEPT  NF_DROP  NF_STOLEN  NF_QUEUE  NF_REPEAT

7 How to use netfilter framework  Iptables subsystem  Write your own kernel functions, register to certain hook points, do whatever you want to process the packets

8 Iptables  Based on netfilter framework  Some modules that register to hook points  Use generic table structure for the definition of rules.  Very powerful Customize your own firewall setting User space tools (iptables) to load rules into different tables

9 Install iptables  Download iptables at  RPM rpm –ivh iptable*.i386.rpm  Source code tar zcvf iptable&*.tar.gz cd iptable*./configure make make install

10 Iptables—basic functionalities  Packet filter control Security Corresponding to “filter” table  NAT-network address translation Switch the source or destination address Sharing internet access Corresponding to “NAT” table  Packet mangle Mangling packets going through the firewall Ex: change TOS or TTL value, mark packets

11 Iptables command  Use iptables command to load the rule set  Basic iptables commands include: Which table to work on An operation Which hook point in this table to use A match A target  For example: Iptables –t filter –A INPUT –p tcp –j DROP Iptables –t nat –A PREROUTING –p tcp –d –j DNAT –to- destination Iptables –l –v –n

12 Add your own functions to kernel  Two ways to extend kernel codes Open source programming  Need to find the place to insert our code  Recompile the whole Linux kernel  Need to reboot the system to let new image work  Time-consuming, difficult to debug Loadable kernel module

13 Loadable kernel module  It is a chunk of code, inserted and unloaded dynamically  Like the normal user space programs but it works in kernel space and have access to kernel resources  Modules take effect immediately after loading it without recompiling  Saving time to extend the kernel

14 Write the kernel module  Different from normal C program Modules execute in kernel space May not use some standard function libraries of C No main functions Module need to define two functions with the name:  init_module: start entry  Cleanup_module: end entry  After linux 2.4, Macro is used, we could use any name as our start and end functions, but need to use Module_init(“startfunctionname”); Module_exit(“endfunctionname”); Use insmod to load, rmmod to unload

15 A simple hello module  helloworld_module.c: #define MODULE #include static int __init init_module(void) { printk(" Hello, world\n"); /* is message priority. */ return 0; } static int __exit cleanup_module(void) { printk(" Goodbye cruel world\n"); }  printk (basic kernel service) outputs to /var/log/messages  obj-m = helloworld_module.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules  Use “insmod./helloworld_module.ko” to load it into the kernel space  cat /var/log/message or dmesg to see the output

16 Netfilter Hook implementation  Steps Fill out the nf_hook_ops structure Write a hook function:  It has specific format Register to system Compile and load the modules  Example: Iptables –t filter –A INPUT –p tcp –j DROP Drop all the incoming tcp packet

17 Netfilter Hook implementation (I)  Fill in a netfilter hook operation structure Data type: (in include/linux/netfilter.h)

18 Example static struct nf_hook_ops localin_ops; localin_ops.hook = localin_handler; localin_ops.pf = PF_INET; localin_ops.hooknum =NF_IP_LOCAL_IN; localin_ops.priority=NF_IP_PRI_FIRST;

19 Netfilter Hook implementation II  Format: must be of function type nf_hookfn Unsigned int nf_hookfn(hooknum,struct sk_buff**skb,in_dev,out_dev,int(*okfn)(struct sk_buff*)))  Hooknum: the hook point where the function registered  skb: a reference to the packet  In_dev,outdev: net device this packet is from/to  Hook function returns one of the following: NF_ACCEPT,NF_DROP,NF_STOLEN NF_QUEUE,NF_REPEAT

20 unsigned int localin_handler (unsigned int hook, struct sk_buff **skb, const struct net_device *indev, const struct net_device *outdev, int (*okfn) (struct sk_buff *)) { struct iphdr *iphead = skb->nh.iph; if ((iphead->protocol)== IPPROTO_TCP) // Drop all TCP packet return NF_DROP; } Example

21 Netfilter Hook implementation III  Call this function to register at the hook int nf_register_hook(struct nf_hook_ops *reg) void nf_unregister_hook(struct nf_hook_ops *reg) #include … static int __init init (void){ return nf_register_hook (&localin_ops); } static void __exit fini (void){ return nf_unregister_hook (&localin_ops); } module_init (init); module_exit (fini);  Compile it and insmod

22 Conclusion  Netfilter is a glue code between the protocol hooks and the kernel function modules  Iptables is useful to set our firewall  Provide a simple way to hack packet

23 Reference            The Linux Kernel Module Programming Guide The Linux Kernel Module Programming Guide  …..