2015年2月24日 星期二

iptables

iptables是網路規則很重要的一環,
可以設定一些限制,




參考資料:
Iptables 指南 1.1.19
Chapter 18. Firewalling
iptables 設定入門
iptables 的用法
iptables指南笔记

2015年2月12日 星期四

送IPv6的RS(Router Solicitation)到指定的interface

IPv6的網路中,
client端會依據RA(Router Advertisement)的封包來設定IPv6 address.
詳情可以參考第十篇 IPv6 Path MTU及Router Advertisement

而RA的發送有兩個時機點,
1. 固定時間發送RA
2. 當收到client端送出RS(Router Solicitation)時,立刻以RA封包回應

現在遇到的問題是,router的發送RA的間隔設定很長的話,而網路又沒有其它client端送出RS封包,如此一來,剛接上去的client端網路要很久後才會被設定,這個是使用者所不能接受的。

因此,現在要嘗試來寫一個user mode的執行檔, 可以讓我針對指定的interface送出RS封包,
在參考busybox的ping6與radvd的source code後,總算完成了。

參考了IPv6 Essentials這本書, 裡面有講到一段話, 非常清楚的說明了RS的條件
「In the IP header of a Router Solicitation message, you will usually see the all-routers multicast address of ff02::2 as a Destination address. The hop limit is set to 255. The ICMP Type field is set to 133, which is the value for the Router Solicitation message. The Code field is unused and set to 0. The following two bytes are used for the Checksum. The next four bytes are unused and reserved for future use. The sender sets them to 0, and the receiver ignores those fields. For a Router Solicitation message, a valid option is the link-layer address of the sending host, if the address of the sending host is known. If the Source address on the IP layer is the unspecified (all-zeros) address, this field is not used. More options may bedefined in future versions of ND. If a host cannot recognize an option, it should ignore the option and process the packet.

Routers that receive this Solicitation message reply with a Router Advertisement message. Routers also issue those messages periodically.」

參考radvd裡面的send_ra
把其改寫成send_rs
但是,結果確把RS封包往LAN port送, 而我是想往WAN port送,
這個時候想到ping6可以指定傳送的介面,例:
# ping6 ff02::2 -I eth2.2

在改寫完後,可以確實的把RS往指定的interface送, 但是,router確不會回傳RA.
在比對完可以正確回傳RA的RS封包與我自己傳送的RS封包後, 跟上面那一段文字比對後,發現有可能是Hop limit我設定為1,
而正常的必需要是255
而這個部分必需要用setsockopt來完成~
int sockopt;
sockopt = 255;
setsockopt(pingsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &sockopt, sizeof(sockopt));

因為我們要送的是multi-cast的封包, 因為設定的Hop limit也是for multi-cast的,必需使用IPV6_MULTICAST_HOPS
uni-cast則是使用IPV6_UNICAST_HOPS

接下來則是sample code了
這中間使用到if_nametoindex→把eth2.2轉成index

getopt32(argc, argv, "qvc:s:I:", &opt_c, &opt_s, &opt_I);
會連動到option_mask32這一個變數,
當有發現到q則option_mask32的bit 0會被舉起來
當有發現到c的option時, 則option_mask32的bit 2會被舉起來
因-c後面要接參數, 所以opt_c接的是第一個參數