1# encoding: utf-8
2"""
3link.py
4
5Created by Thomas Mangin on 2015-03-31.
6Copyright (c) 2009-2017 Exa Networks. All rights reserved.
7License: 3-clause BSD. (See the COPYRIGHT file)
8"""
9
10import socket
11from struct import calcsize
12from collections import namedtuple
13
14from exabgp.netlink.message import Message
15
16
17# 0                   1                   2                   3
18# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
19# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20# |   Family    |   Reserved  |          Device Type              |
21# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22# |                     Interface Index                           |
23# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24# |                      Device Flags                             |
25# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26# |                      Change Mask                              |
27# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28
29
30class Link(Message):
31    class Header(object):
32        PACK = 'BxHiII'
33        LEN = calcsize(PACK)
34
35    # linux/if_link.h
36    format = namedtuple('Info', 'family type index flags change attributes')
37
38    class Command(object):
39        # linux/rtnetlink.h
40        RTM_NEWLINK = 0x10  # Create a new network interface
41        RTM_DELLINK = 0x11  # Destroy a network interface
42        RTM_GETLINK = 0x12  # Retrieve information about a network interface (ifinfomsg)
43        RTM_SETLINK = 0x13  # -
44
45    class Type(object):
46        class Family(object):
47            AF_INET = socket.AF_INET
48            AF_INET6 = socket.AF_INET6
49
50        class Device(object):
51            IFF_UP = 0x0001  # Interface is administratively up.
52            IFF_BROADCAST = 0x0002  # Valid broadcast address set.
53            IFF_DEBUG = 0x0004  # Internal debugging flag.
54            IFF_LOOPBACK = 0x0008  # Interface is a loopback interface.
55            IFF_POINTOPOINT = 0x0010  # Interface is a point-to-point link.
56            IFF_NOTRAILERS = 0x0020  # Avoid use of trailers.
57            IFF_RUNNING = 0x0040  # Interface is operationally up.
58            IFF_NOARP = 0x0080  # No ARP protocol needed for this interface.
59            IFF_PROMISC = 0x0100  # Interface is in promiscuous mode.
60            IFF_ALLMULTI = 0x0200  # Receive all multicast packets.
61            IFF_MASTER = 0x0400  # Master of a load balancing bundle.
62            IFF_SLAVE = 0x0800  # Slave of a load balancing bundle.
63            IFF_MULTICAST = 0x1000  # Supports multicast.
64
65            IFF_PORTSEL = 0x2000  # Is able to select media type via ifmap.
66            IFF_AUTOMEDIA = 0x4000  # Auto media selection active.
67            IFF_DYNAMIC = 0x8000  # Interface was dynamically created.
68
69            IFF_LOWER_UP = 0x10000  # driver signals L1 up
70            IFF_DORMANT = 0x20000  # driver signals dormant
71            IFF_ECHO = 0x40000  # echo sent packet
72
73        class Attribute(object):
74            IFLA_UNSPEC = 0x00
75            IFLA_ADDRESS = 0x01
76            IFLA_BROADCAST = 0x02
77            IFLA_IFNAME = 0x03
78            IFLA_MTU = 0x04
79            IFLA_LINK = 0x05
80            IFLA_QDISC = 0x06
81            IFLA_STATS = 0x07
82
83    @classmethod
84    def getLinks(cls):
85        return cls.extract(Link.Command.RTM_GETLINK)
86