map: backport fixes from trunk
[14.07/openwrt.git] / package / network / ipv6 / map / src / mapcalc.c
index 03f8165..b4b3d73 100644 (file)
@@ -2,7 +2,7 @@
  * mapcalc - MAP parameter calculation
  *
  * Author: Steven Barth <cyrus@openwrt.org>
- * Copyright (c) 2014 cisco Systems, Inc.
+ * Copyright (c) 2014-2015 cisco Systems, Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2
@@ -271,8 +271,10 @@ int main(int argc, char *argv[])
                if (ealen < 0 && pdlen >= 0)
                        ealen = pdlen - prefix6len;
 
-               if (psidlen < 0)
+               if (psidlen <= 0) {
                        psidlen = ealen - (32 - prefix4len);
+                       psid = -1;
+               }
 
                if (psid < 0 && psidlen <= 16 && psidlen >= 0 && pdlen >= 0 && ealen >= psidlen) {
                        bmemcpys64(&psid16, &pd, prefix6len + ealen - psidlen, psidlen);
@@ -343,14 +345,17 @@ int main(int argc, char *argv[])
                }
 
 
-               if (psidlen == 0) {
-                       printf("RULE_%d_PORTSETS=0-65535\n", rulecnt);
-               } else if (psid >= 0) {
+               if (psidlen > 0 && psid >= 0) {
                        printf("RULE_%d_PORTSETS='", rulecnt);
                        for (int k = (offset) ? 1 : 0; k < (1 << offset); ++k) {
                                int start = (k << (16 - offset)) | (psid >> offset);
                                int end = start + (1 << (16 - offset - psidlen)) - 1;
-                               printf("%d-%d ", start, end);
+
+                               if (start == 0)
+                                       start = 1;
+
+                               if (start <= end)
+                                       printf("%d-%d ", start, end);
                        }
                        printf("'\n");
                }