summaryrefslogtreecommitdiffstats
path: root/ripd/rip_offset.c
blob: 0d473482e7f9877450d36af78baef3cab4de5f2b (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* RIP offset-list
 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
 *
 * 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 <zebra.h>

#include "if.h"
#include "prefix.h"
#include "filter.h"
#include "command.h"
#include "linklist.h"
#include "memory.h"

#define RIP_OFFSET_LIST_IN  0
#define RIP_OFFSET_LIST_OUT 1
#define RIP_OFFSET_LIST_MAX 2

struct rip_offset_list
{
  char *ifname;

  struct 
  {
    char *alist_name;
    /* struct access_list *alist; */
    int metric;
  } direct[RIP_OFFSET_LIST_MAX];
};

static struct list *rip_offset_list_master;

int
strcmp_safe (char *s1, char *s2)
{
  if (s1 == NULL && s2 == NULL)
    return 0;
  if (s1 == NULL)
    return -1;
  if (s2 == NULL)
    return 1;
  return strcmp (s1, s2);
}

struct rip_offset_list *
rip_offset_list_new ()
{
  struct rip_offset_list *new;

  new = XMALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
  memset (new, 0, sizeof (struct rip_offset_list));
  return new;
}

void
rip_offset_list_free (struct rip_offset_list *offset)
{
  XFREE (MTYPE_RIP_OFFSET_LIST, offset);
}

struct rip_offset_list *
rip_offset_list_lookup (char *ifname)
{
  struct rip_offset_list *offset;
  struct listnode *nn;

  LIST_LOOP (rip_offset_list_master, offset, nn)
    {
      if (strcmp_safe (offset->ifname, ifname) == 0)
	return offset;
    }
  return NULL;
}

struct rip_offset_list *
rip_offset_list_get (char *ifname)
{
  struct rip_offset_list *offset;
  
  offset = rip_offset_list_lookup (ifname);
  if (offset)
    return offset;

  offset = rip_offset_list_new ();
  if (ifname)
    offset->ifname = strdup (ifname);
  listnode_add_sort (rip_offset_list_master, offset);

  return offset;
}

int
rip_offset_list_set (struct vty *vty, char *alist, char *direct_str,
		     char *metric_str, char *ifname)
{
  int direct;
  int metric;
  struct rip_offset_list *offset;

  /* Check direction. */
  if (strncmp (direct_str, "i", 1) == 0)
    direct = RIP_OFFSET_LIST_IN;
  else if (strncmp (direct_str, "o", 1) == 0)
    direct = RIP_OFFSET_LIST_OUT;
  else
    {
      vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
      return CMD_WARNING;
    }

  /* Check metric. */
  metric = atoi (metric_str);
  if (metric < 0 || metric > 16)
    {
      vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
      return CMD_WARNING;
    }

  /* Get offset-list structure with interface name. */
  offset = rip_offset_list_get (ifname);

  if (offset->direct[direct].alist_name)
    free (offset->direct[direct].alist_name);
  offset->direct[direct].alist_name = strdup (alist);
  offset->direct[direct].metric = metric;

  return CMD_SUCCESS;
}

int
rip_offset_list_unset (struct vty *vty, char *alist, char *direct_str,
		       char *metric_str, char *ifname)
{
  int direct;
  int metric;
  struct rip_offset_list *offset;

  /* Check direction. */
  if (strncmp (direct_str, "i", 1) == 0)
    direct = RIP_OFFSET_LIST_IN;
  else if (strncmp (direct_str, "o", 1) == 0)
    direct = RIP_OFFSET_LIST_OUT;
  else
    {
      vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
      return CMD_WARNING;
    }

  /* Check metric. */
  metric = atoi (metric_str);
  if (metric < 0 || metric > 16)
    {
      vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
      return CMD_WARNING;
    }

  /* Get offset-list structure with interface name. */
  offset = rip_offset_list_lookup (ifname);

  if (offset)
    {
      if (offset->direct[direct].alist_name)
	free (offset->direct[direct].alist_name);
      offset->direct[direct].alist_name = NULL;

      if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL &&
	  offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
	{
	  listnode_delete (rip_offset_list_master, offset);
	  if (offset->ifname)
	    free (offset->ifname);
	  rip_offset_list_free (offset);
	}
    }
  else
    {
      vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
      return CMD_WARNING;
    }
  return CMD_SUCCESS;
}

#define OFFSET_LIST_IN_NAME(O)  ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
#define OFFSET_LIST_IN_METRIC(O)  ((O)->direct[RIP_OFFSET_LIST_IN].metric)

#define OFFSET_LIST_OUT_NAME(O)  ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
#define OFFSET_LIST_OUT_METRIC(O)  ((O)->direct[RIP_OFFSET_LIST_OUT].metric)

/* If metric is modifed return 1. */
int
rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
			  u_int32_t *metric)
{
  struct rip_offset_list *offset;
  struct access_list *alist;

  /* Look up offset-list with interface name. */
  offset = rip_offset_list_lookup (ifp->name);
  if (offset && OFFSET_LIST_IN_NAME (offset))
    {
      alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));

      if (alist 
	  && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
	{
	  *metric += OFFSET_LIST_IN_METRIC (offset);
	  return 1;
	}
      return 0;
    }
  /* Look up offset-list without interface name. */
  offset = rip_offset_list_lookup (NULL);
  if (offset && OFFSET_LIST_IN_NAME (offset))
    {
      alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));

      if (alist 
	  && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
	{
	  *metric += OFFSET_LIST_IN_METRIC (offset);
	  return 1;
	}
      return 0;
    }
  return 0;
}

