Lecture 1.2: Linux and networking Roei Ben-Harush 2015.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Computer System Laboratory
The Journey of a Packet Through the Linux Network Stack
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
CCNA – Network Fundamentals
Intermediate TCP/IP TCP Operation.
CSE551: Computer Network Review r Network Layers r TCP/UDP r IP.
Transport Layer – TCP (Part1) Dr. Sanjay P. Ahuja, Ph.D. Fidelity National Financial Distinguished Professor of CIS School of Computing, UNF.
Packet Injection 101 Vivek Ramachandran. What is packet injection ? Please go through the raw socket tutorial before going further. Simply put packet.
CSCI 530 Lab Firewalls. Overview Firewalls Capabilities Limitations What are we limiting with a firewall? General Network Security Strategies Packet Filtering.
Page: 1 Director 1.0 TECHNION Department of Computer Science The Computer Communication Lab (236340) Summer 2002 Submitted by: David Schwartz Idan Zak.
TCP/IP Protocol Suite 1 Chapter 11 Upon completion you will be able to: User Datagram Protocol Be able to explain process-to-process communication Know.
Design and Implementation of a Server Director Project for the LCCN Lab at the Technion.
FIREWALLS & NETWORK SECURITY with Intrusion Detection and VPNs, 2 nd ed. 6 Packet Filtering By Whitman, Mattord, & Austin© 2008 Course Technology.
1 Netfilter in Linux Bing Qi Department of Computer Science and Engineering Auburn university.
Gursharan Singh Tatla Transport Layer 16-May
What Can IP Do? Deliver datagrams to hosts – The IP address in a datagram header identify a host IP treats a computer as an endpoint of communication Best.
1 Lab 3 Transport Layer T.A. Youngjoo Han. 2 Transport Layer  Providing logical communication b/w application processes running on different hosts 
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.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Network Layer ICMP and fragmentation.
Packet Filtering. 2 Objectives Describe packets and packet filtering Explain the approaches to packet filtering Recommend specific filtering rules.
1 Internet Protocol: Forwarding IP Datagrams Chapter 7.
LOGO “ Add your company slogan ” Damaga ( 다막아 ) 핵심 0703 프로젝트.
ISO Layer Model Lecture 9 October 16, The Need for Protocols Multiple hardware platforms need to have the ability to communicate. Writing communications.
Chapter 6: Packet Filtering
PA3: Router Junxian (Jim) Huang EECS 489 W11 /
LWIP TCP/IP Stack 김백규.
LWIP TCP/IP Stack 김백규.
1 Apache. 2 Module - Apache ♦ Overview This module focuses on configuring and customizing Apache web server. Apache is a commonly used Hypertext Transfer.
1 LAN Protocols (Week 3, Wednesday 9/10/2003) © Abdou Illia, Fall 2003.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
TCP Lecture 13 November 13, TCP Background Transmission Control Protocol (TCP) TCP provides much of the functionality that IP lacks: reliable service.
Hour 4 The Internet Layer 1. What You'll Learn in This Hour: IP addresses The IP header ARP ICMP 2.
Packet Filtering Chapter 4. Learning Objectives Understand packets and packet filtering Understand approaches to packet filtering Set specific filtering.
1 The Internet and Networked Multimedia. 2 Layering  Internet protocols are designed to work in layers, with each layer building on the facilities provided.
Chapter 6-2 the TCP/IP Layers. The four layers of the TCP/IP model are listed in Table 6-2. The layers are The four layers of the TCP/IP model are listed.
TCOM 515 IP Routing. Syllabus Objectives IP header IP addresses, classes and subnetting Routing tables Routing decisions Directly connected routes Static.
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
An initial study on Multi Path Routing Over Multiple Devices in Linux 2.4.x kernel Towards CS522 term project By Syama Sundar Kosuri.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Socket Program Training 10/24/2011. Introduction Video Streaming Server –Darwin VoIP –Asterisk Socket Program –Ex: FTP Backdoor Program 2.
Lecture 4 Overview. Ethernet Data Link Layer protocol Ethernet (IEEE 802.3) is widely used Supported by a variety of physical layer implementations Multi-access.
Virtual Machines Created within the Virtualization layer, such as a hypervisor Shares the physical computer's CPU, hard disk, memory, and network interfaces.
Linux Operations and Administration Chapter Eight Network Communications.
1 12-Jan-16 OSI network layer CCNA Exploration Semester 1 Chapter 5.
PLUS 내부 세미나 1/22 The Libnet Library 이병영 2004/05/06.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
Transport Protocols.
Firewalls A brief introduction to firewalls. What does a Firewall do? Firewalls are essential tools in managing and controlling network traffic Firewalls.
TCP/IP Illustrated, Volume 1: The Protocols Chapter 6. ICMP: Internet Control Message Protocol ( 월 ) 김 철 환
Internet Flow By: Terry Hernandez. Getting from the customers computer onto the internet Internet Browser
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
Ch. 23, 25 Q and A (NAT and UDP) Victor Norman IS333 Spring 2015.
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.
Lecture 1.1: Introduction. 2 Agenda 1 1 What is Firewall Types of Firewall 2 2 Firewall in linux kernel 3 3.
OS – Ex 1 Nezer J. Zaidenberg.
Introduction to TCP/IP networking
LWIP TCP/IP Stack 김백규.
Building an Internet Router
Jimit Mahadevia Nishit Shah This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
CS222 Web Programming Course Outline
Transmission Control Protocol (TCP)
FSU CIS 5930 Internet Protocols
Socket Program Training
TRANSMISSION CONTROL PROTOCOL
Net 323 D: Networks Protocols
Delivery, Forwarding, and Routing of IP Packets
Training 2018/10/11.
Virtual Private Network
Presentation transcript:

