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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* $Id: libnet_link_linux.c,v 1.5 2004/01/03 20:31:02 mike Exp $
*
* libnet 1.1
* libnet_link_linux.c - linux packet socket and pack socket routines
*
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include <sys/time.h>
#include <net/if.h>
#if (__GLIBC__)
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#else
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#endif
#if (HAVE_PACKET_SOCKET)
#ifndef SOL_PACKET
#define SOL_PACKET 263
#endif /* SOL_PACKET */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#endif
#endif /* HAVE_PACKET_SOCKET */
#include "../include/libnet.h"
/* These should not vary across linux systems, and are only defined in
* <pcap-bpf.h>, included from <pcap.h>, but since we have no other dependency
* on libpcap right now, define locally. I'm not sure if this is a good idea,
* but we'll try.
*/
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */
#define DLT_FDDI 10 /* FDDI */
#define DLT_RAW 12 /* raw IP */
#include "../include/gnuc.h"
#ifdef HAVE_OS_PROTO_H
#include "../include/os-proto.h"
#endif
int
libnet_open_link(libnet_t *l)
{
struct ifreq ifr;
int n = 1;
if (l == NULL)
{
return (-1);
}
#if (HAVE_PACKET_SOCKET)
l->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
#else
l->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
#endif
if (l->fd == -1)
{
if (errno == EPERM) {
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s(): UID/EUID 0 or capability CAP_NET_RAW required",
__func__);
} else {
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"socket: %s", strerror(errno));
}
goto bad;
}
memset(&ifr, 0, sizeof (ifr));
strncpy(ifr.ifr_name, l->device, sizeof (ifr.ifr_name) -1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (ioctl(l->fd, SIOCGIFHWADDR, &ifr) < 0 )
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"SIOCGIFHWADDR: %s", strerror(errno));
goto bad;
}
switch (ifr.ifr_hwaddr.sa_family)
{
case ARPHRD_ETHER:
case ARPHRD_METRICOM:
#ifdef ARPHRD_LOOPBACK
case ARPHRD_LOOPBACK:
#endif
l->link_type = DLT_EN10MB;
l->link_offset = 0xe;
break;
case ARPHRD_SLIP:
case ARPHRD_CSLIP:
case ARPHRD_SLIP6:
case ARPHRD_CSLIP6:
case ARPHRD_PPP:
l->link_type = DLT_RAW;
break;
case ARPHRD_FDDI:
l->link_type = DLT_FDDI;
l->link_offset = 0x15;
break;
/* Token Ring */
case ARPHRD_IEEE802:
case ARPHRD_IEEE802_TR:
case ARPHRD_PRONET:
l->link_type = DLT_PRONET;
l->link_offset = 0x16;
break;
default:
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"unknown physical layer type 0x%x\n",
ifr.ifr_hwaddr.sa_family);
goto bad;
}
#ifdef SO_BROADCAST
/*
* man 7 socket
*
* Set or get the broadcast flag. When enabled, datagram sockets
* receive packets sent to a broadcast address and they are allowed
* to send packets to a broadcast address. This option has no
* effect on stream-oriented sockets.
*/
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s: set SO_BROADCAST failed: %s\n",
__func__, strerror(errno));
goto bad;
}
#endif /* SO_BROADCAST */
return (1);
bad:
if (l->fd >= 0)
{
close(l->fd);
}
return (-1);
}
int
libnet_close_link(libnet_t *l)
{
if (close(l->fd) == 0)
{
return (1);
}
else
{
return (-1);
}
}
#if (HAVE_PACKET_SOCKET)
static int
get_iface_index(int fd, const char *device)
{
struct ifreq ifr;
/* memset(&ifr, 0, sizeof(ifr)); */
strncpy (ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1)
{
return (-1);
}
return ifr.ifr_ifindex;
}
#endif
int
libnet_write_link(libnet_t *l, const uint8_t *packet, uint32_t size)
{
int c;
#if (HAVE_PACKET_SOCKET)
struct sockaddr_ll sa;
#else
struct sockaddr sa;
#endif
if (l == NULL)
{
return (-1);
}
memset(&sa, 0, sizeof (sa));
#if (HAVE_PACKET_SOCKET)
sa.sll_family = AF_PACKET;
sa.sll_ifindex = get_iface_index(l->fd, l->device);
if (sa.sll_ifindex == -1)
{
return (-1);
}
sa.sll_protocol = htons(ETH_P_ALL);
#else
strncpy(sa.sa_data, l->device, sizeof (sa.sa_data) - 1);
sa.sa_data[sizeof (sa.sa_data) - 1] = 0;
#endif
c = sendto(l->fd, packet, size, 0,
(struct sockaddr *)&sa, sizeof (sa));
if (c != size)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"libnet_write_link(): only %d bytes written (%s)\n", c,
strerror(errno));
}
return (c);
}
struct libnet_ether_addr *
libnet_get_hwaddr(libnet_t *l)
{
int fd;
struct ifreq ifr;
struct libnet_ether_addr *eap;
/*
* XXX - non-re-entrant!
*/
static struct libnet_ether_addr ea;
if (l == NULL)
{
return (NULL);
}
if (l->device == NULL)
{
if (libnet_select_device(l) == -1)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"libnet_get_hwaddr: can't figure out a device to use\n");
return (NULL);
}
}
/*
* Create dummy socket to perform an ioctl upon.
*/
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"socket: %s", strerror(errno));
goto bad;
}
memset(&ifr, 0, sizeof(ifr));
eap = &ea;
strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0)
{
close(fd);
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"ioctl: %s", strerror(errno));
goto bad;
}
memcpy(eap, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
close(fd);
return (eap);
bad:
return (NULL);
}
/* ---- Emacs Variables ----
* Local Variables:
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
/* EOF */
|