summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/mli_ipl.c
blob: a451a1092c0fa21ef58cbf3a2d4accf6f58dd9d1 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 * Copyright (C) 1993-2000 by Darren Reed.
 * (C)opyright 1997 by Marc Boucher.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and due credit is given
 * to the original authors and the contributors.
 */

/* TODO: (MARCXXX)
	- ipl_init failure -> open ENODEV or whatever
	- prevent multiple LKM loads
	- surround access to ifnet structures by IFNET_LOCK()/IFNET_UNLOCK() ?
	- m != m1 problem
*/

#include <sys/types.h>
#include <sys/conf.h>
#ifdef IPFILTER_LKM
#include <sys/mload.h>
#endif
#include <sys/systm.h>
#include <sys/errno.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#ifdef IFF_DRVRLOCK /* IRIX6 */
#include <sys/hashing.h>
#include <netinet/in_var.h>
#endif
#include <sys/mbuf.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/tcpip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ipfilter.h>
#include "ipl.h"
#include "ip_compat.h"
#include "ip_fil.h"
#include "ip_nat.h"

/*#define IPFDEBUG 1*/

unsigned IPL_EXTERN(devflag) = D_MP;
#ifdef IPFILTER_LKM
char *IPL_EXTERN(mversion) = M_VERSION;
#endif

kmutex_t ipl_mutex, ipf_mutex, ipfi_mutex, ipf_rw;
kmutex_t ipf_frag, ipf_state, ipf_nat, ipf_natfrag, ipf_auth;

int     (*fr_checkp) __P((struct ip *, int, void *, int, mb_t **));

#ifdef IPFILTER_LKM
static int *ipff_addr = 0;
static int ipff_value;
static __psunsigned_t *ipfk_addr = 0;
static __psunsigned_t ipfk_code[4];
#endif

typedef	struct	nif	{
	struct	nif	*nf_next;
	struct ifnet	*nf_ifp;
	int     (*nf_output)(struct ifnet *, struct mbuf *, struct sockaddr *);
	char	nf_name[IFNAMSIZ];
	int	nf_unit;
} nif_t;

static nif_t *nif_head = 0;
static int nif_interfaces = 0;
extern int in_interfaces;

extern ipnat_t *nat_list;

static int
ipl_if_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst)
{
	nif_t *nif;

	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */
	for (nif = nif_head; nif; nif = nif->nf_next)
		if (nif->nf_ifp == ifp)
			break;

	MUTEX_EXIT(&ipfi_mutex);
	if (!nif) {
		printf("IP Filter: ipl_if_output intf %x NOT FOUND\n", ifp);
		return ENETDOWN;
	}

#if	IPFDEBUG >= 4
	static unsigned int cnt = 0;
	if ((++cnt % 200) == 0)
		printf("IP Filter: ipl_if_output(ifp=0x%lx, m=0x%lx, dst=0x%lx), m_type=%d m_flags=0x%lx m_off=0x%lx\n", ifp, m, dst, m->m_type, (unsigned long)(m->m_flags), m->m_off);
#endif
	if (fr_checkp) {
		struct mbuf *m1 = m;
		struct ip *ip;
		int hlen;

		switch(m->m_type) {
		case MT_DATA:
			if (m->m_flags & M_BCAST) {
#if	IPFDEBUG >= 2
				printf("IP Filter: ipl_if_output: passing M_BCAST\n");
#endif
				break;
			}
			/* FALLTHROUGH */
		case MT_HEADER:
#if	IPFDEBUG >= 4
			if (!MBUF_IS_CLUSTER(m) && ((m->m_off < MMINOFF) || (m->m_off > MMAXOFF))) {
				printf("IP Filter: ipl_if_output: bad m_off m_type=%d m_flags=0x%lx m_off=0x%lx\n", m->m_type, (unsigned long)(m->m_flags), m->m_off);
				return (*nif->nf_output)(ifp, m, dst);
			}
#endif
			if (m->m_len < sizeof(char)) {
				printf("IP Filter: ipl_if_output: mbuf block too small (m_len=%d) for IP vers+hlen, m_type=%d m_flags=0x%lx\n", m->m_len, m->m_type, (unsigned long)(m->m_flags));
				return (*nif->nf_output)(ifp, m, dst);
			}
			ip = mtod(m, struct ip *);
			if (ip->ip_v != IPVERSION) {
#if	IPFDEBUG >= 4
				printf("IP Filter: ipl_if_output: bad ip_v m_type=%d m_flags=0x%lx m_off=0x%lx\n", m->m_type, (unsigned long)(m->m_flags), m->m_off);
#endif
				return (*nif->nf_output)(ifp, m, dst);
			}

			hlen = ip->ip_hl << 2;
			if ((*fr_checkp)(ip, hlen, ifp, 1, &m1))
				return EHOSTUNREACH;

			if (!m1)
				return 0;

			m = m1;
			break;

		default:
			printf("IP Filter: ipl_if_output: bad m_type=%d m_flags=0x%lxm_off=0x%lx\n", m->m_type, (unsigned long)(m->m_flags), m->m_off);
			break;
		}
	}
	return (*nif->nf_output)(ifp, m, dst);
}

