ynl.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. from collections import namedtuple
  3. from enum import Enum
  4. import functools
  5. import os
  6. import random
  7. import socket
  8. import struct
  9. from struct import Struct
  10. import sys
  11. import yaml
  12. import ipaddress
  13. import uuid
  14. from .nlspec import SpecFamily
  15. #
  16. # Generic Netlink code which should really be in some library, but I can't quickly find one.
  17. #
  18. class Netlink:
  19. # Netlink socket
  20. SOL_NETLINK = 270
  21. NETLINK_ADD_MEMBERSHIP = 1
  22. NETLINK_CAP_ACK = 10
  23. NETLINK_EXT_ACK = 11
  24. NETLINK_GET_STRICT_CHK = 12
  25. # Netlink message
  26. NLMSG_ERROR = 2
  27. NLMSG_DONE = 3
  28. NLM_F_REQUEST = 1
  29. NLM_F_ACK = 4
  30. NLM_F_ROOT = 0x100
  31. NLM_F_MATCH = 0x200
  32. NLM_F_REPLACE = 0x100
  33. NLM_F_EXCL = 0x200
  34. NLM_F_CREATE = 0x400
  35. NLM_F_APPEND = 0x800
  36. NLM_F_CAPPED = 0x100
  37. NLM_F_ACK_TLVS = 0x200
  38. NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH
  39. NLA_F_NESTED = 0x8000
  40. NLA_F_NET_BYTEORDER = 0x4000
  41. NLA_TYPE_MASK = NLA_F_NESTED | NLA_F_NET_BYTEORDER
  42. # Genetlink defines
  43. NETLINK_GENERIC = 16
  44. GENL_ID_CTRL = 0x10
  45. # nlctrl
  46. CTRL_CMD_GETFAMILY = 3
  47. CTRL_ATTR_FAMILY_ID = 1
  48. CTRL_ATTR_FAMILY_NAME = 2
  49. CTRL_ATTR_MAXATTR = 5
  50. CTRL_ATTR_MCAST_GROUPS = 7
  51. CTRL_ATTR_MCAST_GRP_NAME = 1
  52. CTRL_ATTR_MCAST_GRP_ID = 2
  53. # Extack types
  54. NLMSGERR_ATTR_MSG = 1
  55. NLMSGERR_ATTR_OFFS = 2
  56. NLMSGERR_ATTR_COOKIE = 3
  57. NLMSGERR_ATTR_POLICY = 4
  58. NLMSGERR_ATTR_MISS_TYPE = 5
  59. NLMSGERR_ATTR_MISS_NEST = 6
  60. # Policy types
  61. NL_POLICY_TYPE_ATTR_TYPE = 1
  62. NL_POLICY_TYPE_ATTR_MIN_VALUE_S = 2
  63. NL_POLICY_TYPE_ATTR_MAX_VALUE_S = 3
  64. NL_POLICY_TYPE_ATTR_MIN_VALUE_U = 4
  65. NL_POLICY_TYPE_ATTR_MAX_VALUE_U = 5
  66. NL_POLICY_TYPE_ATTR_MIN_LENGTH = 6
  67. NL_POLICY_TYPE_ATTR_MAX_LENGTH = 7
  68. NL_POLICY_TYPE_ATTR_POLICY_IDX = 8
  69. NL_POLICY_TYPE_ATTR_POLICY_MAXTYPE = 9
  70. NL_POLICY_TYPE_ATTR_BITFIELD32_MASK = 10
  71. NL_POLICY_TYPE_ATTR_PAD = 11
  72. NL_POLICY_TYPE_ATTR_MASK = 12
  73. AttrType = Enum('AttrType', ['flag', 'u8', 'u16', 'u32', 'u64',
  74. 's8', 's16', 's32', 's64',
  75. 'binary', 'string', 'nul-string',
  76. 'nested', 'nested-array',
  77. 'bitfield32', 'sint', 'uint'])
  78. class NlError(Exception):
  79. def __init__(self, nl_msg):
  80. self.nl_msg = nl_msg
  81. self.error = -nl_msg.error
  82. def __str__(self):
  83. return f"Netlink error: {os.strerror(self.error)}\n{self.nl_msg}"
  84. class ConfigError(Exception):
  85. pass
  86. class NlAttr:
  87. ScalarFormat = namedtuple('ScalarFormat', ['native', 'big', 'little'])
  88. type_formats = {
  89. 'u8' : ScalarFormat(Struct('B'), Struct("B"), Struct("B")),
  90. 's8' : ScalarFormat(Struct('b'), Struct("b"), Struct("b")),
  91. 'u16': ScalarFormat(Struct('H'), Struct(">H"), Struct("<H")),
  92. 's16': ScalarFormat(Struct('h'), Struct(">h"), Struct("<h")),
  93. 'u32': ScalarFormat(Struct('I'), Struct(">I"), Struct("<I")),
  94. 's32': ScalarFormat(Struct('i'), Struct(">i"), Struct("<i")),
  95. 'u64': ScalarFormat(Struct('Q'), Struct(">Q"), Struct("<Q")),
  96. 's64': ScalarFormat(Struct('q'), Struct(">q"), Struct("<q"))
  97. }
  98. def __init__(self, raw, offset):
  99. self._len, self._type = struct.unpack("HH", raw[offset : offset + 4])
  100. self.type = self._type & ~Netlink.NLA_TYPE_MASK
  101. self.is_nest = self._type & Netlink.NLA_F_NESTED
  102. self.payload_len = self._len
  103. self.full_len = (self.payload_len + 3) & ~3
  104. self.raw = raw[offset + 4 : offset + self.payload_len]
  105. @classmethod
  106. def get_format(cls, attr_type, byte_order=None):
  107. format = cls.type_formats[attr_type]
  108. if byte_order:
  109. return format.big if byte_order == "big-endian" \
  110. else format.little
  111. return format.native
  112. def as_scalar(self, attr_type, byte_order=None):
  113. format = self.get_format(attr_type, byte_order)
  114. return format.unpack(self.raw)[0]
  115. def as_auto_scalar(self, attr_type, byte_order=None):
  116. if len(self.raw) != 4 and len(self.raw) != 8:
  117. raise Exception(f"Auto-scalar len payload be 4 or 8 bytes, got {len(self.raw)}")
  118. real_type = attr_type[0] + str(len(self.raw) * 8)
  119. format = self.get_format(real_type, byte_order)
  120. return format.unpack(self.raw)[0]
  121. def as_strz(self):
  122. return self.raw.decode('ascii')[:-1]
  123. def as_bin(self):
  124. return self.raw
  125. def as_c_array(self, type):
  126. format = self.get_format(type)
  127. return [ x[0] for x in format.iter_unpack(self.raw) ]
  128. def __repr__(self):
  129. return f"[type:{self.type} len:{self._len}] {self.raw}"
  130. class NlAttrs:
  131. def __init__(self, msg, offset=0):
  132. self.attrs = []
  133. while offset < len(msg):
  134. attr = NlAttr(msg, offset)
  135. offset += attr.full_len
  136. self.attrs.append(attr)
  137. def __iter__(self):
  138. yield from self.attrs
  139. def __repr__(self):
  140. msg = ''
  141. for a in self.attrs:
  142. if msg:
  143. msg += '\n'
  144. msg += repr(a)
  145. return msg
  146. class NlMsg:
  147. def __init__(self, msg, offset, attr_space=None):
  148. self.hdr = msg[offset : offset + 16]
  149. self.nl_len, self.nl_type, self.nl_flags, self.nl_seq, self.nl_portid = \
  150. struct.unpack("IHHII", self.hdr)
  151. self.raw = msg[offset + 16 : offset + self.nl_len]
  152. self.error = 0
  153. self.done = 0
  154. extack_off = None
  155. if self.nl_type == Netlink.NLMSG_ERROR:
  156. self.error = struct.unpack("i", self.raw[0:4])[0]
  157. self.done = 1
  158. extack_off = 20
  159. elif self.nl_type == Netlink.NLMSG_DONE:
  160. self.error = struct.unpack("i", self.raw[0:4])[0]
  161. self.done = 1
  162. extack_off = 4
  163. self.extack = None
  164. if self.nl_flags & Netlink.NLM_F_ACK_TLVS and extack_off:
  165. self.extack = dict()
  166. extack_attrs = NlAttrs(self.raw[extack_off:])
  167. for extack in extack_attrs:
  168. if extack.type == Netlink.NLMSGERR_ATTR_MSG:
  169. self.extack['msg'] = extack.as_strz()
  170. elif extack.type == Netlink.NLMSGERR_ATTR_MISS_TYPE:
  171. self.extack['miss-type'] = extack.as_scalar('u32')
  172. elif extack.type == Netlink.NLMSGERR_ATTR_MISS_NEST:
  173. self.extack['miss-nest'] = extack.as_scalar('u32')
  174. elif extack.type == Netlink.NLMSGERR_ATTR_OFFS:
  175. self.extack['bad-attr-offs'] = extack.as_scalar('u32')
  176. elif extack.type == Netlink.NLMSGERR_ATTR_POLICY:
  177. self.extack['policy'] = self._decode_policy(extack.raw)
  178. else:
  179. if 'unknown' not in self.extack:
  180. self.extack['unknown'] = []
  181. self.extack['unknown'].append(extack)
  182. if attr_space:
  183. # We don't have the ability to parse nests yet, so only do global
  184. if 'miss-type' in self.extack and 'miss-nest' not in self.extack:
  185. miss_type = self.extack['miss-type']
  186. if miss_type in attr_space.attrs_by_val:
  187. spec = attr_space.attrs_by_val[miss_type]
  188. self.extack['miss-type'] = spec['name']
  189. if 'doc' in spec:
  190. self.extack['miss-type-doc'] = spec['doc']
  191. def _decode_policy(self, raw):
  192. policy = {}
  193. for attr in NlAttrs(raw):
  194. if attr.type == Netlink.NL_POLICY_TYPE_ATTR_TYPE:
  195. type = attr.as_scalar('u32')
  196. policy['type'] = Netlink.AttrType(type).name
  197. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MIN_VALUE_S:
  198. policy['min-value'] = attr.as_scalar('s64')
  199. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MAX_VALUE_S:
  200. policy['max-value'] = attr.as_scalar('s64')
  201. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MIN_VALUE_U:
  202. policy['min-value'] = attr.as_scalar('u64')
  203. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MAX_VALUE_U:
  204. policy['max-value'] = attr.as_scalar('u64')
  205. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MIN_LENGTH:
  206. policy['min-length'] = attr.as_scalar('u32')
  207. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MAX_LENGTH:
  208. policy['max-length'] = attr.as_scalar('u32')
  209. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_BITFIELD32_MASK:
  210. policy['bitfield32-mask'] = attr.as_scalar('u32')
  211. elif attr.type == Netlink.NL_POLICY_TYPE_ATTR_MASK:
  212. policy['mask'] = attr.as_scalar('u64')
  213. return policy
  214. def cmd(self):
  215. return self.nl_type
  216. def __repr__(self):
  217. msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}"
  218. if self.error:
  219. msg += '\n\terror: ' + str(self.error)
  220. if self.extack:
  221. msg += '\n\textack: ' + repr(self.extack)
  222. return msg
  223. class NlMsgs:
  224. def __init__(self, data, attr_space=None):
  225. self.msgs = []
  226. offset = 0
  227. while offset < len(data):
  228. msg = NlMsg(data, offset, attr_space=attr_space)
  229. offset += msg.nl_len
  230. self.msgs.append(msg)
  231. def __iter__(self):
  232. yield from self.msgs
  233. genl_family_name_to_id = None
  234. def _genl_msg(nl_type, nl_flags, genl_cmd, genl_version, seq=None):
  235. # we prepend length in _genl_msg_finalize()
  236. if seq is None:
  237. seq = random.randint(1, 1024)
  238. nlmsg = struct.pack("HHII", nl_type, nl_flags, seq, 0)
  239. genlmsg = struct.pack("BBH", genl_cmd, genl_version, 0)
  240. return nlmsg + genlmsg
  241. def _genl_msg_finalize(msg):
  242. return struct.pack("I", len(msg) + 4) + msg
  243. def _genl_load_families():
  244. with socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, Netlink.NETLINK_GENERIC) as sock:
  245. sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
  246. msg = _genl_msg(Netlink.GENL_ID_CTRL,
  247. Netlink.NLM_F_REQUEST | Netlink.NLM_F_ACK | Netlink.NLM_F_DUMP,
  248. Netlink.CTRL_CMD_GETFAMILY, 1)
  249. msg = _genl_msg_finalize(msg)
  250. sock.send(msg, 0)
  251. global genl_family_name_to_id
  252. genl_family_name_to_id = dict()
  253. while True:
  254. reply = sock.recv(128 * 1024)
  255. nms = NlMsgs(reply)
  256. for nl_msg in nms:
  257. if nl_msg.error:
  258. print("Netlink error:", nl_msg.error)
  259. return
  260. if nl_msg.done:
  261. return
  262. gm = GenlMsg(nl_msg)
  263. fam = dict()
  264. for attr in NlAttrs(gm.raw):
  265. if attr.type == Netlink.CTRL_ATTR_FAMILY_ID:
  266. fam['id'] = attr.as_scalar('u16')
  267. elif attr.type == Netlink.CTRL_ATTR_FAMILY_NAME:
  268. fam['name'] = attr.as_strz()
  269. elif attr.type == Netlink.CTRL_ATTR_MAXATTR:
  270. fam['maxattr'] = attr.as_scalar('u32')
  271. elif attr.type == Netlink.CTRL_ATTR_MCAST_GROUPS:
  272. fam['mcast'] = dict()
  273. for entry in NlAttrs(attr.raw):
  274. mcast_name = None
  275. mcast_id = None
  276. for entry_attr in NlAttrs(entry.raw):
  277. if entry_attr.type == Netlink.CTRL_ATTR_MCAST_GRP_NAME:
  278. mcast_name = entry_attr.as_strz()
  279. elif entry_attr.type == Netlink.CTRL_ATTR_MCAST_GRP_ID:
  280. mcast_id = entry_attr.as_scalar('u32')
  281. if mcast_name and mcast_id is not None:
  282. fam['mcast'][mcast_name] = mcast_id
  283. if 'name' in fam and 'id' in fam:
  284. genl_family_name_to_id[fam['name']] = fam
  285. class GenlMsg:
  286. def __init__(self, nl_msg):
  287. self.nl = nl_msg
  288. self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
  289. self.raw = nl_msg.raw[4:]
  290. def cmd(self):
  291. return self.genl_cmd
  292. def __repr__(self):
  293. msg = repr(self.nl)
  294. msg += f"\tgenl_cmd = {self.genl_cmd} genl_ver = {self.genl_version}\n"
  295. for a in self.raw_attrs:
  296. msg += '\t\t' + repr(a) + '\n'
  297. return msg
  298. class NetlinkProtocol:
  299. def __init__(self, family_name, proto_num):
  300. self.family_name = family_name
  301. self.proto_num = proto_num
  302. def _message(self, nl_type, nl_flags, seq=None):
  303. if seq is None:
  304. seq = random.randint(1, 1024)
  305. nlmsg = struct.pack("HHII", nl_type, nl_flags, seq, 0)
  306. return nlmsg
  307. def message(self, flags, command, version, seq=None):
  308. return self._message(command, flags, seq)
  309. def _decode(self, nl_msg):
  310. return nl_msg
  311. def decode(self, ynl, nl_msg, op):
  312. msg = self._decode(nl_msg)
  313. if op is None:
  314. op = ynl.rsp_by_value[msg.cmd()]
  315. fixed_header_size = ynl._struct_size(op.fixed_header)
  316. msg.raw_attrs = NlAttrs(msg.raw, fixed_header_size)
  317. return msg
  318. def get_mcast_id(self, mcast_name, mcast_groups):
  319. if mcast_name not in mcast_groups:
  320. raise Exception(f'Multicast group "{mcast_name}" not present in the spec')
  321. return mcast_groups[mcast_name].value
  322. def msghdr_size(self):
  323. return 16
  324. class GenlProtocol(NetlinkProtocol):
  325. def __init__(self, family_name):
  326. super().__init__(family_name, Netlink.NETLINK_GENERIC)
  327. global genl_family_name_to_id
  328. if genl_family_name_to_id is None:
  329. _genl_load_families()
  330. self.genl_family = genl_family_name_to_id[family_name]
  331. self.family_id = genl_family_name_to_id[family_name]['id']
  332. def message(self, flags, command, version, seq=None):
  333. nlmsg = self._message(self.family_id, flags, seq)
  334. genlmsg = struct.pack("BBH", command, version, 0)
  335. return nlmsg + genlmsg
  336. def _decode(self, nl_msg):
  337. return GenlMsg(nl_msg)
  338. def get_mcast_id(self, mcast_name, mcast_groups):
  339. if mcast_name not in self.genl_family['mcast']:
  340. raise Exception(f'Multicast group "{mcast_name}" not present in the family')
  341. return self.genl_family['mcast'][mcast_name]
  342. def msghdr_size(self):
  343. return super().msghdr_size() + 4
  344. class SpaceAttrs:
  345. SpecValuesPair = namedtuple('SpecValuesPair', ['spec', 'values'])
  346. def __init__(self, attr_space, attrs, outer = None):
  347. outer_scopes = outer.scopes if outer else []
  348. inner_scope = self.SpecValuesPair(attr_space, attrs)
  349. self.scopes = [inner_scope] + outer_scopes
  350. def lookup(self, name):
  351. for scope in self.scopes:
  352. if name in scope.spec:
  353. if name in scope.values:
  354. return scope.values[name]
  355. spec_name = scope.spec.yaml['name']
  356. raise Exception(
  357. f"No value for '{name}' in attribute space '{spec_name}'")
  358. raise Exception(f"Attribute '{name}' not defined in any attribute-set")
  359. #
  360. # YNL implementation details.
  361. #
  362. class YnlFamily(SpecFamily):
  363. def __init__(self, def_path, schema=None, process_unknown=False,
  364. recv_size=0):
  365. super().__init__(def_path, schema)
  366. self.include_raw = False
  367. self.process_unknown = process_unknown
  368. try:
  369. if self.proto == "netlink-raw":
  370. self.nlproto = NetlinkProtocol(self.yaml['name'],
  371. self.yaml['protonum'])
  372. else:
  373. self.nlproto = GenlProtocol(self.yaml['name'])
  374. except KeyError:
  375. raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
  376. self._recv_dbg = False
  377. # Note that netlink will use conservative (min) message size for
  378. # the first dump recv() on the socket, our setting will only matter
  379. # from the second recv() on.
  380. self._recv_size = recv_size if recv_size else 131072
  381. # Netlink will always allocate at least PAGE_SIZE - sizeof(skb_shinfo)
  382. # for a message, so smaller receive sizes will lead to truncation.
  383. # Note that the min size for other families may be larger than 4k!
  384. if self._recv_size < 4000:
  385. raise ConfigError()
  386. self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
  387. self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
  388. self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
  389. self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
  390. self.async_msg_ids = set()
  391. self.async_msg_queue = []
  392. for msg in self.msgs.values():
  393. if msg.is_async:
  394. self.async_msg_ids.add(msg.rsp_value)
  395. for op_name, op in self.ops.items():
  396. bound_f = functools.partial(self._op, op_name)
  397. setattr(self, op.ident_name, bound_f)
  398. def ntf_subscribe(self, mcast_name):
  399. mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
  400. self.sock.bind((0, 0))
  401. self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
  402. mcast_id)
  403. def set_recv_dbg(self, enabled):
  404. self._recv_dbg = enabled
  405. def _recv_dbg_print(self, reply, nl_msgs):
  406. if not self._recv_dbg:
  407. return
  408. print("Recv: read", len(reply), "bytes,",
  409. len(nl_msgs.msgs), "messages", file=sys.stderr)
  410. for nl_msg in nl_msgs:
  411. print(" ", nl_msg, file=sys.stderr)
  412. def _encode_enum(self, attr_spec, value):
  413. enum = self.consts[attr_spec['enum']]
  414. if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
  415. scalar = 0
  416. if isinstance(value, str):
  417. value = [value]
  418. for single_value in value:
  419. scalar += enum.entries[single_value].user_value(as_flags = True)
  420. return scalar
  421. else:
  422. return enum.entries[value].user_value()
  423. def _get_scalar(self, attr_spec, value):
  424. try:
  425. return int(value)
  426. except (ValueError, TypeError) as e:
  427. if 'enum' not in attr_spec:
  428. raise e
  429. return self._encode_enum(attr_spec, value)
  430. def _add_attr(self, space, name, value, search_attrs):
  431. try:
  432. attr = self.attr_sets[space][name]
  433. except KeyError:
  434. raise Exception(f"Space '{space}' has no attribute '{name}'")
  435. nl_type = attr.value
  436. if attr.is_multi and isinstance(value, list):
  437. attr_payload = b''
  438. for subvalue in value:
  439. attr_payload += self._add_attr(space, name, subvalue, search_attrs)
  440. return attr_payload
  441. if attr["type"] == 'nest':
  442. nl_type |= Netlink.NLA_F_NESTED
  443. attr_payload = b''
  444. sub_space = attr['nested-attributes']
  445. sub_attrs = SpaceAttrs(self.attr_sets[sub_space], value, search_attrs)
  446. for subname, subvalue in value.items():
  447. attr_payload += self._add_attr(sub_space, subname, subvalue, sub_attrs)
  448. elif attr["type"] == 'flag':
  449. if not value:
  450. # If value is absent or false then skip attribute creation.
  451. return b''
  452. attr_payload = b''
  453. elif attr["type"] == 'string':
  454. attr_payload = str(value).encode('ascii') + b'\x00'
  455. elif attr["type"] == 'binary':
  456. if isinstance(value, bytes):
  457. attr_payload = value
  458. elif isinstance(value, str):
  459. attr_payload = bytes.fromhex(value)
  460. elif isinstance(value, dict) and attr.struct_name:
  461. attr_payload = self._encode_struct(attr.struct_name, value)
  462. else:
  463. raise Exception(f'Unknown type for binary attribute, value: {value}')
  464. elif attr['type'] in NlAttr.type_formats or attr.is_auto_scalar:
  465. scalar = self._get_scalar(attr, value)
  466. if attr.is_auto_scalar:
  467. attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64')
  468. else:
  469. attr_type = attr["type"]
  470. format = NlAttr.get_format(attr_type, attr.byte_order)
  471. attr_payload = format.pack(scalar)
  472. elif attr['type'] in "bitfield32":
  473. scalar_value = self._get_scalar(attr, value["value"])
  474. scalar_selector = self._get_scalar(attr, value["selector"])
  475. attr_payload = struct.pack("II", scalar_value, scalar_selector)
  476. elif attr['type'] == 'sub-message':
  477. msg_format = self._resolve_selector(attr, search_attrs)
  478. attr_payload = b''
  479. if msg_format.fixed_header:
  480. attr_payload += self._encode_struct(msg_format.fixed_header, value)
  481. if msg_format.attr_set:
  482. if msg_format.attr_set in self.attr_sets:
  483. nl_type |= Netlink.NLA_F_NESTED
  484. sub_attrs = SpaceAttrs(msg_format.attr_set, value, search_attrs)
  485. for subname, subvalue in value.items():
  486. attr_payload += self._add_attr(msg_format.attr_set,
  487. subname, subvalue, sub_attrs)
  488. else:
  489. raise Exception(f"Unknown attribute-set '{msg_format.attr_set}'")
  490. else:
  491. raise Exception(f'Unknown type at {space} {name} {value} {attr["type"]}')
  492. pad = b'\x00' * ((4 - len(attr_payload) % 4) % 4)
  493. return struct.pack('HH', len(attr_payload) + 4, nl_type) + attr_payload + pad
  494. def _decode_enum(self, raw, attr_spec):
  495. enum = self.consts[attr_spec['enum']]
  496. if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
  497. i = 0
  498. value = set()
  499. while raw:
  500. if raw & 1:
  501. value.add(enum.entries_by_val[i].name)
  502. raw >>= 1
  503. i += 1
  504. else:
  505. value = enum.entries_by_val[raw].name
  506. return value
  507. def _decode_binary(self, attr, attr_spec):
  508. if attr_spec.struct_name:
  509. decoded = self._decode_struct(attr.raw, attr_spec.struct_name)
  510. elif attr_spec.sub_type:
  511. decoded = attr.as_c_array(attr_spec.sub_type)
  512. else:
  513. decoded = attr.as_bin()
  514. if attr_spec.display_hint:
  515. decoded = self._formatted_string(decoded, attr_spec.display_hint)
  516. return decoded
  517. def _decode_array_attr(self, attr, attr_spec):
  518. decoded = []
  519. offset = 0
  520. while offset < len(attr.raw):
  521. item = NlAttr(attr.raw, offset)
  522. offset += item.full_len
  523. if attr_spec["sub-type"] == 'nest':
  524. subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
  525. decoded.append({ item.type: subattrs })
  526. elif attr_spec["sub-type"] == 'binary':
  527. subattrs = item.as_bin()
  528. if attr_spec.display_hint:
  529. subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
  530. decoded.append(subattrs)
  531. elif attr_spec["sub-type"] in NlAttr.type_formats:
  532. subattrs = item.as_scalar(attr_spec['sub-type'], attr_spec.byte_order)
  533. if attr_spec.display_hint:
  534. subattrs = self._formatted_string(subattrs, attr_spec.display_hint)
  535. decoded.append(subattrs)
  536. else:
  537. raise Exception(f'Unknown {attr_spec["sub-type"]} with name {attr_spec["name"]}')
  538. return decoded
  539. def _decode_nest_type_value(self, attr, attr_spec):
  540. decoded = {}
  541. value = attr
  542. for name in attr_spec['type-value']:
  543. value = NlAttr(value.raw, 0)
  544. decoded[name] = value.type
  545. subattrs = self._decode(NlAttrs(value.raw), attr_spec['nested-attributes'])
  546. decoded.update(subattrs)
  547. return decoded
  548. def _decode_unknown(self, attr):
  549. if attr.is_nest:
  550. return self._decode(NlAttrs(attr.raw), None)
  551. else:
  552. return attr.as_bin()
  553. def _rsp_add(self, rsp, name, is_multi, decoded):
  554. if is_multi == None:
  555. if name in rsp and type(rsp[name]) is not list:
  556. rsp[name] = [rsp[name]]
  557. is_multi = True
  558. else:
  559. is_multi = False
  560. if not is_multi:
  561. rsp[name] = decoded
  562. elif name in rsp:
  563. rsp[name].append(decoded)
  564. else:
  565. rsp[name] = [decoded]
  566. def _resolve_selector(self, attr_spec, search_attrs):
  567. sub_msg = attr_spec.sub_message
  568. if sub_msg not in self.sub_msgs:
  569. raise Exception(f"No sub-message spec named {sub_msg} for {attr_spec.name}")
  570. sub_msg_spec = self.sub_msgs[sub_msg]
  571. selector = attr_spec.selector
  572. value = search_attrs.lookup(selector)
  573. if value not in sub_msg_spec.formats:
  574. raise Exception(f"No message format for '{value}' in sub-message spec '{sub_msg}'")
  575. spec = sub_msg_spec.formats[value]
  576. return spec
  577. def _decode_sub_msg(self, attr, attr_spec, search_attrs):
  578. msg_format = self._resolve_selector(attr_spec, search_attrs)
  579. decoded = {}
  580. offset = 0
  581. if msg_format.fixed_header:
  582. decoded.update(self._decode_struct(attr.raw, msg_format.fixed_header));
  583. offset = self._struct_size(msg_format.fixed_header)
  584. if msg_format.attr_set:
  585. if msg_format.attr_set in self.attr_sets:
  586. subdict = self._decode(NlAttrs(attr.raw, offset), msg_format.attr_set)
  587. decoded.update(subdict)
  588. else:
  589. raise Exception(f"Unknown attribute-set '{attr_space}' when decoding '{attr_spec.name}'")
  590. return decoded
  591. def _decode(self, attrs, space, outer_attrs = None):
  592. rsp = dict()
  593. if space:
  594. attr_space = self.attr_sets[space]
  595. search_attrs = SpaceAttrs(attr_space, rsp, outer_attrs)
  596. for attr in attrs:
  597. try:
  598. attr_spec = attr_space.attrs_by_val[attr.type]
  599. except (KeyError, UnboundLocalError):
  600. if not self.process_unknown:
  601. raise Exception(f"Space '{space}' has no attribute with value '{attr.type}'")
  602. attr_name = f"UnknownAttr({attr.type})"
  603. self._rsp_add(rsp, attr_name, None, self._decode_unknown(attr))
  604. continue
  605. if attr_spec["type"] == 'nest':
  606. subdict = self._decode(NlAttrs(attr.raw), attr_spec['nested-attributes'], search_attrs)
  607. decoded = subdict
  608. elif attr_spec["type"] == 'string':
  609. decoded = attr.as_strz()
  610. elif attr_spec["type"] == 'binary':
  611. decoded = self._decode_binary(attr, attr_spec)
  612. elif attr_spec["type"] == 'flag':
  613. decoded = True
  614. elif attr_spec.is_auto_scalar:
  615. decoded = attr.as_auto_scalar(attr_spec['type'], attr_spec.byte_order)
  616. elif attr_spec["type"] in NlAttr.type_formats:
  617. decoded = attr.as_scalar(attr_spec['type'], attr_spec.byte_order)
  618. if 'enum' in attr_spec:
  619. decoded = self._decode_enum(decoded, attr_spec)
  620. elif attr_spec.display_hint:
  621. decoded = self._formatted_string(decoded, attr_spec.display_hint)
  622. elif attr_spec["type"] == 'indexed-array':
  623. decoded = self._decode_array_attr(attr, attr_spec)
  624. elif attr_spec["type"] == 'bitfield32':
  625. value, selector = struct.unpack("II", attr.raw)
  626. if 'enum' in attr_spec:
  627. value = self._decode_enum(value, attr_spec)
  628. selector = self._decode_enum(selector, attr_spec)
  629. decoded = {"value": value, "selector": selector}
  630. elif attr_spec["type"] == 'sub-message':
  631. decoded = self._decode_sub_msg(attr, attr_spec, search_attrs)
  632. elif attr_spec["type"] == 'nest-type-value':
  633. decoded = self._decode_nest_type_value(attr, attr_spec)
  634. else:
  635. if not self.process_unknown:
  636. raise Exception(f'Unknown {attr_spec["type"]} with name {attr_spec["name"]}')
  637. decoded = self._decode_unknown(attr)
  638. self._rsp_add(rsp, attr_spec["name"], attr_spec.is_multi, decoded)
  639. return rsp
  640. def _decode_extack_path(self, attrs, attr_set, offset, target):
  641. for attr in attrs:
  642. try:
  643. attr_spec = attr_set.attrs_by_val[attr.type]
  644. except KeyError:
  645. raise Exception(f"Space '{attr_set.name}' has no attribute with value '{attr.type}'")
  646. if offset > target:
  647. break
  648. if offset == target:
  649. return '.' + attr_spec.name
  650. if offset + attr.full_len <= target:
  651. offset += attr.full_len
  652. continue
  653. if attr_spec['type'] != 'nest':
  654. raise Exception(f"Can't dive into {attr.type} ({attr_spec['name']}) for extack")
  655. offset += 4
  656. subpath = self._decode_extack_path(NlAttrs(attr.raw),
  657. self.attr_sets[attr_spec['nested-attributes']],
  658. offset, target)
  659. if subpath is None:
  660. return None
  661. return '.' + attr_spec.name + subpath
  662. return None
  663. def _decode_extack(self, request, op, extack):
  664. if 'bad-attr-offs' not in extack:
  665. return
  666. msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set), op)
  667. offset = self.nlproto.msghdr_size() + self._struct_size(op.fixed_header)
  668. path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
  669. extack['bad-attr-offs'])
  670. if path:
  671. del extack['bad-attr-offs']
  672. extack['bad-attr'] = path
  673. def _struct_size(self, name):
  674. if name:
  675. members = self.consts[name].members
  676. size = 0
  677. for m in members:
  678. if m.type in ['pad', 'binary']:
  679. if m.struct:
  680. size += self._struct_size(m.struct)
  681. else:
  682. size += m.len
  683. else:
  684. format = NlAttr.get_format(m.type, m.byte_order)
  685. size += format.size
  686. return size
  687. else:
  688. return 0
  689. def _decode_struct(self, data, name):
  690. members = self.consts[name].members
  691. attrs = dict()
  692. offset = 0
  693. for m in members:
  694. value = None
  695. if m.type == 'pad':
  696. offset += m.len
  697. elif m.type == 'binary':
  698. if m.struct:
  699. len = self._struct_size(m.struct)
  700. value = self._decode_struct(data[offset : offset + len],
  701. m.struct)
  702. offset += len
  703. else:
  704. value = data[offset : offset + m.len]
  705. offset += m.len
  706. else:
  707. format = NlAttr.get_format(m.type, m.byte_order)
  708. [ value ] = format.unpack_from(data, offset)
  709. offset += format.size
  710. if value is not None:
  711. if m.enum:
  712. value = self._decode_enum(value, m)
  713. elif m.display_hint:
  714. value = self._formatted_string(value, m.display_hint)
  715. attrs[m.name] = value
  716. return attrs
  717. def _encode_struct(self, name, vals):
  718. members = self.consts[name].members
  719. attr_payload = b''
  720. for m in members:
  721. value = vals.pop(m.name) if m.name in vals else None
  722. if m.type == 'pad':
  723. attr_payload += bytearray(m.len)
  724. elif m.type == 'binary':
  725. if m.struct:
  726. if value is None:
  727. value = dict()
  728. attr_payload += self._encode_struct(m.struct, value)
  729. else:
  730. if value is None:
  731. attr_payload += bytearray(m.len)
  732. else:
  733. attr_payload += bytes.fromhex(value)
  734. else:
  735. if value is None:
  736. value = 0
  737. format = NlAttr.get_format(m.type, m.byte_order)
  738. attr_payload += format.pack(value)
  739. return attr_payload
  740. def _formatted_string(self, raw, display_hint):
  741. if display_hint == 'mac':
  742. formatted = ':'.join('%02x' % b for b in raw)
  743. elif display_hint == 'hex':
  744. if isinstance(raw, int):
  745. formatted = hex(raw)
  746. else:
  747. formatted = bytes.hex(raw, ' ')
  748. elif display_hint in [ 'ipv4', 'ipv6' ]:
  749. formatted = format(ipaddress.ip_address(raw))
  750. elif display_hint == 'uuid':
  751. formatted = str(uuid.UUID(bytes=raw))
  752. else:
  753. formatted = raw
  754. return formatted
  755. def handle_ntf(self, decoded):
  756. msg = dict()
  757. if self.include_raw:
  758. msg['raw'] = decoded
  759. op = self.rsp_by_value[decoded.cmd()]
  760. attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
  761. if op.fixed_header:
  762. attrs.update(self._decode_struct(decoded.raw, op.fixed_header))
  763. msg['name'] = op['name']
  764. msg['msg'] = attrs
  765. self.async_msg_queue.append(msg)
  766. def check_ntf(self):
  767. while True:
  768. try:
  769. reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT)
  770. except BlockingIOError:
  771. return
  772. nms = NlMsgs(reply)
  773. self._recv_dbg_print(reply, nms)
  774. for nl_msg in nms:
  775. if nl_msg.error:
  776. print("Netlink error in ntf!?", os.strerror(-nl_msg.error))
  777. print(nl_msg)
  778. continue
  779. if nl_msg.done:
  780. print("Netlink done while checking for ntf!?")
  781. continue
  782. decoded = self.nlproto.decode(self, nl_msg, None)
  783. if decoded.cmd() not in self.async_msg_ids:
  784. print("Unexpected msg id done while checking for ntf", decoded)
  785. continue
  786. self.handle_ntf(decoded)
  787. def operation_do_attributes(self, name):
  788. """
  789. For a given operation name, find and return a supported
  790. set of attributes (as a dict).
  791. """
  792. op = self.find_operation(name)
  793. if not op:
  794. return None
  795. return op['do']['request']['attributes'].copy()
  796. def _encode_message(self, op, vals, flags, req_seq):
  797. nl_flags = Netlink.NLM_F_REQUEST | Netlink.NLM_F_ACK
  798. for flag in flags or []:
  799. nl_flags |= flag
  800. msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
  801. if op.fixed_header:
  802. msg += self._encode_struct(op.fixed_header, vals)
  803. search_attrs = SpaceAttrs(op.attr_set, vals)
  804. for name, value in vals.items():
  805. msg += self._add_attr(op.attr_set.name, name, value, search_attrs)
  806. msg = _genl_msg_finalize(msg)
  807. return msg
  808. def _ops(self, ops):
  809. reqs_by_seq = {}
  810. req_seq = random.randint(1024, 65535)
  811. payload = b''
  812. for (method, vals, flags) in ops:
  813. op = self.ops[method]
  814. msg = self._encode_message(op, vals, flags, req_seq)
  815. reqs_by_seq[req_seq] = (op, msg, flags)
  816. payload += msg
  817. req_seq += 1
  818. self.sock.send(payload, 0)
  819. done = False
  820. rsp = []
  821. op_rsp = []
  822. while not done:
  823. reply = self.sock.recv(self._recv_size)
  824. nms = NlMsgs(reply, attr_space=op.attr_set)
  825. self._recv_dbg_print(reply, nms)
  826. for nl_msg in nms:
  827. if nl_msg.nl_seq in reqs_by_seq:
  828. (op, req_msg, req_flags) = reqs_by_seq[nl_msg.nl_seq]
  829. if nl_msg.extack:
  830. self._decode_extack(req_msg, op, nl_msg.extack)
  831. else:
  832. op = None
  833. req_flags = []
  834. if nl_msg.error:
  835. raise NlError(nl_msg)
  836. if nl_msg.done:
  837. if nl_msg.extack:
  838. print("Netlink warning:")
  839. print(nl_msg)
  840. if Netlink.NLM_F_DUMP in req_flags:
  841. rsp.append(op_rsp)
  842. elif not op_rsp:
  843. rsp.append(None)
  844. elif len(op_rsp) == 1:
  845. rsp.append(op_rsp[0])
  846. else:
  847. rsp.append(op_rsp)
  848. op_rsp = []
  849. del reqs_by_seq[nl_msg.nl_seq]
  850. done = len(reqs_by_seq) == 0
  851. break
  852. decoded = self.nlproto.decode(self, nl_msg, op)
  853. # Check if this is a reply to our request
  854. if nl_msg.nl_seq not in reqs_by_seq or decoded.cmd() != op.rsp_value:
  855. if decoded.cmd() in self.async_msg_ids:
  856. self.handle_ntf(decoded)
  857. continue
  858. else:
  859. print('Unexpected message: ' + repr(decoded))
  860. continue
  861. rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
  862. if op.fixed_header:
  863. rsp_msg.update(self._decode_struct(decoded.raw, op.fixed_header))
  864. op_rsp.append(rsp_msg)
  865. return rsp
  866. def _op(self, method, vals, flags=None, dump=False):
  867. req_flags = flags or []
  868. if dump:
  869. req_flags.append(Netlink.NLM_F_DUMP)
  870. ops = [(method, vals, req_flags)]
  871. return self._ops(ops)[0]
  872. def do(self, method, vals, flags=None):
  873. return self._op(method, vals, flags)
  874. def dump(self, method, vals):
  875. return self._op(method, vals, dump=True)
  876. def do_multi(self, ops):
  877. return self._ops(ops)