2018年4月2日 星期一
Public DNS Reslover
⇨ CloudFlare Public DNS: 1.1.1.1 / 1.0.0.1 (APNIC)
https://blog.cloudflare.com/announcing-1111/
https://www.ithome.com.tw/news/122204
⇨ Google Public DNS: 8.8.8.8 / 8.8.4.4
⇨ Quad 9: 9.9.9.9
https://www.quad9.net/
IBM、Global Cyber Alliance和Packet Clearing House
⇨ OpenDNS: 208.67.222.222 / 208.67.220.220
OpenDNS Family: 208.67.222.123 / 208.67.220.123
⇨ Comodo Secure: 156.154.70.22 (NeuStar) / 156.156.71.22 (AFNIC)
⇨ DNS Advantage: 156.154.70.1 / 156.154.71.1 (NeuStar)
⇨ Norton DNS: 198.153.192.1 / 198.153.194.1 (Symantec)
⇨ DigitalOcean DNS
https://www.digitalocean.com/community/tutorials/an-introduction-to-digitalocean-dns
2017年10月11日 星期三
DPDK
Data Plane Development Kit (DPDK)
Intel所推出的處理封包的open source平台,在User space寫應用程式去處理每個封包
速度比原本的Linux networking stack還快,原本的基本上每個封包都要被複製兩次
因為是bypass kernel network stack機制,輸入ifconfig是看不到此網路介面
以下就是這兩種方式的比較

➠ Environment Abstraction Layer (EAL)
• Intel® DPDK loading and launching
• Support for multi-process and multi-thread execution types
• Core affinity/assignment procedures
• System memory allocation/de-allocation
• Atomic/lock operations
• Time reference
• PCI bus access
• Trace and debug functions
• CPU feature identification
• Interrupt handling
• Alarm operations
➠ Processor affinity
separate cores
➠ Poll Mode Driver (PMD)
no interrupts overhead
➠ Userspace I/O (UIO)
no copying from kernel
➠ Huge pages
no swap, TLB
➠ Lockless synchronization
avoid waiting
# tar -Jxvf dpdk-17.05.2.tar.xz
# cd dpdk-stable-17.05.2/
# make
/home/bh0322/workspace/dpdk-stable-17.05.2/drivers/net/pcap/rte_eth_pcap.c:39:18: fatal error: pcap.h: No such file or directory
#include <pcap.h>
# apt-get install libpcap-dev
Some sample applications can be tested after building them.
# make -C examples RTE_SDK=$(pwd) RTE_TARGET=build O=$(pwd)/build/examples
./build/examples/l3fwd/build/app/l3fwd -l 1,2 -n 4 -- -p 0x3 --config="(0,0,1),(1,0,2)" --parse-ptype
.
├── app //rte.app.mk
├── build
├── buildtools
├── config
├── devtools
├── doc
├── drivers
├── examples //rte.extapp.mk
├── lib
├── mk
├── pkg
├── test
├── usertools
└── x86_64-native-linuxapp-gcc
examples/
├── bond
├── cmdline
├── distributor
├── ethtool
├── exception_path
├── helloworld
├── ip_fragmentation
├── ip_pipeline
├── ip_reassembly
├── ipsec-secgw
├── ipv4_multicast
├── kni
├── l2fwd
├── l2fwd-cat
├── l2fwd-crypto
├── l2fwd-jobstats
├── l2fwd-keepalive
├── l3fwd
├── l3fwd-acl
├── l3fwd-power
├── l3fwd-vf
├── link_status_interrupt
├── load_balancer
├── multi_process
├── netmap_compat
├── packet_ordering
├── performance-thread
├── ptpclient
├── qos_meter
├── qos_sched
├── quota_watermark
├── rxtx_callbacks
├── server_node_efd
├── skeleton
├── tep_termination
├── timer
├── vhost
├── vhost_xen
├── vmdq
├── vmdq_dcb
參考資料
➠ http://dpdk.org/doc/guides-17.05/prog_guide/kernel_nic_interface.html#figure-kernel-nic-intf
➠ http://www.cnblogs.com/yhp-smarthome/p/7102557.html
➠ https://ifun01.com/ZEE4F54.html
➠ https://dpdksummit.com/Archive/pdf/2017India/DPDK-India2017-RamiaJain-ArchitectureRoadmap.pdf
➠ http://fast.dpdk.org/doc/pdf-guides/howto-17.08.pdf
➠ http://www.jianshu.com/p/bc2bde0e4442
➠ https://www.slideshare.net/MichelleHolley1/dpdk-multi-architecture-high-performance-packet-processing-72911726
➠ DPDK
➠ Quick start
➠ Understanding DPDK
➠ DPDK交流社区
Intel所推出的處理封包的open source平台,在User space寫應用程式去處理每個封包
速度比原本的Linux networking stack還快,原本的基本上每個封包都要被複製兩次
因為是bypass kernel network stack機制,輸入ifconfig是看不到此網路介面
以下就是這兩種方式的比較

