xref: /illumos-gate/usr/src/uts/common/inet/ip/ipdrop.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/strsun.h>
31 #include <sys/sunddi.h>
32 #include <sys/kstat.h>
33 #include <sys/kmem.h>
34 #include <net/pfkeyv2.h>
35 #include <inet/common.h>
36 #include <inet/ip.h>
37 #include <inet/ip6.h>
38 #include <inet/ipsec_info.h>
39 #include <inet/ipsec_impl.h>
40 #include <inet/ipdrop.h>
41 
42 /*
43  * Packet drop facility.
44  */
45 
46 /*
47  * Initialize drop facility kstats.
48  */
49 void
50 ip_drop_init(ipsec_stack_t *ipss)
51 {
52 	ipss->ipsec_ip_drop_kstat = kstat_create_netstack("ip", 0, "ipdrop",
53 	    "net", KSTAT_TYPE_NAMED,
54 	    sizeof (struct ip_dropstats) / sizeof (kstat_named_t),
55 	    KSTAT_FLAG_PERSISTENT, ipss->ipsec_netstack->netstack_stackid);
56 
57 	if (ipss->ipsec_ip_drop_kstat == NULL ||
58 	    ipss->ipsec_ip_drop_kstat->ks_data == NULL)
59 		return;
60 
61 	/*
62 	 * Note: here ipss->ipsec_ip_drop_types is initialized, however,
63 	 * if the previous kstat_create_netstack failed, it will remain
64 	 * NULL. Note this is done for all stack instances, so it *could*
65 	 * be NULL. Hence a non-NULL checking is added where
66 	 * ipss->ipsec_ip_drop_types is used. This checking is hidden in
67 	 * the DROPPER macro.
68 	 */
69 	ipss->ipsec_ip_drop_types = ipss->ipsec_ip_drop_kstat->ks_data;
70 
71 	/* TCP IPsec drop statistics. */
72 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_tcp_clear,
73 	    "tcp_clear", KSTAT_DATA_UINT64);
74 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_tcp_secure,
75 	    "tcp_secure", KSTAT_DATA_UINT64);
76 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_tcp_mismatch,
77 	    "tcp_mismatch", KSTAT_DATA_UINT64);
78 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_tcp_ipsec_alloc,
79 	    "tcp_ipsec_alloc", KSTAT_DATA_UINT64);
80 
81 	/* SADB-specific drop statistics. */
82 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_sadb_inlarval_timeout,
83 	    "sadb_inlarval_timeout", KSTAT_DATA_UINT64);
84 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_sadb_inlarval_replace,
85 	    "sadb_inlarval_replace", KSTAT_DATA_UINT64);
86 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_sadb_acquire_nomem,
87 	    "sadb_acquire_nomem", KSTAT_DATA_UINT64);
88 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_sadb_acquire_toofull,
89 	    "sadb_acquire_toofull", KSTAT_DATA_UINT64);
90 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_sadb_acquire_timeout,
91 	    "sadb_acquire_timeout", KSTAT_DATA_UINT64);
92 
93 	/* SPD drop statistics. */
94 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_ahesp_diffid,
95 	    "spd_ahesp_diffid", KSTAT_DATA_UINT64);
96 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_loopback_mismatch,
97 	    "spd_loopback_mismatch", KSTAT_DATA_UINT64);
98 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_explicit,
99 	    "spd_explicit", KSTAT_DATA_UINT64);
100 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_got_secure,
101 	    "spd_got_secure", KSTAT_DATA_UINT64);
102 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_got_clear,
103 	    "spd_got_clear", KSTAT_DATA_UINT64);
104 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_bad_ahalg,
105 	    "spd_bad_ahalg", KSTAT_DATA_UINT64);
106 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_got_ah,
107 	    "spd_got_ah", KSTAT_DATA_UINT64);
108 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_bad_espealg,
109 	    "spd_bad_espealg", KSTAT_DATA_UINT64);
110 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_bad_espaalg,
111 	    "spd_bad_espaalg", KSTAT_DATA_UINT64);
112 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_got_esp,
113 	    "spd_got_esp", KSTAT_DATA_UINT64);
114 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_got_selfencap,
115 	    "spd_got_selfencap", KSTAT_DATA_UINT64);
116 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_bad_selfencap,
117 	    "spd_bad_selfencap", KSTAT_DATA_UINT64);
118 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_nomem,
119 	    "spd_nomem", KSTAT_DATA_UINT64);
120 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_ah_badid,
121 	    "spd_ah_badid", KSTAT_DATA_UINT64);
122 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_ah_innermismatch,
123 	    "spd_ah_innermismatch", KSTAT_DATA_UINT64);
124 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_esp_innermismatch,
125 	    "spd_esp_innermismatch", KSTAT_DATA_UINT64);
126 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_esp_badid,
127 	    "spd_esp_badid", KSTAT_DATA_UINT64);
128 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_no_policy,
129 	    "spd_no_policy", KSTAT_DATA_UINT64);
130 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_malformed_packet,
131 	    "spd_malformed_packet", KSTAT_DATA_UINT64);
132 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_malformed_frag,
133 	    "spd_malformed_frag", KSTAT_DATA_UINT64);
134 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_overlap_frag,
135 	    "spd_overlap_frag", KSTAT_DATA_UINT64);
136 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_evil_frag,
137 	    "spd_evil_frag", KSTAT_DATA_UINT64);
138 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_spd_max_frags,
139 	    "spd_max_frags", KSTAT_DATA_UINT64);
140 
141 	/* ESP-specific drop statistics. */
142 
143 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_nomem,
144 	    "esp_nomem", KSTAT_DATA_UINT64);
145 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_no_sa,
146 	    "esp_no_sa", KSTAT_DATA_UINT64);
147 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_early_replay,
148 	    "esp_early_replay", KSTAT_DATA_UINT64);
149 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_replay,
150 	    "esp_replay", KSTAT_DATA_UINT64);
151 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_bytes_expire,
152 	    "esp_bytes_expire", KSTAT_DATA_UINT64);
153 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_bad_padlen,
154 	    "esp_bad_padlen", KSTAT_DATA_UINT64);
155 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_bad_padding,
156 	    "esp_bad_padding", KSTAT_DATA_UINT64);
157 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_bad_auth,
158 	    "esp_bad_auth", KSTAT_DATA_UINT64);
159 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_crypto_failed,
160 	    "esp_crypto_failed", KSTAT_DATA_UINT64);
161 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_icmp,
162 	    "esp_icmp", KSTAT_DATA_UINT64);
163 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_nat_t_ipsec,
164 	    "esp_nat_t_ipsec", KSTAT_DATA_UINT64);
165 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_esp_nat_t_ka,
166 	    "esp_nat_t_ka", KSTAT_DATA_UINT64);
167 
168 	/* AH-specific drop statistics. */
169 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_nomem,
170 	    "ah_nomem", KSTAT_DATA_UINT64);
171 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_v6_hdrs,
172 	    "ah_bad_v6_hdrs", KSTAT_DATA_UINT64);
173 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_v4_opts,
174 	    "ah_bad_v4_opts", KSTAT_DATA_UINT64);
175 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_no_sa,
176 	    "ah_no_sa", KSTAT_DATA_UINT64);
177 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_length,
178 	    "ah_bad_length", KSTAT_DATA_UINT64);
179 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_auth,
180 	    "ah_bad_auth", KSTAT_DATA_UINT64);
181 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_crypto_failed,
182 	    "ah_crypto_failed", KSTAT_DATA_UINT64);
183 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_early_replay,
184 	    "ah_early_replay", KSTAT_DATA_UINT64);
185 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_replay,
186 	    "ah_replay", KSTAT_DATA_UINT64);
187 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bytes_expire,
188 	    "ah_bytes_expire", KSTAT_DATA_UINT64);
189 
190 	/* IP-specific drop statistics. */
191 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ip_ipsec_not_loaded,
192 	    "ip_ipsec_not_loaded", KSTAT_DATA_UINT64);
193 
194 	kstat_install(ipss->ipsec_ip_drop_kstat);
195 }
196 
197 void
198 ip_drop_destroy(ipsec_stack_t *ipss)
199 {
200 	kstat_delete_netstack(ipss->ipsec_ip_drop_kstat,
201 	    ipss->ipsec_netstack->netstack_stackid);
202 	ipss->ipsec_ip_drop_kstat = NULL;
203 	ipss->ipsec_ip_drop_types = NULL;
204 }
205 
206 /*
207  * Register a packet dropper.
208  */
209 void
210 ip_drop_register(ipdropper_t *ipd, char *name)
211 {
212 	if (ipd->ipd_name != NULL) {
213 		cmn_err(CE_WARN,
214 		    "ip_drop_register: ipdropper %s already registered with %s",
215 		    name, ipd->ipd_name);
216 		return;
217 	}
218 
219 	/* Assume that name is reasonable in length.  This isn't user-land. */
220 	ipd->ipd_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
221 	(void) strcpy(ipd->ipd_name, name);
222 }
223 
224 /*
225  * Un-register a packet dropper.
226  */
227 void
228 ip_drop_unregister(ipdropper_t *ipd)
229 {
230 	if (ipd->ipd_name == NULL) {
231 		cmn_err(CE_WARN,
232 		    "ip_drop_unregister: not registered (%p)\n",
233 		    (void *)ipd);
234 		return;
235 	}
236 	kmem_free(ipd->ipd_name, strlen(ipd->ipd_name) + 1);
237 
238 	ipd->ipd_name = NULL;
239 }
240 
241 /*
242  * Actually drop a packet.  Many things could happen here, but at the least,
243  * the packet will be freemsg()ed.
244  */
245 /* ARGSUSED */
246 void
247 ip_drop_packet(mblk_t *mp, boolean_t inbound, ill_t *arriving,
248     ire_t *outbound_ire, struct kstat_named *counter, ipdropper_t *who_called)
249 {
250 	mblk_t *ipsec_mp = NULL;
251 	ipsec_in_t *ii = NULL;
252 	ipsec_out_t *io = NULL;
253 	ipsec_info_t *in;
254 	uint8_t vers;
255 
256 	if (mp == NULL) {
257 		/*
258 		 * Return immediately - NULL packets should not affect any
259 		 * statistics.
260 		 */
261 		return;
262 	}
263 
264 	if (DB_TYPE(mp) == M_CTL) {
265 		in = (ipsec_info_t *)mp->b_rptr;
266 
267 		if (in->ipsec_info_type == IPSEC_IN)
268 			ii = (ipsec_in_t *)in;
269 		else if (in->ipsec_info_type == IPSEC_OUT)
270 			io = (ipsec_out_t *)in;
271 
272 		/* See if this is an ICMP packet (check for v4/v6). */
273 		vers = (*mp->b_rptr) >> 4;
274 		if (vers != IPV4_VERSION && vers != IPV6_VERSION) {
275 			/*
276 			 * If not, it's some other sort of M_CTL to be freed.
277 			 * For now, treat it like an ordinary packet.
278 			 */
279 			ipsec_mp = mp;
280 			mp = mp->b_cont;
281 		}
282 	}
283 
284 	/* Reality checks */
285 	if (inbound && io != NULL)
286 		cmn_err(CE_WARN,
287 		    "ip_drop_packet: inbound packet with IPSEC_OUT");
288 
289 	if (outbound_ire != NULL && ii != NULL)
290 		cmn_err(CE_WARN,
291 		    "ip_drop_packet: outbound packet with IPSEC_IN");
292 
293 	/* At this point, mp always points to the data. */
294 	/*
295 	 * Can't make the assertion yet - It could be an inbound ICMP
296 	 * message, which is M_CTL but with data in it.
297 	 */
298 	/* ASSERT(mp->b_datap->db_type == M_DATA); */
299 
300 	/* Increment the bean counter, if available. */
301 	if (counter != NULL) {
302 		switch (counter->data_type) {
303 		case KSTAT_DATA_INT32:
304 			counter->value.i32++;
305 			break;
306 		case KSTAT_DATA_UINT32:
307 			counter->value.ui32++;
308 			break;
309 		case KSTAT_DATA_INT64:
310 			counter->value.i64++;
311 			break;
312 		case KSTAT_DATA_UINT64:
313 			counter->value.ui64++;
314 			break;
315 		/* Other types we can't handle for now. */
316 		}
317 
318 		/* TODO?  Copy out kstat name for use in logging. */
319 	}
320 
321 	/* TODO: log the packet details if logging is called for. */
322 	/* TODO: queue the packet onto a snoop-friendly queue. */
323 
324 	/* If I haven't queued the packet or some such nonsense, free it. */
325 	if (ipsec_mp != NULL)
326 		freeb(ipsec_mp);
327 	/*
328 	 * ASSERT this isn't a b_next linked mblk chain where a
329 	 * chained dropper should be used instead
330 	 */
331 	ASSERT(mp->b_prev == NULL && mp->b_next == NULL);
332 	freemsg(mp);
333 }
334