X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fmultilink.c;h=e5f2ac40d80098f15bc0f89ef007c4c47350be88;hp=596fd816d24e5332989db20388e5e36dd88a4ca1;hb=de87fc02dd7cc8518d50edfd9f9dd4a32bb74703;hpb=36a3914e400bbe547fbc991e94382c0cbb5df970 diff --git a/pppd/multilink.c b/pppd/multilink.c index 596fd81..e5f2ac4 100644 --- a/pppd/multilink.c +++ b/pppd/multilink.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include "pppd.h" @@ -31,6 +33,8 @@ extern TDB_CONTEXT *pppdb; extern char db_key[]; static int get_default_epdisc __P((struct epdisc *)); +static int parse_num __P((char *str, const char *key, int *valp)); +static int owns_unit __P((TDB_DATA pid, int unit)); #define set_ip_epdisc(ep, addr) do { \ ep->length = 4; \ @@ -45,7 +49,8 @@ static int get_default_epdisc __P((struct epdisc *)); || ((addr) & 0xfff00000) == 0xac100000 /* 172.16.x.x */ \ || ((addr) & 0xffff0000) == 0xc0a80000) /* 192.168.x.x */ -#ifdef HAVE_MULTILINK +#define process_exists(n) (kill((n), 0) == 0 || errno != ESRCH) + void mp_check_options() { @@ -66,14 +71,9 @@ mp_check_options() if (!wo->neg_endpoint && !noendpoint) { /* get a default endpoint value */ wo->neg_endpoint = get_default_epdisc(&wo->endpoint); - if (wo->neg_endpoint) - info("using default endpoint %s", - epdisc_to_str(&wo->endpoint)); } } -#endif /* HAVE_MULTILINK */ -#ifdef HAVE_MULTILINK /* * Make a new bundle or join us to an existing bundle * if we are doing multilink. @@ -83,18 +83,28 @@ mp_join_bundle() { lcp_options *go = &lcp_gotoptions[0]; lcp_options *ho = &lcp_hisoptions[0]; - int unit; - int i, l; - char *p, *endp; + lcp_options *ao = &lcp_allowoptions[0]; + int unit, pppd_pid; + int l, mtu; + char *p; TDB_DATA key, pid, rec; if (!go->neg_mrru || !ho->neg_mrru) { /* not doing multilink */ if (go->neg_mrru) notice("oops, multilink negotiated only for receive"); - multilink = 0; + mtu = ho->neg_mru? ho->mru: PPP_MRU; + if (mtu > ao->mru) + mtu = ao->mru; + if (demand) { + /* already have a bundle */ + cfg_bundle(0, 0, 0, 0); + netif_set_mtu(0, mtu); + return 0; + } make_new_bundle(0, 0, 0, 0); set_ifunit(1); + netif_set_mtu(0, mtu); return 0; } @@ -122,27 +132,38 @@ mp_join_bundle() epdisc_to_str(&ho->endpoint)); if (bundle_name) p += slprintf(p, bundle_id+l-p, "/%v", bundle_name); - info("bundle_id = %s", bundle_id+7); + + /* + * For demand mode, we only need to configure the bundle + * and attach the link. + */ + mtu = MIN(ho->mrru, ao->mru); + if (demand) { + cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf); + netif_set_mtu(0, mtu); + script_setenv("BUNDLE", bundle_id + 7, 1); + return 0; + } /* * Check if the bundle ID is already in the database. */ unit = -1; + tdb_writelock(pppdb); key.dptr = bundle_id; key.dsize = p - bundle_id; pid = tdb_fetch(pppdb, key); if (pid.dptr != NULL) { - /* bundle ID exists, see if the pppd record still exists */ + /* bundle ID exists, see if the pppd record exists */ rec = tdb_fetch(pppdb, pid); if (rec.dptr != NULL) { /* it is, parse the interface number */ - p = strstr(rec.dptr, "IFNAME=ppp"); - if (p != 0) { - p += 10; /* skip to unit number */ - i = strtol(p, &endp, 10); - if (endp != p && (*endp == 0 || *endp == ';')) - unit = i; - } + parse_num(rec.dptr, "IFNAME=ppp", &unit); + /* check the pid value */ + if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid) + || !process_exists(pppd_pid) + || !owns_unit(pid, unit)) + unit = -1; free(rec.dptr); } free(pid.dptr); @@ -151,9 +172,10 @@ mp_join_bundle() if (unit >= 0) { /* attach to existing unit */ if (bundle_attach(unit)) { - info("attached link to interface %d", ifunit); set_ifunit(0); script_setenv("BUNDLE", bundle_id + 7, 0); + tdb_writeunlock(pppdb); + info("Link attached to %s", ifname); return 1; } /* attach failed because bundle doesn't exist */ @@ -162,12 +184,58 @@ mp_join_bundle() /* we have to make a new bundle */ make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf); set_ifunit(1); + netif_set_mtu(0, mtu); script_setenv("BUNDLE", bundle_id + 7, 1); + tdb_writeunlock(pppdb); + info("New bundle %s created", ifname); + return 0; +} + +static int +parse_num(str, key, valp) + char *str; + const char *key; + int *valp; +{ + char *p, *endp; + int i; + + p = strstr(str, key); + if (p != 0) { + p += strlen(key); + i = strtol(p, &endp, 10); + if (endp != p && (*endp == 0 || *endp == ';')) { + *valp = i; + return 1; + } + } return 0; } -#endif /* HAVE_MULTILINK */ -#ifdef /* HAVE_MULTILINK */ +/* + * Check whether the pppd identified by `key' still owns ppp unit `unit'. + */ +static int +owns_unit(key, unit) + TDB_DATA key; + int unit; +{ + char ifkey[32]; + TDB_DATA kd, vd; + int ret = 0; + + slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit); + kd.dptr = ifkey; + kd.dsize = strlen(ifkey); + vd = tdb_fetch(pppdb, kd); + if (vd.dptr != NULL) { + ret = vd.dsize == key.dsize + && memcmp(vd.dptr, key.dptr, vd.dsize) == 0; + free(vd.dptr); + } + return ret; +} + static int get_default_epdisc(ep) struct epdisc *ep; @@ -200,7 +268,6 @@ get_default_epdisc(ep) return 0; } -#endif /* HAVE_MULTILINK */ /* * epdisc_to_str - make a printable string from an endpoint discriminator. @@ -252,7 +319,6 @@ epdisc_to_str(ep) return str; } -#ifdef HAVE_MULTILINK static int hexc_val(int c) { if (c >= 'a') @@ -261,9 +327,7 @@ static int hexc_val(int c) return c - 'A' + 10; return c - '0'; } -#endif /* HAVE_MULTILINK */ -#ifdef HAVE_MULTILINK int str_to_epdisc(ep, str) struct epdisc *ep; @@ -301,12 +365,10 @@ str_to_epdisc(ep, str) if (i == 0 || str[i] != 0) return 0; set_ip_epdisc(ep, addr); - dbglog("str_to_epdisc -> %s", epdisc_to_str(ep)); return 1; } if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) { ep->length = 6; - dbglog("str_to_epdisc -> %s", epdisc_to_str(ep)); return 1; } @@ -329,8 +391,6 @@ str_to_epdisc(ep, str) if (*str != 0 || (ep->class == EPD_MAC && l != 6)) return 0; ep->length = l; - dbglog("str_to_epdisc -> %s", epdisc_to_str(ep)); return 1; } -#endif /* HAVE_MULTILINK */