async-snmp 0.18.1

Modern async-first SNMP client library for Rust
Documentation
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
//! Protocol version-specific notification handlers.
//!
//! This module contains the internal handlers for processing `SNMPv1`, v2c, and v3
//! notification messages.

use std::net::SocketAddr;

use bytes::Bytes;

use crate::udp_responder::DestinationMetadata;

use crate::error::{Error, Result};
use crate::message::{CommunityMessage, MsgGlobalData};
use crate::pdu::{Pdu, PduType};
use crate::v3::UsmSecurityParams;
use crate::v3::encode::encode_v3_response;
use crate::v3::process::{UsmFailure, V3Inbound, V3LocalContext, V3Role, process_v3_inbound};

use super::varbind::extract_notification_varbinds;
use super::{
    InformAckOutcome, Notification, NotificationPduClass, NotificationWireIdentity,
    ReceivedNotification, ReceiverInner, V3NotificationWireIdentity,
};
use crate::v3::DerivedKeys;

impl super::NotificationReceiver {
    /// Handle `SNMPv1` message.
    pub(super) async fn handle_v1(
        &self,
        data: Bytes,
        source: SocketAddr,
    ) -> Result<Option<ReceivedNotification>> {
        let decoded =
            CommunityMessage::decode_with_target(data, Some(source), self.inner.decode_config)?;
        let decode_anomalies = decoded.anomalies;
        let msg = decoded.value;

        if !crate::util::community_matches(
            &self.inner.communities,
            msg.community().as_bytes(),
            crate::util::EmptyCommunityPolicy::Allow,
        ) {
            tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "dropped v1 notification with unaccepted community");
            return Ok(None);
        }

