blob: 796e40584f5669f66d10d9b4f89528e8a5346195 [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=
32 if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
33 local pfx_bytes=()
34 ip_to_bytes pfx_bytes "$gbmc_ncsi_br_deprecated_ips_lastip"
35
36 local pfx="$(ip_bytes_to_str pfx_bytes)"
37 pfx_bytes[8]=0
38 pfx_bytes[9]=0
39 local host_pfx="$(ip_bytes_to_str pfx_bytes)"
40 read -r -d '' contents <<EOF
41[Address]
42Address=$pfx/128
43PreferredLifetime=0
44[Address]
45Address=$host_pfx/128
46PreferredLifetime=0
47EOF
48 fi
49
50 local file
51 for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do
52 mkdir -p -m 755 "$(dirname "$file")"
53 if [ -z "$contents" ]; then
54 rm -f "$file"
55 else
56 printf '%s' "$contents" >"$file"
57 fi
58 done
59
60 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
61 # currently check the mtime of drop-in files.
62 touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network
63
64 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
65 networkctl reload && networkctl reconfigure @NCSI_IF@
66 fi
67
68 read -r -d '' contents <<EOF
69table inet filter {
70 chain ncsi_input {
71 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
72 }
73 chain ncsi_forward {
74 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
75 }
76}
77EOF
78 rfile=/run/nftables/40-gbmc-ncsi-br.rules
79 mkdir -p -m 755 "$(dirname "$rfile")"
80 printf '%s' "$contents" >"$rfile"
81 systemctl reset-failed nftables && systemctl --no-block restart nftables || true
82}
83
84gbmc_ncsi_br_deprecated_ips_hook() {
85 if [ "$change" = 'init' ]; then
86 gbmc_ncsi_br_deprecated_ips_init=1
87 gbmc_ip_monitor_defer
88 elif [ "$change" = 'defer' ]; then
89 gbmc_ncsi_br_deprecated_ips_update
90 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' ] &&
91 [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
92 local pfx_bytes=()
93 ip_to_bytes pfx_bytes "$ip" || return
94 # No ULA Addresses
95 if (( pfx_bytes[0] & 0xfe == 0xfc )); then
96 return
97 fi
98 # We only want to allow a <pfx>::fd0x address, where x>0
99 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
100 return
101 fi
102 for (( i = 10; i < 16; ++i )); do
103 if (( pfx_bytes[i] != 0 )); then
104 return
105 fi
106 done
107 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
108 gbmc_ncsi_br_deprecated_ips_lastip="$ip"
109 gbmc_ip_monitor_defer
110 fi
111 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
112 gbmc_ncsi_br_deprecated_ips_lastip=
113 gbmc_ip_monitor_defer
114 fi
115 fi
116}
117
118GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook)
119
120gbmc_ncsi_br_deprecated_ips_lib=1