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