summaryrefslogtreecommitdiffstats
path: root/epan/manuf.c
blob: 94adbf5b85694f42f6e29fea5d6a7cb040649adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* manuf.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "manuf.h"
#include <stdlib.h>

// MA-L / OUI - MAC Address Block Large (24-bit prefix)
#define MA_L 0
// MA-M - MAC Address Block Medium (28-bit prefix)
#define MA_M 1
// MA-S / OUI-36 - MAC Address Block Small (36-bit prefix)
#define MA_S 2

typedef struct {
    uint8_t oui24[3];
    /* Identifies the 3-byte prefix as part of MA-M or MA-S (or MA-L if none of those). */
    uint8_t kind;
} manuf_registry_t;

typedef struct {
    uint8_t oui24[3];
    const char *short_name;
    const char *long_name;
} manuf_oui24_t;

typedef struct {
    uint8_t oui28[4];
    const char *short_name;
    const char *long_name;
} manuf_oui28_t;

typedef struct {
    uint8_t oui36[5];
    const char *short_name;
    const char *long_name;
} manuf_oui36_t;

#include "manuf-data.c"

static int
compare_oui24_registry(const void *key, const void *element)
{
    const uint8_t *addr = (const uint8_t *)key;
    const manuf_registry_t *entry = (const manuf_registry_t *)element;

    return memcmp(addr, entry->oui24, 3);
}

static int
compare_oui24_entry(const void *key, const void *element)
{
    const uint8_t *addr = (const uint8_t *)key;
    const manuf_oui24_t *oui = (const manuf_oui24_t *)element;

    return memcmp(addr, oui->oui24, 3);
}

static int
compare_oui28_entry(const void *key, const void *element)
{
    const uint8_t *addr = (const uint8_t *)key;
    const manuf_oui28_t *oui = (const manuf_oui28_t *)element;

    // The caller is expected to have masked out (addr[3] & 0xF0).
    return memcmp(addr, oui->oui28, 4);
}

static int
compare_oui36_entry(const void *key, const void *element)
{
    const uint8_t *addr = (const uint8_t *)key;
    const manuf_oui36_t *oui = (const manuf_oui36_t *)element;

    // The caller is expected to have masked out (addr[4] & 0xF0).
    return memcmp(addr, oui->oui36, 5);
}

static int
select_registry(const uint8_t addr[6])
{
    const manuf_registry_t *entry;

    entry = bsearch(addr, ieee_registry_table, G_N_ELEMENTS(ieee_registry_table), sizeof(manuf_registry_t), compare_oui24_registry);
    if (entry)
        return entry->kind;
    return MA_L;
}

static const manuf_oui24_t *
manuf_oui24_lookup(const uint8_t addr[6])
{
    return bsearch(addr, global_manuf_oui24_table,
                    G_N_ELEMENTS(global_manuf_oui24_table),
                    sizeof(manuf_oui24_t),
                    compare_oui24_entry);
}

static const manuf_oui28_t *
manuf_oui28_lookup(const uint8_t addr[6])
{
    const uint8_t addr28[6] = { addr[0], addr[1], addr[2], addr[3] & 0xF0, };
    return bsearch(addr28, global_manuf_oui28_table,
                    G_N_ELEMENTS(global_manuf_oui28_table),
                    sizeof(manuf_oui28_t),
                    compare_oui28_entry);
}

static const manuf_oui36_t *
manuf_oui36_lookup(const uint8_t addr[6])
{
    const uint8_t addr36[6] = { addr[0], addr[1], addr[2], addr[3], addr[4] & 0xF0, };
    return bsearch(addr36, global_manuf_oui36_table,
                    G_N_ELEMENTS(global_manuf_oui36_table),
                    sizeof(manuf_oui36_t),
                    compare_oui36_entry);
}

const char *
ws_manuf_lookup_str(const uint8_t addr[6], const char **long_name_ptr)
{
    uint8_t addr_copy[6];
    memcpy(addr_copy, addr, 6);
    /* Mask out the broadcast/multicast flag */
    addr_copy[0] &= 0xFE;

    const char *short_name = NULL, *long_name = NULL;

    switch (select_registry(addr_copy)) {
        case MA_L:
        {
            const manuf_oui24_t *ptr = manuf_oui24_lookup(addr_copy);
            if (ptr) {
                short_name = ptr->short_name;
                long_name = ptr->long_name;
            }
            break;
        }
        case MA_M:
        {
            const manuf_oui28_t *ptr = manuf_oui28_lookup(addr_copy);
            if (ptr) {
                short_name = ptr->short_name;
                long_name = ptr->long_name;
            }
            break;
        }
        case MA_S:
        {
            const manuf_oui36_t *ptr = manuf_oui36_lookup(addr_copy);
            if (ptr) {
                short_name = ptr->short_name;
                long_name = ptr->long_name;
            }
            break;
        }
        default:
            ws_assert_not_reached();
    }

    if (long_name_ptr) {
        *long_name_ptr = long_name;
    }
    return short_name;
}

static inline struct ws_manuf *
copy_oui24(struct ws_manuf *dst, const manuf_oui24_t *src)
{
    memcpy(dst->block, src->oui24, sizeof(src->oui24));
    dst->block[3] = 0;
    dst->block[4] = 0;
    dst->mask = 24;
    dst->short_name = src->short_name;
    dst->long_name = src->long_name;
    return dst;
}

