Netfilter Framework
Orion Electric Age

IP sets, ipvs all based on the Linux kernel netfilter subsystem.

Key Structures, Variables and Functions

Netfilter Hooks and Tables

  • Netfilter Hooks in Kernel
    Netfilter Hooks in Kernel

  • Tables in Netfilter Hooks
    Tables in Netfilter Hooks
    Note: Dnat table in LOCAL_OUT actually is POST_RETOUING, but after modifty destination address/port called ip_route_me_harder(), used by iptable_nat and iptable_mangle + ip_queue, in function nf_nat_ipv4_local_fn().

  • Detail of Connection Tracking Hooks
    Detail of Connection Tracking Hooks
    For packet received, routing decision is done by ip_mkroute_input() called by ip_rcv_finish(), while the local out packet routing is __mkroute_output().

  • Transport Layer Trace(CentOS 7.4.1708 kernel-3.10.0.693)
    inet_init(): struct net_protocol inet_protos[MAX_INET_PROTOS], e.g. udp_protocol, tcp_protocol, igmp_protocol, icmp_protocol.

    • UDP receive path:
      1
      2
      3
      4
      5
      6
      ip_rcv_finish() --> ip_route_input_slow() -->ip_route_input_slow()
      --> forward: ip_mkroute_input() --> rth->dst.input = ip_forward;
      --> local_input: rth->dst.input= ip_local_deliver;
      --> skb_dst(skb)->input() --> ip_local_deliver() --> ip_local_deliver_finish() --> ipprot->handler()
      --> udp_rcv() --> __udp4_lib_rcv() --> udp_queue_rcv_skb() --> __udp_enqueue_schedule_skb() --> sk->sk_data_ready() --> sock_def_readable()
      -->__wake_up_common() --> curr->func() --> default_wake_function() --> try_to_wake_up() --> ttwu_queue()
    • UDP send path:
      1
      2
      3
      4
      5
      udp_sendmsg() --> ip_route_output_flow() --> (struct xfrm_policy_afinfo *) ->dst_lookup() --> struct xfrm_policy_afinfo xfrm4_policy_afinfo
      --> xfrm4_dst_lookup() --> __ip_route_output_key() --> __mkroute_output()
      --> rth->dst.input = ip_local_deliver;
      --> rth->dst.output = ip_output;
      --> udp_send_skb() --> ip_local_out() --> ip_output()

Userspace Tools

Reference

  • Linux Kernel Networking: Implemetation and Theory