aboutsummaryrefslogtreecommitdiffstats
path: root/main/libvirt/0001-net-support-set-public-ip-for-forward-mode-nat.patch
blob: 831466f950fe31129aa9ff9f87ec57c7be4a3e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
From da1e7fe01acdaf13b3fb9e3cda53627063014da9 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Tue, 4 Dec 2012 17:03:51 +0100
Subject: [PATCH] net: support set public ip for forward mode nat

Support setting which public ip to use for NAT via attribute
publicaddr. This will construct an iptables line using '-j SNAT
--to-source <publicaddr>' instead of '-j MASQUERADE'.

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---
 docs/formatnetwork.html.in  |  4 +++-
 src/conf/network_conf.c     | 33 +++++++++++++++++++++++++++++++++
 src/conf/network_conf.h     |  1 +
 src/network/bridge_driver.c | 24 ++++++++++++++++--------
 src/util/iptables.c         | 31 ++++++++++++++++++++++++-------
 src/util/iptables.h         |  6 ++++--
 6 files changed, 81 insertions(+), 18 deletions(-)

diff --git a/docs/formatnetwork.html.in b/docs/formatnetwork.html.in
index 49206dd..07f9783 100644
--- a/docs/formatnetwork.html.in
+++ b/docs/formatnetwork.html.in
@@ -125,7 +125,9 @@
             other network device whether ethernet, wireless, dialup,
             or VPN. If the <code>dev</code> attribute is set, the
             firewall rules will restrict forwarding to the named
-            device only. Inbound connections from other networks are
+            device only. If the <code>publicaddr</code> attribute is set,
+	    the given source address will be used with iptables' SNAT
+	    target. Inbound connections from other networks are
             all prohibited; all connections between guests on the same
             network, and to/from the host to the guests, are
             unrestricted and not NATed.<span class="since">Since
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 6ce2e63..36128ac 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -174,6 +174,7 @@ void virNetworkDefFree(virNetworkDefPtr def)
     VIR_FREE(def->name);
     VIR_FREE(def->bridge);
     VIR_FREE(def->domain);
+    VIR_FREE(def->publicaddr);
 
     for (ii = 0 ; ii < def->nForwardPfs && def->forwardPfs ; ii++) {
         virNetworkForwardPfDefClear(&def->forwardPfs[ii]);
@@ -1211,6 +1212,22 @@ error:
     return result;
 }
 
+static  int
+virValidPublicaddr(const char *publicaddr)
+{
+    /* only check for max len and valid chars for now */
+    const int maxlen = sizeof("123.123.123.123-123.123.123.123:65535-65534")-1;
+    int len = strlen(publicaddr);
+
+    if (len > maxlen)
+        return 0;
+
+    if (strspn(publicaddr, "0123456789.-:") < len)
+        return 0;
+
+    return 1;
+}
+
 static virNetworkDefPtr
 virNetworkDefParseXML(xmlXPathContextPtr ctxt)
 {
@@ -1387,6 +1404,21 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
                 def->managed = 1;
         }
 
+        def->publicaddr = virXPathString("string(./@publicaddr)", ctxt);
+	if (def->publicaddr != NULL) {
+	    char *errstr = NULL;
+            if (def->forwardType != VIR_NETWORK_FORWARD_NAT) {
+	        errstr = "Attribute 'publicaddr' is only valid with mode='nat'";
+	    } else if (!virValidPublicaddr(def->publicaddr)) {
+	        errstr = "Attribute 'publicaddr' must be in the format: ipaddr[-ipaddr][:port[-port]]";
+	    }
+
+	    if (errstr != NULL) {
+                virReportError(VIR_ERR_XML_ERROR, "%s", _(errstr));
+                goto error;
+	    }
+	}
+
         /* all of these modes can use a pool of physical interfaces */
         nForwardIfs = virXPathNodeSet("./interface", ctxt, &forwardIfNodes);
         nForwardPfs = virXPathNodeSet("./pf", ctxt, &forwardPfNodes);
@@ -1861,6 +1893,7 @@ char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags)
         }
         virBufferAddLit(&buf, "<forward");
         virBufferEscapeString(&buf, " dev='%s'", dev);
+        virBufferEscapeString(&buf, " publicaddr='%s'", def->publicaddr);
         virBufferAsprintf(&buf, " mode='%s'", mode);
         if (def->forwardType == VIR_NETWORK_FORWARD_HOSTDEV) {
             if (def->managed == 1)
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 3e46304..76fb591 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -206,6 +206,7 @@ struct _virNetworkDef {
     virPortGroupDefPtr portGroups;
     virNetDevBandwidthPtr bandwidth;
     virNetDevVlan vlan;
+    char *publicaddr;
 };
 
 typedef struct _virNetworkObj virNetworkObj;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 75f3c3a..04f178b 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -1438,7 +1438,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     NULL) < 0) {
+                                     NULL,
+				     network->def->publicaddr) < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
                        _("failed to add iptables rule to enable masquerading to %s") :
@@ -1452,7 +1453,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     "udp") < 0) {
+                                     "udp",
+				     network->def->publicaddr) < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
                        _("failed to add iptables rule to enable UDP masquerading to %s") :
@@ -1466,7 +1468,8 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                      &ipdef->address,
                                      prefix,
                                      forwardIf,
