Debian Codegraph @

pppd/multilink.c

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