Class: Yast::RoutingClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
../../src/modules/Routing.rb

Constant Summary

ROUTES_DIR =

“routes” and ifroute-DEV file directory

"/etc/sysconfig/network"
ROUTES_FILE =

“routes” file location

"/etc/sysconfig/network/routes"
IPV4_SYSCTL =

sysctl keys, used as single SCR path components below

"net.ipv4.ip_forward"
IPV6_SYSCTL =
"net.ipv6.conf.all.forwarding"
SYSCTL_AGENT_PATH =

SCR paths

".etc.sysctl_conf"
SYSCTL_IPV4_PATH =
SYSCTL_AGENT_PATH + ".\"#{IPV4_SYSCTL}\""
SYSCTL_IPV6_PATH =
SYSCTL_AGENT_PATH + ".\"#{IPV6_SYSCTL}\""
ANY_DEVICE =

see man routes - difference on implicit device param (aka “-”) in case of /etc/sysconfig/network/routes and /etc/sysconfig/network/ /ifroute-<device>

"-"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Array<String>) devices (readonly)

Returns names of devices with sysconfig configuration

Returns:

  • (Array<String>)

    names of devices with sysconfig configuration



37
38
39
# File '../../src/modules/Routing.rb', line 37

def devices
  @devices
end

Instance Method Details

- (true, false) clear_route_file(device)

Clear file with routes definitions for particular device

Parameters:

  • device

    is a device name (eth0) or special string Routes::ANY_DEVICE

Returns:

  • (true, false)

    if succeedes



295
296
297
298
299
300
301
302
303
304
305
306
307
# File '../../src/modules/Routing.rb', line 295

def clear_route_file(device)
  # work around bnc#19476
  if device == ANY_DEVICE
    filename = ROUTES_FILE

    return SCR.Write(path(".target.string"), filename, "")
  else
    filename = "#{ROUTES_DIR}/ifroute-#{device}"

    return SCR.Execute(path(".target.remove"), filename) if FileUtils.Exists(filename)
    return true
  end
end

- (Object) Export

Dump the Routing settings to a map, for autoinstallation use.

Returns:

  • autoinstallation settings



391
392
393
394
395
396
397
398
399
# File '../../src/modules/Routing.rb', line 391

def Export
  exproute = {}

  exproute["routes"] = deep_copy(@Routes) unless @Routes.empty?
  exproute["ipv4_forward"] = @Forward_v4
  exproute["ipv6_forward"] = @Forward_v6

  exproute
end

- (Object) GetDevices

Get the current devices list

Returns:

  • devices list



403
404
405
# File '../../src/modules/Routing.rb', line 403

def GetDevices
  deep_copy(@devices)
end

- (Object) GetGateway

Get the default gateway

Returns:

  • gateway



409
410
411
412
413
414
415
416
417
# File '../../src/modules/Routing.rb', line 409

def GetGateway
  defgw = ""
  Builtins.maplist(@Routes) do |r|
    if Ops.get_string(r, "destination", "") == "default"
      defgw = Ops.get_string(r, "gateway", "")
    end
  end
  defgw
end

- (Object) Import(settings)

Get all the Routing configuration from a map. When called by routing_auto (preparing autoinstallation data) the map may be empty.

Parameters:

  • settings (Hash)

    autoinstallation settings

Returns:

  • true if success



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File '../../src/modules/Routing.rb', line 365

def Import(settings)
  settings = deep_copy(settings)

  # Getting a list of devices which have already been imported by Lan.Import
  # (bnc#900352)
  @devices = NetworkInterfaces.List("")

  ip_forward = Ops.get_boolean(settings, "ip_forward", false)
  ipv4_forward = Ops.get_boolean(settings, "ipv4_forward", ip_forward)
  ipv6_forward = Ops.get_boolean(settings, "ipv6_forward", ip_forward)

  @Routes = deep_copy(Ops.get_list(settings, "routes", []))
  @Forward_v4 = ipv4_forward
  @Forward_v6 = ipv6_forward

  @Orig_Routes = nil
  @Orig_Forward_v4 = nil
  @Orig_Forward_v6 = nil

  @modified = true

  true
end

- (Object) main



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
# File '../../src/modules/Routing.rb', line 66

def main
  Yast.import "UI"
  textdomain "network"

  Yast.import "NetHwDetection"
  Yast.import "NetworkInterfaces"
  Yast.import "Map"
  Yast.import "Mode"
  Yast.import "SuSEFirewall"
  Yast.import "FileUtils"

  Yast.include self, "network/runtime.rb"
  Yast.include self, "network/routines.rb"

  # All routes
  # list <map <string, string> >:
  # keys: destination, gateway, netmask, [device, [extrapara]]
  @Routes = []

  # Enable IP forwarding
  # .etc.sysctl_conf."net.ipv4.ip_forward"
  @Forward_v4 = false
  @Forward_v6 = false

  # List of available devices
  @devices = []
