| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* ll_addr.c |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/ioctl.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <linux/netdevice.h> |
| 19 |
#include <linux/if_arp.h> |
| 20 |
#include <linux/sockios.h> |
| 21 |
|
| 22 |
#include "rt_names.h" |
| 23 |
#include "utils.h" |
| 24 |
|
| 25 |
const char *ll_addr_n2a(const unsigned char *addr, int alen, int type, |
| 26 |
char *buf, int blen) |
| 27 |
{ |
| 28 |
int i; |
| 29 |
int l; |
| 30 |
|
| 31 |
if (alen == 4 && |
| 32 |
(type == ARPHRD_TUNNEL || type == ARPHRD_SIT |
| 33 |
|| type == ARPHRD_IPGRE)) |
| 34 |
return inet_ntop(AF_INET, addr, buf, blen); |
| 35 |
|
| 36 |
if (alen == 16 && (type == ARPHRD_TUNNEL6 || type == ARPHRD_IP6GRE)) |
| 37 |
return inet_ntop(AF_INET6, addr, buf, blen); |
| 38 |
if (alen == 7 && type == ARPHRD_AX25) |
| 39 |
return ax25_ntop(AF_AX25, addr, buf, blen); |
| 40 |
if (alen == 7 && type == ARPHRD_NETROM) |
| 41 |
return netrom_ntop(AF_NETROM, addr, buf, blen); |
| 42 |
if (alen == 5 && type == ARPHRD_ROSE) |
| 43 |
return rose_ntop(AF_ROSE, addr, buf, blen); |
| 44 |
|
| 45 |
snprintf(buf, blen, "%02x", addr[0]); |
| 46 |
for (i = 1, l = 2; i < alen && l < blen; i++, l += 3) |
| 47 |
snprintf(buf + l, blen - l, ":%02x", addr[i]); |
| 48 |
return buf; |
| 49 |
} |
| 50 |
|
| 51 |
/*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */ |
| 52 |
int ll_addr_a2n(char *lladdr, int len, const char *arg) |
| 53 |
{ |
| 54 |
if (strchr(arg, '.')) { |
| 55 |
inet_prefix pfx; |
| 56 |
if (get_addr_1(&pfx, arg, AF_INET)) { |
| 57 |
fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
if (len < 4) |
| 61 |
return -1; |
| 62 |
memcpy(lladdr, pfx.data, 4); |
| 63 |
return 4; |
| 64 |
} else { |
| 65 |
int i; |
| 66 |
|
| 67 |
for (i = 0; i < len; i++) { |
| 68 |
int temp; |
| 69 |
char *cp = strchr(arg, ':'); |
| 70 |
if (cp) { |
| 71 |
*cp = 0; |
| 72 |
cp++; |
| 73 |
} |
| 74 |
if (sscanf(arg, "%x", &temp) != 1) { |
| 75 |
fprintf(stderr, "\"%s\" is invalid lladdr.\n", |
| 76 |
arg); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
if (temp < 0 || temp > 255) { |
| 80 |
fprintf(stderr, "\"%s\" is invalid lladdr.\n", |
| 81 |
arg); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
lladdr[i] = temp; |
| 85 |
if (!cp) |
| 86 |
break; |
| 87 |
arg = cp; |
| 88 |
} |
| 89 |
return i + 1; |
| 90 |
} |
| 91 |
} |