summaryrefslogtreecommitdiff
path: root/usr.sbin/ospfd/auth.c
blob: a15643c415cb561b4fb847ca0f519a0c2f3d13cc (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
/*	$OpenBSD: auth.c,v 1.2 2005/01/28 17:53:33 norby Exp $ */

/*
 * Copyright (c) 2004, 2005 Esben Norby <norby@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 <string.h>

#include "ospfd.h"
#include "ospf.h"
#include "log.h"
#include "ospfe.h"

int
auth_validate(struct ospf_hdr *pkt, const struct iface *iface)
{
	if (ntohs(pkt->auth_type) != (u_int16_t)iface->auth_type) {
		log_debug("auth_validate: wrong auth type, interface %s",
		    iface->name);
		return (-1);
	}

	switch (iface->auth_type) {
	case AUTH_NONE:
		break;
	case AUTH_SIMPLE:
		if (bcmp(pkt->auth_key.simple, iface->auth_key,
		    sizeof(pkt->auth_key.simple))) {
			log_debug("auth_validate: wrong password, interface %s",
			    iface->name);
			return (-1);
		}
		/* clear the key before chksum */
		bzero(pkt->auth_key.simple,
		     sizeof(pkt->auth_key.simple));
		break;
	case AUTH_CRYPT:
		log_debug("auth_validate: not supported, interface %s",
		    iface->name);
		return (-1);
	default:
		log_debug("auth_validate: unknown auth type, interface %s",
		    iface->name);
		return (-1);
	}

	if (in_cksum(pkt, ntohs(pkt->len))) {
		log_debug("recv_packet: invalid checksum, interface %s",
		    iface->name);
		return (-1);
	}

	return (0);
}

int
auth_gen(void *buf, u_int16_t len, const struct iface *iface)
{
	struct ospf_hdr	*ospf_hdr = buf;

	/* update length, and checksum */
	ospf_hdr->len = htons(len);
	ospf_hdr->chksum = in_cksum(buf, len);

	switch (iface->auth_type) {
	case AUTH_NONE:
		break;
	case AUTH_SIMPLE:
		strncpy(ospf_hdr->auth_key.simple, iface->auth_key,
		    sizeof(ospf_hdr->auth_key.simple));
		break;
	case AUTH_CRYPT:
		log_debug("auth_gen: not supported, interface %s",
		    iface->name);
		return (-1);
	default:
		log_debug("auth_gen: unknown auth type, interface %s",
		    iface->name);
		return (-1);
	}

	return (0);
}