| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Adapted from mpls_ntop and mpls_pton copied from iproute2, |
| 4 |
* lib/mpls_ntop.c and lib/mpls_pton.c |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "nl-default.h" |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
#include <linux/mpls.h> |
| 12 |
|
| 13 |
#include <netlink/netlink-compat.h> |
| 14 |
|
| 15 |
#include "mpls.h" |
| 16 |
|
| 17 |
static const char *mpls_ntop1(const struct mpls_label *addr, |
| 18 |
char *buf, size_t buflen) |
| 19 |
{ |
| 20 |
size_t destlen = buflen; |
| 21 |
char *dest = buf; |
| 22 |
int count = 0; |
| 23 |
|
| 24 |
while (1) { |
| 25 |
uint32_t entry = ntohl(addr[count++].entry); |
| 26 |
uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; |
| 27 |
int len = snprintf(dest, destlen, "%u", label); |
| 28 |
|
| 29 |
if (len < 0 || (unsigned)len >= destlen) |
| 30 |
break; |
| 31 |
|
| 32 |
/* Is this the end? */ |
| 33 |
if (entry & MPLS_LS_S_MASK) |
| 34 |
return buf; |
| 35 |
|
| 36 |
dest += len; |
| 37 |
destlen -= len; |
| 38 |
if (destlen) { |
| 39 |
*dest = '/'; |
| 40 |
dest++; |
| 41 |
destlen--; |
| 42 |
} |
| 43 |
} |
| 44 |
errno = E2BIG; |
| 45 |
|
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen) |
| 50 |
{ |
| 51 |
switch(af) { |
| 52 |
case AF_MPLS: |
| 53 |
errno = 0; |
| 54 |
return mpls_ntop1((struct mpls_label *)addr, buf, buflen); |
| 55 |
} |
| 56 |
|
| 57 |
errno = EINVAL; |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
static int mpls_pton1(const char *name, struct mpls_label *addr, |
| 62 |
unsigned int maxlabels) |
| 63 |
{ |
| 64 |
char *endp; |
| 65 |
unsigned count; |
| 66 |
|
| 67 |
for (count = 0; count < maxlabels; count++) { |
| 68 |
unsigned long label; |
| 69 |
|
| 70 |
label = strtoul(name, &endp, 0); |
| 71 |
/* Fail when the label value is out or range */ |
| 72 |
if (label >= (1 << 20)) |
| 73 |
return 0; |
| 74 |
|
| 75 |
if (endp == name) /* no digits */ |
| 76 |
return 0; |
| 77 |
|
| 78 |
addr->entry = htonl(label << MPLS_LS_LABEL_SHIFT); |
| 79 |
if (*endp == '\0') { |
| 80 |
addr->entry |= htonl(1 << MPLS_LS_S_SHIFT); |
| 81 |
return (count + 1) * sizeof(struct mpls_label); |
| 82 |
} |
| 83 |
|
| 84 |
/* Bad character in the address */ |
| 85 |
if (*endp != '/') |
| 86 |
return 0; |
| 87 |
|
| 88 |
name = endp + 1; |
| 89 |
addr += 1; |
| 90 |
} |
| 91 |
|
| 92 |
/* The address was too long */ |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
int mpls_pton(int af, const char *src, void *addr, size_t alen) |
| 97 |
{ |
| 98 |
unsigned int maxlabels = alen / sizeof(struct mpls_label); |
| 99 |
int err; |
| 100 |
|
| 101 |
switch(af) { |
| 102 |
case AF_MPLS: |
| 103 |
errno = 0; |
| 104 |
err = mpls_pton1(src, (struct mpls_label *)addr, maxlabels); |
| 105 |
break; |
| 106 |
default: |
| 107 |
errno = EAFNOSUPPORT; |
| 108 |
err = -1; |
| 109 |
} |
| 110 |
|
| 111 |
return err; |
| 112 |
} |