| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import re |
| 19 |
import socket |
| 20 |
import struct |
| 21 |
|
| 22 |
from ansible.module_utils.facts.network.base import Network |
| 23 |
|
| 24 |
|
| 25 |
class GenericBsdIfconfigNetwork(Network): |
| 26 |
""" |
| 27 |
This is a generic BSD subclass of Network using the ifconfig command. |
| 28 |
It defines |
| 29 |
- interfaces (a list of interface names) |
| 30 |
- interface_<name> dictionary of ipv4, ipv6, and mac address information. |
| 31 |
- all_ipv4_addresses and all_ipv6_addresses: lists of all configured addresses. |
| 32 |
""" |
| 33 |
platform = 'Generic_BSD_Ifconfig' |
| 34 |
|
| 35 |
def populate(self, collected_facts=None): |
| 36 |
network_facts = {} |
| 37 |
ifconfig_path = self.module.get_bin_path('ifconfig') |
| 38 |
|
| 39 |
if ifconfig_path is None: |
| 40 |
return network_facts |
| 41 |
|
| 42 |
route_path = self.module.get_bin_path('route') |
| 43 |
|
| 44 |
if route_path is None: |
| 45 |
return network_facts |
| 46 |
|
| 47 |
default_ipv4, default_ipv6 = self.get_default_interfaces(route_path) |
| 48 |
interfaces, ips = self.get_interfaces_info(ifconfig_path) |
| 49 |
interfaces = self.detect_type_media(interfaces) |
| 50 |
|
| 51 |
self.merge_default_interface(default_ipv4, interfaces, 'ipv4') |
| 52 |
self.merge_default_interface(default_ipv6, interfaces, 'ipv6') |
| 53 |
network_facts['interfaces'] = sorted(list(interfaces.keys())) |
| 54 |
|
| 55 |
for iface in interfaces: |
| 56 |
network_facts[iface] = interfaces[iface] |
| 57 |
|
| 58 |
network_facts['default_ipv4'] = default_ipv4 |
| 59 |
network_facts['default_ipv6'] = default_ipv6 |
| 60 |
network_facts['all_ipv4_addresses'] = ips['all_ipv4_addresses'] |
| 61 |
network_facts['all_ipv6_addresses'] = ips['all_ipv6_addresses'] |
| 62 |
|
| 63 |
return network_facts |
| 64 |
|
| 65 |
def detect_type_media(self, interfaces): |
| 66 |
for iface in interfaces: |
| 67 |
if 'media' in interfaces[iface]: |
| 68 |
if 'ether' in interfaces[iface]['media'].lower(): |
| 69 |
interfaces[iface]['type'] = 'ether' |
| 70 |
return interfaces |
| 71 |
|
| 72 |
def get_default_interfaces(self, route_path): |
| 73 |
|
| 74 |
# Use the commands: |
| 75 |
# route -n get default |
| 76 |
# route -n get -inet6 default |
| 77 |
# to find out the default outgoing interface, address, and gateway |
| 78 |
|
| 79 |
command = dict(v4=[route_path, '-n', 'get', 'default'], |
| 80 |
v6=[route_path, '-n', 'get', '-inet6', 'default']) |
| 81 |
|
| 82 |
interface = dict(v4={}, v6={}) |
| 83 |
|
| 84 |
for v in 'v4', 'v6': |
| 85 |
|
| 86 |
if v == 'v6' and not socket.has_ipv6: |
| 87 |
continue |
| 88 |
rc, out, err = self.module.run_command(command[v]) |
| 89 |
if not out: |
| 90 |
# v6 routing may result in |
| 91 |
# RTNETLINK answers: Invalid argument |
| 92 |
continue |
| 93 |
for line in out.splitlines(): |
| 94 |
words = line.strip().split(': ') |
| 95 |
# Collect output from route command |
| 96 |
if len(words) > 1: |
| 97 |
if words[0] == 'interface': |
| 98 |
interface[v]['interface'] = words[1] |
| 99 |
if words[0] == 'gateway': |
| 100 |
interface[v]['gateway'] = words[1] |
| 101 |
# help pick the right interface address on OpenBSD |
| 102 |
if words[0] == 'if address': |
| 103 |
interface[v]['address'] = words[1] |
| 104 |
# help pick the right interface address on NetBSD |
| 105 |
if words[0] == 'local addr': |
| 106 |
interface[v]['address'] = words[1] |
| 107 |
|
| 108 |
return interface['v4'], interface['v6'] |
| 109 |
|
| 110 |
def get_interfaces_info(self, ifconfig_path, ifconfig_options='-a'): |
| 111 |
interfaces = {} |
| 112 |
current_if = {} |
| 113 |
ips = dict( |
| 114 |
all_ipv4_addresses=[], |
| 115 |
all_ipv6_addresses=[], |
| 116 |
) |
| 117 |
# FreeBSD, DragonflyBSD, NetBSD, OpenBSD and macOS all implicitly add '-a' |
| 118 |
# when running the command 'ifconfig'. |
| 119 |
# Solaris must explicitly run the command 'ifconfig -a'. |
| 120 |
rc, out, err = self.module.run_command([ifconfig_path, ifconfig_options]) |
| 121 |
|
| 122 |
for line in out.splitlines(): |
| 123 |
|
| 124 |
if line: |
| 125 |
words = line.split() |
| 126 |
|
| 127 |
if words[0] == 'pass': |
| 128 |
continue |
| 129 |
elif re.match(r'^\S', line) and len(words) > 3: |
| 130 |
current_if = self.parse_interface_line(words) |
| 131 |
interfaces[current_if['device']] = current_if |
| 132 |
elif words[0].startswith('options='): |
| 133 |
self.parse_options_line(words, current_if, ips) |
| 134 |
elif words[0] == 'nd6': |
| 135 |
self.parse_nd6_line(words, current_if, ips) |
| 136 |
elif words[0] == 'ether': |
| 137 |
self.parse_ether_line(words, current_if, ips) |
| 138 |
elif words[0] == 'media:': |
| 139 |
self.parse_media_line(words, current_if, ips) |
| 140 |
elif words[0] == 'status:': |
| 141 |
self.parse_status_line(words, current_if, ips) |
| 142 |
elif words[0] == 'lladdr': |
| 143 |
self.parse_lladdr_line(words, current_if, ips) |
| 144 |
elif words[0] == 'inet': |
| 145 |
self.parse_inet_line(words, current_if, ips) |
| 146 |
elif words[0] == 'inet6': |
| 147 |
self.parse_inet6_line(words, current_if, ips) |
| 148 |
elif words[0] == 'tunnel': |
| 149 |
self.parse_tunnel_line(words, current_if, ips) |
| 150 |
else: |
| 151 |
self.parse_unknown_line(words, current_if, ips) |
| 152 |
|
| 153 |
return interfaces, ips |
| 154 |
|
| 155 |
def parse_interface_line(self, words): |
| 156 |
device = words[0][0:-1] |
| 157 |
current_if = {'device': device, 'ipv4': [], 'ipv6': [], 'type': 'unknown'} |
| 158 |
current_if['flags'] = self.get_options(words[1]) |
| 159 |
if 'LOOPBACK' in current_if['flags']: |
| 160 |
current_if['type'] = 'loopback' |
| 161 |
current_if['macaddress'] = 'unknown' # will be overwritten later |
| 162 |
|
| 163 |
if len(words) >= 5: # Newer FreeBSD versions |
| 164 |
current_if['metric'] = words[3] |
| 165 |
current_if['mtu'] = words[5] |
| 166 |
else: |
| 167 |
current_if['mtu'] = words[3] |
| 168 |
|
| 169 |
return current_if |
| 170 |
|
| 171 |
def parse_options_line(self, words, current_if, ips): |
| 172 |
# Mac has options like this... |
| 173 |
current_if['options'] = self.get_options(words[0]) |
| 174 |
|
| 175 |
def parse_nd6_line(self, words, current_if, ips): |
| 176 |
# FreeBSD has options like this... |
| 177 |
current_if['options'] = self.get_options(words[1]) |
| 178 |
|
| 179 |
def parse_ether_line(self, words, current_if, ips): |
| 180 |
current_if['macaddress'] = words[1] |
| 181 |
current_if['type'] = 'ether' |
| 182 |
|
| 183 |
def parse_media_line(self, words, current_if, ips): |
| 184 |
# not sure if this is useful - we also drop information |
| 185 |
current_if['media'] = words[1] |
| 186 |
if len(words) > 2: |
| 187 |
current_if['media_select'] = words[2] |
| 188 |
if len(words) > 3: |
| 189 |
current_if['media_type'] = words[3][1:] |
| 190 |
if len(words) > 4: |
| 191 |
current_if['media_options'] = self.get_options(words[4]) |
| 192 |
|
| 193 |
def parse_status_line(self, words, current_if, ips): |
| 194 |
current_if['status'] = words[1] |
| 195 |
|
| 196 |
def parse_lladdr_line(self, words, current_if, ips): |
| 197 |
current_if['lladdr'] = words[1] |
| 198 |
|
| 199 |
def parse_inet_line(self, words, current_if, ips): |
| 200 |
# netbsd show aliases like this |
| 201 |
# lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33184 |
| 202 |
# inet 127.0.0.1 netmask 0xff000000 |
| 203 |
# inet alias 127.1.1.1 netmask 0xff000000 |
| 204 |
if words[1] == 'alias': |
| 205 |
del words[1] |
| 206 |
|
| 207 |
address = {'address': words[1]} |
| 208 |
# cidr style ip address (eg, 127.0.0.1/24) in inet line |
| 209 |
# used in netbsd ifconfig -e output after 7.1 |
| 210 |
if '/' in address['address']: |
| 211 |
ip_address, cidr_mask = address['address'].split('/') |
| 212 |
|
| 213 |
address['address'] = ip_address |
| 214 |
|
| 215 |
netmask_length = int(cidr_mask) |
| 216 |
netmask_bin = (1 << 32) - (1 << 32 >> int(netmask_length)) |
| 217 |
address['netmask'] = socket.inet_ntoa(struct.pack('!L', netmask_bin)) |
| 218 |
|
| 219 |
if len(words) > 5: |
| 220 |
address['broadcast'] = words[3] |
| 221 |
|
| 222 |
else: |
| 223 |
# Don't just assume columns, use "netmask" as the index for the prior column |
| 224 |
try: |
| 225 |
netmask_idx = words.index('netmask') + 1 |
| 226 |
except ValueError: |
| 227 |
netmask_idx = 3 |
| 228 |
|
| 229 |
# deal with hex netmask |
| 230 |
if re.match('([0-9a-f]){8}$', words[netmask_idx]): |
| 231 |
netmask = '0x' + words[netmask_idx] |
| 232 |
else: |
| 233 |
netmask = words[netmask_idx] |
| 234 |
|
| 235 |
if netmask.startswith('0x'): |
| 236 |
address['netmask'] = socket.inet_ntoa(struct.pack('!L', int(netmask, base=16))) |
| 237 |
else: |
| 238 |
# otherwise assume this is a dotted quad |
| 239 |
address['netmask'] = netmask |
| 240 |
# calculate the network |
| 241 |
address_bin = struct.unpack('!L', socket.inet_aton(address['address']))[0] |
| 242 |
netmask_bin = struct.unpack('!L', socket.inet_aton(address['netmask']))[0] |
| 243 |
address['network'] = socket.inet_ntoa(struct.pack('!L', address_bin & netmask_bin)) |
| 244 |
if 'broadcast' not in address: |
| 245 |
# broadcast may be given or we need to calculate |
| 246 |
try: |
| 247 |
broadcast_idx = words.index('broadcast') + 1 |
| 248 |
except ValueError: |
| 249 |
address['broadcast'] = socket.inet_ntoa(struct.pack('!L', address_bin | (~netmask_bin & 0xffffffff))) |
| 250 |
else: |
| 251 |
address['broadcast'] = words[broadcast_idx] |
| 252 |
|
| 253 |
# add to our list of addresses |
| 254 |
if not words[1].startswith('127.'): |
| 255 |
ips['all_ipv4_addresses'].append(address['address']) |
| 256 |
current_if['ipv4'].append(address) |
| 257 |
|
| 258 |
def parse_inet6_line(self, words, current_if, ips): |
| 259 |
address = {'address': words[1]} |
| 260 |
|
| 261 |
# using cidr style addresses, ala NetBSD ifconfig post 7.1 |
| 262 |
if '/' in address['address']: |
| 263 |
ip_address, cidr_mask = address['address'].split('/') |
| 264 |
|
| 265 |
address['address'] = ip_address |
| 266 |
address['prefix'] = cidr_mask |
| 267 |
|
| 268 |
if len(words) > 5: |
| 269 |
address['scope'] = words[5] |
| 270 |
else: |
| 271 |
if (len(words) >= 4) and (words[2] == 'prefixlen'): |
| 272 |
address['prefix'] = words[3] |
| 273 |
if (len(words) >= 6) and (words[4] == 'scopeid'): |
| 274 |
address['scope'] = words[5] |
| 275 |
|
| 276 |
localhost6 = ['::1', '::1/128', 'fe80::1%lo0'] |
| 277 |
if address['address'] not in localhost6: |
| 278 |
ips['all_ipv6_addresses'].append(address['address']) |
| 279 |
current_if['ipv6'].append(address) |
| 280 |
|
| 281 |
def parse_tunnel_line(self, words, current_if, ips): |
| 282 |
current_if['type'] = 'tunnel' |
| 283 |
|
| 284 |
def parse_unknown_line(self, words, current_if, ips): |
| 285 |
# we are going to ignore unknown lines here - this may be |
| 286 |
# a bad idea - but you can override it in your subclass |
| 287 |
pass |
| 288 |
|
| 289 |
# TODO: these are module scope static function candidates |
| 290 |
# (most of the class is really...) |
| 291 |
def get_options(self, option_string): |
| 292 |
start = option_string.find('<') + 1 |
| 293 |
end = option_string.rfind('>') |
| 294 |
if (start > 0) and (end > 0) and (end > start + 1): |
| 295 |
option_csv = option_string[start:end] |
| 296 |
return option_csv.split(',') |
| 297 |
else: |
| 298 |
return [] |
| 299 |
|
| 300 |
def merge_default_interface(self, defaults, interfaces, ip_type): |
| 301 |
if 'interface' not in defaults: |
| 302 |
return |
| 303 |
if not defaults['interface'] in interfaces: |
| 304 |
return |
| 305 |
ifinfo = interfaces[defaults['interface']] |
| 306 |
# copy all the interface values across except addresses |
| 307 |
for item in ifinfo: |
| 308 |
if item != 'ipv4' and item != 'ipv6': |
| 309 |
defaults[item] = ifinfo[item] |
| 310 |
|
| 311 |
ipinfo = [] |
| 312 |
if 'address' in defaults: |
| 313 |
ipinfo = [x for x in ifinfo[ip_type] if x['address'] == defaults['address']] |
| 314 |
|
| 315 |
if len(ipinfo) == 0: |
| 316 |
ipinfo = ifinfo[ip_type] |
| 317 |
|
| 318 |
if len(ipinfo) > 0: |
| 319 |
for item in ipinfo[0]: |
| 320 |
defaults[item] = ipinfo[0][item] |