1 /*
2 Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*!
30 	@file
31 	IPA_Netlink.h
32 
33 	@brief
34 	IPACM Netlink Messaging Implementation File
35 
36 	@Author
37 	Skylar Chang
38 
39 */
40 #ifndef IPACM_NETLINK_H
41 #define IPACM_NETLINK_H
42 
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <errno.h>
51 #include <pthread.h>
52 #include <sys/select.h>
53 #include <sys/socket.h>
54 #include <linux/if.h>
55 #include <linux/if_addr.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/netlink.h>
58 #include <netinet/in.h>
59 #include "IPACM_Defs.h"
60 
61 #define MAX_NUM_OF_FD 10
62 #define IPA_NL_MSG_MAX_LEN (2048)
63 
64 /*---------------------------------------------------------------------------
65 	 Type representing enumeration of NetLink event indication messages
66 ---------------------------------------------------------------------------*/
67 
68 /*---------------------------------------------------------------------------
69 	 Types representing parsed NetLink message
70 ---------------------------------------------------------------------------*/
71 #define IPA_NLA_PARAM_NONE        (0x0000)
72 #define IPA_NLA_PARAM_PREFIXADDR  (0x0001)
73 #define IPA_NLA_PARAM_LOCALADDR   (0x0002)
74 #define IPA_NLA_PARAM_LABELNAME   (0x0004)
75 #define IPA_NLA_PARAM_BCASTADDR   (0x0008)
76 #define IPA_NLA_PARAM_ACASTADDR   (0x0010)
77 #define IPA_NLA_PARAM_MCASTADDR   (0x0020)
78 #define IPA_NLA_PARAM_CACHEINFO   (0x0080)
79 #define IPA_NLA_PARAM_PROTOINFO   (0x0100)
80 #define IPA_NLA_PARAM_FLAGS       (0x0200)
81 
82 #define IPA_RTA_PARAM_NONE        (0x0000)
83 #define IPA_RTA_PARAM_DST         (0x0001)
84 #define IPA_RTA_PARAM_SRC         (0x0002)
85 #define IPA_RTA_PARAM_GATEWAY     (0x0004)
86 #define IPA_RTA_PARAM_IIF         (0x0008)
87 #define IPA_RTA_PARAM_OIF         (0x0010)
88 #define IPA_RTA_PARAM_CACHEINFO   (0x0020)
89 #define IPA_RTA_PARAM_PRIORITY    (0x0080)
90 #define IPA_RTA_PARAM_METRICS     (0x0100)
91 
92 
93 /*---------------------------------------------------------------------------
94 	 Type representing function callback registered with a socket listener
95 	 thread for reading from a socket on receipt of an incoming message
96 ---------------------------------------------------------------------------*/
97 typedef int (*ipa_sock_thrd_fd_read_f)(int fd);
98 
99 typedef enum
100 {
101 	IPA_INIT = 0,
102 	IPA_LINK_UP_WAIT,
103 	IPA_LINK_UP,
104 	IPA_LINK_DOWN_WAIT,
105 	IPA_LINK_DOWN
106 } ipa_nl_state_e;
107 
108 typedef struct
109 {
110 	int sk_fd;
111 	ipa_sock_thrd_fd_read_f read_func;
112 } ipa_nl_sk_fd_map_info_t;
113 
114 typedef struct
115 {
116 	ipa_nl_sk_fd_map_info_t sk_fds[MAX_NUM_OF_FD];
117 	fd_set fdset;
118 	int num_fd;
119 	int max_fd;
120 } ipa_nl_sk_fd_set_info_t;
121 
122 typedef struct
123 {
124 	int                 sk_fd;       /* socket descriptor */
125 	struct sockaddr_nl  sk_addr_loc; /* local address of socket */
126 } ipa_nl_sk_info_t;
127 
128 typedef struct ipa_nl_addr_s {
129 	struct sockaddr_storage        ip_addr;
130 	unsigned int                   mask;
131 } ipa_nl_addr_t;
132 
133 typedef struct ipa_nl_proto_info_s {
134 	unsigned int                    param_mask;
135 	unsigned int                    flags;
136 	struct ifla_cacheinfo           cache_info;
137 } ipa_nl_proto_info_t;
138 
139 typedef struct
140 {
141 	struct ifinfomsg  metainfo;                   /* from header */
142 } ipa_nl_link_info_t;
143 
144 
145 
146 typedef struct ipa_nl_addr_info_s {
147 	struct ifaddrmsg                metainfo;     /* from header */
148 	struct                                      /* attributes  */
149 	{
150 		unsigned int                  param_mask;
151 		unsigned char                 label_name[IF_NAME_LEN];
152 		struct sockaddr_storage       prefix_addr;
153 		struct sockaddr_storage       local_addr;
154 		struct sockaddr_storage       bcast_addr;
155 		struct sockaddr_storage       acast_addr;
156 		struct sockaddr_storage       mcast_addr;
157 	} attr_info;
158 } ipa_nl_addr_info_t;
159 
160 
161 typedef struct ipa_nl_neigh_info_s {
162 	struct ndmsg                metainfo;     /* from header */
163 	struct                                  /* attributes  */
164 	{
165 		unsigned int                param_mask;
166 		struct sockaddr_storage     local_addr;
167 		struct  sockaddr            lladdr_hwaddr;
168 	} attr_info;
169 } ipa_nl_neigh_info_t;
170 
171 
172 
173 typedef struct ipa_nl_route_info_s {
174 	struct rtmsg                    metainfo;     /* from header */
175 	struct                                      /* attributes  */
176 	{
177 		unsigned int                  param_mask;
178 		struct sockaddr_storage       dst_addr;
179 		struct sockaddr_storage       src_addr;
180 		struct sockaddr_storage       gateway_addr;
181 		struct sockaddr_storage       mark_addr;
182 		struct rta_cacheinfo          cache_info;
183 		__u32		iif_index;                      /* Link index  */
184 		__u32		oif_index;                      /* Link index  */
185 		__u32       priority;
186 		__u32       metrics;
187 		ipa_nl_proto_info_t        proto_info;
188 	} attr_info;
189 } ipa_nl_route_info_t;
190 
191 #define IPA_FLOW_TYPE_INVALID      (-1)
192 
193 typedef struct
194 {
195 	unsigned int type;
196 	bool link_event;
197 	/* Optional parameters */
198 	ipa_nl_link_info_t      nl_link_info;
199 	ipa_nl_addr_info_t      nl_addr_info;
200 	ipa_nl_neigh_info_t      nl_neigh_info;
201 	ipa_nl_route_info_t      nl_route_info;
202 } ipa_nl_msg_t;
203 
204 /* Initialization routine for listener on NetLink sockets interface */
205 int ipa_nl_listener_init
206 (
207 	 unsigned int nl_type,
208 	 unsigned int nl_groups,
209 	 ipa_nl_sk_fd_set_info_t *sk_fdset,
210 	 ipa_sock_thrd_fd_read_f read_f
211 	 );
212 
213 /*  Virtual function registered to receive incoming messages over the NETLINK routing socket*/
214 int ipa_nl_recv_msg(int fd);
215 
216 /* map mask value for ipv6 */
217 int mask_v6(int index, uint32_t *mask);
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif /* IPACM_NETLINK_H */
224