summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ppp/arp.c
blob: 490afd4cbe66cf7034da088ab266dd55a7ef8534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * sys-bsd.c - System-dependent procedures for setting up
 * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
 *
 * Copyright (c) 1989 Carnegie Mellon University.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by Carnegie Mellon University.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Id: arp.c,v 1.6 1997/01/03 20:19:42 wollman Exp $
 *
 */

/*
 * TODO:
 */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <unistd.h>
#include <string.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <fcntl.h>
#ifdef __bsdi__
#include <kvm.h>
#endif
#include <net/if_types.h>
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
#include "log.h"

#if RTM_VERSION >= 3
#include <netinet/if_ether.h>
#endif

static int rtm_seq;

static int get_ether_addr __P((int, u_long, struct sockaddr_dl *));

#define BCOPY(s, d, l)		memcpy(d, s, l)
#define BZERO(s, n)		memset(s, 0, n)
/*
 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
 * if it exists.
 */
#define SET_SA_FAMILY(addr, family)		\
    BZERO((char *) &(addr), sizeof(addr));	\
    addr.sa_family = (family); 			\
    addr.sa_len = sizeof(addr);


#if RTM_VERSION >= 3

/*
 * sifproxyarp - Make a proxy ARP entry for the peer.
 */
static struct {
    struct rt_msghdr		hdr;
    struct sockaddr_inarp	dst;
    struct sockaddr_dl		hwa;
    char			extra[128];
} arpmsg;

static int arpmsg_valid;

int
sifproxyarp(unit, hisaddr)
    int unit;
    u_long hisaddr;
{
    int routes;

    /*
     * Get the hardware address of an interface on the same subnet
     * as our local address.
     */
    memset(&arpmsg, 0, sizeof(arpmsg));
    if (!get_ether_addr(unit, hisaddr, &arpmsg.hwa)) {
	logprintf("Cannot determine ethernet address for proxy ARP\n");
	return 0;
    }

    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
	logprintf("sifproxyarp: opening routing socket: \n");
	return 0;
    }

    arpmsg.hdr.rtm_type = RTM_ADD;
    arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
    arpmsg.hdr.rtm_version = RTM_VERSION;
    arpmsg.hdr.rtm_seq = ++rtm_seq;
    arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
    arpmsg.hdr.rtm_inits = RTV_EXPIRE;
    arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
    arpmsg.dst.sin_family = AF_INET;
    arpmsg.dst.sin_addr.s_addr = hisaddr;
    arpmsg.dst.sin_other = SIN_PROXY;

    arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
	+ arpmsg.hwa.sdl_len;
    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
	logprintf("add proxy arp entry: \n");
	close(routes);
	return 0;
    }

    close(routes);
    arpmsg_valid = 1;
    return 1;
}

/*
 * cifproxyarp - Delete the proxy ARP entry for the peer.
 */
int
cifproxyarp(unit, hisaddr)
    int unit;
    u_long hisaddr;
{
    int routes;

    if (!arpmsg_valid)
	return 0;
    arpmsg_valid = 0;

    arpmsg.hdr.rtm_type = RTM_DELETE;
    arpmsg.hdr.rtm_seq = ++rtm_seq;

    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
	logprintf("sifproxyarp: opening routing socket: \n");
	return 0;
    }

    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
	logprintf("delete proxy arp entry: \n");
	close(routes);
	return 0;
    }

    close(routes);
    return 1;
}

#else	/* RTM_VERSION */

/*
 * sifproxyarp - Make a proxy ARP entry for the peer.
 */
int
sifproxyarp(unit, hisaddr)
    int unit;
    u_long hisaddr;
{
    struct arpreq arpreq;
    struct {
	struct sockaddr_dl	sdl;
	char			space[128];
    } dls;

    BZERO(&arpreq, sizeof(arpreq));

    /*
     * Get the hardware address of an interface on the same subnet
     * as our local address.
     */
    if (!get_ether_addr(unit, hisaddr, &dls.sdl)) {
	LogPrintf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n");
	return 0;
    }

    arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
    arpreq.arp_ha.sa_family = AF_UNSPEC;
    BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
    arpreq.arp_flags = ATF_PERM | ATF_PUBL;
    if (ioctl(unit, SIOCSARP, (caddr_t)&arpreq) < 0) {
	fprintf(stderr, "ioctl(SIOCSARP): \n");
	return 0;
    }

    return 1;
}

/*
 * cifproxyarp - Delete the proxy ARP entry for the peer.
 */
int
cifproxyarp(unit, hisaddr)
    int unit;
    u_long hisaddr;
{
    struct arpreq arpreq;

    BZERO(&arpreq, sizeof(arpreq));
    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
    if (ioctl(unit, SIOCDARP, (caddr_t)&arpreq) < 0) {
	fprintf(stderr, "ioctl(SIOCDARP): \n");
	return 0;
    }
    return 1;
}
#endif	/* RTM_VERSION */


/*
 * get_ether_addr - get the hardware address of an interface on the
 * the same subnet as ipaddr.
 */
#define MAX_IFS		32

int
get_ether_addr(s, ipaddr, hwaddr)
    int    s;
    u_long ipaddr;
    struct sockaddr_dl *hwaddr;
{
    struct ifreq *ifr, *ifend, *ifp;
    u_long ina, mask;
    struct sockaddr_dl *dla;
    struct ifreq ifreq;
    struct ifconf ifc;
    struct ifreq ifs[MAX_IFS];

    ifc.ifc_len = sizeof(ifs);
    ifc.ifc_req = ifs;
    if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
	fprintf(stderr, "ioctl(SIOCGIFCONF): \n");
	return 0;
    }

    /*
     * Scan through looking for an interface with an Internet
     * address on the same subnet as `ipaddr'.
     */
    ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
    for (ifr = ifc.ifc_req; ifr < ifend; ) {
	if (ifr->ifr_addr.sa_family == AF_INET) {
	    ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
	    strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
	    ifreq.ifr_name[sizeof(ifreq.ifr_name)-1]='\0';
	    /*
	     * Check that the interface is up, and not point-to-point
	     * or loopback.
	     */
	    if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
		continue;
	    if ((ifreq.ifr_flags &
		 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
		 != (IFF_UP|IFF_BROADCAST))
		goto nextif;
	    /*
	     * Get its netmask and check that it's on the right subnet.
	     */
	    if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
		continue;
	    mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
	    if ((ipaddr & mask) != (ina & mask))
		goto nextif;

	    break;
	}
nextif:
	ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
    }

    if (ifr >= ifend)
	return 0;
    LogPrintf(LOG_PHASE_BIT, "found interface %s for proxy arp\n", ifr->ifr_name);

    /*
     * Now scan through again looking for a link-level address
     * for this interface.
     */
    ifp = ifr;
    for (ifr = ifc.ifc_req; ifr < ifend; ) {
	if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
	    && ifr->ifr_addr.sa_family == AF_LINK) {
	    /*
	     * Found the link-level address - copy it out
	     */
	    dla = (struct sockaddr_dl *) &ifr->ifr_addr;
#ifdef __bsdi__
	    if (dla->sdl_alen == 0)
	      kmemgetether(ifr->ifr_name, dla);
#endif
	    BCOPY(dla, hwaddr, dla->sdl_len);
	    return 1;
	}
	ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
    }

    return 0;
}

#ifdef __bsdi__
#include <nlist.h>

struct nlist nl[] = {
#define N_IFNET		0
	{ "_ifnet" },
	"",
};


kvm_t *kvmd;

/*
 * Read kernel memory, return 0 on success.
 */
int
kread(addr, buf, size)
  u_long addr;
  char *buf;
  int size;
{

	if (kvm_read(kvmd, addr, buf, size) != size) {
		/* XXX this duplicates kvm_read's error printout */
		(void)fprintf(stderr, "kvm_read %s\n",
			kvm_geterr(kvmd));
		return (-1);
	}
	return (0);
}

kmemgetether(ifname, dlo)
char *ifname;
struct sockaddr_dl *dlo;
{
  struct ifnet ifnet;
  int n;
  u_long addr, ifaddraddr, ifnetfound, ifaddrfound;
  char name[16+32];
  struct sockaddr *sa;
  char *cp;
  struct sockaddr_dl *sdl;
  union {
	  struct ifaddr ifa;
	  struct in_ifaddr in;
  } ifaddr;
  struct arpcom ac;

  kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
  if (kvmd) {
    n = kvm_nlist(kvmd, nl);
    if (n >= 0) {
      addr = nl[N_IFNET].n_value;
      kread(addr, (char *)&addr, sizeof(addr));
      ifaddraddr = ifnetfound = 0;
      while (addr || ifaddraddr) {
	ifnetfound = addr;
	if (ifaddraddr == 0) {
	  if (kread(addr, (char *)&ifnet, sizeof(ifnet)) ||
	      kread((u_long)ifnet.if_name, name, 16))
		return;
	  name[15] = 0;
	  addr = (u_long) ifnet.if_next;
	  cp = (char *)index(name, '\0');
	  cp += sprintf(cp, "%d", ifnet.if_unit);
	  *cp = '\0';
	  ifaddraddr = (u_long)ifnet.if_addrlist;
	}
	ifaddrfound = ifaddraddr;
	if (ifaddraddr) {
	  if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
	    ifaddraddr = 0;
	    continue;
	  }
#define CP(x) ((char *)(x))
	  cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + CP(&ifaddr);
	  sa = (struct sockaddr *)cp;
	  if (sa->sa_family == AF_LINK && strcmp(ifname, name) == 0) {
	    sdl = (struct sockaddr_dl *)sa;
	    cp = (char *)LLADDR(sdl);
	    n = sdl->sdl_alen;
	    if (ifnet.if_type == IFT_ETHER) {
		if (n == 0) {
		  kread(ifnetfound, (char *)&ac, sizeof(ac));
	    	  cp = (char *)LLADDR(sdl);
		  bcopy((char *)ac.ac_enaddr, cp, 6);
		  sdl->sdl_alen = 6;
		}
		bcopy(sdl, dlo, sizeof(*sdl));
		return;
	    }
	  }
	  ifaddraddr = (u_long)ifaddr.ifa.ifa_next;
	}
      }
    }
  }
}
#endif

#ifdef DEBUG
main()
{
    u_long ipaddr;
    int s;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    ipaddr = inet_addr("192.168.1.32");
    sifproxyarp(s, ipaddr);
    close(s);
}
#endif
OpenPOWER on IntegriCloud