| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import * as ipaddr from "ipaddr.js"; |
| 8 |
|
| 9 |
const _ = cockpit.gettext; |
| 10 |
|
| 11 |
/* NetworkManager specific data conversions and utility functions. |
| 12 |
*/ |
| 13 |
|
| 14 |
let byteorder; |
| 15 |
|
| 16 |
export function set_byteorder(bo) { |
| 17 |
byteorder = bo; |
| 18 |
} |
| 19 |
|
| 20 |
export function ip_prefix_to_text(num) { |
| 21 |
return num.toString(); |
| 22 |
} |
| 23 |
|
| 24 |
export function ip_prefix_from_text(text) { |
| 25 |
if (/^[0-9]+$/.test(text.trim())) |
| 26 |
return parseInt(text, 10); |
| 27 |
|
| 28 |
throw cockpit.format(_("Invalid prefix $0"), text); |
| 29 |
} |
| 30 |
|
| 31 |
export function ip_metric_to_text(num) { |
| 32 |
return num.toString(); |
| 33 |
} |
| 34 |
|
| 35 |
export function ip_metric_from_text(text) { |
| 36 |
if (text === "") |
| 37 |
return 0; |
| 38 |
|
| 39 |
if (/^[0-9]+$/.test(text.trim())) |
| 40 |
return parseInt(text, 10); |
| 41 |
|
| 42 |
throw cockpit.format(_("Invalid metric $0"), text); |
| 43 |
} |
| 44 |
|
| 45 |
export function ip_network_address(address, prefix_len) { |
| 46 |
const addrCIDR = address.toString() + "/" + prefix_len; |
| 47 |
const ip_obj = address.kind() === "ipv4" ? ipaddr.IPv4 : ipaddr.IPv6; |
| 48 |
|
| 49 |
try { |
| 50 |
return ip_obj.networkAddressFromCIDR(addrCIDR).toString(); |
| 51 |
} catch (_e) { |
| 52 |
return null; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
export function ip_first_usable_address(address, prefix) { |
| 57 |
try { |
| 58 |
const addr_cidr = address.toString() + "/" + prefix; |
| 59 |
|
| 60 |
if (address.kind() === "ipv4") { |
| 61 |
const netAddr = ipaddr.IPv4.networkAddressFromCIDR(addr_cidr); |
| 62 |
netAddr.octets[3] += (prefix < 31) ? 1 : 0; |
| 63 |
return netAddr.toString(); |
| 64 |
} else { |
| 65 |
const netAddr = ipaddr.IPv6.networkAddressFromCIDR(addr_cidr); |
| 66 |
netAddr.parts[7] += (prefix < 127) ? 1 : 0; |
| 67 |
return netAddr.toString(); |
| 68 |
} |
| 69 |
} catch (_e) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
function toDec(n) { |
| 75 |
return n.toString(10); |
| 76 |
} |
| 77 |
|
| 78 |
function bytes_from_nm32(num) { |
| 79 |
const bytes = []; |
| 80 |
if (byteorder == "be") { |
| 81 |
for (let i = 3; i >= 0; i--) { |
| 82 |
bytes[i] = num & 0xFF; |
| 83 |
num = num >>> 8; |
| 84 |
} |
| 85 |
} else if (byteorder == "le") { |
| 86 |
for (let i = 0; i < 4; i++) { |
| 87 |
bytes[i] = num & 0xFF; |
| 88 |
num = num >>> 8; |
| 89 |
} |
| 90 |
} else { |
| 91 |
throw new Error("byteorder is unset or has invalid value " + JSON.stringify(byteorder)); |
| 92 |
} |
| 93 |
return bytes; |
| 94 |
} |
| 95 |
|
| 96 |
export function validate_ipv4(address) { |
| 97 |
// explicitly require all 4 octets |
| 98 |
// NM does not support any IPv4 short format |
| 99 |
return ipaddr.IPv4.isValidFourPartDecimal(address); |
| 100 |
} |
| 101 |
|
| 102 |
export function validate_ipv6(address) { |
| 103 |
return ipaddr.IPv6.isValid(address); |
| 104 |
} |
| 105 |
|
| 106 |
export function validate_ip(address) { |
| 107 |
return validate_ipv4(address) || validate_ipv6(address); |
| 108 |
} |
| 109 |
|
| 110 |
export function ip4_to_text(num, zero_is_empty) { |
| 111 |
if (num === 0 && zero_is_empty) |
| 112 |
return ""; |
| 113 |
return bytes_from_nm32(num).map(toDec) |
| 114 |
.join('.'); |
| 115 |
} |
| 116 |
|
| 117 |
export function ip4_from_text(text, empty_is_zero) { |
| 118 |
function invalid() { |
| 119 |
throw cockpit.format(_("Invalid address $0"), text); |
| 120 |
} |
| 121 |
|
| 122 |
if (text === "" && empty_is_zero) |
| 123 |
return 0; |
| 124 |
|
| 125 |
const parts = text.split('.'); |
| 126 |
if (parts.length != 4) |
| 127 |
invalid(); |
| 128 |
|
| 129 |
const bytes = parts.map(function(s) { |
| 130 |
if (/^[0-9]+$/.test(s.trim())) |
| 131 |
return parseInt(s, 10); |
| 132 |
else |
| 133 |
return invalid(); |
| 134 |
}); |
| 135 |
|
| 136 |
let num = 0; |
| 137 |
function shift(b) { |
| 138 |
if (isNaN(b) || b < 0 || b > 0xFF) |
| 139 |
invalid(); |
| 140 |
num = 0x100 * num + b; |
| 141 |
} |
| 142 |
|
| 143 |
if (byteorder == "be") { |
| 144 |
for (let i = 0; i < 4; i++) { |
| 145 |
shift(bytes[i]); |
| 146 |
} |
| 147 |
} else if (byteorder == "le") { |
| 148 |
for (let i = 3; i >= 0; i--) { |
| 149 |
shift(bytes[i]); |
| 150 |
} |
| 151 |
} else { |
| 152 |
throw new Error("byteorder is unset or has invalid value " + JSON.stringify(byteorder)); |
| 153 |
} |
| 154 |
|
| 155 |
return num; |
| 156 |
} |
| 157 |
|
| 158 |
export function ip4_prefix_from_text(prefix_mask) { |
| 159 |
const trimmed_mask = prefix_mask.trim(); |
| 160 |
if (/^[0-9]+$/.test(trimmed_mask)) { |
| 161 |
return parseInt(prefix_mask, 10); |
| 162 |
} |
| 163 |
|
| 164 |
// make sure that mask has 4 octets format |
| 165 |
if (validate_ipv4(trimmed_mask)) { |
| 166 |
const prefix = ipaddr.IPv4.parse(trimmed_mask).prefixLengthFromSubnetMask(); |
| 167 |
if (prefix !== null) { |
| 168 |
return prefix; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
throw cockpit.format(_("Invalid prefix or netmask $0"), prefix_mask); |
| 173 |
} |
| 174 |
|
| 175 |
// Shorten IPv6 address according to RFC 5952 |
| 176 |
// https://datatracker.ietf.org/doc/html/rfc5952#section-4 |
| 177 |
// |
| 178 |
// NetworkManager already handles dropping of leading zeros within a single 16 bit field |
| 179 |
// but does not replace the longest consecutive zeros fields with "::". |
| 180 |
function ip6_shorten(ip6_addr) { |
| 181 |
function find_longest_zero(match) { |
| 182 |
let idx = -1; |
| 183 |
let length = 0; |
| 184 |
|
| 185 |
match.forEach((item, i) => { |
| 186 |
const count_zero = item[0].replaceAll(':', '').length; |
| 187 |
if (count_zero > length) { |
| 188 |
idx = i; |
| 189 |
length = count_zero; |
| 190 |
} |
| 191 |
}); |
| 192 |
|
| 193 |
return idx; |
| 194 |
} |
| 195 |
|
| 196 |
const REGEX_MATCH_CONSECUTIVE_ZEROS = /\b:?(?:0:?){2,}/g; |
| 197 |
const match = [...ip6_addr.matchAll(REGEX_MATCH_CONSECUTIVE_ZEROS)]; |
| 198 |
|
| 199 |
// nothing to shorten |
| 200 |
if (match.length === 0) { |
| 201 |
return ip6_addr; |
| 202 |
} |
| 203 |
|
| 204 |
const longest_idx = find_longest_zero(match); |
| 205 |
// replace first (leftmost) match |
| 206 |
const short_addr = ip6_addr.replace(match[longest_idx], "::"); |
| 207 |
|
| 208 |
return short_addr; |
| 209 |
} |
| 210 |
|
| 211 |
export function ip6_to_text(data, zero_is_empty) { |
| 212 |
const parts = []; |
| 213 |
const bytes = cockpit.base64_decode(data); |
| 214 |
for (let i = 0; i < 8; i++) |
| 215 |
parts[i] = ((bytes[2 * i] << 8) + bytes[2 * i + 1]).toString(16); |
| 216 |
const result = parts.join(':'); |
| 217 |
if (result == "0:0:0:0:0:0:0:0" && zero_is_empty) |
| 218 |
return ""; |
| 219 |
return ip6_shorten(result); |
| 220 |
} |
| 221 |
|
| 222 |
export function ip6_from_text(text, empty_is_zero) { |
| 223 |
function invalid() { |
| 224 |
throw cockpit.format(_("Invalid address $0"), text); |
| 225 |
} |
| 226 |
|
| 227 |
if (text === "" && empty_is_zero) |
| 228 |
return cockpit.base64_encode([0, 0, 0, 0, 0, 0, 0, 0, |
| 229 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 230 |
]); |
| 231 |
|
| 232 |
const parts = text.split(':'); |
| 233 |
if (parts.length < 1 || parts.length > 8) |
| 234 |
invalid(); |
| 235 |
|
| 236 |
if (parts[0] === "") |
| 237 |
parts[0] = "0"; |
| 238 |
if (parts[parts.length - 1] === "") |
| 239 |
parts[parts.length - 1] = "0"; |
| 240 |
|
| 241 |
const bytes = []; |
| 242 |
let empty_seen = false; |
| 243 |
let j = 0; |
| 244 |
for (let i = 0; i < parts.length; i++, j++) { |
| 245 |
if (parts[i] === "") { |
| 246 |
if (empty_seen) |
| 247 |
invalid(); |
| 248 |
empty_seen = true; |
| 249 |
while (j < i + (8 - parts.length)) { |
| 250 |
bytes[2 * j] = bytes[2 * j + 1] = 0; |
| 251 |
j++; |
| 252 |
} |
| 253 |
} else { |
| 254 |
if (!/^[0-9a-fA-F]+$/.test(parts[i].trim())) |
| 255 |
invalid(); |
| 256 |
const n = parseInt(parts[i], 16); |
| 257 |
if (isNaN(n) || n < 0 || n > 0xFFFF) |
| 258 |
invalid(); |
| 259 |
bytes[2 * j] = n >> 8; |
| 260 |
bytes[2 * j + 1] = n & 0xFF; |
| 261 |
} |
| 262 |
} |
| 263 |
if (j != 8) |
| 264 |
invalid(); |
| 265 |
|
| 266 |
return cockpit.base64_encode(bytes); |
| 267 |
} |
| 268 |
|
| 269 |
// SSID comes as a base64-encoded string from D-Bus (signature 'ay') |
| 270 |
export const ssid_from_nm = bytes => new TextDecoder().decode( |
| 271 |
new Uint8Array(cockpit.base64_decode(bytes ?? [])) |
| 272 |
); |
| 273 |
|
| 274 |
export const ssid_to_nm = ssid => cockpit.base64_encode(new TextEncoder().encode(ssid)); |
| 275 |
|
| 276 |
export function list_interfaces() { |
| 277 |
return new Promise((resolve, reject) => { |
| 278 |
const client = cockpit.dbus("org.freedesktop.NetworkManager"); |
| 279 |
client.call('/org/freedesktop/NetworkManager', |
| 280 |
'org.freedesktop.NetworkManager', |
| 281 |
'GetAllDevices', []) |
| 282 |
.then(reply => { |
| 283 |
Promise.all(reply[0].map(device => { |
| 284 |
return Promise.all([ |
| 285 |
client.call(device, |
| 286 |
'org.freedesktop.DBus.Properties', |
| 287 |
'Get', ['org.freedesktop.NetworkManager.Device', 'Interface']) |
| 288 |
.then(reply => reply[0]), |
| 289 |
client.call(device, |
| 290 |
'org.freedesktop.DBus.Properties', |
| 291 |
'Get', ['org.freedesktop.NetworkManager.Device', 'Capabilities']) |
| 292 |
.then(reply => reply[0]) |
| 293 |
]); |
| 294 |
})) |
| 295 |
.then(interfaces => { |
| 296 |
client.close(); |
| 297 |
resolve(interfaces.map(i => { |
| 298 |
return { device: i[0].v, capabilities: i[1].v }; |
| 299 |
})); |
| 300 |
}) |
| 301 |
.catch(e => console.warn(JSON.stringify(e))); |
| 302 |
}) |
| 303 |
.catch(e => console.warn(JSON.stringify(e))); |
| 304 |
}); |
| 305 |
} |
| 306 |
|
| 307 |
// Check if we are working with non-persistent multi-connection, |
| 308 |
// namely the `id=Wired Connection` generated by networkmanager. |
| 309 |
// |
| 310 |
// When editing this connection, we need to create a new connection |
| 311 |
// on top of this, remove the multiconnection settings and assign it directly |
| 312 |
// to interface. This is done for Anaconda in order to avoid creating unexpected |
| 313 |
// multiconnections when user modifies the connection during installation. |
| 314 |
export function isNonPersistentMultiCon(connection) { |
| 315 |
const settings = connection?.Settings; |
| 316 |
if (settings && |
| 317 |
settings.connection.interface_name === undefined && |
| 318 |
settings.connection.id === "Wired Connection" && |
| 319 |
connection.Unsaved === true && settings.connection.type && |
| 320 |
settings.connection.multi_connect === 3 && |
| 321 |
settings.connection.type === "802-3-ethernet") { |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
export function createNewConnSettings(settings, iface_name) { |
| 329 |
const newSettings = { |
| 330 |
...settings, |
| 331 |
connection: { |
| 332 |
...settings.connection, |
| 333 |
id: iface_name, |
| 334 |
interface_name: iface_name, |
| 335 |
// increase the current priority by 1, maximum priority is 999 |
| 336 |
autoconnect_priority: Math.min(settings.connection.autoconnect_priority + 1, 999), |
| 337 |
} |
| 338 |
}; |
| 339 |
delete newSettings.connection.uuid; |
| 340 |
delete newSettings.connection.multi_connect; |
| 341 |
|
| 342 |
return newSettings; |
| 343 |
} |
| 344 |
|
| 345 |
export function debug() { |
| 346 |
if (window.debugging == "all" || window.debugging?.includes("networkmanager")) // not-covered: debugging |
| 347 |
console.debug("networkmanager:", ...arguments); // not-covered: debugging |
| 348 |
} |