]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-str.c
6bd2fc91b6cb5be54d43f7ec622834763bd61ffb
[ppp.git] / pppd / sys-str.c
1 /*
2  * sys-str.c - System-dependent procedures for setting up
3  * PPP interfaces on systems which use the STREAMS ppp interface.
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 <errno.h>
27 #include <syslog.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33 #include <sys/stream.h>
34 #include <sys/stropts.h>
35
36 #include <net/if.h>
37 #include <net/route.h>
38 #include <net/if_arp.h>
39 #include <netinet/in.h>
40
41 #include "pppd.h"
42 #include "ppp.h"
43 #include "ppp_str.h"
44
45 #ifndef ifr_mtu
46 #define ifr_mtu         ifr_metric
47 #endif
48
49 #define MAXMODULES      10      /* max number of module names to save */
50 static struct   modlist {
51     char        modname[FMNAMESZ+1];
52 } str_modules[MAXMODULES];
53 static int      str_module_count = 0;
54
55 extern int hungup;              /* has the physical layer been disconnected? */
56
57 /*
58  * ppp_available - check if this kernel supports PPP.
59  */
60 int
61 ppp_available()
62 {
63     int fd, ret;
64
65     fd = open("/dev/tty", O_RDONLY, 0);
66     if (fd < 0)
67         return 1;               /* can't find out - assume we have ppp */
68     ret = ioctl(fd, I_FIND, "pppasync") >= 0;
69     close(fd);
70     return ret;
71 }
72
73
74 /*
75  * establish_ppp - Turn the serial port into a ppp interface.
76  */
77 void
78 establish_ppp()
79 {
80     /* go through and save the name of all the modules, then pop em */
81     for (;;) { 
82         if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
83             ioctl(fd, I_POP, 0) < 0)
84             break;
85         MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
86                    str_modules[str_module_count].modname));
87         str_module_count++;
88     }
89
90     MAINDEBUG((LOG_INFO, "about to push modules..."));
91
92     /* now push the async/fcs module */
93     if (ioctl(fd, I_PUSH, "pppasync") < 0) {
94         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
95         die(1);
96     }
97     /* finally, push the ppp_if module that actually handles the */
98     /* network interface */ 
99     if (ioctl(fd, I_PUSH, "pppif") < 0) {
100         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
101         die(1);
102     }
103     if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
104         syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
105         die(1);
106     }
107     /* read mode, message non-discard mode */
108     if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
109         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
110         die(1);
111     }
112     /* Flush any waiting messages, or we'll never get SIGPOLL */
113     if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
114         syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
115         die(1);
116     }
117     /*
118      * Find out which interface we were given.
119      * (ppp_if handles this ioctl)
120      */
121     if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
122         syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
123         die(1);
124     }
125
126     /* if debug, set debug flags in driver */
127     {
128         int flags = debug ? 0x3 : 0;
129         if (ioctl(fd, SIOCSIFDEBUG, &flags) < 0) {
130             syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
131         }
132     }
133
134     MAINDEBUG((LOG_INFO, "done pushing modules, ifunit %d", ifunit));
135 }
136
137 /*
138  * disestablish_ppp - Restore the serial port to normal operation.
139  * It attempts to reconstruct the stream with the previously popped
140  * modules.  This shouldn't call die() because it's called from die().
141  */
142 void
143 disestablish_ppp()
144 {
145     /*EMPTY*/
146
147     if (hungup) {
148         /* we can't push or pop modules after the stream has hung up */
149         str_module_count = 0;
150         return;
151     }
152
153     while (ioctl(fd, I_POP, 0) == 0)    /* pop any we pushed */
154         ;
155   
156     for (; str_module_count > 0; str_module_count--) {
157         if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
158             syslog(LOG_ERR, "str_restore: couldn't push module %s: %m",
159                    str_modules[str_module_count-1].modname);
160         } else {
161             MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
162                        str_modules[str_module_count-1].modname));
163         }
164     }
165 }
166
167
168 /*
169  * output - Output PPP packet.
170  */
171 void
172 output(unit, p, len)
173     int unit;
174     u_char *p;
175     int len;
176 {
177     struct strbuf       str;
178
179     if (unit != 0)
180         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
181
182     str.len = len;
183     str.buf = (caddr_t) p;
184     if(putmsg(fd, NULL, &str, 0) < 0) {
185         syslog(LOG_ERR, "putmsg: %m");
186         die(1);
187     }
188 }
189
190
191 /*
192  * read_packet - get a PPP packet from the serial device.
193  */
194 int
195 read_packet(buf)
196     u_char *buf;
197 {
198     struct strbuf str;
199     int len, i;
200
201     str.maxlen = MTU+DLLHEADERLEN;
202     str.buf = (caddr_t) buf;
203     i = 0;
204     len = getmsg(fd, NULL, &str, &i);
205     if (len < 0) {
206         if (errno == EAGAIN || errno == EWOULDBLOCK) {
207             return -1;
208         }
209         syslog(LOG_ERR, "getmsg(fd) %m");
210         die(1);
211     }
212     if (len) 
213         MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
214
215     if (str.len < 0) {
216         MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
217         return -1;
218     }
219
220     return str.len;
221 }
222
223
224 /*
225  * ppp_send_config - configure the transmit characteristics of
226  * the ppp interface.
227  */
228 void
229 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
230     int unit, mtu;
231     u_long asyncmap;
232     int pcomp, accomp;
233 {
234     char c;
235     struct ifreq ifr;
236
237     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
238     ifr.ifr_mtu = mtu;
239     if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
240         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
241         quit();
242     }
243
244     if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
245         syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
246         quit();
247     }
248
249     c = pcomp;
250     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
251         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
252         quit();
253     }
254
255     c = accomp;
256     if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
257         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
258         quit();
259     }
260 }
261
262 /*
263  * ppp_recv_config - configure the receive-side characteristics of
264  * the ppp interface.  At present this just sets the MRU.
265  */
266 void
267 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
268     int unit, mru;
269     u_long asyncmap;
270     int pcomp, accomp;
271 {
272     char c;
273
274     if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
275         syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
276     }
277
278 #ifdef notyet
279     if(ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
280         syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
281     }
282
283     c = accomp;
284     if(ioctl(fd, SIOCSIFRCOMPAC, &c) < 0) {
285         syslog(LOG_ERR, "ioctl(SIOCSIFRCOMPAC): %m");
286         quit();
287     }
288 #endif  /* notyet */
289 }
290
291 /*
292  * sifvjcomp - config tcp header compression
293  */
294 int
295 sifvjcomp(u, vjcomp, cidcomp)
296 {
297     char x = vjcomp;
298
299     if(ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
300         syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
301         return 0;
302     }
303     return 1;
304 }
305
306 /*
307  * sifup - Config the interface up.
308  */
309 int
310 sifup(u)
311 {
312     struct ifreq ifr;
313
314     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
315     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
316         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
317         return 0;
318     }
319     ifr.ifr_flags |= IFF_UP;
320     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
321         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
322         return 0;
323     }
324     return 1;
325 }
326
327 /*
328  * sifdown - Config the interface down.
329  */
330 int
331 sifdown(u)
332 {
333     struct ifreq ifr;
334     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
335     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
336         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
337         return 0;
338     }
339     ifr.ifr_flags &= ~IFF_UP;
340     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
341         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
342         return 0;
343     }
344     return 1;
345 }
346
347 /*
348  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
349  */
350 #define SET_SA_FAMILY(addr, family)             \
351     BZERO((char *) &(addr), sizeof(addr));      \
352     addr.sa_family = (family);
353
354 /*
355  * sifaddr - Config the interface IP addresses and netmask.
356  */
357 int
358 sifaddr(u, o, h, m)
359 {
360     int ret;
361     struct ifreq ifr;
362
363     ret = 1;
364     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
365     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
366     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
367     if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
368         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
369         ret = 0;
370     }
371     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
372     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
373         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
374         ret = 0;
375     }
376     if (m != 0) {
377         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
378         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
379         if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
380             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
381             ret = 0;
382         }
383     }
384     return ret;
385 }
386
387 /*
388  * cifaddr - Clear the interface IP addresses, and delete routes
389  * through the interface if possible.
390  */
391 int
392 cifaddr(u, o, h)
393 {
394     struct rtentry rt;
395
396     SET_SA_FAMILY(rt.rt_dst, AF_INET);
397     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
398     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
399     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
400     rt.rt_flags = RTF_HOST;
401     if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
402         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
403         return 0;
404     }
405     return 1;
406 }
407
408 /*
409  * sifdefaultroute - assign a default route through the address given.
410  */
411 int
412 sifdefaultroute(u, g)
413 {
414     struct rtentry rt;
415
416     SET_SA_FAMILY(rt.rt_dst, AF_INET);
417     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
418     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
419     rt.rt_flags = RTF_GATEWAY;
420     if (ioctl(s, SIOCADDRT, &rt) < 0) {
421         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
422         return 0;
423     }
424     return 1;
425 }
426
427 /*
428  * cifdefaultroute - delete a default route through the address given.
429  */
430 int
431 cifdefaultroute(u, g)
432 {
433     struct rtentry rt;
434
435     SET_SA_FAMILY(rt.rt_dst, AF_INET);
436     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
437     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
438     rt.rt_flags = RTF_GATEWAY;
439     if (ioctl(s, SIOCDELRT, &rt) < 0) {
440         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
441         return 0;
442     }
443     return 1;
444 }
445
446 /*
447  * sifproxyarp - Make a proxy ARP entry for the peer.
448  */
449 int
450 sifproxyarp(unit, hisaddr)
451     int unit;
452     u_long hisaddr;
453 {
454     struct arpreq arpreq;
455
456     BZERO(&arpreq, sizeof(arpreq));
457
458     /*
459      * Get the hardware address of an interface on the same subnet
460      * as our local address.
461      */
462     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
463         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
464         return 0;
465     }
466
467     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
468     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
469     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
470     if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
471         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
472         return 0;
473     }
474
475     return 1;
476 }
477
478 /*
479  * cifproxyarp - Delete the proxy ARP entry for the peer.
480  */
481 int
482 cifproxyarp(unit, hisaddr)
483     int unit;
484     u_long hisaddr;
485 {
486     struct arpreq arpreq;
487
488     BZERO(&arpreq, sizeof(arpreq));
489     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
490     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
491     if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
492         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
493         return 0;
494     }
495     return 1;
496 }
497
498 /*
499  * get_ether_addr - get the hardware address of an interface on the
500  * the same subnet as ipaddr.  Code borrowed from myetheraddr.c
501  * in the cslip-2.6 distribution, which is subject to the following
502  * copyright notice:
503  *
504  * Copyright (c) 1990, 1992 The Regents of the University of California.
505  * All rights reserved.
506  *
507  * Redistribution and use in source and binary forms, with or without
508  * modification, are permitted provided that: (1) source code distributions
509  * retain the above copyright notice and this paragraph in its entirety, (2)
510  * distributions including binary code include the above copyright notice and
511  * this paragraph in its entirety in the documentation or other materials
512  * provided with the distribution, and (3) all advertising materials mentioning
513  * features or use of this software display the following acknowledgement:
514  * ``This product includes software developed by the University of California,
515  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
516  * the University nor the names of its contributors may be used to endorse
517  * or promote products derived from this software without specific prior
518  * written permission.
519  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
520  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
521  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
522  */
523
524 #include <fcntl.h>
525 #include <nlist.h>
526 #include <kvm.h>
527 #include <arpa/inet.h>
528
529 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
530 #ifdef _nlist_h
531 #define SUNOS4
532 #endif
533
534 #ifdef SUNOS4
535 #include <netinet/in_var.h>
536 #endif
537 #include <netinet/if_ether.h>
538
539 /* Cast a struct sockaddr to a structaddr_in */
540 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
541
542 /* Determine if "bits" is set in "flag" */
543 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
544
545 static struct nlist nl[] = {
546 #define N_IFNET 0
547         { "_ifnet" },
548         0
549 };
550
551 static void kread();
552
553 int
554 get_ether_addr(ipaddr, hwaddr)
555     u_long ipaddr;
556     struct sockaddr *hwaddr;
557 {
558     register kvm_t *kd;
559     register struct ifnet *ifp;
560     register struct arpcom *ac;
561     struct arpcom arpcom;
562     struct in_addr *inp;
563 #ifdef SUNOS4
564     register struct ifaddr *ifa;
565     register struct in_ifaddr *in;
566     union {
567         struct ifaddr ifa;
568         struct in_ifaddr in;
569     } ifaddr;
570 #endif
571     u_long addr, mask;
572
573     /* Open kernel memory for reading */
574     kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
575     if (kd == 0) {
576         syslog(LOG_ERR, "kvm_open: %m");
577         return 0;
578     }
579
580     /* Fetch namelist */
581     if (kvm_nlist(kd, nl) != 0) {
582         syslog(LOG_ERR, "kvm_nlist failed");
583         return 0;
584     }
585
586     ac = &arpcom;
587     ifp = &arpcom.ac_if;
588 #ifdef SUNOS4
589     ifa = &ifaddr.ifa;
590     in = &ifaddr.in;
591 #endif
592
593     if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
594         != sizeof(addr)) {
595         syslog(LOG_ERR, "error reading ifnet addr");
596         return 0;
597     }
598     for ( ; addr; addr = (u_long)ifp->if_next) {
599         if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
600             syslog(LOG_ERR, "error reading ifnet");
601             return 0;
602         }
603
604         /* Only look at configured, broadcast interfaces */
605         if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
606             continue;
607 #ifdef SUNOS4
608         /* This probably can't happen... */
609         if (ifp->if_addrlist == 0)
610             continue;
611 #endif
612
613         /* Get interface ip address */
614 #ifdef SUNOS4
615         if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
616                      sizeof(ifaddr)) != sizeof(ifaddr)) {
617             syslog(LOG_ERR, "error reading ifaddr");
618             return 0;
619         }
620         inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
621 #else
622         inp = &SATOSIN(&ifp->if_addr)->sin_addr;
623 #endif
624
625         /* Check if this interface on the right subnet */
626 #ifdef SUNOS4
627         mask = in->ia_subnetmask;
628 #else
629         mask = ifp->if_subnetmask;
630 #endif
631         if ((ipaddr & mask) != (inp->s_addr & mask))
632             continue;
633
634         /* Copy out the local ethernet address */
635         hwaddr->sa_family = AF_UNSPEC;
636         BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
637               sizeof(arpcom.ac_enaddr));
638         return 1;               /* success! */
639     }
640
641     /* couldn't find one */
642     return 0;
643 }