-
Notifications
You must be signed in to change notification settings - Fork 34
/
Routing.rb
513 lines (427 loc) · 14.5 KB
/
Routing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
200
201
202
203
204
205
206
207
208
209
210
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
250
251
252
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
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
316
317
318
319
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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
421
422
423
424
425
426
427
428
429
430
431
432
433
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# encoding: utf-8
#***************************************************************************
#
# Copyright (c) 2012 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
#**************************************************************************
# File: modules/Routing.ycp
# Package: Network configuration
# Summary: Routing data (/etc/sysconfig/network/routes)
# Authors: Michal Svec <msvec@suse.cz>
#
#
# See routes(5)
# Does not work with interface-specific routes yet (ifroute-lo...)
require "yast"
module Yast
class RoutingClass < Module
# @return [Array<String>] names of devices with sysconfig configuration
attr_reader :devices
include Logger
# @Orig_Routes [Array] array of hashes. Caches known routes
#
# @Orig_Forward_v4 [Boolean] current status of ipv4 forwarding
# @Orig_Forward_v6 [Boolean] current status of ipv6 forwarding
#
# @modified [Boolean] modified by AY (bnc#649494)
# "routes" and ifroute-DEV file directory
ROUTES_DIR = "/etc/sysconfig/network"
# "routes" file location
ROUTES_FILE = "/etc/sysconfig/network/routes"
SYSCTL_IPV4_PATH = ".etc.sysctl_conf.\"net.ipv4.ip_forward\""
SYSCTL_IPV6_PATH = ".etc.sysctl_conf.\"net.ipv6.conf.all.forwarding\""
# see man routes - difference on implicit device param (aka "-") in
# case of /etc/sysconfig/network/routes and /etc/sysconfig/network/
# /ifroute-<device>
ANY_DEVICE = "-"
def main
Yast.import "UI"
textdomain "network"
Yast.import "NetHwDetection"
Yast.import "NetworkInterfaces"
Yast.import "Map"
Yast.import "Mode"
Yast.import "SuSEFirewall"
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
# Data was modified?
# @return true if modified
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
# Set the routes to contain only the default route, if it is defined.
# Used when there is nothing better.
# @param [String] gw ip of the default gateway
# @return true if success
def ReadFromGateway(gw)
return false if gw == "" || gw == nil
@Routes = [
{
"destination" => "default",
"gateway" => gw,
"netmask" => "-",
"device" => ANY_DEVICE
}
]
true
end
# Remove route with default gateway from Routes list
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
def ReadIPForwarding
if SuSEFirewall.IsEnabled
@Forward_v4 = SuSEFirewall.GetSupportRoute
# FIXME: missing support for setting IPv6 forwarding enablement in
# SuSEFirewall module and in SuSEFirewall2 at all
else
@Forward_v4 = SCR.Read(path(SYSCTL_IPV4_PATH)) == "1"
@Forward_v6 = SCR.Read(path(SYSCTL_IPV6_PATH)) == "1"
end
log.info("Forward_v4=#{@Forward_v4}")
log.info("Forward_v6=#{@Forward_v6}")
nil
end
def WriteIPForwarding
forward_ipv4 = @Forward_v4 ? "1" : "0"
forward_ipv6 = @Forward_v6 ? "1" : "0"
if SuSEFirewall.IsEnabled
# FIXME: missing support for setting IPv6 forwarding enablement in
# SuSEFirewall module and in SuSEFirewall2 at all
SuSEFirewall.SetSupportRoute(@Forward_v4)
else
SCR.Write(
path(SYSCTL_IPV4_PATH),
forward_ipv4
)
SCR.Write(
path(SYSCTL_IPV6_PATH),
forward_ipv6
)
SCR.Write(path(".etc.sysctl_conf"), nil)
end
SCR.Execute(
path(".target.bash"),
"echo #{forward_ipv4} > /proc/sys/net/ipv4/ip_forward"
)
SCR.Execute(
path(".target.bash"),
"echo #{forward_ipv6} > /proc/sys/net/ipv6/conf/all/forwarding",
)
nil
end
# Read routing settings
# If no routes, sets a default gateway from Detection
# @return true if success
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
if @Routes.empty?
ReadFromGateway(NetHwDetection.result["GATEWAY"] || "")
end
true
end
# Write routing settings and apply changes
# @return true if success
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
# From *routes*, select those belonging to *device* and write
# an appropriate config file.
# @param device device name, or "-" for global routes
# @param routes [Array<Hash>] defines route; even unrelated to *device*
# @return [true, false] if it succeedes
def write_route_file(device, routes)
routes = routes.select { |r| r["device"] == device }
if routes.empty?
# 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)
end
else
if device == ANY_DEVICE
scr_path = path(".routes")
else
scr_path = register_ifroute_agent_for_device(device)
end
return SCR.Write(scr_path, routes)
end
end
# Updates routing configuration files
#
# It means /etc/sysconfig/network/routes and
# /etc/sysconfig/network/ifroute-*
#
# @param routes [Array] of hashes which defines route
# @return [true, false] if it succeedes
def write_routes(routes)
# create if not exists, otherwise backup
if SCR.Read(path(".target.size"), ROUTES_FILE) > 0
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
return ret
end
# Get all the Routing configuration from a map.
# When called by routing_auto (preparing autoinstallation data)
# the map may be empty.
# @param [Hash] settings autoinstallation settings
# @return true if success
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
# Dump the Routing settings to a map, for autoinstallation use.
# @return autoinstallation settings
def Export
exproute = {}
exproute["routes"] = deep_copy(@Routes) unless @Routes.empty?
exproute["ipv4_forward"] = @Forward_v4
exproute["ipv6_forward"] = @Forward_v6
exproute
end
# Get the current devices list
# @return devices list
def GetDevices
deep_copy(@devices)
end
# Get the default gateway
# @return gateway
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
# Set the available devices list (for expert routing dialog)
# @param [Array] devs new devices list
# @return true if success
def SetDevices(devs)
devs = deep_copy(devs)
if devs == nil
@devices = []
return false
end
@devices = deep_copy(devs)
true
end
# Create routing text summary
# @returns [String] summary text
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
publish :variable => :Routes, :type => "list <map>"
publish :variable => :Forward_v4, :type => "boolean"
publish :variable => :Forward_v6, :type => "boolean"
publish :function => :Modified, :type => "boolean ()"
publish :function => :ReadFromGateway, :type => "boolean (string)"
publish :function => :RemoveDefaultGw, :type => "void ()"
publish :function => :Read, :type => "boolean ()"
publish :function => :Write, :type => "boolean ()"
publish :function => :Import, :type => "boolean (map)"
publish :function => :Export, :type => "map ()"
publish :function => :GetDevices, :type => "list ()"
publish :function => :GetGateway, :type => "string ()"
publish :function => :SetDevices, :type => "boolean (list)"
publish :function => :Summary, :type => "string ()"
private
def ifroute_term(device)
raise ArgumentError if device.nil? || device.empty?
non_empty_str_term = term(:String, "^ \t\n")
whitespace_term = term(:Whitespace)
optional_whitespace_term = term(:Optional, whitespace_term)
routes_content_term = term(
:List,
term(
:Tuple,
term(
:destination,
non_empty_str_term
),
whitespace_term,
term(:gateway, non_empty_str_term),
whitespace_term,
term(:netmask, non_empty_str_term),
optional_whitespace_term,
term(
:Optional,
term(:device, non_empty_str_term)
),
optional_whitespace_term,
term(
:Optional,
term(
:extrapara,
term(:String, "^\n")
)
)
),
"\n"
)
term(
:ag_anyagent,
term(
:Description,
term(:File, "#{ROUTES_DIR}/ifroute-#{device}"),
"#\n",
false,
routes_content_term
)
)
end
# Registers SCR agent which is used for accessing particular ifroute-device
# file
#
# @param device [String] device name (e.g. eth0, enp0s3, ...)
# @return [Path] SCR path of the agent
# @raise [RuntimeError] if it fails
def register_ifroute_agent_for_device(device)
scr_path = path(".ifroute-#{device}")
SCR.RegisterAgent(scr_path, ifroute_term(device)) or
raise "Cannot SCR.RegisterAgent(#{scr_path}, ...)"
scr_path
end
end
Routing = RoutingClass.new
Routing.main
end