CIDR was introduced to address the exhaustion of IPv4 address space and inefficient allocation of large address blocks. It allows for flexible subnet masks and routing based on the longest prefix match. IPv6 vastly expands the available address space to accommodate future growth. Packet forwarding in both protocols works by routers looking up the destination IP address in their forwarding table to determine the outgoing interface for each packet.