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