# netstat -rn and route interfaces



## Airost (Dec 7, 2022)

Good morning everyone,

Long time not posting anything. I have a question regarding routing. Please see below output for `netstat -4 -rn`:

```
root@ldrive:/home/airost # netstat -4 -rn
Routing tables

Internet:
Destination        Gateway                Flags     Netif Expire
default               195.166.130.255    US         tun0
10.0.0.0/8           link#10                  U          tap0
10.0.0.10             link#11                 UHS         lo0
10.0.0.20             link#10                 UHS         lo0
127.0.0.1             link#5                   UH          lo0
172.16.0.0/16      link#2                  U          bge1
172.16.0.1           link#2                  UHS         lo0
192.168.2.0/24    link#1                  U          bge0
192.168.2.11       link#1                  UHS         lo0
195.166.130.255 link#8                  UHS        tun0
212.159.22.253   link#8                  UHS         lo0
```

And please see output for `ifconfig`:


```
root@ldrive:/home/airost # ifconfig
bge0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=c019b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,VLAN_HWTSO,LINKSTATE>
        ether 44:a8:42:47:7f:29
        inet 192.168.2.11 netmask 0xffffff00 broadcast 192.168.2.255
        media: Ethernet autoselect (1000baseT <full-duplex>)
        status: active
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
bge1: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=c0099<RXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,VLAN_HWTSO,LINKSTATE>
        ether 44:a8:42:47:7f:2a
        inet 172.16.0.1 netmask 0xffff0000 broadcast 172.16.255.255
        inet6 fe80::46a8:42ff:fe47:7f2a%bge1 prefixlen 64 scopeid 0x2
        media: Ethernet autoselect (1000baseT <full-duplex>)
        status: active
        nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
bge2: flags=8802<BROADCAST,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=c019b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,VLAN_HWTSO,LINKSTATE>
        ether 44:a8:42:47:7f:2b
        media: Ethernet autoselect
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
bge3: flags=8802<BROADCAST,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=c019b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,VLAN_HWTSO,LINKSTATE>
        ether 44:a8:42:47:7f:2c
        media: Ethernet autoselect
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
        options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x5
        inet 127.0.0.1 netmask 0xff000000
        groups: lo
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
pflog0: flags=141<UP,RUNNING,PROMISC> metric 0 mtu 33160
        groups: pflog
pfsync0: flags=0<> metric 0 mtu 1500
        syncpeer: 0.0.0.0 maxupd: 128 defer: off
        syncok: 1
        groups: pfsync
tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1492
        options=80000<LINKSTATE>
        inet 212.159.22.253 --> 195.166.130.255 netmask 0xffffffff
        groups: tun
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
        Opened by PID 491
vm-public: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        ether 26:4f:45:88:e9:3e
        id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
        maxage 20 holdcnt 6 proto rstp maxaddr 2000 timeout 1200
        root id 00:00:00:00:00:00 priority 32768 ifcost 0 port 0
        member: bge1 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
                ifmaxaddr 0 port 2 priority 128 path cost 20000
        member: tap1 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
                ifmaxaddr 0 port 11 priority 128 path cost 2000000
        member: tap0 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
                ifmaxaddr 0 port 10 priority 128 path cost 2000000
        groups: bridge vm-switch viid-4c918@
        nd6 options=9<PERFORMNUD,IFDISABLED>
tap0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
        description: vmnet/ubuntuvm/0/public
        options=80000<LINKSTATE>
        ether 58:9c:fc:10:f9:42
        inet 10.0.0.20 netmask 0xff000000 broadcast 10.255.255.255
        groups: tap vm-port
        media: Ethernet autoselect
        status: active
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
        Opened by PID 3771
tap1: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
        description: vmnet/win2012servervm/0/public
        options=80000<LINKSTATE>
        ether 58:9c:fc:10:20:2e
        inet 10.0.0.10 netmask 0xff000000 broadcast 10.255.255.255
        groups: tap vm-port
        media: Ethernet autoselect
        status: active
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
        Opened by PID 4036
vm-private: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        ether 0e:77:7e:ac:c6:c2
        id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
        maxage 20 holdcnt 6 proto rstp maxaddr 2000 timeout 1200
        root id 00:00:00:00:00:00 priority 32768 ifcost 0 port 0
        groups: bridge vm-switch viid-2c17c@
        nd6 options=9<PERFORMNUD,IFDISABLED>
```

