blob: a1b16caf656f4e7a9ea76968dfe60c09760dbd51 [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
William A. Kennington III80a75282022-05-20 09:47:33 -070015[ -n "${gbmc_ncsi_br_deprecated_ips_lib-}" ] && return
William A. Kennington III21e7e452021-11-05 01:31:59 -070016
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
William A. Kennington III80a75282022-05-20 09:47:33 -070028 printf 'gBMC NCSI Deprecated Addrs: %s\n' \
William A. Kennington III21e7e452021-11-05 01:31:59 -070029 "${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 III80a75282022-05-20 09:47:33 -070040 local stateless_ip=
41 if [ -e /sys/class/net/gbmcbr ]; then
42 local gbmcbr_mac="$(ip link show gbmcbr | tail -n 1 | awk '{print $2}')"
43 local gbmcbr_eui48="$(mac_to_eui48 "$gbmcbr_mac")"
44 stateless_ip="$(ip_pfx_concat "$stateless_pfx/80" "$gbmcbr_eui48")"
45 stateless_ip="${stateless_ip%/*}"
46 fi
William A. Kennington III21e7e452021-11-05 01:31:59 -070047 pfx_bytes[8]=0
48 pfx_bytes[9]=0
49 local host_pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III80a75282022-05-20 09:47:33 -070050 read -r -d '' contents <<EOF
51[Network]
52IPv6ProxyNDP=yes
53IPv6ProxyNDPAddress=$pfx
54IPv6ProxyNDPAddress=$stateless_pfx
55IPv6ProxyNDPAddress=$host_pfx
56${stateless_ip:+IPv6ProxyNDPAddress=}$stateless_ip
57EOF
William A. Kennington IIIb823f892021-11-23 20:54:56 -080058 read -r -d '' nfcontents <<EOF
59table inet filter {
60 chain ncsi_input {
61 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
William A. Kennington III80a75282022-05-20 09:47:33 -070062 ip6 daddr $host_pfx/64 goto ncsi_legacy_input
William A. Kennington IIIb823f892021-11-23 20:54:56 -080063 }
64 chain ncsi_forward {
65 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
66 }
67}
68EOF
William A. Kennington III21e7e452021-11-05 01:31:59 -070069 fi
70
71 local file
72 for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do
73 mkdir -p -m 755 "$(dirname "$file")"
74 if [ -z "$contents" ]; then
75 rm -f "$file"
76 else
77 printf '%s' "$contents" >"$file"
78 fi
79 done
80
81 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
82 # currently check the mtime of drop-in files.
83 touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network
84
85 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
86 networkctl reload && networkctl reconfigure @NCSI_IF@
87 fi
88
William A. Kennington IIIb823f892021-11-23 20:54:56 -080089 local rfile=/run/nftables/40-gbmc-ncsi-br.rules
William A. Kennington III21e7e452021-11-05 01:31:59 -070090 mkdir -p -m 755 "$(dirname "$rfile")"
William A. Kennington IIIb823f892021-11-23 20:54:56 -080091 if [ -z "$nfcontents" ]; then
92 rm -f "$rfile"
93 else
94 printf '%s' "$nfcontents" >"$rfile"
95 fi
William A. Kennington III7356f8e2021-12-15 02:21:52 -080096 systemctl reset-failed nftables && systemctl --no-block reload-or-restart nftables || true
William A. Kennington III21e7e452021-11-05 01:31:59 -070097}
98
99gbmc_ncsi_br_deprecated_ips_hook() {
100 if [ "$change" = 'init' ]; then
101 gbmc_ncsi_br_deprecated_ips_init=1
102 gbmc_ip_monitor_defer
103 elif [ "$change" = 'defer' ]; then
104 gbmc_ncsi_br_deprecated_ips_update
William A. Kennington III80a75282022-05-20 09:47:33 -0700105 elif [ "$change" = 'addr' -a "$scope" = 'global' -a "$fam" = 'inet6' ] &&
106 [ "$intf" = 'gbmcbr' -o "$intf" = '@NCSI_IF@' ] &&
107 [[ "$flags" != *deprecated* ]]; then
William A. Kennington III21e7e452021-11-05 01:31:59 -0700108 local pfx_bytes=()
109 ip_to_bytes pfx_bytes "$ip" || return
110 # No ULA Addresses
111 if (( pfx_bytes[0] & 0xfe == 0xfc )); then
112 return
113 fi
114 # We only want to allow a <pfx>::fd0x address, where x>0
115 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
116 return
117 fi
118 for (( i = 10; i < 16; ++i )); do
119 if (( pfx_bytes[i] != 0 )); then
120 return
121 fi
122 done
123 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
124 gbmc_ncsi_br_deprecated_ips_lastip="$ip"
125 gbmc_ip_monitor_defer
126 fi
127 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then
128 gbmc_ncsi_br_deprecated_ips_lastip=
129 gbmc_ip_monitor_defer
130 fi
131 fi
132}
133
134GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook)
135
136gbmc_ncsi_br_deprecated_ips_lib=1