]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
isisd: implement the 'rejected-adjacency' notification
[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 555{
3380c990
EDP
556 /* keep a copy of the raw pdu for NB notifications */
557 size_t pdu_start = stream_get_getp(circuit->rcv_stream);
558 size_t pdu_end = stream_get_endp(circuit->rcv_stream);
559 char raw_pdu[pdu_end - pdu_start];
0c1bd758
CF
560 bool p2p_hello = (pdu_type == P2P_HELLO);
561 int level = p2p_hello ? 0
562 : (pdu_type == L1_LAN_HELLO) ? ISIS_LEVEL1
563 : ISIS_LEVEL2;
564 const char *pdu_name =
565 p2p_hello
566 ? "P2P IIH"
567 : (level == ISIS_LEVEL1) ? "L1 LAN IIH" : "L2 LAN IIH";
568
3380c990
EDP
569
570 stream_get_from(raw_pdu, circuit->rcv_stream, pdu_start,
571 pdu_end - pdu_start);
0c1bd758
CF
572 if (isis->debugs & DEBUG_ADJ_PACKETS) {
573 zlog_debug("ISIS-Adj (%s): Rcvd %s on %s, cirType %s, cirID %u",
574 circuit->area->area_tag, pdu_name,
575 circuit->interface->name,
576 circuit_t2string(circuit->is_type),
577 circuit->circuit_id);
578 if (isis->debugs & DEBUG_PACKET_DUMP)
579 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
580 stream_get_endp(circuit->rcv_stream));
581 }
582
583 if (p2p_hello) {
584 if (circuit->circ_type != CIRCUIT_T_P2P) {
585 zlog_warn("p2p hello on non p2p circuit");
af6911c9
EDP
586#ifndef FABRICD
587 isis_notif_reject_adjacency(
588 circuit, "p2p hello on non p2p circuit",
589 raw_pdu);
590#endif /* ifndef FABRICD */
0c1bd758
CF
591 return ISIS_WARNING;
592 }
593 } else {
594 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
595 zlog_warn("lan hello on non broadcast circuit");
af6911c9
EDP
596#ifndef FABRICD
597 isis_notif_reject_adjacency(
598 circuit, "lan hello on non broadcast circuit",
599 raw_pdu);
600#endif /* ifndef FABRICD */
0c1bd758
CF
601 return ISIS_WARNING;
602 }
603
604 if (circuit->ext_domain) {
d62a17ae 605 zlog_debug(
0c1bd758
CF
606 "level %d LAN Hello received over circuit with externalDomain = true",
607 level);
af6911c9
EDP
608#ifndef FABRICD
609 isis_notif_reject_adjacency(
610 circuit,
611 "LAN Hello received over circuit with externalDomain = true",
612 raw_pdu);
613#endif /* ifndef FABRICD */
0c1bd758
CF
614 return ISIS_WARNING;
615 }
616
af8ac8f9 617 if (!(circuit->is_type & level)) {
0c1bd758
CF
618 if (isis->debugs & DEBUG_ADJ_PACKETS) {
619 zlog_debug(
620 "ISIS-Adj (%s): Interface level mismatch, %s",
621 circuit->area->area_tag,
622 circuit->interface->name);
623 }
af6911c9
EDP
624#ifndef FABRICD
625 isis_notif_reject_adjacency(
626 circuit, "Interface level mismatch", raw_pdu);
627#endif /* ifndef FABRICD */
0c1bd758 628 return ISIS_WARNING;
d62a17ae 629 }
f390d2c7 630 }
eb5d44eb 631
0c1bd758
CF
632 struct iih_info iih = {
633 .circuit = circuit, .ssnpa = ssnpa, .level = level};
634
635 /* Generic IIH Header */
636 iih.circ_type = stream_getc(circuit->rcv_stream) & 0x03;
637 stream_get(iih.sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
638 iih.holdtime = stream_getw(circuit->rcv_stream);
639 iih.pdu_len = stream_getw(circuit->rcv_stream);
640
641 if (p2p_hello) {
642 iih.circuit_id = stream_getc(circuit->rcv_stream);
643 } else {
644 iih.priority = stream_getc(circuit->rcv_stream);
645 stream_get(iih.dis, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
f390d2c7 646 }
0c1bd758 647
17c9dcd5 648 if (pdu_len_validate(iih.pdu_len, circuit)) {
d62a17ae 649 zlog_warn(
0c1bd758
CF
650 "ISIS-Adj (%s): Rcvd %s from (%s) with invalid pdu length %" PRIu16,
651 circuit->area->area_tag, pdu_name,
652 circuit->interface->name, iih.pdu_len);
af6911c9
EDP
653#ifndef FABRICD
654 isis_notif_reject_adjacency(circuit, "Invalid PDU length",
655 raw_pdu);
656#endif /* ifndef FABRICD */
d62a17ae 657 return ISIS_WARNING;
658 }
3f045a08 659
0c1bd758 660 if (!p2p_hello && !(level & iih.circ_type)) {
1a7ecb96 661 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
662 "Level %d LAN Hello with Circuit Type %d", level,
663 iih.circ_type);
af6911c9
EDP
664#ifndef FABRICD
665 isis_notif_reject_adjacency(
666 circuit, "LAN Hello with wrong IS-level", raw_pdu);
667#endif /* ifndef FABRICD */
d62a17ae 668 return ISIS_ERROR;
669 }
3f045a08 670
0c1bd758
CF
671 const char *error_log;
672 int retval = ISIS_WARNING;
d62a17ae 673
0c1bd758
CF
674 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
675 circuit->rcv_stream, &iih.tlvs, &error_log)) {
676 zlog_warn("isis_unpack_tlvs() failed: %s", error_log);
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);
d62a17ae 686 goto out;
687 }
f390d2c7 688
0c1bd758
CF
689 if (!iih.tlvs->protocols_supported.count) {
690 zlog_warn("No supported protocols TLV in %s", pdu_name);
af6911c9
EDP
691#ifndef FABRICD
692 isis_notif_reject_adjacency(
693 circuit, "No supported protocols TLV", raw_pdu);
694#endif /* ifndef FABRICD */
d62a17ae 695 goto out;
696 }
b72f345d 697
3380c990
EDP
698 int auth_code = isis_tlvs_auth_is_valid(iih.tlvs, &circuit->passwd,
699 circuit->rcv_stream, false);
700 if (auth_code != ISIS_AUTH_OK) {
0c1bd758
CF
701 isis_event_auth_failure(circuit->area->area_tag,
702 "IIH authentication failure",
703 iih.sys_id);
3380c990
EDP
704#ifndef FABRICD
705 /* send northbound notification */
706 stream_get_from(raw_pdu, circuit->rcv_stream, pdu_start,
707 pdu_end - pdu_start);
708 if (auth_code == ISIS_AUTH_FAILURE)
709 isis_notif_authentication_failure(circuit, raw_pdu);
710 else /* AUTH_TYPE_FAILURE or NO_VALIDATOR */
711 isis_notif_authentication_type_failure(circuit,
712 raw_pdu);
713#endif /* ifndef FABRICD */
0c1bd758 714 goto out;
d62a17ae 715 }
eb5d44eb 716
0c1bd758
CF
717 if (!memcmp(iih.sys_id, isis->sysid, ISIS_SYS_ID_LEN)) {
718 zlog_warn(
719 "ISIS-Adj (%s): Received IIH with own sysid - discard",
720 circuit->area->area_tag);
af6911c9
EDP
721#ifndef FABRICD
722 isis_notif_reject_adjacency(
723 circuit, "Received IIH with our own sysid", raw_pdu);
724#endif /* ifndef FABRICD */
0c1bd758 725 goto out;
d62a17ae 726 }
19f78ceb 727
0c1bd758
CF
728 if (!p2p_hello
729 && (listcount(circuit->area->area_addrs) == 0
730 || (level == ISIS_LEVEL1
731 && !isis_tlvs_area_addresses_match(
732 iih.tlvs, circuit->area->area_addrs)))) {
d62a17ae 733 if (isis->debugs & DEBUG_ADJ_PACKETS) {
734 zlog_debug(
735 "ISIS-Adj (%s): Area mismatch, level %d IIH on %s",
736 circuit->area->area_tag, level,
737 circuit->interface->name);
738 }
d62a17ae 739 goto out;
f390d2c7 740 }
eb5d44eb 741
41415888 742 iih.v4_usable = (fabricd_ip_addrs(circuit)
0c1bd758 743 && iih.tlvs->ipv4_address.count);
d62a17ae 744
0c1bd758
CF
745 iih.v6_usable = (circuit->ipv6_link && listcount(circuit->ipv6_link)
746 && iih.tlvs->ipv6_address.count);
28a8cfcb 747
c9b0e434
CF
748 if (!iih.v4_usable && !iih.v6_usable) {
749 if (isis->debugs & DEBUG_ADJ_PACKETS) {
750 zlog_warn(
751 "ISIS-Adj (%s): Neither IPv4 nor IPv6 considered usable. Ignoring IIH",
752 circuit->area->area_tag);
753 }
af6911c9
EDP
754#ifndef FABRICD
755 isis_notif_reject_adjacency(
756 circuit, "Neither IPv4 not IPv6 considered usable",
757 raw_pdu);
758#endif /* ifndef FABRICD */
d62a17ae 759 goto out;
c9b0e434 760 }
d8fba7d9 761
0c1bd758 762 retval = p2p_hello ? process_p2p_hello(&iih) : process_lan_hello(&iih);
f390d2c7 763out:
0c1bd758 764 isis_free_tlvs(iih.tlvs);
eb5d44eb 765
d62a17ae 766 return retval;
eb5d44eb 767}
768
a6b60da9
CF
769static void lsp_flood_or_update(struct isis_lsp *lsp,
770 struct isis_circuit *circuit,
771 bool circuit_scoped)
772{
89cdc4df 773 if (!circuit_scoped)
a6b60da9 774 lsp_flood(lsp, circuit);
89cdc4df 775 else
a6b60da9 776 fabricd_update_lsp_no_flood(lsp, circuit);
a6b60da9
CF
777}
778
eb5d44eb 779/*
780 * Process Level 1/2 Link State
781 * ISO - 10589
782 * Section 7.3.15.1 - Action on receipt of a link state PDU
f390d2c7 783 */
af8ac8f9 784static int process_lsp(uint8_t pdu_type, struct isis_circuit *circuit,
9b39405f 785 const uint8_t *ssnpa, uint8_t max_area_addrs)
eb5d44eb 786{
1cbd5b37
CF
787 int level;
788 bool circuit_scoped;
3380c990
EDP
789 size_t pdu_start = stream_get_getp(circuit->rcv_stream);
790 size_t pdu_end = stream_get_endp(circuit->rcv_stream);
791 char raw_pdu[pdu_end - pdu_start];
792
793 stream_get_from(raw_pdu, circuit->rcv_stream, pdu_start,
794 pdu_end - pdu_start);
1cbd5b37
CF
795
796 if (pdu_type == FS_LINK_STATE) {
797 if (!fabricd)
798 return ISIS_ERROR;
799 if (max_area_addrs != L2_CIRCUIT_FLOODING_SCOPE)
800 return ISIS_ERROR;
801 level = ISIS_LEVEL2;
802 circuit_scoped = true;
803
804 /* The stream is used verbatim for sending out new LSPDUs.
805 * So make sure we store it as an L2 LSPDU internally.
806 * (compare for the reverse in `send_lsp`) */
807 stream_putc_at(circuit->rcv_stream, 4, L2_LINK_STATE);
808 stream_putc_at(circuit->rcv_stream, 7, 0);
809 } else {
810 if (pdu_type == L1_LINK_STATE)
811 level = ISIS_LEVEL1;
812 else
813 level = ISIS_LEVEL2;
814 circuit_scoped = false;
815 }
d62a17ae 816
817 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
818 zlog_debug(
1cbd5b37
CF
819 "ISIS-Upd (%s): Rcvd %sL%d LSP on %s, cirType %s, cirID %u",
820 circuit->area->area_tag,
821 circuit_scoped ? "Circuit scoped " : "", level,
d62a17ae 822 circuit->interface->name,
823 circuit_t2string(circuit->is_type),
824 circuit->circuit_id);
825 if (isis->debugs & DEBUG_PACKET_DUMP)
826 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
827 stream_get_endp(circuit->rcv_stream));
828 }
f390d2c7 829
3e300703 830 struct isis_lsp_hdr hdr = {};
eb5d44eb 831
af8ac8f9
CF
832 hdr.pdu_len = stream_getw(circuit->rcv_stream);
833 hdr.rem_lifetime = stream_getw(circuit->rcv_stream);
834 stream_get(hdr.lsp_id, circuit->rcv_stream, sizeof(hdr.lsp_id));
835 hdr.seqno = stream_getl(circuit->rcv_stream);
836 hdr.checksum = stream_getw(circuit->rcv_stream);
837 hdr.lsp_bits = stream_getc(circuit->rcv_stream);
eb5d44eb 838
af8ac8f9
CF
839 if (pdu_len_validate(hdr.pdu_len, circuit)) {
840 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP length %" PRIu16,
841 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
842 hdr.pdu_len);
d62a17ae 843 return ISIS_WARNING;
f390d2c7 844 }
d62a17ae 845
d62a17ae 846 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
af8ac8f9
CF
847 zlog_debug("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08" PRIx32
848 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
849 "s, len %" PRIu16 ", on %s",
850 circuit->area->area_tag, level,
851 rawlspid_print(hdr.lsp_id), hdr.seqno, hdr.checksum,
852 hdr.rem_lifetime, hdr.pdu_len,
853 circuit->interface->name);
f390d2c7 854 }
d62a17ae 855
856 /* lsp is_type check */
af8ac8f9
CF
857 if ((hdr.lsp_bits & IS_LEVEL_1) != IS_LEVEL_1) {
858 zlog_debug(
859 "ISIS-Upd (%s): LSP %s invalid LSP is type 0x%" PRIx8,
860 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
861 hdr.lsp_bits & IS_LEVEL_1_AND_2);
d62a17ae 862 /* continue as per RFC1122 Be liberal in what you accept, and
863 * conservative in what you send */
f390d2c7 864 }
d62a17ae 865
866 /* Checksum sanity check - FIXME: move to correct place */
867 /* 12 = sysid+pdu+remtime */
af8ac8f9
CF
868 if (iso_csum_verify(STREAM_DATA(circuit->rcv_stream) + 12,
869 hdr.pdu_len - 12, hdr.checksum, 12)) {
870 zlog_debug(
871 "ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04" PRIx16,
872 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
873 hdr.checksum);
d62a17ae 874 return ISIS_WARNING;
f390d2c7 875 }
3f045a08 876
d62a17ae 877 /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
878 if (circuit->ext_domain) {
879 zlog_debug(
880 "ISIS-Upd (%s): LSP %s received at level %d over circuit with "
881 "externalDomain = true",
af8ac8f9 882 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 883 level);
d62a17ae 884 return ISIS_WARNING;
885 }
eb5d44eb 886
d62a17ae 887 /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 888 if (!(circuit->is_type & level)) {
d62a17ae 889 zlog_debug(
890 "ISIS-Upd (%s): LSP %s received at level %d over circuit of"
891 " type %s",
af8ac8f9 892 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 893 level, circuit_t2string(circuit->is_type));
d62a17ae 894 return ISIS_WARNING;
895 }
0250758d 896
af8ac8f9
CF
897 struct isis_tlvs *tlvs = NULL;
898 int retval = ISIS_WARNING;
899 const char *error_log;
900
901 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
902 circuit->rcv_stream, &tlvs, &error_log)) {
903 zlog_warn("Something went wrong unpacking the LSP: %s",
904 error_log);
905 goto out;
906 }
907
d62a17ae 908 /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
909
910 /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use
911 * 3 */
912
913 /* 7.3.15.1 a) 7 - password check */
af8ac8f9
CF
914 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
915 ? &circuit->area->area_passwd
916 : &circuit->area->domain_passwd;
3380c990
EDP
917 int auth_code = isis_tlvs_auth_is_valid(tlvs, passwd,
918 circuit->rcv_stream, true);
919 if (auth_code != ISIS_AUTH_OK) {
af8ac8f9
CF
920 isis_event_auth_failure(circuit->area->area_tag,
921 "LSP authentication failure",
922 hdr.lsp_id);
3380c990
EDP
923#ifndef FABRICD
924 /* send northbound notification */
925 if (auth_code == ISIS_AUTH_FAILURE)
926 isis_notif_authentication_failure(circuit, raw_pdu);
927 else /* AUTH_TYPE_FAILURE or NO_VALIDATOR */
928 isis_notif_authentication_type_failure(circuit,
929 raw_pdu);
930#endif /* ifndef FABRICD */
af8ac8f9 931 goto out;
f390d2c7 932 }
af8ac8f9 933
d62a17ae 934 /* Find the LSP in our database and compare it to this Link State header
935 */
af8ac8f9
CF
936 struct isis_lsp *lsp =
937 lsp_search(hdr.lsp_id, circuit->area->lspdb[level - 1]);
938 int comp = 0;
d62a17ae 939 if (lsp)
af8ac8f9
CF
940 comp = lsp_compare(circuit->area->area_tag, lsp, hdr.seqno,
941 hdr.checksum, hdr.rem_lifetime);
d62a17ae 942 if (lsp && (lsp->own_lsp))
943 goto dontcheckadj;
944
945 /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same
946 * level */
947 /* for broadcast circuits, snpa should be compared */
948
949 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
af8ac8f9
CF
950 if (!isis_adj_lookup_snpa(ssnpa,
951 circuit->u.bc.adjdb[level - 1])) {
952 zlog_debug("(%s): DS ======= LSP %s, seq 0x%08" PRIx32
953 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
954 "s on %s",
955 circuit->area->area_tag,
956 rawlspid_print(hdr.lsp_id), hdr.seqno,
957 hdr.checksum, hdr.rem_lifetime,
958 circuit->interface->name);
959 goto out; /* Silently discard */
d62a17ae 960 }
961 }
962 /* for non broadcast, we just need to find same level adj */
963 else {
964 /* If no adj, or no sharing of level */
965 if (!circuit->u.p2p.neighbor) {
af8ac8f9
CF
966 retval = ISIS_OK;
967 goto out;
d62a17ae 968 } else {
969 if (((level == IS_LEVEL_1)
970 && (circuit->u.p2p.neighbor->adj_usage
971 == ISIS_ADJ_LEVEL2))
972 || ((level == IS_LEVEL_2)
973 && (circuit->u.p2p.neighbor->adj_usage
974 == ISIS_ADJ_LEVEL1)))
af8ac8f9 975 goto out;
f390d2c7 976 }
f390d2c7 977 }
f390d2c7 978
af8ac8f9
CF
979 bool lsp_confusion;
980
d62a17ae 981dontcheckadj:
982 /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
983
984 /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
985
986 /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do
987 * it */
988
989 /* 7.3.16.2 - If this is an LSP from another IS with identical seq_num
990 * but
991 * wrong checksum, initiate a purge. */
af8ac8f9 992 if (lsp && (lsp->hdr.seqno == hdr.seqno)
2c92bee4
CF
993 && (lsp->hdr.checksum != hdr.checksum)
994 && hdr.rem_lifetime) {
af8ac8f9
CF
995 zlog_warn("ISIS-Upd (%s): LSP %s seq 0x%08" PRIx32
996 " with confused checksum received.",
997 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
998 hdr.seqno);
999 hdr.rem_lifetime = 0;
1000 lsp_confusion = true;
d62a17ae 1001 } else
af8ac8f9 1002 lsp_confusion = false;
d62a17ae 1003
1004 /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
af8ac8f9 1005 if (hdr.rem_lifetime == 0) {
d62a17ae 1006 if (!lsp) {
1007 /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't
1008 * save */
1009 /* only needed on explicit update, eg - p2p */
1010 if (circuit->circ_type == CIRCUIT_T_P2P)
af8ac8f9
CF
1011 ack_lsp(&hdr, circuit, level);
1012 goto out; /* FIXME: do we need a purge? */
d62a17ae 1013 } else {
af8ac8f9 1014 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN)) {
d62a17ae 1015 /* LSP by some other system -> do 7.3.16.4 b) */
1016 /* 7.3.16.4 b) 1) */
1017 if (comp == LSP_NEWER) {
af8ac8f9
CF
1018 lsp_update(lsp, &hdr, tlvs,
1019 circuit->rcv_stream,
164066e4
CF
1020 circuit->area, level,
1021 lsp_confusion);
af8ac8f9 1022 tlvs = NULL;
d62a17ae 1023 /* ii */
a6b60da9
CF
1024 lsp_flood_or_update(lsp, NULL,
1025 circuit_scoped);
d62a17ae 1026 /* v */
1027 ISIS_FLAGS_CLEAR_ALL(
996c9314
LB
1028 lsp->SSNflags); /* FIXME:
1029 OTHER
1030 than c
1031 */
d62a17ae 1032
1033 /* For the case of lsp confusion, flood
1034 * the purge back to its
1035 * originator so that it can react.
1036 * Otherwise, don't reflood
1037 * through incoming circuit as usual */
1038 if (!lsp_confusion) {
9b39405f
CF
1039 isis_tx_queue_del(
1040 circuit->tx_queue,
1041 lsp);
1042
d62a17ae 1043 /* iv */
1044 if (circuit->circ_type
1045 != CIRCUIT_T_BROADCAST)
1046 ISIS_SET_FLAG(
1047 lsp->SSNflags,
1048 circuit);
1049 }
1050 } /* 7.3.16.4 b) 2) */
1051 else if (comp == LSP_EQUAL) {
1052 /* i */
9b39405f
CF
1053 isis_tx_queue_del(circuit->tx_queue,
1054 lsp);
d62a17ae 1055 /* ii */
1056 if (circuit->circ_type
1057 != CIRCUIT_T_BROADCAST)
1058 ISIS_SET_FLAG(lsp->SSNflags,
1059 circuit);
1060 } /* 7.3.16.4 b) 3) */
1061 else {
9b39405f
CF
1062 isis_tx_queue_add(circuit->tx_queue,
1063 lsp, TX_LSP_NORMAL);
d62a17ae 1064 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1065 }
af8ac8f9 1066 } else if (lsp->hdr.rem_lifetime != 0) {
d62a17ae 1067 /* our own LSP -> 7.3.16.4 c) */
1068 if (comp == LSP_NEWER) {
af8ac8f9 1069 lsp_inc_seqno(lsp, hdr.seqno);
89cdc4df
RM
1070 lsp_flood_or_update(lsp, NULL,
1071 circuit_scoped);
d62a17ae 1072 } else {
9b39405f
CF
1073 isis_tx_queue_add(circuit->tx_queue,
1074 lsp, TX_LSP_NORMAL);
d62a17ae 1075 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1076 }
1077 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1078 zlog_debug(
89cdc4df
RM
1079 "ISIS-Upd (%s): (1) "
1080 "re-originating LSP %s new seq "
1081 "0x%08" PRIx32,
d62a17ae 1082 circuit->area->area_tag,
af8ac8f9
CF
1083 rawlspid_print(hdr.lsp_id),
1084 lsp->hdr.seqno);
d62a17ae 1085 }
f390d2c7 1086 }
af8ac8f9 1087 goto out;
f390d2c7 1088 }
d62a17ae 1089 /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
1090 * purge */
af8ac8f9 1091 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0) {
d62a17ae 1092 if (!lsp) {
1093 /* 7.3.16.4: initiate a purge */
af8ac8f9
CF
1094 lsp_purge_non_exist(level, &hdr, circuit->area);
1095 retval = ISIS_OK;
1096 goto out;
d62a17ae 1097 }
1098 /* 7.3.15.1 d) - If this is our own lsp and we have it */
1099
1100 /* In 7.3.16.1, If an Intermediate system R somewhere in the
1101 * domain
1102 * has information that the current sequence number for source S
1103 * is
1104 * "greater" than that held by S, ... */
1105
562d7ef7 1106 if (comp == LSP_NEWER) {
d62a17ae 1107 /* 7.3.16.1 */
af8ac8f9 1108 lsp_inc_seqno(lsp, hdr.seqno);
562d7ef7 1109 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
d62a17ae 1110 zlog_debug(
af8ac8f9 1111 "ISIS-Upd (%s): (2) re-originating LSP %s new seq 0x%08" PRIx32,
d62a17ae 1112 circuit->area->area_tag,
af8ac8f9
CF
1113 rawlspid_print(hdr.lsp_id),
1114 lsp->hdr.seqno);
562d7ef7
CF
1115 }
1116 lsp_flood(lsp, NULL);
1117 } else if (comp == LSP_EQUAL) {
1118 isis_tx_queue_del(circuit->tx_queue, lsp);
1119 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1120 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1121 } else {
1122 isis_tx_queue_add(circuit->tx_queue, lsp,
1123 TX_LSP_NORMAL);
1124 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
d62a17ae 1125 }
d62a17ae 1126 } else {
1127 /* 7.3.15.1 e) - This lsp originated on another system */
1128
1129 /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db
1130 */
1131 if ((!lsp || comp == LSP_NEWER)) {
1132 /*
1133 * If this lsp is a frag, need to see if we have zero
1134 * lsp present
1135 */
af8ac8f9
CF
1136 struct isis_lsp *lsp0 = NULL;
1137 if (LSP_FRAGMENT(hdr.lsp_id) != 0) {
1138 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1139 memcpy(lspid, hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
d62a17ae 1140 LSP_FRAGMENT(lspid) = 0;
1141 lsp0 = lsp_search(
1142 lspid, circuit->area->lspdb[level - 1]);
1143 if (!lsp0) {
1144 zlog_debug(
1145 "Got lsp frag, while zero lsp not in database");
df0ba689 1146 goto out;
d62a17ae 1147 }
1148 }
1149 /* i */
1150 if (!lsp) {
af8ac8f9
CF
1151 lsp = lsp_new_from_recv(
1152 &hdr, tlvs, circuit->rcv_stream, lsp0,
d62a17ae 1153 circuit->area, level);
af8ac8f9 1154 tlvs = NULL;
d62a17ae 1155 lsp_insert(lsp,
1156 circuit->area->lspdb[level - 1]);
1157 } else /* exists, so we overwrite */
1158 {
af8ac8f9 1159 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1160 circuit->area, level, false);
af8ac8f9 1161 tlvs = NULL;
d62a17ae 1162 }
a6b60da9 1163 lsp_flood_or_update(lsp, circuit, circuit_scoped);
d62a17ae 1164
1165 /* iv */
1166 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1167 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1168 /* FIXME: v) */
1169 }
1170 /* 7.3.15.1 e) 2) LSP equal to the one in db */
1171 else if (comp == LSP_EQUAL) {
9b39405f 1172 isis_tx_queue_del(circuit->tx_queue, lsp);
af8ac8f9 1173 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1174 circuit->area, level, false);
af8ac8f9 1175 tlvs = NULL;
d62a17ae 1176 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1177 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1178 }
1179 /* 7.3.15.1 e) 3) LSP older than the one in db */
1180 else {
9b39405f
CF
1181 isis_tx_queue_add(circuit->tx_queue, lsp,
1182 TX_LSP_NORMAL);
d62a17ae 1183 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1184 }
f390d2c7 1185 }
af8ac8f9
CF
1186
1187 retval = ISIS_OK;
1188
1189out:
e923107c 1190 fabricd_trigger_csnp(circuit->area, circuit_scoped);
df0ba689 1191
af8ac8f9 1192 isis_free_tlvs(tlvs);
d62a17ae 1193 return retval;
eb5d44eb 1194}
1195
1196/*
1197 * Process Sequence Numbers
1198 * ISO - 10589
1199 * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
1200 */
1201
17c9dcd5 1202static int process_snp(uint8_t pdu_type, struct isis_circuit *circuit,
d7c0a89a 1203 const uint8_t *ssnpa)
eb5d44eb 1204{
3380c990
EDP
1205#ifndef FABRICD
1206 size_t pdu_start = stream_get_getp(circuit->rcv_stream);
1207 size_t pdu_end = stream_get_endp(circuit->rcv_stream);
1208 char raw_pdu[pdu_end - pdu_start];
1209#endif /* ifndef FABRICD */
1210
17c9dcd5
CF
1211 bool is_csnp = (pdu_type == L1_COMPLETE_SEQ_NUM
1212 || pdu_type == L2_COMPLETE_SEQ_NUM);
1213 char typechar = is_csnp ? 'C' : 'P';
1214 int level = (pdu_type == L1_COMPLETE_SEQ_NUM
1215 || pdu_type == L1_PARTIAL_SEQ_NUM)
1216 ? ISIS_LEVEL1
1217 : ISIS_LEVEL2;
1218
1219 uint16_t pdu_len = stream_getw(circuit->rcv_stream);
1220 uint8_t rem_sys_id[ISIS_SYS_ID_LEN];
3380c990 1221
17c9dcd5
CF
1222 stream_get(rem_sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
1223 stream_forward_getp(circuit->rcv_stream, 1); /* Circuit ID - unused */
1224
3e300703
DL
1225 uint8_t start_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1226 uint8_t stop_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
17c9dcd5
CF
1227
1228 if (is_csnp) {
1229 stream_get(start_lsp_id, circuit->rcv_stream,
1230 ISIS_SYS_ID_LEN + 2);
1231 stream_get(stop_lsp_id, circuit->rcv_stream,
1232 ISIS_SYS_ID_LEN + 2);
1233 }
1234
1235 if (pdu_len_validate(pdu_len, circuit)) {
1236 zlog_warn("Received a CSNP with bogus length %d", pdu_len);
1237 return ISIS_WARNING;
f390d2c7 1238 }
eb5d44eb 1239
17c9dcd5
CF
1240 if (isis->debugs & DEBUG_SNP_PACKETS) {
1241 zlog_debug(
1242 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, cirType %s, cirID %u",
1243 circuit->area->area_tag, level, typechar,
1244 circuit->interface->name,
1245 circuit_t2string(circuit->is_type),
1246 circuit->circuit_id);
1247 if (isis->debugs & DEBUG_PACKET_DUMP)
1248 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
1249 stream_get_endp(circuit->rcv_stream));
1250 }
eb5d44eb 1251
d62a17ae 1252 /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
1253 if (circuit->ext_domain) {
f390d2c7 1254
d62a17ae 1255 zlog_debug(
1256 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1257 "skipping: circuit externalDomain = true",
1258 circuit->area->area_tag, level, typechar,
1259 circuit->interface->name);
f390d2c7 1260
d62a17ae 1261 return ISIS_OK;
1262 }
eb5d44eb 1263
d62a17ae 1264 /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 1265 if (!(circuit->is_type & level)) {
d62a17ae 1266 zlog_debug(
1267 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1268 "skipping: circuit type %s does not match level %d",
1269 circuit->area->area_tag, level, typechar,
1270 circuit->interface->name,
1271 circuit_t2string(circuit->is_type), level);
eb5d44eb 1272
d62a17ae 1273 return ISIS_OK;
f390d2c7 1274 }
eb5d44eb 1275
d62a17ae 1276 /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
17c9dcd5
CF
1277 if (!is_csnp && (circuit->circ_type == CIRCUIT_T_BROADCAST)
1278 && !circuit->u.bc.is_dr[level - 1]) {
d62a17ae 1279 zlog_debug(
1280 "ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
1281 "skipping: we are not the DIS",
1282 circuit->area->area_tag, level, typechar,
1283 snpa_print(ssnpa), circuit->interface->name);
1284
1285 return ISIS_OK;
1286 }
eb5d44eb 1287
d62a17ae 1288 /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked
1289 */
1290
1291 /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use
1292 * 3
1293 * - already checked */
1294
1295 /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same
1296 * level */
1297 /* for broadcast circuits, snpa should be compared */
1298 /* FIXME : Do we need to check SNPA? */
1299 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
17c9dcd5
CF
1300 if (!isis_adj_lookup(rem_sys_id,
1301 circuit->u.bc.adjdb[level - 1]))
d62a17ae 1302 return ISIS_OK; /* Silently discard */
1303 } else {
8e6fb83b 1304 if (!fabricd && !circuit->u.p2p.neighbor) {
d62a17ae 1305 zlog_warn("no p2p neighbor on circuit %s",
1306 circuit->interface->name);
1307 return ISIS_OK; /* Silently discard */
1308 }
1309 }
eb5d44eb 1310
17c9dcd5
CF
1311 struct isis_tlvs *tlvs;
1312 int retval = ISIS_WARNING;
1313 const char *error_log;
eb5d44eb 1314
17c9dcd5
CF
1315 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
1316 circuit->rcv_stream, &tlvs, &error_log)) {
1317 zlog_warn("Something went wrong unpacking the SNP: %s",
1318 error_log);
1319 goto out;
f390d2c7 1320 }
eb5d44eb 1321
17c9dcd5
CF
1322 struct isis_passwd *passwd = (level == IS_LEVEL_1)
1323 ? &circuit->area->area_passwd
1324 : &circuit->area->domain_passwd;
3380c990
EDP
1325 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV)) {
1326 int auth_code = isis_tlvs_auth_is_valid(
1327 tlvs, passwd, circuit->rcv_stream, false);
1328 if (auth_code != ISIS_AUTH_OK) {
1329 isis_event_auth_failure(circuit->area->area_tag,
1330 "SNP authentication failure",
1331 rem_sys_id);
1332#ifndef FABRICD
1333 /* send northbound notification */
1334 stream_get_from(raw_pdu, circuit->rcv_stream, pdu_start,
1335 pdu_end - pdu_start);
1336 if (auth_code == ISIS_AUTH_FAILURE)
1337 isis_notif_authentication_failure(circuit,
1338 raw_pdu);
1339 else /* AUTH_TYPE_FAILURE or NO_VALIDATOR */
1340 isis_notif_authentication_type_failure(circuit,
1341 raw_pdu);
1342#endif /* ifndef FABRICD */
1343 goto out;
1344 }
d62a17ae 1345 }
eb5d44eb 1346
17c9dcd5
CF
1347 struct isis_lsp_entry *entry_head =
1348 (struct isis_lsp_entry *)tlvs->lsp_entries.head;
1349
d62a17ae 1350 /* debug isis snp-packets */
1351 if (isis->debugs & DEBUG_SNP_PACKETS) {
1352 zlog_debug("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
1353 circuit->area->area_tag, level, typechar,
1354 snpa_print(ssnpa), circuit->interface->name);
17c9dcd5
CF
1355 for (struct isis_lsp_entry *entry = entry_head; entry;
1356 entry = entry->next) {
1357 zlog_debug(
1358 "ISIS-Snp (%s): %cSNP entry %s, seq 0x%08" PRIx32
98c5bc15 1359 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
17c9dcd5
CF
1360 circuit->area->area_tag, typechar,
1361 rawlspid_print(entry->id), entry->seqno,
1362 entry->checksum, entry->rem_lifetime);
f390d2c7 1363 }
f390d2c7 1364 }
3f045a08 1365
df0ba689
CF
1366 bool resync_needed = false;
1367
d62a17ae 1368 /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
17c9dcd5
CF
1369 for (struct isis_lsp_entry *entry = entry_head; entry;
1370 entry = entry->next) {
1371 struct isis_lsp *lsp =
1372 lsp_search(entry->id, circuit->area->lspdb[level - 1]);
1373 bool own_lsp = !memcmp(entry->id, isis->sysid, ISIS_SYS_ID_LEN);
1374 if (lsp) {
1375 /* 7.3.15.2 b) 1) is this LSP newer */
1376 int cmp = lsp_compare(circuit->area->area_tag, lsp,
1377 entry->seqno, entry->checksum,
1378 entry->rem_lifetime);
1379 /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
1380 if (cmp == LSP_EQUAL) {
1381 /* if (circuit->circ_type !=
1382 * CIRCUIT_T_BROADCAST) */
9b39405f 1383 isis_tx_queue_del(circuit->tx_queue, lsp);
17c9dcd5
CF
1384 }
1385 /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM
1386 */
1387 else if (cmp == LSP_OLDER) {
1388 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
9b39405f
CF
1389 isis_tx_queue_add(circuit->tx_queue, lsp,
1390 TX_LSP_NORMAL);
17c9dcd5
CF
1391 }
1392 /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM
1393 on p2p */
1394 else {
1395 if (own_lsp) {
af8ac8f9 1396 lsp_inc_seqno(lsp, entry->seqno);
9b39405f
CF
1397 isis_tx_queue_add(circuit->tx_queue, lsp,
1398 TX_LSP_NORMAL);
17c9dcd5
CF
1399 } else {
1400 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1401 /* if (circuit->circ_type !=
1402 * CIRCUIT_T_BROADCAST) */
9b39405f 1403 isis_tx_queue_del(circuit->tx_queue, lsp);
df0ba689 1404 resync_needed = true;
d62a17ae 1405 }
17c9dcd5
CF
1406 }
1407 } else {
1408 /* 7.3.15.2 b) 5) if it was not found, and all of those
1409 * are not 0,
1410 * insert it and set SSN on it */
1411 if (entry->rem_lifetime && entry->checksum
1412 && entry->seqno && memcmp(entry->id, isis->sysid,
1413 ISIS_SYS_ID_LEN)) {
8f5dbe18
CF
1414 struct isis_lsp *lsp0 = NULL;
1415
1416 if (LSP_FRAGMENT(entry->id)) {
1417 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1418
1419 memcpy(lspid, entry->id,
1420 ISIS_SYS_ID_LEN + 1);
1421 LSP_FRAGMENT(lspid) = 0;
1422 lsp0 = lsp_search(
98c5bc15
CF
1423 lspid,
1424 circuit->area->lspdb[level - 1]);
8f5dbe18 1425 if (!lsp0) {
98c5bc15 1426 zlog_debug("Got lsp frag in snp, while zero not in database");
8f5dbe18
CF
1427 continue;
1428 }
1429 }
f7813c7c 1430 lsp = lsp_new(circuit->area, entry->id,
17c9dcd5 1431 entry->rem_lifetime, 0, 0,
8f5dbe18 1432 entry->checksum, lsp0, level);
17c9dcd5
CF
1433 lsp_insert(lsp,
1434 circuit->area->lspdb[level - 1]);
9b39405f
CF
1435
1436 lsp_set_all_srmflags(lsp, false);
17c9dcd5 1437 ISIS_SET_FLAG(lsp->SSNflags, circuit);
df0ba689 1438 resync_needed = true;
d62a17ae 1439 }
1440 }
1441 }
eb5d44eb 1442
d62a17ae 1443 /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported
1444 */
17c9dcd5 1445 if (is_csnp) {
d62a17ae 1446 /*
1447 * Build a list from our own LSP db bounded with
1448 * start_lsp_id and stop_lsp_id
1449 */
17c9dcd5
CF
1450 struct list *lsp_list = list_new();
1451 lsp_build_list_nonzero_ht(start_lsp_id, stop_lsp_id, lsp_list,
d62a17ae 1452 circuit->area->lspdb[level - 1]);
1453
1454 /* Fixme: Find a better solution */
17c9dcd5
CF
1455 struct listnode *node, *nnode;
1456 struct isis_lsp *lsp;
1457 for (struct isis_lsp_entry *entry = entry_head; entry;
1458 entry = entry->next) {
1459 for (ALL_LIST_ELEMENTS(lsp_list, node, nnode, lsp)) {
af8ac8f9 1460 if (lsp_id_cmp(lsp->hdr.lsp_id, entry->id)
17c9dcd5
CF
1461 == 0) {
1462 list_delete_node(lsp_list, node);
1463 break;
d62a17ae 1464 }
1465 }
1466 }
17c9dcd5 1467
d62a17ae 1468 /* on remaining LSPs we set SRM (neighbor knew not of) */
9b39405f
CF
1469 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
1470 isis_tx_queue_add(circuit->tx_queue, lsp, TX_LSP_NORMAL);
df0ba689 1471 resync_needed = true;
9b39405f
CF
1472 }
1473
d62a17ae 1474 /* lets free it */
6a154c88 1475 list_delete(&lsp_list);
d62a17ae 1476 }
1477
df0ba689
CF
1478 if (fabricd_initial_sync_is_complete(circuit->area) && resync_needed)
1479 zlog_warn("OpenFabric: Needed to resync LSPDB using CSNP!\n");
1480
17c9dcd5
CF
1481 retval = ISIS_OK;
1482out:
1483 isis_free_tlvs(tlvs);
d62a17ae 1484 return retval;
eb5d44eb 1485}
1486
88f9d911
CF
1487static int pdu_size(uint8_t pdu_type, uint8_t *size)
1488{
1489 switch (pdu_type) {
1490 case L1_LAN_HELLO:
1491 case L2_LAN_HELLO:
1492 *size = ISIS_LANHELLO_HDRLEN;
1493 break;
1494 case P2P_HELLO:
1495 *size = ISIS_P2PHELLO_HDRLEN;
1496 break;
1497 case L1_LINK_STATE:
1498 case L2_LINK_STATE:
1cbd5b37 1499 case FS_LINK_STATE:
88f9d911
CF
1500 *size = ISIS_LSP_HDR_LEN;
1501 break;
1502 case L1_COMPLETE_SEQ_NUM:
1503 case L2_COMPLETE_SEQ_NUM:
1504 *size = ISIS_CSNP_HDRLEN;
1505 break;
1506 case L1_PARTIAL_SEQ_NUM:
1507 case L2_PARTIAL_SEQ_NUM:
1508 *size = ISIS_PSNP_HDRLEN;
1509 break;
1510 default:
1511 return 1;
1512 }
1513 *size += ISIS_FIXED_HDR_LEN;
1514 return 0;
1515}
1516
eb5d44eb 1517/*
1518 * PDU Dispatcher
1519 */
1520
d7c0a89a 1521int isis_handle_pdu(struct isis_circuit *circuit, uint8_t *ssnpa)
eb5d44eb 1522{
d62a17ae 1523 int retval = ISIS_OK;
2ab5a2d1
EDP
1524 size_t pdu_start = stream_get_getp(circuit->rcv_stream);
1525 size_t pdu_end = stream_get_endp(circuit->rcv_stream);
1526 char raw_pdu[pdu_end - pdu_start];
1527
1528 stream_get_from(raw_pdu, circuit->rcv_stream, pdu_start,
1529 pdu_end - pdu_start);
eb5d44eb 1530
88f9d911
CF
1531 /* Verify that at least the 8 bytes fixed header have been received */
1532 if (stream_get_endp(circuit->rcv_stream) < ISIS_FIXED_HDR_LEN) {
1a7ecb96 1533 flog_err(EC_ISIS_PACKET, "PDU is too short to be IS-IS.");
88f9d911
CF
1534 return ISIS_ERROR;
1535 }
eb5d44eb 1536
88f9d911
CF
1537 uint8_t idrp = stream_getc(circuit->rcv_stream);
1538 uint8_t length = stream_getc(circuit->rcv_stream);
1539 uint8_t version1 = stream_getc(circuit->rcv_stream);
1540 uint8_t id_len = stream_getc(circuit->rcv_stream);
1541 uint8_t pdu_type = stream_getc(circuit->rcv_stream)
1542 & 0x1f; /* bits 6-8 are reserved */
1543 uint8_t version2 = stream_getc(circuit->rcv_stream);
2ab5a2d1 1544
88f9d911
CF
1545 stream_forward_getp(circuit->rcv_stream, 1); /* reserved */
1546 uint8_t max_area_addrs = stream_getc(circuit->rcv_stream);
1547
39bb53d6
CF
1548 pdu_counter_count(circuit->area->pdu_rx_counters, pdu_type);
1549
88f9d911 1550 if (idrp == ISO9542_ESIS) {
450971aa 1551 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 1552 "No support for ES-IS packet IDRP=%" PRIx8, idrp);
d62a17ae 1553 return ISIS_ERROR;
1554 }
eb5d44eb 1555
88f9d911 1556 if (idrp != ISO10589_ISIS) {
1a7ecb96 1557 flog_err(EC_ISIS_PACKET, "Not an IS-IS packet IDRP=%" PRIx8,
1c50c1c0 1558 idrp);
d62a17ae 1559 return ISIS_ERROR;
1560 }
3f045a08 1561
88f9d911
CF
1562 if (version1 != 1) {
1563 zlog_warn("Unsupported ISIS version %" PRIu8, version1);
1564 return ISIS_WARNING;
1565 }
eb5d44eb 1566
88f9d911 1567 if (id_len != 0 && id_len != ISIS_SYS_ID_LEN) {
af4c2728 1568 flog_err(
1a7ecb96 1569 EC_ISIS_PACKET,
88f9d911
CF
1570 "IDFieldLengthMismatch: ID Length field in a received PDU %" PRIu8
1571 ", while the parameter for this IS is %u",
1572 id_len, ISIS_SYS_ID_LEN);
d62a17ae 1573 return ISIS_ERROR;
1574 }
eb5d44eb 1575
88f9d911
CF
1576 uint8_t expected_length;
1577 if (pdu_size(pdu_type, &expected_length)) {
1578 zlog_warn("Unsupported ISIS PDU %" PRIu8, pdu_type);
d62a17ae 1579 return ISIS_WARNING;
1580 }
88f9d911
CF
1581
1582 if (length != expected_length) {
1a7ecb96 1583 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
1584 "Exepected fixed header length = %" PRIu8
1585 " but got %" PRIu8,
1586 expected_length, length);
88f9d911
CF
1587 return ISIS_ERROR;
1588 }
1589
1590 if (stream_get_endp(circuit->rcv_stream) < length) {
af4c2728 1591 flog_err(
1a7ecb96 1592 EC_ISIS_PACKET,
88f9d911 1593 "PDU is too short to contain fixed header of given PDU type.");
d62a17ae 1594 return ISIS_ERROR;
1595 }
eb5d44eb 1596
88f9d911
CF
1597 if (version2 != 1) {
1598 zlog_warn("Unsupported ISIS PDU version %" PRIu8, version2);
d62a17ae 1599 return ISIS_WARNING;
1600 }
3f045a08 1601
d62a17ae 1602 if (circuit->is_passive) {
1603 zlog_warn("Received ISIS PDU on passive circuit %s",
1604 circuit->interface->name);
1605 return ISIS_WARNING;
1606 }
3f045a08 1607
d62a17ae 1608 /* either 3 or 0 */
1cbd5b37
CF
1609 if (pdu_type != FS_LINK_STATE /* FS PDU doesn't contain max area addr field */
1610 && max_area_addrs != 0
1611 && max_area_addrs != isis->max_area_addrs) {
af4c2728 1612 flog_err(
1a7ecb96 1613 EC_ISIS_PACKET,
88f9d911
CF
1614 "maximumAreaAddressesMismatch: maximumAreaAdresses in a received PDU %" PRIu8
1615 " while the parameter for this IS is %u",
1616 max_area_addrs, isis->max_area_addrs);
2ab5a2d1
EDP
1617#ifndef FABRICD
1618 /* send northbound notification */
1619 isis_notif_max_area_addr_mismatch(circuit, max_area_addrs,
1620 raw_pdu);
1621#endif /* ifndef FABRICD */
d62a17ae 1622 return ISIS_ERROR;
1623 }
eb5d44eb 1624
88f9d911 1625 switch (pdu_type) {
d62a17ae 1626 case L1_LAN_HELLO:
d62a17ae 1627 case L2_LAN_HELLO:
d62a17ae 1628 case P2P_HELLO:
a2d41bb0
CF
1629 if (fabricd && pdu_type != P2P_HELLO)
1630 return ISIS_ERROR;
0c1bd758 1631 retval = process_hello(pdu_type, circuit, ssnpa);
d62a17ae 1632 break;
1633 case L1_LINK_STATE:
d62a17ae 1634 case L2_LINK_STATE:
1cbd5b37 1635 case FS_LINK_STATE:
a2d41bb0
CF
1636 if (fabricd
1637 && pdu_type != L2_LINK_STATE
1638 && pdu_type != FS_LINK_STATE)
1639 return ISIS_ERROR;
9b39405f 1640 retval = process_lsp(pdu_type, circuit, ssnpa, max_area_addrs);
d62a17ae 1641 break;
1642 case L1_COMPLETE_SEQ_NUM:
d62a17ae 1643 case L2_COMPLETE_SEQ_NUM:
d62a17ae 1644 case L1_PARTIAL_SEQ_NUM:
d62a17ae 1645 case L2_PARTIAL_SEQ_NUM:
17c9dcd5 1646 retval = process_snp(pdu_type, circuit, ssnpa);
d62a17ae 1647 break;
1648 default:
1649 return ISIS_ERROR;
1650 }
eb5d44eb 1651
d62a17ae 1652 return retval;
eb5d44eb 1653}
1654
d62a17ae 1655int isis_receive(struct thread *thread)
eb5d44eb 1656{
d62a17ae 1657 struct isis_circuit *circuit;
d7c0a89a 1658 uint8_t ssnpa[ETH_ALEN];
d62a17ae 1659 int retval;
eb5d44eb 1660
d62a17ae 1661 /*
1662 * Get the circuit
1663 */
1664 circuit = THREAD_ARG(thread);
1665 assert(circuit);
eb5d44eb 1666
d62a17ae 1667 circuit->t_read = NULL;
eb5d44eb 1668
d62a17ae 1669 isis_circuit_stream(circuit, &circuit->rcv_stream);
eb5d44eb 1670
d62a17ae 1671 retval = circuit->rx(circuit, ssnpa);
eb5d44eb 1672
b9347997 1673#if ISIS_METHOD != ISIS_METHOD_BPF
d62a17ae 1674 if (retval == ISIS_OK)
1675 retval = isis_handle_pdu(circuit, ssnpa);
b9347997 1676#endif //ISIS_METHOD != ISIS_METHOD_BPF
eb5d44eb 1677
d62a17ae 1678 /*
1679 * prepare for next packet.
1680 */
1681 if (!circuit->is_passive)
1682 isis_circuit_prepare(circuit);
eb5d44eb 1683
d62a17ae 1684 return retval;
eb5d44eb 1685}
1686
eb5d44eb 1687/*
d62a17ae 1688 * SEND SIDE
eb5d44eb 1689 */
88f9d911 1690void fill_fixed_hdr(uint8_t pdu_type, struct stream *stream)
eb5d44eb 1691{
88f9d911
CF
1692 uint8_t length;
1693
1694 if (pdu_size(pdu_type, &length))
1695 assert(!"Unknown PDU Type");
1696
1697 stream_putc(stream, ISO10589_ISIS); /* IDRP */
1698 stream_putc(stream, length); /* Length of fixed header */
1699 stream_putc(stream, 1); /* Version/Protocol ID Extension 1 */
1700 stream_putc(stream, 0); /* ID Length, 0 => 6 */
1701 stream_putc(stream, pdu_type);
1702 stream_putc(stream, 1); /* Subversion */
1703 stream_putc(stream, 0); /* Reserved */
1704 stream_putc(stream, 0); /* Max Area Addresses 0 => 3 */
eb5d44eb 1705}
1706
39bb53d6 1707static uint8_t hello_pdu_type(struct isis_circuit *circuit, int level)
eb5d44eb 1708{
d62a17ae 1709 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
39bb53d6 1710 return (level == IS_LEVEL_1) ? L1_LAN_HELLO : L2_LAN_HELLO;
d62a17ae 1711 else
39bb53d6
CF
1712 return P2P_HELLO;
1713}
1714
1715static void put_hello_hdr(struct isis_circuit *circuit, int level,
1716 size_t *len_pointer)
1717{
1718 uint8_t pdu_type = hello_pdu_type(circuit, level);
88f9d911 1719
0c1bd758 1720 isis_circuit_stream(circuit, &circuit->snd_stream);
88f9d911 1721 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 1722
0c1bd758
CF
1723 stream_putc(circuit->snd_stream, circuit->is_type);
1724 stream_put(circuit->snd_stream, circuit->area->isis->sysid,
1725 ISIS_SYS_ID_LEN);
eb5d44eb 1726
0c1bd758
CF
1727 uint32_t holdtime = circuit->hello_multiplier[level - 1]
1728 * circuit->hello_interval[level - 1];
d62a17ae 1729
0c1bd758
CF
1730 if (holdtime > 0xffff)
1731 holdtime = 0xffff;
d62a17ae 1732
0c1bd758
CF
1733 stream_putw(circuit->snd_stream, holdtime);
1734 *len_pointer = stream_get_endp(circuit->snd_stream);
1735 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
d62a17ae 1736
0c1bd758 1737 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
d7c0a89a
QY
1738 uint8_t *desig_is = (level == IS_LEVEL_1)
1739 ? circuit->u.bc.l1_desig_is
1740 : circuit->u.bc.l2_desig_is;
0c1bd758
CF
1741 stream_putc(circuit->snd_stream, circuit->priority[level - 1]);
1742 stream_put(circuit->snd_stream, desig_is, ISIS_SYS_ID_LEN + 1);
1743 } else {
1744 stream_putc(circuit->snd_stream, circuit->circuit_id);
d62a17ae 1745 }
0c1bd758 1746}
eb5d44eb 1747
0c1bd758
CF
1748int send_hello(struct isis_circuit *circuit, int level)
1749{
1750 size_t len_pointer;
1751 int retval;
1752
1753 if (circuit->is_passive)
1754 return ISIS_OK;
1755
1756 if (circuit->interface->mtu == 0) {
1757 zlog_warn("circuit has zero MTU");
d62a17ae 1758 return ISIS_WARNING;
0c1bd758
CF
1759 }
1760
1761 put_hello_hdr(circuit, level, &len_pointer);
1762
1763 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1764
1765 isis_tlvs_add_auth(tlvs, &circuit->passwd);
1766
a3acf37d
DS
1767 if (!listcount(circuit->area->area_addrs)) {
1768 isis_free_tlvs(tlvs);
d62a17ae 1769 return ISIS_WARNING;
a3acf37d
DS
1770 }
1771
0c1bd758 1772 isis_tlvs_add_area_addresses(tlvs, circuit->area->area_addrs);
d62a17ae 1773
42fe2621 1774 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
0c1bd758
CF
1775 isis_tlvs_add_lan_neighbors(
1776 tlvs, circuit->u.bc.lan_neighs[level - 1]);
58e5d748
CF
1777 } else if (circuit->circ_type == CIRCUIT_T_P2P
1778 && !circuit->disable_threeway_adj) {
42fe2621
CF
1779 uint32_t ext_circuit_id = circuit->idx;
1780 if (circuit->u.p2p.neighbor) {
8e6fb83b
CF
1781 uint8_t threeway_state;
1782
1783 if (fabricd_initial_sync_is_in_progress(circuit->area)
1784 && fabricd_initial_sync_circuit(circuit->area) != circuit)
1785 threeway_state = ISIS_THREEWAY_DOWN;
1786 else
1787 threeway_state = circuit->u.p2p.neighbor->threeway_state;
42fe2621 1788 isis_tlvs_add_threeway_adj(tlvs,
8e6fb83b 1789 threeway_state,
42fe2621
CF
1790 ext_circuit_id,
1791 circuit->u.p2p.neighbor->sysid,
1792 circuit->u.p2p.neighbor->ext_circuit_id);
1793 } else {
1794 isis_tlvs_add_threeway_adj(tlvs,
1795 ISIS_THREEWAY_DOWN,
1796 ext_circuit_id,
1797 NULL, 0);
1798 }
1799 }
0c1bd758
CF
1800
1801 isis_tlvs_set_protocols_supported(tlvs, &circuit->nlpids);
d62a17ae 1802
1803 /*
1804 * MT Supported TLV
1805 *
1806 * TLV gets included if no topology is enabled on the interface,
1807 * if one topology other than #0 is enabled, or if multiple topologies
1808 * are enabled.
1809 */
1810 struct isis_circuit_mt_setting **mt_settings;
1811 unsigned int mt_count;
1812
1813 mt_settings = circuit_mt_settings(circuit, &mt_count);
0c1bd758
CF
1814 if (mt_count == 0 && area_is_mt(circuit->area)) {
1815 tlvs->mt_router_info_empty = true;
1816 } else if ((mt_count == 1
1817 && mt_settings[0]->mtid != ISIS_MT_IPV4_UNICAST)
1818 || (mt_count > 1)) {
1819 for (unsigned int i = 0; i < mt_count; i++)
1820 isis_tlvs_add_mt_router_info(tlvs, mt_settings[i]->mtid,
1821 false, false);
d62a17ae 1822 }
eb5d44eb 1823
41415888
CF
1824 if (circuit->ip_router) {
1825 struct list *circuit_ip_addrs = fabricd_ip_addrs(circuit);
1826
1827 if (circuit_ip_addrs)
1828 isis_tlvs_add_ipv4_addresses(tlvs, circuit_ip_addrs);
1829 }
d62a17ae 1830
0c1bd758
CF
1831 if (circuit->ipv6_router && circuit->ipv6_link)
1832 isis_tlvs_add_ipv6_addresses(tlvs, circuit->ipv6_link);
d62a17ae 1833
0c1bd758 1834 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1835 circuit->pad_hellos, false)) {
0c1bd758
CF
1836 isis_free_tlvs(tlvs);
1837 return ISIS_WARNING; /* XXX: Maybe Log TLV structure? */
f390d2c7 1838 }
d62a17ae 1839
1840 if (isis->debugs & DEBUG_ADJ_PACKETS) {
1841 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1842 zlog_debug(
1843 "ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
1844 circuit->area->area_tag, level,
0c1bd758
CF
1845 circuit->interface->name,
1846 stream_get_endp(circuit->snd_stream));
d62a17ae 1847 } else {
1848 zlog_debug(
1849 "ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
1850 circuit->area->area_tag,
0c1bd758
CF
1851 circuit->interface->name,
1852 stream_get_endp(circuit->snd_stream));
d62a17ae 1853 }
1854 if (isis->debugs & DEBUG_PACKET_DUMP)
1855 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
1856 stream_get_endp(circuit->snd_stream));
f390d2c7 1857 }
eb5d44eb 1858
0c1bd758
CF
1859 isis_free_tlvs(tlvs);
1860
89cdc4df
RM
1861 pdu_counter_count(circuit->area->pdu_tx_counters,
1862 hello_pdu_type(circuit, level));
d62a17ae 1863 retval = circuit->tx(circuit, level);
1864 if (retval != ISIS_OK)
1a7ecb96 1865 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
1866 "ISIS-Adj (%s): Send L%d IIH on %s failed",
1867 circuit->area->area_tag, level,
1868 circuit->interface->name);
eb5d44eb 1869
d62a17ae 1870 return retval;
eb5d44eb 1871}
1872
a0a707ee 1873static int send_hello_cb(struct thread *thread)
eb5d44eb 1874{
7c4f7aab 1875 struct isis_circuit_arg *arg = THREAD_ARG(thread);
7c4f7aab 1876 assert(arg);
89cdc4df 1877
7c4f7aab
CF
1878 struct isis_circuit *circuit = arg->circuit;
1879 int level = arg->level;
d62a17ae 1880
d62a17ae 1881 assert(circuit);
ddfdbd32 1882
7c4f7aab
CF
1883 if (circuit->circ_type == CIRCUIT_T_P2P) {
1884 circuit->u.p2p.t_send_p2p_hello = NULL;
7c4f7aab 1885 send_hello(circuit, 1);
a0a707ee
CF
1886 send_hello_sched(circuit, ISIS_LEVEL1,
1887 1000 * circuit->hello_interval[1]);
7c4f7aab 1888 return ISIS_OK;
d62a17ae 1889 }
ddfdbd32 1890
7c4f7aab
CF
1891 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
1892 zlog_warn("ISIS-Hello (%s): Trying to send hello on unknown circuit type %d",
1893 circuit->area->area_tag, circuit->circ_type);
1894 return ISIS_WARNING;
1895 }
eb5d44eb 1896
7c4f7aab
CF
1897 circuit->u.bc.t_send_lan_hello[level - 1] = NULL;
1898 if (!(circuit->is_type & level)) {
1899 zlog_warn("ISIS-Hello (%s): Trying to send L%d IIH in L%d-only circuit",
1900 circuit->area->area_tag, level, 3 - level);
1901 return ISIS_WARNING;
1902 }
eb5d44eb 1903
7c4f7aab
CF
1904 if (circuit->u.bc.run_dr_elect[level - 1])
1905 isis_dr_elect(circuit, level);
eb5d44eb 1906
7c4f7aab 1907 int rv = send_hello(circuit, level);
eb5d44eb 1908
d62a17ae 1909 /* set next timer thread */
a0a707ee 1910 send_hello_sched(circuit, level, 1000 * circuit->hello_interval[level - 1]);
7c4f7aab 1911 return rv;
eb5d44eb 1912}
1913
a0a707ee
CF
1914static void _send_hello_sched(struct isis_circuit *circuit,
1915 struct thread **threadp,
1916 int level, long delay)
1917{
1918 if (*threadp) {
1919 if (thread_timer_remain_msec(*threadp) < (unsigned long)delay)
1920 return;
1921
1922 thread_cancel(*threadp);
1923 }
1924
1925 thread_add_timer_msec(master, send_hello_cb,
1926 &circuit->level_arg[level - 1],
1927 isis_jitter(delay, IIH_JITTER),
1928 threadp);
1929}
1930
1931void send_hello_sched(struct isis_circuit *circuit, int level, long delay)
1932{
1933 if (circuit->circ_type == CIRCUIT_T_P2P) {
1934 _send_hello_sched(circuit, &circuit->u.p2p.t_send_p2p_hello,
1935 ISIS_LEVEL1, delay);
1936 return;
1937 }
1938
1939 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
1940 zlog_warn("%s: encountered unknown circuit type %d on %s",
1941 __func__, circuit->circ_type,
1942 circuit->interface->name);
1943 return;
1944 }
1945
1946 for (int loop_level = ISIS_LEVEL1; loop_level <= ISIS_LEVEL2; loop_level++) {
1947 if (!(loop_level & level))
1948 continue;
1949
1950 _send_hello_sched(
1951 circuit,
1952 &circuit->u.bc.t_send_lan_hello[loop_level - 1],
1953 loop_level,
1954 delay
1955 );
1956 }
1957}
1958
1959
3f045a08
JB
1960/*
1961 * Count the maximum number of lsps that can be accomodated by a given size.
1962 */
af8ac8f9 1963#define LSP_ENTRIES_LEN (10 + ISIS_SYS_ID_LEN)
d62a17ae 1964static uint16_t get_max_lsp_count(uint16_t size)
3f045a08 1965{
d62a17ae 1966 uint16_t tlv_count;
1967 uint16_t lsp_count;
1968 uint16_t remaining_size;
3f045a08 1969
d62a17ae 1970 /* First count the full size TLVs */
1971 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
1972 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
3f045a08 1973
d62a17ae 1974 /* The last TLV, if any */
1975 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
1976 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
1977 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
3f045a08 1978
d62a17ae 1979 return lsp_count;
3f045a08
JB
1980}
1981
17c9dcd5 1982int send_csnp(struct isis_circuit *circuit, int level)
3f045a08 1983{
17c9dcd5
CF
1984 if (circuit->area->lspdb[level - 1] == NULL
1985 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1986 return ISIS_OK;
d62a17ae 1987
39bb53d6
CF
1988 uint8_t pdu_type = (level == ISIS_LEVEL1) ? L1_COMPLETE_SEQ_NUM
1989 : L2_COMPLETE_SEQ_NUM;
1990
17c9dcd5 1991 isis_circuit_stream(circuit, &circuit->snd_stream);
39bb53d6 1992 fill_fixed_hdr(pdu_type, circuit->snd_stream);
3f045a08 1993
17c9dcd5
CF
1994 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1995 stream_putw(circuit->snd_stream, 0);
1996 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1997 /* with zero circuit id - ref 9.10, 9.11 */
1998 stream_putc(circuit->snd_stream, 0);
3f045a08 1999
17c9dcd5
CF
2000 size_t start_pointer = stream_get_endp(circuit->snd_stream);
2001 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
2002 size_t end_pointer = stream_get_endp(circuit->snd_stream);
2003 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
d62a17ae 2004
17c9dcd5
CF
2005 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
2006 ? &circuit->area->area_passwd
2007 : &circuit->area->domain_passwd;
d62a17ae 2008
17c9dcd5 2009 struct isis_tlvs *tlvs = isis_alloc_tlvs();
3f045a08 2010
17c9dcd5
CF
2011 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2012 isis_tlvs_add_auth(tlvs, passwd);
eb5d44eb 2013
17c9dcd5 2014 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
2015 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
2016 false)) {
17c9dcd5
CF
2017 isis_free_tlvs(tlvs);
2018 return ISIS_WARNING;
2019 }
2020 isis_free_tlvs(tlvs);
d62a17ae 2021
17c9dcd5
CF
2022 uint16_t num_lsps =
2023 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
d62a17ae 2024
17c9dcd5 2025 uint8_t start[ISIS_SYS_ID_LEN + 2];
d62a17ae 2026 memset(start, 0x00, ISIS_SYS_ID_LEN + 2);
17c9dcd5 2027 uint8_t stop[ISIS_SYS_ID_LEN + 2];
d62a17ae 2028 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
2029
17c9dcd5 2030 bool loop = true;
d62a17ae 2031 while (loop) {
17c9dcd5
CF
2032 tlvs = isis_alloc_tlvs();
2033 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2034 isis_tlvs_add_auth(tlvs, passwd);
2035
2036 struct isis_lsp *last_lsp;
2037 isis_tlvs_add_csnp_entries(tlvs, start, stop, num_lsps,
2038 circuit->area->lspdb[level - 1],
2039 &last_lsp);
d62a17ae 2040 /*
2041 * Update the stop lsp_id before encoding this CSNP.
2042 */
17c9dcd5 2043 if (tlvs->lsp_entries.count < num_lsps) {
d62a17ae 2044 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
2045 } else {
af8ac8f9 2046 memcpy(stop, last_lsp->hdr.lsp_id, sizeof(stop));
d62a17ae 2047 }
3f045a08 2048
17c9dcd5
CF
2049 memcpy(STREAM_DATA(circuit->snd_stream) + start_pointer, start,
2050 ISIS_SYS_ID_LEN + 2);
2051 memcpy(STREAM_DATA(circuit->snd_stream) + end_pointer, stop,
2052 ISIS_SYS_ID_LEN + 2);
2053 stream_set_endp(circuit->snd_stream, tlv_start);
2054 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 2055 false, false)) {
17c9dcd5
CF
2056 isis_free_tlvs(tlvs);
2057 return ISIS_WARNING;
d62a17ae 2058 }
eb5d44eb 2059
d62a17ae 2060 if (isis->debugs & DEBUG_SNP_PACKETS) {
2061 zlog_debug(
2062 "ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
2063 circuit->area->area_tag, level,
2064 circuit->interface->name,
2065 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
2066 log_multiline(LOG_DEBUG, " ", "%s",
2067 isis_format_tlvs(tlvs));
d62a17ae 2068 if (isis->debugs & DEBUG_PACKET_DUMP)
2069 zlog_dump_data(
2070 STREAM_DATA(circuit->snd_stream),
2071 stream_get_endp(circuit->snd_stream));
2072 }
eb5d44eb 2073
39bb53d6 2074 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
17c9dcd5 2075 int retval = circuit->tx(circuit, level);
d62a17ae 2076 if (retval != ISIS_OK) {
1a7ecb96 2077 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
2078 "ISIS-Snp (%s): Send L%d CSNP on %s failed",
2079 circuit->area->area_tag, level,
2080 circuit->interface->name);
17c9dcd5 2081 isis_free_tlvs(tlvs);
d62a17ae 2082 return retval;
2083 }
3f045a08 2084
d62a17ae 2085 /*
2086 * Start lsp_id of the next CSNP should be one plus the
2087 * stop lsp_id in this current CSNP.
2088 */
2089 memcpy(start, stop, ISIS_SYS_ID_LEN + 2);
2090 loop = 0;
17c9dcd5 2091 for (int i = ISIS_SYS_ID_LEN + 1; i >= 0; --i) {
d7c0a89a 2092 if (start[i] < (uint8_t)0xff) {
d62a17ae 2093 start[i] += 1;
2094 loop = 1;
2095 break;
2096 }
2097 }
2098 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
17c9dcd5 2099 isis_free_tlvs(tlvs);
d62a17ae 2100 }
3f045a08 2101
17c9dcd5 2102 return ISIS_OK;
eb5d44eb 2103}
2104
d62a17ae 2105int send_l1_csnp(struct thread *thread)
eb5d44eb 2106{
d62a17ae 2107 struct isis_circuit *circuit;
2108 int retval = ISIS_OK;
eb5d44eb 2109
d62a17ae 2110 circuit = THREAD_ARG(thread);
2111 assert(circuit);
eb5d44eb 2112
d62a17ae 2113 circuit->t_send_csnp[0] = NULL;
eb5d44eb 2114
8e6fb83b
CF
2115 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
2116 && circuit->u.bc.is_dr[0])
2117 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 2118 send_csnp(circuit, 1);
2119 }
2120 /* set next timer thread */
2121 thread_add_timer(master, send_l1_csnp, circuit,
2122 isis_jitter(circuit->csnp_interval[0], CSNP_JITTER),
2123 &circuit->t_send_csnp[0]);
eb5d44eb 2124
d62a17ae 2125 return retval;
eb5d44eb 2126}
2127
d62a17ae 2128int send_l2_csnp(struct thread *thread)
eb5d44eb 2129{
d62a17ae 2130 struct isis_circuit *circuit;
2131 int retval = ISIS_OK;
eb5d44eb 2132
d62a17ae 2133 circuit = THREAD_ARG(thread);
2134 assert(circuit);
eb5d44eb 2135
d62a17ae 2136 circuit->t_send_csnp[1] = NULL;
eb5d44eb 2137
8e6fb83b
CF
2138 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
2139 && circuit->u.bc.is_dr[1])
2140 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 2141 send_csnp(circuit, 2);
2142 }
2143 /* set next timer thread */
2144 thread_add_timer(master, send_l2_csnp, circuit,
2145 isis_jitter(circuit->csnp_interval[1], CSNP_JITTER),
2146 &circuit->t_send_csnp[1]);
d70f99e1 2147
d62a17ae 2148 return retval;
eb5d44eb 2149}
2150
eb5d44eb 2151/*
2152 * 7.3.15.4 action on expiration of partial SNP interval
2153 * level 1
2154 */
d62a17ae 2155static int send_psnp(int level, struct isis_circuit *circuit)
eb5d44eb 2156{
d62a17ae 2157 if (circuit->circ_type == CIRCUIT_T_BROADCAST
2158 && circuit->u.bc.is_dr[level - 1])
2159 return ISIS_OK;
eb5d44eb 2160
d62a17ae 2161 if (circuit->area->lspdb[level - 1] == NULL
2162 || dict_count(circuit->area->lspdb[level - 1]) == 0)
2163 return ISIS_OK;
f390d2c7 2164
d62a17ae 2165 if (!circuit->snd_stream)
2166 return ISIS_ERROR;
e38e0df0 2167
39bb53d6
CF
2168 uint8_t pdu_type = (level == ISIS_LEVEL1) ? L1_PARTIAL_SEQ_NUM
2169 : L2_PARTIAL_SEQ_NUM;
2170
17c9dcd5 2171 isis_circuit_stream(circuit, &circuit->snd_stream);
39bb53d6 2172 fill_fixed_hdr(pdu_type, circuit->snd_stream);
17c9dcd5
CF
2173
2174 size_t len_pointer = stream_get_endp(circuit->snd_stream);
2175 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
2176 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2177 stream_putc(circuit->snd_stream, circuit->idx);
2178
2179 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
2180 ? &circuit->area->area_passwd
2181 : &circuit->area->domain_passwd;
2182
2183 struct isis_tlvs *tlvs = isis_alloc_tlvs();
2184
2185 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2186 isis_tlvs_add_auth(tlvs, passwd);
2187
2188 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
2189 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
2190 false)) {
17c9dcd5
CF
2191 isis_free_tlvs(tlvs);
2192 return ISIS_WARNING;
2193 }
2194 isis_free_tlvs(tlvs);
2195
2196 uint16_t num_lsps =
2197 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
f390d2c7 2198
d62a17ae 2199 while (1) {
17c9dcd5
CF
2200 tlvs = isis_alloc_tlvs();
2201 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2202 isis_tlvs_add_auth(tlvs, passwd);
2203
2204 for (dnode_t *dnode =
2205 dict_first(circuit->area->lspdb[level - 1]);
2206 dnode; dnode = dict_next(circuit->area->lspdb[level - 1],
2207 dnode)) {
2208 struct isis_lsp *lsp = dnode_get(dnode);
2209
2210 if (ISIS_CHECK_FLAG(lsp->SSNflags, circuit))
2211 isis_tlvs_add_lsp_entry(tlvs, lsp);
eb5d44eb 2212
17c9dcd5
CF
2213 if (tlvs->lsp_entries.count == num_lsps)
2214 break;
2215 }
2216
2217 if (!tlvs->lsp_entries.count) {
2218 isis_free_tlvs(tlvs);
d62a17ae 2219 return ISIS_OK;
2220 }
2221
17c9dcd5
CF
2222 stream_set_endp(circuit->snd_stream, tlv_start);
2223 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 2224 false, false)) {
17c9dcd5
CF
2225 isis_free_tlvs(tlvs);
2226 return ISIS_WARNING;
d62a17ae 2227 }
2228
2229 if (isis->debugs & DEBUG_SNP_PACKETS) {
2230 zlog_debug(
2231 "ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
2232 circuit->area->area_tag, level,
2233 circuit->interface->name,
2234 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
2235 log_multiline(LOG_DEBUG, " ", "%s",
2236 isis_format_tlvs(tlvs));
d62a17ae 2237 if (isis->debugs & DEBUG_PACKET_DUMP)
2238 zlog_dump_data(
2239 STREAM_DATA(circuit->snd_stream),
2240 stream_get_endp(circuit->snd_stream));
2241 }
2242
39bb53d6 2243 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
17c9dcd5 2244 int retval = circuit->tx(circuit, level);
d62a17ae 2245 if (retval != ISIS_OK) {
1a7ecb96 2246 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
2247 "ISIS-Snp (%s): Send L%d PSNP on %s failed",
2248 circuit->area->area_tag, level,
2249 circuit->interface->name);
17c9dcd5 2250 isis_free_tlvs(tlvs);
d62a17ae 2251 return retval;
2252 }
2253
2254 /*
2255 * sending succeeded, we can clear SSN flags of this circuit
2256 * for the LSPs in list
2257 */
17c9dcd5
CF
2258 struct isis_lsp_entry *entry_head;
2259 entry_head = (struct isis_lsp_entry *)tlvs->lsp_entries.head;
2260 for (struct isis_lsp_entry *entry = entry_head; entry;
2261 entry = entry->next)
2262 ISIS_CLEAR_FLAG(entry->lsp->SSNflags, circuit);
2263 isis_free_tlvs(tlvs);
d62a17ae 2264 }
2265
17c9dcd5 2266 return ISIS_OK;
eb5d44eb 2267}
2268
d62a17ae 2269int send_l1_psnp(struct thread *thread)
eb5d44eb 2270{
2271
d62a17ae 2272 struct isis_circuit *circuit;
2273 int retval = ISIS_OK;
eb5d44eb 2274
d62a17ae 2275 circuit = THREAD_ARG(thread);
2276 assert(circuit);
eb5d44eb 2277
d62a17ae 2278 circuit->t_send_psnp[0] = NULL;
eb5d44eb 2279
d62a17ae 2280 send_psnp(1, circuit);
2281 /* set next timer thread */
2282 thread_add_timer(master, send_l1_psnp, circuit,
2283 isis_jitter(circuit->psnp_interval[0], PSNP_JITTER),
2284 &circuit->t_send_psnp[0]);
eb5d44eb 2285
d62a17ae 2286 return retval;
eb5d44eb 2287}
2288
2289/*
2290 * 7.3.15.4 action on expiration of partial SNP interval
2291 * level 2
2292 */
d62a17ae 2293int send_l2_psnp(struct thread *thread)
eb5d44eb 2294{
d62a17ae 2295 struct isis_circuit *circuit;
2296 int retval = ISIS_OK;
eb5d44eb 2297
d62a17ae 2298 circuit = THREAD_ARG(thread);
2299 assert(circuit);
eb5d44eb 2300
d62a17ae 2301 circuit->t_send_psnp[1] = NULL;
eb5d44eb 2302
d62a17ae 2303 send_psnp(2, circuit);
eb5d44eb 2304
d62a17ae 2305 /* set next timer thread */
2306 thread_add_timer(master, send_l2_psnp, circuit,
2307 isis_jitter(circuit->psnp_interval[1], PSNP_JITTER),
2308 &circuit->t_send_psnp[1]);
eb5d44eb 2309
d62a17ae 2310 return retval;
eb5d44eb 2311}
2312
eb5d44eb 2313/*
2314 * ISO 10589 - 7.3.14.3
2315 */
161fa356
CF
2316void send_lsp(struct isis_circuit *circuit, struct isis_lsp *lsp,
2317 enum isis_tx_type tx_type)
eb5d44eb 2318{
d62a17ae 2319 int clear_srm = 1;
2320 int retval = ISIS_OK;
2321
d62a17ae 2322 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
2323 goto out;
2324
2325 /*
2326 * Do not send if levels do not match
2327 */
2328 if (!(lsp->level & circuit->is_type))
2329 goto out;
2330
2331 /*
2332 * Do not send if we do not have adjacencies in state up on the circuit
2333 */
2334 if (circuit->upadjcount[lsp->level - 1] == 0)
2335 goto out;
2336
2337 /* stream_copy will assert and stop program execution if LSP is larger
2338 * than
2339 * the circuit's MTU. So handle and log this case here. */
2340 if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream)) {
af4c2728 2341 flog_err(
1a7ecb96 2342 EC_ISIS_PACKET,
af8ac8f9
CF
2343 "ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08" PRIx32
2344 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2345 "s on %s. LSP Size is %zu while interface stream size is %zu.",
d62a17ae 2346 circuit->area->area_tag, lsp->level,
af8ac8f9
CF
2347 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2348 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
d62a17ae 2349 circuit->interface->name, stream_get_endp(lsp->pdu),
2350 stream_get_size(circuit->snd_stream));
56e22fb6
EDP
2351#ifndef FABRICD
2352 /* send a northbound notification */
2353 isis_notif_lsp_too_large(circuit, stream_get_endp(lsp->pdu),
2354 rawlspid_print(lsp->hdr.lsp_id));
2355#endif /* ifndef FABRICD */
d62a17ae 2356 if (isis->debugs & DEBUG_PACKET_DUMP)
2357 zlog_dump_data(STREAM_DATA(lsp->pdu),
2358 stream_get_endp(lsp->pdu));
2359 retval = ISIS_ERROR;
2360 goto out;
2361 }
eb5d44eb 2362
d62a17ae 2363 /* copy our lsp to the send buffer */
2364 stream_copy(circuit->snd_stream, lsp->pdu);
2365
1cbd5b37
CF
2366 if (tx_type == TX_LSP_CIRCUIT_SCOPED) {
2367 stream_putc_at(circuit->snd_stream, 4, FS_LINK_STATE);
89cdc4df
RM
2368 stream_putc_at(circuit->snd_stream, 7,
2369 L2_CIRCUIT_FLOODING_SCOPE);
1cbd5b37
CF
2370 }
2371
d62a17ae 2372 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
161fa356 2373 zlog_debug("ISIS-Upd (%s): Sending %sL%d LSP %s, seq 0x%08" PRIx32
af8ac8f9
CF
2374 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2375 "s on %s",
161fa356
CF
2376 circuit->area->area_tag,
2377 (tx_type == TX_LSP_CIRCUIT_SCOPED)
2378 ? "Circuit scoped " : "",
2379 lsp->level,
af8ac8f9
CF
2380 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2381 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
2382 circuit->interface->name);
d62a17ae 2383 if (isis->debugs & DEBUG_PACKET_DUMP)
2384 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
2385 stream_get_endp(circuit->snd_stream));
2386 }
3f045a08 2387
39bb53d6
CF
2388 uint8_t pdu_type = (tx_type == TX_LSP_CIRCUIT_SCOPED) ? FS_LINK_STATE
2389 : (lsp->level == ISIS_LEVEL1) ? L1_LINK_STATE
2390 : L2_LINK_STATE;
2391
d62a17ae 2392 clear_srm = 0;
39bb53d6 2393 pdu_counter_count(circuit->area->pdu_tx_counters, pdu_type);
d62a17ae 2394 retval = circuit->tx(circuit, lsp->level);
2395 if (retval != ISIS_OK) {
1a7ecb96 2396 flog_err(EC_ISIS_PACKET,
1c50c1c0
QY
2397 "ISIS-Upd (%s): Send L%d LSP on %s failed %s",
2398 circuit->area->area_tag, lsp->level,
2399 circuit->interface->name,
2400 (retval == ISIS_WARNING) ? "temporarily"
2401 : "permanently");
d62a17ae 2402 }
f390d2c7 2403
cfd1f27b 2404out:
d62a17ae 2405 if (clear_srm
2406 || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
2407 || (retval != ISIS_OK && retval != ISIS_WARNING)) {
2408 /* SRM flag will trigger retransmission. We will not retransmit
2409 * if we
2410 * encountered a fatal error.
2411 * On success, they should only be cleared if it's a broadcast
2412 * circuit.
2413 * On a P2P circuit, we will wait for the ack from the neighbor
2414 * to clear
2415 * the fag.
2416 */
9b39405f 2417 isis_tx_queue_del(circuit->tx_queue, lsp);
d62a17ae 2418 }
f390d2c7 2419}