Add IPv4/IPv6 IANA special purpose registry as generated data

This commit is contained in:
João Valverde 2023-10-24 15:06:56 +01:00
parent 29decd00bd
commit b82b32fdd7
12 changed files with 476 additions and 5 deletions

View File

@ -77,10 +77,6 @@ indent_size = tab
indent_style = tab
indent_size = tab
[ipv4.[ch]]
indent_style = tab
indent_size = tab
[next_tvb.[ch]]
indent_size = 2

View File

@ -100,6 +100,7 @@ set(LIBWIRESHARK_PUBLIC_HEADERS
iax2_codec_type.h
in_cksum.h
introspection.h
iana-ip.h
ip_opts.h
ipproto.h
ipv4.h
@ -211,9 +212,12 @@ set(LIBWIRESHARK_NONGENERATED_FILES
golay.c
guid-utils.c
iana_charsets.c
iana-ip.c
in_cksum.c
introspection.c
ipproto.c
ipv4.c
ipv6.c
manuf.c
maxmind_db.c
media_params.c

View File

@ -14,7 +14,6 @@
#include <stdlib.h>
#include <string.h>
#include "tvbuff.h"
#include "ipv6.h"
#include "addr_and_mask.h"
#include <wsutil/ws_assert.h>

View File

@ -13,6 +13,7 @@
#ifndef __ADDR_AND_MASK_H__
#define __ADDR_AND_MASK_H__
#include <epan/tvbuff.h>
#include <wsutil/inet_ipv4.h>
#include <wsutil/inet_ipv6.h>

80
epan/iana-ip-data.c Normal file
View File

@ -0,0 +1,80 @@
/*
* This file was generated by running ./tools/make-iana-ip.py.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "iana-ip.h"
_U_ static const struct ws_ipv4_special_block __ipv4_special_block[] = {
{ { 0, 8 }, "\"This network\"", 1, 0, 0, 0, 1 },
{ { 0, 32 }, "\"This host on this network\"", 1, 0, 0, 0, 1 },
{ { 167772160, 8 }, "Private-Use", 1, 1, 1, 0, 0 },
{ { 1681915904, 10 }, "Shared Address Space", 1, 1, 1, 0, 0 },
{ { 2130706432, 8 }, "Loopback", -1, -1, -1, -1, 1 },
{ { 2851995648, 16 }, "Link Local", 1, 1, 0, 0, 1 },
{ { 2886729728, 12 }, "Private-Use", 1, 1, 1, 0, 0 },
{ { 3221225472, 24 }, "IETF Protocol Assignments", 0, 0, 0, 0, 0 },
{ { 3221225472, 29 }, "IPv4 Service Continuity Prefix", 1, 1, 1, 0, 0 },
{ { 3221225480, 32 }, "IPv4 dummy address", 1, 0, 0, 0, 0 },
{ { 3221225481, 32 }, "Port Control Protocol Anycast", 1, 1, 1, 1, 0 },
{ { 3221225482, 32 }, "Traversal Using Relays around NAT Anycast", 1, 1, 1, 1, 0 },
{ { 3221225642, 32 }, "NAT64/DNS64 Discovery", 0, 0, 0, 0, 1 },
{ { 3221225643, 32 }, "NAT64/DNS64 Discovery", 0, 0, 0, 0, 1 },
{ { 3221225984, 24 }, "Documentation (TEST-NET-1)", 0, 0, 0, 0, 0 },
{ { 3223307264, 24 }, "AS112-v4", 1, 1, 1, 1, 0 },
{ { 3224682752, 24 }, "AMT", 1, 1, 1, 1, 0 },
{ { 3232235520, 16 }, "Private-Use", 1, 1, 1, 0, 0 },
{ { 3232706560, 24 }, "Direct Delegation AS112 Service", 1, 1, 1, 1, 0 },
{ { 3323068416, 15 }, "Benchmarking", 1, 1, 1, 0, 0 },
{ { 3325256704, 24 }, "Documentation (TEST-NET-2)", 0, 0, 0, 0, 0 },
{ { 3405803776, 24 }, "Documentation (TEST-NET-3)", 0, 0, 0, 0, 0 },
{ { 4026531840, 4 }, "Reserved", 0, 0, 0, 0, 1 },
{ { 4294967295, 32 }, "Limited Broadcast", 0, 1, 0, 0, 1 },
};
// GCC bug?
DIAG_OFF(missing-braces)
_U_ static const struct ws_ipv6_special_block __ipv6_special_block[] = {
{ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, 128 },
"Loopback Address", 0, 0, 0, 0, 1 },
{ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 128 },
"Unspecified Address", 1, 0, 0, 0, 1 },
{ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }, 96 },
"IPv4-mapped Address", 0, 0, 0, 0, 1 },
{ { { 0x00, 0x64, 0xff, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 96 },
"IPv4-IPv6 Translat.", 1, 1, 1, 1, 0 },
{ { { 0x00, 0x64, 0xff, 0x9b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 48 },
"IPv4-IPv6 Translat.", 1, 1, 1, 0, 0 },
{ { { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 64 },
"Discard-Only Address Block", 1, 1, 1, 0, 0 },
{ { { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 23 },
"IETF Protocol Assignments", -1, -1, -1, -1, 0 },
{ { { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 32 },
"TEREDO", 1, 1, 1, -1, 0 },
{ { { 0x20, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, 128 },
"Port Control Protocol Anycast", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }, 128 },
"Traversal Using Relays around NAT Anycast", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 48 },
"Benchmarking", 1, 1, 1, 0, 0 },
{ { { 0x20, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 32 },
"AMT", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x00, 0x04, 0x01, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 48 },
"AS112-v6", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 28 },
"ORCHIDv2", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 28 },
"Drone Remote ID Protocol Entity Tags (DETs) Prefix", 1, 1, 1, 1, 0 },
{ { { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 32 },
"Documentation", 0, 0, 0, 0, 0 },
{ { { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 16 },
"6to4", 1, 1, 1, -1, 0 },
{ { { 0x26, 0x20, 0x00, 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 48 },
"Direct Delegation AS112 Service", 1, 1, 1, 1, 0 },
{ { { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 7 },
"Unique-Local", 1, 1, 1, -1, 0 },
{ { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 10 },
"Link-Local Unicast", 1, 1, 0, 0, 1 },
};
DIAG_ON(missing-braces)

46
epan/iana-ip.c Normal file
View File

@ -0,0 +1,46 @@
/* iana-ip.c
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "iana-ip.h"
#include "iana-ip-data.c"
const struct ws_ipv4_special_block *
ws_ipv4_special_block_lookup(const ws_in4_addr *addr)
{
size_t count = G_N_ELEMENTS(__ipv4_special_block);
size_t i;
const struct ws_ipv4_special_block *ptr;
for (i = 0; i < count; i++) {
ptr = &__ipv4_special_block[i];
if (ws_ipv4_addr_and_mask_contains(&ptr->block, addr)) {
return ptr;
}
}
return NULL;
}
const struct ws_ipv6_special_block *
ws_ipv6_special_block_lookup(const ws_in6_addr *addr)
{
size_t count = G_N_ELEMENTS(__ipv6_special_block);
size_t i;
const struct ws_ipv6_special_block *ptr;
for (i = 0; i < count; i++) {
ptr = &__ipv6_special_block[i];
if (ws_ipv6_addr_and_prefix_contains(&ptr->block, addr)) {
return ptr;
}
}
return NULL;
}

46
epan/iana-ip.h Normal file
View File

@ -0,0 +1,46 @@
/* iana-ip.h
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __IANA_IP_H__
#define __IANA_IP_H__
#include <wireshark.h>
#include <ipv4.h>
#include <ipv6.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct ws_ipv4_special_block {
ipv4_addr_and_mask block;
const char *name;
/* true = 1; false = 0; n/a = -1 */
int source, destination, forwardable, global, reserved;
};
struct ws_ipv6_special_block {
ipv6_addr_and_prefix block;
const char *name;
/* true = 1; false = 0; n/a = -1 */
int source, destination, forwardable, global, reserved;
};
WS_DLL_PUBLIC
const struct ws_ipv4_special_block *
ws_ipv4_special_block_lookup(const ws_in4_addr *addr);
WS_DLL_PUBLIC
const struct ws_ipv6_special_block *
ws_ipv6_special_block_lookup(const ws_in6_addr *addr);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif

37
epan/ipv4.c Normal file
View File

@ -0,0 +1,37 @@
/* ipv4.c
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <epan/ipv4.h>
#include <epan/addr_and_mask.h>
static int
compare_network(const ipv4_addr_and_mask *a, const ipv4_addr_and_mask *b)
{
uint32_t addr_a, addr_b, nmask;
nmask = MIN(a->nmask, b->nmask);
addr_a = a->addr & nmask;
addr_b = b->addr & nmask;
if (addr_a < addr_b)
return -1;
if (addr_a > addr_b)
return 1;
return 0;
}
bool
ws_ipv4_addr_and_mask_contains(const ipv4_addr_and_mask *ipv4, const ws_in4_addr *in_addr)
{
ipv4_addr_and_mask addr_and_mask;
addr_and_mask.addr = g_ntohl(*in_addr);
addr_and_mask.nmask = ip_get_subnet_mask(32);
return compare_network(ipv4, &addr_and_mask) == 0;
}

View File

@ -16,6 +16,7 @@
#ifndef __IPV4_H__
#define __IPV4_H__
#include <wireshark.h>
#include <wsutil/inet_ipv4.h>
typedef struct {
@ -23,4 +24,8 @@ typedef struct {
guint32 nmask; /* stored in host order */
} ipv4_addr_and_mask;
WS_DLL_PUBLIC
bool
ws_ipv4_addr_and_mask_contains(const ipv4_addr_and_mask *ipv4, const ws_in4_addr *addr);
#endif

