Class: Yast::NetworkInterfacesClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/NetworkInterfaces.rb

Constant Summary

ALIAS_SEPARATOR =

A single character used to separate alias id

"#"
TYPE_REGEX =
"(ip6tnl|mip6mnha|[#{String.CAlpha}]+)"
ID_REGEX =
"([^#{ALIAS_SEPARATOR}]*)"
ALIAS_REGEX =
"(.*)"
DEVNAME_REGEX =
"#{TYPE_REGEX}-?#{ID_REGEX}"
HOTPLUG_TYPES =

Supported hotplug types

["pcmcia", "usb"]

Instance Method Summary (collapse)

Instance Method Details

- (Object) Add

Add a new device

Returns:

  • true if success



1404
1405
1406
1407
1408
1409
# File '../../src/modules/NetworkInterfaces.rb', line 1404

def Add
  @operation = nil
  return false if Select("") != true
  @operation = :add
  true
end

- (Object) alias_name(typ, num, anum)

Create a alias name from its type and numbers

Examples:

alias_name(“eth”, “1”, “2”) -> “eth1#2”

Parameters:

  • typ (String)

    device type

  • num (String)

    device number

  • anum (String)

    alias number

Returns:

  • alias name



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File '../../src/modules/NetworkInterfaces.rb', line 477

def alias_name(typ, num, anum)
  if typ == nil || typ == ""
    Builtins.y2error("wrong type: %1", typ)
    return nil
  end
  if num == nil # || num < 0
    Builtins.y2error("wrong number: %1", num)
    return nil
  end
  if anum == nil || anum == ""
    Builtins.y2error("wrong alias number: %1", anum)
    return nil
  end
  Builtins.sformat("%1#%2", device_name(typ, num), anum)
end

- (Object) alias_num(dev)

Return a device alias number

Examples:

alias_num(“eth1#2”) -> “2”

alias_num(“eth1#blah”) -> “blah”

Parameters:

  • dev (String)

    device

Returns:

  • alias number



439
440
441
# File '../../src/modules/NetworkInterfaces.rb', line 439

def alias_num(dev)
  ifcfg_part(dev, "3")
end

- (Object) CanonicalizeIP(ifcfg)

Canonicalize static ip configuration obtained from sysconfig. (suse#46885)

Static ip configuration formats supported by sysconfig: 1) IPADDR=10.0.0.1/8 2) IPADDR=10.0.0.1 PREFIXLEN=8 3) IPADDR=10.0.0.1 NETMASK=255.0.0.0

Features: - IPADDR (in form <ip>/<prefix>) overrides PREFIXLEN,
- NETMASK is used only if prefix length unspecified) - If prefix length and NETMASK are unspecified, 32 is implied.

Canonicalize it to: - IPADDR="<ipv4>" PREFIXLEN="<prefix>" NETMASK="<netmask>") in case of IPv4 config E.g. IPADDR=10.0.0.1 PREFIXLEN=8 NETMASK=255.0.0.0 - IPADDR="<ipv6>" PREFIXLEN="<prefix>" NETMASK="") in case of IPv6 config E.g. IPADDR=2001:15c0:668e::5 PREFIXLEN=48 NETMASK=""

Parameters:

  • ifcfg

    a map with netconfig (ifcfg) configuration for a one device

Returns:

  • a map with IPADDR, NETMASK and PREFIXLEN adjusted if IPADDR is present. Returns original ifcfg if IPADDR is not present. In case of error, returns nil.



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File '../../src/modules/NetworkInterfaces.rb', line 593

def CanonicalizeIP(ifcfg)
  ifcfg = deep_copy(ifcfg)
  return nil if ifcfg == nil

  ip_and_prefix = Builtins.splitstring(
    Ops.get_string(ifcfg, "IPADDR", ""),
    "/"
  )
  ipaddr = Ops.get(ip_and_prefix, 0, "")
  return deep_copy(ifcfg) if ipaddr == "" # DHCP or inconsistent

  prefixlen = Ops.get(ip_and_prefix, 1, "")
  prefixlen = Ops.get_string(ifcfg, "PREFIXLEN", "") if prefixlen == ""

  if prefixlen == ""
    prefixlen = Builtins.tostring(
      Netmask.ToBits(Ops.get_string(ifcfg, "NETMASK", ""))
    )
  end

  # Now we have ipaddr and prefixlen
  # Let's compute the rest
  netmask = ""
  netmask = Netmask.FromBits(Builtins.tointeger(prefixlen)) if IP.Check4( ipaddr)

  Ops.set(ifcfg, "IPADDR", ipaddr)
  Ops.set(ifcfg, "PREFIXLEN", prefixlen)
  Ops.set(ifcfg, "NETMASK", netmask)

  ifcfg
end

- (Object) CanonicalizeStartmode(ifcfg)

STARTMODE: onboot, on and boot are aliases for auto



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File '../../src/modules/NetworkInterfaces.rb', line 553

def CanonicalizeStartmode(ifcfg)
  ifcfg = deep_copy(ifcfg)
  canonicalize_startmode = {
    "on"     => "auto",
    "boot"   => "auto",
    "onboot" => "auto"
  }
  startmode = Ops.get_string(ifcfg, "STARTMODE", "")
  Ops.set(
    ifcfg,
    "STARTMODE",
    Ops.get(canonicalize_startmode, startmode, startmode)
  )
  deep_copy(ifcfg)
end

- (Object) Change2(name, newdev, check)

Update Devices map

Parameters:

  • dev

    device identifier

  • newdev (Hash{String => Object})

    new device map

  • check (Boolean)

    if check if device already exists

Returns:

  • true if success



1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
# File '../../src/modules/NetworkInterfaces.rb', line 1436

def Change2(name, newdev, check)
  newdev = deep_copy(newdev)
  Builtins.y2debug("Change(%1,%2,%3)", name, newdev, check)
  Builtins.y2debug("Devices=%1", @Devices)
  if Check(name) && check
    Builtins.y2error("Device already present: %1", name)
    return false
  end

  t = !IsEmpty(newdev) ?
    GetTypeFromIfcfgOrName(name, newdev) :
    GetType(name)

  if name == @Name
    int_type = Ops.get_string(@Current, "INTERFACETYPE", "")

    t = int_type if Ops.greater_than(Builtins.size(int_type), 0)
  end
  a = alias_num(name)
  Builtins.y2debug("ChangeDevice(%1)", name)

  devsmap = Ops.get(@Devices, t, {})
  devmap = Ops.get(devsmap, name, {})
  amap = Ops.get_map(devmap, "_aliases", {})

  if a != ""
    Ops.set(amap, a, newdev)
    Ops.set(devmap, "_aliases", amap)
  else
    devmap = deep_copy(newdev)
  end

  Ops.set(devsmap, name, devmap)
  Ops.set(@Devices, t, devsmap)

  Builtins.y2debug("Devices=%1", @Devices)
  true
end

- (Object) Check(dev)

Check presence of the device (alias)

Parameters:

  • dev (String)

    device identifier

Returns:

  • true if device is present



1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
# File '../../src/modules/NetworkInterfaces.rb', line 1354

def Check(dev)
  Builtins.y2debug("Check(%1)", dev)
  typ = GetType(dev)
  #    string num = device_num(dev);
  #    string anum = alias_num(dev);
  return false if !Builtins.haskey(@Devices, typ)

  devsmap = Ops.get(@Devices, typ, {})
  return false if !Builtins.haskey(devsmap, dev)

  Builtins.y2debug("Check passed")
  true
end

- (Object) CleanCacheRead