Lecture 1.2: Linux and networking Roei Ben-Harush 2015

2 Agenda 1 1 Linux kernel modules and networking Netfilter 2 2 About first Assignment Roei Ben-Harush 2015 Virtualization

3 1 1 Roei Ben-Harush 2015 Netfilter Linux kernel modules and networking About first Assignment 4 4

4 Virtual network Roei Ben-Harush 2015  Vm softwares, like Vmware or Virtualbox, provide a service which allow us to build not only virtual computers, but virtual networks too.

5 The Virtual NIC  Although we only have one NIC, we can simulate multiple NICs  vNICs send and receive data through the physical NIC  Data is written to register-variables, sent to the physical registers.  When an answer arrived the NIC interrupts, the virtualization layer inject the interrupt into the vNIC. Roei Ben-Harush 2015 Virtualization layer

6 The Virtual Workshop Roei Ben-Harush 2015 inout

7 The VMs  There are three virtual machines we will work with: 1.Fw: this is the firewall machine, which will sit in the middle of the topology. It runs ubuntu with gui, so you could easily program on it Roei Ben-Harush 2015 in out

8 The VMs  There are three virtual machines in the course site: 2-3. host1 and host2: testing machines which will communicate with each other though your firewall. Runs Ubuntu cli Roei Ben-Harush 2015 in out

9 Linux kernel modules and networking 1 1 Virtualization Linux kernel modules and networking 2 2 Netfilter 3 3 About first Assignment 4 4 Roei Ben-Harush 2015

10 What is a Kernel Module  What is a kernel module? (wiki definition) –An object file that contains code to extend the running kernel, or so-called base kernel, of an operating system.  What is a kernel module? –Pluggable “piece of code” to the operating system that adds functionality: –Device Driver –New features  Since we don’t have hardware we focus on features Roei Ben-Harush 2015

11 How kernel modules different from user- space programs  The kernel can’t use stdlib due to userspace/kernel space issues –Most of C library is implemented in the kernel –Besides, offers some nice utilities  Kernel Modules runs in kernel level (obviously) –It’s important to have as little as possible security holes –Don’t trust the user –Init your variables Roei Ben-Harush 2015

