Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the bridge device name for my tap adapter in C?

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.

like image 802
scottbb Avatar asked Mar 22 '16 20:03

scottbb


People also ask

What is a bridge interface?

The bridge interface is a function that accommodates multiple interfaces in one virtual interface and bridges those interfaces. Each accommodated interface connected to a physical segment is handled as one segment.

What is network bridging in a Linux machine?

A network bridge is a Link Layer device which forwards traffic between networks based on MAC addresses and is therefore also referred to as a Layer 2 device. It makes forwarding decisions based on tables of MAC addresses which it builds by learning what hosts are connected to each network.


1 Answers

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.

like image 174
Stian Skjelstad Avatar answered Oct 05 '22 11:10

Stian Skjelstad