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