[go: up one dir, main page]

Menu

[8b793c]: / misc.c  Maximize  Restore  History

Download this file

280 lines (235 with data), 7.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Mis-connector - user-space router and NATP for misconfigured hosts on LAN.
Random tools.
This file is part of mis-connector.
Copyright ⓒ 2021 Petr Silhavy <petr.silhavy@yandex.com>
SPDX-License-Identifier: GPL-3.0-or-later
Mis-connector is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
Mis-connector is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with mis-connector. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <error.h>
#include <errno.h>
#include <libnet.h>
#include <glib.h>
#include <time.h>
#include "whackparam.h"
//extern GRWLock by_MAC_lock , by_IP_lock;
extern GData *by_MAC_dl , *by_IP_dl ;
// or g_bit_lock, g_bit_trylock, g_bit_unlock
enum
{
ALOCK_NONE = 0,
ALOCK_BIT_UPDATE = 1,
ALOCK_BIT_READ = 2,
ALOCK_BIT_WRITE = 3,
} ;
struct timespec ts = { .tv_sec = 0 , .tv_nsec = 1000000 } ; // 1ms
void
lock_read(struct MC_ALOCK *lck)
{
while( lck->lock & ALOCK_BIT_WRITE ) nanosleep(&ts, NULL);
g_bit_lock (&lck->lock, ALOCK_BIT_UPDATE );
lck->lock |= ALOCK_BIT_READ ;
++lck->read_count ;
g_bit_unlock (&lck->lock, ALOCK_BIT_UPDATE );
}
void
unlock_read(struct MC_ALOCK *lck)
{
if ( lck->lock & ALOCK_BIT_WRITE ) abort();
g_bit_lock (&lck->lock, ALOCK_BIT_UPDATE );
if ( --lck->read_count == 0 ) { lck->read_count = 0 ; lck->lock = 0 ; }
g_bit_unlock (&lck->lock, ALOCK_BIT_UPDATE );
}
void
lock_write(struct MC_ALOCK *lck)
{
while( lck->lock) nanosleep(&ts, NULL);
g_bit_lock (&lck->lock, ALOCK_BIT_UPDATE );
lck->lock |= ALOCK_BIT_WRITE ;
g_bit_unlock (&lck->lock, ALOCK_BIT_UPDATE );
// locked_by = gettid();
}
void
unlock_write(struct MC_ALOCK *lck)
{
if ( ! ( lck->lock & ALOCK_BIT_WRITE ) ) abort();
if ( lck->lock & ALOCK_BIT_READ ) abort();
g_bit_lock (&lck->lock, ALOCK_BIT_UPDATE );
lck->lock = 0 ;
g_bit_unlock (&lck->lock, ALOCK_BIT_UPDATE );
}
char *
addr2name4_r(uint32_t in)
{
uint8_t *p = (uint8_t *)&in;
return g_strdup_printf( "%d.%d.%d.%d", (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
}
void
h_print(struct hash_entry *h, const char * const func, char *text)
{
char *ipi = addr2name4_r(h->in_ip);
char *ipnat = addr2name4_r(h->ip);
char *smac = mac2str(h->mac);
char *gwip = addr2name4_r(h->gw_ip);
fprintf(stderr, "[%s] ″%s″ NATIP: %s IP/GWIP/mac %s/%s/%s\n", func , text,
ipnat, ipi, gwip, smac );
g_free(ipi) ; g_free(ipnat) ; g_free(smac); g_free(gwip);
}
struct hash_entry *
ip_to_mac_ip(unsigned int netip)
{
struct hash_entry *he ;
#if 0
unsigned int ipkey = ipkey_get(netip);
he = sb->ip_to_mac[ipkey] ;
if ( he == NULL )
{
union conv c = { .u = netip } ;
WARNING("No IP entry for %u.%u.%u.%u\n",
c.c[0],
c.c[1],
c.c[2],
c.c[3]);
return NULL ;
}
#else
//
// new
//
GQuark ipkey = ip_key(netip);
lock_read(&by_IP_lock);
if ( ( he = g_datalist_id_get_data(&by_IP_dl, ipkey)) == NULL )
{
unlock_read(&by_IP_lock);
char * ips = addr2name4_r(netip) ;
error(0, errno, "No MAC+IP for net IP %s\n", ips);
g_free(ips);
}
unlock_read(&by_IP_lock);
#endif // 0
/* No collisions handling needed here , civilised IP is unique key */
return he ;
}
#if 0
static int
send_arp_query(struct ether_header *eh)
{
libnet_ptag_t t;
t = libnet_autobuild_arp(
ARPOP_REQUEST, /* operation type */
enet_src, /* sender hardware addr */
(uint8_t *)&i, /* sender protocol addr */
enet_dst, /* target hardware addr */
(uint8_t *)&i, /* target protocol addr */
l); /* libnet context */
if (t == -1)
{
fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l));
goto bad;
}
}
#endif // 0
/* return net order IP */
//unsigned int
struct hash_entry *
mac_to_ip(struct ether_header *eh )
{ /* */
GQuark key = mac_key(eh->ether_shost) ;
struct hash_entry *he ;
lock_read(&by_MAC_lock);
if ( ( he = g_datalist_id_get_data(&by_MAC_dl, key)) == NULL )
{
char * macs = mac2str(eh->ether_shost);
unlock_read(&by_MAC_lock);
error(0, 0, "No IP for MAC %s key %u\n", macs, key);
return 0 ;
}
unlock_read(&by_MAC_lock);
// DEBUGP(stderr,"%s Found IP %u.%u.%u.%u\n", ids[id],
// c.c[0],
// c.c[1],
// c.c[2],
// c.c[3]);
return he/* ->ip */ ;
// return he->ip ;
}
#if 0 ///////////////////////////////////////////////////
if ( sb->mac_to_ip[key] == NULL )
{
he = bm_alloc0(sb, sizeof *he, __func__ );
if ( he == NULL )
{
BUG("No new entry, bm_alloc returns NULL\n");
/* zap old entries, and try againg FIXME !!! or simple *CRASH* */
return ;
}
DEBUGP(stderr, "%s %s allocated -> new_entry\n", ids[id], __func__ );
goto new_entry ;
}
else
{
he = sb->mac_to_ip[key] ;
if ( he->next == NULL && ! memcmp(he->mac, sha, ETH_ALEN) ) /* entry exists */
{
DEBUGP(stderr,"Entry for MAC exists : ");
print_mac(sha);
DEBUGP(stderr,"\n");
return ;
}
/* handle collision */
if ( g_slist_find_mac(he, sha)) /* entry exists */
{
DEBUGP(stderr, "Entry for MAC exists in list : ");
print_mac(sha);
DEBUGP(stderr, "\n");
return ;
}
else
{
struct hash_entry *new = bm_alloc0(sb, sizeof *new, __func__ );
if ( new == NULL )
{
BUG("No new entry, bm_alloc returns NULL\n");
/* zap old entries, and try againg FIXME !!! or simple *CRASH* */
return ;
}
g_slist_append (he, new);
he = new ;
DEBUGP(stderr, "%s %s MAC doesn't exist -> new -> new_entry\n", ids[id], __func__ );
goto new_entry ;
}
}
return ;
new_entry:
memcpy(he->mac, sha, ETH_ALEN);
memcpy(&he->fool_ip, sip, sizeof he->fool_ip);
he->ip = htonl(ip_make(wp));
{
/* unsigned int ipkey = ntohl(ipkey_get(he->ip)); */
unsigned int ipkey = ipkey_get(he->ip);
DEBUGP(stderr, "%s %s ", ids[id], __func__);
print_mac(sha);
DEBUGP(stderr," Fool IP: %u.%u.%u.%u IPkey %d\n",
sip[0],
sip[1],
sip[2],
sip[3],
ipkey);
if ( ipkey > wp->fool_size )
BUG("IP key %u > hash size %d\n", ipkey, wp->fool_size );
else
DEBUGP(stderr,"%s IP key %u\n", __func__, ipkey );
sb->mac_to_ip[key] = he ;
sb->ip_to_mac[ ipkey ] = he ;
}
}
#endif // 0 //////////////////////////////////////////////////////////////////////////