def manage_tuntap(msg): if TUNSETIFF is None: raise NetlinkError(errno.EOPNOTSUPP, 'Arch not supported') if msg['header']['type'] != RTM_NEWLINK: raise NetlinkError(errno.EOPNOTSUPP, 'Unsupported event') ifru_flags = 0 linkinfo = msg.get_attr('IFLA_LINKINFO') infodata = linkinfo.get_attr('IFLA_INFO_DATA') flags = infodata.get_attr('IFTUN_IFR', None) if infodata.get_attr('IFTUN_MODE') == 'tun': ifru_flags |= IFT_TUN elif infodata.get_attr('IFTUN_MODE') == 'tap': ifru_flags |= IFT_TAP else: raise ValueError('invalid mode') if flags is not None: if flags['no_pi']: ifru_flags |= IFT_NO_PI if flags['one_queue']: ifru_flags |= IFT_ONE_QUEUE if flags['vnet_hdr']: ifru_flags |= IFT_VNET_HDR if flags['multi_queue']: ifru_flags |= IFT_MULTI_QUEUE ifr = msg.get_attr('IFLA_IFNAME') if len(ifr) > IFNAMSIZ: raise ValueError('ifname too long') ifr += (IFNAMSIZ - len(ifr)) * '\0' ifr = ifr.encode('ascii') ifr += struct.pack('H', ifru_flags) user = infodata.get_attr('IFTUN_UID') group = infodata.get_attr('IFTUN_GID') # fd = os.open(TUNDEV, os.O_RDWR) try: ioctl(fd, TUNSETIFF, ifr) if user is not None: ioctl(fd, TUNSETOWNER, user) if group is not None: ioctl(fd, TUNSETGROUP, group) ioctl(fd, TUNSETPERSIST, 1) except Exception: raise finally: os.close(fd)
def remove(self, path): netnspath = netns._get_netnspath(path) info = None try: info = self.ipr._dump_one_ns(netnspath, set()) except SkipInode: raise NetlinkError(errno.EEXIST) info['header']['type'] = RTM_DELNETNS info['header']['target'] = self.target info['event'] = 'RTM_DELNETNS' del info['value'] try: netns.remove(netnspath, self.libc) except OSError as e: raise NetlinkError(e.errno) return (info,)
def get(self): for msg in super(NetNSManager, self).get(): info = nsinfmsg() if msg is None: info['header']['error'] = NetlinkError(errno.ECONNRESET) info['header']['type'] = RTM_DELNETNS info['header']['target'] = self.target info['event'] = 'RTM_DELNETNS' yield info return path = '{path}/{name}'.format(**msg) info['header']['error'] = None info['header']['target'] = self.target if path not in self.registry: self.update() if path in self.registry: info.load(self.registry[path]) else: info['attrs'] = [('NSINFO_PATH', path)] del info['value'] if msg['mask'] & 0x200: info['header']['type'] = RTM_DELNETNS info['event'] = 'RTM_DELNETNS' elif not msg['mask'] & 0x100: continue yield info
def hook_apply(self, method, **spec): if method == 'set': if self['kind'] == 'bridge': keys = filter(lambda x: x.startswith('br_'), self.changed) if keys: req = { 'index': self['index'], 'kind': 'bridge', 'family': AF_BRIDGE } for key in keys: req[key] = self[key] (self.sources[self['target']].api(self.api, method, **req)) update = (self.sources[self['target']].api( self.api, 'get', **{'index': self['index']})) self.ndb._event_queue.put(update) elif method == 'add': if self['kind'] == 'tun': self.load_sql() self.load_event.wait(0.1) if 'index' not in self: raise NetlinkError(errno.EAGAIN) update = (self.sources[self['target']].api( self.api, 'get', index=self['index'])) self.ndb._event_queue.put(update)
def hook_apply(self, method, **spec): if method == 'set': if self['kind'] == 'bridge': keys = filter(lambda x: x.startswith('br_'), self.changed) if keys: req = { 'index': self['index'], 'kind': 'bridge', 'family': AF_BRIDGE, } for key in keys: req[key] = self[key] self.sources[self['target']].api(self.api, method, **req) # FIXME: make a reasonable shortcut for this self.load_from_system() elif self['kind'] in ip_tunnels and self['state'] == 'down': # force reading attributes for tunnels in the down state self.load_from_system() elif method == 'add': if self['kind'] == 'tun': self.load_sql() self.load_event.wait(0.1) if 'index' not in self: raise NetlinkError(errno.EAGAIN) update = self.sources[self['target']].api(self.api, 'get', index=self['index']) self.ndb._event_queue.put(update)
def create(self, path): netnspath = netns._get_netnspath(path) try: netns.create(netnspath, self.libc) except OSError as e: raise NetlinkError(e.errno) info = self.ipr._dump_one_ns(netnspath, set()) info['header']['type'] = RTM_NEWNETNS info['header']['target'] = self.target info['event'] = 'RTM_NEWNETNS' del info['value'] return (info,)
def parse(self, data, seq=None, callback=None): ''' Parse string data. At this moment all transport, except of the native Netlink is deprecated in this library, so we should not support any defragmentation on that level ''' offset = 0 result = [] # there must be at least one header in the buffer, # 'IHHII' == 16 bytes while offset <= len(data) - 16: # pick type and length (length, ) = struct.unpack_from('I', data, offset) if length == 0: break error = None (msg_type, ) = struct.unpack_from(self.type_format, data, offset + self.type_offset) if msg_type == self.error_type: code = abs(struct.unpack_from('i', data, offset + 16)[0]) if code > 0: error = NetlinkError(code) msg_class = self.msg_map.get(msg_type, nlmsg) msg = msg_class(data, offset=offset) if msg_type in (NLMSG_DONE, NLMSG_ERROR): # get flags flags = struct.unpack_from('H', data, offset + 6)[0] if flags & NLM_F_ACK_TLVS: msg = nlmsgerr(data, offset=offset) try: msg.decode() if isinstance(msg, nlmsgerr): error = NetlinkError(abs(msg['error']), msg.get_attr('NLMSGERR_ATTR_MSG')) msg['header']['error'] = error # try to decode encapsulated error message if error is not None: enc_type = struct.unpack_from('H', data, offset + 24)[0] enc_class = self.msg_map.get(enc_type, nlmsg) enc = enc_class(data, offset=offset + 20) enc.decode() msg['header']['errmsg'] = enc if callback and seq == msg['header']['sequence_number']: if callback(msg): offset += msg.length continue except NetlinkHeaderDecodeError as e: # in the case of header decoding error, # create an empty message msg = nlmsg() msg['header']['error'] = e except NetlinkDecodeError as e: msg['header']['error'] = e mtype = msg['header'].get('type', None) if mtype in (1, 2, 3, 4): msg['event'] = mtypes.get(mtype, 'none') self.fix_message(msg) offset += msg.length result.append(msg) return result