summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.lib/wpad/l2_packet.c
blob: 30a91c522029e0239f8957bd083b8cca8466af58 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
 * Sun elects to license this software under the BSD license.
 * See README for more details.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libdlpi.h>
#include <sys/ethernet.h>
#include <netinet/in.h>

#include "wpa_impl.h"
#include "eloop.h"
#include "l2_packet.h"

static int
link_init(struct l2_packet_data *l2)
{
	int retval;
	uint8_t paddr[DLPI_PHYSADDR_MAX];
	size_t paddrlen = sizeof (paddr);

	retval = dlpi_bind(l2->dh, DLPI_ANY_SAP, NULL);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "cannot bind on %s: %s",
		    l2->ifname, dlpi_strerror(retval));
		return (-1);
	}

	retval = dlpi_promiscon(l2->dh, DL_PROMISC_SAP);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "cannot enable promiscous"
		    " mode (SAP) on %s: %s",
		    l2->ifname, dlpi_strerror(retval));
		return (-1);
	}

	retval = dlpi_get_physaddr(l2->dh, DL_CURR_PHYS_ADDR, paddr, &paddrlen);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "cannot get physical address for %s: %s",
		    l2->ifname, dlpi_strerror(retval));
		return (-1);
	}
	if (paddrlen != sizeof (l2->own_addr)) {
		wpa_printf(MSG_ERROR, "physical address for %s is not %d bytes",
		    l2->ifname, sizeof (l2->own_addr));
		return (-1);
	}
	(void) memcpy(l2->own_addr, paddr, sizeof (l2->own_addr));

	return (0);
}

/*
 * layer2 packet handling.
 */
int
l2_packet_get_own_addr(struct l2_packet_data *l2, uint8_t *addr)
{
	(void) memcpy(addr, l2->own_addr, sizeof (l2->own_addr));
	return (0);
}

int
l2_packet_send(struct l2_packet_data *l2, uint8_t *buf, size_t buflen)
{
	int retval;

	retval = dlpi_send(l2->dh, NULL, 0, buf, buflen, NULL);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "l2_packet_send: cannot send "
		    "message on %s: %s", l2->ifname, dlpi_strerror(retval));
		return (-1);
	}
	return (0);
}

/* ARGSUSED */
static void
l2_packet_receive(int fd, void *eloop_ctx, void *sock_ctx)
{
	struct l2_packet_data *l2 = eloop_ctx;
	uint64_t buf[IEEE80211_MTU_MAX / sizeof (uint64_t)];
	size_t buflen = sizeof (buf);
	struct l2_ethhdr *ethhdr;
	int retval;

	retval = dlpi_recv(l2->dh, NULL, NULL, buf, &buflen, 0, NULL);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "l2_packet_receive: cannot receive "
		    "message on %s: %s", l2->ifname, dlpi_strerror(retval));
		return;
	}

	ethhdr = (struct l2_ethhdr *)buf;
	if (buflen < sizeof (*ethhdr) ||
	    (ntohs(ethhdr->h_proto) != ETHERTYPE_EAPOL &&
	    ntohs(ethhdr->h_proto) != ETHERTYPE_RSN_PREAUTH))
		return;

	l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source,
	    (unsigned char *)(ethhdr + 1), buflen - sizeof (*ethhdr));
}

/* ARGSUSED */
struct l2_packet_data *
l2_packet_init(const char *ifname, unsigned short protocol,
	void (*rx_callback)(void *, unsigned char *, unsigned char *, size_t),
	void *rx_callback_ctx)
{
	int retval;
	struct l2_packet_data *l2;

	l2 = calloc(1, sizeof (struct l2_packet_data));
	if (l2 == NULL)
		return (NULL);

	(void) strlcpy(l2->ifname, ifname, sizeof (l2->ifname));
	l2->rx_callback = rx_callback;
	l2->rx_callback_ctx = rx_callback_ctx;

	retval = dlpi_open(l2->ifname, &l2->dh, DLPI_RAW);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "unable to open DLPI link %s: %s",
		    l2->ifname, dlpi_strerror(retval));
		free(l2);
		return (NULL);
	}

	/* NOTE: link_init() sets l2->own_addr */
	if (link_init(l2) < 0) {
		dlpi_close(l2->dh);
		free(l2);
		return (NULL);
	}

	(void) eloop_register_read_sock(dlpi_fd(l2->dh), l2_packet_receive, l2,
	    NULL);

	return (l2);
}

void
l2_packet_deinit(struct l2_packet_data *l2)
{
	if (l2 == NULL)
		return;

	eloop_unregister_read_sock(dlpi_fd(l2->dh));
	dlpi_close(l2->dh);
	free(l2);
}