rip_offset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. return XCALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
  58. }
  59. static void
  60. rip_offset_list_free (struct rip_offset_list *offset)
  61. {
  62. XFREE (MTYPE_RIP_OFFSET_LIST, offset);
  63. }
  64. static struct rip_offset_list *
  65. rip_offset_list_lookup (const char *ifname)
  66. {
  67. struct rip_offset_list *offset;
  68. struct listnode *node, *nnode;
  69. for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
  70. {
  71. if (strcmp_safe (offset->ifname, ifname) == 0)
  72. return offset;
  73. }
  74. return NULL;
  75. }
  76. static struct rip_offset_list *
  77. rip_offset_list_get (const char *ifname)
  78. {
  79. struct rip_offset_list *offset;
  80. offset = rip_offset_list_lookup (ifname);
  81. if (offset)
  82. return offset;
  83. offset = rip_offset_list_new ();
  84. if (ifname)
  85. offset->ifname = strdup (ifname);
  86. listnode_add_sort (rip_offset_list_master, offset);
  87. return offset;
  88. }
  89. static int
  90. rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str,
  91. const char *metric_str, const char *ifname)
  92. {
  93. int direct;
  94. int metric;
  95. struct rip_offset_list *offset;
  96. /* Check direction. */
  97. if (strncmp (direct_str, "i", 1) == 0)
  98. direct = RIP_OFFSET_LIST_IN;
  99. else if (strncmp (direct_str, "o", 1) == 0)
  100. direct = RIP_OFFSET_LIST_OUT;
  101. else
  102. {
  103. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  104. return CMD_WARNING;
  105. }
  106. /* Check metric. */
  107. metric = atoi (metric_str);
  108. if (metric < 0 || metric > 16)
  109. {
  110. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  111. return CMD_WARNING;
  112. }
  113. /* Get offset-list structure with interface name. */
  114. offset = rip_offset_list_get (ifname);
  115. if (offset->direct[direct].alist_name)
  116. free (offset->direct[direct].alist_name);
  117. offset->direct[direct].alist_name = strdup (alist);
  118. offset->direct[direct].metric = metric;
  119. return CMD_SUCCESS;
  120. }
  121. static int
  122. rip_offset_list_unset (struct vty *vty, const char *alist,
  123. const char *direct_str, const char *metric_str,
  124. const char *ifname)
  125. {
  126. int direct;
  127. int metric;
  128. struct rip_offset_list *offset;
  129. /* Check direction. */
  130. if (strncmp (direct_str, "i", 1) == 0)
  131. direct = RIP_OFFSET_LIST_IN;
  132. else if (strncmp (direct_str, "o", 1) == 0)
  133. direct = RIP_OFFSET_LIST_OUT;
  134. else
  135. {
  136. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  137. return CMD_WARNING;
  138. }
  139. /* Check metric. */
  140. metric = atoi (metric_str);
  141. if (metric < 0 || metric > 16)
  142. {
  143. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  144. return CMD_WARNING;
  145. }
  146. /* Get offset-list structure with interface name. */
  147. offset = rip_offset_list_lookup (ifname);
  148. if (offset)
  149. {
  150. if (offset->direct[direct].alist_name)
  151. free (offset->direct[direct].alist_name);
  152. offset->direct[direct].alist_name = NULL;
  153. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name == NULL &&
  154. offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
  155. {
  156. listnode_delete (rip_offset_list_master, offset);
  157. if (offset->ifname)
  158. free (offset->ifname);
  159. rip_offset_list_free (offset);
  160. }
  161. }
  162. else
  163. {
  164. vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
  165. return CMD_WARNING;
  166. }
  167. return CMD_SUCCESS;
  168. }
  169. #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIP_OFFSET_LIST_IN].alist_name)
  170. #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
  171. #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
  172. #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
  173. /* If metric is modifed return 1. */
  174. int
  175. rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
  176. u_int32_t *metric)
  177. {
  178. struct rip_offset_list *offset;
  179. struct access_list *alist;
  180. /* Look up offset-list with interface name. */
  181. offset = rip_offset_list_lookup (ifp->name);
  182. if (offset && OFFSET_LIST_IN_NAME (offset))
  183. {
  184. alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
  185. if (alist
  186. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  187. {
  188. *metric += OFFSET_LIST_IN_METRIC (offset);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. /* Look up offset-list without interface name. */
  194. offset = rip_offset_list_lookup (NULL);
  195. if (offset && OFFSET_LIST_IN_NAME (offset))
  196. {
  197. alist = access_list_lookup (AFI_IP, OFFSET_LIST_IN_NAME (offset));
  198. if (alist
  199. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  200. {
  201. *metric += OFFSET_LIST_IN_METRIC (offset);
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. return 0;
  207. }
  208. /* If metric is modifed return 1. */
  209. int
  210. rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
  211. u_int32_t *metric)
  212. {
  213. struct rip_offset_list *offset;
  214. struct access_list *alist;
  215. /* Look up offset-list with interface name. */
  216. offset = rip_offset_list_lookup (ifp->name);
  217. if (offset && OFFSET_LIST_OUT_NAME (offset))
  218. {
  219. alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
  220. if (alist
  221. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  222. {
  223. *metric += OFFSET_LIST_OUT_METRIC (offset);
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. /* Look up offset-list without interface name. */
  229. offset = rip_offset_list_lookup (NULL);
  230. if (offset && OFFSET_LIST_OUT_NAME (offset))
  231. {
  232. alist = access_list_lookup (AFI_IP, OFFSET_LIST_OUT_NAME (offset));
  233. if (alist
  234. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  235. {
  236. *metric += OFFSET_LIST_OUT_METRIC (offset);
  237. return 1;
  238. }
  239. return 0;
  240. }
  241. return 0;
  242. }
  243. DEFUN (rip_offset_list,
  244. rip_offset_list_cmd,
  245. "offset-list WORD (in|out) <0-16>",
  246. "Modify RIP metric\n"
  247. "Access-list name\n"
  248. "For incoming updates\n"
  249. "For outgoing updates\n"
  250. "Metric value\n")
  251. {
  252. return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
  253. }
  254. DEFUN (rip_offset_list_ifname,
  255. rip_offset_list_ifname_cmd,
  256. "offset-list WORD (in|out) <0-16> IFNAME",
  257. "Modify RIP metric\n"
  258. "Access-list name\n"
  259. "For incoming updates\n"
  260. "For outgoing updates\n"
  261. "Metric value\n"
  262. "Interface to match\n")
  263. {
  264. return rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
  265. }
  266. DEFUN (no_rip_offset_list,
  267. no_rip_offset_list_cmd,
  268. "no offset-list WORD (in|out) <0-16>",
  269. NO_STR
  270. "Modify RIP metric\n"
  271. "Access-list name\n"
  272. "For incoming updates\n"
  273. "For outgoing updates\n"
  274. "Metric value\n")
  275. {
  276. return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
  277. }
  278. DEFUN (no_rip_offset_list_ifname,
  279. no_rip_offset_list_ifname_cmd,
  280. "no offset-list WORD (in|out) <0-16> IFNAME",
  281. NO_STR
  282. "Modify RIP metric\n"
  283. "Access-list name\n"
  284. "For incoming updates\n"
  285. "For outgoing updates\n"
  286. "Metric value\n"
  287. "Interface to match\n")
  288. {
  289. return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
  290. }
  291. static int
  292. offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
  293. {
  294. return strcmp_safe (o1->ifname, o2->ifname);
  295. }
  296. static void
  297. offset_list_del (struct rip_offset_list *offset)
  298. {
  299. if (OFFSET_LIST_IN_NAME (offset))
  300. free (OFFSET_LIST_IN_NAME (offset));
  301. if (OFFSET_LIST_OUT_NAME (offset))
  302. free (OFFSET_LIST_OUT_NAME (offset));
  303. if (offset->ifname)
  304. free (offset->ifname);
  305. rip_offset_list_free (offset);
  306. }
  307. void
  308. rip_offset_init ()
  309. {
  310. rip_offset_list_master = list_new ();
  311. rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  312. rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
  313. install_element (RIP_NODE, &rip_offset_list_cmd);
  314. install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
  315. install_element (RIP_NODE, &no_rip_offset_list_cmd);
  316. install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
  317. }
  318. void
  319. rip_offset_clean ()
  320. {
  321. list_delete (rip_offset_list_master);
  322. rip_offset_list_master = list_new ();
  323. rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  324. rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
  325. }
  326. int
  327. config_write_rip_offset_list (struct vty *vty)
  328. {
  329. struct listnode *node, *nnode;
  330. struct rip_offset_list *offset;
  331. for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
  332. {
  333. if (! offset->ifname)
  334. {
  335. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
  336. vty_out (vty, " offset-list %s in %d%s",
  337. offset->direct[RIP_OFFSET_LIST_IN].alist_name,
  338. offset->direct[RIP_OFFSET_LIST_IN].metric,
  339. VTY_NEWLINE);
  340. if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
  341. vty_out (vty, " offset-list %s out %d%s",
  342. offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
  343. offset->direct[RIP_OFFSET_LIST_OUT].metric,
  344. VTY_NEWLINE);
  345. }
  346. else
  347. {
  348. if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
  349. vty_out (vty, " offset-list %s in %d %s%s",
  350. offset->direct[RIP_OFFSET_LIST_IN].alist_name,
  351. offset->direct[RIP_OFFSET_LIST_IN].metric,
  352. offset->ifname, VTY_NEWLINE);
  353. if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
  354. vty_out (vty, " offset-list %s out %d %s%s",
  355. offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
  356. offset->direct[RIP_OFFSET_LIST_OUT].metric,
  357. offset->ifname, VTY_NEWLINE);
  358. }
  359. }
  360. return 0;
  361. }