rip_offset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* RIP offset-list
  2. * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "filter.h"
  25. #include "command.h"
  26. #include "linklist.h"
  27. #include "memory.h"
  28. #include "ripd/ripd.h"
  29. #define RIP_OFFSET_LIST_IN 0
  30. #define RIP_OFFSET_LIST_OUT 1
  31. #define RIP_OFFSET_LIST_MAX 2
  32. struct rip_offset_list
  33. {
  34. char *ifname;
  35. struct
  36. {
  37. char *alist_name;
  38. /* struct access_list *alist; */
  39. int metric;
  40. } direct[RIP_OFFSET_LIST_MAX];
  41. };
  42. static struct list *rip_offset_list_master;
  43. static int
  44. strcmp_safe (const char *s1, const char *s2)
  45. {
  46. if (s1 == NULL && s2 == NULL)
  47. return 0;
  48. if (s1 == NULL)
  49. return -1;
  50. if (s2 == NULL)
  51. return 1;
  52. return strcmp (s1, s2);
  53. }
  54. static struct rip_offset_list *
  55. rip_offset_list_new (void)
  56. {
  57. struct rip_offset_list *new;
  58. new = XMALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
  59. memset (new, 0, sizeof (struct rip_offset_list));
  60. return new;
  61. }
  62. static void
  63. rip_offset_list_free (struct rip_offset_list *offset)
  64. {
  65. XFREE (MTYPE_RIP_OFFSET_LIST, offset);
  66. }
  67. static struct rip_offset_list *
  68. rip_offset_list_lookup (const char *ifname)
  69. {
  70. struct rip_offset_list *offset;
  71. struct listnode *node, *nnode;
  72. for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
  73. {
  74. if (strcmp_safe (offset->ifname, ifname) == 0)
  75. return offset;
  76. }
  77. return NULL;
  78. }
  79. static struct rip_offset_list *
  80. rip_offset_list_get (const char *ifname)
  81. {
  82. struct rip_offset_list *offset;
  83. offset = rip_offset_list_lookup (ifname);
  84. if (offset)
  85. return offset;
  86. offset = rip_offset_list_new ();
  87. if (ifname)
  88. offset->ifname = strdup (ifname);
  89. listnode_add_sort (rip_offset_list_master, offset);
  90. return offset;
  91. }
  92. static int
  93. rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str,
  94. const char *metric_str, const char *ifname)
  95. {
  96. int direct;
  97. int metric;
  98. struct rip_offset_list *offset;
  99. /* Check direction. */
  100. if (strncmp (direct_str, "i", 1) == 0)
  101. direct = RIP_OFFSET_LIST_IN;
  102. else if (strncmp (direct_str, "o", 1) == 0)
  103. direct = RIP_OFFSET_LIST_OUT;
  104. else
  105. {
  106. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  107. return CMD_WARNING;
  108. }
  109. /* Check metric. */
  110. metric = atoi (metric_str);
  111. if (metric < 0 || metric > 16)
  112. {
  113. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  114. return CMD_WARNING;
  115. }
  116. /* Get offset-list structure with interface name. */
  117. offset = rip_offset_list_get (ifname);
  118. if (offset->direct[direct].alist_name)
  119. free (offset->direct[direct].alist_name);
  120. offset->direct[direct].alist_name = strdup (alist);
  121. offset->direct[direct].metric = metric;
  122. return CMD_SUCCESS;
  123. }
  124. static int
  125. rip_offset_list_unset (struct vty *vty, const char *alist,
  126. const char *direct_str, const char *metric_str,
  127. const char *ifname)
  128. {
  129. int direct;
  130. int metric;
  131. struct rip_offset_list *offset;
  132. /* Check direction. */
  133. if (strncmp (direct_str, "i", 1) == 0)
  134. direct = RIP_OFFSET_LIST_IN;
  135. else if (strncmp (direct_str, "o", 1) == 0)
  136. direct = RIP_OFFSET_LIST_OUT;
  137. else
  138. {
  139. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  140. return CMD_WARNING;
  141. }
  142. /* Check metric. */
  143. metric = atoi (metric_str);
  144. if (metric < 0 || metric > 16)
  145. {
  146. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  147. return CMD_WARNING;
  148. }
  149. /* Get offset-list structure with interface name. */
  150. offset = rip_offset_list_lookup (ifname);
  151. if (offset)
  152. {
  153. if (offset->direct[direct].alist_name)
  154. free (offset->direct[direct].alist_name);
  155. offset->direct[direct].alist_name = NULL;
  156. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL &&
  157. offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
  158. {
  159. listnode_delete (rip_offset_list_master, offset);
  160. if (offset->ifname)
  161. free (offset->ifname);
  162. rip_offset_list_free (offset);
  163. }
  164. }
  165. else
  166. {
  167. vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
  168. return CMD_WARNING;
  169. }
  170. return CMD_SUCCESS;
  171. }
  172. #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
  173. #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
  174. #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
  175. #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
  176. /* If metric is modifed return 1. */
  177. int
  178. rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
  179. u_int32_t *metric)
  180. {
  181. struct rip_offset_list *offset;
  182. struct access_list *alist;
  183. /* Look up offset-list with interface name. */
  184. offset = rip_offset_list_lookup (ifp->name);
  185. if (offset && OFFSET_LIST_IN_NAME (offset))
  186. {
  187. alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
  188. if (alist
  189. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  190. {
  191. *metric += OFFSET_LIST_IN_METRIC (offset);
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. /* Look up offset-list without interface name. */
  197. offset = rip_offset_list_lookup (NULL);
  198. if (offset && OFFSET_LIST_IN_NAME (offset))
  199. {
  200. alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
  201. if (alist
  202. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  203. {
  204. *metric += OFFSET_LIST_IN_METRIC (offset);
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. return 0;
  210. }
  211. /* If metric is modifed return 1. */
  212. int
  213. rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
  214. u_int32_t *metric)
  215. {
  216. struct rip_offset_list *offset;
  217. struct access_list *alist;
  218. /* Look up offset-list with interface name. */
  219. offset = rip_offset_list_lookup (ifp->name);
  220. if (offset && OFFSET_LIST_OUT_NAME (offset))
  221. {
  222. alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
  223. if (alist
  224. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  225. {
  226. *metric += OFFSET_LIST_OUT_METRIC (offset);
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. /* Look up offset-list without interface name. */
  232. offset = rip_offset_list_lookup (NULL);
  233. if (offset && OFFSET_LIST_OUT_NAME (offset))
  234. {
  235. alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
  236. if (alist
  237. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  238. {
  239. *metric += OFFSET_LIST_OUT_METRIC (offset);
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. return 0;
  245. }
  246. DEFUN (rip_offset_list,
  247. rip_offset_list_cmd,
  248. "offset-list WORD (in|out) <0-16>",
  249. "Modify RIP metric\n"
  250. "Access-list name\n"
  251. "For incoming updates\n"
  252. "For outgoing updates\n"
  253. "Metric value\n")
  254. {
  255. return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
  256. }
  257. DEFUN (rip_offset_list_ifname,
  258. rip_offset_list_ifname_cmd,
  259. "offset-list WORD (in|out) <0-16> IFNAME",
  260. "Modify RIP metric\n"
  261. "Access-list name\n"
  262. "For incoming updates\n"
  263. "For outgoing updates\n"
  264. "Metric value\n"
  265. "Interface to match\n")
  266. {
  267. return rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
  268. }
  269. DEFUN (no_rip_offset_list,
  270. no_rip_offset_list_cmd,
  271. "no offset-list WORD (in|out) <0-16>",
  272. NO_STR
  273. "Modify RIP metric\n"
  274. "Access-list name\n"
  275. "For incoming updates\n"
  276. "For outgoing updates\n"
  277. "Metric value\n")
  278. {
  279. return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
  280. }
  281. DEFUN (no_rip_offset_list_ifname,
  282. no_rip_offset_list_ifname_cmd,
  283. "no offset-list WORD (in|out) <0-16> IFNAME",
  284. NO_STR
  285. "Modify RIP metric\n"
  286. "Access-list name\n"
  287. "For incoming updates\n"
  288. "For outgoing updates\n"
  289. "Metric value\n"
  290. "Interface to match\n")
  291. {
  292. return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
  293. }
  294. static int
  295. offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
  296. {
  297. return strcmp_safe (o1->ifname, o2->ifname);
  298. }
  299. static void
  300. offset_list_del (struct rip_offset_list *offset)
  301. {
  302. if (OFFSET_LIST_IN_NAME (offset))
  303. free (OFFSET_LIST_IN_NAME (offset));
  304. if (OFFSET_LIST_OUT_NAME (offset))
  305. free (OFFSET_LIST_OUT_NAME (offset));
  306. if (offset->ifname)
  307. free (offset->ifname);
  308. rip_offset_list_free (offset);
  309. }
  310. void
  311. rip_offset_init ()
  312. {
  313. rip_offset_list_master = list_new ();
  314. rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  315. rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
  316. install_element (RIP_NODE, &rip_offset_list_cmd);
  317. install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
  318. install_element (RIP_NODE, &no_rip_offset_list_cmd);
  319. install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
  320. }
  321. void
  322. rip_offset_clean ()
  323. {
  324. list_delete (rip_offset_list_master);
  325. rip_offset_list_master = list_new ();
  326. rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  327. rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
  328. }
  329. int
  330. config_write_rip_offset_list (struct vty *vty)
  331. {
  332. struct listnode *node, *nnode;
  333. struct rip_offset_list *offset;
  334. for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
  335. {
  336. if (! offset->ifname)
  337. {
  338. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
  339. vty_out (vty, " offset-list %s in %d%s",
  340. offset->direct[RIP_OFFSET_LIST_IN].alist_name,
  341. offset->direct[RIP_OFFSET_LIST_IN].metric,
  342. VTY_NEWLINE);
  343. if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
  344. vty_out (vty, " offset-list %s out %d%s",
  345. offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
  346. offset->direct[RIP_OFFSET_LIST_OUT].metric,
  347. VTY_NEWLINE);
  348. }
  349. else
  350. {
  351. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
  352. vty_out (vty, " offset-list %s in %d %s%s",
  353. offset->direct[RIP_OFFSET_LIST_IN].alist_name,
  354. offset->direct[RIP_OFFSET_LIST_IN].metric,
  355. offset->ifname, VTY_NEWLINE);
  356. if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
  357. vty_out (vty, " offset-list %s out %d %s%s",
  358. offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
  359. offset->direct[RIP_OFFSET_LIST_OUT].metric,
  360. offset->ifname, VTY_NEWLINE);
  361. }
  362. }
  363. return 0;
  364. }