Search in sources :

Example 1 with FirewallAutoNatConfig

use of org.eclipse.kura.net.firewall.FirewallAutoNatConfig in project kura by eclipse.

the class NetworkConfiguration method populateNetInterfaceConfiguration.

private void populateNetInterfaceConfiguration(AbstractNetInterface<? extends NetInterfaceAddressConfig> netInterfaceConfig, Map<String, Object> props) throws UnknownHostException, KuraException {
    String interfaceName = netInterfaceConfig.getName();
    StringBuffer keyBuffer = new StringBuffer();
    keyBuffer.append("net.interface.").append(interfaceName).append(".type");
    NetInterfaceType interfaceType = NetInterfaceType.valueOf((String) props.get(keyBuffer.toString()));
    s_logger.trace("Populating interface: {} of type {}", interfaceName, interfaceType);
    // build the prefixes for all the properties associated with this interface
    StringBuilder sbPrefix = new StringBuilder();
    sbPrefix.append("net.interface.").append(interfaceName).append(".");
    String netIfReadOnlyPrefix = sbPrefix.toString();
    String netIfPrefix = sbPrefix.append("config.").toString();
    String netIfConfigPrefix = sbPrefix.toString();
    // [RO] State
    String stateConfig = netIfReadOnlyPrefix + "state";
    if (props.containsKey(stateConfig)) {
        try {
            NetInterfaceState state = (NetInterfaceState) props.get(stateConfig);
            s_logger.trace("got state: {}", state);
            netInterfaceConfig.setState(state);
        } catch (Exception e) {
            s_logger.error("Could not process State configuration. Retaining current value.", e);
        }
    }
    // Auto connect
    boolean autoConnect = false;
    String autoConnectKey = netIfPrefix + "autoconnect";
    if (props.containsKey(autoConnectKey)) {
        autoConnect = (Boolean) props.get(autoConnectKey);
        s_logger.trace("got autoConnect: {}", autoConnect);
        netInterfaceConfig.setAutoConnect(autoConnect);
    }
    // MTU
    String mtuConfig = netIfPrefix + "mtu";
    if (props.containsKey(mtuConfig)) {
        int mtu = (Integer) props.get(mtuConfig);
        s_logger.trace("got MTU: {}", mtu);
        netInterfaceConfig.setMTU(mtu);
    }
    // Driver
    String driverKey = netIfReadOnlyPrefix + "driver";
    if (props.containsKey(driverKey)) {
        String driver = (String) props.get(driverKey);
        s_logger.trace("got Driver: {}", driver);
        netInterfaceConfig.setDriver(driver);
    }
    // Driver Version
    String driverVersionKey = netIfReadOnlyPrefix + "driver.version";
    if (props.containsKey(driverVersionKey)) {
        String driverVersion = (String) props.get(driverVersionKey);
        s_logger.trace("got Driver Version: {}", driverVersion);
        netInterfaceConfig.setDriverVersion(driverVersion);
    }
    // Firmware Version
    String firmwardVersionKey = netIfReadOnlyPrefix + "firmware.version";
    if (props.containsKey(firmwardVersionKey)) {
        String firmwareVersion = (String) props.get(firmwardVersionKey);
        s_logger.trace("got Firmware Version: {}", firmwareVersion);
        netInterfaceConfig.setFirmwareVersion(firmwareVersion);
    }
    // Mac Address
    String macAddressKey = netIfReadOnlyPrefix + "mac";
    if (props.containsKey(macAddressKey)) {
        String macAddress = (String) props.get(macAddressKey);
        s_logger.trace("got Mac Address: {}", macAddress);
        netInterfaceConfig.setHardwareAddress(NetUtil.hardwareAddressToBytes(macAddress));
    }
    // Is Loopback
    String loopbackKey = netIfReadOnlyPrefix + "loopback";
    if (props.containsKey(loopbackKey)) {
        Boolean isLoopback = (Boolean) props.get(loopbackKey);
        s_logger.trace("got Is Loopback: {}", isLoopback);
        netInterfaceConfig.setLoopback(isLoopback);
    }
    // Is Point to Point
    String ptpKey = netIfReadOnlyPrefix + "ptp";
    if (props.containsKey(ptpKey)) {
        Boolean isPtp = (Boolean) props.get(ptpKey);
        s_logger.trace("got Is PtP: {}", isPtp);
        netInterfaceConfig.setPointToPoint(isPtp);
    }
    // Is Up
    String upKey = netIfReadOnlyPrefix + "up";
    if (props.containsKey(upKey)) {
        Boolean isUp = (Boolean) props.get(upKey);
        s_logger.trace("got Is Up: {}", isUp);
        netInterfaceConfig.setUp(isUp);
        if (isUp) {
            netInterfaceConfig.setState(NetInterfaceState.ACTIVATED);
        } else {
            netInterfaceConfig.setState(NetInterfaceState.DISCONNECTED);
        }
    } else {
        s_logger.trace("Setting state to");
        netInterfaceConfig.setState(NetInterfaceState.DISCONNECTED);
    }
    // Is Virtual
    String virtualKey = netIfReadOnlyPrefix + "virtual";
    if (props.containsKey(virtualKey)) {
        Boolean isVirtual = (Boolean) props.get(virtualKey);
        s_logger.trace("got Is Virtual: {}", isVirtual);
        netInterfaceConfig.setVirtual(isVirtual);
    }
    // USB
    String vendorId = (String) props.get(netIfReadOnlyPrefix + "usb.vendor.id");
    String vendorName = (String) props.get(netIfReadOnlyPrefix + "usb.vendor.name");
    String productId = (String) props.get(netIfReadOnlyPrefix + "usb.product.id");
    String productName = (String) props.get(netIfReadOnlyPrefix + "usb.product.name");
    String usbBusNumber = (String) props.get(netIfReadOnlyPrefix + "usb.busNumber");
    String usbDevicePath = (String) props.get(netIfReadOnlyPrefix + "usb.devicePath");
    if (vendorId != null && productId != null) {
        UsbDevice usbDevice = new UsbNetDevice(vendorId, productId, vendorName, productName, usbBusNumber, usbDevicePath, interfaceName);
        s_logger.trace("adding usbDevice: {}, port: {}", usbDevice, usbDevice.getUsbPort());
        netInterfaceConfig.setUsbDevice(usbDevice);
    }
    if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
        // Is Up
        String linkUpKey = netIfReadOnlyPrefix + "eth.link.up";
        if (props.containsKey(linkUpKey)) {
            Boolean linkUp = (Boolean) props.get(linkUpKey);
            s_logger.trace("got Is Link Up: {}", linkUp);
            ((EthernetInterfaceConfigImpl) netInterfaceConfig).setLinkUp(linkUp);
        }
    } else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
        // Wifi Capabilities
        String capabilitiesKey = netIfReadOnlyPrefix + "wifi.capabilities";
        if (props.containsKey(capabilitiesKey)) {
            String capabilitiesString = (String) props.get(capabilitiesKey);
            if (capabilitiesString != null) {
                String[] capabilities = capabilitiesString.split(" ");
                if (capabilities != null && capabilities.length > 0) {
                    EnumSet<Capability> capabilitiesEnum = EnumSet.noneOf(Capability.class);
                    for (String capability : capabilities) {
                        if (capability != null && !capability.isEmpty()) {
                            capabilitiesEnum.add(Capability.valueOf(capability));
                        }
                    }
                    ((WifiInterfaceConfigImpl) netInterfaceConfig).setCapabilities(capabilitiesEnum);
                }
            }
        }
    } else if (netInterfaceConfig instanceof ModemInterfaceConfigImpl) {
        ModemInterfaceConfigImpl modemInterfaceConfig = (ModemInterfaceConfigImpl) netInterfaceConfig;
        String key;
        // manufacturer
        key = netIfReadOnlyPrefix + "manufacturer";
        if (props.containsKey(key)) {
            modemInterfaceConfig.setManufacturer((String) props.get(key));
        }
        // manufacturer
        key = netIfReadOnlyPrefix + "model";
        if (props.containsKey(key)) {
            modemInterfaceConfig.setModel((String) props.get(key));
        }
        // revision id
        key = netIfReadOnlyPrefix + "revisionId";
        if (props.containsKey(key)) {
            String revisionIdString = (String) props.get(key);
            modemInterfaceConfig.setRevisionId(revisionIdString.split(","));
        }
        // serial number
        key = netIfReadOnlyPrefix + "serialNum";
        if (props.containsKey(key)) {
            modemInterfaceConfig.setSerialNumber((String) props.get(key));
        }
        // technology types
        key = netIfReadOnlyPrefix + "technologyTypes";
        if (props.containsKey(key)) {
            ArrayList<ModemTechnologyType> technologyTypes = new ArrayList<ModemTechnologyType>();
            String techTypesString = (String) props.get(netIfReadOnlyPrefix + "technologyTypes");
            if (techTypesString != null && !techTypesString.isEmpty()) {
                for (String techTypeString : techTypesString.split(",")) {
                    if (techTypeString != null && !techTypeString.isEmpty()) {
                        try {
                            ModemTechnologyType modemTechType = ModemTechnologyType.valueOf(techTypeString);
                            technologyTypes.add(modemTechType);
                        } catch (IllegalArgumentException e) {
                            s_logger.error("Could not parse type " + techTypeString);
                        }
                    }
                }
                modemInterfaceConfig.setTechnologyTypes(technologyTypes);
            }
        }
        // modem identifier
        key = netIfConfigPrefix + "identifier";
        if (props.containsKey(key)) {
            modemInterfaceConfig.setModemIdentifier((String) props.get(key));
        }
        // power mode
        key = netIfConfigPrefix + "powerMode";
        if (props.containsKey(key)) {
            ModemPowerMode powerMode = ModemPowerMode.UNKNOWN;
            String modemPowerModeString = (String) props.get(netIfConfigPrefix + "powerMode");
            if (modemPowerModeString != null) {
                powerMode = ModemPowerMode.valueOf(modemPowerModeString);
                modemInterfaceConfig.setPowerMode(powerMode);
            }
        }
        // ppp number
        key = netIfConfigPrefix + "pppNum";
        if (props.containsKey(key)) {
            if (props.get(key) != null) {
                modemInterfaceConfig.setPppNum((Integer) props.get(key));
            }
        }
        // powered on
        key = netIfConfigPrefix + "poweredOn";
        if (props.containsKey(key)) {
            if (props.get(key) != null) {
                modemInterfaceConfig.setPoweredOn((Boolean) props.get(key));
            }
        }
    }
    // Status
    String configStatus4 = null;
    String configStatus4Key = "net.interface." + interfaceName + ".config.ip4.status";
    if (props.containsKey(configStatus4Key)) {
        configStatus4 = (String) props.get(configStatus4Key);
    }
    if (configStatus4 == null) {
        configStatus4 = NetInterfaceStatus.netIPv4StatusDisabled.name();
    }
    s_logger.trace("Status Ipv4? {}", configStatus4);
    String configStatus6 = null;
    String configStatus6Key = "net.interface." + interfaceName + ".config.ip6.status";
    if (props.containsKey(configStatus6Key)) {
        configStatus6 = (String) props.get(configStatus6Key);
    }
    if (configStatus6 == null) {
        configStatus6 = NetInterfaceStatus.netIPv6StatusDisabled.name();
    }
    // POPULATE NetInterfaceAddresses
    for (NetInterfaceAddressConfig netInterfaceAddress : netInterfaceConfig.getNetInterfaceAddresses()) {
        List<NetConfig> netConfigs = new ArrayList<NetConfig>();
        if (netInterfaceAddress instanceof NetInterfaceAddressConfigImpl) {
            ((NetInterfaceAddressConfigImpl) netInterfaceAddress).setNetConfigs(netConfigs);
        } else if (netInterfaceAddress instanceof WifiInterfaceAddressConfigImpl) {
            ((WifiInterfaceAddressConfigImpl) netInterfaceAddress).setNetConfigs(netConfigs);
        } else if (netInterfaceAddress instanceof ModemInterfaceAddressConfigImpl) {
            ((ModemInterfaceAddressConfigImpl) netInterfaceAddress).setNetConfigs(netConfigs);
        }
        // Common NetInterfaceAddress
        if (netInterfaceAddress instanceof NetInterfaceAddressImpl) {
            s_logger.trace("netInterfaceAddress is instanceof NetInterfaceAddressImpl");
            NetInterfaceAddressImpl netInterfaceAddressImpl = (NetInterfaceAddressImpl) netInterfaceAddress;
            // TODO: determine dynamically
            String addressType = ".ip4";
            // populate current address status
            String key = "net.interface." + interfaceName + addressType + ".address";
            if (props.containsKey(key)) {
                IPAddress address = IPAddress.parseHostAddress((String) props.get(key));
                s_logger.trace("got {}: {}", key, address);
                netInterfaceAddressImpl.setAddress(address);
            }
            key = "net.interface." + interfaceName + addressType + ".broadcast";
            if (props.containsKey(key)) {
                IPAddress broadcast = IPAddress.parseHostAddress((String) props.get(key));
                s_logger.trace("got {}: {}", key, broadcast);
                netInterfaceAddressImpl.setBroadcast(broadcast);
            }
            key = "net.interface." + interfaceName + addressType + ".dnsServers";
            if (props.containsKey(key)) {
                List<IPAddress> dnsServers = new ArrayList<IPAddress>();
                String dnsServersString = (String) props.get(key);
                s_logger.trace("got {}: {}", key, dnsServersString);
                for (String dnsServer : dnsServersString.split(",")) {
                    dnsServers.add(IPAddress.parseHostAddress(dnsServer));
                }
                netInterfaceAddressImpl.setDnsServers(dnsServers);
            }
            key = "net.interface." + interfaceName + addressType + ".gateway";
            if (props.containsKey(key)) {
                if (props.get(key) != null && !((String) props.get(key)).trim().equals("")) {
                    IPAddress gateway = IPAddress.parseHostAddress((String) props.get(key));
                    s_logger.trace("got {}: {}", key, gateway);
                    netInterfaceAddressImpl.setGateway(gateway);
                } else {
                    s_logger.trace("got {}: null", key);
                    netInterfaceAddressImpl.setGateway(null);
                }
            }
            key = "net.interface." + interfaceName + addressType + ".netmask";
            if (props.containsKey(key)) {
                IPAddress netmask = IPAddress.parseHostAddress((String) props.get(key));
                s_logger.trace("got {}: {}", key, netmask);
                netInterfaceAddressImpl.setBroadcast(netmask);
            }
            key = "net.interface." + interfaceName + addressType + ".prefix";
            if (props.containsKey(key)) {
                Short prefix = (Short) props.get(key);
                s_logger.trace("got {}: {}", key, prefix);
                netInterfaceAddressImpl.setNetworkPrefixLength(prefix);
            }
        }
        // WifiInterfaceAddress
        if (netInterfaceAddress instanceof WifiInterfaceAddressImpl) {
            s_logger.trace("netInterfaceAddress is instanceof WifiInterfaceAddressImpl");
            WifiInterfaceAddressImpl wifiInterfaceAddressImpl = (WifiInterfaceAddressImpl) netInterfaceAddress;
            // wifi mode
            String configWifiMode = netIfPrefix + "wifi.mode";
            if (props.containsKey(configWifiMode)) {
                // FIXME: INFRA for now while debugging - probably want this as
                WifiMode mode = WifiMode.INFRA;
                // UNKNOWN
                if (props.get(configWifiMode) != null) {
                    mode = WifiMode.valueOf((String) props.get(configWifiMode));
                }
                s_logger.trace("Adding wifiMode: {}", mode);
                wifiInterfaceAddressImpl.setMode(mode);
            }
        }
        // ModemInterfaceAddress
        if (netInterfaceAddress instanceof ModemInterfaceAddressConfigImpl) {
            s_logger.trace("netInterfaceAddress is instanceof ModemInterfaceAddressConfigImpl");
            ModemInterfaceAddressConfigImpl modemInterfaceAddressImpl = (ModemInterfaceAddressConfigImpl) netInterfaceAddress;
            // connection type
            String configConnType = netIfPrefix + "connection.type";
            if (props.containsKey(configConnType)) {
                ModemConnectionType connType = ModemConnectionType.PPP;
                String connTypeStr = (String) props.get(configConnType);
                if (connTypeStr != null && !connTypeStr.isEmpty()) {
                    connType = ModemConnectionType.valueOf(connTypeStr);
                }
                s_logger.trace("Adding modem connection type: {}", connType);
                modemInterfaceAddressImpl.setConnectionType(connType);
            }
            // connection type
            String configConnStatus = netIfPrefix + "connection.status";
            if (props.containsKey(configConnStatus)) {
                ModemConnectionStatus connStatus = ModemConnectionStatus.UNKNOWN;
                String connStatusStr = (String) props.get(configConnStatus);
                if (connStatusStr != null && !connStatusStr.isEmpty()) {
                    connStatus = ModemConnectionStatus.valueOf(connStatusStr);
                }
                s_logger.trace("Adding modem connection status: {}", connStatus);
                modemInterfaceAddressImpl.setConnectionStatus(connStatus);
            }
        }
        // POPULATE NetConfigs
        // dhcp4
        String configDhcp4 = "net.interface." + interfaceName + ".config.dhcpClient4.enabled";
        NetConfigIP4 netConfigIP4 = null;
        boolean dhcpEnabled = false;
        if (props.containsKey(configDhcp4)) {
            dhcpEnabled = (Boolean) props.get(configDhcp4);
            s_logger.trace("DHCP 4 enabled? {}", dhcpEnabled);
        }
        netConfigIP4 = new NetConfigIP4(NetInterfaceStatus.valueOf(configStatus4), autoConnect, dhcpEnabled);
        netConfigs.add(netConfigIP4);
        if (!dhcpEnabled) {
            // NetConfigIP4
            String configIp4 = "net.interface." + interfaceName + ".config.ip4.address";
            if (props.containsKey(configIp4)) {
                s_logger.trace("got {}: {}", configIp4, props.get(configIp4));
                // address
                String addressIp4 = (String) props.get(configIp4);
                s_logger.trace("IPv4 address: {}", addressIp4);
                if (addressIp4 != null && !addressIp4.isEmpty()) {
                    IP4Address ip4Address = (IP4Address) IPAddress.parseHostAddress(addressIp4);
                    netConfigIP4.setAddress(ip4Address);
                }
                // prefix
                String configIp4Prefix = "net.interface." + interfaceName + ".config.ip4.prefix";
                short networkPrefixLength = -1;
                if (props.containsKey(configIp4Prefix)) {
                    if (props.get(configIp4Prefix) instanceof Short) {
                        networkPrefixLength = (Short) props.get(configIp4Prefix);
                    } else if (props.get(configIp4Prefix) instanceof String) {
                        networkPrefixLength = Short.parseShort((String) props.get(configIp4Prefix));
                    }
                    try {
                        netConfigIP4.setNetworkPrefixLength(networkPrefixLength);
                    } catch (KuraException e) {
                        s_logger.error("Exception while setting Network Prefix length!", e);
                    }
                /*
                         * s_logger.trace("IPv4 prefix: " + networkPrefixLength);
                         * netInterfaceAddress.setNetworkPrefixLength(networkPrefixLength);
                         * //FIXME - hack for now
                         * netInterfaceAddress.setBroadcast((IP4Address) IPAddress.parseHostAddress("192.168.1.255"));
                         * ip4Config.setNetworkPrefixLength(networkPrefixLength);
                         */
                }
                // gateway
                String configIp4Gateway = "net.interface." + interfaceName + ".config.ip4.gateway";
                if (props.containsKey(configIp4Gateway)) {
                    String gatewayIp4 = (String) props.get(configIp4Gateway);
                    s_logger.trace("IPv4 gateway: {}", gatewayIp4);
                    if (gatewayIp4 != null && !gatewayIp4.isEmpty()) {
                        IP4Address ip4Gateway = (IP4Address) IPAddress.parseHostAddress(gatewayIp4);
                        netConfigIP4.setGateway(ip4Gateway);
                    }
                }
            }
        }
        // dns servers
        String configDNSs = "net.interface." + interfaceName + ".config.ip4.dnsServers";
        if (props.containsKey(configDNSs)) {
            List<IP4Address> dnsIPs = new ArrayList<IP4Address>();
            String dnsAll = (String) props.get(configDNSs);
            String[] dnss = dnsAll.split(",");
            for (String dns : dnss) {
                if (dns != null && dns.length() > 0) {
                    s_logger.trace("IPv4 DNS: {}", dns);
                    IP4Address dnsIp4 = (IP4Address) IPAddress.parseHostAddress(dns);
                    dnsIPs.add(dnsIp4);
                }
            }
            netConfigIP4.setDnsServers(dnsIPs);
        }
        // win servers
        String configWINSs = "net.interface." + interfaceName + ".config.ip4.winsServers";
        if (props.containsKey(configWINSs)) {
            List<IP4Address> winsIPs = new ArrayList<IP4Address>();
            String winsAll = (String) props.get(configWINSs);
            String[] winss = winsAll.split(",");
            for (String wins : winss) {
                s_logger.trace("WINS: {}", wins);
                IP4Address winsIp4 = (IP4Address) IPAddress.parseHostAddress(wins);
                winsIPs.add(winsIp4);
            }
            netConfigIP4.setWinsServers(winsIPs);
        }
        // domains
        String configDomains = "net.interface." + interfaceName + ".config.ip4.domains";
        if (props.containsKey(configDomains)) {
            List<String> domainNames = new ArrayList<String>();
            String domainsAll = (String) props.get(configDomains);
            String[] domains = domainsAll.split(",");
            for (String domain : domains) {
                s_logger.trace("IPv4 Domain: {}", domain);
                domainNames.add(domain);
            }
            netConfigIP4.setDomains(domainNames);
        }
        // FirewallNatConfig - see if NAT is enabled
        String configNatEnabled = "net.interface." + interfaceName + ".config.nat.enabled";
        if (props.containsKey(configNatEnabled)) {
            boolean natEnabled = (Boolean) props.get(configNatEnabled);
            s_logger.trace("NAT enabled? {}", natEnabled);
            if (natEnabled) {
                FirewallAutoNatConfig natConfig = new FirewallAutoNatConfig(interfaceName, "unknown", true);
                netConfigs.add(natConfig);
            }
        }
        // DhcpServerConfigIP4 - see if there is a DHCP 4 Server
        String configDhcpServerEnabled = "net.interface." + interfaceName + ".config.dhcpServer4.enabled";
        if (props.containsKey(configDhcpServerEnabled)) {
            boolean dhcpServerEnabled = (Boolean) props.get(configDhcpServerEnabled);
            s_logger.trace("DHCP Server 4 enabled? {}", dhcpServerEnabled);
            IP4Address subnet = null;
            IP4Address routerAddress = dhcpEnabled ? (IP4Address) netInterfaceAddress.getAddress() : netConfigIP4.getAddress();
            IP4Address subnetMask = null;
            int defaultLeaseTime = -1;
            int maximumLeaseTime = -1;
            short prefix = -1;
            IP4Address rangeStart = null;
            IP4Address rangeEnd = null;
            boolean passDns = false;
            List<IP4Address> dnServers = new ArrayList<IP4Address>();
            // prefix
            String configDhcpServerPrefix = "net.interface." + interfaceName + ".config.dhcpServer4.prefix";
            if (props.containsKey(configDhcpServerPrefix)) {
                if (props.get(configDhcpServerPrefix) instanceof Short) {
                    prefix = (Short) props.get(configDhcpServerPrefix);
                } else if (props.get(configDhcpServerPrefix) instanceof String) {
                    prefix = Short.parseShort((String) props.get(configDhcpServerPrefix));
                }
                s_logger.trace("DHCP Server prefix: {}", prefix);
            }
            // rangeStart
            String configDhcpServerRangeStart = "net.interface." + interfaceName + ".config.dhcpServer4.rangeStart";
            if (props.containsKey(configDhcpServerRangeStart)) {
                String dhcpServerRangeStart = (String) props.get(configDhcpServerRangeStart);
                s_logger.trace("DHCP Server Range Start: {}", dhcpServerRangeStart);
                if (dhcpServerRangeStart != null && !dhcpServerRangeStart.isEmpty()) {
                    rangeStart = (IP4Address) IPAddress.parseHostAddress(dhcpServerRangeStart);
                }
            }
            // rangeEnd
            String configDhcpServerRangeEnd = "net.interface." + interfaceName + ".config.dhcpServer4.rangeEnd";
            if (props.containsKey(configDhcpServerRangeEnd)) {
                String dhcpServerRangeEnd = (String) props.get(configDhcpServerRangeEnd);
                s_logger.trace("DHCP Server Range End: {}", dhcpServerRangeEnd);
                if (dhcpServerRangeEnd != null && !dhcpServerRangeEnd.isEmpty()) {
                    rangeEnd = (IP4Address) IPAddress.parseHostAddress(dhcpServerRangeEnd);
                }
            }
            // default lease time
            String configDhcpServerDefaultLeaseTime = "net.interface." + interfaceName + ".config.dhcpServer4.defaultLeaseTime";
            if (props.containsKey(configDhcpServerDefaultLeaseTime)) {
                if (props.get(configDhcpServerDefaultLeaseTime) instanceof Integer) {
                    defaultLeaseTime = (Integer) props.get(configDhcpServerDefaultLeaseTime);
                } else if (props.get(configDhcpServerDefaultLeaseTime) instanceof String) {
                    defaultLeaseTime = Integer.parseInt((String) props.get(configDhcpServerDefaultLeaseTime));
                }
                s_logger.trace("DHCP Server Default Lease Time: {}", defaultLeaseTime);
            }
            // max lease time
            String configDhcpServerMaxLeaseTime = "net.interface." + interfaceName + ".config.dhcpServer4.maxLeaseTime";
            if (props.containsKey(configDhcpServerMaxLeaseTime)) {
                if (props.get(configDhcpServerMaxLeaseTime) instanceof Integer) {
                    maximumLeaseTime = (Integer) props.get(configDhcpServerMaxLeaseTime);
                } else if (props.get(configDhcpServerMaxLeaseTime) instanceof String) {
                    maximumLeaseTime = Integer.parseInt((String) props.get(configDhcpServerMaxLeaseTime));
                }
                s_logger.trace("DHCP Server Maximum Lease Time: {}", maximumLeaseTime);
            }
            // passDns
            String configDhcpServerPassDns = "net.interface." + interfaceName + ".config.dhcpServer4.passDns";
            if (props.containsKey(configDhcpServerPassDns)) {
                if (props.get(configDhcpServerPassDns) instanceof Boolean) {
                    passDns = (Boolean) props.get(configDhcpServerPassDns);
                } else if (props.get(configDhcpServerPassDns) instanceof String) {
                    passDns = Boolean.parseBoolean((String) props.get(configDhcpServerPassDns));
                }
                s_logger.trace("DHCP Server Pass DNS?: {}", passDns);
            }
            if (routerAddress != null && rangeStart != null && rangeEnd != null) {
                // get the netmask and subnet
                int prefixInt = prefix;
                int mask = ~((1 << 32 - prefixInt) - 1);
                String subnetMaskString = NetworkUtil.dottedQuad(mask);
                String subnetString = NetworkUtil.calculateNetwork(routerAddress.getHostAddress(), subnetMaskString);
                subnet = (IP4Address) IPAddress.parseHostAddress(subnetString);
                subnetMask = (IP4Address) IPAddress.parseHostAddress(subnetMaskString);
                dnServers.add(routerAddress);
                DhcpServerConfigIP4 dhcpServerConfig = new DhcpServerConfigIP4(interfaceName, dhcpServerEnabled, subnet, routerAddress, subnetMask, defaultLeaseTime, maximumLeaseTime, prefix, rangeStart, rangeEnd, passDns, dnServers);
                netConfigs.add(dhcpServerConfig);
            } else {
                s_logger.trace("Not including DhcpServerConfig - router: " + routerAddress + ", range start: " + rangeStart + ", range end: " + rangeEnd);
            }
        }
        // dhcp6
        String configDhcp6 = "net.interface." + interfaceName + ".config.dhcpClient6.enabled";
        NetConfigIP6 netConfigIP6 = null;
        boolean dhcp6Enabled = false;
        if (props.containsKey(configDhcp6)) {
            dhcp6Enabled = (Boolean) props.get(configDhcp6);
            s_logger.trace("DHCP 6 enabled? {}", dhcp6Enabled);
        }
        if (!dhcp6Enabled) {
            // ip6
            String configIp6 = "net.interface." + interfaceName + ".config.ip6.address";
            if (props.containsKey(configIp6)) {
                // address
                String addressIp6 = (String) props.get(configIp6);
                s_logger.trace("IPv6 address: {}", addressIp6);
                if (addressIp6 != null && !addressIp6.isEmpty()) {
                    IP6Address ip6Address = (IP6Address) IPAddress.parseHostAddress(addressIp6);
                    netConfigIP6.setAddress(ip6Address);
                }
                // dns servers
                String configDNSs6 = "net.interface." + interfaceName + ".config.ip6.dnsServers";
                if (props.containsKey(configDNSs6)) {
                    List<IP6Address> dnsIPs = new ArrayList<IP6Address>();
                    String dnsAll = (String) props.get(configDNSs6);
                    String[] dnss = dnsAll.split(",");
                    for (String dns : dnss) {
                        s_logger.trace("IPv6 DNS: {}", dns);
                        IP6Address dnsIp6 = (IP6Address) IPAddress.parseHostAddress(dns);
                        dnsIPs.add(dnsIp6);
                    }
                    netConfigIP6.setDnsServers(dnsIPs);
                }
                // domains
                String configDomains6 = "net.interface." + interfaceName + ".config.ip6.domains";
                if (props.containsKey(configDomains6)) {
                    List<String> domainNames = new ArrayList<String>();
                    String domainsAll = (String) props.get(configDomains6);
                    String[] domains = domainsAll.split(",");
                    for (String domain : domains) {
                        s_logger.trace("IPv6 Domain: {}", domain);
                        domainNames.add(domain);
                    }
                    netConfigIP6.setDomains(domainNames);
                }
            }
        }
        if (interfaceType == NetInterfaceType.WIFI) {
            s_logger.trace("Adding wifi netconfig");
            // Wifi access point config
            WifiConfig apConfig = getWifiConfig(netIfConfigPrefix, WifiMode.MASTER, props);
            if (apConfig != null) {
                s_logger.trace("Adding AP wifi config");
                netConfigs.add(apConfig);
            } else {
                s_logger.warn("no AP wifi config specified");
            }
            // Wifi client/adhoc config
            // WifiConfig adhocConfig = getWifiConfig(netIfConfigPrefix, WifiMode.ADHOC, props);
            WifiConfig infraConfig = getWifiConfig(netIfConfigPrefix, WifiMode.INFRA, props);
            /*
                 * if(adhocConfig != null && infraConfig != null) {
                 * s_logger.warn("Two conflicting client wifi configs specified");
                 * }
                 */
            if (infraConfig != null) {
                s_logger.trace("Adding client INFRA wifi config");
                netConfigs.add(infraConfig);
            } else {
                s_logger.warn("no INFRA wifi config specified");
            }
        /*
                 * if(adhocConfig != null){
                 * s_logger.trace("Adding client ADHOC wifi config");
                 * netConfigs.add(adhocConfig);
                 * }
                 */
        }
        if (interfaceType == NetInterfaceType.MODEM) {
            s_logger.trace("Adding modem netconfig");
            netConfigs.add(getModemConfig(netIfConfigPrefix, props));
        }
    }
}
Also used : ModemInterfaceAddressConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) UsbDevice(org.eclipse.kura.usb.UsbDevice) ArrayList(java.util.ArrayList) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) ModemTechnologyType(org.eclipse.kura.net.modem.ModemTechnologyType) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) Capability(org.eclipse.kura.net.wifi.WifiInterface.Capability) IP4Address(org.eclipse.kura.net.IP4Address) IP6Address(org.eclipse.kura.net.IP6Address) ModemConnectionStatus(org.eclipse.kura.net.modem.ModemConnectionStatus) ModemConnectionType(org.eclipse.kura.net.modem.ModemConnectionType) NetConfig(org.eclipse.kura.net.NetConfig) DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) NetInterfaceState(org.eclipse.kura.net.NetInterfaceState) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) UsbNetDevice(org.eclipse.kura.usb.UsbNetDevice) EnumSet(java.util.EnumSet) ModemPowerMode(org.eclipse.kura.net.modem.ModemPowerMode) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) IPAddress(org.eclipse.kura.net.IPAddress)

