xref: /titanic_50/usr/src/uts/common/inet/ip/ipdrop.c (revision 1d53067866b073ea6710000ba4dd448441361988)
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 
164 	/* AH-specific drop statistics. */
165 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_nomem,
166 	    "ah_nomem", KSTAT_DATA_UINT64);
167 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_v6_hdrs,
168 	    "ah_bad_v6_hdrs", KSTAT_DATA_UINT64);
169 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_v4_opts,
170 	    "ah_bad_v4_opts", KSTAT_DATA_UINT64);
171 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_no_sa,
172 	    "ah_no_sa", KSTAT_DATA_UINT64);
173 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_length,
174 	    "ah_bad_length", KSTAT_DATA_UINT64);
175 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bad_auth,
176 	    "ah_bad_auth", KSTAT_DATA_UINT64);
177 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_crypto_failed,
178 	    "ah_crypto_failed", KSTAT_DATA_UINT64);
179 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_early_replay,
180 	    "ah_early_replay", KSTAT_DATA_UINT64);
181 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_replay,
182 	    "ah_replay", KSTAT_DATA_UINT64);
183 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ah_bytes_expire,
184 	    "ah_bytes_expire", KSTAT_DATA_UINT64);
185 
186 	/* IP-specific drop statistics. */
187 	kstat_named_init(&ipss->ipsec_ip_drop_types->ipds_ip_ipsec_not_loaded,
188 	    "ip_ipsec_not_loaded", KSTAT_DATA_UINT64);
189 
190 	kstat_install(ipss->ipsec_ip_drop_kstat);
191 }
192 
193 void
194 ip_drop_destroy(ipsec_stack_t *ipss)
195 {
196 	kstat_delete_netstack(ipss->ipsec_ip_drop_kstat,
197 	    ipss->ipsec_netstack->netstack_stackid);
198 	ipss->ipsec_ip_drop_kstat = NULL;
199 	ipss->ipsec_ip_drop_types = NULL;
200 }
201 
202 /*
203  * Register a packet dropper.
204  */
205 void
206 ip_drop_register(ipdropper_t *ipd, char *name)
207 {
208 	if (ipd->ipd_name != NULL) {
209 		cmn_err(CE_WARN,
210 		    "ip_drop_register: ipdropper %s already registered with %s",
211 		    name, ipd->ipd_name);
212 		return;
213 	}
214 
215 	/* Assume that name is reasonable in length.  This isn't user-land. */
216 	ipd->ipd_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
217 	(void) strcpy(ipd->ipd_name, name);
218 }
219 
220 /*
221  * Un-register a packet dropper.
222  */
223 void
224 ip_drop_unregister(ipdropper_t *ipd)
225 {
226 	if (ipd->ipd_name == NULL) {
227 		cmn_err(CE_WARN,
228 		    "ip_drop_unregister: not registered (%p)\n",
229 		    (void *)ipd);
230 		return;
231 	}
232 	kmem_free(ipd->ipd_name, strlen(ipd->ipd_name) + 1);
233 
234 	ipd->ipd_name = NULL;
235 }
236 
237 /*
238  * Actually drop a packet.  Many things could happen here, but at the least,
239  * the packet will be freemsg()ed.
240  */
241 /* ARGSUSED */
242 void
243 ip_drop_packet(mblk_t *mp, boolean_t inbound, ill_t *arriving,
244     ire_t *outbound_ire, struct kstat_named *counter, ipdropper_t *who_called)
245 {
246 	mblk_t *ipsec_mp = NULL;
247 	ipsec_in_t *ii = NULL;
248 	ipsec_out_t *io = NULL;
249 	ipsec_info_t *in;
250 	uint8_t vers;
251 
252 	if (mp == NULL) {
253 		/*
254 		 * Return immediately - NULL packets should not affect any
255 		 * statistics.
256 		 */
257 		return;
258 	}
259 
260 	if (DB_TYPE(mp) == M_CTL) {
261 		in = (ipsec_info_t *)mp->b_rptr;
262 
263 		if (in->ipsec_info_type == IPSEC_IN)
264 			ii = (ipsec_in_t *)in;
265 		else if (in->ipsec_info_type == IPSEC_OUT)
266 			io = (ipsec_out_t *)in;
267 
268 		/* See if this is an ICMP packet (check for v4/v6). */
269 		vers = (*mp->b_rptr) >> 4;
270 		if (vers != IPV4_VERSION && vers != IPV6_VERSION) {
271 			/*
272 			 * If not, it's some other sort of M_CTL to be freed.
273 			 * For now, treat it like an ordinary packet.
274 			 */
275 			ipsec_mp = mp;
276 			mp = mp->b_cont;
277 		}
278 	}
279 
280 	/* Reality checks */
281 	if (inbound && io != NULL)
282 		cmn_err(CE_WARN,
283 		    "ip_drop_packet: inbound packet with IPSEC_OUT");
284 
285 	if (outbound_ire != NULL && ii != NULL)
286 		cmn_err(CE_WARN,
287 		    "ip_drop_packet: outbound packet with IPSEC_IN");
288 
289 	/* At this point, mp always points to the data. */
290 	/*
291 	 * Can't make the assertion yet - It could be an inbound ICMP
292 	 * message, which is M_CTL but with data in it.
293 	 */
294 	/* ASSERT(mp->b_datap->db_type == M_DATA); */
295 
296 	/* Increment the bean counter, if available. */
297 	if (counter != NULL) {
298 		switch (counter->data_type) {
299 		case KSTAT_DATA_INT32:
300 			counter->value.i32++;
301 			break;
302 		case KSTAT_DATA_UINT32:
303 			counter->value.ui32++;
304 			break;
305 		case KSTAT_DATA_INT64:
306 			counter->value.i64++;
307 			break;
308 		case KSTAT_DATA_UINT64:
309 			counter->value.ui64++;
310 			break;
311 		/* Other types we can't handle for now. */
312 		}
313 
314 		/* TODO?  Copy out kstat name for use in logging. */
315 	}
316 
317 	/* TODO: log the packet details if logging is called for. */
318 	/* TODO: queue the packet onto a snoop-friendly queue. */
319 
320 	/* If I haven't queued the packet or some such nonsense, free it. */
321 	if (ipsec_mp != NULL)
322 		freeb(ipsec_mp);
323 	/*
324 	 * ASSERT this isn't a b_next linked mblk chain where a
325 	 * chained dropper should be used instead
326 	 */
327 	ASSERT(mp->b_prev == NULL && mp->b_next == NULL);
328 	freemsg(mp);
329 }
330