re-read all settings again from system for creating new proposal from scratch (#170558)



749
750
751
752
# File '../../src/modules/NetworkInterfaces.rb', line 749

def CleanCacheRead
  @initialized = false
  Read()
end

Clean the hotplug devices compatibility symlink, usually ifcfg-eth-pcmcia -> ifcfg-eth-pcmcia-0.

Returns:

  • true if success



1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
# File '../../src/modules/NetworkInterfaces.rb', line 1645

def CleanHotplugSymlink
  types = ["eth-pcmcia", "eth-usb", "tr-pcmcia", "tr-usb"]
  Builtins.maplist(types) do |t|
    link = Ops.add("/etc/sysconfig/network/ifcfg-", t)
    Builtins.y2debug("link=%1", link)
    lstat = Convert.to_map(SCR.Read(path(".target.lstat"), link))
    if Ops.get_boolean(lstat, "islink", false) == true
      file = Convert.to_string(SCR.Read(path(".target.symlink"), link))
      file = Ops.add("/etc/sysconfig/network/", file)
      Builtins.y2debug("file=%1", file)
      if Ops.greater_than(SCR.Read(path(".target.size"), file), -1)
        Builtins.y2milestone("Cleaning hotplug symlink")
        Builtins.y2milestone("Devices[%1]=%2", t, Ops.get(@Devices, t, {}))
        Ops.set(@Devices, t, Builtins.remove(Ops.get(@Devices, t, {}), ""))
        Builtins.y2milestone("Devices[%1]=%2", t, Ops.get(@Devices, t, {}))
      end
    end
  end

  Builtins.y2debug("Devices=%1", @Devices)
  true
end

- (Object) Commit



1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
# File '../../src/modules/NetworkInterfaces.rb', line 1515

def Commit
  Builtins.y2debug("Name=%1", @Name)
  Builtins.y2debug("Current=%1", @Current)
  Builtins.y2debug("Devices=%1", @Devices)
  Builtins.y2debug("Deleted=%1", @Deleted)
  Builtins.y2debug("operation=%1", @operation)

  if @operation == :add || @operation == :edit
    Change2(@Name, @Current, @operation == :add)
  elsif @operation == :delete
    Delete2(@Name)
  else
    Builtins.y2error("Unknown operation: %1 (%2)", @operation, @Name)
    return false
  end

  Builtins.y2debug("Devices=%1", @Devices)
  Builtins.y2debug("Deleted=%1", @Deleted)

  @Name = ""
  @Current = {}
  @operation = nil

  true
end

- (Object) ConcealSecrets(devs)

Conceal secret information, such as WEP keys, so that the output can be passed to y2log and bugzilla. (#65741)

Parameters:

  • devs (Hash)

    a two-level map of ifcfgs like Devices

Returns:

  • ifcfgs with secret fields masked out



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File '../../src/modules/NetworkInterfaces.rb', line 643

def ConcealSecrets(devs)
  devs = deep_copy(devs)
  return nil if devs == nil
  out = Builtins.mapmap(
    Convert.convert(
      devs,
      :from => "map",
      :to   => "map <string, map <string, map <string, any>>>"
    )
  ) do |t, tdevs|
    tout = Builtins.mapmap(tdevs) do |id, ifcfg|
      { id => ConcealSecrets1(ifcfg) }
    end
    { t => tout }
  end
  deep_copy(out)
end

- (Object) ConcealSecrets1(ifcfg)

Conceal secret information, such as WEP keys, so that the output can be passed to y2log and bugzilla.

Parameters:

  • ifcfg (Hash{String => Object})

    one ifcfg

Returns:

  • ifcfg with secret fields masked out



629
630
631
632
633
634
635
636
637
# File '../../src/modules/NetworkInterfaces.rb', line 629

def ConcealSecrets1(ifcfg)
  ifcfg = deep_copy(ifcfg)
  return nil if ifcfg == nil
  out = Builtins.mapmap(ifcfg) do |k, v|
    v = "CONCEALED" if Builtins.contains(@SensitiveFields, k) && v != ""
    { k => v }
  end
  deep_copy(out)
end

- (Object) Delete(name)

Delete the given device

Parameters:

  • dev

    device to delete

Returns:

  • true if success



1424
1425
1426
1427
1428
1429
# File '../../src/modules/NetworkInterfaces.rb', line 1424

def Delete(name)
  @operation = nil
  return false if Select(name) != true
  @operation = :delete
  true
end

- (Object) Delete2(name)



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File '../../src/modules/NetworkInterfaces.rb', line 1475

def Delete2(name)
  if !Check(name)
    Builtins.y2error("Device not found: %1", name)
    return false
  end

  t = GetType(name)
  #    string d = device_num(name);
  a = alias_num(name)
  devsmap = Ops.get(@Devices, t, {})

  if a != ""
    amap = Ops.get_map(devsmap, [name, "_aliases"], {})
    amap = Builtins.remove(amap, a)
    Ops.set(devsmap, [name, "_aliases"], amap)
  else
    devsmap = Builtins.remove(devsmap, name)
  end

  Ops.set(@Devices, t, devsmap)

  # Originally this avoided errors in the log when deleting an
  # interface that was not present at Read (had no ifcfg file).
  # #115448: OriginalDevices is not updated after Write so
  # returning to the network proposal and deleting a card would not work.
  Builtins.y2milestone("Deleting file: #{name}")
  @Deleted << name
  true
end

- (Object) DeleteAlias(device, aid)

Add the alias to the list of deleted items. Called when exiting from the aliases-of-device dialog.

48191



1508
1509
1510
1511
1512
1513
# File '../../src/modules/NetworkInterfaces.rb', line 1508

def DeleteAlias(device, aid)
  _alias = Builtins.sformat("%1#%2", device, aid)
  Builtins.y2milestone("Deleting alias: %1", _alias)
  @Deleted <<  _alias
  true
end

- (Object) device_name(typ, num)

Create a device name from its type and number

Examples:

device_name(“eth”, “1”) -> “eth1”

device_name(“lo”, “”) -> “lo”

Parameters:

  • typ (String)

    device type

  • num (String)

    device number

Returns:

  • device name



449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File '../../src/modules/NetworkInterfaces.rb', line 449

def device_name(typ, num)
  if typ == nil || typ == ""
    Builtins.y2error("wrong type: %1", typ)
    return nil
  end
  if num == nil
    Builtins.y2error("wrong number: %1", num)
    return nil
  end
  if Builtins.regexpmatch(num, "^[0-9]*$")
    return Builtins.sformat("%1%2", typ, num)
  end
  Builtins.sformat("%1-%2", typ, num)
end

- (Object) device_name_from_alias(alias_name)

Extracts device name from alias name

alias_name := <device_name>ALIAS_SEPARATOR<alias_name>



467
468
469
# File '../../src/modules/NetworkInterfaces.rb', line 467

def device_name_from_alias(alias_name)
  alias_name.sub(/#{ALIAS_SEPARATOR}.*/, "")
end

- (Object) device_num(dev)

Return a device number Obsolete: It is incompatible with new device naming scheme.

Examples:

device_num(“eth1”) -> “1”

device_num(“lo”) -> “”

Parameters:

  • dev (String)

    device

Returns:

  • device number



429
430
431
432
# File '../../src/modules/NetworkInterfaces.rb', line 429

def device_num(dev)
  Builtins.y2warning( "Do not use device_num.")
  ifcfg_part(dev, "2")
end

- (Object) device_type(dev)

Return a device type

Examples:

device_type(“eth1”) -> “eth”

device_type(“eth-pcmcia-0”) -> “eth”

Parameters:

  • dev (String)

    device

Returns:

  • device type



230
231
232
# File '../../src/modules/NetworkInterfaces.rb', line 230

def device_type(dev)
  ifcfg_part(dev, "1")
end

- (Object) Edit(name)

Edit the given device

Parameters:

  • dev

    device to edit

Returns:

  • true if success



1414
1415
1416
1417
1418
1419
# File '../../src/modules/NetworkInterfaces.rb', line 1414

def Edit(name)
  @operation = nil
  return false if Select(name) != true
  @operation = :edit
  true
end

- (Object) Export(devregex)

Export data

Returns:

  • dumped settings (later acceptable by Import())



1280
1281
1282
1283
1284
# File '../../src/modules/NetworkInterfaces.rb', line 1280

def Export(devregex)
  _Devs = Filter(@Devices, devregex)
  Builtins.y2debug("Devs=%1", _Devs)
  Convert.convert(_Devs, :from => "map", :to => "map <string, map>")
end

- (Object) Fastest

Find the fastest available device



1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
# File '../../src/modules/NetworkInterfaces.rb', line 1711

def Fastest
  ret = ""
  devices = List("")

  # Find the fastest device
  Builtins.foreach(@FastestTypes) { |num, type| Builtins.foreach(devices) do |dev|
    if ret == "" &&
        Builtins.regexpmatch(
          dev,
          Ops.add(Ops.add("^", Ops.get(@DeviceRegex, type, "")), "[0-9]*$")
        ) &&
        IsConnected(dev)
      ret = dev
    end
  end }

  Builtins.y2milestone("ret=%1", ret)
  ret
end

- (Object) FastestType(name)



1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
# File '../../src/modules/NetworkInterfaces.rb', line 1731

def FastestType(name)
  ret = ""
  Builtins.maplist(@FastestTypes) do |num, type|
    regex = Ops.get(@DeviceRegex, type, "")
    if ret == "" &&
        Builtins.regexpmatch(name, Ops.add(Ops.add("^", regex), "[0-9]*$"))
      ret = type
    end
  end

  ret
end

- (Object) Filter(devices, devregex)



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File '../../src/modules/NetworkInterfaces.rb', line 756

def Filter(devices, devregex)
  devices = deep_copy(devices)
  if devices == nil || devregex == nil || devregex == ""
    return deep_copy(devices)
  end

  regex = Ops.add(
    Ops.add("^(", Ops.get(@DeviceRegex, devregex, devregex)),
    ")[0-9]*$"
  )
  Builtins.y2debug("regex=%1", regex)
  devices = Builtins.filter(devices) do |file, devmap|
    Builtins.regexpmatch(file, regex) == true
  end
  Builtins.y2debug("devices=%1", devices)
  deep_copy(devices)
end

- (Object) FilterDevices(devregex)

Used in BuildSummary, BuildOverview



775
776
777
# File '../../src/modules/NetworkInterfaces.rb', line 775

def FilterDevices(devregex)
  Filter(@Devices, devregex)
end

- (Object) FilterNOT(devices, devregex)



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File '../../src/modules/NetworkInterfaces.rb', line 780

def FilterNOT(devices, devregex)
  devices = deep_copy(devices)
  return {} if devices == nil || devregex == nil || devregex == ""

  regex = Ops.add(
    Ops.add("^(", Ops.get(@DeviceRegex, devregex, devregex)),
    ")[0-9]*$"
  )
  Builtins.y2debug("regex=%1", regex)
  devices = Builtins.filter(devices) do |file, devmap|
    Builtins.regexpmatch(file, regex) != true
  end
  Builtins.y2debug("devices=%1", devices)
  deep_copy(devices)
end

- (Object) GetDeviceTypeName(dev)

Return device type in human readable form :-)

Examples:

GetDeviceTypeName(eth-bus-pci-0000:01:07.0) -> “Network Card”

GetDeviceTypeName(modem0) -> “Modem”

Parameters:

  • dev (String)

    device

Returns:

  • device type



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File '../../src/modules/NetworkInterfaces.rb', line 395

def GetDeviceTypeName(dev)
  # pppN must be tried before pN, modem before netcard
  if Builtins.regexpmatch(
      dev,
      Ops.add("^", Ops.get(@DeviceRegex, "modem", ""))
    )
    return _("Modem")
  elsif Builtins.regexpmatch(
      dev,
      Ops.add("^", Ops.get(@DeviceRegex, "netcard", ""))
    )
    return _("Network Card")
  elsif Builtins.regexpmatch(
      dev,
      Ops.add("^", Ops.get(@DeviceRegex, "isdn", ""))
    )
    return _("ISDN")
  elsif Builtins.regexpmatch(
      dev,
      Ops.add("^", Ops.get(@DeviceRegex, "dsl", ""))
    )
    return _("DSL")
  else
    return _("Unknown")
  end
end

- (Object) GetDeviceTypes

Return supported network device types (for type netcard) for this hardware



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
# File '../../src/modules/NetworkInterfaces.rb', line 1048

def GetDeviceTypes
  # common linux device types available on all architectures
  common_dev_types = ["eth", "tr", "vlan", "br", "tun", "tap", "bond"]

  # s390 specific device types
  s390_dev_types = ["hsi", "ctc", "escon", "ficon", "iucv", "qeth", "lcs"]

  # device types which cannot be present on s390 arch
  s390_unknown_dev_types = [
    "arc",
    "bnep",
    "dummy",
    "fddi",
    "myri",
    "usb",
    "wlan",
    "ib"
  ]

  # ia64 specific device types
  ia64_dev_types = ["xp"]

  dev_types = deep_copy(common_dev_types)

  if Arch.s390
    dev_types = Convert.convert(
      Builtins.merge(dev_types, s390_dev_types),
      :from => "list",
      :to   => "list <string>"
    )
  else
    if Arch.ia64
      dev_types = Convert.convert(
        Builtins.merge(dev_types, ia64_dev_types),
        :from => "list",
        :to   => "list <string>"
      )
    end

    dev_types = Convert.convert(
      Builtins.merge(dev_types, s390_unknown_dev_types),
      :from => "list",
      :to   => "list <string>"
    )
  end

  Builtins.foreach(dev_types) do |device|
    if !Builtins.contains(
        Builtins.splitstring(Ops.get(@DeviceRegex, "netcard", ""), "|"),
        device
      )
      Builtins.y2error(
        "%1 is not contained in DeviceRegex[\"netcard\"]",
        device
      )
    end
  end

  deep_copy(dev_types)
end

- (Object) GetDevTypeDescription(type, longdescr)

Return textual device type

Examples:

GetDevTypeDescription(“eth”, false) -> “Ethernet”

GetDevTypeDescription(“eth”, true) -> “Ethernet Network Card”

Parameters:

  • type (String)

    device type

  • type (String)

    description type

Returns:

  • textual form of device type



1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File '../../src/modules/NetworkInterfaces.rb', line 1115

def GetDevTypeDescription(type, longdescr)
  if Builtins.issubstring(type, "#")
    # Device type label
    # This is what used to be Virtual Interface (eth0:1).
    # In our data model, additional addresses for an interface
    # are represented as its sub-interfaces.
    # And also we frequently confuse "device" and "interface"
    # :-(
    return _("Additional Address")
  end

  device_types = {
    # Device type label
    "arc"   => [_("ARCnet"), _("ARCnet Network Card")],
    # Device type label
    "atm"   => [
      _("ATM"),
      _("Asynchronous Transfer Mode (ATM)")
    ],
    # Device type label
    "bnep"  => [
      _("Bluetooth"),
      _("Bluetooth Connection")
    ],
    # Device type label
    "bond"  => [_("Bond"), _("Bond Network")],
    # Device type label
    "ci"    => [
      _("CLAW"),
      _("Common Link Access for Workstation (CLAW)")
    ],
    # Device type label
    "contr" => [_("ISDN"), _("ISDN Card")],
    # Device type label
    "ctc"   => [
      _("CTC"),
      _("Channel to Channel Interface (CTC)")
    ],
    # Device type label
    "dsl"   => [_("DSL"), _("DSL Connection")],
    # Device type label
    "dummy" => [_("Dummy"), _("Dummy Network Device")],
    # Device type label
    "escon" => [
      _("ESCON"),
      _("Enterprise System Connector (ESCON)")
    ],
    # Device type label
    "eth"   => [
      _("Ethernet"),
      _("Ethernet Network Card")
    ],
    # Device type label
    "fddi"  => [_("FDDI"), _("FDDI Network Card")],
    # Device type label
    "ficon" => [
      _("FICON"),
      _("Fiberchannel System Connector (FICON)")
    ],
    # Device type label
    "hippi" => [
      _("HIPPI"),
      _("HIgh Performance Parallel Interface (HIPPI)")
    ],
    # Device type label
    "hsi"   => [
      _("Hipersockets"),
      _("Hipersockets Interface (HSI)")
    ],
    # Device type label
    "ippp"  => [_("ISDN"), _("ISDN Connection")],
    # Device type label
    "irlan" => [_("IrDA"), _("Infrared Network Device")],
    # Device type label
    "irda"  => [_("IrDA"), _("Infrared Device")],
    # Device type label
    "isdn"  => [_("ISDN"), _("ISDN Connection")],
    # Device type label
    "iucv"  => [
      _("IUCV"),
      _("Inter User Communication Vehicle (IUCV)")
    ],
    # Device type label
    "lcs"   => [_("OSA LCS"), _("OSA LCS Network Card")],
    # Device type label
    "lo"    => [_("Loopback"), _("Loopback Device")],
    # Device type label
    "modem" => [_("Modem"), _("Modem")],
    # Device type label
    "myri"  => [_("Myrinet"), _("Myrinet Network Card")],
    # Device type label
    "net"   => [_("ISDN"), _("ISDN Connection")],
    # Device type label
    "plip"  => [
      _("Parallel Line"),
      _("Parallel Line Connection")
    ],
    # Device type label
    "ppp"   => [_("Modem"), _("Modem")],
    # Device type label
    "qeth"  => [
      _("QETH"),
      _("OSA-Express or QDIO Device (QETH)")
    ],
    # Device type label
    "sit"   => [
      _("IPv6-in-IPv4"),
      _("IPv6-in-IPv4 Encapsulation Device")
    ],
    # Device type label
    "slip"  => [
      _("Serial Line"),
      _("Serial Line Connection")
    ],
    # Device type label
    "tr"    => [
      _("Token Ring"),
      _("Token Ring Network Card")
    ],
    # Device type label
    "usb"   => [_("USB"), _("USB Network Device")],
    # Device type label
    "vmnet" => [_("VMWare"), _("VMWare Network Device")],
    # Device type label
    "wlan"  => [
      _("Wireless"),
      _("Wireless Network Card")
    ],
    # Device type label
    "xp"    => [_("XPNET"), _("XP Network")],
    # Device type label
    "vlan"  => [_("VLAN"), _("Virtual LAN")],
    # Device type label
    "br"    => [_("Bridge"), _("Network Bridge")],
    # Device type label
    "tun"   => [_("TUN"), _("Network TUNnel")],
    # Device type label
    "tap"   => [_("TAP"), _("Network TAP")],
    # Device type label
    "ib"    => [_("InfiniBand"), _("InfiniBand Device")]
  }

  if Builtins.haskey(device_types, type)
    return Ops.get_string(
      device_types,
      [type, longdescr == true ? 1 : 0],
      ""
    )
  end

  type1 = String.FirstChunk(type, "-")
  if Builtins.haskey(device_types, type1)
    return Ops.get_string(
      device_types,
      [type1, longdescr == true ? 1 : 0],
      ""
    )
  end

  Builtins.y2error("Unknown type: %1", type)
  type
end

- (Object) GetEthTypeFromSysfs(dev)

Detects a subtype of Ethernet device type according /sys or /proc content



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File '../../src/modules/NetworkInterfaces.rb', line 235

def GetEthTypeFromSysfs(dev)
  sys_dir_path = Builtins.sformat("/sys/class/net/%1/", dev)

  if FileUtils.Exists(Ops.add(sys_dir_path, "wireless"))
    return "wlan"
  elsif FileUtils.Exists(Ops.add(sys_dir_path, "phy80211"))
    return "wlan"
  elsif FileUtils.Exists(Ops.add(sys_dir_path, "bridge"))
    return "br"
  elsif FileUtils.Exists(Ops.add(sys_dir_path, "bonding"))
    return "bond"
  elsif FileUtils.Exists(Ops.add(sys_dir_path, "tun_flags"))
    return "tap"
  elsif FileUtils.Exists(Ops.add("/proc/net/vlan/", dev))
    return "vlan"
  elsif FileUtils.Exists(Ops.add("/sys/devices/virtual/net/", dev)) &&
      Builtins.regexpmatch(dev, "dummy.*")
    return "dummy"
  else
    return "eth"
  end
end

- (Object) GetFreeDevice(type)

Return free device

Examples:

GetFreeDevice(“eth”) -> “1”

Parameters:

  • type (String)

    device type

Returns:

  • free device



1342
1343
1344
1345
1346
1347
1348
1349
# File '../../src/modules/NetworkInterfaces.rb', line 1342

def GetFreeDevice(type)
  Builtins.y2debug("type=%1", type)
  freedevs = GetFreeDevices(type, 1)
  ret = Ops.get(freedevs, 0)
  Builtins.y2error("Free device location error: %1", ret) if ret == nil
  Builtins.y2debug("Free device=%1", ret)
  ret
end

- (Object) GetFreeDevices(type, num)



1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
# File '../../src/modules/NetworkInterfaces.rb', line 1296

def GetFreeDevices(type, num)
  Builtins.y2debug("Devices=%1", @Devices)
  Builtins.y2debug("type,num=%1,%2", type, num)
  Builtins.y2debug("Devices[%1]=%2", type, Ops.get(@Devices, type, {}))

  curdevs = []
  Builtins.foreach(
    Convert.convert(
      Map.Keys(Ops.get(@Devices, type, {})),
      :from => "list",
      :to   => "list <string>"
    )
  ) do |dev|
    dev = device_num(dev) if Builtins.issubstring(dev, type)
    curdevs = Builtins.add(curdevs, dev)
  end

  i = 0
  count = 0
  ret = []

  # Hotpluggable devices
  if IsHotplug(type) && !Builtins.contains(curdevs, "")
    Builtins.y2debug("Added simple hotplug device")
    count = Ops.add(count, 1)
    ret = Builtins.add(ret, "")
  end

  # Remaining numbered devices
  while Ops.less_than(count, num)
    ii = Builtins.sformat("%1", i)
    if !Builtins.contains(curdevs, ii)
      ret = Builtins.add(ret, ii)
      count = Ops.add(count, 1)
    end
    i = Ops.add(i, 1)
  end

  Builtins.y2debug("Free devices=%1", ret)
  deep_copy(ret)
end

- (Object) GetIbTypeFromSysfs(dev)

Detects a subtype of InfiniBand device type according /sys or /proc content



259
260
261
262
263
264
265
266
267
268
269
# File '../../src/modules/NetworkInterfaces.rb', line 259

def GetIbTypeFromSysfs(dev)
  sys_dir_path = Builtins.sformat("/sys/class/net/%1/", dev)

  if FileUtils.Exists(Ops.add(sys_dir_path, "bonding"))
    return "bond"
  elsif FileUtils.Exists(Ops.add(sys_dir_path, "create_child"))
    return "ib"
  else
    return "ibchild"
  end
end

- (Array) GetIP(device)

get IP addres + additional IP addresses

Parameters:

  • identifier

    for network interface

Returns:

  • (Array)

    of IP addresses of selected interface



1559
1560
1561
1562
1563
1564
1565
1566
# File '../../src/modules/NetworkInterfaces.rb', line 1559

def GetIP(device)
  Select(device)
  ips = [GetValue(device, "IPADDR")]
  Builtins.foreach(Ops.get_map(@Current, "_aliases", {})) do |key, value|
    ips = Builtins.add(ips, Ops.get_string(value, "IPADDR", ""))
  end
  deep_copy(ips)
end

- (Object) GetType(dev)

Detects device type according cached data

If cached ifcfg for given device is found it is used as parameter for GetTypeFromIfcfgOrName( dev, ifcfg). Otherwise is device handled as unconfigured and result is equal to GetTypeFromIfcfgOrName( dev, nil)

Parameters:

  • dev

    device name

Returns:

  • detected device type



379
380
381
382
383
384
385
386
387
388
# File '../../src/modules/NetworkInterfaces.rb', line 379

def GetType(dev)
  type = GetTypeFromIfcfgOrName(dev, nil)

  Builtins.foreach(@Devices) do |dev_type, confs|
    ifcfg = Ops.get(confs, dev, {})
    type = GetTypeFromIfcfgOrName(dev, ifcfg) if !IsEmpty(ifcfg)
  end

  type
end

- (Object) GetTypeFromIfcfg(ifcfg)

Detects device type according given ifcfg configuration

Returns:

  • device type or nil if type cannot be recognized from ifcfg config



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File '../../src/modules/NetworkInterfaces.rb', line 320

def GetTypeFromIfcfg(ifcfg)
  ifcfg = deep_copy(ifcfg)
  type = nil

  return nil if IsEmpty(ifcfg)

  Builtins.foreach(@TypeByValueMatch) do |key_type|
    rule_key = Ops.get(key_type, 0, "")
    rule_value = Ops.get(key_type, 1, "")
    rule_type = Ops.get(key_type, 2, "")
    type = rule_type if Ops.get_string(ifcfg, rule_key, "") == rule_value
  end

  Builtins.foreach(@TypeByKeyExistence) do |key_type|
    rule_key = Ops.get(key_type, 0, "")
    rule_type = Ops.get(key_type, 1, "")
    type = rule_type if Ops.get_string(ifcfg, rule_key, "") != ""
  end

  Builtins.foreach(@TypeByKeyValue) do |rule_key|
    rule_type = Ops.get_string(ifcfg, rule_key, "")
    type = rule_type if rule_type != ""
  end

  type
end

- (Object) GetTypeFromIfcfgOrName(dev, ifcfg)

Detects device type according its name and ifcfg configuration.

Parameters:

  • dev

    device name

  • ifcfg

    device's ifcfg configuration

Returns:

  • device type



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File '../../src/modules/NetworkInterfaces.rb', line 352

def GetTypeFromIfcfgOrName(dev, ifcfg)
  ifcfg = deep_copy(ifcfg)
  return nil if IsEmpty(dev)

  type = GetTypeFromSysfs(dev)

  type = GetTypeFromIfcfg(ifcfg) if IsEmpty(type)

  type = device_type(dev) if type == nil

  Builtins.y2debug(
    "GetTypeFromIfcfgOrName: device='%1', type='%2'",
    dev,
    type
  )

  type
end

- (Object) GetTypeFromSysfs(dev)

Determines device type according /sys/class/net/<dev>/type value

Firstly, it uses /sys/class/net/<dev>/type for basic decision. Obtained values are translated to device type according <kernel src>/include/uapi/linux/if_arp.h. Sometimes it uses some other checks to specify a "subtype". E.g. in case of "eth" it checks for presence of "wireless" subdir to determine "wlan" device.

Returns:

  • return device type or nil if nothing known found



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File '../../src/modules/NetworkInterfaces.rb', line 279

def GetTypeFromSysfs(dev)
  sys_dir_path = Builtins.sformat("/sys/class/net/%1", dev)
  sys_type_path = Builtins.sformat("%1/type", sys_dir_path)

  return nil if IsEmpty(dev) || !FileUtils.Exists(sys_type_path)

  sys_type = Convert.to_string(
    SCR.Read(path(".target.string"), sys_type_path)
  )

  sys_type = sys_type != nil ?
    Builtins.regexpsub(sys_type, "(.*)\n", "\\1") :
    ""
  sys_type = String.CutBlanks(sys_type)

  type = nil

  case sys_type
    when "1"
      type = GetEthTypeFromSysfs(dev)
    when "32"
      type = GetIbTypeFromSysfs(dev)
    else
      type = Ops.get(@TypeBySysfs, sys_type)
  end

  Builtins.y2debug(
    "GetTypeFromSysFs: device='%1', sysfs type='%2', type='%3'",
    dev,
    sys_type,
    type
  )

  return nil if IsEmpty(type)

  type
end

- (Object) GetValue(name, key)



1541
1542
1543
1544
# File '../../src/modules/NetworkInterfaces.rb', line 1541

def GetValue(name, key)
  return nil if !Select(name)
  Ops.get_string(@Current, key, "")
end

- (Object) HotplugRegex(devs)

Create a list of hot-pluggable device names for the given devices



202
203
204
205
206
207
208
209
210
211
212
# File '../../src/modules/NetworkInterfaces.rb', line 202

def HotplugRegex(devs)
  return "" unless devs

  ret = ""
  devs.each do |dev|
    HOTPLUG_TYPES.each do |hot|
      ret += "|#{dev}-#{hot}|#{dev}-#{hot}-"
    end
  end
  ret
end

- (Object) ifcfg_part(ifcfg, part)



219
220
221
222
223
# File '../../src/modules/NetworkInterfaces.rb', line 219

def ifcfg_part(ifcfg, part)
  return "" if Builtins.regexpmatch(ifcfg, @ifcfg_name_regex) != true
  ret = Builtins.regexpsub(ifcfg, @ifcfg_name_regex, "\\#{part}")
  ret == nil ? "" : ret
end

- (Object) Import(devregex, devices)

Import data

All devices which confirms to <devregex> are silently removed from Devices and replaced by those supplied by <devices>.

Parameters:

  • settings

    settings to be imported

Returns:

  • true on success



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File '../../src/modules/NetworkInterfaces.rb', line 1003

def Import(devregex, devices)
  devices = deep_copy(devices)
  _Devs = FilterNOT(@Devices, devregex)
  Builtins.y2debug("Devs=%1", _Devs)

  devices = Builtins.mapmap(devices) do |typ, devsmap|
    {
      typ => Builtins.mapmap(
        Convert.convert(
          devsmap,
          :from => "map",
          :to   => "map <string, map <string, any>>"
        )
      ) do |num, config|
        config = CanonicalizeIP(config)
        config = CanonicalizeStartmode(config)
        { num => config }
      end
    }
  end

  @Devices = Convert.convert(
    Builtins.union(_Devs, devices),
    :from => "map",
    :to   => "map <string, map <string, map <string, any>>>"
  )

  if devices == nil || devices == {}
    # devices == $[] is used in lan_auto "Reset" as a way how to
    # rollback changes imported from AY
    @initialized = false
  else
    @initialized = true
  end

  Builtins.y2milestone(
    "NetworkInterfaces::Import - done, cache content: %1",
    @Devices
  )

  true
end

- (Object) IsConnected(dev)

Return matching inteface for this hardware ID (uses getcfg-interface) return interface name global string MatchInterface(string dev) { string cmd = "getcfg-interface " + dev; map dn =(map) SCR::Execute(.target.bash_output, cmd); string devname = deletechars(dn:"", "\n");

return devname;

} Test whether device is connected (Link:up) The info is taken from sysfs

Examples:

MatchInterface(“eth-id-00:01:DE:AD:BE:EF”) -> “eth0”

Parameters:

  • dev (String)

    unique device string

  • dev (String)

    unique device string

Returns:

  • true if connected



517
518
519
520
521
522
523
524
525
526
527
528
529
# File '../../src/modules/NetworkInterfaces.rb', line 517

def IsConnected(dev)
  if !Mode.testsuite
    cmd = Ops.add(Ops.add("cat /sys/class/net/", dev), "/carrier")

    ret = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd))
    Builtins.y2milestone("Sysfs returned %1", ret)

    return Builtins.deletechars(Ops.get_string(ret, "stdout", ""), "\n") == "1" ? true : false
  else
    #Assume all devices are connected in testsuite mode
    return true
  end