Example 2 with FirewallAutoNatConfig

use of org.eclipse.kura.net.firewall.FirewallAutoNatConfig in project kura by eclipse.

the class NetworkConfiguration method toString.

@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    Iterator<String> it = this.m_netInterfaceConfigs.keySet().iterator();
    while (it.hasNext()) {
        NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_netInterfaceConfigs.get(it.next());
        sb.append("\nname: " + netInterfaceConfig.getName());
        sb.append(" :: Loopback? " + netInterfaceConfig.isLoopback());
        sb.append(" :: Point to Point? " + netInterfaceConfig.isPointToPoint());
        sb.append(" :: Up? " + netInterfaceConfig.isUp());
        sb.append(" :: Virtual? " + netInterfaceConfig.isVirtual());
        sb.append(" :: Driver: " + netInterfaceConfig.getDriver());
        sb.append(" :: Driver Version: " + netInterfaceConfig.getDriverVersion());
        sb.append(" :: Firmware Version: " + netInterfaceConfig.getFirmwareVersion());
        sb.append(" :: MTU: " + netInterfaceConfig.getMTU());
        if (netInterfaceConfig.getHardwareAddress() != null) {
            sb.append(" :: Hardware Address: " + new String(netInterfaceConfig.getHardwareAddress()));
        }
        sb.append(" :: State: " + netInterfaceConfig.getState());
        sb.append(" :: Type: " + netInterfaceConfig.getType());
        sb.append(" :: Usb Device: " + netInterfaceConfig.getUsbDevice());
        List<? extends NetInterfaceAddress> netInterfaceAddresses = netInterfaceConfig.getNetInterfaceAddresses();
        for (NetInterfaceAddress netInterfaceAddress : netInterfaceAddresses) {
            if (netInterfaceAddress.getAddress() != null) {
                sb.append(" :: Address: " + netInterfaceAddress.getAddress().getHostAddress());
            }
            sb.append(" :: Prefix: " + netInterfaceAddress.getNetworkPrefixLength());
            if (netInterfaceAddress.getNetmask() != null) {
                sb.append(" :: Netmask: " + netInterfaceAddress.getNetmask().getHostAddress());
            }
            if (netInterfaceAddress.getBroadcast() != null) {
                sb.append(" :: Broadcast: " + netInterfaceAddress.getBroadcast().getHostAddress());
            }
        }
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        if (netInterfaceAddressConfigs != null) {
            for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                if (netConfigs != null) {
                    for (NetConfig netConfig : netConfigs) {
                        if (netConfig instanceof NetConfigIP4) {
                            sb.append("\n\tIPv4 ");
                            if (((NetConfigIP4) netConfig).isDhcp()) {
                                sb.append(" :: is DHCP client");
                                Map<String, Object> dhcp4Map = ((NetConfigIP4) netConfig).getProperties();
                                for (Map.Entry<String, Object> entry : dhcp4Map.entrySet()) {
                                    String dhcpKey = entry.getKey();
                                    sb.append(" :: " + dhcpKey + ": " + entry.getValue());
                                }
                            } else if (((NetConfigIP4) netConfig).getAddress() == null) {
                                sb.append(" :: is not configured for STATIC or DHCP");
                            } else {
                                sb.append(" :: is STATIC client");
                                if (((NetConfigIP4) netConfig).getAddress() != null) {
                                    sb.append(" :: Address: " + ((NetConfigIP4) netConfig).getAddress().getHostAddress());
                                }
                                sb.append(" :: Prefix: " + ((NetConfigIP4) netConfig).getNetworkPrefixLength());
                                if (((NetConfigIP4) netConfig).getGateway() != null) {
                                    sb.append(" :: Gateway: " + ((NetConfigIP4) netConfig).getGateway().getHostAddress());
                                }
                                List<IP4Address> dnsServers = ((NetConfigIP4) netConfig).getDnsServers();
                                List<IP4Address> winsServers = ((NetConfigIP4) netConfig).getWinsServers();
                                List<String> domains = ((NetConfigIP4) netConfig).getDomains();
                                if (dnsServers != null) {
                                    for (IP4Address dnsServer : dnsServers) {
                                        sb.append(" :: DNS : " + dnsServer.getHostAddress());
                                    }
                                }
                                if (winsServers != null) {
                                    for (IP4Address winsServer : winsServers) {
                                        sb.append(" :: WINS Server : " + winsServer.getHostAddress());
                                    }
                                }
                                if (domains != null) {
                                    for (String domain : domains) {
                                        sb.append(" :: Domains : " + domain);
                                    }
                                }
                            }
                        } else if (netConfig instanceof NetConfigIP6) {
                            sb.append("\n\tIPv6 ");
                            if (((NetConfigIP6) netConfig).isDhcp()) {
                                sb.append(" :: is DHCP client");
                                Map<String, Object> dhcp6Map = ((NetConfigIP6) netConfig).getProperties();
                                Iterator<String> it2 = dhcp6Map.keySet().iterator();
                                while (it2.hasNext()) {
                                    String dhcpKey = it2.next();
                                    sb.append(" :: " + dhcpKey + ": " + dhcp6Map.get(dhcpKey));
                                }
                            } else {
                                sb.append(" :: is STATIC client");
                                if (((NetConfigIP6) netConfig).getAddress() != null) {
                                    sb.append(" :: Address: " + ((NetConfigIP6) netConfig).getAddress().getHostAddress());
                                }
                                List<IP6Address> dnsServers = ((NetConfigIP6) netConfig).getDnsServers();
                                List<String> domains = ((NetConfigIP6) netConfig).getDomains();
                                for (IP6Address dnsServer : dnsServers) {
                                    sb.append(" :: DNS : " + dnsServer.getHostAddress());
                                }
                                for (String domain : domains) {
                                    sb.append(" :: Domains : " + domain);
                                }
                            }
                        } else if (netConfig instanceof WifiConfig) {
                            sb.append("\n\tWifiConfig ");
                            sb.append(" :: SSID: " + ((WifiConfig) netConfig).getSSID());
                            sb.append(" :: BgScan: " + ((WifiConfig) netConfig).getBgscan());
                            sb.append(" :: Broadcast: " + ((WifiConfig) netConfig).getBroadcast());
                            int[] channels = ((WifiConfig) netConfig).getChannels();
                            if (channels != null && channels.length > 0) {
                                for (int i = 0; i < channels.length; i++) {
                                    sb.append(channels[i]);
                                    if (i + 1 < channels.length) {
                                        sb.append(",");
                                    }
                                }
                            }
                            sb.append(" :: Group Ciphers: " + ((WifiConfig) netConfig).getGroupCiphers());
                            sb.append(" :: Hardware Mode: " + ((WifiConfig) netConfig).getHardwareMode());
                            sb.append(" :: Mode: " + ((WifiConfig) netConfig).getMode());
                            sb.append(" :: Pairwise Ciphers: " + ((WifiConfig) netConfig).getPairwiseCiphers());
                            sb.append(" :: Passkey: " + ((WifiConfig) netConfig).getPasskey());
                            sb.append(" :: Security: " + ((WifiConfig) netConfig).getSecurity());
                        } else if (netConfig instanceof ModemConfig) {
                            sb.append("\n\tModemConfig ");
                            sb.append(" :: APN: " + ((ModemConfig) netConfig).getApn());
                            sb.append(" :: Data Compression: " + ((ModemConfig) netConfig).getDataCompression());
                            sb.append(" :: Dial String: " + ((ModemConfig) netConfig).getDialString());
                            sb.append(" :: Header Compression: " + ((ModemConfig) netConfig).getHeaderCompression());
                            sb.append(" :: Password: " + ((ModemConfig) netConfig).getPassword());
                            sb.append(" :: PPP number: " + ((ModemConfig) netConfig).getPppNumber());
                            sb.append(" :: Profile ID: " + ((ModemConfig) netConfig).getProfileID());
                            sb.append(" :: Username: " + ((ModemConfig) netConfig).getUsername());
                            sb.append(" :: Auth Type: " + ((ModemConfig) netConfig).getAuthType());
                            sb.append(" :: IP Address: " + ((ModemConfig) netConfig).getIpAddress());
                            sb.append(" :: PDP Type: " + ((ModemConfig) netConfig).getPdpType());
                        } else if (netConfig instanceof DhcpServerConfig) {
                            sb.append("\n\tDhcpServerConfig ");
                        // TODO - finish displaying
                        } else if (netConfig instanceof FirewallAutoNatConfig) {
                            sb.append("\n\tFirewallAutoNatConfig ");
                        // TODO - finish displaying
                        } else {
                            if (netConfig != null && netConfig.getClass() != null) {
                                sb.append("\n\tUNKNOWN CONFIG TYPE???: " + netConfig.getClass().getName());
                            } else {
                                sb.append("\n\tNULL NETCONFIG PRESENT?!?");
                            }
                        }
                    }
                }
            }
        }
    }
    return sb.toString();
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) Iterator(java.util.Iterator) DhcpServerConfig(org.eclipse.kura.net.dhcp.DhcpServerConfig) List(java.util.List) ArrayList(java.util.ArrayList) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) IP4Address(org.eclipse.kura.net.IP4Address) IP6Address(org.eclipse.kura.net.IP6Address) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetConfig(org.eclipse.kura.net.NetConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with FirewallAutoNatConfig

use of org.eclipse.kura.net.firewall.FirewallAutoNatConfig in project kura by eclipse.

the class NetworkAdminServiceImpl method updateWifiInterfaceConfig.

@Override
public void updateWifiInterfaceConfig(String interfaceName, boolean autoConnect, WifiAccessPoint accessPoint, List<NetConfig> netConfigs) throws KuraException {
    NetConfigIP4 netConfig4 = null;
    NetConfigIP6 netConfig6 = null;
    WifiConfig wifiConfig = null;
    DhcpServerConfigIP4 dhcpServerConfigIP4 = null;
    FirewallAutoNatConfig natConfig = null;
    boolean hadNetConfig4 = false;
    boolean hadNetConfig6 = false;
    boolean hadWifiConfig = false;
    boolean hadDhcpServerConfigIP4 = false;
    boolean hadNatConfig = false;
    if (netConfigs != null && !netConfigs.isEmpty()) {
        for (NetConfig netConfig : netConfigs) {
            if (!netConfig.isValid()) {
                throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "NetConfig Configuration is invalid: " + netConfig.toString());
            }
            if (netConfig instanceof NetConfigIP4) {
                s_logger.debug("got new NetConfigIP4");
                netConfig4 = (NetConfigIP4) netConfig;
            } else if (netConfig instanceof NetConfigIP6) {
                s_logger.debug("got new NetConfigIP6");
                netConfig6 = (NetConfigIP6) netConfig;
            } else if (netConfig instanceof WifiConfig) {
                s_logger.debug("got new WifiConfig");
                wifiConfig = (WifiConfig) netConfig;
            } else if (netConfig instanceof DhcpServerConfigIP4) {
                s_logger.debug("got new DhcpServerConfigIP4");
                dhcpServerConfigIP4 = (DhcpServerConfigIP4) netConfig;
            } else if (netConfig instanceof FirewallAutoNatConfig) {
                s_logger.debug("got new NatConfig");
                natConfig = (FirewallAutoNatConfig) netConfig;
            }
        }
    }
    // validation
    if (netConfig4 == null && netConfig6 == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "Either IPv4 or IPv6 configuration must be defined");
    }
    if (wifiConfig == null) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, "WiFi configuration must be defined");
    }
    List<String> modifiedInterfaceNames = new ArrayList<String>();
    boolean configurationChanged = false;
    ComponentConfiguration originalNetworkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
    if (originalNetworkComponentConfiguration == null) {
        return;
    }
    try {
        NetworkConfiguration newNetworkConfiguration = new NetworkConfiguration(originalNetworkComponentConfiguration.getConfigurationProperties());
        List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = newNetworkConfiguration.getNetInterfaceConfigs();
        for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if (netInterfaceConfig.getName().equals(interfaceName)) {
                // replace existing configs
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
                    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                        List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
                        List<NetConfig> newNetConfigs = new ArrayList<NetConfig>();
                        WifiMode newWifiMode = wifiConfig != null ? wifiConfig.getMode() : null;
                        for (NetConfig netConfig : existingNetConfigs) {
                            s_logger.debug("looking at existing NetConfig for {} with value: {}", interfaceName, netConfig);
                            if (netConfig instanceof NetConfigIP4) {
                                if (netConfig4 == null) {
                                    s_logger.debug("removing NetConfig4 for {}", interfaceName);
                                } else {
                                    hadNetConfig4 = true;
                                    newNetConfigs.add(netConfig4);
                                    if (!netConfig.equals(netConfig4)) {
                                        s_logger.debug("updating NetConfig4 for {}", interfaceName);
                                        s_logger.debug("Is new State DHCP? {}", netConfig4.isDhcp());
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof NetConfig6) {
                                if (netConfig6 == null) {
                                    s_logger.debug("removing NetConfig6 for {}", interfaceName);
                                } else {
                                    hadNetConfig6 = true;
                                    newNetConfigs.add(netConfig6);
                                    if (!netConfig.equals(netConfig6)) {
                                        s_logger.debug("updating NetConfig6 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating NetConfig6 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof WifiConfig) {
                                if (wifiConfig == null) {
                                    s_logger.debug("removing wifiConfig for {}", interfaceName);
                                } else {
                                    // others
                                    if (newWifiMode.equals(((WifiConfig) netConfig).getMode())) {
                                        hadWifiConfig = true;
                                        newNetConfigs.add(wifiConfig);
                                        s_logger.debug("checking WifiConfig for {} mode", wifiConfig.getMode());
                                        if (!netConfig.equals(wifiConfig)) {
                                            s_logger.debug("updating WifiConfig for {}", interfaceName);
                                            configurationChanged = true;
                                            if (!modifiedInterfaceNames.contains(interfaceName)) {
                                                modifiedInterfaceNames.add(interfaceName);
                                            }
                                        } else {
                                            s_logger.debug("not updating WifiConfig for {} because it is unchanged", interfaceName);
                                        }
                                    } else {
                                        // Keep the old WifiConfig for the non-selected wifi modes
                                        s_logger.debug("adding other WifiConfig: {}", netConfig);
                                        newNetConfigs.add(netConfig);
                                    }
                                }
                            } else if (netConfig instanceof DhcpServerConfigIP4) {
                                if (dhcpServerConfigIP4 == null) {
                                    s_logger.debug("removing DhcpServerConfigIP4 for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadDhcpServerConfigIP4 = true;
                                    newNetConfigs.add(dhcpServerConfigIP4);
                                    if (!netConfig.equals(dhcpServerConfigIP4)) {
                                        s_logger.debug("updating DhcpServerConfigIP4 for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating DhcpServerConfigIP4 for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else if (netConfig instanceof FirewallAutoNatConfig) {
                                if (natConfig == null) {
                                    s_logger.debug("removing FirewallAutoNatConfig for {}", interfaceName);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                } else {
                                    hadNatConfig = true;
                                    newNetConfigs.add(natConfig);
                                    if (!netConfig.equals(natConfig)) {
                                        s_logger.debug("updating FirewallAutoNatConfig for {}", interfaceName);
                                        configurationChanged = true;
                                        if (!modifiedInterfaceNames.contains(interfaceName)) {
                                            modifiedInterfaceNames.add(interfaceName);
                                        }
                                    } else {
                                        s_logger.debug("not updating FirewallNatConfig for {} because it is unchanged", interfaceName);
                                    }
                                }
                            } else {
                                s_logger.debug("Found unsupported configuration: {}", netConfig.toString());
                            }
                        }
                        // add configs that did not match any in the current configuration
                        if (netConfigs != null && !netConfigs.isEmpty()) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof NetConfigIP4 && !hadNetConfig4) {
                                    s_logger.debug("adding new NetConfig4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof NetConfigIP6 && !hadNetConfig6) {
                                    s_logger.debug("adding new NetConfig6 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof WifiConfig && !hadWifiConfig) {
                                    s_logger.debug("adding new WifiConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof DhcpServerConfigIP4 && !hadDhcpServerConfigIP4) {
                                    s_logger.debug("adding new DhcpServerConfigIP4 to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                                if (netConfig instanceof FirewallAutoNatConfig && !hadNatConfig) {
                                    s_logger.debug("adding new FirewallAutoNatConfig to existing config for {}", interfaceName);
                                    newNetConfigs.add(netConfig);
                                    configurationChanged = true;
                                    if (!modifiedInterfaceNames.contains(interfaceName)) {
                                        modifiedInterfaceNames.add(interfaceName);
                                    }
                                }
                            }
                        }
                        // Update the wifi mode
                        if (newWifiMode != null) {
                            s_logger.debug("setting address config wifiMode to: {}", newWifiMode);
                            ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setMode(newWifiMode);
                        }
                        // replace with new list
                        for (NetConfig netConfig : newNetConfigs) {
                            s_logger.debug("Current NetConfig: {} :: {}", netConfig.getClass(), netConfig);
                        }
                        ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(newNetConfigs);
                    }
                }
            }
        }
        if (configurationChanged) {
            submitNetworkConfiguration(modifiedInterfaceNames, newNetworkConfiguration);
        }
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating WifiInterfaceConfig", e);
    }
}
Also used : DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) UnknownHostException(java.net.UnknownHostException) NetConfig6(org.eclipse.kura.net.NetConfig6) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) ArrayList(java.util.ArrayList) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) SelfConfiguringComponent(org.eclipse.kura.configuration.SelfConfiguringComponent) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) NetConfig(org.eclipse.kura.net.NetConfig) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Example 4 with FirewallAutoNatConfig

use of org.eclipse.kura.net.firewall.FirewallAutoNatConfig in project kura by eclipse.

the class NetworkAdminServiceImpl method manageFirewall.

@Override
public void manageFirewall(String gatewayIface) throws KuraException {
    // get desired NAT rules interfaces
    LinkedHashSet<NATRule> desiredNatRules = null;
    ComponentConfiguration networkComponentConfiguration = ((SelfConfiguringComponent) this.m_networkConfigurationService).getConfiguration();
    if (gatewayIface != null && networkComponentConfiguration != null) {
        try {
            NetworkConfiguration netConfiguration = new NetworkConfiguration(networkComponentConfiguration.getConfigurationProperties());
            List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = netConfiguration.getNetInterfaceConfigs();
            for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
                String ifaceName = netInterfaceConfig.getName();
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs != null && !netInterfaceAddressConfigs.isEmpty()) {
                    for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                        List<NetConfig> existingNetConfigs = netInterfaceAddressConfig.getConfigs();
                        if (existingNetConfigs != null && !existingNetConfigs.isEmpty()) {
                            for (NetConfig netConfig : existingNetConfigs) {
                                if (netConfig instanceof FirewallAutoNatConfig) {
                                    if (desiredNatRules == null) {
                                        desiredNatRules = new LinkedHashSet<NATRule>();
                                    }
                                    desiredNatRules.add(new NATRule(ifaceName, gatewayIface, true));
                                }
                            }
                        }
                    }
                }
            }
        } catch (UnknownHostException e) {
            s_logger.warn("Exception while updating firewall configuration", e);
        }
    }
    LinuxFirewall firewall = LinuxFirewall.getInstance();
    if (desiredNatRules != null) {
        firewall.replaceAllNatRules(desiredNatRules);
    } else {
        firewall.deleteAllAutoNatRules();
    }
    firewall.enable();
}
Also used : NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) UnknownHostException(java.net.UnknownHostException) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) NATRule(org.eclipse.kura.linux.net.iptables.NATRule) SelfConfiguringComponent(org.eclipse.kura.configuration.SelfConfiguringComponent) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) NetConfig(org.eclipse.kura.net.NetConfig) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 5 with FirewallAutoNatConfig

