2 * Copyright (C) 1993-2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 * kmemcpy() - copies n bytes from kernel memory into user buffer.
8 * returns 0 on success, -1 on error.
11 #if defined(__sgi) && (IRIX > 602)
12 # include <sys/ptimers.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/ip.h>
33 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
34 # include <net/if_var.h>
38 #include "netinet/ip_compat.h"
39 #include "netinet/ip_fil.h"
48 static const char sccsid
[] = "@(#)kmem.c 1.4 1/12/96 (C) 1992 Darren Reed";
49 static const char rcsid
[] = "@(#)$Id: kmem.c,v 2.2.2.18 2003/11/09 17:22:22 darrenr Exp $";
55 static int kvm_fd
= -1;
56 static char *kvm_errstr
= NULL
;
58 kvm_t
*kvm_open(kernel
, core
, swap
, mode
, errstr
)
59 char *kernel
, *core
, *swap
;
67 kvm_fd
= open(core
, mode
);
68 return (kvm_fd
>= 0) ? (kvm_t
*)&kvm_fd
: NULL
;
71 int kvm_read(kvm
, pos
, buffer
, size
)
81 if (lseek(*kvm
, pos
, 0) == -1) {
82 if (kvm_errstr
!= NULL
) {
83 fprintf(stderr
, "%s:", kvm_errstr
);
89 for (bufp
= buffer
, left
= size
; left
> 0; bufp
+= r
, left
-= r
) {
90 r
= read(*kvm
, bufp
, 1);
98 static kvm_t
*kvm_f
= NULL
;
100 int openkmem(kern
, core
)
108 kvm_f
= kvm_open(kern
, core
, NULL
, O_RDONLY
, NULL
);
111 perror("openkmem:open");
118 int kmemcpy(buf
, pos
, n
)
129 if (openkmem(NULL
, NULL
) == -1)
132 while ((r
= kvm_read(kvm_f
, pos
, buf
, (size_t)n
)) < n
)
135 fprintf(stderr
, "pos=0x%x ", (u_int
)pos
);
136 perror("kmemcpy:read");
148 int kstrncpy(buf
, pos
, n
)
159 if (openkmem(NULL
, NULL
) == -1)
164 r
= kvm_read(kvm_f
, pos
, buf
, (size_t)1);
167 fprintf(stderr
, "pos=0x%x ", (u_int
)pos
);
168 perror("kstrncpy:read");
185 * Given a pointer to an interface in the kernel, return a pointer to a
186 * string which is the interface name.
195 if (ptr
== (void *)-1)
200 if (kmemcpy((char *)&ill
, (u_long
)ptr
, sizeof(ill
)) == -1)
202 ifname
= malloc(ill
.ill_name_length
+ 1);
203 if (kmemcpy(ifname
, (u_long
)ill
.ill_name
,
204 ill
.ill_name_length
) == -1)
208 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
216 if (ptr
== (void *)-1)
221 if (kmemcpy((char *)&netif
, (u_long
)ptr
, sizeof(netif
)) == -1)
223 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
224 defined(__OpenBSD__) || defined(__DragonFly__)
225 return strdup(netif
.if_xname
);
227 if (kstrncpy(buf
, (u_long
)netif
.if_name
, sizeof(buf
)) == -1)
229 if (netif
.if_unit
< 10)
231 else if (netif
.if_unit
< 1000)
233 else if (netif
.if_unit
< 10000)
237 buf
[sizeof(buf
) - len
] = '\0';
238 sprintf(buf
+ strlen(buf
), "%d", netif
.if_unit
% 10000);