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