Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 1 | |
| 2 | """ |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 3 | Network utilities for the OpenFlow test framework |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 4 | """ |
| 5 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 6 | ########################################################################### |
| 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 Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 25 | import socket |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 26 | from fcntl import ioctl |
| 27 | import struct |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 28 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 29 | # From net/if_arp.h |
| 30 | ARPHDR_ETHER = 1 |
| 31 | ARPHDR_LOOPBACK = 772 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 32 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 33 | # From bits/ioctls.h |
| 34 | SIOCGIFHWADDR = 0x8927 # Get hardware address |
| 35 | SIOCGIFINDEX = 0x8933 # name -> if_index mapping |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 36 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 37 | # From netpacket/packet.h |
| 38 | PACKET_ADD_MEMBERSHIP = 1 |
| 39 | PACKET_MR_PROMISC = 1 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 40 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 41 | # From bits/socket.h |
| 42 | SOL_PACKET = 263 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 43 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 44 | def get_if(iff,cmd): |
| 45 | s=socket.socket() |
| 46 | ifreq = ioctl(s, cmd, struct.pack("16s16x",iff)) |
| 47 | s.close() |
| 48 | return ifreq |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 49 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 50 | def get_if_hwaddr(iff): |
| 51 | addrfamily, mac = struct.unpack("16xh6s8x",get_if(iff,SIOCGIFHWADDR)) |
| 52 | if addrfamily in [ARPHDR_ETHER,ARPHDR_LOOPBACK]: |
| 53 | return str2mac(mac) |
| 54 | else: |
| 55 | raise Exception("Unsupported address family (%i)"%addrfamily) |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 56 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 57 | def get_if_index(iff): |
| 58 | return int(struct.unpack("I",get_if(iff, SIOCGIFINDEX)[16:20])[0]) |
| 59 | |
| 60 | def set_promisc(s,iff,val=1): |
| 61 | mreq = struct.pack("IHH8s", get_if_index(iff), PACKET_MR_PROMISC, 0, "") |
| 62 | if val: |
| 63 | cmd = PACKET_ADD_MEMBERSHIP |
| 64 | else: |
| 65 | cmd = PACKET_DROP_MEMBERSHIP |
| 66 | s.setsockopt(SOL_PACKET, cmd, mreq) |
| 67 | |