7

On Linux, using C, how can I find the find the name of the bridge device my ethernet interface is attached to? Is there a sequence of ioctl() calls I need to make to find the master bridge device?

My C program knows the device name of my TAP adapter from a configuration file (in this case, tap0). Ultimately, I need the IP address that my TAP adapter responds to. Because it is bridged, the TAP adapter does not have an IP address; it is the bridge device that has the IP address.

I have a TAP device and VETH device bridged together. ip a shows the following:

1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: tap0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast master br0 state DOWN group default qlen 500
    link/ether 22:d4:fa:a4:89:81 brd ff:ff:ff:ff:ff:ff
3: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 22:8c:ee:b8:e3:30 brd ff:ff:ff:ff:ff:ff
    inet 10.20.30.40/24 scope global br0
       valid_lft forever preferred_lft forever
45: veth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast master br0 state DOWN group default qlen 1000
    link/ether 22:8c:ee:b8:e3:30 brd ff:ff:ff:ff:ff:ff

Note the tap0 and veth0 entries: each of their bridge master is br0 (i.e., they have bridge master br0).

When I call ioctl(SIOCGIFFLAGS), and subsequently ioctl(SIOCGIFPFLAGS) when ifr_name is tap0, the only flags that are set are IFF_UP and IFF_BROADCAST. I'm at a loss of where to go from here.

scottbb
  • 180
  • 3
  • 17
  • You could parse the output of `brctl show` – stark Mar 22 '16 at 22:25
  • @stark That's less desirable than parsing `ip a`. That imposes a requirement to have `bridge-utils` package installed. All of the bridging was set up using the `iproute2` suite only. – scottbb Mar 22 '16 at 22:28
  • 2
    You could try using `strace` on `ip a` and see what it does. – Ross Ridge Mar 24 '16 at 23:29
  • Or better yet, check ip source code https://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/tree/ip/ipaddress.c – Braiam Mar 25 '16 at 00:23
  • @RossRidge Thank you, good advice. I had stalled a bit when I tried `strace` before, but I'm revisiting that approach to pull it apart. My `strace` skills kinda suck, but at the moment, I'm working with `strace -f -v -s 512 ip a show tap0`. Any advice on that front is welcome. – scottbb Mar 25 '16 at 01:12
  • You can try crawling `/sys/class/net/yourinterface`. I think there should be a `master` symlink or the like. I'll look into this after work. You should most certainly not use `ioctl`, not unless you want to get shot down by netdev people. netlink and sysfs are there because they aren't just prettier! – pilona Apr 13 '16 at 19:25

1 Answers1

1

iputils and friends use rtnetlink API between kernel and userspace (the POSIX socket API is just too narrow to perform all the needed tasks, and no clean way to add notifications). If your project is small, it is probably easier and faster to parse the output from the ip utility. This is from experience from writing a network configuration daemon for an embedded linux project. rtnetlink API is initially a bit tricky to use, but if you really want to (many pointers and size-references that needs to be correct), do a little research on it and use iputils source as a starting point.

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19