Add IPV4 properties parameter validation

Added parameter validation for the IPV4 properties: IP Address,
Gateway, and Netmask prefix length.
This is important since we delete the IPV4 interface before adding
the new one (i.e. if the add is unsuccessful we are down an IPV4
interface). This validation helps to prevent some of the
unsuccessful adds.

Took the logic from the network manager valid parameter checks:
https://github.com/openbmc/phosphor-networkd/blob/master/util.cpp#L217

Moved "Netmask Prefix Length" to a "number" to do this. It should
have been a number.

Tested: A variety of good and bad values.
Change-Id: Idf4486489097bc426164b9543ea8c05eb54a2bf8
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/app/common/services/api-utils.js b/app/common/services/api-utils.js
index 43d4772..d7e4e7f 100644
--- a/app/common/services/api-utils.js
+++ b/app/common/services/api-utils.js
@@ -56,6 +56,11 @@
                   });
           return deferred.promise;
         },
+        validIPV4IP: function(ip) {
+          // Checks for [0-255].[0-255].[0-255].[0-255]
+          return ip.match(
+              /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
+        },
         getHostState: function() {
           var deferred = $q.defer();
           $http({
diff --git a/app/configuration/controllers/network-controller.html b/app/configuration/controllers/network-controller.html
index d237d47..41ab8a3 100644
--- a/app/configuration/controllers/network-controller.html
+++ b/app/configuration/controllers/network-controller.html
@@ -61,9 +61,10 @@
 						<label for="net-config__subnet" class="inline">Gateway</label>
 						<input id="net-config__subnet" type="text" ng-disabled="interface.DHCPEnabled" value="" ng-model="ipv4.Gateway"/>
 					</div>
+					<!-- This netmask prefix length max only works with IPV4 -->
 					<div class="column small-12 large-4">
 						<label for="net-config__default-gateway" class="inline">Netmask Prefix Length</label>
-						<input id="net-config__default-gateway" type="text" ng-disabled="interface.DHCPEnabled"  ng-model="ipv4.PrefixLength"/>
+						<input id="net-config__default-gateway" type="number" min="1" max="32" step="1" ng-disabled="interface.DHCPEnabled"  ng-model="ipv4.PrefixLength"/>
 					</div>
 				</fieldset>
 			</fieldset>
diff --git a/app/configuration/controllers/network-controller.js b/app/configuration/controllers/network-controller.js
index 6ebbf4f..d5fdc00 100644
--- a/app/configuration/controllers/network-controller.js
+++ b/app/configuration/controllers/network-controller.js
@@ -58,6 +58,30 @@
         // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
         if (!$scope.interface.DHCPEnabled) {
           for (var i in $scope.interface.ipv4.values) {
+            if (!APIUtils.validIPV4IP(
+                    $scope.interface.ipv4.values[i].Address)) {
+              $scope.set_network_error =
+                  $scope.interface.ipv4.values[i].Address +
+                  ' invalid IP parameter';
+              $scope.loading = false;
+              return;
+            }
+            if (!APIUtils.validIPV4IP(
+                    $scope.interface.ipv4.values[i].Gateway)) {
+              $scope.set_network_error =
+                  $scope.interface.ipv4.values[i].Address +
+                  ' invalid gateway parameter';
+              $scope.loading = false;
+              return;
+            }
+            // The netmask prefix length will be undefined if outside range
+            if (!$scope.interface.ipv4.values[i].PrefixLength) {
+              $scope.set_network_error =
+                  $scope.interface.ipv4.values[i].Address +
+                  ' invalid Prefix Length parameter';
+              $scope.loading = false;
+              return;
+            }
             if ($scope.interface.ipv4.values[i].Address !=
                     $scope.old_interface.ipv4.values[i].Address ||
                 $scope.interface.ipv4.values[i].PrefixLength !=