Query on IPv6 subnetting - networking

Supposing two IPv6 addresses ip1 (abc::1/64), ip2 (abc::2/64) are given, what's the algorithm to determine if they're part of the same subnet or not?
For IPv4, each octet in one address is masked with the subnet octet and the result compared with the result of a similar exercise for the second address. Could we do a similar thing for the each hextet in the IPv6 address?
Also, in IPv6, is a prefix length /64 equivalent to 'ffff:ffff:ffff:ffff::'? How is the netmask represented in IPv6 address format?
Thanks!

Supposing two IPv6 addresses ip1 (abc::1/64), ip2 (abc::2/64) are
given, what's the algorithm to determine if they're part of the same
subnet or not?
They are on the same subnet if the first 64 bits are equal. These are the first four 'blocks' in the address. If you write the full addresses they become 0abc:0000:0000:0000:0000:0000:0000:0001 and 0abc:0000:0000:0000:0000:0000:0000:0002. The first 64 bits (the 0abc:0000:0000:0000: part) is equal, so they are on the same subnet.
For IPv4, each octet in one address is masked with the subnet octet
and the result compared with the result of a similar exercise for the
second address. Could we do a similar thing for the each hextet in the
IPv6 address?
Actually, you mask the bits, not the octet. But you can still use the same technique. Convert the address to bits and mask it with a bit sequence corresponding to the prefix length. So a /64 would be 64 bits with value 1 followed by 64 bits with value 0. For a /48 it would be 48 bits with value 1 followed by 80 bits with value 0.
Also, in IPv6, is a prefix length /64 equivalent to
'ffff:ffff:ffff:ffff::'? How is the netmask represented in IPv6
address format?
Usually you don't, but 'ffff:ffff:ffff:ffff::' corresponds to a /64 netmask.

IPv4 netmasks are a bit bogus because it allows people to specify masks like 255.0.0.255. IPv6 clarifies this by defining the term "prefix length" as the length of contiguous bits in the netmask.
255.0.0.0 ≡ ff00:: ≡ 8-bit prefix
255.255.0.0 ≡ ffff:: ≡ 16-bit prefix
255.255.192.0 ≡ ffff:C000:: ≡ 18-bit prefix
If you have two addresses, IP#1 and IP#2 and a netmask NETMASK then to test if the two addresses in the same network then the following must be true:
IP#1 & NETMASK == IP#2 & NETMASK
This extends true for both IPv4 and IPv6, so all that is necessary is to convert IPv6 prefix lengths into a netmask that is easier to use in the test.

the standard Radix Tree (Trie) (Patricia ) implementation of the Route look up is available for IPv6 .
Netmask is represented as follows :
when we describe an IPv4 IP and network, we use a netmask to define the network. IPv6 has decided, rather than use the old 255.255.255.0 notation, that it's better just to use /# notation. So our netmask would be 128 to describe something similar to /32 (255.255.255.255) or one host. Note that expr 16 \* 8 gives us 128." [1]
[1] http://www.linux-sxs.org/networking/ipv6_for_beginners.html
[2] http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/

Related

Which mask to use for 1024 address size subnet

I am trying to identify if given IPs are from same 1024 SUBNET.
192.168.3.1
192.168.2.19
192.168.1.1
The part I got stuck is that SUBNET address size is 1024.
If I am sure, by checking IP addresses we have to use mask of C class? Because IP is in range between 192.168.0.0 and 192.168.255.255.
But the problem is that I am not sure which mask to use.
I found on the internet table of c class addresses, but I am not sure if it's this one.
Is MASK 255.252.0.0 ?
I believe all these IPs are from same subnet.
I used first table in https://www.aelius.com/njh/subnet_sheet.html
Mask for each one is 255.255.252.0 (We convert mask to binary, and on the end
we got 10000000000 - thats exactly 1024 in decimal)
Address I got for each IP after binary multiplication - 192.168.0.0

Why doesn't ::1 match 127.0.0.1?

