brona / iproute2mac

CLI wrapper for basic network utilites on Mac OS X inspired with iproute2 on Linux systems - ip command.
MIT License
865 stars 70 forks source link

VLAN support #52

Open signal-09 opened 1 month ago

signal-09 commented 1 month ago

It is desirable to manage VLANs as Linux does:

$ sudo ip link add link eth0 name eth0.111 type vlan id 111
$ sudo ip addr add 192.168.111.1/24 brd + dev eth0.111
$ sudo ip link set up dev eth0.111
$ ip addr show dev eth0.111
7: eth0.111@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether da:8d:21:53:7a:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.111.1/24 brd 192.168.111.255 scope global eth0.111
       valid_lft forever preferred_lft forever

To show VLAN id in the output of ip command -details option is needed:

$ ip -details addr show dev eth0.111
7: eth0.111@eth0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether da:8d:21:53:7a:b5 brd ff:ff:ff:ff:ff:ff promiscuity 0  allmulti 0 minmtu 0 maxmtu 65535
    vlan protocol 802.1Q id 111  numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536
    inet 192.168.111.1/24 brd 192.168.111.255 scope global eth0.111
       valid_lft forever preferred_lft forever

In macOS could be translated into:

$ sudo ifconfig vlan111 create
$ sudo ifconfig vlan111 vlan 111 vlandev en0
$ sudo ifconfig vlan111 add 192.168.111.2/24
$ ifconfig vlan111
vlan111: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=3<RXCSUM,TXCSUM>
    ether 14:98:77:71:03:99
    inet 192.168.111.2 netmask 0xffffff00 broadcast 192.168.111.255
    vlan: 111 parent interface: en0
    media: 100baseTX <full-duplex>
    status: active

To remove the interface link (ip link del eth0.111 in Linux) it is possible to use ifconfig vlan111 destroy.

There are some difference between macOS and Linux for VLAN interfaces:

  1. in macOS the link is already UP (active) after the ifconfig <interface> vlan <tag> vlandev <iface> command;
  2. it is impossible to set the link DOWN (inactive), ifconfig <interface> down has no effect;
  3. in macOS interfaces cannot have a dotted notation as in Linux (e.g. en0.111):
    $ sudo ifconfig en0.111 create
    ifconfig: SIOCIFCREATE2: Invalid argument
brona commented 1 month ago

Thanks for documenting the behavior and working on it!