12 Building the Module  The purpose – eliminate the need to re-compile the kernel every time you need to add/remove a specific feature.  A Makefile that adapts itself to current kernel.  Put the module in and out the kernel by command line –Insmod to insert the module –Rmmod to remove it  Initialization function that is called when the module enters the kernel.  Cleanup function that is called when the module is removed from the kernel. Roei Ben-Harush 2015

13 Simple kernel module Roei Ben-Harush 2015 #include /* Needed by all modules */ #include /* Needed for KERN_INFO */ int init_module(void) { printk(KERN_INFO "Hello World!\n"); return 0; /* if non-0 return means init_module failed */ } void cleanup_module(void) { printk(KERN_INFO "Goodbye World!\n"); }

14 Explanation  init_module() is called when module is loaded  cleanup_module() is called when module is unloaded  Another option: Macros! Roei Ben-Harush 2015

15 Simple module with macros Roei Ben-Harush 2015 #include /* Needed by all modules */ #include /* Needed for KERN_INFO and for the Macros */ static int __init my_module_init_function(void) { printk(KERN_INFO "Hello World!\n"); return 0; /* if non-0 return means init_module failed */ } static void __exit my_module_exit_function(void) { printk(KERN_INFO "Goodbye World!\n"); } module_init(my_module_init_function); module_exit(my_module_exit_function);

16 What are Macros and why  __init and __exit macros tells the kernel when we use those functions  This allows to free kernel memory that is used only at init –Kernel memory is scarce, and we need to free it whenever we can Roei Ben-Harush 2015

17 Our Kernel Module – The Firewall!  What will we do with our kernel module? (spoilers ahead) –Register our own functions (AKA: hooks) with the netfilter API, to issue verdicts on packets going in/out/through our Linux box. –Register a char device, to communicate with the user space –Send commands to set module values. –Receive data from the firewall about the state of the system.  When our module will be removed, it will clean up all this mess, as if it was never there. Roei Ben-Harush 2015

18 Linux networking  We need a way to see the packets  Packets has headers for each layer and data  Linux contains set of useful tools and structures to manage packets over the kernel Roei Ben-Harush 2015

Each packet comes with headers, each for every layer. The layers we are interested in are the network layer and transport layer Packet Headers Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: IP header TCP/UDP header Roei Ben-Harush 2015

IP Header Roei Ben-Harush 2015

