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