end

- (Object) IsEmpty(value)



214
215
216
217
# File '../../src/modules/NetworkInterfaces.rb', line 214

def IsEmpty(value)
  value = deep_copy(value)
  TypeRepository.IsEmpty(value)
end

- (Object) IsHotplug(type)

Test hotplugability of a device

Parameters:

  • type (String)

    device type

Returns:

  • true if hotpluggable



496
497
498
499
500
# File '../../src/modules/NetworkInterfaces.rb', line 496

def IsHotplug(type)
  return false if type == "" || type == nil
  return true if HOTPLUG_TYPES.any? {|t| type.end_with?(t)}
  false
end

- (Array) List(devregex)

Get devices of the given type

Parameters:

  • type

    devices type (“” for all)

Returns:

  • (Array)

    of found devices



1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
# File '../../src/modules/NetworkInterfaces.rb', line 1671

def List(devregex)
  ret = []
  if devregex == "" || devregex == nil
    Builtins.maplist(@Devices) do |t, d|
      Builtins.maplist(
        Convert.convert(
          Map.Keys(d),
          :from => "list",
          :to   => "list <string>"
        )
      ) { |device| Ops.set(ret, Builtins.size(ret), device) }
    end
  else
    # it's a regex for type, not the whole name
    regex = Ops.add(
      Ops.add("^(", Ops.get(@DeviceRegex, devregex, devregex)),
      ")$"
    )
    Builtins.maplist(@Devices) do |t, d|
      if Builtins.regexpmatch(t, regex)
        Builtins.maplist(
          Convert.convert(
            Map.Keys(d),
            :from => "list",
            :to   => "list <string>"
          )
        ) { |device| Ops.set(ret, Builtins.size(ret), device) }
      end
    end
  end
  ret = Builtins.filter(ret) do |row|
    next true if row != nil
    Builtins.y2error("Filtering out : %1", row)
    false
  end
  Builtins.y2debug("List(%1) = %2", devregex, ret)
  deep_copy(ret)
