hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Router ID for zebra daemon. |
| 3 | * |
| 4 | * Copyright (C) 2004 James R. Leu |
| 5 | * |
| 6 | * This file is part of Quagga routing suite. |
| 7 | * |
| 8 | * Quagga is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2, or (at your option) any |
| 11 | * later version. |
| 12 | * |
| 13 | * Quagga is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 20 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 21 | * 02111-1307, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <zebra.h> |
| 25 | |
| 26 | #include "if.h" |
| 27 | #include "vty.h" |
| 28 | #include "sockunion.h" |
| 29 | #include "prefix.h" |
| 30 | #include "stream.h" |
| 31 | #include "command.h" |
| 32 | #include "memory.h" |
| 33 | #include "ioctl.h" |
| 34 | #include "connected.h" |
| 35 | #include "network.h" |
| 36 | #include "log.h" |
| 37 | #include "table.h" |
| 38 | #include "rib.h" |
| 39 | |
| 40 | #include "zebra/zserv.h" |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 41 | #include "zebra/router-id.h" |
Paul Jakma | 6dc686a | 2007-04-10 19:24:45 +0000 | [diff] [blame] | 42 | #include "zebra/redistribute.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 43 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 44 | /* 2nd pointer type used primarily to quell a warning on |
| 45 | * ALL_LIST_ELEMENTS_RO |
| 46 | */ |
| 47 | static struct list _rid_all_sorted_list; |
| 48 | static struct list _rid_lo_sorted_list; |
| 49 | static struct list *rid_all_sorted_list = &_rid_all_sorted_list; |
| 50 | static struct list *rid_lo_sorted_list = &_rid_lo_sorted_list; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 51 | static struct prefix rid_user_assigned; |
| 52 | |
| 53 | /* master zebra server structure */ |
| 54 | extern struct zebra_t zebrad; |
| 55 | |
| 56 | static struct connected * |
| 57 | router_id_find_node (struct list *l, struct connected *ifc) |
| 58 | { |
| 59 | struct listnode *node; |
| 60 | struct connected *c; |
| 61 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 62 | for (ALL_LIST_ELEMENTS_RO (l, node, c)) |
| 63 | if (prefix_same (ifc->address, c->address)) |
| 64 | return c; |
| 65 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static int |
| 70 | router_id_bad_address (struct connected *ifc) |
| 71 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 72 | if (ifc->address->family != AF_INET) |
| 73 | return 1; |
Paul Jakma | 6dc686a | 2007-04-10 19:24:45 +0000 | [diff] [blame] | 74 | |
| 75 | /* non-redistributable addresses shouldn't be used for RIDs either */ |
| 76 | if (!zebra_check_addr (ifc->address)) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 77 | return 1; |
Paul Jakma | 6dc686a | 2007-04-10 19:24:45 +0000 | [diff] [blame] | 78 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | router_id_get (struct prefix *p) |
| 84 | { |
| 85 | struct listnode *node; |
| 86 | struct connected *c; |
| 87 | |
| 88 | p->u.prefix4.s_addr = 0; |
| 89 | p->family = AF_INET; |
| 90 | p->prefixlen = 32; |
| 91 | |
| 92 | if (rid_user_assigned.u.prefix4.s_addr) |
| 93 | p->u.prefix4.s_addr = rid_user_assigned.u.prefix4.s_addr; |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 94 | else if (!list_isempty (rid_lo_sorted_list)) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 95 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 96 | node = listtail (rid_lo_sorted_list); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 97 | c = listgetdata (node); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 98 | p->u.prefix4.s_addr = c->address->u.prefix4.s_addr; |
| 99 | } |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 100 | else if (!list_isempty (rid_all_sorted_list)) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 101 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 102 | node = listtail (rid_all_sorted_list); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 103 | c = listgetdata (node); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 104 | p->u.prefix4.s_addr = c->address->u.prefix4.s_addr; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | router_id_set (struct prefix *p) |
| 110 | { |
| 111 | struct prefix p2; |
| 112 | struct listnode *node; |
| 113 | struct zserv *client; |
| 114 | |
| 115 | rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr; |
| 116 | |
| 117 | router_id_get (&p2); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 118 | |
| 119 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 120 | zsend_router_id_update (client, &p2); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
| 124 | router_id_add_address (struct connected *ifc) |
| 125 | { |
| 126 | struct list *l = NULL; |
| 127 | struct listnode *node; |
| 128 | struct prefix before; |
| 129 | struct prefix after; |
| 130 | struct zserv *client; |
| 131 | |
| 132 | if (router_id_bad_address (ifc)) |
| 133 | return; |
| 134 | |
| 135 | router_id_get (&before); |
| 136 | |
| 137 | if (!strncmp (ifc->ifp->name, "lo", 2) |
| 138 | || !strncmp (ifc->ifp->name, "dummy", 5)) |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 139 | l = rid_lo_sorted_list; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 140 | else |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 141 | l = rid_all_sorted_list; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 142 | |
| 143 | if (!router_id_find_node (l, ifc)) |
Stephen Hemminger | b651682 | 2009-12-21 18:50:43 +0300 | [diff] [blame] | 144 | listnode_add_sort (l, ifc); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 145 | |
| 146 | router_id_get (&after); |
| 147 | |
| 148 | if (prefix_same (&before, &after)) |
| 149 | return; |
| 150 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 151 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 152 | zsend_router_id_update (client, &after); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | router_id_del_address (struct connected *ifc) |
| 157 | { |
| 158 | struct connected *c; |
| 159 | struct list *l; |
| 160 | struct prefix after; |
| 161 | struct prefix before; |
| 162 | struct listnode *node; |
| 163 | struct zserv *client; |
| 164 | |
| 165 | if (router_id_bad_address (ifc)) |
| 166 | return; |
| 167 | |
| 168 | router_id_get (&before); |
| 169 | |
| 170 | if (!strncmp (ifc->ifp->name, "lo", 2) |
| 171 | || !strncmp (ifc->ifp->name, "dummy", 5)) |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 172 | l = rid_lo_sorted_list; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 173 | else |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 174 | l = rid_all_sorted_list; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 175 | |
| 176 | if ((c = router_id_find_node (l, ifc))) |
| 177 | listnode_delete (l, c); |
| 178 | |
| 179 | router_id_get (&after); |
| 180 | |
| 181 | if (prefix_same (&before, &after)) |
| 182 | return; |
| 183 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 184 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 185 | zsend_router_id_update (client, &after); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
| 189 | router_id_write (struct vty *vty) |
| 190 | { |
| 191 | if (rid_user_assigned.u.prefix4.s_addr) |
| 192 | vty_out (vty, "router-id %s%s", inet_ntoa (rid_user_assigned.u.prefix4), |
| 193 | VTY_NEWLINE); |
| 194 | } |
| 195 | |
| 196 | DEFUN (router_id, |
| 197 | router_id_cmd, |
| 198 | "router-id A.B.C.D", |
| 199 | "Manually set the router-id\n" |
| 200 | "IP address to use for router-id\n") |
| 201 | { |
| 202 | struct prefix rid; |
| 203 | |
| 204 | rid.u.prefix4.s_addr = inet_addr (argv[0]); |
| 205 | if (!rid.u.prefix4.s_addr) |
| 206 | return CMD_WARNING; |
| 207 | |
| 208 | rid.prefixlen = 32; |
| 209 | rid.family = AF_INET; |
| 210 | |
| 211 | router_id_set (&rid); |
| 212 | |
| 213 | return CMD_SUCCESS; |
| 214 | } |
| 215 | |
| 216 | DEFUN (no_router_id, |
| 217 | no_router_id_cmd, |
| 218 | "no router-id", |
| 219 | NO_STR |
| 220 | "Remove the manually configured router-id\n") |
| 221 | { |
| 222 | struct prefix rid; |
| 223 | |
| 224 | rid.u.prefix4.s_addr = 0; |
| 225 | rid.prefixlen = 0; |
| 226 | rid.family = AF_INET; |
| 227 | |
| 228 | router_id_set (&rid); |
| 229 | |
| 230 | return CMD_SUCCESS; |
| 231 | } |
| 232 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 233 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 234 | router_id_cmp (void *a, void *b) |
| 235 | { |
Stephen Hemminger | b651682 | 2009-12-21 18:50:43 +0300 | [diff] [blame] | 236 | const struct connected *ifa = (const struct connected *)a; |
| 237 | const struct connected *ifb = (const struct connected *)b; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 238 | |
Ken Williams | 24c84db | 2014-04-15 02:23:11 +0000 | [diff] [blame] | 239 | return IPV4_ADDR_CMP(&ifa->address->u.prefix4.s_addr,&ifb->address->u.prefix4.s_addr); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
| 243 | router_id_init (void) |
| 244 | { |
| 245 | install_element (CONFIG_NODE, &router_id_cmd); |
| 246 | install_element (CONFIG_NODE, &no_router_id_cmd); |
| 247 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 248 | memset (rid_all_sorted_list, 0, sizeof (rid_all_sorted_list)); |
| 249 | memset (rid_lo_sorted_list, 0, sizeof (rid_lo_sorted_list)); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 250 | memset (&rid_user_assigned, 0, sizeof (rid_user_assigned)); |
| 251 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame^] | 252 | rid_all_sorted_list->cmp = router_id_cmp; |
| 253 | rid_lo_sorted_list->cmp = router_id_cmp; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 254 | |
| 255 | rid_user_assigned.family = AF_INET; |
| 256 | rid_user_assigned.prefixlen = 32; |
| 257 | } |