➠ Environment Abstraction Layer (EAL)
• Intel® DPDK loading and launching
• Support for multi-process and multi-thread execution types
• Core affinity/assignment procedures
• System memory allocation/de-allocation
• Atomic/lock operations
• Time reference
• PCI bus access
• Trace and debug functions
• CPU feature identification
• Interrupt handling
• Alarm operations
➠ Processor affinity
separate cores
➠ Poll Mode Driver (PMD)
no interrupts overhead
➠ Userspace I/O (UIO)
no copying from kernel
➠ Huge pages
no swap, TLB
➠ Lockless synchronization
avoid waiting
Run-Time Environment (RTE)
# cd dpdk-stable-17.05.2/
# make
/home/bh0322/workspace/dpdk-stable-17.05.2/drivers/net/pcap/rte_eth_pcap.c:39:18: fatal error: pcap.h: No such file or directory
#include <pcap.h>
# apt-get install libpcap-dev
Some sample applications can be tested after building them.
# make -C examples RTE_SDK=$(pwd) RTE_TARGET=build O=$(pwd)/build/examples
./build/examples/l3fwd/build/app/l3fwd -l 1,2 -n 4 -- -p 0x3 --config="(0,0,1),(1,0,2)" --parse-ptype
.
├── app //rte.app.mk
├── build
├── buildtools
├── config
├── devtools
├── doc
├── drivers
├── examples //rte.extapp.mk
├── lib
├── mk
├── pkg
├── test
├── usertools
└── x86_64-native-linuxapp-gcc
examples/
├── bond
├── cmdline
├── distributor
├── ethtool
├── exception_path
├── helloworld
├── ip_fragmentation
├── ip_pipeline
├── ip_reassembly
├── ipsec-secgw
├── ipv4_multicast
├── kni
├── l2fwd
├── l2fwd-cat
├── l2fwd-crypto
├── l2fwd-jobstats
├── l2fwd-keepalive
├── l3fwd
├── l3fwd-acl
├── l3fwd-power
├── l3fwd-vf
├── link_status_interrupt
├── load_balancer
├── multi_process
├── netmap_compat
├── packet_ordering
├── performance-thread
├── ptpclient
├── qos_meter
├── qos_sched
├── quota_watermark
├── rxtx_callbacks
├── server_node_efd
├── skeleton
├── tep_termination
├── timer
├── vhost
├── vhost_xen
├── vmdq
├── vmdq_dcb
參考資料
➠ http://dpdk.org/doc/guides-17.05/prog_guide/kernel_nic_interface.html#figure-kernel-nic-intf
➠ http://www.cnblogs.com/yhp-smarthome/p/7102557.html
➠ https://ifun01.com/ZEE4F54.html
➠ https://dpdksummit.com/Archive/pdf/2017India/DPDK-India2017-RamiaJain-ArchitectureRoadmap.pdf
➠ http://fast.dpdk.org/doc/pdf-guides/howto-17.08.pdf
➠ http://www.jianshu.com/p/bc2bde0e4442
➠ https://www.slideshare.net/MichelleHolley1/dpdk-multi-architecture-high-performance-packet-processing-72911726
➠ DPDK
➠ Quick start
➠ Understanding DPDK
➠ DPDK交流社区
2017年9月18日 星期一
Checksum
➠ 核對總值(checksum)是一個冗餘的欄位,網路協定可藉以察覺傳輸錯誤。有些checksum不僅能偵測錯誤,而且還能自動修復特定類型的錯誤。
➠ 傳輸封包之前,傳送者會計算一個小而長度固定的欄位(checksum),其內包含了資料的某種雜湊值。如果資料的一些位元在傳輸時遭到改變,很有可能損毀之資料會產生不同的checksum。取決你用來產生checksum的函式,checksum的可靠度會有不同的等級。
錯誤不僅是在傳輸期間發生,也可能會在層級間移動資料時發生。每個協定都要負責確保其自身傳輸的正確性,而且不能假設其上或其下之層級會把這件任務搞定。
➠ IP checksum
在IPv4中,checksum是一個16位元的欄位,其中包括整個IP header(含選項在內),只牽涉到總和以及一的補數,因此過於薄弱而不可靠。想做比較可靠的健全度檢查(sanity check),得倚賴L2 CRC或是SSL/IPSec訊息鑑定碼(Message Authentication Codes,簡稱MACs)。
checksum會先由封包的來源地被計算出來,然後往其目的地的沿路上,一個轉運點接著一個轉運點地予以更新,以反應出每個router所施加的header。更新checksum之前,每個轉運點得先檢查封包的健全狀態(比較「封包裡的checksum」與「本地計算出來的checksum」)。如果健全度檢查失敗,封包就會被丟棄,但是不會產生ICMP: L4協定會搞定此事(例如,利用計時器,如果特定時間內沒接收到確認通知訊息,就會強迫重新傳輸)。
➠ 觸發checksum更新需求的情況
情況1: 遞減TTL
router轉送封包之前,必須遞減其IP header中的TTL欄位。
net/ipv4/ip_forward.c
int ip_forward(struct sk_buff *skb)
{
...
/* Decrease ttl after skb cow done */
ip_decrease_ttl(iph);
...
}
include/net/ip.h
變更IP header的一個欄位時,替IP checksum做增值式更新(incremental update)比從頭計算要快。
static inline
int ip_decrease_ttl(struct iphdr *iph)
{
u32 check = (__force u32)iph->check;
check += (__force u32)htons(0x0100);
iph->check = (__force __sum16)(check + (check>=0xFFFF));
return --iph->ttl;
}
情況2: 封包的調整(包括NAT)
牽涉到改變一個或數個IP header欄位的所有功能,都會迫時checksum重新計算。
情況3: IP選項的處理
因為option是header的一部分,它們被包含在checksum內。所以,每當option的處理方式使得IP header必須有所新增(例如,新增時間戳記)或修改時,就會迫時checksum重新計算。
情況4: 分段
當封包被分段時,每個片段都會具有不同的header。多數欄位依然不變,但是和分段有關的欄位就不同了,像是偏移量。因此,checksum也得重新計算。
註1. 計算TCP/IP Checksum
➠ TCP/UDP checksum
(2) 多數L4協定的checksum則包括其header和資料。
多數L2和L4協定都會提供checksum,讓L3也做這件事,就不見得非做不可了,基於相同的理由,IPv6已把checksum移除了。
➠ 某些情況下,已接收的frame上,於硬體中所計算的L4 checksum會失效
情況1: 一個輸入之L2 frame,包含了一些補白,以滿足最小frame尺寸所需,但是NIC不夠聰
明,在計算checksum時沒有跳過補白。
情況2: 當一個輸入之IP片段和前一個已接收之片段重疊時。
情況3: 當一個輸入之IP封包使用IPsec suite的協定之一時。NIC再也無法計算正確的L4
checksum,因為L4 header和payload會被壓縮、被摘要或是被加密。
情況4: IP層發生NAT或類似之干擾時,checksum就必須重算。
➠ L2 CRC (Cyclic redundancy check)
➠ 傳輸封包之前,傳送者會計算一個小而長度固定的欄位(checksum),其內包含了資料的某種雜湊值。如果資料的一些位元在傳輸時遭到改變,很有可能損毀之資料會產生不同的checksum。取決你用來產生checksum的函式,checksum的可靠度會有不同的等級。
錯誤不僅是在傳輸期間發生,也可能會在層級間移動資料時發生。每個協定都要負責確保其自身傳輸的正確性,而且不能假設其上或其下之層級會把這件任務搞定。
➠ IP checksum
在IPv4中,checksum是一個16位元的欄位,其中包括整個IP header(含選項在內),只牽涉到總和以及一的補數,因此過於薄弱而不可靠。想做比較可靠的健全度檢查(sanity check),得倚賴L2 CRC或是SSL/IPSec訊息鑑定碼(Message Authentication Codes,簡稱MACs)。
checksum會先由封包的來源地被計算出來,然後往其目的地的沿路上,一個轉運點接著一個轉運點地予以更新,以反應出每個router所施加的header。更新checksum之前,每個轉運點得先檢查封包的健全狀態(比較「封包裡的checksum」與「本地計算出來的checksum」)。如果健全度檢查失敗,封包就會被丟棄,但是不會產生ICMP: L4協定會搞定此事(例如,利用計時器,如果特定時間內沒接收到確認通知訊息,就會強迫重新傳輸)。
➠ 觸發checksum更新需求的情況
情況1: 遞減TTL
router轉送封包之前,必須遞減其IP header中的TTL欄位。
net/ipv4/ip_forward.c
int ip_forward(struct sk_buff *skb)
{
...
/* Decrease ttl after skb cow done */
ip_decrease_ttl(iph);
...
}
include/net/ip.h
變更IP header的一個欄位時,替IP checksum做增值式更新(incremental update)比從頭計算要快。
static inline
int ip_decrease_ttl(struct iphdr *iph)
{
u32 check = (__force u32)iph->check;
check += (__force u32)htons(0x0100);
iph->check = (__force __sum16)(check + (check>=0xFFFF));
return --iph->ttl;
}
情況2: 封包的調整(包括NAT)
牽涉到改變一個或數個IP header欄位的所有功能,都會迫時checksum重新計算。
情況3: IP選項的處理
因為option是header的一部分,它們被包含在checksum內。所以,每當option的處理方式使得IP header必須有所新增(例如,新增時間戳記)或修改時,就會迫時checksum重新計算。
情況4: 分段
當封包被分段時,每個片段都會具有不同的header。多數欄位依然不變,但是和分段有關的欄位就不同了,像是偏移量。因此,checksum也得重新計算。
註1. 計算TCP/IP Checksum
➠ TCP/UDP checksum
(2) 多數L4協定的checksum則包括其header和資料。
多數L2和L4協定都會提供checksum,讓L3也做這件事,就不見得非做不可了,基於相同的理由,IPv6已把checksum移除了。
➠ 某些情況下,已接收的frame上,於硬體中所計算的L4 checksum會失效
情況1: 一個輸入之L2 frame,包含了一些補白,以滿足最小frame尺寸所需,但是NIC不夠聰
明,在計算checksum時沒有跳過補白。
情況2: 當一個輸入之IP片段和前一個已接收之片段重疊時。
情況3: 當一個輸入之IP封包使用IPsec suite的協定之一時。NIC再也無法計算正確的L4
checksum,因為L4 header和payload會被壓縮、被摘要或是被加密。
情況4: IP層發生NAT或類似之干擾時,checksum就必須重算。
➠ L2 CRC (Cyclic redundancy check)
2017年9月5日 星期二
計算TCP/IP Checksum
在TCP/IP有三個checksum
如何計算:
1. IP header checksum
始終是由核心在軟體中進行計算和查證的
把資料以2 bytes為一組加總(checksum欄位除外)
45 00 00 30 cc 61 40 00 40 06 4c 02 0a 05 04 6b 0a 08 09 ed
4500 + 0030 + cc61 + 4000 + 4006 + 0a05 + 046b + 0a08 + 09ed
= 1b3fc
進位的再加回來
1 + b3fc = b3fd (1011 0011 1111 1101)
結果取1的補數
0100 1100 0000 0010 -> 4c 02
2. TCP checksum
(1) Pseudo Header: Source IP + Destination IP + Protocol + L4 Header Length
0a05 + 046b + 0a08 + 09ed + 0006 + 001c (28 bytes)
= 2287
(2) TCP header
把資料以2 bytes為一組加總(checksum欄位除外)
f3 dd 0c d3 d9 fa f8 26 00 00 00 00 70 02 ff ff 8e e9 00 00 02 04 05 b4 04 02 00 00
f3dd + 0cd3 +d9fa +f826 + 7002 + ffff + 0204 + 05b4 + 0402
= 44e8b
2287 + 44e8b = 47112
進位的再加回來
4 + 7112 = 7116 (0111 0001 0001 0110)
結果取1的補數
1000 1110 1110 1001 -> 8e e9
3. UDP checksum
(1) Pseudo Header: Source IP + Destination IP + Protocol + L4 Header Length
0a05 + 046b + 0808 + 0808 + 0011 + 0028 (40 bytes)
= 1EB9
(2) UDP header
把資料以2 bytes為一組加總(checksum欄位除外)
f3 42 00 35 00 28 73 c2
f342 + 35 + 28
= F39F
(3) UDP Payload
eb 3c 01 00 00 01 00 00 00 00 00 00 03 77 77 77 06 67 6f 6f 67 6c 65 03 63 6f 6d 00 00 01 00 01
eb3c + 100 + 1 + 377 + 7777 + 667 + 6f6f + 676c + 6503 +636f + 6d00 + 1 + 1
= 379E1
1EB9 + F39F + 379E1 = 48C39
進位的再加回來
4 + 8C39 = 8C3D (1000 1100 0011 1101)
結果取1的補數
0111 0011 1100 0010 -> 73 c2
➠ 核心計算checksum
➠ ip_send_check計算外出封包的IP checksum。
net/ipv4/ip_output.c
/* Generate a checksum for an outgoing IP datagram. */
void ip_send_check(struct iphdr *iph)
{
iph->check = 0;
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
}
EXPORT_SYMBOL(ip_send_check);
iphdr->check之值應該先變為零,因為checksum不應該反應出checksum本身。
因此使用的是簡單的求合法,零值欄位就能有效地被排除在所得到之checksum結果之外。
➠ 核心驗證checksum
如果checksum是正確的,而且進行轉送或接收的節點有對整個header執行此演算法(原本的iphdr->check欄位留著不動),結果會得到零。這種檢查損毀的方式比較快。
net/ipv4/ip_input.c
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
...
if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
goto csum_error;
}
----------------------------------------------------------------------------------
arch/x86/include/asm/checksum_64.h
➠ extern __sum16 ip_compute_csum(const void *buff, int len);
* ip_compute_csum - Compute an 16bit IP checksum. * @buff: buffer address. * @len: length of buffer. * * Returns the 16bit folded/inverted checksum of the passed buffer. * Ready to fill in. */
計算checksum的通用函式。它的輸入參數就是一個任意大小的緩衝區。
➠ static inline __sum16 csum_fold(__wsum sum)
* csum_fold - Fold and invert a 32bit checksum.
* sum: 32bit unfolded sum
*
* Fold a 32bit running checksum to 16bit and invert it. This is usually
* the last step before putting a checksum into a packet.
* Make sure not to mix with 64bit checksums.
*/
➠ extern __wsum csum_partial(const void *buff, int len, __wsum sum);
* csum_partial - Compute an internet checksum.
* @buff: buffer to be checksummed
* @len: length of buffer.
* @sum: initial sum to be added in (32bit unfolded)
*
* Returns the 32bit unfolded internet checksum of the buffer.
* Before filling it in it needs to be csum_fold()'ed.
* buff should be aligned to a 64bit boundary if possible.
*/
所計算的checksum缺少csum_fold所做的最後對褶步驟。L4協定會先呼叫csum_partial函式之一對L4資料做checksum計算,接著調用csum_tcpudp_magic之類的函式來計算假標頭checksum,最後求出這兩部分的checksum,並把結果合併。
net/ipv4/tcp_ipv4.c
void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr) { struct tcphdr *th = tcp_hdr(skb); if (skb->ip_summed == CHECKSUM_PARTIAL) { th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0); skb->csum_start = skb_transport_header(skb) - skb->head; skb->csum_offset = offsetof(struct tcphdr, check); } else { th->check = tcp_v4_check(skb->len, saddr, daddr, csum_partial(th, th->doff << 2, skb->csum)); } }
include/net/tcp.h
/* * Calculate(/check) TCP checksum */ static inline __sum16 tcp_v4_check(int len, __be32 saddr, __be32 daddr, __wsum base) { return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base); }
➠ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
* ip_fast_csum - Compute the IPv4 header checksum efficiently.
* iph: ipv4 header
* ihl: length of header / 4
*/
根據所指定的IP header和長度,計算並傳回IP checksum。這個函式可用來驗證輸入封包(input packet),並計算外出封包(outgoing packet)的checksum。
➠ static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __wsum sum)
* csum_tcpup_nofold - Compute an IPv4 pseudo header checksum. * @saddr: source address * @daddr: destination address * @len: length of packet * @proto: ip protocol of packet * @sum: initial sum to be added in (32bit unfolded) * * Returns the pseudo header checksum the input data. Result is * 32bit unfolded. */
➠ static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
__u32 len, __u8 proto,
__wsum sum)
* csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
* @saddr: source address
* @daddr: destination address
* @len: length of packet
* @proto: ip protocol of packet
* @sum: initial sum to be added in (32bit unfolded)
*
* Returns the 16bit pseudo header checksum the input data already
* complemented and ready to be filled in.
*/
static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
__u32 len, __u8 proto,
__wsum sum)
{
return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
}
net/core/skbuff.c
➠ __wsum skb_checksum(const struct sk_buff *skb, int offset, int len, __wsum csum)
幾乎都是由L4協定在特定情況下使用。
➠ Pseudo header 假標頭的定義只是為了計算checksum;假標頭並不存在於網路線上所流動的封包內。
➠ sk_buff和net_device相關欄位
入境/出境 區段的checksum查驗工作
net/ipv4/tcp_ipv4.c
➠ int tcp_v4_rcv(struct sk_buff *skb)
/* This routine computes an IPv4 TCP checksum. */
➠ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
{
struct tcphdr *th = tcp_hdr(skb);
if (skb->ip_summed == CHECKSUM_PARTIAL) {
th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
skb->csum_start = skb_transport_header(skb) - skb->head;
skb->csum_offset = offsetof(struct tcphdr, check);
} else {
th->check = tcp_v4_check(skb->len, saddr, daddr,
csum_partial(th,
th->doff << 2,
skb->csum));
}
}
include/net/tcp.h /*
* Calculate(/check) TCP checksum
*/
static inline __sum16 tcp_v4_check(int len, __be32 saddr,
__be32 daddr, __wsum base)
{
return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
}
➠ Checksum Offloads
net_device->features 裝置能力
NETIF_F_IP_CSUM
此裝置可以在硬體中計算L4 checksum,但是只針對使用IPv4的TCP和UDP。
static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
struct pktgen_dev *pkt_dev)
{
...
if (!(pkt_dev->flags & F_UDPCSUM)) {
skb->ip_summed = CHECKSUM_NONE;
} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
skb->ip_summed = CHECKSUM_PARTIAL;
skb->csum = 0;
udp4_hwcsum(skb, iph->saddr, iph->daddr);
} else {
__wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
/* add protocol-dependent pseudo-header */
udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
datalen + 8, IPPROTO_UDP, csum);
if (udph->check == 0)
udph->check = CSUM_MANGLED_0;
}
if (skb->ip_summed == CHECKSUM_PARTIAL)
參考資料:
http://www.tcpipguide.com/free/t_TCPChecksumCalculationandtheTCPPseudoHeader-2.htm
➠ 範例程式
https://github.com/bruce690813/checksum
➠ Checksum Offloads in the Linux Networking Stack
https://www.kernel.org/doc/Documentation/networking/checksum-offloads.txt
(Layer 3) IP header checksum : IPv4 header (option)
(Layer 4) TCP/UDP checksum : TCP/UDP Header + Pseudo Header + Payload
如何計算:
1. IP header checksum
始終是由核心在軟體中進行計算和查證的
![]() |
| IP封包資料 |
45 00 00 30 cc 61 40 00 40 06 4c 02 0a 05 04 6b 0a 08 09 ed
4500 + 0030 + cc61 + 4000 + 4006 + 0a05 + 046b + 0a08 + 09ed
= 1b3fc
進位的再加回來
1 + b3fc = b3fd (1011 0011 1111 1101)
結果取1的補數
0100 1100 0000 0010 -> 4c 02
2. TCP checksum
![]() |
| TCP封包資料 |
0a05 + 046b + 0a08 + 09ed + 0006 + 001c (28 bytes)
= 2287
(2) TCP header
把資料以2 bytes為一組加總(checksum欄位除外)
f3 dd 0c d3 d9 fa f8 26 00 00 00 00 70 02 ff ff 8e e9 00 00 02 04 05 b4 04 02 00 00
f3dd + 0cd3 +d9fa +f826 + 7002 + ffff + 0204 + 05b4 + 0402
= 44e8b
2287 + 44e8b = 47112
進位的再加回來
4 + 7112 = 7116 (0111 0001 0001 0110)
結果取1的補數
1000 1110 1110 1001 -> 8e e9
3. UDP checksum
![]() |
| UDP封包資料 |
0a05 + 046b + 0808 + 0808 + 0011 + 0028 (40 bytes)
= 1EB9
(2) UDP header
把資料以2 bytes為一組加總(checksum欄位除外)
f3 42 00 35 00 28 73 c2
f342 + 35 + 28
= F39F
(3) UDP Payload
eb 3c 01 00 00 01 00 00 00 00 00 00 03 77 77 77 06 67 6f 6f 67 6c 65 03 63 6f 6d 00 00 01 00 01
eb3c + 100 + 1 + 377 + 7777 + 667 + 6f6f + 676c + 6503 +636f + 6d00 + 1 + 1
= 379E1
1EB9 + F39F + 379E1 = 48C39
進位的再加回來
4 + 8C39 = 8C3D (1000 1100 0011 1101)
結果取1的補數
0111 0011 1100 0010 -> 73 c2
➠ 核心計算checksum
➠ ip_send_check計算外出封包的IP checksum。
net/ipv4/ip_output.c
/* Generate a checksum for an outgoing IP datagram. */
void ip_send_check(struct iphdr *iph)
{
iph->check = 0;
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
}
EXPORT_SYMBOL(ip_send_check);
iphdr->check之值應該先變為零,因為checksum不應該反應出checksum本身。
因此使用的是簡單的求合法,零值欄位就能有效地被排除在所得到之checksum結果之外。
➠ 核心驗證checksum
如果checksum是正確的,而且進行轉送或接收的節點有對整個header執行此演算法(原本的iphdr->check欄位留著不動),結果會得到零。這種檢查損毀的方式比較快。
net/ipv4/ip_input.c
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
...
if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
goto csum_error;
}
----------------------------------------------------------------------------------
arch/x86/include/asm/checksum_64.h
➠ extern __sum16 ip_compute_csum(const void *buff, int len);
* ip_compute_csum - Compute an 16bit IP checksum. * @buff: buffer address. * @len: length of buffer. * * Returns the 16bit folded/inverted checksum of the passed buffer. * Ready to fill in. */
計算checksum的通用函式。它的輸入參數就是一個任意大小的緩衝區。
➠ static inline __sum16 csum_fold(__wsum sum)
* csum_fold - Fold and invert a 32bit checksum.
* sum: 32bit unfolded sum
*
* Fold a 32bit running checksum to 16bit and invert it. This is usually
* the last step before putting a checksum into a packet.
* Make sure not to mix with 64bit checksums.
*/
➠ extern __wsum csum_partial(const void *buff, int len, __wsum sum);
* csum_partial - Compute an internet checksum.
* @buff: buffer to be checksummed
* @len: length of buffer.
* @sum: initial sum to be added in (32bit unfolded)
*
* Returns the 32bit unfolded internet checksum of the buffer.
* Before filling it in it needs to be csum_fold()'ed.
* buff should be aligned to a 64bit boundary if possible.
*/
所計算的checksum缺少csum_fold所做的最後對褶步驟。L4協定會先呼叫csum_partial函式之一對L4資料做checksum計算,接著調用csum_tcpudp_magic之類的函式來計算假標頭checksum,最後求出這兩部分的checksum,並把結果合併。
net/ipv4/tcp_ipv4.c
void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr) { struct tcphdr *th = tcp_hdr(skb); if (skb->ip_summed == CHECKSUM_PARTIAL) { th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0); skb->csum_start = skb_transport_header(skb) - skb->head; skb->csum_offset = offsetof(struct tcphdr, check); } else { th->check = tcp_v4_check(skb->len, saddr, daddr, csum_partial(th, th->doff << 2, skb->csum)); } }
include/net/tcp.h
/* * Calculate(/check) TCP checksum */ static inline __sum16 tcp_v4_check(int len, __be32 saddr, __be32 daddr, __wsum base) { return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base); }
➠ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
* ip_fast_csum - Compute the IPv4 header checksum efficiently.
* iph: ipv4 header
* ihl: length of header / 4
*/
根據所指定的IP header和長度,計算並傳回IP checksum。這個函式可用來驗證輸入封包(input packet),並計算外出封包(outgoing packet)的checksum。
➠ static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __wsum sum)
* csum_tcpup_nofold - Compute an IPv4 pseudo header checksum. * @saddr: source address * @daddr: destination address * @len: length of packet * @proto: ip protocol of packet * @sum: initial sum to be added in (32bit unfolded) * * Returns the pseudo header checksum the input data. Result is * 32bit unfolded. */
➠ static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
__u32 len, __u8 proto,
__wsum sum)
* csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
* @saddr: source address
* @daddr: destination address
* @len: length of packet
* @proto: ip protocol of packet
* @sum: initial sum to be added in (32bit unfolded)
*
* Returns the 16bit pseudo header checksum the input data already
* complemented and ready to be filled in.
*/
static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
__u32 len, __u8 proto,
__wsum sum)
{
return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
}
net/core/skbuff.c
➠ __wsum skb_checksum(const struct sk_buff *skb, int offset, int len, __wsum csum)
幾乎都是由L4協定在特定情況下使用。
➠ Pseudo header 假標頭的定義只是為了計算checksum;假標頭並不存在於網路線上所流動的封包內。
➠ sk_buff和net_device相關欄位
1. 用來儲存關於checksum的資訊
2. 裝置如何通知核心關於它們硬體checksum計算能力
3. L4協定如何使用這類資訊來決定是否替入境和出境封包計算checksum,或者讓NIC來做這件事。
#define CHECKSUM_NONE 0
#define CHECKSUM_UNNECESSARY 1 #define CHECKSUM_COMPLETE 2 #define CHECKSUM_PARTIAL 3
取決於skb是指向已收到之封包或已傳出去之封包,
skb->csum和skb->ip_summed這兩個欄位的意義會有所不同。
➠ 當一個封包被接收到時(RX)
skb->csum 記錄L4 checksum
skb->ip_summed 記錄L4 checksum的狀態
代表裝置驅動程式要告訴L4層的事。一旦L4接收常式接收到一些緩衝區時,
就可能改變skb->ip_summed的初始設定值。
include/linux/skbuff.h代表裝置驅動程式要告訴L4層的事。一旦L4接收常式接收到一些緩衝區時,
就可能改變skb->ip_summed的初始設定值。
#define CHECKSUM_NONE 0
#define CHECKSUM_UNNECESSARY 1 #define CHECKSUM_COMPLETE 2 #define CHECKSUM_PARTIAL 3
➠ 當一個封包被傳輸時(TX)
skb->csum 一個偏移量,它會指向緩衝區內某處(NIC會把它即將計算的checksum放在該處)
skb->ip_summed 記錄L4 checksum的狀態
供L4協定用來通知裝置,是否需要搞定checksum的計算工作。
當IP層知道有東西使得L4 checksum失效時(像是假標頭中有個欄位遭到修改),就會操作此欄位之值。
入境/出境 區段的checksum查驗工作
net/ipv4/tcp_ipv4.c
➠ int tcp_v4_rcv(struct sk_buff *skb)
/* This routine computes an IPv4 TCP checksum. */
➠ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
{
struct tcphdr *th = tcp_hdr(skb);
if (skb->ip_summed == CHECKSUM_PARTIAL) {
th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
skb->csum_start = skb_transport_header(skb) - skb->head;
skb->csum_offset = offsetof(struct tcphdr, check);
} else {
th->check = tcp_v4_check(skb->len, saddr, daddr,
csum_partial(th,
th->doff << 2,
skb->csum));
}
}
include/net/tcp.h /*
* Calculate(/check) TCP checksum
*/
static inline __sum16 tcp_v4_check(int len, __be32 saddr,
__be32 daddr, __wsum base)
{
return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
}
➠ Checksum Offloads
NETIF_F_IP_CSUM
此裝置可以在硬體中計算L4 checksum,但是只針對使用IPv4的TCP和UDP。
$ ethtool -k em3
Features for em3:
rx-checksumming: on
net/core/pktgen.cstatic struct sk_buff *fill_packet_ipv4(struct net_device *odev,
struct pktgen_dev *pkt_dev)
{
...
if (!(pkt_dev->flags & F_UDPCSUM)) {
skb->ip_summed = CHECKSUM_NONE;
} else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
skb->ip_summed = CHECKSUM_PARTIAL;
skb->csum = 0;
udp4_hwcsum(skb, iph->saddr, iph->daddr);
} else {
__wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
/* add protocol-dependent pseudo-header */
udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
datalen + 8, IPPROTO_UDP, csum);
if (udph->check == 0)
udph->check = CSUM_MANGLED_0;
}
if (skb->ip_summed == CHECKSUM_PARTIAL)
參考資料:
http://www.tcpipguide.com/free/t_TCPChecksumCalculationandtheTCPPseudoHeader-2.htm
➠ 範例程式
https://github.com/bruce690813/checksum
➠ Checksum Offloads in the Linux Networking Stack
https://www.kernel.org/doc/Documentation/networking/checksum-offloads.txt
2017年8月29日 星期二
netfilter防火牆
Netfilter為Linux kernel的一個防火牆機制,
一般運作於L2 (Link Layer)跟L3 (Network Layer)之間,
使用者利用iptables或nftables這個utility,
來下一些規則到底層的netfilter模組去做控制 [註一],
規則的組成主要分為chain跟table這兩大項, 匹配原則主要是first match。
chain1 PREROUTING
chain2 INPUT
chain3 FORWARD
chain4 OUTPUT
chain5 POSTROUTING
table1 filter (預設): limit
table2 nat: DNAT, SNAT, redirect
table3 mangle: mark, tos
table4 raw: connection tracking
下面說明當收到一個封包進來時,是如何處理的流程:
RX
當封包進來時,經由Layer1的NIC收到,根據這個封包的
4-tuple (source ip, source port, destination ip, destination port)
hash完交由cpu某一個core來處理,到了Layer2的NIC driver處理完之後
每個封包就會以skb的結構去呈現運作 [註二]。
當封包為route mode往上走到Layer3會檢查checksum validation
當checksum有問題就會丟棄這個封包,
之後開始進入netfilter的hook處理點,
經由PREROUTING處理完之後會查詢routing table
決定這封包的機會(deliver到INPUT)與命運(forward到FORWARD)
如果這封包是deliver上面給這台主機去處理的話,
通常會在INPUT上面的filter去下規則來limit保護;
反之,這封包是要forward出去的話 [註三],
就會在FORWARD上面的filter去下規則來limit保護。
一般封包目的端都是本機,所以查詢routing table之後
封包會deliver往上到INPUT處理,再經由Transport Layer的socket去處理
所以一般在user space上寫應用程式
來接收複製socket裡面的封包到user space的buffer來處理 [註四]
TX
當應用程式處理完要回應封包時,會把這個message送往socket處理
然後查詢routing table決定EGRESS的介面是那一個,
然後經由OUTPUT->POSTROUTING->NIC driver送往NIC出去
[註一]
# lsmod | grep table
ip6table_filter 12815 1
ip6_tables 26902 1 ip6table_filter
iptable_filter 12810 1
ip_tables 27116 1 iptable_filter
x_tables 34802 4 ip6table_filter,ip_tables,iptable_filter,ip6_tables
[註二]
一般常用抓封包的tcpdump是運作於Layer2,所以針對INGRESS進來的封包
下符合條件封包規則drop丟棄( L3),在tcpdump還是可以看到此封包。
[註三]
啟動forward: echo 1 > /proc/sys/net/ipv4/ip_forward
經過FORWARD的封包TTL會減一,通常避免會有loop產生
例子: 無線IP分享器,大多數封包都是走forward,除非是要更改設定那台的設定,封包才會deliver到本機的網頁去做設定的動作。
[註四]
基本上一個封包進來會被複製copy兩次
NIC -> skb buffer
skb buffer -> user buffer
訂閱:
文章 (Atom)













