1 Отредактировано Phreaker (21.05.2011 15:34:06)

Тема: FreeBSD + PacketFilter - балансировка нагрузки

Возникла необходимость раскидать пользователей локальной сети на два канала - КТБ и Днепронет. Синтаксис FreeBSD PF немного отличается от OpenBSD PF. Получилось такое:

ext_if1="ng0"
ext_if2="ng1"
gw_1=$ext_if1:peer
gw_2=$ext_if2:peer
int_if="fxp0"
int_net="192.168.1.0/24"

cs_addr="192.168.1.16"          # Сервер CounterStrike у Родиона

table <blacklist> persist
table <mailservers> {178.219.93.206, 178.219.93.18} persist
table <dnepronet> {192.168.1.2 192.168.1.12 192.168.1.11 192.168.1.13 192.168.1.15 192.168.1.16} persist
table <ktb> {192.168.1.10, 192.168.1.17, 192.168.1.18, 192.168.1.20, 192.168.1.50, 192.168.1.51, 192.168.1.52} persist

#############################################################################################
set skip on lo

# Распределение трафика на клиентов
altq on $int_if cbq bandwidth 100Mb queue {q2, q11, q12, q13, q15, q16, q17, q18, q20, q50, q51, q52}
        queue q2  bandwidth 1Mb priority 1 cbq (default borrow) # FILESERVER
        queue q11 bandwidth 2Mb   priority 7                            # Чеченина
        queue q12 bandwidth 2Mb   priority 3 cbq (borrow)               # Мама
        queue q13 bandwidth 6Mb   priority 6                            # Марина
        queue q15 bandwidth 2Mb   priority 7                            # Вершинин
        queue q16 bandwidth 2Mb   priority 7                            # Родеон
        queue q17 bandwidth 8Mb   priority 7                            # Вова
        queue q18 bandwidth 16Mb  priority 7                            # Серый
        queue q20 bandwidth 8Mb   priority 7                            # AK-47
        queue q50 bandwidth 4Mb   priority 7                            # Fenomen
        queue q51 bandwidth 4Mb   priority 7                            # Alex-nikl
        queue q52 bandwidth 4Mb   priority 7                            # Sergej

nat on $ext_if1 from $int_net to !$int_net -> ($ext_if1)
nat on $ext_if2 from $int_net to !$int_net -> ($ext_if2)
# Редирект радмина на FILESERVER и DARKSTAR
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.2   port 4899
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.12  port 4899
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27005 -> $cs_addr      port 27005
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27016 -> $cs_addr      port 27016
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.100 port 80

# Падлюки всякие
block in from any to any
block drop in quick on $ext_if1 from <blacklist> to any
block drop in quick on $ext_if2 from <blacklist> to any
block in on $int_if proto udp from any to any port 25
pass in on $int_if from $int_net to <mailservers>

pass in quick on $int_if from 192.168.0.0/16 to 192.168.1.1
pass out quick on $int_if from 192.168.1.1 to 192.168.0.0/16

# Исходящий трафик от клиентов
pass in quick on $int_if route-to ($ext_if1 $gw_1) from <dnepronet> to any
pass in quick on $int_if route-to ($ext_if2 $gw_2) from <ktb> to any

# Входящий трафик к клиентам
pass out quick on $int_if from any to 192.168.1.2  queue q2
pass out quick on $int_if from any to 192.168.1.10 queue q10
pass out quick on $int_if from any to 192.168.1.11 queue q11
pass out quick on $int_if from any to 192.168.1.12 queue q12
pass out quick on $int_if from any to 192.168.1.13 queue q13
pass out quick on $int_if from any to 192.168.1.14 queue q14
pass out quick on $int_if from any to 192.168.1.15 queue q15
pass out quick on $int_if from any to 192.168.1.16 queue q16
pass out quick on $int_if from any to 192.168.1.17 queue q17
pass out quick on $int_if from any to 192.168.1.18 queue q18
pass out quick on $int_if from any to 192.168.1.19 queue q19
pass out quick on $int_if from any to 192.168.1.20 queue q20
pass out quick on $int_if from any to 192.168.1.50 queue q50
pass out quick on $int_if from any to 192.168.1.51 queue q51
pass out quick on $int_if from any to 192.168.1.52 queue q52

# Наружный трафик

