summaryrefslogtreecommitdiff
path: root/ipacm/inc/IPACM_Netlink.h
blob: 7a1a9390aef60a628d0906885e409f440817ce38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* 
Copyright (c) 2013, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*!
	@file
	IPA_Netlink.h

	@brief
	IPACM Netlink Messaging Implementation File

	@Author
	Skylar Chang

*/
#ifndef IPACM_NETLINK_H
#define IPACM_NETLINK_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <linux/socket.h>
#include <linux/if.h>
#include <linux/if_addr.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
#include <netinet/in.h>
#include "IPACM_Defs.h"

#define MAX_NUM_OF_FD 10
#define IPA_NL_MSG_MAX_LEN (2048)

/*--------------------------------------------------------------------------- 
	 Type representing enumeration of NetLink event indication messages
---------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------- 
	 Types representing parsed NetLink message
---------------------------------------------------------------------------*/
#define IPA_NLA_PARAM_NONE        (0x0000)
#define IPA_NLA_PARAM_PREFIXADDR  (0x0001)
#define IPA_NLA_PARAM_LOCALADDR   (0x0002)
#define IPA_NLA_PARAM_LABELNAME   (0x0004)
#define IPA_NLA_PARAM_BCASTADDR   (0x0008)
#define IPA_NLA_PARAM_ACASTADDR   (0x0010)
#define IPA_NLA_PARAM_MCASTADDR   (0x0020)
#define IPA_NLA_PARAM_CACHEINFO   (0x0080)
#define IPA_NLA_PARAM_PROTOINFO   (0x0100)
#define IPA_NLA_PARAM_FLAGS       (0x0200)

#define IPA_RTA_PARAM_NONE        (0x0000)
#define IPA_RTA_PARAM_DST         (0x0001)
#define IPA_RTA_PARAM_SRC         (0x0002)
#define IPA_RTA_PARAM_GATEWAY     (0x0004)
#define IPA_RTA_PARAM_IIF         (0x0008)
#define IPA_RTA_PARAM_OIF         (0x0010)
#define IPA_RTA_PARAM_CACHEINFO   (0x0020)
#define IPA_RTA_PARAM_PRIORITY    (0x0080)
#define IPA_RTA_PARAM_METRICS     (0x0100)


/*--------------------------------------------------------------------------- 
	 Type representing function callback registered with a socket listener 
	 thread for reading from a socket on receipt of an incoming message
---------------------------------------------------------------------------*/
typedef int (*ipa_sock_thrd_fd_read_f)(int fd);

typedef enum
{
	IPA_INIT = 0,
	IPA_LINK_UP_WAIT,
	IPA_LINK_UP,
	IPA_LINK_DOWN_WAIT,
	IPA_LINK_DOWN
} ipa_nl_state_e;

typedef struct
{
	int sk_fd;
	ipa_sock_thrd_fd_read_f read_func;
} ipa_nl_sk_fd_map_info_t;

typedef struct
{
	ipa_nl_sk_fd_map_info_t sk_fds[MAX_NUM_OF_FD];
	fd_set fdset;
	int num_fd;
	int max_fd;
} ipa_nl_sk_fd_set_info_t;

typedef struct
{
	int                 sk_fd;       /* socket descriptor */
	struct sockaddr_nl  sk_addr_loc; /* local address of socket */
} ipa_nl_sk_info_t;

typedef struct ipa_nl_addr_s {
	struct sockaddr_storage        ip_addr;
	unsigned int                   mask;
} ipa_nl_addr_t;

typedef struct ipa_nl_proto_info_s {
	unsigned int                    param_mask;
	unsigned int                    flags;
	struct ifla_cacheinfo           cache_info;
} ipa_nl_proto_info_t;

typedef struct
{
	struct ifinfomsg  metainfo;                   /* from header */
} ipa_nl_link_info_t;



typedef struct ipa_nl_addr_info_s {
	struct ifaddrmsg                metainfo;     /* from header */
	struct                                      /* attributes  */
	{
		unsigned int                  param_mask;
		unsigned char                 label_name[IF_NAME_LEN];
		struct sockaddr_storage       prefix_addr;
		struct sockaddr_storage       local_addr;
		struct sockaddr_storage       bcast_addr;
		struct sockaddr_storage       acast_addr;
		struct sockaddr_storage       mcast_addr;
	} attr_info;
} ipa_nl_addr_info_t;


typedef struct ipa_nl_neigh_info_s {
	struct ndmsg                metainfo;     /* from header */
	struct                                  /* attributes  */
	{
		unsigned int                param_mask;
		struct sockaddr_storage     local_addr;
		struct  sockaddr            lladdr_hwaddr;
	} attr_info;
} ipa_nl_neigh_info_t;



typedef struct ipa_nl_route_info_s {
	struct rtmsg                    metainfo;     /* from header */
	struct                                      /* attributes  */
	{
		unsigned int                  param_mask;
		struct sockaddr_storage       dst_addr;
		struct sockaddr_storage       src_addr;
		struct sockaddr_storage       gateway_addr;
		struct sockaddr_storage       mark_addr;
		struct rta_cacheinfo          cache_info;
		__u32		iif_index;                      /* Link index  */
		__u32		oif_index;                      /* Link index  */
		__u32       priority;
		__u32       metrics;
		ipa_nl_proto_info_t        proto_info;
	} attr_info;
} ipa_nl_route_info_t;

#define IPA_FLOW_TYPE_INVALID      (-1)

typedef struct
{
	unsigned int type;
	bool link_event;
	/* Optional parameters */
	ipa_nl_link_info_t      nl_link_info;
	ipa_nl_addr_info_t      nl_addr_info;
	ipa_nl_neigh_info_t      nl_neigh_info;
	ipa_nl_route_info_t      nl_route_info;
} ipa_nl_msg_t;

/* Initialization routine for listener on NetLink sockets interface */
int ipa_nl_listener_init
(
	 unsigned int nl_type,
	 unsigned int nl_groups,
	 ipa_nl_sk_fd_set_info_t *sk_fdset,
	 ipa_sock_thrd_fd_read_f read_f
	 );

/*  Virtual function registered to receive incoming messages over the NETLINK routing socket*/
int ipa_nl_recv_msg(int fd);

/* map mask value for ipv6 */
int mask_v6(int index, uint32_t *mask);

#ifdef __cplusplus
}
#endif

#endif /* IPACM_NETLINK_H */