summaryrefslogtreecommitdiff
path: root/ospf6d/ospf6_nsm.c
blob: aa08d40be986a8793634f64b179319a0e6f2030b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Copyright (C) 1999 Yasuhiro Ohara
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.  
 */

#include "ospf6d.h"

static int
nbs_full_change (struct ospf6_interface *ospf6_interface)
{
  CALL_FOREACH_LSA_HOOK (hook_interface, hook_change, ospf6_interface);
  return 0;
}

static int
nbs_change (state_t nbs_next, char *reason, struct ospf6_neighbor *o6n)
{
  state_t nbs_previous;

  nbs_previous = o6n->state;
  o6n->state = nbs_next;

  if (nbs_previous == nbs_next)
    return 0;

  /* statistics */
  o6n->ospf6_stat_state_changed++;
  gettimeofday (&o6n->last_changed, NULL);

  /* log */
  if (IS_OSPF6_DUMP_NEIGHBOR)
    {
      if (reason)
        zlog_info ("Neighbor status change %s: [%s]->[%s](%s)",
                   o6n->str,
                   ospf6_neighbor_state_string[nbs_previous],
                   ospf6_neighbor_state_string[nbs_next],
                   reason);
      else
        zlog_info ("Neighbor status change %s: [%s]->[%s]",
                   o6n->str,
                   ospf6_neighbor_state_string[nbs_previous],
                   ospf6_neighbor_state_string[nbs_next]);
    }

  if (nbs_previous == NBS_FULL || nbs_next == NBS_FULL)
    nbs_full_change (o6n->ospf6_interface);

  /* check for LSAs that already reached MaxAge */
  if ((nbs_previous == NBS_EXCHANGE || nbs_previous == NBS_LOADING) &&
      (nbs_next != NBS_EXCHANGE && nbs_next != NBS_LOADING))
    {
      ospf6_maxage_remover ();
    }

  CALL_CHANGE_HOOK (&neighbor_hook, o6n);

  return 0;
}

/* RFC2328 section 10.4 */
int
need_adjacency (struct ospf6_neighbor *o6n)
{

  if (o6n->ospf6_interface->state == IFS_PTOP)
    return 1;
  if (o6n->ospf6_interface->state == IFS_DR)
    return 1;
  if (o6n->ospf6_interface->state == IFS_BDR)
    return 1;
  if (o6n->router_id == o6n->ospf6_interface->dr)
    return 1;
  if (o6n->router_id == o6n->ospf6_interface->bdr)
    return 1;

  return 0;
}

int
hello_received (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *HelloReceived*", o6n->str);

  if (o6n->inactivity_timer)
    thread_cancel (o6n->inactivity_timer);

  o6n->inactivity_timer = thread_add_timer (master, inactivity_timer, o6n,
                                            o6n->ospf6_interface->dead_interval);
  if (o6n->state <= NBS_DOWN)
    nbs_change (NBS_INIT, "HelloReceived", o6n);
  return 0;
}

int
twoway_received (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state > NBS_INIT)
    return 0;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *2Way-Received*", o6n->str);

  thread_add_event (master, neighbor_change, o6n->ospf6_interface, 0);

  if (!need_adjacency (o6n))
    {
      nbs_change (NBS_TWOWAY, "No Need Adjacency", o6n);
      return 0;
    }
  else
    nbs_change (NBS_EXSTART, "Need Adjacency", o6n);

  DD_MSBIT_SET (o6n->dbdesc_bits);
  DD_MBIT_SET (o6n->dbdesc_bits);
  DD_IBIT_SET (o6n->dbdesc_bits);

  if (o6n->thread_send_dbdesc)
    thread_cancel (o6n->thread_send_dbdesc);
  o6n->thread_send_dbdesc =
    thread_add_event (master, ospf6_send_dbdesc, o6n, 0);
  if (o6n->thread_rxmt_dbdesc)
    thread_cancel (o6n->thread_rxmt_dbdesc);
  o6n->thread_rxmt_dbdesc = (struct thread *) NULL;

  return 0;
}

int
negotiation_done (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state != NBS_EXSTART)
    return 0;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *NegotiationDone*", o6n->str);

  nbs_change (NBS_EXCHANGE, "NegotiationDone", o6n);
  DD_IBIT_CLEAR (o6n->dbdesc_bits);

  return 0;
}

int
exchange_done (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state != NBS_EXCHANGE)
    return 0;

  if (o6n->thread_rxmt_dbdesc)
    thread_cancel (o6n->thread_rxmt_dbdesc);
  o6n->thread_rxmt_dbdesc = (struct thread *) NULL;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *ExchangeDone*", o6n->str);

  ospf6_lsdb_remove_all (o6n->dbdesc_list);

  thread_add_timer (master, ospf6_neighbor_last_dbdesc_release, o6n,
                    o6n->ospf6_interface->dead_interval);

  if (o6n->request_list->count == 0)
    nbs_change (NBS_FULL, "Requestlist Empty", o6n);
  else
    {
      thread_add_event (master, ospf6_send_lsreq, o6n, 0);
      nbs_change (NBS_LOADING, "Requestlist Not Empty", o6n);
    }
  return 0;
}