# access point web interface
pass in quick on $ext_if1 proto tcp from any to 192.168.1.100 port 80
pass in quick on $int_if from 192.168.1.100 to any
pass out quick on $int_if from any to 192.168.1.100
# Редирект портов
pass in quick on $ext_if1 proto tcp from any to 192.168.1.2  port 4899
pass in quick on $ext_if1 proto tcp from any to 192.168.1.12 port 4899
pass in quick on $ext_if1 proto udp from any to $cs_addr port 27005:27016

# Dnepronet
pass in quick on $ext_if1 reply-to ($ext_if1 $gw_1) from any to ($ext_if1)
pass out quick route-to ($ext_if1 $gw_1) from ($ext_if1) to any
# Ktb
pass in quick on $ext_if2 reply-to ($ext_if2 $gw_2) from any to ($ext_if2)
pass out quick route-to ($ext_if2 $gw_2) from ($ext_if2) to any
# Чтоб пакеты с айпишником днепронета на падали в канал ктб и наоборот :)
pass out on $ext_if1 route-to ($ext_if2 $gw_2) from ($ext_if2) to any
pass out on $ext_if2 route-to ($ext_if1 $gw_1) from ($ext_if1) to any
Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif

2 Отредактировано Phreaker (21.05.2011 15:34:43)

Re: FreeBSD + PacketFilter - балансировка нагрузки

В предыдущем конфиге клиенты из таблицы <dnepronet> ходят через днепронетовский канал, а <ktb> - через ктбовский.

ext_if1="ng0"
ext_if2="ng1"
gw_1=$ext_if1:peer
gw_2=$ext_if2:peer
int_if="fxp0"
int_net="192.168.1.0/24"

cs_addr="192.168.1.16"          # Сервер CounterStrike у Родиона

table <blacklist> persist
table <mailservers> {178.219.93.206, 178.219.93.18} persist
table <dnepronet> {192.168.1.2 192.168.1.12 192.168.1.11 192.168.1.13 192.168.1.15 192.168.1.16} persist
table <ktb> {192.168.1.10, 192.168.1.17, 192.168.1.18, 192.168.1.20, 192.168.1.50, 192.168.1.51, 192.168.1.52} persist
table <noroute> {212.115.224.0/19, 178.219.80.0/20} persist

#############################################################################################
set skip on lo

# Распределение трафика на клиентов
altq on $int_if cbq bandwidth 100Mb queue {q2, q11, q12, q13, q15, q16, q17, q18, q20, q50, q51, q52}
        queue q2  bandwidth 1Mb priority 1 cbq (default borrow) # FILESERVER
        queue q11 bandwidth 2Mb   priority 7                            # Чеченина
        queue q12 bandwidth 2Mb   priority 3 cbq (borrow)               # Мама
        queue q13 bandwidth 6Mb   priority 6                            # Марина
        queue q15 bandwidth 2Mb   priority 7                            # Вершинин
        queue q16 bandwidth 2Mb   priority 7                            # Родеон
        queue q17 bandwidth 8Mb   priority 7                            # Вова
        queue q18 bandwidth 16Mb  priority 7                            # Серый
        queue q20 bandwidth 8Mb   priority 7                            # AK-47
        queue q50 bandwidth 4Mb   priority 7                            # Fenomen
        queue q51 bandwidth 4Mb   priority 7                            # Alex-nikl
        queue q52 bandwidth 4Mb   priority 7                            # Sergej

nat on $ext_if1 from $int_net to !$int_net -> ($ext_if1)
nat on $ext_if2 from $int_net to !$int_net -> ($ext_if2)
# Редирект радмина на FILESERVER и DARKSTAR
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.2   port 4899
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.12  port 4899
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27005 -> $cs_addr      port 27005
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27016 -> $cs_addr      port 27016
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.100 port 80

# Падлюки всякие
block in from any to any
block drop in quick on $ext_if1 from <blacklist> to any
block drop in quick on $ext_if2 from <blacklist> to any
block in on $int_if proto udp from any to any port 25
pass in on $int_if from $int_net to <mailservers>

pass in quick on $int_if from 192.168.0.0/16 to 192.168.1.1
pass out quick on $int_if from 192.168.1.1 to 192.168.0.0/16

# Исходящий трафик от клиентов
pass in quick on $int_if from $int_net to <noroute>
pass in quick on $int_if route-to ($ext_if1 $gw_1) from <dnepronet> to !<noroute>
pass in quick on $int_if route-to ($ext_if2 $gw_2) from <ktb> to !<noroute>

