]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/client/conf-system.c
3d6e5ffdef393de98d62b5eaf6f6bfc5544462ed
[thirdparty/lldpd.git] / src / client / conf-system.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2013 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <unistd.h>
19 #include <string.h>
20 #include <sys/utsname.h>
21
22 #include "client.h"
23 #include "../log.h"
24
25 static int
26 cmd_iface_pattern(struct lldpctl_conn_t *conn, struct writer *w,
27 struct cmd_env *env, void *arg)
28 {
29 log_debug("lldpctl", "set iface pattern");
30
31 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
32 if (config == NULL) {
33 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
34 lldpctl_last_strerror(conn));
35 return 0;
36 }
37
38 const char *value = cmdenv_get(env, "iface-pattern");
39 if (lldpctl_atom_set_str(config,
40 lldpctl_k_config_iface_pattern,
41 value) == NULL) {
42 log_warnx("lldpctl", "unable to set iface-pattern. %s",
43 lldpctl_last_strerror(conn));
44 lldpctl_atom_dec_ref(config);
45 return 0;
46 }
47 log_info("lldpctl", "iface-pattern set to new value %s",
48 value?value:"(none)");
49 lldpctl_atom_dec_ref(config);
50 return 1;
51 }
52
53 static int
54 cmd_iface_promisc(struct lldpctl_conn_t *conn, struct writer *w,
55 struct cmd_env *env, void *arg)
56 {
57 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
58 if (config == NULL) {
59 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
60 lldpctl_last_strerror(conn));
61 return 0;
62 }
63 if (lldpctl_atom_set_int(config,
64 lldpctl_k_config_iface_promisc,
65 arg?1:0) == NULL) {
66 log_warnx("lldpctl", "unable to %s promiscuous mode: %s",
67 arg?"enable":"disable",
68 lldpctl_last_strerror(conn));
69 lldpctl_atom_dec_ref(config);
70 return 0;
71 }
72 log_info("lldpctl", "interface promiscuous mode %s",
73 arg?"enabled":"disabled");
74 lldpctl_atom_dec_ref(config);
75 return 1;
76 }
77
78 static int
79 cmd_system_description(struct lldpctl_conn_t *conn, struct writer *w,
80 struct cmd_env *env, void *arg)
81 {
82 int platform = 0;
83 const char *what = arg;
84 const char *value;
85 if (!strcmp(what, "system")) {
86 value = cmdenv_get(env, "description");
87 } else {
88 value = cmdenv_get(env, "platform");
89 platform = 1;
90 }
91 log_debug("lldpctl", "set %s description", what);
92 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
93 if (config == NULL) {
94 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
95 lldpctl_last_strerror(conn));
96 return 0;
97 }
98 if (lldpctl_atom_set_str(config,
99 platform?lldpctl_k_config_platform:lldpctl_k_config_description,
100 value) == NULL) {
101 log_warnx("lldpctl", "unable to set description. %s",
102 lldpctl_last_strerror(conn));
103 lldpctl_atom_dec_ref(config);
104 return 0;
105 }
106 log_info("lldpctl", "description set to new value %s",
107 value?value:"(none)");
108 lldpctl_atom_dec_ref(config);
109 return 1;
110 }
111
112 static int
113 cmd_management(struct lldpctl_conn_t *conn, struct writer *w,
114 struct cmd_env *env, void *arg)
115 {
116 log_debug("lldpctl", "set management pattern");
117
118 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
119 if (config == NULL) {
120 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
121 lldpctl_last_strerror(conn));
122 return 0;
123 }
124
125 const char *value = cmdenv_get(env, "management-pattern");
126 if (lldpctl_atom_set_str(config,
127 lldpctl_k_config_mgmt_pattern, value) == NULL) {
128 log_warnx("lldpctl", "unable to set management pattern. %s",
129 lldpctl_last_strerror(conn));
130 lldpctl_atom_dec_ref(config);
131 return 0;
132 }
133 log_info("lldpctl", "management pattaren set to new value %s",
134 value?value:"(none)");
135 lldpctl_atom_dec_ref(config);
136 return 1;
137 }
138
139 static int
140 cmd_hostname(struct lldpctl_conn_t *conn, struct writer *w,
141 struct cmd_env *env, void *arg)
142 {
143 struct utsname un;
144 log_debug("lldpctl", "set system name");
145
146 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
147 if (config == NULL) {
148 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
149 lldpctl_last_strerror(conn));
150 return 0;
151 }
152
153 const char *value = cmdenv_get(env, "hostname");
154 if (value && strlen(value) == 1 && value[0] == '.') {
155 if (uname(&un) < 0) {
156 log_warn("lldpctl", "cannot get node name");
157 return 0;
158 }
159 value = un.nodename;
160 }
161 if (lldpctl_atom_set_str(config,
162 lldpctl_k_config_hostname, value) == NULL) {
163 log_warnx("lldpctl", "unable to set system name. %s",
164 lldpctl_last_strerror(conn));
165 lldpctl_atom_dec_ref(config);
166 return 0;
167 }
168 log_info("lldpctl", "system name set to new value %s",
169 value?value:"(none)");
170 lldpctl_atom_dec_ref(config);
171 return 1;
172 }
173
174 static int
175 cmd_update_descriptions(struct lldpctl_conn_t *conn, struct writer *w,
176 struct cmd_env *env, void *arg)
177 {
178 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
179 if (config == NULL) {
180 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
181 lldpctl_last_strerror(conn));
182 return 0;
183 }
184 if (lldpctl_atom_set_int(config,
185 lldpctl_k_config_ifdescr_update,
186 arg?1:0) == NULL) {
187 log_warnx("lldpctl", "unable to %s interface description update: %s",
188 arg?"enable":"disable",
189 lldpctl_last_strerror(conn));
190 lldpctl_atom_dec_ref(config);
191 return 0;
192 }
193 log_info("lldpctl", "interface description update %s",
194 arg?"enabled":"disabled");
195 lldpctl_atom_dec_ref(config);
196 return 1;
197 }
198
199 static int
200 cmd_bondslave_srcmac_type(struct lldpctl_conn_t *conn, struct writer *w,
201 struct cmd_env *env, void *arg)
202 {
203 char *value_str;
204 int value = -1;
205
206 log_debug("lldpctl", "bond slave src mac");
207
208 lldpctl_atom_t *config = lldpctl_get_configuration(conn);
209 if (config == NULL) {
210 log_warnx("lldpctl",
211 "unable to get configuration from lldpd. %s",
212 lldpctl_last_strerror(conn));
213 return 0;
214 }
215
216 value_str = arg;
217 for (lldpctl_map_t *b_map =
218 lldpctl_key_get_map(lldpctl_k_config_bond_slave_src_mac_type);
219 b_map->string; b_map++) {
220 if (!strcmp(b_map->string, value_str)) {
221 value = b_map->value;
222 break;
223 }
224 }
225
226 if (value == -1) {
227 log_warnx("lldpctl", "invalid value");
228 return 0;
229 }
230
231 if (lldpctl_atom_set_int(config,
232 lldpctl_k_config_bond_slave_src_mac_type, value) == NULL) {
233 log_warnx("lldpctl", "unable to set bond slave src mac type."
234 " %s", lldpctl_last_strerror(conn));
235 lldpctl_atom_dec_ref(config);
236 return 0;
237 }
238
239 log_info("lldpctl", "bond slave src mac set to new value: %s",
240 value_str);
241 lldpctl_atom_dec_ref(config);
242
243 return 1;
244 }
245
246 /**
247 * Register `configure system bond-slave-src-mac-type`
248 */
249 static void
250 register_commands_srcmac_type(struct cmd_node *configure)
251 {
252 struct cmd_node *bond_slave_src_mac_type =
253 commands_new(configure,
254 "bond-slave-src-mac-type",
255 "Set LLDP bond slave source MAC type",
256 NULL, NULL, NULL);
257
258 for (lldpctl_map_t *b_map =
259 lldpctl_key_get_map(lldpctl_k_config_bond_slave_src_mac_type);
260 b_map->string; b_map++) {
261 if (!strcmp(b_map->string, "real")) {
262 commands_new(
263 commands_new(bond_slave_src_mac_type,
264 b_map->string, "Real mac",
265 NULL, NULL, NULL),
266 NEWLINE, NULL,
267 NULL, cmd_bondslave_srcmac_type,
268 b_map->string);
269 } else if (!strcmp(b_map->string, "zero")) {
270 commands_new(
271 commands_new(bond_slave_src_mac_type,
272 b_map->string, "All zero mac",
273 NULL, NULL, NULL),
274 NEWLINE, NULL,
275 NULL, cmd_bondslave_srcmac_type,
276 b_map->string);
277 } else if (!strcmp(b_map->string, "fixed")) {
278 commands_new(
279 commands_new(bond_slave_src_mac_type,
280 b_map->string, "Fixed value (3Com card)",
281 NULL, NULL, NULL),
282 NEWLINE, NULL,
283 NULL, cmd_bondslave_srcmac_type,
284 b_map->string);
285 } else if (!strcmp(b_map->string, "local")) {
286 commands_new(
287 commands_new(bond_slave_src_mac_type,
288 b_map->string, "Real Mac with locally "
289 "administered bit set",
290 NULL, NULL, NULL),
291 NEWLINE, NULL,
292 NULL, cmd_bondslave_srcmac_type,
293 b_map->string);
294 }
295 }
296 }
297
298 /**
299 * Register `configure system` commands.
300 *
301 * Those are the commands to configure protocol-independant stuff.
302 */
303 void
304 register_commands_configure_system(struct cmd_node *configure,
305 struct cmd_node *unconfigure)
306 {
307 struct cmd_node *configure_system = commands_new(
308 configure,
309 "system", "System configuration",
310 cmd_check_no_env, NULL, "ports");
311 struct cmd_node *unconfigure_system = commands_new(
312 unconfigure,
313 "system", "System configuration",
314 cmd_check_no_env, NULL, "ports");
315 struct cmd_node *configure_interface = commands_new(
316 configure_system,
317 "interface", "Interface related items",
318 NULL, NULL, NULL);
319 struct cmd_node *unconfigure_interface = commands_new(
320 unconfigure_system,
321 "interface", "Interface related items",
322 NULL, NULL, NULL);
323
324 commands_new(
325 commands_new(
326 commands_new(configure_system,
327 "description", "Override chassis description",
328 NULL, NULL, NULL),
329 NULL, "Chassis description",
330 NULL, cmd_store_env_value, "description"),
331 NEWLINE, "Override chassis description",
332 NULL, cmd_system_description, "system");
333 commands_new(
334 commands_new(unconfigure_system,
335 "description", "Don't override chassis description",
336 NULL, NULL, NULL),
337 NEWLINE, "Don't override chassis description",
338 NULL, cmd_system_description, "system");
339
340 commands_new(
341 commands_new(
342 commands_new(configure_system,
343 "platform", "Override platform description",
344 NULL, NULL, NULL),
345 NULL, "Platform description (CDP)",
346 NULL, cmd_store_env_value, "platform"),
347 NEWLINE, "Override platform description",
348 NULL, cmd_system_description, "platform");
349 commands_new(
350 commands_new(unconfigure_system,
351 "platform", "Don't override platform description",
352 NULL, NULL, NULL),
353 NEWLINE, "Don't override platform description",
354 NULL, cmd_system_description, "platform");
355
356 commands_new(
357 commands_new(
358 commands_new(configure_system,
359 "hostname", "Override system name",
360 NULL, NULL, NULL),
361 NULL, "System name",
362 NULL, cmd_store_env_value, "hostname"),
363 NEWLINE, "Override system name",
364 NULL, cmd_hostname, NULL);
365 commands_new(
366 commands_new(unconfigure_system,
367 "hostname", "Don't override system name",
368 NULL, NULL, NULL),
369 NEWLINE, "Don't override system name",
370 NULL, cmd_hostname, NULL);
371
372 commands_new(
373 commands_new(
374 commands_new(
375 commands_new(
376 commands_new(configure_system,
377 "ip", "IP related options",
378 NULL, NULL, NULL),
379 "management", "IP management related options",
380 NULL, NULL, NULL),
381 "pattern", "Set IP management pattern",
382 NULL, NULL, NULL),
383 NULL, "IP management pattern (comma-separated list of wildcards)",
384 NULL, cmd_store_env_value, "management-pattern"),
385 NEWLINE, "Set IP management pattern",
386 NULL, cmd_management, NULL);
387 commands_new(
388 commands_new(
389 commands_new(
390 commands_new(unconfigure_system,
391 "ip", "IP related options",
392 NULL, NULL, NULL),
393 "management", "IP management related options",
394 NULL, NULL, NULL),
395 "pattern", "Delete any IP management pattern",
396 NULL, NULL, NULL),
397 NEWLINE, "Delete any IP management pattern",
398 NULL, cmd_management, NULL);
399
400 commands_new(
401 commands_new(
402 commands_new(configure_interface,
403 "pattern", "Set active interface pattern",
404 NULL, NULL, NULL),
405 NULL, "Interface pattern (comma-separated list of wildcards)",
406 NULL, cmd_store_env_value, "iface-pattern"),
407 NEWLINE, "Set active interface pattern",
408 NULL, cmd_iface_pattern, NULL);
409 commands_new(
410 commands_new(unconfigure_interface,
411 "pattern", "Delete any interface pattern",
412 NULL, NULL, NULL),
413 NEWLINE, "Delete any interface pattern",
414 NULL, cmd_iface_pattern, NULL);
415
416 commands_new(
417 commands_new(configure_interface,
418 "description", "Update interface descriptions with neighbor name",
419 NULL, NULL, NULL),
420 NEWLINE, "Update interface descriptions with neighbor name",
421 NULL, cmd_update_descriptions, "enable");
422 commands_new(
423 commands_new(unconfigure_interface,
424 "description", "Don't update interface descriptions with neighbor name",
425 NULL, NULL, NULL),
426 NEWLINE, "Don't update interface descriptions with neighbor name",
427 NULL, cmd_update_descriptions, NULL);
428
429 commands_new(
430 commands_new(configure_interface,
431 "promiscuous", "Enable promiscuous mode on managed interfaces",
432 NULL, NULL, NULL),
433 NEWLINE, "Enable promiscuous mode on managed interfaces",
434 NULL, cmd_iface_promisc, "enable");
435 commands_new(
436 commands_new(unconfigure_interface,
437 "promiscuous", "Don't enable promiscuous mode on managed interfaces",
438 NULL, NULL, NULL),
439 NEWLINE, "Don't enable promiscuous mode on managed interfaces",
440 NULL, cmd_iface_promisc, NULL);
441
442 register_commands_srcmac_type(configure_system);
443 }
444