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