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