        let (_, community, pdu) = msg.into_parts();
        match pdu {
            crate::message::CommunityPdu::TrapV1(trap) => {
                let notification = Notification::TrapV1 {
                    community,
                    trap,
                    decode_anomalies,
                };
                if !self.inner.accepts(source, &notification) {
                    tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "notification acceptance policy dropped v1 trap");
                    return Ok(None);
                }
                Ok(Some(ReceivedNotification::trap(
                    notification,
                    source,
                    NotificationWireIdentity {
                        version: crate::Version::V1,
                        request_id: None,
                        v3: None,
                    },
                )))
            }
            crate::message::CommunityPdu::Standard(_) => Ok(None),
        }
    }

    /// Handle `SNMPv2c` message.
    #[cfg(test)]
    pub(super) async fn handle_v2c(
        &self,
        data: Bytes,
        source: SocketAddr,
    ) -> Result<Option<ReceivedNotification>> {
        self.handle_v2c_at(data, source, None).await
    }

    pub(super) async fn handle_v2c_at(
        &self,
        data: Bytes,
        source: SocketAddr,
        response_source: Option<DestinationMetadata>,
    ) -> Result<Option<ReceivedNotification>> {
        let decoded =
            CommunityMessage::decode_with_target(data, Some(source), self.inner.decode_config)?;
        let decode_anomalies = decoded.anomalies;
        let msg = decoded.value;

        if !crate::util::community_matches(
            &self.inner.communities,
            msg.community().as_bytes(),
            crate::util::EmptyCommunityPolicy::Allow,
        ) {
            tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "dropped v2c notification with unaccepted community");
            return Ok(None);
        }

        // V2c messages carry standard PDUs; TrapV1 is only valid in V1 messages.
        let Some(pdu) = msg.pdu().standard() else {
            return Ok(None);
        };

        match pdu.pdu_type() {
            PduType::TrapV2 => {
                let (uptime, trap_oid, varbinds) =
                    extract_notification_varbinds(pdu, self.inner.varbind_validation)?;
                let notification = Notification::TrapV2c {
                    community: msg.community().clone(),
                    uptime,
                    trap_oid,
                    varbinds,
                    request_id: pdu.request_id,
                    decode_anomalies,
                };
                if !self.inner.accepts(source, &notification) {
                    tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "notification acceptance policy dropped v2c trap");
                    return Ok(None);
                }
                Ok(Some(ReceivedNotification::trap(
                    notification,
                    source,
                    NotificationWireIdentity {
                        version: crate::Version::V2c,
                        request_id: Some(pdu.request_id),
                        v3: None,
                    },
                )))
            }
            PduType::InformRequest => {
                let (uptime, trap_oid, varbinds) =
                    extract_notification_varbinds(pdu, self.inner.varbind_validation)?;
                let request_id = pdu.request_id;
                let notification = Notification::InformV2c {
                    community: msg.community().clone(),
                    uptime,
                    trap_oid,
                    varbinds,
                    request_id,
                    decode_anomalies,
                };
                if !self.inner.accepts(source, &notification) {
                    tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "notification acceptance policy dropped v2c Inform");
                    return Ok(None);
                }

                #[cfg(test)]
                let injected_error = self
                    .inner
                    .response_construction_errors
                    .lock()
                    .unwrap()
                    .pop_front();
                #[cfg(not(test))]
                let injected_error: Option<Box<Error>> = None;
                let finalized = if let Some(error) = injected_error {
                    Err(error)
                } else {
                    pdu.to_response(crate::Version::V2c).and_then(|response| {
                        crate::response_finalizer::finalize_response(
                            crate::Version::V2c,
                            pdu,
                            response,
                            self.inner.max_message_size,
                            None,
                            &self.inner.snmp_silent_drops,
                            |response| {
                                CommunityMessage::v2c(msg.community().clone(), response)?.encode()
                            },
                        )
                    })
                };
                let outcome = match finalized {
                    Ok(crate::response_finalizer::FinalizedResponse::Dropped) => {
                        tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.request_id = request_id }, "Inform response and tooBig alternate exceed max message size");
                        InformAckOutcome::SuppressedBySize
                    }
                    Ok(finalized) => {
                        let response_bytes = finalized
                            .into_bytes()
                            .expect("non-dropped response contains bytes");
                        match self
                            .send_response(&response_bytes, source, response_source)
                            .await
                        {
                            Ok(()) => {
                                tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.request_id = request_id }, "sent Inform response");
                                InformAckOutcome::Sent
                            }
                            Err(error) => InformAckOutcome::Failed(
                                Error::Network {
                                    target: source,
                                    source: error,
                                }
                                .boxed(),
                            ),
                        }
                    }
                    Err(error) => InformAckOutcome::Failed(error),
                };

                Ok(Some(ReceivedNotification::inform(
                    notification,
                    source,
                    NotificationWireIdentity {
                        version: crate::Version::V2c,
                        request_id: Some(request_id),
                        v3: None,
                    },
                    outcome,
                )))
            }
            _ => Ok(None), // Not a notification PDU
        }
    }

    /// Handle `SNMPv3` message.
    ///
    /// USM processing (RFC 3414 Section 3.2) runs in the shared
    /// [`process_v3_inbound`] core in the receiver role: informs under this
    /// receiver's engine ID use the authoritative time window (Step 7a),
    /// traps under a remote authoritative engine ID use per-engine
    /// timeliness state (Step 7b).
    #[cfg(test)]
    pub(super) async fn handle_v3(
        &self,
        data: Bytes,
        source: SocketAddr,
    ) -> Result<Option<ReceivedNotification>> {
        self.handle_v3_at(data, source, None).await
    }

    pub(super) async fn handle_v3_at(
        &self,
        data: Bytes,
        source: SocketAddr,
        response_source: Option<DestinationMetadata>,
    ) -> Result<Option<ReceivedNotification>> {
        let (our_boots, our_time) = super::unpack_boots_time(
            self.inner
                .authoritative_snapshot
                .load(std::sync::atomic::Ordering::Relaxed),
        );
        let sample_authoritative_time = || self.inner.authoritative_boots_time();
        let usm_ctx = V3LocalContext {
            engine_id: &self.inner.engine_id,
            engine_boots: our_boots,
            engine_time: our_time,
            authoritative_time: Some(&sample_authoritative_time),
            local_receive_capacity: crate::UDP_RECEIVE_LIMITS.advertised(),
            accepted_receive_size: crate::UDP_RECEIVE_LIMITS.accepted(),
            decode_config: self.inner.decode_config,
            outbound_limit: self.inner.max_message_size,
            usm_users: &self.inner.usm_users,
            stats: &self.inner.usm_stats,
            mpd: None,
            source,
        };
        let role = V3Role::Receiver {
            remote_engines: &self.inner.remote_engines,
        };

        let inbound = match process_v3_inbound(data, &usm_ctx, &role)? {
            V3Inbound::Failed { failure, report } => {
                // The shared core logs USM failures at debug; re-surface them
                // at warn in the receiver role so a misconfigured trap sender
                // is diagnosable at the default log level.
                tracing::warn!(target: "async_snmp::notification", { snmp.source = %source, snmp.failure = ?failure }, "USM processing failed for inbound message");
                if let Some(report) = report {
                    if let Err(e) = self.send_response(&report, source, response_source).await {
                        tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, error = %e }, "failed to send USM report");
                    } else {
                        tracing::debug!(target: "async_snmp::notification", { snmp.source = %source }, "sent USM report");
                    }
                }
                // Authentication-class failures are error indications to the
                // caller; the rest are quietly dropped after the report.
                return match failure {
                    UsmFailure::WrongDigests
                    | UsmFailure::NotInTimeWindows
                    | UsmFailure::DecryptionErrors => Err(Error::Auth { target: source }.boxed()),
                    UsmFailure::UnknownEngineIds
                    | UsmFailure::UnknownUserNames
                    | UsmFailure::UnsupportedSecLevels => Ok(None),
                };
            }
            V3Inbound::RemoteNotInTimeWindow => {
                return Err(Error::Auth { target: source }.boxed());
            }
            V3Inbound::Message(inbound) => inbound,
        };
        let global_data = &inbound.global_data;
        let usm_params = &inbound.usm_params;
        let scoped_pdu = &inbound.scoped_pdu;
        let security_level = inbound.security_level;
        let decode_anomalies = inbound.decode_anomalies.clone();
        let username = usm_params.username.clone();
        let wire_identity = NotificationWireIdentity {
            version: crate::Version::V3,
            request_id: Some(inbound.scoped_pdu.pdu.request_id),
            v3: Some(V3NotificationWireIdentity {
                msg_id: global_data.msg_id,
                authoritative_engine_id: usm_params.engine_id.clone(),
                username: username.clone(),
                security_level,
            }),
        };

        let context_engine_id = scoped_pdu.context_engine_id.clone();
        let context_name = scoped_pdu.context_name.clone();
        let pdu = &scoped_pdu.pdu;

        let pdu_class = match pdu.pdu_type() {
            PduType::TrapV2 => NotificationPduClass::Trap,
            PduType::InformRequest => {
                // The receiver of a Confirmed-class PDU is authoritative.
                if usm_params.engine_id.as_ref() != self.inner.engine_id.as_ref() {
                    tracing::warn!(target: "async_snmp::notification", { snmp.source = %source }, "dropped v3 Inform localized to a foreign authoritative engine ID");
                    return Ok(None);
                }
                NotificationPduClass::Inform
            }
            _ => return Ok(None),
        };
        match pdu.pdu_type() {
            PduType::TrapV2 => {
                let (uptime, trap_oid, varbinds) =
                    extract_notification_varbinds(pdu, self.inner.varbind_validation)?;
                let notification = Notification::TrapV3 {
                    username,
                    context_engine_id,
                    context_name,
                    security_level,
                    uptime,
                    trap_oid,
                    varbinds,
                    request_id: pdu.request_id,
                    decode_anomalies,
                };
                if !self.inner.accepts(source, &notification) {
                    tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.security_level = ?security_level, pdu_class = ?pdu_class }, "notification acceptance policy dropped v3 notification");
                    return Ok(None);
                }
                Ok(Some(ReceivedNotification::trap(
                    notification,
                    source,
                    wire_identity,
                )))
            }
            PduType::InformRequest => {
                let (uptime, trap_oid, varbinds) =
                    extract_notification_varbinds(pdu, self.inner.varbind_validation)?;
                let request_id = pdu.request_id;
                let notification = Notification::InformV3 {
                    username,
                    context_engine_id: context_engine_id.clone(),
                    context_name: context_name.clone(),
                    security_level,
                    uptime,
                    trap_oid,
                    varbinds,
                    request_id,
                    decode_anomalies,
                };
                if !self.inner.accepts(source, &notification) {
                    tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.security_level = ?security_level, pdu_class = ?pdu_class }, "notification acceptance policy dropped v3 notification");
                    return Ok(None);
                }

                #[cfg(test)]
                let injected_error = self
                    .inner
                    .response_construction_errors
                    .lock()
                    .unwrap()
                    .pop_front();
                #[cfg(not(test))]
                let injected_error: Option<Box<Error>> = None;
                let finalized = if let Some(error) = injected_error {
                    Err(error)
                } else {
                    pdu.to_response(crate::Version::V3).and_then(|response| {
                        crate::response_finalizer::finalize_response(
                            crate::Version::V3,
                            pdu,
                            response,
                            self.inner.max_message_size,
                            Some(global_data.msg_max_size.as_usize()),
                            &self.inner.snmp_silent_drops,
                            |response_pdu| {
                                build_v3_response(
                                    &self.inner,
                                    global_data,
                                    usm_params,
                                    response_pdu,
                                    context_engine_id.clone(),
                                    context_name.clone(),
                                    Some(&inbound.derived_keys),
                                )
                            },
                        )
                    })
                };
                let outcome = match finalized {
                    Ok(crate::response_finalizer::FinalizedResponse::Dropped) => {
                        tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.request_id = request_id, snmp.security_level = ?security_level }, "V3 Inform response and tooBig alternate exceed max message size");
                        InformAckOutcome::SuppressedBySize
                    }
                    Ok(finalized) => {
                        let response_bytes = finalized
                            .into_bytes()
                            .expect("non-dropped response contains bytes");
                        match self
                            .send_response(&response_bytes, source, response_source)
                            .await
                        {
                            Ok(()) => {
                                tracing::debug!(target: "async_snmp::notification", { snmp.source = %source, snmp.request_id = request_id, snmp.security_level = ?security_level }, "sent V3 Inform response");
                                InformAckOutcome::Sent
                            }
                            Err(error) => InformAckOutcome::Failed(
                                Error::Network {
                                    target: source,
                                    source: error,
                                }
                                .boxed(),
                            ),
                        }
                    }
                    Err(error) => InformAckOutcome::Failed(error),
                };

                Ok(Some(ReceivedNotification::inform(
                    notification,
                    source,
                    wire_identity,
                    outcome,
                )))
            }
            _ => Ok(None),
        }
    }
}

