{"id":1747,"date":"2013-04-10T16:03:53","date_gmt":"2013-04-10T15:03:53","guid":{"rendered":"http:\/\/mccltd.net\/blog\/?p=1747"},"modified":"2013-04-10T16:03:53","modified_gmt":"2013-04-10T15:03:53","slug":"iproute2-crib-notes","status":"publish","type":"post","link":"http:\/\/darenmatthews.com\/blog\/?p=1747","title":{"rendered":"iproute2 Crib Notes"},"content":{"rendered":"<p>An aide-memoir from practicing iproute2, the utilities used to control TCP\/IP networking and Linux Traffic Control<\/p>\n<p><strong>&#8220;ip link list&#8221; &#8211; shows us our links<\/strong><\/p>\n<p>[Daren@home Daren]$ ip link list<br \/>\n1: lo: &lt;LOOPBACK,UP&gt; mtu 3924 qdisc noqueue<br \/>\nlink\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br \/>\n2: dummy: &lt;BROADCAST,NOARP&gt; mtu 1500 qdisc noop<br \/>\nlink\/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff<br \/>\n3: eth0: &lt;BROADCAST,MULTICAST,PROMISC,UP&gt; mtu 1400 qdisc pfifo_fast qlen 100<br \/>\nlink\/ether 48:54:e8:2a:47:16 brd ff:ff:ff:ff:ff:ff<br \/>\n4: eth1: &lt;BROADCAST,MULTICAST,PROMISC,UP&gt; mtu 1500 qdisc pfifo_fast qlen 100<br \/>\nlink\/ether 00:e0:4c:39:24:78 brd ff:ff:ff:ff:ff:ff<br \/>\n3764: ppp0: &lt;POINTOPOINT,MULTICAST,NOARP,UP&gt; mtu 1492 qdisc pfifo_fast qlen 10<br \/>\nlink\/ppp<br \/>\n<strong><!--more--><br \/>\n&#8220;ip address&#8221; &#8211; shows us our IP addresses<\/strong><\/p>\n<p>[Daren@home Daren]$ ip address show<br \/>\n1: lo: &lt;LOOPBACK,UP&gt; mtu 3924 qdisc noqueue<br \/>\nlink\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br \/>\ninet 127.0.0.1\/8 brd 127.255.255.255 scope host lo<br \/>\n2: dummy: &lt;BROADCAST,NOARP&gt; mtu 1500 qdisc noop<br \/>\nlink\/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff<br \/>\n3: eth0: &lt;BROADCAST,MULTICAST,PROMISC,UP&gt; mtu 1400 qdisc pfifo_fast qlen 100<br \/>\nlink\/ether 48:54:e8:2a:47:16 brd ff:ff:ff:ff:ff:ff<br \/>\ninet 10.0.0.1\/8 brd 10.255.255.255 scope global eth0<br \/>\n4: eth1: &lt;BROADCAST,MULTICAST,PROMISC,UP&gt; mtu 1500 qdisc pfifo_fast qlen 100<br \/>\nlink\/ether 00:e0:4c:39:24:78 brd ff:ff:ff:ff:ff:ff<br \/>\n3764: ppp0: &lt;POINTOPOINT,MULTICAST,NOARP,UP&gt; mtu 1492 qdisc pfifo_fast qlen 10<br \/>\nlink\/ppp<br \/>\ninet 10.10.0.251 peer 10.10.0.1\/32 scope global ppp0<\/p>\n<p>[Daren@home Daren]$ ip route show<br \/>\n10.10.0.1 dev ppp0 proto kernel scope link src 10.10.0.251<br \/>\n10.0.0.0\/8 dev eth0 proto kernel scope link src 10.0.0.1<br \/>\n127.0.0.0\/8 dev lo scope link<br \/>\ndefault via 10.10.0.1 dev ppp0<\/p>\n<p><em>(For comparison and reference, this is what the old route utility displayed):<\/em><\/p>\n<p>[Daren@home Daren]$ route -n<br \/>\nKernel IP routing table<br \/>\nDestination Gateway Genmask Flags Metric Ref Use<br \/>\nIface<br \/>\n10.10.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0<br \/>\n10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0<br \/>\n127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo<br \/>\n0.0.0.0 10.10.0.1 0.0.0.0 UG 0 0 0 ppp0<\/p>\n<p><strong>ARP<\/strong><br \/>\nYou can view your machines current arp\/neighbor cache\/table like so:<\/p>\n<p>[root@shimla \/home\/src\/iputils]# ip neigh show<br \/>\n10.5.50.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable<br \/>\n10.5.50.1 dev eth0 lladdr 00:06:29:21:73:c8 nud reachable<\/p>\n<p>As you can see my machine nagios1 (10.5.50.41) knows where to find filer1 (10.5.50.42) and filergateway (10.5.50.1). Now let&#8217;s add another machine to the arp cache.<\/p>\n<p>[root@shimla \/home\/daren\/.gnome-desktop]# ping -c 1 Linux-DOM0<br \/>\nPING Linux-DOM0.darenmatthews.com (10.5.50.43) from 10.5.50.41 : 56(84) bytes of data.<br \/>\n64 bytes from 10.5.50.43: icmp_seq=0 ttl=255 time=0.9 ms<\/p>\n<p>&#8212; Linux-DOM0.darenmatthews.com ping statistics &#8212;<br \/>\n1 packets transmitted, 1 packets received, 0% packet loss<br \/>\nround-trip min\/avg\/max = 0.9\/0.9\/0.9 ms<\/p>\n<p>[root@shimla \/home\/src\/iputils]# ip neigh show<br \/>\n10.5.50.43 dev eth0 lladdr 00:06:29:21:80:20 nud reachable<br \/>\n10.5.50.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable<br \/>\n10.5.50.1 dev eth0 lladdr 00:06:29:21:73:c8 nud reachable<\/p>\n<p>As a result of nagios1 trying to contact Linux-DOM0, Linux-DOM0&#8217;s hardware address\/location has now been added to the arp\/neighbor cache. So until the entry for Linux-DOM0 times out (as a result of no communication between the two) nagios1 knows where to find Linux-DOM0 and has no need to send an ARP request.<\/p>\n<p>Now let&#8217;s delete Linux-DOM0 from our arp cache:<\/p>\n<p>[root@shimla \/home\/src\/iputils]# ip neigh delete 10.5.50.43 dev eth0<br \/>\n[root@shimla \/home\/src\/iputils]# ip neigh show<br \/>\n10.5.50.43 dev eth0 nud failed<br \/>\n10.5.50.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable<br \/>\n10.5.50.1 dev eth0 lladdr 00:06:29:21:73:c8 nud stale<\/p>\n<p>Now nagios1 has again forgotten where to find Linux-DOM0 and will need to send another ARP request the next time he needs to communicate with Linux-DOM0<\/p>\n<p><strong>Rules &#8211; The routing policy database<\/strong><\/p>\n<p>If you have a large router, you may well cater for the needs of different people, who should be served differently. The routing policy database allows you to do this by having multiple sets of routing tables. (note: If you want to use this feature, make sure that your kernel is compiled with the &#8220;IP: advanced router&#8221; and &#8220;IP: policy routing&#8221; features).<\/p>\n<p>When the kernel needs to make a routing decision, it finds out which table needs to be consulted. By default, there are three tables. The old &#8216;route&#8217; tool modifies the main and local tables, as does the ip tool (by default).<\/p>\n<p>The default rules:<\/p>\n<p>[Daren@home Daren]$ ip rule list<br \/>\n0: from all lookup local<br \/>\n32766: from all lookup main<br \/>\n32767: from all lookup default<\/p>\n<p>This lists the priority of all rules. We see that all rules apply to all packets (&#8216;from all&#8217;). We&#8217;ve seen the &#8216;main&#8217; table before, it is output by ip route ls, but the &#8216;local&#8217; and &#8216;default&#8217; table are new.<\/p>\n<p>If we want to do fancy things, we generate rules which point to different tables which allow us to override system wide routing rules.<\/p>\n<p>For the exact semantics on what the kernel does when there are more matching rules, see Alexey&#8217;s ip-cref documentation.<br \/>\n4.1. Simple source policy routing<\/p>\n<p>Let&#8217;s take a real example once again, I have 2 (actually 3, about time I returned them) cable modems, connected to a Linux NAT (&#8216;masquerading&#8217;) router. People living here pay me to use the Internet. Suppose one of my house mates only visits hotmail and wants to pay less. This is fine with me, but they&#8217;ll end up using the low-end cable modem.<\/p>\n<p>The &#8216;fast&#8217; cable modem is known as 10.10.0.251 and is a PPP link to 10.10.0.1. The &#8216;slow&#8217; cable modem is known by various ip addresses, 212.13.50.120 in this example and is a link to 192.168.99.253.<\/p>\n<p>The local table:<\/p>\n<p>[Daren@home Daren]$ ip route list table local<br \/>\nbroadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1<br \/>\nlocal 10.0.0.1 dev eth0 proto kernel scope host src 10.0.0.1<br \/>\nbroadcast 10.0.0.0 dev eth0 proto kernel scope link src 10.0.0.1<br \/>\nlocal 10.10.0.251 dev ppp0 proto kernel scope host src 10.10.0.251<br \/>\nbroadcast 10.255.255.255 dev eth0 proto kernel scope link src 10.0.0.1<br \/>\nbroadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1<br \/>\nlocal 212.13.50.120 dev ppp2 proto kernel scope host src 212.13.50.120<br \/>\nlocal 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1<br \/>\nlocal 127.0.0.0\/8 dev lo proto kernel scope host src 127.0.0.1<\/p>\n<p>Lots of obvious things, but things that need to be specified somewhere. Well, here they are. The default table is empty.<\/p>\n<p>Let&#8217;s view the &#8216;main&#8217; table:<\/p>\n<p>[Daren@home Daren]$ ip route list table main<br \/>\n192.168.99.253 dev ppp2 proto kernel scope link src 212.13.50.120<br \/>\n10.10.0.1 dev ppp0 proto kernel scope link src 10.10.0.251<br \/>\n10.0.0.0\/8 dev eth0 proto kernel scope link src 10.0.0.1<br \/>\n127.0.0.0\/8 dev lo scope link<br \/>\ndefault via 10.10.0.1 dev ppp0<\/p>\n<p>We now generate a new rule which we call &#8216;TestUser&#8217;, for our hypothetical visitor. Although we can work with pure numbers, it&#8217;s far easier if we add our tables to \/etc\/iproute2\/rt_tables.<\/p>\n<p># echo 200 TestUser &gt;&gt; \/etc\/iproute2\/rt_tables<br \/>\n# ip rule add from 10.0.0.10 table TestUser<br \/>\n# ip rule ls<br \/>\n0: from all lookup local<br \/>\n32765: from 10.0.0.10 lookup TestUser<br \/>\n32766: from all lookup main<br \/>\n32767: from all lookup default<\/p>\n<p>Now all that is left is to generate TestUser&#8217;s table, and flush the route cache:<\/p>\n<p># ip route add default via 192.168.99.253 dev ppp2 table TestUser<br \/>\n# ip route flush cache<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An aide-memoir from practicing iproute2, the utilities used to control TCP\/IP networking and Linux Traffic Control &#8220;ip link list&#8221; &#8211; shows us our links [Daren@home Daren]$ ip link list 1: lo: &lt;LOOPBACK,UP&gt; mtu 3924 qdisc noqueue link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: dummy: &lt;BROADCAST,NOARP&gt; mtu 1500 qdisc noop link\/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 3: eth0: &lt;BROADCAST,MULTICAST,PROMISC,UP&gt; mtu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[84],"tags":[],"_links":{"self":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1747"}],"collection":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1747"}],"version-history":[{"count":1,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1747\/revisions"}],"predecessor-version":[{"id":1748,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1747\/revisions\/1748"}],"wp:attachment":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1747"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}