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>
27 from optparse import OptionParser
37 TUNSETIFF = 0x400454ca
42 SIOCGIFNETMASK = 0x891B
43 SIOCSIFNETMASK = 0x891C
46 def create_socket(socket_name):
47 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
48 sock.bind(socket_name)
55 while '\n' not in chunk:
57 chunk = conn.recv(1024)
58 except (OSError, socket.error), e:
59 if e[0] != errno.EINTR:
70 msg = ''.join(msg).split('\n')[0]
71 # The message might have arguments that will be appended
72 # as a '|' separated list after the message type
76 dmsg = base64.b64decode(msg)
79 darg = base64.b64decode(arg)
80 dargs.append(darg.rstrip())
82 return (dmsg.rstrip(), dargs)
84 def send_reply(conn, reply):
85 encoded = base64.b64encode(reply)
86 conn.send("%s\n" % encoded)
88 def set_ifupdown(vif_name, up):
89 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
92 ifreq = struct.pack("16sH", vif_name, 0)
93 ifr = fcntl.ioctl(sock.fileno(), SIOCGIFFLAGS, ifreq)
97 raise RuntimeError("Could not set device %s UP" % vif_name)
99 name, flags = struct.unpack("16sH", ifr)
101 flags = flags | IFF_UP | IFF_RUNNING
103 flags = flags & ~IFF_UP & ~IFF_RUNNING
106 ifreq = struct.pack("16sH", vif_name, flags)
107 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFFLAGS, ifreq)
111 raise RuntimeError("Could not set device %s UP" % vif_name)
113 def set_ifaddr(vif_name, ip, prefix):
114 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
117 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
118 socket.inet_aton(ip), "\x00"*8)
119 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFADDR, ifreq)
123 raise RuntimeError("Could not set IP address for device %s" % vif_name)
125 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - prefix)) & 0xffffffff))
126 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
127 socket.inet_aton(netmask), "\x00"*8)
128 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFNETMASK, ifreq)
132 raise RuntimeError("Could not set IP mask for device %s" % vif_name)
134 def create_tap(vif_name, vif_type, pi):
141 fd = os.open("/dev/net/tun", os.O_RDWR)
143 ifreq = struct.pack("16sH", vif_name, flags)
144 ifr = fcntl.ioctl(fd, TUNSETIFF, ifreq)
147 raise RuntimeError("Could not configure device %s" % vif_name)
151 def passfd_action(fd, args):
152 """ Sends the file descriptor associated to the TAP device
153 to another process through a unix socket.
155 address = args.pop(0)
157 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
158 sock.connect(address)
159 passfd.sendfd(sock, fd, '0')
163 def down_action(fd, vif_name):
164 updown_action(fd, vif_name, False)
168 usage = ("usage: %prog -t <vif-type> -a <ip-address> -n <prefix> "
169 "-N <device-name> -p <pi> -S <socket-name>")
171 parser = OptionParser(usage = usage)
173 parser.add_option("-t", "--vif-type", dest="vif_type",
174 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
175 "Defaults to IFF_TAP. ", type="str")
177 parser.add_option("-a", "--ip-address", dest="ip_address",
178 help = "IPv4 address to assign to interface.",
181 parser.add_option("-n", "--prefix", dest="prefix",
182 help = "IPv4 network prefix for the interface. ",
185 parser.add_option("-N", "--vif-name", dest="vif_name",
186 help="The name of the virtual interface", type="str")
188 parser.add_option("-p", "--pi", dest="pi", action="store_true",
189 default=False, help="Enable PI header")
191 parser.add_option("-S", "--socket-name", dest="socket_name",
192 help = "Name for the unix socket used to interact with this process",
195 (options, args) = parser.parse_args()
198 if options.vif_type and options.vif_type == "IFF_TUN":
201 return (vif_type, options.vif_name, options.ip_address, options.prefix,
202 options.pi, options.socket_name)
204 if __name__ == '__main__':
206 (vif_type, vif_name, ip_address, prefix, pi, socket_name) = get_options()
208 # create and configure the virtual device
209 fd = create_tap(vif_name, vif_type, pi)
210 set_ifaddr(vif_name, ip_address, prefix)
211 set_ifupdown(vif_name, True)
213 # create unix socket to receive instructions
214 sock = create_socket(socket_name)
217 # wait for messages to arrive and process them
221 conn, addr = sock.accept()
226 (msg, args) = recv_msg(conn)
227 except socket.timeout, e:
232 # Ignore - connection lost
237 reply = stop_action(vif_name)
238 elif msg == PASSFD_MSG:
239 reply = passfd_action(fd, args)
242 send_reply(conn, reply)