IP Header, how Linux knows it struct iphdr { #if defined(__LITTLE_ENDIAN_BITFIELD) __u8 ihl:4, version:4; #elif defined (__BIG_ENDIAN_BITFIELD) __u8 version:4, ihl:4; #else #error "Please fix " #endif __u8 tos; __u16 tot_len; __u16 id; __u16 frag_off; __u8 ttl; __u8 protocol; __u16 check; __u32 saddr; __u32 daddr; /*The options start here. */ }; Declared in linux/ip.h To be on the safe side, don’t forget to use htonl, htons, ntohl, ntohs when processing the fields Roei Ben-Harush 2015

Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: IP header Each packet comes with headers, each for every layer. The layers we are interested in are the network layer and transport layer Packet Headers TCP/UDP header Roei Ben-Harush 2015

Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: IP header Each packet comes with headers, each for every layer. The layers we are interested in are the network layer and transport layer Packet Headers TCP/UDP header Roei Ben-Harush 2015

TCP Header Roei Ben-Harush 2015

TCP Header, how Linux knows it struct tcphdr { __u16 source; __u16 dest; __u32 seq; __u32 ack_seq; #if defined(__LITTLE_ENDIAN_BITFIELD) __u16 res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; #elif defined(__BIG_ENDIAN_BITFIELD) … #else #error "Adjust your defines" #endif __u16 window; __u16 check; __u16 urg_ptr; }; Defined in linux/tcp.h Roei Ben-Harush 2015

UDP Header struct udphdr { __u16 source; __u16 dest; __u16 len; __u16 check; }; Defined in linux/udp.h Roei Ben-Harush 2015

ICMP Header struct icmphdr { __u8 type; __u8 code; __sum16 checksum; union { struct { __be16 id; __be16 sequence; } echo; __be32 gateway; struct { __be16 __unused; __be16 mtu; } frag; } un; }; Defined in linux/icmp.h Roei Ben-Harush 2015

Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: sk_buff IP header: TCP/UDP header The Linux struct that holds a pointer to the packet and metadata about the packet Hold many fields for many purposes head, data, tail, end transport layer headers tcphdr, udphdr, icmphdr network layer headers iph, ipv6h The packet is copied directly to the RAM (DMA) and each part of the kernel that touches it, just gets a pointer Roei Ben-Harush 2015

29 Agenda 1 1 Virtualization Linux kernel modules and networking 2 2 Netfilter 3 3 About first Assignment 4 4 Roei Ben-Harush 2015

30 Netfilter  Easy API to handle and examine packets from the internet –#include  Allow us to hook functions to the packet’s route on a specific (important) points though the kernel  There are 5 inspection points we can hook into, each for a different type of packets. Roei Ben-Harush 2015

31 Netfilter hook points  Where packets come in, having passed the simple sanity checks they are passed to the netfilter framework's NF_INET_PRE_ROUTING hook. Roei Ben-Harush 2015

32 Netfilter hook points  The routing code decides whether the packet is destined for another interface or a local process. If it's destined for the box itself, the netfilter framework is called again for the NF_INET_LOCAL_IN hook, before being passed to the process Roei Ben-Harush 2015

33 Netfilter hook points  If it's destined to pass to another interface instead, the netfilter framework is called for the NF_INET_FORWARD hook Roei Ben-Harush 2015

34 Netfilter hook points  The packet then passes a final netfilter hook, the NF_INET_POST_ROUTING hook, before the packet reaches the wire again. Roei Ben-Harush 2015

35 Netfilter hook points  The NF_INET_LOCAL_OUT hook is called for packets that are created locally. –Routing code is called before this hook to figure out the IP address and after this hook to decide the route. Roei Ben-Harush 2015

36  The hook function has a specific signature  It has specific input arguments that the kernel puts there – a sk_buff pointer to the sk_buff struct for the packet that has just passed through that specific point at the route –The hooknum (the place that the function was called from) –device_in and device_out, the network device sturct for the device that received the packet and for the device that should transmit the packet –A pointer to an “okay function” that will deal with the packet if all the hooks on that point will return ACCEPT struct nf_hook_ops { struct list_head list; /* User fills in from here down. */ nf_hookfn *hook; int pf; int hooknum; /* Hooks are ordered in ascending priority. */ int priority; }; unsigned int hook_func_in(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) The hook Roei Ben-Harush 2015

37 Agenda 1 1 Virtualization Linux kernel modules and networking 2 2 Netfilter 3 3 About first Assignment 4 4 Roei Ben-Harush 2015

38 Assignment 1 – Building the lab  In this assignment we will create the virtual lab  Use Virtualbox or Vmware workstation. –Don’t use Vmware player, it doesn’t support virtual networks  Pay attention for network configuration (make it persistent)  In the course site you’ll have 3 vms (for Virtualbox): Host1and Fw, so wont have to lose time installing Ubuntu machines. Most of the heavy coding will be performed on the fw vm. Roei Ben-Harush 2015

39 Assignment 1  Make sure you don’t have kernel panics when you load the module – this would have heavy penalty on your grade  In this assignment, you decide the verdict for each packet base on it’s type, not content. Make use of that fact to make it easier on you. That said, you can use whatever method you want.  This code will be used in the next assignments. Make it fabulous Roei Ben-Harush 2015