ipset 和 dnsmasq 不匹配怎么办?

2015-01-22 16:30:47 +08:00
 sxd1988

路由器没有OpenWRT的支持,刷了Tomato by shibby的固件
ipset的版本
ipset v4.5, protocol version 4.
Kernel module protocol version 4.

dnsmasq的版本
Dnsmasq version 2.71 Copyright (c) 2000-2014 Simon Kelley
Compile time options: IPv6 GNU-getopt no-RTC no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset Tomato-helper auth DNSSEC

这个版本的dnsmasq是支持ipset的,于是创建了一个ipset
ipset -N ss iphash

并在dnsmasq里面添加了
ipset=/google.com/ss

通过查看dnsmasq的日志可以看到类似这样的
Jan 22 15:40:59 dnsmasq[28160]: reply www.google.com is 74.125.68.105
Jan 22 15:40:59 dnsmasq[28160]: ipset add ss 74.125.68.147 www.google.com

也就是说dnsmasq有打算将IP地址添加到set里面?但是通过ipset -L查看却得到空的结果
Name: ss
Type: iphash
References: 1
Header: hashsize: 1024 probes: 8 resize: 50
Members:

ipset -h查看帮助,里面说明的添加IP地址的指令是这样的
--add -A setname IP
Add an IP to a set
经过测试,通过这个指令手动添加单个IP地址是可行的。

但是从这里 http://ipset.netfilter.org/ipset.man.html 查到的指令却是这样的
ipset add SETNAME ADD-ENTRY [ ADD-OPTIONS ]

区别就在“--”之间,怀疑就是这个问题导致的dnsmasq和ipset没办法衔接上,但是却想不到解决的办法,请求各位大大的帮助,谢谢!

12783 次点击
所在节点    问与答
19 条回复
LazyZhu
2015-01-22 16:38:01 +08:00
--ipset=/google.com/ss
sxd1988
2015-01-22 16:47:39 +08:00
@LazyZhu 你是说在Custom configuration里面的ipset前面加“--”?我试试
Do
2015-01-22 16:47:55 +08:00
OP玩的不多不清楚。Tomato的shibby Mod里面ipset模块是默认不加载的,需要init时候自行加载,你可以查查是不是这个原因。(在不加载的时候ipset命令也是能运行,dnsmasq的ipset参数也是正常工作。)
sxd1988
2015-01-22 16:51:28 +08:00
@LazyZhu 不行,dnsmasq -c 1500 直接不认,返回这样的结果
dnsmasq: bad option at line 61 of /etc/dnsmasq.conf
如果直接 dnsmasq --ipset=/google.com/ss 则返回这样的结果
dnsmasq: failed to bind DHCP server socket: Address already in use

问题可能不是出在添加规则上,而是出在添加规则后没有被正确的执行。
LazyZhu
2015-01-22 16:51:49 +08:00
@sxd1988 不是, 我的意思是如果命令行的话用--ipset=/google.com/ss
LazyZhu
2015-01-22 16:59:45 +08:00
另外楼上也提到了问题可能出在内核的ipset,
检查加载了没:
insmod ip_set
sxd1988
2015-01-22 17:09:40 +08:00
@LazyZhu
@Do
应该也不是,因为手动的ipset --add -A ss <IP地址> 是可以正常添加IP地址的,并可以正常通过iptables使用shadowsocks
sxd1988
2015-01-22 17:13:46 +08:00
@Do
init的时候添加了modprobe ip_set

@LazyZhu insmod ip_set返回了这样的信息
insmod: can't insert '/lib/modules/2.6.36.4brcmarm/kernel/net/ipv4/netfilter/ip_set.ko': File exists
LazyZhu
2015-01-22 17:17:54 +08:00
那可能是ipset版本问题了, dnsmasq不支持旧的的ipset
http://ipset.netfilter.org/install.html
sxd1988
2015-01-22 17:27:06 +08:00
@LazyZhu 有可能,我的kernel版本2.6.36.4应该能用ipset-6.24吧?晚上回去试着编译一下。话说就算编译好了,可以替换系统里面原有的吗?
LazyZhu
2015-01-22 17:36:31 +08:00
重编译的应该是dnsmasq,
openwrt有个patch,看看是不是应用了:
https://dev.openwrt.org/browser/trunk/package/network/services/dnsmasq/patches/110-ipset-remove-old-kernel-support.patch
或者编译最新的dnsmasq试试
sxd1988
2015-01-22 23:20:06 +08:00
@LazyZhu 我用的Tomato,编译好了DNSMasq,可是却不知道怎么替换掉原来的,提示是只读文件系统
sxd1988
2015-01-23 09:28:54 +08:00
@LazyZhu 果然是你说的这个问题,我的Tomato的linux是2.6.36,在dnsmasq的/src/ipset.c中有这样一句判断
old_kernel = (version < KERNEL_VERSION(2,6,32));
判定成新版本的ipset,可是tomato中用的还是旧的4.5版本的ipset,于是强制old_kernel = 1;重新编译。

