Debian Codegraph @

lib/osdep/darwin_tap.c

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