use of org.eclipse.kura.net.firewall.FirewallAutoNatConfig in project kura by eclipse.

the class FirewallAutoNatConfigReader method getConfig.

private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraProps) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    NetInterfaceType type = netInterfaceConfig.getType();
    if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI) {
        s_logger.debug("Getting NAT config for {}", interfaceName);
        if (kuraProps != null) {
            s_logger.debug("Getting NAT config from kuraProps");
            boolean natEnabled = false;
            boolean useMasquerade = false;
            String prop = null;
            String srcIface = null;
            String dstIface = null;
            StringBuilder sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.enabled");
            if ((prop = kuraProps.getProperty(sb.toString())) != null) {
                natEnabled = Boolean.parseBoolean(prop);
            }
            sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.masquerade");
            if ((prop = kuraProps.getProperty(sb.toString())) != null) {
                useMasquerade = Boolean.parseBoolean(prop);
            }
            sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.src.interface");
            if ((prop = kuraProps.getProperty(sb.toString())) != null) {
                srcIface = prop;
            }
            sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.nat.dst.interface");
            if ((prop = kuraProps.getProperty(sb.toString())) != null) {
                dstIface = prop;
            }
            if (natEnabled) {
                FirewallAutoNatConfig natConfig = new FirewallAutoNatConfig(srcIface, dstIface, useMasquerade);
                List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                if (netInterfaceAddressConfigs == null) {
                    throw KuraException.internalError("NetInterfaceAddress list is null for interface " + interfaceName);
                } else if (netInterfaceAddressConfigs.size() == 0) {
                    throw KuraException.internalError("NetInterfaceAddress list is empty for interface " + interfaceName);
                }
                for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                    List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                    if (netConfigs == null) {
                        netConfigs = new ArrayList<NetConfig>();
                        if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
                            ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        } else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                            ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        }
                    }
                    netConfigs.add(natConfig);
                }
            }
        } else {
            // get it from the firewall file if possible
            LinuxFirewall firewall = LinuxFirewall.getInstance();
            Set<NATRule> natRules = firewall.getAutoNatRules();
            if (natRules != null && !natRules.isEmpty()) {
                Iterator<NATRule> it = natRules.iterator();
                while (it.hasNext()) {
                    NATRule rule = it.next();
                    if (rule.getSourceInterface().equals(interfaceName)) {
                        s_logger.debug("found NAT rule: {}", rule);
                        // this is the one we care about
                        FirewallAutoNatConfig natConfig = new FirewallAutoNatConfig(rule.getSourceInterface(), rule.getDestinationInterface(), rule.isMasquerade());
                        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
                        if (netInterfaceAddressConfigs == null) {
                            throw KuraException.internalError("NetInterfaceAddress list is null for interface " + interfaceName);
                        } else if (netInterfaceAddressConfigs.size() == 0) {
                            throw KuraException.internalError("NetInterfaceAddress list is empty for interface " + interfaceName);
                        }
                        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                            if (netConfigs == null) {
                                netConfigs = new ArrayList<NetConfig>();
                                if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
                                    ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                                } else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                                    ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                                }
                            }
                            netConfigs.add(natConfig);
                        }
                    }
                }
            }
        }
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) NATRule(org.eclipse.kura.linux.net.iptables.NATRule) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Aggregations

FirewallAutoNatConfig (org.eclipse.kura.net.firewall.FirewallAutoNatConfig)12 NetConfig (org.eclipse.kura.net.NetConfig)11 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)10 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)7 ArrayList (java.util.ArrayList)6 KuraException (org.eclipse.kura.KuraException)6 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)6 UnknownHostException (java.net.UnknownHostException)5 NetConfigIP6 (org.eclipse.kura.net.NetConfigIP6)5 NATRule (org.eclipse.kura.linux.net.iptables.NATRule)4 IP4Address (org.eclipse.kura.net.IP4Address)4 DhcpServerConfigIP4 (org.eclipse.kura.net.dhcp.DhcpServerConfigIP4)4 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)4 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)3 SelfConfiguringComponent (org.eclipse.kura.configuration.SelfConfiguringComponent)3 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)3 LinuxFirewall (org.eclipse.kura.linux.net.iptables.LinuxFirewall)3 IPAddress (org.eclipse.kura.net.IPAddress)3 NetInterfaceConfig (org.eclipse.kura.net.NetInterfaceConfig)3 ModemConfig (org.eclipse.kura.net.modem.ModemConfig)3