ripng_offset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* RIPng 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. /* RIPng support by Vincent Jardin <vincent.jardin@6wind.com>
  22. * Copyright (C) 2002 6WIND
  23. */
  24. #include <zebra.h>
  25. #include "if.h"
  26. #include "prefix.h"
  27. #include "filter.h"
  28. #include "command.h"
  29. #include "linklist.h"
  30. #include "memory.h"
  31. #include "ripngd/ripngd.h"
  32. #define RIPNG_OFFSET_LIST_IN 0
  33. #define RIPNG_OFFSET_LIST_OUT 1
  34. #define RIPNG_OFFSET_LIST_MAX 2
  35. struct ripng_offset_list
  36. {
  37. char *ifname;
  38. struct
  39. {
  40. char *alist_name;
  41. /* struct access_list *alist; */
  42. int metric;
  43. } direct[RIPNG_OFFSET_LIST_MAX];
  44. };
  45. static struct list *ripng_offset_list_master;
  46. static int
  47. strcmp_safe (const char *s1, const char *s2)
  48. {
  49. if (s1 == NULL && s2 == NULL)
  50. return 0;
  51. if (s1 == NULL)
  52. return -1;
  53. if (s2 == NULL)
  54. return 1;
  55. return strcmp (s1, s2);
  56. }
  57. static struct ripng_offset_list *
  58. ripng_offset_list_new ()
  59. {
  60. struct ripng_offset_list *new;
  61. new = XCALLOC (MTYPE_RIPNG_OFFSET_LIST, sizeof (struct ripng_offset_list));
  62. return new;
  63. }
  64. static void
  65. ripng_offset_list_free (struct ripng_offset_list *offset)
  66. {
  67. XFREE (MTYPE_RIPNG_OFFSET_LIST, offset);
  68. }
  69. static struct ripng_offset_list *
  70. ripng_offset_list_lookup (const char *ifname)
  71. {
  72. struct ripng_offset_list *offset;
  73. struct listnode *node, *nnode;
  74. for (ALL_LIST_ELEMENTS (ripng_offset_list_master, node, nnode, offset))
  75. {
  76. if (strcmp_safe (offset->ifname, ifname) == 0)
  77. return offset;
  78. }
  79. return NULL;
  80. }
  81. static struct ripng_offset_list *
  82. ripng_offset_list_get (const char *ifname)
  83. {
  84. struct ripng_offset_list *offset;
  85. offset = ripng_offset_list_lookup (ifname);
  86. if (offset)
  87. return offset;
  88. offset = ripng_offset_list_new ();
  89. if (ifname)
  90. offset->ifname = strdup (ifname);
  91. listnode_add_sort (ripng_offset_list_master, offset);
  92. return offset;
  93. }
  94. static int
  95. ripng_offset_list_set (struct vty *vty, const char *alist,
  96. const char *direct_str, const char *metric_str,
  97. const char *ifname)
  98. {
  99. int direct;
  100. int metric;
  101. struct ripng_offset_list *offset;
  102. /* Check direction. */
  103. if (strncmp (direct_str, "i", 1) == 0)
  104. direct = RIPNG_OFFSET_LIST_IN;
  105. else if (strncmp (direct_str, "o", 1) == 0)
  106. direct = RIPNG_OFFSET_LIST_OUT;
  107. else
  108. {
  109. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  110. return CMD_WARNING;
  111. }
  112. /* Check metric. */
  113. metric = atoi (metric_str);
  114. if (metric < 0 || metric > 16)
  115. {
  116. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  117. return CMD_WARNING;
  118. }
  119. /* Get offset-list structure with interface name. */
  120. offset = ripng_offset_list_get (ifname);
  121. if (offset->direct[direct].alist_name)
  122. free (offset->direct[direct].alist_name);
  123. offset->direct[direct].alist_name = strdup (alist);
  124. offset->direct[direct].metric = metric;
  125. return CMD_SUCCESS;
  126. }
  127. static int
  128. ripng_offset_list_unset (struct vty *vty, const char *alist,
  129. const char *direct_str, const char *metric_str,
  130. const char *ifname)
  131. {
  132. int direct;
  133. int metric;
  134. struct ripng_offset_list *offset;
  135. /* Check direction. */
  136. if (strncmp (direct_str, "i", 1) == 0)
  137. direct = RIPNG_OFFSET_LIST_IN;
  138. else if (strncmp (direct_str, "o", 1) == 0)
  139. direct = RIPNG_OFFSET_LIST_OUT;
  140. else
  141. {
  142. vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
  143. return CMD_WARNING;
  144. }
  145. /* Check metric. */
  146. metric = atoi (metric_str);
  147. if (metric < 0 || metric > 16)
  148. {
  149. vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
  150. return CMD_WARNING;
  151. }
  152. /* Get offset-list structure with interface name. */
  153. offset = ripng_offset_list_lookup (ifname);
  154. if (offset)
  155. {
  156. if (offset->direct[direct].alist_name)
  157. free (offset->direct[direct].alist_name);
  158. offset->direct[direct].alist_name = NULL;
  159. if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name == NULL &&
  160. offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name == NULL)
  161. {
  162. listnode_delete (ripng_offset_list_master, offset);
  163. if (offset->ifname)
  164. free (offset->ifname);
  165. ripng_offset_list_free (offset);
  166. }
  167. }
  168. else
  169. {
  170. vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
  171. return CMD_WARNING;
  172. }
  173. return CMD_SUCCESS;
  174. }
  175. #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].alist_name)
  176. #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].metric)
  177. #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
  178. #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].metric)
  179. /* If metric is modifed return 1. */
  180. int
  181. ripng_offset_list_apply_in (struct prefix_ipv6 *p, struct interface *ifp,
  182. u_char *metric)
  183. {
  184. struct ripng_offset_list *offset;
  185. struct access_list *alist;
  186. /* Look up offset-list with interface name. */
  187. offset = ripng_offset_list_lookup (ifp->name);
  188. if (offset && OFFSET_LIST_IN_NAME (offset))
  189. {
  190. alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
  191. if (alist
  192. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  193. {
  194. *metric += OFFSET_LIST_IN_METRIC (offset);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. /* Look up offset-list without interface name. */
  200. offset = ripng_offset_list_lookup (NULL);
  201. if (offset && OFFSET_LIST_IN_NAME (offset))
  202. {
  203. alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
  204. if (alist
  205. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  206. {
  207. *metric += OFFSET_LIST_IN_METRIC (offset);
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. return 0;
  213. }
  214. /* If metric is modifed return 1. */
  215. int
  216. ripng_offset_list_apply_out (struct prefix_ipv6 *p, struct interface *ifp,
  217. u_char *metric)
  218. {
  219. struct ripng_offset_list *offset;
  220. struct access_list *alist;
  221. /* Look up offset-list with interface name. */
  222. offset = ripng_offset_list_lookup (ifp->name);
  223. if (offset && OFFSET_LIST_OUT_NAME (offset))
  224. {
  225. alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
  226. if (alist
  227. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  228. {
  229. *metric += OFFSET_LIST_OUT_METRIC (offset);
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. /* Look up offset-list without interface name. */
  235. offset = ripng_offset_list_lookup (NULL);
  236. if (offset && OFFSET_LIST_OUT_NAME (offset))
  237. {
  238. alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
  239. if (alist
  240. && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
  241. {
  242. *metric += OFFSET_LIST_OUT_METRIC (offset);
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. return 0;
  248. }
  249. DEFUN (ripng_offset_list,
  250. ripng_offset_list_cmd,
  251. "offset-list WORD (in|out) <0-16>",
  252. "Modify RIPng metric\n"
  253. "Access-list name\n"
  254. "For incoming updates\n"
  255. "For outgoing updates\n"
  256. "Metric value\n")
  257. {
  258. return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
  259. }
  260. DEFUN (ripng_offset_list_ifname,
  261. ripng_offset_list_ifname_cmd,
  262. "offset-list WORD (in|out) <0-16> IFNAME",
  263. "Modify RIPng metric\n"
  264. "Access-list name\n"
  265. "For incoming updates\n"
  266. "For outgoing updates\n"
  267. "Metric value\n"
  268. "Interface to match\n")
  269. {
  270. return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
  271. }
  272. DEFUN (no_ripng_offset_list,
  273. no_ripng_offset_list_cmd,
  274. "no offset-list WORD (in|out) <0-16>",
  275. NO_STR
  276. "Modify RIPng metric\n"
  277. "Access-list name\n"
  278. "For incoming updates\n"
  279. "For outgoing updates\n"
  280. "Metric value\n")
  281. {
  282. return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
  283. }
  284. DEFUN (no_ripng_offset_list_ifname,
  285. no_ripng_offset_list_ifname_cmd,
  286. "no offset-list WORD (in|out) <0-16> IFNAME",
  287. NO_STR
  288. "Modify RIPng metric\n"
  289. "Access-list name\n"
  290. "For incoming updates\n"
  291. "For outgoing updates\n"
  292. "Metric value\n"
  293. "Interface to match\n")
  294. {
  295. return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
  296. }
  297. static int
  298. offset_list_cmp (struct ripng_offset_list *o1, struct ripng_offset_list *o2)
  299. {
  300. return strcmp_safe (o1->ifname, o2->ifname);
  301. }
  302. static void
  303. offset_list_del (struct ripng_offset_list *offset)
  304. {
  305. if (OFFSET_LIST_IN_NAME (offset))
  306. free (OFFSET_LIST_IN_NAME (offset));
  307. if (OFFSET_LIST_OUT_NAME (offset))
  308. free (OFFSET_LIST_OUT_NAME (offset));
  309. if (offset->ifname)
  310. free (offset->ifname);
  311. ripng_offset_list_free (offset);
  312. }
  313. void
  314. ripng_offset_init (void)
  315. {
  316. ripng_offset_list_master = list_new ();
  317. ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  318. ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
  319. install_element (RIPNG_NODE, &ripng_offset_list_cmd);
  320. install_element (RIPNG_NODE, &ripng_offset_list_ifname_cmd);
  321. install_element (RIPNG_NODE, &no_ripng_offset_list_cmd);
  322. install_element (RIPNG_NODE, &no_ripng_offset_list_ifname_cmd);
  323. }
  324. void
  325. ripng_offset_clean (void)
  326. {
  327. list_delete (ripng_offset_list_master);
  328. ripng_offset_list_master = list_new ();
  329. ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
  330. ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
  331. }
  332. int
  333. config_write_ripng_offset_list (struct vty *vty)
  334. {
  335. struct listnode *node, *nnode;
  336. struct ripng_offset_list *offset;
  337. for (ALL_LIST_ELEMENTS (ripng_offset_list_master, node, nnode, offset))
  338. {
  339. if (! offset->ifname)
  340. {
  341. if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
  342. vty_out (vty, " offset-list %s in %d%s",
  343. offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
  344. offset->direct[RIPNG_OFFSET_LIST_IN].metric,
  345. VTY_NEWLINE);
  346. if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
  347. vty_out (vty, " offset-list %s out %d%s",
  348. offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
  349. offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
  350. VTY_NEWLINE);
  351. }
  352. else
  353. {
  354. if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
  355. vty_out (vty, " offset-list %s in %d %s%s",
  356. offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
  357. offset->direct[RIPNG_OFFSET_LIST_IN].metric,
  358. offset->ifname, VTY_NEWLINE);
  359. if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
  360. vty_out (vty, " offset-list %s out %d %s%s",
  361. offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
  362. offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
  363. offset->ifname, VTY_NEWLINE);
  364. }
  365. }
  366. return 0;
  367. }