blob: c7c8051df1e27a4ed4171078ea71d320116416df [file] [log] [blame]
Dan Talaycoe37999f2010-02-09 15:27:12 -08001
2"""
Dan Talaycod7e2dbe2010-02-13 21:51:15 -08003Network utilities for the OpenFlow test framework
Dan Talaycoe37999f2010-02-09 15:27:12 -08004"""
5
Dan Talayco19dbc792010-02-12 23:00:54 -08006###########################################################################
7## ##
8## Promiscuous mode enable/disable ##
9## ##
10## Based on code from Scapy by Phillippe Biondi ##
11## ##
12## ##
13## This program is free software; you can redistribute it and/or modify it ##
14## under the terms of the GNU General Public License as published by the ##
15## Free Software Foundation; either version 2, or (at your option) any ##
16## later version. ##
17## ##
18## This program is distributed in the hope that it will be useful, but ##
19## WITHOUT ANY WARRANTY; without even the implied warranty of ##
20## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ##
21## General Public License for more details. ##
22## ##
23#############################################################################
24
Dan Talaycoe37999f2010-02-09 15:27:12 -080025import socket
Dan Talayco19dbc792010-02-12 23:00:54 -080026from fcntl import ioctl
27import struct
Dan Talaycoe37999f2010-02-09 15:27:12 -080028
Dan Talayco19dbc792010-02-12 23:00:54 -080029# From net/if_arp.h
30ARPHDR_ETHER = 1
31ARPHDR_LOOPBACK = 772
Dan Talaycoe37999f2010-02-09 15:27:12 -080032
Dan Talayco19dbc792010-02-12 23:00:54 -080033# From bits/ioctls.h
34SIOCGIFHWADDR = 0x8927 # Get hardware address
35SIOCGIFINDEX = 0x8933 # name -> if_index mapping
Dan Talaycoe37999f2010-02-09 15:27:12 -080036
Dan Talayco19dbc792010-02-12 23:00:54 -080037# From netpacket/packet.h
38PACKET_ADD_MEMBERSHIP = 1
Rich Lanee5779d32012-10-05 17:56:04 -070039PACKET_DROP_MEMBERSHIP = 2
Dan Talayco19dbc792010-02-12 23:00:54 -080040PACKET_MR_PROMISC = 1
Dan Talaycoe37999f2010-02-09 15:27:12 -080041
Dan Talayco19dbc792010-02-12 23:00:54 -080042# From bits/socket.h
43SOL_PACKET = 263
Dan Talaycoe37999f2010-02-09 15:27:12 -080044
Dan Talayco19dbc792010-02-12 23:00:54 -080045def get_if(iff,cmd):
46 s=socket.socket()
47 ifreq = ioctl(s, cmd, struct.pack("16s16x",iff))
48 s.close()
49 return ifreq
Dan Talaycoe37999f2010-02-09 15:27:12 -080050
Dan Talayco19dbc792010-02-12 23:00:54 -080051def get_if_index(iff):
52 return int(struct.unpack("I",get_if(iff, SIOCGIFINDEX)[16:20])[0])
53
54def set_promisc(s,iff,val=1):
55 mreq = struct.pack("IHH8s", get_if_index(iff), PACKET_MR_PROMISC, 0, "")
56 if val:
57 cmd = PACKET_ADD_MEMBERSHIP
58 else:
59 cmd = PACKET_DROP_MEMBERSHIP
60 s.setsockopt(SOL_PACKET, cmd, mreq)
61