4 * lsock.c (C) 1995-1998 Darren Reed
6 * See the IPFILTER.LICENCE file for details on licencing.
10 static const char sccsid
[] = "@(#)lsock.c 1.2 1/11/96 (C)1995 Darren Reed";
11 static const char rcsid
[] = "@(#)Id: lsock.c,v 2.3.4.1 2006/03/17 13:45:34 darrenr Exp";
19 #include <sys/types.h>
21 #include <sys/param.h>
31 # include <linux/notifier.h>
35 #include "linux/netdevice.h"
39 #include <linux/sched.h>
40 #include <linux/netdevice.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
49 #include <net/inet/sock.h>
54 struct task_struct
*proc
;
58 # define KMEM _PATH_KMEM
62 # define KMEM "/dev/kmem"
65 # define KERNEL "/System.map"
68 int kmemcpy(buf
, pos
, n
)
76 kfd
= open(KMEM
, O_RDONLY
);
78 if (lseek(kfd
, (off_t
)pos
, SEEK_SET
) == -1)
83 if (read(kfd
, buf
, n
) == -1)
91 struct nlist names
[3] = {
97 struct task_struct
*getproc()
99 struct task_struct
*p
, **pp
;
101 pid_t pid
= getpid();
104 n
= nlist(KERNEL
, names
);
107 fprintf(stderr
, "nlist(%#x) == %d\n", names
, n
);
110 if (KMCPY(&nproc
, names
[1].n_value
, sizeof(nproc
)) == -1)
112 fprintf(stderr
, "read nproc (%#x)\n", names
[1].n_value
);
115 siz
= nproc
* sizeof(struct task_struct
*);
116 if (KMCPY(&v
, names
[0].n_value
, sizeof(v
)) == -1)
118 fprintf(stderr
, "read(%#x,%#x,%d) proc\n",
119 names
[0].n_value
, &v
, sizeof(v
));
122 pp
= (struct task_struct
**)malloc(siz
);
123 if (KMCPY(pp
, v
, siz
) == -1)
125 fprintf(stderr
, "read(%#x,%#x,%d) proc\n",
129 proc
= (struct task_struct
*)malloc(siz
);
130 for (n
= 0; n
< NR_TASKS
; n
++)
132 if (KMCPY((proc
+ n
), pp
[n
], sizeof(*proc
)) == -1)
134 fprintf(stderr
, "read(%#x,%#x,%d) proc\n",
135 pp
[n
], proc
+ n
, sizeof(*proc
));
142 for (n
= NR_TASKS
; n
; n
--, p
++)
152 struct sock
*find_tcp(fd
, ti
)
158 struct files_struct
*fs
;
159 struct task_struct
*p
;
162 if (!(p
= getproc()))
166 o
= (struct file
**)calloc(1, sizeof(*o
) * (fs
->count
+ 1));
167 if (KMCPY(o
, fs
->fd
, (fs
->count
+ 1) * sizeof(*o
)) == -1)
169 fprintf(stderr
, "read(%#x,%#x,%d) - fd - failed\n",
170 fs
->fd
, o
, sizeof(*o
));
173 f
= (struct file
*)calloc(1, sizeof(*f
));
174 if (KMCPY(f
, o
[fd
], sizeof(*f
)) == -1)
176 fprintf(stderr
, "read(%#x,%#x,%d) - o[fd] - failed\n",
177 o
[fd
], f
, sizeof(*f
));
181 i
= (struct inode
*)calloc(1, sizeof(*i
));
182 if (KMCPY(i
, f
->f_inode
, sizeof(*i
)) == -1)
184 fprintf(stderr
, "read(%#x,%#x,%d) - f_inode - failed\n",
185 f
->f_inode
, i
, sizeof(*i
));
188 return i
->u
.socket_i
.data
;
191 int do_socket(dev
, mtu
, ti
, gwip
)
197 struct sockaddr_in rsin
, lsin
;
201 printf("Dest. Port: %d\n", ti
->ti_dport
);
203 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
210 if (fcntl(fd
, F_SETFL
, FNDELAY
) == -1)
216 bzero((char *)&lsin
, sizeof(lsin
));
217 lsin
.sin_family
= AF_INET
;
218 bcopy((char *)&ti
->ti_src
, (char *)&lsin
.sin_addr
,
219 sizeof(struct in_addr
));
220 if (bind(fd
, (struct sockaddr
*)&lsin
, sizeof(lsin
)) == -1)
226 (void) getsockname(fd
, (struct sockaddr
*)&lsin
, &len
);
227 ti
->ti_sport
= lsin
.sin_port
;
228 printf("sport %d\n", ntohs(lsin
.sin_port
));
229 nfd
= initdevice(dev
, 0);
233 if (!(s
= find_tcp(fd
, ti
)))
236 bzero((char *)&rsin
, sizeof(rsin
));
237 rsin
.sin_family
= AF_INET
;
238 bcopy((char *)&ti
->ti_dst
, (char *)&rsin
.sin_addr
,
239 sizeof(struct in_addr
));
240 rsin
.sin_port
= ti
->ti_dport
;
241 if (connect(fd
, (struct sockaddr
*)&rsin
, sizeof(rsin
)) == -1 &&
242 errno
!= EINPROGRESS
)
247 KMCPY(&sk
, s
, sizeof(sk
));
248 ti
->ti_win
= sk
.window
;
249 ti
->ti_seq
= sk
.sent_seq
- 1;
250 ti
->ti_ack
= sk
.rcv_ack_seq
;
251 ti
->ti_flags
= TH_SYN
;
253 if (send_tcp(nfd
, mtu
, (ip_t
*)ti
, gwip
) == -1)
255 (void)write(fd
, "Hello World\n", 12);