iptables ip iw

Guide to IP Layer Network Administration with Linux

http://linux-ip.net/html/index.html


配置工具: https://en.wikipedia.org/wiki/Iproute2

    第一代网络配置工具: ifconfig+route+arp+netstat(基于proc和ioctl)、 iptables、iw

    第二代网络配置工具: ip(基于netlink)、 iptables 、iw

  

Linux内核维护了4张iptables表、 一个RPDB(Routing Policy DataBase)、 256张route表。 在内核协议栈里面设置了5个hook点: PREROUTING、 INPUT、 FORWARD、 OUTPUT、 POSTROUTING。 四张iptables表优先级由高到底: raw --> mangle --> nat --> filter。 路由的过程是先在RPDB里面找到对应的路由表,然后在路由表里面找到路由项。 256张路由表: 254号是主路由,255号是local路由+广播地址,253号表是default表,0号是全路由。每一个路由表对应一个或多个路由策略。 通过路由策略,可以为每个网卡提供一张路由表。

路由类型: unicast/segment/nat/unreachable/prohibit/blackhole

路由策略: lookup/nat/unreachable/prohibit/blackhole

默认PRDB: ip rule show

    0:    from all lookup local

    32766:    from all lookup main

    32767:    from all lookup default

数据流如下:

    输入流: Driver --> hook(PREROUTING) --> ROUTE --> hook(INPUT)

    输出流: App --> hook(OUTPUT) --> ROUTE --> hook(POSTROUTING)

    转发流: Driver --> hook(PREROUTING) --> ROUTE --> hook(FORWARD) --> ROUTE -->hook(POSTROUTING)


Bridge

  • brctl show #显示网桥信息
  • brctl addbr/delbr <bridge_name>
  • brctl addif/delif <bridge_name> <device_name>


Linux路由表索引/etc/iproute2/rt_tables, android路由表索引/data/misc/net/rt_tables。

  • ip rule show 查看路由策略route policy。
  • ip route show table [TABLE_ID|TABLE_NAME] 查看TABLE_ID号路由表。
  • ip rule add from 192.168.1.10/32 table 1 pref 100 增加路由策略。
  • ip route add default via 192.168.1.1 table 1 向1号表添加默认路由。
  • ip route add 192.168.0.0/24 via 192.168.1.2 table 1 向1号表添加网段路由。
  • ip route flush cache 使路由生效。


  • ip neigh show 查看邻居表。(arp -a)
  • ip addr add 192.168.1.100/24 dev eth0 配置IP,可配置成多IP。
  • ip link set dev eth0 address 08:00:27:75:2a:67


 查看iptables:

iptables -nvL –line-number

iptables -t nat -L

iptables -t filter -L


清空iptables:

    iptables -t nat -F

    iptables -t nat -X

    iptables -t raw -F

    iptables -t raw -X

    iptables -t mangle -F

    iptables -t mangle -X

    iptables -t filter -F

    iptables -t filter -X

默认ACCEPT

    iptables -P INPUT ACCEPT

    iptables -P OUTPUT ACCEPT

    iptables -P FORWARD ACCEPT


-A添加 -I插入 -D删除 -R修改


Example: create_ap

ip link set down dev wlan0

ip addr flush wlan0

ip link set up dev wlan0

ip addr add 192.168.12.1/24 broadcast 192.168.12.255 dev wlan0

iptables -w -t nat -I POSTROUTING -s 192.168.12.0/24 ! -o wlan0 -j MASQUERADE

iptables -w -I FORWARD -i wlan0 -s 192.168.12.0/24 -j ACCEPT

iptables -w -I FORWARD -i eth0 -d 192.168.12.0/24 -j ACCEPT

iptables -w -I INPUT -p tcp -m tcp --dport 5353 -j ACCEPT

iptables -w -I INPUT -p udp -m udp --dport 5353 -j ACCEPT

iptables -w -t nat -I PREROUTING -s 192.168.12.0/24 -d 192.168.12.1 -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 5353

iptables -w -t nat -I PREROUTING -s 192.168.12.0/24 -d 192.168.12.1 -p udp -m udp --dport 53 -j REDIRECT --to-ports 5353


把所有10.8.0.0网段的数据包SNAT成192.168.5.3的ip然后发出去 ------>

iptables-t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT --to-source 192.168.5.3

将源地址为172.16.93.0/24网段的数据包的源地址都转换为10.0.0.1 ------>

iptables -t nat -A POSTROUTING -s 172.16.93.0/24  -j SNAT --to-source 10.0.0.1

将请求IP为10.0.0.1的数据包转发到后端172.16.93.1主机上 ------>

iptables -t nat -A PREROUTING -d 10.0.0.1 -j DNAT –-to-destination 172.16.93.1

将请求IP为10.0.0.1并且端口为80的数据包转发到后端的172.16.93.1主机上 ------>

iptables -t nat -A PREROUTING -d 10.0.0.1 -p tcp –-dport 80 -j DNAT –-to-destination 172.16.93.1

(通过定义不同的端口,就可以实现PNAT,将同一个IP不同的端口请求转发到后端不同的主机)

在上条规则的基础上,发往后端的数据包的目标端口改为8080,在后端主机的web服务器上使用8080端口接收 ---->

iptables -t nat -A PREROUTING -d 10.0.0.1 -p tcp –-dport 80 -j DNAT –-to-destination 172.16.93.1:8080


对接收到的源ip地址为192.168.1.100和192.168.1.200的数据包进行源NAT(SNAT):

iptables -A POSTROUTING -o eth0 -s 192.168.1.100 -j SNAT --to 202.110.123.100

iptables -A POSTROUTING -o eth0 -s 192.168.1.200 -j SNAT --to 202.110.123.200

所有来自192.168.1.100和192.168.1.200的数据包都将分别被伪装成由 202.110.123.100和202.110.123.200。


对接收到的目的ip为202.110.123.100和202.110.123.200的所有数据包进行目的NAT(DNAT):

iptables -A PREROUTING -i eth0 -d 202.110.123.100 -j DNAT --to 192.168.1.100

iptables -A PREROUTING -i eth0 -d 202.110.123.200 -j DNAT --to 192.168.1.200

所有目的ip为202.110.123.100和202.110.123.200的数据包都将分别被转发给192.168.1.100和 192.168.1.200。


所有从eth1进入的请求80和82端口的数据,被转发到80端口 ---->

iptables -t nat -A PREROUTING - -i eth1 -p tcp -m multiport --dports 80,82 -j REDIRECT --to-ports 80


在路由之前所有从eth0进入的目的端口为53的数据包,都发送到1.2.3.4这台服务器解析 ---->

iptables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination 1.2.3.4:53

iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 53 -j DNAT --to-destination 1.2.3.4:53


所有从eth0(外网卡)出来的数据包的源地址改成61.99.28.1 ---->

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 61.99.28.1


增加10.0.0.0/24网段的静态路由 ----> route add -net 10.0.0.0/24 gw 172.16.93.129


iw list可以查看wlan interface支持的工作模式。



猜你喜欢

转载自blog.csdn.net/hpp205/article/details/79444589