4 # nfdcpd: A promiscuous, NFQUEUE-based DHCP server for virtual machine hosting
5 # Copyright (c) 2010 GRNET SA
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 import logging.handlers
35 from select import select
36 from socket import AF_INET, AF_INET6
38 from scapy.layers.l2 import Ether
39 from scapy.layers.inet import IP, UDP
40 from scapy.layers.inet6 import *
41 from scapy.layers.dhcp import BOOTP, DHCP
42 from scapy.sendrecv import sendp
44 DEFAULT_PATH = "/var/run/ganeti-dhcpd"
45 DEFAULT_NFQUEUE_NUM = 42
46 DEFAULT_USER = "nobody"
47 DEFAULT_LEASE_TIME = 604800 # 1 week
48 DEFAULT_RENEWAL_TIME = 600 # 10 min
50 LOG_FILENAME = "/var/log/nfdhcpd/nfdhcpd.log"
52 SYSFS_NET = "/sys/class/net"
53 DHCP_DUMMY_SERVER_IP = "1.2.3.4"
55 LOG_FORMAT = "%(asctime)-15s %(levelname)-6s %(message)s"
56 PERIODIC_RA_TIMEOUT = 30 # seconds
68 DHCPDISCOVER: "DHCPDISCOVER",
69 DHCPOFFER: "DHCPOFFER",
70 DHCPREQUEST: "DHCPREQUEST",
71 DHCPDECLINE: "DHCPDECLINE",
74 DHCPRELEASE: "DHCPRELEASE",
75 DHCPINFORM: "DHCPINFORM",
79 DHCPDISCOVER: DHCPOFFER,
84 class ClientFileHandler(pyinotify.ProcessEvent):
85 def __init__(self, server):
86 pyinotify.ProcessEvent.__init__(self)
89 def process_IN_DELETE(self, event):
90 self.server.remove_iface(event.name)
92 def process_IN_CLOSE_WRITE(self, event):
93 self.server.add_iface(os.path.join(event.path, event.name))
97 def __init__(self, mac=None, ips=None, link=None, hostname=None):
100 self.hostname = hostname
109 return self.mac is not None and self.ips is not None\
110 and self.hostname is not None
113 class Subnet(object):
114 def __init__(self, net=None, gw=None, dev=None):
115 if isinstance(net, str):
116 self.net = IPy.IP(net)
124 return str(self.net.netmask())
128 return str(self.net.broadcast())
132 return self.net.net()
136 return self.net.prefixlen()
139 def _make_eui64(net, mac):
140 """ Compute an EUI-64 address from an EUI-48 (MAC) address
143 comp = mac.split(":")
144 prefix = IPy.IP(net).net().strFullsize().split(":")[:4]
145 eui64 = comp[:3] + ["ff", "fe"] + comp[3:]
146 eui64[0] = "%02x" % (int(eui64[0], 16) ^ 0x02)
147 for l in range(0, len(eui64), 2):
148 prefix += ["".join(eui64[l:l+2])]
149 return IPy.IP(":".join(prefix))
151 def make_eui64(self, mac):
152 return self._make_eui64(self.net, mac)
154 def make_ll64(self, mac):
155 return self._make_eui64("fe80::", mac)
158 class VMNetProxy(object):
159 def __init__(self, data_path, dhcp_queue_num=None,
160 rs_queue_num=None, ns_queue_num=None):
161 self.data_path = data_path
169 self.wm = pyinotify.WatchManager()
170 mask = pyinotify.EventsCodes.ALL_FLAGS["IN_DELETE"]
171 mask |= pyinotify.EventsCodes.ALL_FLAGS["IN_CLOSE_WRITE"]
172 handler = ClientFileHandler(self)
173 self.notifier = pyinotify.Notifier(self.wm, handler)
174 self.wm.add_watch(self.data_path, mask, rec=True)
177 if dhcp_queue_num is not None:
178 self._setup_nfqueue(dhcp_queue_num, AF_INET, self.dhcp_response)
180 if rs_queue_num is not None:
181 self._setup_nfqueue(rs_queue_num, AF_INET6, self.rs_response)
183 if ns_queue_num is not None:
184 self._setup_nfqueue(ns_queue_num, AF_INET6, self.ns_response)
186 def _setup_nfqueue(self, queue_num, family, callback):
187 logging.debug("Setting up NFQUEUE for queue %d, AF %s" %
190 q.set_callback(callback)
191 q.fast_open(queue_num, family)
192 q.set_queue_maxlen(5000)
193 # This is mandatory for the queue to operate
194 q.set_mode(nfqueue.NFQNL_COPY_PACKET)
195 self.nfq[q.get_fd()] = q
197 def build_config(self):
201 for file in glob.glob(os.path.join(self.data_path, "*")):
204 def get_ifindex(self, iface):
205 """ Get the interface index from sysfs
208 file = os.path.abspath(os.path.join(SYSFS_NET, iface, "ifindex"))
209 if not file.startswith(SYSFS_NET):
216 ifindex = int(f.readline().strip())
224 def get_iface_hw_addr(self, iface):
225 """ Get the interface hardware address from sysfs
228 file = os.path.abspath(os.path.join(SYSFS_NET, iface, "address"))
229 if not file.startswith(SYSFS_NET):
235 addr = f.readline().strip()
241 def parse_routing_table(self, table="main", family=4):
242 """ Parse the given routing table to get connected route, gateway and
246 ipro = subprocess.Popen(["ip", "-%d" % family, "ro", "ls",
247 "table", table], stdout=subprocess.PIPE)
248 routes = ipro.stdout.readlines()
255 match = re.match(r'^default.*via ([^\s]+).*dev ([^\s]+)', route)
257 def_gw, def_dev = match.groups()
261 # Find the least-specific connected route
263 def_net = re.match("^([^\\s]+) dev %s" %
264 def_dev, route).groups()[0]
265 def_net = IPy.IP(def_net)
269 return Subnet(net=def_net, gw=def_gw, dev=def_dev)
271 def parse_binding_file(self, path):
272 """ Read a client configuration from a tap file
276 iffile = open(path, 'r')
278 return (None, None, None, None)
285 if line.startswith("IP="):
286 ip = line.strip().split("=")[1]
288 elif line.startswith("MAC="):
289 mac = line.strip().split("=")[1]
290 elif line.startswith("LINK="):
291 link = line.strip().split("=")[1]
292 elif line.startswith("HOSTNAME="):
293 hostname = line.strip().split("=")[1]
295 return Client(mac=mac, ips=ips, link=link, hostname=hostname)
297 def add_iface(self, path):
298 """ Add an interface to monitor
301 iface = os.path.basename(path)
303 logging.debug("Updating configuration for %s" % iface)
304 binding = self.parse_binding_file(path)
305 ifindex = self.get_ifindex(iface)
308 logging.warn("Stale configuration for %s found" % iface)
310 if binding.is_valid():
311 binding.iface = iface
312 self.clients[binding.mac] = binding
313 self.subnets[binding.link] = self.parse_routing_table(
315 logging.debug("Added client %s on %s" %
316 (binding.hostname, iface))
317 self.ifaces[ifindex] = iface
318 self.v6nets[iface] = self.parse_routing_table(binding.link, 6)
320 def remove_iface(self, iface):
321 """ Cleanup clients on a removed interface
324 if iface in self.v6nets:
325 del self.v6nets.iface
327 for mac in self.clients.keys():
328 if self.clients[mac].iface == iface:
329 del self.clients[mac]
331 for ifindex in self.ifaces.keys():
332 if self.ifaces[ifindex] == iface:
333 del self.ifaces[ifindex]
335 logging.debug("Removed interface %s" % iface)
337 def dhcp_response(self, i, payload):
338 """ Generate a reply to a BOOTP/DHCP request
341 # Decode the response - NFQUEUE relays IP packets
342 pkt = IP(payload.get_data())
344 # Get the actual interface from the ifindex
345 iface = self.ifaces[payload.get_indev()]
347 # Signal the kernel that it shouldn't further process the packet
348 payload.set_verdict(nfqueue.NF_DROP)
350 # Get the client MAC address
351 resp = pkt.getlayer(BOOTP).copy()
353 mac = resp.chaddr[:hlen].encode("hex")
354 mac, _ = re.subn(r'([0-9a-fA-F]{2})', r'\1:', mac, hlen-1)
356 # Server responses are always BOOTREPLYs
357 resp.op = "BOOTREPLY"
361 binding = self.clients[mac]
363 logging.warn("Invalid client %s on %s" % (mac, iface))
366 if iface != binding.iface:
367 logging.warn("Received spoofed DHCP request for %s from interface"
368 " %s instead of %s" %
369 (mac, iface, binding.iface))
372 resp = Ether(dst=mac, src=self.get_iface_hw_addr(iface))/\
373 IP(src=DHCP_DUMMY_SERVER_IP, dst=binding.ip)/\
374 UDP(sport=pkt.dport, dport=pkt.sport)/resp
375 subnet = self.subnets[binding.link]
378 logging.warn("Invalid request from %s on %s, no DHCP"
379 " payload found" % (binding.mac, iface))
383 requested_addr = binding.ip
384 for opt in pkt[DHCP].options:
385 if type(opt) is tuple and opt[0] == "message-type":
387 if type(opt) is tuple and opt[0] == "requested_addr":
388 requested_addr = opt[1]
390 logging.info("%s from %s on %s" %
391 (DHCP_TYPES.get(req_type, "UNKNOWN"), binding.mac, iface))
393 if req_type == DHCPREQUEST and requested_addr != binding.ip:
395 logging.info("Sending DHCPNAK to %s on %s: requested %s"
397 (binding.mac, iface, requested_addr, binding.ip))
399 elif req_type in (DHCPDISCOVER, DHCPREQUEST):
400 resp_type = DHCP_REQRESP[req_type]
401 resp.yiaddr = self.clients[mac].ip
403 ("hostname", binding.hostname),
404 ("domain", binding.hostname.split('.', 1)[-1]),
405 ("router", subnet.gw),
406 ("name_server", "194.177.210.10"),
407 ("name_server", "194.177.210.211"),
408 ("broadcast_address", str(subnet.broadcast)),
409 ("subnet_mask", str(subnet.netmask)),
410 ("renewal_time", DEFAULT_RENEWAL_TIME),
411 ("lease_time", DEFAULT_LEASE_TIME),
414 elif req_type == DHCPINFORM:
415 resp_type = DHCP_REQRESP[req_type]
417 ("hostname", binding.hostname),
418 ("domain", binding.hostname.split('.', 1)[-1]),
419 ("name_server", "194.177.210.10"),
420 ("name_server", "194.177.210.211"),
423 elif req_type == DHCPRELEASE:
425 logging.info("DHCPRELEASE from %s on %s" %
426 (binding.mac, iface))
429 # Finally, always add the server identifier and end options
431 ("message-type", resp_type),
432 ("server_id", DHCP_DUMMY_SERVER_IP),
435 resp /= DHCP(options=dhcp_options)
437 logging.info("%s to %s (%s) on %s" %
438 (DHCP_TYPES[resp_type], mac, binding.ip, iface))
439 sendp(resp, iface=iface, verbose=False)
441 def rs_response(self, i, payload):
442 """ Generate a reply to a BOOTP/DHCP request
445 # Get the actual interface from the ifindex
446 iface = self.ifaces[payload.get_indev()]
447 ifmac = self.get_iface_hw_addr(iface)
448 subnet = self.v6nets[iface]
449 ifll = subnet.make_ll64(ifmac)
451 # Signal the kernel that it shouldn't further process the packet
452 payload.set_verdict(nfqueue.NF_DROP)
454 resp = Ether(src=self.get_iface_hw_addr(iface))/\
455 IPv6(src=str(ifll))/ICMPv6ND_RA(routerlifetime=14400)/\
456 ICMPv6NDOptPrefixInfo(prefix=str(subnet.prefix),
457 prefixlen=subnet.prefixlen)
459 logging.info("RA on %s for %s" % (iface, subnet.net))
460 sendp(resp, iface=iface, verbose=False)
462 def ns_response(self, i, payload):
463 """ Generate a reply to an ICMPv6 neighbor solicitation
466 # Get the actual interface from the ifindex
467 iface = self.ifaces[payload.get_indev()]
468 ifmac = self.get_iface_hw_addr(iface)
469 subnet = self.v6nets[iface]
470 ifll = subnet.make_ll64(ifmac)
472 ns = IPv6(payload.get_data())
474 if not (subnet.net.overlaps(ns.tgt) or str(ns.tgt) == str(ifll)):
475 logging.debug("Received NS for a non-routable IP (%s)" % ns.tgt)
476 payload.set_verdict(nfqueue.NF_ACCEPT)
479 payload.set_verdict(nfqueue.NF_DROP)
481 resp = Ether(src=ifmac, dst=ns.lladdr)/\
482 IPv6(src=str(ifll), dst=ns.src)/\
483 ICMPv6ND_NA(R=1, O=0, S=1, tgt=ns.tgt)/\
484 ICMPv6NDOptDstLLAddr(lladdr=ifmac)
486 logging.info("NA on %s for %s" % (iface, ns.tgt))
487 sendp(resp, iface=iface, verbose=False)
490 def send_periodic_ra(self):
491 logging.debug("Sending out periodic RAs")
492 for client in self.clients.values():
494 subnet = self.v6nets[iface]
495 ifmac = self.get_iface_hw_addr(iface)
496 ifll = subnet.make_ll64(ifmac)
497 resp = Ether(src=ifmac)/\
498 IPv6(src=str(ifll))/ICMPv6ND_RA(routerlifetime=14400)/\
499 ICMPv6NDOptPrefixInfo(prefix=str(subnet.prefix),
500 prefixlen=subnet.prefixlen)
502 sendp(resp, iface=iface, verbose=False)
504 logging.debug("Periodic RA on %s failed" % iface)
507 """ Loop forever, serving DHCP requests
512 iwfd = self.notifier._fd
515 timeout = PERIODIC_RA_TIMEOUT
516 self.send_periodic_ra()
519 rlist, _, xlist = select(self.nfq.keys() + [iwfd], [], [], timeout)
520 # First check if there are any inotify (= configuration change)
522 if not (rlist or xlist):
523 # We were woken up by a timeout
525 self.send_periodic_ra()
529 self.notifier.read_events()
530 self.notifier.process_events()
534 self.nfq[fd].process_pending()
536 # Calculate the new timeout
537 timeout = PERIODIC_RA_TIMEOUT - (time.time() - start)
539 # Just to be safe we won't miss anything
541 logging.debug("Send extra RAs")
542 self.send_periodic_ra()
543 timeout = PERIODIC_RA_TIMEOUT
547 if __name__ == "__main__":
550 from pwd import getpwnam, getpwuid
552 parser = optparse.OptionParser()
553 parser.add_option("-p", "--path", dest="data_path",
554 help="The location of the data files", metavar="DIR",
555 default=DEFAULT_PATH)
556 parser.add_option("-c", "--dhcp-queue", dest="dhcp_queue",
557 help="The nfqueue to receive DHCP requests from"
558 " (default: %d" % DEFAULT_NFQUEUE_NUM, type="int",
559 metavar="NUM", default=DEFAULT_NFQUEUE_NUM)
560 parser.add_option("-r", "--rs-queue", dest="rs_queue",
561 help="The nfqueue to receive IPv6 router"
562 " solicitations from (default: %d)" %
563 DEFAULT_NFQUEUE_NUM, type="int",
564 metavar="NUM", default=DEFAULT_NFQUEUE_NUM)
565 parser.add_option("-n", "--ns-queue", dest="ns_queue",
566 help="The nfqueue to receive IPv6 neighbor"
567 " solicitations from (default: %d)" %
568 DEFAULT_NFQUEUE_NUM, type="int",
569 metavar="NUM", default=44)
570 parser.add_option("-u", "--user", dest="user",
571 help="An unprivileged user to run as",
572 metavar="UID", default=DEFAULT_USER)
573 parser.add_option("-d", "--debug", action="store_true", dest="debug",
574 help="Turn on debugging messages")
575 parser.add_option("-f", "--foreground", action="store_false", dest="daemonize",
576 default=True, help="Do not daemonize, stay in the foreground")
579 opts, args = parser.parse_args()
582 d = daemon.DaemonContext()
585 pidfile = open("/var/run/nfdhcpd.pid", "w")
586 pidfile.write("%s" % os.getpid())
589 logger = logging.getLogger()
591 logger.setLevel(logging.DEBUG)
593 logger.setLevel(logging.INFO)
596 handler = logging.handlers.RotatingFileHandler(LOG_FILENAME,
599 handler = logging.StreamHandler()
601 handler.setFormatter(logging.Formatter(LOG_FORMAT))
602 logger.addHandler(handler)
604 logging.info("Starting up")
605 proxy = VMNetProxy(opts.data_path, opts.dhcp_queue,
606 opts.rs_queue, opts.ns_queue)
608 # Drop all capabilities except CAP_NET_RAW and change uid
610 uid = getpwuid(int(opts.user))
612 uid = getpwnam(opts.user)
614 logging.info("Setting capabilities and changing uid")
615 logging.debug("User: %s, uid: %d, gid: %d" %
616 (opts.user, uid.pw_uid, uid.pw_gid))
617 capng_clear(CAPNG_SELECT_BOTH)
618 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_NET_RAW)
619 capng_change_id(uid.pw_uid, uid.pw_gid,
620 CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)
621 logging.info("Ready to serve requests")
625 # vim: set ts=4 sts=4 sw=4 et :