end

- (Object) Modified

Data was modified?

Returns:

  • true if modified



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File '../../src/modules/Routing.rb', line 96

def Modified
  # probably called without Read()  (bnc#649494)
  no_orig_values = @Orig_Routes.nil?
  no_orig_values &&= @Orig_Forward_v4.nil?
  no_orig_values &&= @Orig_Forward_v6.nil?
  no_orig_values &&= @modified != true

  return false if no_orig_values

  ret = @Routes != @Orig_Routes
  ret ||= @Forward_v4 != @Orig_Forward_v4
  ret ||= @Forward_v6 != @Orig_Forward_v6

  Builtins.y2debug("Routing#modified: #{ret}")
  ret
end

- (Object) Read

Read routing settings If no routes, sets a default gateway from Detection

Returns:

  • true if success



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File '../../src/modules/Routing.rb', line 211

def Read
  # read available devices
  NetworkInterfaces.Read
  @devices = NetworkInterfaces.List("")

  # read routes
  @Routes = SCR.Read(path(".routes")) || []

  @devices.each do |device|
    # Mode.test required for old testsuite. Dynamic agent registration break
    # stubing there
    register_ifroute_agent_for_device(device) unless Mode.test

    dev_routes = SCR.Read(path(".ifroute-#{device}")) || []

    next if dev_routes.nil? || dev_routes.empty?

    dev_routes.map! do |route|
      route["device"] = device if route["device"] == ANY_DEVICE
      route
    end

    @Routes.concat dev_routes
  end

  @Routes.uniq!
  log.info("Routes=#{@Routes}")

  ReadIPForwarding()

  # save routes to check for changes later
  @Orig_Routes = deep_copy(@Routes)
  @Orig_Forward_v4 = @Forward_v4
  @Orig_Forward_v6 = @Forward_v6

  ReadFromGateway(NetHwDetection.result["GATEWAY"] || "") if @Routes.empty?

  true
end

- (Object) ReadFromGateway(gw)

Set the routes to contain only the default route, if it is defined. Used when there is nothing better.

Parameters:

  • gw (String)

    ip of the default gateway

Returns:

  • true if success



117
118
119
120
121
122
123
124
125
126
127
128
# File '../../src/modules/Routing.rb', line 117

def ReadFromGateway(gw)
  return false if gw == "" || gw.nil?
  @Routes = [
    {
      "destination" => "default",
      "gateway"     => gw,
      "netmask"     => "-",
      "device"      => ANY_DEVICE
    }
  ]
  true
end

- (Object) ReadIPForwarding

Reads current status for both IPv4 and IPv6 forwarding



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File '../../src/modules/Routing.rb', line 147

def ReadIPForwarding
  if SuSEFirewall.IsEnabled
    @Forward_v4 = SuSEFirewall.GetSupportRoute
  else
    @Forward_v4 = SCR.Read(path(SYSCTL_IPV4_PATH)) == "1"
  end

  @Forward_v6 = SCR.Read(path(SYSCTL_IPV6_PATH)) == "1"

  log.info("Forward_v4=#{@Forward_v4}")
  log.info("Forward_v6=#{@Forward_v6}")

  nil
end

- (Object) RemoveDefaultGw

Remove route with default gateway from Routes list



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File '../../src/modules/Routing.rb', line 131

def RemoveDefaultGw
  route = []
  Builtins.y2milestone(
    "Resetting default gateway - interface has been set to DHCP mode"
  )
  Builtins.foreach(@Routes) do |row|
    if Ops.get_string(row, "destination", "") != "default"
      route = Builtins.add(route, row)
    end
  end
  @Routes = deep_copy(route)

  nil
end

- (Object) SetDevices(devs)

Set the available devices list (for expert routing dialog)

Parameters:

  • devs (Array)

    new devices list

Returns:

  • true if success



422
423
424
425
426
427
428
429
430
# File '../../src/modules/Routing.rb', line 422

def SetDevices(devs)
  devs = deep_copy(devs)
  if devs.nil?
    @devices = []
    return false
  end
  @devices = deep_copy(devs)
  true
end

- (Object) Summary

Create routing text summary



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File '../../src/modules/Routing.rb', line 434

def Summary
  return "" if @Routes.nil? || @Routes.empty?

  Yast.import "Summary"
  summary = ""

  gw = GetGateway()
  gwhost = NetHwDetection.ResolveIP(gw)
  gw = "#{gw} (#{gwhost})" unless gwhost.empty?

  # Summary text
  summary = Summary.AddListItem(summary, _("Gateway: %s") % gw) unless gw.empty?

  on_off = @Forward_v4 ? "on" : "off"
  # Summary text
  summary = Summary.AddListItem(summary, _("IP Forwarding for IPv4: %s") % on_off)

  on_off = @Forward_v6 ? "on" : "off"
  # Summary text
  summary = Summary.AddListItem(summary, _("IP Forwarding for IPv6: %s") % on_off)

  return "" if summary.empty?

  "<ul>#{summary}</ul>"
