]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_packet.c
ospfd: Cleanup oi->obuf to always be created
[mirror_frr.git] / ospfd / ospf_packet.c
1 /*
2 * OSPF Sending and Receiving OSPF Packets.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "monotime.h"
25 #include "thread.h"
26 #include "memory.h"
27 #include "linklist.h"
28 #include "prefix.h"
29 #include "if.h"
30 #include "table.h"
31 #include "sockunion.h"
32 #include "stream.h"
33 #include "log.h"
34 #include "sockopt.h"
35 #include "checksum.h"
36 #ifdef CRYPTO_INTERNAL
37 #include "md5.h"
38 #endif
39 #include "vrf.h"
40 #include "lib_errors.h"
41
42 #include "ospfd/ospfd.h"
43 #include "ospfd/ospf_network.h"
44 #include "ospfd/ospf_interface.h"
45 #include "ospfd/ospf_ism.h"
46 #include "ospfd/ospf_asbr.h"
47 #include "ospfd/ospf_lsa.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_nsm.h"
51 #include "ospfd/ospf_packet.h"
52 #include "ospfd/ospf_spf.h"
53 #include "ospfd/ospf_flood.h"
54 #include "ospfd/ospf_dump.h"
55 #include "ospfd/ospf_errors.h"
56
57 /*
58 * OSPF Fragmentation / fragmented writes
59 *
60 * ospfd can support writing fragmented packets, for cases where
61 * kernel will not fragment IP_HDRINCL and/or multicast destined
62 * packets (ie TTBOMK all kernels, BSD, SunOS, Linux). However,
63 * SunOS, probably BSD too, clobber the user supplied IP ID and IP
64 * flags fields, hence user-space fragmentation will not work.
65 * Only Linux is known to leave IP header unmolested.
66 * Further, fragmentation really should be done the kernel, which already
67 * supports it, and which avoids nasty IP ID state problems.
68 *
69 * Fragmentation of OSPF packets can be required on networks with router
70 * with many many interfaces active in one area, or on networks with links
71 * with low MTUs.
72 */
73 #ifdef GNU_LINUX
74 #define WANT_OSPF_WRITE_FRAGMENT
75 #endif
76
77 /* Packet Type String. */
78 const struct message ospf_packet_type_str[] = {
79 {OSPF_MSG_HELLO, "Hello"},
80 {OSPF_MSG_DB_DESC, "Database Description"},
81 {OSPF_MSG_LS_REQ, "Link State Request"},
82 {OSPF_MSG_LS_UPD, "Link State Update"},
83 {OSPF_MSG_LS_ACK, "Link State Acknowledgment"},
84 {0}};
85
86 /* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
87 particular types, offset is the "type" field of a packet. */
88 static const uint16_t ospf_packet_minlen[] = {
89 0,
90 OSPF_HELLO_MIN_SIZE,
91 OSPF_DB_DESC_MIN_SIZE,
92 OSPF_LS_REQ_MIN_SIZE,
93 OSPF_LS_UPD_MIN_SIZE,
94 OSPF_LS_ACK_MIN_SIZE,
95 };
96
97 /* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
98 types, offset is the "LSA type" field. */
99 static const uint16_t ospf_lsa_minlen[] = {
100 0,
101 OSPF_ROUTER_LSA_MIN_SIZE,
102 OSPF_NETWORK_LSA_MIN_SIZE,
103 OSPF_SUMMARY_LSA_MIN_SIZE,
104 OSPF_SUMMARY_LSA_MIN_SIZE,
105 OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
106 0,
107 OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
108 0,
109 0,
110 0,
111 0,
112 };
113
114 /* for ospf_check_auth() */
115 static int ospf_check_sum(struct ospf_header *);
116
117 /* OSPF authentication checking function */
118 static int ospf_auth_type(struct ospf_interface *oi)
119 {
120 int auth_type;
121
122 if (OSPF_IF_PARAM(oi, auth_type) == OSPF_AUTH_NOTSET)
123 auth_type = oi->area->auth_type;
124 else
125 auth_type = OSPF_IF_PARAM(oi, auth_type);
126
127 /* Handle case where MD5 key list is not configured aka Cisco */
128 if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC
129 && list_isempty(OSPF_IF_PARAM(oi, auth_crypt)))
130 return OSPF_AUTH_NULL;
131
132 return auth_type;
133 }
134
135 static struct ospf_packet *ospf_packet_new(size_t size)
136 {
137 struct ospf_packet *new;
138
139 new = XCALLOC(MTYPE_OSPF_PACKET, sizeof(struct ospf_packet));
140 new->s = stream_new(size);
141
142 return new;
143 }
144
145 void ospf_packet_free(struct ospf_packet *op)
146 {
147 if (op->s)
148 stream_free(op->s);
149
150 XFREE(MTYPE_OSPF_PACKET, op);
151 }
152
153 struct ospf_fifo *ospf_fifo_new(void)
154 {
155 struct ospf_fifo *new;
156
157 new = XCALLOC(MTYPE_OSPF_FIFO, sizeof(struct ospf_fifo));
158 return new;
159 }
160
161 /* Add new packet to fifo. */
162 void ospf_fifo_push(struct ospf_fifo *fifo, struct ospf_packet *op)
163 {
164 if (fifo->tail)
165 fifo->tail->next = op;
166 else
167 fifo->head = op;
168
169 fifo->tail = op;
170
171 fifo->count++;
172 }
173
174 /* Add new packet to head of fifo. */
175 static void ospf_fifo_push_head(struct ospf_fifo *fifo, struct ospf_packet *op)
176 {
177 op->next = fifo->head;
178
179 if (fifo->tail == NULL)
180 fifo->tail = op;
181
182 fifo->head = op;
183
184 fifo->count++;
185 }
186
187 /* Delete first packet from fifo. */
188 struct ospf_packet *ospf_fifo_pop(struct ospf_fifo *fifo)
189 {
190 struct ospf_packet *op;
191
192 op = fifo->head;
193
194 if (op) {
195 fifo->head = op->next;
196
197 if (fifo->head == NULL)
198 fifo->tail = NULL;
199
200 fifo->count--;
201 }
202
203 return op;
204 }
205
206 /* Return first fifo entry. */
207 struct ospf_packet *ospf_fifo_head(struct ospf_fifo *fifo)
208 {
209 return fifo->head;
210 }
211
212 /* Flush ospf packet fifo. */
213 void ospf_fifo_flush(struct ospf_fifo *fifo)
214 {
215 struct ospf_packet *op;
216 struct ospf_packet *next;
217
218 for (op = fifo->head; op; op = next) {
219 next = op->next;
220 ospf_packet_free(op);
221 }
222 fifo->head = fifo->tail = NULL;
223 fifo->count = 0;
224 }
225
226 /* Free ospf packet fifo. */
227 void ospf_fifo_free(struct ospf_fifo *fifo)
228 {
229 ospf_fifo_flush(fifo);
230
231 XFREE(MTYPE_OSPF_FIFO, fifo);
232 }
233
234 static void ospf_packet_add(struct ospf_interface *oi, struct ospf_packet *op)
235 {
236 /* Add packet to end of queue. */
237 ospf_fifo_push(oi->obuf, op);
238
239 /* Debug of packet fifo*/
240 /* ospf_fifo_debug (oi->obuf); */
241 }
242
243 static void ospf_packet_add_top(struct ospf_interface *oi,
244 struct ospf_packet *op)
245 {
246 /* Add packet to head of queue. */
247 ospf_fifo_push_head(oi->obuf, op);
248
249 /* Debug of packet fifo*/
250 /* ospf_fifo_debug (oi->obuf); */
251 }
252
253 static void ospf_packet_delete(struct ospf_interface *oi)
254 {
255 struct ospf_packet *op;
256
257 op = ospf_fifo_pop(oi->obuf);
258
259 if (op)
260 ospf_packet_free(op);
261 }
262
263 static struct ospf_packet *ospf_packet_dup(struct ospf_packet *op)
264 {
265 struct ospf_packet *new;
266
267 if (stream_get_endp(op->s) != op->length)
268 /* XXX size_t */
269 zlog_debug(
270 "ospf_packet_dup stream %lu ospf_packet %u size mismatch",
271 (unsigned long)STREAM_SIZE(op->s), op->length);
272
273 /* Reserve space for MD5 authentication that may be added later. */
274 new = ospf_packet_new(stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
275 stream_copy(new->s, op->s);
276
277 new->dst = op->dst;
278 new->length = op->length;
279
280 return new;
281 }
282
283 /* XXX inline */
284 static unsigned int ospf_packet_authspace(struct ospf_interface *oi)
285 {
286 int auth = 0;
287
288 if (ospf_auth_type(oi) == OSPF_AUTH_CRYPTOGRAPHIC)
289 auth = OSPF_AUTH_MD5_SIZE;
290
291 return auth;
292 }
293
294 static unsigned int ospf_packet_max(struct ospf_interface *oi)
295 {
296 int max;
297
298 max = oi->ifp->mtu - ospf_packet_authspace(oi);
299
300 max -= (OSPF_HEADER_SIZE + sizeof(struct ip));
301
302 return max;
303 }
304
305
306 static int ospf_check_md5_digest(struct ospf_interface *oi,
307 struct ospf_header *ospfh)
308 {
309 #ifdef CRYPTO_OPENSSL
310 EVP_MD_CTX *ctx;
311 #elif CRYPTO_INTERNAL
312 MD5_CTX ctx;
313 #endif
314 unsigned char digest[OSPF_AUTH_MD5_SIZE];
315 struct crypt_key *ck;
316 struct ospf_neighbor *nbr;
317 uint16_t length = ntohs(ospfh->length);
318
319 /* Get secret key. */
320 ck = ospf_crypt_key_lookup(OSPF_IF_PARAM(oi, auth_crypt),
321 ospfh->u.crypt.key_id);
322 if (ck == NULL) {
323 flog_warn(EC_OSPF_MD5, "interface %s: ospf_check_md5 no key %d",
324 IF_NAME(oi), ospfh->u.crypt.key_id);
325 return 0;
326 }
327
328 /* check crypto seqnum. */
329 nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, &ospfh->router_id);
330
331 if (nbr
332 && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum)) {
333 flog_warn(
334 EC_OSPF_MD5,
335 "interface %s: ospf_check_md5 bad sequence %d (expect %d)",
336 IF_NAME(oi), ntohl(ospfh->u.crypt.crypt_seqnum),
337 ntohl(nbr->crypt_seqnum));
338 return 0;
339 }
340
341 /* Generate a digest for the ospf packet - their digest + our digest. */
342 #ifdef CRYPTO_OPENSSL
343 unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
344 ctx = EVP_MD_CTX_new();
345 EVP_DigestInit(ctx, EVP_md5());
346 EVP_DigestUpdate(ctx, ospfh, length);
347 EVP_DigestUpdate(ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
348 EVP_DigestFinal(ctx, digest, &md5_size);
349 EVP_MD_CTX_free(ctx);
350 #elif CRYPTO_INTERNAL
351 memset(&ctx, 0, sizeof(ctx));
352 MD5Init(&ctx);
353 MD5Update(&ctx, ospfh, length);
354 MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
355 MD5Final(digest, &ctx);
356 #endif
357
358 /* compare the two */
359 if (memcmp((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE)) {
360 flog_warn(EC_OSPF_MD5,
361 "interface %s: ospf_check_md5 checksum mismatch",
362 IF_NAME(oi));
363 return 0;
364 }
365
366 /* save neighbor's crypt_seqnum */
367 if (nbr)
368 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
369 return 1;
370 }
371
372 /* This function is called from ospf_write(), it will detect the
373 authentication scheme and if it is MD5, it will change the sequence
374 and update the MD5 digest. */
375 static int ospf_make_md5_digest(struct ospf_interface *oi,
376 struct ospf_packet *op)
377 {
378 struct ospf_header *ospfh;
379 unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0};
380 #ifdef CRYPTO_OPENSSL
381 EVP_MD_CTX *ctx;
382 #elif CRYPTO_INTERNAL
383 MD5_CTX ctx;
384 #endif
385 void *ibuf;
386 uint32_t t;
387 struct crypt_key *ck;
388 const uint8_t *auth_key;
389
390 ibuf = STREAM_DATA(op->s);
391 ospfh = (struct ospf_header *)ibuf;
392
393 if (ntohs(ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
394 return 0;
395
396 /* We do this here so when we dup a packet, we don't have to
397 waste CPU rewriting other headers.
398
399 Note that quagga_time /deliberately/ is not used here */
400 t = (time(NULL) & 0xFFFFFFFF);
401 if (t > oi->crypt_seqnum)
402 oi->crypt_seqnum = t;
403 else
404 oi->crypt_seqnum++;
405
406 ospfh->u.crypt.crypt_seqnum = htonl(oi->crypt_seqnum);
407
408 /* Get MD5 Authentication key from auth_key list. */
409 if (list_isempty(OSPF_IF_PARAM(oi, auth_crypt)))
410 auth_key = (const uint8_t *)digest;
411 else {
412 ck = listgetdata(listtail(OSPF_IF_PARAM(oi, auth_crypt)));
413 auth_key = ck->auth_key;
414 }
415
416 /* Generate a digest for the entire packet + our secret key. */
417 #ifdef CRYPTO_OPENSSL
418 unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
419 ctx = EVP_MD_CTX_new();
420 EVP_DigestInit(ctx, EVP_md5());
421 EVP_DigestUpdate(ctx, ibuf, ntohs(ospfh->length));
422 EVP_DigestUpdate(ctx, auth_key, OSPF_AUTH_MD5_SIZE);
423 EVP_DigestFinal(ctx, digest, &md5_size);
424 EVP_MD_CTX_free(ctx);
425 #elif CRYPTO_INTERNAL
426 memset(&ctx, 0, sizeof(ctx));
427 MD5Init(&ctx);
428 MD5Update(&ctx, ibuf, ntohs(ospfh->length));
429 MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
430 MD5Final(digest, &ctx);
431 #endif
432
433 /* Append md5 digest to the end of the stream. */
434 stream_put(op->s, digest, OSPF_AUTH_MD5_SIZE);
435
436 /* We do *NOT* increment the OSPF header length. */
437 op->length = ntohs(ospfh->length) + OSPF_AUTH_MD5_SIZE;
438
439 if (stream_get_endp(op->s) != op->length)
440 /* XXX size_t */
441 flog_warn(
442 EC_OSPF_MD5,
443 "ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
444 (unsigned long)stream_get_endp(op->s), op->length);
445
446 return OSPF_AUTH_MD5_SIZE;
447 }
448
449
450 static int ospf_ls_req_timer(struct thread *thread)
451 {
452 struct ospf_neighbor *nbr;
453
454 nbr = THREAD_ARG(thread);
455 nbr->t_ls_req = NULL;
456
457 /* Send Link State Request. */
458 if (ospf_ls_request_count(nbr))
459 ospf_ls_req_send(nbr);
460
461 /* Set Link State Request retransmission timer. */
462 OSPF_NSM_TIMER_ON(nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
463
464 return 0;
465 }
466
467 void ospf_ls_req_event(struct ospf_neighbor *nbr)
468 {
469 if (nbr->t_ls_req) {
470 thread_cancel(nbr->t_ls_req);
471 nbr->t_ls_req = NULL;
472 }
473 nbr->t_ls_req = NULL;
474 thread_add_event(master, ospf_ls_req_timer, nbr, 0, &nbr->t_ls_req);
475 }
476
477 /* Cyclic timer function. Fist registered in ospf_nbr_new () in
478 ospf_neighbor.c */
479 int ospf_ls_upd_timer(struct thread *thread)
480 {
481 struct ospf_neighbor *nbr;
482
483 nbr = THREAD_ARG(thread);
484 nbr->t_ls_upd = NULL;
485
486 /* Send Link State Update. */
487 if (ospf_ls_retransmit_count(nbr) > 0) {
488 struct list *update;
489 struct ospf_lsdb *lsdb;
490 int i;
491 int retransmit_interval;
492
493 retransmit_interval =
494 OSPF_IF_PARAM(nbr->oi, retransmit_interval);
495
496 lsdb = &nbr->ls_rxmt;
497 update = list_new();
498
499 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
500 struct route_table *table = lsdb->type[i].db;
501 struct route_node *rn;
502
503 for (rn = route_top(table); rn; rn = route_next(rn)) {
504 struct ospf_lsa *lsa;
505
506 if ((lsa = rn->info) != NULL) {
507 /* Don't retransmit an LSA if we
508 received it within
509 the last RxmtInterval seconds - this
510 is to allow the
511 neighbour a chance to acknowledge the
512 LSA as it may
513 have ben just received before the
514 retransmit timer
515 fired. This is a small tweak to what
516 is in the RFC,
517 but it will cut out out a lot of
518 retransmit traffic
519 - MAG */
520 if (monotime_since(&lsa->tv_recv, NULL)
521 >= retransmit_interval * 1000000LL)
522 listnode_add(update, rn->info);
523 }
524 }
525 }
526
527 if (listcount(update) > 0)
528 ospf_ls_upd_send(nbr, update, OSPF_SEND_PACKET_DIRECT,
529 0);
530 list_delete(&update);
531 }
532
533 /* Set LS Update retransmission timer. */
534 OSPF_NSM_TIMER_ON(nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
535
536 return 0;
537 }
538
539 int ospf_ls_ack_timer(struct thread *thread)
540 {
541 struct ospf_interface *oi;
542
543 oi = THREAD_ARG(thread);
544 oi->t_ls_ack = NULL;
545
546 /* Send Link State Acknowledgment. */
547 if (listcount(oi->ls_ack) > 0)
548 ospf_ls_ack_send_delayed(oi);
549
550 /* Set LS Ack timer. */
551 OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
552
553 return 0;
554 }
555
556 #ifdef WANT_OSPF_WRITE_FRAGMENT
557 static void ospf_write_frags(int fd, struct ospf_packet *op, struct ip *iph,
558 struct msghdr *msg, unsigned int maxdatasize,
559 unsigned int mtu, int flags, uint8_t type)
560 {
561 #define OSPF_WRITE_FRAG_SHIFT 3
562 uint16_t offset;
563 struct iovec *iovp;
564 int ret;
565
566 assert(op->length == stream_get_endp(op->s));
567 assert(msg->msg_iovlen == 2);
568
569 /* we can but try.
570 *
571 * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
572 * well as the IP_MF flag, making this all quite pointless.
573 *
574 * However, for a system on which IP_MF is left alone, and ip_id left
575 * alone or else which sets same ip_id for each fragment this might
576 * work, eg linux.
577 *
578 * XXX-TODO: It would be much nicer to have the kernel's use their
579 * existing fragmentation support to do this for us. Bugs/RFEs need to
580 * be raised against the various kernels.
581 */
582
583 /* set More Frag */
584 iph->ip_off |= IP_MF;
585
586 /* ip frag offset is expressed in units of 8byte words */
587 offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT;
588
589 iovp = &msg->msg_iov[1];
590
591 while ((stream_get_endp(op->s) - stream_get_getp(op->s))
592 > maxdatasize) {
593 /* data length of this frag is to next offset value */
594 iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT;
595 iph->ip_len = iovp->iov_len + sizeof(struct ip);
596 assert(iph->ip_len <= mtu);
597
598 sockopt_iphdrincl_swab_htosys(iph);
599
600 ret = sendmsg(fd, msg, flags);
601
602 sockopt_iphdrincl_swab_systoh(iph);
603
604 if (ret < 0)
605 flog_err(
606 EC_LIB_SOCKET,
607 "*** ospf_write_frags: sendmsg failed to %s,"
608 " id %d, off %d, len %d, mtu %u failed with %s",
609 inet_ntoa(iph->ip_dst), iph->ip_id, iph->ip_off,
610 iph->ip_len, mtu, safe_strerror(errno));
611
612 if (IS_DEBUG_OSPF_PACKET(type - 1, SEND)) {
613 zlog_debug(
614 "ospf_write_frags: sent id %d, off %d, len %d to %s\n",
615 iph->ip_id, iph->ip_off, iph->ip_len,
616 inet_ntoa(iph->ip_dst));
617 if (IS_DEBUG_OSPF_PACKET(type - 1, DETAIL)) {
618 zlog_debug(
619 "-----------------IP Header Dump----------------------");
620 ospf_ip_header_dump(iph);
621 zlog_debug(
622 "-----------------------------------------------------");
623 }
624 }
625
626 iph->ip_off += offset;
627 stream_forward_getp(op->s, iovp->iov_len);
628 iovp->iov_base = stream_pnt(op->s);
629 }
630
631 /* setup for final fragment */
632 iovp->iov_len = stream_get_endp(op->s) - stream_get_getp(op->s);
633 iph->ip_len = iovp->iov_len + sizeof(struct ip);
634 iph->ip_off &= (~IP_MF);
635 }
636 #endif /* WANT_OSPF_WRITE_FRAGMENT */
637
638 static int ospf_write(struct thread *thread)
639 {
640 struct ospf *ospf = THREAD_ARG(thread);
641 struct ospf_interface *oi;
642 struct ospf_interface *last_serviced_oi = NULL;
643 struct ospf_packet *op;
644 struct sockaddr_in sa_dst;
645 struct ip iph;
646 struct msghdr msg;
647 struct iovec iov[2];
648 uint8_t type;
649 int ret;
650 int flags = 0;
651 struct listnode *node;
652 #ifdef WANT_OSPF_WRITE_FRAGMENT
653 static uint16_t ipid = 0;
654 uint16_t maxdatasize;
655 #endif /* WANT_OSPF_WRITE_FRAGMENT */
656 #define OSPF_WRITE_IPHL_SHIFT 2
657 int pkt_count = 0;
658
659 #ifdef GNU_LINUX
660 unsigned char cmsgbuf[64] = {};
661 struct cmsghdr *cm = (struct cmsghdr *)cmsgbuf;
662 struct in_pktinfo *pi;
663 #endif
664
665 if (ospf->fd < 0 || ospf->oi_running == 0) {
666 if (IS_DEBUG_OSPF_EVENT)
667 zlog_debug(
668 "ospf_write failed to send, fd %d, instance %u"
669 ,ospf->fd, ospf->oi_running);
670 return -1;
671 }
672
673 node = listhead(ospf->oi_write_q);
674 assert(node);
675 oi = listgetdata(node);
676
677 #ifdef WANT_OSPF_WRITE_FRAGMENT
678 /* seed ipid static with low order bits of time */
679 if (ipid == 0)
680 ipid = (time(NULL) & 0xffff);
681 #endif /* WANT_OSPF_WRITE_FRAGMENT */
682
683 while ((pkt_count < ospf->write_oi_count) && oi
684 && (last_serviced_oi != oi)) {
685 /* If there is only packet in the queue, the oi is removed from
686 write-q, so fix up the last interface that was serviced */
687 if (last_serviced_oi == NULL) {
688 last_serviced_oi = oi;
689 }
690 pkt_count++;
691 #ifdef WANT_OSPF_WRITE_FRAGMENT
692 /* convenience - max OSPF data per packet */
693 maxdatasize = oi->ifp->mtu - sizeof(struct ip);
694 #endif /* WANT_OSPF_WRITE_FRAGMENT */
695 /* Get one packet from queue. */
696 op = ospf_fifo_head(oi->obuf);
697 assert(op);
698 assert(op->length >= OSPF_HEADER_SIZE);
699
700 if (op->dst.s_addr == htonl(OSPF_ALLSPFROUTERS)
701 || op->dst.s_addr == htonl(OSPF_ALLDROUTERS))
702 ospf_if_ipmulticast(ospf, oi->address,
703 oi->ifp->ifindex);
704
705 /* Rewrite the md5 signature & update the seq */
706 ospf_make_md5_digest(oi, op);
707
708 /* Retrieve OSPF packet type. */
709 stream_set_getp(op->s, 1);
710 type = stream_getc(op->s);
711
712 /* reset get pointer */
713 stream_set_getp(op->s, 0);
714
715 memset(&iph, 0, sizeof(struct ip));
716 memset(&sa_dst, 0, sizeof(sa_dst));
717
718 sa_dst.sin_family = AF_INET;
719 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
720 sa_dst.sin_len = sizeof(sa_dst);
721 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
722 sa_dst.sin_addr = op->dst;
723 sa_dst.sin_port = htons(0);
724
725 /* Set DONTROUTE flag if dst is unicast. */
726 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
727 if (!IN_MULTICAST(htonl(op->dst.s_addr)))
728 flags = MSG_DONTROUTE;
729
730 iph.ip_hl = sizeof(struct ip) >> OSPF_WRITE_IPHL_SHIFT;
731 /* it'd be very strange for header to not be 4byte-word aligned
732 * but.. */
733 if (sizeof(struct ip)
734 > (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT))
735 iph.ip_hl++; /* we presume sizeof struct ip cant
736 overflow ip_hl.. */
737
738 iph.ip_v = IPVERSION;
739 iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
740 iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length;
741
742 #if defined(__DragonFly__)
743 /*
744 * DragonFly's raw socket expects ip_len/ip_off in network byte
745 * order.
746 */
747 iph.ip_len = htons(iph.ip_len);
748 #endif
749
750 #ifdef WANT_OSPF_WRITE_FRAGMENT
751 /* XXX-MT: not thread-safe at all..
752 * XXX: this presumes this is only programme sending OSPF
753 * packets
754 * otherwise, no guarantee ipid will be unique
755 */
756 iph.ip_id = ++ipid;
757 #endif /* WANT_OSPF_WRITE_FRAGMENT */
758
759 iph.ip_off = 0;
760 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
761 iph.ip_ttl = OSPF_VL_IP_TTL;
762 else
763 iph.ip_ttl = OSPF_IP_TTL;
764 iph.ip_p = IPPROTO_OSPFIGP;
765 iph.ip_sum = 0;
766 iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
767 iph.ip_dst.s_addr = op->dst.s_addr;
768
769 memset(&msg, 0, sizeof(msg));
770 msg.msg_name = (caddr_t)&sa_dst;
771 msg.msg_namelen = sizeof(sa_dst);
772 msg.msg_iov = iov;
773 msg.msg_iovlen = 2;
774
775 iov[0].iov_base = (char *)&iph;
776 iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT;
777 iov[1].iov_base = stream_pnt(op->s);
778 iov[1].iov_len = op->length;
779
780 #ifdef GNU_LINUX
781 msg.msg_control = (caddr_t)cm;
782 cm->cmsg_level = SOL_IP;
783 cm->cmsg_type = IP_PKTINFO;
784 cm->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
785 pi = (struct in_pktinfo *)CMSG_DATA(cm);
786 pi->ipi_ifindex = oi->ifp->ifindex;
787
788 msg.msg_controllen = cm->cmsg_len;
789 #endif
790
791 /* Sadly we can not rely on kernels to fragment packets
792 * because of either IP_HDRINCL and/or multicast
793 * destination being set.
794 */
795
796 #ifdef WANT_OSPF_WRITE_FRAGMENT
797 if (op->length > maxdatasize)
798 ospf_write_frags(ospf->fd, op, &iph, &msg, maxdatasize,
799 oi->ifp->mtu, flags, type);
800 #endif /* WANT_OSPF_WRITE_FRAGMENT */
801
802 /* send final fragment (could be first) */
803 sockopt_iphdrincl_swab_htosys(&iph);
804 ret = sendmsg(ospf->fd, &msg, flags);
805 sockopt_iphdrincl_swab_systoh(&iph);
806 if (IS_DEBUG_OSPF_EVENT)
807 zlog_debug(
808 "ospf_write to %s, "
809 "id %d, off %d, len %d, interface %s, mtu %u:",
810 inet_ntoa(iph.ip_dst), iph.ip_id, iph.ip_off,
811 iph.ip_len, oi->ifp->name, oi->ifp->mtu);
812
813 if (ret < 0)
814 flog_err(
815 EC_LIB_SOCKET,
816 "*** sendmsg in ospf_write failed to %s, "
817 "id %d, off %d, len %d, interface %s, mtu %u: %s",
818 inet_ntoa(iph.ip_dst), iph.ip_id, iph.ip_off,
819 iph.ip_len, oi->ifp->name, oi->ifp->mtu,
820 safe_strerror(errno));
821
822 /* Show debug sending packet. */
823 if (IS_DEBUG_OSPF_PACKET(type - 1, SEND)) {
824 if (IS_DEBUG_OSPF_PACKET(type - 1, DETAIL)) {
825 zlog_debug(
826 "-----------------------------------------------------");
827 ospf_ip_header_dump(&iph);
828 stream_set_getp(op->s, 0);
829 ospf_packet_dump(op->s);
830 }
831
832 zlog_debug("%s sent to [%s] via [%s].",
833 lookup_msg(ospf_packet_type_str, type, NULL),
834 inet_ntoa(op->dst), IF_NAME(oi));
835
836 if (IS_DEBUG_OSPF_PACKET(type - 1, DETAIL))
837 zlog_debug(
838 "-----------------------------------------------------");
839 }
840
841 switch (type) {
842 case OSPF_MSG_HELLO:
843 oi->hello_out++;
844 break;
845 case OSPF_MSG_DB_DESC:
846 oi->db_desc_out++;
847 break;
848 case OSPF_MSG_LS_REQ:
849 oi->ls_req_out++;
850 break;
851 case OSPF_MSG_LS_UPD:
852 oi->ls_upd_out++;
853 break;
854 case OSPF_MSG_LS_ACK:
855 oi->ls_ack_out++;
856 break;
857 default:
858 break;
859 }
860
861 /* Now delete packet from queue. */
862 ospf_packet_delete(oi);
863
864 /* Move this interface to the tail of write_q to
865 serve everyone in a round robin fashion */
866 list_delete_node(ospf->oi_write_q, node);
867 if (ospf_fifo_head(oi->obuf) == NULL) {
868 oi->on_write_q = 0;
869 last_serviced_oi = NULL;
870 oi = NULL;
871 } else {
872 listnode_add(ospf->oi_write_q, oi);
873 }
874
875 /* Setup to service from the head of the queue again */
876 if (!list_isempty(ospf->oi_write_q)) {
877 node = listhead(ospf->oi_write_q);
878 oi = listgetdata(node);
879 }
880 }
881
882 /* If packets still remain in queue, call write thread. */
883 if (!list_isempty(ospf->oi_write_q))
884 thread_add_write(master, ospf_write, ospf, ospf->fd,
885 &ospf->t_write);
886
887 return 0;
888 }
889
890 /* OSPF Hello message read -- RFC2328 Section 10.5. */
891 static void ospf_hello(struct ip *iph, struct ospf_header *ospfh,
892 struct stream *s, struct ospf_interface *oi, int size)
893 {
894 struct ospf_hello *hello;
895 struct ospf_neighbor *nbr;
896 int old_state;
897 struct prefix p;
898
899 /* increment statistics. */
900 oi->hello_in++;
901
902 hello = (struct ospf_hello *)stream_pnt(s);
903
904 /* If Hello is myself, silently discard. */
905 if (IPV4_ADDR_SAME(&ospfh->router_id, &oi->ospf->router_id)) {
906 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV)) {
907 zlog_debug(
908 "ospf_header[%s/%s]: selforiginated, "
909 "dropping.",
910 lookup_msg(ospf_packet_type_str, ospfh->type,
911 NULL),
912 inet_ntoa(iph->ip_src));
913 }
914 return;
915 }
916
917 /* get neighbor prefix. */
918 p.family = AF_INET;
919 p.prefixlen = ip_masklen(hello->network_mask);
920 p.u.prefix4 = iph->ip_src;
921
922 /* Compare network mask. */
923 /* Checking is ignored for Point-to-Point and Virtual link. */
924 if (oi->type != OSPF_IFTYPE_POINTOPOINT
925 && oi->type != OSPF_IFTYPE_VIRTUALLINK)
926 if (oi->address->prefixlen != p.prefixlen) {
927 flog_warn(
928 EC_OSPF_PACKET,
929 "Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
930 inet_ntoa(ospfh->router_id), IF_NAME(oi),
931 (int)oi->address->prefixlen, (int)p.prefixlen);
932 return;
933 }
934
935 /* Compare Router Dead Interval. */
936 if (OSPF_IF_PARAM(oi, v_wait) != ntohl(hello->dead_interval)) {
937 flog_warn(EC_OSPF_PACKET,
938 "Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
939 "(expected %u, but received %u).",
940 inet_ntoa(ospfh->router_id),
941 OSPF_IF_PARAM(oi, v_wait),
942 ntohl(hello->dead_interval));
943 return;
944 }
945
946 /* Compare Hello Interval - ignored if fast-hellos are set. */
947 if (OSPF_IF_PARAM(oi, fast_hello) == 0) {
948 if (OSPF_IF_PARAM(oi, v_hello)
949 != ntohs(hello->hello_interval)) {
950 flog_warn(
951 EC_OSPF_PACKET,
952 "Packet %s [Hello:RECV]: HelloInterval mismatch "
953 "(expected %u, but received %u).",
954 inet_ntoa(ospfh->router_id),
955 OSPF_IF_PARAM(oi, v_hello),
956 ntohs(hello->hello_interval));
957 return;
958 }
959 }
960
961 if (IS_DEBUG_OSPF_EVENT)
962 zlog_debug("Packet %s [Hello:RECV]: Options %s vrf %s",
963 inet_ntoa(ospfh->router_id),
964 ospf_options_dump(hello->options),
965 ospf_vrf_id_to_name(oi->ospf->vrf_id));
966
967 /* Compare options. */
968 #define REJECT_IF_TBIT_ON 1 /* XXX */
969 #ifdef REJECT_IF_TBIT_ON
970 if (CHECK_FLAG(hello->options, OSPF_OPTION_MT)) {
971 /*
972 * This router does not support non-zero TOS.
973 * Drop this Hello packet not to establish neighbor
974 * relationship.
975 */
976 flog_warn(EC_OSPF_PACKET,
977 "Packet %s [Hello:RECV]: T-bit on, drop it.",
978 inet_ntoa(ospfh->router_id));
979 return;
980 }
981 #endif /* REJECT_IF_TBIT_ON */
982
983 if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)
984 && CHECK_FLAG(hello->options, OSPF_OPTION_O)) {
985 /*
986 * This router does know the correct usage of O-bit
987 * the bit should be set in DD packet only.
988 */
989 flog_warn(EC_OSPF_PACKET,
990 "Packet %s [Hello:RECV]: O-bit abuse?",
991 inet_ntoa(ospfh->router_id));
992 #ifdef STRICT_OBIT_USAGE_CHECK
993 return; /* Reject this packet. */
994 #else /* STRICT_OBIT_USAGE_CHECK */
995 UNSET_FLAG(hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
996 #endif /* STRICT_OBIT_USAGE_CHECK */
997 }
998
999 /* new for NSSA is to ensure that NP is on and E is off */
1000
1001 if (oi->area->external_routing == OSPF_AREA_NSSA) {
1002 if (!(CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_NP)
1003 && CHECK_FLAG(hello->options, OSPF_OPTION_NP)
1004 && !CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_E)
1005 && !CHECK_FLAG(hello->options, OSPF_OPTION_E))) {
1006 flog_warn(
1007 EC_OSPF_PACKET,
1008 "NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x",
1009 inet_ntoa(ospfh->router_id), OPTIONS(oi),
1010 hello->options);
1011 return;
1012 }
1013 if (IS_DEBUG_OSPF_NSSA)
1014 zlog_debug("NSSA-Hello:RECV:Packet from %s:",
1015 inet_ntoa(ospfh->router_id));
1016 } else
1017 /* The setting of the E-bit found in the Hello Packet's Options
1018 field must match this area's ExternalRoutingCapability A
1019 mismatch causes processing to stop and the packet to be
1020 dropped. The setting of the rest of the bits in the Hello
1021 Packet's Options field should be ignored. */
1022 if (CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_E)
1023 != CHECK_FLAG(hello->options, OSPF_OPTION_E)) {
1024 flog_warn(
1025 EC_OSPF_PACKET,
1026 "Packet %s [Hello:RECV]: my options: %x, his options %x",
1027 inet_ntoa(ospfh->router_id), OPTIONS(oi),
1028 hello->options);
1029 return;
1030 }
1031
1032 /* get neighbour struct */
1033 nbr = ospf_nbr_get(oi, ospfh, iph, &p);
1034
1035 /* neighbour must be valid, ospf_nbr_get creates if none existed */
1036 assert(nbr);
1037
1038 old_state = nbr->state;
1039
1040 /* Add event to thread. */
1041 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_PacketReceived);
1042
1043 /* RFC2328 Section 9.5.1
1044 If the router is not eligible to become Designated Router,
1045 (snip) It must also send an Hello Packet in reply to an
1046 Hello Packet received from any eligible neighbor (other than
1047 the current Designated Router and Backup Designated Router). */
1048 if (oi->type == OSPF_IFTYPE_NBMA)
1049 if (PRIORITY(oi) == 0 && hello->priority > 0
1050 && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
1051 && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
1052 OSPF_NSM_TIMER_ON(nbr->t_hello_reply,
1053 ospf_hello_reply_timer,
1054 OSPF_HELLO_REPLY_DELAY);
1055
1056 /* on NBMA network type, it happens to receive bidirectional Hello
1057 packet
1058 without advance 1-Way Received event.
1059 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
1060 if (oi->type == OSPF_IFTYPE_NBMA
1061 && (old_state == NSM_Down || old_state == NSM_Attempt)) {
1062 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_OneWayReceived);
1063 nbr->priority = hello->priority;
1064 nbr->d_router = hello->d_router;
1065 nbr->bd_router = hello->bd_router;
1066 return;
1067 }
1068
1069 if (ospf_nbr_bidirectional(&oi->ospf->router_id, hello->neighbors,
1070 size - OSPF_HELLO_MIN_SIZE)) {
1071 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_TwoWayReceived);
1072 nbr->options |= hello->options;
1073 } else {
1074 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_OneWayReceived);
1075 /* Set neighbor information. */
1076 nbr->priority = hello->priority;
1077 nbr->d_router = hello->d_router;
1078 nbr->bd_router = hello->bd_router;
1079 return;
1080 }
1081
1082 /* If neighbor itself declares DR and no BDR exists,
1083 cause event BackupSeen */
1084 if (IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->d_router))
1085 if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
1086 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_BackupSeen);
1087
1088 /* neighbor itself declares BDR. */
1089 if (oi->state == ISM_Waiting
1090 && IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->bd_router))
1091 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_BackupSeen);
1092
1093 /* had not previously. */
1094 if ((IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->d_router)
1095 && IPV4_ADDR_CMP(&nbr->address.u.prefix4, &nbr->d_router))
1096 || (IPV4_ADDR_CMP(&nbr->address.u.prefix4, &hello->d_router)
1097 && IPV4_ADDR_SAME(&nbr->address.u.prefix4, &nbr->d_router)))
1098 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
1099
1100 /* had not previously. */
1101 if ((IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->bd_router)
1102 && IPV4_ADDR_CMP(&nbr->address.u.prefix4, &nbr->bd_router))
1103 || (IPV4_ADDR_CMP(&nbr->address.u.prefix4, &hello->bd_router)
1104 && IPV4_ADDR_SAME(&nbr->address.u.prefix4, &nbr->bd_router)))
1105 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
1106
1107 /* Neighbor priority check. */
1108 if (nbr->priority >= 0 && nbr->priority != hello->priority)
1109 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
1110
1111 /* Set neighbor information. */
1112 nbr->priority = hello->priority;
1113 nbr->d_router = hello->d_router;
1114 nbr->bd_router = hello->bd_router;
1115 }
1116
1117 /* Save DD flags/options/Seqnum received. */
1118 static void ospf_db_desc_save_current(struct ospf_neighbor *nbr,
1119 struct ospf_db_desc *dd)
1120 {
1121 nbr->last_recv.flags = dd->flags;
1122 nbr->last_recv.options = dd->options;
1123 nbr->last_recv.dd_seqnum = ntohl(dd->dd_seqnum);
1124 }
1125
1126 /* Process rest of DD packet. */
1127 static void ospf_db_desc_proc(struct stream *s, struct ospf_interface *oi,
1128 struct ospf_neighbor *nbr,
1129 struct ospf_db_desc *dd, uint16_t size)
1130 {
1131 struct ospf_lsa *new, *find;
1132 struct lsa_header *lsah;
1133
1134 stream_forward_getp(s, OSPF_DB_DESC_MIN_SIZE);
1135 for (size -= OSPF_DB_DESC_MIN_SIZE; size >= OSPF_LSA_HEADER_SIZE;
1136 size -= OSPF_LSA_HEADER_SIZE) {
1137 lsah = (struct lsa_header *)stream_pnt(s);
1138 stream_forward_getp(s, OSPF_LSA_HEADER_SIZE);
1139
1140 /* Unknown LS type. */
1141 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) {
1142 flog_warn(EC_OSPF_PACKET,
1143 "Packet [DD:RECV]: Unknown LS type %d.",
1144 lsah->type);
1145 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1146 return;
1147 }
1148
1149 if (IS_OPAQUE_LSA(lsah->type)
1150 && !CHECK_FLAG(nbr->options, OSPF_OPTION_O)) {
1151 flog_warn(EC_OSPF_PACKET,
1152 "LSA[Type%d:%s]: Opaque capability mismatch?",
1153 lsah->type, inet_ntoa(lsah->id));
1154 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1155 return;
1156 }
1157
1158 switch (lsah->type) {
1159 case OSPF_AS_EXTERNAL_LSA:
1160 case OSPF_OPAQUE_AS_LSA:
1161 /* Check for stub area. Reject if AS-External from stub
1162 but
1163 allow if from NSSA. */
1164 if (oi->area->external_routing == OSPF_AREA_STUB) {
1165 flog_warn(
1166 EC_OSPF_PACKET,
1167 "Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
1168 lsah->type, inet_ntoa(lsah->id),
1169 (oi->area->external_routing
1170 == OSPF_AREA_STUB)
1171 ? "STUB"
1172 : "NSSA");
1173 OSPF_NSM_EVENT_SCHEDULE(nbr,
1174 NSM_SeqNumberMismatch);
1175 return;
1176 }
1177 break;
1178 default:
1179 break;
1180 }
1181
1182 /* Create LS-request object. */
1183 new = ospf_ls_request_new(lsah);
1184
1185 /* Lookup received LSA, then add LS request list. */
1186 find = ospf_lsa_lookup_by_header(oi->area, lsah);
1187
1188 /* ospf_lsa_more_recent is fine with NULL pointers */
1189 switch (ospf_lsa_more_recent(find, new)) {
1190 case -1:
1191 /* Neighbour has a more recent LSA, we must request it
1192 */
1193 ospf_ls_request_add(nbr, new);
1194 /* fallthru */
1195 case 0:
1196 /* If we have a copy of this LSA, it's either less
1197 * recent
1198 * and we're requesting it from neighbour (the case
1199 * above), or
1200 * it's as recent and we both have same copy (this
1201 * case).
1202 *
1203 * In neither of these two cases is there any point in
1204 * describing our copy of the LSA to the neighbour in a
1205 * DB-Summary packet, if we're still intending to do so.
1206 *
1207 * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
1208 * backward compatible optimisation to OSPF DB Exchange
1209 * /
1210 * DB Description process implemented here.
1211 */
1212 if (find)
1213 ospf_lsdb_delete(&nbr->db_sum, find);
1214 ospf_lsa_discard(new);
1215 break;
1216 default:
1217 /* We have the more recent copy, nothing specific to do:
1218 * - no need to request neighbours stale copy
1219 * - must leave DB summary list copy alone
1220 */
1221 if (IS_DEBUG_OSPF_EVENT)
1222 zlog_debug(
1223 "Packet [DD:RECV]: LSA received Type %d, "
1224 "ID %s is not recent.",
1225 lsah->type, inet_ntoa(lsah->id));
1226 ospf_lsa_discard(new);
1227 }
1228 }
1229
1230 /* Master */
1231 if (IS_SET_DD_MS(nbr->dd_flags)) {
1232 nbr->dd_seqnum++;
1233
1234 /* Both sides have no More, then we're done with Exchange */
1235 if (!IS_SET_DD_M(dd->flags) && !IS_SET_DD_M(nbr->dd_flags))
1236 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_ExchangeDone);
1237 else
1238 ospf_db_desc_send(nbr);
1239 }
1240 /* Slave */
1241 else {
1242 nbr->dd_seqnum = ntohl(dd->dd_seqnum);
1243
1244 /* Send DD packet in reply.
1245 *
1246 * Must be done to acknowledge the Master's DD, regardless of
1247 * whether we have more LSAs ourselves to describe.
1248 *
1249 * This function will clear the 'More' bit, if after this DD
1250 * we have no more LSAs to describe to the master..
1251 */
1252 ospf_db_desc_send(nbr);
1253
1254 /* Slave can raise ExchangeDone now, if master is also done */
1255 if (!IS_SET_DD_M(dd->flags) && !IS_SET_DD_M(nbr->dd_flags))
1256 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_ExchangeDone);
1257 }
1258
1259 /* Save received neighbor values from DD. */
1260 ospf_db_desc_save_current(nbr, dd);
1261
1262 if (!nbr->t_ls_req)
1263 ospf_ls_req_send(nbr);
1264 }
1265
1266 static int ospf_db_desc_is_dup(struct ospf_db_desc *dd,
1267 struct ospf_neighbor *nbr)
1268 {
1269 /* Is DD duplicated? */
1270 if (dd->options == nbr->last_recv.options
1271 && dd->flags == nbr->last_recv.flags
1272 && dd->dd_seqnum == htonl(nbr->last_recv.dd_seqnum))
1273 return 1;
1274
1275 return 0;
1276 }
1277
1278 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
1279 static void ospf_db_desc(struct ip *iph, struct ospf_header *ospfh,
1280 struct stream *s, struct ospf_interface *oi,
1281 uint16_t size)
1282 {
1283 struct ospf_db_desc *dd;
1284 struct ospf_neighbor *nbr;
1285
1286 /* Increment statistics. */
1287 oi->db_desc_in++;
1288
1289 dd = (struct ospf_db_desc *)stream_pnt(s);
1290
1291 nbr = ospf_nbr_lookup(oi, iph, ospfh);
1292 if (nbr == NULL) {
1293 flog_warn(EC_OSPF_PACKET, "Packet[DD]: Unknown Neighbor %s",
1294 inet_ntoa(ospfh->router_id));
1295 return;
1296 }
1297
1298 /* Check MTU. */
1299 if ((OSPF_IF_PARAM(oi, mtu_ignore) == 0)
1300 && (ntohs(dd->mtu) > oi->ifp->mtu)) {
1301 flog_warn(
1302 EC_OSPF_PACKET,
1303 "Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
1304 inet_ntoa(nbr->router_id), ntohs(dd->mtu), IF_NAME(oi),
1305 oi->ifp->mtu);
1306 return;
1307 }
1308
1309 /*
1310 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is
1311 * not
1312 * required. In fact at least JunOS sends DD packets with P bit clear.
1313 * Until proper solution is developped, this hack should help.
1314 *
1315 * Update: According to the RFCs, N bit is specified /only/ for Hello
1316 * options, unfortunately its use in DD options is not specified. Hence
1317 * some
1318 * implementations follow E-bit semantics and set it in DD options, and
1319 * some
1320 * treat it as unspecified and hence follow the directive "default for
1321 * options is clear", ie unset.
1322 *
1323 * Reset the flag, as ospfd follows E-bit semantics.
1324 */
1325 if ((oi->area->external_routing == OSPF_AREA_NSSA)
1326 && (CHECK_FLAG(nbr->options, OSPF_OPTION_NP))
1327 && (!CHECK_FLAG(dd->options, OSPF_OPTION_NP))) {
1328 if (IS_DEBUG_OSPF_EVENT)
1329 zlog_debug(
1330 "Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1331 inet_ntoa(nbr->router_id));
1332 SET_FLAG(dd->options, OSPF_OPTION_NP);
1333 }
1334
1335 #ifdef REJECT_IF_TBIT_ON
1336 if (CHECK_FLAG(dd->options, OSPF_OPTION_MT)) {
1337 /*
1338 * In Hello protocol, optional capability must have checked
1339 * to prevent this T-bit enabled router be my neighbor.
1340 */
1341 flog_warn(EC_OSPF_PACKET, "Packet[DD]: Neighbor %s: T-bit on?",
1342 inet_ntoa(nbr->router_id));
1343 return;
1344 }
1345 #endif /* REJECT_IF_TBIT_ON */
1346
1347 if (CHECK_FLAG(dd->options, OSPF_OPTION_O)
1348 && !CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)) {
1349 /*
1350 * This node is not configured to handle O-bit, for now.
1351 * Clear it to ignore unsupported capability proposed by
1352 * neighbor.
1353 */
1354 UNSET_FLAG(dd->options, OSPF_OPTION_O);
1355 }
1356
1357 /* Add event to thread. */
1358 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_PacketReceived);
1359
1360 /* Process DD packet by neighbor status. */
1361 switch (nbr->state) {
1362 case NSM_Down:
1363 case NSM_Attempt:
1364 case NSM_TwoWay:
1365 flog_warn(
1366 EC_OSPF_PACKET,
1367 "Packet[DD]: Neighbor %s state is %s, packet discarded.",
1368 inet_ntoa(nbr->router_id),
1369 lookup_msg(ospf_nsm_state_msg, nbr->state, NULL));
1370 break;
1371 case NSM_Init:
1372 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_TwoWayReceived);
1373 /* If the new state is ExStart, the processing of the current
1374 packet should then continue in this new state by falling
1375 through to case ExStart below. */
1376 if (nbr->state != NSM_ExStart)
1377 break;
1378 /* fallthru */
1379 case NSM_ExStart:
1380 /* Initial DBD */
1381 if ((IS_SET_DD_ALL(dd->flags) == OSPF_DD_FLAG_ALL)
1382 && (size == OSPF_DB_DESC_MIN_SIZE)) {
1383 if (IPV4_ADDR_CMP(&nbr->router_id, &oi->ospf->router_id)
1384 > 0) {
1385 /* We're Slave---obey */
1386 if (CHECK_FLAG(oi->ospf->config,
1387 OSPF_LOG_ADJACENCY_DETAIL))
1388 zlog_info(
1389 "Packet[DD]: Neighbor %s Negotiation done (Slave).",
1390 inet_ntoa(nbr->router_id));
1391
1392 nbr->dd_seqnum = ntohl(dd->dd_seqnum);
1393
1394 /* Reset I/MS */
1395 UNSET_FLAG(nbr->dd_flags,
1396 (OSPF_DD_FLAG_MS | OSPF_DD_FLAG_I));
1397 } else {
1398 /* We're Master, ignore the initial DBD from
1399 * Slave */
1400 if (CHECK_FLAG(oi->ospf->config,
1401 OSPF_LOG_ADJACENCY_DETAIL))
1402 zlog_info(
1403 "Packet[DD]: Neighbor %s: Initial DBD from Slave, "
1404 "ignoring.",
1405 inet_ntoa(nbr->router_id));
1406 break;
1407 }
1408 }
1409 /* Ack from the Slave */
1410 else if (!IS_SET_DD_MS(dd->flags) && !IS_SET_DD_I(dd->flags)
1411 && ntohl(dd->dd_seqnum) == nbr->dd_seqnum
1412 && IPV4_ADDR_CMP(&nbr->router_id, &oi->ospf->router_id)
1413 < 0) {
1414 zlog_info(
1415 "Packet[DD]: Neighbor %s Negotiation done (Master).",
1416 inet_ntoa(nbr->router_id));
1417 /* Reset I, leaving MS */
1418 UNSET_FLAG(nbr->dd_flags, OSPF_DD_FLAG_I);
1419 } else {
1420 flog_warn(EC_OSPF_PACKET,
1421 "Packet[DD]: Neighbor %s Negotiation fails.",
1422 inet_ntoa(nbr->router_id));
1423 break;
1424 }
1425
1426 /* This is where the real Options are saved */
1427 nbr->options = dd->options;
1428
1429 if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)) {
1430 if (IS_DEBUG_OSPF_EVENT)
1431 zlog_debug(
1432 "Neighbor[%s] is %sOpaque-capable.",
1433 inet_ntoa(nbr->router_id),
1434 CHECK_FLAG(nbr->options, OSPF_OPTION_O)
1435 ? ""
1436 : "NOT ");
1437
1438 if (!CHECK_FLAG(nbr->options, OSPF_OPTION_O)
1439 && IPV4_ADDR_SAME(&DR(oi),
1440 &nbr->address.u.prefix4)) {
1441 flog_warn(
1442 EC_OSPF_PACKET,
1443 "DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.",
1444 inet_ntoa(nbr->router_id));
1445 /* This situation is undesirable, but not a real
1446 * error. */
1447 }
1448 }
1449
1450 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_NegotiationDone);
1451
1452 /* continue processing rest of packet. */
1453 ospf_db_desc_proc(s, oi, nbr, dd, size);
1454 break;
1455 case NSM_Exchange:
1456 if (ospf_db_desc_is_dup(dd, nbr)) {
1457 if (IS_SET_DD_MS(nbr->dd_flags))
1458 /* Master: discard duplicated DD packet. */
1459 zlog_info(
1460 "Packet[DD] (Master): Neighbor %s packet duplicated.",
1461 inet_ntoa(nbr->router_id));
1462 else
1463 /* Slave: cause to retransmit the last Database
1464 Description. */
1465 {
1466 zlog_info(
1467 "Packet[DD] [Slave]: Neighbor %s packet duplicated.",
1468 inet_ntoa(nbr->router_id));
1469 ospf_db_desc_resend(nbr);
1470 }
1471 break;
1472 }
1473
1474 /* Otherwise DD packet should be checked. */
1475 /* Check Master/Slave bit mismatch */
1476 if (IS_SET_DD_MS(dd->flags)
1477 != IS_SET_DD_MS(nbr->last_recv.flags)) {
1478 flog_warn(EC_OSPF_PACKET,
1479 "Packet[DD]: Neighbor %s MS-bit mismatch.",
1480 inet_ntoa(nbr->router_id));
1481 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1482 if (IS_DEBUG_OSPF_EVENT)
1483 zlog_debug(
1484 "Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1485 dd->flags, nbr->dd_flags);
1486 break;
1487 }
1488
1489 /* Check initialize bit is set. */
1490 if (IS_SET_DD_I(dd->flags)) {
1491 zlog_info("Packet[DD]: Neighbor %s I-bit set.",
1492 inet_ntoa(nbr->router_id));
1493 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1494 break;
1495 }
1496
1497 /* Check DD Options. */
1498 if (dd->options != nbr->options) {
1499 #ifdef ORIGINAL_CODING
1500 /* Save the new options for debugging */
1501 nbr->options = dd->options;
1502 #endif /* ORIGINAL_CODING */
1503 flog_warn(EC_OSPF_PACKET,
1504 "Packet[DD]: Neighbor %s options mismatch.",
1505 inet_ntoa(nbr->router_id));
1506 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1507 break;
1508 }
1509
1510 /* Check DD sequence number. */
1511 if ((IS_SET_DD_MS(nbr->dd_flags)
1512 && ntohl(dd->dd_seqnum) != nbr->dd_seqnum)
1513 || (!IS_SET_DD_MS(nbr->dd_flags)
1514 && ntohl(dd->dd_seqnum) != nbr->dd_seqnum + 1)) {
1515 flog_warn(
1516 EC_OSPF_PACKET,
1517 "Packet[DD]: Neighbor %s sequence number mismatch.",
1518 inet_ntoa(nbr->router_id));
1519 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1520 break;
1521 }
1522
1523 /* Continue processing rest of packet. */
1524 ospf_db_desc_proc(s, oi, nbr, dd, size);
1525 break;
1526 case NSM_Loading:
1527 case NSM_Full:
1528 if (ospf_db_desc_is_dup(dd, nbr)) {
1529 if (IS_SET_DD_MS(nbr->dd_flags)) {
1530 /* Master should discard duplicate DD packet. */
1531 zlog_info(
1532 "Packet[DD]: Neighbor %s duplicated, "
1533 "packet discarded.",
1534 inet_ntoa(nbr->router_id));
1535 break;
1536 } else {
1537 if (monotime_since(&nbr->last_send_ts, NULL)
1538 < nbr->v_inactivity * 1000000LL) {
1539 /* In states Loading and Full the slave
1540 must resend
1541 its last Database Description packet
1542 in response to
1543 duplicate Database Description
1544 packets received
1545 from the master. For this reason the
1546 slave must
1547 wait RouterDeadInterval seconds
1548 before freeing the
1549 last Database Description packet.
1550 Reception of a
1551 Database Description packet from the
1552 master after
1553 this interval will generate a
1554 SeqNumberMismatch
1555 neighbor event. RFC2328 Section 10.8
1556 */
1557 ospf_db_desc_resend(nbr);
1558 break;
1559 }
1560 }
1561 }
1562
1563 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
1564 break;
1565 default:
1566 flog_warn(EC_OSPF_PACKET,
1567 "Packet[DD]: Neighbor %s NSM illegal status %u.",
1568 inet_ntoa(nbr->router_id), nbr->state);
1569 break;
1570 }
1571 }
1572
1573 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1574
1575 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1576 static void ospf_ls_req(struct ip *iph, struct ospf_header *ospfh,
1577 struct stream *s, struct ospf_interface *oi,
1578 uint16_t size)
1579 {
1580 struct ospf_neighbor *nbr;
1581 uint32_t ls_type;
1582 struct in_addr ls_id;
1583 struct in_addr adv_router;
1584 struct ospf_lsa *find;
1585 struct list *ls_upd;
1586 unsigned int length;
1587
1588 /* Increment statistics. */
1589 oi->ls_req_in++;
1590
1591 nbr = ospf_nbr_lookup(oi, iph, ospfh);
1592 if (nbr == NULL) {
1593 flog_warn(EC_OSPF_PACKET,
1594 "Link State Request: Unknown Neighbor %s.",
1595 inet_ntoa(ospfh->router_id));
1596 return;
1597 }
1598
1599 /* Add event to thread. */
1600 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_PacketReceived);
1601
1602 /* Neighbor State should be Exchange or later. */
1603 if (nbr->state != NSM_Exchange && nbr->state != NSM_Loading
1604 && nbr->state != NSM_Full) {
1605 flog_warn(
1606 EC_OSPF_PACKET,
1607 "Link State Request received from %s: Neighbor state is %s, packet discarded.",
1608 inet_ntoa(ospfh->router_id),
1609 lookup_msg(ospf_nsm_state_msg, nbr->state, NULL));
1610 return;
1611 }
1612
1613 /* Send Link State Update for ALL requested LSAs. */
1614 ls_upd = list_new();
1615 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1616
1617 while (size >= OSPF_LSA_KEY_SIZE) {
1618 /* Get one slice of Link State Request. */
1619 ls_type = stream_getl(s);
1620 ls_id.s_addr = stream_get_ipv4(s);
1621 adv_router.s_addr = stream_get_ipv4(s);
1622
1623 /* Verify LSA type. */
1624 if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA) {
1625 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
1626 list_delete(&ls_upd);
1627 return;
1628 }
1629
1630 /* Search proper LSA in LSDB. */
1631 find = ospf_lsa_lookup(oi->ospf, oi->area, ls_type, ls_id,
1632 adv_router);
1633 if (find == NULL) {
1634 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
1635 list_delete(&ls_upd);
1636 return;
1637 }
1638
1639 /* Packet overflows MTU size, send immediately. */
1640 if (length + ntohs(find->data->length) > ospf_packet_max(oi)) {
1641 if (oi->type == OSPF_IFTYPE_NBMA)
1642 ospf_ls_upd_send(nbr, ls_upd,
1643 OSPF_SEND_PACKET_DIRECT, 0);
1644 else
1645 ospf_ls_upd_send(nbr, ls_upd,
1646 OSPF_SEND_PACKET_INDIRECT, 0);
1647
1648 /* Only remove list contents. Keep ls_upd. */
1649 list_delete_all_node(ls_upd);
1650
1651 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1652 }
1653
1654 /* Append LSA to update list. */
1655 listnode_add(ls_upd, find);
1656 length += ntohs(find->data->length);
1657
1658 size -= OSPF_LSA_KEY_SIZE;
1659 }
1660
1661 /* Send rest of Link State Update. */
1662 if (listcount(ls_upd) > 0) {
1663 if (oi->type == OSPF_IFTYPE_NBMA)
1664 ospf_ls_upd_send(nbr, ls_upd, OSPF_SEND_PACKET_DIRECT,
1665 0);
1666 else
1667 ospf_ls_upd_send(nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT,
1668 0);
1669
1670 list_delete(&ls_upd);
1671 } else
1672 list_delete(&ls_upd);
1673 }
1674
1675 /* Get the list of LSAs from Link State Update packet.
1676 And process some validation -- RFC2328 Section 13. (1)-(2). */
1677 static struct list *ospf_ls_upd_list_lsa(struct ospf_neighbor *nbr,
1678 struct stream *s,
1679 struct ospf_interface *oi, size_t size)
1680 {
1681 uint16_t count, sum;
1682 uint32_t length;
1683 struct lsa_header *lsah;
1684 struct ospf_lsa *lsa;
1685 struct list *lsas;
1686
1687 lsas = list_new();
1688
1689 count = stream_getl(s);
1690 size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
1691
1692 for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
1693 size -= length, stream_forward_getp(s, length), count--) {
1694 lsah = (struct lsa_header *)stream_pnt(s);
1695 length = ntohs(lsah->length);
1696
1697 if (length > size) {
1698 flog_warn(
1699 EC_OSPF_PACKET,
1700 "Link State Update: LSA length exceeds packet size.");
1701 break;
1702 }
1703
1704 /* Validate the LSA's LS checksum. */
1705 sum = lsah->checksum;
1706 if (!ospf_lsa_checksum_valid(lsah)) {
1707 /* (bug #685) more details in a one-line message make it
1708 * possible
1709 * to identify problem source on the one hand and to
1710 * have a better
1711 * chance to compress repeated messages in syslog on the
1712 * other */
1713 flog_warn(
1714 EC_OSPF_PACKET,
1715 "Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
1716 sum, lsah->checksum, inet_ntoa(lsah->id),
1717 inet_ntoa(nbr->src), inet_ntoa(nbr->router_id),
1718 inet_ntoa(lsah->adv_router));
1719 continue;
1720 }
1721
1722 /* Examine the LSA's LS type. */
1723 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) {
1724 flog_warn(EC_OSPF_PACKET,
1725 "Link State Update: Unknown LS type %d",
1726 lsah->type);
1727 continue;
1728 }
1729
1730 /*
1731 * What if the received LSA's age is greater than MaxAge?
1732 * Treat it as a MaxAge case -- endo.
1733 */
1734 if (ntohs(lsah->ls_age) > OSPF_LSA_MAXAGE)
1735 lsah->ls_age = htons(OSPF_LSA_MAXAGE);
1736
1737 if (CHECK_FLAG(nbr->options, OSPF_OPTION_O)) {
1738 #ifdef STRICT_OBIT_USAGE_CHECK
1739 if ((IS_OPAQUE_LSA(lsah->type)
1740 && !CHECK_FLAG(lsah->options, OSPF_OPTION_O))
1741 || (!IS_OPAQUE_LSA(lsah->type)
1742 && CHECK_FLAG(lsah->options, OSPF_OPTION_O))) {
1743 /*
1744 * This neighbor must know the exact usage of
1745 * O-bit;
1746 * the bit will be set in Type-9,10,11 LSAs
1747 * only.
1748 */
1749 flog_warn(EC_OSPF_PACKET,
1750 "LSA[Type%d:%s]: O-bit abuse?",
1751 lsah->type, inet_ntoa(lsah->id));
1752 continue;
1753 }
1754 #endif /* STRICT_OBIT_USAGE_CHECK */
1755
1756 /* Do not take in AS External Opaque-LSAs if we are a
1757 * stub. */
1758 if (lsah->type == OSPF_OPAQUE_AS_LSA
1759 && nbr->oi->area->external_routing
1760 != OSPF_AREA_DEFAULT) {
1761 if (IS_DEBUG_OSPF_EVENT)
1762 zlog_debug(
1763 "LSA[Type%d:%s]: We are a stub, don't take this LSA.",
1764 lsah->type,
1765 inet_ntoa(lsah->id));
1766 continue;
1767 }
1768 } else if (IS_OPAQUE_LSA(lsah->type)) {
1769 flog_warn(EC_OSPF_PACKET,
1770 "LSA[Type%d:%s]: Opaque capability mismatch?",
1771 lsah->type, inet_ntoa(lsah->id));
1772 continue;
1773 }
1774
1775 /* Create OSPF LSA instance. */
1776 lsa = ospf_lsa_new_and_data(length);
1777
1778 lsa->vrf_id = oi->ospf->vrf_id;
1779 /* We may wish to put some error checking if type NSSA comes in
1780 and area not in NSSA mode */
1781 switch (lsah->type) {
1782 case OSPF_AS_EXTERNAL_LSA:
1783 case OSPF_OPAQUE_AS_LSA:
1784 lsa->area = NULL;
1785 break;
1786 case OSPF_OPAQUE_LINK_LSA:
1787 lsa->oi = oi; /* Remember incoming interface for
1788 flooding control. */
1789 /* Fallthrough */
1790 default:
1791 lsa->area = oi->area;
1792 break;
1793 }
1794
1795 memcpy(lsa->data, lsah, length);
1796
1797 if (IS_DEBUG_OSPF_EVENT)
1798 zlog_debug(
1799 "LSA[Type%d:%s]: %p new LSA created with Link State Update",
1800 lsa->data->type, inet_ntoa(lsa->data->id),
1801 (void *)lsa);
1802 listnode_add(lsas, lsa);
1803 }
1804
1805 return lsas;
1806 }
1807
1808 /* Cleanup Update list. */
1809 static void ospf_upd_list_clean(struct list *lsas)
1810 {
1811 struct listnode *node, *nnode;
1812 struct ospf_lsa *lsa;
1813
1814 for (ALL_LIST_ELEMENTS(lsas, node, nnode, lsa))
1815 ospf_lsa_discard(lsa);
1816
1817 list_delete(&lsas);
1818 }
1819
1820 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1821 static void ospf_ls_upd(struct ospf *ospf, struct ip *iph,
1822 struct ospf_header *ospfh, struct stream *s,
1823 struct ospf_interface *oi, uint16_t size)
1824 {
1825 struct ospf_neighbor *nbr;
1826 struct list *lsas;
1827 struct listnode *node, *nnode;
1828 struct ospf_lsa *lsa = NULL;
1829 /* unsigned long ls_req_found = 0; */
1830
1831 /* Dis-assemble the stream, update each entry, re-encapsulate for
1832 * flooding */
1833
1834 /* Increment statistics. */
1835 oi->ls_upd_in++;
1836
1837 /* Check neighbor. */
1838 nbr = ospf_nbr_lookup(oi, iph, ospfh);
1839 if (nbr == NULL) {
1840 flog_warn(EC_OSPF_PACKET,
1841 "Link State Update: Unknown Neighbor %s on int: %s",
1842 inet_ntoa(ospfh->router_id), IF_NAME(oi));
1843 return;
1844 }
1845
1846 /* Add event to thread. */
1847 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_PacketReceived);
1848
1849 /* Check neighbor state. */
1850 if (nbr->state < NSM_Exchange) {
1851 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
1852 zlog_debug(
1853 "Link State Update: "
1854 "Neighbor[%s] state %s is less than Exchange",
1855 inet_ntoa(ospfh->router_id),
1856 lookup_msg(ospf_nsm_state_msg, nbr->state,
1857 NULL));
1858 return;
1859 }
1860
1861 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1862 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1863 * of section 13.
1864 */
1865 lsas = ospf_ls_upd_list_lsa(nbr, s, oi, size);
1866
1867 if (lsas == NULL)
1868 return;
1869 #define DISCARD_LSA(L, N) \
1870 { \
1871 if (IS_DEBUG_OSPF_EVENT) \
1872 zlog_debug( \
1873 "ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
1874 " Type-%d", \
1875 N, (void *)lsa, (int)lsa->data->type); \
1876 ospf_lsa_discard(L); \
1877 continue; \
1878 }
1879
1880 /* Process each LSA received in the one packet.
1881 *
1882 * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
1883 * text below are from the steps in RFC 2328, Section 13.
1884 */
1885 for (ALL_LIST_ELEMENTS(lsas, node, nnode, lsa)) {
1886 struct ospf_lsa *ls_ret, *current;
1887 int ret = 1;
1888
1889 if (IS_DEBUG_OSPF_NSSA) {
1890 char buf1[INET_ADDRSTRLEN];
1891 char buf2[INET_ADDRSTRLEN];
1892 char buf3[INET_ADDRSTRLEN];
1893
1894 zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
1895 lsa->data->type,
1896 inet_ntop(AF_INET, &ospfh->router_id, buf1,
1897 INET_ADDRSTRLEN),
1898 inet_ntop(AF_INET, &lsa->data->id, buf2,
1899 INET_ADDRSTRLEN),
1900 inet_ntop(AF_INET, &lsa->data->adv_router,
1901 buf3, INET_ADDRSTRLEN));
1902 }
1903
1904 listnode_delete(lsas,
1905 lsa); /* We don't need it in list anymore */
1906
1907 /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa()
1908 */
1909
1910 /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */
1911
1912 /* (3) Do not take in AS External LSAs if we are a stub or NSSA.
1913 */
1914
1915 /* Do not take in AS NSSA if this neighbor and we are not NSSA
1916 */
1917
1918 /* Do take in Type-7's if we are an NSSA */
1919
1920 /* If we are also an ABR, later translate them to a Type-5
1921 * packet */
1922
1923 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1924 translate them to a separate Type-5 packet. */
1925
1926 if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
1927 /* Reject from STUB or NSSA */
1928 if (nbr->oi->area->external_routing
1929 != OSPF_AREA_DEFAULT) {
1930 if (IS_DEBUG_OSPF_NSSA)
1931 zlog_debug(
1932 "Incoming External LSA Discarded: We are NSSA/STUB Area");
1933 DISCARD_LSA(lsa, 1);
1934 }
1935
1936 if (lsa->data->type == OSPF_AS_NSSA_LSA)
1937 if (nbr->oi->area->external_routing != OSPF_AREA_NSSA) {
1938 if (IS_DEBUG_OSPF_NSSA)
1939 zlog_debug(
1940 "Incoming NSSA LSA Discarded: Not NSSA Area");
1941 DISCARD_LSA(lsa, 2);
1942 }
1943
1944 /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
1945 if (lsa->data->type == OSPF_ROUTER_LSA)
1946 if (!IPV4_ADDR_SAME(&lsa->data->id,
1947 &lsa->data->adv_router)) {
1948 char buf1[INET_ADDRSTRLEN];
1949 char buf2[INET_ADDRSTRLEN];
1950 char buf3[INET_ADDRSTRLEN];
1951
1952 flog_err(EC_OSPF_ROUTER_LSA_MISMATCH,
1953 "Incoming Router-LSA from %s with "
1954 "Adv-ID[%s] != LS-ID[%s]",
1955 inet_ntop(AF_INET, &ospfh->router_id,
1956 buf1, INET_ADDRSTRLEN),
1957 inet_ntop(AF_INET, &lsa->data->id,
1958 buf2, INET_ADDRSTRLEN),
1959 inet_ntop(AF_INET,
1960 &lsa->data->adv_router, buf3,
1961 INET_ADDRSTRLEN));
1962 flog_err(
1963 EC_OSPF_DOMAIN_CORRUPT,
1964 "OSPF domain compromised by attack or corruption. "
1965 "Verify correct operation of -ALL- OSPF routers.");
1966 DISCARD_LSA(lsa, 0);
1967 }
1968
1969 /* Find the LSA in the current database. */
1970
1971 current = ospf_lsa_lookup_by_header(oi->area, lsa->data);
1972
1973 /* (4) If the LSA's LS age is equal to MaxAge, and there is
1974 currently
1975 no instance of the LSA in the router's link state database,
1976 and none of router's neighbors are in states Exchange or
1977 Loading,
1978 then take the following actions: */
1979
1980 if (IS_LSA_MAXAGE(lsa) && !current
1981 && ospf_check_nbr_status(oi->ospf)) {
1982 /* (4a) Response Link State Acknowledgment. */
1983 ospf_ls_ack_send(nbr, lsa);
1984
1985 /* (4b) Discard LSA. */
1986 if (IS_DEBUG_OSPF(lsa, LSA)) {
1987 zlog_debug(
1988 "Link State Update[%s]: LS age is equal to MaxAge.",
1989 dump_lsa_key(lsa));
1990 }
1991 DISCARD_LSA(lsa, 3);
1992 }
1993
1994 if (IS_OPAQUE_LSA(lsa->data->type)
1995 && IPV4_ADDR_SAME(&lsa->data->adv_router,
1996 &oi->ospf->router_id)) {
1997 /*
1998 * Even if initial flushing seems to be completed, there
1999 * might
2000 * be a case that self-originated LSA with MaxAge still
2001 * remain
2002 * in the routing domain.
2003 * Just send an LSAck message to cease retransmission.
2004 */
2005 if (IS_LSA_MAXAGE(lsa)) {
2006 zlog_info("LSA[%s]: Boomerang effect?",
2007 dump_lsa_key(lsa));
2008 ospf_ls_ack_send(nbr, lsa);
2009 ospf_lsa_discard(lsa);
2010
2011 if (current != NULL && !IS_LSA_MAXAGE(current))
2012 ospf_opaque_lsa_refresh_schedule(
2013 current);
2014 continue;
2015 }
2016
2017 /*
2018 * If an instance of self-originated Opaque-LSA is not
2019 * found
2020 * in the LSDB, there are some possible cases here.
2021 *
2022 * 1) This node lost opaque-capability after restart.
2023 * 2) Else, a part of opaque-type is no more supported.
2024 * 3) Else, a part of opaque-id is no more supported.
2025 *
2026 * Anyway, it is still this node's responsibility to
2027 * flush it.
2028 * Otherwise, the LSA instance remains in the routing
2029 * domain
2030 * until its age reaches to MaxAge.
2031 */
2032 /* XXX: We should deal with this for *ALL* LSAs, not
2033 * just opaque */
2034 if (current == NULL) {
2035 if (IS_DEBUG_OSPF_EVENT)
2036 zlog_debug(
2037 "LSA[%s]: Previously originated Opaque-LSA,"
2038 "not found in the LSDB.",
2039 dump_lsa_key(lsa));
2040
2041 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
2042
2043 ospf_opaque_self_originated_lsa_received(nbr,
2044 lsa);
2045 ospf_ls_ack_send(nbr, lsa);
2046
2047 continue;
2048 }
2049 }
2050
2051 /* It might be happen that received LSA is self-originated
2052 * network LSA, but
2053 * router ID is changed. So, we should check if LSA is a
2054 * network-LSA whose
2055 * Link State ID is one of the router's own IP interface
2056 * addresses but whose
2057 * Advertising Router is not equal to the router's own Router ID
2058 * According to RFC 2328 12.4.2 and 13.4 this LSA should be
2059 * flushed.
2060 */
2061
2062 if (lsa->data->type == OSPF_NETWORK_LSA) {
2063 struct listnode *oinode, *oinnode;
2064 struct ospf_interface *out_if;
2065 int Flag = 0;
2066
2067 for (ALL_LIST_ELEMENTS(oi->ospf->oiflist, oinode,
2068 oinnode, out_if)) {
2069 if (out_if == NULL)
2070 break;
2071
2072 if ((IPV4_ADDR_SAME(&out_if->address->u.prefix4,
2073 &lsa->data->id))
2074 && (!(IPV4_ADDR_SAME(
2075 &oi->ospf->router_id,
2076 &lsa->data->adv_router)))) {
2077 if (out_if->network_lsa_self) {
2078 ospf_lsa_flush_area(
2079 lsa, out_if->area);
2080 if (IS_DEBUG_OSPF_EVENT)
2081 zlog_debug(
2082 "ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
2083 (void *)lsa,
2084 (int)lsa->data
2085 ->type);
2086 ospf_lsa_discard(lsa);
2087 Flag = 1;
2088 }
2089 break;
2090 }
2091 }
2092 if (Flag)
2093 continue;
2094 }
2095
2096 /* (5) Find the instance of this LSA that is currently contained
2097 in the router's link state database. If there is no
2098 database copy, or the received LSA is more recent than
2099 the database copy the following steps must be performed.
2100 (The sub steps from RFC 2328 section 13 step (5) will be
2101 performed in
2102 ospf_flood() ) */
2103
2104 if (current == NULL
2105 || (ret = ospf_lsa_more_recent(current, lsa)) < 0) {
2106 /* CVE-2017-3224 */
2107 if (current && (lsa->data->ls_seqnum ==
2108 htonl(OSPF_MAX_SEQUENCE_NUMBER)
2109 && !IS_LSA_MAXAGE(lsa))) {
2110 zlog_debug(
2111 "Link State Update[%s]: has Max Seq but not MaxAge. Dropping it",
2112 dump_lsa_key(lsa));
2113
2114 DISCARD_LSA(lsa, 4);
2115 }
2116
2117 /* Actual flooding procedure. */
2118 if (ospf_flood(oi->ospf, nbr, current, lsa)
2119 < 0) /* Trap NSSA later. */
2120 DISCARD_LSA(lsa, 5);
2121 continue;
2122 }
2123
2124 /* (6) Else, If there is an instance of the LSA on the sending
2125 neighbor's Link state request list, an error has occurred in
2126 the Database Exchange process. In this case, restart the
2127 Database Exchange process by generating the neighbor event
2128 BadLSReq for the sending neighbor and stop processing the
2129 Link State Update packet. */
2130
2131 if (ospf_ls_request_lookup(nbr, lsa)) {
2132 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
2133 flog_warn(
2134 EC_OSPF_PACKET,
2135 "LSA[%s] instance exists on Link state request list",
2136 dump_lsa_key(lsa));
2137
2138 /* Clean list of LSAs. */
2139 ospf_upd_list_clean(lsas);
2140 /* this lsa is not on lsas list already. */
2141 ospf_lsa_discard(lsa);
2142 return;
2143 }
2144
2145 /* If the received LSA is the same instance as the database copy
2146 (i.e., neither one is more recent) the following two steps
2147 should be performed: */
2148
2149 if (ret == 0) {
2150 /* If the LSA is listed in the Link state retransmission
2151 list
2152 for the receiving adjacency, the router itself is
2153 expecting
2154 an acknowledgment for this LSA. The router should
2155 treat the
2156 received LSA as an acknowledgment by removing the LSA
2157 from
2158 the Link state retransmission list. This is termed
2159 an
2160 "implied acknowledgment". */
2161
2162 ls_ret = ospf_ls_retransmit_lookup(nbr, lsa);
2163
2164 if (ls_ret != NULL) {
2165 ospf_ls_retransmit_delete(nbr, ls_ret);
2166
2167 /* Delayed acknowledgment sent if advertisement
2168 received
2169 from Designated Router, otherwise do nothing.
2170 */
2171 if (oi->state == ISM_Backup)
2172 if (NBR_IS_DR(nbr))
2173 listnode_add(
2174 oi->ls_ack,
2175 ospf_lsa_lock(lsa));
2176
2177 DISCARD_LSA(lsa, 6);
2178 } else
2179 /* Acknowledge the receipt of the LSA by sending a
2180 Link State Acknowledgment packet back out the
2181 receiving
2182 interface. */
2183 {
2184 ospf_ls_ack_send(nbr, lsa);
2185 DISCARD_LSA(lsa, 7);
2186 }
2187 }
2188
2189 /* The database copy is more recent. If the database copy
2190 has LS age equal to MaxAge and LS sequence number equal to
2191 MaxSequenceNumber, simply discard the received LSA without
2192 acknowledging it. (In this case, the LSA's LS sequence number
2193 is
2194 wrapping, and the MaxSequenceNumber LSA must be completely
2195 flushed before any new LSA instance can be introduced). */
2196
2197 else if (ret > 0) /* Database copy is more recent */
2198 {
2199 if (IS_LSA_MAXAGE(current)
2200 && current->data->ls_seqnum
2201 == htonl(OSPF_MAX_SEQUENCE_NUMBER)) {
2202 DISCARD_LSA(lsa, 8);
2203 }
2204 /* Otherwise, as long as the database copy has not been
2205 sent in a
2206 Link State Update within the last MinLSArrival
2207 seconds, send the
2208 database copy back to the sending neighbor,
2209 encapsulated within
2210 a Link State Update Packet. The Link State Update
2211 Packet should
2212 be sent directly to the neighbor. In so doing, do not
2213 put the
2214 database copy of the LSA on the neighbor's link state
2215 retransmission list, and do not acknowledge the
2216 received (less
2217 recent) LSA instance. */
2218 else {
2219 if (monotime_since(&current->tv_orig, NULL)
2220 >= ospf->min_ls_arrival * 1000LL)
2221 /* Trap NSSA type later.*/
2222 ospf_ls_upd_send_lsa(
2223 nbr, current,
2224 OSPF_SEND_PACKET_DIRECT);
2225 DISCARD_LSA(lsa, 9);
2226 }
2227 }
2228 }
2229 #undef DISCARD_LSA
2230
2231 assert(listcount(lsas) == 0);
2232 list_delete(&lsas);
2233 }
2234
2235 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
2236 static void ospf_ls_ack(struct ip *iph, struct ospf_header *ospfh,
2237 struct stream *s, struct ospf_interface *oi,
2238 uint16_t size)
2239 {
2240 struct ospf_neighbor *nbr;
2241
2242 /* increment statistics. */
2243 oi->ls_ack_in++;
2244
2245 nbr = ospf_nbr_lookup(oi, iph, ospfh);
2246 if (nbr == NULL) {
2247 flog_warn(EC_OSPF_PACKET,
2248 "Link State Acknowledgment: Unknown Neighbor %s.",
2249 inet_ntoa(ospfh->router_id));
2250 return;
2251 }
2252
2253 /* Add event to thread. */
2254 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_PacketReceived);
2255
2256 if (nbr->state < NSM_Exchange) {
2257 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
2258 zlog_debug(
2259 "Link State Acknowledgment: "
2260 "Neighbor[%s] state %s is less than Exchange",
2261 inet_ntoa(ospfh->router_id),
2262 lookup_msg(ospf_nsm_state_msg, nbr->state,
2263 NULL));
2264 return;
2265 }
2266
2267 while (size >= OSPF_LSA_HEADER_SIZE) {
2268 struct ospf_lsa *lsa, *lsr;
2269
2270 lsa = ospf_lsa_new();
2271 lsa->data = (struct lsa_header *)stream_pnt(s);
2272 lsa->vrf_id = oi->ospf->vrf_id;
2273
2274 /* lsah = (struct lsa_header *) stream_pnt (s); */
2275 size -= OSPF_LSA_HEADER_SIZE;
2276 stream_forward_getp(s, OSPF_LSA_HEADER_SIZE);
2277
2278 if (lsa->data->type < OSPF_MIN_LSA
2279 || lsa->data->type >= OSPF_MAX_LSA) {
2280 lsa->data = NULL;
2281 ospf_lsa_discard(lsa);
2282 continue;
2283 }
2284
2285 lsr = ospf_ls_retransmit_lookup(nbr, lsa);
2286
2287 if (lsr != NULL && ospf_lsa_more_recent(lsr, lsa) == 0)
2288 ospf_ls_retransmit_delete(nbr, lsr);
2289
2290 lsa->data = NULL;
2291 ospf_lsa_discard(lsa);
2292 }
2293
2294 return;
2295 }
2296
2297 static struct stream *ospf_recv_packet(struct ospf *ospf, int fd,
2298 struct interface **ifp,
2299 struct stream *ibuf)
2300 {
2301 int ret;
2302 struct ip *iph;
2303 uint16_t ip_len;
2304 ifindex_t ifindex = 0;
2305 struct iovec iov;
2306 /* Header and data both require alignment. */
2307 char buff[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
2308 struct msghdr msgh;
2309
2310 memset(&msgh, 0, sizeof(struct msghdr));
2311 msgh.msg_iov = &iov;
2312 msgh.msg_iovlen = 1;
2313 msgh.msg_control = (caddr_t)buff;
2314 msgh.msg_controllen = sizeof(buff);
2315
2316 ret = stream_recvmsg(ibuf, fd, &msgh, 0, OSPF_MAX_PACKET_SIZE + 1);
2317 if (ret < 0) {
2318 flog_warn(EC_OSPF_PACKET, "stream_recvmsg failed: %s",
2319 safe_strerror(errno));
2320 return NULL;
2321 }
2322 if ((unsigned int)ret < sizeof(iph)) /* ret must be > 0 now */
2323 {
2324 flog_warn(
2325 EC_OSPF_PACKET,
2326 "ospf_recv_packet: discarding runt packet of length %d "
2327 "(ip header size is %u)",
2328 ret, (unsigned int)sizeof(iph));
2329 return NULL;
2330 }
2331
2332 /* Note that there should not be alignment problems with this assignment
2333 because this is at the beginning of the stream data buffer. */
2334 iph = (struct ip *)STREAM_DATA(ibuf);
2335 sockopt_iphdrincl_swab_systoh(iph);
2336
2337 ip_len = iph->ip_len;
2338
2339 #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
2340 /*
2341 * Kernel network code touches incoming IP header parameters,
2342 * before protocol specific processing.
2343 *
2344 * 1) Convert byteorder to host representation.
2345 * --> ip_len, ip_id, ip_off
2346 *
2347 * 2) Adjust ip_len to strip IP header size!
2348 * --> If user process receives entire IP packet via RAW
2349 * socket, it must consider adding IP header size to
2350 * the "ip_len" field of "ip" structure.
2351 *
2352 * For more details, see <netinet/ip_input.c>.
2353 */
2354 ip_len = ip_len + (iph->ip_hl << 2);
2355 #endif
2356
2357 #if defined(__DragonFly__)
2358 /*
2359 * in DragonFly's raw socket, ip_len/ip_off are read
2360 * in network byte order.
2361 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
2362 */
2363 ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2);
2364 #endif
2365
2366 ifindex = getsockopt_ifindex(AF_INET, &msgh);
2367
2368 *ifp = if_lookup_by_index(ifindex, ospf->vrf_id);
2369
2370 if (ret != ip_len) {
2371 flog_warn(
2372 EC_OSPF_PACKET,
2373 "ospf_recv_packet read length mismatch: ip_len is %d, "
2374 "but recvmsg returned %d",
2375 ip_len, ret);
2376 return NULL;
2377 }
2378
2379 return ibuf;
2380 }
2381
2382 static struct ospf_interface *
2383 ospf_associate_packet_vl(struct ospf *ospf, struct interface *ifp,
2384 struct ip *iph, struct ospf_header *ospfh)
2385 {
2386 struct ospf_interface *rcv_oi;
2387 struct ospf_vl_data *vl_data;
2388 struct ospf_area *vl_area;
2389 struct listnode *node;
2390
2391 if (IN_MULTICAST(ntohl(iph->ip_dst.s_addr))
2392 || !OSPF_IS_AREA_BACKBONE(ospfh))
2393 return NULL;
2394
2395 /* look for local OSPF interface matching the destination
2396 * to determine Area ID. We presume therefore the destination address
2397 * is unique, or at least (for "unnumbered" links), not used in other
2398 * areas
2399 */
2400 if ((rcv_oi = ospf_if_lookup_by_local_addr(ospf, NULL, iph->ip_dst))
2401 == NULL)
2402 return NULL;
2403
2404 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
2405 vl_area =
2406 ospf_area_lookup_by_area_id(ospf, vl_data->vl_area_id);
2407 if (!vl_area)
2408 continue;
2409
2410 if (OSPF_AREA_SAME(&vl_area, &rcv_oi->area)
2411 && IPV4_ADDR_SAME(&vl_data->vl_peer, &ospfh->router_id)) {
2412 if (IS_DEBUG_OSPF_EVENT)
2413 zlog_debug("associating packet with %s",
2414 IF_NAME(vl_data->vl_oi));
2415 if (!CHECK_FLAG(vl_data->vl_oi->ifp->flags, IFF_UP)) {
2416 if (IS_DEBUG_OSPF_EVENT)
2417 zlog_debug(
2418 "This VL is not up yet, sorry");
2419 return NULL;
2420 }
2421
2422 return vl_data->vl_oi;
2423 }
2424 }
2425
2426 if (IS_DEBUG_OSPF_EVENT)
2427 zlog_debug("couldn't find any VL to associate the packet with");
2428
2429 return NULL;
2430 }
2431
2432 static int ospf_check_area_id(struct ospf_interface *oi,
2433 struct ospf_header *ospfh)
2434 {
2435 /* Check match the Area ID of the receiving interface. */
2436 if (OSPF_AREA_SAME(&oi->area, &ospfh))
2437 return 1;
2438
2439 return 0;
2440 }
2441
2442 /* Unbound socket will accept any Raw IP packets if proto is matched.
2443 To prevent it, compare src IP address and i/f address with masking
2444 i/f network mask. */
2445 static int ospf_check_network_mask(struct ospf_interface *oi,
2446 struct in_addr ip_src)
2447 {
2448 struct in_addr mask, me, him;
2449
2450 if (oi->type == OSPF_IFTYPE_POINTOPOINT
2451 || oi->type == OSPF_IFTYPE_VIRTUALLINK)
2452 return 1;
2453
2454 masklen2ip(oi->address->prefixlen, &mask);
2455
2456 me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
2457 him.s_addr = ip_src.s_addr & mask.s_addr;
2458
2459 if (IPV4_ADDR_SAME(&me, &him))
2460 return 1;
2461
2462 return 0;
2463 }
2464
2465 /* Return 1, if the packet is properly authenticated and checksummed,
2466 0 otherwise. In particular, check that AuType header field is valid and
2467 matches the locally configured AuType, and that D.5 requirements are met. */
2468 static int ospf_check_auth(struct ospf_interface *oi, struct ospf_header *ospfh)
2469 {
2470 struct crypt_key *ck;
2471 uint16_t iface_auth_type;
2472 uint16_t pkt_auth_type = ntohs(ospfh->auth_type);
2473
2474 switch (pkt_auth_type) {
2475 case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
2476 if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type(oi))) {
2477 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2478 flog_warn(
2479 EC_OSPF_PACKET,
2480 "interface %s: auth-type mismatch, local %s, rcvd Null",
2481 IF_NAME(oi),
2482 lookup_msg(ospf_auth_type_str,
2483 iface_auth_type, NULL));
2484 return 0;
2485 }
2486 if (!ospf_check_sum(ospfh)) {
2487 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2488 flog_warn(
2489 EC_OSPF_PACKET,
2490 "interface %s: Null auth OK, but checksum error, Router-ID %s",
2491 IF_NAME(oi),
2492 inet_ntoa(ospfh->router_id));
2493 return 0;
2494 }
2495 return 1;
2496 case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
2497 if (OSPF_AUTH_SIMPLE
2498 != (iface_auth_type = ospf_auth_type(oi))) {
2499 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2500 flog_warn(
2501 EC_OSPF_PACKET,
2502 "interface %s: auth-type mismatch, local %s, rcvd Simple",
2503 IF_NAME(oi),
2504 lookup_msg(ospf_auth_type_str,
2505 iface_auth_type, NULL));
2506 return 0;
2507 }
2508 if (memcmp(OSPF_IF_PARAM(oi, auth_simple), ospfh->u.auth_data,
2509 OSPF_AUTH_SIMPLE_SIZE)) {
2510 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2511 flog_warn(EC_OSPF_PACKET,
2512 "interface %s: Simple auth failed",
2513 IF_NAME(oi));
2514 return 0;
2515 }
2516 if (!ospf_check_sum(ospfh)) {
2517 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2518 flog_warn(
2519 EC_OSPF_PACKET,
2520 "interface %s: Simple auth OK, checksum error, Router-ID %s",
2521 IF_NAME(oi),
2522 inet_ntoa(ospfh->router_id));
2523 return 0;
2524 }
2525 return 1;
2526 case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
2527 if (OSPF_AUTH_CRYPTOGRAPHIC
2528 != (iface_auth_type = ospf_auth_type(oi))) {
2529 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2530 flog_warn(
2531 EC_OSPF_PACKET,
2532 "interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
2533 IF_NAME(oi),
2534 lookup_msg(ospf_auth_type_str,
2535 iface_auth_type, NULL));
2536 return 0;
2537 }
2538 if (ospfh->checksum) {
2539 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2540 flog_warn(
2541 EC_OSPF_PACKET,
2542 "interface %s: OSPF header checksum is not 0",
2543 IF_NAME(oi));
2544 return 0;
2545 }
2546 /* only MD5 crypto method can pass ospf_packet_examin() */
2547 if (NULL == (ck = listgetdata(
2548 listtail(OSPF_IF_PARAM(oi, auth_crypt))))
2549 || ospfh->u.crypt.key_id != ck->key_id ||
2550 /* Condition above uses the last key ID on the list,
2551 which is
2552 different from what ospf_crypt_key_lookup() does. A
2553 bug? */
2554 !ospf_check_md5_digest(oi, ospfh)) {
2555 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2556 flog_warn(EC_OSPF_MD5,
2557 "interface %s: MD5 auth failed",
2558 IF_NAME(oi));
2559 return 0;
2560 }
2561 return 1;
2562 default:
2563 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
2564 flog_warn(
2565 EC_OSPF_PACKET,
2566 "interface %s: invalid packet auth-type (%02x)",
2567 IF_NAME(oi), pkt_auth_type);
2568 return 0;
2569 }
2570 }
2571
2572 static int ospf_check_sum(struct ospf_header *ospfh)
2573 {
2574 uint32_t ret;
2575 uint16_t sum;
2576
2577 /* clear auth_data for checksum. */
2578 memset(ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
2579
2580 /* keep checksum and clear. */
2581 sum = ospfh->checksum;
2582 memset(&ospfh->checksum, 0, sizeof(uint16_t));
2583
2584 /* calculate checksum. */
2585 ret = in_cksum(ospfh, ntohs(ospfh->length));
2586
2587 if (ret != sum) {
2588 zlog_info("ospf_check_sum(): checksum mismatch, my %X, his %X",
2589 ret, sum);
2590 return 0;
2591 }
2592
2593 return 1;
2594 }
2595
2596 /* Verify, that given link/TOS records are properly sized/aligned and match
2597 Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
2598 static unsigned ospf_router_lsa_links_examin(struct router_lsa_link *link,
2599 uint16_t linkbytes,
2600 const uint16_t num_links)
2601 {
2602 unsigned counted_links = 0, thislinklen;
2603
2604 while (linkbytes) {
2605 thislinklen =
2606 OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
2607 if (thislinklen > linkbytes) {
2608 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2609 zlog_debug("%s: length error in link block #%u",
2610 __func__, counted_links);
2611 return MSG_NG;
2612 }
2613 link = (struct router_lsa_link *)((caddr_t)link + thislinklen);
2614 linkbytes -= thislinklen;
2615 counted_links++;
2616 }
2617 if (counted_links != num_links) {
2618 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2619 zlog_debug("%s: %u link blocks declared, %u present",
2620 __func__, num_links, counted_links);
2621 return MSG_NG;
2622 }
2623 return MSG_OK;
2624 }
2625
2626 /* Verify, that the given LSA is properly sized/aligned (including type-specific
2627 minimum length constraint). */
2628 static unsigned ospf_lsa_examin(struct lsa_header *lsah, const uint16_t lsalen,
2629 const uint8_t headeronly)
2630 {
2631 unsigned ret;
2632 struct router_lsa *rlsa;
2633 if (lsah->type < OSPF_MAX_LSA && ospf_lsa_minlen[lsah->type]
2634 && lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]) {
2635 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2636 zlog_debug("%s: undersized (%u B) %s", __func__, lsalen,
2637 lookup_msg(ospf_lsa_type_msg, lsah->type,
2638 NULL));
2639 return MSG_NG;
2640 }
2641 switch (lsah->type) {
2642 case OSPF_ROUTER_LSA:
2643 /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1
2644 * (12+)-byte link blocks */
2645 if (headeronly) {
2646 ret = (lsalen - OSPF_LSA_HEADER_SIZE
2647 - OSPF_ROUTER_LSA_MIN_SIZE)
2648 % 4
2649 ? MSG_NG
2650 : MSG_OK;
2651 break;
2652 }
2653 rlsa = (struct router_lsa *)lsah;
2654 ret = ospf_router_lsa_links_examin(
2655 (struct router_lsa_link *)rlsa->link,
2656 lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic
2657 header, "flags",
2658 0, "# links" */
2659 ntohs(rlsa->links) /* 16 bits */
2660 );
2661 break;
2662 case OSPF_AS_EXTERNAL_LSA:
2663 /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long
2664 * blocks */
2665 case OSPF_AS_NSSA_LSA:
2666 /* RFC3101 C, idem */
2667 ret = (lsalen - OSPF_LSA_HEADER_SIZE
2668 - OSPF_AS_EXTERNAL_LSA_MIN_SIZE)
2669 % 12
2670 ? MSG_NG
2671 : MSG_OK;
2672 break;
2673 /* Following LSA types are considered OK length-wise as soon as their
2674 * minimum
2675 * length constraint is met and length of the whole LSA is a multiple of
2676 * 4
2677 * (basic LSA header size is already a multiple of 4). */
2678 case OSPF_NETWORK_LSA:
2679 /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
2680 case OSPF_SUMMARY_LSA:
2681 case OSPF_ASBR_SUMMARY_LSA:
2682 /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS
2683 * blocks */
2684 case OSPF_OPAQUE_LINK_LSA:
2685 case OSPF_OPAQUE_AREA_LSA:
2686 case OSPF_OPAQUE_AS_LSA:
2687 /* RFC5250 A.2, "some number of octets (of application-specific
2688 * data) padded to 32-bit alignment." This is considered
2689 * equivalent
2690 * to 4-byte alignment of all other LSA types, see
2691 * OSPF-ALIGNMENT.txt
2692 * file for the detailed analysis of this passage. */
2693 ret = lsalen % 4 ? MSG_NG : MSG_OK;
2694 break;
2695 default:
2696 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2697 zlog_debug("%s: unsupported LSA type 0x%02x", __func__,
2698 lsah->type);
2699 return MSG_NG;
2700 }
2701 if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET(0, RECV))
2702 zlog_debug("%s: alignment error in %s", __func__,
2703 lookup_msg(ospf_lsa_type_msg, lsah->type, NULL));
2704 return ret;
2705 }
2706
2707 /* Verify if the provided input buffer is a valid sequence of LSAs. This
2708 includes verification of LSA blocks length/alignment and dispatching
2709 of deeper-level checks. */
2710 static unsigned
2711 ospf_lsaseq_examin(struct lsa_header *lsah, /* start of buffered data */
2712 size_t length, const uint8_t headeronly,
2713 /* When declared_num_lsas is not 0, compare it to the real
2714 number of LSAs
2715 and treat the difference as an error. */
2716 const uint32_t declared_num_lsas)
2717 {
2718 uint32_t counted_lsas = 0;
2719
2720 while (length) {
2721 uint16_t lsalen;
2722 if (length < OSPF_LSA_HEADER_SIZE) {
2723 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2724 zlog_debug(
2725 "%s: undersized (%zu B) trailing (#%u) LSA header",
2726 __func__, length, counted_lsas);
2727 return MSG_NG;
2728 }
2729 /* save on ntohs() calls here and in the LSA validator */
2730 lsalen = ntohs(lsah->length);
2731 if (lsalen < OSPF_LSA_HEADER_SIZE) {
2732 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2733 zlog_debug(
2734 "%s: malformed LSA header #%u, declared length is %u B",
2735 __func__, counted_lsas, lsalen);
2736 return MSG_NG;
2737 }
2738 if (headeronly) {
2739 /* less checks here and in ospf_lsa_examin() */
2740 if (MSG_OK != ospf_lsa_examin(lsah, lsalen, 1)) {
2741 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2742 zlog_debug(
2743 "%s: malformed header-only LSA #%u",
2744 __func__, counted_lsas);
2745 return MSG_NG;
2746 }
2747 lsah = (struct lsa_header *)((caddr_t)lsah
2748 + OSPF_LSA_HEADER_SIZE);
2749 length -= OSPF_LSA_HEADER_SIZE;
2750 } else {
2751 /* make sure the input buffer is deep enough before
2752 * further checks */
2753 if (lsalen > length) {
2754 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2755 zlog_debug(
2756 "%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
2757 __func__, counted_lsas, lsalen,
2758 length);
2759 return MSG_NG;
2760 }
2761 if (MSG_OK != ospf_lsa_examin(lsah, lsalen, 0)) {
2762 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2763 zlog_debug("%s: malformed LSA #%u",
2764 __func__, counted_lsas);
2765 return MSG_NG;
2766 }
2767 lsah = (struct lsa_header *)((caddr_t)lsah + lsalen);
2768 length -= lsalen;
2769 }
2770 counted_lsas++;
2771 }
2772
2773 if (declared_num_lsas && counted_lsas != declared_num_lsas) {
2774 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2775 zlog_debug(
2776 "%s: #LSAs declared (%u) does not match actual (%u)",
2777 __func__, declared_num_lsas, counted_lsas);
2778 return MSG_NG;
2779 }
2780 return MSG_OK;
2781 }
2782
2783 /* Verify a complete OSPF packet for proper sizing/alignment. */
2784 static unsigned ospf_packet_examin(struct ospf_header *oh,
2785 const unsigned bytesonwire)
2786 {
2787 uint16_t bytesdeclared, bytesauth;
2788 unsigned ret;
2789 struct ospf_ls_update *lsupd;
2790
2791 /* Length, 1st approximation. */
2792 if (bytesonwire < OSPF_HEADER_SIZE) {
2793 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2794 zlog_debug("%s: undersized (%u B) packet", __func__,
2795 bytesonwire);
2796 return MSG_NG;
2797 }
2798 /* Now it is safe to access header fields. Performing length check,
2799 * allow
2800 * for possible extra bytes of crypto auth/padding, which are not
2801 * counted
2802 * in the OSPF header "length" field. */
2803 if (oh->version != OSPF_VERSION) {
2804 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2805 zlog_debug("%s: invalid (%u) protocol version",
2806 __func__, oh->version);
2807 return MSG_NG;
2808 }
2809 bytesdeclared = ntohs(oh->length);
2810 if (ntohs(oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
2811 bytesauth = 0;
2812 else {
2813 if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE) {
2814 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2815 zlog_debug(
2816 "%s: unsupported crypto auth length (%u B)",
2817 __func__, oh->u.crypt.auth_data_len);
2818 return MSG_NG;
2819 }
2820 bytesauth = OSPF_AUTH_MD5_SIZE;
2821 }
2822 if (bytesdeclared + bytesauth > bytesonwire) {
2823 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2824 zlog_debug(
2825 "%s: packet length error (%u real, %u+%u declared)",
2826 __func__, bytesonwire, bytesdeclared,
2827 bytesauth);
2828 return MSG_NG;
2829 }
2830 /* Length, 2nd approximation. The type-specific constraint is checked
2831 against declared length, not amount of bytes on wire. */
2832 if (oh->type >= OSPF_MSG_HELLO && oh->type <= OSPF_MSG_LS_ACK
2833 && bytesdeclared
2834 < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]) {
2835 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2836 zlog_debug("%s: undersized (%u B) %s packet", __func__,
2837 bytesdeclared,
2838 lookup_msg(ospf_packet_type_str, oh->type,
2839 NULL));
2840 return MSG_NG;
2841 }
2842 switch (oh->type) {
2843 case OSPF_MSG_HELLO:
2844 /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes
2845 followed
2846 by N>=0 router-IDs. */
2847 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE)
2848 % 4
2849 ? MSG_NG
2850 : MSG_OK;
2851 break;
2852 case OSPF_MSG_DB_DESC:
2853 /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes
2854 followed
2855 by N>=0 header-only LSAs. */
2856 ret = ospf_lsaseq_examin(
2857 (struct lsa_header *)((caddr_t)oh + OSPF_HEADER_SIZE
2858 + OSPF_DB_DESC_MIN_SIZE),
2859 bytesdeclared - OSPF_HEADER_SIZE
2860 - OSPF_DB_DESC_MIN_SIZE,
2861 1, /* header-only LSAs */
2862 0);
2863 break;
2864 case OSPF_MSG_LS_REQ:
2865 /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes
2866 * request blocks. */
2867 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE)
2868 % OSPF_LSA_KEY_SIZE
2869 ? MSG_NG
2870 : MSG_OK;
2871 break;
2872 case OSPF_MSG_LS_UPD:
2873 /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes
2874 followed
2875 by N>=0 full LSAs (with N declared beforehand). */
2876 lsupd = (struct ospf_ls_update *)((caddr_t)oh
2877 + OSPF_HEADER_SIZE);
2878 ret = ospf_lsaseq_examin(
2879 (struct lsa_header *)((caddr_t)lsupd
2880 + OSPF_LS_UPD_MIN_SIZE),
2881 bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
2882 0, /* full LSAs */
2883 ntohl(lsupd->num_lsas) /* 32 bits */
2884 );
2885 break;
2886 case OSPF_MSG_LS_ACK:
2887 /* RFC2328 A.3.6, packet header followed by N>=0 header-only
2888 * LSAs. */
2889 ret = ospf_lsaseq_examin(
2890 (struct lsa_header *)((caddr_t)oh + OSPF_HEADER_SIZE
2891 + OSPF_LS_ACK_MIN_SIZE),
2892 bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
2893 1, /* header-only LSAs */
2894 0);
2895 break;
2896 default:
2897 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2898 zlog_debug("%s: invalid packet type 0x%02x", __func__,
2899 oh->type);
2900 return MSG_NG;
2901 }
2902 if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET(0, RECV))
2903 zlog_debug("%s: malformed %s packet", __func__,
2904 lookup_msg(ospf_packet_type_str, oh->type, NULL));
2905 return ret;
2906 }
2907
2908 /* OSPF Header verification. */
2909 static int ospf_verify_header(struct stream *ibuf, struct ospf_interface *oi,
2910 struct ip *iph, struct ospf_header *ospfh)
2911 {
2912 /* Check Area ID. */
2913 if (!ospf_check_area_id(oi, ospfh)) {
2914 flog_warn(EC_OSPF_PACKET,
2915 "interface %s: ospf_read invalid Area ID %s.",
2916 IF_NAME(oi), inet_ntoa(ospfh->area_id));
2917 return -1;
2918 }
2919
2920 /* Check network mask, Silently discarded. */
2921 if (!ospf_check_network_mask(oi, iph->ip_src)) {
2922 flog_warn(
2923 EC_OSPF_PACKET,
2924 "interface %s: ospf_read network address is not same [%s]",
2925 IF_NAME(oi), inet_ntoa(iph->ip_src));
2926 return -1;
2927 }
2928
2929 /* Check authentication. The function handles logging actions, where
2930 * required. */
2931 if (!ospf_check_auth(oi, ospfh))
2932 return -1;
2933
2934 return 0;
2935 }
2936
2937 /* Starting point of packet process function. */
2938 int ospf_read(struct thread *thread)
2939 {
2940 int ret;
2941 struct stream *ibuf;
2942 struct ospf *ospf;
2943 struct ospf_interface *oi;
2944 struct ip *iph;
2945 struct ospf_header *ospfh;
2946 uint16_t length;
2947 struct interface *ifp = NULL;
2948 struct connected *c;
2949
2950 /* first of all get interface pointer. */
2951 ospf = THREAD_ARG(thread);
2952
2953 /* prepare for next packet. */
2954 ospf->t_read = NULL;
2955 thread_add_read(master, ospf_read, ospf, ospf->fd, &ospf->t_read);
2956
2957 stream_reset(ospf->ibuf);
2958 ibuf = ospf_recv_packet(ospf, ospf->fd, &ifp, ospf->ibuf);
2959 if (ibuf == NULL)
2960 return -1;
2961 /* This raw packet is known to be at least as big as its IP header. */
2962
2963 /* Note that there should not be alignment problems with this assignment
2964 because this is at the beginning of the stream data buffer. */
2965 iph = (struct ip *)STREAM_DATA(ibuf);
2966 /* Note that sockopt_iphdrincl_swab_systoh was called in
2967 * ospf_recv_packet. */
2968
2969 if (ifp == NULL) {
2970 /* Handle cases where the platform does not support retrieving
2971 the ifindex,
2972 and also platforms (such as Solaris 8) that claim to support
2973 ifindex
2974 retrieval but do not. */
2975 c = if_lookup_address((void *)&iph->ip_src, AF_INET,
2976 ospf->vrf_id);
2977 if (c)
2978 ifp = c->ifp;
2979 if (ifp == NULL)
2980 return 0;
2981 }
2982
2983 /* IP Header dump. */
2984 if (IS_DEBUG_OSPF_PACKET(0, RECV))
2985 ospf_ip_header_dump(iph);
2986
2987 /* Self-originated packet should be discarded silently. */
2988 if (ospf_if_lookup_by_local_addr(ospf, NULL, iph->ip_src)) {
2989 if (IS_DEBUG_OSPF_PACKET(0, RECV)) {
2990 zlog_debug(
2991 "ospf_read[%s]: Dropping self-originated packet",
2992 inet_ntoa(iph->ip_src));
2993 }
2994 return 0;
2995 }
2996
2997 /* Advance from IP header to OSPF header (iph->ip_hl has been verified
2998 by ospf_recv_packet() to be correct). */
2999 stream_forward_getp(ibuf, iph->ip_hl * 4);
3000
3001 ospfh = (struct ospf_header *)stream_pnt(ibuf);
3002 if (MSG_OK
3003 != ospf_packet_examin(
3004 ospfh, stream_get_endp(ibuf) - stream_get_getp(ibuf)))
3005 return -1;
3006 /* Now it is safe to access all fields of OSPF packet header. */
3007
3008 /* associate packet with ospf interface */
3009 oi = ospf_if_lookup_recv_if(ospf, iph->ip_src, ifp);
3010
3011 /* ospf_verify_header() relies on a valid "oi" and thus can be called
3012 only
3013 after the passive/backbone/other checks below are passed. These
3014 checks
3015 in turn access the fields of unverified "ospfh" structure for their
3016 own
3017 purposes and must remain very accurate in doing this. */
3018
3019 /* If incoming interface is passive one, ignore it. */
3020 if (oi && OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE) {
3021 char buf[3][INET_ADDRSTRLEN];
3022
3023 if (IS_DEBUG_OSPF_EVENT)
3024 zlog_debug(
3025 "ignoring packet from router %s sent to %s, "
3026 "received on a passive interface, %s",
3027 inet_ntop(AF_INET, &ospfh->router_id, buf[0],
3028 sizeof(buf[0])),
3029 inet_ntop(AF_INET, &iph->ip_dst, buf[1],
3030 sizeof(buf[1])),
3031 inet_ntop(AF_INET, &oi->address->u.prefix4,
3032 buf[2], sizeof(buf[2])));
3033
3034 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) {
3035 /* Try to fix multicast membership.
3036 * Some OS:es may have problems in this area,
3037 * make sure it is removed.
3038 */
3039 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
3040 ospf_if_set_multicast(oi);
3041 }
3042 return 0;
3043 }
3044
3045
3046 /* if no local ospf_interface,
3047 * or header area is backbone but ospf_interface is not
3048 * check for VLINK interface
3049 */
3050 if ((oi == NULL) || (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
3051 && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))) {
3052 if ((oi = ospf_associate_packet_vl(ospf, ifp, iph, ospfh))
3053 == NULL) {
3054 if (!ospf->instance && IS_DEBUG_OSPF_EVENT)
3055 zlog_debug(
3056 "Packet from [%s] received on link %s"
3057 " but no ospf_interface",
3058 inet_ntoa(iph->ip_src), ifp->name);
3059 return 0;
3060 }
3061 }
3062
3063 /* else it must be a local ospf interface, check it was received on
3064 * correct link
3065 */
3066 else if (oi->ifp != ifp) {
3067 if (IS_DEBUG_OSPF_EVENT)
3068 flog_warn(EC_OSPF_PACKET,
3069 "Packet from [%s] received on wrong link %s",
3070 inet_ntoa(iph->ip_src), ifp->name);
3071 return 0;
3072 } else if (oi->state == ISM_Down) {
3073 char buf[2][INET_ADDRSTRLEN];
3074 flog_warn(
3075 EC_OSPF_PACKET,
3076 "Ignoring packet from %s to %s received on interface that is "
3077 "down [%s]; interface flags are %s",
3078 inet_ntop(AF_INET, &iph->ip_src, buf[0],
3079 sizeof(buf[0])),
3080 inet_ntop(AF_INET, &iph->ip_dst, buf[1],
3081 sizeof(buf[1])),
3082 ifp->name, if_flag_dump(ifp->flags));
3083 /* Fix multicast memberships? */
3084 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
3085 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
3086 else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS))
3087 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
3088 if (oi->multicast_memberships)
3089 ospf_if_set_multicast(oi);
3090 return 0;
3091 }
3092
3093 /*
3094 * If the received packet is destined for AllDRouters, the packet
3095 * should be accepted only if the received ospf interface state is
3096 * either DR or Backup -- endo.
3097 */
3098 if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS)
3099 && (oi->state != ISM_DR && oi->state != ISM_Backup)) {
3100 flog_warn(
3101 EC_OSPF_PACKET,
3102 "Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
3103 inet_ntoa(iph->ip_src), IF_NAME(oi),
3104 lookup_msg(ospf_ism_state_msg, oi->state, NULL));
3105 /* Try to fix multicast membership. */
3106 SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
3107 ospf_if_set_multicast(oi);
3108 return 0;
3109 }
3110
3111 /* Verify more OSPF header fields. */
3112 ret = ospf_verify_header(ibuf, oi, iph, ospfh);
3113 if (ret < 0) {
3114 if (IS_DEBUG_OSPF_PACKET(0, RECV))
3115 zlog_debug(
3116 "ospf_read[%s]: Header check failed, "
3117 "dropping.",
3118 inet_ntoa(iph->ip_src));
3119 return ret;
3120 }
3121
3122 /* Show debug receiving packet. */
3123 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV)) {
3124 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, DETAIL)) {
3125 zlog_debug(
3126 "-----------------------------------------------------");
3127 ospf_packet_dump(ibuf);
3128 }
3129
3130 zlog_debug("%s received from [%s] via [%s]",
3131 lookup_msg(ospf_packet_type_str, ospfh->type, NULL),
3132 inet_ntoa(ospfh->router_id), IF_NAME(oi));
3133 zlog_debug(" src [%s],", inet_ntoa(iph->ip_src));
3134 zlog_debug(" dst [%s]", inet_ntoa(iph->ip_dst));
3135
3136 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, DETAIL))
3137 zlog_debug(
3138 "-----------------------------------------------------");
3139 }
3140
3141 stream_forward_getp(ibuf, OSPF_HEADER_SIZE);
3142
3143 /* Adjust size to message length. */
3144 length = ntohs(ospfh->length) - OSPF_HEADER_SIZE;
3145
3146 /* Read rest of the packet and call each sort of packet routine. */
3147 switch (ospfh->type) {
3148 case OSPF_MSG_HELLO:
3149 ospf_hello(iph, ospfh, ibuf, oi, length);
3150 break;
3151 case OSPF_MSG_DB_DESC:
3152 ospf_db_desc(iph, ospfh, ibuf, oi, length);
3153 break;
3154 case OSPF_MSG_LS_REQ:
3155 ospf_ls_req(iph, ospfh, ibuf, oi, length);
3156 break;
3157 case OSPF_MSG_LS_UPD:
3158 ospf_ls_upd(ospf, iph, ospfh, ibuf, oi, length);
3159 break;
3160 case OSPF_MSG_LS_ACK:
3161 ospf_ls_ack(iph, ospfh, ibuf, oi, length);
3162 break;
3163 default:
3164 flog_warn(EC_OSPF_PACKET,
3165 "interface %s: OSPF packet header type %d is illegal",
3166 IF_NAME(oi), ospfh->type);
3167 break;
3168 }
3169
3170 return 0;
3171 }
3172
3173 /* Make OSPF header. */
3174 static void ospf_make_header(int type, struct ospf_interface *oi,
3175 struct stream *s)
3176 {
3177 struct ospf_header *ospfh;
3178
3179 ospfh = (struct ospf_header *)STREAM_DATA(s);
3180
3181 ospfh->version = (uint8_t)OSPF_VERSION;
3182 ospfh->type = (uint8_t)type;
3183
3184 ospfh->router_id = oi->ospf->router_id;
3185
3186 ospfh->checksum = 0;
3187 ospfh->area_id = oi->area->area_id;
3188 ospfh->auth_type = htons(ospf_auth_type(oi));
3189
3190 memset(ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
3191
3192 stream_forward_endp(s, OSPF_HEADER_SIZE);
3193 }
3194
3195 /* Make Authentication Data. */
3196 static int ospf_make_auth(struct ospf_interface *oi, struct ospf_header *ospfh)
3197 {
3198 struct crypt_key *ck;
3199
3200 switch (ospf_auth_type(oi)) {
3201 case OSPF_AUTH_NULL:
3202 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3203 */
3204 break;
3205 case OSPF_AUTH_SIMPLE:
3206 memcpy(ospfh->u.auth_data, OSPF_IF_PARAM(oi, auth_simple),
3207 OSPF_AUTH_SIMPLE_SIZE);
3208 break;
3209 case OSPF_AUTH_CRYPTOGRAPHIC:
3210 /* If key is not set, then set 0. */
3211 if (list_isempty(OSPF_IF_PARAM(oi, auth_crypt))) {
3212 ospfh->u.crypt.zero = 0;
3213 ospfh->u.crypt.key_id = 0;
3214 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
3215 } else {
3216 ck = listgetdata(
3217 listtail(OSPF_IF_PARAM(oi, auth_crypt)));
3218 ospfh->u.crypt.zero = 0;
3219 ospfh->u.crypt.key_id = ck->key_id;
3220 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
3221 }
3222 /* note: the seq is done in ospf_make_md5_digest() */
3223 break;
3224 default:
3225 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3226 */
3227 break;
3228 }
3229
3230 return 0;
3231 }
3232
3233 /* Fill rest of OSPF header. */
3234 static void ospf_fill_header(struct ospf_interface *oi, struct stream *s,
3235 uint16_t length)
3236 {
3237 struct ospf_header *ospfh;
3238
3239 ospfh = (struct ospf_header *)STREAM_DATA(s);
3240
3241 /* Fill length. */
3242 ospfh->length = htons(length);
3243
3244 /* Calculate checksum. */
3245 if (ntohs(ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
3246 ospfh->checksum = in_cksum(ospfh, length);
3247 else
3248 ospfh->checksum = 0;
3249
3250 /* Add Authentication Data. */
3251 ospf_make_auth(oi, ospfh);
3252 }
3253
3254 static int ospf_make_hello(struct ospf_interface *oi, struct stream *s)
3255 {
3256 struct ospf_neighbor *nbr;
3257 struct route_node *rn;
3258 uint16_t length = OSPF_HELLO_MIN_SIZE;
3259 struct in_addr mask;
3260 unsigned long p;
3261 int flag = 0;
3262
3263 /* Set netmask of interface. */
3264 if (!(CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)
3265 && oi->type == OSPF_IFTYPE_POINTOPOINT)
3266 && oi->type != OSPF_IFTYPE_VIRTUALLINK)
3267 masklen2ip(oi->address->prefixlen, &mask);
3268 else
3269 memset((char *)&mask, 0, sizeof(struct in_addr));
3270 stream_put_ipv4(s, mask.s_addr);
3271
3272 /* Set Hello Interval. */
3273 if (OSPF_IF_PARAM(oi, fast_hello) == 0)
3274 stream_putw(s, OSPF_IF_PARAM(oi, v_hello));
3275 else
3276 stream_putw(s, 0); /* hello-interval of 0 for fast-hellos */
3277
3278 if (IS_DEBUG_OSPF_EVENT)
3279 zlog_debug("make_hello: options: %x, int: %s", OPTIONS(oi),
3280 IF_NAME(oi));
3281
3282 /* Set Options. */
3283 stream_putc(s, OPTIONS(oi));
3284
3285 /* Set Router Priority. */
3286 stream_putc(s, PRIORITY(oi));
3287
3288 /* Set Router Dead Interval. */
3289 stream_putl(s, OSPF_IF_PARAM(oi, v_wait));
3290
3291 /* Set Designated Router. */
3292 stream_put_ipv4(s, DR(oi).s_addr);
3293
3294 p = stream_get_endp(s);
3295
3296 /* Set Backup Designated Router. */
3297 stream_put_ipv4(s, BDR(oi).s_addr);
3298
3299 /* Add neighbor seen. */
3300 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
3301 if ((nbr = rn->info))
3302 if (nbr->router_id.s_addr
3303 != 0) /* Ignore 0.0.0.0 node. */
3304 if (nbr->state
3305 != NSM_Attempt) /* Ignore Down neighbor. */
3306 if (nbr->state
3307 != NSM_Down) /* This is myself for
3308 DR election. */
3309 if (!IPV4_ADDR_SAME(
3310 &nbr->router_id,
3311 &oi->ospf->router_id)) {
3312 /* Check neighbor is
3313 * sane? */
3314 if (nbr->d_router.s_addr
3315 != 0
3316 && IPV4_ADDR_SAME(
3317 &nbr->d_router,
3318 &oi->address
3319 ->u
3320 .prefix4)
3321 && IPV4_ADDR_SAME(
3322 &nbr->bd_router,
3323 &oi->address
3324 ->u
3325 .prefix4))
3326 flag = 1;
3327
3328 /* Hello packet overflows interface MTU. */
3329 if (length + sizeof(uint32_t)
3330 > ospf_packet_max(oi)) {
3331 flog_err(
3332 EC_OSPF_LARGE_HELLO,
3333 "Oversized Hello packet! Larger than MTU. Not sending it out");
3334 return 0;
3335 }
3336
3337 stream_put_ipv4(
3338 s,
3339 nbr->router_id
3340 .s_addr);
3341 length += 4;
3342 }
3343
3344 /* Let neighbor generate BackupSeen. */
3345 if (flag == 1)
3346 stream_putl_at(s, p, 0); /* ipv4 address, normally */
3347
3348 return length;
3349 }
3350
3351 static int ospf_make_db_desc(struct ospf_interface *oi,
3352 struct ospf_neighbor *nbr, struct stream *s)
3353 {
3354 struct ospf_lsa *lsa;
3355 uint16_t length = OSPF_DB_DESC_MIN_SIZE;
3356 uint8_t options;
3357 unsigned long pp;
3358 int i;
3359 struct ospf_lsdb *lsdb;
3360
3361 /* Set Interface MTU. */
3362 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3363 stream_putw(s, 0);
3364 else
3365 stream_putw(s, oi->ifp->mtu);
3366
3367 /* Set Options. */
3368 options = OPTIONS(oi);
3369 if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE))
3370 SET_FLAG(options, OSPF_OPTION_O);
3371 stream_putc(s, options);
3372
3373 /* DD flags */
3374 pp = stream_get_endp(s);
3375 stream_putc(s, nbr->dd_flags);
3376
3377 /* Set DD Sequence Number. */
3378 stream_putl(s, nbr->dd_seqnum);
3379
3380 /* shortcut unneeded walk of (empty) summary LSDBs */
3381 if (ospf_db_summary_isempty(nbr))
3382 goto empty;
3383
3384 /* Describe LSA Header from Database Summary List. */
3385 lsdb = &nbr->db_sum;
3386
3387 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
3388 struct route_table *table = lsdb->type[i].db;
3389 struct route_node *rn;
3390
3391 for (rn = route_top(table); rn; rn = route_next(rn))
3392 if ((lsa = rn->info) != NULL) {
3393 if (IS_OPAQUE_LSA(lsa->data->type)
3394 && (!CHECK_FLAG(options, OSPF_OPTION_O))) {
3395 /* Suppress advertising
3396 * opaque-information. */
3397 /* Remove LSA from DB summary list. */
3398 ospf_lsdb_delete(lsdb, lsa);
3399 continue;
3400 }
3401
3402 if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
3403 struct lsa_header *lsah;
3404 uint16_t ls_age;
3405
3406 /* DD packet overflows interface MTU. */
3407 if (length + OSPF_LSA_HEADER_SIZE
3408 > ospf_packet_max(oi))
3409 break;
3410
3411 /* Keep pointer to LS age. */
3412 lsah = (struct lsa_header
3413 *)(STREAM_DATA(s)
3414 + stream_get_endp(
3415 s));
3416
3417 /* Proceed stream pointer. */
3418 stream_put(s, lsa->data,
3419 OSPF_LSA_HEADER_SIZE);
3420 length += OSPF_LSA_HEADER_SIZE;
3421
3422 /* Set LS age. */
3423 ls_age = LS_AGE(lsa);
3424 lsah->ls_age = htons(ls_age);
3425 }
3426
3427 /* Remove LSA from DB summary list. */
3428 ospf_lsdb_delete(lsdb, lsa);
3429 }
3430 }
3431
3432 /* Update 'More' bit */
3433 if (ospf_db_summary_isempty(nbr)) {
3434 empty:
3435 if (nbr->state >= NSM_Exchange) {
3436 UNSET_FLAG(nbr->dd_flags, OSPF_DD_FLAG_M);
3437 /* Rewrite DD flags */
3438 stream_putc_at(s, pp, nbr->dd_flags);
3439 } else {
3440 assert(IS_SET_DD_M(nbr->dd_flags));
3441 }
3442 }
3443 return length;
3444 }
3445
3446 static int ospf_make_ls_req_func(struct stream *s, uint16_t *length,
3447 unsigned long delta, struct ospf_neighbor *nbr,
3448 struct ospf_lsa *lsa)
3449 {
3450 struct ospf_interface *oi;
3451
3452 oi = nbr->oi;
3453
3454 /* LS Request packet overflows interface MTU
3455 * delta is just number of bytes required for 1 LS Req
3456 * ospf_packet_max will return the number of bytes can
3457 * be accomodated without ospf header. So length+delta
3458 * can be compared to ospf_packet_max
3459 * to check if it can fit another lsreq in the same packet.
3460 */
3461
3462 if (*length + delta > ospf_packet_max(oi))
3463 return 0;
3464
3465 stream_putl(s, lsa->data->type);
3466 stream_put_ipv4(s, lsa->data->id.s_addr);
3467 stream_put_ipv4(s, lsa->data->adv_router.s_addr);
3468
3469 ospf_lsa_unlock(&nbr->ls_req_last);
3470 nbr->ls_req_last = ospf_lsa_lock(lsa);
3471
3472 *length += 12;
3473 return 1;
3474 }
3475
3476 static int ospf_make_ls_req(struct ospf_neighbor *nbr, struct stream *s)
3477 {
3478 struct ospf_lsa *lsa;
3479 uint16_t length = OSPF_LS_REQ_MIN_SIZE;
3480 unsigned long delta = 12;
3481 struct route_table *table;
3482 struct route_node *rn;
3483 int i;
3484 struct ospf_lsdb *lsdb;
3485
3486 lsdb = &nbr->ls_req;
3487
3488 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
3489 table = lsdb->type[i].db;
3490 for (rn = route_top(table); rn; rn = route_next(rn))
3491 if ((lsa = (rn->info)) != NULL)
3492 if (ospf_make_ls_req_func(s, &length, delta,
3493 nbr, lsa)
3494 == 0) {
3495 route_unlock_node(rn);
3496 break;
3497 }
3498 }
3499 return length;
3500 }
3501
3502 static int ls_age_increment(struct ospf_lsa *lsa, int delay)
3503 {
3504 int age;
3505
3506 age = IS_LSA_MAXAGE(lsa) ? OSPF_LSA_MAXAGE : LS_AGE(lsa) + delay;
3507
3508 return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
3509 }
3510
3511 static int ospf_make_ls_upd(struct ospf_interface *oi, struct list *update,
3512 struct stream *s)
3513 {
3514 struct ospf_lsa *lsa;
3515 struct listnode *node;
3516 uint16_t length = 0;
3517 unsigned int size_noauth;
3518 unsigned long delta = stream_get_endp(s);
3519 unsigned long pp;
3520 int count = 0;
3521
3522 if (IS_DEBUG_OSPF_EVENT)
3523 zlog_debug("ospf_make_ls_upd: Start");
3524
3525 pp = stream_get_endp(s);
3526 stream_forward_endp(s, OSPF_LS_UPD_MIN_SIZE);
3527 length += OSPF_LS_UPD_MIN_SIZE;
3528
3529 /* Calculate amount of packet usable for data. */
3530 size_noauth = stream_get_size(s) - ospf_packet_authspace(oi);
3531
3532 while ((node = listhead(update)) != NULL) {
3533 struct lsa_header *lsah;
3534 uint16_t ls_age;
3535
3536 if (IS_DEBUG_OSPF_EVENT)
3537 zlog_debug("ospf_make_ls_upd: List Iteration %d",
3538 count);
3539
3540 lsa = listgetdata(node);
3541
3542 assert(lsa->data);
3543
3544 /* Will it fit? Minimum it has to fit atleast one */
3545 if ((length + delta + ntohs(lsa->data->length) > size_noauth) &&
3546 (count > 0))
3547 break;
3548
3549 /* Keep pointer to LS age. */
3550 lsah = (struct lsa_header *)(STREAM_DATA(s)
3551 + stream_get_endp(s));
3552
3553 /* Put LSA to Link State Request. */
3554 stream_put(s, lsa->data, ntohs(lsa->data->length));
3555
3556 /* Set LS age. */
3557 /* each hop must increment an lsa_age by transmit_delay
3558 of OSPF interface */
3559 ls_age = ls_age_increment(lsa,
3560 OSPF_IF_PARAM(oi, transmit_delay));
3561 lsah->ls_age = htons(ls_age);
3562
3563 length += ntohs(lsa->data->length);
3564 count++;
3565
3566 list_delete_node(update, node);
3567 ospf_lsa_unlock(&lsa); /* oi->ls_upd_queue */
3568 }
3569
3570 /* Now set #LSAs. */
3571 stream_putl_at(s, pp, count);
3572
3573 if (IS_DEBUG_OSPF_EVENT)
3574 zlog_debug("ospf_make_ls_upd: Stop");
3575 return length;
3576 }
3577
3578 static int ospf_make_ls_ack(struct ospf_interface *oi, struct list *ack,
3579 struct stream *s)
3580 {
3581 struct listnode *node, *nnode;
3582 uint16_t length = OSPF_LS_ACK_MIN_SIZE;
3583 unsigned long delta = OSPF_LSA_HEADER_SIZE;
3584 struct ospf_lsa *lsa;
3585
3586 for (ALL_LIST_ELEMENTS(ack, node, nnode, lsa)) {
3587 assert(lsa);
3588
3589 /* LS Ack packet overflows interface MTU
3590 * delta is just number of bytes required for
3591 * 1 LS Ack(1 LS Hdr) ospf_packet_max will return
3592 * the number of bytes can be accomodated without
3593 * ospf header. So length+delta can be compared
3594 * against ospf_packet_max to check if it can fit
3595 * another ls header in the same packet.
3596 */
3597 if ((length + delta) > ospf_packet_max(oi))
3598 break;
3599
3600 stream_put(s, lsa->data, OSPF_LSA_HEADER_SIZE);
3601 length += OSPF_LSA_HEADER_SIZE;
3602
3603 listnode_delete(ack, lsa);
3604 ospf_lsa_unlock(&lsa); /* oi->ls_ack_direct.ls_ack */
3605 }
3606
3607 return length;
3608 }
3609
3610 static void ospf_hello_send_sub(struct ospf_interface *oi, in_addr_t addr)
3611 {
3612 struct ospf_packet *op;
3613 uint16_t length = OSPF_HEADER_SIZE;
3614
3615 op = ospf_packet_new(oi->ifp->mtu);
3616
3617 /* Prepare OSPF common header. */
3618 ospf_make_header(OSPF_MSG_HELLO, oi, op->s);
3619
3620 /* Prepare OSPF Hello body. */
3621 length += ospf_make_hello(oi, op->s);
3622 if (length == OSPF_HEADER_SIZE) {
3623 /* Hello overshooting MTU */
3624 ospf_packet_free(op);
3625 return;
3626 }
3627
3628 /* Fill OSPF header. */
3629 ospf_fill_header(oi, op->s, length);
3630
3631 /* Set packet length. */
3632 op->length = length;
3633
3634 op->dst.s_addr = addr;
3635
3636 if (IS_DEBUG_OSPF_EVENT) {
3637 if (oi->ospf->vrf_id)
3638 zlog_debug(
3639 "%s: Hello Tx interface %s ospf vrf %s id %u",
3640 __PRETTY_FUNCTION__, oi->ifp->name,
3641 ospf_vrf_id_to_name(oi->ospf->vrf_id),
3642 oi->ospf->vrf_id);
3643 }
3644 /* Add packet to the top of the interface output queue, so that they
3645 * can't get delayed by things like long queues of LS Update packets
3646 */
3647 ospf_packet_add_top(oi, op);
3648
3649 /* Hook thread to write packet. */
3650 OSPF_ISM_WRITE_ON(oi->ospf);
3651 }
3652
3653 static void ospf_poll_send(struct ospf_nbr_nbma *nbr_nbma)
3654 {
3655 struct ospf_interface *oi;
3656
3657 oi = nbr_nbma->oi;
3658 assert(oi);
3659
3660 /* If this is passive interface, do not send OSPF Hello. */
3661 if (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE)
3662 return;
3663
3664 if (oi->type != OSPF_IFTYPE_NBMA)
3665 return;
3666
3667 if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
3668 return;
3669
3670 if (PRIORITY(oi) == 0)
3671 return;
3672
3673 if (nbr_nbma->priority == 0 && oi->state != ISM_DR
3674 && oi->state != ISM_Backup)
3675 return;
3676
3677 ospf_hello_send_sub(oi, nbr_nbma->addr.s_addr);
3678 }
3679
3680 int ospf_poll_timer(struct thread *thread)
3681 {
3682 struct ospf_nbr_nbma *nbr_nbma;
3683
3684 nbr_nbma = THREAD_ARG(thread);
3685 nbr_nbma->t_poll = NULL;
3686
3687 if (IS_DEBUG_OSPF(nsm, NSM_TIMERS))
3688 zlog_debug("NSM[%s:%s]: Timer (Poll timer expire)",
3689 IF_NAME(nbr_nbma->oi), inet_ntoa(nbr_nbma->addr));
3690
3691 ospf_poll_send(nbr_nbma);
3692
3693 if (nbr_nbma->v_poll > 0)
3694 OSPF_POLL_TIMER_ON(nbr_nbma->t_poll, ospf_poll_timer,
3695 nbr_nbma->v_poll);
3696
3697 return 0;
3698 }
3699
3700
3701 int ospf_hello_reply_timer(struct thread *thread)
3702 {
3703 struct ospf_neighbor *nbr;
3704
3705 nbr = THREAD_ARG(thread);
3706 nbr->t_hello_reply = NULL;
3707
3708 assert(nbr->oi);
3709
3710 if (IS_DEBUG_OSPF(nsm, NSM_TIMERS))
3711 zlog_debug("NSM[%s:%s]: Timer (hello-reply timer expire)",
3712 IF_NAME(nbr->oi), inet_ntoa(nbr->router_id));
3713
3714 ospf_hello_send_sub(nbr->oi, nbr->address.u.prefix4.s_addr);
3715
3716 return 0;
3717 }
3718
3719 /* Send OSPF Hello. */
3720 void ospf_hello_send(struct ospf_interface *oi)
3721 {
3722 /* If this is passive interface, do not send OSPF Hello. */
3723 if (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE)
3724 return;
3725
3726 if (oi->type == OSPF_IFTYPE_NBMA) {
3727 struct ospf_neighbor *nbr;
3728 struct route_node *rn;
3729
3730 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
3731 if ((nbr = rn->info))
3732 if (nbr != oi->nbr_self)
3733 if (nbr->state != NSM_Down) {
3734 /* RFC 2328 Section 9.5.1
3735 If the router is not
3736 eligible to become Designated
3737 Router,
3738 it must periodically send
3739 Hello Packets to both the
3740 Designated Router and the
3741 Backup Designated Router (if
3742 they
3743 exist). */
3744 if (PRIORITY(oi) == 0
3745 && IPV4_ADDR_CMP(
3746 &DR(oi),
3747 &nbr->address.u
3748 .prefix4)
3749 && IPV4_ADDR_CMP(
3750 &BDR(oi),
3751 &nbr->address.u
3752 .prefix4))
3753 continue;
3754
3755 /* If the router is eligible to
3756 become Designated Router, it
3757 must periodically send Hello
3758 Packets to all neighbors that
3759 are also eligible. In
3760 addition, if the router is
3761 itself the
3762 Designated Router or Backup
3763 Designated Router, it must
3764 also
3765 send periodic Hello Packets
3766 to all other neighbors. */
3767
3768 if (nbr->priority == 0
3769 && oi->state == ISM_DROther)
3770 continue;
3771 /* if oi->state == Waiting, send
3772 * hello to all neighbors */
3773 ospf_hello_send_sub(
3774 oi,
3775 nbr->address.u.prefix4
3776 .s_addr);
3777 }
3778 } else {
3779 /* Decide destination address. */
3780 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3781 ospf_hello_send_sub(oi, oi->vl_data->peer_addr.s_addr);
3782 else
3783 ospf_hello_send_sub(oi, htonl(OSPF_ALLSPFROUTERS));
3784 }
3785 }
3786
3787 /* Send OSPF Database Description. */
3788 void ospf_db_desc_send(struct ospf_neighbor *nbr)
3789 {
3790 struct ospf_interface *oi;
3791 struct ospf_packet *op;
3792 uint16_t length = OSPF_HEADER_SIZE;
3793
3794 oi = nbr->oi;
3795 op = ospf_packet_new(oi->ifp->mtu);
3796
3797 /* Prepare OSPF common header. */
3798 ospf_make_header(OSPF_MSG_DB_DESC, oi, op->s);
3799
3800 /* Prepare OSPF Database Description body. */
3801 length += ospf_make_db_desc(oi, nbr, op->s);
3802
3803 /* Fill OSPF header. */
3804 ospf_fill_header(oi, op->s, length);
3805
3806 /* Set packet length. */
3807 op->length = length;
3808
3809 /* Decide destination address. */
3810 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3811 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
3812 else
3813 op->dst = nbr->address.u.prefix4;
3814
3815 /* Add packet to the interface output queue. */
3816 ospf_packet_add(oi, op);
3817
3818 /* Hook thread to write packet. */
3819 OSPF_ISM_WRITE_ON(oi->ospf);
3820
3821 /* Remove old DD packet, then copy new one and keep in neighbor
3822 * structure. */
3823 if (nbr->last_send)
3824 ospf_packet_free(nbr->last_send);
3825 nbr->last_send = ospf_packet_dup(op);
3826 monotime(&nbr->last_send_ts);
3827 }
3828
3829 /* Re-send Database Description. */
3830 void ospf_db_desc_resend(struct ospf_neighbor *nbr)
3831 {
3832 struct ospf_interface *oi;
3833
3834 oi = nbr->oi;
3835
3836 /* Add packet to the interface output queue. */
3837 ospf_packet_add(oi, ospf_packet_dup(nbr->last_send));
3838
3839 /* Hook thread to write packet. */
3840 OSPF_ISM_WRITE_ON(oi->ospf);
3841 }
3842
3843 /* Send Link State Request. */
3844 void ospf_ls_req_send(struct ospf_neighbor *nbr)
3845 {
3846 struct ospf_interface *oi;
3847 struct ospf_packet *op;
3848 uint16_t length = OSPF_HEADER_SIZE;
3849
3850 oi = nbr->oi;
3851 op = ospf_packet_new(oi->ifp->mtu);
3852
3853 /* Prepare OSPF common header. */
3854 ospf_make_header(OSPF_MSG_LS_REQ, oi, op->s);
3855
3856 /* Prepare OSPF Link State Request body. */
3857 length += ospf_make_ls_req(nbr, op->s);
3858 if (length == OSPF_HEADER_SIZE) {
3859 ospf_packet_free(op);
3860 return;
3861 }
3862
3863 /* Fill OSPF header. */
3864 ospf_fill_header(oi, op->s, length);
3865
3866 /* Set packet length. */
3867 op->length = length;
3868
3869 /* Decide destination address. */
3870 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3871 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
3872 else
3873 op->dst = nbr->address.u.prefix4;
3874
3875 /* Add packet to the interface output queue. */
3876 ospf_packet_add(oi, op);
3877
3878 /* Hook thread to write packet. */
3879 OSPF_ISM_WRITE_ON(oi->ospf);
3880
3881 /* Add Link State Request Retransmission Timer. */
3882 OSPF_NSM_TIMER_ON(nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
3883 }
3884
3885 /* Send Link State Update with an LSA. */
3886 void ospf_ls_upd_send_lsa(struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
3887 int flag)
3888 {
3889 struct list *update;
3890
3891 update = list_new();
3892
3893 listnode_add(update, lsa);
3894
3895 /*ospf instance is going down, send self originated
3896 * MAXAGE LSA update to neighbors to remove from LSDB */
3897 if (nbr->oi->ospf->inst_shutdown && IS_LSA_MAXAGE(lsa))
3898 ospf_ls_upd_send(nbr, update, flag, 1);
3899 else
3900 ospf_ls_upd_send(nbr, update, flag, 0);
3901
3902 list_delete(&update);
3903 }
3904
3905 /* Determine size for packet. Must be at least big enough to accomodate next
3906 * LSA on list, which may be bigger than MTU size.
3907 *
3908 * Return pointer to new ospf_packet
3909 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3910 * on packet sizes (in which case offending LSA is deleted from update list)
3911 */
3912 static struct ospf_packet *ospf_ls_upd_packet_new(struct list *update,
3913 struct ospf_interface *oi)
3914 {
3915 struct ospf_lsa *lsa;
3916 struct listnode *ln;
3917 size_t size;
3918 static char warned = 0;
3919
3920 lsa = listgetdata((ln = listhead(update)));
3921 assert(lsa->data);
3922
3923 if ((OSPF_LS_UPD_MIN_SIZE + ntohs(lsa->data->length))
3924 > ospf_packet_max(oi)) {
3925 if (!warned) {
3926 flog_warn(
3927 EC_OSPF_LARGE_LSA,
3928 "ospf_ls_upd_packet_new: oversized LSA encountered!"
3929 "will need to fragment. Not optimal. Try divide up"
3930 " your network with areas. Use 'debug ospf packet send'"
3931 " to see details, or look at 'show ip ospf database ..'");
3932 warned = 1;
3933 }
3934
3935 if (IS_DEBUG_OSPF_PACKET(0, SEND))
3936 zlog_debug(
3937 "ospf_ls_upd_packet_new: oversized LSA id:%s,"
3938 " %d bytes originated by %s, will be fragmented!",
3939 inet_ntoa(lsa->data->id),
3940 ntohs(lsa->data->length),
3941 inet_ntoa(lsa->data->adv_router));
3942
3943 /*
3944 * Allocate just enough to fit this LSA only, to avoid including
3945 * other
3946 * LSAs in fragmented LSA Updates.
3947 */
3948 size = ntohs(lsa->data->length)
3949 + (oi->ifp->mtu - ospf_packet_max(oi))
3950 + OSPF_LS_UPD_MIN_SIZE;
3951 } else
3952 size = oi->ifp->mtu;
3953
3954 if (size > OSPF_MAX_PACKET_SIZE) {
3955 flog_warn(EC_OSPF_LARGE_LSA,
3956 "ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
3957 " %d bytes, packet size %ld, dropping it completely."
3958 " OSPF routing is broken!",
3959 inet_ntoa(lsa->data->id), ntohs(lsa->data->length),
3960 (long int)size);
3961 list_delete_node(update, ln);
3962 return NULL;
3963 }
3964
3965 /* IP header is built up separately by ospf_write(). This means, that we
3966 * must
3967 * reduce the "affordable" size just calculated by length of an IP
3968 * header.
3969 * This makes sure, that even if we manage to fill the payload with LSA
3970 * data
3971 * completely, the final packet (our data plus IP header) still fits
3972 * into
3973 * outgoing interface MTU. This correction isn't really meaningful for
3974 * an
3975 * oversized LSA, but for consistency the correction is done for both
3976 * cases.
3977 *
3978 * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
3979 */
3980 return ospf_packet_new(size - sizeof(struct ip));
3981 }
3982
3983 static void ospf_ls_upd_queue_send(struct ospf_interface *oi,
3984 struct list *update, struct in_addr addr,
3985 int send_lsupd_now)
3986 {
3987 struct ospf_packet *op;
3988 uint16_t length = OSPF_HEADER_SIZE;
3989
3990 if (IS_DEBUG_OSPF_EVENT)
3991 zlog_debug("listcount = %d, [%s]dst %s", listcount(update),
3992 IF_NAME(oi), inet_ntoa(addr));
3993
3994 /* Check that we have really something to process */
3995 if (listcount(update) == 0)
3996 return;
3997
3998 op = ospf_ls_upd_packet_new(update, oi);
3999
4000 /* Prepare OSPF common header. */
4001 ospf_make_header(OSPF_MSG_LS_UPD, oi, op->s);
4002
4003 /* Prepare OSPF Link State Update body.
4004 * Includes Type-7 translation.
4005 */
4006 length += ospf_make_ls_upd(oi, update, op->s);
4007
4008 /* Fill OSPF header. */
4009 ospf_fill_header(oi, op->s, length);
4010
4011 /* Set packet length. */
4012 op->length = length;
4013
4014 /* Decide destination address. */
4015 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
4016 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
4017 else
4018 op->dst.s_addr = addr.s_addr;
4019
4020 /* Add packet to the interface output queue. */
4021 ospf_packet_add(oi, op);
4022 /* Call ospf_write() right away to send ospf packets to neighbors */
4023 if (send_lsupd_now) {
4024 struct thread os_packet_thd;
4025
4026 os_packet_thd.arg = (void *)oi->ospf;
4027 if (oi->on_write_q == 0) {
4028 listnode_add(oi->ospf->oi_write_q, oi);
4029 oi->on_write_q = 1;
4030 }
4031 ospf_write(&os_packet_thd);
4032 /*
4033 * We are fake calling ospf_write with a fake
4034 * thread. Imagine that we have oi_a already
4035 * enqueued and we have turned on the write
4036 * thread(t_write).
4037 * Now this function calls this for oi_b
4038 * so the on_write_q has oi_a and oi_b on
4039 * it, ospf_write runs and clears the packets
4040 * for both oi_a and oi_b. Removing them from
4041 * the on_write_q. After this thread of execution
4042 * finishes we will execute the t_write thread
4043 * with nothing in the on_write_q causing an
4044 * assert. So just make sure that the t_write
4045 * is actually turned off.
4046 */
4047 if (list_isempty(oi->ospf->oi_write_q))
4048 OSPF_TIMER_OFF(oi->ospf->t_write);
4049 } else {
4050 /* Hook thread to write packet. */
4051 OSPF_ISM_WRITE_ON(oi->ospf);
4052 }
4053 }
4054
4055 static int ospf_ls_upd_send_queue_event(struct thread *thread)
4056 {
4057 struct ospf_interface *oi = THREAD_ARG(thread);
4058 struct route_node *rn;
4059 struct route_node *rnext;
4060 struct list *update;
4061 char again = 0;
4062
4063 oi->t_ls_upd_event = NULL;
4064
4065 if (IS_DEBUG_OSPF_EVENT)
4066 zlog_debug("ospf_ls_upd_send_queue start");
4067
4068 for (rn = route_top(oi->ls_upd_queue); rn; rn = rnext) {
4069 rnext = route_next(rn);
4070
4071 if (rn->info == NULL)
4072 continue;
4073
4074 update = (struct list *)rn->info;
4075
4076 ospf_ls_upd_queue_send(oi, update, rn->p.u.prefix4, 0);
4077
4078 /* list might not be empty. */
4079 if (listcount(update) == 0) {
4080 list_delete((struct list **)&rn->info);
4081 route_unlock_node(rn);
4082 } else
4083 again = 1;
4084 }
4085
4086 if (again != 0) {
4087 if (IS_DEBUG_OSPF_EVENT)
4088 zlog_debug(
4089 "ospf_ls_upd_send_queue: update lists not cleared,"
4090 " %d nodes to try again, raising new event",
4091 again);
4092 oi->t_ls_upd_event = NULL;
4093 thread_add_event(master, ospf_ls_upd_send_queue_event, oi, 0,
4094 &oi->t_ls_upd_event);
4095 }
4096
4097 if (IS_DEBUG_OSPF_EVENT)
4098 zlog_debug("ospf_ls_upd_send_queue stop");
4099
4100 return 0;
4101 }
4102
4103 void ospf_ls_upd_send(struct ospf_neighbor *nbr, struct list *update, int flag,
4104 int send_lsupd_now)
4105 {
4106 struct ospf_interface *oi;
4107 struct ospf_lsa *lsa;
4108 struct prefix_ipv4 p;
4109 struct route_node *rn;
4110 struct listnode *node;
4111
4112 oi = nbr->oi;
4113
4114 p.family = AF_INET;
4115 p.prefixlen = IPV4_MAX_BITLEN;
4116
4117 /* Decide destination address. */
4118 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
4119 p.prefix = oi->vl_data->peer_addr;
4120 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
4121 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
4122 else if (flag == OSPF_SEND_PACKET_DIRECT)
4123 p.prefix = nbr->address.u.prefix4;
4124 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
4125 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
4126 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
4127 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
4128 else
4129 p.prefix.s_addr = htonl(OSPF_ALLDROUTERS);
4130
4131 if (oi->type == OSPF_IFTYPE_NBMA) {
4132 if (flag == OSPF_SEND_PACKET_INDIRECT)
4133 flog_warn(
4134 EC_OSPF_PACKET,
4135 "* LS-Update is directly sent on NBMA network.");
4136 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix))
4137 flog_warn(EC_OSPF_PACKET,
4138 "* LS-Update is sent to myself.");
4139 }
4140
4141 rn = route_node_get(oi->ls_upd_queue, (struct prefix *)&p);
4142
4143 if (rn->info == NULL)
4144 rn->info = list_new();
4145 else
4146 route_unlock_node(rn);
4147
4148 for (ALL_LIST_ELEMENTS_RO(update, node, lsa))
4149 listnode_add(rn->info,
4150 ospf_lsa_lock(lsa)); /* oi->ls_upd_queue */
4151 if (send_lsupd_now) {
4152 struct list *send_update_list;
4153 struct route_node *rnext;
4154
4155 for (rn = route_top(oi->ls_upd_queue); rn; rn = rnext) {
4156 rnext = route_next(rn);
4157
4158 if (rn->info == NULL)
4159 continue;
4160
4161 send_update_list = (struct list *)rn->info;
4162
4163 ospf_ls_upd_queue_send(oi, send_update_list,
4164 rn->p.u.prefix4, 1);
4165 }
4166 } else
4167 thread_add_event(master, ospf_ls_upd_send_queue_event, oi, 0,
4168 &oi->t_ls_upd_event);
4169 }
4170
4171 static void ospf_ls_ack_send_list(struct ospf_interface *oi, struct list *ack,
4172 struct in_addr dst)
4173 {
4174 struct ospf_packet *op;
4175 uint16_t length = OSPF_HEADER_SIZE;
4176
4177 op = ospf_packet_new(oi->ifp->mtu);
4178
4179 /* Prepare OSPF common header. */
4180 ospf_make_header(OSPF_MSG_LS_ACK, oi, op->s);
4181
4182 /* Prepare OSPF Link State Acknowledgment body. */
4183 length += ospf_make_ls_ack(oi, ack, op->s);
4184
4185 /* Fill OSPF header. */
4186 ospf_fill_header(oi, op->s, length);
4187
4188 /* Set packet length. */
4189 op->length = length;
4190
4191 /* Decide destination address. */
4192 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
4193 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
4194 else
4195 op->dst.s_addr = dst.s_addr;
4196
4197 /* Add packet to the interface output queue. */
4198 ospf_packet_add(oi, op);
4199
4200 /* Hook thread to write packet. */
4201 OSPF_ISM_WRITE_ON(oi->ospf);
4202 }
4203
4204 static int ospf_ls_ack_send_event(struct thread *thread)
4205 {
4206 struct ospf_interface *oi = THREAD_ARG(thread);
4207
4208 oi->t_ls_ack_direct = NULL;
4209
4210 while (listcount(oi->ls_ack_direct.ls_ack))
4211 ospf_ls_ack_send_list(oi, oi->ls_ack_direct.ls_ack,
4212 oi->ls_ack_direct.dst);
4213
4214 return 0;
4215 }
4216
4217 void ospf_ls_ack_send(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
4218 {
4219 struct ospf_interface *oi = nbr->oi;
4220
4221 if (listcount(oi->ls_ack_direct.ls_ack) == 0)
4222 oi->ls_ack_direct.dst = nbr->address.u.prefix4;
4223
4224 listnode_add(oi->ls_ack_direct.ls_ack, ospf_lsa_lock(lsa));
4225
4226 thread_add_event(master, ospf_ls_ack_send_event, oi, 0,
4227 &oi->t_ls_ack_direct);
4228 }
4229
4230 /* Send Link State Acknowledgment delayed. */
4231 void ospf_ls_ack_send_delayed(struct ospf_interface *oi)
4232 {
4233 struct in_addr dst;
4234
4235 /* Decide destination address. */
4236 /* RFC2328 Section 13.5 On non-broadcast
4237 networks, delayed Link State Acknowledgment packets must be
4238 unicast separately over each adjacency (i.e., neighbor whose
4239 state is >= Exchange). */
4240 if (oi->type == OSPF_IFTYPE_NBMA) {
4241 struct ospf_neighbor *nbr;
4242 struct route_node *rn;
4243
4244 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
4245 if ((nbr = rn->info) != NULL)
4246 if (nbr != oi->nbr_self
4247 && nbr->state >= NSM_Exchange)
4248 while (listcount(oi->ls_ack))
4249 ospf_ls_ack_send_list(
4250 oi, oi->ls_ack,
4251 nbr->address.u.prefix4);
4252 return;
4253 }
4254 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
4255 dst.s_addr = oi->vl_data->peer_addr.s_addr;
4256 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
4257 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
4258 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
4259 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
4260 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
4261 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
4262 else
4263 dst.s_addr = htonl(OSPF_ALLDROUTERS);
4264
4265 while (listcount(oi->ls_ack))
4266 ospf_ls_ack_send_list(oi, oi->ls_ack, dst);
4267 }
4268
4269 /*
4270 * On pt-to-pt links, all OSPF control packets are sent to the multicast
4271 * address. As a result, the kernel does not need to learn the interface
4272 * MAC of the OSPF neighbor. However, in our world, this will delay
4273 * convergence. Take the case when due to a link flap, all routes now
4274 * want to use an interface which was deemed to be costlier prior to this
4275 * event. For routes that will be installed, the missing MAC will have
4276 * punt-to-CPU set on them. This may overload the CPU control path that
4277 * can be avoided if the MAC was known apriori.
4278 */
4279 #define OSPF_PING_NBR_STR_MAX (BUFSIZ)
4280 void ospf_proactively_arp(struct ospf_neighbor *nbr)
4281 {
4282 char ping_nbr[OSPF_PING_NBR_STR_MAX];
4283 int ret;
4284
4285 if (!nbr || !nbr->oi || !nbr->oi->ifp)
4286 return;
4287
4288 snprintf(ping_nbr, sizeof(ping_nbr),
4289 "ping -c 1 -I %s %s > /dev/null 2>&1 &", nbr->oi->ifp->name,
4290 inet_ntoa(nbr->address.u.prefix4));
4291
4292 ret = system(ping_nbr);
4293 if (IS_DEBUG_OSPF_EVENT)
4294 zlog_debug("Executed %s %s", ping_nbr,
4295 ((ret == 0) ? "successfully" : "but failed"));
4296 }