58
epan/ipv6.c Normal file
View File

@ -0,0 +1,58 @@
/* ipv6.c
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <epan/ipv6.h>
static const uint8_t bitmasks[9] =
{ 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
static int
compare_network(const ipv6_addr_and_prefix *a, const ipv6_addr_and_prefix *b)
{
uint32_t prefix;
int pos = 0;
prefix = MIN(a->prefix, b->prefix); /* MIN() like IPv4 */
prefix = MIN(prefix, 128); /* sanitize, max prefix is 128 */
while (prefix >= 8) {
int byte_a = (int) (a->addr.bytes[pos]);
int byte_b = (int) (b->addr.bytes[pos]);
if (byte_a != byte_b) {
return byte_a - byte_b;
}
prefix -= 8;
pos++;
}
if (prefix != 0) {
int byte_a = (int) (a->addr.bytes[pos] & (bitmasks[prefix]));
int byte_b = (int) (b->addr.bytes[pos] & (bitmasks[prefix]));
if (byte_a != byte_b) {
return byte_a - byte_b;
}
}
return 0;
}
bool
ws_ipv6_addr_and_prefix_contains(const ipv6_addr_and_prefix *ipv6, const ws_in6_addr *in_addr)
{
ipv6_addr_and_prefix addr_and_mask;
addr_and_mask.addr = *in_addr;
addr_and_mask.prefix = 128;
return compare_network(ipv6, &addr_and_mask) == 0;
}

