lunes, 18 de abril de 2016

LINUX-SySAdmin-fundamentos y lab de routing

En linux la comunicación entre servidores se da a través de rutas.
Las rutas pueden ser comprendidas como carreteras donde viajan los datagramas hacia un destino (host IP).

Existe bastante literatura al respecto que explica el funcionamiento del routing. La intención de este articulo es observar la información desplegada por los distintos comandos y aprender a interpretarla adecuadamente.



Observemos el siguiente ejemplo:

[root@localhost ~]# ifconfig
enp0s3: flags=4163  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fee4:3f08  prefixlen 64  scopeid 0x20
        ether 08:00:27:e4:3f:08  txqueuelen 1000  (Ethernet)
        RX packets 655  bytes 71494 (69.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 332  bytes 73433 (71.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163  mtu 1500
        ether 08:00:27:e8:4c:90  txqueuelen 1000  (Ethernet)
        RX packets 291  bytes 45390 (44.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33  bytes 3042 (2.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@localhost ~]# ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.120 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.112 ms
^C
--- 192.168.56.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.112/0.116/0.120/0.004 ms
[root@localhost ~]# ping 8.8.8.8
connect: Network is unreachable
[root@localhost ~]#


Hay 2 interfaces de red ( enp0s3 y enp0s8 ) Sin embargo sólo enp0s3 está operando.
Luego en la tabla de rutas (route -n) la interfaz enp0s3 contiene la siguiente tabla de ruteo:
El campo "Destination" indica el IP HOST de destino. Como termina en cero, quiere decir que puede acceder a todos los HOST IP que estén dentro de la subred 192.168.56.X
El campo "Gateway" indica la puerta de enlace. Como es cero, indica que no pasa por ningún gateway, sino que es directo a la IP HOST.
El campo "Flags" indica "U" de up, está funcionando la carretera al host IP.
El campo "Iface" indica la interfaz de red o la carretera por la cual viaja el paquete

Finalmente si se intenta hacer ping a la ip publica 8.8.8.8 se obtiene el mensaje "Network is unreachable"
Esto es debido a que el routing tiene los siguientes criterios de búsqueda:

Al enviar un datagrama (solicitando un ping a un host) el sistema busca en la tabla de rutas en el siguiente orden:
Primero intenta buscar la IP destino directamente.
Segundo intenta buscar un router que coincida con la subred de la IP destino.
Tercero intenta buscar en el default router.

Si no encuentra respuesta en estos criterios, el sistema devuelve un código de rechazo en el encaminamiento del datagrama que se traduce en el mensaje indicado anteriormente.

Ahora levantaremos la interfaz que está offline y Observemos como cambia la tabla de rutas.

[root@localhost ~]# /sbin/ifup enp0s8
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@localhost ~]# ifconfig -a
enp0s3: flags=4163  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fee4:3f08  prefixlen 64  scopeid 0x20
        ether 08:00:27:e4:3f:08  txqueuelen 1000  (Ethernet)
        RX packets 865  bytes 93038 (90.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 434  bytes 88697 (86.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163  mtu 1500
        inet 192.168.8.106  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::a00:27ff:fee8:4c90  prefixlen 64  scopeid 0x20
        ether 08:00:27:e8:4c:90  txqueuelen 1000  (Ethernet)
        RX packets 370  bytes 58056 (56.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43  bytes 4116 (4.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    100    0        0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@localhost ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=31.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=39.2 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 31.346/35.303/39.261/3.961 ms
[root@localhost ~]# ping 192.168.8.1
PING 192.168.8.1 (192.168.8.1) 56(84) bytes of data.
64 bytes from 192.168.8.1: icmp_seq=1 ttl=64 time=4.30 ms
64 bytes from 192.168.8.1: icmp_seq=2 ttl=64 time=3.46 ms
^C
--- 192.168.8.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 3.463/3.883/4.304/0.425 ms
[root@localhost ~]#

Después de lavntar la interfaz enp0s8 se añaden dos registros a la tabla de rutas. Una es hacia el destino a la subred 192.168.8.X y otro destino a 0.0.0.0 

Cuando el destino es 0.0.0.0 quiere decir que es hacia internet. Donde la puerta de salida o gateway es la ip 192.168.8.1 cuyo flag indica que está online (U) y es un gateway (G). LA interfaz que lo provee es la Iface=enp0s8

Al ejecutar un ping a la ip pública 8.8.8.8 ahora si se obtiene el retorno esperado de la estadística de paquetes.  

Comando para mostar la tabla de ruta. 

Los comandos para mostrar la tabla de ruta pueden ser los siguientes: route y netstat. Un ejemplo de los parámetros de uso y la vistas obtenidas. 

[root@localhost ~]# route 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         www.huaweimobil 0.0.0.0         UG    100    0        0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    100    0        0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@localhost ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         www.huaweimobil 0.0.0.0         UG        0 0          0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U         0 0          0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
[root@localhost ~]# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG        0 0          0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U         0 0          0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
[root@localhost ~]# 

Cabe señalar que el param "n" indica mostrar los host ID, y no sus strings de máscaras. 
Es la diferencia que se puede apreciar entre el comando route y route -n. 
El primero muestra "www.huaweimobil" y el segundo muestra "192.168.8.1"

Agregar / eliminar rutas

Con el comando "route add" se puede agregar una ruta ya sea como host directo, como gateway o como default route. 

Como default route:

[root@localhost ~]#  route add default gw 192.168.1.10

Es importante señalar que la ip debe ser alcanzada por el server local. Sino se despliega el siguiente error an intentar agregar una ip el cual no es posible alcanzar

[root@localhost ~]# route add default gw 192.168.1.10
SIOCADDRT: Network is unreachable
[root@localhost ~]# 

Rechazar un determinado host. 

Para que sel server no pueda alcanzar o ser alcanzado por un determinado host, se puede indicar a la tabla de ruta que lo rechace al intentar alcanzarlo, y de paso rechazar cualquier petición desde este host. De la siguiente forma:

[root@localhost ~]# ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.135 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.161 ms
64 bytes from 192.168.56.1: icmp_seq=3 ttl=64 time=0.142 ms
^C64 bytes from 192.168.56.1: icmp_seq=4 ttl=64 time=0.135 ms
64 bytes from 192.168.56.1: icmp_seq=5 ttl=64 time=0.134 ms
^C
--- 192.168.56.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.134/0.141/0.161/0.014 ms
[root@localhost ~]# route add -host 192.168.56.1 reject
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    100    0        0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.56.1    -               255.255.255.255 !H    0      -        0 -
[root@localhost ~]# ping 192.168.56.1
connect: Network is unreachable
[root@localhost ~]#

Inclusive, desde el host remoto no es posible obtener respuesta desde el local server. 

[gcepeda@santorini ~]$ ifconfig -a | grep inet
          inet addr:192.168.56.1  Bcast:192.168.56.255  Mask:255.255.255.0
[gcepeda@santorini ~]$ ping 192.168.56.101
PING 192.168.56.101 (192.168.56.101) 56(84) bytes of data.
From 192.168.56.1 icmp_seq=1 Destination Host Unreachable
From 192.168.56.1 icmp_seq=2 Destination Host Unreachable
From 192.168.56.1 icmp_seq=3 Destination Host Unreachable
^C
--- 192.168.56.101 ping statistics ---
4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 3016ms
pipe 3
[gcepeda@santorini ~]$ 

Para levantar el bloqueo, se utiliza el siguiente comando:

[root@localhost ~]# route del -host 192.168.56.1 reject
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    100    0        0 enp0s8
192.168.8.0     0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
[root@localhost ~]# ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.000 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.109 ms
^C
--- 192.168.56.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.000/0.054/0.109/0.055 ms
[root@localhost ~]# 

Como Network

[root@localhost ~]# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.3.10
El comando indica:
-net 192.168.3.0: como destino todos los host id de la subred 192.168.3
netmask 255.255.255.0: la máscara asociada a la subred,
gw 192.168.3.10: la ip de la puerta o gateway por donde enrutar la solicitud.

Como Host en particular

Basado en el ejemplo anterior, si se requiere agregar la ruta a un host en particular:

[root@localhost ~]# route add -host 192.168.3.4 netmask 255.255.255.0 gw 192.168.3.10
El comando indica: 
host 192.168.3.4: indica el destino que se quiere alcanzar. En este caso un host en particular.  
netmask 255.255.255.0: la máscara asociada a la subred,
gw 192.168.3.10: la ip de la puerta o gateway por donde enrutar la solicitud.


Fuentes:

http://www.thegeekstuff.com/2012/04/ip-routing-intro/
http://www.thegeekstuff.com/2012/04/route-examples/
http://www.thegeekstuff.com/2012/05/route-flags/
http://unix.stackexchange.com/questions/94018/what-is-the-meaning-of-0-0-0-0-as-a-gateway
https://superuser.com/questions/575615/static-routing-table-behaviour-with-gateway-set-to-linux
https://serverfault.com/questions/337410/how-to-remove-route-with-ip

1 comentario: