Move <machine/in_cksum.h> to <sys/in_cksum.h>. This file is now platform
[dragonfly.git] / contrib / ipfilter / kmem.c
1 /*
2  * Copyright (C) 1993-2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 /*
7  * kmemcpy() - copies n bytes from kernel memory into user buffer.
8  * returns 0 on success, -1 on error.
9  */
10
11 #if defined(__sgi) && (IRIX > 602)
12 # include <sys/ptimers.h>
13 #endif
14 #include <stdio.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <sys/file.h>
22 #ifndef __sgi
23 #include <kvm.h>
24 #endif
25 #include <fcntl.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>
32 #include <net/if.h>
33 #if __FreeBSD_version >= 300000
34 # include <net/if_var.h>
35 #endif
36
37 #include "kmem.h"
38 #include "netinet/ip_compat.h"
39 #include "netinet/ip_fil.h"
40 #include "ipf.h"
41
42
43 #ifndef __STDC__
44 # define        const
45 #endif
46
47 #if !defined(lint)
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.16 2002/12/06 11:40:27 darrenr Exp $";
50 #endif
51
52 #ifdef  __sgi
53 typedef int     kvm_t;
54
55 static  int     kvm_fd = -1;
56 static  char    *kvm_errstr;
57
58 kvm_t *kvm_open(kernel, core, swap, mode, errstr)
59 char *kernel, *core, *swap;
60 int mode;
61 char *errstr;
62 {
63         kvm_errstr = errstr;
64
65         if (core == NULL)
66                 core = "/dev/kmem";
67         kvm_fd = open(core, mode);
68         return (kvm_fd >= 0) ? (kvm_t *)&kvm_fd : NULL;
69 }
70
71 int kvm_read(kvm, pos, buffer, size)
72 kvm_t *kvm;
73 u_long pos;
74 char *buffer;
75 size_t size;
76 {
77         size_t left;
78         char *bufp;
79         int r;
80
81         if (lseek(*kvm, pos, 0) == -1) {
82                 fprintf(stderr, "%s", kvm_errstr);
83                 perror("lseek");
84                 return -1;
85         }
86
87         for (bufp = buffer, left = size; left > 0; bufp += r, left -= r) {
88                 r = read(*kvm, bufp, 1);
89                 if (r <= 0)
90                         return -1;
91         }
92         return size;
93 }
94 #endif
95
96 static  kvm_t   *kvm_f = NULL;
97
98 int     openkmem(kern, core)
99 char    *kern, *core;
100 {
101         union {
102                 int ui;
103                 kvm_t *uk;
104         } k;
105
106         kvm_f = kvm_open(kern, core, NULL, O_RDONLY, "");
107         if (kvm_f == NULL)
108             {
109                 perror("openkmem:open");
110                 return -1;
111             }
112         k.uk = kvm_f;
113         return k.ui;
114 }
115
116 int     kmemcpy(buf, pos, n)
117 register char   *buf;
118 long    pos;
119 register int    n;
120 {
121         register int    r;
122
123         if (!n)
124                 return 0;
125
126         if (kvm_f == NULL)
127                 if (openkmem(NULL, NULL) == -1)
128                         return -1;
129
130         while ((r = kvm_read(kvm_f, pos, buf, (size_t)n)) < n)
131                 if (r <= 0)
132                     {
133                         fprintf(stderr, "pos=0x%x ", (u_int)pos);
134                         perror("kmemcpy:read");
135                         return -1;
136                     }
137                 else
138                     {
139                         buf += r;
140                         pos += r;
141                         n -= r;
142                     }
143         return 0;
144 }
145
146 int     kstrncpy(buf, pos, n)
147 register char   *buf;
148 long    pos;
149 register int    n;
150 {
151         register int    r;
152
153         if (!n)
154                 return 0;
155
156         if (kvm_f == NULL)
157                 if (openkmem(NULL, NULL) == -1)
158                         return -1;
159
160         while (n > 0)
161             {
162                 r = kvm_read(kvm_f, pos, buf, (size_t)1);
163                 if (r <= 0)
164                     {
165                         fprintf(stderr, "pos=0x%x ", (u_int)pos);
166                         perror("kstrncpy:read");
167                         return -1;
168                     }
169                 else
170                     {
171                         if (*buf == '\0')
172                                 break;
173                         buf++;
174                         pos++;
175                         n--;
176                     }
177             }
178         return 0;
179 }
180
181
182 /*
183  * Given a pointer to an interface in the kernel, return a pointer to a
184  * string which is the interface name.
185  */
186 char *getifname(ptr)
187 void *ptr;
188 {
189 #if SOLARIS
190         char *ifname;
191         ill_t ill;
192
193         if (ptr == (void *)-1)
194                 return "!";
195         if (ptr == NULL)
196                 return "-";
197
198         if (kmemcpy((char *)&ill, (u_long)ptr, sizeof(ill)) == -1)
199                 return "X";
200         ifname = malloc(ill.ill_name_length + 1);
201         if (kmemcpy(ifname, (u_long)ill.ill_name,
202                     ill.ill_name_length) == -1)
203                 return "X";
204         return ifname;
205 #else
206 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
207     defined(__OpenBSD__)
208 #else
209         char buf[32];
210         int len;
211 # endif
212         struct ifnet netif;
213
214         if (ptr == (void *)-1)
215                 return "!";
216         if (ptr == NULL)
217                 return "-";
218
219         if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
220                 return "X";
221 # if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
222     defined(__OpenBSD__) || defined(__DragonFly__)
223         return strdup(netif.if_xname);
224 # else
225         if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
226                 return "X";
227         if (netif.if_unit < 10)
228                 len = 2;
229         else if (netif.if_unit < 1000)
230                 len = 3;
231         else if (netif.if_unit < 10000)
232                 len = 4;
233         else
234                 len = 5;
235         buf[sizeof(buf) - len] = '\0';
236         sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
237         return strdup(buf);
238 # endif
239 #endif
240 }