My question is why in routing it shows most interfaces as loopback? Whenever I was reading online on google, everyone is posting correct interfaces i.e. 10.0.0.10 and 10.0.0.20 should be tap1 and tap0. And another question is what is link#?? means? why it's not an IP address?

I am wondering if all my previous threads on this forum regarding port forwarding to jails are related to routing issue. I just noticed when I was running jails and cloned lo0 interface then I forwarded port to lo1 in jails all was working fine, when I don't clone lo0 and I am trying to do any other way I don't get to work at all. I stopped using jails since I replaced my old server with new (second hand) Dell R430 and now using VM. Any help and advice appreciated.

Seb


----------



## sko (Dec 7, 2022)

Airost said:


> why in routing it shows most interfaces as loopback?


because those addresses belong to the host and hence are reachable to the host via loopback.
only the attached _network_ is routed through the physical interface.


e.g.


Airost said:


> 172.16.0.0/16 link#2 U bge1
> 172.16.0.1 link#2 UHS lo0



172.16.0.0/16 is reachable over bge1
172.16.0.1 is the local address, so it is available via loopback
[...] etc


----------



## PMc (Dec 7, 2022)

Airost said:


> And another question is what is link#?? means? why it's not an IP address?


Because it is not routed, and can be sent directly. An IP-address at that place would be the next-hop where to send the packet. 

Having `bge0 inet 192.168.2.11 netmask 0xffffff00` means that all addresses within 192.168.2.0/24 can be put onto that interface and will reach their destination directly.
To the contrary, having `route add default  195.166.130.255` means: figure out how to send to 195.166.130.255, and then send the concerned packets there.

The link#N is simply the internal enumeration of the installed interfaces: each interface has a distinct number.

The difference comes into play when looking into `arp`. Given we want to send a packet to 192.168.2.150, and we have this routing:
`192.168.2.0/24    link#1                  U          bge0`

link#1 equates to bge0. So we send a arp broadcast onto bge0: `who has 192.168.2.150`. In reply we will get the MAC address of that machine. Then we write that MAC address onto the packet and send it onto bge0.
If we had a gateway IP address in the routing table, then we would ask for the MAC address of that gateway, not the MAC address of the packet's destination. We do not change the destination IP address present within the packet, but we write the gateway MAC address onto it before sending.

Now this is the handling of other machines' addresses. Our local addresses are special case, these should always be sent through the lo interface. (Sending them through an external interface is possible in principle, but then that interface needs to support echoback, and it calls for trouble.)
This is why local addresses have the link#N of the interface they belong to, but have lo0 as the netif. (Some of the actual processing is done by if-then clauses in the sys/netinet code.)



Airost said:


> I am wondering if all my previous threads on this forum regarding port forwarding to jails are related to routing issue. I just noticed when I was running jails and cloned lo0 interface then I forwarded port to lo1 in jails all was working fine, when I don't clone lo0 and I am trying to do any other way I don't get to work at all.


Jails are where you need to care about these things. I for my part didn't fine a use-case where I would want to clone lo0. Maybe You have one; I'm not sure how your "forwarding" is supposed to work.

Just watch carefully how your packets travel. You can watch them with `tcpdump` on the concerned interfaces, and/or with `ipfw` by creating some logging rules (set net.inet.ip.fw.one_pass=0 to see them twice, on the way in and out) . And remember: external routed packets come IN through some interface, go through the routing, and then go OUT to some interface; but internal packets at first go OUT through loX, and afterwards come IN again.


----------

