forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if.c
286 lines (258 loc) · 6.56 KB
/
if.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2014 Red Hat, Inc. -- Jiri Benc <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "if.h"
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "ethtool.h"
#include "handler.h"
#include "label.h"
#include "list.h"
#include "netlink.h"
#include "netns.h"
#include "utils.h"
#include "compat.h"
static int fill_if_link(struct if_entry *dest, struct nlmsg *msg)
{
struct ifinfomsg *ifi;
struct nlattr **tb, **linkinfo = NULL;
int err;
if (nlmsg_get_hdr(msg)->nlmsg_type != RTM_NEWLINK)
return ENOENT;
ifi = nlmsg_get(msg, sizeof(*ifi));
if (!ifi)
return ENOENT;
tb = nlmsg_attrs(msg, IFLA_MAX);
if (!tb)
return ENOMEM;
if (!tb[IFLA_IFNAME]) {
err = ENOENT;
goto out;
}
dest->if_index = ifi->ifi_index;
dest->if_name = strdup(nla_read_str(tb[IFLA_IFNAME]));
if (!dest->if_name) {
err = ENOMEM;
goto err_ifname;
}
if (ifi->ifi_flags & IFF_UP) {
dest->flags |= IF_UP;
if (ifi->ifi_flags & IFF_RUNNING)
dest->flags |= IF_HAS_LINK;
}
if (tb[IFLA_MASTER])
dest->master_index = nla_read_u32(tb[IFLA_MASTER]);
if (tb[IFLA_LINK]) {
dest->link_index = nla_read_u32(tb[IFLA_LINK]);
if (tb[IFLA_LINK_NETNSID])
dest->link_netnsid = nla_read_s32(tb[IFLA_LINK_NETNSID]);
}
if (tb[IFLA_MTU])
dest->mtu = nla_read_u32(tb[IFLA_MTU]);
if (tb[IFLA_LINKINFO]) {
linkinfo = nla_nested_attrs(tb[IFLA_LINKINFO], IFLA_INFO_MAX);
if (!linkinfo) {
err = ENOMEM;
goto err_ifname;
}
}
if (tb[IFLA_ADDRESS]) {
err = mac_addr_fill_netlink(&dest->mac_addr, tb[IFLA_ADDRESS]);
if (err)
goto err_ifname;
}
if (ifi->ifi_flags & IFF_LOOPBACK) {
dest->driver = strdup("loopback");
dest->flags |= IF_LOOPBACK;
} else
dest->driver = ethtool_driver(dest->if_name);
if (!dest->driver) {
/* No ethtool ops available, try IFLA_INFO_KIND */
if (tb[IFLA_LINKINFO] && linkinfo && linkinfo[IFLA_INFO_KIND])
dest->driver = strdup(RTA_DATA(linkinfo[IFLA_INFO_KIND]));
}
if (!dest->driver) {
/* Allow the program to continue at least with generic stuff
* as there may be interfaces that do not implement any of
* the mechanisms for driver detection that we use */
dest->driver = strdup("unknown driver, please report a bug");
}
if ((err = if_handler_init(dest)))
goto err_driver;
if ((err = if_handler_netlink(dest, linkinfo)))
if (err != ENOENT)
goto err_driver;
err = 0;
goto out;
err_driver:
free(dest->driver);
dest->driver = NULL;
err_ifname:
free(dest->if_name);
dest->if_name = NULL;
out:
free(tb);
free(linkinfo);
return err;
}
static int fill_if_addr(struct if_entry *dest, struct nlmsg *alist)
{
struct if_addr *entry;
struct ifaddrmsg *ifa;
struct nlattr **rta_tb;
int err;
for_each_nlmsg(ainfo, alist) {
rta_tb = NULL;
err = 0;
ifa = nlmsg_get(ainfo, sizeof(*ifa));
if (!ifa)
return ENOENT;
if (ifa->ifa_index != dest->if_index)
goto skip;
if (nlmsg_get_hdr(ainfo)->nlmsg_type != RTM_NEWADDR)
goto skip;
if (ifa->ifa_family != AF_INET &&
ifa->ifa_family != AF_INET6)
/* only IP addresses supported (at least for now) */
goto skip;
rta_tb = nlmsg_attrs(ainfo, IFA_MAX);
if (!rta_tb) {
err = ENOMEM;
goto skip;
}
if (!rta_tb[IFA_LOCAL] && !rta_tb[IFA_ADDRESS])
/* don't care about broadcast and anycast adresses */
goto skip;
entry = calloc(1, sizeof(struct if_addr));
if (!entry) {
err = ENOMEM;
goto skip;
}
list_append(&dest->addr, node(entry));
if (!rta_tb[IFA_LOCAL]) {
rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
rta_tb[IFA_ADDRESS] = NULL;
}
if ((err = addr_init_netlink(&entry->addr, ifa, rta_tb[IFA_LOCAL])))
goto skip;
if (rta_tb[IFA_ADDRESS] &&
memcmp(nla_read(rta_tb[IFA_ADDRESS]), nla_read(rta_tb[IFA_LOCAL]),
ifa->ifa_family == AF_INET ? 4 : 16)) {
if ((err = addr_init_netlink(&entry->peer, ifa, rta_tb[IFA_ADDRESS])))
goto skip;
}
skip:
nlmsg_unget(ainfo, sizeof(*ifa));
if (rta_tb)
free(rta_tb);
if (err)
return err;
}
return 0;
}
struct if_entry *if_create(void)
{
struct if_entry *entry;
entry = calloc(1, sizeof(struct if_entry));
if (!entry)
return NULL;
list_init(&entry->addr);
list_init(&entry->rev_master);
list_init(&entry->rev_link);
list_init(&entry->properties);
entry->link_netnsid = -1;
entry->peer_netnsid = -1;
mac_addr_init(&entry->mac_addr);
return entry;
}
int if_list(struct list *result, struct netns_entry *ns)
{
struct nl_handle hnd;
struct nlmsg *linfo, *ainfo;
struct if_entry *entry;
int err;
list_init(result);
if ((err = rtnl_open(&hnd)))
return err;
err = rtnl_ifi_dump(&hnd, RTM_GETLINK, AF_UNSPEC, &linfo);
if (err)
goto out_close;
err = rtnl_ifi_dump(&hnd, RTM_GETADDR, AF_UNSPEC, &ainfo);
if (err)
goto out_linfo;
for_each_nlmsg(l, linfo) {
entry = if_create();
if (!entry) {
err = ENOMEM;
goto out_ainfo;
}
list_append(result, node(entry));
entry->ns = ns;
if ((err = fill_if_link(entry, l)))
goto out_ainfo;
if ((err = fill_if_addr(entry, ainfo)))
goto out_ainfo;
if ((err = if_handler_scan(entry)))
goto out_ainfo;
}
err = 0;
out_ainfo:
nlmsg_free(ainfo);
out_linfo:
nlmsg_free(linfo);
out_close:
nl_close(&hnd);
return err;
}
static void if_addr_destruct(struct if_addr *entry)
{
addr_destruct(&entry->addr);
addr_destruct(&entry->peer);
}
static void if_list_destruct(struct if_entry *entry)
{
if_handler_cleanup(entry);
free(entry->internal_ns);
free(entry->if_name);
free(entry->edge_label);
mac_addr_destruct(&entry->mac_addr);
label_free_property(&entry->properties);
list_free(&entry->addr, (destruct_f) if_addr_destruct);
}
void if_list_free(struct list *list)
{
list_free(list, (destruct_f) if_list_destruct);
}
int if_add_warning(struct if_entry *entry, char *fmt, ...)
{
va_list ap;
char *warn;
int err = ENOMEM;
va_start(ap, fmt);
entry->warnings++;
if (vasprintf(&warn, fmt, ap) < 0)
goto out;
err = label_add(&entry->ns->warnings, "%s: %s", ifstr(entry), warn);
free(warn);
out:
va_end(ap);
return err;
}