File: common.h

package info (click to toggle)
ladvd 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,240 kB
  • sloc: ansic: 11,169; sh: 189; makefile: 184
file content (336 lines) | stat: -rw-r--r-- 7,834 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * $Id$
 *
 * Copyright (c) 2008, 2009
 *      Sten Spans <sten@blinkenlights.nl>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _common_h
#define _common_h

#include "config.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#if defined(HAVE_STRNVIS) && !defined(BROKEN_STRNVIS)
#include <vis.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>
#if HAVE_SYS_SYSCTL_H
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <pwd.h>

#include <event.h>
#if HAVE_EVHTTP_H
#include <evhttp.h>
#endif /* HAVE_EVHTTP_H */

#if HAVE_NET_IF_H
#include <net/if.h>
#define _LINUX_IF_H
#define IFF_LOWER_UP 0x10000
#endif
#if HAVE_NETINET_IF_ETHER_H
#include <netinet/if_ether.h>
#endif

#ifndef __noreturn
# define __noreturn	__attribute__ ((noreturn))
#endif
#ifndef __packed
# define __packed	__attribute__ ((packed))
#endif
#ifndef __nonnull
# define __nonnull(x)	__attribute__ ((__nonnull__ x))
#endif
#ifndef __unused
#  define __unused(x)	__attribute__((__unused__)) x
#endif
#if __GNUC__ >= 3
# define likely(x)	__builtin_expect (!!(x), 1)
# define unlikely(x)	__builtin_expect (!!(x), 0)
#else
# define likely(x)	(x)
# define unlikely(x)	(x)
#endif

#include "ether.h"
#include "compat/compat.h"

#define SLEEPTIME   30
#define LADVD_TTL   180

#ifndef IFDESCRSIZE
#define IFDESCRSIZE 64
#endif

#define LLDP_INVENTORY_SIZE 32

#define NETIF_BONDING_LACP  	1
#define NETIF_BONDING_FAILOVER	2
#define NETIF_BONDING_OTHER	3
#define NETIF_CHILD_ACTIVE	1
#define NETIF_CHILD_BACKUP	2

#define NETIF_INDEX_MAX		UINT32_MAX

struct netif {
    uint32_t index;
    char name[IFNAMSIZ];
    char description[IFDESCRSIZE];
    uint8_t hwaddr[ETHER_ADDR_LEN];
    uint16_t mtu;
    int8_t duplex;
    int8_t autoneg_supported; 
    int8_t autoneg_enabled; 
    uint16_t autoneg_pmd;
    uint16_t mau;

    uint32_t ipaddr4;
    uint32_t ipaddr6[4];

    uint8_t argv;
    int8_t type;
    uint8_t child;
    uint8_t bonding_mode;
    uint8_t lacp_index;

    uint16_t vlan_id; 
    uint32_t vlan_parent;

    uint8_t protos;
    uint8_t update;

    struct netif *parent;
    struct netif *subif;

    // should be last
    TAILQ_ENTRY(netif) entries;

    uint8_t link_event;
    uint8_t device_identified;
    char device_name[IFDESCRSIZE];
};

TAILQ_HEAD(nhead, netif);

struct exclif {
    char name[IFNAMSIZ];
    TAILQ_ENTRY(exclif) entries;
};

TAILQ_HEAD(ehead, exclif);

struct hinv {
    char hw_revision[LLDP_INVENTORY_SIZE + 1];
    char fw_revision[LLDP_INVENTORY_SIZE + 1];
    char sw_revision[LLDP_INVENTORY_SIZE + 1];
    char serial_number[LLDP_INVENTORY_SIZE + 1];
    char manufacturer[LLDP_INVENTORY_SIZE + 1];
    char model_name[LLDP_INVENTORY_SIZE + 1];
    char asset_id[LLDP_INVENTORY_SIZE + 1];
};

struct my_sysinfo {
    struct utsname uts;
    char uts_str[256];
    uint8_t uts_rel[3];
    char platform[256];
    char hostname[256];
    char country[3];
    char location[256];
    uint16_t cap;
    uint16_t cap_active;
    uint16_t cap_lldpmed;
    int8_t lldpmed_devtype;
    uint8_t hwaddr[ETHER_ADDR_LEN];
    uint16_t physif_count;

    uint32_t maddr4;
    uint32_t maddr6[4];
    const char *mifname;
    struct netif *mnetif;

    struct hinv hinv;
};

#define CAP_REPEATER	(1 << 0)
#define CAP_BRIDGE	(1 << 1)
#define CAP_HOST	(1 << 2)
#define CAP_ROUTER	(1 << 3)
#define CAP_SWITCH	(1 << 4)
#define CAP_WLAN	(1 << 5)
#define CAP_DOCSIS	(1 << 6)
#define CAP_PHONE	(1 << 7)
#define CAP_OTHER	(1 << 8)
#define CAP_MAX		9
#define CAP_STRING	"rBHRSWCTO"

