2 * multilink.c - support routines for multilink.
4 * Copyright (c) 2000-2002 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. Redistributions of any form whatsoever must retain the following
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
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.
41 #include <netinet/in.h>
44 #include "pppd-private.h"
48 #include "multilink.h"
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 */
56 extern TDB_CONTEXT *pppdb;
59 static void make_bundle_links(int append);
60 static void remove_bundle_link(void);
61 static void iterate_bundle_links(void (*func)(char *));
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);
67 #define set_ip_epdisc(ep, addr) do { \
69 ep->value[0] = addr >> 24; \
70 ep->value[1] = addr >> 16; \
71 ep->value[2] = addr >> 8; \
72 ep->value[3] = addr; \
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 */
80 #define process_exists(n) (kill((n), 0) == 0 || errno != ESRCH)
82 multilink_join_hook_fn *multilink_join_hook = NULL;
86 return multilink_master;
91 return doing_multilink;
95 mp_check_options(void)
97 lcp_options *wo = &lcp_wantoptions[0];
98 lcp_options *ao = &lcp_allowoptions[0];
103 /* if we're doing multilink, we have to negotiate MRRU */
105 /* mrru not specified, default to mru */
112 if (!wo->neg_endpoint && !noendpoint) {
113 /* get a default endpoint value */
114 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
119 * Make a new bundle or join us to an existing bundle
120 * if we are doing multilink.
125 lcp_options *go = &lcp_gotoptions[0];
126 lcp_options *ho = &lcp_hisoptions[0];
127 lcp_options *ao = &lcp_allowoptions[0];
131 TDB_DATA key, pid, rec;
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");
140 /* XXX should check the peer_authname and ho->endpoint
141 are the same as previously */
145 if (!go->neg_mrru || !ho->neg_mrru) {
146 /* not doing multilink */
148 notice("oops, multilink negotiated only for receive");
149 mtu = ho->neg_mru? ho->mru: PPP_MRU;
153 /* already have a bundle */
154 cfg_bundle(0, 0, 0, 0);
158 make_new_bundle(0, 0, 0, 0);
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.
172 l = 4 * strlen(peer_authname) + 10;
173 if (ho->neg_endpoint)
174 l += 3 * ho->endpoint.length + 8;
176 l += 3 * strlen(bundle_name) + 2;
177 bundle_id = malloc(l);
179 novm("bundle identifier");
182 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
183 if (ho->neg_endpoint || bundle_name)
185 if (ho->neg_endpoint)
186 p += slprintf(p, bundle_id+l-p, "%s",
187 epdisc_to_str(&ho->endpoint));
189 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
191 /* Make the key for the list of links belonging to the bundle */
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);
199 * For demand mode, we only need to configure the bundle
200 * and attach the link.
202 mtu = MIN(ho->mrru, ao->mru);
204 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
206 ppp_script_setenv("BUNDLE", bundle_id + 7, 1);
211 * Check if the bundle ID is already in the database.
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))
237 /* attach to existing unit */
238 if (bundle_attach(unit)) {
240 ppp_script_setenv("BUNDLE", bundle_id + 7, 0);
241 make_bundle_links(1);
243 info("Link attached to %s", ifname);
246 /* attach failed because bundle doesn't exist */
249 /* we have to make a new bundle */
250 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
253 ppp_script_setenv("BUNDLE", bundle_id + 7, 1);
254 make_bundle_links(0);
256 info("New bundle %s created", ifname);
257 multilink_master = 1;
261 void mp_exit_bundle(void)
264 remove_bundle_link();
268 static void sendhup(char *str)
272 if (parse_num(str, "PPPD_PID=", &pid) && pid != getpid()) {
274 dbglog("sending SIGHUP to process %d", pid);
279 void mp_bundle_terminated(void)
283 bundle_terminating = 1;
284 upper_layers_down(0);
285 notice("Connection terminated.");
289 ppp_script_unsetenv("IFNAME");
294 iterate_bundle_links(sendhup);
295 key.dptr = blinks_id;
296 key.dsize = strlen(blinks_id);
297 tdb_delete(pppdb, key);
300 new_phase(PHASE_DEAD);
303 multilink_master = 0;
306 static void make_bundle_links(int append)
313 key.dptr = blinks_id;
314 key.dsize = strlen(blinks_id);
315 slprintf(entry, sizeof(entry), "%s;", db_key);
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");
326 l = rec.dsize + strlen(entry);
329 novm("bundle link list");
330 slprintf(p, l, "%s%s", rec.dptr, entry);
332 warn("bundle link list not found");
334 if (rec.dptr != NULL)
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");
346 static void remove_bundle_link(void)
353 key.dptr = blinks_id;
354 key.dsize = strlen(blinks_id);
355 slprintf(entry, sizeof(entry), "%s;", db_key);
357 rec = tdb_fetch(pppdb, key);
358 if (rec.dptr == NULL || rec.dsize <= 0) {
359 if (rec.dptr != NULL)
363 rec.dptr[rec.dsize-1] = 0;
364 p = strstr(rec.dptr, entry);
366 q = p + strlen(entry);
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)");
376 static void iterate_bundle_links(void (*func)(char *))
378 TDB_DATA key, rec, pp;
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)
392 while ((q = strchr(p, ';')) != NULL) {
396 pp = tdb_fetch(pppdb, key);
397 if (pp.dptr != NULL && pp.dsize > 0) {
398 pp.dptr[pp.dsize-1] = 0;
409 parse_num(char *str, const char *key, int *valp)
414 p = strstr(str, key);
417 i = strtol(p, &endp, 10);
418 if (endp != p && (*endp == 0 || *endp == ';')) {
427 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
430 owns_unit(TDB_DATA key, int unit)
436 slprintf(ifkey, sizeof(ifkey), "UNIT=%d", unit);
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;
449 get_default_epdisc(struct epdisc *ep)
454 /* First try for an ethernet MAC address */
455 if (get_first_ether_hwaddr(ep->value) >= 0) {
461 /* see if our hostname corresponds to a reasonable IP address */
462 hp = gethostbyname(hostname);
464 addr = *(u_int32_t *)hp->h_addr;
465 if (!ppp_bad_ip_addr(addr)) {
467 if (!LOCAL_IP_ADDR(addr)) {
469 set_ip_epdisc(ep, addr);
479 * epdisc_to_str - make a printable string from an endpoint discriminator.
482 static char *endp_class_names[] = {
483 "null", "local", "IP", "MAC", "magic", "phone"
487 epdisc_to_str(struct epdisc *ep)
489 static char str[MAX_ENDP_LEN*3+8];
490 u_char *p = ep->value;
494 if (ep->class == EPD_NULL && ep->length == 0)
496 if (ep->class == EPD_IP && ep->length == 4) {
500 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
506 if (ep->class == EPD_MAC && ep->length == 6)
508 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
511 if (ep->class <= EPD_PHONENUM)
512 q += slprintf(q, sizeof(str)-1, "%s",
513 endp_class_names[ep->class]);
515 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
517 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
518 if ((i & mask) == 0) {
522 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
527 static int hexc_val(int c)
537 str_to_epdisc(struct epdisc *ep, char *str)
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) {
549 if (i > EPD_PHONENUM) {
550 /* not a class name, try a decimal class number */
551 i = strtol(str, &endp, 10);
553 return 0; /* can't parse class number */
561 if (*str != ':' && *str != '.')
567 i = parse_dotted_ip(str, &addr);
568 if (i == 0 || str[i] != 0)
570 set_ip_epdisc(ep, addr);
573 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
579 for (l = 0; l < MAX_ENDP_LEN; ++l) {
583 for (p = str; isxdigit(*p); ++p)
588 ep->value[l] = hexc_val(*str++);
590 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
591 if (*str == ':' || *str == '.')
594 if (*str != 0 || (ep->class == EPD_MAC && l != 6))