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