24 #include <netlink-private/netlink.h>
25 #include <netlink/netlink.h>
26 #include <netlink/utils.h>
27 #include <netlink/addr.h>
28 #include <netlink-private/route/mpls.h>
29 #include <linux/socket.h>
33 static inline uint16_t dn_ntohs(uint16_t addr)
42 return ((uint16_t) u.byte[0]) | (((uint16_t) u.byte[1]) << 8);
45 static inline int do_digit(
char *str, uint16_t *addr, uint16_t scale,
46 size_t *pos,
size_t len,
int *started)
48 uint16_t tmp = *addr / scale;
53 if (((tmp) > 0) || *started || (scale == 1)) {
57 *addr -= (tmp * scale);
63 static const char *dnet_ntop(
const char *addrbuf,
size_t addrlen,
char *str,
66 uint16_t addr = dn_ntohs(*(uint16_t *)addrbuf);
67 uint16_t area = addr >> 10;
79 if (do_digit(str + pos, &area, 10, &pos, len, &started))
82 if (do_digit(str + pos, &area, 1, &pos, len, &started))
92 if (do_digit(str + pos, &addr, 1000, &pos, len, &started))
95 if (do_digit(str + pos, &addr, 100, &pos, len, &started))
98 if (do_digit(str + pos, &addr, 10, &pos, len, &started))
101 if (do_digit(str + pos, &addr, 1, &pos, len, &started))
112 static int dnet_num(
const char *src, uint16_t * dst)
118 while ((tmp = *src++) != 0) {
120 if ((tmp < 0) || (tmp > 9))
131 static inline int dnet_pton(
const char *src,
char *addrbuf)
137 pos = dnet_num(src, &area);
138 if ((pos == 0) || (area > 63) ||
139 ((*(src + pos) !=
'.') && (*(src + pos) !=
',')))
142 pos = dnet_num(src + pos + 1, &node);
143 if ((pos == 0) || (node > 1023))
146 *(uint16_t *)addrbuf = dn_ntohs((area << 10) | node);
151 static void addr_destroy(
struct nl_addr *addr)
156 if (addr->a_refcnt != 1)
182 struct nl_addr *addr;
184 addr = calloc(1,
sizeof(*addr) + maxsize);
189 addr->a_maxsize = maxsize;
213 struct nl_addr *addr;
219 addr->a_family = family;
223 addr->a_prefixlen = 20;
226 addr->a_prefixlen = size*8;
230 memcpy(addr->a_addr, buf, size);
294 int err, copy = 0, len = 0, family = AF_UNSPEC, plen = 0;
295 char *str, *prefix = NULL, buf[256];
296 struct nl_addr *addr = NULL;
298 str = strdup(addrstr);
304 if (hint != AF_MPLS) {
305 prefix = strchr(str,
'/');
310 if (!strcasecmp(str,
"none")) {
315 if (!strcasecmp(str,
"default") ||
316 !strcasecmp(str,
"all") ||
317 !strcasecmp(str,
"any")) {
339 err = -NLE_AF_NOSUPPORT;
346 if (hint == AF_INET || hint == AF_UNSPEC) {
347 if (inet_pton(AF_INET, str, buf) > 0) {
352 if (hint == AF_INET) {
358 if (hint == AF_INET6 || hint == AF_UNSPEC) {
359 if (inet_pton(AF_INET6, str, buf) > 0) {
364 if (hint == AF_INET6) {
370 if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str,
':')) {
371 unsigned int a, b, c, d, e, f;
373 if (sscanf(str,
"%02x:%02x:%02x:%02x:%02x:%02x",
374 &a, &b, &c, &d, &e, &f) == 6) {
377 buf[0] = (
unsigned char) a;
378 buf[1] = (
unsigned char) b;
379 buf[2] = (
unsigned char) c;
380 buf[3] = (
unsigned char) d;
381 buf[4] = (
unsigned char) e;
382 buf[5] = (
unsigned char) f;
386 if (hint == AF_LLC) {
392 if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
393 (strchr(str,
'.') || strchr(str,
','))) {
394 if (dnet_pton(str, buf) > 0) {
399 if (hint == AF_DECnet) {
405 if (hint == AF_MPLS) {
406 len = mpls_pton(AF_MPLS, str, buf,
sizeof(buf));
416 if (hint == AF_UNSPEC && strchr(str,
':')) {
420 long l = strtol(s, &p, 16);
422 if (s == p || l > 0xff || i >=
sizeof(buf)) {
427 buf[i++] = (
unsigned char) l;
455 long pl = strtol(++prefix, &p, 0);
491 new =
nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
493 new->a_prefixlen = addr->a_prefixlen;
538 if (addr->a_refcnt == 1)
552 return addr->a_refcnt > 1;
590 d = a->a_family - b->a_family;
592 d = a->a_len - b->a_len;
594 if (a->a_len && d == 0) {
595 d = memcmp(a->a_addr, b->a_addr, a->a_len);
598 return (a->a_prefixlen - b->a_prefixlen);
620 int d = a->a_family - b->a_family;
623 int len = min(a->a_prefixlen, b->a_prefixlen);
626 d = memcmp(a->a_addr, b->a_addr, bytes);
627 if (d == 0 && (len % 8) != 0) {
628 int mask = (0xFF00 >> (len % 8)) & 0xFF;
630 d = (a->a_addr[bytes] & mask) -
631 (b->a_addr[bytes] & mask);
648 for (i = 0; i < addr->a_len; i++)
671 ret = inet_pton(family, addr, buf);
677 ret = mpls_pton(family, addr, buf,
sizeof(buf));
683 ret = dnet_pton(addr, buf);
689 if (sscanf(addr,
"%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
705 switch (addr->a_len) {
733 switch (addr->a_family) {
735 struct sockaddr_in *sai = (
struct sockaddr_in *) sa;
737 if (*salen <
sizeof(*sai))
740 if (addr->a_len == 4)
741 memcpy(&sai->sin_addr, addr->a_addr, 4);
742 else if (addr->a_len != 0)
745 memset(&sai->sin_addr, 0, 4);
747 sai->sin_family = addr->a_family;
748 *salen =
sizeof(*sai);
753 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6 *) sa;
755 if (*salen <
sizeof(*sa6))
758 if (addr->a_len == 16)
759 memcpy(&sa6->sin6_addr, addr->a_addr, 16);
760 else if (addr->a_len != 0)
763 memset(&sa6->sin6_addr, 0, 16);
765 sa6->sin6_family = addr->a_family;
766 *salen =
sizeof(*sa6);
801 char buf[INET6_ADDRSTRLEN+5];
802 struct addrinfo hint = {
803 .ai_flags = AI_NUMERICHOST,
804 .ai_family = addr->a_family,
809 err = getaddrinfo(buf, NULL, &hint, result);
812 case EAI_ADDRFAMILY:
return -NLE_AF_NOSUPPORT;
813 case EAI_AGAIN:
return -NLE_AGAIN;
814 case EAI_BADFLAGS:
return -NLE_INVAL;
815 case EAI_FAIL:
return -NLE_NOADDR;
816 case EAI_FAMILY:
return -NLE_AF_NOSUPPORT;
817 case EAI_MEMORY:
return -NLE_NOMEM;
818 case EAI_NODATA:
return -NLE_NOADDR;
819 case EAI_NONAME:
return -NLE_OBJ_NOTFOUND;
820 case EAI_SERVICE:
return -NLE_OPNOTSUPP;
821 case EAI_SOCKTYPE:
return -NLE_BAD_SOCK;
822 default:
return -NLE_FAILURE;
845 struct sockaddr_in6 buf;
846 socklen_t salen =
sizeof(buf);
852 err = getnameinfo((
struct sockaddr *) &buf, salen, host, hostlen,
853 NULL, 0, NI_NAMEREQD);
855 return nl_syserr2nlerr(err);
876 addr->a_family = family;
889 return addr->a_family;
914 if (len > addr->a_maxsize)
918 memset(addr->a_addr, 0, addr->a_maxsize);
921 memcpy(addr->a_addr, buf, len);
937 return (
void*)addr->a_addr;
961 addr->a_prefixlen = prefixlen;
972 return addr->a_prefixlen;
993 char *
nl_addr2str(
const struct nl_addr *addr,
char *buf,
size_t size)
998 if (!addr || !addr->a_len) {
999 snprintf(buf, size,
"none");
1006 switch (addr->a_family) {
1008 inet_ntop(AF_INET, addr->a_addr, buf, size);
1012 inet_ntop(AF_INET6, addr->a_addr, buf, size);
1016 mpls_ntop(AF_MPLS, addr->a_addr, buf, size);
1020 dnet_ntop(addr->a_addr, addr->a_len, buf, size);
1025 snprintf(buf, size,
"%02x",
1026 (
unsigned char) addr->a_addr[0]);
1027 for (i = 1; i < addr->a_len; i++) {
1028 snprintf(tmp,
sizeof(tmp),
":%02x",
1029 (
unsigned char) addr->a_addr[i]);
1030 strncat(buf, tmp, size - strlen(buf) - 1);
1036 if (addr->a_family != AF_MPLS &&
1037 addr->a_prefixlen != (8 * addr->a_len)) {
1038 snprintf(tmp,
sizeof(tmp),
"/%u", addr->a_prefixlen);
1039 strncat(buf, tmp, size - strlen(buf) - 1);
1052 static const struct trans_tbl afs[] = {
1053 __ADD(AF_UNSPEC,unspec),
1054 __ADD(AF_UNIX,unix),
1055 __ADD(AF_INET,inet),
1056 __ADD(AF_AX25,ax25),
1058 __ADD(AF_APPLETALK,appletalk),
1059 __ADD(AF_NETROM,netrom),
1060 __ADD(AF_BRIDGE,bridge),
1061 __ADD(AF_ATMPVC,atmpvc),
1063 __ADD(AF_INET6,inet6),
1064 __ADD(AF_ROSE,rose),
1065 __ADD(AF_DECnet,decnet),
1066 __ADD(AF_NETBEUI,netbeui),
1067 __ADD(AF_SECURITY,security),
1069 __ADD(AF_NETLINK,netlink),
1070 __ADD(AF_PACKET,packet),
1072 __ADD(AF_ECONET,econet),
1073 __ADD(AF_ATMSVC,atmsvc),
1078 __ADD(AF_IRDA,irda),
1079 __ADD(AF_PPPOX,pppox),
1080 __ADD(AF_WANPIPE,wanpipe),
1086 __ADD(AF_TIPC,tipc),
1088 __ADD(AF_BLUETOOTH,bluetooth),
1090 __ADD(AF_IUCV,iucv),
1093 __ADD(AF_RXRPC,rxrpc),
1096 __ADD(AF_ISDN,isdn),
1099 __ADD(AF_PHONET,phonet),
1101 #ifdef AF_IEEE802154
1102 __ADD(AF_IEEE802154,ieee802154),
1105 __ADD(AF_CAIF,caif),
1114 __ADD(AF_VSOCK,vsock),
1116 __ADD(AF_MPLS,mpls),
1119 char *nl_af2str(
int family,
char *buf,
size_t size)
1121 return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
1124 int nl_str2af(
const char *name)
1126 int fam = __str2type(name, afs, ARRAY_SIZE(afs));
1127 return fam >= 0 ? fam : -EINVAL;
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
int nl_addr_info(const struct nl_addr *addr, struct addrinfo **result)
Call getaddrinfo() for an abstract address object.
int nl_addr_iszero(const struct nl_addr *addr)
Returns true if the address consists of all zeros.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
int nl_addr_fill_sockaddr(const struct nl_addr *addr, struct sockaddr *sa, socklen_t *salen)
Fill out sockaddr structure with values from abstract address object.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nl_addr_resolve(const struct nl_addr *addr, char *host, size_t hostlen)
Resolve abstract address object to a name using getnameinfo().
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
int nl_addr_parse(const char *addrstr, int hint, struct nl_addr **result)
Allocate abstract address based on character string.
int nl_addr_set_binary_addr(struct nl_addr *addr, const void *buf, size_t len)
Set binary address of abstract address object.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
int nl_addr_cmp_prefix(const struct nl_addr *a, const struct nl_addr *b)
Compare the prefix of two abstract addresses.
int nl_addr_valid(const char *addr, int family)
Check if address string is parseable for a specific address family.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
int nl_addr_shared(const struct nl_addr *addr)
Check whether an abstract address is shared.
int nl_addr_guess_family(const struct nl_addr *addr)
Guess address family of abstract address based on address size.
int nla_len(const struct nlattr *nla)
Return length of the payload .
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.