| 1 | #!/bin/sh |
| 2 | # NetworkManager device handler for geneve interfaces. |
| 3 | # |
| 4 | # Put this script in "/etc/NetworkManager/dispatcher.d/device" and |
| 5 | # create a generic connection with: |
| 6 | # |
| 7 | # nmcli connection add type generic \ |
| 8 | # con-name geneve1 \ |
| 9 | # ifname geneve1 \ |
| 10 | # generic.device-handler geneve \ |
| 11 | # connection.autoconnect no |
| 12 | # |
| 13 | # Then add the following parameters at the bottom of file |
| 14 | # /etc/NetworkManager/system-connections/geneve1 , and do a "nmcli |
| 15 | # connection reload". |
| 16 | # |
| 17 | # [user] |
| 18 | # geneve.remote=172.25.14.15 |
| 19 | # geneve.vni=5555 |
| 20 | # geneve.dstport=6082 |
| 21 | # |
| 22 | # Now, when activating connection "geneve1", NetworkManager calls this |
| 23 | # script to create the device according to parameters in the user |
| 24 | # settings, and then performs IP configuration on it. |
| 25 | # |
| 26 | # This script will use the following properties from the [user] setting: |
| 27 | # |
| 28 | # - geneve.remote (required) |
| 29 | # - geneve.vni (required) |
| 30 | # - geneve.ttl |
| 31 | # - geneve.dstport |
| 32 | # |
| 33 | # See the GENEVE section of "man ip-link" for more details. |
| 34 | |
| 35 | ifname=$1 |
| 36 | action=$2 |
| 37 | |
| 38 | require() |
| 39 | { |
| 40 | if ! command -v "$1" > /dev/null ; then |
| 41 | echo "ERROR='$1' is not installed" |
| 42 | exit 1 |
| 43 | fi |
| 44 | } |
| 45 | |
| 46 | get_iplink_param() |
| 47 | { |
| 48 | ip -j -d link show "$1" | jq -r .[0].linkinfo.info_data."$2" |
| 49 | } |
| 50 | |
| 51 | require jq |
| 52 | |
| 53 | if [ "$action" = device-add ]; then |
| 54 | # Create the interface here and then write a line to stdout |
| 55 | # containing "IFINDEX=" followed by the ifindex of the interface |
| 56 | # just created, so that NetworkManager can manage it and configure |
| 57 | # IPs on the interface. The name of the returned ifindex must be |
| 58 | # the same as "$ifname". |
| 59 | |
| 60 | vni=$CONNECTION_USER_GENEVE__VNI |
| 61 | remote=$CONNECTION_USER_GENEVE__REMOTE |
| 62 | ttl=$CONNECTION_USER_GENEVE__TTL |
| 63 | dstport=$CONNECTION_USER_GENEVE__DSTPORT |
| 64 | |
| 65 | if [ -z "$vni" ] || [ -z "$remote" ]; then |
| 66 | echo "ERROR=Missing VNI or remote" |
| 67 | exit 2 |
| 68 | fi |
| 69 | |
| 70 | if [ -d /sys/class/net/"$ifname" ]; then |
| 71 | # If the interface already exists, reuse it after checking |
| 72 | # that the parameters are compatible. |
| 73 | # NOTE: it's not strictly necessary to handle an already |
| 74 | # existing interface, but if the script doesn't, it won't be |
| 75 | # possible to re-activate the connection when it's up. |
| 76 | |
| 77 | if [ "$vni" != "$(get_iplink_param "$ifname" id)" ]; then |
| 78 | echo "ERROR=The link already exists with different VNI" |
| 79 | exit 3 |
| 80 | fi |
| 81 | if [ "$remote" != "$(get_iplink_param "$ifname" remote)" ]; then |
| 82 | echo "ERROR=The link already exists with different remote" |
| 83 | exit 3 |
| 84 | fi |
| 85 | if [ -n "$ttl" ] && [ "$ttl" != "$(get_iplink_param "$ifname" ttl)" ]; then |
| 86 | echo "ERROR=The link already exists with different TTL" |
| 87 | exit 3 |
| 88 | fi |
| 89 | if [ -n "$dstport" ] && [ "$dstport" != "$(get_iplink_param "$ifname" port)" ]; then |
| 90 | echo "ERROR=The link already exists with different dstport" |
| 91 | exit 3 |
| 92 | fi |
| 93 | echo IFINDEX="$(cat /sys/class/net/"$ifname"/ifindex)" |
| 94 | exit 0 |
| 95 | fi |
| 96 | |
| 97 | # The interface doesn't exist, create it |
| 98 | |
| 99 | if ! err=$(ip link add "$ifname" type geneve vni "$vni" remote "$remote" \ |
| 100 | ${tos:+tos "$tos"} \ |
| 101 | ${ttl:+ttl "$ttl"} \ |
| 102 | ${dstport:+dstport "$dstport"} 2>&1); then |
| 103 | echo "ERROR=Failed creating the interface: $err" |
| 104 | exit 4 |
| 105 | fi |
| 106 | |
| 107 | echo IFINDEX="$(cat /sys/class/net/"$ifname"/ifindex)" |
| 108 | exit 0 |
| 109 | elif [ "$action" = device-delete ]; then |
| 110 | # NM automatically deletes the link on deactivation, |
| 111 | # it's not necessary to do it here. The "device-delete" |
| 112 | # action can be used to perform additional operations. |
| 113 | exit 0 |
| 114 | fi |
| 115 | |
| 116 | exit 5 |