blob: 96037ea450c4b896bb7efe106507b4ac276b1a15 [file] [log] [blame]
William A. Kennington III21e7e452021-11-05 01:31:59 -07001# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15[ -z "${gbmc_ncsi_br_deprecated_ips_lib-}" ] || return
16
William A. Kennington III2f5e1692021-11-05 01:31:59 -070017source /usr/share/network/lib.sh || exit
18
William A. Kennington III21e7e452021-11-05 01:31:59 -070019gbmc_ncsi_br_deprecated_ips_init=
20gbmc_ncsi_br_deprecated_ips_confip=
21gbmc_ncsi_br_deprecated_ips_lastip=
22
23gbmc_ncsi_br_deprecated_ips_update() {
24 [ -n "$gbmc_ncsi_br_deprecated_ips_init" ] || return
25 [ "$gbmc_ncsi_br_deprecated_ips_confip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ] || return
26 gbmc_ncsi_br_deprecated_ips_confip="$gbmc_ncsi_br_deprecated_ips_lastip"
27
28 printf 'gBMC Bridge NCSI Deprecated Addrs: %s\n' \
29 "${gbmc_ncsi_br_deprecated_ips_lastip:-(deleted)}" >&2
30
31 local contents=
William A. Kennington IIIb823f892021-11-23 20:54:56 -080032 local nfcontents=
William A. Kennington III21e7e452021-11-05 01:31:59 -070033 if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
34 local pfx_bytes=()
35 ip_to_bytes pfx_bytes "$gbmc_ncsi_br_deprecated_ips_lastip"
36
37 local pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III3dbea932021-11-05 01:31:59 -070038 (( pfx_bytes[9] &= 0xf0 ))
39 local stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III51def642022-02-13 23:11:59 -080040 local gbmcbr_mac="$(ip link show gbmcbr | tail -n 1 | awk '{print $2}')"
41 local gbmcbr_eui48="$(mac_to_eui48 "$gbmcbr_mac")"
42 local stateless_ip="$(ip_pfx_concat "$stateless_pfx/80" "$gbmcbr_eui48")"
43 stateless_ip="${stateless_ip%/*}"
William A. Kennington III21e7e452021-11-05 01:31:59 -070044 pfx_bytes[8]=0
45 pfx_bytes[9]=0
46 local host_pfx="$(ip_bytes_to_str pfx_bytes)"
47 read -r -d '' contents <<EOF
48[Address]
49Address=$pfx/128
50PreferredLifetime=0
51[Address]
William A. Kennington III3dbea932021-11-05 01:31:59 -070052Address=$stateless_pfx/128
53PreferredLifetime=0
54[Address]
William A. Kennington III51def642022-02-13 23:11:59 -080055Address=$stateless_ip/128
56PreferredLifetime=0
57[Address]
William A. Kennington III21e7e452021-11-05 01:31:59 -070058Address=$host_pfx/128
59PreferredLifetime=0
60EOF
William A. Kennington IIIb823f892021-11-23 20:54:56 -080061 read -r -d '' nfcontents <<EOF
62table inet filter {
63 chain ncsi_input {
64 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
65 }
66 chain ncsi_forward {
67 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
68 }
69}
70EOF
William A. Kennington III21e7e452021-11-05 01:31:59 -070071 fi
72
73 local file
74 for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do
75 mkdir -p -m 755 "$(dirname "$file")"
76 if [ -z "$contents" ]; then
77 rm -f "$file"
78 else
79 printf '%s' "$contents" >"$file"
80 fi
81 done
82
83 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
84 # currently check the mtime of drop-in files.
85 touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network
86
87 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
88 networkctl reload && networkctl reconfigure @NCSI_IF@
89 fi
90
William A. Kennington IIIb823f892021-11-23 20:54:56 -080091 local rfile=/run/nftables/40-gbmc-ncsi-br.rules
William A. Kennington III21e7e452021-11-05 01:31:59 -070092 mkdir -p -m 755 "$(dirname "$rfile")"
William A. Kennington IIIb823f892021-11-23 20:54:56 -080093 if [ -z "$nfcontents" ]; then
94 rm -f "$rfile"
95 else
96 printf '%s' "$nfcontents" >"$rfile"
97 fi
William A. Kennington III7356f8e2021-12-15 02:21:52 -080098 systemctl reset-failed nftables && systemctl --no-block reload-or-restart nftables || true
William A. Kennington III21e7e452021-11-05 01:31:59 -070099}
100
101gbmc_ncsi_br_deprecated_ips_hook() {
102 if [ "$change" = 'init' ]; then
103 gbmc_ncsi_br_deprecated_ips_init=1
104 gbmc_ip_monitor_defer
105 elif [ "$change" = 'defer' ]; then
106 gbmc_ncsi_br_deprecated_ips_update
107 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' ] &&
108 [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
109 local pfx_bytes=()
110 ip_to_bytes pfx_bytes "$ip" || return
111 # No ULA Addresses
112 if (( pfx_bytes[0] & 0xfe == 0xfc )); then
113 return
114 fi
115 # We only want to allow a <pfx>::fd0x address, where x>0
116 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
117 return
118 fi
119 for (( i = 10; i < 16; ++i )); do
120 if (( pfx_bytes[i] != 0 )); then
121 return
122 fi
123 done
124 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
125 gbmc_ncsi_br_deprecated_ips_lastip="$ip"
126 gbmc_ip_monitor_defer
127 fi
128 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
129 gbmc_ncsi_br_deprecated_ips_lastip=
130 gbmc_ip_monitor_defer
131 fi
132 fi
133}
134
135GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook)
136
137gbmc_ncsi_br_deprecated_ips_lib=1