summaryrefslogtreecommitdiffstats
path: root/isys/iface.c
blob: c1251be1a3c9f3c42eccc3f280885e07dc86a280 (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * iface.c - Network interface configuration API
 *
 * Copyright (C) 2006, 2007, 2008  Red Hat, Inc.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): David Cantrell <dcantrell@redhat.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <netinet/in.h>

#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>

#include <glib.h>
#include <NetworkManager.h>
#include <nm-client.h>
#include <nm-device.h>
#include <nm-ip4-config.h>
#include <nm-setting-ip4-config.h>

#include "isys.h"
#include "iface.h"
#include "str.h"

/* Internal-only function prototypes. */
static struct nl_handle *_iface_get_handle(void);
static struct nl_cache *_iface_get_link_cache(struct nl_handle **);
static int _iface_have_valid_addr(void *addr, int family, int length);
static int _iface_redirect_io(char *device, int fd, int mode);

/*
 * Return a libnl handle for NETLINK_ROUTE.
 */
static struct nl_handle *_iface_get_handle(void) {
    struct nl_handle *handle = NULL;

    if ((handle = nl_handle_alloc()) == NULL) {
        return NULL;
    }

    if (nl_connect(handle, NETLINK_ROUTE)) {
        nl_handle_destroy(handle);
        return NULL;
    }

    return handle;
}

/*
 * Return an NETLINK_ROUTE cache.
 */
static struct nl_cache *_iface_get_link_cache(struct nl_handle **handle) {
    struct nl_cache *cache = NULL;

    if ((*handle = _iface_get_handle()) == NULL) {
        return NULL;
    }

    if ((cache = rtnl_link_alloc_cache(*handle)) == NULL) {
        nl_close(*handle);
        nl_handle_destroy(*handle);
        return NULL;
    }

    return cache;
}

/*
 * Determine if a struct in_addr or struct in6_addr contains a valid address.
 */
static int _iface_have_valid_addr(void *addr, int family, int length) {
    char buf[length+1];

    if ((addr == NULL) || (family != AF_INET && family != AF_INET6)) {
        return 0;
    }

    memset(buf, '\0', sizeof(buf));

    if (inet_ntop(family, addr, buf, length) == NULL) {
        return 0;
    } else {
        /* check for unknown addresses */
        if (family == AF_INET) {
            if (!strncmp(buf, "0.0.0.0", 7)) {
                return 0;
            }
        } else if (family == AF_INET6) {
            if (!strncmp(buf, "::", 2)) {
                return 0;
            }
        }
    }

    return 1;
}

/*
 * Redirect I/O to another device (e.g., stdout to /dev/tty5)
 */
int _iface_redirect_io(char *device, int fd, int mode) {
    int io = -1;

    if ((io = open(device, mode)) == -1) {
        return 1;
    }

    if (close(fd) == -1) {
        return 2;
    }

    if (dup2(io, fd) == -1) {
        return 3;
    }

    if (close(io) == -1) {
        return 4;
    }

    return 0;
}

/*
 * Given an interface name (e.g., eth0) and address family (e.g., AF_INET),
 * return the IP address in human readable format (i.e., the output from
 * inet_ntop()).  Return NULL for no match or error.
 */
char *iface_ip2str(char *ifname, int family) {
    int i;
    NMClient *client = NULL;
    NMIP4Config *ip4config = NULL;
    NMIP4Address *ipaddr = NULL;
    NMDevice *candidate = NULL;
    struct in_addr tmp_addr;
    const GPtrArray *devices;
    const char *iface;
    char ipstr[INET_ADDRSTRLEN+1];

    if (ifname == NULL) {
        return NULL;
    }

    /* DCFIXME: add IPv6 once NM gains support */
    if (family != AF_INET) {
        return NULL;
    }

    g_type_init();

    client = nm_client_new();
    if (!client) {
        return NULL;
    }

    if (nm_client_get_state(client) != NM_STATE_CONNECTED) {
        g_object_unref(client);
        return NULL;
    }

    devices = nm_client_get_devices(client);
    for (i=0; i < devices->len; i++) {
        candidate = g_ptr_array_index(devices, i);
        iface = nm_device_get_iface(candidate);

        if (nm_device_get_state(candidate) != NM_DEVICE_STATE_ACTIVATED)
            continue;

        if (!iface || strcmp(iface, ifname))
            continue;

        if (!(ip4config = nm_device_get_ip4_config(candidate)))
            continue;

        if (!(ipaddr = nm_ip4_config_get_addresses(ip4config)->data))
            continue;

        memset(&ipstr, '\0', sizeof(ipstr));
        tmp_addr.s_addr = nm_ip4_address_get_address(ipaddr);

        if (inet_ntop(AF_INET, &tmp_addr, ipstr, INET_ADDRSTRLEN) == NULL) {
            g_object_unref(client);
            return NULL;
        }

        g_object_unref(client);
        return g_strdup(ipstr);
    }

    g_object_unref(client);
    return NULL;
}