/* If metric is modifed return 1. */
int
rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
			  u_int32_t *metric)
{
  struct rip_offset_list *offset;
  struct access_list *alist;

  /* Look up offset-list with interface name. */
  offset = rip_offset_list_lookup (ifp->name);
  if (offset && OFFSET_LIST_OUT_NAME (offset))
    {
      alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));

      if (alist 
	  && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
	{
	  *metric += OFFSET_LIST_OUT_METRIC (offset);
	  return 1;
	}
      return 0;
    }

  /* Look up offset-list without interface name. */
  offset = rip_offset_list_lookup (NULL);
  if (offset && OFFSET_LIST_OUT_NAME (offset))
    {
      alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));

      if (alist 
	  && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
	{
	  *metric += OFFSET_LIST_OUT_METRIC (offset);
	  return 1;
	}
      return 0;
    }
  return 0;
}

DEFUN (rip_offset_list,
       rip_offset_list_cmd,
       "offset-list WORD (in|out) <0-16>",
       "Modify RIP metric\n"
       "Access-list name\n"
       "For incoming updates\n"
       "For outgoing updates\n"
       "Metric value\n")
{
  return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
}

DEFUN (rip_offset_list_ifname,
       rip_offset_list_ifname_cmd,
       "offset-list WORD (in|out) <0-16> IFNAME",
       "Modify RIP metric\n"
       "Access-list name\n"
       "For incoming updates\n"
       "For outgoing updates\n"
       "Metric value\n"
       "Interface to match\n")
{
  return rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
}

DEFUN (no_rip_offset_list,
       no_rip_offset_list_cmd,
       "no offset-list WORD (in|out) <0-16>",
       NO_STR
       "Modify RIP metric\n"
       "Access-list name\n"
       "For incoming updates\n"
       "For outgoing updates\n"
       "Metric value\n")
{
  return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
}

DEFUN (no_rip_offset_list_ifname,
       no_rip_offset_list_ifname_cmd,
       "no offset-list WORD (in|out) <0-16> IFNAME",
       NO_STR
       "Modify RIP metric\n"
       "Access-list name\n"
       "For incoming updates\n"
       "For outgoing updates\n"
       "Metric value\n"
       "Interface to match\n")
{
  return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
}

int
offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
{
  return strcmp_safe (o1->ifname, o2->ifname);
}

void
offset_list_del (struct rip_offset_list *offset)
{
  if (OFFSET_LIST_IN_NAME (offset))
    free (OFFSET_LIST_IN_NAME (offset));
  if (OFFSET_LIST_OUT_NAME (offset))
    free (OFFSET_LIST_OUT_NAME (offset));
  if (offset->ifname)
    free (offset->ifname);
  rip_offset_list_free (offset);
}

void
rip_offset_init ()
{
  rip_offset_list_master = list_new ();
  rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  rip_offset_list_master->del = (void (*)(void *)) offset_list_del;

  install_element (RIP_NODE, &rip_offset_list_cmd);
  install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
  install_element (RIP_NODE, &no_rip_offset_list_cmd);
  install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
}

void
rip_offset_clean ()
{
  list_delete (rip_offset_list_master);

  rip_offset_list_master = list_new ();
  rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
}

int
config_write_rip_offset_list (struct vty *vty)
{
  struct listnode *nn;
  struct rip_offset_list *offset;

  LIST_LOOP (rip_offset_list_master, offset, nn)
    {
      if (! offset->ifname)
	{
	  if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
	    vty_out (vty, " offset-list %s in %d%s",
		     offset->direct[RIP_OFFSET_LIST_IN].alist_name,
		     offset->direct[RIP_OFFSET_LIST_IN].metric,
		     VTY_NEWLINE);
	  if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
	    vty_out (vty, " offset-list %s out %d%s",
		     offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
		     offset->direct[RIP_OFFSET_LIST_OUT].metric,
		     VTY_NEWLINE);
	}
      else
	{
	  if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
	    vty_out (vty, " offset-list %s in %d %s%s",
		     offset->direct[RIP_OFFSET_LIST_IN].alist_name,
		     offset->direct[RIP_OFFSET_LIST_IN].metric,
		     offset->ifname, VTY_NEWLINE);
	  if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
	    vty_out (vty, " offset-list %s out %d %s%s",
		     offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
		     offset->direct[RIP_OFFSET_LIST_OUT].metric,
		     offset->ifname, VTY_NEWLINE);
	}
    }

  return 0;
}