#define NETIF_INVALID	INT8_MIN
#define NETIF_VLAN	-1
#define NETIF_REGULAR	0
#define NETIF_WIRELESS	1
#define NETIF_TAP	2
#define NETIF_PARENT	10
#define NETIF_TEAMING	11
#define NETIF_BONDING	12
#define NETIF_BRIDGE	13
#define NETIF_OLD	INT8_MAX

#define OPT_DAEMON	(1 << 0)
#define OPT_SEND	(1 << 1)
#define OPT_RECV	(1 << 2)
#define OPT_AUTO	(1 << 3)
#define OPT_ONCE	(1 << 4)
#define OPT_ARGV	(1 << 5)
#define OPT_DEBUG	(1 << 6)
#define OPT_MNETIF	(1 << 7)
#define OPT_MADDR	(1 << 8)
#define OPT_WIRELESS	(1 << 9)
#define OPT_TAP		(1 << 10)
#define OPT_IFDESCR	(1 << 11)
#define OPT_USEDESCR	(1 << 12)
#define OPT_CHASSIS_IF	(1 << 13)
#define OPT_CHECK	(1 << 31)

extern uint32_t options;

struct parent_req {
    uint8_t op;
    uint32_t index;
    char name[IFNAMSIZ];
    ssize_t len;
    char buf[512];
};

#define TEAM_NETIF_CNT 32
struct parent_team_info {
    uint8_t mode;
    uint8_t cnt;
    uint32_t netifs[TEAM_NETIF_CNT];
    uint32_t netif_active;
};

#define PARENT_REQ_MIN	    offsetof(struct parent_req, buf)
#define PARENT_REQ_MAX	    sizeof(struct parent_req)
#define PARENT_REQ_LEN(l)   PARENT_REQ_MIN + l

#define DECODE_STR	1
#define DECODE_PRINT	2

#define PEER_HOSTNAME	0
#define PEER_PORTNAME	1
#define PEER_PORTDESCR	2
#define PEER_CAP	3
#define PEER_ADDR_INET4	4
#define PEER_ADDR_INET6	5
#define PEER_ADDR_802	6
#define PEER_VLAN_ID	7
#define PEER_MAX	8
#define PEER_STR(x,y)  ((x)?(free(y)):(x = y))

static inline
void peer_free(char *p[]) {
    int s;
    for (s = 0; s < PEER_MAX; s++) {
	if (!p[s])
	    continue;
	free(p[s]);
	p[s] = NULL;
    }
}

struct parent_msg {
    uint32_t index;
    char name[IFNAMSIZ];
    uint8_t proto;
    time_t received;
    ssize_t len;
    unsigned char msg[ETHER_MAX_LEN];

    uint8_t decode;
    uint16_t ttl;
    char *peer[PEER_MAX];

    uint8_t lock;

    // should be last
    TAILQ_ENTRY(parent_msg) entries;
};

TAILQ_HEAD(mhead, parent_msg);

#define PARENT_MSG_MIN	    offsetof(struct parent_msg, msg)
#define PARENT_MSG_MAX	    offsetof(struct parent_msg, decode)
#define PARENT_MSG_SIZ	    sizeof(struct parent_msg)
#define PARENT_MSG_LEN(l)   PARENT_MSG_MIN + l
#define PARENT_OPEN	    0
#define PARENT_CLOSE	    1
#define PARENT_DESCR	    2
#define PARENT_ALIAS	    3
#define PARENT_DEVICE	    4
#define PARENT_DEVICE_ID    5
#define PARENT_ETHTOOL_GSET 6
#define PARENT_ETHTOOL_GDRV 7
#define PARENT_TEAMNL	    8
#define PARENT_MAX	    9

struct proto {
    uint8_t enabled;
    const char *name;
    uint8_t dst_addr[ETHER_ADDR_LEN];
    uint8_t llc_org[3];
    uint16_t llc_pid;
    size_t (* const build) (uint8_t, void *, struct netif *, struct nhead *,
			    struct my_sysinfo *);
    unsigned char * (* const check) (void *, size_t);
    size_t (* const decode) (struct parent_msg *);
};

void cli_main(int argc, char *argv[]) __noreturn;
void child_init(int reqfd, int msgfd, int ifc, char *ifl[], struct passwd *pwd);
void parent_init(int reqfd, int msgfd, pid_t pid);
void parent_signal(int fd, short event, void *pid);

void sysinfo_fetch(struct my_sysinfo *);
void netif_init();
uint16_t netif_fetch(int ifc, char *ifl[], struct my_sysinfo *, struct nhead *);
int netif_media(struct netif *);

#endif /* _common_h */