summaryrefslogtreecommitdiff
path: root/usr.sbin/bgpd/config.c
blob: e4ae55b9f4e83f6c5d009bf3c1f1e99bdb7f1809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*	$OpenBSD: config.c,v 1.13 2004/01/02 22:47:33 itojun Exp $ */

/*
 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>

#include <errno.h>
#include <ifaddrs.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "bgpd.h"

void			*sconf;

u_int32_t	get_bgpid(void);
u_int32_t	get_id(struct peer *);

int
merge_config(struct bgpd_config *xconf, struct bgpd_config *conf)
{
	enum reconf_action	 reconf = RECONF_NONE;
	struct peer		*p, *next;

	/* merge conf (new) into xconf (old)  */
	if (!conf->as) {
		logit(LOG_CRIT, "configuration error: AS not given");
		return (1);
	}
	if (xconf->as != conf->as) {
		xconf->as = conf->as;
		reconf = RECONF_REINIT;
	}
	if (conf->bgpid && xconf->bgpid != conf->bgpid) {
		xconf->bgpid = conf->bgpid;
		reconf = RECONF_REINIT;
	}
	if (!xconf->bgpid)
		xconf->bgpid = get_bgpid();

	if (conf->holdtime && !xconf->holdtime)
		xconf->holdtime = conf->holdtime;
	if (!conf->holdtime && xconf->holdtime)
		conf->holdtime = xconf->holdtime;

	if (conf->min_holdtime && !xconf->min_holdtime)
		xconf->min_holdtime = conf->min_holdtime;
	if (!conf->min_holdtime && xconf->min_holdtime)
		conf->min_holdtime = xconf->min_holdtime;
	if (!xconf->min_holdtime)
		xconf->min_holdtime = conf->min_holdtime = MIN_HOLDTIME;

	memcpy(&xconf->listen_addr, &conf->listen_addr,
	    sizeof(xconf->listen_addr));

	if ((xconf->flags & BGPD_FLAG_NO_FIB_UPDATE) !=
	    (conf->flags & BGPD_FLAG_NO_FIB_UPDATE)) {
		if (!(conf->flags & BGPD_FLAG_NO_FIB_UPDATE))
			kroute_fib_couple();
		else
			kroute_fib_decouple();
	}

	xconf->flags = conf->flags;
	xconf->log = conf->log;

	/*
	 * as we cannot get the negotiated holdtime in the main process,
	 * the session engine needs to check it against the possibly new values
	 * and decide on session reestablishment.
	 */

	xconf->holdtime = conf->holdtime;
	xconf->min_holdtime = conf->min_holdtime;

	for (p = conf->peers; p != NULL; p = p->next) {
		p->conf.reconf_action = reconf;
		p->conf.ebgp = (p->conf.remote_as != xconf->as);
		if (!p->conf.id)
			p->conf.id = get_id(p);
	}

	for (p = xconf->peers; p != NULL; p = next) {
		next = p->next;
		free(p);
	}

	/* merge peers done by session engine except for initial config */
	xconf->peers = conf->peers;

	return (0);
}

u_int32_t
get_bgpid(void)
{
	struct ifaddrs		*ifap, *ifa;
	u_int32_t		 ip = 0, cur, localnet;

	localnet = inet_addr("127.0.0.0");

	if (getifaddrs(&ifap) == -1)
		fatal("getifaddrs");

	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr->sa_family != AF_INET)
			continue;
		cur = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
		if ((cur & localnet) == localnet)	/* skip 127/8 */
			continue;
		if (cur > ip)
			ip = cur;
	}
	freeifaddrs(ifap);

	return (ip);
}

u_int32_t
get_id(struct peer *p)
{
	/*
	 * XXX this collides with multiviews and will need more clue later XXX
	 */
	return (ntohl(p->conf.remote_addr.sin_addr.s_addr));
}