summaryrefslogtreecommitdiff
path: root/scripts/vyatta_net_name
blob: f4251892f0eacb9a968809fce212300691150600 (plain)
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
#! /usr/bin/perl

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 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.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc.
# All Rights Reserved.

use strict;
use lib "/opt/vyatta/share/perl5/";
use XorpConfigParser;
use Vyatta::Config;
use Sys::Syslog qw(:standard :macros);
use Fcntl qw(:flock);

my $BOOTFILE    = "/opt/vyatta/etc/config/config.boot";
my $VYATTACFG   = "/opt/vyatta/config/active";

my $UDEVDIR     = "/run/udev/";
my $VYATTAUDEV	= $UDEVDIR . "vyatta";
my $LOCKFILE	= $UDEVDIR . ".vyatta-lock";
my $UDEVLOG	= $UDEVDIR . "log/";
my $LOGFILE     = $UDEVLOG . "vyatta-net-name.coldplug";

# Check if interface name is free to use
sub is_available {
    my ($interfaces, $ifname) = @_;

    my $count = grep { $_ eq $ifname } values %$interfaces;
    return ($count == 0);
}

# Find next available interface name
sub find_available {
    my ($interfaces, $ifprefix) = @_;
    $ifprefix =~ s/\d+$//;

    for (my $id = 0; ; $id++) {
	my $ifname = sprintf("%s%d", $ifprefix, $id);

	# is it in Vyatta config?
	return $ifname if (is_available($interfaces, $ifname));
    }
}

# Find the hardware id in the parsed config node for interface
sub get_hwid_from_children {
    my $children = shift;

    foreach my $attr (@$children) {
	next unless ($attr->{'name'} =~ /^hw-id ([0-9a-f:]+)/);
	return $1;
    }

    return;	# not found
}


# Leave file for vyatta_interface_rescan
sub leave_rescan_hint {
    my ($ifname, $hwaddr) = @_;
    my $name = "$VYATTAUDEV/$ifname";

    mkdir($VYATTAUDEV);
    open (my $f, '>', $name)
	or die "Can't create $name : $!";

    print {$f} "$hwaddr\n";
    close $f;
    return 1;
}

# Use biosdevname program (ethernet only)
# to try and find name based on PCI slot and DMI info
sub biosdevname {
    my $ifname = shift;

    # biosdevname works only on ethernet devices
    return $ifname unless ($ifname =~ /^eth/);

    # Don't use biosdevname in Xen
    return $ifname if ( -d "/proc/xen" );

    # Let the interface name changes ordered by previous invocations of this
    # script complete before we call biosdevname.  If we don't, biosdevame
    # may generate incorrect name.
    sleep 1;

    my $biosname = `/sbin/biosdevname --policy all_ethN -i $ifname 2>/dev/null`;
    chomp $biosname;

    # if biosdevname has no answer it outputs a nothing
    return ($biosname eq '') ? $ifname : $biosname;
}

# parse vyatta config.boot
# if file does not then running before off livecd then return empty hash
sub parse_config_boot {
    my $interfaces = {};

    if ( -f $BOOTFILE ) {
	my $xcp = new XorpConfigParser();
	$xcp->parse($BOOTFILE);

	my $inode = $xcp->get_node(['interfaces']);
	if ($inode) {
	    foreach my $child (@{$inode->{'children'}}) {
		# is hwid defined in config?
		my $hwid = get_hwid_from_children($child->{'children'});
		next unless $hwid;
	    
		# split into type 'ethernet' and 'eth0'
		my ($type, $intf) = ($child->{'name'} =~ /^(\w+) (\w+)/);
		next unless defined($type);
		next unless ($type eq 'ethernet') || ($type eq 'wireless');

		$interfaces->{$hwid} = $intf;
	    }
	}
    }

    return $interfaces;
}

sub logit {
    my ($log, $msg) = @_;
    my $now = localtime;
    print $log "$now: $msg";
}

