1 /**************************************************
2 OpengateM - MAC address authentication system
3 module to get mac address
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
25 #include "opengatemmng.h"
27 /***************************************/
28 /* arp form is reformed to arp form */
29 /* mac addr by arp 00:01:12:0b:.. */
30 /* mac addr by ndp 0:1:12:b:.. */
31 /* these are formated to 00:01:12:0b:..*/
32 /***************************************/
33 int reFormatMacAddr(char* macAddr)
35 int m1,m2,m3,m4,m5,m6;
36 if(sscanf(macAddr, "%x:%x:%x:%x:%x:%x", &m1,&m2,&m3,&m4,&m5,&m6)!=6) return FALSE;
37 snprintf(macAddr, ADDRMAXLN,"%02x:%02x:%02x:%02x:%02x:%02x", m1,m2,m3,m4,m5,m6);
41 /************************************************************************/
42 /* get MAC address from ndp entry for clientAddr (nnnn:nnnn::nnnn:nnnn) */
43 /* result is stored in queue */
44 /************************************************************************/
45 int getMacAddrListFromNdp(char* interface){
51 char macAddr[ADDRMAXLN];
53 char ifStr[WORDMAXLN];
56 /* if interface is null, error return */
57 if(isNull(interface)){
58 err_msg("ERR at %s#%d: NIC device name is not set",__FILE__,__LINE__);
62 /* exec NDP 'ndp -a' , output to pipe */
63 if(isNull(ndpPath=GetConfValue("NdpPath"))){
64 err_msg("ERR at %s#%d:ndp path is not set in conf",__FILE__,__LINE__);
67 if((fpipe=Popenl(1, "r", ndpPath,"-a",(char *)0)) == NULL){
68 err_msg("ERR at %s#%d: exec ndp -na error",__FILE__,__LINE__);
72 /*** get ndp response from pipe */
73 /* skip first title line */
74 if(fgets(buf, BUFFMAXLN, fpipe)==NULL){
75 err_msg("ERR at %s#%d: readin error",__FILE__,__LINE__);
80 /* get MAC address from arp response */
81 /* arp response takes following format */
82 /* "[IPv6 Addr] [Mac] [InterfaceID] [Expire] [Status] [Flags] [Prbs]" */
83 while(fgets(buf, BUFFMAXLN, fpipe)!=NULL){
85 firstTokenP = strtok(buf," "); /* first token */
86 secondTokenP = strtok(NULL," "); /* second token */
87 thirdTokenP = strtok(NULL," "); /* third token */
89 /* skip if invalid address */
90 if(strstr(secondTokenP, ":")==NULL)continue;
92 /* if other interface, skip */
93 if(strstr(thirdTokenP, interface)==NULL) continue;
95 /* remove interface description(%fxp1) from the following form */
96 /* 'fe80::202:b3ff:fe0a:c30e%fxp1' */
97 snprintf(ifStr, WORDMAXLN, "%%%s", interface);
98 if((p=strstr(firstTokenP, ifStr))!=NULL) *p='\0';
100 /* Convert to ARP format */
101 strlcpy(macAddr, secondTokenP, ADDRMAXLN);
102 ReFormatMacAddr(macAddr);
104 /* Insert to queue */
105 /* initializing queue Initqueue() is needed previously */
106 Enqueue(macAddr, firstTokenP);
114 /*******************************************************************/
115 /* get MAC address for clientAddr (nnn.nnn.nnn.nnn) by arp request */
116 /* result is stored in queue */
117 /*******************************************************************/
118 int getMacAddrListFromArp(char* interface){
125 char options[WORDMAXLN];
128 /* if null, error return */
129 if(isNull(interface)){
130 err_msg("ERR at %s#%d: NIC device name is not set",__FILE__,__LINE__);
134 /* set arp options */
135 snprintf(options, WORDMAXLN, "-a -i %s", interface);
137 /* exec arp 'arp -a -i fxp1', output to pipe */
138 if(isNull(arpPath=GetConfValue("ArpPath"))){
139 err_msg("ERR at %s#%d: arp path is not set in conf",__FILE__,__LINE__);
142 if( (fpipe=Popenl(1, "r", arpPath, options, (char *)0)) == NULL){
143 err_msg("ERR at %s#%d: exec arp error",__FILE__,__LINE__);
147 /* get arp response from pipe */
148 while(fgets(buf, BUFFMAXLN, fpipe)!=NULL){
150 /* arp response takes following format */
151 /* "? (133.49.22.1) at 8:0:20:a5:4:62 [ethernet]" */
152 /* get ip address from above string */
153 if((startp=strstr(buf," ("))==NULL) continue;
155 if((endp=strstr(startp, ") "))==NULL) continue;
157 /* save as ip address and cut off string at endp */
161 /* get MAC address from above string */
162 if((startp=strstr(endp+1," at "))==NULL) continue;
164 if((endp=strstr(startp, " "))==NULL) continue;
166 /* save as mac address and cut off string at endp */
170 /* skip if invalid address */
171 if((strstr(macAddr, ":"))==NULL) continue;
174 /* initializing queue Initqueue() is needed previously */
175 Enqueue(macAddr, ipAddr);
185 /****************************************/
186 /****************************************/
187 int GetMacAddrListFromArp(char* interface)
190 if(debug>1) err_msg("DEBUG:=>getMacAddrListFromArp(%s)", interface);
191 ret=getMacAddrListFromArp(interface);
192 if(debug>1) err_msg("DEBUG:(%d)<=getMacAddrListFromArp( )",ret);
197 int GetMacAddrListFromNdp(char* interface){
199 if(debug>1) err_msg("DEBUG:=>getMacAddrListFromNdp(%s)", interface);
200 ret=getMacAddrListFromNdp(interface);
201 if(debug>1) err_msg("DEBUG:(%d)<=getMacAddrListFromNdp( )",ret);
206 int ReFormatMacAddr(char* macAddr){
208 if(debug>1) err_msg("DEBUG:=>reFormatMacAddr(%s)", macAddr);
209 ret=reFormatMacAddr(macAddr);
210 if(debug>1) err_msg("DEBUG:(%d)<=reFormatMacAddr(%s)", ret, macAddr);