2011年12月10日 星期六

[Linux] route & /proc/net/route

Linux環境下, 通常可用 route 指令查詢目前的routing資訊.

jason@jason-laptop:~$ route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.2.0 * 255.255.255.0 U 2 0 0 wlan0

10.110.215.0 * 255.255.255.0 U 1 0 0 eth0

link-local * 255.255.0.0 U 1000 0 0 eth0

default 10.110.215.254 0.0.0.0 UG 0 0 0 eth0

jason@jason-laptop:~$

假若route程式未被編入"bin"目錄, 亦可直接自/proc/net/route取得相關資訊

jason@jason-laptop:~$ cat /proc/net/route

Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT

wlan0 0002A8C0 00000000 0001 0 0 2 00FFFFFF 00 0

eth0 00D76E0A 00000000 0001 0 0 1 00FFFFFF 00 0

eth0 0000FEA9 00000000 0001 0 0 1000 0000FFFF 00 0

eth0 00000000 FED76E0A 0003 0 0 0 00000000 00 0

此欄位中

1) IP address16進制表示.

0002A8C0 換算為十進制為C0.A8.02.00 = 192.168.2.0

2) Flag使用數字表示, 其含義可參照 net-tools-1.60中的檔案 "net-support.h" "inet_gr.c"

範例中,

1 = 1 = U

3 = 2*1 +1 = UG


(Ref.0)

Exploring the /proc/net/ Directory


(Ref.1) route --help 摘錄

route flag:

U (route is up)

H (target is a host)

G (use gateway)

R (reinstate route for dynamic routing)

D (dynamically installed by daemon or redirect)

M (modified from routing daemon or redirect)

A (installed by addrconf)

C (cache entry)

! (reject route)


(Ref.2) net-support.h 摘錄

/* Keep this in sync with /usr/src/linux/include/linux/route.h */
#define RTF_UP 0x0001 /* route usable "U" */
#define RTF_GATEWAY 0x0002 /* destination is a gateway "G" */
#define RTF_HOST 0x0004 /* host entry (net otherwise) "H" */
#define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
#define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
#define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
#define RTF_MTU 0x0040 /* specific MTU for this route */
#ifndef RTF_MSS
#define RTF_MSS RTF_MTU /* Compatibility :-( */
#endif
#define RTF_WINDOW 0x0080 /* per route window clamping */
#define RTF_IRTT 0x0100 /* Initial round trip time */
#define RTF_REJECT 0x0200 /* Reject route */

沒有留言: