2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from __future__ import print_function
29 from optparse import OptionParser
39 TUNSETIFF = 0x400454ca
44 SIOCGIFNETMASK = 0x891B
45 SIOCSIFNETMASK = 0x891C
48 def create_socket(socket_name):
49 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
50 sock.bind(socket_name)
57 while '\n' not in chunk:
59 chunk = conn.recv(1024)
60 except (OSError, socket.error), e:
61 if e[0] != errno.EINTR:
72 msg = ''.join(msg).split('\n')[0]
73 # The message might have arguments that will be appended
74 # as a '|' separated list after the message type
78 dmsg = base64.b64decode(msg)
81 darg = base64.b64decode(arg)
82 dargs.append(darg.rstrip())
84 return (dmsg.rstrip(), dargs)
86 def send_reply(conn, reply):
87 encoded = base64.b64encode(reply)
88 conn.send("%s\n" % encoded)
90 def set_ifupdown(vif_name, up):
91 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
94 ifreq = struct.pack("16sH", vif_name, 0)
95 ifr = fcntl.ioctl(sock.fileno(), SIOCGIFFLAGS, ifreq)
99 raise RuntimeError("Could not set device %s UP" % vif_name)
101 name, flags = struct.unpack("16sH", ifr)
103 flags = flags | IFF_UP | IFF_RUNNING
105 flags = flags & ~IFF_UP & ~IFF_RUNNING
108 ifreq = struct.pack("16sH", vif_name, flags)
109 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFFLAGS, ifreq)
113 raise RuntimeError("Could not set device %s UP" % vif_name)
115 def set_ifaddr(vif_name, ip, prefix):
116 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
119 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
120 socket.inet_aton(ip), "\x00"*8)
121 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFADDR, ifreq)
125 raise RuntimeError("Could not set IP address for device %s" % vif_name)
127 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - prefix)) & 0xffffffff))
128 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
129 socket.inet_aton(netmask), "\x00"*8)
130 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFNETMASK, ifreq)
134 raise RuntimeError("Could not set IP mask for device %s" % vif_name)
136 def create_tap(vif_name, vif_type, pi):
143 # xxx : Thierry : not quite sure where this gets closed
144 fd = os.open("/dev/net/tun", os.O_RDWR)
146 ifreq = struct.pack("16sH", vif_name, flags)
147 ifr = fcntl.ioctl(fd, TUNSETIFF, ifreq)
150 raise RuntimeError("Could not configure device %s" % vif_name)
154 def passfd_action(fd, args):
155 """ Sends the file descriptor associated to the TAP device
156 to another process through a unix socket.
158 address = args.pop(0)
160 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
161 sock.connect(address)
162 passfd.sendfd(sock, fd, '0')
166 def down_action(fd, vif_name):
167 updown_action(fd, vif_name, False)
171 usage = ("usage: %prog -t <vif-type> -a <ip-address> -n <prefix> "
172 "-N <device-name> -p <pi> -S <socket-name>")
174 parser = OptionParser(usage = usage)
176 parser.add_option("-t", "--vif-type", dest="vif_type",
177 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
178 "Defaults to IFF_TAP. ", type="str")
180 parser.add_option("-a", "--ip-address", dest="ip_address",
181 help = "IPv4 address to assign to interface.",
184 parser.add_option("-n", "--prefix", dest="prefix",
185 help = "IPv4 network prefix for the interface. ",
188 parser.add_option("-N", "--vif-name", dest="vif_name",
189 help="The name of the virtual interface", type="str")
191 parser.add_option("-p", "--pi", dest="pi", action="store_true",
192 default=False, help="Enable PI header")
194 parser.add_option("-S", "--socket-name", dest="socket_name",
195 help = "Name for the unix socket used to interact with this process",
198 (options, args) = parser.parse_args()
201 if options.vif_type and options.vif_type == "IFF_TUN":
204 return (vif_type, options.vif_name, options.ip_address, options.prefix,
205 options.pi, options.socket_name)
207 if __name__ == '__main__':
209 (vif_type, vif_name, ip_address, prefix, pi, socket_name) = get_options()
211 # create and configure the virtual device
212 fd = create_tap(vif_name, vif_type, pi)
213 set_ifaddr(vif_name, ip_address, prefix)
214 set_ifupdown(vif_name, True)
216 # create unix socket to receive instructions
217 sock = create_socket(socket_name)
220 # wait for messages to arrive and process them
224 conn, addr = sock.accept()
229 (msg, args) = recv_msg(conn)
230 except socket.timeout, e:
235 # Ignore - connection lost
240 reply = stop_action(vif_name)
241 elif msg == PASSFD_MSG:
242 reply = passfd_action(fd, args)
245 send_reply(conn, reply)