/// Build a V3 response message with appropriate security.
///
/// The response uses the receiver's current authoritative engine tuple and
/// echoes only the requester's username from the incoming USM parameters.
fn build_v3_response(
    inner: &ReceiverInner,
    incoming: &MsgGlobalData,
    incoming_usm: &UsmSecurityParams,
    response_pdu: Pdu,
    context_engine_id: Bytes,
    context_name: Bytes,
    derived_keys: Option<&DerivedKeys>,
) -> Result<Bytes> {
    // Derive both fields from one elapsed-time sample immediately before
    // encoding so the response is current and cannot straddle a rollover.
    let (engine_boots, engine_time) = inner.authoritative_boots_time()?;
    let response_usm = UsmSecurityParams::new(
        inner.engine_id.clone(),
        engine_boots,
        engine_time,
        incoming_usm.username.clone(),
    )?;

    encode_v3_response(
        response_pdu,
        incoming.msg_id,
        // RFC 3412 Section 6.3: msgMaxSize advertises this receiver's own
        // receive capacity, not the sender's echoed value. The receiver has no
        // configurable limit, so advertise the default UDP receive capacity.
        crate::UDP_RECEIVE_LIMITS.advertised(),
        incoming.msg_flags.security_level,
        response_usm,
        context_engine_id,
        context_name,
        derived_keys,
        inner.salt_counter.as_ref(),
        inner.des_salt_state.as_ref(),
        inner.local_addr,
    )
}