Merge branch 'master' into config_file

Conflicts:
	openbsc/src/vty_interface.c
diff --git a/openbsc/include/vty/command.h b/openbsc/include/vty/command.h
index f536f2e..1be05aa 100644
--- a/openbsc/include/vty/command.h
+++ b/openbsc/include/vty/command.h
@@ -61,6 +61,7 @@
 
 /* There are some command levels which called from command node. */
 enum node_type {
+	GSMNET_NODE,
 	BTS_NODE,
 	TRX_NODE,
 	TS_NODE,
diff --git a/openbsc/include/vty/vty.h b/openbsc/include/vty/vty.h
index d43d992..4a1a6ff 100644
--- a/openbsc/include/vty/vty.h
+++ b/openbsc/include/vty/vty.h
@@ -129,6 +129,7 @@
 
 /* Prototypes. */
 void vty_init (void);
+int vty_read_config_file(const char *file_name);
 void vty_init_vtysh (void);
 void vty_reset (void);
 struct vty *vty_new (void);
@@ -136,8 +137,7 @@
 int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
 int vty_out_newline(struct vty *);
 int vty_read(struct vty *vty);
-void vty_read_config (char *, char *);
-void vty_time_print (struct vty *, int);
+//void vty_time_print (struct vty *, int);
 void vty_close (struct vty *);
 char *vty_get_cwd (void);
 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
@@ -147,5 +147,5 @@
 int vty_shell_serv (struct vty *);
 void vty_hello (struct vty *);
 
-
+void *tall_vty_ctx;
 #endif
diff --git a/openbsc/src/openbsc.cfg.1-1 b/openbsc/src/openbsc.cfg.1-1
new file mode 100644
index 0000000..acd6681
--- /dev/null
+++ b/openbsc/src/openbsc.cfg.1-1
@@ -0,0 +1,51 @@
+!
+! OpenBSC configuration saved from vty
+!   !
+password foo
+!
+line vty
+ no login
+!
+network
+ network country code 1
+ mobile network code 1
+ short name OpenBSC
+ long name OpenBSC
+ bts 0
+  type bs11
+  band GSM900
+  location_area_code 1
+  training_sequence_code 7
+  base_station_id_code 63
+  oml e1 line 0 timeslot 1 sub-slot full
+  oml e1 tei 25
+  trx 0
+   arfcn 121
+   max_power_red 0
+   rsl e1 line 0 timeslot 1 sub-slot full
+   rsl e1 tei 1
+    timeslot 0
+     phys_chan_config CCCH+SDCCH4
+     e1 line 0 timeslot 1 sub-slot full
+    timeslot 1
+     phys_chan_config SDCCH8
+     e1 line 0 timeslot 2 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 3
+
diff --git a/openbsc/src/openbsc.cfg.1-2 b/openbsc/src/openbsc.cfg.1-2
new file mode 100644
index 0000000..6bb6522
--- /dev/null
+++ b/openbsc/src/openbsc.cfg.1-2
@@ -0,0 +1,79 @@
+!
+! OpenBSC configuration saved from vty
+!   !
+password foo
+!
+line vty
+ no login
+!
+network
+ network country code 1
+ mobile network code 1
+ short name OpenBSC
+ long name OpenBSC
+ bts 0
+  type bs11
+  band GSM900
+  location_area_code 1
+  training_sequence_code 7
+  base_station_id_code 63
+  oml e1 line 0 timeslot 1 sub-slot full
+  oml e1 tei 25
+  trx 0
+   arfcn 121
+   max_power_red 0
+   rsl e1 line 0 timeslot 1 sub-slot full
+   rsl e1 tei 1
+    timeslot 0
+     phys_chan_config CCCH+SDCCH4
+     e1 line 0 timeslot 1 sub-slot full
+    timeslot 1
+     phys_chan_config SDCCH8
+     e1 line 0 timeslot 2 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 3
+  trx 1
+   arfcn 123
+   max_power_red 0
+   rsl e1 line 0 timeslot 1 sub-slot full
+   rsl e1 tei 2
+    timeslot 0
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 0
+    timeslot 1
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 3
diff --git a/openbsc/src/openbsc.cfg.2-2 b/openbsc/src/openbsc.cfg.2-2
new file mode 100644
index 0000000..461f8e9
--- /dev/null
+++ b/openbsc/src/openbsc.cfg.2-2
@@ -0,0 +1,145 @@
+!
+! OpenBSC configuration saved from vty
+!   !
+password foo
+!
+line vty
+ no login
+!
+network
+ network country code 1
+ mobile network code 1
+ short name OpenBSC
+ long name OpenBSC
+ bts 0
+  type bs11
+  band GSM900
+  location_area_code 1
+  training_sequence_code 7
+  base_station_id_code 63
+  oml e1 line 0 timeslot 1 sub-slot full
+  oml e1 tei 25
+  trx 0
+   arfcn 121
+   max_power_red 0
+   rsl e1 line 0 timeslot 1 sub-slot full
+   rsl e1 tei 1
+    timeslot 0
+     phys_chan_config CCCH+SDCCH4
+     e1 line 0 timeslot 1 sub-slot full
+    timeslot 1
+     phys_chan_config SDCCH8
+     e1 line 0 timeslot 2 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 2 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 3 sub-slot 3
+  trx 1
+   arfcn 123
+   max_power_red 0
+   rsl e1 line 0 timeslot 1 sub-slot full
+   rsl e1 tei 2
+    timeslot 0
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 0
+    timeslot 1
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 4 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 0 timeslot 5 sub-slot 3
+ bts 1
+  type bs11
+  band GSM900
+  location_area_code 2
+  training_sequence_code 7
+  base_station_id_code 63
+  oml e1 line 1 timeslot 6 sub-slot full
+  oml e1 tei 25
+  trx 0
+   arfcn 122
+   max_power_red 0
+   rsl e1 line 1 timeslot 6 sub-slot full
+   rsl e1 tei 1
+    timeslot 0
+     phys_chan_config CCCH+SDCCH4
+     e1 line 1 timeslot 7 sub-slot 0
+    timeslot 1
+     phys_chan_config SDCCH8
+     e1 line 1 timeslot 7 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 7 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 7 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 8 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 8 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 8 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 8 sub-slot 3
+  trx 1
+   arfcn 124
+   max_power_red 0
+   rsl e1 line 1 timeslot 6 sub-slot full
+   rsl e1 tei 2
+    timeslot 0
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 9 sub-slot 0
+    timeslot 1
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 9 sub-slot 1
+    timeslot 2
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 9 sub-slot 2
+    timeslot 3
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 9 sub-slot 3
+    timeslot 4
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 10 sub-slot 0
+    timeslot 5
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 10 sub-slot 1
+    timeslot 6
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 10 sub-slot 2
+    timeslot 7
+     phys_chan_config TCH/F
+     e1 line 1 timeslot 10 sub-slot 3
diff --git a/openbsc/src/vty/buffer.c b/openbsc/src/vty/buffer.c
index be6623d..8ab7311 100644
--- a/openbsc/src/vty/buffer.c
+++ b/openbsc/src/vty/buffer.c
@@ -32,8 +32,6 @@
 #include <vty/buffer.h>
 #include <vty/vty.h>
 
-static void *tall_vbuf_ctx;
-
 /* Buffer master. */
 struct buffer {
 	/* Data list. */
@@ -71,7 +69,7 @@
 {
 	struct buffer *b;
 
-	b = talloc_zero(tall_vbuf_ctx, struct buffer);
+	b = talloc_zero(tall_vty_ctx, struct buffer);
 
 	if (size)
 		b->size = size;
@@ -105,7 +103,7 @@
 
 	for (data = b->head; data; data = data->next)
 		totlen += data->cp - data->sp;
-	if (!(s = _talloc_zero(tall_vbuf_ctx, (totlen + 1), "buffer_getstr")))
+	if (!(s = _talloc_zero(tall_vty_ctx, (totlen + 1), "buffer_getstr")))
 		return NULL;
 	p = s;
 	for (data = b->head; data; data = data->next) {
@@ -140,7 +138,7 @@
 {
 	struct buffer_data *d;
 
-	d = _talloc_zero(tall_vbuf_ctx,
+	d = _talloc_zero(tall_vty_ctx,
 			 offsetof(struct buffer_data, data[b->size]),
 			 "buffer_add");
 	if (!d)
@@ -463,8 +461,3 @@
 	}
 	return b->head ? BUFFER_PENDING : BUFFER_EMPTY;
 }
-
-static __attribute__((constructor)) void on_dso_load_vty_buf(void)
-{
-	tall_vbuf_ctx = talloc_named_const(NULL, 1, "vty_buffer");
-}
diff --git a/openbsc/src/vty/command.c b/openbsc/src/vty/command.c
index 9841343..16e0dfb 100644
--- a/openbsc/src/vty/command.c
+++ b/openbsc/src/vty/command.c
@@ -49,8 +49,6 @@
 #include <openbsc/gsm_subscriber.h>
 #include <openbsc/talloc.h>
 
-static void *tall_vcmd_ctx;
-
 /* Command vector which includes some level of command lists. Normally
    each daemon maintains each own cmdvec. */
 vector cmdvec;
@@ -175,7 +173,7 @@
 		len += strlen(argv[i]) + 1;
 	if (!len)
 		return NULL;
-	p = str = _talloc_zero(tall_vcmd_ctx, len, "arvg_concat");
+	p = str = _talloc_zero(tall_vty_ctx, len, "arvg_concat");
 	for (i = shift; i < argc; i++) {
 		size_t arglen;
 		memcpy(p, argv[i], (arglen = strlen(argv[i])));
@@ -277,7 +275,7 @@
 		       *cp != '\0')
 			cp++;
 		strlen = cp - start;
-		token = _talloc_zero(tall_vcmd_ctx, strlen + 1, "make_strvec");
+		token = _talloc_zero(tall_vty_ctx, strlen + 1, "make_strvec");
 		memcpy(token, start, strlen);
 		*(token + strlen) = '\0';
 		vector_set(strvec, token);
@@ -333,7 +331,7 @@
 		cp++;
 
 	strlen = cp - start;
-	token = _talloc_zero(tall_vcmd_ctx, strlen + 1, "cmd_desc_str");
+	token = _talloc_zero(tall_vty_ctx, strlen + 1, "cmd_desc_str");
 	memcpy(token, start, strlen);
 	*(token + strlen) = '\0';
 
@@ -404,11 +402,11 @@
 
 		len = cp - sp;
 
-		token = _talloc_zero(tall_vcmd_ctx, len + 1, "cmd_make_descvec");
+		token = _talloc_zero(tall_vty_ctx, len + 1, "cmd_make_descvec");
 		memcpy(token, sp, len);
 		*(token + len) = '\0';
 
-		desc = talloc_zero(tall_vcmd_ctx, struct desc);
+		desc = talloc_zero(tall_vty_ctx, struct desc);
 		desc->cmd = token;
 		desc->str = cmd_desc_str(&dp);
 
@@ -509,7 +507,6 @@
 /* This function write configuration of this host. */
 static int config_write_host(struct vty *vty)
 {
-#if 0
 	if (host.name)
 		vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
 
@@ -529,6 +526,7 @@
 				VTY_NEWLINE);
 	}
 
+#if 0
 	if (zlog_default->default_lvl != LOG_DEBUG) {
 		vty_out(vty, "! N.B. The 'log trap' command is deprecated.%s",
 			VTY_NEWLINE);
@@ -581,7 +579,7 @@
 
 	if (zlog_default->record_priority == 1)
 		vty_out(vty, "log record-priority%s", VTY_NEWLINE);
-
+#endif
 	if (host.advanced)
 		vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
 
@@ -598,7 +596,6 @@
 	else if (!host.motd)
 		vty_out(vty, "no banner motd%s", VTY_NEWLINE);
 
-#endif
 	return 1;
 }
 
@@ -1805,16 +1802,9 @@
 				descvec = vector_slot(strvec, index);
 				for (j = 0; j < vector_active(descvec); j++)
 					if ((desc = vector_slot(descvec, j))) {
-						if ((string =
-						     cmd_entry_function
-						     (vector_slot(vline, index),
-						      desc->cmd)))
-							if (cmd_unique_string
-							    (matchvec, string))
-								vector_set
-								    (matchvec,
-								     strdup
-								     (string));
+						if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
+							if (cmd_unique_string (matchvec, string))
+								vector_set (matchvec, talloc_strdup(tall_vty_ctx, string));
 					}
 			}
 		}
@@ -1855,7 +1845,7 @@
 			if (len < lcd) {
 				char *lcdstr;
 
-				lcdstr = _talloc_zero(tall_vcmd_ctx, lcd + 1,
+				lcdstr = _talloc_zero(tall_vty_ctx, lcd + 1,
 						      "complete-lcdstr");
 				memcpy(lcdstr, matchvec->index[0], lcd);
 				lcdstr[lcd] = '\0';
@@ -2298,10 +2288,18 @@
       config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
 {
 	switch (vty->node) {
-	case BTS_NODE:
-		vty->node = VIEW_NODE;
+	case GSMNET_NODE:
+		vty->node = CONFIG_NODE;
 		vty->index = NULL;
 		break;
+	case BTS_NODE:
+		vty->node = GSMNET_NODE;
+		{
+			/* set vty->index correctly ! */
+			struct gsm_bts *bts = vty->index;
+			vty->index = bts->network;
+		}
+		break;
 	case TRX_NODE:
 		vty->node = BTS_NODE;
 		{
@@ -2476,13 +2474,13 @@
 	config_file = host.config;
 
 	config_file_sav =
-	    _talloc_zero(tall_vcmd_ctx,
+	    _talloc_zero(tall_vty_ctx,
 			 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
 			 "config_file_sav");
 	strcpy(config_file_sav, config_file);
 	strcat(config_file_sav, CONF_BACKUP_EXT);
 
-	config_file_tmp = _talloc_zero(tall_vcmd_ctx, strlen(config_file) + 8,
+	config_file_tmp = _talloc_zero(tall_vty_ctx, strlen(config_file) + 8,
 					"config_file_tmp");
 	sprintf(config_file_tmp, "%s.XXXXXX", config_file);
 
@@ -2663,7 +2661,7 @@
 	if (host.name)
 		talloc_free(host.name);
 
-	host.name = strdup(argv[0]);
+	host.name = talloc_strdup(tall_vty_ctx, argv[0]);
 	return CMD_SUCCESS;
 }
 
@@ -2698,7 +2696,7 @@
 			host.password = NULL;
 			if (host.password_encrypt)
 				talloc_free(host.password_encrypt);
-			host.password_encrypt = strdup(strdup(argv[1]));
+			host.password_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
 			return CMD_SUCCESS;
 		} else {
 			vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
@@ -2721,10 +2719,10 @@
 	if (host.encrypt) {
 		if (host.password_encrypt)
 			talloc_free(host.password_encrypt);
-		host.password_encrypt = strdup(zencrypt(argv[0]));
+		host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
 	} else
 #endif
-		host.password = strdup(argv[0]);
+		host.password = talloc_strdup(tall_vty_ctx, argv[0]);
 
 	return CMD_SUCCESS;
 }
@@ -2757,7 +2755,7 @@
 
 			if (host.enable_encrypt)
 				talloc_free(host.enable_encrypt);
-			host.enable_encrypt = strdup(argv[1]);
+			host.enable_encrypt = talloc_strdup(tall_vty_ctx, argv[1]);
 
 			return CMD_SUCCESS;
 		} else {
@@ -2782,10 +2780,10 @@
 	if (host.encrypt) {
 		if (host.enable_encrypt)
 			talloc_free(host.enable_encrypt);
-		host.enable_encrypt = strdup(zencrypt(argv[0]));
+		host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(argv[0]));
 	} else
 #endif
-		host.enable = strdup(argv[0]);
+		host.enable = talloc_strdup(tall_vty_ctx, argv[0]);
 
 	return CMD_SUCCESS;
 }
@@ -2829,12 +2827,12 @@
 	if (host.password) {
 		if (host.password_encrypt)
 			talloc_free(host.password_encrypt);
-		host.password_encrypt = strdup(zencrypt(host.password));
+		host.password_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.password));
 	}
 	if (host.enable) {
 		if (host.enable_encrypt)
 			talloc_free(host.enable_encrypt);
-		host.enable_encrypt = strdup(zencrypt(host.enable));
+		host.enable_encrypt = talloc_strdup(tall_vty_ctx, zencrypt(host.enable));
 	}
 
 	return CMD_SUCCESS;
@@ -3108,7 +3106,7 @@
 	if (host.logfile)
 		talloc_free(host.logfile);
 
-	host.logfile = strdup(fname);
+	host.logfile = talloc_strdup(tall_vty_ctx, fname);
 
 	return CMD_SUCCESS;
 }
@@ -3298,7 +3296,7 @@
 {
 	if (host.motdfile)
 		talloc_free(host.motdfile);
-	host.motdfile = strdup(argv[0]);
+	host.motdfile = talloc_strdup(tall_vty_ctx, argv[0]);
 
 	return CMD_SUCCESS;
 }
@@ -3326,7 +3324,7 @@
 /* Set config filename.  Called from vty.c */
 void host_config_set(char *filename)
 {
-	host.config = strdup(filename);
+	host.config = talloc_strdup(tall_vty_ctx, filename);
 }
 
 void install_default(enum node_type node)
@@ -3421,8 +3419,3 @@
 	}
 	srand(time(NULL));
 }
