/
__init__.py
1948 lines (1617 loc) · 57.8 KB
/
__init__.py
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Netlink
-------
basics
======
General netlink packet structure::
nlmsg packet:
header
data
Generic netlink message header::
nlmsg header:
uint32 length
uint16 type
uint16 flags
uint32 sequence number
uint32 pid
The `length` field is the length of all the packet, including
data and header. The `type` field is used to distinguish different
message types, commands etc. Please note, that there is no
explicit protocol field -- you choose a netlink protocol, when
you create a socket.
The `sequence number` is very important. Netlink is an asynchronous
protocol -- it means, that the packet order doesn't matter and is
not guaranteed. But responses to a request are always marked with
the same sequence number, so you can treat it as a cookie.
Please keep in mind, that a netlink request can initiate a
cascade of events, and netlink messages from these events can
carry sequence number == 0. E.g., it is so when you remove a
primary IP addr from an interface, when `promote_secondaries`
sysctl is set.
Beside of incapsulated headers and other protocol-specific data,
netlink messages can carry NLA (netlink attributes). NLA
structure is as follows::
NLA header:
uint16 length
uint16 type
NLA data:
data-specific struct
# optional:
NLA
NLA
...
So, NLA structures can be nested, forming a tree.
Complete structure of a netlink packet::
nlmsg header:
uint32 length
uint16 type
uint16 flags
uint32 sequence number
uint32 pid
[ optional protocol-specific data ]
[ optional NLA tree ]
More information about netlink protocol you can find in
the man pages.
pyroute2 and netlink
====================
packets
~~~~~~~
To simplify the development, pyroute2 provides an easy way to
describe packet structure. As an example, you can take the
ifaddrmsg description -- `pyroute2/netlink/rtnl/ifaddrmsg.py`.
To describe a packet, you need to inherit from `nlmsg` class::
from pyroute2.netlink import nlmsg
class foo_msg(nlmsg):
fields = ( ... )
nla_map = ( ... )
NLA are described in the same way, but the parent class should be
`nla`, instead of `nlmsg`. And yes, it is important to use the
proper parent class -- it affects the header structure.
fields attribute
~~~~~~~~~~~~~~~~
The `fields` attribute describes the structure of the
protocol-specific data. It is a tuple of tuples, where each
member contains a field name and its data format.
Field data format should be specified as for Python `struct`
module. E.g., ifaddrmsg structure::
struct ifaddrmsg {
__u8 ifa_family;
__u8 ifa_prefixlen;
__u8 ifa_flags;
__u8 ifa_scope;
__u32 ifa_index;
};
should be described as follows::
class ifaddrmsg(nlmsg):
fields = (('family', 'B'),
('prefixlen', 'B'),
('flags', 'B'),
('scope', 'B'),
('index', 'I'))
Format strings are passed directly to the `struct` module,
so you can use all the notations like `>I`, `16s` etc. All
fields are parsed from the stream separately, so if you
want to explicitly fix alignemt, as if it were C struct,
use the `pack` attribute::
class tstats(nla):
pack = 'struct'
fields = (('version', 'H'),
('ac_exitcode', 'I'),
('ac_flag', 'B'),
...)
Explicit padding bytes also can be used, when struct
packing doesn't work well::
class ipq_mode_msg(nlmsg):
pack = 'struct'
fields = (('value', 'B'),
('__pad', '7x'),
('range', 'I'),
('__pad', '12x'))
nla_map attribute
~~~~~~~~~~~~~~~~~
The `nla_map` attribute is a tuple of NLA descriptions. Each
description is also a tuple in two different forms: either
two fields, name and format, or three fields: type, name and
format.
Please notice, that the format field is a string name of
corresponding NLA class::
class ifaddrmsg(nlmsg):
...
nla_map = (('IFA_UNSPEC', 'hex'),
('IFA_ADDRESS', 'ipaddr'),
('IFA_LOCAL', 'ipaddr'),
...)
This code will create mapping, where IFA_ADDRESS NLA will be of
type 1 and IFA_LOCAL -- of type 2, etc. Both NLA will be decoded
as IP addresses (class `ipaddr`). IFA_UNSPEC will be of type 0,
and if it will be in the NLA tree, it will be just dumped in hex.
NLA class names are should be specified as strings, since they
are resolved in runtime.
There are several pre-defined NLA types, that you will get with
`nla` class:
- `none` -- ignore this NLA
- `flag` -- boolean flag NLA (no payload; NLA exists = True)
- `uint8`, `uint16`, `uint32`, `uint64` -- unsigned int
- `be8`, `be16`, `be32`, `be64` -- big-endian unsigned int
- `ipaddr` -- IP address, IPv4 or IPv6
- `ip4addr` -- only IPv4 address type
- `ip6addr` -- only IPv6 address type
- `target` -- a univeral target (IPv4, IPv6, MPLS)
- `l2addr` -- MAC address
- `hex` -- hex dump as a string -- useful for debugging
- `cdata` -- a binary data
- `string` -- UTF-8 string
- `asciiz` -- zero-terminated ASCII string, no decoding
- `array` -- array of simple types (uint8, uint16 etc.)
Please refer to `pyroute2/netlink/__init__.py` for details.
You can also make your own NLA descriptions::
class ifaddrmsg(nlmsg):
...
nla_map = (...
('IFA_CACHEINFO', 'cacheinfo'),
...)
class cacheinfo(nla):
fields = (('ifa_preferred', 'I'),
('ifa_valid', 'I'),
('cstamp', 'I'),
('tstamp', 'I'))
Custom NLA descriptions should be defined in the same class,
where they are used.
Also, it is possible to use not autogenerated type numbers, as
for ifaddrmsg, but specify them explicitly::
class iw_event(nla):
...
nla_map = ((0x8B00, 'SIOCSIWCOMMIT', 'hex'),
(0x8B01, 'SIOCGIWNAME', 'hex'),
(0x8B02, 'SIOCSIWNWID', 'hex'),
(0x8B03, 'SIOCGIWNWID', 'hex'),
...)
Here you can see custom NLA type numbers -- 0x8B00, 0x8B01 etc.
It is not permitted to mix these two forms in one class: you should
use ether autogenerated type numbers (two fields tuples), or
explicit numbers (three fields typles).
array types
~~~~~~~~~~~
There are different array-like NLA types in the kernel, and
some of them are covered by pyroute2. An array of simple type
elements::
# declaration
nla_map = (('NLA_TYPE', 'array(uint8)'), ...)
# data layout
+======+======+----------------------------
| len | type | uint8 | uint8 | uint 8 | ...
+======+======+----------------------------
# decoded
{'attrs': [['NLA_TYPE', (2, 3, 4, 5, ...)], ...], ...}
An array of NLAs::
# declaration
nla_map = (('NLA_TYPE', '*type'), ...)
# data layout
+=======+=======+-----------------------+-----------------------+--
| len | type* | len | type | payload | len | type | payload | ...
+=======+=======+-----------------------+-----------------------+--
# type* -- in that case the type is OR'ed with NLA_F_NESTED
# decoded
{'attrs': [['NLA_TYPE', [payload, payload, ...]], ...], ...}
parsed netlink message
~~~~~~~~~~~~~~~~~~~~~~
Netlink messages are represented by pyroute2 as dictionaries
as follows::
{'header': {'pid': ...,
'length: ...,
'flags': ...,
'error': None, # if you are lucky
'type': ...,
'sequence_number': ...},
# fields attributes
'field_name1': value,
...
'field_nameX': value,
# nla tree
'attrs': [['NLA_NAME1', value],
...
['NLA_NAMEX', value],
['NLA_NAMEY', {'field_name1': value,
...
'field_nameX': value,
'attrs': [['NLA_NAME.... ]]}]]}
As an example, a message from the wireless subsystem about new
scan event::
{'index': 4,
'family': 0,
'__align': 0,
'header': {'pid': 0,
'length': 64,
'flags': 0,
'error': None,
'type': 16,
'sequence_number': 0},
'flags': 69699,
'ifi_type': 1,
'event': 'RTM_NEWLINK',
'change': 0,
'attrs': [['IFLA_IFNAME', 'wlp3s0'],
['IFLA_WIRELESS',
{'attrs': [['SIOCGIWSCAN',
'00:00:00:00:00:00:00:00:00:00:00:00']]}]]}
One important detail is that NLA chain is represented as a list of
elements `['NLA_TYPE', value]`, not as a dictionary. The reason is that
though in the kernel *usually* NLA chain is a dictionary, the netlink
protocol by itself doesn't require elements of each type to be unique.
In a message there may be several NLA of the same type.
encoding and decoding algo
~~~~~~~~~~~~~~~~~~~~~~~~~~
The message encoding works as follows:
1. Reserve space for the message header (if there is)
2. Iterate defined `fields`, encoding values with `struct.pack()`
3. Iterate NLA from the `attrs` field, looking up types in `nla_map`
4. Encode the header
Since every NLA is also an `nlmsg` object, there is a recursion.
The decoding process is a bit simpler:
1. Decode the header
2. Iterate `fields`, decoding values with `struct.unpack()`
3. Iterate NLA until the message ends
If the `fields` attribute is an empty list, the step 2 will be skipped.
The step 3 will be skipped in the case of the empty `nla_map`. If both
attributes are empty lists, only the header will be encoded/decoded.
create and send messages
~~~~~~~~~~~~~~~~~~~~~~~~
Using high-level interfaces like `IPRoute` or `IPDB`, you will never
need to manually construct and send netlink messages. But in the case
you really need it, it is simple as well.
Having a description class, like `ifaddrmsg` from above, you need to:
- instantiate it
- fill the fields
- encode the packet
- send the encoded data
The code::
from pyroute2.netlink import NLM_F_REQUEST
from pyroute2.netlink import NLM_F_ACK
from pyroute2.netlink import NLM_F_CREATE
from pyroute2.netlink import NLM_F_EXCL
from pyroute2.iproute import RTM_NEWADDR
from pyroute2.netlink.rtnl.ifaddrmsg import ifaddrmsg
##
# add an addr to an interface
#
# create the message
msg = ifaddrmsg()
# fill the protocol-specific fields
msg['index'] = index # index of the interface
msg['family'] = AF_INET # address family
msg['prefixlen'] = 24 # the address mask
msg['scope'] = scope # see /etc/iproute2/rt_scopes
# attach NLA -- it MUST be a list / mutable
msg['attrs'] = [['IFA_LOCAL', '192.168.0.1'],
['IFA_ADDRESS', '192.162.0.1']]
# fill generic netlink fields
msg['header']['sequence_number'] = nonce # an unique seq number
msg['header']['pid'] = os.getpid()
msg['header']['type'] = RTM_NEWADDR
msg['header']['flags'] = NLM_F_REQUEST |\\
NLM_F_ACK |\\
NLM_F_CREATE |\\
NLM_F_EXCL
# encode the packet
msg.encode()
# send the buffer
nlsock.sendto(msg.data, (0, 0))
Please notice, that NLA list *MUST* be mutable.
'''
import weakref
import traceback
import logging
import struct
import types
import sys
import io
import re
from socket import inet_pton
from socket import inet_ntop
from socket import AF_INET
from socket import AF_INET6
from socket import AF_UNSPEC
from pyroute2.common import AF_MPLS
from pyroute2.common import hexdump
from pyroute2.common import basestring
from pyroute2.netlink.exceptions import NetlinkError
from pyroute2.netlink.exceptions import NetlinkDecodeError
from pyroute2.netlink.exceptions import NetlinkNLADecodeError
log = logging.getLogger(__name__)
# make pep8 happy
_ne = NetlinkError # reexport for compatibility
_de = NetlinkDecodeError #
class NotInitialized(Exception):
pass
_letters = re.compile('[A-Za-z]')
_fmt_letters = re.compile('[^!><@=][!><@=]')
##
# That's a hack for the code linter, which works under
# Python3, see unicode reference in the code below
if sys.version[0] == '3':
unicode = str
NLMSG_MIN_TYPE = 0x10
GENL_NAMSIZ = 16 # length of family name
GENL_MIN_ID = NLMSG_MIN_TYPE
GENL_MAX_ID = 1023
GENL_ADMIN_PERM = 0x01
GENL_CMD_CAP_DO = 0x02
GENL_CMD_CAP_DUMP = 0x04
GENL_CMD_CAP_HASPOL = 0x08
#
# List of reserved static generic netlink identifiers:
#
GENL_ID_GENERATE = 0
GENL_ID_CTRL = NLMSG_MIN_TYPE
#
# Controller
#
CTRL_CMD_UNSPEC = 0x0
CTRL_CMD_NEWFAMILY = 0x1
CTRL_CMD_DELFAMILY = 0x2
CTRL_CMD_GETFAMILY = 0x3
CTRL_CMD_NEWOPS = 0x4
CTRL_CMD_DELOPS = 0x5
CTRL_CMD_GETOPS = 0x6
CTRL_CMD_NEWMCAST_GRP = 0x7
CTRL_CMD_DELMCAST_GRP = 0x8
CTRL_CMD_GETMCAST_GRP = 0x9 # unused
CTRL_ATTR_UNSPEC = 0x0
CTRL_ATTR_FAMILY_ID = 0x1
CTRL_ATTR_FAMILY_NAME = 0x2
CTRL_ATTR_VERSION = 0x3
CTRL_ATTR_HDRSIZE = 0x4
CTRL_ATTR_MAXATTR = 0x5
CTRL_ATTR_OPS = 0x6
CTRL_ATTR_MCAST_GROUPS = 0x7
CTRL_ATTR_OP_UNSPEC = 0x0
CTRL_ATTR_OP_ID = 0x1
CTRL_ATTR_OP_FLAGS = 0x2
CTRL_ATTR_MCAST_GRP_UNSPEC = 0x0
CTRL_ATTR_MCAST_GRP_NAME = 0x1
CTRL_ATTR_MCAST_GRP_ID = 0x2
# Different Netlink families
#
NETLINK_ROUTE = 0 # Routing/device hook
NETLINK_UNUSED = 1 # Unused number
NETLINK_USERSOCK = 2 # Reserved for user mode socket protocols
NETLINK_FIREWALL = 3 # Firewalling hook
NETLINK_SOCK_DIAG = 4 # INET socket monitoring
NETLINK_NFLOG = 5 # netfilter/iptables ULOG
NETLINK_XFRM = 6 # ipsec
NETLINK_SELINUX = 7 # SELinux event notifications
NETLINK_ISCSI = 8 # Open-iSCSI
NETLINK_AUDIT = 9 # auditing
NETLINK_FIB_LOOKUP = 10
NETLINK_CONNECTOR = 11
NETLINK_NETFILTER = 12 # netfilter subsystem
NETLINK_IP6_FW = 13
NETLINK_DNRTMSG = 14 # DECnet routing messages
NETLINK_KOBJECT_UEVENT = 15 # Kernel messages to userspace
NETLINK_GENERIC = 16
# leave room for NETLINK_DM (DM Events)
NETLINK_SCSITRANSPORT = 18 # SCSI Transports
# NLA flags
NLA_F_NESTED = 1 << 15
NLA_F_NET_BYTEORDER = 1 << 14
# Netlink message flags values (nlmsghdr.flags)
#
NLM_F_REQUEST = 1 # It is request message.
NLM_F_MULTI = 2 # Multipart message, terminated by NLMSG_DONE
NLM_F_ACK = 4 # Reply with ack, with zero or error code
NLM_F_ECHO = 8 # Echo this request
# Modifiers to GET request
NLM_F_ROOT = 0x100 # specify tree root
NLM_F_MATCH = 0x200 # return all matching
NLM_F_ATOMIC = 0x400 # atomic GET
NLM_F_DUMP = (NLM_F_ROOT | NLM_F_MATCH)
# Modifiers to NEW request
NLM_F_REPLACE = 0x100 # Override existing
NLM_F_EXCL = 0x200 # Do not touch, if it exists
NLM_F_CREATE = 0x400 # Create, if it does not exist
NLM_F_APPEND = 0x800 # Add to end of list
NLMSG_NOOP = 0x1 # Nothing
NLMSG_ERROR = 0x2 # Error
NLMSG_DONE = 0x3 # End of a dump
NLMSG_OVERRUN = 0x4 # Data lost
NLMSG_CONTROL = 0xe # Custom message type for messaging control
NLMSG_TRANSPORT = 0xf # Custom message type for NL as a transport
NLMSG_MIN_TYPE = 0x10 # < 0x10: reserved control messages
NLMSG_MAX_LEN = 0xffff # Max message length
mtypes = {1: 'NLMSG_NOOP',
2: 'NLMSG_ERROR',
3: 'NLMSG_DONE',
4: 'NLMSG_OVERRUN'}
IPRCMD_NOOP = 0
IPRCMD_STOP = 1
IPRCMD_ACK = 2
IPRCMD_ERR = 3
IPRCMD_REGISTER = 4
IPRCMD_RELOAD = 5
IPRCMD_ROUTE = 6
IPRCMD_CONNECT = 7
IPRCMD_DISCONNECT = 8
IPRCMD_SERVE = 9
IPRCMD_SHUTDOWN = 10
IPRCMD_SUBSCRIBE = 11
IPRCMD_UNSUBSCRIBE = 12
IPRCMD_PROVIDE = 13
IPRCMD_REMOVE = 14
IPRCMD_DISCOVER = 15
IPRCMD_UNREGISTER = 16
SOL_NETLINK = 270
NETLINK_ADD_MEMBERSHIP = 1
NETLINK_DROP_MEMBERSHIP = 2
NETLINK_PKTINFO = 3
NETLINK_BROADCAST_ERROR = 4
NETLINK_NO_ENOBUFS = 5
NETLINK_RX_RING = 6
NETLINK_TX_RING = 7
NETLINK_LISTEN_ALL_NSID = 8
clean_cbs = {}
# Cached results for some struct operations.
# No cache invalidation required.
cache_fmt = {}
cache_hdr = {}
cache_jit = {}
class nlmsg_base(dict):
'''
Netlink base class. You do not need to inherit it directly, unless
you're inventing completely new protocol structure.
Use nlmsg or nla classes.
The class provides several methods, but often one need to customize
only `decode()` and `encode()`.
'''
fields = tuple()
header = tuple()
pack = None # pack pragma
cell_header = None
align = 4
nla_map = {} # NLA mapping
sql_constraints = {}
sql_extra_fields = tuple()
sql_extend = tuple()
nla_flags = 0 # NLA flags
value_map = {}
is_nla = False
prefix = None
own_parent = False
# caches
__compiled_nla = False
__compiled_ft = False
__t_nla_map = None
__r_nla_map = None
__slots__ = (
"_buf",
"data",
"offset",
"length",
"parent",
"decoded",
"_nla_init",
"_nla_array",
"_nla_flags",
"value",
"_ft_decode",
"_r_value_map",
"__weakref__"
)
def msg_align(self, l):
return (l + self.align - 1) & ~ (self.align - 1)
def __init__(self,
data=None,
offset=0,
length=None,
parent=None,
init=None):
global cache_jit
dict.__init__(self)
for i in self.fields:
self[i[0]] = 0 # FIXME: only for number values
self._buf = None
self.data = data or bytearray()
self.offset = offset
self.length = length or 0
if parent is not None:
# some structures use parents, some not,
# so don't create cycles without need
self.parent = parent if self.own_parent else weakref.proxy(parent)
else:
self.parent = None
self.decoded = False
self._nla_init = init
self._nla_array = False
self._nla_flags = self.nla_flags
self['attrs'] = []
self['value'] = NotInitialized
self.value = NotInitialized
# work only on non-empty mappings
if self.nla_map and not self.__class__.__compiled_nla:
self.compile_nla()
# compile fast-track for particular types
if id(self.__class__) in cache_jit:
self._ft_decode = cache_jit[id(self.__class__)]['ft_decode']
else:
self.compile_ft()
self._r_value_map = dict([
(x[1], x[0]) for x in self.value_map.items()
])
if self.header:
self['header'] = {}
@classmethod
def sql_schema(cls):
ret = []
for field in cls.fields:
if field[0][0] != '_':
ret.append(((field[0], ),
' '.join(('BIGINT',
cls.sql_constraints.get(field[0], '')))))
for nla in cls.nla_map:
if isinstance(nla[0], basestring):
nla_name = nla[0]
nla_type = nla[1]
else:
nla_name = nla[1]
nla_type = nla[2]
nla_type = getattr(cls, nla_type, None)
sql_type = getattr(nla_type, 'sql_type', None)
if sql_type:
sql_type = ' '.join((sql_type,
cls.sql_constraints.get(nla_name, '')))
ret.append(((nla_name, ), sql_type))
for (fname, ftype) in cls.sql_extra_fields:
if isinstance(fname, basestring):
fname = (fname, )
ret.append((fname, ftype))
for (dcls, prefix) in cls.sql_extend:
for fname, ftype in dcls.sql_schema():
ret.append(((prefix, ) + fname, ftype))
return ret
@property
def buf(self):
logging.error('nlmsg.buf is deprecated:\n%s',
''.join(traceback.format_stack()))
if self._buf is None:
self._buf = io.BytesIO()
self._buf.write(self.data[self.offset:self.length or None])
self._buf.seek(0)
return self._buf
def copy(self):
'''
Return a decoded copy of the netlink message. Works
correctly only if the message was encoded, or is
received from the socket.
'''
ret = type(self)(data=self.data, offset=self.offset)
ret.decode()
return ret
def reset(self, buf=None):
self.data = bytearray()
self.offset = 0
self.decoded = False
def register_clean_cb(self, cb):
global clean_cbs
if self.parent is not None:
return self.parent.register_clean_cb(cb)
else:
# get the msg_seq -- if applicable
seq = self.get('header', {}).get('sequence_number', None)
if seq is not None and seq not in clean_cbs:
clean_cbs[seq] = []
# attach the callback
clean_cbs[seq].append(cb)
def unregister_clean_cb(self):
global clean_cbs
seq = self.get('header', {}).get('sequence_number', None)
msf = self.get('header', {}).get('flags', 0)
if (seq is not None) and \
(not msf & NLM_F_REQUEST) and \
seq in clean_cbs:
for cb in clean_cbs[seq]:
try:
cb()
except:
log.error('Cleanup callback fail: %s' % (cb))
log.error(traceback.format_exc())
del clean_cbs[seq]
def _strip_one(self, name):
for i in tuple(self['attrs']):
if i[0] == name:
self['attrs'].remove(i)
return self
def strip(self, attrs):
'''
Remove an NLA from the attrs chain. The `attrs`
parameter can be either string, or iterable. In
the latter case, will be stripped NLAs, specified
in the provided list.
'''
if isinstance(attrs, basestring):
self._strip_one(attrs)
else:
for name in attrs:
self._strip_one(name)
return self
def __ops(self, rvalue, op0, op1):
lvalue = self.getvalue()
res = self.__class__()
for key in lvalue:
if key not in ('header', 'attrs'):
if op0 == '__sub__':
# operator -, complement
if (key not in rvalue) or (lvalue[key] != rvalue[key]):
res[key] = lvalue[key]
elif op0 == '__and__':
# operator &, intersection
if (key in rvalue) and (lvalue[key] == rvalue[key]):
res[key] = lvalue[key]
if 'attrs' in lvalue:
res['attrs'] = []
for attr in lvalue['attrs']:
if isinstance(attr[1], nla):
diff = getattr(attr[1], op0)(rvalue.get_attr(attr[0]))
if diff is not None:
res['attrs'].append([attr[0], diff])
else:
if op0 == '__sub__':
# operator -, complement
if rvalue.get_attr(attr[0]) != attr[1]:
res['attrs'].append(attr)
elif op0 == '__and__':
# operator &, intersection
if rvalue.get_attr(attr[0]) == attr[1]:
res['attrs'].append(attr)
if not len(res):
return None
else:
if 'header' in res:
del res['header']
if 'value' in res:
del res['value']
if 'attrs' in res and not len(res['attrs']):
del res['attrs']
return res
def __sub__(self, rvalue):
'''
Subjunction operation.
'''
return self.__ops(rvalue, '__sub__', '__ne__')
def __and__(self, rvalue):
'''
Conjunction operation.
'''
return self.__ops(rvalue, '__and__', '__eq__')
def __ne__(self, rvalue):
return not self.__eq__(rvalue)
def __eq__(self, rvalue):
'''
Having nla, we are able to use it in operations like::
if nla == 'some value':
...
'''
lvalue = self.getvalue()
if lvalue is self:
for key in self:
try:
if key == 'attrs':
for nla in self[key]:
lv = self.get_attr(nla[0])
if isinstance(lv, dict):
lv = nlmsg().setvalue(lv)
rv = rvalue.get_attr(nla[0])
if isinstance(rv, dict):
rv = nlmsg().setvalue(rv)
# this strange condition means a simple thing:
# None, 0, empty container and NotInitialized in
# that context should be treated as equal.
if (lv != rv) and not \
((not lv or lv is NotInitialized) and
(not rv or rv is NotInitialized)):
return False
else:
lv = self.get(key)
rv = rvalue.get(key)
if (lv != rv) and not \
((not lv or lv is NotInitialized) and
(not rv or rv is NotInitialized)):
return False
except Exception:
# on any error -- is not equal
return False
return True
else:
return lvalue == rvalue
@classmethod
def get_size(self):
size = 0
for field in self.fields:
size += struct.calcsize(field[1])
return size
@classmethod
def nla2name(self, name):
'''
Convert NLA name into human-friendly name
Example: IFLA_ADDRESS -> address
Requires self.prefix to be set
'''
return name[(name.find(self.prefix) + 1) * len(self.prefix):].lower()
@classmethod
def name2nla(self, name):
'''
Convert human-friendly name into NLA name
Example: address -> IFLA_ADDRESS
Requires self.prefix to be set
'''
name = name.upper()
if name.find(self.prefix) == -1:
name = "%s%s" % (self.prefix, name)
return name
def decode(self):
'''
Decode the message. The message should have the `buf`
attribute initialized. e.g.::
data = sock.recv(16384)
msg = ifinfmsg(data)
If you want to customize the decoding process, override
the method, but don't forget to call parent's `decode()`::
class CustomMessage(nlmsg):
def decode(self):
nlmsg.decode(self)
... # do some custom data tuning
'''
offset = self.offset
global cache_hdr
global clean_cbs
# Decode the header
if self.header is not None:
##
# ~~ self['header'][name] = struct.unpack_from(...)
#
# Instead of `struct.unpack()` all the NLA headers, it is
# much cheaper to cache decoded values. The resulting dict
# will be not much bigger than some hundreds ov values.
#
# The code might look ugly, but line_profiler shows here
# a notable performance gain.
#
# The chain is:
# dict.get(key, None) or dict.set(unpack(key, ...)) or dict[key]
#
# If there is no such key in the dict, get() returns None, and
# Python executes __setitem__(), which always return None, and
# then dict[key] is returned.
#
# If the key exists, the statement after the first `or` is not
# executed.
if self.is_nla:
key = tuple(self.data[offset:offset + 4])
self['header'] = cache_hdr.get(key, None) or \
(cache_hdr
.__setitem__(key,
dict(zip(('length', 'type'),
struct.unpack_from('HH',
self.data,
offset))))) or \
cache_hdr[key]
##
offset += 4
self.length = self['header']['length']
else:
for name, fmt in self.header:
self['header'][name] = struct.unpack_from(fmt,
self.data,
offset)[0]
offset += struct.calcsize(fmt)
# update length from header
# it can not be less than 4
if 'header' in self:
self.length = max(self['header']['length'], 4)
# handle the array case
if self._nla_array:
self.setvalue([])
while offset < self.offset + self.length:
cell = type(self)(data=self.data,
offset=offset,
parent=self)
cell._nla_array = False
if cell.cell_header is not None:
cell.header = cell.cell_header
cell.decode()
self.value.append(cell)
offset += (cell.length + 4 - 1) & ~ (4 - 1)
else:
self._ft_decode(self, offset)
if clean_cbs:
self.unregister_clean_cb()
self.decoded = True
def encode(self):
'''
Encode the message into the binary buffer::
msg.encode()
sock.send(msg.data)
If you want to customize the encoding process, override
the method::
class CustomMessage(nlmsg):
def encode(self):
... # do some custom data tuning
nlmsg.encode(self)
'''
offset = self.offset