# Determine network name to use based on Vyatta config during boot
sub coldplug {
    my ($ifname, $hwaddr, $predef_ifname) = @_;

    # at this time root directory is read-only so use log file instead
    mkdir ($UDEVLOG);
    open (my $log, '>>', $LOGFILE)
	or die "Can't open $LOGFILE : $!";
    logit($log, "lookup $ifname $hwaddr\n");

    # parse config file to produce map of existing hw-id values
    my $interfaces = parse_config_boot();

    # is name already in config file
    my $newname = $interfaces->{$hwaddr};
    if ($newname) {
	logit($log, "use hw-id $hwaddr in config mapped to '$newname'\n");
	return $newname;
    }

    # add already assigned names
    if (opendir(my $dir, $VYATTAUDEV)) {
	foreach my $intf (grep { ! /^\./ } readdir($dir)) {
	    if (open (my $f, '<', "$VYATTAUDEV/$intf")) {
		my $hwid = <$f>;
		close $f;
		chomp $hwid;

		$interfaces->{$hwid} = $intf;
	    }
	}
    }

    if ($predef_ifname) {
        $newname = $predef_ifname;
        logit($log, "predefined interface name for $ifname returned '$newname'\n");
    }
    else {
        $newname = biosdevname($ifname);
        logit($log, "biosdevname for $ifname returned '$newname'\n");
    }

    unless (is_available($interfaces, $newname)) {
	$newname = find_available($interfaces, $newname);
    }

    logit($log, "new name for '$ifname' is '$newname'\n");
    close $log;

    leave_rescan_hint($newname, $hwaddr);

    return $newname;
}

# Determine name from active config
sub hotplug {
    my ($ifname, $hwaddr, $predef_ifname) = @_;

    # real filesystem available use real logging
    openlog("vyatta-net-name", "", LOG_DAEMON);

    # Parse active config
    my $cfg = new Vyatta::Config;
    $cfg->setLevel('interfaces');

    my $interfaces = {};
    foreach my $type ($cfg->listOrigNodes()) {
	next unless ($type eq 'ethernet') || ($type eq 'wireless');
	foreach my $intf ($cfg->listOrigNodes($type)) {
	    my $hwid = $cfg->returnOrigValue("$type $intf hw-id");
	    next unless $hwid;
	    # TBD this could be a hash with name and path?
	    $interfaces->{$hwid} = $intf;
	}
    }

    my $newname = $interfaces->{$hwaddr};
    if ($newname) {
	syslog(LOG_DEBUG, "use hw-id %s in config mapped to '%s'", $hwaddr, $newname);
	return $newname;
    }

    if ($predef_ifname) {
        $newname = $predef_ifname;
        syslog(LOG_DEBUG, "predefined interface name for %s returned '%s'", $ifname, $newname);
    }
    else{
        $newname = biosdevname($ifname);
        syslog(LOG_DEBUG, "biosdevname for %s returned '%s'", $ifname, $newname);
    }

    unless (is_available($interfaces, $newname)) {
	$newname = find_available($interfaces, $newname);
    }

    syslog(LOG_INFO, "new name for '%s' is '%s'", $ifname, $newname);

    return $newname;
}

my $LOCKF;
sub lock_file {
    open ($LOCKF, '>', $LOCKFILE)
	or die "Can't open $LOCKFILE : $!";

    flock ($LOCKF, LOCK_EX)
	or die "Can't lock $LOCKFILE : $!";

}

sub unlock_file {
    close $LOCKF;
    $LOCKF = undef;
}

# This script is called from udev with two or three arguments
# it outputs the new name (if any) to stdout
if ($#ARGV > 2 or $#ARGV < 1) {
    die "vyatta_net_name called with wrong args:" . join(' ', @ARGV) . "\n";
}

my $ifname = $ARGV[0];
my $hwaddr = $ARGV[1];

my $predef_ifname = "";
if ($ARGV[2]){
    $predef_ifname = $ARGV[2];
}

lock_file;
my $newname;
if ( -d $VYATTACFG ) {
    $newname = hotplug($ifname, $hwaddr, $predef_ifname);
} else {
    $newname = coldplug($ifname, $hwaddr, $predef_ifname);
}
unlock_file;

print "$newname\n" if ($newname);

exit 0;