Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Sources/ContainerizationNetlink/NetlinkSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Logging
/// core high-level type offered to perform actions to the netlink surface in the kernel.
public struct NetlinkSession {
private static let receiveDataLength = 65536
private static let mtu: UInt32 = 1280
private let socket: any NetlinkSocket
private let log: Logger

Expand Down Expand Up @@ -70,7 +71,9 @@ public struct NetlinkSession {
public func linkSet(interface: String, up: Bool) throws {
// ip link set dev [interface] [up|down]
let interfaceIndex = try getInterfaceIndex(interface)
let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size
let mtuAttr = RTAttribute(
len: UInt16(RTAttribute.size + MemoryLayout<UInt32>.size), type: LinkAttributeType.IFLA_MTU)
let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size + mtuAttr.paddedLen
var requestBuffer = [UInt8](repeating: 0, count: requestSize)
var requestOffset = 0

Expand All @@ -89,6 +92,17 @@ public struct NetlinkSession {
change: InterfaceFlags.DEFAULT_CHANGE)
requestOffset = try requestInfo.appendBuffer(&requestBuffer, offset: requestOffset)

requestOffset = try mtuAttr.appendBuffer(&requestBuffer, offset: requestOffset)
guard
let newRequestOffset = requestBuffer.copyIn(
as: UInt32.self,
value: Self.mtu,
offset: requestOffset)
else {
throw NetlinkDataError.sendMarshalFailure
}
requestOffset = newRequestOffset

guard requestOffset == requestSize else {
throw Error.unexpectedOffset(offset: requestOffset, size: requestSize)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/ContainerizationNetlink/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct InterfaceFlags {

struct LinkAttributeType {
static let IFLA_EXT_IFNAME: UInt16 = 3
static let IFLA_MTU: UInt16 = 4
static let IFLA_EXT_MASK: UInt16 = 29
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/ContainerizationNetlinkTests/NetlinkSessionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct NetlinkSessionTest {
])

// Network down for interface.
let expectedDownRequest = "2000000010000500000000000cc00cc0110000000200000000000000ffffffff"
let expectedDownRequest = "2800000010000500000000000cc00cc0110000000200000000000000ffffffff0800040000050000"
mockSocket.responses.append([
0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x0c, 0xc0,
Expand Down Expand Up @@ -71,7 +71,7 @@ struct NetlinkSessionTest {
])

// Network up for interface.
let expectedUpRequest = "200000001000050000000000c00cc00c110000000200000001000000ffffffff"
let expectedUpRequest = "280000001000050000000000c00cc00c110000000200000001000000ffffffff0800040000050000"
mockSocket.responses.append([
0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0xc0, 0x0c, 0xc0, 0x0c,
Expand Down