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