Skip to content

Commit

Permalink
fix: parse properly IPv6 address in the cmdline ip= arg
Browse files Browse the repository at this point in the history
Fixes #4953

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Feb 10, 2022
1 parent d991f39 commit 6fadfa8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
30 changes: 29 additions & 1 deletion internal/app/machined/pkg/controllers/network/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ type CmdlineNetworking struct {
IgnoreInterfaces []string
}

// splitIPArgument splits the `ip=` kernel argument honoring the IPv6 addresses in square brackets.
func splitIPArgument(val string) []string {
var (
squared, prev int
parts []string
)

for i, c := range val {
switch c {
case '[':
squared++
case ']':
squared--
case ':':
if squared != 0 {
continue
}

parts = append(parts, strings.Trim(val[prev:i], "[]"))
prev = i + 1
}
}

parts = append(parts, strings.Trim(val[prev:], "[]"))

return parts
}

// ParseCmdlineNetwork parses `ip=` and Talos specific kernel cmdline argument producing all the available configuration options.
//
//nolint:gocyclo,cyclop
Expand Down Expand Up @@ -56,7 +84,7 @@ func ParseCmdlineNetwork(cmdline *procfs.Cmdline) (CmdlineNetworking, error) {

// https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
// ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip>:<ntp0-ip>
fields := strings.Split(*ipSettings, ":")
fields := splitIPArgument(*ipSettings)

// If dhcp is specified, we'll handle it as a normal discovered
// interface
Expand Down
12 changes: 12 additions & 0 deletions internal/app/machined/pkg/controllers/network/cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ func (suite *CmdlineSuite) TestParse() {
NTPAddresses: []netaddr.IP{netaddr.MustParseIP("10.0.0.1")},
},
},
{
name: "ipv6",
cmdline: "ip=[2001:db8::a]:[2001:db8::b]:[fe80::1]::master1:eth1::[2001:4860:4860::6464]:[2001:4860:4860::64]:[2001:4860:4806::]",
expectedSettings: network.CmdlineNetworking{
Address: netaddr.MustParseIPPrefix("2001:db8::a/128"),
Gateway: netaddr.MustParseIP("fe80::1"),
Hostname: "master1",
LinkName: "eth1",
DNSAddresses: []netaddr.IP{netaddr.MustParseIP("2001:4860:4860::6464"), netaddr.MustParseIP("2001:4860:4860::64")},
NTPAddresses: []netaddr.IP{netaddr.MustParseIP("2001:4860:4806::")},
},
},
{
name: "unparseable IP",
cmdline: "ip=xyz:",
Expand Down
3 changes: 3 additions & 0 deletions website/content/docs/v0.15/Reference/kernel.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Several of these are enforced by the Kernel Self Protection Project [KSPP](https
This parameter is useful in the environments where DHCP doesn't provide IP addresses or when default DNS and NTP servers should be overridden
before loading machine configuration.
Partial configuration can be applied as well, e.g. `ip=<:::::::<dns0-ip>:<dns1-ip>:<ntp0-ip>` sets only the DHCP and DNS servers.

IPv6 addresses can be specified by enclosing them in the square brackets, e.g. `ip=[2001:db8::a]:[2001:db8::b]:[fe80::1]::master1:eth1::[2001:4860:4860::6464]:[2001:4860:4860::64]:[2001:4860:4806::]`.

#### `panic`

The amount of time to wait after a panic before a reboot is issued.
Expand Down

0 comments on commit 6fadfa8

Please sign in to comment.