[133] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | # |
---|
[186] | 4 | # Ethernet over WebSocket |
---|
[133] | 5 | # |
---|
| 6 | # depends on: |
---|
[265] | 7 | # - python-2.7.5 |
---|
| 8 | # - python-pytun-2.1 |
---|
[278] | 9 | # - websocket-client-0.14.0 |
---|
[265] | 10 | # - tornado-2.4 |
---|
[133] | 11 | # |
---|
| 12 | # =========================================================================== |
---|
[276] | 13 | # Copyright (c) 2012-2015, Atzm WATANABE <atzm@atzm.org> |
---|
[133] | 14 | # All rights reserved. |
---|
| 15 | # |
---|
| 16 | # Redistribution and use in source and binary forms, with or without |
---|
| 17 | # modification, are permitted provided that the following conditions are met: |
---|
| 18 | # |
---|
| 19 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
| 20 | # this list of conditions and the following disclaimer. |
---|
| 21 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
| 22 | # notice, this list of conditions and the following disclaimer in the |
---|
| 23 | # documentation and/or other materials provided with the distribution. |
---|
| 24 | # |
---|
| 25 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 26 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 27 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 28 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
| 29 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 30 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 31 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 32 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 33 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 34 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 35 | # POSSIBILITY OF SUCH DAMAGE. |
---|
| 36 | # =========================================================================== |
---|
| 37 | # |
---|
| 38 | # $Id$ |
---|
| 39 | |
---|
| 40 | import os |
---|
| 41 | import sys |
---|
[156] | 42 | import ssl |
---|
[160] | 43 | import time |
---|
[183] | 44 | import json |
---|
[175] | 45 | import fcntl |
---|
[150] | 46 | import base64 |
---|
[212] | 47 | import socket |
---|
[256] | 48 | import struct |
---|
[190] | 49 | import urllib2 |
---|
[150] | 50 | import hashlib |
---|
[151] | 51 | import getpass |
---|
[266] | 52 | import operator |
---|
[133] | 53 | import argparse |
---|
| 54 | |
---|
[280] | 55 | import logging |
---|
| 56 | import logging.config |
---|
| 57 | |
---|
[185] | 58 | import tornado |
---|
[133] | 59 | import websocket |
---|
| 60 | |
---|
[183] | 61 | from tornado.web import Application, RequestHandler |
---|
[182] | 62 | from tornado.websocket import WebSocketHandler |
---|
[183] | 63 | from tornado.httpserver import HTTPServer |
---|
| 64 | from tornado.ioloop import IOLoop |
---|
| 65 | |
---|
[166] | 66 | from pytun import TunTapDevice, IFF_TAP, IFF_NO_PI |
---|
[133] | 67 | |
---|
[166] | 68 | |
---|
[164] | 69 | class EthernetFrame(object): |
---|
| 70 | def __init__(self, data): |
---|
| 71 | self.data = data |
---|
| 72 | |
---|
[176] | 73 | @property |
---|
| 74 | def dst_multicast(self): |
---|
| 75 | return ord(self.data[0]) & 1 |
---|
[164] | 76 | |
---|
| 77 | @property |
---|
[176] | 78 | def src_multicast(self): |
---|
| 79 | return ord(self.data[6]) & 1 |
---|
| 80 | |
---|
| 81 | @property |
---|
[164] | 82 | def dst_mac(self): |
---|
| 83 | return self.data[:6] |
---|
| 84 | |
---|
| 85 | @property |
---|
| 86 | def src_mac(self): |
---|
| 87 | return self.data[6:12] |
---|
| 88 | |
---|
| 89 | @property |
---|
| 90 | def tagged(self): |
---|
| 91 | return ord(self.data[12]) == 0x81 and ord(self.data[13]) == 0 |
---|
| 92 | |
---|
| 93 | @property |
---|
| 94 | def vid(self): |
---|
| 95 | if self.tagged: |
---|
| 96 | return ((ord(self.data[14]) << 8) | ord(self.data[15])) & 0x0fff |
---|
[183] | 97 | return 0 |
---|
[164] | 98 | |
---|
[198] | 99 | @staticmethod |
---|
| 100 | def format_mac(mac, sep=':'): |
---|
| 101 | return sep.join(b.encode('hex') for b in mac) |
---|
[164] | 102 | |
---|
[198] | 103 | |
---|
[280] | 104 | class FDB(object): |
---|
[196] | 105 | class Entry(object): |
---|
[195] | 106 | def __init__(self, port, ageout): |
---|
| 107 | self.port = port |
---|
| 108 | self._time = time.time() |
---|
| 109 | self._ageout = ageout |
---|
| 110 | |
---|
| 111 | @property |
---|
| 112 | def age(self): |
---|
| 113 | return time.time() - self._time |
---|
| 114 | |
---|
| 115 | @property |
---|
| 116 | def agedout(self): |
---|
| 117 | return self.age > self._ageout |
---|
| 118 | |
---|
[280] | 119 | def __init__(self, ageout, logger, debug): |
---|
[164] | 120 | self._ageout = ageout |
---|
[280] | 121 | self._logger = logger |
---|
[164] | 122 | self._debug = debug |
---|
[195] | 123 | self._table = {} |
---|
[164] | 124 | |
---|
[195] | 125 | def _set_entry(self, vid, mac, port): |
---|
| 126 | if vid not in self._table: |
---|
| 127 | self._table[vid] = {} |
---|
[196] | 128 | self._table[vid][mac] = self.Entry(port, self._ageout) |
---|
[164] | 129 | |
---|
[195] | 130 | def _del_entry(self, vid, mac): |
---|
| 131 | if vid in self._table: |
---|
| 132 | if mac in self._table[vid]: |
---|
| 133 | del self._table[vid][mac] |
---|
| 134 | if not self._table[vid]: |
---|
| 135 | del self._table[vid] |
---|
[164] | 136 | |
---|
[207] | 137 | def _get_entry(self, vid, mac): |
---|
[195] | 138 | try: |
---|
| 139 | entry = self._table[vid][mac] |
---|
| 140 | except KeyError: |
---|
[164] | 141 | return None |
---|
| 142 | |
---|
[195] | 143 | if not entry.agedout: |
---|
| 144 | return entry |
---|
[164] | 145 | |
---|
[195] | 146 | self._del_entry(vid, mac) |
---|
[164] | 147 | |
---|
[280] | 148 | if self._debug: |
---|
| 149 | self._logger.debug('fdb aged out: port:%d; vid:%d; mac:%s', |
---|
| 150 | entry.port.number, vid, mac.encode('hex')) |
---|
| 151 | |
---|
[208] | 152 | def each(self): |
---|
[207] | 153 | for vid in sorted(self._table.iterkeys()): |
---|
| 154 | for mac in sorted(self._table[vid].iterkeys()): |
---|
| 155 | entry = self._get_entry(vid, mac) |
---|
| 156 | if entry: |
---|
| 157 | yield (vid, mac, entry) |
---|
[195] | 158 | |
---|
| 159 | def lookup(self, frame): |
---|
| 160 | mac = frame.dst_mac |
---|
| 161 | vid = frame.vid |
---|
[207] | 162 | entry = self._get_entry(vid, mac) |
---|
[195] | 163 | return getattr(entry, 'port', None) |
---|
| 164 | |
---|
[166] | 165 | def learn(self, port, frame): |
---|
| 166 | mac = frame.src_mac |
---|
| 167 | vid = frame.vid |
---|
[195] | 168 | self._set_entry(vid, mac, port) |
---|
[166] | 169 | |
---|
[280] | 170 | if self._debug: |
---|
| 171 | self._logger.debug('fdb learned: port:%d; vid:%d; mac:%s', |
---|
| 172 | port.number, vid, mac.encode('hex')) |
---|
| 173 | |
---|
[164] | 174 | def delete(self, port): |
---|
[208] | 175 | for vid, mac, entry in self.each(): |
---|
[207] | 176 | if entry.port.number == port.number: |
---|
| 177 | self._del_entry(vid, mac) |
---|
[164] | 178 | |
---|
[280] | 179 | if self._debug: |
---|
| 180 | self._logger.debug('fdb deleted: port:%d; vid:%d; mac:%s', |
---|
| 181 | port.number, vid, mac.encode('hex')) |
---|
[164] | 182 | |
---|
[280] | 183 | |
---|
| 184 | class SwitchingHub(object): |
---|
[195] | 185 | class Port(object): |
---|
| 186 | def __init__(self, number, interface): |
---|
| 187 | self.number = number |
---|
| 188 | self.interface = interface |
---|
| 189 | self.tx = 0 |
---|
| 190 | self.rx = 0 |
---|
| 191 | self.shut = False |
---|
[183] | 192 | |
---|
[280] | 193 | def __init__(self, fdb, logger, debug): |
---|
[197] | 194 | self.fdb = fdb |
---|
[280] | 195 | self._logger = logger |
---|
[133] | 196 | self._debug = debug |
---|
[183] | 197 | self._table = {} |
---|
| 198 | self._next = 1 |
---|
[133] | 199 | |
---|
[183] | 200 | @property |
---|
| 201 | def portlist(self): |
---|
[266] | 202 | return sorted(self._table.itervalues(), |
---|
| 203 | key=operator.attrgetter('number')) |
---|
[133] | 204 | |
---|
[183] | 205 | def get_port(self, portnum): |
---|
| 206 | return self._table[portnum] |
---|
| 207 | |
---|
| 208 | def register_port(self, interface): |
---|
[186] | 209 | try: |
---|
[187] | 210 | self._set_privattr('portnum', interface, self._next) # XXX |
---|
[195] | 211 | self._table[self._next] = self.Port(self._next, interface) |
---|
[186] | 212 | return self._next |
---|
| 213 | finally: |
---|
| 214 | self._next += 1 |
---|
[183] | 215 | |
---|
| 216 | def unregister_port(self, interface): |
---|
[187] | 217 | portnum = self._get_privattr('portnum', interface) |
---|
| 218 | self._del_privattr('portnum', interface) |
---|
[197] | 219 | self.fdb.delete(self._table[portnum]) |
---|
[187] | 220 | del self._table[portnum] |
---|
[183] | 221 | |
---|
| 222 | def send(self, dst_interfaces, frame): |
---|
[187] | 223 | portnums = (self._get_privattr('portnum', i) for i in dst_interfaces) |
---|
| 224 | ports = (self._table[n] for n in portnums) |
---|
| 225 | ports = (p for p in ports if not p.shut) |
---|
[266] | 226 | ports = sorted(ports, key=operator.attrgetter('number')) |
---|
[183] | 227 | |
---|
| 228 | for p in ports: |
---|
| 229 | p.interface.write_message(frame.data, True) |
---|
| 230 | p.tx += 1 |
---|
| 231 | |
---|
[280] | 232 | if self._debug and ports: |
---|
| 233 | self._logger.debug('sent: port:%s; vid:%d; %s -> %s', |
---|
| 234 | ','.join(str(p.number) for p in ports), |
---|
| 235 | frame.vid, |
---|
| 236 | frame.src_mac.encode('hex'), |
---|
| 237 | frame.dst_mac.encode('hex')) |
---|
[183] | 238 | |
---|
| 239 | def receive(self, src_interface, frame): |
---|
[187] | 240 | port = self._table[self._get_privattr('portnum', src_interface)] |
---|
[183] | 241 | |
---|
| 242 | if not port.shut: |
---|
| 243 | port.rx += 1 |
---|
| 244 | self._forward(port, frame) |
---|
| 245 | |
---|
| 246 | def _forward(self, src_port, frame): |
---|
[166] | 247 | try: |
---|
[176] | 248 | if not frame.src_multicast: |
---|
[197] | 249 | self.fdb.learn(src_port, frame) |
---|
[133] | 250 | |
---|
[176] | 251 | if not frame.dst_multicast: |
---|
[197] | 252 | dst_port = self.fdb.lookup(frame) |
---|
[164] | 253 | |
---|
[166] | 254 | if dst_port: |
---|
[183] | 255 | self.send([dst_port.interface], frame) |
---|
[166] | 256 | return |
---|
[133] | 257 | |
---|
[187] | 258 | ports = set(self.portlist) - set([src_port]) |
---|
[183] | 259 | self.send((p.interface for p in ports), frame) |
---|
[162] | 260 | |
---|
[166] | 261 | except: # ex. received invalid frame |
---|
[280] | 262 | self._logger.exception( |
---|
| 263 | 'caught error while forwarding frame: port:%d; frame:%s', |
---|
| 264 | src_port.number, frame.data.encode('hex')) |
---|
[133] | 265 | |
---|
[187] | 266 | def _privattr(self, name): |
---|
[280] | 267 | return '_%s_%s_%s' % (type(self).__name__, id(self), name) |
---|
[164] | 268 | |
---|
[187] | 269 | def _set_privattr(self, name, obj, value): |
---|
| 270 | return setattr(obj, self._privattr(name), value) |
---|
| 271 | |
---|
| 272 | def _get_privattr(self, name, obj, defaults=None): |
---|
| 273 | return getattr(obj, self._privattr(name), defaults) |
---|
| 274 | |
---|
| 275 | def _del_privattr(self, name, obj): |
---|
| 276 | return delattr(obj, self._privattr(name)) |
---|
| 277 | |
---|
| 278 | |
---|
[256] | 279 | class NetworkInterface(object): |
---|
| 280 | SIOCGIFADDR = 0x8915 # from <linux/sockios.h> |
---|
| 281 | SIOCSIFADDR = 0x8916 |
---|
| 282 | SIOCGIFNETMASK = 0x891b |
---|
| 283 | SIOCSIFNETMASK = 0x891c |
---|
| 284 | SIOCGIFMTU = 0x8921 |
---|
| 285 | SIOCSIFMTU = 0x8922 |
---|
| 286 | |
---|
| 287 | def __init__(self, ifname): |
---|
| 288 | self._ifname = struct.pack('16s', str(ifname)[:15]) |
---|
| 289 | |
---|
| 290 | def _ioctl(self, req, data): |
---|
| 291 | try: |
---|
| 292 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 293 | return fcntl.ioctl(sock.fileno(), req, data) |
---|
| 294 | finally: |
---|
| 295 | sock.close() |
---|
| 296 | |
---|
| 297 | @property |
---|
| 298 | def address(self): |
---|
| 299 | try: |
---|
| 300 | data = struct.pack('16s18x', self._ifname) |
---|
| 301 | ret = self._ioctl(self.SIOCGIFADDR, data) |
---|
| 302 | return socket.inet_ntoa(ret[20:24]) |
---|
| 303 | except IOError: |
---|
| 304 | return '' |
---|
| 305 | |
---|
| 306 | @property |
---|
| 307 | def netmask(self): |
---|
| 308 | try: |
---|
| 309 | data = struct.pack('16s18x', self._ifname) |
---|
| 310 | ret = self._ioctl(self.SIOCGIFNETMASK, data) |
---|
| 311 | return socket.inet_ntoa(ret[20:24]) |
---|
| 312 | except IOError: |
---|
| 313 | return '' |
---|
| 314 | |
---|
| 315 | @property |
---|
| 316 | def mtu(self): |
---|
| 317 | try: |
---|
| 318 | data = struct.pack('16s18x', self._ifname) |
---|
| 319 | ret = self._ioctl(self.SIOCGIFMTU, data) |
---|
| 320 | return struct.unpack('i', ret[16:20])[0] |
---|
| 321 | except IOError: |
---|
| 322 | return 0 |
---|
| 323 | |
---|
| 324 | @address.setter |
---|
| 325 | def address(self, addr): |
---|
| 326 | data = struct.pack('16si4s10x', self._ifname, socket.AF_INET, |
---|
| 327 | socket.inet_aton(addr)) |
---|
| 328 | self._ioctl(self.SIOCSIFADDR, data) |
---|
| 329 | |
---|
| 330 | @netmask.setter |
---|
| 331 | def netmask(self, addr): |
---|
| 332 | data = struct.pack('16si4s10x', self._ifname, socket.AF_INET, |
---|
| 333 | socket.inet_aton(addr)) |
---|
| 334 | self._ioctl(self.SIOCSIFNETMASK, data) |
---|
| 335 | |
---|
| 336 | @mtu.setter |
---|
| 337 | def mtu(self, mtu): |
---|
| 338 | data = struct.pack('16si12x', self._ifname, mtu) |
---|
| 339 | self._ioctl(self.SIOCSIFMTU, data) |
---|
| 340 | |
---|
| 341 | |
---|
[179] | 342 | class Htpasswd(object): |
---|
| 343 | def __init__(self, path): |
---|
| 344 | self._path = path |
---|
| 345 | self._stat = None |
---|
| 346 | self._data = {} |
---|
| 347 | |
---|
| 348 | def auth(self, name, passwd): |
---|
| 349 | passwd = base64.b64encode(hashlib.sha1(passwd).digest()) |
---|
| 350 | return self._data.get(name) == passwd |
---|
| 351 | |
---|
| 352 | def load(self): |
---|
| 353 | old_stat = self._stat |
---|
| 354 | |
---|
| 355 | with open(self._path) as fp: |
---|
| 356 | fileno = fp.fileno() |
---|
| 357 | fcntl.flock(fileno, fcntl.LOCK_SH | fcntl.LOCK_NB) |
---|
| 358 | self._stat = os.fstat(fileno) |
---|
| 359 | |
---|
| 360 | unchanged = old_stat and \ |
---|
[267] | 361 | old_stat.st_ino == self._stat.st_ino and \ |
---|
| 362 | old_stat.st_dev == self._stat.st_dev and \ |
---|
| 363 | old_stat.st_mtime == self._stat.st_mtime |
---|
[179] | 364 | |
---|
| 365 | if not unchanged: |
---|
| 366 | self._data = self._parse(fp) |
---|
| 367 | |
---|
| 368 | return self |
---|
| 369 | |
---|
| 370 | def _parse(self, fp): |
---|
| 371 | data = {} |
---|
| 372 | for line in fp: |
---|
| 373 | line = line.strip() |
---|
| 374 | if 0 <= line.find(':'): |
---|
| 375 | name, passwd = line.split(':', 1) |
---|
| 376 | if passwd.startswith('{SHA}'): |
---|
| 377 | data[name] = passwd[5:] |
---|
| 378 | return data |
---|
| 379 | |
---|
| 380 | |
---|
[182] | 381 | class BasicAuthMixIn(object): |
---|
| 382 | def _execute(self, transforms, *args, **kwargs): |
---|
| 383 | def do_execute(): |
---|
| 384 | sp = super(BasicAuthMixIn, self) |
---|
| 385 | return sp._execute(transforms, *args, **kwargs) |
---|
| 386 | |
---|
| 387 | def auth_required(): |
---|
[185] | 388 | stream = getattr(self, 'stream', self.request.connection.stream) |
---|
| 389 | stream.write(tornado.escape.utf8( |
---|
[182] | 390 | 'HTTP/1.1 401 Authorization Required\r\n' |
---|
| 391 | 'WWW-Authenticate: Basic realm=etherws\r\n\r\n' |
---|
| 392 | )) |
---|
[185] | 393 | stream.close() |
---|
[182] | 394 | |
---|
| 395 | try: |
---|
| 396 | if not self._htpasswd: |
---|
| 397 | return do_execute() |
---|
| 398 | |
---|
| 399 | creds = self.request.headers.get('Authorization') |
---|
| 400 | |
---|
| 401 | if not creds or not creds.startswith('Basic '): |
---|
| 402 | return auth_required() |
---|
| 403 | |
---|
| 404 | name, passwd = base64.b64decode(creds[6:]).split(':', 1) |
---|
| 405 | |
---|
| 406 | if self._htpasswd.load().auth(name, passwd): |
---|
| 407 | return do_execute() |
---|
| 408 | except: |
---|
[280] | 409 | self._logger.exception('caught error while doing authorization') |
---|
[182] | 410 | |
---|
| 411 | return auth_required() |
---|
| 412 | |
---|
| 413 | |
---|
[280] | 414 | class ServerHandler(BasicAuthMixIn, WebSocketHandler): |
---|
[191] | 415 | IFTYPE = 'server' |
---|
[253] | 416 | IFOP_ALLOWED = False |
---|
[191] | 417 | |
---|
[287] | 418 | def __init__(self, app, req, switch, htpasswd, noorigin, logger, debug): |
---|
[253] | 419 | super(ServerHandler, self).__init__(app, req) |
---|
[186] | 420 | self._switch = switch |
---|
| 421 | self._htpasswd = htpasswd |
---|
[287] | 422 | self._noorigin = noorigin |
---|
[280] | 423 | self._logger = logger |
---|
[186] | 424 | self._debug = debug |
---|
| 425 | |
---|
[203] | 426 | @property |
---|
| 427 | def target(self): |
---|
[276] | 428 | conn = self.request.connection |
---|
| 429 | if hasattr(conn, 'address'): |
---|
[280] | 430 | return ':'.join(str(e) for e in conn.address[:2]) |
---|
[276] | 431 | if hasattr(conn, 'context'): |
---|
[280] | 432 | return ':'.join(str(e) for e in conn.context.address[:2]) |
---|
[276] | 433 | return str(conn) |
---|
[186] | 434 | |
---|
| 435 | def open(self): |
---|
| 436 | try: |
---|
| 437 | return self._switch.register_port(self) |
---|
| 438 | finally: |
---|
[280] | 439 | self._logger.info('connected: %s', self.request.remote_ip) |
---|
[186] | 440 | |
---|
| 441 | def on_message(self, message): |
---|
| 442 | self._switch.receive(self, EthernetFrame(message)) |
---|
| 443 | |
---|
| 444 | def on_close(self): |
---|
| 445 | self._switch.unregister_port(self) |
---|
[280] | 446 | self._logger.info('disconnected: %s', self.request.remote_ip) |
---|
[186] | 447 | |
---|
[287] | 448 | def check_origin(self, origin): |
---|
| 449 | if self._noorigin: |
---|
| 450 | return True |
---|
| 451 | return super(ServerHandler, self).check_origin(origin) |
---|
[186] | 452 | |
---|
[287] | 453 | |
---|
[280] | 454 | class BaseClientHandler(object): |
---|
[253] | 455 | IFTYPE = 'baseclient' |
---|
| 456 | IFOP_ALLOWED = False |
---|
[251] | 457 | |
---|
[280] | 458 | def __init__(self, ioloop, switch, target, logger, debug, *args, **kwargs): |
---|
[251] | 459 | self._ioloop = ioloop |
---|
| 460 | self._switch = switch |
---|
[253] | 461 | self._target = target |
---|
[280] | 462 | self._logger = logger |
---|
[251] | 463 | self._debug = debug |
---|
[253] | 464 | self._args = args |
---|
| 465 | self._kwargs = kwargs |
---|
| 466 | self._device = None |
---|
[251] | 467 | |
---|
| 468 | @property |
---|
[253] | 469 | def address(self): |
---|
| 470 | raise NotImplementedError('unsupported') |
---|
| 471 | |
---|
| 472 | @property |
---|
| 473 | def netmask(self): |
---|
| 474 | raise NotImplementedError('unsupported') |
---|
| 475 | |
---|
| 476 | @property |
---|
| 477 | def mtu(self): |
---|
| 478 | raise NotImplementedError('unsupported') |
---|
| 479 | |
---|
| 480 | @address.setter |
---|
| 481 | def address(self, address): |
---|
| 482 | raise NotImplementedError('unsupported') |
---|
| 483 | |
---|
| 484 | @netmask.setter |
---|
| 485 | def netmask(self, netmask): |
---|
| 486 | raise NotImplementedError('unsupported') |
---|
| 487 | |
---|
| 488 | @mtu.setter |
---|
| 489 | def mtu(self, mtu): |
---|
| 490 | raise NotImplementedError('unsupported') |
---|
| 491 | |
---|
| 492 | def open(self): |
---|
| 493 | raise NotImplementedError('unsupported') |
---|
| 494 | |
---|
| 495 | def write_message(self, message, binary=False): |
---|
| 496 | raise NotImplementedError('unsupported') |
---|
| 497 | |
---|
| 498 | def read(self): |
---|
| 499 | raise NotImplementedError('unsupported') |
---|
| 500 | |
---|
| 501 | @property |
---|
[251] | 502 | def target(self): |
---|
[253] | 503 | return self._target |
---|
[251] | 504 | |
---|
| 505 | @property |
---|
[280] | 506 | def logger(self): |
---|
| 507 | return self._logger |
---|
| 508 | |
---|
| 509 | @property |
---|
[253] | 510 | def device(self): |
---|
| 511 | return self._device |
---|
| 512 | |
---|
| 513 | @property |
---|
[251] | 514 | def closed(self): |
---|
[253] | 515 | return not self.device |
---|
[251] | 516 | |
---|
[253] | 517 | def close(self): |
---|
| 518 | if self.closed: |
---|
| 519 | raise ValueError('I/O operation on closed %s' % self.IFTYPE) |
---|
| 520 | self.leave_switch() |
---|
| 521 | self.unregister_device() |
---|
[280] | 522 | self.logger.info('disconnected: %s', self.target) |
---|
[253] | 523 | |
---|
| 524 | def register_device(self, device): |
---|
| 525 | self._device = device |
---|
| 526 | |
---|
| 527 | def unregister_device(self): |
---|
| 528 | self._device.close() |
---|
| 529 | self._device = None |
---|
| 530 | |
---|
| 531 | def fileno(self): |
---|
| 532 | if self.closed: |
---|
| 533 | raise ValueError('I/O operation on closed %s' % self.IFTYPE) |
---|
| 534 | return self.device.fileno() |
---|
| 535 | |
---|
| 536 | def __call__(self, fd, events): |
---|
| 537 | try: |
---|
| 538 | data = self.read() |
---|
| 539 | if data is not None: |
---|
| 540 | self._switch.receive(self, EthernetFrame(data)) |
---|
| 541 | return |
---|
| 542 | except: |
---|
[280] | 543 | self.logger.exception('caught error while receiving frame') |
---|
[253] | 544 | self.close() |
---|
| 545 | |
---|
| 546 | def join_switch(self): |
---|
| 547 | self._ioloop.add_handler(self.fileno(), self, self._ioloop.READ) |
---|
| 548 | return self._switch.register_port(self) |
---|
| 549 | |
---|
| 550 | def leave_switch(self): |
---|
| 551 | self._switch.unregister_port(self) |
---|
| 552 | self._ioloop.remove_handler(self.fileno()) |
---|
| 553 | |
---|
| 554 | |
---|
| 555 | class NetdevHandler(BaseClientHandler): |
---|
| 556 | IFTYPE = 'netdev' |
---|
| 557 | IFOP_ALLOWED = True |
---|
| 558 | ETH_P_ALL = 0x0003 # from <linux/if_ether.h> |
---|
| 559 | |
---|
[252] | 560 | @property |
---|
| 561 | def address(self): |
---|
| 562 | if self.closed: |
---|
| 563 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 564 | return NetworkInterface(self.target).address |
---|
[252] | 565 | |
---|
| 566 | @property |
---|
| 567 | def netmask(self): |
---|
| 568 | if self.closed: |
---|
| 569 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 570 | return NetworkInterface(self.target).netmask |
---|
[252] | 571 | |
---|
| 572 | @property |
---|
| 573 | def mtu(self): |
---|
| 574 | if self.closed: |
---|
| 575 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 576 | return NetworkInterface(self.target).mtu |
---|
[252] | 577 | |
---|
| 578 | @address.setter |
---|
| 579 | def address(self, address): |
---|
| 580 | if self.closed: |
---|
| 581 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 582 | NetworkInterface(self.target).address = address |
---|
[252] | 583 | |
---|
| 584 | @netmask.setter |
---|
| 585 | def netmask(self, netmask): |
---|
| 586 | if self.closed: |
---|
| 587 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 588 | NetworkInterface(self.target).netmask = netmask |
---|
[252] | 589 | |
---|
| 590 | @mtu.setter |
---|
| 591 | def mtu(self, mtu): |
---|
| 592 | if self.closed: |
---|
| 593 | raise ValueError('I/O operation on closed netdev') |
---|
[256] | 594 | NetworkInterface(self.target).mtu = mtu |
---|
[252] | 595 | |
---|
[251] | 596 | def open(self): |
---|
| 597 | if not self.closed: |
---|
| 598 | raise ValueError('Already opened') |
---|
[253] | 599 | self.register_device(socket.socket( |
---|
| 600 | socket.PF_PACKET, socket.SOCK_RAW, socket.htons(self.ETH_P_ALL))) |
---|
| 601 | self.device.bind((self.target, self.ETH_P_ALL)) |
---|
[280] | 602 | self.logger.info('connected: %s', self.target) |
---|
[253] | 603 | return self.join_switch() |
---|
[251] | 604 | |
---|
| 605 | def write_message(self, message, binary=False): |
---|
| 606 | if self.closed: |
---|
| 607 | raise ValueError('I/O operation on closed netdev') |
---|
[253] | 608 | self.device.sendall(message) |
---|
[251] | 609 | |
---|
[253] | 610 | def read(self): |
---|
[251] | 611 | if self.closed: |
---|
| 612 | raise ValueError('I/O operation on closed netdev') |
---|
| 613 | buf = [] |
---|
| 614 | while True: |
---|
[253] | 615 | buf.append(self.device.recv(65535)) |
---|
| 616 | if len(buf[-1]) < 65535: |
---|
[251] | 617 | break |
---|
| 618 | return ''.join(buf) |
---|
| 619 | |
---|
| 620 | |
---|
[253] | 621 | class TapHandler(BaseClientHandler): |
---|
[191] | 622 | IFTYPE = 'tap' |
---|
[253] | 623 | IFOP_ALLOWED = True |
---|
[166] | 624 | |
---|
[203] | 625 | @property |
---|
[212] | 626 | def address(self): |
---|
| 627 | if self.closed: |
---|
| 628 | raise ValueError('I/O operation on closed tap') |
---|
| 629 | try: |
---|
[253] | 630 | return self.device.addr |
---|
[212] | 631 | except: |
---|
| 632 | return '' |
---|
| 633 | |
---|
| 634 | @property |
---|
| 635 | def netmask(self): |
---|
| 636 | if self.closed: |
---|
| 637 | raise ValueError('I/O operation on closed tap') |
---|
| 638 | try: |
---|
[253] | 639 | return self.device.netmask |
---|
[212] | 640 | except: |
---|
| 641 | return '' |
---|
| 642 | |
---|
| 643 | @property |
---|
| 644 | def mtu(self): |
---|
| 645 | if self.closed: |
---|
| 646 | raise ValueError('I/O operation on closed tap') |
---|
[253] | 647 | return self.device.mtu |
---|
[212] | 648 | |
---|
| 649 | @address.setter |
---|
| 650 | def address(self, address): |
---|
| 651 | if self.closed: |
---|
| 652 | raise ValueError('I/O operation on closed tap') |
---|
[253] | 653 | self.device.addr = address |
---|
[212] | 654 | |
---|
| 655 | @netmask.setter |
---|
| 656 | def netmask(self, netmask): |
---|
| 657 | if self.closed: |
---|
| 658 | raise ValueError('I/O operation on closed tap') |
---|
[253] | 659 | self.device.netmask = netmask |
---|
[212] | 660 | |
---|
| 661 | @mtu.setter |
---|
| 662 | def mtu(self, mtu): |
---|
| 663 | if self.closed: |
---|
| 664 | raise ValueError('I/O operation on closed tap') |
---|
[253] | 665 | self.device.mtu = mtu |
---|
[212] | 666 | |
---|
[253] | 667 | @property |
---|
| 668 | def target(self): |
---|
| 669 | if self.closed: |
---|
| 670 | return self._target |
---|
| 671 | return self.device.name |
---|
| 672 | |
---|
[166] | 673 | def open(self): |
---|
| 674 | if not self.closed: |
---|
[202] | 675 | raise ValueError('Already opened') |
---|
[253] | 676 | self.register_device(TunTapDevice(self.target, IFF_TAP | IFF_NO_PI)) |
---|
| 677 | self.device.up() |
---|
[280] | 678 | self.logger.info('connected: %s', self.target) |
---|
[253] | 679 | return self.join_switch() |
---|
[166] | 680 | |
---|
| 681 | def write_message(self, message, binary=False): |
---|
| 682 | if self.closed: |
---|
| 683 | raise ValueError('I/O operation on closed tap') |
---|
[253] | 684 | self.device.write(message) |
---|
[166] | 685 | |
---|
[253] | 686 | def read(self): |
---|
[166] | 687 | if self.closed: |
---|
| 688 | raise ValueError('I/O operation on closed tap') |
---|
[162] | 689 | buf = [] |
---|
| 690 | while True: |
---|
[253] | 691 | buf.append(self.device.read(65535)) |
---|
| 692 | if len(buf[-1]) < 65535: |
---|
[162] | 693 | break |
---|
[166] | 694 | return ''.join(buf) |
---|
[162] | 695 | |
---|
| 696 | |
---|
[253] | 697 | class ClientHandler(BaseClientHandler): |
---|
[191] | 698 | IFTYPE = 'client' |
---|
[253] | 699 | IFOP_ALLOWED = False |
---|
[191] | 700 | |
---|
[253] | 701 | def __init__(self, *args, **kwargs): |
---|
| 702 | super(ClientHandler, self).__init__(*args, **kwargs) |
---|
[281] | 703 | self._sslopt = self._kwargs.get('sslopt', {}) |
---|
| 704 | self._options = dict(self.authopts, **self.proxyopts) |
---|
[253] | 705 | |
---|
[151] | 706 | def open(self): |
---|
[160] | 707 | if not self.closed: |
---|
[202] | 708 | raise websocket.WebSocketException('Already opened') |
---|
[151] | 709 | |
---|
[251] | 710 | # XXX: may be blocked |
---|
[265] | 711 | self.register_device(websocket.WebSocket(sslopt=self._sslopt)) |
---|
| 712 | self.device.connect(self.target, **self._options) |
---|
[280] | 713 | self.logger.info('connected: %s', self.target) |
---|
[265] | 714 | return self.join_switch() |
---|
[181] | 715 | |
---|
[151] | 716 | def write_message(self, message, binary=False): |
---|
[160] | 717 | if self.closed: |
---|
[202] | 718 | raise websocket.WebSocketException('Closed socket') |
---|
[151] | 719 | if binary: |
---|
| 720 | flag = websocket.ABNF.OPCODE_BINARY |
---|
[160] | 721 | else: |
---|
| 722 | flag = websocket.ABNF.OPCODE_TEXT |
---|
[253] | 723 | self.device.send(message, flag) |
---|
[151] | 724 | |
---|
[253] | 725 | def read(self): |
---|
| 726 | if self.closed: |
---|
| 727 | raise websocket.WebSocketException('Closed socket') |
---|
| 728 | return self.device.recv() |
---|
[151] | 729 | |
---|
[281] | 730 | @property |
---|
| 731 | def authopts(self): |
---|
| 732 | cred = self._kwargs.get('cred') |
---|
| 733 | |
---|
| 734 | if not isinstance(cred, dict): |
---|
| 735 | return {} |
---|
[287] | 736 | if cred.get('user') is None: |
---|
[281] | 737 | return {} |
---|
[287] | 738 | if cred.get('passwd') is None: |
---|
[281] | 739 | return {} |
---|
| 740 | |
---|
| 741 | token = base64.b64encode('%s:%s' % (cred['user'], cred['passwd'])) |
---|
| 742 | return {'header': ['Authorization: Basic %s' % token]} |
---|
| 743 | |
---|
| 744 | @property |
---|
| 745 | def proxyopts(self): |
---|
[277] | 746 | scheme, remain = urllib2.splittype(self.target) |
---|
[151] | 747 | |
---|
[277] | 748 | proxy = urllib2.getproxies().get(scheme.replace('ws', 'http')) |
---|
| 749 | if not proxy: |
---|
| 750 | return {} |
---|
| 751 | |
---|
| 752 | host = urllib2.splitport(urllib2.splithost(remain)[0])[0] |
---|
| 753 | if urllib2.proxy_bypass(host): |
---|
| 754 | return {} |
---|
| 755 | |
---|
| 756 | hostport = urllib2.splithost(urllib2.splittype(proxy)[1])[0] |
---|
| 757 | host, port = urllib2.splitport(hostport) |
---|
| 758 | port = int(port) if port else 0 |
---|
| 759 | return {'http_proxy_host': host, 'http_proxy_port': port} |
---|
| 760 | |
---|
| 761 | |
---|
[280] | 762 | class ControlServerHandler(BasicAuthMixIn, RequestHandler): |
---|
[183] | 763 | NAMESPACE = 'etherws.control' |
---|
[191] | 764 | IFTYPES = { |
---|
[253] | 765 | NetdevHandler.IFTYPE: NetdevHandler, |
---|
| 766 | TapHandler.IFTYPE: TapHandler, |
---|
| 767 | ClientHandler.IFTYPE: ClientHandler, |
---|
[186] | 768 | } |
---|
[183] | 769 | |
---|
[280] | 770 | def __init__(self, app, req, ioloop, switch, htpasswd, logger, debug): |
---|
[253] | 771 | super(ControlServerHandler, self).__init__(app, req) |
---|
[183] | 772 | self._ioloop = ioloop |
---|
| 773 | self._switch = switch |
---|
| 774 | self._htpasswd = htpasswd |
---|
[280] | 775 | self._logger = logger |
---|
[183] | 776 | self._debug = debug |
---|
| 777 | |
---|
| 778 | def post(self): |
---|
[202] | 779 | try: |
---|
| 780 | request = json.loads(self.request.body) |
---|
| 781 | except Exception as e: |
---|
| 782 | return self._jsonrpc_response(error={ |
---|
| 783 | 'code': 0 - 32700, |
---|
| 784 | 'message': 'Parse error', |
---|
[280] | 785 | 'data': '%s: %s' % (type(e).__name__, str(e)), |
---|
[202] | 786 | }) |
---|
[183] | 787 | |
---|
| 788 | try: |
---|
[202] | 789 | id_ = request.get('id') |
---|
| 790 | params = request.get('params') |
---|
| 791 | version = request['jsonrpc'] |
---|
| 792 | method = request['method'] |
---|
| 793 | if version != '2.0': |
---|
| 794 | raise ValueError('Invalid JSON-RPC version: %s' % version) |
---|
| 795 | except Exception as e: |
---|
| 796 | return self._jsonrpc_response(id_=id_, error={ |
---|
| 797 | 'code': 0 - 32600, |
---|
| 798 | 'message': 'Invalid Request', |
---|
[280] | 799 | 'data': '%s: %s' % (type(e).__name__, str(e)), |
---|
[202] | 800 | }) |
---|
[183] | 801 | |
---|
[202] | 802 | try: |
---|
[183] | 803 | if not method.startswith(self.NAMESPACE + '.'): |
---|
[202] | 804 | raise ValueError('Invalid method namespace: %s' % method) |
---|
[183] | 805 | handler = 'handle_' + method[len(self.NAMESPACE) + 1:] |
---|
[202] | 806 | handler = getattr(self, handler) |
---|
| 807 | except Exception as e: |
---|
| 808 | return self._jsonrpc_response(id_=id_, error={ |
---|
| 809 | 'code': 0 - 32601, |
---|
| 810 | 'message': 'Method not found', |
---|
[280] | 811 | 'data': '%s: %s' % (type(e).__name__, str(e)), |
---|
[202] | 812 | }) |
---|
[183] | 813 | |
---|
[202] | 814 | try: |
---|
| 815 | return self._jsonrpc_response(id_=id_, result=handler(params)) |
---|
[183] | 816 | except Exception as e: |
---|
[280] | 817 | self._logger.exception('caught error while processing request') |
---|
[202] | 818 | return self._jsonrpc_response(id_=id_, error={ |
---|
| 819 | 'code': 0 - 32602, |
---|
| 820 | 'message': 'Invalid params', |
---|
[280] | 821 | 'data': '%s: %s' % (type(e).__name__, str(e)), |
---|
[202] | 822 | }) |
---|
[183] | 823 | |
---|
[198] | 824 | def handle_listFdb(self, params): |
---|
| 825 | list_ = [] |
---|
[208] | 826 | for vid, mac, entry in self._switch.fdb.each(): |
---|
[207] | 827 | list_.append({ |
---|
| 828 | 'vid': vid, |
---|
| 829 | 'mac': EthernetFrame.format_mac(mac), |
---|
| 830 | 'port': entry.port.number, |
---|
| 831 | 'age': int(entry.age), |
---|
| 832 | }) |
---|
[199] | 833 | return {'entries': list_} |
---|
[198] | 834 | |
---|
[183] | 835 | def handle_listPort(self, params): |
---|
[202] | 836 | return {'entries': [self._portstat(p) for p in self._switch.portlist]} |
---|
[183] | 837 | |
---|
| 838 | def handle_addPort(self, params): |
---|
[202] | 839 | type_ = params['type'] |
---|
| 840 | target = params['target'] |
---|
[253] | 841 | opt = getattr(self, '_optparse_' + type_)(params.get('options', {})) |
---|
[202] | 842 | cls = self.IFTYPES[type_] |
---|
[280] | 843 | interface = cls(self._ioloop, self._switch, target, |
---|
| 844 | self._logger, self._debug, **opt) |
---|
[202] | 845 | portnum = interface.open() |
---|
| 846 | return {'entries': [self._portstat(self._switch.get_port(portnum))]} |
---|
[183] | 847 | |
---|
[211] | 848 | def handle_setPort(self, params): |
---|
[202] | 849 | port = self._switch.get_port(int(params['port'])) |
---|
[211] | 850 | shut = params.get('shut') |
---|
| 851 | if shut is not None: |
---|
| 852 | port.shut = bool(shut) |
---|
[202] | 853 | return {'entries': [self._portstat(port)]} |
---|
[183] | 854 | |
---|
[211] | 855 | def handle_delPort(self, params): |
---|
[202] | 856 | port = self._switch.get_port(int(params['port'])) |
---|
[211] | 857 | port.interface.close() |
---|
[202] | 858 | return {'entries': [self._portstat(port)]} |
---|
[183] | 859 | |
---|
[212] | 860 | def handle_setInterface(self, params): |
---|
| 861 | portnum = int(params['port']) |
---|
| 862 | port = self._switch.get_port(portnum) |
---|
| 863 | address = params.get('address') |
---|
| 864 | netmask = params.get('netmask') |
---|
| 865 | mtu = params.get('mtu') |
---|
[253] | 866 | if not port.interface.IFOP_ALLOWED: |
---|
[212] | 867 | raise ValueError('Port %d has unsupported interface: %s' % |
---|
| 868 | (portnum, port.interface.IFTYPE)) |
---|
| 869 | if address is not None: |
---|
| 870 | port.interface.address = address |
---|
| 871 | if netmask is not None: |
---|
| 872 | port.interface.netmask = netmask |
---|
| 873 | if mtu is not None: |
---|
| 874 | port.interface.mtu = mtu |
---|
| 875 | return {'entries': [self._ifstat(port)]} |
---|
| 876 | |
---|
| 877 | def handle_listInterface(self, params): |
---|
| 878 | return {'entries': [self._ifstat(p) for p in self._switch.portlist |
---|
[253] | 879 | if p.interface.IFOP_ALLOWED]} |
---|
[212] | 880 | |
---|
[251] | 881 | def _optparse_netdev(self, opt): |
---|
[253] | 882 | return {} |
---|
[251] | 883 | |
---|
[186] | 884 | def _optparse_tap(self, opt): |
---|
[253] | 885 | return {} |
---|
[183] | 886 | |
---|
[186] | 887 | def _optparse_client(self, opt): |
---|
| 888 | if opt.get('insecure'): |
---|
[273] | 889 | sslopt = {'cert_reqs': ssl.CERT_NONE} |
---|
[265] | 890 | else: |
---|
| 891 | sslopt = {'cert_reqs': ssl.CERT_REQUIRED, |
---|
| 892 | 'ca_certs': opt.get('cacerts')} |
---|
[186] | 893 | cred = {'user': opt.get('user'), 'passwd': opt.get('passwd')} |
---|
[265] | 894 | return {'sslopt': sslopt, 'cred': cred} |
---|
[183] | 895 | |
---|
[202] | 896 | def _jsonrpc_response(self, id_=None, result=None, error=None): |
---|
| 897 | res = {'jsonrpc': '2.0', 'id': id_} |
---|
| 898 | if result: |
---|
| 899 | res['result'] = result |
---|
| 900 | if error: |
---|
| 901 | res['error'] = error |
---|
| 902 | self.finish(res) |
---|
| 903 | |
---|
[183] | 904 | @staticmethod |
---|
[186] | 905 | def _portstat(port): |
---|
| 906 | return { |
---|
| 907 | 'port': port.number, |
---|
[191] | 908 | 'type': port.interface.IFTYPE, |
---|
[203] | 909 | 'target': port.interface.target, |
---|
[186] | 910 | 'tx': port.tx, |
---|
| 911 | 'rx': port.rx, |
---|
| 912 | 'shut': port.shut, |
---|
| 913 | } |
---|
[183] | 914 | |
---|
[212] | 915 | @staticmethod |
---|
| 916 | def _ifstat(port): |
---|
| 917 | return { |
---|
| 918 | 'port': port.number, |
---|
| 919 | 'type': port.interface.IFTYPE, |
---|
| 920 | 'target': port.interface.target, |
---|
| 921 | 'address': port.interface.address, |
---|
| 922 | 'netmask': port.interface.netmask, |
---|
| 923 | 'mtu': port.interface.mtu, |
---|
| 924 | } |
---|
[183] | 925 | |
---|
[212] | 926 | |
---|
[206] | 927 | def _print_error(error): |
---|
[205] | 928 | print(' %s (%s)' % (error['message'], error['code'])) |
---|
| 929 | print(' %s' % error['data']) |
---|
| 930 | |
---|
| 931 | |
---|
[206] | 932 | def _start_sw(args): |
---|
[186] | 933 | def daemonize(nochdir=False, noclose=False): |
---|
| 934 | if os.fork() > 0: |
---|
| 935 | sys.exit(0) |
---|
[134] | 936 | |
---|
[186] | 937 | os.setsid() |
---|
[134] | 938 | |
---|
[186] | 939 | if os.fork() > 0: |
---|
| 940 | sys.exit(0) |
---|
[134] | 941 | |
---|
[186] | 942 | if not nochdir: |
---|
| 943 | os.chdir('/') |
---|
[134] | 944 | |
---|
[186] | 945 | if not noclose: |
---|
| 946 | os.umask(0) |
---|
| 947 | sys.stdin.close() |
---|
| 948 | sys.stdout.close() |
---|
| 949 | sys.stderr.close() |
---|
| 950 | os.close(0) |
---|
| 951 | os.close(1) |
---|
| 952 | os.close(2) |
---|
| 953 | sys.stdin = open(os.devnull) |
---|
| 954 | sys.stdout = open(os.devnull, 'a') |
---|
| 955 | sys.stderr = open(os.devnull, 'a') |
---|
[134] | 956 | |
---|
[186] | 957 | def checkabspath(ns, path): |
---|
[184] | 958 | val = getattr(ns, path, '') |
---|
| 959 | if not val.startswith('/'): |
---|
[202] | 960 | raise ValueError('Invalid %: %s' % (path, val)) |
---|
[184] | 961 | |
---|
| 962 | def getsslopt(ns, key, cert): |
---|
| 963 | kval = getattr(ns, key, None) |
---|
| 964 | cval = getattr(ns, cert, None) |
---|
| 965 | if kval and cval: |
---|
| 966 | return {'keyfile': kval, 'certfile': cval} |
---|
| 967 | elif kval or cval: |
---|
[202] | 968 | raise ValueError('Both %s and %s are required' % (key, cert)) |
---|
[184] | 969 | return None |
---|
| 970 | |
---|
[186] | 971 | def setrealpath(ns, *keys): |
---|
| 972 | for k in keys: |
---|
| 973 | v = getattr(ns, k, None) |
---|
| 974 | if v is not None: |
---|
| 975 | v = os.path.realpath(v) |
---|
| 976 | open(v).close() # check readable |
---|
| 977 | setattr(ns, k, v) |
---|
| 978 | |
---|
[184] | 979 | def setport(ns, port, isssl): |
---|
| 980 | val = getattr(ns, port, None) |
---|
| 981 | if val is None: |
---|
| 982 | if isssl: |
---|
| 983 | return setattr(ns, port, 443) |
---|
| 984 | return setattr(ns, port, 80) |
---|
| 985 | if not (0 <= val <= 65535): |
---|
[202] | 986 | raise ValueError('Invalid %s: %s' % (port, val)) |
---|
[184] | 987 | |
---|
| 988 | def sethtpasswd(ns, htpasswd): |
---|
| 989 | val = getattr(ns, htpasswd, None) |
---|
| 990 | if val: |
---|
| 991 | return setattr(ns, htpasswd, Htpasswd(val)) |
---|
| 992 | |
---|
[272] | 993 | # if args.debug: |
---|
| 994 | # websocket.enableTrace(True) |
---|
[183] | 995 | |
---|
| 996 | if args.ageout <= 0: |
---|
[202] | 997 | raise ValueError('Invalid ageout: %s' % args.ageout) |
---|
[183] | 998 | |
---|
[280] | 999 | setrealpath(args, 'logconf') |
---|
[186] | 1000 | setrealpath(args, 'htpasswd', 'sslkey', 'sslcert') |
---|
| 1001 | setrealpath(args, 'ctlhtpasswd', 'ctlsslkey', 'ctlsslcert') |
---|
[183] | 1002 | |
---|
[186] | 1003 | checkabspath(args, 'path') |
---|
| 1004 | checkabspath(args, 'ctlpath') |
---|
[183] | 1005 | |
---|
[184] | 1006 | sslopt = getsslopt(args, 'sslkey', 'sslcert') |
---|
| 1007 | ctlsslopt = getsslopt(args, 'ctlsslkey', 'ctlsslcert') |
---|
[143] | 1008 | |
---|
[184] | 1009 | setport(args, 'port', sslopt) |
---|
| 1010 | setport(args, 'ctlport', ctlsslopt) |
---|
[143] | 1011 | |
---|
[184] | 1012 | sethtpasswd(args, 'htpasswd') |
---|
| 1013 | sethtpasswd(args, 'ctlhtpasswd') |
---|
[167] | 1014 | |
---|
[280] | 1015 | if args.logconf: |
---|
| 1016 | logging.config.fileConfig(args.logconf) |
---|
| 1017 | logger = logging.getLogger('etherws') |
---|
| 1018 | else: |
---|
| 1019 | logger = logging.getLogger('etherws') |
---|
| 1020 | logger.setLevel(logging.DEBUG if args.debug else logging.INFO) |
---|
| 1021 | |
---|
[183] | 1022 | ioloop = IOLoop.instance() |
---|
[280] | 1023 | fdb = FDB(args.ageout, logger, args.debug) |
---|
| 1024 | switch = SwitchingHub(fdb, logger, args.debug) |
---|
[167] | 1025 | |
---|
[184] | 1026 | if args.port == args.ctlport and args.host == args.ctlhost: |
---|
| 1027 | if args.path == args.ctlpath: |
---|
[202] | 1028 | raise ValueError('Same path/ctlpath on same host') |
---|
[184] | 1029 | if args.sslkey != args.ctlsslkey: |
---|
[202] | 1030 | raise ValueError('Different sslkey/ctlsslkey on same host') |
---|
[184] | 1031 | if args.sslcert != args.ctlsslcert: |
---|
[202] | 1032 | raise ValueError('Different sslcert/ctlsslcert on same host') |
---|
[133] | 1033 | |
---|
[184] | 1034 | app = Application([ |
---|
[253] | 1035 | (args.path, ServerHandler, { |
---|
[184] | 1036 | 'switch': switch, |
---|
| 1037 | 'htpasswd': args.htpasswd, |
---|
[287] | 1038 | 'noorigin': args.noorigin, |
---|
[280] | 1039 | 'logger': logger, |
---|
[184] | 1040 | 'debug': args.debug, |
---|
| 1041 | }), |
---|
[253] | 1042 | (args.ctlpath, ControlServerHandler, { |
---|
[184] | 1043 | 'ioloop': ioloop, |
---|
| 1044 | 'switch': switch, |
---|
| 1045 | 'htpasswd': args.ctlhtpasswd, |
---|
[280] | 1046 | 'logger': logger, |
---|
[184] | 1047 | 'debug': args.debug, |
---|
| 1048 | }), |
---|
| 1049 | ]) |
---|
| 1050 | server = HTTPServer(app, ssl_options=sslopt) |
---|
| 1051 | server.listen(args.port, address=args.host) |
---|
[151] | 1052 | |
---|
[184] | 1053 | else: |
---|
[253] | 1054 | app = Application([(args.path, ServerHandler, { |
---|
[184] | 1055 | 'switch': switch, |
---|
| 1056 | 'htpasswd': args.htpasswd, |
---|
[287] | 1057 | 'noorigin': args.noorigin, |
---|
[280] | 1058 | 'logger': logger, |
---|
[184] | 1059 | 'debug': args.debug, |
---|
| 1060 | })]) |
---|
| 1061 | server = HTTPServer(app, ssl_options=sslopt) |
---|
| 1062 | server.listen(args.port, address=args.host) |
---|
| 1063 | |
---|
[253] | 1064 | ctl = Application([(args.ctlpath, ControlServerHandler, { |
---|
[184] | 1065 | 'ioloop': ioloop, |
---|
| 1066 | 'switch': switch, |
---|
| 1067 | 'htpasswd': args.ctlhtpasswd, |
---|
[280] | 1068 | 'logger': logger, |
---|
[184] | 1069 | 'debug': args.debug, |
---|
| 1070 | })]) |
---|
| 1071 | ctlserver = HTTPServer(ctl, ssl_options=ctlsslopt) |
---|
| 1072 | ctlserver.listen(args.ctlport, address=args.ctlhost) |
---|
| 1073 | |
---|
[151] | 1074 | if not args.foreground: |
---|
| 1075 | daemonize() |
---|
| 1076 | |
---|
[138] | 1077 | ioloop.start() |
---|
[133] | 1078 | |
---|
| 1079 | |
---|
[206] | 1080 | def _start_ctl(args): |
---|
[272] | 1081 | def have_ssl_cert_verification(): |
---|
| 1082 | return 'context' in urllib2.urlopen.__code__.co_varnames |
---|
| 1083 | |
---|
[202] | 1084 | def request(args, method, params=None, id_=0): |
---|
[190] | 1085 | req = urllib2.Request(args.ctlurl) |
---|
| 1086 | req.add_header('Content-type', 'application/json') |
---|
| 1087 | if args.ctluser: |
---|
| 1088 | if not args.ctlpasswd: |
---|
[209] | 1089 | args.ctlpasswd = getpass.getpass('Control Password: ') |
---|
[190] | 1090 | token = base64.b64encode('%s:%s' % (args.ctluser, args.ctlpasswd)) |
---|
| 1091 | req.add_header('Authorization', 'Basic %s' % token) |
---|
[253] | 1092 | method = '.'.join([ControlServerHandler.NAMESPACE, method]) |
---|
[202] | 1093 | data = {'jsonrpc': '2.0', 'method': method, 'id': id_} |
---|
| 1094 | if params is not None: |
---|
| 1095 | data['params'] = params |
---|
[272] | 1096 | if have_ssl_cert_verification(): |
---|
| 1097 | ctx = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, |
---|
| 1098 | cafile=args.ctlsslcert) |
---|
| 1099 | if args.ctlinsecure: |
---|
| 1100 | ctx.check_hostname = False |
---|
| 1101 | ctx.verify_mode = ssl.CERT_NONE |
---|
| 1102 | fp = urllib2.urlopen(req, json.dumps(data), context=ctx) |
---|
| 1103 | elif args.ctlsslcert: |
---|
| 1104 | raise EnvironmentError('do not support certificate verification') |
---|
| 1105 | else: |
---|
| 1106 | fp = urllib2.urlopen(req, json.dumps(data)) |
---|
| 1107 | return json.loads(fp.read()) |
---|
[190] | 1108 | |
---|
[255] | 1109 | def print_table(rows): |
---|
| 1110 | cols = zip(*rows) |
---|
| 1111 | maxlen = [0] * len(cols) |
---|
| 1112 | for i in xrange(len(cols)): |
---|
| 1113 | maxlen[i] = max(len(str(c)) for c in cols[i]) |
---|
| 1114 | fmt = ' '.join(['%%-%ds' % maxlen[i] for i in xrange(len(cols))]) |
---|
| 1115 | fmt = ' ' + fmt |
---|
| 1116 | for row in rows: |
---|
| 1117 | print(fmt % tuple(row)) |
---|
| 1118 | |
---|
[199] | 1119 | def print_portlist(result): |
---|
[255] | 1120 | rows = [['Port', 'Type', 'State', 'RX', 'TX', 'Target']] |
---|
[199] | 1121 | for r in result: |
---|
[255] | 1122 | rows.append([r['port'], r['type'], 'shut' if r['shut'] else 'up', |
---|
| 1123 | r['rx'], r['tx'], r['target']]) |
---|
| 1124 | print_table(rows) |
---|
[199] | 1125 | |
---|
[212] | 1126 | def print_iflist(result): |
---|
[255] | 1127 | rows = [['Port', 'Type', 'Address', 'Netmask', 'MTU', 'Target']] |
---|
[212] | 1128 | for r in result: |
---|
[255] | 1129 | rows.append([r['port'], r['type'], r['address'], |
---|
| 1130 | r['netmask'], r['mtu'], r['target']]) |
---|
| 1131 | print_table(rows) |
---|
[212] | 1132 | |
---|
[190] | 1133 | def handle_ctl_addport(args): |
---|
[205] | 1134 | opts = { |
---|
| 1135 | 'user': getattr(args, 'user', None), |
---|
| 1136 | 'passwd': getattr(args, 'passwd', None), |
---|
| 1137 | 'cacerts': getattr(args, 'cacerts', None), |
---|
| 1138 | 'insecure': getattr(args, 'insecure', None), |
---|
| 1139 | } |
---|
[253] | 1140 | if args.iftype == ClientHandler.IFTYPE: |
---|
[205] | 1141 | if not args.target.startswith('ws://') and \ |
---|
| 1142 | not args.target.startswith('wss://'): |
---|
| 1143 | raise ValueError('Invalid target URL scheme: %s' % args.target) |
---|
[210] | 1144 | if not opts['user'] and opts['passwd']: |
---|
| 1145 | raise ValueError('Authentication required but username empty') |
---|
| 1146 | if opts['user'] and not opts['passwd']: |
---|
| 1147 | opts['passwd'] = getpass.getpass('Client Password: ') |
---|
[202] | 1148 | result = request(args, 'addPort', { |
---|
[204] | 1149 | 'type': args.iftype, |
---|
[191] | 1150 | 'target': args.target, |
---|
[205] | 1151 | 'options': opts, |
---|
[202] | 1152 | }) |
---|
| 1153 | if 'error' in result: |
---|
[206] | 1154 | _print_error(result['error']) |
---|
[199] | 1155 | else: |
---|
| 1156 | print_portlist(result['result']['entries']) |
---|
[190] | 1157 | |
---|
[211] | 1158 | def handle_ctl_setport(args): |
---|
[190] | 1159 | if args.port <= 0: |
---|
[202] | 1160 | raise ValueError('Invalid port: %d' % args.port) |
---|
[211] | 1161 | req = {'port': args.port} |
---|
| 1162 | shut = getattr(args, 'shut', None) |
---|
| 1163 | if shut is not None: |
---|
| 1164 | req['shut'] = bool(shut) |
---|
| 1165 | result = request(args, 'setPort', req) |
---|
[202] | 1166 | if 'error' in result: |
---|
[206] | 1167 | _print_error(result['error']) |
---|
[199] | 1168 | else: |
---|
| 1169 | print_portlist(result['result']['entries']) |
---|
[190] | 1170 | |
---|
| 1171 | def handle_ctl_delport(args): |
---|
| 1172 | if args.port <= 0: |
---|
[202] | 1173 | raise ValueError('Invalid port: %d' % args.port) |
---|
| 1174 | result = request(args, 'delPort', {'port': args.port}) |
---|
| 1175 | if 'error' in result: |
---|
[206] | 1176 | _print_error(result['error']) |
---|
[199] | 1177 | else: |
---|
| 1178 | print_portlist(result['result']['entries']) |
---|
[190] | 1179 | |
---|
| 1180 | def handle_ctl_listport(args): |
---|
[202] | 1181 | result = request(args, 'listPort') |
---|
| 1182 | if 'error' in result: |
---|
[206] | 1183 | _print_error(result['error']) |
---|
[199] | 1184 | else: |
---|
| 1185 | print_portlist(result['result']['entries']) |
---|
[190] | 1186 | |
---|
[212] | 1187 | def handle_ctl_setif(args): |
---|
| 1188 | if args.port <= 0: |
---|
| 1189 | raise ValueError('Invalid port: %d' % args.port) |
---|
| 1190 | req = {'port': args.port} |
---|
| 1191 | address = getattr(args, 'address', None) |
---|
| 1192 | netmask = getattr(args, 'netmask', None) |
---|
| 1193 | mtu = getattr(args, 'mtu', None) |
---|
| 1194 | if address is not None: |
---|
| 1195 | if address: |
---|
| 1196 | socket.inet_aton(address) # validate |
---|
| 1197 | req['address'] = address |
---|
| 1198 | if netmask is not None: |
---|
| 1199 | if netmask: |
---|
| 1200 | socket.inet_aton(netmask) # validate |
---|
| 1201 | req['netmask'] = netmask |
---|
| 1202 | if mtu is not None: |
---|
| 1203 | if mtu < 576: |
---|
| 1204 | raise ValueError('Invalid MTU: %d' % mtu) |
---|
| 1205 | req['mtu'] = mtu |
---|
| 1206 | result = request(args, 'setInterface', req) |
---|
| 1207 | if 'error' in result: |
---|
| 1208 | _print_error(result['error']) |
---|
| 1209 | else: |
---|
| 1210 | print_iflist(result['result']['entries']) |
---|
| 1211 | |
---|
| 1212 | def handle_ctl_listif(args): |
---|
| 1213 | result = request(args, 'listInterface') |
---|
| 1214 | if 'error' in result: |
---|
| 1215 | _print_error(result['error']) |
---|
| 1216 | else: |
---|
| 1217 | print_iflist(result['result']['entries']) |
---|
| 1218 | |
---|
[198] | 1219 | def handle_ctl_listfdb(args): |
---|
[202] | 1220 | result = request(args, 'listFdb') |
---|
| 1221 | if 'error' in result: |
---|
[206] | 1222 | return _print_error(result['error']) |
---|
[255] | 1223 | rows = [['Port', 'VLAN', 'MAC', 'Age']] |
---|
| 1224 | for r in result['result']['entries']: |
---|
| 1225 | rows.append([r['port'], r['vid'], r['mac'], r['age']]) |
---|
| 1226 | print_table(rows) |
---|
| 1227 | |
---|
[199] | 1228 | locals()['handle_ctl_' + args.control_method](args) |
---|
[190] | 1229 | |
---|
| 1230 | |
---|
[206] | 1231 | def _main(): |
---|
[186] | 1232 | parser = argparse.ArgumentParser() |
---|
[190] | 1233 | subcommand = parser.add_subparsers(dest='subcommand') |
---|
[186] | 1234 | |
---|
[204] | 1235 | # - sw |
---|
[215] | 1236 | parser_sw = subcommand.add_parser('sw', |
---|
| 1237 | help='start virtual switch') |
---|
[190] | 1238 | |
---|
[215] | 1239 | parser_sw.add_argument('--debug', action='store_true', default=False, |
---|
| 1240 | help='run as debug mode') |
---|
[280] | 1241 | parser_sw.add_argument('--logconf', |
---|
| 1242 | help='path to logging config') |
---|
[215] | 1243 | parser_sw.add_argument('--foreground', action='store_true', default=False, |
---|
| 1244 | help='run as foreground mode') |
---|
| 1245 | parser_sw.add_argument('--ageout', type=int, default=300, |
---|
| 1246 | help='FDB ageout time (sec)') |
---|
[186] | 1247 | |
---|
[215] | 1248 | parser_sw.add_argument('--path', default='/', |
---|
| 1249 | help='http(s) path to serve WebSocket') |
---|
| 1250 | parser_sw.add_argument('--host', default='', |
---|
| 1251 | help='listen address to serve WebSocket') |
---|
| 1252 | parser_sw.add_argument('--port', type=int, |
---|
| 1253 | help='listen port to serve WebSocket') |
---|
| 1254 | parser_sw.add_argument('--htpasswd', |
---|
| 1255 | help='path to htpasswd file to auth WebSocket') |
---|
| 1256 | parser_sw.add_argument('--sslkey', |
---|
| 1257 | help='path to SSL private key for WebSocket') |
---|
| 1258 | parser_sw.add_argument('--sslcert', |
---|
| 1259 | help='path to SSL certificate for WebSocket') |
---|
[186] | 1260 | |
---|
[215] | 1261 | parser_sw.add_argument('--ctlpath', default='/ctl', |
---|
| 1262 | help='http(s) path to serve control API') |
---|
| 1263 | parser_sw.add_argument('--ctlhost', default='127.0.0.1', |
---|
| 1264 | help='listen address to serve control API') |
---|
| 1265 | parser_sw.add_argument('--ctlport', type=int, default=7867, |
---|
| 1266 | help='listen port to serve control API') |
---|
| 1267 | parser_sw.add_argument('--ctlhtpasswd', |
---|
| 1268 | help='path to htpasswd file to auth control API') |
---|
| 1269 | parser_sw.add_argument('--ctlsslkey', |
---|
| 1270 | help='path to SSL private key for control API') |
---|
| 1271 | parser_sw.add_argument('--ctlsslcert', |
---|
| 1272 | help='path to SSL certificate for control API') |
---|
[186] | 1273 | |
---|
[287] | 1274 | parser_sw.add_argument('--noorigin', action='store_true', default=False, |
---|
| 1275 | help='do not check origin header') |
---|
| 1276 | |
---|
[204] | 1277 | # - ctl |
---|
[215] | 1278 | parser_ctl = subcommand.add_parser('ctl', |
---|
| 1279 | help='control virtual switch') |
---|
| 1280 | parser_ctl.add_argument('--ctlurl', default='http://127.0.0.1:7867/ctl', |
---|
| 1281 | help='URL to control API') |
---|
| 1282 | parser_ctl.add_argument('--ctluser', |
---|
| 1283 | help='username to auth control API') |
---|
| 1284 | parser_ctl.add_argument('--ctlpasswd', |
---|
| 1285 | help='password to auth control API') |
---|
[272] | 1286 | parser_ctl.add_argument('--ctlsslcert', |
---|
| 1287 | help='path to SSL certificate for control API') |
---|
| 1288 | parser_ctl.add_argument( |
---|
| 1289 | '--ctlinsecure', action='store_true', default=False, |
---|
| 1290 | help='do not verify control API certificate') |
---|
[190] | 1291 | |
---|
[204] | 1292 | control_method = parser_ctl.add_subparsers(dest='control_method') |
---|
[190] | 1293 | |
---|
[204] | 1294 | # -- ctl addport |
---|
[215] | 1295 | parser_ctl_addport = control_method.add_parser('addport', |
---|
| 1296 | help='create and add port') |
---|
[204] | 1297 | iftype = parser_ctl_addport.add_subparsers(dest='iftype') |
---|
[190] | 1298 | |
---|
[251] | 1299 | # --- ctl addport netdev |
---|
| 1300 | parser_ctl_addport_netdev = iftype.add_parser(NetdevHandler.IFTYPE, |
---|
[257] | 1301 | help='Network device') |
---|
[251] | 1302 | parser_ctl_addport_netdev.add_argument('target', |
---|
| 1303 | help='device name to add interface') |
---|
| 1304 | |
---|
[204] | 1305 | # --- ctl addport tap |
---|
[215] | 1306 | parser_ctl_addport_tap = iftype.add_parser(TapHandler.IFTYPE, |
---|
| 1307 | help='TAP device') |
---|
| 1308 | parser_ctl_addport_tap.add_argument('target', |
---|
| 1309 | help='device name to create interface') |
---|
[190] | 1310 | |
---|
[204] | 1311 | # --- ctl addport client |
---|
[253] | 1312 | parser_ctl_addport_client = iftype.add_parser(ClientHandler.IFTYPE, |
---|
[215] | 1313 | help='WebSocket client') |
---|
| 1314 | parser_ctl_addport_client.add_argument('target', |
---|
| 1315 | help='URL to connect WebSocket') |
---|
| 1316 | parser_ctl_addport_client.add_argument('--user', |
---|
| 1317 | help='username to auth WebSocket') |
---|
| 1318 | parser_ctl_addport_client.add_argument('--passwd', |
---|
| 1319 | help='password to auth WebSocket') |
---|
| 1320 | parser_ctl_addport_client.add_argument('--cacerts', |
---|
| 1321 | help='path to CA certificate') |
---|
[204] | 1322 | parser_ctl_addport_client.add_argument( |
---|
[215] | 1323 | '--insecure', action='store_true', default=False, |
---|
| 1324 | help='do not verify server certificate') |
---|
[190] | 1325 | |
---|
[211] | 1326 | # -- ctl setport |
---|
[215] | 1327 | parser_ctl_setport = control_method.add_parser('setport', |
---|
| 1328 | help='set port config') |
---|
| 1329 | parser_ctl_setport.add_argument('port', type=int, |
---|
| 1330 | help='port number to set config') |
---|
| 1331 | parser_ctl_setport.add_argument('--shut', type=int, choices=(0, 1), |
---|
| 1332 | help='set shutdown state') |
---|
[190] | 1333 | |
---|
[204] | 1334 | # -- ctl delport |
---|
[215] | 1335 | parser_ctl_delport = control_method.add_parser('delport', |
---|
| 1336 | help='delete port') |
---|
| 1337 | parser_ctl_delport.add_argument('port', type=int, |
---|
| 1338 | help='port number to delete') |
---|
[198] | 1339 | |
---|
[204] | 1340 | # -- ctl listport |
---|
[215] | 1341 | parser_ctl_listport = control_method.add_parser('listport', |
---|
| 1342 | help='show port list') |
---|
[204] | 1343 | |
---|
[212] | 1344 | # -- ctl setif |
---|
[215] | 1345 | parser_ctl_setif = control_method.add_parser('setif', |
---|
| 1346 | help='set interface config') |
---|
| 1347 | parser_ctl_setif.add_argument('port', type=int, |
---|
| 1348 | help='port number to set config') |
---|
| 1349 | parser_ctl_setif.add_argument('--address', |
---|
| 1350 | help='IPv4 address to set interface') |
---|
| 1351 | parser_ctl_setif.add_argument('--netmask', |
---|
| 1352 | help='IPv4 netmask to set interface') |
---|
| 1353 | parser_ctl_setif.add_argument('--mtu', type=int, |
---|
| 1354 | help='MTU to set interface') |
---|
[212] | 1355 | |
---|
| 1356 | # -- ctl listif |
---|
[215] | 1357 | parser_ctl_listif = control_method.add_parser('listif', |
---|
| 1358 | help='show interface list') |
---|
[212] | 1359 | |
---|
[204] | 1360 | # -- ctl listfdb |
---|
[215] | 1361 | parser_ctl_listfdb = control_method.add_parser('listfdb', |
---|
| 1362 | help='show FDB entries') |
---|
[204] | 1363 | |
---|
[190] | 1364 | # -- go |
---|
[186] | 1365 | args = parser.parse_args() |
---|
| 1366 | |
---|
[205] | 1367 | try: |
---|
[206] | 1368 | globals()['_start_' + args.subcommand](args) |
---|
[205] | 1369 | except Exception as e: |
---|
[206] | 1370 | _print_error({ |
---|
[205] | 1371 | 'code': 0 - 32603, |
---|
| 1372 | 'message': 'Internal error', |
---|
[280] | 1373 | 'data': '%s: %s' % (type(e).__name__, str(e)), |
---|
[205] | 1374 | }) |
---|
[186] | 1375 | |
---|
[205] | 1376 | |
---|
[133] | 1377 | if __name__ == '__main__': |
---|
[206] | 1378 | _main() |
---|