userspace: Define and use struct eth_addr.
[cascardo/ovs.git] / lib / lldp / lldpd-structs.h
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3  * Copyright (c) 2015 Nicira, Inc.
4  * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef _LLDPD_STRUCTS_H
20 #define _LLDPD_STRUCTS_H
21
22 #include <net/if.h>
23 #ifndef _WIN32
24 #include <netinet/in.h>
25 #endif
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include "aa-structs.h"
29 #include "lldp-const.h"
30 #include "packets.h"
31
32 enum {
33     LLDPD_AF_UNSPEC = 0,
34     LLDPD_AF_IPV4,
35     LLDPD_AF_IPV6,
36     LLDPD_AF_LAST
37 };
38
39 inline static int
40 lldpd_af(int af)
41 {
42     switch (af) {
43     case LLDPD_AF_IPV4: return AF_INET;
44     case LLDPD_AF_IPV6: return AF_INET6;
45     case LLDPD_AF_LAST: return AF_MAX;
46     default: return AF_UNSPEC;
47     }
48 }
49
50 #define LLDPD_MGMT_MAXADDRSIZE 16 /* sizeof(struct in6_addr) */
51 struct lldpd_mgmt {
52     struct ovs_list m_entries;
53     int             m_family;
54     union {
55         struct in_addr  inet;
56         struct in6_addr inet6;
57         u_int8_t        octets[LLDPD_MGMT_MAXADDRSIZE];
58     } m_addr;
59     size_t    m_addrsize;
60     u_int32_t m_iface;
61 };
62
63 struct lldpd_chassis {
64     struct ovs_list list;
65     u_int16_t       c_refcount;   /* Reference count by ports */
66     u_int16_t       c_index;      /* Monotonic index */
67     u_int8_t        c_protocol;   /* Protocol used to get this chassis */
68     u_int8_t        c_id_subtype;
69     uint8_t         *c_id;        /* Typically an Ethernet address. */
70     int             c_id_len;
71     char            *c_name;
72     char            *c_descr;
73
74     u_int16_t       c_cap_available;
75     u_int16_t       c_cap_enabled;
76
77     u_int16_t       c_ttl;
78
79     struct ovs_list c_mgmt;     /* Contains "struct lldp_mgmt"s. */
80 };
81 /* WARNING: any change to this structure should also be reflected into
82    `lldpd_copy_chassis()` which is not using marshaling. */
83
84 struct lldpd_port {
85     struct ovs_list      p_entries;
86     struct lldpd_chassis *p_chassis; /* Attached chassis */
87     time_t               p_lastchange; /* Time of last change of values */
88     time_t               p_lastupdate; /* Time of last update received */
89     struct lldpd_frame   *p_lastframe; /* Frame received during last update */
90     u_int8_t             p_protocol;   /* Protocol used to get this port */
91     u_int8_t             p_hidden_in:1; /* Considered hidden for reception */
92     u_int8_t             p_hidden_out:1; /* Considered hidden for emission */
93     /* Important: all fields that should be ignored to check if a port has
94      * been changed should be before p_id_subtype. Check
95      * `lldpd_reset_timer()`.
96      */
97     u_int8_t             p_id_subtype;
98     char                 *p_id;
99     int                  p_id_len;
100     char                 *p_descr;
101     u_int16_t            p_mfs;
102     struct lldpd_aa_element_tlv        p_element;
103     struct ovs_list p_isid_vlan_maps; /* Contains "struct lldpd_aa_isid_vlan_maps_tlv"s. */
104 };
105
106 /* Smart mode / Hide mode */
107 #define SMART_INCOMING_FILTER     (1<<0) /* Incoming filtering enabled */
108 #define SMART_INCOMING_ONE_PROTO  (1<<1) /* On reception, keep only 1 proto */
109 #define SMART_INCOMING_ONE_NEIGH  (1<<2) /* On recep., keep only 1 neighbor */
110 #define SMART_OUTGOING_FILTER     (1<<3) /* Outgoing filtering enabled */
111 #define SMART_OUTGOING_ONE_PROTO  (1<<4) /* On emission, keep only one proto */
112 #define SMART_OUTGOING_ONE_NEIGH  (1<<5) /* On emission, consider only
113                                             one neighbor */
114 #define SMART_INCOMING (SMART_INCOMING_FILTER | \
115              SMART_INCOMING_ONE_PROTO |         \
116              SMART_INCOMING_ONE_NEIGH)
117 #define SMART_OUTGOING (SMART_OUTGOING_FILTER | \
118             SMART_OUTGOING_ONE_PROTO |          \
119             SMART_OUTGOING_ONE_NEIGH)
120
121 struct lldpd_config {
122     int c_paused;           /* lldpd is paused */
123     int c_tx_interval;      /* Transmit interval */
124     int c_smart;            /* Bitmask for smart configuration (see SMART_*) */
125     int c_receiveonly;      /* Receive only mode */
126     int c_max_neighbors;    /* Maximum number of neighbors (per protocol) */
127
128     char *c_mgmt_pattern;   /* Pattern to match a management address */
129     char *c_cid_pattern;    /* Pattern to match interfaces to use for chassis
130                              * ID */
131     char *c_iface_pattern;  /* Pattern to match interfaces to use */
132
133     char *c_platform;       /* Override platform description (for CDP) */
134     char *c_description;    /* Override chassis description */
135     char *c_hostname;       /* Override system name */
136     int c_advertise_version; /* Should the precise version be advertised? */
137     int c_set_ifdescr;      /* Set interface description */
138     int c_promisc;          /* Interfaces should be in promiscuous mode */
139     int c_tx_hold;          /* Transmit hold */
140     int c_bond_slave_src_mac_type; /* Src mac type in lldp frames over bond
141                                     * slaves */
142     int c_lldp_portid_type; /* The PortID type */
143 };
144
145 struct lldpd_frame {
146     int size;
147     unsigned char frame[];
148 };
149
150 struct lldpd_hardware;
151 struct lldpd;
152 struct lldpd_ops {
153     int (*send)(struct lldpd *,
154                 struct lldpd_hardware *,
155                 char *, size_t); /* Function to send a frame */
156     int (*recv)(struct lldpd *,
157                 struct lldpd_hardware *,
158                 int, char *, size_t); /* Function to receive a frame */
159     int (*cleanup)(struct lldpd *, struct lldpd_hardware *); /* Cleanup */
160 };
161
162 /* An interface is uniquely identified by h_ifindex, h_ifname and h_ops. This
163  * means if an interface becomes enslaved, it will be considered as a new
164  * interface. The same applies for renaming and we include the index in case of
165  * renaming to an existing interface.
166  */
167 struct lldpd_hardware {
168     struct ovs_list   h_entries;
169
170     struct lldpd      *h_cfg;     /* Pointer to main configuration */
171     void              *h_recv;    /* FD for reception */
172     int               h_sendfd;   /* FD for sending, only used by h_ops */
173     int               h_mangle;   /* 1 if we have to mangle the MAC address */
174     struct lldpd_ops  *h_ops;     /* Hardware-dependent functions */
175     void              *h_data;    /* Hardware-dependent data */
176     void              *h_timer;   /* Timer for this port */
177
178     int               h_mtu;
179     int               h_flags;    /* Packets will be sent only
180                                    * if IFF_RUNNING. Will be
181                                    * removed if this is left
182                                    * to 0. */
183     int               h_ifindex;  /* Interface index, used by SNMP */
184     char              h_ifname[IFNAMSIZ]; /* Should be unique */
185     struct eth_addr   h_lladdr;
186
187     u_int64_t         h_tx_cnt;
188     u_int64_t         h_rx_cnt;
189     u_int64_t         h_rx_discarded_cnt;
190     u_int64_t         h_rx_unrecognized_cnt;
191     u_int64_t         h_ageout_cnt;
192     u_int64_t         h_insert_cnt;
193     u_int64_t         h_delete_cnt;
194     u_int64_t         h_drop_cnt;
195
196     u_int16_t         h_lport_cksum; /* Checksum on local port to see if there
197                                       * is a change
198                                       */
199     struct lldpd_port h_lport;  /* Port attached to this hardware port */
200     struct ovs_list h_rports;   /* Contains "struct lldp_port"s. */
201 };
202
203 struct lldpd_interface;
204 struct lldpd_interface_list;
205
206 struct lldpd_neighbor_change {
207     char              *ifname;
208 #define NEIGHBOR_CHANGE_DELETED -1
209 #define NEIGHBOR_CHANGE_ADDED    1
210 #define NEIGHBOR_CHANGE_UPDATED  0
211     int               state;
212     struct lldpd_port *neighbor;
213 };
214
215 /* Cleanup functions */
216 void lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *);
217 void lldpd_chassis_cleanup(struct lldpd_chassis *, bool all);
218 void lldpd_remote_cleanup(struct lldpd_hardware *,
219     void (*expire)(struct lldpd_hardware *, struct lldpd_port *), bool all);
220 void lldpd_port_cleanup(struct lldpd_port *, bool all);
221 void lldpd_config_cleanup(struct lldpd_config *);
222
223 #endif