|
| 1 | +package init |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright The CryptOS Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/sha256" |
| 22 | + "encoding/binary" |
| 23 | + "errors" |
| 24 | + "log" |
| 25 | + "net" |
| 26 | + "net/netip" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/CryptOS-PKI/cryptos-node/internal/console" |
| 30 | + "github.com/CryptOS-PKI/cryptos-node/internal/init/netlink" |
| 31 | +) |
| 32 | + |
| 33 | +// linkLocalInterval is how often a maintenance node checks whether it needs a |
| 34 | +// self-assigned address. A laptop can be cabled in at any time after boot. |
| 35 | +const linkLocalInterval = 2 * time.Second |
| 36 | + |
| 37 | +// nicState is the part of an interface the link-local fallback reads. |
| 38 | +type nicState struct { |
| 39 | + Name string |
| 40 | + MAC net.HardwareAddr |
| 41 | + Up bool |
| 42 | + Carrier bool |
| 43 | + Loopback bool |
| 44 | + Addrs []net.Addr |
| 45 | +} |
| 46 | + |
| 47 | +// linkLocalDeps are the host operations the fallback needs, injectable so the |
| 48 | +// selection logic is tested without touching the host's interfaces. |
| 49 | +type linkLocalDeps struct { |
| 50 | + nics func() ([]nicState, error) |
| 51 | + linkUp func(name string) error |
| 52 | + addAddr func(name string, p netip.Prefix) error |
| 53 | +} |
| 54 | + |
| 55 | +// linkLocalAddr returns the IPv4 link-local address (RFC 3927) a node with |
| 56 | +// this MAC assigns itself: drawn from 169.254.1.0 to 169.254.254.255 and seeded |
| 57 | +// from the MAC, as RFC 3927 section 2.1 recommends, so a node comes back on the |
| 58 | +// same address every boot and the operator can keep using it. |
| 59 | +func linkLocalAddr(mac net.HardwareAddr) netip.Addr { |
| 60 | + sum := sha256.Sum256(mac) |
| 61 | + n := binary.BigEndian.Uint16(sum[:2]) % (254 * 256) |
| 62 | + return netip.AddrFrom4([4]byte{169, 254, byte(1 + n/256), byte(n % 256)}) |
| 63 | +} |
| 64 | + |
| 65 | +// step makes one pass. When no interface has a reachable IPv4 address (the |
| 66 | +// kernel's ip=dhcp got no answer, and nothing has been assigned yet), it brings |
| 67 | +// down Ethernet links up so their carrier shows, and gives the first one with |
| 68 | +// carrier its link-local address in 169.254.0.0/16. Only one interface gets one, |
| 69 | +// so the node never holds two connected routes for the same prefix. It returns |
| 70 | +// the prefix it assigned and the interface, or the zero prefix when it did |
| 71 | +// nothing. |
| 72 | +// |
| 73 | +// No ARP probe is sent before claiming the address: the case this serves is a |
| 74 | +// laptop cabled straight to the node, where the only other host on the link |
| 75 | +// picks its own address the same way, and a clash is a 1 in 65024 chance that |
| 76 | +// the operator sees at once and fixes by replugging. |
| 77 | +func (d linkLocalDeps) step() (netip.Prefix, string, error) { |
| 78 | + nics, err := d.nics() |
| 79 | + if err != nil { |
| 80 | + return netip.Prefix{}, "", err |
| 81 | + } |
| 82 | + var all []net.Addr |
| 83 | + for _, n := range nics { |
| 84 | + all = append(all, n.Addrs...) |
| 85 | + } |
| 86 | + if len(console.ReachableAddrs(all)) > 0 { |
| 87 | + return netip.Prefix{}, "", nil |
| 88 | + } |
| 89 | + var errs []error |
| 90 | + for _, n := range nics { |
| 91 | + if n.Loopback || len(n.MAC) != 6 { |
| 92 | + continue |
| 93 | + } |
| 94 | + if !n.Up { |
| 95 | + if err := d.linkUp(n.Name); err != nil { |
| 96 | + errs = append(errs, err) |
| 97 | + } |
| 98 | + continue |
| 99 | + } |
| 100 | + if !n.Carrier { |
| 101 | + continue |
| 102 | + } |
| 103 | + p := netip.PrefixFrom(linkLocalAddr(n.MAC), 16) |
| 104 | + if err := d.addAddr(n.Name, p); err != nil { |
| 105 | + return netip.Prefix{}, "", errors.Join(append(errs, err)...) |
| 106 | + } |
| 107 | + return p, n.Name, errors.Join(errs...) |
| 108 | + } |
| 109 | + return netip.Prefix{}, "", errors.Join(errs...) |
| 110 | +} |
| 111 | + |
| 112 | +// runLinkLocalFallback runs step every interval until ctx ends, so a maintenance |
| 113 | +// node with no DHCP server stays reachable on a self-assigned address. The |
| 114 | +// console picks the address up on its next refresh (console.ReachableAddrs). |
| 115 | +func runLinkLocalFallback(ctx context.Context, d linkLocalDeps, every time.Duration) { |
| 116 | + t := time.NewTicker(every) |
| 117 | + defer t.Stop() |
| 118 | + var lastErr string |
| 119 | + for { |
| 120 | + p, nic, err := d.step() |
| 121 | + if p.IsValid() { |
| 122 | + log.Printf("maintenance: no DHCP address; assigned link-local %s to %s", p, nic) |
| 123 | + } |
| 124 | + msg := "" |
| 125 | + if err != nil { |
| 126 | + msg = err.Error() |
| 127 | + } |
| 128 | + if msg != "" && msg != lastErr { |
| 129 | + log.Printf("maintenance: link-local fallback: %v", err) |
| 130 | + } |
| 131 | + lastErr = msg |
| 132 | + select { |
| 133 | + case <-ctx.Done(): |
| 134 | + return |
| 135 | + case <-t.C: |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// startLinkLocalFallback starts runLinkLocalFallback on this host's interfaces |
| 141 | +// and returns the func that stops it. |
| 142 | +func startLinkLocalFallback(ctx context.Context) func() { |
| 143 | + ctx, cancel := context.WithCancel(ctx) |
| 144 | + go runLinkLocalFallback(ctx, linkLocalDeps{ |
| 145 | + nics: localNICs, |
| 146 | + linkUp: netlink.SetLinkUp, |
| 147 | + addAddr: netlink.AddAddress, |
| 148 | + }, linkLocalInterval) |
| 149 | + return cancel |
| 150 | +} |
| 151 | + |
| 152 | +func localNICs() ([]nicState, error) { |
| 153 | + ifs, err := net.Interfaces() |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + out := make([]nicState, 0, len(ifs)) |
| 158 | + for _, ifc := range ifs { |
| 159 | + addrs, err := ifc.Addrs() |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + out = append(out, nicState{ |
| 164 | + Name: ifc.Name, |
| 165 | + MAC: ifc.HardwareAddr, |
| 166 | + Up: ifc.Flags&net.FlagUp != 0, |
| 167 | + Carrier: ifc.Flags&net.FlagRunning != 0, |
| 168 | + Loopback: ifc.Flags&net.FlagLoopback != 0, |
| 169 | + Addrs: addrs, |
| 170 | + }) |
| 171 | + } |
| 172 | + return out, nil |
| 173 | +} |
0 commit comments