<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Записки на полях &#187; shaping</title>
	<atom:link href="http://snake.khd.ru/tag/shaping/feed/" rel="self" type="application/rss+xml" />
	<link>http://snake.khd.ru</link>
	<description>Логи начинающего сисадмина</description>
	<lastBuildDate>Sun, 25 Jul 2010 00:55:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Каждому по потребностям &#8211; шейпинг трафика при помощи IPFW</title>
		<link>http://snake.khd.ru/2009/10/kazhdomu-po-potrebnostyam-shejping-trafika-pri-pomoshhi-ipfw/</link>
		<comments>http://snake.khd.ru/2009/10/kazhdomu-po-potrebnostyam-shejping-trafika-pri-pomoshhi-ipfw/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 13:37:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[ipfw]]></category>
		<category><![CDATA[nat]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[shaping]]></category>

		<guid isPermaLink="false">http://snake.khd.ru/?p=118</guid>
		<description><![CDATA[Исходные данные: FreeBSD 7.2, IPFW, MPD4.3. две ADSL линии.
Задача &#8211; раздавать интернет пользователям &#8211; у каждой группы пользователей свой канал.

Итак, настраиваем mpd. В /usr/local/etc/mpd4 нас интересуют два файла:

snake@snake &#91;mpd4&#93;#ls -al
total 118
drwxr-xr-x   2 root   wheel    512 16 окт 22:57 .
drwxr-xr-x  24 root   wheel   1536 [...]]]></description>
			<content:encoded><![CDATA[<p>Исходные данные: FreeBSD 7.2, IPFW, MPD4.3. две ADSL линии.<br />
Задача &#8211; раздавать интернет пользователям &#8211; у каждой группы пользователей свой канал.<br />
<span id="more-118"></span><br />
Итак, настраиваем mpd. В /usr/local/etc/mpd4 нас интересуют два файла:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#ls -al</span>
total <span style="color: #000000;">118</span>
drwxr-xr-x   <span style="color: #000000;">2</span> root   wheel    <span style="color: #000000;">512</span> <span style="color: #000000;">16</span> окт <span style="color: #000000;">22</span>:<span style="color: #000000;">57</span> .
drwxr-xr-x  <span style="color: #000000;">24</span> root   wheel   <span style="color: #000000;">1536</span> <span style="color: #000000;">18</span> окт <span style="color: #000000;">20</span>:<span style="color: #000000;">53</span> ..
<span style="color: #660033;">-rw-r--r--</span>   <span style="color: #000000;">1</span> snake  snake   <span style="color: #000000;">2799</span> <span style="color: #000000;">19</span> сен 00:<span style="color: #000000;">33</span> mpd.conf
<span style="color: #660033;">-rw-r--r--</span>   <span style="color: #000000;">1</span> snake  snake    <span style="color: #000000;">638</span> <span style="color: #000000;">17</span> сен <span style="color: #000000;">23</span>:<span style="color: #000000;">14</span> mpd.links</pre></div></div>

<p>Содержимое mpd.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">startup:
        <span style="color: #666666; font-style: italic;"># configure the console</span>
        <span style="color: #000000; font-weight: bold;">set</span> console port <span style="color: #000000;">5005</span>
        <span style="color: #000000; font-weight: bold;">set</span> console ip 127.0.0.1
        <span style="color: #000000; font-weight: bold;">set</span> console user user <span style="color: #7a0874; font-weight: bold;">test</span>
        <span style="color: #000000; font-weight: bold;">set</span> console open
&nbsp;
default:
        load vtk_unlim
        load vtk_unlim2
&nbsp;
vtk_unlim:
        new <span style="color: #660033;">-i</span> ng0 pppoe_vtk pppoe_vtk <span style="color: #666666; font-style: italic;">#имя соединения в mpd.links</span>
        <span style="color: #000000; font-weight: bold;">set</span> iface route default <span style="color: #666666; font-style: italic;">#установить соединение маршрутом по умолчанию</span>
        <span style="color: #000000; font-weight: bold;">set</span> iface disable on-demand
        <span style="color: #000000; font-weight: bold;">set</span> iface idle <span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">set</span> ipcp <span style="color: #c20cb9; font-weight: bold;">yes</span> vjcomp
        <span style="color: #000000; font-weight: bold;">set</span> ipcp ranges 0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span> 0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">set</span> bundle disable multilink
        <span style="color: #000000; font-weight: bold;">set</span> auth authname mylogin
        <span style="color: #000000; font-weight: bold;">set</span> auth password MyPaSsWoRd
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> no acfcomp protocomp
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> disable pap chap<span style="color: #666666; font-style: italic;">#настройки шифрования</span>
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> accept chap
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> mtu <span style="color: #000000;">1400</span> <span style="color: #666666; font-style: italic;">#настройка MTU</span>
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> keep-alive <span style="color: #000000;">10</span> <span style="color: #000000;">60</span>
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> max redial <span style="color: #000000;">0</span> <span style="color: #666666; font-style: italic;">#число попыток реконнекта</span>
        <span style="color: #000000; font-weight: bold;">set</span> bundle disable noretry <span style="color: #666666; font-style: italic;">#перезванивать при обрыве</span>
        <span style="color: #000000; font-weight: bold;">set</span> iface up-script <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mpd4<span style="color: #000000; font-weight: bold;">/</span>up <span style="color: #666666; font-style: italic;">#скрипт, выполняющийся при установке соединения</span>
        open
&nbsp;
vtk_unlim2:
        new <span style="color: #660033;">-i</span> ng1 pppoe_vtk2 pppoe_vtk2
        <span style="color: #000000; font-weight: bold;">set</span> iface route default
        <span style="color: #000000; font-weight: bold;">set</span> iface disable on-demand
        <span style="color: #000000; font-weight: bold;">set</span> iface idle <span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">set</span> ipcp <span style="color: #c20cb9; font-weight: bold;">yes</span> vjcomp
        <span style="color: #000000; font-weight: bold;">set</span> ipcp ranges 0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span> 0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">set</span> bundle disable multilink
        <span style="color: #000000; font-weight: bold;">set</span> auth authname mylogin2
        <span style="color: #000000; font-weight: bold;">set</span> auth password mYpAsSwOrD
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> no acfcomp protocomp
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> disable pap chap
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> accept chap
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> mtu <span style="color: #000000;">1400</span>
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> keep-alive <span style="color: #000000;">10</span> <span style="color: #000000;">60</span>
        <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> max redial <span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">set</span> bundle disable noretry
        <span style="color: #000000; font-weight: bold;">set</span> iface up-script <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mpd4<span style="color: #000000; font-weight: bold;">/</span>up2
        open</pre></div></div>

<p>Содержимое скриптов запуска:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">out_if</span>=<span style="color: #ff0000;">&quot;-interface ng0&quot;</span>
route delete default
route add default <span style="color: #007800;">$out_if</span></pre></div></div>

<p>Скрипт назначает наш pppoe канал маршрутом по умолчанию как для самого сервера, так и для клиентов</p>
<p>Скрипт up2:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">out_if</span>=<span style="color: #ff0000;">&quot;-interface ng1&quot;</span>
setfib <span style="color: #000000;">1</span> route add <span style="color: #660033;">-net</span> default <span style="color: #007800;">$out_if</span>
<span style="color: #007800;">gw</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> ng1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;inet&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">host</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> ng1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;inet&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">fw</span>=<span style="color: #ff0000;">&quot;/sbin/ipfw -q&quot;</span>
<span style="color: #007800;">users2</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.fire.new <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;users2=&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;=&quot;</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">num</span>=<span style="color: #000000; font-weight: bold;">`</span>ipfw show <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;fwd&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.d<span style="color: #000000; font-weight: bold;">/</span>natd.sh
<span style="color: #007800;">$fw</span> delete <span style="color: #007800;">$num</span>
<span style="color: #007800;">$fw</span> add <span style="color: #007800;">$num</span> fwd <span style="color: #007800;">$gw</span> ip from <span style="color: #007800;">$host</span> to any</pre></div></div>

<p>Суть этого скрипта в том, что он выдирает из вывода ifconfig текущие шлюз и ip (т.к. на втором соединении ип динамический) и добавляет эти значения в правило ipfw, обеспечивающее прохождение трафика. Подробней про эти правила будет ниже.</p>
<p>Файл mpd.links:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pppoe_vtk:
    <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> <span style="color: #7a0874; font-weight: bold;">type</span> pppoe <span style="color: #666666; font-style: italic;"># Тип соединения PPPoE</span>
    <span style="color: #000000; font-weight: bold;">set</span> pppoe iface em1 <span style="color: #666666; font-style: italic;">#Интерфейс, на котором создается pppoe соединение</span>
    <span style="color: #000000; font-weight: bold;">set</span> pppoe service <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000000; font-weight: bold;">set</span> pppoe disable incoming <span style="color: #666666; font-style: italic;">#запрещаем входящие соединения</span>
    <span style="color: #000000; font-weight: bold;">set</span> pppoe <span style="color: #7a0874; font-weight: bold;">enable</span> originate
&nbsp;
pppoe_vtk2:
    <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #c20cb9; font-weight: bold;">link</span> <span style="color: #7a0874; font-weight: bold;">type</span> pppoe
    <span style="color: #000000; font-weight: bold;">set</span> pppoe iface fxp0
    <span style="color: #000000; font-weight: bold;">set</span> pppoe service <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000000; font-weight: bold;">set</span> pppoe disable incoming
    <span style="color: #000000; font-weight: bold;">set</span> pppoe <span style="color: #7a0874; font-weight: bold;">enable</span> originate</pre></div></div>

<p>mpd настроен. При запуске он должен автоматически поднять соединения и мы увидим нечто вроде этого:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ng0: <span style="color: #007800;">flags</span>=88d1<span style="color: #000000; font-weight: bold;">&lt;</span>UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST<span style="color: #000000; font-weight: bold;">&gt;</span> metric <span style="color: #000000;">0</span> mtu <span style="color: #000000;">1400</span>
        inet 85.15.66.66 --<span style="color: #000000; font-weight: bold;">&gt;</span> 85.15.64.119 netmask 0xffffffff
ng1: <span style="color: #007800;">flags</span>=88d1<span style="color: #000000; font-weight: bold;">&lt;</span>UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST<span style="color: #000000; font-weight: bold;">&gt;</span> metric <span style="color: #000000;">0</span> mtu <span style="color: #000000;">1400</span>
        inet 85.15.81.133 --<span style="color: #000000; font-weight: bold;">&gt;</span> 85.15.80.1 netmask 0xffffffff</pre></div></div>

<p>Не забудьте прописать в /etc/syslog.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">!</span>mpd
<span style="color: #000000; font-weight: bold;">*</span>.<span style="color: #000000; font-weight: bold;">*</span>                                             <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>mpd.log</pre></div></div>

<p>Теперь займемся настройкой нат. Я использовал natd. Про свежевыпущенный ipfw nat скажу ниже</p>
<p>Прописываем в /etc/rc.conf следующее:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#cat /etc/rc.conf | grep natd</span>
<span style="color: #007800;">natd_enable</span>=<span style="color: #ff0000;">&quot;YES&quot;</span></pre></div></div>

<p>далее создаем файлы конфигурации (в моем случае /etc/natd2.conf, /etc/natd3.conf):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#cat /etc/natd2.conf</span>
<span style="color: #666666; font-style: italic;"># порт, на котором висит natd</span>
port <span style="color: #000000;">8448</span>
<span style="color: #666666; font-style: italic;"># интерфейс</span>
interface ng0
<span style="color: #666666; font-style: italic;"># стараться не изменять порты</span>
same_ports <span style="color: #c20cb9; font-weight: bold;">yes</span>
<span style="color: #666666; font-style: italic;"># перенаправлять только трафик с адресом источника 10.0.0.0/8, 172.16.0.0/12 и 192.168.0.0/16.</span>
unregistered_only <span style="color: #c20cb9; font-weight: bold;">yes</span>
dynamic <span style="color: #c20cb9; font-weight: bold;">yes</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># проброс портов для отдельных машин наружу</span>
redirect_port tcp 10.12.51.24:<span style="color: #000000;">51024</span> <span style="color: #000000;">51024</span>
redirect_port udp 10.12.51.24:<span style="color: #000000;">51024</span> <span style="color: #000000;">51024</span>
redirect_port tcp 10.12.51.24:<span style="color: #000000;">51025</span> <span style="color: #000000;">51025</span>
redirect_port udp 10.12.51.24:<span style="color: #000000;">51025</span> <span style="color: #000000;">51025</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#cat /etc/natd3.conf</span>
port <span style="color: #000000;">8558</span>
interface ng1
same_ports <span style="color: #c20cb9; font-weight: bold;">yes</span>
unregistered_only <span style="color: #c20cb9; font-weight: bold;">yes</span>
dynamic <span style="color: #c20cb9; font-weight: bold;">yes</span>
redirect_port tcp 10.12.51.118:<span style="color: #000000;">31027</span> <span style="color: #000000;">31027</span>
redirect_port udp 10.12.51.118:<span style="color: #000000;">31027</span> <span style="color: #000000;">31027</span></pre></div></div>

<p>создаем скрипт в /usr/local/etc/rc.d</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#cat /usr/local/etc/rc.d/natd.sh</span>
<span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>natd <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>natd2.conf
<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>natd <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>natd3.conf</pre></div></div>

<p>Запускаем, смотрим: в sockstat должно быть что-то такое:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">snake<span style="color: #000000; font-weight: bold;">@</span>snake <span style="color: #7a0874; font-weight: bold;">&#91;</span>mpd4<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#sockstat -l4 | grep natd</span>
root     natd       <span style="color: #000000;">1033</span>  <span style="color: #000000;">4</span>  div4   <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000;">8558</span>                <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000; font-weight: bold;">*</span>
root     natd       <span style="color: #000000;">1031</span>  <span style="color: #000000;">4</span>  div4   <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000;">8448</span>                <span style="color: #000000; font-weight: bold;">*</span>:<span style="color: #000000; font-weight: bold;">*</span></pre></div></div>

<p>Теперь приступим к самой ответственной части &#8211; а именно настройке фаервола :)<br />
Тут я приведу конфиг полностью, с пояснениями и комментариями<br />
Про ядерный nat ipfw можно почтитать <a href='http://www.lissyara.su/?id=1967' target='_blank'>тут</a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#Создаем каналы-пайпы. Труба одна для каждой группы пользователей</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">100</span> config bw <span style="color: #007800;">$downi</span> queue <span style="color: #000000;">50</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">200</span> config bw <span style="color: #007800;">$upi</span>   queue <span style="color: #000000;">50</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">300</span> config bw <span style="color: #007800;">$downi</span> queue <span style="color: #000000;">50</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">400</span> config bw <span style="color: #007800;">$up2</span> queue <span style="color: #000000;">50</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">500</span> config bw <span style="color: #007800;">$downvtk</span> queue <span style="color: #000000;">50</span>
<span style="color: #007800;">$fw</span> pipe <span style="color: #000000;">600</span> config bw <span style="color: #007800;">$upvtk</span> queue <span style="color: #000000;">50</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Очереди трафика (подробнее смотрим man ipfw) </span>
<span style="color: #666666; font-style: italic;">#именно они обеспечивают справедливое распределение канала между пользователями</span>
<span style="color: #666666; font-style: italic;">#inet --&gt; LAN</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">101</span> config weight <span style="color: #007800;">$tcp2w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">100</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask dst-ip 0xffffffff
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">102</span> config weight <span style="color: #007800;">$tcp1w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">100</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask dst-ip 0xffffffff
&nbsp;
<span style="color: #666666; font-style: italic;">#LAN --&gt; inet</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">201</span> config weight <span style="color: #007800;">$tcp2w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">200</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask src-ip 0xffffffff
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">202</span> config weight <span style="color: #007800;">$tcp1w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">200</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask src-ip 0xffffffff
&nbsp;
<span style="color: #666666; font-style: italic;">#inet --&gt; LAN</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">301</span> config weight <span style="color: #007800;">$tcp1w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">300</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask dst-ip 0xffffffff
&nbsp;
<span style="color: #666666; font-style: italic;">#LAN --&gt; inet</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">401</span> config weight <span style="color: #007800;">$tcp1w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">400</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask src-ip 0xffffffff
&nbsp;
<span style="color: #666666; font-style: italic;">#VTK --&gt; LAN</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">501</span> config weight <span style="color: #007800;">$tcp2w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">500</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask dst-ip 0xffffffff
&nbsp;
<span style="color: #666666; font-style: italic;">#LAN --&gt; VTK</span>
<span style="color: #007800;">$fw</span> queue <span style="color: #000000;">601</span> config weight <span style="color: #007800;">$tcp2w</span> queue <span style="color: #000000;">50</span> pipe <span style="color: #000000;">600</span> gred <span style="color: #000000;">0.002</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.1</span> mask src-ip 0xffffffff
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">#Настройки ядерного NAT. У меня нормально так и не заработал, но может у вас получится ;) </span>
<span style="color: #007800;">$fw</span> nat <span style="color: #000000;">300</span> config log <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">$wan</span> same_ports deny_in
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">#Selfcare &amp; service</span>
<span style="color: #666666; font-style: italic;">##loopback</span>
<span style="color: #666666; font-style: italic;"># разрешаем ходить локалхост трафику, но только в пределах интерфейса loopback</span>
<span style="color: #007800;">$fwa</span> 01000 allow ip from any to any via lo0
<span style="color: #007800;">$fwa</span> 01010 deny ip from any to 127.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span>
<span style="color: #007800;">$fwa</span> 01020 deny ip from 127.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> to any
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">##Deny networks</span>
<span style="color: #666666; font-style: italic;"># Запрещаем частные подсети на внешних интерфейсах</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1200</span> deny log ip from 10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1210</span> deny log ip from any to 10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1220</span> deny log ip from 172.16.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">12</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1230</span> deny log ip from any to 172.16.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">12</span> out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1240</span> deny log ip from 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1250</span> deny log ip from any to 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> out via <span style="color: #007800;">$inet</span>
&nbsp;
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1205</span> deny log ip from 10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1215</span> deny log ip from any to 10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> out via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1225</span> deny log ip from 172.16.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">12</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1235</span> deny log ip from any to 172.16.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">12</span> out via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1245</span> deny log ip from 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">1255</span> deny log ip from any to 192.168.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> out via <span style="color: #007800;">$inet2</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">#Allowing connetctions</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">##Intranet connections</span>
<span style="color: #666666; font-style: italic;"># Разрешаем соединение с сервером из локалки</span>
<span style="color: #007800;">$fwa</span> 02100 allow ip from <span style="color: #007800;">$hostel</span> to <span style="color: #007800;">$iplan</span> <span style="color: #007800;">$usr_ports</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02110 allow ip from me to <span style="color: #007800;">$hostel</span> out via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02115 allow ip from me to <span style="color: #007800;">$hostel</span> out via <span style="color: #007800;">$vlan</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">##DNS (разрешаем связь с DNS серверами прова </span>
<span style="color: #666666; font-style: italic;">#и доступ к DNS-серверу на машине из локальной сети</span>
<span style="color: #007800;">$fwa</span> 02310 allow udp from me to <span style="color: #007800;">$vtk_dns</span> out
<span style="color: #007800;">$fwa</span> 02320 allow udp from <span style="color: #007800;">$vtk_dns</span> to me <span style="color: #000000; font-weight: bold;">in</span>
<span style="color: #007800;">$fwa</span> 02330 allow udp from <span style="color: #007800;">$hostel</span> to <span style="color: #007800;">$iplan</span> <span style="color: #000000;">53</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02340 allow udp from <span style="color: #007800;">$iplan</span> <span style="color: #000000;">53</span> to <span style="color: #007800;">$hostel</span> out via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02350 deny udp from <span style="color: #007800;">$hostel</span> to <span style="color: #007800;">$vtk_dns</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02360 deny udp from <span style="color: #007800;">$hostel</span> to <span style="color: #007800;">$vtk_dns</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$vlan</span>
<span style="color: #007800;">$fwa</span> 02370 deny udp from <span style="color: #007800;">$vtk_dns</span> to <span style="color: #007800;">$hostel</span> out via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 02380 deny udp from <span style="color: #007800;">$vtk_dns</span> to <span style="color: #007800;">$hostel</span> out via <span style="color: #007800;">$vlan</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;"># Ради чего все затевалось - разрешаем пользователям из локалки доступ вовне</span>
<span style="color: #007800;">$fwa</span> 03141 allow ip from <span style="color: #ff0000;">&quot;table(1)&quot;</span>  to not me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 03142 allow ip from <span style="color: #ff0000;">&quot;table(1)&quot;</span> to not me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$vlan</span>
<span style="color: #007800;">$fwa</span> 03143 allow ip from not me to <span style="color: #ff0000;">&quot;table(1)&quot;</span> out via <span style="color: #007800;">$lan</span>
<span style="color: #007800;">$fwa</span> 03144 allow ip from not me to <span style="color: #ff0000;">&quot;table(1)&quot;</span> out via <span style="color: #007800;">$vlan</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#NAT &amp; queues</span>
<span style="color: #666666; font-style: italic;">##Outcoming queues (исходящие очереди)</span>
<span style="color: #666666; font-style: italic;">#Настройка очередей. Трафик сервера идет отдельной очередью</span>
<span style="color: #007800;">$fwa</span> 05010 queue <span style="color: #000000;">201</span> ip from me to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 05020 queue <span style="color: #000000;">202</span> ip from <span style="color: #007800;">$users</span> to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 05030 queue <span style="color: #000000;">401</span> ip from <span style="color: #007800;">$users2</span> to any out via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> 05040 queue <span style="color: #000000;">601</span> ip from any to <span style="color: #007800;">$vtk</span> out via <span style="color: #007800;">$wan</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">##NAT</span>
<span style="color: #666666; font-style: italic;">#Заворачиваем трафик от разных групп на разные natd</span>
<span style="color: #007800;">$fwa</span> 07200 divert <span style="color: #000000;">8448</span> ip from <span style="color: #007800;">$users</span> to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 07250 divert <span style="color: #000000;">8448</span> ip from any to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 07100 divert <span style="color: #000000;">8558</span> ip from <span style="color: #007800;">$users2</span> to any out via <span style="color: #007800;">$inet2</span>
<span style="color: #666666; font-style: italic;">#Говорим фаерволу, что трафик второй группы </span>
<span style="color: #666666; font-style: italic;">#нужно пускать через другой шлюз. </span>
<span style="color: #666666; font-style: italic;">#Именно это правило мы меняем, когда mpd делает реконнект</span>
<span style="color: #666666; font-style: italic;">#Переменные $gw и $host определяются при каждом перезапуске</span>
<span style="color: #666666; font-style: italic;">#скрипта с правилами</span>
<span style="color: #007800;">$fwa</span> 07130 fwd <span style="color: #007800;">$gw</span> ip from <span style="color: #007800;">$host</span> to any
<span style="color: #007800;">$fwa</span> 07150 divert <span style="color: #000000;">8558</span> ip from any to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #666666; font-style: italic;"># тот самый ядерный nat. оставил для примера :)</span>
<span style="color: #007800;">$fwa</span> 07400 nat <span style="color: #000000;">300</span> ip from any to any via <span style="color: #007800;">$wan</span>
<span style="color: #666666; font-style: italic;">#----------------------------------</span>
<span style="color: #666666; font-style: italic;">##Incoming queues (входящие очереди)</span>
<span style="color: #007800;">$fwa</span> 08010 queue <span style="color: #000000;">101</span> ip from any to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 08020 queue <span style="color: #000000;">102</span> ip from any to <span style="color: #007800;">$users</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 08030 queue <span style="color: #000000;">301</span> ip from any to <span style="color: #007800;">$users2</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> 08040 queue <span style="color: #000000;">501</span> ip from <span style="color: #007800;">$vtk</span> to any <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$wan</span>
<span style="color: #666666; font-style: italic;">#-----------------------------------</span>
<span style="color: #666666; font-style: italic;">#Allowing connections</span>
<span style="color: #666666; font-style: italic;">##Outcoming allowers (разрешающие правила для соединений)</span>
<span style="color: #007800;">$fwa</span> 09000 allow ip from <span style="color: #007800;">$users</span> to <span style="color: #007800;">$vtk</span> out via <span style="color: #007800;">$wan</span>
<span style="color: #007800;">$fwa</span> 09010 allow ip from me to <span style="color: #007800;">$vtk</span> out via <span style="color: #007800;">$wan</span>
<span style="color: #007800;">$fwa</span> 09023 allow ip from me <span style="color: #000000;">12553</span> to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09034 allow ip from me <span style="color: #000000;">12554</span> to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09040 allow ip from <span style="color: #007800;">$users</span> to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09050 allow ip from <span style="color: #007800;">$users2</span> to any out via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> 09060 allow ip from me to any out via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09070 allow ip from me to any out via <span style="color: #007800;">$inet2</span>
<span style="color: #666666; font-style: italic;">#----------------------------------</span>
<span style="color: #666666; font-style: italic;">##Incoming allowers</span>
<span style="color: #007800;">$fwa</span> 09100 allow ip from <span style="color: #007800;">$vtk</span> to <span style="color: #007800;">$users</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$wan</span>
<span style="color: #007800;">$fwa</span> 09110 allow ip from <span style="color: #007800;">$vtk</span> to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$wan</span>
<span style="color: #007800;">$fwa</span> 09120 allow ip from any to <span style="color: #007800;">$users</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09130 allow ip from any to <span style="color: #007800;">$users2</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet2</span>
<span style="color: #007800;">$fwa</span> 09143 allow ip from any to me <span style="color: #000000;">12553</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09154 allow ip from any to me <span style="color: #000000;">12554</span> <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #007800;">$fwa</span> 09160 allow ip from any to me <span style="color: #000000; font-weight: bold;">in</span> via <span style="color: #007800;">$inet</span>
<span style="color: #666666; font-style: italic;">#$fwa 09700 allow ip from any to me in via $inet2</span>
<span style="color: #666666; font-style: italic;">#----------------------------------</span>
<span style="color: #666666; font-style: italic;">##ICMP</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">50200</span> allow icmp from me to any
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">50250</span> allow icmp from any to me
<span style="color: #666666; font-style: italic;">##Allow all outcoming</span>
<span style="color: #007800;">$fwa</span> <span style="color: #000000;">60000</span> allow ip from me to any</pre></div></div>

<p>На этом в общем-то все :) выставляем на клиентских машинах шлюзом по умолчанию наш сервер и радуемся &#8211; инет должен работать</p>
]]></content:encoded>
			<wfw:commentRss>http://snake.khd.ru/2009/10/kazhdomu-po-potrebnostyam-shejping-trafika-pri-pomoshhi-ipfw/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
