Debian Codegraph @

lib/osdep/freebsd_tap.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, 2008, Andrea Bittau <[email protected]>
3 *
4 * OS dependent API for FreeBSD. TAP routines
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 #include <net/if.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "aircrack-ng/defs.h"
38 #include "osdep.h"
39
40 struct tip_fbsd
41 {
42 int tf_fd;
43 int tf_ioctls;
44 struct ifreq tf_ifr;
45 char tf_name[IFNAMSIZ];
46 int tf_destroy;
47 };
48
49 static int ti_do_open_fbsd(struct tif * ti, char * name)
50 {
51 int fd;
52 char * iface = "/dev/tap";
53 struct stat st;
54 struct tip_fbsd * priv = ti_priv(ti);
55 int s;
56 unsigned int flags;
57 struct ifreq * ifr;
58
59 /* open tap */
60 if (name)
61 iface = name;
62 else
63 priv->tf_destroy = 1; /* we create, we destroy */
64
65 fd = open(iface, O_RDWR);
66 if (fd == -1) return -1;
67
68 /* get name */
69 if (fstat(fd, &st) == -1) goto err;
70 snprintf(priv->tf_name,
71 sizeof(priv->tf_name) - 1,
72 "%s",
73 devname(st.st_rdev, S_IFCHR));
74
75 /* bring iface up */
76 s = socket(PF_INET, SOCK_DGRAM, 0);
77 if (s == -1) goto err;
78 priv->tf_ioctls = s;
79
80 /* get flags */
81 ifr = &priv->tf_ifr;
82 memset(ifr, 0, sizeof(*ifr));
83 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), "%s", priv->tf_name);
84 if (ioctl(s, SIOCGIFFLAGS, ifr) == -1) goto err2;
85 flags = (ifr->ifr_flags & 0xffff) | (ifr->ifr_flagshigh << 16);
86
87 /* set flags */
88 flags |= IFF_UP;
89 ifr->ifr_flags = flags & 0xffff;
90 ifr->ifr_flagshigh = flags >> 16;
91 if (ioctl(s, SIOCSIFFLAGS, ifr) == -1) goto err2;
92
93 return fd;
94 err:
95 /* XXX destroy */
96 close(fd);
97 return -1;
98 err2:
99 close(s);
100 goto err;
101 }
102
103 static void ti_do_free(struct tif * ti)
104 {
105 struct tip_fbsd * priv = ti_priv(ti);
106
107 free(priv);
108 free(ti);
109 }
110
111 static void ti_destroy(struct tip_fbsd * priv)
112 {
113 ioctl(priv->tf_ioctls, SIOCIFDESTROY, &priv->tf_ifr);
114 }
115
116 static void ti_close_fbsd(struct tif * ti)
117 {
118 struct tip_fbsd * priv = ti_priv(ti);
119
120 if (priv->tf_destroy) ti_destroy(priv);
121
122 close(priv->tf_fd);
123 close(priv->tf_ioctls);
124 ti_do_free(ti);
125 }
126
127 static char * ti_name_fbsd(struct tif * ti)
128 {
129 struct tip_fbsd * priv = ti_priv(ti);
130
131 return priv->tf_name;
132 }
133
134 static int ti_set_mtu_fbsd(struct tif * ti, int mtu)
135 {
136 struct tip_fbsd * priv = ti_priv(ti);
137
138 priv->tf_ifr.ifr_mtu = mtu;
139
140 return ioctl(priv->tf_ioctls, SIOCSIFMTU, &priv->tf_ifr);
141 }
142
143 static int ti_set_mac_fbsd(struct tif * ti, unsigned char * mac)
144 {
145 struct tip_fbsd * priv = ti_priv(ti);
146 struct ifreq * ifr = &priv->tf_ifr;
147
148 ifr->ifr_addr.sa_family = AF_LINK;
149 ifr->ifr_addr.sa_len = 6;
150 memcpy(ifr->ifr_addr.sa_data, mac, 6);
151
152 return ioctl(priv->tf_ioctls, SIOCSIFLLADDR, ifr);
153 }
154
155 static int ti_set_ip_fbsd(struct tif * ti, struct in_addr * ip)
156 {
157 struct tip_fbsd * priv = ti_priv(ti);
158 struct ifaliasreq ifra;
159 struct sockaddr_in * s_in;
160
161 /* assume same size */
162 memset(&ifra, 0, sizeof(ifra));
163 strcpy(ifra.ifra_name, priv->tf_ifr.ifr_name);
164
165 s_in = (struct sockaddr_in *) &ifra.ifra_addr;
166 s_in->sin_family = PF_INET;
167 s_in->sin_addr = *ip;
168 s_in->sin_len = sizeof(*s_in);
169
170 return ioctl(priv->tf_ioctls, SIOCAIFADDR, &ifra);
171 }
172
173 static int ti_fd_fbsd(struct tif * ti)
174 {
175 struct tip_fbsd * priv = ti_priv(ti);
176
177 return priv->tf_fd;
178 }
179
180 static int ti_read_fbsd(struct tif * ti, void * buf, int len)
181 {
182 return read(ti_fd(ti), buf, len);
183 }
184
185 static int ti_write_fbsd(struct tif * ti, void * buf, int len)
186 {
187 return write(ti_fd(ti), buf, len);
188 }
189
190 static struct tif * ti_open_fbsd(char * iface)
191 {
192 struct tif * ti;
193 struct tip_fbsd * priv;
194 int fd;
195
196 /* setup ti struct */
197 ti = ti_alloc(sizeof(*priv));
198 if (!ti) return NULL;
199 ti->ti_name = ti_name_fbsd;
200 ti->ti_set_mtu = ti_set_mtu_fbsd;
201 ti->ti_close = ti_close_fbsd;
202 ti->ti_fd = ti_fd_fbsd;
203 ti->ti_read = ti_read_fbsd;
204 ti->ti_write = ti_write_fbsd;
205 ti->ti_set_mac = ti_set_mac_fbsd;
206 ti->ti_set_ip = ti_set_ip_fbsd;
207
208 /* setup iface */
209 fd = ti_do_open_fbsd(ti, iface);
210 if (fd == -1)
211 {
212 ti_do_free(ti);
213 return NULL;
214 }
215
216 /* setup private state */
217 priv = ti_priv(ti);
218 priv->tf_fd = fd;
219
220 return ti;
221 }
222
223 EXPORT struct tif * ti_open(char * iface) { return ti_open_fbsd(iface); }