-
-static __attribute__((constructor)) void on_dso_load_vty_command(void)
-{
-	tall_vcmd_ctx = talloc_named_const(NULL, 1, "vty_command");
-}
diff --git a/openbsc/src/vty/vector.c b/openbsc/src/vty/vector.c
index c8885a4..371f71d 100644
--- a/openbsc/src/vty/vector.c
+++ b/openbsc/src/vty/vector.c
@@ -23,15 +23,14 @@
 #include <unistd.h>
 
 #include <vty/vector.h>
+#include <vty/vty.h>
 #include <openbsc/talloc.h>
 #include <memory.h>
 
-static void *tall_vvec_ctx;
-
 /* Initialize vector : allocate memory and return vector. */
 vector vector_init(unsigned int size)
 {
-	vector v = talloc_zero(tall_vvec_ctx, struct _vector);
+	vector v = talloc_zero(tall_vty_ctx, struct _vector);
 	if (!v)
 		return NULL;
 
@@ -41,7 +40,7 @@
 
 	v->alloced = size;
 	v->active = 0;
-	v->index = _talloc_zero(tall_vvec_ctx, sizeof(void *) * size,
+	v->index = _talloc_zero(tall_vty_ctx, sizeof(void *) * size,
 				"vector_init:index");
 	if (!v->index) {
 		talloc_free(v);
@@ -69,7 +68,7 @@
 vector vector_copy(vector v)
 {
 	unsigned int size;
-	vector new = talloc_zero(tall_vvec_ctx, struct _vector);
+	vector new = talloc_zero(tall_vty_ctx, struct _vector);
 	if (!new)
 		return NULL;
 
@@ -77,7 +76,7 @@
 	new->alloced = v->alloced;
 
 	size = sizeof(void *) * (v->alloced);
-	new->index = _talloc_zero(tall_vvec_ctx, size, "vector_copy:index");
+	new->index = _talloc_zero(tall_vty_ctx, size, "vector_copy:index");
 	if (!new->index) {
 		talloc_free(new);
 		return NULL;
@@ -93,7 +92,7 @@
 	if (v->alloced > num)
 		return;
 
-	v->index = talloc_realloc_size(tall_vvec_ctx, v->index,
+	v->index = talloc_realloc_size(tall_vty_ctx, v->index,
 				       sizeof(void *) * (v->alloced * 2));
 	memset(&v->index[v->alloced], 0, sizeof(void *) * v->alloced);
 	v->alloced *= 2;
@@ -189,8 +188,3 @@
 
 	return count;
 }
-
-static __attribute__((constructor)) void on_dso_load_vty_vec(void)
-{
-	tall_vvec_ctx = talloc_named_const(NULL, 1, "vty_vector");
-}
diff --git a/openbsc/src/vty/vty.c b/openbsc/src/vty/vty.c
index 0549914..5472fa0 100644
--- a/openbsc/src/vty/vty.c
+++ b/openbsc/src/vty/vty.c
@@ -33,7 +33,7 @@
 
 static int no_password_check = 1;
 
-static void *tall_vty_ctx;
+void *tall_vty_ctx;
 
 static void vty_clear_buf(struct vty *vty)
 {
@@ -1373,6 +1373,39 @@
 	return 0;
 }
 
+/* Read up configuration file */
+static int
+vty_read_file(FILE *confp)
+{
+	int ret;
+	struct vty *vty;
+
+	vty = vty_new();
+	vty->fd = 0;
+	vty->type = VTY_FILE;
+	vty->node = CONFIG_NODE;
+
+	ret = config_from_file(vty, confp);
+
+	if (ret != CMD_SUCCESS) {
+		switch (ret) {
+		case CMD_ERR_AMBIGUOUS:
+			fprintf(stderr, "Ambiguous command.\n");
+			break;
+		case CMD_ERR_NO_MATCH:
+			fprintf(stderr, "Ther is no such command.\n");
+			break;
+		}
+		fprintf(stderr, "Error occurred during reading below "
+			"line:\n%s\n", vty->buf);
+		vty_close(vty);
+		return -EINVAL;
+	}
+
+	vty_close(vty);
+	return 0;
+}
+
 /* Create new vty structure. */
 struct vty *
 vty_create (int vty_sock, void *priv)
@@ -1616,14 +1649,15 @@
 	vtyvec = vector_init(VECTOR_MIN_SIZE);
 }
 
+extern void *tall_bsc_ctx;
 /* Install vty's own commands like `who' command. */
 void vty_init()
 {
+	tall_vty_ctx = talloc_named_const(tall_bsc_ctx, 1, "vty");
+
 	/* For further configuration read, preserve current directory. */
 	vty_save_cwd();
 
-	host.config = "openbsc.cfg";
-
 	vtyvec = vector_init(VECTOR_MIN_SIZE);
 
 	/* Install bgp top node. */
@@ -1647,7 +1681,19 @@
 #endif
 }
 
-static __attribute__((constructor)) void on_dso_load_vty(void)
+int vty_read_config_file(const char *file_name)
 {
-	tall_vty_ctx = talloc_named_const(NULL, 1, "vty");
+	FILE *cfile;
+	int rc;
+
+	cfile = fopen(file_name, "r");
+	if (!cfile)
+		return -ENOENT;
+
+	rc = vty_read_file(cfile);
+	fclose(cfile);
+
+	host_config_set(file_name);
+
+	return rc;
 }
diff --git a/openbsc/src/vty_interface.c b/openbsc/src/vty_interface.c
index 00b1df2..45b2c74 100644
--- a/openbsc/src/vty_interface.c
+++ b/openbsc/src/vty_interface.c
@@ -33,9 +33,16 @@
 #include <openbsc/e1_input.h>
 #include <openbsc/abis_nm.h>
 #include <openbsc/db.h>
+#include <openbsc/talloc.h>
 
 static struct gsm_network *gsmnet;
 
+struct cmd_node net_node = {
+	GSMNET_NODE,
+	"%s(network)#",
+	1,
+};
+
 struct cmd_node bts_node = {
 	BTS_NODE,
 	"%s(bts)#",
@@ -159,10 +166,10 @@
 
 static void config_write_ts_single(struct vty *vty, struct gsm_bts_trx_ts *ts)
 {
-	vty_out(vty, "\t\tts %u%s", ts->nr, VTY_NEWLINE);
-	vty_out(vty, "\t\t\tphys_chan_config %s%s", gsm_pchan_name(ts->pchan),
+	vty_out(vty, "    ts %u%s", ts->nr, VTY_NEWLINE);
+	vty_out(vty, "     phys_chan_config %s%s", gsm_pchan_name(ts->pchan),
 		VTY_NEWLINE);
-	vty_out(vty, "\t\t\te1_subslot %u %u %u%s", ts->e1_link.e1_nr,
+	vty_out(vty, "     e1_subslot %u %u %u%s", ts->e1_link.e1_nr,
 		ts->e1_link.e1_ts, ts->e1_link.e1_ts_ss, VTY_NEWLINE);
 }
 
@@ -170,9 +177,9 @@
 {
 	int i;
 
-	vty_out(vty, "\ttrx %u%s", trx->nr, VTY_NEWLINE);
-	vty_out(vty, "\t\tarfcn %u%s", trx->arfcn, VTY_NEWLINE);
-	vty_out(vty, "\t\tmax_power_red %u%s", trx->max_power_red, VTY_NEWLINE);
+	vty_out(vty, "  trx %u%s", trx->nr, VTY_NEWLINE);
+	vty_out(vty, "   arfcn %u%s", trx->arfcn, VTY_NEWLINE);
+	vty_out(vty, "   max_power_red %u%s", trx->max_power_red, VTY_NEWLINE);
 
 	for (i = 0; i < TRX_NR_TS; i++)
 		config_write_ts_single(vty, &trx->ts[i]);
@@ -182,15 +189,15 @@
 {
 	struct gsm_bts_trx *trx;
 
-	vty_out(vty, "bts %u%s", bts->nr, VTY_NEWLINE);
-	vty_out(vty, "\ttype %s%s", btstype2str(bts->type), VTY_NEWLINE);
-	vty_out(vty, "\tband %s%s", gsm_band_name(bts->band), VTY_NEWLINE);
-	vty_out(vty, "\tlocation_area_code %u%s", bts->location_area_code,
+	vty_out(vty, " bts %u%s", bts->nr, VTY_NEWLINE);
+	vty_out(vty, "  type %s%s", btstype2str(bts->type), VTY_NEWLINE);
+	vty_out(vty, "  band %s%s", gsm_band_name(bts->band), VTY_NEWLINE);
+	vty_out(vty, "  location_area_code %u%s", bts->location_area_code,
 		VTY_NEWLINE);
-	vty_out(vty, "\ttraining_sequence_code %u%s", bts->tsc, VTY_NEWLINE);
-	vty_out(vty, "\tbase_station_id_code %u%s", bts->bsic, VTY_NEWLINE);
+	vty_out(vty, "  training_sequence_code %u%s", bts->tsc, VTY_NEWLINE);
+	vty_out(vty, "  base_station_id_code %u%s", bts->bsic, VTY_NEWLINE);
 	if (is_ipaccess_bts(bts))
-		vty_out(vty, "\tunit_id %u %u%s",
+		vty_out(vty, "  ip.access unit_id %u %u%s",
 			bts->ip_access.site_id, bts->ip_access.bts_id, VTY_NEWLINE);
 
 	llist_for_each_entry(trx, &bts->trx_list, list)
@@ -207,6 +214,16 @@
 	return CMD_SUCCESS;
 }
 
+static int config_write_net(struct vty *vty)
+{
+	vty_out(vty, "network%s", VTY_NEWLINE);
+	vty_out(vty, " country code %u%s", gsmnet->country_code, VTY_NEWLINE);
+	vty_out(vty, " mobile network code %u%s", gsmnet->network_code, VTY_NEWLINE);
+	vty_out(vty, " short name '%s'%s", gsmnet->name_short, VTY_NEWLINE);
+	vty_out(vty, " long name '%s'%s", gsmnet->name_long, VTY_NEWLINE);
+
+	return CMD_SUCCESS;
+}
 
 static void trx_dump_vty(struct vty *vty, struct gsm_bts_trx *trx)
 {
@@ -654,6 +671,63 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(cfg_net,
+      cfg_net_cmd,
+      "network",
+      "Configure the GSM network")
+{
+	vty->index = gsmnet;
+	vty->node = GSMNET_NODE;
+
+	return CMD_SUCCESS;
+}
+
+
+DEFUN(cfg_net_ncc,
+      cfg_net_ncc_cmd,
+      "network country code <1-999>",
+      "Set the GSM network country code")
+{
+	gsmnet->country_code = atoi(argv[0]);
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_net_mnc,
+      cfg_net_mnc_cmd,
+      "mobile network code <1-999>",
+      "Set the GSM mobile network code")
+{
+	gsmnet->network_code = atoi(argv[0]);
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_net_name_short,
+      cfg_net_name_short_cmd,
+      "short name NAME",
+      "Set the short GSM network name")
+{
+	if (gsmnet->name_short)
+		talloc_free(gsmnet->name_short);
+
+	gsmnet->name_short = talloc_strdup(gsmnet, argv[0]);
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_net_name_long,
+      cfg_net_name_long_cmd,
+      "long name NAME",
+      "Set the long GSM network name")
+{
+	if (gsmnet->name_long)
+		talloc_free(gsmnet->name_long);
+
+	gsmnet->name_long = talloc_strdup(gsmnet, argv[0]);
+
+	return CMD_SUCCESS;
+}
 
 /* per-BTS configuration */
 DEFUN(cfg_bts,
@@ -772,8 +846,8 @@
 
 DEFUN(cfg_bts_unit_id,
       cfg_bts_unit_id_cmd,
-      "unit_id <0-65534> <0-255>",
-      "Set the BTS Unit ID of this BTS\n")
+      "ip.access unit_id <0-65534> <0-255>",
+      "Set the ip.access BTS Unit ID of this BTS\n")
 {
 	struct gsm_bts *bts = vty->index;
 	int site_id = atoi(argv[0]);
@@ -789,6 +863,12 @@
 			bts_id, VTY_NEWLINE);
 		return CMD_WARNING;
 	}
+
+	if (!is_ipaccess_bts(bts)) {
+		vty_out(vty, "%% BTS is not of ip.access type%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
 	bts->ip_access.site_id = site_id;
 	bts->ip_access.bts_id = bts_id;
 
@@ -1075,7 +1155,14 @@
 	install_element(VIEW_NODE, &sms_send_imsi_cmd);
 #endif
 
-	install_element(CONFIG_NODE, &cfg_bts_cmd);
+	install_element(CONFIG_NODE, &cfg_net_cmd);
+	install_node(&net_node, config_write_net);
+	install_default(GSMNET_NODE);
+	install_element(GSMNET_NODE, &cfg_net_mnc_cmd);
+	install_element(GSMNET_NODE, &cfg_net_name_short_cmd);
+	install_element(GSMNET_NODE, &cfg_net_name_long_cmd);
+
+	install_element(GSMNET_NODE, &cfg_bts_cmd);
 	install_node(&bts_node, config_write_bts);
 	install_default(BTS_NODE);
 	install_element(BTS_NODE, &cfg_bts_type_cmd);