blob: 09a383509181b5e4aaea207b076259257193df24 [file] [log] [blame]
Dan Talaycoe37999f2010-02-09 15:27:12 -08001
Matteo Scandoloa229eca2017-08-08 13:05:28 -07002# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17
Dan Talaycoe37999f2010-02-09 15:27:12 -080018"""
Dan Talaycod7e2dbe2010-02-13 21:51:15 -080019Network utilities for the OpenFlow test framework
Dan Talaycoe37999f2010-02-09 15:27:12 -080020"""
21
Dan Talayco19dbc792010-02-12 23:00:54 -080022###########################################################################
23## ##
24## Promiscuous mode enable/disable ##
25## ##
26## Based on code from Scapy by Phillippe Biondi ##
27## ##
28## ##
29## This program is free software; you can redistribute it and/or modify it ##
30## under the terms of the GNU General Public License as published by the ##
31## Free Software Foundation; either version 2, or (at your option) any ##
32## later version. ##
33## ##
34## This program is distributed in the hope that it will be useful, but ##
35## WITHOUT ANY WARRANTY; without even the implied warranty of ##
36## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ##
37## General Public License for more details. ##
38## ##
39#############################################################################
40
Dan Talaycoe37999f2010-02-09 15:27:12 -080041import socket
Dan Talayco19dbc792010-02-12 23:00:54 -080042from fcntl import ioctl
43import struct
Dan Talaycoe37999f2010-02-09 15:27:12 -080044
Dan Talayco19dbc792010-02-12 23:00:54 -080045# From net/if_arp.h
46ARPHDR_ETHER = 1
47ARPHDR_LOOPBACK = 772
Dan Talaycoe37999f2010-02-09 15:27:12 -080048
Dan Talayco19dbc792010-02-12 23:00:54 -080049# From bits/ioctls.h
50SIOCGIFHWADDR = 0x8927 # Get hardware address
51SIOCGIFINDEX = 0x8933 # name -> if_index mapping
Dan Talaycoe37999f2010-02-09 15:27:12 -080052
Dan Talayco19dbc792010-02-12 23:00:54 -080053# From netpacket/packet.h
54PACKET_ADD_MEMBERSHIP = 1
Rich Lanee5779d32012-10-05 17:56:04 -070055PACKET_DROP_MEMBERSHIP = 2
Dan Talayco19dbc792010-02-12 23:00:54 -080056PACKET_MR_PROMISC = 1
Dan Talaycoe37999f2010-02-09 15:27:12 -080057
Dan Talayco19dbc792010-02-12 23:00:54 -080058# From bits/socket.h
59SOL_PACKET = 263
Dan Talaycoe37999f2010-02-09 15:27:12 -080060
Dan Talayco19dbc792010-02-12 23:00:54 -080061def get_if(iff,cmd):
62 s=socket.socket()
63 ifreq = ioctl(s, cmd, struct.pack("16s16x",iff))
64 s.close()
65 return ifreq
Dan Talaycoe37999f2010-02-09 15:27:12 -080066
Dan Talayco19dbc792010-02-12 23:00:54 -080067def get_if_index(iff):
68 return int(struct.unpack("I",get_if(iff, SIOCGIFINDEX)[16:20])[0])
69
70def set_promisc(s,iff,val=1):
71 mreq = struct.pack("IHH8s", get_if_index(iff), PACKET_MR_PROMISC, 0, "")
72 if val:
73 cmd = PACKET_ADD_MEMBERSHIP
74 else:
75 cmd = PACKET_DROP_MEMBERSHIP
76 s.setsockopt(SOL_PACKET, cmd, mreq)
77