在Linux上使用“u32 match ip sport 80”tc我可以匹配80端口,但是我怎样才能匹配端口范围10000 – 20000?
你可以使用面具,但很难:
u32 match ip sport 4096 0xf000 = sport 4096-8191 u32 match ip sport 8192 0xe000 = sport 8192-16383
0xf000 / 0xe000是掩码。 比较之前,从数据包中提取的单词与此掩码是按位进行比较的。
value = 0001000000000000 mask = 1111000000000000 start = 0001000000000000 end = 0001111111111111
你也可以为一个classid创build多个filter:
tc filter add dev eth0 parent 1:0 prio 3 protocol ip u32 match ip protocol 6 0xff match ip sport 10000 0xffff classid 1:13 tc filter add dev eth0 parent 1:0 prio 3 protocol ip u32 match ip protocol 6 0xff match ip sport 10001 0xffff classid 1:13 tc filter add dev eth0 parent 1:0 prio 3 protocol ip u32 match ip protocol 6 0xff match ip sport 10002 0xffff classid 1:13
对于你的例子,你需要创build几个'tcfilter':
first: sport 10000 0xfff0 (10000-10015) second: sport 10016 0xffe0 (10016-10047) .............
等等
10000=10011100010000 0010011100010000 | first not zero 1111111111111111 mask 1111111111110000=0xfff0
[基本上取范围内的第一个数字(在本例中为10,000),并将其转换为二进制(0010011100010000); 然后从右向左扫描这个二进制数,直到遇到第一个非0位; 然后将该位左边的位全部设置为1,并将其右边的所有位设置为零。 这就是你如何以0xfff0作为掩码。]
你可以使用ematch扩展匹配 – 而不是u32filter,它支持“小于”和“大于”比较。 看看手册页 。
以下内容匹配源端口范围为70-90(不包括)
tcfilteradd dev eth0 parent 1:protocol ip prio 1 basic match“cmp(u16 at 0 layer transport gt 70)and cmp(u16 at 0 layer transport lt 90)”flowid 1:3
我做了一个脚本来做感兴趣的人所描述的@alvosu。
http://blog.roeften.com/2017/01/mask-calculator.html
目前它将包括范围右侧的更多端口。
小提琴在http://jsfiddle.net/hp6dfnvg/13/
一个混乱的python实现:
import logging logging.getLogger().setLevel(logging.DEBUG) fp = 20001 # From port tp = 25000 # to port dev = 'ifb0' # dev to add filter rule to flow = '1:10' # flow id to set def find_lsm(p,o=1): m = o i = 1 while(True): if(p & m > 0): return m,i m = m << 1 i += 1 l = 0xffff ms = 2 me = None cp = fp i = 0 t = '' while(True): cm,s = find_lsm(cp) if cm >= tp: break e = cp | cm - 1 m = l & (l << s -1) logging.info('Range %i - %i, mask %08x', cp, e, m ) i += 1 t += "tc filter add dev {} parent 1: prio {} protocol ip u32 match ip protocol 17 0xff match ip dport {} 0x{:0>8x} flowid {}\n".format(dev,i,cp,m,flow) cp += cm print t
我在我的博客上创build了这个简单的脚本来创build任何端口范围的掩码…
我厌倦了使用Googlesearch,只是为了find错误的方法来做到这一点…享受!
http://marcelustrojahn.blogspot.com/2011/06/u32-port-masks_14.html 网站是下降:(