IPsec Invalid SPI Explained: When One End Rebuilds the Tunnel and the Other Never Notices
Every IPsec session is stamped with a number called an SPI — a security parameter index — that tells the receiving router exactly which session, keys and rules an encrypted packet belongs to. When the two ends of a tunnel disagree about the current session, one side keeps sending packets stamped with an SPI the other side deleted when it rebuilt the tunnel. The receiver looks the number up, finds nothing, drops the packet and logs %CRYPTO-4-RECVD_PKT_INV_SPI. The tunnel interface still looks up, the management session may still look alive, and no user traffic passes — which is why this fault so often appears minutes after a brief outage that everyone thought had already healed.
The short answer
What does an IPsec invalid SPI error mean? One end of a VPN tunnel is sending encrypted packets stamped with a session number — an SPI — that the receiving router no longer has. The receiver cannot match the packet to any live session, so it cannot decrypt it, and it drops the packet and logs %CRYPTO-4-RECVD_PKT_INV_SPI: decaps: rec'd IPSEC packet has invalid spi. In almost every case the two ends disagree about which session is current: one side rebuilt the tunnel (after a reboot, a bounce, or a rekey) and moved to a fresh session, while the other side is still sending on the old one. The fix is to clear the stale session for that one peer so both sides rebuild together, then remove the reason they drifted apart — usually Dead Peer Detection being off or mismatched, or lifetimes that differ wildly between the ends.
Picture two offices that shred and reissue their visitor badges every few hours. Every sealed parcel between them must carry a current badge number, and the receiving office checks the number against today's list before opening anything. Now one office's badge printer breaks, a new list is issued at that end — and nobody tells the other office. Parcels keep arriving with yesterday's badge number. The receiving office is not being difficult when it bins them: a parcel whose badge matches nothing on the list could be anything, so the rule is drop it and write it in the log. That log entry is the invalid SPI message. The offices are still connected, the phone line between them still works — the parcels are simply stamped with a session the receiver has already thrown away.
How the receiver decides a packet is an orphan
When an ESP packet arrives, the router does not start by decrypting it. It reads three things from the outside of the packet — the destination address, the protocol (50 for ESP), and the SPI — and looks that combination up in its database of live sessions. That lookup is the whole design: the SPI is how one router can hold hundreds of tunnels and still know instantly which keys a given packet needs. If the lookup finds a session, decryption proceeds. If it finds nothing, there is no safe fallback — guessing keys is not an option — so the packet is dropped and the invalid-SPI message is logged with the destination, the protocol, the SPI value itself, and the sender's address. Every field in that log line is a clue: the SPI is the exact number to go hunting for in your own session table.
Neither router is broken. Each is correctly following its own session table — the tables simply disagree, and the receiver's drop is the only safe answer to a packet it cannot place.
The three ways the two ends drift apart
1. One peer rebooted or bounced, and the other never noticed. This is the cause behind most cases. When a router restarts, its session database is empty; when the tunnel is re-established it negotiates fresh sessions with fresh SPIs. The surviving peer, if nothing told it the other end died, keeps its old outbound session and keeps encrypting onto it. Every one of those packets arrives stamped with a number the rebuilt peer has never heard of. The brief outage ends, the link comes back, the tunnel is declared restored — and the traffic black-holes until the stale side is cleaned up.
2. Lifetimes differ wildly between the ends. Sessions expire on timers — a time lifetime, a traffic-volume lifetime, or both — and expiry triggers a rekey to a fresh session. If the two ends' lifetimes are far apart, one side expires and rebuilds sessions on a rhythm the other side does not share, and any hiccup in the rekey negotiation leaves the same one-sided state as a reboot: one end on the new session, the other still trusting the old one. Aligned lifetimes do not make rekeys disappear; they make both ends expect them at the same time.
3. Dead Peer Detection is off, or set differently at each end. DPD exists precisely for cause 1: the peers probe each other, and a peer that stops answering gets its sessions torn down, so the next packets trigger a clean, mutual rebuild instead of hitting a stale table. With DPD off, a silent death leaves the survivor's sessions in place indefinitely — the stale state has no expiry mechanism except the lifetime timers. With DPD on at one end only, cleanup happens on one side's schedule, which can itself feed the desync. The setting is cheap; the mismatch is the expensive part.
Why the tunnel still looks up
This fault fools the first check most engineers run. The management session between the peers (the IKE session that negotiates and guards the tunnel) can be alive and current while the data sessions it produced are the stale ones — they are related, but they are not the same table. Interfaces stay up, the peer answers pings, monitoring that watches the tunnel's state rather than its traffic stays green, and users report the one symptom that matters: nothing passes. The invalid-SPI log is the router telling you which layer is lying. When a site-to-site tunnel is reported down, read the log before you read the session state — the message names the fault, the peer, and the exact SPI to compare.
Checks, in order
Step 1 — read the log line properly. Note the SPI value, the destination and the source address from the message itself, and check the log history (show logging | include CRYPTO) for how often it repeats. Once, right after a known restart, and then silence: that was the rebuild clearing its throat — confirm traffic passes and move on. Repeating every few minutes: a standing mismatch; continue.
Step 2 — compare the SPI against your own table. Run show crypto ipsec sa | include spi and look for the SPI from the log among the sessions this router expects to receive on. It will not be there — that absence is the smoking gun, and it confirms the sender is encrypting onto a session you no longer hold, rather than your router dropping good traffic.
Step 3 — check the management session. show crypto isakmp sa shows whether the negotiation session to that peer is alive and current. A live management session alongside invalid-SPI drops is the classic shape of this fault: negotiation healthy, data sessions desynchronised. It also tells you a rebuild has something healthy to build on.
Step 4 — compare the two ends' settings before you clear anything. Lifetimes and DPD configuration on both peers. This takes minutes while both ends are still in front of you, and it is the difference between fixing the fault and scheduling its return — the clear in the next section will work either way, but only this step decides whether you will be back here after the next reboot.
The safe fix order
Clear one peer, not the world. The working fix is to clear the stale sessions for that peer only — clear crypto sa peer <ip> — which forces both sides to negotiate a fresh, matching set of sessions; traffic usually flows as soon as the rebuild completes. Scope the command to the one peer address. On a router terminating many tunnels, clearing all crypto sessions bounces every tunnel on the box to repair one, which turns a single-site fault into a self-inflicted outage.
Pick the moment. Clearing sessions drops the tunnel's traffic until the renegotiation finishes — normally seconds, but seconds matter on a production link carrying live work. This is a safe fix, not a free one: do it in a change window, or at least with the business warned, rather than the instant you spot the log line during peak traffic. The exception is a tunnel that is already passing nothing — then there is nothing left to lose and the clear is the fastest path back.
Then remove the cause. Turn DPD on at both ends with matching settings, so a dead peer's sessions are cleaned up automatically next time. Align the session lifetimes on both peers. Verify afterwards the way a sceptic would: the invalid-SPI messages stop, show crypto ipsec sa shows encapsulating and decapsulating packet counters both rising, and a real user flow — not just a ping to the peer address — crosses the tunnel.
What not to do
Do not reboot your router to clear the state: it works, the way demolishing a house fixes a stuck door, and it exports the outage to everything else the box carries. Do not clear sessions repeatedly as a standing remedy — if the message returns after a clean rebuild, the desync has a live cause (settings from step 4, or a peer that keeps bouncing) and repetition is masking it. And do not assume a quiet log after the fix means the settings are right: verify DPD and lifetimes match on both ends while you still have both ends open, because the next reboot is exactly when a mismatch you left behind will present this same fault again. For the exact log string itself — what each field means and the short-form checks — the companion decoder entry is %CRYPTO-4-RECVD_PKT_INV_SPI. For link-layer encryption between directly connected devices — a different mechanism from tunnel IPsec, with its own overhead cost — see MACsec Explained.
Try it yourself
When two tunnel ends may have drifted in configuration as well as in state, run both ends' configs through the free Network Config Verifier and compare what it flags on each side — mismatched settings are far easier to spot side by side than from memory in the middle of an incident.
Fact-check sources: the invalid-SPI message format, meaning and platform line, the stale-session causes (one peer rebooted or bounced while the other's session stayed stale; DPD off or mismatched; lifetimes differing wildly), the check order (show crypto isakmp sa; show crypto ipsec sa | include spi to compare expected SPIs against the logged SPI; show logging | include CRYPTO for frequency) and the fix (clear crypto sa peer, DPD on both ends with matching settings, align lifetimes) — Cisco IPsec troubleshooting documentation, as carried in this site's Error Decoder entry for %CRYPTO-4-RECVD_PKT_INV_SPI. SPI as a 32-bit value in the ESP header, looked up with the destination address and protocol to select the security association, ESP as IP protocol 50 — RFC 4303 (ESP) and RFC 4301 (IPsec architecture).
Related guides
- An SPI (security parameter index) is a 32-bit number carried in the header of every ESP packet (IP protocol 50). Together with the destination address and the protocol, it is the lookup key the receiver uses to find the matching security association — the stored session holding the keys and rules for that traffic. No matching association, no decryption: the packet is dropped, not queued or retried.
- The classic cause is a one-sided rebuild: one peer rebooted, bounced, or rekeyed and built a fresh session with fresh SPIs, while the other peer never noticed and is still encrypting onto the old session. Mismatched lifetimes make the same desync more likely, because one side expires and rebuilds sessions the other side still trusts, and Dead Peer Detection (DPD) being off or mismatched means neither side cleans up the dead session on its own.
- The symptom pattern is a tunnel that looks up but passes no traffic, usually starting right after a brief outage, a peer reboot, or a rekey — with the invalid-SPI message repeating in the log. A message that appears once after a restart and then stops is the normal untidiness of a rebuild; a message repeating every few minutes is a standing mismatch that will not heal by waiting.
- The safe fix order is: confirm the SPI in the log is not among the SPIs this side expects, check how often the message repeats, then clear only that one peer's sessions (clear crypto sa peer <ip>) at a moment when a short renegotiation is acceptable, and let both sides rebuild together. Then fix the cause — DPD on both ends with matching settings, and lifetimes aligned — so the desync does not return at the next reboot or rekey. Never clear every crypto session on a multi-tunnel router to fix one tunnel.