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. 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
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 #include <netinet/in.h>
48 bool endpoint_specified; /* user gave explicit endpoint discriminator */
49 char *bundle_id; /* identifier for our bundle */
51 extern TDB_CONTEXT *pppdb;
54 static int get_default_epdisc __P((struct epdisc *));
55 static int parse_num __P((char *str, const char *key, int *valp));
56 static int owns_unit __P((TDB_DATA pid, int unit));
58 #define set_ip_epdisc(ep, addr) do { \
60 ep->value[0] = addr >> 24; \
61 ep->value[1] = addr >> 16; \
62 ep->value[2] = addr >> 8; \
63 ep->value[3] = addr; \
66 #define LOCAL_IP_ADDR(addr) \
67 (((addr) & 0xff000000) == 0x0a000000 /* 10.x.x.x */ \
68 || ((addr) & 0xfff00000) == 0xac100000 /* 172.16.x.x */ \
69 || ((addr) & 0xffff0000) == 0xc0a80000) /* 192.168.x.x */
71 #define process_exists(n) (kill((n), 0) == 0 || errno != ESRCH)
76 lcp_options *wo = &lcp_wantoptions[0];
77 lcp_options *ao = &lcp_allowoptions[0];
81 /* if we're doing multilink, we have to negotiate MRRU */
83 /* mrru not specified, default to mru */
90 if (!wo->neg_endpoint && !noendpoint) {
91 /* get a default endpoint value */
92 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
97 * Make a new bundle or join us to an existing bundle
98 * if we are doing multilink.
103 lcp_options *go = &lcp_gotoptions[0];
104 lcp_options *ho = &lcp_hisoptions[0];
105 lcp_options *ao = &lcp_allowoptions[0];
109 TDB_DATA key, pid, rec;
111 if (!go->neg_mrru || !ho->neg_mrru) {
112 /* not doing multilink */
114 notice("oops, multilink negotiated only for receive");
115 mtu = ho->neg_mru? ho->mru: PPP_MRU;
119 /* already have a bundle */
120 cfg_bundle(0, 0, 0, 0);
121 netif_set_mtu(0, mtu);
124 make_new_bundle(0, 0, 0, 0);
126 netif_set_mtu(0, mtu);
131 * Find the appropriate bundle or join a new one.
132 * First we make up a name for the bundle.
133 * The length estimate is worst-case assuming every
134 * character has to be quoted.
136 l = 4 * strlen(peer_authname) + 10;
137 if (ho->neg_endpoint)
138 l += 3 * ho->endpoint.length + 8;
140 l += 3 * strlen(bundle_name) + 2;
141 bundle_id = malloc(l);
143 novm("bundle identifier");
146 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
147 if (ho->neg_endpoint || bundle_name)
149 if (ho->neg_endpoint)
150 p += slprintf(p, bundle_id+l-p, "%s",
151 epdisc_to_str(&ho->endpoint));
153 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
156 * For demand mode, we only need to configure the bundle
157 * and attach the link.
159 mtu = MIN(ho->mrru, ao->mru);
161 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
162 netif_set_mtu(0, mtu);
163 script_setenv("BUNDLE", bundle_id + 7, 1);
168 * Check if the bundle ID is already in the database.
171 tdb_writelock(pppdb);
172 key.dptr = bundle_id;
173 key.dsize = p - bundle_id;
174 pid = tdb_fetch(pppdb, key);
175 if (pid.dptr != NULL) {
176 /* bundle ID exists, see if the pppd record exists */
177 rec = tdb_fetch(pppdb, pid);
178 if (rec.dptr != NULL) {
179 /* it is, parse the interface number */
180 parse_num(rec.dptr, "IFNAME=ppp", &unit);
181 /* check the pid value */
182 if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
183 || !process_exists(pppd_pid)
184 || !owns_unit(pid, unit))
192 /* attach to existing unit */
193 if (bundle_attach(unit)) {
195 script_setenv("BUNDLE", bundle_id + 7, 0);
196 tdb_writeunlock(pppdb);
197 info("Link attached to %s", ifname);
200 /* attach failed because bundle doesn't exist */
203 /* we have to make a new bundle */
204 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
206 netif_set_mtu(0, mtu);
207 script_setenv("BUNDLE", bundle_id + 7, 1);
208 tdb_writeunlock(pppdb);
209 info("New bundle %s created", ifname);
214 parse_num(str, key, valp)
222 p = strstr(str, key);
225 i = strtol(p, &endp, 10);
226 if (endp != p && (*endp == 0 || *endp == ';')) {
235 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
246 slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit);
248 kd.dsize = strlen(ifkey);
249 vd = tdb_fetch(pppdb, kd);
250 if (vd.dptr != NULL) {
251 ret = vd.dsize == key.dsize
252 && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
259 get_default_epdisc(ep)
266 /* First try for an ethernet MAC address */
267 p = get_first_ethernet();
268 if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
274 /* see if our hostname corresponds to a reasonable IP address */
275 hp = gethostbyname(hostname);
277 addr = *(u_int32_t *)hp->h_addr;
278 if (!bad_ip_adrs(addr)) {
280 if (!LOCAL_IP_ADDR(addr)) {
282 set_ip_epdisc(ep, addr);
292 * epdisc_to_str - make a printable string from an endpoint discriminator.
295 static char *endp_class_names[] = {
296 "null", "local", "IP", "MAC", "magic", "phone"
303 static char str[MAX_ENDP_LEN*3+8];
304 u_char *p = ep->value;
308 if (ep->class == EPD_NULL && ep->length == 0)
310 if (ep->class == EPD_IP && ep->length == 4) {
314 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
320 if (ep->class == EPD_MAC && ep->length == 6)
322 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
325 if (ep->class <= EPD_PHONENUM)
326 q += slprintf(q, sizeof(str)-1, "%s",
327 endp_class_names[ep->class]);
329 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
331 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
332 if ((i & mask) == 0) {
336 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
341 static int hexc_val(int c)
351 str_to_epdisc(ep, str)
358 for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
359 int sl = strlen(endp_class_names[i]);
360 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
365 if (i > EPD_PHONENUM) {
366 /* not a class name, try a decimal class number */
367 i = strtol(str, &endp, 10);
369 return 0; /* can't parse class number */
377 if (*str != ':' && *str != '.')
383 i = parse_dotted_ip(str, &addr);
384 if (i == 0 || str[i] != 0)
386 set_ip_epdisc(ep, addr);
389 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
395 for (l = 0; l < MAX_ENDP_LEN; ++l) {
399 for (p = str; isxdigit(*p); ++p)
404 ep->value[l] = hexc_val(*str++);
406 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
407 if (*str == ':' || *str == '.')
410 if (*str != 0 || (ep->class == EPD_MAC && l != 6))