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