/* Given an interface's MAC address, return the name (e.g., eth0) in human
 * readable format.  Return NULL for no match
 */
char *iface_mac2device(char *mac) {
    struct nl_handle *handle = NULL;
    struct nl_cache *cache = NULL;
    struct rtnl_link *link = NULL;
    struct nl_addr *mac_as_nl_addr = NULL;
    char *retval = NULL;
    int i, n;

    if (mac == NULL) {
        return NULL;
    }

    if ((mac_as_nl_addr = nl_addr_parse(mac, AF_LLC)) == NULL) {
        return NULL;
    }

    if ((cache = _iface_get_link_cache(&handle)) == NULL) {
        return NULL;
    }

    n = nl_cache_nitems(cache);
    for (i = 0; i <= n; i++) {
        struct nl_addr *addr;

        if ((link = rtnl_link_get(cache, i)) == NULL) {
            continue;
        }

        addr = rtnl_link_get_addr(link);

        if (!nl_addr_cmp(mac_as_nl_addr, addr)) {
            retval = strdup(rtnl_link_get_name(link));
            rtnl_link_put(link);
            break;
        }

        rtnl_link_put(link);
    }

    nl_close(handle);
    nl_handle_destroy(handle);

    return retval;
}

/*
 * Given an interface name (e.g., eth0), return the MAC address in human
 * readable format (e.g., 00:11:52:12:D9:A0).  Return NULL for no match.
 */
char *iface_mac2str(char *ifname) {
    int buflen = 20;
    char *buf = NULL;
    struct nl_handle *handle = NULL;
    struct nl_cache *cache = NULL;
    struct rtnl_link *link = NULL;
    struct nl_addr *addr = NULL;

    if (ifname == NULL) {
        return NULL;
    }

    if ((cache = _iface_get_link_cache(&handle)) == NULL) {
        return NULL;
    }

    if ((link = rtnl_link_get_by_name(cache, ifname)) == NULL) {
        goto mac2str_error2;
    }

    if ((addr = rtnl_link_get_addr(link)) == NULL) {
        goto mac2str_error3;
    }

    if ((buf = calloc(sizeof(char *), buflen)) == NULL) {
        goto mac2str_error4;
    }

    if ((buf = nl_addr2str(addr, buf, buflen)) != NULL) {
        buf = str2upper(buf);
    }

mac2str_error4:
    nl_addr_destroy(addr);
mac2str_error3:
    rtnl_link_put(link);
mac2str_error2:
    nl_close(handle);
    nl_handle_destroy(handle);

    return buf;
}

/*
 * Convert an IPv4 CIDR prefix to a dotted-quad netmask.  Return NULL on
 * failure.
 */
struct in_addr *iface_prefix2netmask(int prefix) {
    int mask = 0;
    char *buf = NULL;
    struct in_addr *ret;

    if ((buf = calloc(sizeof(char *), INET_ADDRSTRLEN + 1)) == NULL) {
        return NULL;
    }

    mask = htonl(~((1 << (32 - prefix)) - 1));

    if (inet_ntop(AF_INET, (struct in_addr *) &mask, buf,
                  INET_ADDRSTRLEN) == NULL) {
        return NULL;
    }

    if ((ret = calloc(sizeof(struct in_addr), 1)) == NULL) {
        return NULL;
    }

    memcpy(ret, (struct in_addr *) &mask, sizeof(struct in_addr));
    return ret;
}

/*
 * Initialize a new iface_t structure to default values.
 */
