AF_LINK may not be the first address returned for the interface.
Technically, it *might* not even exist on the interface even though
other families do.
This is likely a driver bug if this really is the case though.
As such it's just easier to use direct ioctls rather than thump around
getifaddrs results. As it stands, the code makes a lot of getifaddrs
calls anyway, so an extra ioctl or two won't break the bank.
For AF_LINK addrs from getifaddrs(2), ifa_data is struct if_data.
This in turn holds ifi_link_state which we can use to report
link status if the interface does not support media where it's normally
reported.
Based on OpenBSD.
no need to skip a prefix "0x" of output of snprintb.
ifconfig expect the result of snprintb has "0x" prefix, but snprintb(3) specification has changed
so that if 0 is passed, it prints "0" instead of "0x0". (common/lib/libutil/snprintb.c r1.22)
First proposed by jmcneill in 2017 and modified by me.
How to use:
- Set callback function:
ether_set_vlan_cb(struct ethercom *, ether_vlancb_t)
- Callback. This function is called when a vlan is attached/detached to the
parent interface:
int (*ether_vlancb_t)(struct ethercom *ec, uint16_t vlanid, bool set);
- ifconfig(8)
ifconfig ixg0 [-]vlan-hwfilter
Note that ETHERCAP_VLAN_HWFILTER is set by default on ixg(4) because
the PF driver usually enable "all block" filter by default.
Unportable left shift reported with MKSANITIZER=yes USE_SANITIZER=undefined:
# ifconfig
alc0: flags=0x8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ec_capabilities=3<VLAN_MTU,VLAN_HWTAGGING>
ec_enabled=0
address: xx:xx:xx:xx:xx:xx
/public/src.git/sbin/ifconfig/af_inet.c:102:34: runtime error: left shift of 16777215 by 8 places cannot be represented in type 'int'
inet 192.168.0.38/24 broadcast 192.168.0.255 flags 0x0
inet6 xxxx::xxxx:xxxx:xxxx:xxx%alc0/64 flags 0x0 scopeid 0x1
lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
inet 127.0.0.1/8 flags 0x0
inet6 ::1/128 flags 0x20<NODAD>
inet6 fe80::1%lo0/64 flags 0x0 scopeid 0x2
Change shifting left 1 to shifting 1U. This corrects the issue.
if (cidr < 32) { /* more than 1 bit in mask */
/* check for non-contig netmask */
if ((mask ^ (((1 << cidr) - 1) << (32 - cidr))) != 0) // <- here
return -1; /* noncontig, no pfxlen */
}
Solution suggested by <uwe>
Sponsored by <The NetBSD Foundation>
mask set is non-contiguous. We don't prohibit setting such things
(even if they are basically useless) so they can be set by accident.
ifconfig ifN 10.0.0.1 netmask 225.0.0.0
produced
ifN .. inet 10.0.0.1/8
with the previous form (since 225 is 0xE1), now it produces
ifN ... inet 10.0.0.1 netmask 0xe1000000
If the "netmask" form ever appears in ifconfig output, it (now)
means that the netmask is non-contig, which should make that case
obvious (whther intended, or set by accident)