Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Network utilities for the OpenFlow controller |
| 5 | """ |
| 6 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 7 | ########################################################################### |
| 8 | ## ## |
| 9 | ## Promiscuous mode enable/disable ## |
| 10 | ## ## |
| 11 | ## Based on code from Scapy by Phillippe Biondi ## |
| 12 | ## ## |
| 13 | ## ## |
| 14 | ## This program is free software; you can redistribute it and/or modify it ## |
| 15 | ## under the terms of the GNU General Public License as published by the ## |
| 16 | ## Free Software Foundation; either version 2, or (at your option) any ## |
| 17 | ## later version. ## |
| 18 | ## ## |
| 19 | ## This program is distributed in the hope that it will be useful, but ## |
| 20 | ## WITHOUT ANY WARRANTY; without even the implied warranty of ## |
| 21 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## |
| 22 | ## General Public License for more details. ## |
| 23 | ## ## |
| 24 | ############################################################################# |
| 25 | |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 26 | import socket |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 27 | from fcntl import ioctl |
| 28 | import struct |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 29 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 30 | # From net/if_arp.h |
| 31 | ARPHDR_ETHER = 1 |
| 32 | ARPHDR_LOOPBACK = 772 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 33 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 34 | # From bits/ioctls.h |
| 35 | SIOCGIFHWADDR = 0x8927 # Get hardware address |
| 36 | SIOCGIFINDEX = 0x8933 # name -> if_index mapping |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 37 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 38 | # From netpacket/packet.h |
| 39 | PACKET_ADD_MEMBERSHIP = 1 |
| 40 | PACKET_MR_PROMISC = 1 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 41 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 42 | # From bits/socket.h |
| 43 | SOL_PACKET = 263 |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 44 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 45 | def get_if(iff,cmd): |
| 46 | s=socket.socket() |
| 47 | ifreq = ioctl(s, cmd, struct.pack("16s16x",iff)) |
| 48 | s.close() |
| 49 | return ifreq |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 50 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 51 | def get_if_hwaddr(iff): |
| 52 | addrfamily, mac = struct.unpack("16xh6s8x",get_if(iff,SIOCGIFHWADDR)) |
| 53 | if addrfamily in [ARPHDR_ETHER,ARPHDR_LOOPBACK]: |
| 54 | return str2mac(mac) |
| 55 | else: |
| 56 | raise Exception("Unsupported address family (%i)"%addrfamily) |
Dan Talayco | e37999f | 2010-02-09 15:27:12 -0800 | [diff] [blame] | 57 | |
Dan Talayco | 19dbc79 | 2010-02-12 23:00:54 -0800 | [diff] [blame] | 58 | def get_if_index(iff): |
| 59 | return int(struct.unpack("I",get_if(iff, SIOCGIFINDEX)[16:20])[0]) |
| 60 | |
| 61 | def set_promisc(s,iff,val=1): |
| 62 | mreq = struct.pack("IHH8s", get_if_index(iff), PACKET_MR_PROMISC, 0, "") |
| 63 | if val: |
| 64 | cmd = PACKET_ADD_MEMBERSHIP |
| 65 | else: |
| 66 | cmd = PACKET_DROP_MEMBERSHIP |
| 67 | s.setsockopt(SOL_PACKET, cmd, mreq) |
| 68 | |