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