static inline struct ws_manuf *
copy_oui28(struct ws_manuf *dst, const manuf_oui28_t *src)
{
    memcpy(dst->block, src->oui28, sizeof(src->oui28));
    dst->block[4] = 0;
    dst->mask = 28;
    dst->short_name = src->short_name;
    dst->long_name = src->long_name;
    return dst;
}

static inline struct ws_manuf *
copy_oui36(struct ws_manuf *dst, const manuf_oui36_t *src)
{
    memcpy(dst->block, src->oui36, sizeof(src->oui36));
    dst->mask = 36;
    dst->short_name = src->short_name;
    dst->long_name = src->long_name;
    return dst;
}

void
ws_manuf_iter_init(ws_manuf_iter_t *iter)
{
    iter->idx24 = 0;
    copy_oui24(&iter->buf24, &global_manuf_oui24_table[iter->idx24]);
    iter->idx28 = 0;
    copy_oui28(&iter->buf28, &global_manuf_oui28_table[iter->idx28]);
    iter->idx36 = 0;
    copy_oui36(&iter->buf36, &global_manuf_oui36_table[iter->idx36]);
}

/**
 * Iterate between 3 registries in ascending order. This is not the same as
 * fully iterating through one registry followed by another. For example, after
 * visiting "00:55:B1", it could go to  "00:55:DA:00/28", and eventually end up
 * at "00:56:2B" again.
 *
 * The "iter" structure must be zero initialized before the first iteration.
 */
bool
ws_manuf_iter_next(ws_manuf_iter_t *iter, struct ws_manuf *result)
{
    struct ws_manuf *vector[3] = { NULL, NULL, NULL };
    size_t idx = 0;
    struct ws_manuf *ptr;

    /* Read current positions. */
    if (iter->idx24 < G_N_ELEMENTS(global_manuf_oui24_table)) {
        vector[idx++] = &iter->buf24;
    }
    if (iter->idx28 < G_N_ELEMENTS(global_manuf_oui28_table)) {
        vector[idx++] = &iter->buf28;
    }
    if (iter->idx36 < G_N_ELEMENTS(global_manuf_oui36_table)) {
        vector[idx++] = &iter->buf36;
    }

    /* None remaining, we're done. */
    if (idx == 0)
        return false;

    /* Select smallest current prefix out of the 3 registries.
     * There is at least one entry and index 0 is non-empty. */
    ptr = vector[0];
    for (size_t i = 1; i < idx; i++) {
        if (vector[i] && memcmp(vector[i]->block, ptr->block, MANUF_BLOCK_SIZE) < 0) {
            ptr = vector[i];
        }
    }

    /* We have the next smallest element, return result. */
    memcpy(result, ptr, sizeof(struct ws_manuf));

    /* Advance iterator and copy new element. */
    if (ptr->mask == 24) {
        iter->idx24++;
        if (iter->idx24 < G_N_ELEMENTS(global_manuf_oui24_table)) {
            copy_oui24(&iter->buf24, &global_manuf_oui24_table[iter->idx24]);
        }
    }
    else if (ptr->mask == 28) {
        iter->idx28++;
        if (iter->idx28 < G_N_ELEMENTS(global_manuf_oui28_table)) {
            copy_oui28(&iter->buf28, &global_manuf_oui28_table[iter->idx28]);
        }
    }
    else if (ptr->mask == 36) {
        iter->idx36++;
        if (iter->idx36 < G_N_ELEMENTS(global_manuf_oui36_table)) {
            copy_oui36(&iter->buf36, &global_manuf_oui36_table[iter->idx36]);
        }
    }
    else
        ws_assert_not_reached();

    return true;
}

const char *
ws_manuf_block_str(char *buf, size_t buf_size, const struct ws_manuf *ptr)
{
    if (ptr->mask == 24) {
        /* The mask is implied as the full 24 bits when printing a traditional OUI.*/
        snprintf(buf, buf_size, "%02"PRIX8":%02"PRIX8":%02"PRIX8,
            ptr->block[0], ptr->block[1], ptr->block[2]);
    }
    else if (ptr->mask == 28) {
        snprintf(buf, buf_size, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8"/28",
            ptr->block[0], ptr->block[1], ptr->block[2], ptr->block[3]);
    }
    else if (ptr->mask == 36) {
        snprintf(buf, buf_size, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8"/36",
            ptr->block[0], ptr->block[1], ptr->block[2], ptr->block[3], ptr->block[4]);
    }
    else {
        ws_assert_not_reached();
    }

    return buf;
}

void
ws_manuf_dump(FILE *fp)
{
    ws_manuf_iter_t iter;
    struct ws_manuf item;
    char strbuf[64];

    ws_manuf_iter_init(&iter);

    while (ws_manuf_iter_next(&iter, &item)) {
        fprintf(fp, "%-17s\t%-12s\t%s\n",
            ws_manuf_block_str(strbuf, sizeof(strbuf), &item),
            item.short_name,
            item.long_name);
    }
}

size_t
ws_manuf_count(void)
{
    return G_N_ELEMENTS(global_manuf_oui24_table) +
            G_N_ELEMENTS(global_manuf_oui28_table) +
            G_N_ELEMENTS(global_manuf_oui36_table);
}