#include "ospf6_zebra.h"
#include "lib/keychain.h"
+#define OSPF6D_COMPAT_AUTHSEQ_NAME "%s/ospf6d-at-seq-no.dat", frr_runstatedir
+
unsigned char conf_debug_ospf6_auth[2];
DEFINE_MTYPE_STATIC(OSPF6D, OSPF6_AUTH_HASH_XOR, "OSPF6 auth hash xor");
+static void ospf6_auth_seqno_nvm_update(struct ospf6 *ospf6);
+
/*Apad is the hexadecimal value 0x878FE1F3. */
const uint8_t ospf6_hash_apad_max[KEYCHAIN_MAX_HASH_SIZE] = {
0x87, 0x8f, 0xe1, 0xf3, 0x87, 0x8f, 0xe1, 0xf3, 0x87, 0x8f, 0xe1,
install_element(ENABLE_NODE, &clear_ipv6_ospf6_intf_auth_cmd);
}
-enum ospf6_auth_err ospf6_auth_nvm_file_exist(void)
-{
- struct stat buffer;
- int exist;
-
- exist = stat(OSPF6_AUTH_SEQ_NUM_FILE, &buffer);
- if (exist == 0)
- return OSPF6_AUTH_FILE_EXIST;
- else
- return OSPF6_AUTH_FILE_DO_NOT_EXIST;
-}
-
/*
* Record in non-volatile memory the given ospf6 process,
* authentication trailer higher order sequence number.
*/
-void ospf6_auth_seqno_nvm_update(struct ospf6 *ospf6)
+static void ospf6_auth_seqno_nvm_update(struct ospf6 *ospf6)
{
const char *inst_name;
json_object *json;
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6_AUTH_SEQ_NUM_FILE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
*/
json_object_int_add(json_instance, "sequence_number", ospf6->seqnum_h);
- json_object_to_file_ext((char *)OSPF6_AUTH_SEQ_NUM_FILE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ frr_daemon_state_save(&json);
}
/*
* Delete authentication sequence number for a given OSPF6 process
* from non-volatile memory.
*/
-void ospf6_auth_seqno_nvm_delete(struct ospf6 *ospf6)
+__attribute__((unused)) static void
+ospf6_auth_seqno_nvm_delete(struct ospf6 *ospf6)
{
const char *inst_name;
json_object *json;
json_object *json_instances;
+ json_object *json_instance;
zlog_err("Higher order sequence number delete for %s process",
ospf6->name);
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6_AUTH_SEQ_NUM_FILE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
- json_instances = json_object_new_object();
- json_object_object_add(json, "instances", json_instances);
+ json_object_put(json);
+ return;
+ }
+
+ json_object_object_get_ex(json_instances, inst_name, &json_instance);
+ if (json_instance) {
+ json_object_put(json);
+ return;
}
- json_object_object_del(json_instances, inst_name);
+ json_object_object_del(json_instance, "sequence_number");
- json_object_to_file_ext((char *)OSPF6_AUTH_SEQ_NUM_FILE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ frr_daemon_state_save(&json);
}
+static struct json_object *ospf6_auth_seqno_compat_read(const char *inst_name)
+{
+ /* try legacy location */
+ char compat_path[512];
+ json_object *json;
+ json_object *json_instances = NULL;
+ json_object *json_instance = NULL;
+ json_object *json_seqnum = NULL;
+
+ snprintf(compat_path, sizeof(compat_path), OSPF6D_COMPAT_AUTHSEQ_NAME);
+ json = json_object_from_file(compat_path);
+
+ if (json)
+ json_object_object_get_ex(json, "instances", &json_instances);
+ if (json_instances)
+ json_object_object_get_ex(json_instances, inst_name,
+ &json_instance);
+ if (json_instance)
+ json_object_object_get_ex(json_instance, "sequence_number",
+ &json_seqnum);
+ if (json_seqnum)
+ /* => free the file-level object and still return this */
+ json_seqnum = json_object_get(json_seqnum);
+
+ if (json) {
+ json_object_free(json);
+ unlink(compat_path);
+ }
+ return json_seqnum;
+}
+
/*
* Fetch from non-volatile memory the stored ospf6 process
* authentication sequence number.
*/
-void ospf6_auth_seqno_nvm_read(struct ospf6 *ospf6)
+static void ospf6_auth_seqno_nvm_read(struct ospf6 *ospf6)
{
const char *inst_name;
json_object *json;
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6_AUTH_SEQ_NUM_FILE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
json_object_object_get_ex(json_instance, "sequence_number",
&json_seqnum);
- ospf6->seqnum_h = json_object_get_int(json_seqnum);
+
+ if (json_seqnum)
+ /* cf. reference taken in compat_read above */
+ json_seqnum = json_object_get(json_seqnum);
+ else
+ json_seqnum = ospf6_auth_seqno_compat_read(inst_name);
+
+ ospf6->seqnum_l = 0;
+ if (json_seqnum) {
+ ospf6->seqnum_h = json_object_get_int(json_seqnum);
+ ospf6->seqnum_h += 1;
+ } else {
+ ospf6->seqnum_h = 0;
+ }
+
+ if (json_seqnum)
+ json_object_put(json_seqnum);
zlog_err("Higher order sequence number %d read for %s process %s",
ospf6->seqnum_h, ospf6->name, strerror(errno));
- json_object_object_del(json_instances, inst_name);
- json_object_to_file_ext((char *)OSPF6_AUTH_SEQ_NUM_FILE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ json_object_object_del(json_instance, "sequence_number");
+
+ frr_daemon_state_save(&json);
+}
+
+void ospf6_auth_init(struct ospf6 *o)
+{
+ ospf6_auth_seqno_nvm_read(o);
+ ospf6_auth_seqno_nvm_update(o);
}
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6D_GR_STATE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
json_object_int_add(json_instance, "timestamp",
time(NULL) + ospf6->gr_info.grace_period);
- json_object_to_file_ext((char *)OSPF6D_GR_STATE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ frr_daemon_state_save(&json);
}
/*
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6D_GR_STATE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
json_object_object_del(json_instances, inst_name);
- json_object_to_file_ext((char *)OSPF6D_GR_STATE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ frr_daemon_state_save(&json);
}
/*
inst_name = ospf6->name ? ospf6->name : VRF_DEFAULT_NAME;
- json = json_object_from_file((char *)OSPF6D_GR_STATE);
- if (json == NULL)
- json = json_object_new_object();
+ json = frr_daemon_state_load();
json_object_object_get_ex(json, "instances", &json_instances);
if (!json_instances) {
ospf6->gr_info.grace_period);
}
- json_object_object_del(json_instances, inst_name);
+ json_object_object_del(json_instance, "gracePeriod");
+ json_object_object_del(json_instance, "timestamp");
- json_object_to_file_ext((char *)OSPF6D_GR_STATE, json,
- JSON_C_TO_STRING_PRETTY);
- json_object_free(json);
+ frr_daemon_state_save(&json);
}
void ospf6_gr_unplanned_start_interface(struct ospf6_interface *oi)
/* Default configuration file name for ospf6d. */
#define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
+/* GR and auth trailer persistent state */
+#define OSPF6D_STATE_NAME "%s/ospf6d.json", frr_libstatedir
+#define OSPF6D_COMPAT_STATE_NAME "%s/ospf6d-gr.json", frr_runstatedir
+/* for extra confusion, "ospf6d-at-seq-no.dat" is handled directly in
+ * ospf6_auth_trailer.c; the alternative would be somehow merging JSON which
+ * is excessive for just supporting a legacy compatibility file location
+ */
+
/* Default port values. */
#define OSPF6_VTY_PORT 2606
&frr_ospf6_route_map_info,
};
+/* actual paths filled in main() */
+static char state_path[512];
+static char state_compat_path[512];
+static char *state_paths[] = {
+ state_path,
+ state_compat_path,
+ NULL,
+};
+
/* clang-format off */
FRR_DAEMON_INFO(ospf6d, OSPF6,
.vty_port = OSPF6_VTY_PORT,
.yang_modules = ospf6d_yang_modules,
.n_yang_modules = array_size(ospf6d_yang_modules),
+
+ .state_paths = state_paths,
);
/* clang-format on */
exit(1);
}
+ snprintf(state_path, sizeof(state_path), OSPF6D_STATE_NAME);
+ snprintf(state_compat_path, sizeof(state_compat_path),
+ OSPF6D_COMPAT_STATE_NAME);
+
/* OSPF6 master init. */
ospf6_master_init(frr_init());