3

With iproute2 on Linux, gre/gretap interfaces are setup with ip link add, which for GRE needs remote and optionally local attributes specifying the endpoints. However, I can't find any way to read back these attributes. ip link show doesn't show them, and they don't seem to be represented anywhere in the data structures returned by the C function getifaddrs (e.g. if I wanted to implement my own tool to get them). I'm working with an environment where there are a very large number of such interfaces, and I need to be able to find the one associated with a particular remote address. Is there any way to do that without storing the mapping somewhere out-of-band?

1 Answers1

4

To get all information from an interface, you can use ip's -details option:

-d, -details

    Output more detailed information.

# ip link add name gretaptest type gretap remote 192.0.2.1 local 192.0.2.2
# ip -details link show dev gretaptest
49: gretaptest@NONE: <BROADCAST,MULTICAST> mtu 1462 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 5e:1d:10:f9:05:f9 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 68 maxmtu 0 
    gretap remote 192.0.2.1 local 192.0.2.2 ttl inherit erspan_ver 0 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 

If you want to use this in scripts, you really should use -json along the jq tool. Very basic example (without any check):

# ip -json -details link show dev gretaptest | jq '.[].linkinfo.info_data | .remote, .local'
"192.0.2.1"
"192.0.2.2"

If you want to implement this directly in your tools using available system calls and functions, you should start by taking a look there:

  • rtnetlink (7) - Linux IPv4 routing socket (it's a lot more than just IPv4)

    for interfaces it's probably RTM_GETLINK

  • rtnetlink (3) - macros to manipulate rtnetlink messages

You can run strace on the ipcommand to just see the kind of system calls and structures in use.

Some documentation found around:

networking:generic_netlink_howto

There are higher libraries built on top of this such as libnl too.

A.B
  • 9,037
  • 2
  • 19
  • 37
  • also added a few links for a direct implementation (it doesn't look easy considering all the glue code) – A.B Nov 27 '20 at 01:29
  • 1
    Also for multiple interfaces, if sticking to json, have a look at a very similar answer of mine on UL SE where there are better json examples: https://unix.stackexchange.com/questions/616157/iproute2-how-to-display-the-type-of-a-network-devices/616162#616162 – A.B Nov 27 '20 at 01:39
  • Thanks - this is perfect! – R.. GitHub STOP HELPING ICE Nov 27 '20 at 02:55