interface.c revision 1.11
1/*	$OpenBSD: interface.c,v 1.11 2013/05/30 16:22:52 claudio Exp $ */
2
3/*
4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include <sys/time.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <net/if.h>
27#include <net/if_types.h>
28#include <fcntl.h>
29#include <ctype.h>
30#include <err.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <string.h>
35#include <event.h>
36
37#include "ldpd.h"
38#include "ldp.h"
39#include "log.h"
40#include "ldpe.h"
41
42void		 if_hello_timer(int, short, void *);
43void		 if_start_hello_timer(struct iface *);
44void		 if_stop_hello_timer(struct iface *);
45struct nbr	*if_elect(struct nbr *, struct nbr *);
46
47struct {
48	int			state;
49	enum iface_event	event;
50	enum iface_action	action;
51	int			new_state;
52} iface_fsm[] = {
53    /* current state	event that happened	action to take	resulting state */
54    {IF_STA_DOWN,	IF_EVT_UP,		IF_ACT_STRT,	0},
55    {IF_STA_LOOPBACK,	IF_EVT_DOWN,		IF_ACT_NOTHING,	IF_STA_DOWN},
56    {IF_STA_ANY,	IF_EVT_DOWN,		IF_ACT_RST,	IF_STA_DOWN},
57    {-1,		IF_EVT_NOTHING,		IF_ACT_NOTHING,	0},
58};
59
60const char * const if_event_names[] = {
61	"NOTHING",
62	"UP",
63	"DOWN"
64};
65
66const char * const if_action_names[] = {
67	"NOTHING",
68	"START",
69	"RESET"
70};
71
72int
73if_fsm(struct iface *iface, enum iface_event event)
74{
75	int	old_state;
76	int	new_state = 0;
77	int	i, ret = 0;
78
79	old_state = iface->state;
80
81	for (i = 0; iface_fsm[i].state != -1; i++)
82		if ((iface_fsm[i].state & old_state) &&
83		    (iface_fsm[i].event == event)) {
84			new_state = iface_fsm[i].new_state;
85			break;
86		}
87
88	if (iface_fsm[i].state == -1) {
89		/* event outside of the defined fsm, ignore it. */
90		log_debug("if_fsm: interface %s, "
91		    "event %s not expected in state %s", iface->name,
92		    if_event_names[event], if_state_name(old_state));
93		return (0);
94	}
95
96	switch (iface_fsm[i].action) {
97	case IF_ACT_STRT:
98		ret = if_act_start(iface);
99		break;
100	case IF_ACT_RST:
101		ret = if_act_reset(iface);
102		break;
103	case IF_ACT_NOTHING:
104		/* do nothing */
105		break;
106	}
107
108	if (ret) {
109		log_debug("if_fsm: error changing state for interface %s, "
110		    "event %s, state %s", iface->name, if_event_names[event],
111		    if_state_name(old_state));
112		return (-1);
113	}
114
115	if (new_state != 0)
116		iface->state = new_state;
117
118	log_debug("if_fsm: event %s resulted in action %s and changing "
119	    "state for interface %s from %s to %s",
120	    if_event_names[event], if_action_names[iface_fsm[i].action],
121	    iface->name, if_state_name(old_state), if_state_name(iface->state));
122
123	return (ret);
124}
125
126struct iface *
127if_new(struct kif *kif, struct kif_addr *ka)
128{
129	struct iface		*iface;
130
131	if ((iface = calloc(1, sizeof(*iface))) == NULL)
132		err(1, "if_new: calloc");
133
134	iface->state = IF_STA_DOWN;
135
136	LIST_INIT(&iface->lde_nbr_list);
137
138	strlcpy(iface->name, kif->ifname, sizeof(iface->name));
139
140	/* get type */
141	if (kif->flags & IFF_POINTOPOINT)
142		iface->type = IF_TYPE_POINTOPOINT;
143	if (kif->flags & IFF_BROADCAST &&
144	    kif->flags & IFF_MULTICAST)
145		iface->type = IF_TYPE_BROADCAST;
146	if (kif->flags & IFF_LOOPBACK) {
147		iface->type = IF_TYPE_POINTOPOINT;
148		iface->state = IF_STA_LOOPBACK;
149	}
150
151	/* get mtu, index and flags */
152	iface->mtu = kif->mtu;
153	iface->ifindex = kif->ifindex;
154	iface->flags = kif->flags;
155	iface->linkstate = kif->link_state;
156	iface->media_type = kif->media_type;
157	iface->baudrate = kif->baudrate;
158
159	/* set address, mask and p2p addr */
160	iface->addr = ka->addr;
161	iface->mask = ka->mask;
162	if (kif->flags & IFF_POINTOPOINT) {
163		iface->dst = ka->dstbrd;
164	}
165
166	return (iface);
167}
168
169void
170if_del(struct iface *iface)
171{
172	log_debug("if_del: interface %s", iface->name);
173
174	if (evtimer_pending(&iface->hello_timer, NULL))
175		evtimer_del(&iface->hello_timer);
176
177	free(iface);
178}
179
180void
181if_init(struct ldpd_conf *xconf, struct iface *iface)
182{
183	/* set event handlers for interface */
184	evtimer_set(&iface->hello_timer, if_hello_timer, iface);
185
186	iface->discovery_fd = xconf->ldp_discovery_socket;
187}
188
189/* timers */
190/* ARGSUSED */
191void
192if_hello_timer(int fd, short event, void *arg)
193{
194	struct iface *iface = arg;
195	struct timeval tv;
196
197	send_hello(iface);
198
199	/* reschedule hello_timer */
200	timerclear(&tv);
201	tv.tv_sec = iface->hello_interval;
202	if (evtimer_add(&iface->hello_timer, &tv) == -1)
203		fatal("if_hello_timer");
204}
205
206void
207if_start_hello_timer(struct iface *iface)
208{
209	struct timeval tv;
210
211	timerclear(&tv);
212	tv.tv_sec = iface->hello_interval;
213	if (evtimer_add(&iface->hello_timer, &tv) == -1)
214		fatal("if_start_hello_timer");
215}
216
217void
218if_stop_hello_timer(struct iface *iface)
219{
220	if (evtimer_del(&iface->hello_timer) == -1)
221		fatal("if_stop_hello_timer");
222}
223
224/* actions */
225int
226if_act_start(struct iface *iface)
227{
228	struct in_addr		 addr;
229	struct timeval		 now;
230
231	if (!((iface->flags & IFF_UP) &&
232	    LINK_STATE_IS_UP(iface->linkstate))) {
233		log_debug("if_act_start: interface %s link down",
234		    iface->name);
235		return (0);
236	}
237
238	if (iface->media_type == IFT_CARP && iface->passive == 0) {
239		/* force passive mode on carp interfaces */
240		log_warnx("if_act_start: forcing interface %s to passive",
241		    iface->name);
242		iface->passive = 1;
243	}
244
245	gettimeofday(&now, NULL);
246	iface->uptime = now.tv_sec;
247
248	inet_aton(AllRouters, &addr);
249	if (if_join_group(iface, &addr))
250		return (-1);
251	iface->state = IF_STA_ACTIVE;
252
253	/* hello timer needs to be started in any case */
254	if_start_hello_timer(iface);
255	return (0);
256}
257
258int
259if_act_reset(struct iface *iface)
260{
261	struct in_addr		 addr;
262
263	if_stop_hello_timer(iface);
264
265	inet_aton(AllRouters, &addr);
266	if (if_leave_group(iface, &addr)) {
267		log_warnx("if_act_reset: error leaving group %s, "
268		    "interface %s", inet_ntoa(addr), iface->name);
269	}
270	return (0);
271}
272
273struct ctl_iface *
274if_to_ctl(struct iface *iface)
275{
276	static struct ctl_iface	 ictl;
277	struct timeval		 tv, now, res;
278
279	memcpy(ictl.name, iface->name, sizeof(ictl.name));
280	memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr));
281	memcpy(&ictl.mask, &iface->mask, sizeof(ictl.mask));
282	ictl.rtr_id.s_addr = ldpe_router_id();
283	ictl.ifindex = iface->ifindex;
284	ictl.state = iface->state;
285	ictl.mtu = iface->mtu;
286	ictl.baudrate = iface->baudrate;
287	ictl.holdtime = iface->holdtime;
288	ictl.hello_interval = iface->hello_interval;
289	ictl.flags = iface->flags;
290	ictl.type = iface->type;
291	ictl.linkstate = iface->linkstate;
292	ictl.mediatype = iface->media_type;
293	ictl.priority = iface->priority;
294	ictl.passive = iface->passive;
295
296	gettimeofday(&now, NULL);
297	if (evtimer_pending(&iface->hello_timer, &tv)) {
298		timersub(&tv, &now, &res);
299		ictl.hello_timer = res.tv_sec;
300	} else
301		ictl.hello_timer = -1;
302
303	if (iface->state != IF_STA_DOWN &&
304	    iface->uptime != 0) {
305		ictl.uptime = now.tv_sec - iface->uptime;
306	} else
307		ictl.uptime = 0;
308
309	return (&ictl);
310}
311
312/* misc */
313int
314if_set_mcast_ttl(int fd, u_int8_t ttl)
315{
316	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
317	    (char *)&ttl, sizeof(ttl)) < 0) {
318		log_warn("if_set_mcast_ttl: error setting "
319		    "IP_MULTICAST_TTL to %d", ttl);
320		return (-1);
321	}
322
323	return (0);
324}
325
326int
327if_set_tos(int fd, int tos)
328{
329	if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) {
330		log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos);
331		return (-1);
332	}
333
334	return (0);
335}
336
337int
338if_set_recvif(int fd, int enable)
339{
340	if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable,
341	    sizeof(enable)) < 0) {
342		log_warn("if_set_recvif: error setting IP_RECVIF");
343		return (-1);
344	}
345	return (0);
346}
347
348void
349if_set_recvbuf(int fd)
350{
351	int	bsize;
352
353	bsize = 65535;
354	while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize,
355	    sizeof(bsize)) == -1)
356		bsize /= 2;
357}
358
359int
360if_set_reuse(int fd, int enable)
361{
362	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable,
363	    sizeof(int)) < 0) {
364		log_warn("if_set_reuse: error setting SO_REUSEADDR");
365		return (-1);
366	}
367
368	return (0);
369}
370
371/*
372 * only one JOIN or DROP per interface and address is allowed so we need
373 * to keep track of what is added and removed.
374 */
375struct if_group_count {
376	LIST_ENTRY(if_group_count)	entry;
377	struct in_addr			addr;
378	unsigned int			ifindex;
379	int				count;
380};
381
382LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist);
383
384int
385if_join_group(struct iface *iface, struct in_addr *addr)
386{
387	struct ip_mreq		 mreq;
388	struct if_group_count	*ifg;
389
390	LIST_FOREACH(ifg, &ifglist, entry)
391		if (iface->ifindex == ifg->ifindex &&
392		    addr->s_addr == ifg->addr.s_addr)
393			break;
394	if (ifg == NULL) {
395		if ((ifg = calloc(1, sizeof(*ifg))) == NULL)
396			fatal("if_join_group");
397		ifg->addr.s_addr = addr->s_addr;
398		ifg->ifindex = iface->ifindex;
399		LIST_INSERT_HEAD(&ifglist, ifg, entry);
400	}
401
402	if (ifg->count++ != 0)
403		/* already joined */
404		return (0);
405
406	mreq.imr_multiaddr.s_addr = addr->s_addr;
407	mreq.imr_interface.s_addr = iface->addr.s_addr;
408
409	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
410	    IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
411		log_warn("if_join_group: error IP_ADD_MEMBERSHIP, "
412		    "interface %s address %s", iface->name,
413		    inet_ntoa(*addr));
414		return (-1);
415	}
416	return (0);
417}
418
419int
420if_leave_group(struct iface *iface, struct in_addr *addr)
421{
422	struct ip_mreq		 mreq;
423	struct if_group_count	*ifg;
424
425	LIST_FOREACH(ifg, &ifglist, entry)
426		if (iface->ifindex == ifg->ifindex &&
427		    addr->s_addr == ifg->addr.s_addr)
428			break;
429
430	/* if interface is not found just try to drop membership */
431	if (ifg && --ifg->count != 0)
432		/* others still joined */
433		return (0);
434
435	mreq.imr_multiaddr.s_addr = addr->s_addr;
436	mreq.imr_interface.s_addr = iface->addr.s_addr;
437
438	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
439	    IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
440		log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, "
441		    "interface %s address %s", iface->name,
442		    inet_ntoa(*addr));
443		return (-1);
444	}
445
446	if (ifg) {
447		LIST_REMOVE(ifg, entry);
448		free(ifg);
449	}
450	return (0);
451}
452
453int
454if_set_mcast(struct iface *iface)
455{
456	if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF,
457	    &iface->addr.s_addr, sizeof(iface->addr.s_addr)) < 0) {
458		log_debug("if_set_mcast: error setting "
459		    "IP_MULTICAST_IF, interface %s", iface->name);
460		return (-1);
461	}
462
463	return (0);
464}
465
466int
467if_set_mcast_loop(int fd)
468{
469	u_int8_t	loop = 0;
470
471	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
472	    (char *)&loop, sizeof(loop)) < 0) {
473		log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP");
474		return (-1);
475	}
476
477	return (0);
478}
479