1 /*
2  * network_conf.h: network XML handling
3  *
4  * Copyright (C) 2006-2016 Red Hat, Inc.
5  * Copyright (C) 2006-2008 Daniel P. Berrange
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #define DNS_RECORD_LENGTH_SRV  (512 - 30)  /* Limit minus overhead as mentioned in RFC-2782 */
25 
26 #include "internal.h"
27 #include "virthread.h"
28 #include "virsocketaddr.h"
29 #include "virnetdevbandwidth.h"
30 #include "virnetdevvportprofile.h"
31 #include "virnetdevvlan.h"
32 #include "virmacaddr.h"
33 #include "device_conf.h"
34 #include "virbitmap.h"
35 #include "networkcommon_conf.h"
36 #include "virobject.h"
37 #include "virmacmap.h"
38 #include "virenum.h"
39 #include "virxml.h"
40 
41 struct _virNetworkXMLOption {
42     virObject parent;
43 
44     virXMLNamespace ns;
45 };
46 typedef struct _virNetworkXMLOption virNetworkXMLOption;
47 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkXMLOption, virObjectUnref);
48 
49 
50 typedef enum {
51     VIR_NETWORK_FORWARD_NONE   = 0,
52     VIR_NETWORK_FORWARD_NAT,
53     VIR_NETWORK_FORWARD_ROUTE,
54     VIR_NETWORK_FORWARD_OPEN,
55     VIR_NETWORK_FORWARD_BRIDGE,
56     VIR_NETWORK_FORWARD_PRIVATE,
57     VIR_NETWORK_FORWARD_VEPA,
58     VIR_NETWORK_FORWARD_PASSTHROUGH,
59     VIR_NETWORK_FORWARD_HOSTDEV,
60 
61     VIR_NETWORK_FORWARD_LAST,
62 } virNetworkForwardType;
63 
64 typedef enum {
65    VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_DEFAULT = 0,
66    VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_KERNEL,
67    VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT,
68 
69    VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LAST,
70 } virNetworkBridgeMACTableManagerType;
71 
72 VIR_ENUM_DECL(virNetworkBridgeMACTableManager);
73 
74 typedef enum {
75     VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NONE = 0,
76     VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_PCI,
77     VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV,
78     /* USB Device to be added here when supported */
79 
80     VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST,
81 } virNetworkForwardHostdevDeviceType;
82 
83 typedef enum {
84     VIR_NETWORK_DHCP_LEASETIME_UNIT_SECONDS = 0,
85     VIR_NETWORK_DHCP_LEASETIME_UNIT_MINUTES,
86     VIR_NETWORK_DHCP_LEASETIME_UNIT_HOURS,
87 
88     VIR_NETWORK_DHCP_LEASETIME_UNIT_LAST,
89 } virNetworkDHCPLeaseTimeUnitType;
90 
91 VIR_ENUM_DECL(virNetworkDHCPLeaseTimeUnit);
92 
93 /* The backend driver used for devices from the pool. Currently used
94  * only for PCI devices (vfio vs. kvm), but could be used for other
95  * device types in the future.
96  */
97 typedef enum {
98     VIR_NETWORK_FORWARD_DRIVER_NAME_DEFAULT, /* kvm now, could change */
99     VIR_NETWORK_FORWARD_DRIVER_NAME_KVM,    /* force legacy kvm style */
100     VIR_NETWORK_FORWARD_DRIVER_NAME_VFIO,   /* force vfio */
101 
102     VIR_NETWORK_FORWARD_DRIVER_NAME_LAST
103 } virNetworkForwardDriverNameType;
104 
105 VIR_ENUM_DECL(virNetworkForwardDriverName);
106 
107 typedef struct _virNetworkDHCPLeaseTimeDef virNetworkDHCPLeaseTimeDef;
108 struct _virNetworkDHCPLeaseTimeDef {
109     unsigned long long expiry;
110     virNetworkDHCPLeaseTimeUnitType unit;
111 };
112 
113 typedef struct _virNetworkDHCPRangeDef virNetworkDHCPRangeDef;
114 struct _virNetworkDHCPRangeDef {
115     virSocketAddrRange addr;
116     virNetworkDHCPLeaseTimeDef *lease;
117 };
118 
119 typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
120 struct _virNetworkDHCPHostDef {
121     char *mac;
122     char *id;
123     char *name;
124     virSocketAddr ip;
125     virNetworkDHCPLeaseTimeDef *lease;
126 };
127 
128 typedef struct _virNetworkDNSTxtDef virNetworkDNSTxtDef;
129 struct _virNetworkDNSTxtDef {
130     char *name;
131     char *value;
132 };
133 
134 typedef struct _virNetworkDNSSrvDef virNetworkDNSSrvDef;
135 struct _virNetworkDNSSrvDef {
136     char *domain;
137     char *service;
138     char *protocol;
139     char *target;
140     unsigned int port;
141     unsigned int priority;
142     unsigned int weight;
143 };
144 
145 typedef struct _virNetworkDNSHostDef virNetworkDNSHostDef;
146 struct _virNetworkDNSHostDef {
147     virSocketAddr ip;
148     size_t nnames;
149     char **names;
150 };
151 
152 
153 typedef struct _virNetworkDNSForwarder virNetworkDNSForwarder;
154 struct _virNetworkDNSForwarder {
155     virSocketAddr addr;
156     char *domain;
157 };
158 
159 typedef struct _virNetworkDNSDef virNetworkDNSDef;
160 struct _virNetworkDNSDef {
161     int enable;            /* enum virTristateBool */
162     int forwardPlainNames; /* enum virTristateBool */
163     size_t ntxts;
164     virNetworkDNSTxtDef *txts;
165     size_t nhosts;
166     virNetworkDNSHostDef *hosts;
167     size_t nsrvs;
168     virNetworkDNSSrvDef *srvs;
169     size_t nfwds;
170     virNetworkDNSForwarder *forwarders;
171 };
172 
173 typedef struct _virNetworkIPDef virNetworkIPDef;
174 struct _virNetworkIPDef {
175     char *family;               /* ipv4 or ipv6 - default is ipv4 */
176     virSocketAddr address;      /* Bridge IP address */
177 
178     /* One or the other of the following two will be used for a given
179      * IP address, but never both. The parser guarantees this.
180      * Use virNetworkIPDefPrefix/virNetworkIPDefNetmask rather
181      * than accessing the data directly - these utility functions
182      * will convert one into the other as necessary.
183      */
184     unsigned int prefix;        /* ipv6 - only prefix allowed */
185     virSocketAddr netmask;      /* ipv4 - either netmask or prefix specified */
186 
187     int localPTR; /* virTristateBool */
188 
189     size_t nranges;             /* Zero or more dhcp ranges */
190     virNetworkDHCPRangeDef *ranges;
191 
192     size_t nhosts;              /* Zero or more dhcp hosts */
193     virNetworkDHCPHostDef *hosts;
194 
195     char *tftproot;
196     char *bootfile;
197     virSocketAddr bootserver;
198    };
199 
200 typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
201 struct _virNetworkForwardIfDef {
202     int type;
203     union {
204         virPCIDeviceAddress pci; /*PCI Address of device */
205         /* when USB devices are supported a new variable to be added here */
206         char *dev;      /* name of device */
207     }device;
208     int connections; /* how many guest interfaces are connected to this device? */
209 };
210 
211 typedef struct _virNetworkForwardPfDef virNetworkForwardPfDef;
212 struct _virNetworkForwardPfDef {
213     char *dev;      /* name of device */
214     int connections; /* how many guest interfaces are connected to this device? */
215 };
216 
217 typedef struct _virNetworkForwardDef virNetworkForwardDef;
218 struct _virNetworkForwardDef {
219     int type;     /* One of virNetworkForwardType constants */
220     bool managed;  /* managed attribute for hostdev mode */
221     int driverName; /* enum virNetworkForwardDriverNameType */
222 
223     /* If there are multiple forward devices (i.e. a pool of
224      * interfaces), they will be listed here.
225      */
226     size_t npfs;
227     virNetworkForwardPfDef *pfs;
228 
229     size_t nifs;
230     virNetworkForwardIfDef *ifs;
231 
232     /* ranges for NAT */
233     virSocketAddrRange addr;
234     virPortRange port;
235 
236     virTristateBool natIPv6;
237 };
238 
239 typedef struct _virPortGroupDef virPortGroupDef;
240 struct _virPortGroupDef {
241     char *name;
242     bool isDefault;
243     virNetDevVPortProfile *virtPortProfile;
244     virNetDevBandwidth *bandwidth;
245     virNetDevVlan vlan;
246     int trustGuestRxFilters; /* enum virTristateBool */
247 };
248 
249 typedef struct _virNetworkDef virNetworkDef;
250 struct _virNetworkDef {
251     unsigned char uuid[VIR_UUID_BUFLEN];
252     bool uuid_specified;
253     char *name;
254     int   connections; /* # of guest interfaces connected to this network */
255 
256     char *bridge;       /* Name of bridge device */
257     char *bridgeZone;  /* name of firewalld zone for bridge */
258     int  macTableManager; /* enum virNetworkBridgeMACTableManager */
259     char *domain;
260     int domainLocalOnly; /* enum virTristateBool: yes disables dns forwarding */
261     unsigned long delay;   /* Bridge forward delay (ms) */
262     bool stp; /* Spanning tree protocol */
263     unsigned int mtu; /* MTU for bridge, 0 means "default" i.e. unset in config */
264     virMacAddr mac; /* mac address of bridge device */
265     bool mac_specified;
266 
267     /* specified if ip6tables rules added
268      * when no ipv6 gateway addresses specified.
269      */
270     bool ipv6nogw;
271 
272     virNetworkForwardDef forward;
273 
274     size_t nips;
275     virNetworkIPDef *ips; /* ptr to array of IP addresses on this network */
276 
277     size_t nroutes;
278     virNetDevIPRoute **routes; /* ptr to array of static routes on this interface */
279 
280     virNetworkDNSDef dns;   /* dns related configuration */
281     virNetDevVPortProfile *virtPortProfile;
282 
283     size_t nPortGroups;
284     virPortGroupDef *portGroups;
285     virNetDevBandwidth *bandwidth;
286     virNetDevVlan vlan;
287     int trustGuestRxFilters; /* enum virTristateBool */
288     virTristateBool isolatedPort;
289 
290     /* Application-specific custom metadata */
291     xmlNodePtr metadata;
292 
293     /* Network specific XML namespace data */
294     void *namespaceData;
295     virXMLNamespace ns;
296 };
297 
298 typedef enum {
299     VIR_NETWORK_TAINT_HOOK,                 /* Hook script was executed over
300                                                network. We can't guarantee
301                                                connectivity or other settings
302                                                as the script may have played
303                                                with iptables, tc, you name it.
304                                              */
305 
306     VIR_NETWORK_TAINT_LAST
307 } virNetworkTaintFlags;
308 
309 void virNetworkDefFree(virNetworkDef *def);
310 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkDef, virNetworkDefFree);
311 
312 enum {
313     VIR_NETWORK_OBJ_LIST_ADD_LIVE = (1 << 0),
314     VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE = (1 << 1),
315 };
316 
317 virNetworkXMLOption *
318 virNetworkXMLOptionNew(virXMLNamespace *xmlns);
319 
320 virNetworkDef *
321 virNetworkDefCopy(virNetworkDef *def,
322                   virNetworkXMLOption *xmlopt,
323                   unsigned int flags);
324 
325 virNetworkDef *
326 virNetworkDefParseXML(xmlXPathContextPtr ctxt,
327                       virNetworkXMLOption *xmlopt);
328 
329 virNetworkDef *
330 virNetworkDefParseString(const char *xmlStr,
331                          virNetworkXMLOption *xmlopt,
332                          bool validate);
333 
334 virNetworkDef *
335 virNetworkDefParseFile(const char *filename,
336                        virNetworkXMLOption *xmlopt);
337 
338 virNetworkDef *
339 virNetworkDefParseNode(xmlDocPtr xml,
340                        xmlNodePtr root,
341                        virNetworkXMLOption *xmlopt);
342 
343 char *
344 virNetworkDefFormat(const virNetworkDef *def,
345                     virNetworkXMLOption *xmlopt,
346                     unsigned int flags);
347 
348 int
349 virNetworkDefFormatBuf(virBuffer *buf,
350                        const virNetworkDef *def,
351                        virNetworkXMLOption *xmlopt,
352                        unsigned int flags);
353 
354 const char *
355 virNetworkDefForwardIf(const virNetworkDef *def,
356                        size_t n);
357 
358 virPortGroupDef *
359 virPortGroupFindByName(virNetworkDef *net,
360                        const char *portgroup);
361 
362 virNetworkIPDef *
363 virNetworkDefGetIPByIndex(const virNetworkDef *def,
364                           int family,
365                           size_t n);
366 
367 virNetDevIPRoute *
368 virNetworkDefGetRouteByIndex(const virNetworkDef *def,
369                              int family,
370                              size_t n);
371 
372 int
373 virNetworkIPDefPrefix(const virNetworkIPDef *def);
374 
375 int
376 virNetworkIPDefNetmask(const virNetworkIPDef *def,
377                        virSocketAddr *netmask);
378 
379 int
380 virNetworkSaveXML(const char *configDir,
381                   virNetworkDef *def,
382                   const char *xml);
383 
384 int
385 virNetworkSaveConfig(const char *configDir,
386                      virNetworkDef *def,
387                      virNetworkXMLOption *xmlopt);
388 
389 char *
390 virNetworkConfigFile(const char *dir,
391                      const char *name);
392 
393 void
394 virNetworkSetBridgeMacAddr(virNetworkDef *def);
395 
396 int
397 virNetworkPortOptionsParseXML(xmlXPathContextPtr ctxt,
398                               virTristateBool *isolatedPort);
399 
400 void
401 virNetworkPortOptionsFormat(virTristateBool isolatedPort,
402                             virBuffer *buf);
403 
404 VIR_ENUM_DECL(virNetworkForward);
405 
406 #define VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE \
407                 (VIR_CONNECT_LIST_NETWORKS_ACTIVE | \
408                  VIR_CONNECT_LIST_NETWORKS_INACTIVE)
409 
410 #define VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT \
411                 (VIR_CONNECT_LIST_NETWORKS_PERSISTENT | \
412                  VIR_CONNECT_LIST_NETWORKS_TRANSIENT)
413 
414 #define VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART \
415                 (VIR_CONNECT_LIST_NETWORKS_AUTOSTART | \
416                  VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART)
417 
418 #define VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL \
419                 (VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE     | \
420                  VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT | \
421                  VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART)
422 
423 /* for testing */
424 int
425 virNetworkDefUpdateSection(virNetworkDef *def,
426                            unsigned int command, /* virNetworkUpdateCommand */
427                            unsigned int section, /* virNetworkUpdateSection */
428                            int parentIndex,
429                            const char *xml,
430                            unsigned int flags);  /* virNetworkUpdateFlags */
431 
432 VIR_ENUM_DECL(virNetworkTaint);
433