顯示具有 pi 標籤的文章。 顯示所有文章
顯示具有 pi 標籤的文章。 顯示所有文章

2015年12月2日 星期三

iptables

iptables 需要將ppp設為MASQUERADE偽裝IP才有辦法連到internet,但是這樣做會造成inner ip顯示全部變為gateway ip。修正方法為改用SNAT加指定EXTIP,但是就變成要有固定IP才可以使用。

出現錯誤訊息

Options error: server and client cannot be used together

代表conf檔案內有用到不是server指令的指令

iptables restart in ubuntu

sudo service ufw stop
sudo service ufw start

刪除單一chain下的規則
iptables -t nat -D POSTROUTING <number>

enable forwarding in ubuntu
echo 1 >/proc/sys/net/ipv4/ip_forward

如果要兩個網路能夠成功的達成路由﹐對方網路也必須有相應的路由設定指向本地網路才行

pfsense 內 openvpn檔案存放位置/var/etc/openvpn

traceroute
mac: traceroute
dos: tracert
linux: tracepath

terminate openvpn
sudo killall openvpn
參考資料
http://acman.bluenest.net/wordpress/archives/118
http://askubuntu.com/questions/298419/how-to-disconnect-from-openvpn
iptables
http://serverfault.com/questions/431593/iptables-forwarding-between-two-interface
http://askubuntu.com/questions/161551/how-to-start-stop-iptables
http://stackoverflow.com/questions/8239047/iptables-how-to-delete-postrouting-rule
http://s2.naes.tn.edu.tw/~kv/iptables.htm
https://gigenchang.wordpress.com/2014/04/19/10%E5%88%86%E9%90%98%E5%AD%B8%E6%9C%83iptables/
http://www.pcnet.idv.tw/pcnet/network/network_ip_routing.htm
pfsense存放位置
https://forum.pfsense.org/index.php?topic=13123.0
openvpn with two interface in and out
https://forums.openvpn.net/topic11033.html
https://forums.openvpn.net/topic14072.html
http://ubuntuforums.org/showthread.php?t=1606136
route
http://yinung2.blogspot.tw/2013/05/route_17.html
bridge vs routing
https://community.openvpn.net/openvpn/wiki/BridgingAndRouting
https://openvpn.net/index.php/open-source/documentation/miscellaneous/76-ethernet-bridging.html
chain using openvpn
http://serverfault.com/questions/512160/vpn-chaining-using-openvpn
dns server on openvpn
http://superuser.com/questions/637579/setting-dns-servers-using-openvpn-client-config-file
show route on mac
http://stackoverflow.com/questions/6782658/how-to-get-default-gateway-in-mac-osx
自己

2015年10月5日 星期一

Forward PPTP server packet from Pi

這個問題花了我不少時間去處理,原因居然是因為Rasbian使用的3.18 kernel將PPTP packet視為invalid packet。不過還好後來有找到解決方法,分享給各位。

ping: sendmsg: Operation not permitted
出現以上訊息時代表iptables沒設定好,可重設iptables
# Reset/Flush iptables
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Flush end
但須注意此時已經policy設為ACCEPT,為debug使用,記得結束後要將iptables -P 設回需要的policy

PPTP forward參考設定如下

#!/bin/sh
### BEGIN INIT INFO
# Provides:         firewall.sh
# Required-Start:   $all
# Required-Stop:
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
### END INIT INFO

INTIF="你的對內介面,如brlan"

INTNET="對內介面的網段"

INTIP="PPTP server的IP"

EXTIF="對外介面,如為撥接通常為ppp0"

#Loading required stateful/NAT kernel modules...

/sbin/depmod -a
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
/sbin/modprobe iptable_nat
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_nat_irc

#below has to be loaded for pptp
/sbin/modprobe nf_nat_pptp 
/sbin/modprobe nf_conntrack_pptp 
/sbin/modprobe nf_conntrack_proto_gre 
#就是上面這三個kernel需要load,否則再怎麼設都是枉然

echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

UNIVERSE="0.0.0.0/0"

iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

iptables -A FORWARD -i $EXTIF -o $INTIF -d $INTIP -j ACCEPT

# Enable SNAT (MASQUERADE) functionality on $EXTIF
iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

#DMZ setting
iptables -t nat -A PREROUTING -i $EXTIF -p tcp -m multiport --dport 1:65535 -j DNAT --to $INTIP
iptables -t nat -A PREROUTING -i $EXTIF -p udp -m multiport --dport 1:65535 -j DNAT --to $INTIP

# PPTP PREROUTING GRE packets
iptables -t nat -A PREROUTING -i $EXTIF -p 47 -j DNAT --to $INTIP
iptables -A FORWARD -i $EXTIF -p 47 -j ACCEPT

如果不是使用DMZ的話可將DMZ setting改成--dport 1723只轉port 1723到PPTP server就好
另外如果是使用pfsense之類的firewall,INTIP為pfsense的WAN端




將script註冊為service
sudo update-rc.d <script undre /etc/init.d/> defaults
將service移除
sudo update-rc.d -f foobar remove
設定啟動相依性(如在某service啟動後啟動)
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs

2015/10/5更新
遇到一個問題是source ip被snat蓋掉。因為以前使用ddwrt時沒有遇到這個問題,因此研究了一下,發現是需要設-m state --state NEW在iptables裡對應PREROUTING的port。才能夠觸發output。否則單純用MASQUERADE是可以但是就變成snat會將source ip蓋掉變成內部ip,不是我要的。
因此上面#DMZ setting要多增加兩行
iptables -t nat -A INPUT -i $EXTIF -p udp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT
iptables -t nat -A INPUT -i $EXTIF -p tcp -m state --state NEW -m multiport --dport 1:65535 -j ACCEPT

參考資料
register service
https://mkaz.github.io/2013/07/03/run-script-at-start-on-debian/
setting linux firewall
http://www.aboutdebian.com/firewall.htm
http://www.linuxjournal.com/article/3866
PPTP forward
http://ubuntuforums.org/showthread.php?t=801207
http://www.linuxquestions.org/questions/linux-networking-3/port-forward-gre-and-pptp-using-iptables-210334/
http://serverfault.com/questions/466030/pptp-iptables-routing-issue
https://wiki.archlinux.org/index.php/PPTP_server#iptables_firewall_configuration
https://lists.debian.org/debian-firewall/2004/04/msg00103.html
http://wiki.linuxmce.org/index.php/PPTP_server
tcpdump PPTP packet
http://serverfault.com/questions/342604/how-to-sniff-request-packet-on-vpn-server
DMZ, nat preserve source ip
https://www.debian-administration.org/article/73/Port_forwarding_for_iptables_DMZ