]> git.ozlabs.org Git - ppp.git/blob - pppd/multilink.c
Makefile.am: Add explicit openssl directory to pppd include path
[ppp.git] / pppd / multilink.c
1 /*
2  * multilink.c - support routines for multilink.
3  *
4  * Copyright (c) 2000-2002 Paul Mackerras. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. The name(s) of the authors of this software must not be used to
14  *    endorse or promote products derived from this software without
15  *    prior written permission.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  *    acknowledgment:
19  *    "This product includes software developed by Paul Mackerras
20  *     <paulus@samba.org>".
21  *
22  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <string.h>
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <netdb.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <netinet/in.h>
42 #include <unistd.h>
43
44 #include "pppd-private.h"
45 #include "fsm.h"
46 #include "lcp.h"
47 #include "tdb.h"
48 #include "multilink.h"
49
50 bool endpoint_specified;        /* user gave explicit endpoint discriminator */
51 char *bundle_id;                /* identifier for our bundle */
52 char *blinks_id;                /* key for the list of links */
53 bool doing_multilink;           /* multilink was enabled and agreed to */
54 bool multilink_master;          /* we own the multilink bundle */
55
56 extern TDB_CONTEXT *pppdb;
57 extern char db_key[];
58
59 static void make_bundle_links(int append);
60 static void remove_bundle_link(void);
61 static void iterate_bundle_links(void (*func)(char *));
62
63 static int get_default_epdisc(struct epdisc *);
64 static int parse_num(char *str, const char *key, int *valp);
65 static int owns_unit(TDB_DATA pid, int unit);
66
67 #define set_ip_epdisc(ep, addr) do {    \
68         ep->length = 4;                 \
69         ep->value[0] = addr >> 24;      \
70         ep->value[1] = addr >> 16;      \
71         ep->value[2] = addr >> 8;       \
72         ep->value[3] = addr;            \
73 } while (0)
74
75 #define LOCAL_IP_ADDR(addr)                                               \
76         (((addr) & 0xff000000) == 0x0a000000            /* 10.x.x.x */    \
77          || ((addr) & 0xfff00000) == 0xac100000         /* 172.16.x.x */  \
78          || ((addr) & 0xffff0000) == 0xc0a80000)        /* 192.168.x.x */
79
80 #define process_exists(n)       (kill((n), 0) == 0 || errno != ESRCH)
81
82 multilink_join_hook_fn *multilink_join_hook = NULL;
83
84 bool mp_master()
85 {
86     return multilink_master;
87 }
88
89 bool mp_on()
90 {
91     return doing_multilink;
92 }
93
94 void
95 mp_check_options(void)
96 {
97         lcp_options *wo = &lcp_wantoptions[0];
98         lcp_options *ao = &lcp_allowoptions[0];
99
100         doing_multilink = 0;
101         if (!multilink)
102                 return;
103         /* if we're doing multilink, we have to negotiate MRRU */
104         if (!wo->neg_mrru) {
105                 /* mrru not specified, default to mru */
106                 wo->mrru = wo->mru;
107                 wo->neg_mrru = 1;
108         }
109         ao->mrru = ao->mru;
110         ao->neg_mrru = 1;
111
112         if (!wo->neg_endpoint && !noendpoint) {
113                 /* get a default endpoint value */
114                 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
115         }
116 }
117
118 /*
119  * Make a new bundle or join us to an existing bundle
120  * if we are doing multilink.
121  */
122 int
123 mp_join_bundle(void)
124 {
125         lcp_options *go = &lcp_gotoptions[0];
126         lcp_options *ho = &lcp_hisoptions[0];
127         lcp_options *ao = &lcp_allowoptions[0];
128         int unit, pppd_pid;
129         int l, mtu;
130         char *p;
131         TDB_DATA key, pid, rec;
132
133         if (doing_multilink) {
134                 /* have previously joined a bundle */
135                 if (!go->neg_mrru || !ho->neg_mrru) {
136                         notice("oops, didn't get multilink on renegotiation");
137                         lcp_close(0, "multilink required");
138                         return 0;
139                 }
140                 /* XXX should check the peer_authname and ho->endpoint
141                    are the same as previously */
142                 return 0;
143         }
144
145         if (!go->neg_mrru || !ho->neg_mrru) {
146                 /* not doing multilink */
147                 if (go->neg_mrru)
148                         notice("oops, multilink negotiated only for receive");
149                 mtu = ho->neg_mru? ho->mru: PPP_MRU;
150                 if (mtu > ao->mru)
151                         mtu = ao->mru;
152                 if (demand) {
153                         /* already have a bundle */
154                         cfg_bundle(0, 0, 0, 0);
155                         ppp_set_mtu(0, mtu);
156                         return 0;
157                 }
158                 make_new_bundle(0, 0, 0, 0);
159                 set_ifunit(1);
160                 ppp_set_mtu(0, mtu);
161                 return 0;
162         }
163
164         doing_multilink = 1;
165
166         /*
167          * Find the appropriate bundle or join a new one.
168          * First we make up a name for the bundle.
169          * The length estimate is worst-case assuming every
170          * character has to be quoted.
171          */
172         l = 4 * strlen(peer_authname) + 10;
173         if (ho->neg_endpoint)
174                 l += 3 * ho->endpoint.length + 8;
175         if (bundle_name)
176                 l += 3 * strlen(bundle_name) + 2;
177         bundle_id = malloc(l);
178         if (bundle_id == 0)
179                 novm("bundle identifier");
180
181         p = bundle_id;
182         p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
183         if (ho->neg_endpoint || bundle_name)
184                 *p++ = '/';
185         if (ho->neg_endpoint)
186                 p += slprintf(p, bundle_id+l-p, "%s",
187                               epdisc_to_str(&ho->endpoint));
188         if (bundle_name)
189                 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
190
191         /* Make the key for the list of links belonging to the bundle */
192         l = p - bundle_id;
193         blinks_id = malloc(l + 7);
194         if (blinks_id == NULL)
195                 novm("bundle links key");
196         slprintf(blinks_id, l + 7, "BUNDLE_LINKS=%s", bundle_id + 7);
197
198         /*
199          * For demand mode, we only need to configure the bundle
200          * and attach the link.
201          */
202         mtu = MIN(ho->mrru, ao->mru);
203         if (demand) {
204                 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
205                 ppp_set_mtu(0, mtu);
206                 ppp_script_setenv("BUNDLE", bundle_id + 7, 1);
207                 return 0;
208         }
209
210         /*
211          * Check if the bundle ID is already in the database.
212          */
213         unit = -1;
214         lock_db();
215         key.dptr = bundle_id;
216         key.dsize = p - bundle_id;
217         pid = tdb_fetch(pppdb, key);
218         if (pid.dptr != NULL) {
219                 /* bundle ID exists, see if the pppd record exists */
220                 rec = tdb_fetch(pppdb, pid);
221                 if (rec.dptr != NULL && rec.dsize > 0) {
222                         /* make sure the string is null-terminated */
223                         rec.dptr[rec.dsize-1] = 0;
224                         /* parse the interface number */
225                         parse_num(rec.dptr, "UNIT=", &unit);
226                         /* check the pid value */
227                         if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
228                             || !process_exists(pppd_pid)
229                             || !owns_unit(pid, unit))
230                                 unit = -1;
231                         free(rec.dptr);
232                 }
233                 free(pid.dptr);
234         }
235
236         if (unit >= 0) {
237                 /* attach to existing unit */
238                 if (bundle_attach(unit)) {
239                         set_ifunit(0);
240                         ppp_script_setenv("BUNDLE", bundle_id + 7, 0);
241                         make_bundle_links(1);
242                         unlock_db();
243                         info("Link attached to %s", ifname);
244                         return 1;
245                 }
246                 /* attach failed because bundle doesn't exist */
247         }
248
249         /* we have to make a new bundle */
250         make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
251         set_ifunit(1);
252         ppp_set_mtu(0, mtu);
253         ppp_script_setenv("BUNDLE", bundle_id + 7, 1);
254         make_bundle_links(0);
255         unlock_db();
256         info("New bundle %s created", ifname);
257         multilink_master = 1;
258         return 0;
259 }
260
261 void mp_exit_bundle(void)
262 {
263         lock_db();
264         remove_bundle_link();
265         unlock_db();
266 }
267
268 static void sendhup(char *str)
269 {
270         int pid;
271
272         if (parse_num(str, "PPPD_PID=", &pid) && pid != getpid()) {
273                 if (debug)
274                         dbglog("sending SIGHUP to process %d", pid);
275                 kill(pid, SIGHUP);
276         }
277 }
278
279 void mp_bundle_terminated(void)
280 {
281         TDB_DATA key;
282
283         bundle_terminating = 1;
284         upper_layers_down(0);
285         notice("Connection terminated.");
286         print_link_stats();
287         if (!demand) {
288                 remove_pidfiles();
289                 ppp_script_unsetenv("IFNAME");
290         }
291
292         lock_db();
293         destroy_bundle();
294         iterate_bundle_links(sendhup);
295         key.dptr = blinks_id;
296         key.dsize = strlen(blinks_id);
297         tdb_delete(pppdb, key);
298         unlock_db();
299
300         new_phase(PHASE_DEAD);
301
302         doing_multilink = 0;
303         multilink_master = 0;
304 }
305
306 static void make_bundle_links(int append)
307 {
308         TDB_DATA key, rec;
309         char *p;
310         char entry[32];
311         int l;
312
313         key.dptr = blinks_id;
314         key.dsize = strlen(blinks_id);
315         slprintf(entry, sizeof(entry), "%s;", db_key);
316         p = entry;
317         if (append) {
318                 rec = tdb_fetch(pppdb, key);
319                 if (rec.dptr != NULL && rec.dsize > 0) {
320                         rec.dptr[rec.dsize-1] = 0;
321                         if (strstr(rec.dptr, db_key) != NULL) {
322                                 /* already in there? strange */
323                                 warn("link entry already exists in tdb");
324                                 return;
325                         }
326                         l = rec.dsize + strlen(entry);
327                         p = malloc(l);
328                         if (p == NULL)
329                                 novm("bundle link list");
330                         slprintf(p, l, "%s%s", rec.dptr, entry);
331                 } else {
332                         warn("bundle link list not found");
333                 }
334                 if (rec.dptr != NULL)
335                         free(rec.dptr);
336         }
337         rec.dptr = p;
338         rec.dsize = strlen(p) + 1;
339         if (tdb_store(pppdb, key, rec, TDB_REPLACE))
340                 error("couldn't %s bundle link list",
341                       append? "update": "create");
342         if (p != entry)
343                 free(p);
344 }
345
346 static void remove_bundle_link(void)
347 {
348         TDB_DATA key, rec;
349         char entry[32];
350         char *p, *q;
351         int l;
352
353         key.dptr = blinks_id;
354         key.dsize = strlen(blinks_id);
355         slprintf(entry, sizeof(entry), "%s;", db_key);
356
357         rec = tdb_fetch(pppdb, key);
358         if (rec.dptr == NULL || rec.dsize <= 0) {
359                 if (rec.dptr != NULL)
360                         free(rec.dptr);
361                 return;
362         }
363         rec.dptr[rec.dsize-1] = 0;
364         p = strstr(rec.dptr, entry);
365         if (p != NULL) {
366                 q = p + strlen(entry);
367                 l = strlen(q) + 1;
368                 memmove(p, q, l);
369                 rec.dsize = p - rec.dptr + l;
370                 if (tdb_store(pppdb, key, rec, TDB_REPLACE))
371                         error("couldn't update bundle link list (removal)");
372         }
373         free(rec.dptr);
374 }
375
376 static void iterate_bundle_links(void (*func)(char *))
377 {
378         TDB_DATA key, rec, pp;
379         char *p, *q;
380
381         key.dptr = blinks_id;
382         key.dsize = strlen(blinks_id);
383         rec = tdb_fetch(pppdb, key);
384         if (rec.dptr == NULL || rec.dsize <= 0) {
385                 error("bundle link list not found (iterating list)");
386                 if (rec.dptr != NULL)
387                         free(rec.dptr);
388                 return;
389         }
390         p = rec.dptr;
391         p[rec.dsize-1] = 0;
392         while ((q = strchr(p, ';')) != NULL) {
393                 *q = 0;
394                 key.dptr = p;
395                 key.dsize = q - p;
396                 pp = tdb_fetch(pppdb, key);
397                 if (pp.dptr != NULL && pp.dsize > 0) {
398                         pp.dptr[pp.dsize-1] = 0;
399                         func(pp.dptr);
400                 }
401                 if (pp.dptr != NULL)
402                         free(pp.dptr);
403                 p = q + 1;
404         }
405         free(rec.dptr);
406 }
407
408 static int
409 parse_num(char *str, const char *key, int *valp)
410 {
411         char *p, *endp;
412         int i;
413
414         p = strstr(str, key);
415         if (p != 0) {
416                 p += strlen(key);
417                 i = strtol(p, &endp, 10);
418                 if (endp != p && (*endp == 0 || *endp == ';')) {
419                         *valp = i;
420                         return 1;
421                 }
422         }
423         return 0;
424 }
425
426 /*
427  * Check whether the pppd identified by `key' still owns ppp unit `unit'.
428  */
429 static int
430 owns_unit(TDB_DATA key, int unit)
431 {
432         char ifkey[32];
433         TDB_DATA kd, vd;
434         int ret = 0;
435
436         slprintf(ifkey, sizeof(ifkey), "UNIT=%d", unit);
437         kd.dptr = ifkey;
438         kd.dsize = strlen(ifkey);
439         vd = tdb_fetch(pppdb, kd);
440         if (vd.dptr != NULL) {
441                 ret = vd.dsize == key.dsize
442                         && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
443                 free(vd.dptr);
444         }
445         return ret;
446 }
447
448 static int
449 get_default_epdisc(struct epdisc *ep)
450 {
451         struct hostent *hp;
452         u_int32_t addr;
453
454         /* First try for an ethernet MAC address */
455         if (get_first_ether_hwaddr(ep->value) >= 0) {
456                 ep->class = EPD_MAC;
457                 ep->length = 6;
458                 return 1;
459         }
460
461         /* see if our hostname corresponds to a reasonable IP address */
462         hp = gethostbyname(hostname);
463         if (hp != NULL) {
464                 addr = *(u_int32_t *)hp->h_addr;
465                 if (!ppp_bad_ip_addr(addr)) {
466                         addr = ntohl(addr);
467                         if (!LOCAL_IP_ADDR(addr)) {
468                                 ep->class = EPD_IP;
469                                 set_ip_epdisc(ep, addr);
470                                 return 1;
471                         }
472                 }
473         }
474
475         return 0;
476 }
477
478 /*
479  * epdisc_to_str - make a printable string from an endpoint discriminator.
480  */
481
482 static char *endp_class_names[] = {
483     "null", "local", "IP", "MAC", "magic", "phone"
484 };
485
486 char *
487 epdisc_to_str(struct epdisc *ep)
488 {
489         static char str[MAX_ENDP_LEN*3+8];
490         u_char *p = ep->value;
491         int i, mask = 0;
492         char *q, c, c2;
493
494         if (ep->class == EPD_NULL && ep->length == 0)
495                 return "null";
496         if (ep->class == EPD_IP && ep->length == 4) {
497                 u_int32_t addr;
498
499                 GETLONG(addr, p);
500                 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
501                 return str;
502         }
503
504         c = ':';
505         c2 = '.';
506         if (ep->class == EPD_MAC && ep->length == 6)
507                 c2 = ':';
508         else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
509                 mask = 3;
510         q = str;
511         if (ep->class <= EPD_PHONENUM)
512                 q += slprintf(q, sizeof(str)-1, "%s",
513                               endp_class_names[ep->class]);
514         else
515                 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
516         c = ':';
517         for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
518                 if ((i & mask) == 0) {
519                         *q++ = c;
520                         c = c2;
521                 }
522                 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
523         }
524         return str;
525 }
526
527 static int hexc_val(int c)
528 {
529         if (c >= 'a')
530                 return c - 'a' + 10;
531         if (c >= 'A')
532                 return c - 'A' + 10;
533         return c - '0';
534 }
535
536 int
537 str_to_epdisc(struct epdisc *ep, char *str)
538 {
539         int i, l;
540         char *p, *endp;
541
542         for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
543                 int sl = strlen(endp_class_names[i]);
544                 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
545                         str += sl;
546                         break;
547                 }
548         }
549         if (i > EPD_PHONENUM) {
550                 /* not a class name, try a decimal class number */
551                 i = strtol(str, &endp, 10);
552                 if (endp == str)
553                         return 0;       /* can't parse class number */
554                 str = endp;
555         }
556         ep->class = i;
557         if (*str == 0) {
558                 ep->length = 0;
559                 return 1;
560         }
561         if (*str != ':' && *str != '.')
562                 return 0;
563         ++str;
564
565         if (i == EPD_IP) {
566                 u_int32_t addr;
567                 i = parse_dotted_ip(str, &addr);
568                 if (i == 0 || str[i] != 0)
569                         return 0;
570                 set_ip_epdisc(ep, addr);
571                 return 1;
572         }
573         if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
574                 ep->length = 6;
575                 return 1;
576         }
577
578         p = str;
579         for (l = 0; l < MAX_ENDP_LEN; ++l) {
580                 if (*str == 0)
581                         break;
582                 if (p <= str)
583                         for (p = str; isxdigit(*p); ++p)
584                                 ;
585                 i = p - str;
586                 if (i == 0)
587                         return 0;
588                 ep->value[l] = hexc_val(*str++);
589                 if ((i & 1) == 0)
590                         ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
591                 if (*str == ':' || *str == '.')
592                         ++str;
593         }
594         if (*str != 0 || (ep->class == EPD_MAC && l != 6))
595                 return 0;
596         ep->length = l;
597         return 1;
598 }