-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRouting.pyx
464 lines (353 loc) · 14 KB
/
Routing.pyx
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
# This software was developed by employees of the National Institute of
# Standards and Technology (NIST), and others.
# This software has been contributed to the public domain.
# Pursuant to title 15 Untied States Code Section 105, works of NIST
# employees are not subject to copyright protection in the United States
# and are considered to be in the public domain.
# As a result, a formal license is not needed to use this software.
#
# This software is provided "AS IS."
# NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
# OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
# AND DATA ACCURACY. NIST does not warrant or make any representations
# regarding the use of the software or the results thereof, including but
# not limited to the correctness, accuracy, reliability or usefulness of
# this software.
# Tony Cheneau <tony.cheneau@nist.gov>
from NLTypes cimport nl_sock
from NLUtils cimport *
from libc.stdio cimport stdout
from libc.stdlib cimport free
from libc.string cimport memset
# TODO:
# - add table support
# - fix bug for removing default route
# constant values
NETLINK_ROUTE = 0
NL_DUMP_LINE = 0
cdef void delete_route_cb(nl_object * obj, void * arg):
cdef rtnl_route * route = <rtnl_route *> obj
cdef nl_sock * sock = <nl_sock *> arg
cdef int err = 1
err = rtnl_route_delete(sock, route, 0)
if (err < 0): raise Exception(nl_geterror(err))
cdef void delete_addr_cb(nl_object * obj, void * arg):
cdef rtnl_addr * addr = <rtnl_addr *> obj
cdef nl_sock * sock = <nl_sock *> arg
cdef int err = 1
err = rtnl_addr_delete(sock, addr, 0)
if (err < 0): raise Exception(nl_geterror(err))
def need_clean_route_obj(method_to_decorate):
def wrapper(self, * args, **kwargs):
self.__alloc_route()
method_to_decorate(self, *args, **kwargs)
return wrapper
def need_clean_addr_obj(method_to_decorate):
def wrapper(self, * args, **kwargs):
self.__alloc_addr()
method_to_decorate(self, *args, **kwargs)
return wrapper
def sync_cache(method_to_decorate):
def wrapper(self, * args, **kwargs):
self.__resync_caches()
ret = method_to_decorate(self, *args, **kwargs)
self.__resync_caches()
return ret
return wrapper
cdef class Routing:
"""Routing provides access to the (current) routing cache.
Example of usage:
>>> r = Routing()
>>> print r
inet 127.0.0.0 table local type broadcast via dev lo
inet 127.0.0.1 table local type local via dev lo
inet 127.0.0.0/8 table local type local via dev lo
>>> r.set_family("inet6") # work with IPv6 addresses
>>>
"""
cdef nl_sock * sock
cdef nl_cache * link_cache
cdef nl_cache * route_cache
cdef rtnl_route * __route
cdef nl_dump_params params
cdef str family
cdef char buff[2048]
def __cinit__(self):
# self.params.dp_fd = stdout
self.params.dp_buf = self.buff
self.params.dp_buflen = len(self.buff)
self.params.dp_type = NL_DUMP_LINE
self.sock = nl_socket_alloc()
if self.sock is NULL:
raise MemoryError()
err = nl_connect(self.sock, NETLINK_ROUTE)
if err < 0:
raise "Unable to connect netlink socket: %s" % nl_geterror(err)
self.link_cache = nl_cli_link_alloc_cache(self.sock)
self.route_cache = nl_cli_route_alloc_cache(self.sock, 0)
if self.route_cache is NULL:
raise MemoryError()
def __alloc_route(self):
if self.__route is not NULL:
rtnl_route_put(self.__route)
self.__route = nl_cli_route_alloc()
if self.__route is NULL:
raise MemoryError()
def __resync_caches(self):
"""decorator: synchronize/refill the link and the routing caches"""
nl_cache_refill(self.sock, self.link_cache)
nl_cache_refill(self.sock, self.route_cache)
def set_family(self, family):
"""set the family for the displayed routes (inet, inet6)
"""
if family is None:
self.family = None
else:
self.family = family
def __set_family(self, arg):
nl_cli_route_parse_family(self.__route, arg)
def __repr__(self):
self.__alloc_route()
self.__resync_caches()
if self.family:
self.__set_family(self.family)
memset(self.buff, 0, len(self.buff))
nl_cache_dump_filter(self.route_cache,
&self.params,
<nl_object *>self.__route)
return self.buff
@need_clean_route_obj
@sync_cache
def add(self, destination=None, nexthop=None, source=None, table=None):
"""
- nexthop is a tuple of address and interface.
It can also be a list of such tuples.
Example of usage:
>>> r = Routing()
>>> r.add("2000::/3", ("fe80::a2","eth0"))
Adding a default route (require the address family to be set):
>>> r = Routing.Routing()
>>> r.set_family("inet6")
>>> r.add("default", ("fe80::a2", "em1"))
"""
err = 1
# ./nl-route-add --family=inet6 -d 2000::/3 -n via=fe80::a2,dev=eth0
NH_ARGS = ["via", "dev"]
if self.family:
self.__set_family(self.family)
if destination:
nl_cli_route_parse_dst(self.__route, destination)
if source:
nl_cli_route_parse_src(self.__route, source)
if table:
nl_cli_route_parse_table(self.__route, table)
if isinstance(nexthop, list):
for hop in nexthop:
hop = ",".join([ "=".join(arg) for arg in zip(NH_ARGS, hop) ])
nl_cli_route_parse_nexthop(self.__route, hop, self.link_cache)
elif nexthop:
nh = ",".join([ "=".join(arg) for arg in zip(NH_ARGS, nexthop) ])
nl_cli_route_parse_nexthop(self.__route, nh, self.link_cache)
err = rtnl_route_add(self.sock, self.__route, 0)
if (err < 0): raise(Exception(nl_geterror(err)))
@need_clean_route_obj
@sync_cache
def remove(self, destination=None, nexthop=None, source=None, table=None):
"""Remove a route from the node
Caveat: it seems that default routes cannot be removed for the moment
Equivalent libnl CLI command:
# ./nl-route-delete -d 2000::/3 -n via=fe80::a2,dev=eth0
"""
NH_ARGS = ["via", "dev"]
if self.family:
self.__set_family(self.family)
if destination:
nl_cli_route_parse_dst(self.__route, destination)
if source:
nl_cli_route_parse_src(self.__route, source)
if table:
nl_cli_route_parse_table(self.__route, table)
if isinstance(nexthop, list):
for hop in nexthop:
hop = ",".join([ "=".join(arg) for arg in zip(NH_ARGS, hop) ])
nl_cli_route_parse_nexthop(self.__route, hop, self.link_cache)
elif nexthop:
nh = ",".join([ "=".join(arg) for arg in zip(NH_ARGS, nexthop) ])
nl_cli_route_parse_nexthop(self.__route, nh, self.link_cache)
nl_cache_foreach_filter(self.route_cache, <nl_object *> self.__route, &delete_route_cb, self.sock)
def __dealloc__(self):
if self.sock is not NULL:
nl_close(self.sock)
nl_socket_free(self.sock)
if self.__route is not NULL:
rtnl_route_put(self.__route)
if self.link_cache is not NULL:
nl_cache_free(self.link_cache)
if self.route_cache is not NULL:
nl_cache_free(self.route_cache)
cdef class Addressing:
"""Address provides access to address assigned on the node.
Example of usage:
>>> a = Addressing()
>>> print a
127.0.0.1/8 inet dev lo scope host <permanent>
192.168.1.1/24 inet dev eth0 scope global <permanent>
>>> a.set_family("inet6")
>>>
"""
cdef nl_sock * sock
cdef nl_cache * link_cache
cdef nl_cache * addr_cache
cdef rtnl_addr * __addr
cdef nl_dump_params params
cdef str family
cdef char buff[2048]
def __cinit__(self):
# self.params.dp_fd = stdout
self.params.dp_buf = self.buff
self.params.dp_buflen = len(self.buff)
self.params.dp_type = NL_DUMP_LINE
self.sock = nl_socket_alloc()
if self.sock is NULL:
raise MemoryError()
err = nl_connect(self.sock, NETLINK_ROUTE)
if err < 0:
raise "Unable to connect netlink socket: %s" % nl_geterror(err)
self.link_cache = nl_cli_link_alloc_cache(self.sock)
self.addr_cache = nl_cli_alloc_cache(self.sock, "address", rtnl_addr_alloc_cache)
if self.addr_cache is NULL:
raise MemoryError()
def __alloc_addr(self):
if self.__addr is not NULL:
rtnl_addr_put(self.__addr)
self.__addr = nl_cli_addr_alloc()
if self.__addr is NULL:
raise MemoryError()
def __resync_caches(self):
"""decorator: synchronize/refill the link and the routing caches"""
nl_cache_refill(self.sock, self.link_cache)
nl_cache_refill(self.sock, self.addr_cache)
def set_family(self, family):
"""set the family for the displayed routes (inet, inet6)
"""
if family is None:
self.family = None
else:
self.family = family
def __set_family(self, arg):
nl_cli_addr_parse_family(self.__addr, arg)
def __repr__(self):
self.__alloc_addr()
self.__resync_caches()
if self.family is not None:
self.__set_family(self.family)
memset(self.buff, 0, len(self.buff))
nl_cache_dump_filter(self.addr_cache,
&self.params,
<nl_object *>self.__addr)
return self.buff
@need_clean_addr_obj
@sync_cache
def add(self, address, interface, preferred=0, valid=0, replace=False):
"""Add an address to an interface.
- address: adress that should be added
- interface: interface on which the address should be assigned
- preferred: preferred lifetime, in milliseconds (IPv6 only)
- valid: valid lifetime, in milliseconds (IPv6 only)
- replace: update an existing address (can reset preferred and valid lifetime)
Example of usage:
>>> a = Addressing()
>>> a.add("2000::/3", "eth0")
>>> # set preferred and valid lifetime to 1000 seconds
>>> a.add("fe80::1/64", "em1", "1000000", "1000000")
>>> a.add("fe80::1/64", "em1", "2000000", "2000000", replace=True)
"""
cdef int nlflags = NLM_F_CREATE
err = 1
if preferred:
nl_cli_addr_parse_preferred(self.__addr, preferred)
if valid:
nl_cli_addr_parse_valid(self.__addr, valid)
if replace:
nlflags |= NLM_F_REPLACE
nl_cli_addr_parse_dev(self.__addr, self.link_cache, interface)
nl_cli_addr_parse_local(self.__addr, address)
err = rtnl_addr_add(self.sock, self.__addr, nlflags)
if (err < 0): raise(Exception(nl_geterror(err)))
@need_clean_addr_obj
@sync_cache
def remove(self, address, interface):
nl_cli_addr_parse_dev(self.__addr, self.link_cache, interface)
nl_cli_addr_parse_local(self.__addr, address)
nl_cache_foreach_filter(self.addr_cache, <nl_object *> self.__addr, &delete_addr_cb, self.sock)
def __dealloc__(self):
if self.sock is not NULL:
nl_close(self.sock)
nl_socket_free(self.sock)
if self.__addr is not NULL:
rtnl_addr_put(self.__addr)
if self.link_cache is not NULL:
nl_cache_free(self.link_cache)
if self.addr_cache is not NULL:
nl_cache_free(self.addr_cache)
cdef class Link:
"""
Provide an interface to the Link Layer address management
Example of usage:
>>> l = Link()
>>> l.get_ll("wlan0")
"""
cdef nl_sock * sock
cdef nl_cache * link_cache
cdef rtnl_link * __link
cdef nl_dump_params params
cdef char buff[2048]
def __cinit__(self):
self.params.dp_buf = self.buff
self.params.dp_buflen = len(self.buff)
self.params.dp_type = NL_DUMP_LINE
self.sock = nl_socket_alloc()
if self.sock is NULL:
raise MemoryError()
err = nl_connect(self.sock, NETLINK_ROUTE)
if err < 0:
raise "Unable to connect netlink socket: %s" % nl_geterror(err)
self.__link = nl_cli_link_alloc()
if self.__link is NULL:
raise MemoryError()
self.link_cache = nl_cli_link_alloc_cache_family(self.sock,
rtnl_link_get_family(self.__link))
if self.link_cache is NULL:
raise MemoryError()
def get_lladdr(self, ifaces):
"""
Retrieve the Link-Layer addresses associated to an interface
"""
if self.__link is not NULL:
rtnl_link_put(self.__link)
self.__link = nl_cli_link_alloc()
if self.__link is NULL:
raise MemoryError()
nl_cache_refill(self.sock, self.link_cache)
if isinstance(ifaces, str):
nl_cli_link_parse_name(self.__link, ifaces)
elif isinstance(ifaces, list):
for iface in ifaces:
nl_cli_link_parse_name(self.__link, iface)
memset(self.buff, 0, len(self.buff))
nl_cache_dump_filter(self.link_cache,
&self.params,
<nl_object *>self.__link)
try:
return self.buff.split()[2]
except AttributeError, IndexError:
return ""
def __dealloc__(self):
if self.sock is not NULL:
nl_close(self.sock)
nl_socket_free(self.sock)
if self.__link is not NULL:
rtnl_link_put(self.__link)
if self.link_cache is not NULL:
nl_cache_free(self.link_cache)