int
loading_done (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state != NBS_LOADING)
    return 0;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *LoadingDone*", o6n->str);

  assert (o6n->request_list->count == 0);

  nbs_change (NBS_FULL, "LoadingDone", o6n);

  return 0;
}

int
adj_ok (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *AdjOK?*", o6n->str);

  if (o6n->state == NBS_TWOWAY)
    {
      if (!need_adjacency (o6n))
        {
          nbs_change (NBS_TWOWAY, "No Need Adjacency", o6n);
          return 0;
        }
      else
        nbs_change (NBS_EXSTART, "Need Adjacency", o6n);

      DD_MSBIT_SET (o6n->dbdesc_bits);
      DD_MBIT_SET (o6n->dbdesc_bits);
      DD_IBIT_SET (o6n->dbdesc_bits);

      if (o6n->thread_send_dbdesc)
        thread_cancel (o6n->thread_send_dbdesc);
      o6n->thread_send_dbdesc =
        thread_add_event (master, ospf6_send_dbdesc, o6n, 0);

      return 0;
    }

  if (o6n->state >= NBS_EXSTART)
    {
      if (need_adjacency (o6n))
        return 0;
      else
        {
          nbs_change (NBS_TWOWAY, "No Need Adjacency", o6n);
          ospf6_neighbor_lslist_clear (o6n);
        }
    }
  return 0;
}

int
seqnumber_mismatch (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state < NBS_EXCHANGE)
    return 0;

  /* statistics */
  o6n->ospf6_stat_seqnum_mismatch++;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *SeqNumberMismatch*", o6n->str);

  nbs_change (NBS_EXSTART, "SeqNumberMismatch", o6n);

  DD_MSBIT_SET (o6n->dbdesc_bits);
  DD_MBIT_SET (o6n->dbdesc_bits);
  DD_IBIT_SET (o6n->dbdesc_bits);
  ospf6_neighbor_lslist_clear (o6n);

  if (o6n->thread_send_dbdesc)
    thread_cancel (o6n->thread_send_dbdesc);
  o6n->thread_send_dbdesc =
    thread_add_event (master, ospf6_send_dbdesc, o6n, 0);

  return 0;
}

int
bad_lsreq (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state < NBS_EXCHANGE)
    return 0;

  /* statistics */
  o6n->ospf6_stat_bad_lsreq++;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *BadLSReq*", o6n->str);

  nbs_change (NBS_EXSTART, "BadLSReq", o6n);

  DD_MSBIT_SET (o6n->dbdesc_bits);
  DD_MBIT_SET (o6n->dbdesc_bits);
  DD_IBIT_SET (o6n->dbdesc_bits);
  ospf6_neighbor_lslist_clear (o6n);

  if (o6n->thread_send_dbdesc)
    thread_cancel (o6n->thread_send_dbdesc);
  o6n->thread_send_dbdesc =
    thread_add_event (master, ospf6_send_dbdesc, o6n, 0);

  return 0;
}

int
oneway_received (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  if (o6n->state < NBS_TWOWAY)
    return 0;

  /* statistics */
  o6n->ospf6_stat_oneway_received++;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *1Way-Received*", o6n->str);

  nbs_change (NBS_INIT, "1Way-Received", o6n);

  thread_add_event (master, neighbor_change, o6n->ospf6_interface, 0);

  ospf6_neighbor_thread_cancel_all (o6n);
  ospf6_neighbor_lslist_clear (o6n);
  return 0;
}

int
inactivity_timer (struct thread *thread)
{
  struct ospf6_neighbor *o6n;

  o6n = (struct ospf6_neighbor *) THREAD_ARG (thread);
  assert (o6n);

  /* statistics */
  o6n->ospf6_stat_inactivity_timer++;

  if (IS_OSPF6_DUMP_NEIGHBOR)
    zlog_info ("Neighbor Event %s: *InactivityTimer*", o6n->str);

  o6n->inactivity_timer = NULL;
  o6n->dr = o6n->bdr = o6n->prevdr = o6n->prevbdr = 0;
  nbs_change (NBS_DOWN, "InactivityTimer", o6n);

  thread_add_event (master, neighbor_change, o6n->ospf6_interface, 0);

  listnode_delete (o6n->ospf6_interface->neighbor_list, o6n);
  ospf6_neighbor_delete (o6n);

  return 0;
}