# Входящий трафик к клиентам
pass out quick on $int_if from any to 192.168.1.2  queue q2
pass out quick on $int_if from any to 192.168.1.10 queue q10
pass out quick on $int_if from any to 192.168.1.11 queue q11
pass out quick on $int_if from any to 192.168.1.12 queue q12
pass out quick on $int_if from any to 192.168.1.13 queue q13
pass out quick on $int_if from any to 192.168.1.14 queue q14
pass out quick on $int_if from any to 192.168.1.15 queue q15
pass out quick on $int_if from any to 192.168.1.16 queue q16
pass out quick on $int_if from any to 192.168.1.17 queue q17
pass out quick on $int_if from any to 192.168.1.18 queue q18
pass out quick on $int_if from any to 192.168.1.19 queue q19
pass out quick on $int_if from any to 192.168.1.20 queue q20
pass out quick on $int_if from any to 192.168.1.50 queue q50
pass out quick on $int_if from any to 192.168.1.51 queue q51
pass out quick on $int_if from any to 192.168.1.52 queue q52

# Наружный трафик

# access point web interface
pass in quick on $ext_if1 proto tcp from any to 192.168.1.100 port 80
pass in quick on $int_if from 192.168.1.100 to any
pass out quick on $int_if from any to 192.168.1.100
# Редирект портов
pass in quick on $ext_if1 proto tcp from any to 192.168.1.2  port 4899
pass in quick on $ext_if1 proto tcp from any to 192.168.1.12 port 4899
pass in quick on $ext_if1 proto udp from any to $cs_addr port 27005:27016

# Dnepronet
pass in quick on $ext_if1 reply-to ($ext_if1 $gw_1) from any to ($ext_if1)
pass out quick route-to ($ext_if1 $gw_1) from ($ext_if1) to any
# Ktb
pass in quick on $ext_if2 reply-to ($ext_if2 $gw_2) from any to ($ext_if2)
pass out quick route-to ($ext_if2 $gw_2) from ($ext_if2) to any
# Чтоб пакеты с айпишником днепронета на падали в канал ктб и наоборот :)
pass out on $ext_if1 route-to ($ext_if2 $gw_2) from ($ext_if2) to any
pass out on $ext_if2 route-to ($ext_if1 $gw_1) from ($ext_if1) to any

а в конфиг mpd дописываем маршруты на сетки днепронета и ктб

...cutted...
ktb:
        create bundle static B4
        set iface route 212.115.224.0/19
        set ipcp ranges 0.0.0.0/0 0.0.0.0/0
        create link static L4 pppoe
        set link action bundle B4
        set auth authname XXXXXXXXX
        set auth password XXXXXXXXX
        set link max-redial 0
        set link mtu 1460
        set link keep-alive 10 60
        set pppoe iface rl1
        set pppoe service ""
        open

dnepronet_home:
        create bundle static B1
        set iface route default
        set iface route 178.219.80.0/20
        set ipcp ranges 0.0.0.0/0 0.0.0.0/0

        create link static L1 pppoe
        set link action bundle B1
        set auth authname XXXXXXXXX
        set auth password XXXXXXXXX
        set link max-redial 0
        set link mtu 1460
        set link keep-alive 10 60
        set pppoe iface rl0
        set pppoe service ""
        open
...cutted...

Итак, клиенты ходят своими маршрутами, а к ресурсам внутри сети провайдеров обращаются по кратчайшему пути

Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif

3 Отредактировано Phreaker (21.05.2011 15:35:06)

Re: FreeBSD + PacketFilter - балансировка нагрузки

а вот так заводим VPN с другой локалкой. локалка - 192.168.2.0/24, айпи туннеля 192.168.250.1 и 192.168.250.2 соответсвенно.

ext_if1="ng0"
ext_if2="ng1"
gw_1=$ext_if1:peer
gw_2=$ext_if2:peer
int_if="fxp0"
int_net="192.168.1.0/24"

cs_addr="192.168.1.16"          # Сервер CounterStrike у Родиона

table <blacklist> persist
table <mailservers> {178.219.93.206, 178.219.93.18} persist
table <dnepronet> {192.168.1.2 192.168.1.12 192.168.1.11 192.168.1.13 192.168.1.15 192.168.1.16} persist
table <ktb> {192.168.1.10, 192.168.1.17, 192.168.1.18, 192.168.1.20, 192.168.1.50, 192.168.1.51, 192.168.1.52} persist
table <noroute> {192.168.2.0/24, 192.168.250.0/24, 212.115.224.0/19, 178.219.80.0/20} persist
table <nonat> {192.168.1.0/24 192.168.2.0/24 192.168.250.0/24}
#############################################################################################
set skip on lo

