1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3  * Copyright (c) 2008 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 <stdlib.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include "lldpd-structs.h"
22 #include "log.h"
23 
24 void
lldpd_chassis_mgmt_cleanup(struct lldpd_chassis * chassis)25 lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *chassis)
26 {
27 	struct lldpd_mgmt *mgmt, *mgmt_next;
28 
29 	log_debug("alloc", "cleanup management addresses for chassis %s",
30 	    chassis->c_name ? chassis->c_name : "(unknown)");
31 
32 	for (mgmt = TAILQ_FIRST(&chassis->c_mgmt);
33 	     mgmt != NULL;
34 	     mgmt = mgmt_next) {
35 		mgmt_next = TAILQ_NEXT(mgmt, m_entries);
36 		free(mgmt);
37 	}
38 	TAILQ_INIT(&chassis->c_mgmt);
39 }
40 
41 void
lldpd_chassis_cleanup(struct lldpd_chassis * chassis,int all)42 lldpd_chassis_cleanup(struct lldpd_chassis *chassis, int all)
43 {
44 	lldpd_chassis_mgmt_cleanup(chassis);
45 	log_debug("alloc", "cleanup chassis %s",
46 	    chassis->c_name ? chassis->c_name : "(unknown)");
47 #ifdef ENABLE_LLDPMED
48 	free(chassis->c_med_hw);
49 	free(chassis->c_med_sw);
50 	free(chassis->c_med_fw);
51 	free(chassis->c_med_sn);
52 	free(chassis->c_med_manuf);
53 	free(chassis->c_med_model);
54 	free(chassis->c_med_asset);
55 #endif
56 	free(chassis->c_id);
57 	free(chassis->c_name);
58 	free(chassis->c_descr);
59 	if (all)
60 		free(chassis);
61 }
62 
63 #ifdef ENABLE_DOT1
64 void
lldpd_vlan_cleanup(struct lldpd_port * port)65 lldpd_vlan_cleanup(struct lldpd_port *port)
66 {
67 	struct lldpd_vlan *vlan, *vlan_next;
68 	for (vlan = TAILQ_FIRST(&port->p_vlans);
69 	    vlan != NULL;
70 	    vlan = vlan_next) {
71 		free(vlan->v_name);
72 		vlan_next = TAILQ_NEXT(vlan, v_entries);
73 		free(vlan);
74 	}
75 	TAILQ_INIT(&port->p_vlans);
76 	port->p_pvid = 0;
77 }
78 
79 void
lldpd_ppvid_cleanup(struct lldpd_port * port)80 lldpd_ppvid_cleanup(struct lldpd_port *port)
81 {
82 	struct lldpd_ppvid *ppvid, *ppvid_next;
83 	for (ppvid = TAILQ_FIRST(&port->p_ppvids);
84 	    ppvid != NULL;
85 	    ppvid = ppvid_next) {
86 		ppvid_next = TAILQ_NEXT(ppvid, p_entries);
87 		free(ppvid);
88 	}
89 	TAILQ_INIT(&port->p_ppvids);
90 }
91 
92 void
lldpd_pi_cleanup(struct lldpd_port * port)93 lldpd_pi_cleanup(struct lldpd_port *port)
94 {
95 	struct lldpd_pi *pi, *pi_next;
96 	for (pi = TAILQ_FIRST(&port->p_pids);
97 	    pi != NULL;
98 	    pi = pi_next) {
99 		free(pi->p_pi);
100 		pi_next = TAILQ_NEXT(pi, p_entries);
101 		free(pi);
102 	}
103 	TAILQ_INIT(&port->p_pids);
104 }
105 #endif
106 
107 #ifdef ENABLE_CUSTOM
108 void
lldpd_custom_tlv_add(struct lldpd_port * port,struct lldpd_custom * curr)109 lldpd_custom_tlv_add(struct lldpd_port *port, struct lldpd_custom *curr)
110 {
111 	struct lldpd_custom *custom;
112 
113 	if ((custom = malloc(sizeof(struct lldpd_custom)))) {
114 		memcpy(custom, curr, sizeof(struct lldpd_custom));
115 		if ((custom->oui_info = malloc(custom->oui_info_len))) {
116 			memcpy(custom->oui_info, curr->oui_info, custom->oui_info_len);
117 			TAILQ_INSERT_TAIL(&port->p_custom_list, custom, next);
118 		} else {
119 			free(custom);
120 			log_warn("rpc", "could not allocate memory for custom TLV info");
121 		}
122 	}
123 }
124 
125 void
lldpd_custom_tlv_cleanup(struct lldpd_port * port,struct lldpd_custom * curr)126 lldpd_custom_tlv_cleanup(struct lldpd_port *port, struct lldpd_custom *curr)
127 {
128 	struct lldpd_custom *custom, *custom_next;
129 	for (custom = TAILQ_FIRST(&port->p_custom_list);
130 	    custom != NULL;
131 	    custom = custom_next) {
132 		custom_next = TAILQ_NEXT(custom, next);
133 		if (!memcmp(curr->oui, custom->oui, sizeof(curr->oui)) &&
134 		    curr->subtype == custom->subtype) {
135 			TAILQ_REMOVE(&port->p_custom_list, custom, next);
136 			free(custom->oui_info);
137 			free(custom);
138 		}
139 	}
140 }
141 
142 void
lldpd_custom_list_cleanup(struct lldpd_port * port)143 lldpd_custom_list_cleanup(struct lldpd_port *port)
144 {
145 	struct lldpd_custom *custom, *custom_next;
146 	for (custom = TAILQ_FIRST(&port->p_custom_list);
147 	    custom != NULL;
148 	    custom = custom_next) {
149 		custom_next = TAILQ_NEXT(custom, next);
150 		free(custom->oui_info);
151 		free(custom);
152 	}
153 	TAILQ_INIT(&port->p_custom_list);
154 }
155 #endif
156 
157 /* Cleanup a remote port. The before last argument, `expire` is a function that
158  * should be called when a remote port is removed. If the last argument is 1,
159  * all remote ports are removed.
160  */
161 void
lldpd_remote_cleanup(struct lldpd_hardware * hardware,void (* expire)(struct lldpd_hardware *,struct lldpd_port *),int all)162 lldpd_remote_cleanup(struct lldpd_hardware *hardware,
163     void(*expire)(struct lldpd_hardware *, struct lldpd_port *),
164     int all)
165 {
166 	struct lldpd_port *port, *port_next;
167 	int del;
168 	time_t now = time(NULL);
169 
170 	log_debug("alloc", "cleanup remote port on %s",
171 	    hardware->h_ifname);
172 	for (port = TAILQ_FIRST(&hardware->h_rports);
173 	     port != NULL;
174 	     port = port_next) {
175 		port_next = TAILQ_NEXT(port, p_entries);
176 		del = all;
177 		if (!all && expire &&
178 		    (now >= port->p_lastupdate + port->p_ttl)) {
179 			if (port->p_ttl > 0) hardware->h_ageout_cnt++;
180 			del = 1;
181 		}
182 		if (del) {
183 			if (expire) expire(hardware, port);
184 			/* This TAILQ_REMOVE is dangerous. It should not be
185 			 * called while in liblldpctl because we don't have a
186 			 * real list. It is only needed to be called when we
187 			 * don't delete the entire list. */
188 			if (!all) TAILQ_REMOVE(&hardware->h_rports, port, p_entries);
189 
190 			hardware->h_delete_cnt++;
191 			/* Register last removal to be able to report lldpStatsRemTablesLastChangeTime */
192 			hardware->h_lport.p_lastremove = time(NULL);
193 			lldpd_port_cleanup(port, 1);
194 			free(port);
195 		}
196 	}
197 	if (all) TAILQ_INIT(&hardware->h_rports);
198 }
199 
200 /* If `all' is true, clear all information, including information that
201    are not refreshed periodically. Port should be freed manually. */
202 void
lldpd_port_cleanup(struct lldpd_port * port,int all)203 lldpd_port_cleanup(struct lldpd_port *port, int all)
204 {
205 #ifdef ENABLE_LLDPMED
206 	int i;
207 	if (all)
208 		for (i=0; i < LLDP_MED_LOCFORMAT_LAST; i++)
209 			free(port->p_med_location[i].data);
210 #endif
211 #ifdef ENABLE_DOT1
212 	lldpd_vlan_cleanup(port);
213 	lldpd_ppvid_cleanup(port);
214 	lldpd_pi_cleanup(port);
215 #endif
216 	/* will set these to NULL so we don't free wrong memory */
217 
218 	if (all) {
219 		free(port->p_id);
220 		port->p_id = NULL;
221 		free(port->p_descr);
222 		port->p_descr = NULL;
223 		free(port->p_lastframe);
224 		if (port->p_chassis) { /* chassis may not have been attributed, yet */
225 			port->p_chassis->c_refcount--;
226 			port->p_chassis = NULL;
227 		}
228 #ifdef ENABLE_CUSTOM
229 		lldpd_custom_list_cleanup(port);
230 #endif
231 	}
232 }
233 
234 void
lldpd_config_cleanup(struct lldpd_config * config)235 lldpd_config_cleanup(struct lldpd_config *config)
236 {
237 	log_debug("alloc", "general configuration cleanup");
238 	free(config->c_mgmt_pattern);
239 	free(config->c_cid_pattern);
240 	free(config->c_cid_string);
241 	free(config->c_iface_pattern);
242 	free(config->c_perm_ifaces);
243 	free(config->c_hostname);
244 	free(config->c_platform);
245 	free(config->c_description);
246 }
247