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