-
Notifications
You must be signed in to change notification settings - Fork 2
/
gre_ip6filter.c
211 lines (179 loc) · 4.22 KB
/
gre_ip6filter.c
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
/*
* gre_ipfilter.c
* gre
*
* Created by Summer Town on 11/30/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include <sys/systm.h>
#include <netinet/kpi_ipfilter.h>
#include "gre_locks.h"
#include "gre_ip_encap.h"
#include "if_gre.h"
#include "gre_ipfilter.h"
static errno_t gre_ip6filter_attach(void);
static errno_t gre_ip6filter_detach(void);
static errno_t gre_ipv6_infilter(void *, mbuf_t *, int, u_int8_t);
static lck_mtx_t *gre_ip6f_mtx = NULL;
static ipfilter_t gre_ipv6filter = NULL;
/*
* gre_ipfilter_init(), initialize resources required by ip filter
*/
int
gre_ip6filter_init(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
if (gre_ip6f_mtx != NULL) {
#ifdef DEBUG
printf("%s: gre_if6p_mtx already inited\n", __FUNCTION__);
#endif
goto success;
}
gre_ip6f_mtx = lck_mtx_alloc_init(gre_ipf_lck_grp, gre_ipf_lck_attributes);
if (gre_ip6f_mtx == NULL)
goto failed;
if (gre_ip6filter_attach()) {/* attach ip filter */
lck_mtx_free(gre_ip6f_mtx, gre_ipf_lck_grp);
gre_ip6f_mtx = NULL;
goto failed;
}
success:
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return 0;
failed:
#ifdef DEBUG
printf("%s: fail\n", __FUNCTION__);
#endif
return -1;
}
/*
* gre_ipfilter_dispose(), the opposite to gre_ipfilter_init(), ie clean up
*/
int
gre_ip6filter_dispose(void)
{
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
if (gre_ip6f_mtx == NULL) {
#ifdef DEBUG
printf("%s: gre_ifp_mtx already freed\n", __FUNCTION__);
#endif
return 0;
}
if (gre_ip6filter_detach() == 0) {
if (gre_ip6f_mtx != NULL) {
lck_mtx_free(gre_ip6f_mtx, gre_ipf_lck_grp);
gre_ip6f_mtx = NULL;
}
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return 0;
}
#ifdef DEBUG
printf("%s: error dispose ipfilter\n", __FUNCTION__);
#endif
return -1;
}
/*
* is called to notify the filter that it has been detached.
*/
static void
gre_ipv6_if_detach(void *cookie)
{
lck_mtx_lock(gre_ip6f_mtx);
if (gre_ipv6filter) {
gre_ipv6filter = NULL;
lck_mtx_unlock(gre_ip6f_mtx);
wakeup(&gre_ipv6filter);
} else
lck_mtx_unlock(gre_ip6f_mtx);
}
/*
* gre_ip6filter_attach(), attach ipv6 filter
*/
static errno_t
gre_ip6filter_attach(void)
{
if (gre_ipv6filter)
return 0;
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
errno_t err = 0;
struct ipf_filter ipf;
bzero(&ipf, sizeof(struct ipf_filter));
ipf.cookie = (caddr_t)&gre_ipv6filter;
ipf.name = "org.gmshake.nke.gre_ipv6filter";
ipf.ipf_input = gre_ipv6_infilter;
ipf.ipf_detach = gre_ipv6_if_detach;
err = ipf_addv6(&ipf, &gre_ipv6filter);
if (err)
printf("%s: ipf_addv6(), err=0x%x\n", __FUNCTION__, err);
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return err;
}
/*
* gre_ipfilter_detach(), detach ipv6 filter
*/
static errno_t
gre_ip6filter_detach(void)
{
if (gre_ipv6filter == NULL)
return 0;
#ifdef DEBUG
printf("%s ...\n", __FUNCTION__);
#endif
errno_t err = 0;
err = ipf_remove(gre_ipv6filter);
if (err == 0) {
lck_mtx_lock(gre_ip6f_mtx);
if (gre_ipv6filter) {
/* wait for the detach process */
msleep(&gre_ipv6filter, gre_ip6f_mtx, PDROP, NULL, NULL);
} else {
lck_mtx_unlock(gre_ip6f_mtx);
}
}
#ifdef DEBUG
printf("%s: done\n", __FUNCTION__);
#endif
return err;
}
/* the caller who call this function(ipv6_infilter) will free the mbuf when
* it returns any error except EJUSTRETURN.
* so, remember to check the function called by this function if it frees
* the mbuf chain on error. That is, do remember return EJUSTRETURN
* if you frees the mbuf or the function called by this function frees the mbuf.
* Otherwise, DOUBLE FREE, causing kernel panic...
*
* return ZERO if this filter is not interested in the packet
* otherwise, it means this filter deal with the packet, and other filters will
* not see this packet
*
* @param cookie
* @param m
* @param offset ip header offset
* @param protocol proto, IPPROTO_GRE/IPPROTO_MOBILE
*/
static errno_t
gre_ipv6_infilter(void *cookie, mbuf_t *data, int offset, u_int8_t protocol)
{
errno_t error;
error = gre_encap6_input(data, &offset, protocol);
if (error && error != EJUSTRETURN) {
#if DEBUG
printf("%s invalid return value: %d\n", __FUNCTION__, error);
#endif
error = EJUSTRETURN; // FIXIT
}
return error;
}