# Распределение трафика на клиентов
altq on $int_if cbq bandwidth 100Mb queue {q2, q11, q12, q13, q15, q16, q17, q18, q20, q50, q51, q52}
        queue q2  bandwidth 1Mb priority 1 cbq (default borrow) # FILESERVER
        queue q11 bandwidth 2Mb   priority 7                            # Чеченина
        queue q12 bandwidth 2Mb   priority 3 cbq (borrow)               # Мама
        queue q13 bandwidth 6Mb   priority 6                            # Марина
        queue q15 bandwidth 2Mb   priority 7                            # Вершинин
        queue q16 bandwidth 2Mb   priority 7                            # Родеон
        queue q17 bandwidth 8Mb   priority 7                            # Вова
        queue q18 bandwidth 16Mb  priority 7                            # Серый
        queue q20 bandwidth 8Mb   priority 7                            # AK-47
        queue q50 bandwidth 4Mb   priority 7                            # Fenomen
        queue q51 bandwidth 4Mb   priority 7                            # Alex-nikl
        queue q52 bandwidth 4Mb   priority 7                            # Sergej

nat on $ext_if1 from $int_net to !<nonat> -> ($ext_if1)
nat on $ext_if2 from $int_net to !<nonat> -> ($ext_if2)
# Редирект радмина на FILESERVER и DARKSTAR
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.2   port 4899
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.12  port 4899
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27005 -> $cs_addr      port 27005
rdr on $ext_if1 proto udp from any to ($ext_if1) port 27016 -> $cs_addr      port 27016
rdr on $ext_if1 proto tcp from any to ($ext_if1) port XXXX  -> 192.168.1.100 port 80

# Падлюки всякие
block in from any to any
block drop in quick on $ext_if1 from <blacklist> to any
block drop in quick on $ext_if2 from <blacklist> to any
block in on $int_if proto udp from any to any port 25
pass in on $int_if from $int_net to <mailservers>

pass in quick on $int_if from 192.168.0.0/16 to 192.168.1.1
pass out quick on $int_if from 192.168.1.1 to 192.168.0.0/16

# VPN
pass in on ng2 from any to 192.168.0.0/16
pass out on ng2 from 192.168.0.0/16 to any

# Исходящий трафик от клиентов
pass in quick on $int_if from $int_net to <noroute>
pass in quick on $int_if route-to ($ext_if1 $gw_1) from <dnepronet> to !<noroute>
pass in quick on $int_if route-to ($ext_if2 $gw_2) from <ktb> to !<noroute>

# Входящий трафик к клиентам
pass out quick on $int_if from any to 192.168.1.2  queue q2
pass out quick on $int_if from any to 192.168.1.10 queue q10
pass out quick on $int_if from any to 192.168.1.11 queue q11
pass out quick on $int_if from any to 192.168.1.12 queue q12
pass out quick on $int_if from any to 192.168.1.13 queue q13
pass out quick on $int_if from any to 192.168.1.14 queue q14
pass out quick on $int_if from any to 192.168.1.15 queue q15
pass out quick on $int_if from any to 192.168.1.16 queue q16
pass out quick on $int_if from any to 192.168.1.17 queue q17
pass out quick on $int_if from any to 192.168.1.18 queue q18
pass out quick on $int_if from any to 192.168.1.19 queue q19
pass out quick on $int_if from any to 192.168.1.20 queue q20
pass out quick on $int_if from any to 192.168.1.50 queue q50
pass out quick on $int_if from any to 192.168.1.51 queue q51
pass out quick on $int_if from any to 192.168.1.52 queue q52

# Наружный трафик

# access point web interface
pass in quick on $ext_if1 proto tcp from any to 192.168.1.100 port 80
pass in quick on $int_if from 192.168.1.100 to any
pass out quick on $int_if from any to 192.168.1.100
# Редирект портов
pass in quick on $ext_if1 proto tcp from any to 192.168.1.2  port 4899
pass in quick on $ext_if1 proto tcp from any to 192.168.1.12 port 4899
pass in quick on $ext_if1 proto udp from any to $cs_addr port 27005:27016

# Dnepronet
pass in quick on $ext_if1 reply-to ($ext_if1 $gw_1) from any to ($ext_if1)
pass out quick route-to ($ext_if1 $gw_1) from ($ext_if1) to any
# Ktb
pass in quick on $ext_if2 reply-to ($ext_if2 $gw_2) from any to ($ext_if2)
pass out quick route-to ($ext_if2 $gw_2) from ($ext_if2) to any

