]> git.proxmox.com Git - systemd.git/blob - src/network/networkd-address-pool.c
Imported Upstream version 228
[systemd.git] / src / network / networkd-address-pool.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "alloc-util.h"
23 #include "networkd-address-pool.h"
24 #include "networkd.h"
25 #include "set.h"
26 #include "string-util.h"
27
28 int address_pool_new(
29 Manager *m,
30 AddressPool **ret,
31 int family,
32 const union in_addr_union *u,
33 unsigned prefixlen) {
34
35 AddressPool *p;
36
37 assert(m);
38 assert(ret);
39 assert(u);
40
41 p = new0(AddressPool, 1);
42 if (!p)
43 return -ENOMEM;
44
45 p->manager = m;
46 p->family = family;
47 p->prefixlen = prefixlen;
48 p->in_addr = *u;
49
50 LIST_PREPEND(address_pools, m->address_pools, p);
51
52 *ret = p;
53 return 0;
54 }
55
56 int address_pool_new_from_string(
57 Manager *m,
58 AddressPool **ret,
59 int family,
60 const char *p,
61 unsigned prefixlen) {
62
63 union in_addr_union u;
64 int r;
65
66 assert(m);
67 assert(ret);
68 assert(p);
69
70 r = in_addr_from_string(family, p, &u);
71 if (r < 0)
72 return r;
73
74 return address_pool_new(m, ret, family, &u, prefixlen);
75 }
76
77 void address_pool_free(AddressPool *p) {
78
79 if (!p)
80 return;
81
82 if (p->manager)
83 LIST_REMOVE(address_pools, p->manager->address_pools, p);
84
85 free(p);
86 }
87
88 static bool address_pool_prefix_is_taken(
89 AddressPool *p,
90 const union in_addr_union *u,
91 unsigned prefixlen) {
92
93 Iterator i;
94 Link *l;
95 Network *n;
96
97 assert(p);
98 assert(u);
99
100 HASHMAP_FOREACH(l, p->manager->links, i) {
101 Address *a;
102 Iterator j;
103
104 /* Don't clash with assigned addresses */
105 SET_FOREACH(a, l->addresses, j) {
106 if (a->family != p->family)
107 continue;
108
109 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
110 return true;
111 }
112
113 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
114 LIST_FOREACH(addresses, a, l->pool_addresses) {
115 if (a->family != p->family)
116 continue;
117
118 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
119 return true;
120 }
121 }
122
123 /* And don't clash with configured but un-assigned addresses either */
124 LIST_FOREACH(networks, n, p->manager->networks) {
125 Address *a;
126
127 LIST_FOREACH(addresses, a, n->static_addresses) {
128 if (a->family != p->family)
129 continue;
130
131 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
132 return true;
133 }
134 }
135
136 return false;
137 }
138
139 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
140 union in_addr_union u;
141
142 assert(p);
143 assert(prefixlen > 0);
144 assert(found);
145
146 if (p->prefixlen > prefixlen)
147 return 0;
148
149 u = p->in_addr;
150 for (;;) {
151 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
152 _cleanup_free_ char *s = NULL;
153
154 in_addr_to_string(p->family, &u, &s);
155 log_debug("Found range %s/%u", strna(s), prefixlen);
156
157 *found = u;
158 return 1;
159 }
160
161 if (!in_addr_prefix_next(p->family, &u, prefixlen))
162 return 0;
163
164 if (!in_addr_prefix_intersect(p->family, &p->in_addr, p->prefixlen, &u, prefixlen))
165 return 0;
166 }
167
168 return 0;
169 }