end

- (Object) ListDevicesExcept(dev)

list of all devices except given one by parameter dev also loopback is ommited



1774
1775
1776
1777
# File '../../src/modules/NetworkInterfaces.rb', line 1774

def ListDevicesExcept(dev)
  devices = Builtins.filter(LocateNOT("DEVICE", dev)) { |s| s != "lo" }
  deep_copy(devices)
end

- (Array) Locate(key, val)

Locate devices of the given type and value

Parameters:

  • key (String)

    device key

  • val (String)

    device value

Returns:

  • (Array)

    of devices with key=val



1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
# File '../../src/modules/NetworkInterfaces.rb', line 1573

def Locate(key, val)
  ret = []
  Builtins.maplist(@Devices) do |typ, devsmap|
    Builtins.maplist(
      Convert.convert(devsmap, :from => "map", :to => "map <string, map>")
    ) do |device, devmap|
      if Ops.get_string(devmap, key, "") == val
        ret = Builtins.add(ret, device)
      end
    end
  end

  deep_copy(ret)
end

- (Array) LocateNOT(key, val)

Locate devices of the given type and value

Parameters:

  • key (String)

    device key

  • val (String)

    device value

Returns:

  • (Array)

    of devices with key!=val



1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
# File '../../src/modules/NetworkInterfaces.rb', line 1592

