]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
isisd: Provide statistics about retransmitted LSPs
[mirror_frr.git] / isisd / isis_pdu.c
CommitLineData
eb5d44eb 1/*
d62a17ae 2 * IS-IS Rout(e)ing protocol - isis_pdu.c
eb5d44eb 3 * PDU processing
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
d62a17ae 6 * Tampere University of Technology
eb5d44eb 7 * Institute of Communications Engineering
8 *
d62a17ae 9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
eb5d44eb 12 * any later version.
13 *
d62a17ae 14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb5d44eb 17 * more details.
896014f4
DL
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
eb5d44eb 22 */
23
eb5d44eb 24#include <zebra.h>
eb5d44eb 25
26#include "memory.h"
27#include "thread.h"
28#include "linklist.h"
29#include "log.h"
30#include "stream.h"
31#include "vty.h"
3f045a08 32#include "hash.h"
eb5d44eb 33#include "prefix.h"
34#include "if.h"
6a270cd9 35#include "checksum.h"
3f045a08 36#include "md5.h"
38937bd5 37#include "lib_errors.h"
eb5d44eb 38
39#include "isisd/dict.h"
eb5d44eb 40#include "isisd/isis_constants.h"
41#include "isisd/isis_common.h"
3f045a08 42#include "isisd/isis_flags.h"
eb5d44eb 43#include "isisd/isis_adjacency.h"
44#include "isisd/isis_circuit.h"
45#include "isisd/isis_network.h"
46#include "isisd/isis_misc.h"
47#include "isisd/isis_dr.h"
eb5d44eb 48#include "isisd/isisd.h"
49#include "isisd/isis_dynhn.h"
50#include "isisd/isis_lsp.h"
51#include "isisd/isis_pdu.h"
52#include "isisd/iso_checksum.h"
53#include "isisd/isis_csm.h"
54#include "isisd/isis_events.h"
f8c06e2c 55#include "isisd/isis_te.h"
99894f9a 56#include "isisd/isis_mt.h"
841791b6 57#include "isisd/isis_tlvs.h"
54ece698 58#include "isisd/isis_errors.h"
8e6fb83b 59#include "isisd/fabricd.h"
9b39405f 60#include "isisd/isis_tx_queue.h"
39bb53d6 61#include "isisd/isis_pdu_counter.h"
eb5d44eb 62
af8ac8f9
CF
63static int ack_lsp(struct isis_lsp_hdr *hdr, struct isis_circuit *circuit,
64 int level)
eb5d44eb 65{
af8ac8f9
CF
66 unsigned long lenp;
67 int retval;
d7c0a89a 68 uint16_t length;
af8ac8f9
CF
69 uint8_t pdu_type =
70 (level == IS_LEVEL_1) ? L1_PARTIAL_SEQ_NUM : L2_PARTIAL_SEQ_NUM;
d62a17ae 71
af8ac8f9 72 isis_circuit_stream(circuit, &circuit->snd_stream);
3f045a08 73
af8ac8f9 74 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 75
af8ac8f9
CF
76 lenp = stream_get_endp(circuit->snd_stream);
77 stream_putw(circuit->snd_stream, 0); /* PDU length */
78 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
79 stream_putc(circuit->snd_stream, circuit->idx);
80 stream_putc(circuit->snd_stream, 9); /* code */
81 stream_putc(circuit->snd_stream, 16); /* len */
d62a17ae 82
af8ac8f9
CF
83 stream_putw(circuit->snd_stream, hdr->rem_lifetime);
84 stream_put(circuit->snd_stream, hdr->lsp_id, ISIS_SYS_ID_LEN + 2);
85 stream_putl(circuit->snd_stream, hdr->seqno);
86 stream_putw(circuit->snd_stream, hdr->checksum);
3f045a08 87
d7c0a89a 88 length = (uint16_t)stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
89 /* Update PDU length */
90 stream_putw_at(circuit->snd_stream, lenp, length);
3f045a08 91
39bb53d6 92 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
af8ac8f9
CF
93 retval = circuit->tx(circuit, level);
94 if (retval != ISIS_OK)
1a7ecb96 95 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
96 "ISIS-Upd (%s): Send L%d LSP PSNP on %s failed",
97 circuit->area->area_tag, level,
98 circuit->interface->name);
d62a17ae 99
100 return retval;
eb5d44eb 101}
102
eb5d44eb 103/*
d62a17ae 104 * RECEIVE SIDE
eb5d44eb 105 */
106
0c1bd758
CF
107struct iih_info {
108 struct isis_circuit *circuit;
d7c0a89a 109 uint8_t *ssnpa;
0c1bd758 110 int level;
eb5d44eb 111
0c1bd758
CF
112 uint8_t circ_type;
113 uint8_t sys_id[ISIS_SYS_ID_LEN];
114 uint16_t holdtime;
115 uint16_t pdu_len;
d62a17ae 116
0c1bd758 117 uint8_t circuit_id;
eb5d44eb 118
0c1bd758
CF
119 uint8_t priority;
120 uint8_t dis[ISIS_SYS_ID_LEN + 1];
d8fba7d9 121
0c1bd758
CF
122 bool v4_usable;
123 bool v6_usable;
eb5d44eb 124
0c1bd758
CF
125 struct isis_tlvs *tlvs;
126};
eb5d44eb 127
0c1bd758
CF
128static int process_p2p_hello(struct iih_info *iih)
129{
42fe2621
CF
130 struct isis_threeway_adj *tw_adj = iih->tlvs->threeway_adj;
131 if (tw_adj) {
132 if (tw_adj->state > ISIS_THREEWAY_DOWN) {
133 if (isis->debugs & DEBUG_ADJ_PACKETS) {
134 zlog_debug("ISIS-Adj (%s): Rcvd P2P IIH from (%s) with invalid three-way state: %d\n",
135 iih->circuit->area->area_tag,
136 iih->circuit->interface->name,
137 tw_adj->state);
138 }
139 return ISIS_WARNING;
140 }
141
142 if (tw_adj->neighbor_set
143 && (memcmp(tw_adj->neighbor_id, isis->sysid, ISIS_SYS_ID_LEN)
144 || tw_adj->neighbor_circuit_id != (uint32_t) iih->circuit->idx)) {
145
146 if (isis->debugs & DEBUG_ADJ_PACKETS) {
147 zlog_debug("ISIS-Adj (%s): Rcvd P2P IIH from (%s) which lists IS/Circuit different from us as neighbor.\n",
148 iih->circuit->area->area_tag,
149 iih->circuit->interface->name);
150 }
151
152 return ISIS_WARNING;
153 }
154 }
155
d62a17ae 156 /*
157 * My interpertation of the ISO, if no adj exists we will create one for
158 * the circuit
159 */
0c1bd758 160 struct isis_adjacency *adj = iih->circuit->u.p2p.neighbor;
d62a17ae 161 /* If an adjacency exists, check it is with the source of the hello
162 * packets */
163 if (adj) {
0c1bd758 164 if (memcmp(iih->sys_id, adj->sysid, ISIS_SYS_ID_LEN)) {
d62a17ae 165 zlog_debug(
166 "hello source and adjacency do not match, set adj down\n");
167 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
168 "adj do not exist");
0c1bd758 169 return ISIS_OK;
f390d2c7 170 }
d62a17ae 171 }
0c1bd758 172 if (!adj || adj->level != iih->circ_type) {
d62a17ae 173 if (!adj) {
0c1bd758
CF
174 adj = isis_new_adj(iih->sys_id, NULL, iih->circ_type,
175 iih->circuit);
d62a17ae 176 } else {
0c1bd758 177 adj->level = iih->circ_type;
f390d2c7 178 }
0c1bd758 179 iih->circuit->u.p2p.neighbor = adj;
d62a17ae 180 /* Build lsp with the new neighbor entry when a new
181 * adjacency is formed. Set adjacency circuit type to
182 * IIH PDU header circuit type before lsp is regenerated
183 * when an adjacency is up. This will result in the new
184 * adjacency entry getting added to the lsp tlv neighbor list.
185 */
0c1bd758 186 adj->circuit_t = iih->circ_type;
d62a17ae 187 isis_adj_state_change(adj, ISIS_ADJ_INITIALIZING, NULL);
188 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
f390d2c7 189 }
eb5d44eb 190
42fe2621
CF
191 if (tw_adj && adj->threeway_state == ISIS_THREEWAY_DOWN)
192 adj->ext_circuit_id = tw_adj->local_circuit_id;
193
d62a17ae 194 /* 8.2.6 Monitoring point-to-point adjacencies */
0c1bd758 195 adj->hold_time = iih->holdtime;
d62a17ae 196 adj->last_upd = time(NULL);
197
0c1bd758
CF
198 bool changed;
199 isis_tlvs_to_adj(iih->tlvs, adj, &changed);
200 changed |= tlvs_to_adj_mt_set(iih->tlvs, iih->v4_usable, iih->v6_usable,
201 adj);
d62a17ae 202
203 /* Update MPLS TE Remote IP address parameter if possible */
0c1bd758
CF
204 if (IS_MPLS_TE(isisMplsTE) && iih->circuit->mtc
205 && IS_CIRCUIT_TE(iih->circuit->mtc) && adj->ipv4_address_count)
206 set_circuitparams_rmt_ipaddr(iih->circuit->mtc,
207 adj->ipv4_addresses[0]);
d62a17ae 208
209 /* lets take care of the expiry */
210 THREAD_TIMER_OFF(adj->t_expire);
211 thread_add_timer(master, isis_adj_expire, adj, (long)adj->hold_time,
212 &adj->t_expire);
213
8e6fb83b
CF
214 /* While fabricds initial sync is in progress, ignore hellos from other
215 * interfaces than the one we are performing the initial sync on. */
216 if (fabricd_initial_sync_is_in_progress(iih->circuit->area)
217 && fabricd_initial_sync_circuit(iih->circuit->area) != iih->circuit)
218 return ISIS_OK;
219
d62a17ae 220 /* 8.2.5.2 a) a match was detected */
0c1bd758
CF
221 if (isis_tlvs_area_addresses_match(iih->tlvs,
222 iih->circuit->area->area_addrs)) {
d62a17ae 223 /* 8.2.5.2 a) 2) If the system is L1 - table 5 */
0c1bd758
CF
224 if (iih->circuit->area->is_type == IS_LEVEL_1) {
225 switch (iih->circ_type) {
d62a17ae 226 case IS_LEVEL_1:
227 case IS_LEVEL_1_AND_2:
42fe2621
CF
228 if (adj->adj_state != ISIS_ADJ_UP
229 || adj->adj_usage == ISIS_ADJ_LEVEL1) {
230 isis_adj_process_threeway(adj, tw_adj,
231 ISIS_ADJ_LEVEL1);
d62a17ae 232 }
233 break;
234 case IS_LEVEL_2:
235 if (adj->adj_state != ISIS_ADJ_UP) {
236 /* (7) reject - wrong system type event
237 */
238 zlog_warn("wrongSystemType");
0c1bd758 239 return ISIS_WARNING;
d62a17ae 240 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
241 /* (6) down - wrong system */
242 isis_adj_state_change(adj,
243 ISIS_ADJ_DOWN,
244 "Wrong System");
245 }
246 break;
247 }
f390d2c7 248 }
d62a17ae 249
250 /* 8.2.5.2 a) 3) If the system is L1L2 - table 6 */
0c1bd758
CF
251 if (iih->circuit->area->is_type == IS_LEVEL_1_AND_2) {
252 switch (iih->circ_type) {
d62a17ae 253 case IS_LEVEL_1:
42fe2621
CF
254 if (adj->adj_state != ISIS_ADJ_UP
255 || adj->adj_usage == ISIS_ADJ_LEVEL1) {
256 isis_adj_process_threeway(adj, tw_adj,
257 ISIS_ADJ_LEVEL1);
d62a17ae 258 } else if ((adj->adj_usage
259 == ISIS_ADJ_LEVEL1AND2)
260 || (adj->adj_usage
261 == ISIS_ADJ_LEVEL2)) {
262 /* (8) down - wrong system */
263 isis_adj_state_change(adj,
264 ISIS_ADJ_DOWN,
265 "Wrong System");
266 }
267 break;
268 case IS_LEVEL_2:
42fe2621
CF
269 if (adj->adj_state != ISIS_ADJ_UP
270 || adj->adj_usage == ISIS_ADJ_LEVEL2) {
271 isis_adj_process_threeway(adj, tw_adj,
272 ISIS_ADJ_LEVEL2);
d62a17ae 273 } else if ((adj->adj_usage == ISIS_ADJ_LEVEL1)
274 || (adj->adj_usage
275 == ISIS_ADJ_LEVEL1AND2)) {
276 /* (8) down - wrong system */
277 isis_adj_state_change(adj,
278 ISIS_ADJ_DOWN,
279 "Wrong System");
d62a17ae 280 }
281 break;
282 case IS_LEVEL_1_AND_2:
42fe2621
CF
283 if (adj->adj_state != ISIS_ADJ_UP
284 || adj->adj_usage == ISIS_ADJ_LEVEL1AND2) {
285 isis_adj_process_threeway(adj, tw_adj,
286 ISIS_ADJ_LEVEL1AND2);
d62a17ae 287 } else if ((adj->adj_usage == ISIS_ADJ_LEVEL1)
288 || (adj->adj_usage
289 == ISIS_ADJ_LEVEL2)) {
290 /* (8) down - wrong system */
291 isis_adj_state_change(adj,
292 ISIS_ADJ_DOWN,
293 "Wrong System");
d62a17ae 294 }
295 break;
296 }
f390d2c7 297 }
d62a17ae 298
299 /* 8.2.5.2 a) 4) If the system is L2 - table 7 */
0c1bd758
CF
300 if (iih->circuit->area->is_type == IS_LEVEL_2) {
301 switch (iih->circ_type) {
d62a17ae 302 case IS_LEVEL_1:
303 if (adj->adj_state != ISIS_ADJ_UP) {
304 /* (5) reject - wrong system type event
305 */
306 zlog_warn("wrongSystemType");
0c1bd758 307 return ISIS_WARNING;
d62a17ae 308 } else if ((adj->adj_usage
309 == ISIS_ADJ_LEVEL1AND2)
310 || (adj->adj_usage
311 == ISIS_ADJ_LEVEL2)) {
312 /* (6) down - wrong system */
313 isis_adj_state_change(adj,
314 ISIS_ADJ_DOWN,
315 "Wrong System");
316 }
317 break;
318 case IS_LEVEL_1_AND_2:
319 case IS_LEVEL_2:
42fe2621
CF
320 if (adj->adj_state != ISIS_ADJ_UP
321 || adj->adj_usage == ISIS_ADJ_LEVEL2) {
322 isis_adj_process_threeway(adj, tw_adj,
323 ISIS_ADJ_LEVEL2);
d62a17ae 324 } else if (adj->adj_usage
325 == ISIS_ADJ_LEVEL1AND2) {
326 /* (6) down - wrong system */
327 isis_adj_state_change(adj,
328 ISIS_ADJ_DOWN,
329 "Wrong System");
d62a17ae 330 }
331 break;
332 }
f390d2c7 333 }
d62a17ae 334 }
335 /* 8.2.5.2 b) if no match was detected */
0c1bd758
CF
336 else if (listcount(iih->circuit->area->area_addrs) > 0) {
337 if (iih->circuit->area->is_type == IS_LEVEL_1) {
d62a17ae 338 /* 8.2.5.2 b) 1) is_type L1 and adj is not up */
339 if (adj->adj_state != ISIS_ADJ_UP) {
340 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
341 "Area Mismatch");
342 /* 8.2.5.2 b) 2)is_type L1 and adj is up */
343 } else {
344 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
345 "Down - Area Mismatch");
346 }
f390d2c7 347 }
d62a17ae 348 /* 8.2.5.2 b 3 If the system is L2 or L1L2 - table 8 */
349 else {
0c1bd758 350 switch (iih->circ_type) {
d62a17ae 351 case IS_LEVEL_1:
352 if (adj->adj_state != ISIS_ADJ_UP) {
353 /* (6) reject - Area Mismatch event */
354 zlog_warn("AreaMismatch");
0c1bd758 355 return ISIS_WARNING;
d62a17ae 356 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
357 /* (7) down - area mismatch */
358 isis_adj_state_change(adj,
359 ISIS_ADJ_DOWN,
360 "Area Mismatch");
361
362 } else if ((adj->adj_usage
363 == ISIS_ADJ_LEVEL1AND2)
364 || (adj->adj_usage
365 == ISIS_ADJ_LEVEL2)) {
366 /* (7) down - wrong system */
367 isis_adj_state_change(adj,
368 ISIS_ADJ_DOWN,
369 "Wrong System");
370 }
371 break;
372 case IS_LEVEL_1_AND_2:
373 case IS_LEVEL_2:
42fe2621
CF
374 if (adj->adj_state != ISIS_ADJ_UP
375 || adj->adj_usage == ISIS_ADJ_LEVEL2) {
376 isis_adj_process_threeway(adj, tw_adj,
377 ISIS_ADJ_LEVEL2);
d62a17ae 378 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
379 /* (7) down - wrong system */
380 isis_adj_state_change(adj,
381 ISIS_ADJ_DOWN,
382 "Wrong System");
383 } else if (adj->adj_usage
384 == ISIS_ADJ_LEVEL1AND2) {
0c1bd758 385 if (iih->circ_type == IS_LEVEL_2) {
d62a17ae 386 /* (7) down - wrong system */
387 isis_adj_state_change(
388 adj, ISIS_ADJ_DOWN,
389 "Wrong System");
390 } else {
391 /* (7) down - area mismatch */
392 isis_adj_state_change(
393 adj, ISIS_ADJ_DOWN,
394 "Area Mismatch");
395 }
d62a17ae 396 }
397 break;
398 }
f390d2c7 399 }
d62a17ae 400 } else {
401 /* down - area mismatch */
402 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "Area Mismatch");
f390d2c7 403 }
d8fba7d9 404
0c1bd758 405 if (adj->adj_state == ISIS_ADJ_UP && changed) {
d62a17ae 406 lsp_regenerate_schedule(adj->circuit->area,
407 isis_adj_usage2levels(adj->adj_usage),
408 0);
409 }
eb5d44eb 410
d62a17ae 411 /* 8.2.5.2 c) if the action was up - comparing circuit IDs */
412 /* FIXME - Missing parts */
413
414 /* some of my own understanding of the ISO, why the heck does
415 * it not say what should I change the system_type to...
416 */
417 switch (adj->adj_usage) {
418 case ISIS_ADJ_LEVEL1:
419 adj->sys_type = ISIS_SYSTYPE_L1_IS;
420 break;
421 case ISIS_ADJ_LEVEL2:
422 adj->sys_type = ISIS_SYSTYPE_L2_IS;
423 break;
424 case ISIS_ADJ_LEVEL1AND2:
425 adj->sys_type = ISIS_SYSTYPE_L2_IS;
426 break;
427 case ISIS_ADJ_NONE:
428 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
429 break;
430 }
eb5d44eb 431
d62a17ae 432 if (isis->debugs & DEBUG_ADJ_PACKETS) {
433 zlog_debug(
434 "ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
c59f88c8 435 " cir id %hhu, length %" PRIu16,
0c1bd758
CF
436 iih->circuit->area->area_tag,
437 iih->circuit->interface->name,
438 circuit_t2string(iih->circuit->is_type),
439 iih->circuit->circuit_id, iih->pdu_len);
d62a17ae 440 }
eb5d44eb 441
0c1bd758 442 return ISIS_OK;
eb5d44eb 443}
444
0c1bd758 445static int process_lan_hello(struct iih_info *iih)
eb5d44eb 446{
d62a17ae 447 struct isis_adjacency *adj;
0c1bd758
CF
448 adj = isis_adj_lookup(iih->sys_id,
449 iih->circuit->u.bc.adjdb[iih->level - 1]);
450 if ((adj == NULL) || (memcmp(adj->snpa, iih->ssnpa, ETH_ALEN))
451 || (adj->level != iih->level)) {
452 if (!adj) {
453 /* Do as in 8.4.2.5 */
454 adj = isis_new_adj(iih->sys_id, iih->ssnpa, iih->level,
455 iih->circuit);
456 } else {
457 if (iih->ssnpa) {
458 memcpy(adj->snpa, iih->ssnpa, 6);
459 } else {
460 memset(adj->snpa, ' ', 6);
461 }
462 adj->level = iih->level;
463 }
464 isis_adj_state_change(adj, ISIS_ADJ_INITIALIZING, NULL);
d62a17ae 465
0c1bd758
CF
466 if (iih->level == IS_LEVEL_1)
467 adj->sys_type = ISIS_SYSTYPE_L1_IS;
468 else
469 adj->sys_type = ISIS_SYSTYPE_L2_IS;
470 list_delete_all_node(
471 iih->circuit->u.bc.lan_neighs[iih->level - 1]);
472 isis_adj_build_neigh_list(
473 iih->circuit->u.bc.adjdb[iih->level - 1],
474 iih->circuit->u.bc.lan_neighs[iih->level - 1]);
475 }
476
477 if (adj->dis_record[iih->level - 1].dis == ISIS_IS_DIS) {
d7c0a89a
QY
478 uint8_t *dis = (iih->level == 1)
479 ? iih->circuit->u.bc.l1_desig_is
480 : iih->circuit->u.bc.l2_desig_is;
0c1bd758
CF
481
482 if (memcmp(dis, iih->dis, ISIS_SYS_ID_LEN + 1)) {
483 thread_add_event(master, isis_event_dis_status_change,
484 iih->circuit, 0, NULL);
485 memcpy(dis, iih->dis, ISIS_SYS_ID_LEN + 1);
486 }
d62a17ae 487 }
3f045a08 488
0c1bd758
CF
489 adj->circuit_t = iih->circ_type;
490 adj->hold_time = iih->holdtime;
491 adj->last_upd = time(NULL);
492 adj->prio[iih->level - 1] = iih->priority;
493 memcpy(adj->lanid, iih->dis, ISIS_SYS_ID_LEN + 1);
3f045a08 494
0c1bd758
CF
495 bool changed;
496 isis_tlvs_to_adj(iih->tlvs, adj, &changed);
497 changed |= tlvs_to_adj_mt_set(iih->tlvs, iih->v4_usable, iih->v6_usable,
498 adj);
499
500 /* lets take care of the expiry */
501 THREAD_TIMER_OFF(adj->t_expire);
502 thread_add_timer(master, isis_adj_expire, adj, (long)adj->hold_time,
503 &adj->t_expire);
504
505 /*
506 * If the snpa for this circuit is found from LAN Neighbours TLV
507 * we have two-way communication -> adjacency can be put to state "up"
508 */
509 bool own_snpa_found =
510 isis_tlvs_own_snpa_found(iih->tlvs, iih->circuit->u.bc.snpa);
511
512 if (adj->adj_state != ISIS_ADJ_UP) {
513 if (own_snpa_found) {
514 isis_adj_state_change(
515 adj, ISIS_ADJ_UP,
516 "own SNPA found in LAN Neighbours TLV");
517 }
518 } else {
519 if (!own_snpa_found) {
520 isis_adj_state_change(
521 adj, ISIS_ADJ_INITIALIZING,
522 "own SNPA not found in LAN Neighbours TLV");
523 }
d62a17ae 524 }
eb5d44eb 525
0c1bd758
CF
526 if (adj->adj_state == ISIS_ADJ_UP && changed)
527 lsp_regenerate_schedule(adj->circuit->area, iih->level, 0);
528
529 if (isis->debugs & DEBUG_ADJ_PACKETS) {
d62a17ae 530 zlog_debug(
0c1bd758
CF
531 "ISIS-Adj (%s): Rcvd L%d LAN IIH from %s on %s, cirType %s, cirID %u, length %zd",
532 iih->circuit->area->area_tag, iih->level,
533 snpa_print(iih->ssnpa), iih->circuit->interface->name,
534 circuit_t2string(iih->circuit->is_type),
535 iih->circuit->circuit_id,
536 stream_get_endp(iih->circuit->rcv_stream));
d62a17ae 537 }
0c1bd758
CF
538 return ISIS_OK;
539}
eb5d44eb 540
17c9dcd5
CF
541static int pdu_len_validate(uint16_t pdu_len, struct isis_circuit *circuit)
542{
543 if (pdu_len < stream_get_getp(circuit->rcv_stream)
544 || pdu_len > ISO_MTU(circuit)
545 || pdu_len > stream_get_endp(circuit->rcv_stream))
546 return 1;
547
548 if (pdu_len < stream_get_endp(circuit->rcv_stream))
549 stream_set_endp(circuit->rcv_stream, pdu_len);
550 return 0;
551}
552
0c1bd758 553static int process_hello(uint8_t pdu_type, struct isis_circuit *circuit,
d7c0a89a 554 uint8_t *ssnpa)
0c1bd758
CF
555{
556 bool p2p_hello = (pdu_type == P2P_HELLO);
557 int level = p2p_hello ? 0
558 : (pdu_type == L1_LAN_HELLO) ? ISIS_LEVEL1
559 : ISIS_LEVEL2;
560 const char *pdu_name =
561 p2p_hello
562 ? "P2P IIH"
563 : (level == ISIS_LEVEL1) ? "L1 LAN IIH" : "L2 LAN IIH";
564
565 if (isis->debugs & DEBUG_ADJ_PACKETS) {
566 zlog_debug("ISIS-Adj (%s): Rcvd %s on %s, cirType %s, cirID %u",
567 circuit->area->area_tag, pdu_name,
568 circuit->interface->name,
569 circuit_t2string(circuit->is_type),
570 circuit->circuit_id);
571 if (isis->debugs & DEBUG_PACKET_DUMP)
572 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
573 stream_get_endp(circuit->rcv_stream));
574 }
575
576 if (p2p_hello) {
577 if (circuit->circ_type != CIRCUIT_T_P2P) {
578 zlog_warn("p2p hello on non p2p circuit");
579 return ISIS_WARNING;
580 }
581 } else {
582 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
583 zlog_warn("lan hello on non broadcast circuit");
584 return ISIS_WARNING;
585 }
586
587 if (circuit->ext_domain) {
d62a17ae 588 zlog_debug(
0c1bd758
CF
589 "level %d LAN Hello received over circuit with externalDomain = true",
590 level);
591 return ISIS_WARNING;
592 }
593
af8ac8f9 594 if (!(circuit->is_type & level)) {
0c1bd758
CF
595 if (isis->debugs & DEBUG_ADJ_PACKETS) {
596 zlog_debug(
597 "ISIS-Adj (%s): Interface level mismatch, %s",
598 circuit->area->area_tag,
599 circuit->interface->name);
600 }
601 return ISIS_WARNING;
d62a17ae 602 }
f390d2c7 603 }
eb5d44eb 604
0c1bd758
CF
605 struct iih_info iih = {
606 .circuit = circuit, .ssnpa = ssnpa, .level = level};
607
608 /* Generic IIH Header */
609 iih.circ_type = stream_getc(circuit->rcv_stream) & 0x03;
610 stream_get(iih.sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
611 iih.holdtime = stream_getw(circuit->rcv_stream);
612 iih.pdu_len = stream_getw(circuit->rcv_stream);
613
614 if (p2p_hello) {
615 iih.circuit_id = stream_getc(circuit->rcv_stream);
616 } else {
617 iih.priority = stream_getc(circuit->rcv_stream);
618 stream_get(iih.dis, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
f390d2c7 619 }
0c1bd758 620
17c9dcd5 621 if (pdu_len_validate(iih.pdu_len, circuit)) {
d62a17ae 622 zlog_warn(
0c1bd758
CF
623 "ISIS-Adj (%s): Rcvd %s from (%s) with invalid pdu length %" PRIu16,
624 circuit->area->area_tag, pdu_name,
625 circuit->interface->name, iih.pdu_len);
d62a17ae 626 return ISIS_WARNING;
627 }
3f045a08 628
0c1bd758 629 if (!p2p_hello && !(level & iih.circ_type)) {
1a7ecb96 630 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
631 "Level %d LAN Hello with Circuit Type %d", level,
632 iih.circ_type);
d62a17ae 633 return ISIS_ERROR;
634 }
3f045a08 635
0c1bd758
CF
636 const char *error_log;
637 int retval = ISIS_WARNING;
d62a17ae 638
0c1bd758
CF
639 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
640 circuit->rcv_stream, &iih.tlvs, &error_log)) {
641 zlog_warn("isis_unpack_tlvs() failed: %s", error_log);
d62a17ae 642 goto out;
643 }
eb5d44eb 644
0c1bd758
CF
645 if (!iih.tlvs->area_addresses.count) {
646 zlog_warn("No Area addresses TLV in %s", pdu_name);
d62a17ae 647 goto out;
648 }
f390d2c7 649
0c1bd758
CF
650 if (!iih.tlvs->protocols_supported.count) {
651 zlog_warn("No supported protocols TLV in %s", pdu_name);
d62a17ae 652 goto out;
653 }
b72f345d 654
0c1bd758 655 if (!isis_tlvs_auth_is_valid(iih.tlvs, &circuit->passwd,
af8ac8f9 656 circuit->rcv_stream, false)) {
0c1bd758
CF
657 isis_event_auth_failure(circuit->area->area_tag,
658 "IIH authentication failure",
659 iih.sys_id);
660 goto out;
d62a17ae 661 }
eb5d44eb 662
0c1bd758
CF
663 if (!memcmp(iih.sys_id, isis->sysid, ISIS_SYS_ID_LEN)) {
664 zlog_warn(
665 "ISIS-Adj (%s): Received IIH with own sysid - discard",
666 circuit->area->area_tag);
667 goto out;
d62a17ae 668 }
19f78ceb 669
0c1bd758
CF
670 if (!p2p_hello
671 && (listcount(circuit->area->area_addrs) == 0
672 || (level == ISIS_LEVEL1
673 && !isis_tlvs_area_addresses_match(
674 iih.tlvs, circuit->area->area_addrs)))) {
d62a17ae 675 if (isis->debugs & DEBUG_ADJ_PACKETS) {
676 zlog_debug(
677 "ISIS-Adj (%s): Area mismatch, level %d IIH on %s",
678 circuit->area->area_tag, level,
679 circuit->interface->name);
680 }
d62a17ae 681 goto out;
f390d2c7 682 }
eb5d44eb 683
41415888 684 iih.v4_usable = (fabricd_ip_addrs(circuit)
0c1bd758 685 && iih.tlvs->ipv4_address.count);
d62a17ae 686
0c1bd758
CF
687 iih.v6_usable = (circuit->ipv6_link && listcount(circuit->ipv6_link)
688 && iih.tlvs->ipv6_address.count);
28a8cfcb 689
c9b0e434
CF
690 if (!iih.v4_usable && !iih.v6_usable) {
691 if (isis->debugs & DEBUG_ADJ_PACKETS) {
692 zlog_warn(
693 "ISIS-Adj (%s): Neither IPv4 nor IPv6 considered usable. Ignoring IIH",
694 circuit->area->area_tag);
695 }
696
d62a17ae 697 goto out;
c9b0e434 698 }
d8fba7d9 699
0c1bd758 700 retval = p2p_hello ? process_p2p_hello(&iih) : process_lan_hello(&iih);
f390d2c7 701out:
0c1bd758 702 isis_free_tlvs(iih.tlvs);
eb5d44eb 703
d62a17ae 704 return retval;
eb5d44eb 705}
706
a6b60da9
CF
707static void lsp_flood_or_update(struct isis_lsp *lsp,
708 struct isis_circuit *circuit,
709 bool circuit_scoped)
710{
711 if (!circuit_scoped) {
712 lsp_flood(lsp, circuit);
713 } else {
714 fabricd_update_lsp_no_flood(lsp, circuit);
715 }
716}
717
eb5d44eb 718/*
719 * Process Level 1/2 Link State
720 * ISO - 10589
721 * Section 7.3.15.1 - Action on receipt of a link state PDU
f390d2c7 722 */
af8ac8f9 723static int process_lsp(uint8_t pdu_type, struct isis_circuit *circuit,
9b39405f 724 const uint8_t *ssnpa, uint8_t max_area_addrs)
eb5d44eb 725{
1cbd5b37
CF
726 int level;
727 bool circuit_scoped;
728
729 if (pdu_type == FS_LINK_STATE) {
730 if (!fabricd)
731 return ISIS_ERROR;
732 if (max_area_addrs != L2_CIRCUIT_FLOODING_SCOPE)
733 return ISIS_ERROR;
734 level = ISIS_LEVEL2;
735 circuit_scoped = true;
736
737 /* The stream is used verbatim for sending out new LSPDUs.
738 * So make sure we store it as an L2 LSPDU internally.
739 * (compare for the reverse in `send_lsp`) */
740 stream_putc_at(circuit->rcv_stream, 4, L2_LINK_STATE);
741 stream_putc_at(circuit->rcv_stream, 7, 0);
742 } else {
743 if (pdu_type == L1_LINK_STATE)
744 level = ISIS_LEVEL1;
745 else
746 level = ISIS_LEVEL2;
747 circuit_scoped = false;
748 }
d62a17ae 749
750 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
751 zlog_debug(
1cbd5b37
CF
752 "ISIS-Upd (%s): Rcvd %sL%d LSP on %s, cirType %s, cirID %u",
753 circuit->area->area_tag,
754 circuit_scoped ? "Circuit scoped " : "", level,
d62a17ae 755 circuit->interface->name,
756 circuit_t2string(circuit->is_type),
757 circuit->circuit_id);
758 if (isis->debugs & DEBUG_PACKET_DUMP)
759 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
760 stream_get_endp(circuit->rcv_stream));
761 }
f390d2c7 762
3e300703 763 struct isis_lsp_hdr hdr = {};
eb5d44eb 764
af8ac8f9
CF
765 hdr.pdu_len = stream_getw(circuit->rcv_stream);
766 hdr.rem_lifetime = stream_getw(circuit->rcv_stream);
767 stream_get(hdr.lsp_id, circuit->rcv_stream, sizeof(hdr.lsp_id));
768 hdr.seqno = stream_getl(circuit->rcv_stream);
769 hdr.checksum = stream_getw(circuit->rcv_stream);
770 hdr.lsp_bits = stream_getc(circuit->rcv_stream);
eb5d44eb 771
af8ac8f9
CF
772 if (pdu_len_validate(hdr.pdu_len, circuit)) {
773 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP length %" PRIu16,
774 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
775 hdr.pdu_len);
d62a17ae 776 return ISIS_WARNING;
f390d2c7 777 }
d62a17ae 778
d62a17ae 779 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
af8ac8f9
CF
780 zlog_debug("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08" PRIx32
781 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
782 "s, len %" PRIu16 ", on %s",
783 circuit->area->area_tag, level,
784 rawlspid_print(hdr.lsp_id), hdr.seqno, hdr.checksum,
785 hdr.rem_lifetime, hdr.pdu_len,
786 circuit->interface->name);
f390d2c7 787 }
d62a17ae 788
789 /* lsp is_type check */
af8ac8f9
CF
790 if ((hdr.lsp_bits & IS_LEVEL_1) != IS_LEVEL_1) {
791 zlog_debug(
792 "ISIS-Upd (%s): LSP %s invalid LSP is type 0x%" PRIx8,
793 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
794 hdr.lsp_bits & IS_LEVEL_1_AND_2);
d62a17ae 795 /* continue as per RFC1122 Be liberal in what you accept, and
796 * conservative in what you send */
f390d2c7 797 }
d62a17ae 798
799 /* Checksum sanity check - FIXME: move to correct place */
800 /* 12 = sysid+pdu+remtime */
af8ac8f9
CF
801 if (iso_csum_verify(STREAM_DATA(circuit->rcv_stream) + 12,
802 hdr.pdu_len - 12, hdr.checksum, 12)) {
803 zlog_debug(
804 "ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04" PRIx16,
805 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
806 hdr.checksum);
d62a17ae 807 return ISIS_WARNING;
f390d2c7 808 }
3f045a08 809
d62a17ae 810 /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
811 if (circuit->ext_domain) {
812 zlog_debug(
813 "ISIS-Upd (%s): LSP %s received at level %d over circuit with "
814 "externalDomain = true",
af8ac8f9 815 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 816 level);
d62a17ae 817 return ISIS_WARNING;
818 }
eb5d44eb 819
d62a17ae 820 /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 821 if (!(circuit->is_type & level)) {
d62a17ae 822 zlog_debug(
823 "ISIS-Upd (%s): LSP %s received at level %d over circuit of"
824 " type %s",
af8ac8f9 825 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 826 level, circuit_t2string(circuit->is_type));
d62a17ae 827 return ISIS_WARNING;
828 }
0250758d 829
af8ac8f9
CF
830 struct isis_tlvs *tlvs = NULL;
831 int retval = ISIS_WARNING;
832 const char *error_log;
833
834 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
835 circuit->rcv_stream, &tlvs, &error_log)) {
836 zlog_warn("Something went wrong unpacking the LSP: %s",
837 error_log);
838 goto out;
839 }
840
d62a17ae 841 /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
842
843 /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use
844 * 3 */
845
846 /* 7.3.15.1 a) 7 - password check */
af8ac8f9
CF
847 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
848 ? &circuit->area->area_passwd
849 : &circuit->area->domain_passwd;
850 if (!isis_tlvs_auth_is_valid(tlvs, passwd, circuit->rcv_stream, true)) {
851 isis_event_auth_failure(circuit->area->area_tag,
852 "LSP authentication failure",
853 hdr.lsp_id);
854 goto out;
f390d2c7 855 }
af8ac8f9 856
d62a17ae 857 /* Find the LSP in our database and compare it to this Link State header
858 */
af8ac8f9
CF
859 struct isis_lsp *lsp =
860 lsp_search(hdr.lsp_id, circuit->area->lspdb[level - 1]);
861 int comp = 0;
d62a17ae 862 if (lsp)
af8ac8f9
CF
863 comp = lsp_compare(circuit->area->area_tag, lsp, hdr.seqno,
864 hdr.checksum, hdr.rem_lifetime);
d62a17ae 865 if (lsp && (lsp->own_lsp))
866 goto dontcheckadj;
867
868 /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same
869 * level */
870 /* for broadcast circuits, snpa should be compared */
871
872 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
af8ac8f9
CF
873 if (!isis_adj_lookup_snpa(ssnpa,
874 circuit->u.bc.adjdb[level - 1])) {
875 zlog_debug("(%s): DS ======= LSP %s, seq 0x%08" PRIx32
876 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
877 "s on %s",
878 circuit->area->area_tag,
879 rawlspid_print(hdr.lsp_id), hdr.seqno,
880 hdr.checksum, hdr.rem_lifetime,
881 circuit->interface->name);
882 goto out; /* Silently discard */
d62a17ae 883 }
884 }
885 /* for non broadcast, we just need to find same level adj */
886 else {
887 /* If no adj, or no sharing of level */
888 if (!circuit->u.p2p.neighbor) {
af8ac8f9
CF
889 retval = ISIS_OK;
890 goto out;
d62a17ae 891 } else {
892 if (((level == IS_LEVEL_1)
893 && (circuit->u.p2p.neighbor->adj_usage
894 == ISIS_ADJ_LEVEL2))
895 || ((level == IS_LEVEL_2)
896 && (circuit->u.p2p.neighbor->adj_usage
897 == ISIS_ADJ_LEVEL1)))
af8ac8f9 898 goto out;
f390d2c7 899 }
f390d2c7 900 }
f390d2c7 901
af8ac8f9
CF
902 bool lsp_confusion;
903
d62a17ae 904dontcheckadj:
905 /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
906
907 /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
908
909 /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do
910 * it */
911
912 /* 7.3.16.2 - If this is an LSP from another IS with identical seq_num
913 * but
914 * wrong checksum, initiate a purge. */
af8ac8f9 915 if (lsp && (lsp->hdr.seqno == hdr.seqno)
2c92bee4
CF
916 && (lsp->hdr.checksum != hdr.checksum)
917 && hdr.rem_lifetime) {
af8ac8f9
CF
918 zlog_warn("ISIS-Upd (%s): LSP %s seq 0x%08" PRIx32
919 " with confused checksum received.",
920 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
921 hdr.seqno);
922 hdr.rem_lifetime = 0;
923 lsp_confusion = true;
d62a17ae 924 } else
af8ac8f9 925 lsp_confusion = false;
d62a17ae 926
927 /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
af8ac8f9 928 if (hdr.rem_lifetime == 0) {
d62a17ae 929 if (!lsp) {
930 /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't
931 * save */
932 /* only needed on explicit update, eg - p2p */
933 if (circuit->circ_type == CIRCUIT_T_P2P)
af8ac8f9
CF
934 ack_lsp(&hdr, circuit, level);
935 goto out; /* FIXME: do we need a purge? */
d62a17ae 936 } else {
af8ac8f9 937 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN)) {
d62a17ae 938 /* LSP by some other system -> do 7.3.16.4 b) */
939 /* 7.3.16.4 b) 1) */
940 if (comp == LSP_NEWER) {
af8ac8f9
CF
941 lsp_update(lsp, &hdr, tlvs,
942 circuit->rcv_stream,
164066e4
CF
943 circuit->area, level,
944 lsp_confusion);
af8ac8f9 945 tlvs = NULL;
d62a17ae 946 /* ii */
a6b60da9
CF
947 lsp_flood_or_update(lsp, NULL,
948 circuit_scoped);
d62a17ae 949 /* v */
950 ISIS_FLAGS_CLEAR_ALL(
996c9314
LB
951 lsp->SSNflags); /* FIXME:
952 OTHER
953 than c
954 */
d62a17ae 955
956 /* For the case of lsp confusion, flood
957 * the purge back to its
958 * originator so that it can react.
959 * Otherwise, don't reflood
960 * through incoming circuit as usual */
961 if (!lsp_confusion) {
9b39405f
CF
962 isis_tx_queue_del(
963 circuit->tx_queue,
964 lsp);
965
d62a17ae 966 /* iv */
967 if (circuit->circ_type
968 != CIRCUIT_T_BROADCAST)
969 ISIS_SET_FLAG(
970 lsp->SSNflags,
971 circuit);
972 }
973 } /* 7.3.16.4 b) 2) */
974 else if (comp == LSP_EQUAL) {
975 /* i */
9b39405f
CF
976 isis_tx_queue_del(circuit->tx_queue,
977 lsp);
d62a17ae 978 /* ii */
979 if (circuit->circ_type
980 != CIRCUIT_T_BROADCAST)
981 ISIS_SET_FLAG(lsp->SSNflags,
982 circuit);
983 } /* 7.3.16.4 b) 3) */
984 else {
9b39405f
CF
985 isis_tx_queue_add(circuit->tx_queue,
986 lsp, TX_LSP_NORMAL);
d62a17ae 987 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
988 }
af8ac8f9 989 } else if (lsp->hdr.rem_lifetime != 0) {
d62a17ae 990 /* our own LSP -> 7.3.16.4 c) */
991 if (comp == LSP_NEWER) {
af8ac8f9 992 lsp_inc_seqno(lsp, hdr.seqno);
a6b60da9 993 lsp_flood_or_update(lsp, NULL, circuit_scoped);
d62a17ae 994 } else {
9b39405f
CF
995 isis_tx_queue_add(circuit->tx_queue,
996 lsp, TX_LSP_NORMAL);
d62a17ae 997 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
998 }
999 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1000 zlog_debug(
af8ac8f9 1001 "ISIS-Upd (%s): (1) re-originating LSP %s new seq 0x%08" PRIx32,
d62a17ae 1002 circuit->area->area_tag,
af8ac8f9
CF
1003 rawlspid_print(hdr.lsp_id),
1004 lsp->hdr.seqno);
d62a17ae 1005 }
f390d2c7 1006 }
af8ac8f9 1007 goto out;
f390d2c7 1008 }
d62a17ae 1009 /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
1010 * purge */
af8ac8f9 1011 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0) {
d62a17ae 1012 if (!lsp) {
1013 /* 7.3.16.4: initiate a purge */
af8ac8f9
CF
1014 lsp_purge_non_exist(level, &hdr, circuit->area);
1015 retval = ISIS_OK;
1016 goto out;
d62a17ae 1017 }
1018 /* 7.3.15.1 d) - If this is our own lsp and we have it */
1019
1020 /* In 7.3.16.1, If an Intermediate system R somewhere in the
1021 * domain
1022 * has information that the current sequence number for source S
1023 * is
1024 * "greater" than that held by S, ... */
1025
562d7ef7 1026 if (comp == LSP_NEWER) {
d62a17ae 1027 /* 7.3.16.1 */
af8ac8f9 1028 lsp_inc_seqno(lsp, hdr.seqno);
562d7ef7 1029 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
d62a17ae 1030 zlog_debug(
af8ac8f9 1031 "ISIS-Upd (%s): (2) re-originating LSP %s new seq 0x%08" PRIx32,
d62a17ae 1032 circuit->area->area_tag,
af8ac8f9
CF
1033 rawlspid_print(hdr.lsp_id),
1034 lsp->hdr.seqno);
562d7ef7
CF
1035 }
1036 lsp_flood(lsp, NULL);
1037 } else if (comp == LSP_EQUAL) {
1038 isis_tx_queue_del(circuit->tx_queue, lsp);
1039 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1040 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1041 } else {
1042 isis_tx_queue_add(circuit->tx_queue, lsp,
1043 TX_LSP_NORMAL);
1044 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
d62a17ae 1045 }
d62a17ae 1046 } else {
1047 /* 7.3.15.1 e) - This lsp originated on another system */
1048
1049 /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db
1050 */
1051 if ((!lsp || comp == LSP_NEWER)) {
1052 /*
1053 * If this lsp is a frag, need to see if we have zero
1054 * lsp present
1055 */
af8ac8f9
CF
1056 struct isis_lsp *lsp0 = NULL;
1057 if (LSP_FRAGMENT(hdr.lsp_id) != 0) {
1058 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1059 memcpy(lspid, hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
d62a17ae 1060 LSP_FRAGMENT(lspid) = 0;
1061 lsp0 = lsp_search(
1062 lspid, circuit->area->lspdb[level - 1]);
1063 if (!lsp0) {
1064 zlog_debug(
1065 "Got lsp frag, while zero lsp not in database");
df0ba689 1066 goto out;
d62a17ae 1067 }
1068 }
1069 /* i */
1070 if (!lsp) {
af8ac8f9
CF
1071 lsp = lsp_new_from_recv(
1072 &hdr, tlvs, circuit->rcv_stream, lsp0,
d62a17ae 1073 circuit->area, level);
af8ac8f9 1074 tlvs = NULL;
d62a17ae 1075 lsp_insert(lsp,
1076 circuit->area->lspdb[level - 1]);
1077 } else /* exists, so we overwrite */
1078 {
af8ac8f9 1079 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1080 circuit->area, level, false);
af8ac8f9 1081 tlvs = NULL;
d62a17ae 1082 }
a6b60da9 1083 lsp_flood_or_update(lsp, circuit, circuit_scoped);
d62a17ae 1084
1085 /* iv */
1086 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1087 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1088 /* FIXME: v) */
1089 }
1090 /* 7.3.15.1 e) 2) LSP equal to the one in db */
1091 else if (comp == LSP_EQUAL) {
9b39405f 1092 isis_tx_queue_del(circuit->tx_queue, lsp);
af8ac8f9 1093 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1094 circuit->area, level, false);
af8ac8f9 1095 tlvs = NULL;
d62a17ae 1096 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1097 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1098 }
1099 /* 7.3.15.1 e) 3) LSP older than the one in db */
1100 else {
9b39405f
CF
1101 isis_tx_queue_add(circuit->tx_queue, lsp,
1102 TX_LSP_NORMAL);
d62a17ae 1103 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1104 }
f390d2c7 1105 }
af8ac8f9
CF
1106
1107 retval = ISIS_OK;
1108
1109out:
e923107c 1110 fabricd_trigger_csnp(circuit->area, circuit_scoped);
df0ba689 1111
af8ac8f9 1112 isis_free_tlvs(tlvs);
d62a17ae 1113 return retval;
eb5d44eb 1114}
1115
1116/*
1117 * Process Sequence Numbers
1118 * ISO - 10589
1119 * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
1120 */
1121
17c9dcd5 1122static int process_snp(uint8_t pdu_type, struct isis_circuit *circuit,
d7c0a89a 1123 const uint8_t *ssnpa)
eb5d44eb 1124{
17c9dcd5
CF
1125 bool is_csnp = (pdu_type == L1_COMPLETE_SEQ_NUM
1126 || pdu_type == L2_COMPLETE_SEQ_NUM);
1127 char typechar = is_csnp ? 'C' : 'P';
1128 int level = (pdu_type == L1_COMPLETE_SEQ_NUM
1129 || pdu_type == L1_PARTIAL_SEQ_NUM)
1130 ? ISIS_LEVEL1
1131 : ISIS_LEVEL2;
1132
1133 uint16_t pdu_len = stream_getw(circuit->rcv_stream);
1134 uint8_t rem_sys_id[ISIS_SYS_ID_LEN];
1135 stream_get(rem_sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
1136 stream_forward_getp(circuit->rcv_stream, 1); /* Circuit ID - unused */
1137
3e300703
DL
1138 uint8_t start_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1139 uint8_t stop_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
17c9dcd5
CF
1140
1141 if (is_csnp) {
1142 stream_get(start_lsp_id, circuit->rcv_stream,
1143 ISIS_SYS_ID_LEN + 2);
1144 stream_get(stop_lsp_id, circuit->rcv_stream,
1145 ISIS_SYS_ID_LEN + 2);
1146 }
1147
1148 if (pdu_len_validate(pdu_len, circuit)) {
1149 zlog_warn("Received a CSNP with bogus length %d", pdu_len);
1150 return ISIS_WARNING;
f390d2c7 1151 }
eb5d44eb 1152
17c9dcd5
CF
1153 if (isis->debugs & DEBUG_SNP_PACKETS) {
1154 zlog_debug(
1155 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, cirType %s, cirID %u",
1156 circuit->area->area_tag, level, typechar,
1157 circuit->interface->name,
1158 circuit_t2string(circuit->is_type),
1159 circuit->circuit_id);
1160 if (isis->debugs & DEBUG_PACKET_DUMP)
1161 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
1162 stream_get_endp(circuit->rcv_stream));
1163 }
eb5d44eb 1164
d62a17ae 1165 /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
1166 if (circuit->ext_domain) {
f390d2c7 1167
d62a17ae 1168 zlog_debug(
1169 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1170 "skipping: circuit externalDomain = true",
1171 circuit->area->area_tag, level, typechar,
1172 circuit->interface->name);
f390d2c7 1173
d62a17ae 1174 return ISIS_OK;
1175 }
eb5d44eb 1176
d62a17ae 1177 /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 1178 if (!(circuit->is_type & level)) {
d62a17ae 1179 zlog_debug(
1180 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1181 "skipping: circuit type %s does not match level %d",
1182 circuit->area->area_tag, level, typechar,
1183 circuit->interface->name,
1184 circuit_t2string(circuit->is_type), level);
eb5d44eb 1185
d62a17ae 1186 return ISIS_OK;
f390d2c7 1187 }
eb5d44eb 1188
d62a17ae 1189 /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
17c9dcd5
CF
1190 if (!is_csnp && (circuit->circ_type == CIRCUIT_T_BROADCAST)
1191 && !circuit->u.bc.is_dr[level - 1]) {
d62a17ae 1192 zlog_debug(
1193 "ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
1194 "skipping: we are not the DIS",
1195 circuit->area->area_tag, level, typechar,
1196 snpa_print(ssnpa), circuit->interface->name);
1197
1198 return ISIS_OK;
1199 }
eb5d44eb 1200
d62a17ae 1201 /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked
1202 */
1203
1204 /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use
1205 * 3
1206 * - already checked */
1207
1208 /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same
1209 * level */
1210 /* for broadcast circuits, snpa should be compared */
1211 /* FIXME : Do we need to check SNPA? */
1212 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
17c9dcd5
CF
1213 if (!isis_adj_lookup(rem_sys_id,
1214 circuit->u.bc.adjdb[level - 1]))
d62a17ae 1215 return ISIS_OK; /* Silently discard */
1216 } else {
8e6fb83b 1217 if (!fabricd && !circuit->u.p2p.neighbor) {
d62a17ae 1218 zlog_warn("no p2p neighbor on circuit %s",
1219 circuit->interface->name);
1220 return ISIS_OK; /* Silently discard */
1221 }
1222 }
eb5d44eb 1223
17c9dcd5
CF
1224 struct isis_tlvs *tlvs;
1225 int retval = ISIS_WARNING;
1226 const char *error_log;
eb5d44eb 1227
17c9dcd5
CF
1228 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
1229 circuit->rcv_stream, &tlvs, &error_log)) {
1230 zlog_warn("Something went wrong unpacking the SNP: %s",
1231 error_log);
1232 goto out;
f390d2c7 1233 }
eb5d44eb 1234
17c9dcd5
CF
1235 struct isis_passwd *passwd = (level == IS_LEVEL_1)
1236 ? &circuit->area->area_passwd
1237 : &circuit->area->domain_passwd;
1238 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV)
af8ac8f9
CF
1239 && !isis_tlvs_auth_is_valid(tlvs, passwd, circuit->rcv_stream,
1240 false)) {
17c9dcd5
CF
1241 isis_event_auth_failure(circuit->area->area_tag,
1242 "SNP authentication failure",
1243 rem_sys_id);
1244 goto out;
d62a17ae 1245 }
eb5d44eb 1246
17c9dcd5
CF
1247 struct isis_lsp_entry *entry_head =
1248 (struct isis_lsp_entry *)tlvs->lsp_entries.head;
1249
d62a17ae 1250 /* debug isis snp-packets */
1251 if (isis->debugs & DEBUG_SNP_PACKETS) {
1252 zlog_debug("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
1253 circuit->area->area_tag, level, typechar,
1254 snpa_print(ssnpa), circuit->interface->name);
17c9dcd5
CF
1255 for (struct isis_lsp_entry *entry = entry_head; entry;
1256 entry = entry->next) {
1257 zlog_debug(
1258 "ISIS-Snp (%s): %cSNP entry %s, seq 0x%08" PRIx32
98c5bc15 1259 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
17c9dcd5
CF
1260 circuit->area->area_tag, typechar,
1261 rawlspid_print(entry->id), entry->seqno,
1262 entry->checksum, entry->rem_lifetime);
f390d2c7 1263 }
f390d2c7 1264 }
3f045a08 1265
df0ba689
CF
1266 bool resync_needed = false;
1267
d62a17ae 1268 /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
17c9dcd5
CF
1269 for (struct isis_lsp_entry *entry = entry_head; entry;
1270 entry = entry->next) {
1271 struct isis_lsp *lsp =
1272 lsp_search(entry->id, circuit->area->lspdb[level - 1]);
1273 bool own_lsp = !memcmp(entry->id, isis->sysid, ISIS_SYS_ID_LEN);
1274 if (lsp) {
1275 /* 7.3.15.2 b) 1) is this LSP newer */
1276 int cmp = lsp_compare(circuit->area->area_tag, lsp,
1277 entry->seqno, entry->checksum,
1278 entry->rem_lifetime);
1279 /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
1280 if (cmp == LSP_EQUAL) {
1281 /* if (circuit->circ_type !=
1282 * CIRCUIT_T_BROADCAST) */
9b39405f 1283 isis_tx_queue_del(circuit->tx_queue, lsp);
17c9dcd5
CF
1284 }
1285 /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM
1286 */
1287 else if (cmp == LSP_OLDER) {
1288 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
9b39405f
CF
1289 isis_tx_queue_add(circuit->tx_queue, lsp,
1290 TX_LSP_NORMAL);
17c9dcd5
CF
1291 }
1292 /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM
1293 on p2p */
1294 else {
1295 if (own_lsp) {
af8ac8f9 1296 lsp_inc_seqno(lsp, entry->seqno);
9b39405f
CF
1297 isis_tx_queue_add(circuit->tx_queue, lsp,
1298 TX_LSP_NORMAL);
17c9dcd5
CF
1299 } else {
1300 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1301 /* if (circuit->circ_type !=
1302 * CIRCUIT_T_BROADCAST) */
9b39405f 1303 isis_tx_queue_del(circuit->tx_queue, lsp);
df0ba689 1304 resync_needed = true;
d62a17ae 1305 }
17c9dcd5
CF
1306 }
1307 } else {
1308 /* 7.3.15.2 b) 5) if it was not found, and all of those
1309 * are not 0,
1310 * insert it and set SSN on it */
1311 if (entry->rem_lifetime && entry->checksum
1312 && entry->seqno && memcmp(entry->id, isis->sysid,
1313 ISIS_SYS_ID_LEN)) {
8f5dbe18
CF
1314 struct isis_lsp *lsp0 = NULL;
1315
1316 if (LSP_FRAGMENT(entry->id)) {
1317 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1318
1319 memcpy(lspid, entry->id,
1320 ISIS_SYS_ID_LEN + 1);
1321 LSP_FRAGMENT(lspid) = 0;
1322 lsp0 = lsp_search(
98c5bc15
CF
1323 lspid,
1324 circuit->area->lspdb[level - 1]);
8f5dbe18 1325 if (!lsp0) {
98c5bc15 1326 zlog_debug("Got lsp frag in snp, while zero not in database");
8f5dbe18
CF
1327 continue;
1328 }
1329 }
f7813c7c 1330 lsp = lsp_new(circuit->area, entry->id,
17c9dcd5 1331 entry->rem_lifetime, 0, 0,
8f5dbe18 1332 entry->checksum, lsp0, level);
17c9dcd5
CF
1333 lsp_insert(lsp,
1334 circuit->area->lspdb[level - 1]);
9b39405f
CF
1335
1336 lsp_set_all_srmflags(lsp, false);
17c9dcd5 1337 ISIS_SET_FLAG(lsp->SSNflags, circuit);
df0ba689 1338 resync_needed = true;
d62a17ae 1339 }
1340 }
1341 }
eb5d44eb 1342
d62a17ae 1343 /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported
1344 */
17c9dcd5 1345 if (is_csnp) {
d62a17ae 1346 /*
1347 * Build a list from our own LSP db bounded with
1348 * start_lsp_id and stop_lsp_id
1349 */
17c9dcd5
CF
1350 struct list *lsp_list = list_new();
1351 lsp_build_list_nonzero_ht(start_lsp_id, stop_lsp_id, lsp_list,
d62a17ae 1352 circuit->area->lspdb[level - 1]);
1353
1354 /* Fixme: Find a better solution */
17c9dcd5
CF
1355 struct listnode *node, *nnode;
1356 struct isis_lsp *lsp;
1357 for (struct isis_lsp_entry *entry = entry_head; entry;
1358 entry = entry->next) {
1359 for (ALL_LIST_ELEMENTS(lsp_list, node, nnode, lsp)) {
af8ac8f9 1360 if (lsp_id_cmp(lsp->hdr.lsp_id, entry->id)
17c9dcd5
CF
1361 == 0) {
1362 list_delete_node(lsp_list, node);
1363 break;
d62a17ae 1364 }
1365 }
1366 }
17c9dcd5 1367
d62a17ae 1368 /* on remaining LSPs we set SRM (neighbor knew not of) */
9b39405f
CF
1369 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
1370 isis_tx_queue_add(circuit->tx_queue, lsp, TX_LSP_NORMAL);
df0ba689 1371 resync_needed = true;
9b39405f
CF
1372 }
1373
d62a17ae 1374 /* lets free it */
6a154c88 1375 list_delete(&lsp_list);
d62a17ae 1376 }
1377
df0ba689
CF
1378 if (fabricd_initial_sync_is_complete(circuit->area) && resync_needed)
1379 zlog_warn("OpenFabric: Needed to resync LSPDB using CSNP!\n");
1380
17c9dcd5
CF
1381 retval = ISIS_OK;
1382out:
1383 isis_free_tlvs(tlvs);
d62a17ae 1384 return retval;
eb5d44eb 1385}
1386
88f9d911
CF
1387static int pdu_size(uint8_t pdu_type, uint8_t *size)
1388{
1389 switch (pdu_type) {
1390 case L1_LAN_HELLO:
1391 case L2_LAN_HELLO:
1392 *size = ISIS_LANHELLO_HDRLEN;
1393 break;
1394 case P2P_HELLO:
1395 *size = ISIS_P2PHELLO_HDRLEN;
1396 break;
1397 case L1_LINK_STATE:
1398 case L2_LINK_STATE:
1cbd5b37 1399 case FS_LINK_STATE:
88f9d911
CF
1400 *size = ISIS_LSP_HDR_LEN;
1401 break;
1402 case L1_COMPLETE_SEQ_NUM:
1403 case L2_COMPLETE_SEQ_NUM:
1404 *size = ISIS_CSNP_HDRLEN;
1405 break;
1406 case L1_PARTIAL_SEQ_NUM:
1407 case L2_PARTIAL_SEQ_NUM:
1408 *size = ISIS_PSNP_HDRLEN;
1409 break;
1410 default:
1411 return 1;
1412 }
1413 *size += ISIS_FIXED_HDR_LEN;
1414 return 0;
1415}
1416
eb5d44eb 1417/*
1418 * PDU Dispatcher
1419 */
1420
d7c0a89a 1421int isis_handle_pdu(struct isis_circuit *circuit, uint8_t *ssnpa)
eb5d44eb 1422{
d62a17ae 1423 int retval = ISIS_OK;
eb5d44eb 1424
88f9d911
CF
1425 /* Verify that at least the 8 bytes fixed header have been received */
1426 if (stream_get_endp(circuit->rcv_stream) < ISIS_FIXED_HDR_LEN) {
1a7ecb96 1427 flog_err(EC_ISIS_PACKET, "PDU is too short to be IS-IS.");
88f9d911
CF
1428 return ISIS_ERROR;
1429 }
eb5d44eb 1430
88f9d911
CF
1431 uint8_t idrp = stream_getc(circuit->rcv_stream);
1432 uint8_t length = stream_getc(circuit->rcv_stream);
1433 uint8_t version1 = stream_getc(circuit->rcv_stream);
1434 uint8_t id_len = stream_getc(circuit->rcv_stream);
1435 uint8_t pdu_type = stream_getc(circuit->rcv_stream)
1436 & 0x1f; /* bits 6-8 are reserved */
1437 uint8_t version2 = stream_getc(circuit->rcv_stream);
1438 stream_forward_getp(circuit->rcv_stream, 1); /* reserved */
1439 uint8_t max_area_addrs = stream_getc(circuit->rcv_stream);
1440
39bb53d6
CF
1441 pdu_counter_count(circuit->area->pdu_rx_counters, pdu_type);
1442
88f9d911 1443 if (idrp == ISO9542_ESIS) {
450971aa 1444 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 1445 "No support for ES-IS packet IDRP=%" PRIx8, idrp);
d62a17ae 1446 return ISIS_ERROR;
1447 }
eb5d44eb 1448
88f9d911 1449 if (idrp != ISO10589_ISIS) {
1a7ecb96 1450 flog_err(EC_ISIS_PACKET, "Not an IS-IS packet IDRP=%" PRIx8,
1c50c1c0 1451 idrp);
d62a17ae 1452 return ISIS_ERROR;
1453 }
3f045a08 1454
88f9d911
CF
1455 if (version1 != 1) {
1456 zlog_warn("Unsupported ISIS version %" PRIu8, version1);
1457 return ISIS_WARNING;
1458 }
eb5d44eb 1459
88f9d911 1460 if (id_len != 0 && id_len != ISIS_SYS_ID_LEN) {
af4c2728 1461 flog_err(
1a7ecb96 1462 EC_ISIS_PACKET,
88f9d911
CF
1463 "IDFieldLengthMismatch: ID Length field in a received PDU %" PRIu8
1464 ", while the parameter for this IS is %u",
1465 id_len, ISIS_SYS_ID_LEN);
d62a17ae 1466 return ISIS_ERROR;
1467 }
eb5d44eb 1468
88f9d911
CF
1469 uint8_t expected_length;
1470 if (pdu_size(pdu_type, &expected_length)) {
1471 zlog_warn("Unsupported ISIS PDU %" PRIu8, pdu_type);
d62a17ae 1472 return ISIS_WARNING;
1473 }
88f9d911
CF
1474
1475 if (length != expected_length) {
1a7ecb96 1476 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
1477 "Exepected fixed header length = %" PRIu8
1478 " but got %" PRIu8,
1479 expected_length, length);
88f9d911
CF
1480 return ISIS_ERROR;
1481 }
1482
1483 if (stream_get_endp(circuit->rcv_stream) < length) {
af4c2728 1484 flog_err(
1a7ecb96 1485 EC_ISIS_PACKET,
88f9d911 1486 "PDU is too short to contain fixed header of given PDU type.");
d62a17ae 1487 return ISIS_ERROR;
1488 }
eb5d44eb 1489
88f9d911
CF
1490 if (version2 != 1) {
1491 zlog_warn("Unsupported ISIS PDU version %" PRIu8, version2);
d62a17ae 1492 return ISIS_WARNING;
1493 }
3f045a08 1494
d62a17ae 1495 if (circuit->is_passive) {
1496 zlog_warn("Received ISIS PDU on passive circuit %s",
1497 circuit->interface->name);
1498 return ISIS_WARNING;
1499 }
3f045a08 1500
d62a17ae 1501 /* either 3 or 0 */
1cbd5b37
CF
1502 if (pdu_type != FS_LINK_STATE /* FS PDU doesn't contain max area addr field */
1503 && max_area_addrs != 0
1504 && max_area_addrs != isis->max_area_addrs) {
af4c2728 1505 flog_err(
1a7ecb96 1506 EC_ISIS_PACKET,
88f9d911
CF
1507 "maximumAreaAddressesMismatch: maximumAreaAdresses in a received PDU %" PRIu8
1508 " while the parameter for this IS is %u",
1509 max_area_addrs, isis->max_area_addrs);
d62a17ae 1510 return ISIS_ERROR;
1511 }
eb5d44eb 1512
88f9d911 1513 switch (pdu_type) {
d62a17ae 1514 case L1_LAN_HELLO:
d62a17ae 1515 case L2_LAN_HELLO:
d62a17ae 1516 case P2P_HELLO:
a2d41bb0
CF
1517 if (fabricd && pdu_type != P2P_HELLO)
1518 return ISIS_ERROR;
0c1bd758 1519 retval = process_hello(pdu_type, circuit, ssnpa);
d62a17ae 1520 break;
1521 case L1_LINK_STATE:
d62a17ae 1522 case L2_LINK_STATE:
1cbd5b37 1523 case FS_LINK_STATE:
a2d41bb0
CF
1524 if (fabricd
1525 && pdu_type != L2_LINK_STATE
1526 && pdu_type != FS_LINK_STATE)
1527 return ISIS_ERROR;
9b39405f 1528 retval = process_lsp(pdu_type, circuit, ssnpa, max_area_addrs);
d62a17ae 1529 break;
1530 case L1_COMPLETE_SEQ_NUM:
d62a17ae 1531 case L2_COMPLETE_SEQ_NUM:
d62a17ae 1532 case L1_PARTIAL_SEQ_NUM:
d62a17ae 1533 case L2_PARTIAL_SEQ_NUM:
17c9dcd5 1534 retval = process_snp(pdu_type, circuit, ssnpa);
d62a17ae 1535 break;
1536 default:
1537 return ISIS_ERROR;
1538 }
eb5d44eb 1539
d62a17ae 1540 return retval;
eb5d44eb 1541}
1542
d62a17ae 1543int isis_receive(struct thread *thread)
eb5d44eb 1544{
d62a17ae 1545 struct isis_circuit *circuit;
d7c0a89a 1546 uint8_t ssnpa[ETH_ALEN];
d62a17ae 1547 int retval;
eb5d44eb 1548
d62a17ae 1549 /*
1550 * Get the circuit
1551 */
1552 circuit = THREAD_ARG(thread);
1553 assert(circuit);
eb5d44eb 1554
d62a17ae 1555 circuit->t_read = NULL;
eb5d44eb 1556
d62a17ae 1557 isis_circuit_stream(circuit, &circuit->rcv_stream);
eb5d44eb 1558
d62a17ae 1559 retval = circuit->rx(circuit, ssnpa);
eb5d44eb 1560
b9347997 1561#if ISIS_METHOD != ISIS_METHOD_BPF
d62a17ae 1562 if (retval == ISIS_OK)
1563 retval = isis_handle_pdu(circuit, ssnpa);
b9347997 1564#endif //ISIS_METHOD != ISIS_METHOD_BPF
eb5d44eb 1565
d62a17ae 1566 /*
1567 * prepare for next packet.
1568 */
1569 if (!circuit->is_passive)
1570 isis_circuit_prepare(circuit);
eb5d44eb 1571
d62a17ae 1572 return retval;
eb5d44eb 1573}
1574
eb5d44eb 1575/*
d62a17ae 1576 * SEND SIDE
eb5d44eb 1577 */
88f9d911 1578void fill_fixed_hdr(uint8_t pdu_type, struct stream *stream)
eb5d44eb 1579{
88f9d911
CF
1580 uint8_t length;
1581
1582 if (pdu_size(pdu_type, &length))
1583 assert(!"Unknown PDU Type");
1584
1585 stream_putc(stream, ISO10589_ISIS); /* IDRP */
1586 stream_putc(stream, length); /* Length of fixed header */
1587 stream_putc(stream, 1); /* Version/Protocol ID Extension 1 */
1588 stream_putc(stream, 0); /* ID Length, 0 => 6 */
1589 stream_putc(stream, pdu_type);
1590 stream_putc(stream, 1); /* Subversion */
1591 stream_putc(stream, 0); /* Reserved */
1592 stream_putc(stream, 0); /* Max Area Addresses 0 => 3 */
eb5d44eb 1593}
1594
39bb53d6 1595static uint8_t hello_pdu_type(struct isis_circuit *circuit, int level)
eb5d44eb 1596{
d62a17ae 1597 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
39bb53d6 1598 return (level == IS_LEVEL_1) ? L1_LAN_HELLO : L2_LAN_HELLO;
d62a17ae 1599 else
39bb53d6
CF
1600 return P2P_HELLO;
1601}
1602
1603static void put_hello_hdr(struct isis_circuit *circuit, int level,
1604 size_t *len_pointer)
1605{
1606 uint8_t pdu_type = hello_pdu_type(circuit, level);
88f9d911 1607
0c1bd758 1608 isis_circuit_stream(circuit, &circuit->snd_stream);
88f9d911 1609 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 1610
0c1bd758
CF
1611 stream_putc(circuit->snd_stream, circuit->is_type);
1612 stream_put(circuit->snd_stream, circuit->area->isis->sysid,
1613 ISIS_SYS_ID_LEN);
eb5d44eb 1614
0c1bd758
CF
1615 uint32_t holdtime = circuit->hello_multiplier[level - 1]
1616 * circuit->hello_interval[level - 1];
d62a17ae 1617
0c1bd758
CF
1618 if (holdtime > 0xffff)
1619 holdtime = 0xffff;
d62a17ae 1620
0c1bd758
CF
1621 stream_putw(circuit->snd_stream, holdtime);
1622 *len_pointer = stream_get_endp(circuit->snd_stream);
1623 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
d62a17ae 1624
0c1bd758 1625 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
d7c0a89a
QY
1626 uint8_t *desig_is = (level == IS_LEVEL_1)
1627 ? circuit->u.bc.l1_desig_is
1628 : circuit->u.bc.l2_desig_is;
0c1bd758
CF
1629 stream_putc(circuit->snd_stream, circuit->priority[level - 1]);
1630 stream_put(circuit->snd_stream, desig_is, ISIS_SYS_ID_LEN + 1);
1631 } else {
1632 stream_putc(circuit->snd_stream, circuit->circuit_id);
d62a17ae 1633 }
0c1bd758 1634}
eb5d44eb 1635
0c1bd758
CF
1636int send_hello(struct isis_circuit *circuit, int level)
1637{
1638 size_t len_pointer;
1639 int retval;
1640
1641 if (circuit->is_passive)
1642 return ISIS_OK;
1643
1644 if (circuit->interface->mtu == 0) {
1645 zlog_warn("circuit has zero MTU");
d62a17ae 1646 return ISIS_WARNING;
0c1bd758
CF
1647 }
1648
1649 put_hello_hdr(circuit, level, &len_pointer);
1650
1651 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1652
1653 isis_tlvs_add_auth(tlvs, &circuit->passwd);
1654
a3acf37d
DS
1655 if (!listcount(circuit->area->area_addrs)) {
1656 isis_free_tlvs(tlvs);
d62a17ae 1657 return ISIS_WARNING;
a3acf37d
DS
1658 }
1659
0c1bd758 1660 isis_tlvs_add_area_addresses(tlvs, circuit->area->area_addrs);
d62a17ae 1661
42fe2621 1662 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
0c1bd758
CF
1663 isis_tlvs_add_lan_neighbors(
1664 tlvs, circuit->u.bc.lan_neighs[level - 1]);
58e5d748
CF
1665 } else if (circuit->circ_type == CIRCUIT_T_P2P
1666 && !circuit->disable_threeway_adj) {
42fe2621
CF
1667 uint32_t ext_circuit_id = circuit->idx;
1668 if (circuit->u.p2p.neighbor) {
8e6fb83b
CF
1669 uint8_t threeway_state;
1670
1671 if (fabricd_initial_sync_is_in_progress(circuit->area)
1672 && fabricd_initial_sync_circuit(circuit->area) != circuit)
1673 threeway_state = ISIS_THREEWAY_DOWN;
1674 else
1675 threeway_state = circuit->u.p2p.neighbor->threeway_state;
42fe2621 1676 isis_tlvs_add_threeway_adj(tlvs,
8e6fb83b 1677 threeway_state,
42fe2621
CF
1678 ext_circuit_id,
1679 circuit->u.p2p.neighbor->sysid,
1680 circuit->u.p2p.neighbor->ext_circuit_id);
1681 } else {
1682 isis_tlvs_add_threeway_adj(tlvs,
1683 ISIS_THREEWAY_DOWN,
1684 ext_circuit_id,
1685 NULL, 0);
1686 }
1687 }
0c1bd758
CF
1688
1689 isis_tlvs_set_protocols_supported(tlvs, &circuit->nlpids);
d62a17ae 1690
1691 /*
1692 * MT Supported TLV
1693 *
1694 * TLV gets included if no topology is enabled on the interface,
1695 * if one topology other than #0 is enabled, or if multiple topologies
1696 * are enabled.
1697 */
1698 struct isis_circuit_mt_setting **mt_settings;
1699 unsigned int mt_count;
1700
1701 mt_settings = circuit_mt_settings(circuit, &mt_count);
0c1bd758
CF
1702 if (mt_count == 0 && area_is_mt(circuit->area)) {
1703 tlvs->mt_router_info_empty = true;
1704 } else if ((mt_count == 1
1705 && mt_settings[0]->mtid != ISIS_MT_IPV4_UNICAST)
1706 || (mt_count > 1)) {
1707 for (unsigned int i = 0; i < mt_count; i++)
1708 isis_tlvs_add_mt_router_info(tlvs, mt_settings[i]->mtid,
1709 false, false);
d62a17ae 1710 }
eb5d44eb 1711
41415888
CF
1712 if (circuit->ip_router) {
1713 struct list *circuit_ip_addrs = fabricd_ip_addrs(circuit);
1714
1715 if (circuit_ip_addrs)
1716 isis_tlvs_add_ipv4_addresses(tlvs, circuit_ip_addrs);
1717 }
d62a17ae 1718
0c1bd758
CF
1719 if (circuit->ipv6_router && circuit->ipv6_link)
1720 isis_tlvs_add_ipv6_addresses(tlvs, circuit->ipv6_link);
d62a17ae 1721
0c1bd758 1722 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1723 circuit->pad_hellos, false)) {
0c1bd758
CF
1724 isis_free_tlvs(tlvs);
1725 return ISIS_WARNING; /* XXX: Maybe Log TLV structure? */
f390d2c7 1726 }
d62a17ae 1727
1728 if (isis->debugs & DEBUG_ADJ_PACKETS) {
1729 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1730 zlog_debug(
1731 "ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
1732 circuit->area->area_tag, level,
0c1bd758
CF
1733 circuit->interface->name,
1734 stream_get_endp(circuit->snd_stream));
d62a17ae 1735 } else {
1736 zlog_debug(
1737 "ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
1738 circuit->area->area_tag,
0c1bd758
CF
1739 circuit->interface->name,
1740 stream_get_endp(circuit->snd_stream));
d62a17ae 1741 }
1742 if (isis->debugs & DEBUG_PACKET_DUMP)
1743 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
1744 stream_get_endp(circuit->snd_stream));
f390d2c7 1745 }
eb5d44eb 1746
0c1bd758
CF
1747 isis_free_tlvs(tlvs);
1748
39bb53d6 1749 pdu_counter_count(circuit->area->pdu_tx_counters, hello_pdu_type(circuit, level));
d62a17ae 1750 retval = circuit->tx(circuit, level);
1751 if (retval != ISIS_OK)
1a7ecb96 1752 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
1753 "ISIS-Adj (%s): Send L%d IIH on %s failed",
1754 circuit->area->area_tag, level,
1755 circuit->interface->name);
eb5d44eb 1756
d62a17ae 1757 return retval;
eb5d44eb 1758}
1759
a0a707ee 1760static int send_hello_cb(struct thread *thread)
eb5d44eb 1761{
7c4f7aab
CF
1762 struct isis_circuit_arg *arg = THREAD_ARG(thread);
1763
1764 assert(arg);
1765
1766 struct isis_circuit *circuit = arg->circuit;
1767 int level = arg->level;
d62a17ae 1768
d62a17ae 1769 assert(circuit);
ddfdbd32 1770
7c4f7aab
CF
1771 if (circuit->circ_type == CIRCUIT_T_P2P) {
1772 circuit->u.p2p.t_send_p2p_hello = NULL;
7c4f7aab 1773 send_hello(circuit, 1);
a0a707ee
CF
1774 send_hello_sched(circuit, ISIS_LEVEL1,
1775 1000 * circuit->hello_interval[1]);
7c4f7aab 1776 return ISIS_OK;
d62a17ae 1777 }
ddfdbd32 1778
7c4f7aab
CF
1779 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
1780 zlog_warn("ISIS-Hello (%s): Trying to send hello on unknown circuit type %d",
1781 circuit->area->area_tag, circuit->circ_type);
1782 return ISIS_WARNING;
1783 }
eb5d44eb 1784
7c4f7aab
CF
1785 circuit->u.bc.t_send_lan_hello[level - 1] = NULL;
1786 if (!(circuit->is_type & level)) {
1787 zlog_warn("ISIS-Hello (%s): Trying to send L%d IIH in L%d-only circuit",
1788 circuit->area->area_tag, level, 3 - level);
1789 return ISIS_WARNING;
1790 }
eb5d44eb 1791
7c4f7aab
CF
1792 if (circuit->u.bc.run_dr_elect[level - 1])
1793 isis_dr_elect(circuit, level);
eb5d44eb 1794
7c4f7aab 1795 int rv = send_hello(circuit, level);
eb5d44eb 1796
d62a17ae 1797 /* set next timer thread */
a0a707ee 1798 send_hello_sched(circuit, level, 1000 * circuit->hello_interval[level - 1]);
7c4f7aab 1799 return rv;
eb5d44eb 1800}
1801
a0a707ee
CF
1802static void _send_hello_sched(struct isis_circuit *circuit,
1803 struct thread **threadp,
1804 int level, long delay)
1805{
1806 if (*threadp) {
1807 if (thread_timer_remain_msec(*threadp) < (unsigned long)delay)
1808 return;
1809
1810 thread_cancel(*threadp);
1811 }
1812
1813 thread_add_timer_msec(master, send_hello_cb,
1814 &circuit->level_arg[level - 1],
1815 isis_jitter(delay, IIH_JITTER),
1816 threadp);
1817}
1818
1819void send_hello_sched(struct isis_circuit *circuit, int level, long delay)
1820{
1821 if (circuit->circ_type == CIRCUIT_T_P2P) {
1822 _send_hello_sched(circuit, &circuit->u.p2p.t_send_p2p_hello,
1823 ISIS_LEVEL1, delay);
1824 return;
1825 }
1826
1827 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
1828 zlog_warn("%s: encountered unknown circuit type %d on %s",
1829 __func__, circuit->circ_type,
1830 circuit->interface->name);
1831 return;
1832 }
1833
1834 for (int loop_level = ISIS_LEVEL1; loop_level <= ISIS_LEVEL2; loop_level++) {
1835 if (!(loop_level & level))
1836 continue;
1837
1838 _send_hello_sched(
1839 circuit,
1840 &circuit->u.bc.t_send_lan_hello[loop_level - 1],
1841 loop_level,
1842 delay
1843 );
1844 }
1845}
1846
1847
3f045a08
JB
1848/*
1849 * Count the maximum number of lsps that can be accomodated by a given size.
1850 */
af8ac8f9 1851#define LSP_ENTRIES_LEN (10 + ISIS_SYS_ID_LEN)
d62a17ae 1852static uint16_t get_max_lsp_count(uint16_t size)
3f045a08 1853{
d62a17ae 1854 uint16_t tlv_count;
1855 uint16_t lsp_count;
1856 uint16_t remaining_size;
3f045a08 1857
d62a17ae 1858 /* First count the full size TLVs */
1859 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
1860 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
3f045a08 1861
d62a17ae 1862 /* The last TLV, if any */
1863 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
1864 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
1865 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
3f045a08 1866
d62a17ae 1867 return lsp_count;
3f045a08
JB
1868}
1869
17c9dcd5 1870int send_csnp(struct isis_circuit *circuit, int level)
3f045a08 1871{
17c9dcd5
CF
1872 if (circuit->area->lspdb[level - 1] == NULL
1873 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1874 return ISIS_OK;
d62a17ae 1875
39bb53d6
CF
1876 uint8_t pdu_type = (level == ISIS_LEVEL1) ? L1_COMPLETE_SEQ_NUM
1877 : L2_COMPLETE_SEQ_NUM;
1878
17c9dcd5 1879 isis_circuit_stream(circuit, &circuit->snd_stream);
39bb53d6 1880 fill_fixed_hdr(pdu_type, circuit->snd_stream);
3f045a08 1881
17c9dcd5
CF
1882 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1883 stream_putw(circuit->snd_stream, 0);
1884 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1885 /* with zero circuit id - ref 9.10, 9.11 */
1886 stream_putc(circuit->snd_stream, 0);
3f045a08 1887
17c9dcd5
CF
1888 size_t start_pointer = stream_get_endp(circuit->snd_stream);
1889 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
1890 size_t end_pointer = stream_get_endp(circuit->snd_stream);
1891 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
d62a17ae 1892
17c9dcd5
CF
1893 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1894 ? &circuit->area->area_passwd
1895 : &circuit->area->domain_passwd;
d62a17ae 1896
17c9dcd5 1897 struct isis_tlvs *tlvs = isis_alloc_tlvs();
3f045a08 1898
17c9dcd5
CF
1899 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1900 isis_tlvs_add_auth(tlvs, passwd);
eb5d44eb 1901
17c9dcd5 1902 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
1903 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
1904 false)) {
17c9dcd5
CF
1905 isis_free_tlvs(tlvs);
1906 return ISIS_WARNING;
1907 }
1908 isis_free_tlvs(tlvs);
d62a17ae 1909
17c9dcd5
CF
1910 uint16_t num_lsps =
1911 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
d62a17ae 1912
17c9dcd5 1913 uint8_t start[ISIS_SYS_ID_LEN + 2];
d62a17ae 1914 memset(start, 0x00, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1915 uint8_t stop[ISIS_SYS_ID_LEN + 2];
d62a17ae 1916 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1917
17c9dcd5 1918 bool loop = true;
d62a17ae 1919 while (loop) {
17c9dcd5
CF
1920 tlvs = isis_alloc_tlvs();
1921 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1922 isis_tlvs_add_auth(tlvs, passwd);
1923
1924 struct isis_lsp *last_lsp;
1925 isis_tlvs_add_csnp_entries(tlvs, start, stop, num_lsps,
1926 circuit->area->lspdb[level - 1],
1927 &last_lsp);
d62a17ae 1928 /*
1929 * Update the stop lsp_id before encoding this CSNP.
1930 */
17c9dcd5 1931 if (tlvs->lsp_entries.count < num_lsps) {
d62a17ae 1932 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1933 } else {
af8ac8f9 1934 memcpy(stop, last_lsp->hdr.lsp_id, sizeof(stop));
d62a17ae 1935 }
3f045a08 1936
17c9dcd5
CF
1937 memcpy(STREAM_DATA(circuit->snd_stream) + start_pointer, start,
1938 ISIS_SYS_ID_LEN + 2);
1939 memcpy(STREAM_DATA(circuit->snd_stream) + end_pointer, stop,
1940 ISIS_SYS_ID_LEN + 2);
1941 stream_set_endp(circuit->snd_stream, tlv_start);
1942 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1943 false, false)) {
17c9dcd5
CF
1944 isis_free_tlvs(tlvs);
1945 return ISIS_WARNING;
d62a17ae 1946 }
eb5d44eb 1947
d62a17ae 1948 if (isis->debugs & DEBUG_SNP_PACKETS) {
1949 zlog_debug(
1950 "ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
1951 circuit->area->area_tag, level,
1952 circuit->interface->name,
1953 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
1954 log_multiline(LOG_DEBUG, " ", "%s",
1955 isis_format_tlvs(tlvs));
d62a17ae 1956 if (isis->debugs & DEBUG_PACKET_DUMP)
1957 zlog_dump_data(
1958 STREAM_DATA(circuit->snd_stream),
1959 stream_get_endp(circuit->snd_stream));
1960 }
eb5d44eb 1961
39bb53d6 1962 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
17c9dcd5 1963 int retval = circuit->tx(circuit, level);
d62a17ae 1964 if (retval != ISIS_OK) {
1a7ecb96 1965 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
1966 "ISIS-Snp (%s): Send L%d CSNP on %s failed",
1967 circuit->area->area_tag, level,
1968 circuit->interface->name);
17c9dcd5 1969 isis_free_tlvs(tlvs);
d62a17ae 1970 return retval;
1971 }
3f045a08 1972
d62a17ae 1973 /*
1974 * Start lsp_id of the next CSNP should be one plus the
1975 * stop lsp_id in this current CSNP.
1976 */
1977 memcpy(start, stop, ISIS_SYS_ID_LEN + 2);
1978 loop = 0;
17c9dcd5 1979 for (int i = ISIS_SYS_ID_LEN + 1; i >= 0; --i) {
d7c0a89a 1980 if (start[i] < (uint8_t)0xff) {
d62a17ae 1981 start[i] += 1;
1982 loop = 1;
1983 break;
1984 }
1985 }
1986 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1987 isis_free_tlvs(tlvs);
d62a17ae 1988 }
3f045a08 1989
17c9dcd5 1990 return ISIS_OK;
eb5d44eb 1991}
1992
d62a17ae 1993int send_l1_csnp(struct thread *thread)
eb5d44eb 1994{
d62a17ae 1995 struct isis_circuit *circuit;
1996 int retval = ISIS_OK;
eb5d44eb 1997
d62a17ae 1998 circuit = THREAD_ARG(thread);
1999 assert(circuit);
eb5d44eb 2000
d62a17ae 2001 circuit->t_send_csnp[0] = NULL;
eb5d44eb 2002
8e6fb83b
CF
2003 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
2004 && circuit->u.bc.is_dr[0])
2005 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 2006 send_csnp(circuit, 1);
2007 }
2008 /* set next timer thread */
2009 thread_add_timer(master, send_l1_csnp, circuit,
2010 isis_jitter(circuit->csnp_interval[0], CSNP_JITTER),
2011 &circuit->t_send_csnp[0]);
eb5d44eb 2012
d62a17ae 2013 return retval;
eb5d44eb 2014}
2015
d62a17ae 2016int send_l2_csnp(struct thread *thread)
eb5d44eb 2017{
d62a17ae 2018 struct isis_circuit *circuit;
2019 int retval = ISIS_OK;
eb5d44eb 2020
d62a17ae 2021 circuit = THREAD_ARG(thread);
2022 assert(circuit);
eb5d44eb 2023
d62a17ae 2024 circuit->t_send_csnp[1] = NULL;
eb5d44eb 2025
8e6fb83b
CF
2026 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
2027 && circuit->u.bc.is_dr[1])
2028 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 2029 send_csnp(circuit, 2);
2030 }
2031 /* set next timer thread */
2032 thread_add_timer(master, send_l2_csnp, circuit,
2033 isis_jitter(circuit->csnp_interval[1], CSNP_JITTER),
2034 &circuit->t_send_csnp[1]);
d70f99e1 2035
d62a17ae 2036 return retval;
eb5d44eb 2037}
2038
eb5d44eb 2039/*
2040 * 7.3.15.4 action on expiration of partial SNP interval
2041 * level 1
2042 */
d62a17ae 2043static int send_psnp(int level, struct isis_circuit *circuit)
eb5d44eb 2044{
d62a17ae 2045 if (circuit->circ_type == CIRCUIT_T_BROADCAST
2046 && circuit->u.bc.is_dr[level - 1])
2047 return ISIS_OK;
eb5d44eb 2048
d62a17ae 2049 if (circuit->area->lspdb[level - 1] == NULL
2050 || dict_count(circuit->area->lspdb[level - 1]) == 0)
2051 return ISIS_OK;
f390d2c7 2052
d62a17ae 2053 if (!circuit->snd_stream)
2054 return ISIS_ERROR;
e38e0df0 2055
39bb53d6
CF
2056 uint8_t pdu_type = (level == ISIS_LEVEL1) ? L1_PARTIAL_SEQ_NUM
2057 : L2_PARTIAL_SEQ_NUM;
2058
17c9dcd5 2059 isis_circuit_stream(circuit, &circuit->snd_stream);
39bb53d6 2060 fill_fixed_hdr(pdu_type, circuit->snd_stream);
17c9dcd5
CF
2061
2062 size_t len_pointer = stream_get_endp(circuit->snd_stream);
2063 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
2064 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2065 stream_putc(circuit->snd_stream, circuit->idx);
2066
2067 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
2068 ? &circuit->area->area_passwd
2069 : &circuit->area->domain_passwd;
2070
2071 struct isis_tlvs *tlvs = isis_alloc_tlvs();
2072
2073 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2074 isis_tlvs_add_auth(tlvs, passwd);
2075
2076 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
2077 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
2078 false)) {
17c9dcd5
CF
2079 isis_free_tlvs(tlvs);
2080 return ISIS_WARNING;
2081 }
2082 isis_free_tlvs(tlvs);
2083
2084 uint16_t num_lsps =
2085 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
f390d2c7 2086
d62a17ae 2087 while (1) {
17c9dcd5
CF
2088 tlvs = isis_alloc_tlvs();
2089 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2090 isis_tlvs_add_auth(tlvs, passwd);
2091
2092 for (dnode_t *dnode =
2093 dict_first(circuit->area->lspdb[level - 1]);
2094 dnode; dnode = dict_next(circuit->area->lspdb[level - 1],
2095 dnode)) {
2096 struct isis_lsp *lsp = dnode_get(dnode);
2097
2098 if (ISIS_CHECK_FLAG(lsp->SSNflags, circuit))
2099 isis_tlvs_add_lsp_entry(tlvs, lsp);
eb5d44eb 2100
17c9dcd5
CF
2101 if (tlvs->lsp_entries.count == num_lsps)
2102 break;
2103 }
2104
2105 if (!tlvs->lsp_entries.count) {
2106 isis_free_tlvs(tlvs);
d62a17ae 2107 return ISIS_OK;
2108 }
2109
17c9dcd5
CF
2110 stream_set_endp(circuit->snd_stream, tlv_start);
2111 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 2112 false, false)) {
17c9dcd5
CF
2113 isis_free_tlvs(tlvs);
2114 return ISIS_WARNING;
d62a17ae 2115 }
2116
2117 if (isis->debugs & DEBUG_SNP_PACKETS) {
2118 zlog_debug(
2119 "ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
2120 circuit->area->area_tag, level,
2121 circuit->interface->name,
2122 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
2123 log_multiline(LOG_DEBUG, " ", "%s",
2124 isis_format_tlvs(tlvs));
d62a17ae 2125 if (isis->debugs & DEBUG_PACKET_DUMP)
2126 zlog_dump_data(
2127 STREAM_DATA(circuit->snd_stream),
2128 stream_get_endp(circuit->snd_stream));
2129 }
2130
39bb53d6 2131 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
17c9dcd5 2132 int retval = circuit->tx(circuit, level);
d62a17ae 2133 if (retval != ISIS_OK) {
1a7ecb96 2134 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
2135 "ISIS-Snp (%s): Send L%d PSNP on %s failed",
2136 circuit->area->area_tag, level,
2137 circuit->interface->name);
17c9dcd5 2138 isis_free_tlvs(tlvs);
d62a17ae 2139 return retval;
2140 }
2141
2142 /*
2143 * sending succeeded, we can clear SSN flags of this circuit
2144 * for the LSPs in list
2145 */
17c9dcd5
CF
2146 struct isis_lsp_entry *entry_head;
2147 entry_head = (struct isis_lsp_entry *)tlvs->lsp_entries.head;
2148 for (struct isis_lsp_entry *entry = entry_head; entry;
2149 entry = entry->next)
2150 ISIS_CLEAR_FLAG(entry->lsp->SSNflags, circuit);
2151 isis_free_tlvs(tlvs);
d62a17ae 2152 }
2153
17c9dcd5 2154 return ISIS_OK;
eb5d44eb 2155}
2156
d62a17ae 2157int send_l1_psnp(struct thread *thread)
eb5d44eb 2158{
2159
d62a17ae 2160 struct isis_circuit *circuit;
2161 int retval = ISIS_OK;
eb5d44eb 2162
d62a17ae 2163 circuit = THREAD_ARG(thread);
2164 assert(circuit);
eb5d44eb 2165
d62a17ae 2166 circuit->t_send_psnp[0] = NULL;
eb5d44eb 2167
d62a17ae 2168 send_psnp(1, circuit);
2169 /* set next timer thread */
2170 thread_add_timer(master, send_l1_psnp, circuit,
2171 isis_jitter(circuit->psnp_interval[0], PSNP_JITTER),
2172 &circuit->t_send_psnp[0]);
eb5d44eb 2173
d62a17ae 2174 return retval;
eb5d44eb 2175}
2176
2177/*
2178 * 7.3.15.4 action on expiration of partial SNP interval
2179 * level 2
2180 */
d62a17ae 2181int send_l2_psnp(struct thread *thread)
eb5d44eb 2182{
d62a17ae 2183 struct isis_circuit *circuit;
2184 int retval = ISIS_OK;
eb5d44eb 2185
d62a17ae 2186 circuit = THREAD_ARG(thread);
2187 assert(circuit);
eb5d44eb 2188
d62a17ae 2189 circuit->t_send_psnp[1] = NULL;
eb5d44eb 2190
d62a17ae 2191 send_psnp(2, circuit);
eb5d44eb 2192
d62a17ae 2193 /* set next timer thread */
2194 thread_add_timer(master, send_l2_psnp, circuit,
2195 isis_jitter(circuit->psnp_interval[1], PSNP_JITTER),
2196 &circuit->t_send_psnp[1]);
eb5d44eb 2197
d62a17ae 2198 return retval;
eb5d44eb 2199}
2200
eb5d44eb 2201/*
2202 * ISO 10589 - 7.3.14.3
2203 */
161fa356
CF
2204void send_lsp(struct isis_circuit *circuit, struct isis_lsp *lsp,
2205 enum isis_tx_type tx_type)
eb5d44eb 2206{
d62a17ae 2207 int clear_srm = 1;
2208 int retval = ISIS_OK;
2209
d62a17ae 2210 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
2211 goto out;
2212
2213 /*
2214 * Do not send if levels do not match
2215 */
2216 if (!(lsp->level & circuit->is_type))
2217 goto out;
2218
2219 /*
2220 * Do not send if we do not have adjacencies in state up on the circuit
2221 */
2222 if (circuit->upadjcount[lsp->level - 1] == 0)
2223 goto out;
2224
2225 /* stream_copy will assert and stop program execution if LSP is larger
2226 * than
2227 * the circuit's MTU. So handle and log this case here. */
2228 if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream)) {
af4c2728 2229 flog_err(
1a7ecb96 2230 EC_ISIS_PACKET,
af8ac8f9
CF
2231 "ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08" PRIx32
2232 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2233 "s on %s. LSP Size is %zu while interface stream size is %zu.",
d62a17ae 2234 circuit->area->area_tag, lsp->level,
af8ac8f9
CF
2235 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2236 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
d62a17ae 2237 circuit->interface->name, stream_get_endp(lsp->pdu),
2238 stream_get_size(circuit->snd_stream));
2239 if (isis->debugs & DEBUG_PACKET_DUMP)
2240 zlog_dump_data(STREAM_DATA(lsp->pdu),
2241 stream_get_endp(lsp->pdu));
2242 retval = ISIS_ERROR;
2243 goto out;
2244 }
eb5d44eb 2245
d62a17ae 2246 /* copy our lsp to the send buffer */
2247 stream_copy(circuit->snd_stream, lsp->pdu);
2248
1cbd5b37
CF
2249 if (tx_type == TX_LSP_CIRCUIT_SCOPED) {
2250 stream_putc_at(circuit->snd_stream, 4, FS_LINK_STATE);
2251 stream_putc_at(circuit->snd_stream, 7, L2_CIRCUIT_FLOODING_SCOPE);
2252 }
2253
d62a17ae 2254 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
161fa356 2255 zlog_debug("ISIS-Upd (%s): Sending %sL%d LSP %s, seq 0x%08" PRIx32
af8ac8f9
CF
2256 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2257 "s on %s",
161fa356
CF
2258 circuit->area->area_tag,
2259 (tx_type == TX_LSP_CIRCUIT_SCOPED)
2260 ? "Circuit scoped " : "",
2261 lsp->level,
af8ac8f9
CF
2262 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2263 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
2264 circuit->interface->name);
d62a17ae 2265 if (isis->debugs & DEBUG_PACKET_DUMP)
2266 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
2267 stream_get_endp(circuit->snd_stream));
2268 }
3f045a08 2269
39bb53d6
CF
2270 uint8_t pdu_type = (tx_type == TX_LSP_CIRCUIT_SCOPED) ? FS_LINK_STATE
2271 : (lsp->level == ISIS_LEVEL1) ? L1_LINK_STATE
2272 : L2_LINK_STATE;
2273
d62a17ae 2274 clear_srm = 0;
39bb53d6 2275 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
d62a17ae 2276 retval = circuit->tx(circuit, lsp->level);
2277 if (retval != ISIS_OK) {
1a7ecb96 2278 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
2279 "ISIS-Upd (%s): Send L%d LSP on %s failed %s",
2280 circuit->area->area_tag, lsp->level,
2281 circuit->interface->name,
2282 (retval == ISIS_WARNING) ? "temporarily"
2283 : "permanently");
d62a17ae 2284 }
f390d2c7 2285
cfd1f27b 2286out:
d62a17ae 2287 if (clear_srm
2288 || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
2289 || (retval != ISIS_OK && retval != ISIS_WARNING)) {
2290 /* SRM flag will trigger retransmission. We will not retransmit
2291 * if we
2292 * encountered a fatal error.
2293 * On success, they should only be cleared if it's a broadcast
2294 * circuit.
2295 * On a P2P circuit, we will wait for the ack from the neighbor
2296 * to clear
2297 * the fag.
2298 */
9b39405f 2299 isis_tx_queue_del(circuit->tx_queue, lsp);
d62a17ae 2300 }
f390d2c7 2301}