mirror_iproute2/lib/mpls_ntop.c
David Ahern aac40403ea ip: mpls: fix printing of mpls labels
If the kernel returns more labels than iproute2 expects, none of
the labels are printed and (null) is shown instead:
    $ ip -f mpls ro ls
    101 as to (null) via inet 172.16.2.2 dev virt12
    201 as to 202/203 via inet6 2001:db8:2::2 dev virt12

Remove the use of MPLS_MAX_LABELS and rely on buffer length that is
passed to mpls_ntop. With this change ip can print the label stack
returned by the kernel up to 255 characters (limit is due to size of
buf passed in) which amounts to 31 labels with a separator.

With this change the above is:
    $ ip/ip -f mpls ro ls
    101 as to 102/103/104/105/106/107/108/109/110 via inet 172.16.2.2 dev virt12

Signed-off-by: David Ahern <dsahern@gmail.com>
Reviewed-by: Simon Horman <simon.horman@netronome.com>
2017-05-11 11:08:02 -07:00

51 lines
934 B
C

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <linux/mpls.h>
#include "utils.h"
static const char *mpls_ntop1(const struct mpls_label *addr, char *buf, size_t buflen)
{
size_t destlen = buflen;
char *dest = buf;
int count = 0;
while (1) {
uint32_t entry = ntohl(addr[count++].entry);
uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
int len = snprintf(dest, destlen, "%u", label);
if (len >= destlen)
break;
/* Is this the end? */
if (entry & MPLS_LS_S_MASK)
return buf;
dest += len;
destlen -= len;
if (destlen) {
*dest = '/';
dest++;
destlen--;
}
}
errno = -E2BIG;
return NULL;
}
const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen)
{
switch(af) {
case AF_MPLS:
errno = 0;
return mpls_ntop1((struct mpls_label *)addr, buf, buflen);
default:
errno = EAFNOSUPPORT;
}
return NULL;
}