Giter Site home page Giter Site logo

dmolik / netlink-examples Goto Github PK

View Code? Open in Web Editor NEW
24.0 5.0 11.0 59 KB

Linux Netlink and IPTables Examples

License: GNU General Public License v3.0

Makefile 0.55% C 99.45%
linux netlink networking sockets veth route device rtnetlink namespace libiptc

netlink-examples's Introduction

Netlink Examples

Some low-level examples of the Linux Netlink interface.

  1. pair create a virtual ethernet pair
  2. link_address create a pair and add an address
  3. refactor rewrite link_address in a more concise manner
  4. namespace new network namespace, code courtesy iproute2
  5. pair_ns move the virtual peer into the network namespace
  6. ns_addr add an address to a virtual peer in a namespace
  7. ns_gw add a default route in the namespace and turn on the links
  8. masquerade a libiptc example to add a masquerade route
  9. forward a libiptc module to add forwarding rules
  10. final Combine it all into a single binary

You're going to need a C compiler, GNU Make, and iptables-devel or iptables-dev in order to compile the final output.

The goal of this project is to recreate the following script:

#!/bin/bash

ip link add veth1 type veth peer name vpeer1

ip link set vpeer1 netns ns1

ip addr add 172.16.1.1/24 dev veth1
ip link set veth1 up

ip netns exec ns1 ip addr add 172.16.1.2/24 dev vpeer1
ip netns exec ns1 ip link set vpeer1 up
ip netns exec ns1 ip link set lo up

ip netns exec ns1 ip route add default via 172.16.1.1
echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A POSTROUTING -s 172.16.1.0/255.255.255.0 -o eth0 -j MASQUERADE

iptables -A FORWARD -i eth0 -o veth1 -j ACCEPT
iptables -A FORWARD -o eth0 -i veth1 -j ACCEPT

Further reading

I found the following list very helpful when putting together this project

netlink-examples's People

Contributors

dmolik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.