Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 3 | from subprocess import call, Popen, PIPE |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 4 | from IPy import IP |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 5 | import sys |
| 6 | import subprocess |
| 7 | import dbus |
| 8 | import string |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 9 | import socket |
| 10 | import re |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 11 | import os |
| 12 | import fcntl |
| 13 | import glib |
| 14 | import gobject |
| 15 | import dbus.service |
| 16 | import dbus.mainloop.glib |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 17 | from ConfigParser import SafeConfigParser |
| 18 | import glob |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 19 | import obmc.mapper |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 20 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 21 | #MAC address mask for locally administered. |
| 22 | MAC_LOCAL_ADMIN_MASK = 0x20000000000 |
| 23 | BROADCAST_MAC = 0xFFFFFFFFFFFF |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 24 | DBUS_NAME = 'org.openbmc.NetworkManager' |
| 25 | OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 26 | INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface' |
| 27 | INVENTORY_ROOT = '/xyz/openbmc_project/inventory' |
| 28 | MAC_PROPERTY = 'MACAddress' |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 29 | |
| 30 | network_providers = { |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 31 | 'networkd' : { |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 32 | 'bus_name' : 'org.freedesktop.network1', |
| 33 | 'ip_object_name' : '/org/freedesktop/network1/network/default', |
| 34 | 'hw_object_name' : '/org/freedesktop/network1/link/_31', |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 35 | 'ip_if_name' : 'org.freedesktop.network1.Network', |
| 36 | 'hw_if_name' : 'org.freedesktop.network1.Link', |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 37 | 'method' : 'org.freedesktop.network1.Network.SetAddr' |
| 38 | }, |
| 39 | 'NetworkManager' : { |
| 40 | 'bus_name' : 'org.freedesktop.NetworkManager', |
| 41 | 'ip_object_name' : '/org/freedesktop/NetworkManager', |
| 42 | 'hw_object_name' : '/org/freedesktop/NetworkManager', |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 43 | 'ip_if_name' : 'org.freedesktop.NetworkManager', |
| 44 | 'hw_if_name' : 'org.freedesktop.NetworkManager', |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 45 | 'method' : 'org.freedesktop.NetworkManager' # FIXME: |
| 46 | }, |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | def getPrefixLen(mask): |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 50 | prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')]) |
| 51 | return prefixLen |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 52 | |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 53 | # Enable / Disable the UseDHCP setting in .network file |
| 54 | def modifyNetConfig(confFile, usentp): |
| 55 | parser = SafeConfigParser() |
| 56 | parser.optionxform = str |
| 57 | parser.read(confFile) |
| 58 | sections = parser.sections() |
| 59 | |
| 60 | if "Match" not in sections: |
| 61 | raise NameError, "[Match] section not found" |
| 62 | |
| 63 | interface = parser.get('Match', 'Name') |
| 64 | if interface == '': |
| 65 | raise NameError, "Invalid interface" |
| 66 | |
| 67 | if "DHCP" not in sections: |
| 68 | parser.add_section("DHCP") |
| 69 | if usentp.lower() == "yes": |
| 70 | parser.set('DHCP', 'UseNTP', "true") |
| 71 | elif usentp.lower() == "no": |
| 72 | parser.set('DHCP', 'UseNTP', "false") |
| 73 | |
| 74 | print "Updating" + confFile + '\n' |
| 75 | with open(confFile, 'wb') as configfile: |
| 76 | parser.write(configfile) |
| 77 | |
| 78 | rc = call(["ip", "addr", "flush", interface]) |
| 79 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 80 | rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) |
| 81 | return rc |
| 82 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 83 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 84 | # Get Mac address from the eeprom |
| 85 | def get_mac_from_eeprom(): |
| 86 | bus = dbus.SystemBus() |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 87 | mapper = obmc.mapper.Mapper(bus) |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 88 | |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 89 | # Get the inventory subtree, limited |
| 90 | # to objects that implement NetworkInterface. |
| 91 | for path, info in \ |
| 92 | mapper.get_subtree( |
| 93 | path=INVENTORY_ROOT, |
| 94 | interfaces=[INV_INTF_NAME]).iteritems(): |
| 95 | # Find a NetworkInterface with 'bmc' in the path. |
| 96 | if 'bmc' not in path: |
| 97 | continue |
| 98 | |
| 99 | # Only expecting a single service to implement |
| 100 | # NetworkInterface. Get the service connection |
| 101 | # from the mapper response |
| 102 | conn = info.keys()[0] |
| 103 | |
| 104 | # Get the inventory object implementing NetworkInterface. |
| 105 | obj = bus.get_object(conn, path) |
| 106 | |
| 107 | # Get the MAC address |
| 108 | mproxy = obj.get_dbus_method('Get', dbus.PROPERTIES_IFACE) |
| 109 | return mproxy(INV_INTF_NAME, MAC_PROPERTY) |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 110 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 111 | class IfAddr (): |
| 112 | def __init__ (self, family, scope, flags, prefixlen, addr, gw): |
| 113 | self.family = family |
| 114 | self.scope = scope |
| 115 | self.flags = flags |
| 116 | self.prefixlen = prefixlen |
| 117 | self.addr = addr |
| 118 | self.gw = gw |
| 119 | |
| 120 | class NetMan (dbus.service.Object): |
| 121 | def __init__(self, bus, name): |
| 122 | self.bus = bus |
| 123 | self.name = name |
| 124 | dbus.service.Object.__init__(self,bus,name) |
| 125 | |
| 126 | def setNetworkProvider(self, provider): |
| 127 | self.provider = provider |
| 128 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 129 | def _isvaliddev(self, device): |
| 130 | devices = os.listdir ("/sys/class/net") |
| 131 | if not device in devices : return False |
| 132 | else: return True |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 133 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 134 | def _ishwdev (self, device): |
| 135 | f = open ("/sys/class/net/"+device+"/type") |
| 136 | type = f.read() |
| 137 | return False if (int(type) == 772) else True |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 138 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 139 | def _isvalidmask (self, mask): |
| 140 | for x in mask.split('.'): |
| 141 | try: |
| 142 | y = int(x) |
| 143 | except: |
| 144 | return False |
| 145 | if y > 255: return False |
| 146 | return mask.count('.') == 3 |
| 147 | |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 148 | def validatemac(self, mac): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 149 | macre = '([a-fA-F0-9]{2}[:|\-]?){6}' |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 150 | if re.compile(macre).search(mac) is None: |
| 151 | raise ValueError("Malformed MAC address") |
| 152 | |
| 153 | # Don't allow Broadcast or global unique mac |
| 154 | int_mac = int(mac.replace(":", ""), 16) |
| 155 | if not (int_mac ^ BROADCAST_MAC): |
| 156 | raise ValueError("Given Mac is BroadCast Mac Address") |
| 157 | |
| 158 | if not int_mac & MAC_LOCAL_ADMIN_MASK: |
Dinesh Chinari | e8d4211 | 2017-06-18 23:28:32 -0500 | [diff] [blame] | 159 | eep_mac = get_mac_from_eeprom() |
| 160 | if eep_mac: |
| 161 | int_eep_mac = int(eep_mac, 16) |
| 162 | if int_eep_mac != int_mac: |
| 163 | raise ValueError("Given MAC address is neither a local Admin type \ |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 164 | nor is same as in eeprom") |
| 165 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 166 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 167 | def _isvalidipv4(self, ipstr, netmask): |
| 168 | ip_parts = ipstr.split(".") |
| 169 | if len(ip_parts) != 4: |
| 170 | return "Malformed" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 171 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 172 | first, second, third, fourth = [int(part) for part in ip_parts] |
| 173 | if first == 0 and second == 0 and third == 0 and fourth == 0: |
| 174 | return "Invalid" # "this" network disallowed |
| 175 | if first == 169 and second == 254: |
| 176 | return "Link Local" |
| 177 | if first >= 224: |
| 178 | return "Invalid" # class D multicast and class E disallowed |
| 179 | if first == 192 and second == 88 and third == 99: |
| 180 | return "Invalid" # ipv6 relay |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 181 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 182 | # check validity against netmask |
| 183 | if netmask != '0': |
| 184 | ip_bin = (first << 24) + (second << 16) + (third << 8) + fourth |
| 185 | mask_parts = netmask.split(".") |
| 186 | if len(mask_parts) == 4: # long form netmask |
| 187 | mask_bin = (int(mask_parts[0]) << 24) + (int(mask_parts[1]) << 16) + (int(mask_parts[2]) << 8) + int(mask_parts[3]) |
| 188 | elif netmask.count(".") == 0: # short form netmask |
| 189 | mask_bin = 0xffffffff ^ (1 << 32 - int(netmask)) - 1 |
| 190 | else: |
| 191 | return "Malformed" # bad netmask |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 192 | |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 193 | if ip_bin & ~mask_bin == 0: |
| 194 | return "Invalid" # disallowed by this netmask |
| 195 | if ip_bin | mask_bin == 0xFFFFFFFF: |
| 196 | return "Invalid" # disallowed by this netmask |
| 197 | |
| 198 | return "Valid" |
| 199 | |
| 200 | |
| 201 | def _isvalidip(self, ipaddr, netmask = '0'): |
| 202 | try: |
| 203 | ip = IP(ipaddr) |
| 204 | except ValueError: |
| 205 | return "Malformed" |
| 206 | |
| 207 | ipstr = ip.strNormal(0) |
| 208 | ipstr_masked = ip.strNormal(2) |
| 209 | if ipstr_masked.count("/") != 0 and netmask == '0': |
| 210 | netmask = ipstr_masked.split("/")[1] |
| 211 | |
| 212 | if ip.version() == 4: # additional checks for ipv4 |
| 213 | return self._isvalidipv4(ipstr, netmask) |
| 214 | # TODO: check ipv6 openbmc/openbmc#496 |
| 215 | |
| 216 | return "Valid" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 217 | |
| 218 | def _getAddr (self, target, device): |
| 219 | netprov = network_providers [self.provider] |
| 220 | bus_name = netprov ['bus_name'] |
| 221 | |
| 222 | if (target == "ip"): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 223 | ipaddr = "" |
| 224 | defgw = "" |
| 225 | prefixlen = "0" |
| 226 | |
| 227 | proc = subprocess.Popen(["ip", "addr", "show", "dev", device], stdout=PIPE) |
| 228 | procout = proc.communicate() |
| 229 | if procout: |
| 230 | ipout = procout[0].splitlines()[2].strip() |
| 231 | ipaddr,prefixlen = ipout.split ()[1].split("/") |
| 232 | |
| 233 | proc = subprocess.Popen(["ip", "route", "show", "dev", device, "default", "0.0.0.0/0"], stdout=PIPE) |
| 234 | procout = proc.communicate() |
| 235 | if procout[0]: |
| 236 | ipout = procout[0].splitlines()[0].strip() |
| 237 | defgw = ipout.split ()[2] |
| 238 | |
| 239 | return 2, int(prefixlen), ipaddr, defgw |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 240 | |
| 241 | if (target == "mac"): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 242 | proc = subprocess.Popen(["ip", "link", "show", "dev", device], stdout=PIPE) |
| 243 | ipout = proc.communicate()[0].splitlines()[1].strip() |
| 244 | mac = ipout.split ()[1] |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 245 | return mac |
| 246 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 247 | @dbus.service.method(DBUS_NAME, "", "") |
| 248 | def test(self): |
| 249 | print("TEST") |
| 250 | |
Vishwanatha Subbanna | 92a5d1e | 2016-08-30 21:23:32 +0530 | [diff] [blame] | 251 | @dbus.service.method(DBUS_NAME, "sas", "x") |
| 252 | def SetNtpServer (self, device, ntpservers): |
| 253 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 254 | |
| 255 | # Convert the array into space separated value string |
| 256 | ntp_ip = " ".join(ntpservers) |
| 257 | if not ntp_ip : raise ValueError, "Invalid Data" |
| 258 | |
| 259 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
| 260 | |
| 261 | parser = SafeConfigParser() |
| 262 | parser.optionxform = str |
| 263 | parser.read(confFile) |
| 264 | sections = parser.sections() |
| 265 | if "Match" not in sections: |
| 266 | raise NameError, "[Match] section not found" |
| 267 | |
| 268 | interface = parser.get('Match', 'Name') |
| 269 | if interface != device: |
| 270 | raise ValueError, "Device [" + device + "] Not Configured" |
| 271 | |
| 272 | if "Network" not in sections: |
| 273 | raise NameError, "[Network] section not found" |
| 274 | |
| 275 | parser.set('Network', 'NTP', ntp_ip) |
| 276 | print "Updating " + confFile + '\n' |
| 277 | with open(confFile, 'wb') as configfile: |
| 278 | parser.write(configfile) |
| 279 | rc = call(["ip", "addr", "flush", device]) |
| 280 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 281 | rc = call(["systemctl", "try-restart", "systemd-timesyncd.service"]) |
| 282 | return rc |
| 283 | |
| 284 | @dbus.service.method(DBUS_NAME, "s", "x") |
| 285 | def UpdateUseNtpField (self, usentp): |
| 286 | filelist = glob.glob("/etc/systemd/network/*.network") |
| 287 | for configfile in filelist: |
| 288 | modifyNetConfig(configfile,usentp) |
| 289 | return 0 |
| 290 | |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 291 | @dbus.service.method(DBUS_NAME, "s", "x") |
| 292 | def EnableDHCP (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 293 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 294 | |
| 295 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 296 | |
| 297 | print("Making .network file...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 298 | try: |
| 299 | networkconf = open (confFile, "w+") |
| 300 | except IOError: |
| 301 | raise IOError, "Failed to open " + confFile |
| 302 | |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 303 | networkconf.write ('[Match]'+ '\n') |
| 304 | networkconf.write ('Name=' + (device) + '\n') |
| 305 | networkconf.write ('[Network]' + '\n') |
Ratan Gupta | 968d203 | 2017-03-24 19:59:44 +0530 | [diff] [blame] | 306 | networkconf.write ('DHCP=yes' + '\n') |
Patrick Williams | ec01f6e | 2017-03-08 22:00:23 -0600 | [diff] [blame] | 307 | networkconf.write ('[DHCP]' + '\n') |
| 308 | networkconf.write ('ClientIdentifier=mac' + '\n') |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 309 | networkconf.close () |
| 310 | |
| 311 | print("Restarting networkd service...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 312 | rc = call(["ip", "addr", "flush", device]) |
| 313 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 314 | return rc |
Hariharasubramanian R | 3a224e7 | 2016-02-10 12:32:08 -0600 | [diff] [blame] | 315 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 316 | @dbus.service.method(DBUS_NAME, "ssss", "x") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 317 | def SetAddress4 (self, device, ipaddr, netmask, gateway): |
| 318 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 319 | if not self._isvalidmask (netmask) : raise ValueError, "Invalid Mask" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 320 | prefixLen = getPrefixLen (netmask) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 321 | if prefixLen == 0: raise ValueError, "Invalid Mask" |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 322 | valid = self._isvalidip (ipaddr, netmask) |
| 323 | if valid != "Valid": raise ValueError, valid + " IP Address" |
| 324 | valid = self._isvalidip (gateway) |
| 325 | if valid != "Valid": raise ValueError, valid + " IP Address" |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 326 | |
| 327 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 328 | |
| 329 | print("Making .network file...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 330 | try: |
| 331 | networkconf = open (confFile, "w+") |
| 332 | except IOError: |
| 333 | raise IOError, "Failed to open " + confFile |
| 334 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 335 | networkconf.write ('[Match]'+ '\n') |
| 336 | networkconf.write ('Name=' + (device) + '\n') |
| 337 | networkconf.write ('[Network]' + '\n') |
| 338 | networkconf.write ('Address=' + ipaddr + '/' + str(prefixLen) + '\n') |
| 339 | networkconf.write ('Gateway=' + gateway + '\n') |
Chris Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 340 | networkconf.close() |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 341 | |
| 342 | print("Restarting networkd service...") |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 343 | rc = call(["ip", "addr", "flush", device]) |
| 344 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
| 345 | return rc |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 346 | |
Hariharasubramanian R | cc28a6f | 2016-04-29 12:25:12 -0500 | [diff] [blame] | 347 | @dbus.service.method(DBUS_NAME, "s", "s") |
| 348 | def GetAddressType (self, device): |
| 349 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 350 | |
| 351 | confFile = "/etc/systemd/network/00-bmc-" + device + ".network" |
| 352 | if not os.path.exists(confFile): |
| 353 | print "Config file (%s) not found !" % confFile |
| 354 | netprov = network_providers [self.provider] |
| 355 | bus_name = netprov ['bus_name'] |
| 356 | obj_name = netprov ['ip_object_name'] |
| 357 | o = self.bus.get_object(bus_name, obj_name, introspect=False) |
| 358 | i = dbus.Interface(o, 'org.freedesktop.DBus.Properties') |
| 359 | f = i.Get (netprov ['ip_if_name'], "SourcePath") |
| 360 | print "Using default networkd config file (%s)" % f |
| 361 | confFile = f |
| 362 | |
| 363 | with open(confFile, "r") as f: |
| 364 | for line in f: |
| 365 | config = line.split ("=") |
| 366 | if (len (config) < 2) : continue |
| 367 | if config [0].upper() == "DHCP": |
| 368 | v = config[1].strip().upper() |
| 369 | if (v=="YES" or v=="IPV4" or v=="IPV6"): |
| 370 | return "DHCP" |
| 371 | return "STATIC" |
| 372 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 373 | #family, prefixlen, ip, defgw |
| 374 | @dbus.service.method(DBUS_NAME, "s", "iyss") |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 375 | def GetAddress4 (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 376 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 377 | return self._getAddr ("ip", device) |
| 378 | |
| 379 | @dbus.service.method(DBUS_NAME, "s", "s") |
| 380 | def GetHwAddress (self, device): |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 381 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 382 | return self._getAddr ("mac", device) |
| 383 | |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 384 | @dbus.service.method(DBUS_NAME, "ss", "i") |
| 385 | def SetHwAddress (self, device, mac): |
| 386 | if not self._isvaliddev (device) : raise ValueError, "Invalid Device" |
| 387 | if not self._ishwdev (device) : raise ValueError, "Not a Hardware Device" |
Ratan Gupta | 8613b87 | 2016-10-07 17:15:58 +0530 | [diff] [blame] | 388 | |
| 389 | self.validatemac(mac) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 390 | |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 391 | rc = subprocess.call(["fw_setenv", "ethaddr", mac]) |
Hariharasubramanian R | 308cffc | 2016-03-03 09:35:16 -0600 | [diff] [blame] | 392 | |
| 393 | print("Restarting networkd service...") |
| 394 | rc = call(["ip", "link", "set", "dev", device, "down"]) |
| 395 | rc = call(["ip", "link", "set", "dev", device, "address", mac]) |
| 396 | rc = call(["ip", "link", "set", "dev", device, "up"]) |
| 397 | |
| 398 | rc = call(["systemctl", "restart", "systemd-networkd.service"]) |
Adriana Kobylak | 88c733b | 2016-02-03 16:46:58 -0600 | [diff] [blame] | 399 | return rc |
| 400 | |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 401 | #string of nameservers |
| 402 | @dbus.service.method(DBUS_NAME,"s", "s") |
| 403 | def SetNameServers (self, nameservers): |
| 404 | dns_entry = nameservers.split() |
| 405 | fail_msg = '' |
| 406 | dhcp_auto = False |
| 407 | file_opened = False |
| 408 | if len(dns_entry) > 0: |
| 409 | for dns in dns_entry: |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 410 | valid = self._isvalidip (dns) |
| 411 | if valid != "Valid": |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 412 | if dns == "DHCP_AUTO=": |
| 413 | #This DNS is supplied by DHCP. |
| 414 | dhcp_auto = True |
| 415 | else: |
Edward A. James | 75757c0 | 2016-07-15 09:11:18 -0500 | [diff] [blame] | 416 | print valid + " DNS Address [" + dns + "]" |
vishwa | 5c912c8 | 2016-03-24 07:59:28 -0500 | [diff] [blame] | 417 | fail_msg = fail_msg + '[' + dns + ']' |
| 418 | else: |
| 419 | #Only over write on a first valid input |
| 420 | if file_opened == False: |
| 421 | resolv_conf = open("/etc/resolv.conf",'w') |
| 422 | file_opened = True |
| 423 | if dhcp_auto == True: |
| 424 | resolv_conf.write("### Generated automatically via DHCP ###\n") |
| 425 | else: |
| 426 | resolv_conf.write("### Generated manually via dbus settings ###\n") |
| 427 | dns_ip = 'nameserver ' + dns + '\n' |
| 428 | resolv_conf.write(dns_ip) |
| 429 | if file_opened == True: |
| 430 | resolv_conf.close() |
| 431 | else: |
| 432 | raise ValueError, "Invalid DNS entry" |
| 433 | if len(fail_msg) > 0: |
| 434 | return 'Failures encountered processing' + fail_msg |
| 435 | else: |
| 436 | return "DNS entries updated Successfully" |
| 437 | |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 438 | def main(): |
| 439 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 440 | bus = dbus.SystemBus() |
| 441 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 442 | obj = NetMan (bus, OBJ_NAME) |
| 443 | obj.setNetworkProvider ("networkd") |
| 444 | mainloop = gobject.MainLoop() |
| 445 | print("Started") |
| 446 | mainloop.run() |
| 447 | |
| 448 | if __name__ == '__main__': |
Adriana Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame] | 449 | sys.exit(main()) |