2 * Network availability and change notifications for linux.
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
7 * Copyright (c) Novell, Inc. 2011
16 #if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
28 #define NL_DEBUG_STMT(a) do { } while (0)
29 #define NL_DEBUG_PRINT(...)
33 #define NL_DEBUG_STMT(a) do { a } while (0)
34 #define NL_DEBUG_PRINT(...) g_message(__VA_ARGS__)
39 #define ADDR_BYTE_LENGTH 16
40 #define ADDR_STR_LENGTH INET6_ADDRSTRLEN
43 #define ADDR_STR_LENGTH INET_ADDRSTRLEN
49 EVT_AVAILABILITY
= 1 << 0,
50 #define EVT_AVAILABILITY (1 << 0)
52 #define EVT_ADDRESS (1 << 1)
53 EVT_ALL
= EVT_AVAILABILITY
| EVT_ADDRESS
54 #define EVT_ALL (EVT_AVAILABILITY | EVT_ADDRESS)
63 #define INIT(x) { x, #x }
64 #define FIND_NAME(a, b) value_to_name (a, b)
66 #define FIND_RT_TYPE_NAME(b) FIND_NAME (rt_types, b)
67 static value2name_t rt_types
[] = {
83 #define FIND_RTM_TYPE_NAME(b) FIND_NAME (rtm_types, b)
84 static value2name_t rtm_types
[] = {
92 INIT (RTN_UNREACHABLE
),
100 #define FIND_RTM_PROTO_NAME(b) FIND_NAME (rtm_protocols, b)
101 static value2name_t rtm_protocols
[] = {
102 INIT (RTPROT_UNSPEC
),
103 INIT (RTPROT_REDIRECT
),
104 INIT (RTPROT_KERNEL
),
106 INIT (RTPROT_STATIC
),
110 #define FIND_RTM_SCOPE_NAME(b) FIND_NAME (rtm_scopes, b)
111 static value2name_t rtm_scopes
[] = {
112 INIT (RT_SCOPE_UNIVERSE
),
113 INIT (RT_SCOPE_SITE
),
114 INIT (RT_SCOPE_LINK
),
115 INIT (RT_SCOPE_HOST
),
116 INIT (RT_SCOPE_NOWHERE
),
120 #define FIND_RTM_ATTRS_NAME(b) FIND_NAME (rtm_attrs, b)
121 static value2name_t rtm_attrs
[] = {
131 INIT (RTA_MULTIPATH
),
132 INIT (RTA_PROTOINFO
),
134 INIT (RTA_CACHEINFO
),
141 #define FIND_RT_TABLE_NAME(b) FIND_NAME (rtm_tables, b)
142 static value2name_t rtm_tables
[] = {
143 INIT (RT_TABLE_UNSPEC
),
144 INIT (RT_TABLE_COMPAT
),
145 INIT (RT_TABLE_DEFAULT
),
146 INIT (RT_TABLE_MAIN
),
147 INIT (RT_TABLE_LOCAL
),
152 value_to_name (value2name_t
*tbl
, int value
)
154 static char auto_name
[16];
157 if (tbl
->value
== value
)
161 snprintf (auto_name
, sizeof (auto_name
), "#%d", value
);
164 #endif /* NL_DEBUG */
167 CreateNLSocket (void)
170 struct sockaddr_nl sa
;
173 sock
= socket (AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
175 ret
= fcntl (sock
, F_GETFL
, 0);
178 ret
= fcntl (sock
, F_SETFL
, ret
);
183 memset (&sa
, 0, sizeof (sa
));
186 sa
.nl_family
= AF_NETLINK
;
187 sa
.nl_pid
= getpid ();
188 sa
.nl_groups
= RTMGRP_IPV4_ROUTE
| RTMGRP_IPV6_ROUTE
| RTMGRP_NOTIFY
;
189 /* RTNLGRP_IPV4_IFADDR | RTNLGRP_IPV6_IFADDR
192 if (bind (sock
, (struct sockaddr
*) &sa
, sizeof (sa
)) < 0)
199 ReadEvents (gpointer sock
, gpointer buffer
, gint32 count
, gint32 size
)
201 struct nlmsghdr
*nlp
;
208 NL_DEBUG_PRINT ("ENTER ReadEvents()");
210 s
= GPOINTER_TO_INT (sock
);
211 /* This socket is not found by IO layer, so we do everything here */
213 while ((count
= recv (s
, buffer
, size
, 0)) == -1 && errno
== EINTR
);
215 NL_DEBUG_PRINT ("EXIT ReadEvents()");
219 for (nlp
= (struct nlmsghdr
*) buffer
; NLMSG_OK (nlp
, count
); nlp
= NLMSG_NEXT (nlp
, count
)) {
231 gboolean have_pref_src
;
233 char dst
[ADDR_BYTE_LENGTH
];
234 char src
[ADDR_BYTE_LENGTH
];
235 char pref_src
[ADDR_BYTE_LENGTH
];
236 char gw
[ADDR_BYTE_LENGTH
];
238 msg_type
= nlp
->nlmsg_type
;
239 NL_DEBUG_PRINT ("TYPE: %d %s", msg_type
, FIND_RT_TYPE_NAME (msg_type
));
240 if (msg_type
!= RTM_NEWROUTE
&& msg_type
!= RTM_DELROUTE
)
243 rtp
= (struct rtmsg
*) NLMSG_DATA (nlp
);
244 family
= rtp
->rtm_family
;
246 if (family
!= AF_INET
&& family
!= AF_INET6
) {
248 if (family
!= AF_INET
) {
253 addr_length
= (family
== AF_INET
) ? 4 : 16;
254 table
= rtp
->rtm_table
;
256 protocol
= rtp
->rtm_protocol
;
257 scope
= rtp
->rtm_scope
;
259 rtm_type
= rtp
->rtm_type
;
260 NL_DEBUG_PRINT ("\tRTMSG table: %d %s", table
, FIND_RT_TABLE_NAME (table
));
261 if (table
!= RT_TABLE_MAIN
&& table
!= RT_TABLE_LOCAL
)
264 NL_DEBUG_PRINT ("\tRTMSG protocol: %d %s", protocol
, FIND_RTM_PROTO_NAME (protocol
));
265 NL_DEBUG_PRINT ("\tRTMSG scope: %d %s", scope
, FIND_RTM_SCOPE_NAME (scope
));
266 NL_DEBUG_PRINT ("\tRTMSG type: %d %s", rtm_type
, FIND_RTM_TYPE_NAME (rtm_type
));
268 rtap
= (struct rtattr
*) RTM_RTA (rtp
);
269 rtl
= RTM_PAYLOAD (nlp
);
270 // loop & get every attribute
274 // table = RT_TABLE_LOCAL, Scope = HOST + pref.src == src + type=LOCAL -> new if addr
275 // RT_TABLE_MAIN, Scope = Universe, unicast, gateway exists -> NEW default route
277 // table = RT_TABLE_LOCAL, Scope = HOST, perfsrc = dst + type=LOCAL -> if addr deleted
278 // RT_TABLE_MAIN - DELROUTE + unicast -> event (gw down?)
279 have_dst
= have_src
= have_pref_src
= have_gw
= FALSE
;
280 for(; RTA_OK (rtap
, rtl
); rtap
= RTA_NEXT(rtap
, rtl
)) {
283 char ip
[ADDR_STR_LENGTH
];
286 NL_DEBUG_PRINT ("\tAttribute: %d %d (%s)", rtap
->rta_len
, rtap
->rta_type
, FIND_RTM_ATTRS_NAME (rtap
->rta_type
));
287 data
= RTA_DATA (rtap
);
288 switch (rtap
->rta_type
) {
291 memcpy (dst
, data
, addr_length
);
294 inet_ntop (family
, RTA_DATA (rtap
), ip
, sizeof (ip
));
295 NL_DEBUG_PRINT ("\t\tDst: %s", ip
);
299 have_pref_src
= TRUE
;
300 memcpy (pref_src
, data
, addr_length
);
303 inet_ntop (family
, RTA_DATA (rtap
), ip
, sizeof (ip
));
304 NL_DEBUG_PRINT ("\t\tPref. Src.: %s", ip
);
309 memcpy (src
, data
, addr_length
);
312 inet_ntop (family
, RTA_DATA (rtap
), ip
, sizeof (ip
));
313 NL_DEBUG_PRINT ("\tSrc: %s", ip
);
318 memcpy (gw
, data
, addr_length
);
321 inet_ntop (family
, RTA_DATA (rtap
), ip
, sizeof (ip
));
322 NL_DEBUG_PRINT ("\t\tGateway: %s", ip
);
329 if (msg_type
== RTM_NEWROUTE
) {
330 if (table
== RT_TABLE_MAIN
) {
331 NL_DEBUG_PRINT ("NEWROUTE: Availability changed");
332 result
|= EVT_AVAILABILITY
;
333 } else if (table
== RT_TABLE_LOCAL
) {
334 NL_DEBUG_PRINT ("NEWROUTE: new IP");
335 if (have_dst
&& have_pref_src
&& memcmp (dst
, pref_src
, addr_length
) == 0)
336 result
|= EVT_ADDRESS
;
338 } else if (msg_type
== RTM_DELROUTE
) {
339 if (table
== RT_TABLE_MAIN
) {
340 if (rtm_type
== RTN_UNICAST
&& (have_dst
|| have_pref_src
)) {
341 result
|= EVT_AVAILABILITY
;
342 NL_DEBUG_PRINT ("DELROUTE: Availability changed");
344 } else if (table
== RT_TABLE_LOCAL
) {
345 if (have_dst
&& have_pref_src
&& memcmp (dst
, pref_src
, addr_length
) == 0) {
346 result
|= EVT_ADDRESS
;
347 NL_DEBUG_PRINT ("DELROUTE: deleted IP");
351 while ((count
= recv (s
, buffer
, size
, 0)) == -1 && errno
== EINTR
);
353 NL_DEBUG_PRINT ("EXIT ReadEvents() -> %d", result
);
356 nlp
= (struct nlmsghdr
*) buffer
;
358 NL_DEBUG_PRINT ("EXIT ReadEvents() -> %d", result
);
363 CloseNLSocket (gpointer sock
)
365 return close (GPOINTER_TO_INT (sock
));
375 ReadEvents (gpointer sock
, gpointer buffer
, gint32 count
, gint32 size
)
381 CreateNLSocket (void)
387 CloseNLSocket (gpointer sock
)
391 #endif /* linux/netlink.h + linux/rtnetlink.h */