interface.c revision 1.13
1/*	$OpenBSD: interface.c,v 1.13 2013/06/01 18:35:02 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_ANY,	IF_EVT_DOWN,		IF_ACT_RST,	IF_STA_DOWN},
56    {-1,		IF_EVT_NOTHING,		IF_ACT_NOTHING,	0},
57};
58
59const char * const if_event_names[] = {
60	"NOTHING",
61	"UP",
62	"DOWN"
63};
64
65const char * const if_action_names[] = {
66	"NOTHING",
67	"START",
68	"RESET"
69};
70
71int
72if_fsm(struct iface *iface, enum iface_event event)
73{
74	int	old_state;
75	int	new_state = 0;
76	int	i, ret = 0;
77
78	old_state = iface->state;
79
80	for (i = 0; iface_fsm[i].state != -1; i++)
81		if ((iface_fsm[i].state & old_state) &&
82		    (iface_fsm[i].event == event)) {
83			new_state = iface_fsm[i].new_state;
84			break;
85		}
86
87	if (iface_fsm[i].state == -1) {
88		/* event outside of the defined fsm, ignore it. */
89		log_debug("if_fsm: interface %s, "
90		    "event %s not expected in state %s", iface->name,
91		    if_event_names[event], if_state_name(old_state));
92		return (0);
93	}
94
95	switch (iface_fsm[i].action) {
96	case IF_ACT_STRT:
97		ret = if_act_start(iface);
98		break;
99	case IF_ACT_RST:
100		ret = if_act_reset(iface);
101		break;
102	case IF_ACT_NOTHING:
103		/* do nothing */
104		break;
105	}
106
107	if (ret) {
108		log_debug("if_fsm: error changing state for interface %s, "
109		    "event %s, state %s", iface->name, if_event_names[event],
110		    if_state_name(old_state));
111		return (-1);
112	}
113
114	if (new_state != 0)
115		iface->state = new_state;
116
117	log_debug("if_fsm: event %s resulted in action %s and changing "
118	    "state for interface %s from %s to %s",
119	    if_event_names[event], if_action_names[iface_fsm[i].action],
120	    iface->name, if_state_name(old_state), if_state_name(iface->state));
121
122	return (ret);
123}
124
125struct iface *
126if_new(struct kif *kif, struct kif_addr *ka)
127{
128	struct iface		*iface;
129
130	if ((iface = calloc(1, sizeof(*iface))) == NULL)
131		err(1, "if_new: calloc");
132
133	iface->state = IF_STA_DOWN;
134
135	LIST_INIT(&iface->lde_nbr_list);
136
137	strlcpy(iface->name, kif->ifname, sizeof(iface->name));
138
139	/* get type */
140	if (kif->flags & IFF_POINTOPOINT)
141		iface->type = IF_TYPE_POINTOPOINT;
142	if (kif->flags & IFF_BROADCAST &&
143	    kif->flags & IFF_MULTICAST)
144		iface->type = IF_TYPE_BROADCAST;
145
146	/* get mtu, index and flags */
147	iface->mtu = kif->mtu;
148	iface->ifindex = kif->ifindex;
149	iface->flags = kif->flags;
150	iface->linkstate = kif->link_state;
151	iface->media_type = kif->media_type;
152	iface->baudrate = kif->baudrate;
153
154	/* set address, mask and p2p addr */
155	iface->addr = ka->addr;
156	iface->mask = ka->mask;
157	if (kif->flags & IFF_POINTOPOINT) {
158		iface->dst = ka->dstbrd;
159	}
160
161	return (iface);
162}
163
164void
165if_del(struct iface *iface)
166{
167	log_debug("if_del: interface %s", iface->name);
168
169	if (evtimer_pending(&iface->hello_timer, NULL))
170		evtimer_del(&iface->hello_timer);
171
172	free(iface);
173}
174
175void
176if_init(struct ldpd_conf *xconf, struct iface *iface)
177{
178	/* set event handlers for interface */
179	evtimer_set(&iface->hello_timer, if_hello_timer, iface);
180
181	iface->discovery_fd = xconf->ldp_discovery_socket;
182}
183
184/* timers */
185/* ARGSUSED */
186void
187if_hello_timer(int fd, short event, void *arg)
188{
189	struct iface *iface = arg;
190	struct timeval tv;
191
192	send_hello(iface);
193
194	/* reschedule hello_timer */
195	timerclear(&tv);
196	tv.tv_sec = iface->hello_interval;
197	if (evtimer_add(&iface->hello_timer, &tv) == -1)
198		fatal("if_hello_timer");
199}
200
201void
202if_start_hello_timer(struct iface *iface)
203{
204	struct timeval tv;
205
206	timerclear(&tv);
207	tv.tv_sec = iface->hello_interval;
208	if (evtimer_add(&iface->hello_timer, &tv) == -1)
209		fatal("if_start_hello_timer");
210}
211
212void
213if_stop_hello_timer(struct iface *iface)
214{
215	if (evtimer_del(&iface->hello_timer) == -1)
216		fatal("if_stop_hello_timer");
217}
218
219/* actions */
220int
221if_act_start(struct iface *iface)
222{
223	struct in_addr		 addr;
224	struct timeval		 now;
225
226	if (!((iface->flags & IFF_UP) &&
227	    LINK_STATE_IS_UP(iface->linkstate))) {
228		log_debug("if_act_start: interface %s link down",
229		    iface->name);
230		return (0);
231	}
232
233	gettimeofday(&now, NULL);
234	iface->uptime = now.tv_sec;
235
236	inet_aton(AllRouters, &addr);
237	if (if_join_group(iface, &addr))
238		return (-1);
239	iface->state = IF_STA_ACTIVE;
240
241	/* hello timer needs to be started in any case */
242	if_start_hello_timer(iface);
243	return (0);
244}
245
246int
247if_act_reset(struct iface *iface)
248{
249	struct in_addr		 addr;
250
251	if_stop_hello_timer(iface);
252
253	inet_aton(AllRouters, &addr);
254	if (if_leave_group(iface, &addr)) {
255		log_warnx("if_act_reset: error leaving group %s, "
256		    "interface %s", inet_ntoa(addr), iface->name);
257	}
258	return (0);
259}
260
261struct ctl_iface *
262if_to_ctl(struct iface *iface)
263{
264	static struct ctl_iface	 ictl;
265	struct timeval		 tv, now, res;
266
267	memcpy(ictl.name, iface->name, sizeof(ictl.name));
268	memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr));
269	memcpy(&ictl.mask, &iface->mask, sizeof(ictl.mask));
270	ictl.rtr_id.s_addr = ldpe_router_id();
271	ictl.ifindex = iface->ifindex;
272	ictl.state = iface->state;
273	ictl.mtu = iface->mtu;
274	ictl.baudrate = iface->baudrate;
275	ictl.holdtime = iface->holdtime;
276	ictl.hello_interval = iface->hello_interval;
277	ictl.flags = iface->flags;
278	ictl.type = iface->type;
279	ictl.linkstate = iface->linkstate;
280	ictl.mediatype = iface->media_type;
281	ictl.priority = iface->priority;
282
283	gettimeofday(&now, NULL);
284	if (evtimer_pending(&iface->hello_timer, &tv)) {
285		timersub(&tv, &now, &res);
286		ictl.hello_timer = res.tv_sec;
287	} else
288		ictl.hello_timer = -1;
289
290	if (iface->state != IF_STA_DOWN &&
291	    iface->uptime != 0) {
292		ictl.uptime = now.tv_sec - iface->uptime;
293	} else
294		ictl.uptime = 0;
295
296	return (&ictl);
297}
298
299/* misc */
300int
301if_set_mcast_ttl(int fd, u_int8_t ttl)
302{
303	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
304	    (char *)&ttl, sizeof(ttl)) < 0) {
305		log_warn("if_set_mcast_ttl: error setting "
306		    "IP_MULTICAST_TTL to %d", ttl);
307		return (-1);
308	}
309
310	return (0);
311}
312
313int
314if_set_tos(int fd, int tos)
315{
316	if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) {
317		log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos);
318		return (-1);
319	}
320
321	return (0);
322}
323
324int
325if_set_recvif(int fd, int enable)
326{
327	if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable,
328	    sizeof(enable)) < 0) {
329		log_warn("if_set_recvif: error setting IP_RECVIF");
330		return (-1);
331	}
332	return (0);
333}
334
335void
336if_set_recvbuf(int fd)
337{
338	int	bsize;
339
340	bsize = 65535;
341	while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize,
342	    sizeof(bsize)) == -1)
343		bsize /= 2;
344}
345
346int
347if_set_reuse(int fd, int enable)
348{
349	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable,
350	    sizeof(int)) < 0) {
351		log_warn("if_set_reuse: error setting SO_REUSEADDR");
352		return (-1);
353	}
354
355	return (0);
356}
357
358/*
359 * only one JOIN or DROP per interface and address is allowed so we need
360 * to keep track of what is added and removed.
361 */
362struct if_group_count {
363	LIST_ENTRY(if_group_count)	entry;
364	struct in_addr			addr;
365	unsigned int			ifindex;
366	int				count;
367};
368
369LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist);
370
371int
372if_join_group(struct iface *iface, struct in_addr *addr)
373{
374	struct ip_mreq		 mreq;
375	struct if_group_count	*ifg;
376
377	LIST_FOREACH(ifg, &ifglist, entry)
378		if (iface->ifindex == ifg->ifindex &&
379		    addr->s_addr == ifg->addr.s_addr)
380			break;
381	if (ifg == NULL) {
382		if ((ifg = calloc(1, sizeof(*ifg))) == NULL)
383			fatal("if_join_group");
384		ifg->addr.s_addr = addr->s_addr;
385		ifg->ifindex = iface->ifindex;
386		LIST_INSERT_HEAD(&ifglist, ifg, entry);
387	}
388
389	if (ifg->count++ != 0)
390		/* already joined */
391		return (0);
392
393	mreq.imr_multiaddr.s_addr = addr->s_addr;
394	mreq.imr_interface.s_addr = iface->addr.s_addr;
395
396	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
397	    IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
398		log_warn("if_join_group: error IP_ADD_MEMBERSHIP, "
399		    "interface %s address %s", iface->name,
400		    inet_ntoa(*addr));
401		return (-1);
402	}
403	return (0);
404}
405
406int
407if_leave_group(struct iface *iface, struct in_addr *addr)
408{
409	struct ip_mreq		 mreq;
410	struct if_group_count	*ifg;
411
412	LIST_FOREACH(ifg, &ifglist, entry)
413		if (iface->ifindex == ifg->ifindex &&
414		    addr->s_addr == ifg->addr.s_addr)
415			break;
416
417	/* if interface is not found just try to drop membership */
418	if (ifg && --ifg->count != 0)
419		/* others still joined */
420		return (0);
421
422	mreq.imr_multiaddr.s_addr = addr->s_addr;
423	mreq.imr_interface.s_addr = iface->addr.s_addr;
424
425	if (setsockopt(iface->discovery_fd, IPPROTO_IP,
426	    IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) {
427		log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, "
428		    "interface %s address %s", iface->name,
429		    inet_ntoa(*addr));
430		return (-1);
431	}
432
433	if (ifg) {
434		LIST_REMOVE(ifg, entry);
435		free(ifg);
436	}
437	return (0);
438}
439
440int
441if_set_mcast(struct iface *iface)
442{
443	if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF,
444	    &iface->addr.s_addr, sizeof(iface->addr.s_addr)) < 0) {
445		log_debug("if_set_mcast: error setting "
446		    "IP_MULTICAST_IF, interface %s", iface->name);
447		return (-1);
448	}
449
450	return (0);
451}
452
453int
454if_set_mcast_loop(int fd)
455{
456	u_int8_t	loop = 0;
457
458	if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
459	    (char *)&loop, sizeof(loop)) < 0) {
460		log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP");
461		return (-1);
462	}
463
464	return (0);
465}
466