1/*
2 * RIPng routes function.
3 * Copyright (C) 1998 Kunihiro Ishiguro
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
18 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "table.h"
27#include "memory.h"
28#include "if.h"
29
30#include "ripngd/ripngd.h"
31#include "ripngd/ripng_route.h"
32
33struct ripng_aggregate *
34ripng_aggregate_new ()
35{
36  struct ripng_aggregate *new;
37
38  new = XCALLOC (MTYPE_RIPNG_AGGREGATE, sizeof (struct ripng_aggregate));
39  return new;
40}
41
42void
43ripng_aggregate_free (struct ripng_aggregate *aggregate)
44{
45  XFREE (MTYPE_RIPNG_AGGREGATE, aggregate);
46}
47
48/* Aggregate count increment check. */
49void
50ripng_aggregate_increment (struct route_node *child, struct ripng_info *rinfo)
51{
52  struct route_node *np;
53  struct ripng_aggregate *aggregate;
54
55  for (np = child; np; np = np->parent)
56    if ((aggregate = np->aggregate) != NULL)
57      {
58	aggregate->count++;
59	rinfo->suppress++;
60      }
61}
62
63/* Aggregate count decrement check. */
64void
65ripng_aggregate_decrement (struct route_node *child, struct ripng_info *rinfo)
66{
67  struct route_node *np;
68  struct ripng_aggregate *aggregate;
69
70  for (np = child; np; np = np->parent)
71    if ((aggregate = np->aggregate) != NULL)
72      {
73	aggregate->count--;
74	rinfo->suppress--;
75      }
76}
77
78/* RIPng routes treatment. */
79int
80ripng_aggregate_add (struct prefix *p)
81{
82  struct route_node *top;
83  struct route_node *rp;
84  struct ripng_info *rinfo;
85  struct ripng_aggregate *aggregate;
86  struct ripng_aggregate *sub;
87
88  /* Get top node for aggregation. */
89  top = route_node_get (ripng->table, p);
90
91  /* Allocate new aggregate. */
92  aggregate = ripng_aggregate_new ();
93  aggregate->metric = 1;
94
95  top->aggregate = aggregate;
96
97  /* Suppress routes match to the aggregate. */
98  for (rp = route_lock_node (top); rp; rp = route_next_until (rp, top))
99    {
100      /* Suppress normal route. */
101      if ((rinfo = rp->info) != NULL)
102	{
103	  aggregate->count++;
104	  rinfo->suppress++;
105	}
106      /* Suppress aggregate route.  This may not need. */
107      if (rp != top && (sub = rp->aggregate) != NULL)
108	{
109	  aggregate->count++;
110	  sub->suppress++;
111	}
112    }
113
114  return 0;
115}
116
117/* Delete RIPng static route. */
118int
119ripng_aggregate_delete (struct prefix *p)
120{
121  struct route_node *top;
122  struct route_node *rp;
123  struct ripng_info *rinfo;
124  struct ripng_aggregate *aggregate;
125  struct ripng_aggregate *sub;
126
127  /* Get top node for aggregation. */
128  top = route_node_get (ripng->table, p);
129
130  /* Allocate new aggregate. */
131  aggregate = top->aggregate;
132
133  /* Suppress routes match to the aggregate. */
134  for (rp = route_lock_node (top); rp; rp = route_next_until (rp, top))
135    {
136      /* Suppress normal route. */
137      if ((rinfo = rp->info) != NULL)
138	{
139	  aggregate->count--;
140	  rinfo->suppress--;
141	}
142
143      if (rp != top && (sub = rp->aggregate) != NULL)
144	{
145	  aggregate->count--;
146	  sub->suppress--;
147	}
148    }
149
150  top->aggregate = NULL;
151  ripng_aggregate_free (aggregate);
152
153  route_unlock_node (top);
154  route_unlock_node (top);
155
156  return 0;
157}
158