Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network: Longest prefix matching

Tags:

A router (IPv4)

Destination             Interface
0.0.0.0/0               m0
172.58.128.0/17         m1
172.58.128.0/19         m2 
172.58.160.0/19         m3

I need choose which interface is correct. I did so:

172.58.218.80 m2
172.58.165.90 m3
172.58.124.36 m1
172.58.169.18 m3  
172.58.155.112 m2
172.59.12.142 m0

Can anyone check if I did this right? Beacuse I do not really understand the "longest prefix match".

How can I know the address is been "match" in the routing table?

Thanks


Thanks. resmon6

So Step1 convert it to binary.

10101100.00111010.10000000.00000000 172.58.128.0/17  
10101100.00111010.10000000.00000000 172.58.128.0/19         
10101100.00111010.10100000.00000000 172.58.160.0/19

Step2 use subnet mask to get the prefix.

10101100.00111010.1    is the prefix for 172.58.128.0/17
10101100.00111010.100  is the prefix for 172.58.128.0/19
10101100.00111010.101  is the prefix for 172.58.160.0/19

Step3

10101100.00111010.11011010.01010000 172.58.218.80 
10101100.00111010.10100101.01011010 172.58.165.90 
10101100.00111010.01111100.00100100 172.58.124.36 
10101100.00111010.10101001.00010010 172.58.169.18  
10101100.00111010.10011011.01110000 172.58.155.112 
10101100.00111011.00001100.10001110 172.59.12.142

-------------------------------------------------------- 
10101100.00111010.1 
10101100.00111010.100 
10101100.00111010.101

So it means

172.58.218.80 m1
172.58.165.90 m3
172.58.124.36 m0
172.58.169.18 m3
172.58.155.112 m2
172.59.12.142 m0

Can you help me to check it is correct now ?

Thanks

like image 879
Jimmy Avatar asked Feb 17 '12 21:02

Jimmy


1 Answers

When you have a routing table the prefix is the route itself converted to binary, matching only the number of bits in the subnet mask. So if you take this routing table

192.168.100.0/24
192.168.0.0/16

and convert it to binary you get

11000000.10101000.01100100.00000000   192.168.100.0/24
11000000.10101000.00000000.00000000   192.168.0.0/16

Then you use the subnet mask to get the prefix. Use the number of bits in the subnet mask, starting from the left.

11000000.10101000.01100100   is the prefix for 192.168.100.0/24
11000000.10101000            is the prefix for 192.168.0.0/16

Now when you are choosing a route for a destination you will convert your destination IP to binary and choose the route with the longest prefix that matches.

You can see that the destination for 192.168.100.50 matches both route prefixes so it chooses the longer prefix of 192.168.100.0/24

192.168.100.50/32 11000000.10101000.01100100.00110010
-----------------------------------------------------
192.168.100.0/24  11000000.10101000.01100100
192.168.0.0/16    11000000.10101000

I know subnetting can be confusing at first but I don't want to do your work for you. I will tell you that it's not correct.

like image 75
resmon6 Avatar answered Sep 22 '22 17:09

resmon6