]>
Commit | Line | Data |
---|---|---|
ab4c538f | 1 | /* |
ec84794b | 2 | * Copyright (C) 2010-2019 Red Hat, Inc. All rights reserved. |
ab4c538f FDN |
3 | * |
4 | * Author: Fabio M. Di Nitto <fabbione@kronosnet.org> | |
5 | * | |
312ef273 | 6 | * This software licensed under LGPL-2.0+ |
ab4c538f FDN |
7 | */ |
8 | ||
de3d1e09 FDN |
9 | #ifndef __LIBNOZZLE_H__ |
10 | #define __LIBNOZZLE_H__ | |
bba2a8d7 | 11 | |
bc098de2 | 12 | #include <sys/types.h> |
04afc256 | 13 | #include <net/if.h> |
b721bf3d | 14 | |
512471bb FDN |
15 | /** |
16 | * | |
17 | * @file libnozzle.h | |
18 | * @brief tap interfaces management API include file | |
ec84794b | 19 | * @copyright Copyright (C) 2010-2019 Red Hat, Inc. All rights reserved. |
512471bb FDN |
20 | * |
21 | * nozzle is a commodity library to manage tap (ethernet) interfaces | |
22 | */ | |
23 | ||
df5be92a | 24 | typedef struct nozzle_iface *nozzle_t; |
bba2a8d7 | 25 | |
3a0ca2d5 FDN |
26 | /** |
27 | * nozzle_open | |
b0911f55 | 28 | * |
3a0ca2d5 FDN |
29 | * @brief create a new tap device on the system. |
30 | * | |
31 | * devname - pointer to device name of at least size IFNAMSIZ. | |
32 | * if the dev strlen is 0, then the system will assign a name automatically. | |
33 | * if a string is specified, the system will try to create a device with | |
34 | * the specified name. | |
35 | * NOTE: on FreeBSD the tap device names can only be tapX where X is a | |
36 | * number from 0 to 255. On Linux such limitation does not apply. | |
37 | * The name must be unique to the system. If an interface with the same | |
38 | * name is already configured on the system, an error will be returned. | |
39 | * | |
bd7d56f8 | 40 | * devname_size - length of the buffer provided in dev (has to be at least IFNAMSIZ). |
3a0ca2d5 FDN |
41 | * |
42 | * updownpath - nozzle supports the typical filesystem structure to execute | |
43 | * actions for: down.d post-down.d pre-up.d up.d | |
44 | * in the form of: | |
45 | * updownpath/<action>/<interface_name> | |
46 | * updownpath specifies where to find those directories on the | |
47 | * filesystem and it must be an absolute path. | |
48 | * | |
49 | * @return | |
50 | * nozzle_open returns | |
51 | * a pointer to a nozzle struct on success | |
52 | * NULL on error and errno is set. | |
53 | */ | |
54 | ||
55 | nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath); | |
56 | ||
39d58f55 FDN |
57 | /** |
58 | * nozzle_close | |
b0911f55 | 59 | * |
39d58f55 FDN |
60 | * @brief deconfigure and destroy a nozzle device |
61 | * | |
62 | * nozzle - pointer to the nozzle struct to destroy | |
63 | * | |
39d58f55 FDN |
64 | * @return |
65 | * 0 on success | |
04122ab7 | 66 | * -1 on error and errno is set. |
39d58f55 FDN |
67 | */ |
68 | ||
02493d92 | 69 | int nozzle_close(nozzle_t nozzle); |
bba2a8d7 | 70 | |
e8772954 FDN |
71 | |
72 | #define NOZZLE_PREUP 0 | |
73 | #define NOZZLE_UP 1 | |
74 | #define NOZZLE_DOWN 2 | |
75 | #define NOZZLE_POSTDOWN 3 | |
76 | ||
77 | /** | |
78 | * nozzle_run_updown | |
b0911f55 CC |
79 | * |
80 | * @brief execute updown commands associated with a nozzle device. | |
e8772954 FDN |
81 | * |
82 | * nozzle - pointer to the nozzle struct | |
83 | * | |
84 | * action - pre-up.d / up.d / down.d / post-down.d (see defines above) | |
85 | * | |
86 | * exec_string - pointers to string to record executing action stdout/stderr. | |
87 | * The string is malloc'ed, the caller needs to free the buffer. | |
88 | * If the script generates no output this string might be NULL. | |
89 | * | |
b0911f55 CC |
90 | * It is the application responsibility to call helper scripts |
91 | * before or after creating/destroying interfaces or IP addresses. | |
92 | * | |
e8772954 FDN |
93 | * @return |
94 | * 0 on success | |
54422a86 FDN |
95 | * -1 on error and errno is set (sanity checks and internal calls. |
96 | * -2 on error from executing the shell scripts, and no errno is set. | |
e8772954 FDN |
97 | */ |
98 | ||
99 | int nozzle_run_updown(const nozzle_t nozzle, uint8_t action, char **exec_string); | |
100 | ||
a12591c4 FDN |
101 | /** |
102 | * nozzle_set_up | |
b0911f55 | 103 | * |
a0f76f1a | 104 | * @brief equivalent of ifconfig up |
a12591c4 FDN |
105 | * |
106 | * nozzle - pointer to the nozzle struct | |
107 | * | |
a12591c4 FDN |
108 | * @return |
109 | * 0 on success | |
04122ab7 | 110 | * -1 on error and errno is set. |
a12591c4 FDN |
111 | */ |
112 | ||
a0f76f1a | 113 | int nozzle_set_up(nozzle_t nozzle); |
22d18ba4 FDN |
114 | |
115 | /** | |
116 | * nozzle_set_down | |
b0911f55 | 117 | * |
1b36edad | 118 | * @brief equivalent of ifconfig down |
22d18ba4 FDN |
119 | * |
120 | * nozzle - pointer to the nozzle struct | |
121 | * | |
22d18ba4 FDN |
122 | * @return |
123 | * 0 on success | |
04122ab7 | 124 | * -1 on error and errno is set. |
22d18ba4 FDN |
125 | */ |
126 | ||
1b36edad | 127 | int nozzle_set_down(nozzle_t nozzle); |
a12591c4 | 128 | |
7025a73b FDN |
129 | /** |
130 | * nozzle_add_ip | |
b0911f55 | 131 | * |
7025a73b FDN |
132 | * @brief equivalent of ip addr or ifconfig <ipaddress/prefix> |
133 | * | |
134 | * nozzle - pointer to the nozzle struct | |
135 | * | |
c538e696 | 136 | * ipaddr - string containing either an IPv4 or an IPv6 address. |
74351a70 FDN |
137 | * Please note that Linux will automatically remove any IPv6 addresses from an interface |
138 | * with MTU < 1280. libnozzle will cache those IPs and re-instate them when MTU is > 1280. | |
139 | * MTU must be set via nozzle_set_mtu for IPv6 to be re-instated. | |
7025a73b FDN |
140 | * |
141 | * prefix - 24, 64 or any valid network prefix for the requested address. | |
142 | * | |
7025a73b FDN |
143 | * @return |
144 | * 0 on success | |
04122ab7 | 145 | * -1 on error and errno is set. |
7025a73b FDN |
146 | */ |
147 | ||
fe202289 | 148 | int nozzle_add_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix); |
54cd9049 FDN |
149 | |
150 | /** | |
151 | * nozzle_del_ip | |
b0911f55 | 152 | * |
54cd9049 FDN |
153 | * @brief equivalent of ip addr del or ifconfig del <ipaddress/prefix> |
154 | * | |
155 | * nozzle - pointer to the nozzle struct | |
156 | * | |
c538e696 | 157 | * ipaddr - string containing either an IPv4 or an IPv6 address. |
54cd9049 FDN |
158 | * |
159 | * prefix - 24, 64 or any valid network prefix for the requested address. | |
160 | * | |
54cd9049 FDN |
161 | * @return |
162 | * 0 on success | |
04122ab7 | 163 | * -1 on error and errno is set. |
54cd9049 FDN |
164 | */ |
165 | ||
fe202289 | 166 | int nozzle_del_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix); |
04122ab7 | 167 | |
e960d7ac FDN |
168 | |
169 | #define IPADDR_CHAR_MAX 128 | |
170 | #define PREFIX_CHAR_MAX 4 | |
171 | ||
172 | struct nozzle_ip { | |
173 | char ipaddr[IPADDR_CHAR_MAX + 1]; | |
174 | char prefix[PREFIX_CHAR_MAX + 1]; | |
175 | int domain; /* AF_INET or AF_INET6 */ | |
176 | struct nozzle_ip *next; | |
177 | }; | |
178 | ||
04122ab7 FDN |
179 | /** |
180 | * nozzle_get_ips | |
b0911f55 | 181 | * |
210558a4 | 182 | * @brief retrieve the list of all configured ips for a given interface |
04122ab7 | 183 | * |
04122ab7 FDN |
184 | * nozzle - pointer to the nozzle struct |
185 | * | |
e960d7ac FDN |
186 | * nozzle_ip - pointer to the head of a list of nozzle_ip structs. |
187 | * The last IP will have next = NULL. | |
188 | * nozzle_ip can be NULL if there are no IP addresses | |
189 | * associated with this nozzle device. | |
190 | * *DO NOT* free those structs as they are used internally | |
191 | * for IP address tracking. | |
04122ab7 FDN |
192 | * |
193 | * @return | |
194 | * 0 on success | |
195 | * -1 on error and errno is set. | |
04122ab7 FDN |
196 | * |
197 | */ | |
198 | ||
e960d7ac | 199 | int nozzle_get_ips(const nozzle_t nozzle, struct nozzle_ip **nozzle_ip); |
727d0eea | 200 | |
81e4a13f FDN |
201 | /** |
202 | * nozzle_get_mtu | |
b0911f55 | 203 | * |
210558a4 | 204 | * @brief retrieve mtu on a given nozzle interface |
81e4a13f FDN |
205 | * |
206 | * nozzle - pointer to the nozzle struct | |
207 | * | |
208 | * @return | |
209 | * MTU on success | |
210 | * -1 on error and errno is set. | |
211 | */ | |
212 | ||
73c0c80a | 213 | int nozzle_get_mtu(const nozzle_t nozzle); |
ee82f183 FDN |
214 | |
215 | /** | |
216 | * nozzle_set_mtu | |
b0911f55 | 217 | * |
ee82f183 FDN |
218 | * @brief set mtu on a given nozzle interface |
219 | * | |
220 | * nozzle - pointer to the nozzle struct | |
221 | * | |
222 | * mtu - new MTU value | |
223 | * | |
ee82f183 FDN |
224 | * @return |
225 | * 0 on success | |
226 | * -1 on error and errno is set. | |
ee82f183 FDN |
227 | */ |
228 | ||
fe202289 | 229 | int nozzle_set_mtu(nozzle_t nozzle, const int mtu); |
ee82f183 FDN |
230 | |
231 | /** | |
232 | * nozzle_reset_mtu | |
b0911f55 | 233 | * |
ee82f183 FDN |
234 | * @brief reset mtu on a given nozzle interface to the system default |
235 | * | |
236 | * nozzle - pointer to the nozzle struct | |
237 | * | |
ee82f183 FDN |
238 | * @return |
239 | * 0 on success | |
240 | * -1 on error and errno is set. | |
ee82f183 FDN |
241 | */ |
242 | ||
fe202289 | 243 | int nozzle_reset_mtu(nozzle_t nozzle); |
58006e7f | 244 | |
085f1d3c FDN |
245 | /** |
246 | * nozzle_get_mac | |
b0911f55 | 247 | * |
210558a4 | 248 | * @brief retrieve mac address on a given nozzle interface |
085f1d3c FDN |
249 | * |
250 | * nozzle - pointer to the nozzle struct | |
251 | * | |
252 | * ether_addr - pointers to string containing the current mac address. | |
253 | * The string is malloc'ed, the caller needs to free this buffer. | |
254 | * @return | |
255 | * 0 on success. | |
256 | * -1 on error and errno is set. | |
257 | */ | |
258 | ||
73c0c80a | 259 | int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr); |
2b0b03f1 FDN |
260 | |
261 | /** | |
262 | * nozzle_set_mac | |
b0911f55 | 263 | * |
2b0b03f1 FDN |
264 | * @brief set mac address on a given nozzle interface |
265 | * | |
266 | * nozzle - pointer to the nozzle struct | |
267 | * | |
268 | * ether_addr - pointers to string containing the new mac address. | |
269 | * | |
270 | * @return | |
271 | * 0 on success. | |
272 | * -1 on error and errno is set. | |
273 | */ | |
274 | ||
73c0c80a | 275 | int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr); |
2b0b03f1 FDN |
276 | |
277 | /** | |
278 | * nozzle_reset_mac | |
b0911f55 | 279 | * |
2b0b03f1 FDN |
280 | * @brief reset mac address on a given nozzle interface to system default |
281 | * | |
282 | * nozzle - pointer to the nozzle struct | |
283 | * | |
284 | * @return | |
285 | * 0 on success. | |
286 | * -1 on error and errno is set. | |
287 | */ | |
288 | ||
73c0c80a | 289 | int nozzle_reset_mac(nozzle_t nozzle); |
12d17cd3 | 290 | |
f080dfa3 FDN |
291 | /** |
292 | * nozzle_get_handle_by_name | |
b0911f55 | 293 | * |
32e67475 | 294 | * @brief find a nozzle handle by device name |
f080dfa3 FDN |
295 | * |
296 | * devname - string containing the name of the interface | |
297 | * | |
298 | * @return | |
299 | * handle on success. | |
300 | * NULL on error and errno is set. | |
301 | */ | |
302 | ||
78fde66b | 303 | nozzle_t nozzle_get_handle_by_name(const char *devname); |
f080dfa3 | 304 | |
97f07fda FDN |
305 | /** |
306 | * nozzle_get_name_by_handle | |
b0911f55 | 307 | * |
210558a4 | 308 | * @brief retrieve nozzle interface name by handle |
97f07fda FDN |
309 | * |
310 | * nozzle - pointer to the nozzle struct | |
311 | * | |
312 | * @return | |
313 | * pointer to the interface name | |
314 | * NULL on error and errno is set. | |
315 | */ | |
316 | ||
317 | const char *nozzle_get_name_by_handle(const nozzle_t nozzle); | |
318 | ||
32e67475 FDN |
319 | /** |
320 | * nozzle_get_fd | |
b0911f55 | 321 | * |
0182269e | 322 | * @brief |
32e67475 FDN |
323 | * |
324 | * nozzle - pointer to the nozzle struct | |
325 | * | |
326 | * @return | |
327 | * fd associated to a given nozzle on success. | |
328 | * -1 on error and errno is set. | |
329 | */ | |
330 | ||
c3cba36f | 331 | int nozzle_get_fd(const nozzle_t nozzle); |
d748a045 | 332 | |
bba2a8d7 | 333 | #endif |