def LocateNOT(key, val)
  ret = []
  Builtins.maplist(@Devices) do |typ, devsmap|
    Builtins.maplist(
      Convert.convert(devsmap, :from => "map", :to => "map <string, map>")
    ) do |device, devmap|
      if Ops.get_string(devmap, key, "") != val
        ret = Builtins.add(ret, device)
      end
    end
  end

  deep_copy(ret)
end

- (Object) LocateProvider(provider)

Check if any device is using the specified provider

Parameters:

  • provider (String)

    provider identification

Returns:

  • true if there is any



1610
1611
1612
1613
# File '../../src/modules/NetworkInterfaces.rb', line 1610

def LocateProvider(provider)
  devs = Locate("PROVIDER", provider)
  Ops.greater_than(Builtins.size(devs), 0)
end

- (Object) main



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File '../../src/modules/NetworkInterfaces.rb', line 50

def main
  textdomain "base"

  Yast.import "Arch"
  Yast.import "Map"
  Yast.import "Mode"
  Yast.import "Netmask"
  Yast.import "TypeRepository"
  Yast.import "FileUtils"
  Yast.import "IP"

  # Current device identifier
  # @example eth0, eth1:blah, lo, ...
  # Add, Edit and Delete copy the requested device info (via Select)
  # to Name and Current,
  # Commit puts it back
  @Name = ""

  # Current device information
  # @example $["BOOTPROTO":"dhcp", "STARTMODE":"auto"]
  @Current = {}

  # Interface information:
  # Devices[string type, string id] is a map with the contents of
  # ifcfg-<i>type</i>-<i>id</i>. Separating type from id is useful because
  # the type determines the fields of the interface file.
  # Multiple addresses for an interface are nested maps
  # [type, id, "_aliases", aid]
  # @see #Read
  @Devices = {}

  # Devices information
  # @see #Read
  @OriginalDevices = {}

  # Deleted devices
  @Deleted = []

  # True if devices are already read
  @initialized = false

  # Which operation is pending?
  # global
  @operation = nil
  # FIXME: used in lan/address.ycp (#17346) -> "global"

  # Predefined network card regular expressions
  @CardRegex =
    # other: irlan|lo|plip|...
    {
      "netcard" => "arc|ath|bnep|ci|ctc|dummy|bond|escon|eth|fddi|ficon|hsi|qeth|lcs|iucv|myri|tr|usb|wlan|xp|vlan|br|tun|tap|ib|em|p|p[0-9]+p",
      "modem"   => "ppp|modem",
      "isdn"    => "isdn|ippp",
      "dsl"     => "dsl"
    }

  # Predefined network device regular expressions
  @DeviceRegex = {
    # device types
    "netcard" => Ops.add(
      Ops.add(
        Ops.get(@CardRegex, "netcard", ""),
        HotplugRegex(["ath", "eth", "tr", "wlan"])
      ),
      "|usb-usb|usb-usb-"
    ),
    "modem"   => Ops.get(@CardRegex, "modem", ""),
    "isdn"    => Ops.add(
      Ops.get(@CardRegex, "isdn", ""),
      HotplugRegex(["isdn", "ippp"])
    ),
    "dsl"     => Ops.get(@CardRegex, "dsl", ""),
    # device groups
    "dialup"  => Ops.add(
      Ops.add(
        Ops.add(
          Ops.add(Ops.get(@CardRegex, "modem", ""), "|"),
          Ops.get(@CardRegex, "dsl", "")
        ),
        "|"
      ),
      Ops.get(@CardRegex, "isdn", "")
    )
  }

  # Types in order from fastest to slowest.
  # @see #FastestRegexps
  @FastestTypes = { 1 => "dsl", 2 => "isdn", 3 => "modem", 4 => "netcard" }

  # @see #Push
  @stack = {}

  # -------------------- components of configuration names --------------------

  # ifcfg name = type + id + alias_id
  # If id is numeric, it is not separated from type, otherwise separated by "-"
  # Id may be empty
  # Alias_id, if nonempty, is separated by alias_separator
  @ifcfg_name_regex = "^#{DEVNAME_REGEX}#{ALIAS_SEPARATOR}?#{ALIAS_REGEX}$"

  # Translates type code exposed by kernel in sysfs onto internaly used dev types.
  @TypeBySysfs = {
    "1"     => "eth",
    "24"    => "eth",
    "32"    => "ib",
    "512"   => "ppp",
    "768"   => "ipip",
    "769"   => "ip6tnl",
    "772"   => "lo",
    "776"   => "sit",
    "778"   => "gre",
    "783"   => "irda",
    "801"   => "wlan_aux",
    "65534" => "tun"
  }

  @TypeByKeyValue = ["INTERFACETYPE"]
  @TypeByKeyExistence = [
    ["ETHERDEVICE", "vlan"],
    ["WIRELESS_MODE", "wlan"],
    ["MODEM_DEVICE", "ppp"]
  ]
  @TypeByValueMatch = [
    ["BONDING_MASTER", "yes", "bond"],
    ["BRIDGE", "yes", "br"],
    ["WIRELESS", "yes", "wlan"],
    ["TUNNEL", "tap", "tap"],
    ["TUNNEL", "tun", "tun"],
    ["TUNNEL", "sit", "sit"],
    ["TUNNEL", "gre", "gre"],
    ["TUNNEL", "ipip", "ipip"],
    ["PPPMODE", "pppoe", "ppp"],
    ["PPPMODE", "pppoatm", "ppp"],
    ["PPPMODE", "capi-adsl", "ppp"],
    ["PPPMODE", "pptp", "ppp"],
    ["ENCAP", "syncppp", "isdn"],
    ["ENCAP", "rawip", "isdn"]
  ]

  @SensitiveFields = [
    "WIRELESS_WPA_PASSWORD",
    "WIRELESS_WPA_PSK",
    # the unnumbered one should be empty but just in case
    "WIRELESS_KEY",
    "WIRELESS_KEY_0",
    "WIRELESS_KEY_1",
    "WIRELESS_KEY_2",
    "WIRELESS_KEY_3"
  ]