end

- (Object) Write

Write routing settings and apply changes

Returns:

  • true if success



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File '../../src/modules/Routing.rb', line 253

def Write
  Builtins.y2milestone("Writing configuration")
  if !Modified()
    Builtins.y2milestone("No changes to routing -> nothing to write")
    return true
  end

  steps = [
    # Progress stage 1
    _("Write IP forwarding settings"),
    # Progress stage 2
    _("Write routing settings")
  ]

  caption = _("Saving Routing Configuration")

  Progress.New(caption, " ", Builtins.size(steps), steps, [], "")

  # Progress stage 1/2
  ProgressNextStage(_("Writing IP forwarding settings..."))

  WriteIPForwarding()

  # at first stop the running routes
  # FIXME: SCR::Execute(.target.bash, "/etc/init.d/route stop");
  # sysconfig does not support restarting routes only,
  # so we let our caller do it together with other things

  # Progress stage 2/2
  ProgressNextStage(_("Writing routing settings..."))

  ret = write_routes(@Routes)

  Progress.NextStage

  ret
end

- (Object) write_ipv4_forwarding(forward_ipv4)

Configures system for IPv4 forwarding

Parameters:

  • true (Boolean)

    when forwarding should be enabled



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File '../../src/modules/Routing.rb', line 165

def write_ipv4_forwarding(forward_ipv4)
  sysctl_val = forward_ipv4 ? "1" : "0"

  if SuSEFirewall.IsEnabled
    SuSEFirewall.SetSupportRoute(forward_ipv4)
  else
    SCR.Write(
      path(SYSCTL_IPV4_PATH),
      sysctl_val
    )
    SCR.Write(path(SYSCTL_AGENT_PATH), nil)
  end

  SCR.Execute(path(".target.bash"), "sysctl -w #{IPV4_SYSCTL}=#{sysctl_val}")

  nil
end

- (Object) write_ipv6_forwarding(forward_ipv6)

Configures system for IPv6 forwarding

Parameters:

  • true (Boolean)

    when forwarding should be enabled



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File '../../src/modules/Routing.rb', line 186

def write_ipv6_forwarding(forward_ipv6)
  sysctl_val = forward_ipv6 ? "1" : "0"

  # SuSEfirewall2 has no direct support for IPv6 (aka FW_FORWARD).
  # Sysctl has to be configured manualy. bnc#916013
  SCR.Write(
    path(SYSCTL_IPV6_PATH),
    sysctl_val
  )
  SCR.Write(path(SYSCTL_AGENT_PATH), nil)

  SCR.Execute(path(".target.bash"), "sysctl -w #{IPV6_SYSCTL}=#{sysctl_val}")

  nil
end

- (true, false) write_route_file(device, routes)

From routes, select those belonging to device and write an appropriate config file.

Parameters:

  • device

    device name, or “-” for global routes

  • routes (Array<Hash>)

    defines route; even unrelated to device

Returns:

  • (true, false)

    if it succeedes



314
315
316
317
318
319
320
321
322
323
324
325
326
# File '../../src/modules/Routing.rb', line 314

def write_route_file(device, routes)
  routes = routes.select { |r| r["device"] == device }

  return clear_route_file(device) if routes.empty?

  if device == ANY_DEVICE
    scr_path = path(".routes")
  else
    scr_path = register_ifroute_agent_for_device(device)
  end

  SCR.Write(scr_path, routes)
end

- (true, false) write_routes(routes)

Updates routing configuration files

It means /etc/sysconfig/network/routes and /etc/sysconfig/network/ifroute-*

Parameters:

  • routes (Array)

    of hashes which defines route

Returns:

  • (true, false)

    if it succeedes



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File '../../src/modules/Routing.rb', line 335

def write_routes(routes)
  # create if not exists, otherwise backup
  if FileUtils.Exists(ROUTES_FILE)
    SCR.Execute(
      path(".target.bash"),
      "/bin/cp #{ROUTES_FILE} #{ROUTES_FILE}.YaST2save"
    )
  else
    SCR.Write(path(".target.string"), ROUTES_FILE, "")
  end

  ret = true

  # update the routes config
  Routing.devices.each do |device|
    written = write_route_file(device, routes)
    ret &&= written
  end

  written = write_route_file(ANY_DEVICE, routes)
  ret &&= written

  ret
end

- (Object) WriteIPForwarding

Configures system for both IPv4 and IPv6 forwarding



203
204
205
206
# File '../../src/modules/Routing.rb', line 203

def WriteIPForwarding
  write_ipv4_forwarding(@Forward_v4)
  write_ipv6_forwarding(@Forward_v6)
end