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