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/socket.h>
55 #include <linux/if.h>
56 #include <linux/if_addr.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/netlink.h>
59 #include <netinet/in.h>
60 #include "IPACM_Defs.h"
61 
62 #define MAX_NUM_OF_FD 10
63 #define IPA_NL_MSG_MAX_LEN (2048)
64 
65 /*---------------------------------------------------------------------------
66 	 Type representing enumeration of NetLink event indication messages
67 ---------------------------------------------------------------------------*/
68 
69 /*---------------------------------------------------------------------------
70 	 Types representing parsed NetLink message
71 ---------------------------------------------------------------------------*/
72 #define IPA_NLA_PARAM_NONE        (0x0000)
73 #define IPA_NLA_PARAM_PREFIXADDR  (0x0001)
74 #define IPA_NLA_PARAM_LOCALADDR   (0x0002)
75 #define IPA_NLA_PARAM_LABELNAME   (0x0004)
76 #define IPA_NLA_PARAM_BCASTADDR   (0x0008)
77 #define IPA_NLA_PARAM_ACASTADDR   (0x0010)
78 #define IPA_NLA_PARAM_MCASTADDR   (0x0020)
79 #define IPA_NLA_PARAM_CACHEINFO   (0x0080)
80 #define IPA_NLA_PARAM_PROTOINFO   (0x0100)
81 #define IPA_NLA_PARAM_FLAGS       (0x0200)
82 
83 #define IPA_RTA_PARAM_NONE        (0x0000)
84 #define IPA_RTA_PARAM_DST         (0x0001)
85 #define IPA_RTA_PARAM_SRC         (0x0002)
86 #define IPA_RTA_PARAM_GATEWAY     (0x0004)
87 #define IPA_RTA_PARAM_IIF         (0x0008)
88 #define IPA_RTA_PARAM_OIF         (0x0010)
89 #define IPA_RTA_PARAM_CACHEINFO   (0x0020)
90 #define IPA_RTA_PARAM_PRIORITY    (0x0080)
91 #define IPA_RTA_PARAM_METRICS     (0x0100)
92 
93 
94 /*---------------------------------------------------------------------------
95 	 Type representing function callback registered with a socket listener
96 	 thread for reading from a socket on receipt of an incoming message
97 ---------------------------------------------------------------------------*/
98 typedef int (*ipa_sock_thrd_fd_read_f)(int fd);
99 
100 typedef enum
101 {
102 	IPA_INIT = 0,
103 	IPA_LINK_UP_WAIT,
104 	IPA_LINK_UP,
105 	IPA_LINK_DOWN_WAIT,
106 	IPA_LINK_DOWN
107 } ipa_nl_state_e;
108 
109 typedef struct
110 {
111 	int sk_fd;
112 	ipa_sock_thrd_fd_read_f read_func;
113 } ipa_nl_sk_fd_map_info_t;
114 
115 typedef struct
116 {
117 	ipa_nl_sk_fd_map_info_t sk_fds[MAX_NUM_OF_FD];
118 	fd_set fdset;
119 	int num_fd;
120 	int max_fd;
121 } ipa_nl_sk_fd_set_info_t;
122 
123 typedef struct
124 {
125 	int                 sk_fd;       /* socket descriptor */
126 	struct sockaddr_nl  sk_addr_loc; /* local address of socket */
127 } ipa_nl_sk_info_t;
128 
129 typedef struct ipa_nl_addr_s {
130 	struct sockaddr_storage        ip_addr;
131 	unsigned int                   mask;
132 } ipa_nl_addr_t;
133 
134 typedef struct ipa_nl_proto_info_s {
135 	unsigned int                    param_mask;
136 	unsigned int                    flags;
137 	struct ifla_cacheinfo           cache_info;
138 } ipa_nl_proto_info_t;
139 
140 typedef struct
141 {
142 	struct ifinfomsg  metainfo;                   /* from header */
143 } ipa_nl_link_info_t;
144 
145 
146 
147 typedef struct ipa_nl_addr_info_s {
148 	struct ifaddrmsg                metainfo;     /* from header */
149 	struct                                      /* attributes  */
150 	{
151 		unsigned int                  param_mask;
152 		unsigned char                 label_name[IF_NAME_LEN];
153 		struct sockaddr_storage       prefix_addr;
154 		struct sockaddr_storage       local_addr;
155 		struct sockaddr_storage       bcast_addr;
156 		struct sockaddr_storage       acast_addr;
157 		struct sockaddr_storage       mcast_addr;
158 	} attr_info;
159 } ipa_nl_addr_info_t;
160 
161 
162 typedef struct ipa_nl_neigh_info_s {
163 	struct ndmsg                metainfo;     /* from header */
164 	struct                                  /* attributes  */
165 	{
166 		unsigned int                param_mask;
167 		struct sockaddr_storage     local_addr;
168 		struct  sockaddr            lladdr_hwaddr;
169 	} attr_info;
170 } ipa_nl_neigh_info_t;
171 
172 
173 
174 typedef struct ipa_nl_route_info_s {
175 	struct rtmsg                    metainfo;     /* from header */
176 	struct                                      /* attributes  */
177 	{
178 		unsigned int                  param_mask;
179 		struct sockaddr_storage       dst_addr;
180 		struct sockaddr_storage       src_addr;
181 		struct sockaddr_storage       gateway_addr;
182 		struct sockaddr_storage       mark_addr;
183 		struct rta_cacheinfo          cache_info;
184 		__u32		iif_index;                      /* Link index  */
185 		__u32		oif_index;                      /* Link index  */
186 		__u32       priority;
187 		__u32       metrics;
188 		ipa_nl_proto_info_t        proto_info;
189 	} attr_info;
190 } ipa_nl_route_info_t;
191 
192 #define IPA_FLOW_TYPE_INVALID      (-1)
193 
194 typedef struct
195 {
196 	unsigned int type;
197 	bool link_event;
198 	/* Optional parameters */
199 	ipa_nl_link_info_t      nl_link_info;
200 	ipa_nl_addr_info_t      nl_addr_info;
201 	ipa_nl_neigh_info_t      nl_neigh_info;
202 	ipa_nl_route_info_t      nl_route_info;
203 } ipa_nl_msg_t;
204 
205 /* Initialization routine for listener on NetLink sockets interface */
206 int ipa_nl_listener_init
207 (
208 	 unsigned int nl_type,
209 	 unsigned int nl_groups,
210 	 ipa_nl_sk_fd_set_info_t *sk_fdset,
211 	 ipa_sock_thrd_fd_read_f read_f
212 	 );
213 
214 /*  Virtual function registered to receive incoming messages over the NETLINK routing socket*/
215 int ipa_nl_recv_msg(int fd);
216 
217 /* map mask value for ipv6 */
218 int mask_v6(int index, uint32_t *mask);
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif /* IPACM_NETLINK_H */
225