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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
28 from optparse import OptionParser
38 TUNSETIFF = 0x400454ca
43 SIOCGIFNETMASK = 0x891B
44 SIOCSIFNETMASK = 0x891C
47 def create_socket(socket_name):
48 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
49 sock.bind(socket_name)
56 while '\n' not in chunk:
58 chunk = conn.recv(1024)
59 except (OSError, socket.error), e:
60 if e[0] != errno.EINTR:
71 msg = ''.join(msg).split('\n')[0]
72 # The message might have arguments that will be appended
73 # as a '|' separated list after the message type
77 dmsg = base64.b64decode(msg)
80 darg = base64.b64decode(arg)
81 dargs.append(darg.rstrip())
83 return (dmsg.rstrip(), dargs)
85 def send_reply(conn, reply):
86 encoded = base64.b64encode(reply)
87 conn.send("%s\n" % encoded)
89 def set_ifupdown(vif_name, up):
90 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
93 ifreq = struct.pack("16sH", vif_name, 0)
94 ifr = fcntl.ioctl(sock.fileno(), SIOCGIFFLAGS, ifreq)
98 raise RuntimeError("Could not set device %s UP" % vif_name)
100 name, flags = struct.unpack("16sH", ifr)
102 flags = flags | IFF_UP | IFF_RUNNING
104 flags = flags & ~IFF_UP & ~IFF_RUNNING
107 ifreq = struct.pack("16sH", vif_name, flags)
108 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFFLAGS, ifreq)
112 raise RuntimeError("Could not set device %s UP" % vif_name)
114 def set_ifaddr(vif_name, ip, prefix):
115 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
118 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
119 socket.inet_aton(ip), "\x00"*8)
120 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFADDR, ifreq)
124 raise RuntimeError("Could not set IP address for device %s" % vif_name)
126 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - prefix)) & 0xffffffff))
127 ifreq = struct.pack("16sH2s4s8s", vif_name, socket.AF_INET, "\x00"*2,
128 socket.inet_aton(netmask), "\x00"*8)
129 ifr = fcntl.ioctl(sock.fileno(), SIOCSIFNETMASK, ifreq)
133 raise RuntimeError("Could not set IP mask for device %s" % vif_name)
135 def create_tap(vif_name, vif_type, pi):
142 fd = os.open("/dev/net/tun", os.O_RDWR)
144 ifreq = struct.pack("16sH", vif_name, flags)
145 ifr = fcntl.ioctl(fd, TUNSETIFF, ifreq)
148 raise RuntimeError("Could not configure device %s" % vif_name)
152 def passfd_action(fd, args):
153 """ Sends the file descriptor associated to the TAP device
154 to another process through a unix socket.
156 address = args.pop(0)
158 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
159 sock.connect(address)
160 passfd.sendfd(sock, fd, '0')
164 def down_action(fd, vif_name):
165 updown_action(fd, vif_name, False)
169 usage = ("usage: %prog -t <vif-type> -a <ip-address> -n <prefix> "
170 "-N <device-name> -p <pi> -S <socket-name>")
172 parser = OptionParser(usage = usage)
174 parser.add_option("-t", "--vif-type", dest="vif_type",
175 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
176 "Defaults to IFF_TAP. ", type="str")
178 parser.add_option("-a", "--ip-address", dest="ip_address",
179 help = "IPv4 address to assign to interface.",
182 parser.add_option("-n", "--prefix", dest="prefix",
183 help = "IPv4 network prefix for the interface. ",
186 parser.add_option("-N", "--vif-name", dest="vif_name",
187 help="The name of the virtual interface", type="str")
189 parser.add_option("-p", "--pi", dest="pi", action="store_true",
190 default=False, help="Enable PI header")
192 parser.add_option("-S", "--socket-name", dest="socket_name",
193 help = "Name for the unix socket used to interact with this process",
196 (options, args) = parser.parse_args()
199 if options.vif_type and options.vif_type == "IFF_TUN":
202 return (vif_type, options.vif_name, options.ip_address, options.prefix,
203 options.pi, options.socket_name)
205 if __name__ == '__main__':
207 (vif_type, vif_name, ip_address, prefix, pi, socket_name) = get_options()
209 # create and configure the virtual device
210 fd = create_tap(vif_name, vif_type, pi)
211 set_ifaddr(vif_name, ip_address, prefix)
212 set_ifupdown(vif_name, True)
214 # create unix socket to receive instructions
215 sock = create_socket(socket_name)
218 # wait for messages to arrive and process them
222 conn, addr = sock.accept()
227 (msg, args) = recv_msg(conn)
228 except socket.timeout, e:
233 # Ignore - connection lost
238 reply = stop_action(vif_name)
239 elif msg == PASSFD_MSG:
240 reply = passfd_action(fd, args)
243 send_reply(conn, reply)