]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-linux.c
fixed some compilation warnings
[ppp.git] / pppd / sys-linux.c
1 /*
2  * sys-linux.c - System-dependent procedures for setting up
3  * PPP interfaces on Linux systems
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by Carnegie Mellon University.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 /*
22  * TODO:
23  */
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <sys/errno.h>
33 #include <mntent.h>
34
35 #include <net/if.h>
36 #include <linux/ppp.h>
37 #include <linux/route.h>
38 #include <linux/if_ether.h>
39 #include <netinet/in.h>
40 #include <net/if_ppp.h>
41
42 #include "pppd.h"
43 #include "ppp.h"
44 #include "fsm.h"
45 #include "ipcp.h"
46
47 static int initdisc = -1;       /* Initial TTY discipline */
48 static int prev_kdebugflag = 0;
49 extern int kdebugflag;
50 extern u_long netmask;
51
52 #define MAX_IFS         32
53
54 /* prototypes */
55 void die         __ARGS((int));
56
57 /*
58  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
59  * if it exists.
60  */
61
62 #define SET_SA_FAMILY(addr, family)                     \
63     memset ((char *) &(addr), '\0', sizeof(addr));      \
64     addr.sa_family = (family);
65
66 /*
67  * set_kdebugflag - Define the debugging level for the kernel
68  */
69
70 int set_kdebugflag (int requested_level)
71 {
72     if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
73         syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
74         return (0);
75     }
76
77     if (prev_kdebugflag != requested_level) {
78         if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
79             syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
80             return (0);
81         }
82         syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
83     }
84     return (1);
85 }
86
87 /*
88  * establish_ppp - Turn the serial port into a ppp interface.
89  */
90
91 void establish_ppp (void)
92 {
93     int pppdisc = N_PPP;
94
95     if (ioctl(fd, TIOCEXCL, 0) < 0) {
96         syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
97     }
98
99     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
100         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
101         die (1);
102     }
103     
104     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
105         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
106         die (1);
107     }
108
109     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
110         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
111         die (1);
112     }
113
114     set_kdebugflag (kdebugflag);
115 }
116
117 /*
118  * disestablish_ppp - Restore the serial port to normal operation.
119  * This shouldn't call die() because it's called from die().
120  */
121
122 void disestablish_ppp(void)
123 {
124     int x;
125     char *s;
126
127     if (initdisc >= 0) {
128         set_kdebugflag (prev_kdebugflag);
129         /*
130          * Check whether the link seems not to be 8-bit clean.
131          */
132         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
133             s = NULL;
134             switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
135             case SC_RCV_B7_0:
136                 s = "bit 7 set to 1";
137                 break;
138             case SC_RCV_B7_1:
139                 s = "bit 7 set to 0";
140                 break;
141             case SC_RCV_EVNP:
142                 s = "odd parity";
143                 break;
144             case SC_RCV_ODDP:
145                 s = "even parity";
146                 break;
147             }
148             if (s != NULL) {
149                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
150                 syslog(LOG_WARNING, "All received characters had %s", s);
151             }
152         }
153
154     if (ioctl(fd, TIOCSETD, &initdisc) < 0)
155         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
156
157     if (ioctl(fd, TIOCNXCL, 0) < 0)
158         syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
159
160     initdisc = -1;
161     }
162 }
163
164 /*
165  * output - Output PPP packet.
166  */
167
168 void output (int unit, unsigned char *p, int len)
169 {
170     if (unit != 0)
171         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
172
173     if (debug)
174         log_packet(p, len, "sent ");
175     
176     if (write(fd, p, len) < 0) {
177         syslog(LOG_ERR, "write: %m");
178         die(1);
179     }
180 }
181
182 /*
183  * read_packet - get a PPP packet from the serial device.
184  */
185
186 int read_packet (unsigned char *buf)
187 {
188     int len;
189   
190     len = read(fd, buf, MTU + DLLHEADERLEN);
191     if (len < 0) {
192         if (errno == EWOULDBLOCK) {
193 #if 0
194             MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
195 #endif
196             return -1;
197         }
198         syslog(LOG_ERR, "read(fd): %m");
199         die(1);
200     }
201     return len;
202 }
203
204 /*
205  * ppp_send_config - configure the transmit characteristics of
206  * the ppp interface.
207  */
208 void ppp_send_config (int unit,int mtu,u_long asyncmap,int pcomp,int accomp)
209 {
210     u_int x;
211     struct ifreq ifr;
212   
213     MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
214     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
215     ifr.ifr_mtu = mtu;
216     if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
217         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
218         quit();
219     }
220
221     MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
222     if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
223         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
224         quit();
225     }
226     
227     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
228         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
229         quit();
230     }
231
232     x = pcomp  ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
233     x = accomp ? x | SC_COMP_AC   : x & ~SC_COMP_AC;
234
235     MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
236     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
237         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
238         quit();
239     }
240 }
241
242 /*
243  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
244  */
245 void
246 ppp_set_xaccm(unit, accm)
247     int unit;
248     ext_accm accm;
249 {
250     MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
251                 accm[0], accm[1], accm[2], accm[3]));
252     if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
253         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
254 }
255
256 /*
257  * ppp_recv_config - configure the receive-side characteristics of
258  * the ppp interface.
259  */
260 void ppp_recv_config (int unit,int mru,u_long asyncmap,int pcomp,int accomp)
261 {
262     u_int x;
263
264     MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
265     if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
266         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
267
268     MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
269     if (ioctl(fd, PPPIOCRASYNCMAP, (caddr_t) &asyncmap) < 0) {
270         syslog(LOG_ERR, "ioctl(PPPIOCRASYNCMAP): %m");
271         quit();
272     }
273   
274     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
275         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
276         quit();
277     }
278
279     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
280     MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
281     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
282         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
283         quit();
284     }
285 }
286
287 /*
288  * sifvjcomp - config tcp header compression
289  */
290
291 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
292 {
293     u_int x;
294
295     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
296         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
297         return 0;
298     }
299
300     x = vjcomp  ? x | SC_COMP_TCP     : x &~ SC_COMP_TCP;
301     x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
302
303     if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
304         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
305         return 0;
306     }
307
308     if (vjcomp) {
309         if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
310             syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
311             return 0;
312         }
313     }
314
315     return 1;
316 }
317
318 /*
319  * sifup - Config the interface up and enable IP packets to pass.
320  */
321
322 int sifup (int u)
323 {
324     struct ifreq ifr;
325
326     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
327     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
328         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
329         return 0;
330     }
331
332     ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
333     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
334         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
335         return 0;
336     }
337     return 1;
338 }
339
340 /*
341  * sifdown - Config the interface down and disable IP.
342  */
343
344 int sifdown (int u)
345 {
346     struct ifreq ifr;
347
348     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
349     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
350         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
351         return 0;
352     }
353
354     ifr.ifr_flags &= ~IFF_UP;
355     ifr.ifr_flags |= IFF_POINTOPOINT;
356     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
357         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
358         return 0;
359     }
360     return 1;
361 }
362
363 /*
364  * sifaddr - Config the interface IP addresses and netmask.
365  */
366
367 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
368 {
369     struct ifreq   ifr; 
370     struct rtentry rt;
371     
372     SET_SA_FAMILY (ifr.ifr_addr,    AF_INET); 
373     SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET); 
374     SET_SA_FAMILY (ifr.ifr_netmask, AF_INET); 
375
376     strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
377 /*
378  *  Set our IP address
379  */
380     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
381     if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
382         if (errno != EEXIST)
383             syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
384         else
385             syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
386         return (0);
387     } 
388 /*
389  *  Set the gateway address
390  */
391     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
392     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
393         syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m"); 
394         return (0);
395     } 
396 /*
397  *  Set the netmask
398  */
399     if (net_mask != 0) {
400         ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
401         if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
402             syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m"); 
403             return (0);
404         } 
405     }
406 /*
407  *  Add the device route
408  */
409     memset (&rt, '\0', sizeof (rt));
410
411     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
412     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
413     rt.rt_dev = ifname;  /* MJC */
414
415     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
416     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
417     rt.rt_flags = RTF_UP | RTF_HOST;
418
419     if (ioctl(s, SIOCADDRT, &rt) < 0) {
420         syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
421         return (0);
422     }
423     return 1;
424 }
425
426 /*
427  * cifaddr - Clear the interface IP addresses, and delete routes
428  * through the interface if possible.
429  */
430
431 int cifaddr (int unit, int our_adr, int his_adr)
432 {
433     struct rtentry rt;
434 /*
435  *  Delete the route through the device
436  */
437     memset (&rt, '\0', sizeof (rt));
438
439     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
440     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
441     rt.rt_dev = ifname;  /* MJC */
442
443     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
444     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
445     rt.rt_flags = RTF_UP | RTF_HOST;
446
447     if (ioctl(s, SIOCDELRT, &rt) < 0) {
448         syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
449         return (0);
450     }
451     return 1;
452 }
453
454 /*
455  * path_to_route - determine the path to the proc file system data
456  */
457
458 FILE *route_fd = (FILE *) 0;
459 static char route_buffer [100];
460
461 static char *path_to_route (void);
462 static int open_route_table (void);
463 static void close_route_table (void);
464 static int read_route_table (struct rtentry *rt);
465 static int defaultroute_exists (void);
466
467 /*
468  * path_to_route - find the path to the route tables in the proc file system
469  */
470
471 static char *path_to_route (void)
472 {
473     struct mntent *mntent;
474     FILE *fp;
475
476     fp = fopen (MOUNTED, "r");
477     if (fp != 0) {
478         while ((mntent = getmntent (fp)) != 0) {
479             if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
480                 continue;
481
482             if (strcmp (mntent->mnt_type, "proc") == 0) {
483                 strncpy (route_buffer, mntent->mnt_dir,
484                          sizeof (route_buffer)-10);
485                 route_buffer [sizeof (route_buffer)-10] = '\0';
486                 strcat (route_buffer, "/net/route");
487
488                 fclose (fp);
489                 return (route_buffer);
490             }
491         }
492         fclose (fp);
493     }
494     syslog (LOG_ERR, "proc file system not mounted");
495     return 0;
496 }
497
498 /*
499  * open_route_table - open the interface to the route table
500  */
501
502 static int open_route_table (void)
503 {
504     char *path;
505
506     if (route_fd != (FILE *) 0)
507         close_route_table();
508
509     path = path_to_route();
510     if (path == NULL)
511         return 0;
512
513     route_fd = fopen (path, "r");
514     if (route_fd == (FILE *) 0) {
515         syslog (LOG_ERR, "can not open %s: %m", path);
516         return 0;
517     }
518
519     /* read and discard the header line. */
520     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
521         close_route_table();
522         return 0;
523     }
524     return 1;
525 }
526
527 /*
528  * close_route_table - close the interface to the route table
529  */
530
531 static void close_route_table (void)
532 {
533     if (route_fd != (FILE *) 0) {
534         fclose (route_fd);
535         route_fd = (FILE *) 0;
536     }
537 }
538
539 /*
540  * read_route_table - read the next entry from the route table
541  */
542
543 static int read_route_table (struct rtentry *rt)
544 {
545     static char delims[] = " \t\n";
546     char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
547
548     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
549         return 0;
550
551     memset (rt, '\0', sizeof (struct rtentry));
552
553     dev_ptr  = strtok (route_buffer, delims); /* interface name */
554     dst_ptr  = strtok (NULL,         delims); /* destination address */
555     gw_ptr   = strtok (NULL,         delims); /* gateway */
556     flag_ptr = strtok (NULL,         delims); /* flags */
557 #if 0
558     ptr      = strtok (NULL,         delims); /* reference count */
559     ptr      = strtok (NULL,         delims); /* useage count */
560     ptr      = strtok (NULL,         delims); /* metric */
561     ptr      = strtok (NULL,         delims); /* mask */
562 #endif
563
564     ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
565       strtoul (dst_ptr, NULL, 16);
566
567     ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
568       strtoul (gw_ptr, NULL, 16);
569
570     rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
571     rt->rt_dev   = dev_ptr;
572
573     return 1;
574 }
575
576 /*
577  * defaultroute_exists - determine if there is a default route
578  */
579
580 static int defaultroute_exists (void)
581 {
582     struct rtentry rt;
583     int    result = 0;
584
585     if (!open_route_table())
586         return 0;
587
588     while (read_route_table(&rt) != 0) {
589         if (rt.rt_flags & RTF_UP == 0)
590             continue;
591
592         if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
593             syslog (LOG_ERR,
594                     "ppp not replacing existing default route to %s[%s]",
595                     rt.rt_dev,
596                     inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
597                                sin_addr.s_addr));
598             result = 1;
599             break;
600         }
601     }
602     close_route_table();
603     return result;
604 }
605
606 /*
607  * sifdefaultroute - assign a default route through the address given.
608  */
609
610 int sifdefaultroute (int unit, int gateway)
611 {
612     struct rtentry rt;
613
614     if (defaultroute_exists())
615         return 0;
616
617     memset (&rt, '\0', sizeof (rt));
618     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
619     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
620     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
621     
622     rt.rt_flags = RTF_UP | RTF_GATEWAY;
623     if (ioctl(s, SIOCADDRT, &rt) < 0) {
624         syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
625         return 0;
626     }
627     return 1;
628 }
629
630 /*
631  * cifdefaultroute - delete a default route through the address given.
632  */
633
634 int cifdefaultroute (int unit, int gateway)
635 {
636     struct rtentry rt;
637   
638     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
639     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
640     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
641     
642     rt.rt_flags = RTF_UP | RTF_GATEWAY;
643     if (ioctl(s, SIOCDELRT, &rt) < 0) {
644         syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
645         return 0;
646     }
647     return 1;
648 }
649
650 /*
651  * sifproxyarp - Make a proxy ARP entry for the peer.
652  */
653
654 int sifproxyarp (int unit, u_long his_adr)
655 {
656     struct arpreq arpreq;
657
658     memset (&arpreq, '\0', sizeof(arpreq));
659 /*
660  * Get the hardware address of an interface on the same subnet
661  * as our local address.
662  */
663     if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
664         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
665         return 0;
666     }
667     
668     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
669     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
670     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
671     
672     if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
673         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
674         return 0;
675     }
676     return 1;
677 }
678
679 /*
680  * cifproxyarp - Delete the proxy ARP entry for the peer.
681  */
682
683 int cifproxyarp (int unit, u_long his_adr)
684 {
685     struct arpreq arpreq;
686   
687     memset (&arpreq, '\0', sizeof(arpreq));
688     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
689     
690     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
691     if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
692         syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
693         return 0;
694     }
695     return 1;
696 }
697      
698 /*
699  * get_ether_addr - get the hardware address of an interface on the
700  * the same subnet as ipaddr.
701  */
702
703 int get_ether_addr (u_long ipaddr, struct sockaddr *hwaddr)
704 {
705     struct ifreq *ifr, *ifend, *ifp;
706     int i;
707     u_long ina, mask;
708     struct sockaddr_dl *dla;
709     struct ifreq ifreq;
710     struct ifconf ifc;
711     struct ifreq ifs[MAX_IFS];
712     
713     ifc.ifc_len = sizeof(ifs);
714     ifc.ifc_req = ifs;
715     if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
716         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
717         return 0;
718     }
719     MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
720                 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
721 /*
722  * Scan through looking for an interface with an Internet
723  * address on the same subnet as `ipaddr'.
724  */
725     ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
726     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
727         if (ifr->ifr_addr.sa_family == AF_INET) {
728             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
729             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
730             MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
731                         ifreq.ifr_name));
732 /*
733  * Check that the interface is up, and not point-to-point
734  * or loopback.
735  */
736             if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
737                 continue;
738             if ((ifreq.ifr_flags &
739                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
740                 != (IFF_UP|IFF_BROADCAST))
741                 continue;
742 /*
743  * Get its netmask and check that it's on the right subnet.
744  */
745             if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
746                 continue;
747             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
748             MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
749                         ip_ntoa(ina), ntohl(mask)));
750             if (((ipaddr ^ ina) & mask) != 0)
751                 continue;
752             break;
753         }
754     }
755     
756     if (ifr >= ifend)
757         return 0;
758
759     syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
760 /*
761  * Now get the hardware address.
762  */
763     if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
764         syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
765         return 0;
766     }
767
768     hwaddr->sa_family = ARPHRD_ETHER;
769     memcpy (&hwaddr->sa_data, &ifr->ifr_hwaddr, ETH_ALEN);
770
771     MAINDEBUG ((LOG_DEBUG,
772                 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
773                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[0],
774                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[1],
775                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[2],
776                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[3],
777                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[4],
778                 (int) ((unsigned char *) &ifr->ifr_hwaddr)[5]));
779     return 1;
780 }
781
782 /*
783  * ppp_available - check whether the system has any ppp interfaces
784  * (in fact we check whether we can do an ioctl on ppp0).
785  */
786
787 int ppp_available(void)
788 {
789     int s, ok;
790     struct ifreq ifr;
791     
792     s = socket(AF_INET, SOCK_DGRAM, 0);
793     if (s < 0)
794         return 1;               /* can't tell - maybe we're not root */
795     
796     strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
797     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
798     close(s);
799     
800     return ok;
801 }