int
IPL_EXTERN(_kernel)(struct ifnet *rcvif, struct mbuf *m)
{
#if	IPFDEBUG >= 4
	static unsigned int cnt = 0;
	if ((++cnt % 200) == 0)
		printf("IP Filter: ipl_ipfilter_kernel(rcvif=0x%lx, m=0x%lx\n", rcvif, m);
#endif

	/*
	 * Check if we want to allow this packet to be processed.
	 * Consider it to be bad if not.
	 */
	if (fr_checkp) {
		struct mbuf *m1 = m;
		struct ip *ip;
		int hlen;

		if ((m->m_type != MT_DATA) && (m->m_type != MT_HEADER)) {
			printf("IP Filter: ipl_ipfilter_kernel: bad m_type=%d m_flags=0x%lx m_off=0x%lx\n", m->m_type, (unsigned long)(m->m_flags), m->m_off);
			return IPF_ACCEPTIT;
		}

#if	IPFDEBUG >= 4
		if (!MBUF_IS_CLUSTER(m) && ((m->m_off < MMINOFF) || (m->m_off > MMAXOFF))) {
			printf("IP Filter: ipl_ipfilter_kernel: bad m_off m_type=%d m_flags=0x%lx m_off=0x%lx\n", m->m_type, (unsigned long)(m->m_flags), m->m_off);
			return IPF_ACCEPTIT;
		}
#endif
		if (m->m_len < sizeof(char)) {
			printf("IP Filter: ipl_ipfilter_kernel: mbuf block too small (m_len=%d) for IP vers+hlen, m_type=%d m_flags=0x%lx\n", m->m_len, m->m_type, (unsigned long)(m->m_flags));
			return IPF_ACCEPTIT;
		}
		ip = mtod(m, struct ip *);
		if (ip->ip_v != IPVERSION) {
			printf("IP Filter: ipl_ipfilter_kernel: bad ip_v\n");
			m_freem(m);
			return IPF_DROPIT;
		}

		hlen = ip->ip_hl << 2;
		if ((*fr_checkp)(ip, hlen, rcvif, 0, &m1) || !m1)
			return IPF_DROPIT;
		if (m != m1)
			printf("IP Filter: ipl_ipfilter_kernel: m != m1\n");
	}

	return IPF_ACCEPTIT;
}

static int
ipfilterattach(void)
{
#ifdef IPFILTER_LKM
	__psunsigned_t *addr_ff, *addr_fk;

	st_findaddr("ipfilterflag", &addr_ff);
#if	IPFDEBUG >= 4
	printf("IP Filter: st_findaddr ipfilterflag=0x%lx\n", addr_ff);
#endif
	if (!addr_ff)
		return ESRCH;

	st_findaddr("ipfilter_kernel", &addr_fk);
#if	IPFDEBUG >= 4
	printf("IP Filter: st_findaddr ipfilter_kernel=0x%lx\n", addr_fk);
#endif
	if (!addr_fk)
		return ESRCH;

	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */

	ipff_addr = (int *)addr_ff;
	
	ipff_value = *ipff_addr;
	*ipff_addr = 0;


	ipfk_addr = addr_fk;

	bcopy(ipfk_addr, ipfk_code,
		sizeof(ipfk_code));

	/* write a "li t4, ipl_ipfilter_kernel" instruction */
	ipfk_addr[0] = 0x3c0c0000 |
		       (((__psunsigned_t)IPL_EXTERN(_kernel) >> 16) & 0xffff);
	ipfk_addr[1] = 0x358c0000 |
		       ((__psunsigned_t)IPL_EXTERN(_kernel) & 0xffff);
	/* write a "jr t4" instruction" */
	ipfk_addr[2] = 0x01800008;

	/* write a "nop" instruction */
	ipfk_addr[3] = 0;

	icache_inval(ipfk_addr, sizeof(ipfk_code));

	*ipff_addr = 1; /* enable ipfilter_kernel */

	MUTEX_EXIT(&ipfi_mutex);
#else
	extern int ipfilterflag;

	ipfilterflag = 1;
#endif

	return 0;
}

/*
 * attach the packet filter to each non-loopback interface that is running
 */
