<?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>GustavoHenrique.net &#187; debian</title>
	<atom:link href="http://blog.gustavohenrique.net/tag/debian/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gustavohenrique.net</link>
	<description>Tecnologia e Software Livre</description>
	<lastBuildDate>Mon, 05 Mar 2012 10:50:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Roteamento em Linux com 2 links de internet</title>
		<link>http://blog.gustavohenrique.net/2008/12/roteamento-em-linux-com-2-links-de-internet/</link>
		<comments>http://blog.gustavohenrique.net/2008/12/roteamento-em-linux-com-2-links-de-internet/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 13:16:43 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[iproute2]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[roteamento]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=50</guid>
		<description><![CDATA[Introdução Como primeiro artigo sobre Linux, vou abordar um pouco sobre roteamento de pacotes usando iproute e iptables. Imagine um cenário onde há um servidor que compartilha a internet para a rede interna, atuando como gateway, e por algum motivo foi contratado mais um link de internet. Ou então há 2 ou mais servidores proxy [...]]]></description>
			<content:encoded><![CDATA[<h2>Introdução</h2>
<p>Como primeiro artigo sobre Linux, vou abordar um pouco sobre roteamento de pacotes usando iproute e iptables.<br />
Imagine um cenário onde há um servidor que compartilha a internet para a rede interna, atuando como gateway, e por algum motivo foi contratado mais um link de internet. Ou então há 2 ou mais servidores proxy e se deseja definir para qual deles cada IP da rede interna deve seguir caminho. Outro cenário seria separar a parte de navegação dos demais serviços (msn, emule, e-mail&#8230;), cada uma usando um link diferente. Nesse artigo vou demonstrar que o Linux proporciona maneiras simples e eficientes de implementar esse tipo de roteamento. As distribuições utilizadas foram ubuntu server 5.04 e 5.10 e debian lenny.<br />
<span id="more-50"></span></p>
<h2>Funcionamento</h2>
<p>No exemplo em questão, o servidor que vou configurar é um gateway compartilhando a internet para vários clientes. O iptables realiza uma marcação para todos os pacotes oriundos de cada IP cliente e o iproute determina para qual link os pacotes deverão ser encaminhados de acordo com a marcação feita. Vou usar a marcação 1 para pacotes via link1, marcação 2 para pacotes à serem encaminhados para o link2 e a rota principal do próprio gateway será pelo link1.</p>
<h2>Instalação</h2>
<p>O iptables e iproute são pacotes presentes em quase todas as distribuições. Na maioria o iptables é instalado por padrão. Nesse caso, vou instalar apenas o iproute, via apt-get, que é o gerenciador de pacotes padrão das distribuições debian e ubuntu.<br />
<code>sudo apt-get install iproute2</code></p>
<h2>Criando a tabela de roteamento</h2>
<p>Vou criar 2 tabelas chamadas link1 e link2, adicionando duas linhas no final do arquivo <code>/etc/iproute2/rt_tables</code>, ficando assim:<br />
<code><br />
root@gateway# cat /etc/iproute2/rt_tables<br />
255     local<br />
254     main<br />
253     default<br />
0       unspec<br />
250     link1<br />
251     link2<br />
</code></p>
<p>Agora vou criar o script <code>rotas</code>, dentro do diretório <code>/etc/init.d/</code>, que utiliza o iproute para definir as rotas.<br />
<code><br />
root@gateway# vim /etc/init.d/rotas<br />
</code></p>
<p>conteúdo do script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># DEFINICAO DOS GATEWAYS (IPs FICTICIOS)</span>
<span style="color: #007800;">GW_LINK1</span>=189.12.34.1
<span style="color: #007800;">GW_LINK2</span>=200.56.78.1
&nbsp;
<span style="color: #666666; font-style: italic;"># PLACAS DE REDE</span>
<span style="color: #007800;">ETH_LINK1</span>=eth1
<span style="color: #007800;">ETH_LINK2</span>=eth2
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa o cache de rotas</span>
  ip route flush cache
  <span style="color: #666666; font-style: italic;"># Pacotes com marcacao 1 vao para o link1</span>
  ip rule add fwmark <span style="color: #000000;">1</span> prio <span style="color: #000000;">20</span> table link1
  <span style="color: #666666; font-style: italic;"># Pacotes com marcacao 2 vao para o link2</span>
  ip rule add fwmark <span style="color: #000000;">2</span> prio <span style="color: #000000;">20</span> table link2
  <span style="color: #666666; font-style: italic;"># Associa a rota do link1 a interface de rede e tabela correspondentes</span>
  ip route add default via <span style="color: #007800;">$GW_LINK1</span> dev <span style="color: #007800;">$ETH_LINK1</span> table link1
  <span style="color: #666666; font-style: italic;"># Associa a rota do link1 a interface de rede e tabela correspondentes</span>
  ip route add default via <span style="color: #007800;">$GW_LINK2</span> dev <span style="color: #007800;">$ETH_LINK2</span> table link2
  <span style="color: #666666; font-style: italic;"># Adiciona a rota padrao ao link1</span>
  route add default gw <span style="color: #007800;">$GW_LINK1</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Tabela de roteamento criada.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stop<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa o cache</span>
  ip route flush cache
  <span style="color: #666666; font-style: italic;"># Deleta as regras de acordo com as marcacoes</span>
  ip rule del fwmark <span style="color: #000000;">2</span>
  ip rule del fwmark <span style="color: #000000;">3</span>
  <span style="color: #666666; font-style: italic;"># Deleta a rota padrao</span>
  route del default
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Limpeza da tabela de roteamento concluida.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">in</span>
  <span style="color: #ff0000;">'start'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'stop'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'restart'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p>Reparem que o que o script <code>rotas</code> faz é dizer que pacotes marcados como 1 (fwmark 1) fazem parte da tabela link1 e marcados como 2 (fwmark 2) da tabela link2. Em seguida cria uma regra dizendo que os pacotes da tabela link1 seguem pelo gateway e interface de rede correspondentes. O mesmo para a tabela link2.<br />
O próximo passo é fazer a marcação dos pacotes.</p>
<h2>Marcando os pacotes com iptables</h2>
<p>Esse script de firewall é apenas para demonstrar como fazer a marcação de pacotes. As regras devem ser incorporadas à um firewall melhor configurado e mais seguro.<br />
O iptables na verdade é um software que age como frontend para manipulação do firewall do Linux, chamado de netfilter. Esse por sua vez possui uma tabela chamada <code>mangle</code> que é onde ficam armazenadas as regras de marcação de pacotes e outra chamada <code>nat</code>, que contém as regras de <a href="http://pt.wikipedia.org/wiki/Nat">NAT</a>.<br />
Assim como antes, vou colocar o script dentro do diretório <code>/etc/init.d</code><br />
<code><br />
root@gateway# vim /etc/init.d/firewall<br />
</code></p>
<p>E dentro do script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># modprobe eh usado para carregar modulos do kernel</span>
<span style="color: #007800;">MOD</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> modprobe<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># iptables</span>
<span style="color: #007800;">IPT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> iptables<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Interfaces de rede</span>
<span style="color: #007800;">I_LINK1</span>=<span style="color: #ff0000;">&quot;eth1&quot;</span>
<span style="color: #007800;">I_LINK2</span>=<span style="color: #ff0000;">&quot;eth2&quot;</span>
<span style="color: #007800;">I_LAN</span>=<span style="color: #ff0000;">&quot;eth0&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stop<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa a tabela mangle</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-F</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-X</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Firewall parado.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Carrega o modulo do kernel</span>
  <span style="color: #007800;">$MOD</span> ip_tables
&nbsp;
  <span style="color: #666666; font-style: italic;"># Limpa as regras anteriores</span>
  stop;
&nbsp;
  <span style="color: #666666; font-style: italic;"># Habilita redirecionamento de IP</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>ipv4<span style="color: #000000; font-weight: bold;">/</span>ip_forward
&nbsp;
  <span style="color: #666666; font-style: italic;"># Clientes para o link1</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.2 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.4 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Clientes para o link2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.3 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.5 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.6 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Apenas a navegacao vai para o link2. E-mail, msn e outros vao para o link1 que é a rota padrao.</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.7 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.7 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Firewall iniciado.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">in</span>
  <span style="color: #ff0000;">'start'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'stop'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p>Por fim, vou colocar os dois scripts para carregarem durante a inicialização do sistema. Há muitas maneiras de fazer isso, então vou fazer da mais preguiçosa possível porque isso varia de acordo com o administrador e distribuição utilizada:<br />
<code><br />
root@gateway#: chmod +x /etc/init.d/rotas<br />
root@gateway#: chmod +x /etc/init.d/firewall<br />
root@gateway#: ln -s /etc/init.d/rotas /etc/rcS.d/S79rotas<br />
root@gateway#: ln -s /etc/init.d/firewall /etc/rcS.d/S80firewall<br />
</code></p>
<h2>Conclusão</h2>
<p>Apenas com esses 2 scripts não será possível compartilhar a internet. Porém o objetivo aqui é mostrar uma breve abordagem de como fazer roteamento no Linux. Artigos, dicas e scripts para compartilhar internet existem aos milhares na internet. Com uma rápida pesquisa no google e implementando o que foi descrito aqui, é possível criar um servidor de internet em Linux muito mais estável e eficiente do que um rodando Windows.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gustavohenrique.net/2008/12/roteamento-em-linux-com-2-links-de-internet/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