The loopback IP address in IPv6 format is:
0000:0000: 0000:0000: 0000:0000: 0000:0001
which is shortened to:
0:0: 0:0: 0:0: 0:1
which is then shortened to (keep last 2 words, ie. 4 bytes, to match IPv4):
:0:1 (But I still don't know why it's double colons instead of single colon before 1, maybe for implying that there are 4 bytes here)
and simplified as:
::1
Expand ::1 into IPv4 format, it is:
0.0.0.1
But this is not the same as 127.0.0.1, where's the number 127?
First, :0:1 is not a valid IPv6 address. I'll explain why:
IPv6 address are 128 bit addresses written as eight hexadecimal parts separated by colons. Because this can be quite tedious to write, and because many IPv6 address (like 0:0:0:0:0:0:0:1) contain many parts that are zero, it's allowed to replace one sequence of zeroes with two colons. The full address is still the same. It's just easier to write :: than 0:0:0:etc.
IPv4 and IPv6 are two different protocols. You cannot compare the IPv4 loopback address 127.0.0.1 with the IPv6 loopback address ::1. They are completely unrelated. Both of them just happen to end with the same digit.

Cidr blocks AWS explanation

Can someone explain exactly how CIDR blocks work and how it translates into 0.0.0.0/32 for example? Please use laymen’s terms or perhaps even an analogy to something not network related. Can’t seems to find an explanation that clicks with me. Thanks!!
Classless Inter-Domain Routing (CIDR) blocks are for specifying a range to IP addresses in format of IPv4 or IPv6. For the sake of simplicity I will explain rest of this in format of IPv4 however it is applicable to IPv6.
General format for CIDR Blocks: x.y.z.t/p
x, y, z and t are numbers from 0 to 255. Basically, each represents an 8 bit binary number. That's why it is range is up to 255. Combination of this numbers becomes an IPv4 IP address that must be unique to be able to identify a specific instance.
In case of AWS, p is a number from 16 to 28. It represents the number of bits that are inherited from given IP address. For example: 10.0.0.0/16 represents an IP address in following format: 10.0.x.y where x and y are any number from 0 to 255. So, actually it represents a range of IP addresses, starting from 10.0.0.0 to 10.0.255.255.
However for each CIDR block, AWS prohibits 5 possible IP addresses. Those are the first 4 available addresses and the last available address. In this case:
10.0.0.0: Network address
10.0.0.1: Reserved for VPC router
10.0.0.2: DNS server
10.0.0.3: Reserved for future use
10.0.255.255: Network broadcast
See here for official doc.
Actually this is one of the main reasons why AWS permits numeric value of p up to /28. Because for p=30, there will be 4 available values however AWS needs 5 IP address to use. In my opinion for p=29, they might found it inefficient to occupy 5 addresses to provide 3 possible IP address.
Number of possible IP addresses can be calculated by using this formula:
NumberOfPossibleIPs = 2^(32-p) - 5
Classless Inter-Domain Routing (CIDR) block basically is a method for allocating IP addresses and IP routing. When you create a network or route table, you need to specify what range are you working in. "0.0.0.0" means that it will match to any IP address. Some IP addresses are specific, like 10.0.0.0, which will match to any IP address beginning with 10. With any IP address range, you can be more specific by using a suffix(something like /32 from your example). These allow the notation to specify number of bits to be used from Prefix(actual IP-range like 10.0.0.0). It represents the bit length of the subnet mask, as indicated above. The subnet mask is like masking when painting. You place a mask over what you DO NOT want to paint on.
For example, 10.10.0.0/16 will have 256 * 256 IP address in its range.
NOTE: Some of the IP address in a range are reserved for various purposes. According to AWS VPC documentation, following are the reserved IP addresses.
10.0.0.0: Network address.
10.0.0.1: Reserved by AWS for the VPC router.
10.0.0.2: Reserved by AWS. The IP address of the DNS server is always the base of the VPC network range plus two; however, we also reserve the base of each subnet range plus two. For VPCs with multiple CIDR blocks, the IP address of the DNS server is located in the primary CIDR. For more information, see Amazon DNS Server.
10.0.0.3: Reserved by AWS for future use.
10.0.0.255: Network broadcast address. We do not support broadcast in a VPC, therefore we reserve this address.
Hope this helps!
All of the above answers are great, but are missing something pretty important for the people who don't understand addressing.
IP addresses are literally just a string of binary, broken up into 4 "octets". Each octet is a 2^8 block; 00000000. So to a machine, an IP address looks like this (with (.) added for human-ness):
00000000(.)00000000(.)00000000(.)00000000
When we're talking about the "mask" on the IP address, it means "the bits that don't change". The /8 or /255.0.0.0 on the end of the block signifies the number of bits that are not allowed to be used by this network.
So, lets say we have a CIDR block of 10.0.0.0/8 - this can also be written in the format 10.0.0.0/255.0.0.0, and you may in fact see this for of notation in older versions of linux. You will also note that 255 is the decimal representation of the binary string 11111111 - 8 binary "ones". So what the machine sees is the following:
Net: 00001010(.)00000000(.)00000000(.)00000000
Mask: 11111111(.)00000000(.)00000000(.)00000000
The part of the mask with 0's is usable address space within the network.
So the following example addresses are valid in this network, because on the 0 parts of the masked range are changing:
00001010(.)00000001(.)00110000(.)00111000
00001010(.)00110001(.)00110100(.)00111001
When we say "cidr block" we simply mean "the human-readable shorthand way of expressing binary strings understood by a machine". In the above example, the first octet can be expressed as 10, and the latter octets 0. And the Mask can be expressed as 255 and the latter octets of 0, or; because the mask is always a sequence of 1's, then a sequence of 0's, the length of the 1's, i.e. 8
And as such, we get a cidr of 10.0.0.0/255.0.0.0, or 10.0.0.0/8
A few more examples:
-- 172.1.1.0/24
net: 10101100.00000001.00000001.00000000
mask: 11111111.11111111.11111111.00000000
^ 24 bits for the mask ^ 8 bits of usable space
-- 10.10.10.8/29
net: 00001010.00001010.00001010.00001000
mask: 11111111.11111111.11111111.11111000
^ 29 bits for the mask. ^ 3 bits of usable space
Importantly though, this is only one aspect of networking. Usually a couple of these are reserved for things. See other answers for AWS specific things. In their examples, the "first 4" ip addresses reserved for AWS will be the first 4 usable addresses, which would be
...00 - Network address
...01 - Router
...10 - DNS
...11 - Futureproofing