-                                     "tcp") < 0) {
+                                     "tcp",
+				     network->def->publicaddr) < 0) {
         virReportError(VIR_ERR_SYSTEM_ERROR,
                        forwardIf ?
                        _("failed to add iptables rule to enable TCP masquerading to %s") :
@@ -1482,13 +1485,15 @@ networkAddMasqueradingIptablesRules(struct network_driver *driver,
                                     &ipdef->address,
                                     prefix,
                                     forwardIf,
-                                    "udp");
+                                    "udp",
+				    network->def->publicaddr);
  masqerr4:
     iptablesRemoveForwardMasquerade(driver->iptables,
                                     &ipdef->address,
                                     prefix,
                                     forwardIf,
-                                    NULL);
+                                    NULL,
+				    network->def->publicaddr);
  masqerr3:
     iptablesRemoveForwardAllowRelatedIn(driver->iptables,
                                         &ipdef->address,
@@ -1518,17 +1523,20 @@ networkRemoveMasqueradingIptablesRules(struct network_driver *driver,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        "tcp");
+                                        "tcp",
+					network->def->publicaddr);
         iptablesRemoveForwardMasquerade(driver->iptables,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        "udp");
+                                        "udp",
+					network->def->publicaddr);
         iptablesRemoveForwardMasquerade(driver->iptables,
                                         &ipdef->address,
                                         prefix,
                                         forwardIf,
-                                        NULL);
+					NULL,
+					network->def->publicaddr);
 
         iptablesRemoveForwardAllowRelatedIn(driver->iptables,
                                             &ipdef->address,
diff --git a/src/util/iptables.c b/src/util/iptables.c
index 407ca3a..4a89673 100644
--- a/src/util/iptables.c
+++ b/src/util/iptables.c
@@ -804,6 +804,7 @@ iptablesForwardMasquerade(iptablesContext *ctx,
                           unsigned int prefix,
                           const char *physdev,
                           const char *protocol,
+			  const char *publicaddr,
                           int action)
 {
     int ret;
@@ -833,10 +834,24 @@ iptablesForwardMasquerade(iptablesContext *ctx,
     if (physdev && physdev[0])
         virCommandAddArgList(cmd, "--out-interface", physdev, NULL);
 
-    virCommandAddArgList(cmd, "--jump", "MASQUERADE", NULL);
+    /* Use --jump SNAT if public addr is specified */
+    if (publicaddr && publicaddr[0]) {
+        char tmpstr[sizeof("123.123.123.123-123.123.123.123:65535-65535")];
+	const char *portstr = "";
 
-    if (protocol && protocol[0])
-        virCommandAddArgList(cmd, "--to-ports", "1024-65535", NULL);
+	memset(tmpstr, 0, sizeof(tmpstr));
+        if (protocol && protocol[0] && (strchr(publicaddr, ':') == NULL))
+	    portstr = ":1024-65535";
+	snprintf(tmpstr, sizeof(tmpstr), "%s%s", publicaddr, portstr);
+
+        virCommandAddArgList(cmd, "--jump", "SNAT",
+	                          "--to-source", tmpstr, NULL);
+    } else {
+        virCommandAddArgList(cmd, "--jump", "MASQUERADE", NULL);
+
+        if (protocol && protocol[0])
+            virCommandAddArgList(cmd, "--to-ports", "1024-65535", NULL);
+    }
 
     ret = iptablesCommandRunAndFree(cmd);
     VIR_FREE(networkstr);
@@ -861,9 +876,10 @@ iptablesAddForwardMasquerade(iptablesContext *ctx,
                              virSocketAddr *netaddr,
                              unsigned int prefix,
                              const char *physdev,
-                             const char *protocol)
+                             const char *protocol,
+			     const char *publicaddr)
 {
-    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, protocol, ADD);
+    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, protocol, publicaddr, ADD);
 }
 
 /**
@@ -884,9 +900,10 @@ iptablesRemoveForwardMasquerade(iptablesContext *ctx,
                                 virSocketAddr *netaddr,
                                 unsigned int prefix,
                                 const char *physdev,
-                                const char *protocol)
+                                const char *protocol,
+			        const char *publicaddr)
 {
-    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, protocol, REMOVE);
+    return iptablesForwardMasquerade(ctx, netaddr, prefix, physdev, protocol, publicaddr, REMOVE);
 }
 
 
diff --git a/src/util/iptables.h b/src/util/iptables.h
index e54f8b1..a9d2772 100644
--- a/src/util/iptables.h
+++ b/src/util/iptables.h
@@ -105,12 +105,14 @@ int              iptablesAddForwardMasquerade    (iptablesContext *ctx,
                                                   virSocketAddr *netaddr,
                                                   unsigned int prefix,
                                                   const char *physdev,
-                                                  const char *protocol);
+                                                  const char *protocol,
+						  const char *publicaddr);
 int              iptablesRemoveForwardMasquerade (iptablesContext *ctx,
                                                   virSocketAddr *netaddr,
                                                   unsigned int prefix,
                                                   const char *physdev,
-                                                  const char *protocol);
+                                                  const char *protocol,
+						  const char *publicaddr);
 int              iptablesAddOutputFixUdpChecksum (iptablesContext *ctx,
                                                   const char *iface,
                                                   int port);
-- 
1.8.0.1