end

- (Object) Modified(devregex)

Were the devices changed?

Returns:

  • true if modified



1288
1289
1290
1291
1292
1293
1294
# File '../../src/modules/NetworkInterfaces.rb', line 1288

def Modified(devregex)
  _Devs = Filter(@Devices, devregex)
  _OriginalDevs = Filter(@OriginalDevices, devregex)
  Builtins.y2debug("OriginalDevs=%1", _OriginalDevs)
  Builtins.y2debug("Devs=%1", _Devs)
  _Devs == _OriginalDevs
end

- (Object) Pop



1756
1757
1758
1759
1760
1761
1762
1763
1764
# File '../../src/modules/NetworkInterfaces.rb', line 1756

def Pop
  Builtins.y2milestone("POP: %1", @stack)
  @Name = Ops.get_string(@stack, "Name", "")
  @Current = Ops.get_map(@stack, "Current", {})
  @operation = Ops.get_symbol(@stack, "operation")
  @stack = {}

  nil
end

- (Object) Push

DSL needs to save its config while the underlying network card is being configured.



1746
1747
1748
1749
1750
1751
1752
1753
1754
# File '../../src/modules/NetworkInterfaces.rb', line 1746

def Push
  Builtins.y2error("Stack not empty: %1", @stack) if @stack != {}
  Ops.set(@stack, "Name", @Name)
  Ops.set(@stack, "Current", @Current)
  Ops.set(@stack, "operation", @operation)
  Builtins.y2milestone("PUSH: %1", @stack)

  nil