How to know if given IPV6 address belongs to the same subnet as local machine

Currenly I have a c function that takes a ip address and subnetmask and checks if given ipv4 is private/public.
It does by following logic to matchsubnet:
(local ipv4 address && local subnetmask) == (given ipv4 && given subnetmask)
What logic can be written for ipv6? I can write a function to receive an ip and subnet prefix. Can i get local subnet prefix?
I order to determine if two hosts are on the same subnet take both their addresses and prefix lengths (subnet masks).
If the prefix lengths are different then they are not on the same network.
If the prefix lengths are the same then take the length ( very probably 64, so let's assume that) and compare the first 64 bits of both addresses with each other. If they are the same then they are both on the same network.
It is basically the same as you do for IPv4 with making the bits that don't being to the prefix. With 128 addresses it might be easier though to implement it as a loop comparing 4 bits at a time (comparing the hexadecimal notation) or 8 bits at a time (comparing the parsed bytes).

What does "192.168.1.1/21" mean?

When they write a slash followed by a number after an IP address, what does that mean?
The part after the slash is how many subnet mask bits to use. Since the use of classless routing you use slash instead of saying class A or B whatever. Example:
192.168.1.1/24 is 192.168.1.1 255.255.255.0
255.255.255.0 is using 24 of the 32 bits to create the subnet.
in binary it looks like this:
11111111.11111111.11111111.00000000
so a /30 would look like:
255.255.255.252 or in binary
11111111.11111111.11111111.11111100
the remaing 00 is for hosts; the 1's are the network.
It's CIDR notation for a subnet.
The /21 denotes that the first (most-significant) 21 bits are common to all addresses in the subnet. The individual hosts in the network have addresses that are different only in the last 11 bits.

Resources