summaryrefslogtreecommitdiff
path: root/usr.sbin/npppd/common/rtev_common.c
blob: ea9403067eb6f5ff7b354e95470008c99d2c295b (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*-
 * Copyright (c) 2009 Internet Initiative Japan Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/* $Id: rtev_common.c,v 1.1 2010/01/11 04:20:57 yasuoka Exp $ */
/*
 * PF_ROUTE related utility functions.
 * <p>
 * When use with libevent, call rtev_libevent_init() to initialize this
 * library.</p>
 * usage:
 * <pre>
 *	    #include <sys/types.h>
 *	    #include <time.h>
 *	    #include <event.h>
 *	    #include <ifaddrs.h>
 *	    #include <rtev.h>
 *	
 *	    int main()
 *	    {
 *	    	event_init();
 *	    	rtev_libevent_init(5, 100, 16);	// init after event_init()
 *
 *	    	event_loop();
 *
 *	    	rtev_fini();			// fini before exit()
 *	    	exit(0);
 *	    }
 *
 *	    void hogehoge()
 *	    {
 *		struct ifaddrs *ifa = NULL;
 *
 *		getifaddrs(&ifa);	// rtev.h replaces getifaddrs(3)
 *		    :
 *		freeifaddrs(&ifa);
 *	    }
 * </pre>
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/route.h>
#include <net/if_dl.h>

#include <ifaddrs.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>

#define	NO_RTEV_WRAPPER 1
#include "bytebuf.h"
#include "rtev.h"
#include "rtev_local.h"

#ifdef RTEV_DEBUG
#include "debugutil.h"
#endif

#ifndef	RTEV_BUFSIZ
#define	RTEV_BUFSIZ		131072	/* 128K */
#endif

static int   rtev_base_on_rtevent(rtev_impl *);
static int   rtev_base_on_write(rtev_impl *, int, int);

static struct ifaddrs *rtev_cached_ifa = NULL;
static int rtev_event_serial = 0;
static int rtev_send_serial = 0;
static int rtev_ifa_cache_serial = 0;
static bytebuffer *rtev_sndbuf = NULL;
static u_char rtev_buffer_space[RTEV_BUFSIZ];
static rtev_impl *singleton_impl = NULL;

static inline int             rtev_update_ifa_cache(rtev_impl *);
static int                    rtev_update_ifa_cache_do(rtev_impl *);
static void                   ifa_rbentry_init (void);
static void                   ifa_rbentry_fini (void);
static inline int             ifa_rb_ifacenam_insert (struct ifaddrs *);
static inline int             ifa_rb_sockaddr_insert (struct ifaddrs *);
static inline struct ifaddrs  *ifa_rb_ifacenam_find (const char *);
static inline struct ifaddrs  *ifa_rb_sockaddr_find (struct sockaddr const *);

/**
 * Write a routing message.
 * @return not zero indicates error.  See errno.
 */
int
rtev_write(void *rtm_msg)
{
	int rval;
	struct rt_msghdr *rtm;

	rtm = rtm_msg;
	rtm->rtm_seq = rtev_send_serial++;
	if (bytebuffer_put(rtev_sndbuf, rtm, rtm->rtm_msglen) == NULL)
		rval = -1;
	else
		rval = rtm->rtm_msglen;

	if (singleton_impl->impl_on_write != NULL)
		singleton_impl->impl_on_write(singleton_impl);

	return rval;
}

/**
 * same as getifaddrs(3) but returned obeject is cached.
 * The cached object may be freed by the event handler of this library,
 * so you cannot use it after the event handler returns the event loop.
 */
int
rtev_getifaddrs(struct ifaddrs **ifa)
{
	if (rtev_update_ifa_cache(singleton_impl) != 0)
		return 1;

	*ifa = rtev_cached_ifa;

	return 0;
}

/**
 * checks whether given address is the primary address of the interface.
 * @return not zero if the address is the primary.
 */
int
rtev_ifa_is_primary(const char *ifname, struct sockaddr *sa)
{
	int count;
	struct ifaddrs *ifa;

	for (count = 0, ifa = rtev_getifaddrs_by_ifname(ifname); ifa != NULL;
	    ifa = ifa->ifa_next) {
		
		if (strcmp(ifa->ifa_name, ifname) != 0)
			break;
		if (ifa->ifa_addr->sa_family != sa->sa_family)
			continue;
		switch (sa->sa_family) {
		case AF_INET:
			if (((struct sockaddr_in *)sa)->sin_addr.s_addr ==
			    ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr
			    .s_addr) {
				if (count == 0)
					return 1;
				return 0;
			}
			count++;
			break;
		case AF_INET6:
			if (IN6_ARE_ADDR_EQUAL(
			    &((struct sockaddr_in6 *)sa)->sin6_addr,
			    &((struct sockaddr_in6 *)ifa->ifa_addr)
			    ->sin6_addr)){
				if (count == 0)
					return 1;
				return 0;
			}
			count++;
			break;
		}
	}
	return 0;
}

/** returns the routing event serial number */
int
rtev_get_event_serial()
{
	return rtev_event_serial;
}

/** same as ifaddrs(3), but returned object is the first entry of 'ifname' */
struct ifaddrs *
rtev_getifaddrs_by_ifname(const char *ifname)
{
	if (rtev_update_ifa_cache(singleton_impl) != 0)
		return NULL;

	return ifa_rb_ifacenam_find(ifname);
}

struct ifaddrs *
rtev_getifaddrs_by_sockaddr(struct sockaddr const *sa)
{
	if (rtev_update_ifa_cache(singleton_impl) != 0)
		return NULL;

	return ifa_rb_sockaddr_find(sa);
}

/** same as if_nametoindex(3), but fast and no memory allocation. */
unsigned int
rtev_if_nametoindex(const char *ifname)
{
	unsigned int ni;
	struct ifaddrs *ifa;

	/* adapted from lib/libc/net/if_nametoindex.c */
	ni = 0;

	for (ifa = rtev_getifaddrs_by_ifname(ifname); ifa != NULL;
	    ifa = ifa->ifa_next) {
		if (ifa->ifa_addr &&
		    ifa->ifa_addr->sa_family == AF_LINK) {
			if (strcmp(ifa->ifa_name, ifname) == 0) {
				ni = ((struct sockaddr_dl*)ifa->ifa_addr)
				    ->sdl_index;
				break;
			} else
				break;
		}
	}

	if (!ni)
		errno = ENXIO;

	return ni;
}

/** API have write pending packet internaly */
int
rtev_has_write_pending(void)
{
	if (bytebuffer_position(rtev_sndbuf) > 0)
		return 1;
	return 0;
}

/** finalize this library. */
void
rtev_fini(void)
{
	if (singleton_impl != NULL)
		singleton_impl->impl_fini(singleton_impl->impl);
	singleton_impl = NULL;

	if (rtev_cached_ifa != NULL)
		freeifaddrs(rtev_cached_ifa);
	rtev_cached_ifa = NULL;
	ifa_rbentry_fini();

	if (rtev_sndbuf != NULL) {
		bytebuffer_unwrap(rtev_sndbuf);
		bytebuffer_destroy(rtev_sndbuf);
	}
	rtev_sndbuf = NULL;
}

/* protected virtual */
int
rtev_base_init(rtev_impl *impl, int flags)
{
	ifa_rbentry_init();

	if (rtev_sndbuf == NULL) {
		if ((rtev_sndbuf = bytebuffer_wrap(rtev_buffer_space,
		    sizeof(rtev_buffer_space))) == NULL)
			goto reigai;
		bytebuffer_clear(rtev_sndbuf);
	}
	impl->base_on_rtevent = rtev_base_on_rtevent;
	impl->base_on_write = rtev_base_on_write;
	impl->base_flags = flags;
	singleton_impl = impl;

	return 0;
reigai:
	rtev_fini();

	return 1;
}

static int
rtev_base_on_rtevent(rtev_impl *impl)
{

	RTEV_DBG((LOG_DEBUG, "%s", __func__));

	rtev_event_serial++;

	if ((impl->base_flags & RTEV_UPDATE_IFA_ON_DEMAND) == 0)
		return rtev_update_ifa_cache_do(impl);

	return 0;
}

static inline int
rtev_update_ifa_cache(rtev_impl *impl)
{
	if (rtev_event_serial == rtev_ifa_cache_serial && 
	    rtev_cached_ifa != NULL)
		return 0;

	return rtev_update_ifa_cache_do(impl);
}

static int
rtev_update_ifa_cache_do(rtev_impl *impl)
{
	const char *ifname;
	struct ifaddrs *ifa;
	struct ifaddrs *ifa0;

	RTEV_DBG((LOG_DEBUG, "%s", __func__));

	ifa0 = NULL;
	rtev_ifa_cache_serial = rtev_event_serial;
	if (getifaddrs(&ifa0) != 0)
		return 1;
	if (rtev_cached_ifa != NULL) {
		ifa_rbentry_fini();
		freeifaddrs(rtev_cached_ifa);
		rtev_cached_ifa = NULL;
	}

	for (ifa = ifa0, ifname = NULL; ifa != NULL; ifa = ifa->ifa_next) {
		if (ifname == NULL || strcmp(ifa->ifa_name, ifname)) {
			ifname = ifa->ifa_name;
			if (ifa_rb_ifacenam_find(ifname) == NULL) {
				if (ifa_rb_ifacenam_insert(ifa) != 0)
					goto on_error;
			}
		}
		if (ifa->ifa_addr != NULL &&
		    ifa_rb_sockaddr_find(ifa->ifa_addr) == NULL) {
			if (ifa_rb_sockaddr_insert(ifa) != 0)
				goto on_error;
		}
	}
	rtev_cached_ifa = ifa0;

	return 0;

on_error:
	if (ifa0)
		freeifaddrs(ifa0);
	rtev_cached_ifa = NULL;
	ifa_rbentry_fini();

	return 1;
}

static int
rtev_base_on_write(rtev_impl *impl, int rtsock, int npackets)
{
	int i, rval;
	struct rt_msghdr *rtm;

	rval = 0;
	bytebuffer_flip(rtev_sndbuf);
	for (i = 0; i < npackets && bytebuffer_remaining(rtev_sndbuf) > 0; i++){
		rtm = bytebuffer_pointer(rtev_sndbuf);
		if (send(rtsock, rtm, rtm->rtm_msglen, 0) <= 0 && 
		    !(rtm->rtm_type == RTM_DELETE && errno == ESRCH) &&
		    !(rtm->rtm_type == RTM_ADD    && errno == EEXIST)) {
			rval = 1;
		}
		bytebuffer_get(rtev_sndbuf, BYTEBUFFER_GET_DIRECT,
		    rtm->rtm_msglen);
	}
	bytebuffer_compact(rtev_sndbuf);

	return rval;
}

/*
 * Red-black trees for interface name and interface address lookups.
 */
#include <sys/tree.h>	/* BSD sys/tree.h */

struct ifa_rbentry {
	struct ifaddrs *ifa;
	RB_ENTRY(ifa_rbentry) rbe;
};

static inline int ifacenam_compar(struct ifa_rbentry *, struct ifa_rbentry *);
static RB_HEAD(ifa_rb_ifacenam, ifa_rbentry) ifa_rb_ifacenam;
RB_PROTOTYPE(ifa_rb_ifacenam, ifa_rbentry, rbe, ifacenam_compar);
RB_GENERATE(ifa_rb_ifacenam, ifa_rbentry, rbe, ifacenam_compar);

static inline int sockaddr_compar(struct ifa_rbentry *, struct ifa_rbentry *);
static RB_HEAD(ifa_rb_sockaddr, ifa_rbentry) ifa_rb_sockaddr;
RB_PROTOTYPE(ifa_rb_sockaddr, ifa_rbentry, rbe, sockaddr_compar);
RB_GENERATE(ifa_rb_sockaddr, ifa_rbentry, rbe, sockaddr_compar);

static void
ifa_rbentry_init(void)
{
	RB_INIT(&ifa_rb_ifacenam);
	RB_INIT(&ifa_rb_sockaddr);
}

static void
ifa_rbentry_fini(void)
{
	struct ifa_rbentry *e, *n;
	
	for (e = RB_MIN(ifa_rb_ifacenam, &ifa_rb_ifacenam); e; e = n) {
		n = RB_NEXT(ifa_rb_ifacenam, &ifa_rb_ifacenam, e);
		RB_REMOVE(ifa_rb_ifacenam, &ifa_rb_ifacenam, e);
		free(e);
	}
	for (e = RB_MIN(ifa_rb_sockaddr, &ifa_rb_sockaddr); e; e = n) {
		n = RB_NEXT(ifa_rb_sockaddr, &ifa_rb_sockaddr, e);
		RB_REMOVE(ifa_rb_sockaddr, &ifa_rb_sockaddr, e);
		free(e);
	}
}

static inline int
ifa_rb_ifacenam_insert(struct ifaddrs *ifa)
{
	struct ifa_rbentry *e;

	if ((e = malloc(sizeof(struct ifa_rbentry))) == NULL)
		return -1;

	e->ifa = ifa;
	RB_INSERT(ifa_rb_ifacenam, &ifa_rb_ifacenam, e);

	return 0;
}

static inline int
ifa_rb_sockaddr_insert(struct ifaddrs *ifa)
{
	struct ifa_rbentry *e;

	if ((e = malloc(sizeof(struct ifa_rbentry))) == NULL)
		return -1;

	e->ifa = ifa;
	RB_INSERT(ifa_rb_sockaddr, &ifa_rb_sockaddr, e);

	return 0;
}

static inline struct ifaddrs *
ifa_rb_ifacenam_find(const char *ifname)
{
	struct ifa_rbentry *e, e0;
	struct ifaddrs ifa;

	e = &e0;
	e->ifa = &ifa;
	e->ifa->ifa_name = (char *)ifname;

	e = RB_FIND(ifa_rb_ifacenam, &ifa_rb_ifacenam, e);
	if (e == NULL)
		return NULL;

	return e->ifa;
}

static inline struct ifaddrs *
ifa_rb_sockaddr_find(struct sockaddr const *sa)
{
	struct ifa_rbentry *e, e0;
	struct ifaddrs ifa;

	e = &e0;
	e->ifa = &ifa;
	e->ifa->ifa_addr = (struct sockaddr *)sa;

	e = RB_FIND(ifa_rb_sockaddr, &ifa_rb_sockaddr, e);
	if (e == NULL)
		return NULL;

	return e->ifa;
}

static inline int
ifacenam_compar(struct ifa_rbentry *a, struct ifa_rbentry *b)
{
	return strcmp(a->ifa->ifa_name, b->ifa->ifa_name);
}

static inline int
sockaddr_compar(struct ifa_rbentry *a, struct ifa_rbentry *b)
{
	int cmp;

	cmp = b->ifa->ifa_addr->sa_family - a->ifa->ifa_addr->sa_family;
	if (cmp != 0)
		return cmp;
	return memcmp(a->ifa->ifa_addr->sa_data, b->ifa->ifa_addr->sa_data,
	    MIN(a->ifa->ifa_addr->sa_len, b->ifa->ifa_addr->sa_len) -
		offsetof(struct sockaddr, sa_data));
}