void iface_init_iface_t(iface_t *iface) {
    int i;

    memset(&iface->device, '\0', sizeof(iface->device));
    memset(&iface->ipaddr, 0, sizeof(iface->ipaddr));
    memset(&iface->netmask, 0, sizeof(iface->netmask));
    memset(&iface->broadcast, 0, sizeof(iface->broadcast));
    memset(&iface->ip6addr, 0, sizeof(iface->ip6addr));
    memset(&iface->gateway, 0, sizeof(iface->gateway));
    memset(&iface->gateway6, 0, sizeof(iface->gateway6));

    for (i = 0; i < MAXNS; i++) {
        iface->dns[i] = NULL;
    }

    iface->macaddr = NULL;
    iface->ip6prefix = 0;
    iface->nextserver = NULL;
    iface->bootfile = NULL;
    iface->numdns = 0;
    iface->hostname = NULL;
    iface->domain = NULL;
    iface->search = NULL;
    iface->dhcptimeout = 0;
    iface->vendorclass = NULL;
    iface->ssid = NULL;
    iface->wepkey = NULL;
    iface->mtu = 0;
    iface->subchannels = NULL;
    iface->portname = NULL;
    iface->peerid = NULL;
    iface->nettype = NULL;
    iface->ctcprot = NULL;
    iface->layer2 = NULL;
    iface->portno = NULL;
    iface->flags = 0;
    iface->ipv4method = IPV4_UNUSED_METHOD;
    iface->ipv6method = IPV6_UNUSED_METHOD;

    return;
}

/*
 * Given a pointer to a struct in_addr, return 1 if it contains a valid
 * address, 0 otherwise.
 */
int iface_have_in_addr(struct in_addr *addr) {
    return _iface_have_valid_addr(addr, AF_INET, INET_ADDRSTRLEN);
}

/*
 * Given a pointer to a struct in6_addr, return 1 if it contains a valid
 * address, 0 otherwise.
 */
int iface_have_in6_addr(struct in6_addr *addr6) {
    return _iface_have_valid_addr(addr6, AF_INET6, INET6_ADDRSTRLEN);
}

/* Check if NM has an active connection */
gboolean is_nm_connected(void) {
    NMState state;
    NMClient *client = NULL;

    g_type_init();

    client = nm_client_new();
    if (!client)
        return FALSE;

    state = nm_client_get_state(client);
    g_object_unref(client);

    if (state == NM_STATE_CONNECTED)
        return TRUE;
    else
        return FALSE;
}

/* Check if NM is already running */
gboolean is_nm_running(void) {
    gboolean running;
    NMClient *client = NULL;

    g_type_init();

    client = nm_client_new();
    if (!client)
        return FALSE;

    running = nm_client_get_manager_running(client);
    g_object_unref(client);
    return running;
}

/*
 * Wait for NetworkManager to appear on the system bus
 */
int wait_for_nm(void) {
    int count = 0;

    /* send message and block until a reply or error comes back */
    while (count < 45) {
        if (is_nm_running())
            return 0;

        sleep(1);
        count++;
    }

    return 1;
}

/*
 * Start NetworkManager -- requires that you have already written out the
 * control files in /etc/sysconfig for the interface.
 */
int iface_start_NetworkManager(void) {
    pid_t pid;

    if (is_nm_running())
        return 0;  /* already running */

    /* Start NetworkManager */
    pid = fork();
    if (pid == 0) {
        if (setpgrp() == -1) {
            exit(1);
        }

        if (_iface_redirect_io("/dev/null", STDIN_FILENO, O_RDONLY) ||
            _iface_redirect_io(OUTPUT_TERMINAL, STDOUT_FILENO, O_WRONLY) ||
            _iface_redirect_io(OUTPUT_TERMINAL, STDERR_FILENO, O_WRONLY)) {
            exit(2);
        }

        if (execl(NETWORKMANAGER, NETWORKMANAGER,
                  "--pid-file=/var/run/NetworkManager/NetworkManager.pid",
                  NULL) == -1) {
            exit(3);
        }
    } else if (pid == -1) {
        return 1;
    } else {
        return wait_for_nm();
    }

    return 0;
}

/*
 * Set the MTU on the specified device.
 */
int iface_set_interface_mtu(char *ifname, int mtu) {
    int ret = 0;
    struct nl_handle *handle = NULL;
    struct nl_cache *cache = NULL;
    struct rtnl_link *link = NULL;
    struct rtnl_link *request = NULL;

    if (ifname == NULL) {
        return -1;
    }

    if (mtu <= 0) {
        return -2;
    }

    if ((cache = _iface_get_link_cache(&handle)) == NULL) {
        return -3;
    }

    if ((link = rtnl_link_get_by_name(cache, ifname)) == NULL) {
        ret = -4;
        goto ifacemtu_error1;
    }

    request = rtnl_link_alloc();
    rtnl_link_set_mtu(request, mtu);

    if (rtnl_link_change(handle, link, request, 0)) {
        ret = -5;
        goto ifacemtu_error2;
    }

ifacemtu_error2:
    rtnl_link_put(link);
ifacemtu_error1:
    nl_close(handle);
    nl_handle_destroy(handle);

    return ret;
}