1 /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef CLD80211_LIB_H
31 #define CLD80211_LIB_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 #include <netlink/genl/genl.h>
37 #include <stdbool.h>
38 
39 #ifndef UNUSED
40 #define UNUSED(x)    (void)(x)
41 #endif
42 
43 struct cld80211_ctx {
44 	struct nl_sock *sock;
45 	int netlink_familyid;
46 	/* socket pair used to exit from blocking poll*/
47 	int exit_sockets[2];
48 	int sock_buf_size;
49 	int nlctrl_familyid;
50 };
51 
52 /**
53  * enum cld80211_attr - Driver/Application embeds the data in nlmsg with the
54  *                      help of below attributes
55  * CLD80211_ATTR_VENDOR_DATA: Embed all other attributes in this nested
56  *                            attribute.
57  * CLD80211_ATTR_DATA: Embed driver/application data in this attribute
58  * CLD80211_ATTR_META_DATA: Embed meta data for above data. This will  help
59  * wlan driver to peek into request message packet without opening up definition
60  * of complete request message.
61  *
62  * Any new message in future can be added as another attribute
63  */
64 enum cld80211_attr {
65 	CLD80211_ATTR_VENDOR_DATA = 1,
66 	CLD80211_ATTR_DATA,
67 	CLD80211_ATTR_META_DATA,
68 
69 	__CLD80211_ATTR_AFTER_LAST,
70 	CLD80211_ATTR_MAX = __CLD80211_ATTR_AFTER_LAST - 1
71 };
72 
73 /**
74  * Create socket of type NETLINK_GENERIC
75  * Retuns valid sock only if socket creation is succesful and cld80211
76  * family is present, returns NULL otherwise
77  */
78 struct cld80211_ctx *cld80211_init();
79 
80 /**
81  * free the socket created in cld80211_init()
82  */
83 void cld80211_deinit(struct cld80211_ctx *ctx);
84 
85 /**
86  * Allocate nl_msg and populate family and genl header details
87  */
88 struct nl_msg *cld80211_msg_alloc(struct cld80211_ctx *ctx, int cmd,
89 				  struct nlattr **nla_data, int pid);
90 
91 /**
92  * Send nlmsg to driver and return; It doesn't wait for response
93  */
94 int cld80211_send_msg(struct cld80211_ctx *ctx, struct nl_msg *nlmsg);
95 
96 /**
97  * Send nlmsg to driver and get response, if any
98  */
99 int cld80211_send_recv_msg(struct cld80211_ctx *ctx, struct nl_msg *nlmsg,
100 		      int (*valid_handler)(struct nl_msg *, void *),
101 		      void *valid_data);
102 
103 /**
104  * Add membership for multicast group "mcgroup" to receive the messages
105  * sent to this group from driver
106  */
107 int cld80211_add_mcast_group(struct cld80211_ctx *ctx, const char* mcgroup);
108 
109 /**
110  * Remove membership of multicast group "mcgroup" to stop receiving messages
111  * sent to this group from driver
112  */
113 int cld80211_remove_mcast_group(struct cld80211_ctx *ctx, const char* mcgroup);
114 
115 /**
116  * Receive messages from driver on cld80211 family. Client can do
117  * a select()/poll() on the socket before calling this API.
118  * sock: nl_sock created for communication
119  * cb: nl callback context provided by client
120  * Returns corresponding errno when a failure happens while receiving nl msg
121  */
122 int cld80211_recv_msg(struct nl_sock *sock, struct nl_cb *cb);
123 
124 /**
125  * Receive messages from driver on cld80211 family from the
126  * multicast groups subscribed
127  * timeout: Timeout in milliseconds for poll(); -1 is for infinite timeout.
128  * recv_multi_msg: Boolean flag to be sent false/true from client to indicate
129  *                 whether it wants to receive only one message or multiple
130  *                 messages from timeoutblock.
131  *                 false: Receive only one message and return
132  *                 true: Continue in the loop to receive multiple message till
133  *                       client explicitly sends exit via exit_cld80211_recv().
134  * cbctx: Context provided by client, which is to be used when an
135  *        nlmsg is received
136  * Returns corresponding errno when a failure happens while receiving nl msg
137  */
138 int cld80211_recv(struct cld80211_ctx *ctx, int timeout, bool recv_multi_msg,
139 		  int (*valid_handler)(struct nl_msg *, void *),
140 		  void *cbctx);
141 
142 /**
143  * poll() is a blocking call on sock. Client has to unblock the poll()
144  * first to exit gracefully.
145  */
146 void exit_cld80211_recv(struct cld80211_ctx *ctx);
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif
152