diff --git a/backend/src/services/internet-exchange-execution.test.ts b/backend/src/services/internet-exchange-execution.test.ts index 0d0489d6..c512d215 100644 --- a/backend/src/services/internet-exchange-execution.test.ts +++ b/backend/src/services/internet-exchange-execution.test.ts @@ -14,6 +14,26 @@ const route: IxRouterObject = { } const device = (rows: Record>>) => [{ serverId: 17, source: "", rows }] +test("managed MSS rules are backfilled and removed without adopting a hand-written rule", () => { + const path = "/ip/firewall/mangle" + const from = { serverId: 17, path, fields: { comment: name + ":linux-mss-from-il-wg", action: "change-mss", + chain: "prerouting", "in-interface": "il-wg", "new-mss": "1380", "tcp-mss": "1381-65535" } } + const to = { serverId: 17, path, fields: { comment: name + ":linux-mss-to-il-wg", action: "change-mss", + chain: "postrouting", "out-interface": "il-wg", "new-mss": "1380", "tcp-mss": "1381-65535" } } + const manual = { ...to.fields, comment: "manual MSS", ".id": "*A" } + const fromRow = { ...from.fields, ".id": "*B" } + const toRow = { ...to.fields, ".id": "*C" } + const desired = [from, to] + const live = (rows: Array>) => device({ [path]: rows }) + assert.deepEqual(planIxNative(id, desired, desired, live([manual])).map(task => [task.fields, task.placeBefore]), + [[from.fields, "*A"], [to.fields, "*A"]]) + assert.deepEqual(planIxNative(id, desired, desired, live([fromRow, manual])).map(task => task.fields), [to.fields]) + assert.deepEqual(planIxNative(id, desired, desired, live([fromRow, toRow, manual])), []) + assert.deepEqual(planIxNative(id, [], desired, live([fromRow, toRow, manual])).map(task => task.rosId), ["*C", "*B"]) + assert.throws(() => planIxNative(id, desired, [], live([fromRow, manual])), /незавершённой операции/) + assert.throws(() => planIxNative(id, desired, desired, live([{ ...fromRow, "new-mss": "1460" }, toRow])), /изменён вне операции/) +}) + test("a new VRF is inserted before main and no foreign route is altered", () => { const changes = planIxNative(id, [vrf, route], [], device({ "/ip/vrf": [{ ".id": "*A", name: "main", interfaces: "all", builtin: "true" }], diff --git a/backend/src/services/internet-exchange-execution.ts b/backend/src/services/internet-exchange-execution.ts index a4f47e72..ff8b7a0f 100644 --- a/backend/src/services/internet-exchange-execution.ts +++ b/backend/src/services/internet-exchange-execution.ts @@ -98,6 +98,9 @@ export function planIxNative(id: string, desired: IxRouterObject[], previous: Ix rows.some(row => egressObject(row.comment)))).map(device => device.serverId)) // An older applied revision had no egress list. A partial set still needs reconciliation. previous = previous.filter(object => !egressObject(object.fields.comment) || withEgressList.has(object.serverId)) + // Older revisions were compiled without MSS rules. Backfill only absent managed MSS rules. + previous = previous.filter(object => !object.fields.comment?.startsWith(namespace + ":linux-mss-") || + devices.some(device => device.serverId === object.serverId && managedRow(device, object))) const desiredKeys = new Set(desired.map(deviceMarker)) const previousByKey = new Map(previous.map(object => [deviceMarker(object), object])) const result: Task[] = [] @@ -356,7 +359,7 @@ async function configureIxLinuxTunnel(segment: IxSegment, input: IxLinuxClientRe const binding: IxSite["interfaces"][number] = { name, kind: input.kind, prefixes: input.prefixes, linux: { name: input.name, clientId: previous?.linux?.clientId, enabled: input.enabled, destinations: input.destinations, peerEndpoint: input.peerEndpoint, - exit: input.exit === "local-wan" ? localExit : input.exit }, + tunnelMtu: input.tunnelMtu ?? previous?.linux?.tunnelMtu, exit: input.exit === "local-wan" ? localExit : input.exit }, } if (input.kind === "wireguard") { const existing = previous?.provisionWireguard diff --git a/backend/src/services/internet-exchange-plan.test.ts b/backend/src/services/internet-exchange-plan.test.ts index 5cd92e9a..ec00e1c8 100644 --- a/backend/src/services/internet-exchange-plan.test.ts +++ b/backend/src/services/internet-exchange-plan.test.ts @@ -32,6 +32,8 @@ test("managed clients get their own return path and fail closed if both exit pro provisionWireguard: { peerPublicKey: "A".repeat(43) + "=", listenPort: 51900, localAddress: "10.254.1.1/30", peerAddress: "10.254.1.2", hubEndpoint: "192.0.2.10" } }) const objects = compileIxConfiguration(id, value) + const clamp = objects.findIndex(object => object.fields.comment === table + ":linux-mss-from-linux-wg") + assert.ok(clamp >= 0 && clamp < objects.findIndex(object => object.fields.comment === table + ":linux-linux-wg-route")) const clientTable = ixClientRoutingTable(table, "linux-wg") const defaults = objects.filter(object => object.fields["routing-table"] === clientTable) assert.deepEqual(defaults.map(object => object.fields.distance), ["10", "11", "254"]) @@ -45,6 +47,35 @@ test("managed clients get their own return path and fail closed if both exit pro assert.ok(objects.every(object => object.path !== "/ip/vrf" || !object.fields.interfaces.includes("ether1"))) }) +test("managed Linux tunnels clamp TCP SYN in both directions before routing marks can end prerouting", () => { + const value = fixture(), site = value.sites[0] + site.interfaces.push( + { name: "linux-wg", kind: "wireguard", prefixes: [], linux: { name: "vps", enabled: true, destinations: [] }, + provisionWireguard: { localAddress: "10.254.0.1/30", peerAddress: "10.254.0.2", listenPort: 51820, peerPublicKey: "A".repeat(43) + "=" } }, + { name: "linux-gre", kind: "gre-ipsec", prefixes: [], endpoint: "192.0.2.90", localEndpoint: "192.0.2.17", + linux: { name: "vps", enabled: false, destinations: [] }, + provisionGre: { localAddress: "10.254.0.5/30", peerAddress: "10.254.0.6", credentialId: id } }, + { name: "linux-wg6", kind: "wireguard", prefixes: [], + linux: { name: "vps6", enabled: true, destinations: [], tunnelMtu: 1360 }, + provisionWireguard: { localAddress: "fd00:254::1/126", peerAddress: "fd00:254::2", listenPort: 51821, peerPublicKey: "B".repeat(43) + "=" } }, + ) + const rules = compileIxConfiguration(id, ixSegmentConfigurationSchema.parse(value)).filter(object => object.fields.action === "change-mss") + assert.equal(rules.length, 6) + for (const [binding, family, mss, disabled] of [ + ["linux-wg", "ip", 1380, "false"], ["linux-gre", "ip", 1380, "true"], ["linux-wg6", "ipv6", 1300, "false"], + ] as const) { + const list = ixClientRoutingTable(table, binding) + "-src" + const shared = { protocol: "tcp", "tcp-flags": "syn", "tcp-mss": (mss + 1) + "-65535", + action: "change-mss", "new-mss": String(mss), passthrough: "true", disabled } + assert.deepEqual(rules.filter(object => object.fields.comment.endsWith("-" + binding)), [ + { serverId: site.serverId, path: "/" + family + "/firewall/mangle", + fields: { ...shared, chain: "prerouting", "in-interface": binding, "src-address-list": list, comment: table + ":linux-mss-from-" + binding } }, + { serverId: site.serverId, path: "/" + family + "/firewall/mangle", + fields: { ...shared, chain: "postrouting", "out-interface": binding, "dst-address-list": list, comment: table + ":linux-mss-to-" + binding } }, + ]) + } +}) + test("a managed remote exit prepares both CHRs and returns only prefixes of users assigned to it", () => { const value = fixture(), site = value.sites[0] site.interfaces.push({ name: "it-abc", kind: "wireguard", prefixes: [], peerServerId: 99, diff --git a/backend/src/services/internet-exchange-plan.ts b/backend/src/services/internet-exchange-plan.ts index 48e1f4d5..0f61aa44 100644 --- a/backend/src/services/internet-exchange-plan.ts +++ b/backend/src/services/internet-exchange-plan.ts @@ -84,6 +84,11 @@ function compileLinuxPolicy(namespace: string, site: IxSite, binding: IxSite["in const permitted = privateTable + "-dst" const sourcesCidr = [...new Set([host(tunnel.peerAddress), ...binding.prefixes.map(canonicalBgpPolicyPrefix)])] const destinations = [...new Set([host(tunnel.localAddress.split("/")[0]), ...policy.destinations.map(canonicalBgpPolicyPrefix)])] + // Use the WireGuard default as a conservative ceiling for both managed tunnel types. + const mss = (policy.tunnelMtu ?? 1420) - (family === 4 ? 40 : 60) + const clamp = { protocol: "tcp", "tcp-flags": "syn", "tcp-mss": (mss + 1) + "-65535", + action: "change-mss", "new-mss": String(mss), passthrough: "true", disabled: String(!policy.enabled) } + const mangle = ip + "/firewall/mangle" const ingress = { chain: "prerouting", "in-interface": binding.name } const exit = site.groups.find(group => group.id === policy.exit) const paths = exit ? groupPaths(exit) : [] @@ -114,6 +119,13 @@ function compileLinuxPolicy(namespace: string, site: IxSite, binding: IxSite["in add(filter, key + "-allow-inbound", { ...inbound, "src-address-list": permitted, "in-interface": namespace, action: "accept", disabled: String(!policy.enabled) }) add(filter, key + "-deny-inbound", { ...inbound, action: "drop" }) + // The real VRF member is visible before routing and after routing, respectively. + add(mangle, "linux-mss-from-" + binding.name, { + ...clamp, ...ingress, "src-address-list": sources, + }) + add(mangle, "linux-mss-to-" + binding.name, { + ...clamp, chain: "postrouting", "out-interface": binding.name, "dst-address-list": sources, + }) if (!exit) return add("/routing/table", key + "-table", { name: privateTable, fib: "true" }) for (const [index, path] of paths.entries()) { @@ -122,7 +134,7 @@ function compileLinuxPolicy(namespace: string, site: IxSite, binding: IxSite["in }) } add(routePath(family), key + "-default-drop", { "routing-table": privateTable, "dst-address": defaultRoute[family], blackhole: "yes", distance: "254" }) - add(ip + "/firewall/mangle", key + "-route", { ...ingress, "src-address-list": sources, "dst-address-list": "!" + permitted, + add(mangle, key + "-route", { ...ingress, "src-address-list": sources, "dst-address-list": "!" + permitted, action: "mark-routing", "new-routing-mark": privateTable, passthrough: "false", disabled: String(!policy.enabled) }) const mainPaths = paths.filter(path => path.viaMain) for (const [index, path] of mainPaths.entries()) { diff --git a/packages/contracts/src/internet-exchange.ts b/packages/contracts/src/internet-exchange.ts index 0c0385ca..f69ff74e 100644 --- a/packages/contracts/src/internet-exchange.ts +++ b/packages/contracts/src/internet-exchange.ts @@ -48,6 +48,7 @@ const linuxPolicy = z.object({ clientId: z.string().uuid().optional(), enabled: z.boolean(), destinations: z.array(prefix).max(64), + tunnelMtu: z.number().int().min(1280).max(1420).optional(), exit: key.optional(), peerEndpoint: address.optional(), }).strict() @@ -152,6 +153,7 @@ export const ixLinuxClientRequest = z.object({ peerAddress: address.optional(), prefixes: z.array(prefix).max(64).default([]), destinations: linuxPolicy.shape.destinations.default([]), + tunnelMtu: linuxPolicy.shape.tunnelMtu, exit: key.optional(), transit: ixTransitOptions.optional(), rotate: z.boolean().optional(),