static void
nifattach()
{
	nif_t *nif, *qf2;
	struct ifnet *ifp;
	struct frentry *f;
	ipnat_t *np;

	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */

	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
		if ((!(ifp->if_flags & IFF_RUNNING)) ||
			(ifp->if_flags & IFF_LOOPBACK))
			continue;

		/*
		 * Look for entry already setup for this device
		 */
		for (nif = nif_head; nif; nif = nif->nf_next)
			if (nif->nf_ifp == ifp)
				break;
		if (nif)
			continue;

		if (ifp->if_output == ipl_if_output) {
			printf("IP Filter: ERROR INTF 0x%lx STILL ATTACHED\n",
				ifp);
			continue;
		}
#if	IPFDEBUG >= 4
		printf("IP Filter: nifattach nif %x opt %x\n",
		       ifp, ifp->if_output);
#endif
		KMALLOC(nif, nif_t *);
		if (!nif) {
			printf("IP Filter: malloc(%d) for nif_t failed\n",
			       sizeof(nif_t));
			continue;
		}

		nif->nf_ifp = ifp;
		strncpy(nif->nf_name, ifp->if_name, sizeof(nif->nf_name));
		nif->nf_name[sizeof(nif->nf_name) - 1] = '\0';
		nif->nf_unit = ifp->if_unit;

		nif->nf_next = nif_head;
		nif_head = nif;

		/*
		 * Activate any rules directly associated with this interface
		 */
		MUTEX_ENTER(&ipf_mutex);
		for (f = ipfilter[0][0]; f; f = f->fr_next) {
			if ((f->fr_ifa == (struct ifnet *)-1)) {
				if (f->fr_ifname[0] &&
				    (GETUNIT(f->fr_ifname, 4) == ifp))
					f->fr_ifa = ifp;
			}
		}
		for (f = ipfilter[1][0]; f; f = f->fr_next) {
			if ((f->fr_ifa == (struct ifnet *)-1)) {
				if (f->fr_ifname[0] &&
				    (GETUNIT(f->fr_ifname, 4) == ifp))
					f->fr_ifa = ifp;
			}
		}
		MUTEX_EXIT(&ipf_mutex);
		MUTEX_ENTER(&ipf_nat);
		for (np = nat_list; np; np = np->in_next) {
			if ((np->in_ifp == (void *)-1)) {
				if (np->in_ifname[0] &&
				    (GETUNIT(np->in_ifname, 4) == ifp))
					np->in_ifp = (void *)ifp;
			}
		}
		MUTEX_EXIT(&ipf_nat);

		nif->nf_output = ifp->if_output;
		ifp->if_output = ipl_if_output;

#if	IPFDEBUG >= 4
		printf("IP Filter: nifattach: ifp(%lx)->if_output FROM %lx TO %lx\n",
			ifp, nif->nf_output, ifp->if_output);
#endif

		printf("IP Filter: attach to [%s,%d]\n",
			nif->nf_name, ifp->if_unit);
	}
	if (!nif_head)
		printf("IP Filter: not attached to any interfaces\n");

	nif_interfaces = in_interfaces;

	MUTEX_EXIT(&ipfi_mutex);

	return;
}

/*
 * look for bad consistancies between the list of interfaces the filter knows
 * about and those which are currently configured.
 */
int
ipfsync(void)
{
	register struct frentry *f;
	register ipnat_t *np;
	register nif_t *nif, **qp;
	register struct ifnet *ifp;

	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */
	for (qp = &nif_head; (nif = *qp); ) {
		for (ifp = ifnet; ifp; ifp = ifp->if_next)
			if ((nif->nf_ifp == ifp) &&
			    (nif->nf_unit == ifp->if_unit) &&
			    !strcmp(nif->nf_name, ifp->if_name)) {
				break;
			}
		if (ifp) {
			qp = &nif->nf_next;
			continue;
		}
		printf("IP Filter: detaching [%s]\n", nif->nf_name);
		*qp = nif->nf_next;

		/*
		 * Disable any rules directly associated with this interface
		 */
		MUTEX_ENTER(&ipf_mutex);
		for (f = ipfilter[0][0]; f; f = f->fr_next)
			if (f->fr_ifa == (void *)nif->nf_ifp)
				f->fr_ifa = (struct ifnet *)-1;
		for (f = ipfilter[1][0]; f; f = f->fr_next)
			if (f->fr_ifa == (void *)nif->nf_ifp)
				f->fr_ifa = (struct ifnet *)-1;
		MUTEX_EXIT(&ipf_mutex);
		MUTEX_ENTER(&ipf_nat);
		for (np = nat_list; np; np = np->in_next)
			if (np->in_ifp == (void *)nif->nf_ifp)
				np->in_ifp =(struct ifnet *)-1;
		MUTEX_EXIT(&ipf_nat);

		KFREE(nif);
		nif = *qp;
	}
	MUTEX_EXIT(&ipfi_mutex);

	nifattach();

	return 0;
}


