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>
49 bool endpoint_specified; /* user gave explicit endpoint discriminator */
50 char *bundle_id; /* identifier for our bundle */
51 char *blinks_id; /* key for the list of links */
52 bool doing_multilink; /* multilink was enabled and agreed to */
53 bool multilink_master; /* we own the multilink bundle */
55 extern TDB_CONTEXT *pppdb;
58 static void make_bundle_links(int append);
59 static void remove_bundle_link(void);
60 static void iterate_bundle_links(void (*func)(char *));
62 static int get_default_epdisc(struct epdisc *);
63 static int parse_num(char *str, const char *key, int *valp);
64 static int owns_unit(TDB_DATA pid, int unit);
66 #define set_ip_epdisc(ep, addr) do { \
68 ep->value[0] = addr >> 24; \
69 ep->value[1] = addr >> 16; \
70 ep->value[2] = addr >> 8; \
71 ep->value[3] = addr; \
74 #define LOCAL_IP_ADDR(addr) \
75 (((addr) & 0xff000000) == 0x0a000000 /* 10.x.x.x */ \
76 || ((addr) & 0xfff00000) == 0xac100000 /* 172.16.x.x */ \
77 || ((addr) & 0xffff0000) == 0xc0a80000) /* 192.168.x.x */
79 #define process_exists(n) (kill((n), 0) == 0 || errno != ESRCH)
82 mp_check_options(void)
84 lcp_options *wo = &lcp_wantoptions[0];
85 lcp_options *ao = &lcp_allowoptions[0];
90 /* if we're doing multilink, we have to negotiate MRRU */
92 /* mrru not specified, default to mru */
99 if (!wo->neg_endpoint && !noendpoint) {
100 /* get a default endpoint value */
101 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
106 * Make a new bundle or join us to an existing bundle
107 * if we are doing multilink.
112 lcp_options *go = &lcp_gotoptions[0];
113 lcp_options *ho = &lcp_hisoptions[0];
114 lcp_options *ao = &lcp_allowoptions[0];
118 TDB_DATA key, pid, rec;
120 if (doing_multilink) {
121 /* have previously joined a bundle */
122 if (!go->neg_mrru || !ho->neg_mrru) {
123 notice("oops, didn't get multilink on renegotiation");
124 lcp_close(0, "multilink required");
127 /* XXX should check the peer_authname and ho->endpoint
128 are the same as previously */
132 if (!go->neg_mrru || !ho->neg_mrru) {
133 /* not doing multilink */
135 notice("oops, multilink negotiated only for receive");
136 mtu = ho->neg_mru? ho->mru: PPP_MRU;
140 /* already have a bundle */
141 cfg_bundle(0, 0, 0, 0);
142 netif_set_mtu(0, mtu);
145 make_new_bundle(0, 0, 0, 0);
147 netif_set_mtu(0, mtu);
154 * Find the appropriate bundle or join a new one.
155 * First we make up a name for the bundle.
156 * The length estimate is worst-case assuming every
157 * character has to be quoted.
159 l = 4 * strlen(peer_authname) + 10;
160 if (ho->neg_endpoint)
161 l += 3 * ho->endpoint.length + 8;
163 l += 3 * strlen(bundle_name) + 2;
164 bundle_id = malloc(l);
166 novm("bundle identifier");
169 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
170 if (ho->neg_endpoint || bundle_name)
172 if (ho->neg_endpoint)
173 p += slprintf(p, bundle_id+l-p, "%s",
174 epdisc_to_str(&ho->endpoint));
176 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
178 /* Make the key for the list of links belonging to the bundle */
180 blinks_id = malloc(l + 7);
181 if (blinks_id == NULL)
182 novm("bundle links key");
183 slprintf(blinks_id, l + 7, "BUNDLE_LINKS=%s", bundle_id + 7);
186 * For demand mode, we only need to configure the bundle
187 * and attach the link.
189 mtu = MIN(ho->mrru, ao->mru);
191 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
192 netif_set_mtu(0, mtu);
193 script_setenv("BUNDLE", bundle_id + 7, 1);
198 * Check if the bundle ID is already in the database.
202 key.dptr = bundle_id;
203 key.dsize = p - bundle_id;
204 pid = tdb_fetch(pppdb, key);
205 if (pid.dptr != NULL) {
206 /* bundle ID exists, see if the pppd record exists */
207 rec = tdb_fetch(pppdb, pid);
208 if (rec.dptr != NULL && rec.dsize > 0) {
209 /* make sure the string is null-terminated */
210 rec.dptr[rec.dsize-1] = 0;
211 /* parse the interface number */
212 parse_num(rec.dptr, "UNIT=", &unit);
213 /* check the pid value */
214 if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
215 || !process_exists(pppd_pid)
216 || !owns_unit(pid, unit))
224 /* attach to existing unit */
225 if (bundle_attach(unit)) {
227 script_setenv("BUNDLE", bundle_id + 7, 0);
228 make_bundle_links(1);
230 info("Link attached to %s", ifname);
233 /* attach failed because bundle doesn't exist */
236 /* we have to make a new bundle */
237 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
239 netif_set_mtu(0, mtu);
240 script_setenv("BUNDLE", bundle_id + 7, 1);
241 make_bundle_links(0);
243 info("New bundle %s created", ifname);
244 multilink_master = 1;
248 void mp_exit_bundle(void)
251 remove_bundle_link();
255 static void sendhup(char *str)
259 if (parse_num(str, "PPPD_PID=", &pid) && pid != getpid()) {
261 dbglog("sending SIGHUP to process %d", pid);
266 void mp_bundle_terminated(void)
270 bundle_terminating = 1;
271 upper_layers_down(0);
272 notice("Connection terminated.");
276 script_unsetenv("IFNAME");
281 iterate_bundle_links(sendhup);
282 key.dptr = blinks_id;
283 key.dsize = strlen(blinks_id);
284 tdb_delete(pppdb, key);
287 new_phase(PHASE_DEAD);
290 multilink_master = 0;
293 static void make_bundle_links(int append)
300 key.dptr = blinks_id;
301 key.dsize = strlen(blinks_id);
302 slprintf(entry, sizeof(entry), "%s;", db_key);
305 rec = tdb_fetch(pppdb, key);
306 if (rec.dptr != NULL && rec.dsize > 0) {
307 rec.dptr[rec.dsize-1] = 0;
308 if (strstr(rec.dptr, db_key) != NULL) {
309 /* already in there? strange */
310 warn("link entry already exists in tdb");
313 l = rec.dsize + strlen(entry);
316 novm("bundle link list");
317 slprintf(p, l, "%s%s", rec.dptr, entry);
319 warn("bundle link list not found");
321 if (rec.dptr != NULL)
325 rec.dsize = strlen(p) + 1;
326 if (tdb_store(pppdb, key, rec, TDB_REPLACE))
327 error("couldn't %s bundle link list",
328 append? "update": "create");
333 static void remove_bundle_link(void)
340 key.dptr = blinks_id;
341 key.dsize = strlen(blinks_id);
342 slprintf(entry, sizeof(entry), "%s;", db_key);
344 rec = tdb_fetch(pppdb, key);
345 if (rec.dptr == NULL || rec.dsize <= 0) {
346 if (rec.dptr != NULL)
350 rec.dptr[rec.dsize-1] = 0;
351 p = strstr(rec.dptr, entry);
353 q = p + strlen(entry);
356 rec.dsize = p - rec.dptr + l;
357 if (tdb_store(pppdb, key, rec, TDB_REPLACE))
358 error("couldn't update bundle link list (removal)");
363 static void iterate_bundle_links(void (*func)(char *))
365 TDB_DATA key, rec, pp;
368 key.dptr = blinks_id;
369 key.dsize = strlen(blinks_id);
370 rec = tdb_fetch(pppdb, key);
371 if (rec.dptr == NULL || rec.dsize <= 0) {
372 error("bundle link list not found (iterating list)");
373 if (rec.dptr != NULL)
379 while ((q = strchr(p, ';')) != NULL) {
383 pp = tdb_fetch(pppdb, key);
384 if (pp.dptr != NULL && pp.dsize > 0) {
385 pp.dptr[pp.dsize-1] = 0;
396 parse_num(char *str, const char *key, int *valp)
401 p = strstr(str, key);
404 i = strtol(p, &endp, 10);
405 if (endp != p && (*endp == 0 || *endp == ';')) {
414 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
417 owns_unit(TDB_DATA key, int unit)
423 slprintf(ifkey, sizeof(ifkey), "UNIT=%d", unit);
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;
436 get_default_epdisc(struct epdisc *ep)
441 /* First try for an ethernet MAC address */
442 if (get_first_ether_hwaddr(ep->value) >= 0) {
448 /* see if our hostname corresponds to a reasonable IP address */
449 hp = gethostbyname(hostname);
451 addr = *(u_int32_t *)hp->h_addr;
452 if (!bad_ip_adrs(addr)) {
454 if (!LOCAL_IP_ADDR(addr)) {
456 set_ip_epdisc(ep, addr);
466 * epdisc_to_str - make a printable string from an endpoint discriminator.
469 static char *endp_class_names[] = {
470 "null", "local", "IP", "MAC", "magic", "phone"
474 epdisc_to_str(struct epdisc *ep)
476 static char str[MAX_ENDP_LEN*3+8];
477 u_char *p = ep->value;
481 if (ep->class == EPD_NULL && ep->length == 0)
483 if (ep->class == EPD_IP && ep->length == 4) {
487 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
493 if (ep->class == EPD_MAC && ep->length == 6)
495 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
498 if (ep->class <= EPD_PHONENUM)
499 q += slprintf(q, sizeof(str)-1, "%s",
500 endp_class_names[ep->class]);
502 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
504 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
505 if ((i & mask) == 0) {
509 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
514 static int hexc_val(int c)
524 str_to_epdisc(struct epdisc *ep, char *str)
529 for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
530 int sl = strlen(endp_class_names[i]);
531 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
536 if (i > EPD_PHONENUM) {
537 /* not a class name, try a decimal class number */
538 i = strtol(str, &endp, 10);
540 return 0; /* can't parse class number */
548 if (*str != ':' && *str != '.')
554 i = parse_dotted_ip(str, &addr);
555 if (i == 0 || str[i] != 0)
557 set_ip_epdisc(ep, addr);
560 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
566 for (l = 0; l < MAX_ENDP_LEN; ++l) {
570 for (p = str; isxdigit(*p); ++p)
575 ep->value[l] = hexc_val(*str++);
577 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
578 if (*str == ':' || *str == '.')
581 if (*str != 0 || (ep->class == EPD_MAC && l != 6))