K2.6 patches and update.
[tomato.git] / release / src-rt / wl / eapd / dcs_eap.c
blob6ba2fb0615e44f6e94ca1d6dc5c9dc707b541b5b
1 /*
2 * Application-specific portion of EAPD
3 * (DCS)
5 * Copyright (C) 2010, Broadcom Corporation
6 * All Rights Reserved.
7 *
8 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
9 * the contents of this file may not be disclosed to third parties, copied
10 * or duplicated in any form, in whole or in part, without the prior
11 * written permission of Broadcom Corporation.
13 * $Id: dcs_eap.c 241391 2011-02-18 03:35:48Z stakita $
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <typedefs.h>
26 #include <bcmutils.h>
27 #include <proto/ethernet.h>
28 #include <proto/eapol.h>
29 #include <proto/eap.h>
30 #include <bcmendian.h>
31 #include <wlutils.h>
32 #include <eapd.h>
33 #include <shutils.h>
34 #include <UdpLib.h>
35 #include <security_ipc.h>
37 void
38 dcs_app_recv_handler(eapd_wksp_t *nwksp, eapd_cb_t *from, uint8 *pData,
39 int *pLen)
44 void
45 dcs_app_set_eventmask(eapd_app_t *app)
47 memset(app->bitvec, 0, sizeof(app->bitvec));
49 setbit(app->bitvec, WLC_E_DCS_REQUEST);
50 setbit(app->bitvec, WLC_E_SCAN_COMPLETE);
51 return;
54 int
55 dcs_app_init(eapd_wksp_t *nwksp)
57 int reuse = 1;
58 eapd_dcs_t *dcs;
59 eapd_cb_t *cb;
60 struct sockaddr_in addr;
63 if (nwksp == NULL)
64 return -1;
66 dcs = &nwksp->dcs;
67 dcs->appSocket = -1;
69 cb = dcs->cb;
70 if (cb == NULL) {
71 EAPD_INFO("No any DCS application need to run.\n");
72 return 0;
75 while (cb) {
76 EAPD_INFO("dcs: init brcm interface %s \n", cb->ifname);
77 cb->brcmSocket = eapd_add_brcm(nwksp, cb->ifname);
78 if (!cb->brcmSocket)
79 return -1;
80 /* set this brcmSocket have DCS capability */
81 cb->brcmSocket->flag |= EAPD_CAP_DCS;
83 cb = cb->next;
86 /* appSocket for dcs */
87 dcs->appSocket = socket(AF_INET, SOCK_DGRAM, 0);
88 if (dcs->appSocket < 0) {
89 EAPD_ERROR("UDP Open failed.\n");
90 return -1;
92 #if defined(__ECOS)
93 if (setsockopt(dcs->appSocket, SOL_SOCKET, SO_REUSEPORT, (char*)&reuse,
94 sizeof(reuse)) < 0) {
95 EAPD_ERROR("UDP setsockopt failed.\n");
96 close(dcs->appSocket);
97 dcs->appSocket = -1;
98 return -1;
100 #else
101 if (setsockopt(dcs->appSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse,
102 sizeof(reuse)) < 0) {
103 EAPD_ERROR("UDP setsockopt failed.\n");
104 close(dcs->appSocket);
105 dcs->appSocket = -1;
106 return -1;
108 #endif
110 memset(&addr, 0, sizeof(struct sockaddr_in));
111 addr.sin_family = AF_INET;
112 addr.sin_addr.s_addr = INADDR_ANY;
113 addr.sin_port = htons(EAPD_WKSP_DCS_UDP_RPORT);
114 if (bind(dcs->appSocket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
115 EAPD_ERROR("UDP Bind failed, close dcs appSocket %d\n", dcs->appSocket);
116 close(dcs->appSocket);
117 dcs->appSocket = -1;
118 return -1;
120 EAPD_INFO("DCS appSocket %d opened\n", dcs->appSocket);
122 return 0;
126 dcs_app_deinit(eapd_wksp_t *nwksp)
128 eapd_dcs_t *dcs;
129 eapd_cb_t *cb, *tmp_cb;
131 if (nwksp == NULL) {
132 EAPD_ERROR("Wrong argument...\n");
133 return -1;
136 dcs = &nwksp->dcs;
137 cb = dcs->cb;
138 while (cb) {
139 /* close brcm drvSocket */
140 if (cb->brcmSocket) {
141 EAPD_INFO("close dcs brcmSocket %d\n", cb->brcmSocket->drvSocket);
142 eapd_del_brcm(nwksp, cb->brcmSocket);
145 tmp_cb = cb;
146 cb = cb->next;
147 free(tmp_cb);
150 /* close appSocke */
151 if (dcs->appSocket >= 0) {
152 EAPD_INFO("close dcs appSocket %d\n", dcs->appSocket);
153 close(dcs->appSocket);
154 dcs->appSocket = -1;
157 return 0;
161 dcs_app_sendup(eapd_wksp_t *nwksp, uint8 *pData, int pLen, char *from)
163 eapd_dcs_t *dcs;
165 if (nwksp == NULL) {
166 EAPD_ERROR("Wrong argument...\n");
167 return -1;
170 dcs = &nwksp->dcs;
171 if (dcs->appSocket >= 0) {
172 /* send to dcs */
173 int sentBytes = 0;
174 struct sockaddr_in to;
176 to.sin_addr.s_addr = inet_addr(EAPD_WKSP_UDP_ADDR);
177 to.sin_family = AF_INET;
178 to.sin_port = htons(EAPD_WKSP_DCS_UDP_SPORT);
180 sentBytes = sendto(dcs->appSocket, pData, pLen, 0,
181 (struct sockaddr *)&to, sizeof(struct sockaddr_in));
183 if (sentBytes != pLen) {
184 EAPD_ERROR("UDP send failed; sentBytes = %d\n", sentBytes);
186 else {
187 /* EAPD_ERROR("Send %d bytes to dcs\n", sentBytes); */
190 else {
191 EAPD_ERROR("dcs appSocket not created\n");
193 return 0;
196 #if EAPD_WKSP_AUTO_CONFIG
198 dcs_app_enabled(char *name)
200 char value[128], comb[32], prefix[8];
201 char os_name[IFNAMSIZ];
202 int unit;
204 memset(os_name, 0, sizeof(os_name));
206 if (nvifname_to_osifname(name, os_name, sizeof(os_name)))
207 return 0;
208 if (wl_probe(os_name) ||
209 wl_ioctl(os_name, WLC_GET_INSTANCE, &unit, sizeof(unit)))
210 return 0;
211 if (osifname_to_nvifname(name, prefix, sizeof(prefix)))
212 return 0;
214 strcat(prefix, "_");
215 /* ignore if disabled */
216 eapd_safe_get_conf(value, sizeof(value), strcat_r(prefix, "radio", comb));
217 if (atoi(value) == 0) {
218 EAPD_INFO("DCS:ignored interface %s. radio disabled\n", os_name);
219 return 0;
222 /* ignore if BSS is disabled */
223 eapd_safe_get_conf(value, sizeof(value), strcat_r(prefix, "bss_enabled", comb));
224 if (atoi(value) == 0) {
225 EAPD_INFO("DCS: ignored interface %s, %s is disabled \n", os_name, comb);
226 return 0;
229 /* if come to here return enabled */
230 return 1;
232 #endif /* EAPD_WKSP_AUTO_CONFIG */
235 dcs_app_handle_event(eapd_wksp_t *nwksp, uint8 *pData, int Len, char *from)
237 int type;
238 eapd_dcs_t *dcs;
239 eapd_cb_t *cb;
240 bcm_event_t *dpkt = (bcm_event_t *) pData;
241 wl_event_msg_t *event = &(dpkt->event);
243 type = ntohl(event->event_type);
245 dcs = &nwksp->dcs;
246 cb = dcs->cb;
247 while (cb) {
248 if (isset(dcs->bitvec, type) &&
249 !strcmp(cb->ifname, from)) {
251 /* prepend ifname, we reserved IFNAMSIZ length already */
252 pData -= IFNAMSIZ;
253 Len += IFNAMSIZ;
254 memcpy(pData, event->ifname, IFNAMSIZ);
256 /* send to dcs use cb->ifname */
257 dcs_app_sendup(nwksp, pData, Len, cb->ifname);
258 break;
260 cb = cb->next;
263 return 0;