|
1 | 1 | #!/usr/bin/env python3 |
2 | | - |
3 | 2 | import importlib.machinery |
4 | 3 | import json |
5 | 4 | import os |
@@ -359,49 +358,32 @@ def vpd_inject(out, vpds): |
359 | 358 |
|
360 | 359 |
|
361 | 360 | def fallback_base_mac(): |
362 | | - """Find MAC address of first suitable non-loopback interface. |
363 | | -
|
364 | | - Prefers real (globally unique) MACs over locally administered ones. |
365 | | - Prioritizes interface types: eth* > wan > wifi* > others. |
366 | | - """ |
| 361 | + """Find lowest valid MAC address from all interfaces.""" |
367 | 362 | base_path = '/sys/class/net' |
368 | | - interfaces = [] |
| 363 | + macs = [] |
369 | 364 |
|
370 | 365 | for iface in os.listdir(base_path): |
371 | | - if iface == 'lo': |
372 | | - continue |
373 | 366 | try: |
374 | | - # pylint: disable=invalid-name |
375 | 367 | fn = os.path.join(base_path, iface, 'address') |
376 | 368 | with open(fn, 'r', encoding='ascii') as f: |
377 | 369 | mac = f.read().strip() |
378 | 370 |
|
379 | | - # Check if locally administered (bit 1 of first octet is set) |
380 | | - first_byte = int(mac.split(':')[0], 16) |
381 | | - is_local = bool(first_byte & 0x02) |
382 | | - |
383 | | - # Prefer: eth* > wan > wifi* > others, then real MACs > local MACs |
384 | | - priority = 0 |
385 | | - if iface.startswith('eth'): |
386 | | - priority = 400 |
387 | | - elif iface == 'wan': |
388 | | - priority = 300 |
389 | | - elif iface.startswith('wifi'): |
390 | | - priority = 200 |
391 | | - else: |
392 | | - priority = 100 |
393 | | - |
394 | | - # Real MACs get +100 bonus |
395 | | - if not is_local: |
396 | | - priority += 100 |
397 | | - |
398 | | - interfaces.append((priority, iface, mac)) |
399 | | - except FileNotFoundError: |
| 371 | + # Skip invalid/empty/unset MAC addresses |
| 372 | + if not mac or len(mac) < 17 or mac == '00:00:00:00:00:00': |
| 373 | + continue |
| 374 | + |
| 375 | + # Validate MAC address format |
| 376 | + parts = mac.split(':') |
| 377 | + if len(parts) != 6: |
| 378 | + continue |
| 379 | + |
| 380 | + macs.append(mac) |
| 381 | + except (FileNotFoundError, ValueError): |
400 | 382 | continue |
401 | 383 |
|
402 | | - if interfaces: |
403 | | - interfaces.sort(reverse=True) # Highest priority first |
404 | | - return interfaces[0][2] # Return MAC |
| 384 | + if macs: |
| 385 | + macs.sort() |
| 386 | + return macs[0] |
405 | 387 |
|
406 | 388 | return None |
407 | 389 |
|
|
0 commit comments