end

- (Object) Read

Read devices from files

Returns:

  • true if sucess



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File '../../src/modules/NetworkInterfaces.rb', line 663

def Read
  return true if @initialized == true

  @Devices = {}

  # Variables which could be suffixed and thus duplicated
  _Locals = [
    "IPADDR",
    "REMOTE_IPADDR",
    "NETMASK",
    "PREFIXLEN",
    "BROADCAST",
    "SCOPE",
    "LABEL",
    "IP_OPTIONS"
  ]

  # preparation
  allfiles = SCR.Dir(path(".network.section"))
  allfiles = [] if allfiles == nil
  devices = Builtins.filter(allfiles) do |file|
    !Builtins.regexpmatch(file, "[~]")
  end
  Builtins.y2debug("devices=%1", devices)

  # Read devices
  Builtins.maplist(devices) do |d|
    pth = Ops.add(Ops.add(".network.value.\"", d), "\"")
    Builtins.y2debug("pth=%1", pth)
    values = SCR.Dir(Builtins.topath(pth))
    Builtins.y2debug("values=%1", values)
    config = {}
    Builtins.maplist(values) do |val|
      item = Convert.to_string(
        SCR.Read(Builtins.topath(Ops.add(Ops.add(pth, "."), val)))
      )
      Builtins.y2debug("item=%1", item)
      next if item == nil
      # No underscore '_' -> global
      # Also temporarily standard globals
      if Ops.less_than(Builtins.find(val, "_"), 0) ||
          Builtins.contains(_Locals, val)
        Ops.set(config, val, item)
        next
      end
      # Try to strip _suffix
      v = Builtins.substring(val, 0, Builtins.findlastof(val, "_"))
      s = Builtins.substring(val, Builtins.findlastof(val, "_"))
      s = Builtins.substring(s, 1) if Ops.greater_than(Builtins.size(s), 1)
      Builtins.y2milestone("%1:%2:%3", val, v, s)
      # Global
      if !Builtins.contains(_Locals, v)
        Ops.set(config, val, item)
      else
        __aliases = Ops.get_map(config, "_aliases", {})
        suf = Ops.get_map(__aliases, s, {})
        Ops.set(suf, v, item)
        Ops.set(__aliases, s, suf)
        Ops.set(config, "_aliases", __aliases)
      end
    end
    Builtins.y2milestone("config=%1", ConcealSecrets1(config))
    # canonicalize, #46885
    caliases = Builtins.mapmap(Ops.get_map(config, "_aliases", {})) do |a, c|
      { a => CanonicalizeIP(c) }
    end
    if caliases != {} # unconditionally?
      Ops.set(config, "_aliases", caliases)
    end
    config = CanonicalizeIP(config)
    config = CanonicalizeStartmode(config)
    devtype = GetTypeFromIfcfg(config)
    devtype = GetType(d) if devtype == nil
    dev = Ops.get(@Devices, devtype, {})
    Ops.set(dev, d, config)
    Ops.set(@Devices, devtype, dev)
  end
  Builtins.y2debug("Devices=%1", @Devices)

  @OriginalDevices = deep_copy(@Devices)
  @initialized = true
  true
end

- (Object) RealType(type, hotplug)

Return real type of the device (incl. PCMCIA, USB, …)

Examples:

RealType(“eth”, “usb”) -> “eth-usb”

Parameters:

  • type (String)

    basic device type

  • hotplug (String)

    hot plug type

Returns:

  • real type



536
537
538
539
540
541
542
543
544
545
546
547
548
# File '../../src/modules/NetworkInterfaces.rb', line 536

def RealType(type, hotplug)
  Builtins.y2debug("type=%1", type)
  if type == "" || type == nil
    Builtins.y2error("Wrong type: %1", type)
    return "eth"
  end

  return type if hotplug == "" || hotplug == nil

  realtype = Ops.add(Ops.add(type, "-"), hotplug)
  Builtins.y2debug("realtype=%1", realtype)
  realtype
end

- (Object) Select(name)

Select the given device

Parameters:

  • device

    to select (“” for new device, default values)

Returns:

  • true if success



1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
# File '../../src/modules/NetworkInterfaces.rb', line 1371

def Select(name)
  @Name = ""
  @Current = {}

  Builtins.y2debug("name=%1", name)
  if name != "" && !Check(name)
    Builtins.y2error("No such device: %1", name)
    return false
  end

  @Name = name
  t = GetType(@Name)
  @Current = Ops.get(@Devices, [t, @Name], {})
  a = alias_num(@Name)
  if a != nil && a != ""
    @Current = Ops.get_map(@Current, ["_aliases", a], {})
  end

  if @Current == {}
    # Default device map
    @Current =
      # FIXME: remaining items
      {}
  end

  Builtins.y2debug("Name=%1", @Name)
  Builtins.y2debug("Current=%1", @Current)

  true
end

- (Object) SetValue(name, key, value)



1546
1547
1548
1549
1550
1551
1552
1553
# File '../../src/modules/NetworkInterfaces.rb', line 1546

def SetValue(name, key, value)
  return nil unless Edit(name)
  return false if key == nil || key == "" || value == nil

  @Current[key] = value

  Commit()
end

