]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address-pool.c
Merge pull request #1063 from poettering/dbus-interface-from-type
[thirdparty/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 "networkd.h"
23 #include "networkd-address-pool.h"
24
25 int address_pool_new(
26 Manager *m,
27 AddressPool **ret,
28 int family,
29 const union in_addr_union *u,
30 unsigned prefixlen) {
31
32 AddressPool *p;
33
34 assert(m);
35 assert(ret);
36 assert(u);
37
38 p = new0(AddressPool, 1);
39 if (!p)
40 return -ENOMEM;
41
42 p->manager = m;
43 p->family = family;
44 p->prefixlen = prefixlen;
45 p->in_addr = *u;
46
47 LIST_PREPEND(address_pools, m->address_pools, p);
48
49 *ret = p;
50 return 0;
51 }
52
53 int address_pool_new_from_string(
54 Manager *m,
55 AddressPool **ret,
56 int family,
57 const char *p,
58 unsigned prefixlen) {
59
60 union in_addr_union u;
61 int r;
62
63 assert(m);
64 assert(ret);
65 assert(p);
66
67 r = in_addr_from_string(family, p, &u);
68 if (r < 0)
69 return r;
70
71 return address_pool_new(m, ret, family, &u, prefixlen);
72 }
73
74 void address_pool_free(AddressPool *p) {
75
76 if (!p)
77 return;
78
79 if (p->manager)
80 LIST_REMOVE(address_pools, p->manager->address_pools, p);
81
82 free(p);
83 }
84
85 static bool address_pool_prefix_is_taken(
86 AddressPool *p,
87 const union in_addr_union *u,
88 unsigned prefixlen) {
89
90 Iterator i;
91 Link *l;
92 Network *n;
93
94 assert(p);
95 assert(u);
96
97 HASHMAP_FOREACH(l, p->manager->links, i) {
98 Address *a;
99
100 /* Don't clash with assigned addresses */
101 LIST_FOREACH(addresses, a, l->addresses) {
102 if (a->family != p->family)
103 continue;
104
105 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
106 return true;
107 }
108
109 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
110 LIST_FOREACH(addresses, a, l->pool_addresses) {
111 if (a->family != p->family)
112 continue;
113
114 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
115 return true;
116 }
117 }
118
119 /* And don't clash with configured but un-assigned addresses either */
120 LIST_FOREACH(networks, n, p->manager->networks) {
121 Address *a;
122
123 LIST_FOREACH(addresses, a, n->static_addresses) {
124 if (a->family != p->family)
125 continue;
126
127 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
136 union in_addr_union u;
137
138 assert(p);
139 assert(prefixlen > 0);
140 assert(found);
141
142 if (p->prefixlen > prefixlen)
143 return 0;
144
145 u = p->in_addr;
146 for (;;) {
147 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
148 _cleanup_free_ char *s = NULL;
149
150 in_addr_to_string(p->family, &u, &s);
151 log_debug("Found range %s/%u", strna(s), prefixlen);
152
153 *found = u;
154 return 1;
155 }
156
157 if (!in_addr_prefix_next(p->family, &u, prefixlen))
158 return 0;
159
160 if (!in_addr_prefix_intersect(p->family, &p->in_addr, p->prefixlen, &u, prefixlen))
161 return 0;
162 }
163
164 return 0;
165 }