pass out on $ext_if1 route-to ($ext_if2 $gw_2) from ($ext_if2) to any
pass out on $ext_if2 route-to ($ext_if1 $gw_1) from ($ext_if1) to any
Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif

4

Re: FreeBSD + PacketFilter - балансировка нагрузки

http://argo-uln.blogspot.com/2008/02/pf … -62-3.html
полезная ссылка

Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif

5 Отредактировано Phreaker (27.03.2012 23:17:31)

Re: FreeBSD + PacketFilter - балансировка нагрузки

То же самое, используя IPFW NAT и множественные таблицы маршрутизации (появилось в FreeBSD7)

#!/bin/sh
/sbin/ipfw -q -f flush all
/sbin/ipfw -q -f flush pipe

int_if="em0"
ext_if1="ng0"
ext_if2="ng1"

/sbin/ipfw table 1 add 172.18.1.0/24    # Servers
/sbin/ipfw table 1 add 172.16.2.0/24    # подсеть
/sbin/ipfw table 1 add 172.16.4.0/24    # еще одна подсеть
/sbin/ipfw table 2 add 172.16.3.0/24    # тоже подсеть
/sbin/ipfw table 2 add 172.16.1.0/24    # (дофига подсетей)
/sbin/ipfw table 9 add 172.17.1.0/24    # PPTP
/sbin/ipfw table 9 add 212.115.224.0/19 # KTB
/sbin/ipfw table 9 add 46.98.0.0/16     # KTB
/sbin/ipfw table 9 add 178.219.80.0/20  # Dnepronet
/sbin/ipfw table 10 add 192.168.0.0/16  # Падлюки
/sbin/ipfw table 10 add 169.254.0.0/16  # Падлюки
/sbin/ipfw table 10 add 10.0.0.0/8      # Падлюки

/sbin/ipfw add 00050 allow ip from any to any via lo0
/sbin/ipfw add 00055 deny all from any to 127.0.0.0/8
/sbin/ipfw add 00060 deny all from 127.0.0.0/8 to any

/sbin/ipfw add 00100 deny ip from table\(10\) to any via $int_if

/sbin/ipfw add 1040 setfib 0 ip from table\(1\) to not table\(9\) in recv $int_if
/sbin/ipfw add 1050 setfib 1 ip from table\(2\) to not table\(9\) in recv $int_if

/sbin/ipfw add 01200 add allow ip from any to any via $int_if

/sbin/ipfw add 01300 allow gre from any to any via $ext_if1
/sbin/ipfw add 01310 allow tcp from any to me 1723 in recv $ext_if1
/sbin/ipfw add 01320 allow tcp from me 1723 to any out xmit $ext_if1
/sbin/ipfw add 01330 allow tcp from any to me 22
/sbin/ipfw add 01340 allow tcp from me 22 to any
/sbin/ipfw add 01350 allow icmp from any to me icmptypes 8
/sbin/ipfw add 01360 allow icmp from me to any icmptypes 0

/sbin/ipfw nat 1 config log if $ext_if1 same_ports reset deny_in redirect_port tcp 172.18.1.3:80 80 redirect_port tcp 172.18.1.2:25 25 redirect_port tcp 172.18.1.2:110 110 redirect_port tcp 172.18.1.2:465 465
/sbin/ipfw nat 2 config log if $ext_if2 same_ports reset deny_in

/sbin/ipfw add 02000 nat 1 ip from any to any via $ext_if1
/sbin/ipfw add 03000 nat 2 ip from any to any via $ext_if2

скрипт писался "на горячую", допилить до совершенства не было возможности...

в ведре пишем такое

options         IPFIREWALL
options         IPFIREWALL_VERBOSE
options         IPFIREWALL_VERBOSE_LIMIT=100
options         IPFIREWALL_DEFAULT_TO_ACCEPT
options         IPFIREWALL_NAT
options         LIBALIAS
#options        IPFIREWALL_FORWARD
options         DUMMYNET
#options        IPDIVERT

options         ROUTETABLES=4

а в /boot/loader.conf (нужное раскомментировать)

alias_ftp_load="YES"
#alias_irc_load="YES"
#alias_pptp_load="YES"
Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif

6

Re: FreeBSD + PacketFilter - балансировка нагрузки

Кошерные ссылки
http://www.ndd.ru/al/ipfw/prim.html
http://www.lissyara.su/articles/freebsd … at_options

Мир, труд, май - модем, линукс, пиво
OS: FreeBSD 6.4 --> FreeBSD 7.4 --> FreeBSD 8.4
http://yoip.com.ua/_res/userbar/bl-bar.gif