| 1 | /* |
|---|
| 2 | * routing.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <bcmnvram.h> |
|---|
| 25 | #include <shutils.h> |
|---|
| 26 | #include <utils.h> |
|---|
| 27 | #include <syslog.h> |
|---|
| 28 | #include <signal.h> |
|---|
| 29 | #include <errno.h> |
|---|
| 30 | #include <wlutils.h> |
|---|
| 31 | #include <services.h> |
|---|
| 32 | |
|---|
| 33 | #ifdef HAVE_QUAGGA |
|---|
| 34 | |
|---|
| 35 | int zebra_ospf_init(void); |
|---|
| 36 | int zebra_ospf6_init(void); |
|---|
| 37 | int zebra_bgp_init(void); |
|---|
| 38 | int zebra_ripd_init(void); |
|---|
| 39 | |
|---|
| 40 | int zebra_init(void) |
|---|
| 41 | { |
|---|
| 42 | char *sub; |
|---|
| 43 | char var[32], *next; |
|---|
| 44 | |
|---|
| 45 | sub = nvram_safe_get("wk_mode"); |
|---|
| 46 | foreach(var, sub, next) { |
|---|
| 47 | if (!strcmp(var, "gateway")) { |
|---|
| 48 | printf("zebra disabled.\n"); |
|---|
| 49 | return 0; |
|---|
| 50 | } else if (!strcmp(var, "ospf")) { |
|---|
| 51 | zebra_ospf_init(); |
|---|
| 52 | dd_syslog(LOG_INFO, |
|---|
| 53 | "zebra : zebra (ospf) successfully initiated\n"); |
|---|
| 54 | } else if (!strcmp(var, "ospf6")) { |
|---|
| 55 | zebra_ospf6_init(); |
|---|
| 56 | dd_syslog(LOG_INFO, |
|---|
| 57 | "zebra : zebra (ospf6) successfully initiated\n"); |
|---|
| 58 | } else if (!strcmp(var, "bgp")) { |
|---|
| 59 | zebra_bgp_init(); |
|---|
| 60 | dd_syslog(LOG_INFO, |
|---|
| 61 | "zebra : zebra (ospf) successfully initiated\n"); |
|---|
| 62 | } else if (!strcmp(var, "router")) { |
|---|
| 63 | zebra_ripd_init(); |
|---|
| 64 | dd_syslog(LOG_INFO, |
|---|
| 65 | "zebra : zebra (router) successfully initiated\n"); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void start_quagga_writememory(void) |
|---|
| 72 | { |
|---|
| 73 | FILE *in = fopen("/tmp/zebra.conf", "rb"); |
|---|
| 74 | |
|---|
| 75 | if (in != NULL) { |
|---|
| 76 | fseek(in, 0, SEEK_END); |
|---|
| 77 | int len = ftell(in); |
|---|
| 78 | |
|---|
| 79 | rewind(in); |
|---|
| 80 | char *buf = malloc(len + 1); |
|---|
| 81 | |
|---|
| 82 | fread(buf, len, 1, in); |
|---|
| 83 | buf[len] = 0; |
|---|
| 84 | fclose(in); |
|---|
| 85 | nvram_set("zebra_copt", "1"); |
|---|
| 86 | nvram_set("zebra_conf", buf); |
|---|
| 87 | free(buf); |
|---|
| 88 | } else { |
|---|
| 89 | nvram_set("zebra_copt", "0"); |
|---|
| 90 | nvram_unset("zebra_conf"); |
|---|
| 91 | } |
|---|
| 92 | in = fopen("/tmp/ospfd.conf", "rb"); |
|---|
| 93 | |
|---|
| 94 | if (in != NULL) { |
|---|
| 95 | fseek(in, 0, SEEK_END); |
|---|
| 96 | int len = ftell(in); |
|---|
| 97 | |
|---|
| 98 | rewind(in); |
|---|
| 99 | char *buf = malloc(len + 1); |
|---|
| 100 | |
|---|
| 101 | fread(buf, len, 1, in); |
|---|
| 102 | buf[len] = 0; |
|---|
| 103 | fclose(in); |
|---|
| 104 | nvram_set("ospfd_copt", "1"); |
|---|
| 105 | nvram_set("ospfd_conf", buf); |
|---|
| 106 | free(buf); |
|---|
| 107 | } else { |
|---|
| 108 | nvram_set("ospfd_copt", "0"); |
|---|
| 109 | nvram_unset("ospfd_conf"); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | in = fopen("/tmp/ospf6d.conf", "rb"); |
|---|
| 113 | |
|---|
| 114 | if (in != NULL) { |
|---|
| 115 | fseek(in, 0, SEEK_END); |
|---|
| 116 | int len = ftell(in); |
|---|
| 117 | |
|---|
| 118 | rewind(in); |
|---|
| 119 | char *buf = malloc(len + 1); |
|---|
| 120 | |
|---|
| 121 | fread(buf, len, 1, in); |
|---|
| 122 | buf[len] = 0; |
|---|
| 123 | fclose(in); |
|---|
| 124 | nvram_set("ospf6d_copt", "1"); |
|---|
| 125 | nvram_set("ospf6d_conf", buf); |
|---|
| 126 | free(buf); |
|---|
| 127 | } else { |
|---|
| 128 | nvram_set("ospf6d_copt", "0"); |
|---|
| 129 | nvram_unset("ospf6d_conf"); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | in = fopen("/tmp/bgpd.conf", "rb"); |
|---|
| 133 | |
|---|
| 134 | if (in != NULL) { |
|---|
| 135 | fseek(in, 0, SEEK_END); |
|---|
| 136 | int len = ftell(in); |
|---|
| 137 | |
|---|
| 138 | rewind(in); |
|---|
| 139 | char *buf = malloc(len + 1); |
|---|
| 140 | |
|---|
| 141 | fread(buf, len, 1, in); |
|---|
| 142 | buf[len] = 0; |
|---|
| 143 | fclose(in); |
|---|
| 144 | nvram_set("bgpd_copt", "1"); |
|---|
| 145 | nvram_set("bgpd_conf", buf); |
|---|
| 146 | free(buf); |
|---|
| 147 | } else { |
|---|
| 148 | nvram_set("bgpd_copt", "0"); |
|---|
| 149 | nvram_unset("bgpd_conf"); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | in = fopen("/tmp/ripd.conf", "rb"); |
|---|
| 153 | |
|---|
| 154 | if (in != NULL) { |
|---|
| 155 | fseek(in, 0, SEEK_END); |
|---|
| 156 | int len = ftell(in); |
|---|
| 157 | |
|---|
| 158 | rewind(in); |
|---|
| 159 | char *buf = malloc(len + 1); |
|---|
| 160 | |
|---|
| 161 | fread(buf, len, 1, in); |
|---|
| 162 | buf[len] = 0; |
|---|
| 163 | fclose(in); |
|---|
| 164 | nvram_set("ripd_copt", "1"); |
|---|
| 165 | nvram_set("ripd_conf", buf); |
|---|
| 166 | free(buf); |
|---|
| 167 | } else { |
|---|
| 168 | nvram_set("ripd_copt", "0"); |
|---|
| 169 | nvram_unset("ripd_conf"); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | nvram_commit(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | int zebra_ospf_init(void) |
|---|
| 176 | { |
|---|
| 177 | char *lf = nvram_safe_get("lan_ifname"); |
|---|
| 178 | char *wf = get_wan_face(); |
|---|
| 179 | |
|---|
| 180 | FILE *fp; |
|---|
| 181 | int ret1, ret2, s = 0, i = 0; |
|---|
| 182 | |
|---|
| 183 | /* |
|---|
| 184 | * Write configuration file based on current information |
|---|
| 185 | */ |
|---|
| 186 | if (!(fp = fopen("/tmp/zebra.conf", "w"))) { |
|---|
| 187 | perror("/tmp/zebra.conf"); |
|---|
| 188 | return errno; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | if (nvram_match("zebra_copt", "1")) { |
|---|
| 192 | if (nvram_match("zebra_log", "1")) { |
|---|
| 193 | fprintf(fp, "log file /var/log/zebra.log\n"); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | if (strlen(nvram_safe_get("zebra_conf")) > 0) { |
|---|
| 198 | fwritenvram("zebra_conf", fp); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | fclose(fp); |
|---|
| 202 | |
|---|
| 203 | if (!(fp = fopen("/tmp/ospfd.conf", "w"))) { |
|---|
| 204 | perror("/tmp/ospfd.conf"); |
|---|
| 205 | return errno; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | if (nvram_match("ospfd_copt", "1") |
|---|
| 209 | && strlen(nvram_safe_get("ospfd_conf"))) { |
|---|
| 210 | fwritenvram("ospfd_conf", fp); |
|---|
| 211 | } else { |
|---|
| 212 | fprintf(fp, "!\n"); |
|---|
| 213 | // fprintf (fp, "password %s\n", nvram_safe_get ("http_passwd")); |
|---|
| 214 | // fprintf (fp, "enable password %s\n", nvram_safe_get |
|---|
| 215 | // ("http_passwd")); |
|---|
| 216 | fprintf(fp, "!\n!\n!\n"); |
|---|
| 217 | |
|---|
| 218 | fprintf(fp, "interface %s\n!\n", lf); |
|---|
| 219 | if (wf && strlen(wf) > 0) |
|---|
| 220 | fprintf(fp, "interface %s\n", wf); |
|---|
| 221 | |
|---|
| 222 | int cnt = get_wl_instances(); |
|---|
| 223 | int c; |
|---|
| 224 | |
|---|
| 225 | for (c = 0; c < cnt; c++) { |
|---|
| 226 | if (nvram_nmatch("1", "wl%d_br1_enable", c)) { |
|---|
| 227 | fprintf(fp, "!\n! 'Subnet' WDS bridge\n"); |
|---|
| 228 | fprintf(fp, "interface br1\n"); |
|---|
| 229 | } |
|---|
| 230 | if (nvram_nmatch("ap", "wl%d_mode", c)) |
|---|
| 231 | for (s = 1; s <= MAX_WDS_DEVS; s++) { |
|---|
| 232 | char wdsdevospf[32] = { 0 }; |
|---|
| 233 | char *dev; |
|---|
| 234 | |
|---|
| 235 | sprintf(wdsdevospf, "wl%d_wds%d_ospf", |
|---|
| 236 | c, s); |
|---|
| 237 | dev = nvram_nget("wl%d_wds%d_if", c, s); |
|---|
| 238 | |
|---|
| 239 | if (nvram_nmatch |
|---|
| 240 | ("1", "wl%d_wds%d_enable", c, s)) { |
|---|
| 241 | fprintf(fp, "!\n! WDS: %s\n", |
|---|
| 242 | nvram_nget |
|---|
| 243 | ("wl%d_wds%d_desc", c, |
|---|
| 244 | s)); |
|---|
| 245 | fprintf(fp, "interface %s\n", |
|---|
| 246 | dev); |
|---|
| 247 | |
|---|
| 248 | if (atoi |
|---|
| 249 | (nvram_safe_get(wdsdevospf)) |
|---|
| 250 | > 0) |
|---|
| 251 | fprintf(fp, |
|---|
| 252 | " ip ospf cost %s\n", |
|---|
| 253 | nvram_safe_get |
|---|
| 254 | (wdsdevospf)); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | fprintf(fp, "!\n"); |
|---|
| 258 | } |
|---|
| 259 | fprintf(fp, "router ospf\n"); |
|---|
| 260 | fprintf(fp, " passive-interface lo\n"); |
|---|
| 261 | fprintf(fp, " ospf router-id %s\n", |
|---|
| 262 | nvram_safe_get("lan_ipaddr")); |
|---|
| 263 | fprintf(fp, " redistribute kernel\n"); |
|---|
| 264 | fprintf(fp, " redistribute connected\n"); |
|---|
| 265 | fprintf(fp, " redistribute static\n"); |
|---|
| 266 | fprintf(fp, " network 0.0.0.0/0 area 0\n"); // handle all routing |
|---|
| 267 | fprintf(fp, " default-information originate\n"); |
|---|
| 268 | |
|---|
| 269 | for (s = 1; s <= MAX_WDS_DEVS; s++) { |
|---|
| 270 | char wdsdevospf[32] = { 0 }; |
|---|
| 271 | sprintf(wdsdevospf, "wl_wds%d_ospf", s); |
|---|
| 272 | |
|---|
| 273 | if (atoi(nvram_safe_get(wdsdevospf)) < 0) |
|---|
| 274 | fprintf(fp, " passive-interface %s\n", |
|---|
| 275 | nvram_nget("wl_wds%d_if", s)); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | if (nvram_match("zebra_log", "1")) { |
|---|
| 279 | fprintf(fp, "!\n"); |
|---|
| 280 | fprintf(fp, "log file /var/log/ospf.log\n"); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | fprintf(fp, "!\nline vty\n!\n"); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | fflush(fp); |
|---|
| 287 | fclose(fp); |
|---|
| 288 | |
|---|
| 289 | if (nvram_match("dyn_default", "1")) |
|---|
| 290 | while (!eval("ip", "route", "del", "default")) ; |
|---|
| 291 | |
|---|
| 292 | ret1 = eval("zebra", "-d", "-r", "-f", "/tmp/zebra.conf"); |
|---|
| 293 | ret2 = eval("ospfd", "-d", "-f", "/tmp/ospfd.conf"); |
|---|
| 294 | |
|---|
| 295 | return ret1 + ret2; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | int zebra_ospf6_init(void) |
|---|
| 299 | { |
|---|
| 300 | char *lf = nvram_safe_get("lan_ifname"); |
|---|
| 301 | char *wf = get_wan_face(); |
|---|
| 302 | |
|---|
| 303 | FILE *fp; |
|---|
| 304 | int ret1, ret2, s = 0, i = 0; |
|---|
| 305 | |
|---|
| 306 | /* |
|---|
| 307 | * Write configuration file based on current information |
|---|
| 308 | */ |
|---|
| 309 | if (!(fp = fopen("/tmp/zebra.conf", "w"))) { |
|---|
| 310 | perror("/tmp/zebra.conf"); |
|---|
| 311 | return errno; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | if (nvram_match("zebra_copt", "1")) { |
|---|
| 315 | if (nvram_match("zebra_log", "1")) { |
|---|
| 316 | fprintf(fp, "log file /var/log/zebra.log\n"); |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | if (strlen(nvram_safe_get("zebra_conf")) > 0) { |
|---|
| 321 | fwritenvram("zebra_conf", fp); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | fclose(fp); |
|---|
| 325 | |
|---|
| 326 | if (!(fp = fopen("/tmp/ospf6d.conf", "w"))) { |
|---|
| 327 | perror("/tmp/ospf6d.conf"); |
|---|
| 328 | return errno; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | if (nvram_match("ospf6d_copt", "1") |
|---|
| 332 | && strlen(nvram_safe_get("ospf6d_conf"))) { |
|---|
| 333 | fwritenvram("ospf6d_conf", fp); |
|---|
| 334 | } else { |
|---|
| 335 | fprintf(fp, "!\n"); |
|---|
| 336 | // fprintf (fp, "password %s\n", nvram_safe_get ("http_passwd")); |
|---|
| 337 | // fprintf (fp, "enable password %s\n", nvram_safe_get |
|---|
| 338 | // ("http_passwd")); |
|---|
| 339 | fprintf(fp, "!\n!\n!\n"); |
|---|
| 340 | |
|---|
| 341 | fprintf(fp, "interface %s\n!\n", lf); |
|---|
| 342 | if (wf && strlen(wf) > 0) |
|---|
| 343 | fprintf(fp, "interface %s\n", wf); |
|---|
| 344 | |
|---|
| 345 | int cnt = get_wl_instances(); |
|---|
| 346 | int c; |
|---|
| 347 | |
|---|
| 348 | for (c = 0; c < cnt; c++) { |
|---|
| 349 | if (nvram_nmatch("1", "wl%d_br1_enable", c)) { |
|---|
| 350 | fprintf(fp, "!\n! 'Subnet' WDS bridge\n"); |
|---|
| 351 | fprintf(fp, "interface br1\n"); |
|---|
| 352 | } |
|---|
| 353 | if (nvram_nmatch("ap", "wl%d_mode", c)) |
|---|
| 354 | for (s = 1; s <= MAX_WDS_DEVS; s++) { |
|---|
| 355 | char wdsdevospf[32] = { 0 }; |
|---|
| 356 | char *dev; |
|---|
| 357 | |
|---|
| 358 | sprintf(wdsdevospf, "wl%d_wds%d_ospf", |
|---|
| 359 | c, s); |
|---|
| 360 | dev = nvram_nget("wl%d_wds%d_if", c, s); |
|---|
| 361 | |
|---|
| 362 | if (nvram_nmatch |
|---|
| 363 | ("1", "wl%d_wds%d_enable", c, s)) { |
|---|
| 364 | fprintf(fp, "!\n! WDS: %s\n", |
|---|
| 365 | nvram_nget |
|---|
| 366 | ("wl%d_wds%d_desc", c, |
|---|
| 367 | s)); |
|---|
| 368 | fprintf(fp, "interface %s\n", |
|---|
| 369 | dev); |
|---|
| 370 | |
|---|
| 371 | if (atoi |
|---|
| 372 | (nvram_safe_get(wdsdevospf)) |
|---|
| 373 | > 0) |
|---|
| 374 | fprintf(fp, |
|---|
| 375 | " ip ospf cost %s\n", |
|---|
| 376 | nvram_safe_get |
|---|
| 377 | (wdsdevospf)); |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | fprintf(fp, "!\n"); |
|---|
| 381 | } |
|---|
| 382 | fprintf(fp, "router osp6f\n"); |
|---|
| 383 | // fprintf(fp, " passive-interface lo\n"); |
|---|
| 384 | // fprintf(fp, " ospf router-id %s\n", |
|---|
| 385 | // nvram_safe_get("lan_ipaddr")); |
|---|
| 386 | fprintf(fp, " redistribute kernel\n"); |
|---|
| 387 | fprintf(fp, " redistribute connected\n"); |
|---|
| 388 | fprintf(fp, " redistribute static\n"); |
|---|
| 389 | // fprintf(fp, " network 0.0.0.0/0 area 0\n"); // handle all routing |
|---|
| 390 | // fprintf(fp, " default-information originate\n"); |
|---|
| 391 | |
|---|
| 392 | for (s = 1; s <= MAX_WDS_DEVS; s++) { |
|---|
| 393 | char wdsdevospf[32] = { 0 }; |
|---|
| 394 | sprintf(wdsdevospf, "wl_wds%d_ospf", s); |
|---|
| 395 | |
|---|
| 396 | if (atoi(nvram_safe_get(wdsdevospf)) < 0) |
|---|
| 397 | fprintf(fp, " passive-interface %s\n", |
|---|
| 398 | nvram_nget("wl_wds%d_if", s)); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | if (nvram_match("zebra_log", "1")) { |
|---|
| 402 | fprintf(fp, "!\n"); |
|---|
| 403 | fprintf(fp, "log file /var/log/ospf6.log\n"); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | fprintf(fp, "!\nline vty\n!\n"); |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | fflush(fp); |
|---|
| 410 | fclose(fp); |
|---|
| 411 | |
|---|
| 412 | // if (nvram_match("dyn_default", "1")) |
|---|
| 413 | // while (!eval("ip", "route", "del", "default")) ; |
|---|
| 414 | |
|---|
| 415 | ret1 = eval("zebra", "-d", "-r", "-f", "/tmp/zebra.conf"); |
|---|
| 416 | ret2 = eval("ospf6d", "-d", "-f", "/tmp/ospf6d.conf"); |
|---|
| 417 | |
|---|
| 418 | return ret1 + ret2; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | int zebra_ripd_init(void) |
|---|
| 422 | { |
|---|
| 423 | |
|---|
| 424 | char *lt = nvram_safe_get("dr_lan_tx"); |
|---|
| 425 | char *lr = nvram_safe_get("dr_lan_rx"); |
|---|
| 426 | char *wt = nvram_safe_get("dr_wan_tx"); |
|---|
| 427 | char *wr = nvram_safe_get("dr_wan_rx"); |
|---|
| 428 | char *lf = nvram_safe_get("lan_ifname"); |
|---|
| 429 | char *wf = get_wan_face(); |
|---|
| 430 | |
|---|
| 431 | FILE *fp; |
|---|
| 432 | int ret1, ret2; |
|---|
| 433 | |
|---|
| 434 | // printf("Start zebra\n"); |
|---|
| 435 | if (!strcmp(lt, "0") && !strcmp(lr, "0") && |
|---|
| 436 | !strcmp(wt, "0") && !strcmp(wr, "0") |
|---|
| 437 | && !nvram_match("zebra_copt", "1")) { |
|---|
| 438 | fprintf(stderr, "zebra disabled.\n"); |
|---|
| 439 | return 0; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | /* |
|---|
| 443 | * Write configuration file based on current information |
|---|
| 444 | */ |
|---|
| 445 | if (!(fp = fopen("/tmp/zebra.conf", "w"))) { |
|---|
| 446 | perror("/tmp/zebra.conf"); |
|---|
| 447 | return errno; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | if (nvram_match("zebra_copt", "1")) { |
|---|
| 451 | if (nvram_match("zebra_log", "1")) { |
|---|
| 452 | fprintf(fp, "log file /var/log/zebra.log\n"); |
|---|
| 453 | } |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | if (strlen(nvram_safe_get("zebra_conf")) > 0) { |
|---|
| 457 | fwritenvram("zebra_conf", fp); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | fclose(fp); |
|---|
| 461 | |
|---|
| 462 | if (!(fp = fopen("/tmp/ripd.conf", "w"))) { |
|---|
| 463 | perror("/tmp/ripd.conf"); |
|---|
| 464 | return errno; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | if (nvram_match("ripd_copt", "1")) { |
|---|
| 468 | fwritenvram("ripd_conf", fp); |
|---|
| 469 | } else { |
|---|
| 470 | |
|---|
| 471 | fprintf(fp, "router rip\n"); |
|---|
| 472 | fprintf(fp, " network %s\n", lf); |
|---|
| 473 | if (wf && strlen(wf) > 0) |
|---|
| 474 | fprintf(fp, " network %s\n", wf); |
|---|
| 475 | fprintf(fp, "redistribute connected\n"); |
|---|
| 476 | // fprintf(fp, "redistribute kernel\n"); |
|---|
| 477 | // fprintf(fp, "redistribute static\n"); |
|---|
| 478 | |
|---|
| 479 | fprintf(fp, "interface %s\n", lf); |
|---|
| 480 | if (strcmp(lt, "0") != 0) |
|---|
| 481 | fprintf(fp, " ip rip send version %s\n", lt); |
|---|
| 482 | if (strcmp(lr, "0") != 0) |
|---|
| 483 | fprintf(fp, " ip rip receive version %s\n", lr); |
|---|
| 484 | |
|---|
| 485 | if (wf && strlen(wf) > 0) |
|---|
| 486 | fprintf(fp, "interface %s\n", wf); |
|---|
| 487 | if (strcmp(wt, "0") != 0) |
|---|
| 488 | fprintf(fp, " ip rip send version %s\n", wt); |
|---|
| 489 | if (strcmp(wr, "0") != 0) |
|---|
| 490 | fprintf(fp, " ip rip receive version %s\n", wr); |
|---|
| 491 | |
|---|
| 492 | fprintf(fp, "router rip\n"); |
|---|
| 493 | if (strcmp(lt, "0") == 0) |
|---|
| 494 | fprintf(fp, " distribute-list private out %s\n", lf); |
|---|
| 495 | if (strcmp(lr, "0") == 0) |
|---|
| 496 | fprintf(fp, " distribute-list private in %s\n", lf); |
|---|
| 497 | if (wf && strlen(wf) > 0) { |
|---|
| 498 | if (strcmp(wt, "0") == 0) |
|---|
| 499 | fprintf(fp, |
|---|
| 500 | " distribute-list private out %s\n", |
|---|
| 501 | wf); |
|---|
| 502 | if (strcmp(wr, "0") == 0) |
|---|
| 503 | fprintf(fp, |
|---|
| 504 | " distribute-list private in %s\n", |
|---|
| 505 | wf); |
|---|
| 506 | } |
|---|
| 507 | fprintf(fp, "access-list private deny any\n"); |
|---|
| 508 | |
|---|
| 509 | // fprintf(fp, "debug rip events\n"); |
|---|
| 510 | // fprintf(fp, "log file /tmp/ripd.log\n"); |
|---|
| 511 | fflush(fp); |
|---|
| 512 | } |
|---|
| 513 | fclose(fp); |
|---|
| 514 | |
|---|
| 515 | ret1 = eval("zebra", "-d", "-f", "/tmp/zebra.conf"); |
|---|
| 516 | ret2 = eval("ripd", "-d", "-f", "/tmp/ripd.conf"); |
|---|
| 517 | |
|---|
| 518 | return ret1 + ret2; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | int zebra_bgp_init(void) |
|---|
| 522 | { |
|---|
| 523 | |
|---|
| 524 | char *lt = nvram_safe_get("dr_lan_tx"); |
|---|
| 525 | char *lr = nvram_safe_get("dr_lan_rx"); |
|---|
| 526 | char *wt = nvram_safe_get("dr_wan_tx"); |
|---|
| 527 | char *wr = nvram_safe_get("dr_wan_rx"); |
|---|
| 528 | char *lf = nvram_safe_get("lan_ifname"); |
|---|
| 529 | char *wf = get_wan_face(); |
|---|
| 530 | |
|---|
| 531 | FILE *fp; |
|---|
| 532 | int ret1, ret2; |
|---|
| 533 | |
|---|
| 534 | if (!strcmp(lt, "0") && !strcmp(lr, "0") && |
|---|
| 535 | !strcmp(wt, "0") && !strcmp(wr, "0") |
|---|
| 536 | && !nvram_match("zebra_copt", "1")) { |
|---|
| 537 | return 0; |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | /* |
|---|
| 541 | * Write configuration file based on current information |
|---|
| 542 | */ |
|---|
| 543 | if (!(fp = fopen("/tmp/zebra.conf", "w"))) { |
|---|
| 544 | perror("/tmp/zebra.conf"); |
|---|
| 545 | return errno; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | if (nvram_match("zebra_copt", "1")) { |
|---|
| 549 | if (nvram_match("zebra_log", "1")) { |
|---|
| 550 | fprintf(fp, "log file /var/log/zebra.log\n"); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | if (strlen(nvram_safe_get("zebra_conf")) > 0) { |
|---|
| 555 | fwritenvram("zebra_conf", fp); |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | fclose(fp); |
|---|
| 559 | |
|---|
| 560 | if (!(fp = fopen("/tmp/bgpd.conf", "w"))) { |
|---|
| 561 | perror("/tmp/bgpd.conf"); |
|---|
| 562 | return errno; |
|---|
| 563 | } |
|---|
| 564 | if (nvram_match("bgpd_copt", "1")) { |
|---|
| 565 | fwritenvram("bgpd_conf", fp); |
|---|
| 566 | } else { |
|---|
| 567 | fprintf(fp, "router bgp %s\n", |
|---|
| 568 | nvram_safe_get("routing_bgp_as")); |
|---|
| 569 | fprintf(fp, " network %s/%d\n", nvram_safe_get("lan_ipaddr"), |
|---|
| 570 | get_net(nvram_safe_get("lan_netmask"))); |
|---|
| 571 | if (wf && strlen(wf) > 0 && strcmp(get_wan_ipaddr(), "0.0.0.0")) |
|---|
| 572 | fprintf(fp, " network %s/%s\n", |
|---|
| 573 | get_wan_ipaddr(), |
|---|
| 574 | nvram_safe_get("wan_netmask")); |
|---|
| 575 | fprintf(fp, "neighbor %s local-as %s\n", lf, |
|---|
| 576 | nvram_safe_get("routing_bgp_as")); |
|---|
| 577 | if (wf && strlen(wf) > 0) |
|---|
| 578 | fprintf(fp, "neighbor %s local-as %s\n", wf, |
|---|
| 579 | nvram_safe_get("routing_bgp_as")); |
|---|
| 580 | fprintf(fp, "neighbor %s remote-as %s\n", |
|---|
| 581 | nvram_safe_get("routing_bgp_neighbor_ip"), |
|---|
| 582 | nvram_safe_get("routing_bgp_neighbor_as")); |
|---|
| 583 | fprintf(fp, "access-list all permit any\n"); |
|---|
| 584 | |
|---|
| 585 | fflush(fp); |
|---|
| 586 | } |
|---|
| 587 | fclose(fp); |
|---|
| 588 | |
|---|
| 589 | ret1 = eval("zebra", "-d", "-f", "/tmp/zebra.conf"); |
|---|
| 590 | ret2 = eval("bgpd", "-d", "-f", "/tmp/bgpd.conf"); |
|---|
| 591 | |
|---|
| 592 | return ret1 + ret2; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | #endif |
|---|
| 596 | |
|---|
| 597 | #ifdef HAVE_BIRD |
|---|
| 598 | int bird_init(void) |
|---|
| 599 | { |
|---|
| 600 | FILE *fp; |
|---|
| 601 | int ret1; |
|---|
| 602 | |
|---|
| 603 | /* |
|---|
| 604 | * compatibitly for old nvram style (site needs to be enhanced) |
|---|
| 605 | */ |
|---|
| 606 | if (has_gateway()) |
|---|
| 607 | return 0; |
|---|
| 608 | nvram_set("routing_ospf", "off"); |
|---|
| 609 | nvram_set("routing_bgp", "off"); |
|---|
| 610 | nvram_set("routing_rip2", "off"); |
|---|
| 611 | |
|---|
| 612 | if (nvram_match("wk_mode", "ospf")) |
|---|
| 613 | nvram_set("routing_ospf", "on"); |
|---|
| 614 | if (nvram_match("wk_mode", "router")) |
|---|
| 615 | nvram_set("routing_rip2", "on"); |
|---|
| 616 | if (nvram_match("wk_mode", "bgp")) |
|---|
| 617 | nvram_set("routing_bgp", "on"); |
|---|
| 618 | |
|---|
| 619 | if (nvram_match("dr_setting", "1")) { |
|---|
| 620 | nvram_set("routing_wan", "on"); |
|---|
| 621 | nvram_set("routing_lan", "off"); |
|---|
| 622 | } |
|---|
| 623 | if (nvram_match("dr_setting", "2")) { |
|---|
| 624 | nvram_set("routing_wan", "off"); |
|---|
| 625 | nvram_set("routing_lan", "on"); |
|---|
| 626 | } |
|---|
| 627 | if (nvram_match("dr_setting", "3")) { |
|---|
| 628 | nvram_set("routing_wan", "on"); |
|---|
| 629 | nvram_set("routing_lan", "on"); |
|---|
| 630 | } |
|---|
| 631 | if (nvram_match("dr_setting", "0")) { |
|---|
| 632 | nvram_set("routing_wan", "off"); |
|---|
| 633 | nvram_set("routing_lan", "off"); |
|---|
| 634 | } |
|---|
| 635 | // DD-WRT bird support |
|---|
| 636 | if (nvram_match("routing_rip2", "on") || |
|---|
| 637 | nvram_match("routing_ospf", "on") |
|---|
| 638 | || nvram_match("routing_bgp", "on")) { |
|---|
| 639 | mkdir("/tmp/bird", 0744); |
|---|
| 640 | if (!(fp = fopen("/tmp/bird/bird.conf", "w"))) { |
|---|
| 641 | perror("/tmp/bird/bird.conf"); |
|---|
| 642 | return errno; |
|---|
| 643 | } |
|---|
| 644 | fprintf(fp, "router id %s;\n", nvram_safe_get("lan_ipaddr")); |
|---|
| 645 | fprintf(fp, |
|---|
| 646 | "protocol kernel { learn; persist; scan time 10; import all; export all; }\n"); |
|---|
| 647 | fprintf(fp, "protocol device { scan time 10; } \n"); |
|---|
| 648 | fprintf(fp, "protocol direct { interface \"*\";}\n"); |
|---|
| 649 | |
|---|
| 650 | if (nvram_match("routing_rip2", "on")) { |
|---|
| 651 | |
|---|
| 652 | fprintf(fp, "protocol rip WRT54G_rip {\n"); |
|---|
| 653 | if (nvram_match("routing_lan", "on")) |
|---|
| 654 | fprintf(fp, " interface \"%s\" { };\n", |
|---|
| 655 | nvram_safe_get("lan_ifname")); |
|---|
| 656 | if (nvram_match("routing_wan", "on")) { |
|---|
| 657 | if (getSTA()) |
|---|
| 658 | fprintf(fp, |
|---|
| 659 | " interface \"%s\" { };\n", |
|---|
| 660 | getSTA()); |
|---|
| 661 | else |
|---|
| 662 | fprintf(fp, |
|---|
| 663 | " interface \"%s\" { };\n", |
|---|
| 664 | nvram_safe_get("wan_ifname")); |
|---|
| 665 | |
|---|
| 666 | } |
|---|
| 667 | fprintf(fp, " honor always;\n"); |
|---|
| 668 | fprintf(fp, |
|---|
| 669 | " import filter { print \"importing\"; accept; };\n"); |
|---|
| 670 | fprintf(fp, |
|---|
| 671 | " export filter { print \"exporting\"; accept; };\n"); |
|---|
| 672 | fprintf(fp, "}\n"); |
|---|
| 673 | |
|---|
| 674 | } |
|---|
| 675 | if (nvram_match("routing_ospf", "on")) { |
|---|
| 676 | fprintf(fp, "protocol ospf WRT54G_ospf {\n"); |
|---|
| 677 | fprintf(fp, "area 0 {\n"); |
|---|
| 678 | if (nvram_match("routing_wan", "on")) |
|---|
| 679 | fprintf(fp, |
|---|
| 680 | "interface \"%s\" { cost 1; authentication simple; password \"%s\"; };\n", |
|---|
| 681 | nvram_safe_get("wan_ifname"), |
|---|
| 682 | nvram_safe_get("http_passwd")); |
|---|
| 683 | if (nvram_match("routing_lan", "on")) |
|---|
| 684 | fprintf(fp, |
|---|
| 685 | "interface \"%s\" { cost 1; authentication simple; password \"%s\"; };\n", |
|---|
| 686 | nvram_safe_get("lan_ifname"), |
|---|
| 687 | nvram_safe_get("http_passwd")); |
|---|
| 688 | fprintf(fp, "};\n}\n"); |
|---|
| 689 | } // if wk_mode = ospf |
|---|
| 690 | |
|---|
| 691 | if (nvram_match("routing_bgp", "on")) { |
|---|
| 692 | fprintf(fp, "protocol bgp {\n"); |
|---|
| 693 | fprintf(fp, "local as %s;\n", |
|---|
| 694 | nvram_safe_get("routing_bgp_as")); |
|---|
| 695 | fprintf(fp, "neighbor %s as %s;\n", |
|---|
| 696 | nvram_safe_get("routing_bgp_neighbor_ip"), |
|---|
| 697 | nvram_safe_get("routing_bgp_neighbor_as")); |
|---|
| 698 | fprintf(fp, "export all;\n"); |
|---|
| 699 | fprintf(fp, "import all;\n"); |
|---|
| 700 | fprintf(fp, "}\n"); |
|---|
| 701 | } |
|---|
| 702 | fflush(fp); |
|---|
| 703 | fclose(fp); |
|---|
| 704 | |
|---|
| 705 | ret1 = eval("bird", "-c", "/tmp/bird/bird.conf"); |
|---|
| 706 | dd_syslog(LOG_INFO, |
|---|
| 707 | "bird : bird daemon successfully started\n"); |
|---|
| 708 | } |
|---|
| 709 | return 0; |
|---|
| 710 | |
|---|
| 711 | } |
|---|
| 712 | #endif /* HAVE_BIRD */ |
|---|
| 713 | #if defined(HAVE_BIRD) || defined(HAVE_QUAGGA) |
|---|
| 714 | /* |
|---|
| 715 | * Written by Sparq in 2002/07/16 |
|---|
| 716 | */ |
|---|
| 717 | void start_zebra(void) |
|---|
| 718 | { |
|---|
| 719 | |
|---|
| 720 | if (!nvram_invmatch("zebra_enable", "0")) |
|---|
| 721 | return; |
|---|
| 722 | |
|---|
| 723 | #ifdef HAVE_BIRD |
|---|
| 724 | |
|---|
| 725 | if (bird_init() != 0) |
|---|
| 726 | return; |
|---|
| 727 | |
|---|
| 728 | #elif defined(HAVE_QUAGGA) |
|---|
| 729 | |
|---|
| 730 | if (zebra_init() != 0) |
|---|
| 731 | return; |
|---|
| 732 | |
|---|
| 733 | #endif /* HAVE_BIRD */ |
|---|
| 734 | return; |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | /* |
|---|
| 738 | * Written by Sparq in 2002/07/16 |
|---|
| 739 | */ |
|---|
| 740 | void stop_zebra(void) |
|---|
| 741 | { |
|---|
| 742 | |
|---|
| 743 | #ifdef HAVE_QUAGGA |
|---|
| 744 | stop_process("zebra", "zebra daemon"); |
|---|
| 745 | stop_process("ripd", "rip daemon"); |
|---|
| 746 | stop_process("ospfd", "ospf daemon"); |
|---|
| 747 | stop_process("ospf6d", "ospf6 daemon"); |
|---|
| 748 | stop_process("bgpd", "bgp daemon"); |
|---|
| 749 | return; |
|---|
| 750 | |
|---|
| 751 | #elif defined(HAVE_BIRD) |
|---|
| 752 | stop_process("bird", "bird daemon"); |
|---|
| 753 | return; |
|---|
| 754 | |
|---|
| 755 | #else |
|---|
| 756 | return; |
|---|
| 757 | #endif |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | #endif |
|---|