Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 1 | #include <linux/export.h> |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 2 | #include <linux/netfilter/ipset/pfxlen.h> |
| 3 | |
Jozsef Kadlecsik | ca0f6a5 | 2015-06-13 19:45:33 +0200 | [diff] [blame] | 4 | /* Prefixlen maps for fast conversions, by Jan Engelhardt. */ |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 5 | |
Aaron Conole | 63c2af9 | 2017-09-11 21:52:39 +0200 | [diff] [blame] | 6 | #ifdef E |
| 7 | #undef E |
| 8 | #endif |
| 9 | |
| 10 | #define PREFIXES_MAP \ |
| 11 | E(0x00000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 12 | E(0x80000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 13 | E(0xC0000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 14 | E(0xE0000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 15 | E(0xF0000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 16 | E(0xF8000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 17 | E(0xFC000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 18 | E(0xFE000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 19 | E(0xFF000000, 0x00000000, 0x00000000, 0x00000000), \ |
| 20 | E(0xFF800000, 0x00000000, 0x00000000, 0x00000000), \ |
| 21 | E(0xFFC00000, 0x00000000, 0x00000000, 0x00000000), \ |
| 22 | E(0xFFE00000, 0x00000000, 0x00000000, 0x00000000), \ |
| 23 | E(0xFFF00000, 0x00000000, 0x00000000, 0x00000000), \ |
| 24 | E(0xFFF80000, 0x00000000, 0x00000000, 0x00000000), \ |
| 25 | E(0xFFFC0000, 0x00000000, 0x00000000, 0x00000000), \ |
| 26 | E(0xFFFE0000, 0x00000000, 0x00000000, 0x00000000), \ |
| 27 | E(0xFFFF0000, 0x00000000, 0x00000000, 0x00000000), \ |
| 28 | E(0xFFFF8000, 0x00000000, 0x00000000, 0x00000000), \ |
| 29 | E(0xFFFFC000, 0x00000000, 0x00000000, 0x00000000), \ |
| 30 | E(0xFFFFE000, 0x00000000, 0x00000000, 0x00000000), \ |
| 31 | E(0xFFFFF000, 0x00000000, 0x00000000, 0x00000000), \ |
| 32 | E(0xFFFFF800, 0x00000000, 0x00000000, 0x00000000), \ |
| 33 | E(0xFFFFFC00, 0x00000000, 0x00000000, 0x00000000), \ |
| 34 | E(0xFFFFFE00, 0x00000000, 0x00000000, 0x00000000), \ |
| 35 | E(0xFFFFFF00, 0x00000000, 0x00000000, 0x00000000), \ |
| 36 | E(0xFFFFFF80, 0x00000000, 0x00000000, 0x00000000), \ |
| 37 | E(0xFFFFFFC0, 0x00000000, 0x00000000, 0x00000000), \ |
| 38 | E(0xFFFFFFE0, 0x00000000, 0x00000000, 0x00000000), \ |
| 39 | E(0xFFFFFFF0, 0x00000000, 0x00000000, 0x00000000), \ |
| 40 | E(0xFFFFFFF8, 0x00000000, 0x00000000, 0x00000000), \ |
| 41 | E(0xFFFFFFFC, 0x00000000, 0x00000000, 0x00000000), \ |
| 42 | E(0xFFFFFFFE, 0x00000000, 0x00000000, 0x00000000), \ |
| 43 | E(0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000), \ |
| 44 | E(0xFFFFFFFF, 0x80000000, 0x00000000, 0x00000000), \ |
| 45 | E(0xFFFFFFFF, 0xC0000000, 0x00000000, 0x00000000), \ |
| 46 | E(0xFFFFFFFF, 0xE0000000, 0x00000000, 0x00000000), \ |
| 47 | E(0xFFFFFFFF, 0xF0000000, 0x00000000, 0x00000000), \ |
| 48 | E(0xFFFFFFFF, 0xF8000000, 0x00000000, 0x00000000), \ |
| 49 | E(0xFFFFFFFF, 0xFC000000, 0x00000000, 0x00000000), \ |
| 50 | E(0xFFFFFFFF, 0xFE000000, 0x00000000, 0x00000000), \ |
| 51 | E(0xFFFFFFFF, 0xFF000000, 0x00000000, 0x00000000), \ |
| 52 | E(0xFFFFFFFF, 0xFF800000, 0x00000000, 0x00000000), \ |
| 53 | E(0xFFFFFFFF, 0xFFC00000, 0x00000000, 0x00000000), \ |
| 54 | E(0xFFFFFFFF, 0xFFE00000, 0x00000000, 0x00000000), \ |
| 55 | E(0xFFFFFFFF, 0xFFF00000, 0x00000000, 0x00000000), \ |
| 56 | E(0xFFFFFFFF, 0xFFF80000, 0x00000000, 0x00000000), \ |
| 57 | E(0xFFFFFFFF, 0xFFFC0000, 0x00000000, 0x00000000), \ |
| 58 | E(0xFFFFFFFF, 0xFFFE0000, 0x00000000, 0x00000000), \ |
| 59 | E(0xFFFFFFFF, 0xFFFF0000, 0x00000000, 0x00000000), \ |
| 60 | E(0xFFFFFFFF, 0xFFFF8000, 0x00000000, 0x00000000), \ |
| 61 | E(0xFFFFFFFF, 0xFFFFC000, 0x00000000, 0x00000000), \ |
| 62 | E(0xFFFFFFFF, 0xFFFFE000, 0x00000000, 0x00000000), \ |
| 63 | E(0xFFFFFFFF, 0xFFFFF000, 0x00000000, 0x00000000), \ |
| 64 | E(0xFFFFFFFF, 0xFFFFF800, 0x00000000, 0x00000000), \ |
| 65 | E(0xFFFFFFFF, 0xFFFFFC00, 0x00000000, 0x00000000), \ |
| 66 | E(0xFFFFFFFF, 0xFFFFFE00, 0x00000000, 0x00000000), \ |
| 67 | E(0xFFFFFFFF, 0xFFFFFF00, 0x00000000, 0x00000000), \ |
| 68 | E(0xFFFFFFFF, 0xFFFFFF80, 0x00000000, 0x00000000), \ |
| 69 | E(0xFFFFFFFF, 0xFFFFFFC0, 0x00000000, 0x00000000), \ |
| 70 | E(0xFFFFFFFF, 0xFFFFFFE0, 0x00000000, 0x00000000), \ |
| 71 | E(0xFFFFFFFF, 0xFFFFFFF0, 0x00000000, 0x00000000), \ |
| 72 | E(0xFFFFFFFF, 0xFFFFFFF8, 0x00000000, 0x00000000), \ |
| 73 | E(0xFFFFFFFF, 0xFFFFFFFC, 0x00000000, 0x00000000), \ |
| 74 | E(0xFFFFFFFF, 0xFFFFFFFE, 0x00000000, 0x00000000), \ |
| 75 | E(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000), \ |
| 76 | E(0xFFFFFFFF, 0xFFFFFFFF, 0x80000000, 0x00000000), \ |
| 77 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xC0000000, 0x00000000), \ |
| 78 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xE0000000, 0x00000000), \ |
| 79 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xF0000000, 0x00000000), \ |
| 80 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xF8000000, 0x00000000), \ |
| 81 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFC000000, 0x00000000), \ |
| 82 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFE000000, 0x00000000), \ |
| 83 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000, 0x00000000), \ |
| 84 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFF800000, 0x00000000), \ |
| 85 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFC00000, 0x00000000), \ |
| 86 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFE00000, 0x00000000), \ |
| 87 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFF00000, 0x00000000), \ |
| 88 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFF80000, 0x00000000), \ |
| 89 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFC0000, 0x00000000), \ |
| 90 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFE0000, 0x00000000), \ |
| 91 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFF0000, 0x00000000), \ |
| 92 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFF8000, 0x00000000), \ |
| 93 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFC000, 0x00000000), \ |
| 94 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFE000, 0x00000000), \ |
| 95 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFF000, 0x00000000), \ |
| 96 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFF800, 0x00000000), \ |
| 97 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFC00, 0x00000000), \ |
| 98 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFE00, 0x00000000), \ |
| 99 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFF00, 0x00000000), \ |
| 100 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFF80, 0x00000000), \ |
| 101 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFC0, 0x00000000), \ |
| 102 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFE0, 0x00000000), \ |
| 103 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFF0, 0x00000000), \ |
| 104 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFF8, 0x00000000), \ |
| 105 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFC, 0x00000000), \ |
| 106 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0x00000000), \ |
| 107 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000), \ |
| 108 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000), \ |
| 109 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xC0000000), \ |
| 110 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xE0000000), \ |
| 111 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xF0000000), \ |
| 112 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xF8000000), \ |
| 113 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFC000000), \ |
| 114 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFE000000), \ |
| 115 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF000000), \ |
| 116 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFF800000), \ |
| 117 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFC00000), \ |
| 118 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFE00000), \ |
| 119 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFF00000), \ |
| 120 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFF80000), \ |
| 121 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFC0000), \ |
| 122 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFE0000), \ |
| 123 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFF0000), \ |
| 124 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFF8000), \ |
| 125 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFC000), \ |
| 126 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFE000), \ |
| 127 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFF000), \ |
| 128 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFF800), \ |
| 129 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFC00), \ |
| 130 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFE00), \ |
| 131 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFF00), \ |
| 132 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFF80), \ |
| 133 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFC0), \ |
| 134 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFE0), \ |
| 135 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFF0), \ |
| 136 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFF8), \ |
| 137 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFC), \ |
| 138 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE), \ |
| 139 | E(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF), |
| 140 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 141 | #define E(a, b, c, d) \ |
| 142 | {.ip6 = { \ |
Joe Perches | b80edf0 | 2014-03-12 10:04:19 -0700 | [diff] [blame] | 143 | htonl(a), htonl(b), \ |
| 144 | htonl(c), htonl(d), \ |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 145 | } } |
| 146 | |
Jozsef Kadlecsik | ca0f6a5 | 2015-06-13 19:45:33 +0200 | [diff] [blame] | 147 | /* This table works for both IPv4 and IPv6; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 148 | * just use prefixlen_netmask_map[prefixlength].ip. |
| 149 | */ |
| 150 | const union nf_inet_addr ip_set_netmask_map[] = { |
Aaron Conole | 63c2af9 | 2017-09-11 21:52:39 +0200 | [diff] [blame] | 151 | PREFIXES_MAP |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 152 | }; |
| 153 | EXPORT_SYMBOL_GPL(ip_set_netmask_map); |
| 154 | |
| 155 | #undef E |
Jozsef Kadlecsik | ca0f6a5 | 2015-06-13 19:45:33 +0200 | [diff] [blame] | 156 | #define E(a, b, c, d) \ |
| 157 | {.ip6 = { (__force __be32)a, (__force __be32)b, \ |
| 158 | (__force __be32)c, (__force __be32)d, \ |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 159 | } } |
| 160 | |
Jozsef Kadlecsik | ca0f6a5 | 2015-06-13 19:45:33 +0200 | [diff] [blame] | 161 | /* This table works for both IPv4 and IPv6; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 162 | * just use prefixlen_hostmask_map[prefixlength].ip. |
| 163 | */ |
| 164 | const union nf_inet_addr ip_set_hostmask_map[] = { |
Aaron Conole | 63c2af9 | 2017-09-11 21:52:39 +0200 | [diff] [blame] | 165 | PREFIXES_MAP |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 166 | }; |
| 167 | EXPORT_SYMBOL_GPL(ip_set_hostmask_map); |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame] | 168 | |
| 169 | /* Find the largest network which matches the range from left, in host order. */ |
| 170 | u32 |
| 171 | ip_set_range_to_cidr(u32 from, u32 to, u8 *cidr) |
| 172 | { |
| 173 | u32 last; |
| 174 | u8 i; |
| 175 | |
| 176 | for (i = 1; i < 32; i++) { |
| 177 | if ((from & ip_set_hostmask(i)) != from) |
| 178 | continue; |
| 179 | last = from | ~ip_set_hostmask(i); |
| 180 | if (!after(last, to)) { |
| 181 | *cidr = i; |
| 182 | return last; |
| 183 | } |
| 184 | } |
| 185 | *cidr = 32; |
| 186 | return from; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(ip_set_range_to_cidr); |