Update /dev/modem symlink

Returns:

  • true if success



1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
# File '../../src/modules/NetworkInterfaces.rb', line 1617

def UpdateModemSymlink
  ret = false
  if Builtins.contains(Map.Keys(@Devices), "modem")
    ml = Map.Keys(Ops.get(@Devices, "modem", {}))
    ms = Ops.get_string(ml, 0, "0")
    # map mm = Devices["modem"]:$[][ms]:$[];
    mm = Ops.get(@Devices, ["modem", ms], {})
    mdev = Ops.get_string(mm, "MODEM_DEVICE", "")
    if mdev != "" && mdev != "/dev/modem"
      curlink = nil
      m = Convert.to_map(SCR.Read(path(".target.lstat"), "/dev/modem"))
      if Ops.get_boolean(m, "islink", false) == true
        curlink = Convert.to_string(
          SCR.Read(path(".target.symlink"), "/dev/modem")
        )
      end
      if curlink != mdev
        SCR.Execute(path(".target.symlink"), mdev, "/dev/modem")
        ret = true
      end
    end
  end
  ret
end

- (Object) ValidCharsIfcfg

46803: forbid “/” (filename), maybe also “-” (separator) “_” (escape)



1767
1768
1769
# File '../../src/modules/NetworkInterfaces.rb', line 1767

def ValidCharsIfcfg
  String.ValidCharsFilename
end

- (Object) Write(devregex)



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File '../../src/modules/NetworkInterfaces.rb', line 796

def Write(devregex)
  Builtins.y2milestone("Writing configuration")
  Builtins.y2debug("Devices=%1", @Devices)
  Builtins.y2debug("Deleted=%1", @Deleted)

  _Devs = Filter(@Devices, devregex)
  _OriginalDevs = Filter(@OriginalDevices, devregex)
  Builtins.y2milestone("OriginalDevs=%1", ConcealSecrets(_OriginalDevs))
  Builtins.y2milestone("Devs=%1", ConcealSecrets(_Devs))

  # Check for changes
  if _Devs == _OriginalDevs
    Builtins.y2milestone(
      "No changes to %1 devices -> nothing to write",
      devregex
    )
    return true
  end

  # remove deleted devices
  Builtins.y2milestone("Deleted=%1", @Deleted)
  Builtins.foreach(@Deleted) do |d|
    anum = alias_num(d)
    if anum == ""
      # delete config file
      p = Builtins.add(path(".network.section"), d)
      Builtins.y2debug("deleting: %1", p)
      SCR.Write(p, nil)
    else
      dev = device_name_from_alias(d)
      typ = GetType(dev)
      base = Builtins.add(path(".network.value"), dev)
      # look in OriginalDevs because we need to catch all variables
      # of the alias

      dev_aliases = _OriginalDevs[typ][dev]["_aliases"][anum] || {}
      dev_aliases.keys.each do |key|
        p = base + "#{key}_#{anum}"
        Builtins.y2debug("deleting: %1", p)
        SCR.Write(p, nil)
      end
    end
  end
  @Deleted = []

  # write all devices
  Builtins.maplist(
    Convert.convert(
      _Devs,
      :from => "map",
      :to   => "map <string, map <string, map <string, any>>>"
    )
  ) { |typ, devsmap| Builtins.maplist(devsmap) do |config, devmap|
    next if devmap == Ops.get_map(_OriginalDevs, [typ, config], {})
    # write sysconfig
    p = Ops.add(Ops.add(".network.value.\"", config), "\".")
    if Ops.greater_than(
        Builtins.size(Ops.get_string(devmap, "IPADDR", "")),
        0
      ) &&
        Builtins.find(Ops.get_string(devmap, "IPADDR", ""), "/") == -1
      if Ops.greater_than(
          Builtins.size(Ops.get_string(devmap, "IPADDR", "")),
          0
        ) &&
          Ops.greater_than(
            Builtins.size(Ops.get_string(devmap, "NETMASK", "")),
            0
          )
        Ops.set(
          devmap,
          "IPADDR",
          Builtins.sformat(
            "%1/%2",
            Ops.get_string(devmap, "IPADDR", ""),
            Netmask.ToBits(Ops.get_string(devmap, "NETMASK", ""))
          )
        )
        devmap = Builtins.remove(devmap, "NETMASK") 
        #TODO : delete NETMASK from config file
      else
        if Ops.greater_than(
            Builtins.size(Ops.get_string(devmap, "IPADDR", "")),
            0
          ) &&
            Ops.greater_than(
              Builtins.size(Ops.get_string(devmap, "PREFIXLEN", "")),
              0
            )
          Ops.set(
            devmap,
            "IPADDR",
            Builtins.sformat(
              "%1/%2",
              Ops.get_string(devmap, "IPADDR", ""),
              Ops.get_string(devmap, "PREFIXLEN", "")
            )
          )
          devmap = Builtins.remove(devmap, "PREFIXLEN") 
          #TODO : delete PREFIXLEN from config file
        end
      end
    end
    # write all keys to config
    Builtins.maplist(
      Convert.convert(
        Map.Keys(devmap),
        :from => "list",
        :to   => "list <string>"
      )
    ) do |k|
      # Write aliases
      if k == "_aliases"
        Builtins.maplist(Ops.get_map(devmap, k, {})) do |anum, amap|
          # Normally defaulting the label would be done
          # when creating the map, not here when
          # writing, but we create it in 2 ways so it's
          # better here. Actually it does not work because
          # the edit dialog nukes LABEL :-(
          if Ops.greater_than(Builtins.size(Ops.get(amap, "IPADDR", "")), 0) &&
              Ops.greater_than(
                Builtins.size(Ops.get(amap, "NETMASK", "")),
                0
              )
            Ops.set(
              amap,
              "IPADDR",
              Builtins.sformat(
                "%1/%2",
                Ops.get(amap, "IPADDR", ""),
                Netmask.ToBits(Ops.get(amap, "NETMASK", ""))
              )
            )
            amap = Builtins.remove(amap, "NETMASK") 
            #TODO : delete NETMASK from config file
          else
            if Ops.greater_than(
                Builtins.size(Ops.get(amap, "IPADDR", "")),
                0
              ) &&
                Ops.greater_than(
                  Builtins.size(Ops.get(amap, "PREFIXLEN", "")),
                  0
                )
              Ops.set(
                amap,
                "IPADDR",
                Builtins.sformat(
                  "%1/%2",
                  Ops.get(amap, "IPADDR", ""),
                  Ops.get(amap, "PREFIXLEN", "")
                )
              )
              amap = Builtins.remove(amap, "PREFIXLEN") 
              #TODO : delete PREFIXLEN from config file
            end
          end
          Builtins.maplist(amap) do |ak, av|
            akk = Ops.add(Ops.add(ak, "_"), anum)
            SCR.Write(Builtins.topath(Ops.add(p, akk)), av) #			    seen_label = seen_label || ak == "LABEL";
          end
        end
      else
        # Write regular keys
        SCR.Write(
          Builtins.topath(Ops.add(p, k)),
          Ops.get_string(devmap, k, "")
        )
      end
    end

    # 0600 if contains encryption key (#24842)
    has_key = Builtins.find(@SensitiveFields) do |k|
      Ops.get_string(devmap, k, "") != ""
    end != nil
    file = Ops.add("/etc/sysconfig/network/ifcfg-", config)
    if has_key
      Builtins.y2debug("Permission change: %1", config)
      SCR.Write(
        Builtins.add(path(".network.section_private"), config),
        true
      )
    end
    @OriginalDevices = {} if @OriginalDevices == nil
    if Ops.get(@OriginalDevices, typ) == nil
      Ops.set(@OriginalDevices, typ, {})
    end
    Ops.set(
      @OriginalDevices,
      [typ, config],
      Ops.get(@Devices, [typ, config], {})
    )
  end }

  # Finish him
  SCR.Write(path(".network"), nil)

  true
end