summaryrefslogtreecommitdiff
path: root/src/conf/networkcommon_conf.c
blob: 6f88d173c6cfd5f03cf47c8824ed60169038dcd8 (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
/*
 * networkcommon_conf.c: network XML handling
 *
 * Copyright (C) 2006-2014 Red Hat, Inc.
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "virerror.h"
#include "networkcommon_conf.h"
#include "virxml.h"

#define VIR_FROM_THIS VIR_FROM_NETWORK

virNetDevIPRoute *
virNetDevIPRouteCreate(const char *errorDetail,
                       const char *family,
                       const char *address,
                       const char *netmask,
                       const char *gateway,
                       unsigned int prefix,
                       bool hasPrefix,
                       unsigned int metric,
                       bool hasMetric)
{
    g_autoptr(virNetDevIPRoute) def = NULL;
    virSocketAddr testAddr;

    def = g_new0(virNetDevIPRoute, 1);

    def->family = g_strdup(family);

    def->prefix = prefix;
    def->has_prefix = hasPrefix;
    def->metric = metric;
    def->has_metric = hasMetric;

    /* Note: both network and gateway addresses must be specified */

    if (!address) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Missing required address attribute in route definition"),
                       errorDetail);
        return NULL;
    }

    if (!gateway) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Missing required gateway attribute in route definition"),
                       errorDetail);
        return NULL;
    }

    if (virSocketAddrParse(&def->address, address, AF_UNSPEC) < 0) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Bad network address '%2$s' in route definition"),
                       errorDetail, address);
        return NULL;
    }

    if (virSocketAddrParse(&def->gateway, gateway, AF_UNSPEC) < 0) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Bad gateway address '%2$s' in route definition"),
                       errorDetail, gateway);
        return NULL;
    }

    /* validate network address, etc. for each family */
    if ((def->family == NULL) || (STREQ(def->family, "ipv4"))) {
        if (!(VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) ||
              VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_UNSPEC))) {
            virReportError(VIR_ERR_XML_ERROR,
                           def->family == NULL ?
                           _("%1$s: No family specified for non-IPv4 address '%2$s' in route definition") :
                           _("%1$s: IPv4 family specified for non-IPv4 address '%2$s' in route definition"),
                           errorDetail, address);
            return NULL;
        }
        if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->gateway, AF_INET)) {
            virReportError(VIR_ERR_XML_ERROR,
                           def->family == NULL ?
                           _("%1$s: No family specified for non-IPv4 gateway '%2$s' in route definition") :
                           _("%1$s: IPv4 family specified for non-IPv4 gateway '%2$s' in route definition"),
                           errorDetail, address);
            return NULL;
        }
        if (netmask) {
            if (virSocketAddrParse(&def->netmask, netmask, AF_UNSPEC) < 0) {
                virReportError(VIR_ERR_XML_ERROR,
                               _("%1$s: Bad netmask address '%2$s' in route definition"),
                               errorDetail, netmask);
                return NULL;
            }
            if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->netmask, AF_INET)) {
                virReportError(VIR_ERR_XML_ERROR,
                               _("%1$s: Invalid netmask '%2$s' for address '%3$s' (both must be IPv4)"),
                               errorDetail, netmask, address);
                return NULL;
            }
            if (def->has_prefix) {
                /* can't have both netmask and prefix at the same time */
                virReportError(VIR_ERR_XML_ERROR,
                               _("%1$s: Route definition cannot have both a prefix and a netmask"),
                               errorDetail);
                return NULL;
            }
        }
        if (def->prefix > 32) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("%1$s: Invalid prefix %2$u specified in route definition, must be 0 - 32"),
                           errorDetail, def->prefix);
            return NULL;
        }
    } else if (STREQ(def->family, "ipv6")) {
        if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("%1$s: ipv6 family specified for non-IPv6 address '%2$s' in route definition"),
                           errorDetail, address);
            return NULL;
        }
        if (netmask) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("%1$s: Specifying netmask invalid for IPv6 address '%2$s' in route definition"),
                           errorDetail, address);
            return NULL;
        }
        if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->gateway, AF_INET6)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("%1$s: ipv6 specified for non-IPv6 gateway address '%2$s' in route definition"),
                           errorDetail, gateway);
            return NULL;
        }
        if (def->prefix > 128) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("%1$s: Invalid prefix %2$u specified in route definition, must be 0 - 128"),
                           errorDetail, def->prefix);
            return NULL;
        }
    } else {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Unrecognized family '%2$s' in route definition"),
                       errorDetail, def->family);
        return NULL;
    }

    /* make sure the address is a network address */
    if (netmask) {
        if (virSocketAddrMask(&def->address, &def->netmask, &testAddr) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%1$s: Error converting address '%2$s' with netmask '%3$s' to network-address in route definition"),
                           errorDetail, address, netmask);
            return NULL;
        }
    } else {
        if (virSocketAddrMaskByPrefix(&def->address,
                                      def->prefix, &testAddr) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%1$s: Error converting address '%2$s' with prefix %3$u to network-address in route definition"),
                           errorDetail, address, def->prefix);
            return NULL;
        }
    }
    if (!virSocketAddrEqual(&def->address, &testAddr)) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("%1$s: Address '%2$s' in route definition is not a network address"),
                       errorDetail, address);
        return NULL;
    }

    return g_steal_pointer(&def);
}

virNetDevIPRoute *
virNetDevIPRouteParseXML(const char *errorDetail,
                         xmlNodePtr node)
{
    g_autofree char *family = virXMLPropString(node, "family");
    g_autofree char *address = virXMLPropString(node, "address");
    g_autofree char *netmask = virXMLPropString(node, "netmask");
    g_autofree char *gateway = virXMLPropString(node, "gateway");
    unsigned int prefix = 0;
    unsigned int metric = 0;
    bool hasPrefix = false;
    bool hasMetric = false;
    int rc;

    if ((rc = virXMLPropUInt(node, "prefix", 10, VIR_XML_PROP_NONE, &prefix)) < 0)
        return NULL;

    if (rc == 1)
        hasPrefix = true;

    if ((rc = virXMLPropUInt(node, "metric", 10, VIR_XML_PROP_NONZERO, &metric)) < 0)
        return NULL;

    if (rc == 1)
        hasMetric = true;

    return virNetDevIPRouteCreate(errorDetail, family, address, netmask,
                                  gateway, prefix, hasPrefix, metric,
                                  hasMetric);
}

int
virNetDevIPRouteFormat(virBuffer *buf,
                       const virNetDevIPRoute *def)
{
    g_autofree char *address = NULL;
    g_autofree char *netmask = NULL;
    g_autofree char *gateway = NULL;

    virBufferAddLit(buf, "<route");

    if (def->family)
        virBufferAsprintf(buf, " family='%s'", def->family);

    if (!(address = virSocketAddrFormat(&def->address)))
        return -1;
    virBufferAsprintf(buf, " address='%s'", address);

    if (VIR_SOCKET_ADDR_VALID(&def->netmask)) {
        if (!(netmask = virSocketAddrFormat(&def->netmask)))
            return -1;
        virBufferAsprintf(buf, " netmask='%s'", netmask);
    }
    if (def->has_prefix)
        virBufferAsprintf(buf, " prefix='%u'", def->prefix);

    if (!(gateway = virSocketAddrFormat(&def->gateway)))
        return -1;
    virBufferAsprintf(buf, " gateway='%s'", gateway);

    if (def->has_metric && def->metric > 0)
        virBufferAsprintf(buf, " metric='%u'", def->metric);
    virBufferAddLit(buf, "/>\n");

    return 0;
}