summaryrefslogtreecommitdiff
path: root/src/nm-netlink.c
blob: ad68790db1354a8c4b67e81a935b7d0292a0ac81 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2007 - 2008 Red Hat, Inc.
 */

#include "config.h"

#include "nm-netlink.h"
#include "nm-utils.h"

#include <glib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <netlink/object-api.h>

static struct nl_cache * link_cache = NULL;
static struct nl_handle * def_nl_handle = NULL;


static struct nl_cache *
get_link_cache (void)
{
	struct nl_handle * nlh;

	nlh = nm_netlink_get_default_handle ();
	if (G_UNLIKELY (!nlh)) {
		nm_warning ("couldn't allocate netlink handle.");
		return NULL;
	}

	if (G_UNLIKELY (!link_cache))
		link_cache = rtnl_link_alloc_cache (nlh);

	if (G_UNLIKELY (!link_cache)) {
		nm_warning ("couldn't allocate netlink link cache: %s", nl_geterror ());
		return NULL;
	}

	nl_cache_update (nlh, link_cache);
	return link_cache;
}


struct nl_handle *
nm_netlink_get_default_handle (void)
{
	struct nl_cb *cb;
#ifdef LIBNL_NEEDS_ADDR_CACHING_WORKAROUND
	struct nl_cache *addr_cache;
#endif

	if (def_nl_handle)
		return def_nl_handle;

	cb = nl_cb_alloc(NL_CB_VERBOSE);
	def_nl_handle = nl_handle_alloc_cb (cb);
	if (!def_nl_handle) {
		nm_warning ("couldn't allocate netlink handle.");
		return NULL;
	}

	if (nl_connect (def_nl_handle, NETLINK_ROUTE) < 0) {
		nm_error ("couldn't connect to netlink: %s", nl_geterror ());
		return NULL;
	}

#ifdef LIBNL_NEEDS_ADDR_CACHING_WORKAROUND
	/* Work around apparent libnl bug; rtnl_addr requires that all
	 * addresses have the "peer" attribute set in order to be compared
	 * for equality, but this attribute is not normally set. As a
	 * result, most addresses will not compare as equal even to
	 * themselves, busting caching.
	 */
	addr_cache = rtnl_addr_alloc_cache (def_nl_handle);
	nl_cache_get_ops (addr_cache)->co_obj_ops->oo_id_attrs &= ~0x80;
	nl_cache_free (addr_cache);
#endif

	return def_nl_handle;
}

int
nm_netlink_iface_to_index (const char *iface)
{
	struct nl_cache * cache;

	g_return_val_if_fail (iface != NULL, -1);

	cache = get_link_cache ();
	if (!cache)
		return RTNL_LINK_NOT_FOUND;

	return rtnl_link_name2i (cache, iface);
}


#define MAX_IFACE_LEN	33
char *
nm_netlink_index_to_iface (int idx)
{
	struct nl_cache * cache;
	char * buf = NULL;

	cache = get_link_cache ();
	if (!cache)
		return NULL;

	buf = g_malloc0 (MAX_IFACE_LEN);
	if (buf == NULL) {
		nm_warning ("Not enough memory to allocate interface name buffer.");
		return NULL;
	}

	if (rtnl_link_i2name (cache, idx, buf, MAX_IFACE_LEN - 1) == NULL) {
		g_free (buf);
		buf = NULL;
	}

	return buf;
}

struct rtnl_link *
nm_netlink_index_to_rtnl_link (int idx)
{
	struct nl_cache *cache;

	cache = get_link_cache ();
	if (!cache)
		return NULL;

	return rtnl_link_get (cache, idx);
}