interface.c revision 1.8
1/*	$OpenBSD: interface.c,v 1.8 2011/07/04 04:34:14 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,	IF_STA_ACTIVE},
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_DOWN;
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	inet_aton(AllRouters, &addr);
264	if (if_leave_group(iface, &addr)) {
265		log_warnx("if_act_reset: error leaving group %s, "
266		    "interface %s", inet_ntoa(addr), iface->name);
267	}
268	return (0);
269}
270
271struct ctl_iface *
272if_to_ctl(struct iface *iface)
273{
274	static struct ctl_iface	 ictl;
275	struct timeval		 tv, now, res;
276
277	memcpy(ictl.name, iface->name, sizeof(ictl.name));
278	memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr));
279	memcpy(&ictl.mask, &iface->mask, sizeof(ictl.mask));
280	ictl.rtr_id.s_addr = ldpe_router_id();
281	ictl.ifindex = iface->ifindex;
282	ictl.state = iface->state;
283	ictl.mtu = iface->mtu;
284	ictl.baudrate = iface->baudrate;
285	ictl.holdtime = iface->holdtime;
286	ictl.hello_interval = iface->hello_interval;
287	ictl.flags = iface->flags;
288	ictl.type = iface->type;
289	ictl.linkstate = iface->linkstate;
290	ictl.mediatype = iface->media_type;
291	ictl.priority = iface->priority;
292	ictl.passive = iface->passive;
293
294	gettimeofday(&now, NULL);
295	if (evtimer_pending(&iface->hello_timer, &tv)) {
296		timersub(&tv, &now, &res);
297		ictl.hello_timer = res.tv_sec;
298	} else
299		ictl.hello_timer = -1;
300
301	if (iface->state != IF_STA_DOWN) {
302		ictl.uptime = now.tv_sec - iface->uptime;
303	} else
304		ictl.uptime = 0;
305
306	return (&ictl);
307}
308
309/* misc */
310int
311if_set_mcast_ttl(int fd, u_int8_t ttl)
312{
313	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
314	    (char *)&ttl, sizeof(ttl)) < 0) {
315		log_warn("if_set_mcast_ttl: error setting "
316		    "IP_MULTICAST_TTL to %d", ttl);
317		return (-1);
318	}
319
320	return (0);
321}
322
323int
324if_set_tos(int fd, int tos)
325{
326	if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) {
327		log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos);
328		return (-1);
329	}
330
331	return (0);
332}
333
334int
335if_set_recvif(int fd, int enable)
336{
337	if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable,
338	    sizeof(enable)) < 0) {
339		log_warn("if_set_recvif: error setting IP_RECVIF");
340		return (-1);
341	}
342	return (0);
343}
344
345void
346if_set_recvbuf(int fd)
347{
348	int	bsize;
349
350	bsize = 65535;
351	while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize,
352	    sizeof(bsize)) == -1)
353		bsize /= 2;
354}
355
356int
357if_set_reuse(int fd, int enable)
358{
359	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable,
360	    sizeof(int)) < 0) {
361		log_warn("if_set_reuse: error setting SO_REUSEADDR");
362		return (-1);
363	}
364
365	return (0);
366}
367
368/*
369 * only one JOIN or DROP per interface and address is allowed so we need
370 * to keep track of what is added and removed.
371 */
372struct if_group_count {
373	LIST_ENTRY(if_group_count)	entry;
374	struct in_addr			addr;
375	unsigned int			ifindex;
376	int				count;
377};
378
379LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist);
380
381int
382if_join_group(struct iface *iface, struct in_addr *addr)
383{
384	struct ip_mreq		 mreq;
385	struct if_group_count	*ifg;
386
387	LIST_FOREACH(ifg, &ifglist, entry)
388		if (iface->ifindex == ifg->ifindex &&
389		    addr->s_addr == ifg->addr.s_addr)
390			break;
391	if (ifg == NULL) {
392		if ((ifg = calloc(1, sizeof(*ifg))) == NULL)
393			fatal("if_join_group");
394		ifg->addr.s_addr = addr->s_addr;
395		ifg->ifindex = iface->ifindex;
396		LIST_INSERT_HEAD(&ifglist, ifg, entry);
397	}
398
399	if (ifg->count++ != 0)
400		/* already joined */
401		return (0);
402
403	mreq.imr_multiaddr.s_addr = addr->s_addr;
404	mreq.imr_interface.s_addr = iface->addr.s_addr;
405
406	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
407	    IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
408		log_warn("if_join_group: error IP_ADD_MEMBERSHIP, "
409		    "interface %s address %s", iface->name,
410		    inet_ntoa(*addr));
411		return (-1);
412	}
413	return (0);
414}
415
416int
417if_leave_group(struct iface *iface, struct in_addr *addr)
418{
419	struct ip_mreq		 mreq;
420	struct if_group_count	*ifg;
421
422	LIST_FOREACH(ifg, &ifglist, entry)
423		if (iface->ifindex == ifg->ifindex &&
424		    addr->s_addr == ifg->addr.s_addr)
425			break;
426
427	/* if interface is not found just try to drop membership */
428	if (ifg && --ifg->count != 0)
429		/* others still joined */
430		return (0);
431
432	mreq.imr_multiaddr.s_addr = addr->s_addr;
433	mreq.imr_interface.s_addr = iface->addr.s_addr;
434
435	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
436	    IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
437		log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, "
438		    "interface %s address %s", iface->name,
439		    inet_ntoa(*addr));
440		return (-1);
441	}
442
443	if (ifg) {
444		LIST_REMOVE(ifg, entry);
445		free(ifg);
446	}
447	return (0);
448}
449
450int
451if_set_mcast(struct iface *iface)
452{
453	if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF,
454	    &iface->addr.s_addr, sizeof(iface->addr.s_addr)) < 0) {
455		log_debug("if_set_mcast: error setting "
456		    "IP_MULTICAST_IF, interface %s", iface->name);
457		return (-1);
458	}
459
460	return (0);
461}
462
463int
464if_set_mcast_loop(int fd)
465{
466	u_int8_t	loop = 0;
467
468	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
469	    (char *)&loop, sizeof(loop)) < 0) {
470		log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP");
471		return (-1);
472	}
473
474	return (0);
475}
476