/*
 * unhook the IP filter from all defined interfaces with IP addresses
 */
static void
nifdetach()
{
	nif_t *nif, *qf2, **qp;
	struct ifnet *ifp;

	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */
	/*
	 * Make two passes, first get rid of all the unknown devices, next
	 * unlink known devices.
	 */
	for (qp = &nif_head; (nif = *qp); ) {
		for (ifp = ifnet; ifp; ifp = ifp->if_next)
			if (nif->nf_ifp == ifp)
				break;
		if (ifp) {
			qp = &nif->nf_next;
			continue;
		}
		printf("IP Filter: removing [%s]\n", nif->nf_name);
		*qp = nif->nf_next;
		KFREE(nif);
	}

	while ((nif = nif_head)) {
		nif_head = nif->nf_next;
		for (ifp = ifnet; ifp; ifp = ifp->if_next)
			if (nif->nf_ifp == ifp)
				break;
		if (ifp) {
			printf("IP Filter: detaching [%s,%d]\n",
				nif->nf_name, ifp->if_unit);

#if	IPFDEBUG >= 4
			printf("IP Filter: nifdetach: ifp(%lx)->if_output FROM %lx TO %lx\n",
				ifp, ifp->if_output, nif->nf_output);
#endif
			ifp->if_output = nif->nf_output;
		}
		KFREE(nif);
	}
	MUTEX_EXIT(&ipfi_mutex);

	return;
}


static void
ipfilterdetach(void)
{
#ifdef IPFILTER_LKM
	MUTEX_ENTER(&ipfi_mutex); /* sets interrupt priority level to splhi */

	if (ipff_addr) {
		*ipff_addr = 0;

		if (ipfk_addr)
			bcopy(ipfk_code, ipfk_addr, sizeof(ipfk_code));

		*ipff_addr = ipff_value;
	}

	MUTEX_EXIT(&ipfi_mutex);
#else
	extern int ipfilterflag;

	ipfilterflag = 0;
#endif
}

/* called by ipldetach() */
void
ipfilter_sgi_detach(void)
{
	nifdetach();

	ipfilterdetach();
}

/* called by iplattach() */
int
ipfilter_sgi_attach(void)
{
	int error;

	nif_interfaces = 0;

	error = ipfilterattach();

	if (!error)
		nifattach();

	return error;
}

/* this function is called from ipfr_slowtimer at 500ms intervals to
   keep our interface list in sync */
void
ipfilter_sgi_intfsync(void)
{
	MUTEX_ENTER(&ipfi_mutex);
	if (nif_interfaces != in_interfaces) {
		/* if the number of interfaces has changed, resync */
		MUTEX_EXIT(&ipfi_mutex);
		ipfsync();
	} else
		MUTEX_EXIT(&ipfi_mutex);
}

#ifdef IPFILTER_LKM
/* this routine should be treated as an interrupt routine and should
   not call any routines that would cause it to sleep, such as: biowait(),
   sleep(), psema() or delay().
*/
int
IPL_EXTERN(unload)(void)
{
	int error = 0;

	error = ipldetach();

	LOCK_DEALLOC(ipl_mutex.l);
	LOCK_DEALLOC(ipf_rw.l);
	LOCK_DEALLOC(ipf_auth.l);
	LOCK_DEALLOC(ipf_natfrag.l);
	LOCK_DEALLOC(ipf_nat.l);
	LOCK_DEALLOC(ipf_state.l);
	LOCK_DEALLOC(ipf_frag.l);
	LOCK_DEALLOC(ipf_mutex.l);
	LOCK_DEALLOC(ipfi_mutex.l);

	return error;
}
#endif

void
IPL_EXTERN(init)(void)
{
#ifdef IPFILTER_LKM
	int error;
#endif

	ipfi_mutex.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_mutex.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_frag.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_state.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_nat.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_natfrag.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_auth.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipf_rw.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);
	ipl_mutex.l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP);

	if (!ipfi_mutex.l || !ipf_mutex.l || !ipf_frag.l || !ipf_state.l ||
	    !ipf_nat.l || !ipf_natfrag.l || !ipf_auth.l || !ipf_rw.l ||
	    !ipl_mutex.l)
		panic("IP Filter: LOCK_ALLOC failed");

#ifdef IPFILTER_LKM
	error = iplattach();
	if (error) {
		IPL_EXTERN(unload)();
	}
#endif

	return;
}

OpenPOWER on IntegriCloud