在没办法替换原有的dnsmasq的情况下,只好把自己编译的dnsmasq放到jffs里面,然后在dnsmasq的配置文件中配置
port=60053
添加iptables规则
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 60053

成功!
sxd1988
2015-01-23 14:40:34 +08:00
发现一个更好的方式,就是在启动脚本里面添加
mount --bind /jffs/dnsmasq /usr/sbin/dnsmasq
然后
killall dnsmasq
dnsmasq会自动重启,重启以后就是JFFS里面的新版了

这样做替代了原有的dnsmasq,iptables也少了一条规则。
xmvagrant
2015-02-04 17:31:52 +08:00
@LazyZhu
@sxd1988 我的OpenWrt版本是Attitude Adjustment 12.09,Kernel Version是3.3.8,安装dnsmasq-full时使用 http://downloads.openwrt.org/snapshots/trunk/ar71xx/generic/packages/base/dnsmasq-full_2.72-4_ar71xx.ipk 这里的版本,装好后运行/etc/init.d/dnsmasq start无报错,但是dnsmasq进程未能启动,试了 https://github.com/aa65535/openwrt-dnsmasq 这里预编译好的版本也一样情况,是不是我的OpenWrt版本太老了dnsmasq-full不支持,请大神帮我分析下是什么情况,谢谢!
LazyZhu
2015-02-04 17:48:40 +08:00
@xmvagrant 升级至BB
xmvagrant
2015-02-04 17:53:46 +08:00
@LazyZhu 嗯,我试试看,多谢。
xmvagrant
2015-02-05 01:14:07 +08:00
@LazyZhu 已升到BB,顺利搞定shadowsocks + dnsmasq-full + ipset + iptables的方案,只是instagram的图片有些打得开,有些又不能开,不知道什么原因,你有遇到过吗?
oglop
2015-09-23 12:35:02 +08:00
我用的是 2.72 的 dnsmasq (按 ls 的说法用 mount --bind 替换了 router 自带的 2.66 ), tomato shibby v131, 为何还是不会自动加 ipset ?
我把下面两行加到 webui 的 advanced->dhcp/dns->Dnsmasq Custom configuration 里了

```
server=/.xiami.com/127.0.0.1#53
ipset=/.xiami.com/gfwlist
```
重启路由器,dnsmasq.conf 里面也有我加的那两条了
```
root@unknown:/tmp/home/root# cat /etc/dnsmasq.conf
pid-file=/var/run/dnsmasq.pid
resolv-file=/etc/resolv.dnsmasq
addn-hosts=/etc/dnsmasq/hosts
dhcp-hostsfile=/etc/dnsmasq/dhcp
expand-hosts
min-port=4096
stop-dns-rebind
rebind-localhost-ok
interface=br0
dhcp-range=tag:br0,192.168.2.200,192.168.2.253,255.255.255.0,1440m
dhcp-option=tag:br0,3,192.168.2.1
interface=br1
dhcp-range=tag:br1,10.0.0.2,10.0.0.254,255.255.255.0,1440m
dhcp-option=tag:br1,3,10.0.0.1
dhcp-lease-max=255
dhcp-authoritative
interface=ppp4,ppp5,ppp6,ppp7,ppp8,ppp9
no-dhcp-interface=ppp4,ppp5,ppp6,ppp7,ppp8,ppp9
server=/.xiami.com/127.0.0.1#53
ipset=/.xiami.com/gfwlist
```
可是为何 ipset 里没有?
```
root@unknown:/tmp/home/root# ipset -L
Name: gfwlist
Type: iphash
References: 1
Header: hashsize: 1024 probes: 8 resize: 50
Members:
```
看 log 里似乎 dnsmasq 没有试着添加 ipset ?
```
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: started, version 2.72 cachesize 1500
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: compile time options: IPv6 GNU-getopt no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth no-DNSSEC loop-detect
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp9 does not currently exist
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp8 does not currently exist
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp7 does not currently exist
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp6 does not currently exist
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp5 does not currently exist
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: warning: interface ppp4 does not currently exist
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: asynchronous logging enabled, queue limit is 5 messages
Jan 1 01:00:35 unknown daemon.info dnsmasq-dhcp[809]: DHCP, IP range 10.0.0.2 -- 10.0.0.254, lease time 1d
Jan 1 01:00:35 unknown daemon.info dnsmasq-dhcp[809]: DHCP, IP range 192.168.2.200 -- 192.168.2.253, lease time 1d
Jan 1 01:00:35 unknown daemon.warn dnsmasq[809]: ignoring nameserver 127.0.0.1 - local interface
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: reading /etc/resolv.dnsmasq
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: using nameserver 202.65.242.122#53
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: using nameserver 202.65.242.130#53
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: read /etc/hosts - 3 addresses
Jan 1 01:00:35 unknown daemon.info dnsmasq[809]: read /etc/dnsmasq/hosts/hosts - 3 addresses
Jan 1 01:00:35 unknown daemon.info dnsmasq-dhcp[809]: read /etc/dnsmasq/dhcp/dhcp-hosts
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/164529

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX