]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/ipfilter/ipsend/sock.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / ipfilter / ipsend / sock.c
1 /* $FreeBSD$ */
2 /*
3  * sock.c (C) 1995-1998 Darren Reed
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  */
8 #if !defined(lint)
9 static const char sccsid[] = "@(#)sock.c        1.2 1/11/96 (C)1995 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: sock.c,v 2.8.4.7 2007/09/13 07:19:34 darrenr Exp $";
11 #endif
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/stat.h>
16 #if defined(__NetBSD__) && defined(__vax__)
17 /*
18  * XXX need to declare boolean_t for _KERNEL <sys/files.h>
19  * which ends up including <sys/device.h> for vax.  See PR#32907
20  * for further details.
21  */
22 typedef int     boolean_t;
23 #endif
24 #ifndef ultrix
25 #include <fcntl.h>
26 #endif
27 #if (__FreeBSD_version >= 300000)
28 # include <sys/dirent.h>
29 #else
30 # include <sys/dir.h>
31 #endif
32 #if !defined(__osf__)
33 # ifdef __NetBSD__ 
34 #  include <machine/lock.h>
35 # endif
36 # define _KERNEL
37 # define        KERNEL
38 # ifdef ultrix
39 #  undef        LOCORE
40 #  include <sys/smp_lock.h>
41 # endif
42 # include <sys/file.h>
43 # undef  _KERNEL
44 # undef  KERNEL
45 #endif
46 #include <nlist.h>
47 #include <sys/user.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/proc.h>
51 #if !defined(ultrix) && !defined(hpux) && !defined(__osf__)
52 # include <kvm.h>
53 #endif
54 #ifdef sun
55 #include <sys/systm.h>
56 #include <sys/session.h>
57 #endif
58 #if BSD >= 199103
59 #include <sys/sysctl.h>
60 #include <sys/filedesc.h>
61 #include <paths.h>
62 #endif
63 #include <math.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/tcp.h>
68 #include <net/if.h>
69 #if defined(__FreeBSD__)
70 # include "radix_ipf.h"
71 #endif
72 #ifndef __osf__
73 # include <net/route.h>
74 #endif
75 #include <netinet/ip_var.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <stdio.h>
80 #include <unistd.h>
81 #include <string.h>
82 #include <stdlib.h>
83 #include <stddef.h>
84 #include <pwd.h>
85 #include "ipsend.h"
86
87
88 int     nproc;
89 struct  proc    *proc;
90
91 #ifndef KMEM
92 # ifdef _PATH_KMEM
93 #  define       KMEM    _PATH_KMEM
94 # endif
95 #endif
96 #ifndef KERNEL
97 # ifdef _PATH_UNIX
98 #  define       KERNEL  _PATH_UNIX
99 # endif
100 #endif
101 #ifndef KMEM
102 # define        KMEM    "/dev/kmem"
103 #endif
104 #ifndef KERNEL
105 # define        KERNEL  "/vmunix"
106 #endif
107
108
109 #if BSD < 199103
110 static  struct  proc    *getproc __P((void));
111 #else
112 static  struct  kinfo_proc      *getproc __P((void));
113 #endif
114
115
116 int     kmemcpy(buf, pos, n)
117 char    *buf;
118 void    *pos;
119 int     n;
120 {
121         static  int     kfd = -1;
122         off_t   offset = (u_long)pos;
123
124         if (kfd == -1)
125                 kfd = open(KMEM, O_RDONLY);
126
127         if (lseek(kfd, offset, SEEK_SET) == -1)
128             {
129                 perror("lseek");
130                 return -1;
131             }
132         if (read(kfd, buf, n) == -1)
133             {
134                 perror("read");
135                 return -1;
136             }
137         return n;
138 }
139
140 struct  nlist   names[4] = {
141         { "_proc" },
142         { "_nproc" },
143 #ifdef  ultrix
144         { "_u" },
145 #else
146         { NULL },
147 #endif
148         { NULL }
149         };
150
151 #if BSD < 199103
152 static struct proc *getproc()
153 {
154         struct  proc    *p;
155         pid_t   pid = getpid();
156         int     siz, n;
157
158         n = nlist(KERNEL, names);
159         if (n != 0)
160             {
161                 fprintf(stderr, "nlist(%#x) == %d\n", names, n);
162                 return NULL;
163             }
164         if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
165             {
166                 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
167                 return NULL;
168             }
169         siz = nproc * sizeof(struct proc);
170         if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
171             {
172                 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
173                         names[0].n_value, &p, sizeof(p));
174                 return NULL;
175             }
176         proc = (struct proc *)malloc(siz);
177         if (KMCPY(proc, p, siz) == -1)
178             {
179                 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
180                         p, proc, siz);
181                 return NULL;
182             }
183
184         p = proc;
185
186         for (n = nproc; n; n--, p++)
187                 if (p->p_pid == pid)
188                         break;
189         if (!n)
190                 return NULL;
191
192         return p;
193 }
194
195
196 struct  tcpcb   *find_tcp(fd, ti)
197 int     fd;
198 struct  tcpiphdr *ti;
199 {
200         struct  tcpcb   *t;
201         struct  inpcb   *i;
202         struct  socket  *s;
203         struct  user    *up;
204         struct  proc    *p;
205         struct  file    *f, **o;
206
207         if (!(p = getproc()))
208                 return NULL;
209         up = (struct user *)malloc(sizeof(*up));
210 #ifndef ultrix
211         if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
212             {
213                 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
214                 return NULL;
215             }
216 #else
217         if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
218             {
219                 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
220                 return NULL;
221             }
222 #endif
223
224         o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
225         if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
226             {
227                 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
228                         up->u_ofile, o, sizeof(*o));
229                 return NULL;
230             }
231         f = (struct file *)calloc(1, sizeof(*f));
232         if (KMCPY(f, o[fd], sizeof(*f)) == -1)
233             {
234                 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
235                         up->u_ofile[fd], f, sizeof(*f));
236                 return NULL;
237             }
238
239         s = (struct socket *)calloc(1, sizeof(*s));
240         if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
241             {
242                 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
243                         o[fd], s, sizeof(*s));
244                 return NULL;
245             }
246
247         i = (struct inpcb *)calloc(1, sizeof(*i));
248         if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
249             {
250                 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
251                         s->so_pcb, i, sizeof(*i));
252                 return NULL;
253             }
254
255         t = (struct tcpcb *)calloc(1, sizeof(*t));
256         if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
257             {
258                 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
259                         i->inp_ppcb, t, sizeof(*t));
260                 return NULL;
261             }
262         return (struct tcpcb *)i->inp_ppcb;
263 }
264 #else
265 static struct kinfo_proc *getproc()
266 {
267         static  struct  kinfo_proc kp;
268         pid_t   pid = getpid();
269         int     mib[4];
270         size_t  n;
271
272         mib[0] = CTL_KERN;
273         mib[1] = KERN_PROC;
274         mib[2] = KERN_PROC_PID;
275         mib[3] = pid;
276
277         n = sizeof(kp);
278         if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
279             {
280                 perror("sysctl");
281                 return NULL;
282             }
283         return &kp;
284 }
285
286
287 struct  tcpcb   *find_tcp(tfd, ti)
288 int     tfd;
289 struct  tcpiphdr *ti;
290 {
291         struct  tcpcb   *t;
292         struct  inpcb   *i;
293         struct  socket  *s;
294         struct  filedesc        *fd;
295         struct  kinfo_proc      *p;
296         struct  file    *f, **o;
297
298         if (!(p = getproc()))
299                 return NULL;
300
301         fd = (struct filedesc *)malloc(sizeof(*fd));
302         if (fd == NULL)
303                 return NULL;
304 #if defined( __FreeBSD_version) && __FreeBSD_version >= 500013
305         if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1)
306             {
307                 fprintf(stderr, "read(%#lx,%#lx) failed\n",
308                         (u_long)p, (u_long)p->ki_fd);
309                 free(fd);
310                 return NULL;
311             }
312 #else
313         if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
314             {
315                 fprintf(stderr, "read(%#lx,%#lx) failed\n",
316                         (u_long)p, (u_long)p->kp_proc.p_fd);
317                 free(fd);
318                 return NULL;
319             }
320 #endif
321
322         o = NULL;
323         f = NULL;
324         s = NULL;
325         i = NULL;
326         t = NULL;
327
328         o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
329         if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
330             {
331                 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
332                         (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o));
333                 goto finderror;
334             }
335         f = (struct file *)calloc(1, sizeof(*f));
336         if (KMCPY(f, o[tfd], sizeof(*f)) == -1)
337             {
338                 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n",
339                         (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f));
340                 goto finderror;
341             }
342
343         s = (struct socket *)calloc(1, sizeof(*s));
344         if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
345             {
346                 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
347                         (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
348                 goto finderror;
349             }
350
351         i = (struct inpcb *)calloc(1, sizeof(*i));
352         if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
353             {
354                 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
355                         (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
356                 goto finderror;
357             }
358
359         t = (struct tcpcb *)calloc(1, sizeof(*t));
360         if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
361             {
362                 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
363                         (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
364                 goto finderror;
365             }
366         return (struct tcpcb *)i->inp_ppcb;
367
368 finderror:
369         if (o != NULL)
370                 free(o);
371         if (f != NULL)
372                 free(f);
373         if (s != NULL)
374                 free(s);
375         if (i != NULL)
376                 free(i);
377         if (t != NULL)
378                 free(t);
379         return NULL;
380 }
381 #endif /* BSD < 199301 */
382
383 int     do_socket(dev, mtu, ti, gwip)
384 char    *dev;
385 int     mtu;
386 struct  tcpiphdr *ti;
387 struct  in_addr gwip;
388 {
389         struct  sockaddr_in     rsin, lsin;
390         struct  tcpcb   *t, tcb;
391         int     fd, nfd;
392         socklen_t len;
393
394         printf("Dest. Port: %d\n", ti->ti_dport);
395
396         fd = socket(AF_INET, SOCK_STREAM, 0);
397         if (fd == -1)
398             {
399                 perror("socket");
400                 return -1;
401             }
402
403         if (fcntl(fd, F_SETFL, FNDELAY) == -1)
404             {
405                 perror("fcntl");
406                 return -1;
407             }
408
409         bzero((char *)&lsin, sizeof(lsin));
410         lsin.sin_family = AF_INET;
411         bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
412               sizeof(struct in_addr));
413         if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
414             {
415                 perror("bind");
416                 return -1;
417             }
418         len = sizeof(lsin);
419         (void) getsockname(fd, (struct sockaddr *)&lsin, &len);
420         ti->ti_sport = lsin.sin_port;
421         printf("sport %d\n", ntohs(lsin.sin_port));
422
423         nfd = initdevice(dev, 1);
424         if (nfd == -1)
425                 return -1;
426
427         if (!(t = find_tcp(fd, ti)))
428                 return -1;
429
430         bzero((char *)&rsin, sizeof(rsin));
431         rsin.sin_family = AF_INET;
432         bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
433               sizeof(struct in_addr));
434         rsin.sin_port = ti->ti_dport;
435         if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
436             errno != EINPROGRESS)
437             {
438                 perror("connect");
439                 return -1;
440             }
441         KMCPY(&tcb, t, sizeof(tcb));
442         ti->ti_win = tcb.rcv_adv;
443         ti->ti_seq = tcb.snd_nxt - 1;
444         ti->ti_ack = tcb.rcv_nxt;
445
446         if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
447                 return -1;
448         (void)write(fd, "Hello World\n", 12);
449         sleep(2);
450         close(fd);
451         return 0;
452 }