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