View File

@ -17,6 +17,7 @@
#ifndef __IPV6_UTILS_H__
#define __IPV6_UTILS_H__
#include <wireshark.h>
#include <wsutil/inet_ipv6.h>
typedef struct {
@ -24,4 +25,8 @@ typedef struct {
guint32 prefix;
} ipv6_addr_and_prefix;
WS_DLL_PUBLIC
bool
ws_ipv6_addr_and_prefix_contains(const ipv6_addr_and_prefix *ipv6, const ws_in6_addr *addr);
#endif /* __IPV6_UTILS_H__ */

194
tools/make-iana-ip.py Normal file
View File

@ -0,0 +1,194 @@
#!/usr/bin/env python3
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
'''Update the IANA IP registry file.
Make-iana-ip creates a file containing information about IPv4/IPv6 allocation blocks.
'''
import csv
import html
import io
import ipaddress
import os
import re
import sys
import ctypes
import urllib.request, urllib.error, urllib.parse
def exit_msg(msg=None, status=1):
if msg is not None:
sys.stderr.write(msg + '\n\n')
sys.stderr.write(__doc__ + '\n')
sys.exit(status)
def open_url(url):
'''Open a URL.
Returns a tuple containing the body and response dict. The body is a
str in Python 3 and bytes in Python 2 in order to be compatibile with
csv.reader.
'''
if len(sys.argv) > 1:
url_path = os.path.join(sys.argv[1], url[1])
url_fd = open(url_path)
body = url_fd.read()
url_fd.close()
else:
url_path = '/'.join(url)
req_headers = { 'User-Agent': 'Wireshark iana-ip' }
try:
req = urllib.request.Request(url_path, headers=req_headers)
response = urllib.request.urlopen(req)
body = response.read().decode('UTF-8', 'replace')
except Exception:
exit_msg('Error opening ' + url_path)
return body
class IPv4SpecialBlock(ipaddress.IPv4Network):
def __str__(self):
addr = int(self.network_address)
mask = self.prefixlen
line = '{{ {:d}, {:d} }}'.format(addr, mask)
return line
class IPv6SpecialBlock(ipaddress.IPv6Network):
@staticmethod
def addr_c_array(byte_array):
if len(byte_array) != 16:
raise ValueError("Expected byte array of length 16")
c_array = ", ".join(f"0x{byte:02x}" for byte in byte_array)
return f"{{ {c_array} }}"
def __str__(self):
addr = self.network_address.packed
mask = self.prefixlen
line = '{{ {}, {} }}'.format(self.addr_c_array(addr), mask)
return line
class IPRegisty(list):
@staticmethod
def true_or_false(val):
if val == 'True':
return '1'
elif val == 'False':
return '0'
else:
return '-1'
def append(self, row):
ip, name, _, _, termin_date, source, destination, forward, glob, reserved = row
if termin_date[0].isdigit():
# skip allocations that have expired
return
name = re.sub(r'\[.*\]', '', name)
name = '"' + name.replace('"', '\\"') + '"'
source = self.true_or_false(source)
destination = self.true_or_false(destination)
forward = self.true_or_false(forward)
glob = self.true_or_false(glob)
reserved = self.true_or_false(reserved)
super().append([ip, name, source, destination, forward, glob, reserved])
class IPv4Registry(IPRegisty):
@staticmethod
def ipv4_addr_and_mask(s):
ip = IPv4SpecialBlock(s)
return ip
def append(self, row):
# some lines contain multiple (comma separated) blocks
ip_list = row[0].split(',')
for s in ip_list:
# remove annotations like "1.1.1.1 [2]"
ip_str = s.split()[0]
row = [self.ipv4_addr_and_mask(ip_str)] + row[1:]
super().append(row)
def dump(self, fd):
fd.write('_U_ static const struct ws_ipv4_special_block __ipv4_special_block[] = {\n')
for row in self:
line = ' {{ {}, {}, {}, {}, {}, {}, {} }},\n'.format(*row)
fd.write(line)
fd.write('};\n')
class IPv6Registry(IPRegisty):
@staticmethod
def ipv6_addr_and_mask(s):
ip_str = s.split()[0]
ip = IPv6SpecialBlock(ip_str)
return ip
def append(self, row):
# remove annotations like "1.1.1.1 [2]"
ip_str = row[0].split()[0]
row = [self.ipv6_addr_and_mask(ip_str)] + row[1:]
super().append(row)
def dump(self, fd):
fd.write('// GCC bug?\n')
fd.write('DIAG_OFF(missing-braces)\n')
fd.write('_U_ static const struct ws_ipv6_special_block __ipv6_special_block[] = {\n')
for row in self:
line = \
''' {{ {},
{}, {}, {}, {}, {}, {} }},\n'''.format(*row)
fd.write(line)
fd.write('};\n')
fd.write('DIAG_ON(missing-braces)\n')
IANA_URLS = {
'IPv4': { 'url': ["https://www.iana.org/assignments/iana-ipv4-special-registry/", "iana-ipv4-special-registry-1.csv"], 'min_entries': 2 },
'IPv6': { 'url': ["https://www.iana.org/assignments/iana-ipv6-special-registry/", "iana-ipv6-special-registry-1.csv"], 'min_entries': 2 },
}
def dump_registry(db, fd, reg):
db_url = IANA_URLS[db]['url']
print('Loading {} data from {}'.format(db, db_url))
body = open_url(db_url)
iana_csv = csv.reader(body.splitlines())
# Pop the title row.
next(iana_csv)
for iana_row in iana_csv:
# Address Block,Name,RFC,Allocation Date,Termination Date,Source,Destination,Forwardable,Globally Reachable,Reserved-by-Protocol
# ::1/128,Loopback Address,[RFC4291],2006-02,N/A,False,False,False,False,True
reg.append(iana_row)
if len(reg) < IANA_URLS[db]['min_entries']:
exit_msg("Too few {} entries. Got {}, wanted {}".format(db, len(reg), IANA_URLS[db]['min_entries']))
reg.dump(fd)
def main():
this_dir = os.path.dirname(__file__)
iana_path = os.path.join('epan', 'iana-ip-data.c')
try:
fd = io.open(iana_path, 'w', encoding='UTF-8')
except Exception:
exit_msg("Couldn't open \"{}\" file for reading".format(iana_path))
fd.write('''/*
* This file was generated by running ./tools/make-iana-ip.py.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "iana-ip.h"
''')
dump_registry('IPv4', fd, IPv4Registry())
fd.write('\n')
dump_registry('IPv6', fd, IPv6Registry())
fd.close()
if __name__ == '__main__':
main()