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