]> git.ozlabs.org Git - ppp.git/blob - pppd/multilink.c
Remove the requirement that redistributions in binary form reproduce
[ppp.git] / pppd / multilink.c
1 /*
2  * multilink.c - support routines for multilink.
3  *
4  * Copyright (c) 2000-2002 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. 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.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  *    acknowledgment:
19  *    "This product includes software developed by Paul Mackerras
20  *     <paulus@samba.org>".
21  *
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.
29  */
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <netdb.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <netinet/in.h>
37
38 #include "pppd.h"
39 #include "fsm.h"
40 #include "lcp.h"
41 #include "tdb.h"
42
43 bool endpoint_specified;        /* user gave explicit endpoint discriminator */
44 char *bundle_id;                /* identifier for our bundle */
45
46 extern TDB_CONTEXT *pppdb;
47 extern char db_key[];
48
49 static int get_default_epdisc __P((struct epdisc *));
50 static int parse_num __P((char *str, const char *key, int *valp));
51 static int owns_unit __P((TDB_DATA pid, int unit));
52
53 #define set_ip_epdisc(ep, addr) do {    \
54         ep->length = 4;                 \
55         ep->value[0] = addr >> 24;      \
56         ep->value[1] = addr >> 16;      \
57         ep->value[2] = addr >> 8;       \
58         ep->value[3] = addr;            \
59 } while (0)
60
61 #define LOCAL_IP_ADDR(addr)                                               \
62         (((addr) & 0xff000000) == 0x0a000000            /* 10.x.x.x */    \
63          || ((addr) & 0xfff00000) == 0xac100000         /* 172.16.x.x */  \
64          || ((addr) & 0xffff0000) == 0xc0a80000)        /* 192.168.x.x */
65
66 #define process_exists(n)       (kill((n), 0) == 0 || errno != ESRCH)
67
68 void
69 mp_check_options()
70 {
71         lcp_options *wo = &lcp_wantoptions[0];
72         lcp_options *ao = &lcp_allowoptions[0];
73
74         if (!multilink)
75                 return;
76         /* if we're doing multilink, we have to negotiate MRRU */
77         if (!wo->neg_mrru) {
78                 /* mrru not specified, default to mru */
79                 wo->mrru = wo->mru;
80                 wo->neg_mrru = 1;
81         }
82         ao->mrru = ao->mru;
83         ao->neg_mrru = 1;
84
85         if (!wo->neg_endpoint && !noendpoint) {
86                 /* get a default endpoint value */
87                 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
88         }
89 }
90
91 /*
92  * Make a new bundle or join us to an existing bundle
93  * if we are doing multilink.
94  */
95 int
96 mp_join_bundle()
97 {
98         lcp_options *go = &lcp_gotoptions[0];
99         lcp_options *ho = &lcp_hisoptions[0];
100         lcp_options *ao = &lcp_allowoptions[0];
101         int unit, pppd_pid;
102         int l, mtu;
103         char *p;
104         TDB_DATA key, pid, rec;
105
106         if (!go->neg_mrru || !ho->neg_mrru) {
107                 /* not doing multilink */
108                 if (go->neg_mrru)
109                         notice("oops, multilink negotiated only for receive");
110                 mtu = ho->neg_mru? ho->mru: PPP_MRU;
111                 if (mtu > ao->mru)
112                         mtu = ao->mru;
113                 if (demand) {
114                         /* already have a bundle */
115                         cfg_bundle(0, 0, 0, 0);
116                         netif_set_mtu(0, mtu);
117                         return 0;
118                 }
119                 make_new_bundle(0, 0, 0, 0);
120                 set_ifunit(1);
121                 netif_set_mtu(0, mtu);
122                 return 0;
123         }
124
125         /*
126          * Find the appropriate bundle or join a new one.
127          * First we make up a name for the bundle.
128          * The length estimate is worst-case assuming every
129          * character has to be quoted.
130          */
131         l = 4 * strlen(peer_authname) + 10;
132         if (ho->neg_endpoint)
133                 l += 3 * ho->endpoint.length + 8;
134         if (bundle_name)
135                 l += 3 * strlen(bundle_name) + 2;
136         bundle_id = malloc(l);
137         if (bundle_id == 0)
138                 novm("bundle identifier");
139
140         p = bundle_id;
141         p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
142         if (ho->neg_endpoint || bundle_name)
143                 *p++ = '/';
144         if (ho->neg_endpoint)
145                 p += slprintf(p, bundle_id+l-p, "%s",
146                               epdisc_to_str(&ho->endpoint));
147         if (bundle_name)
148                 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
149
150         /*
151          * For demand mode, we only need to configure the bundle
152          * and attach the link.
153          */
154         mtu = MIN(ho->mrru, ao->mru);
155         if (demand) {
156                 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
157                 netif_set_mtu(0, mtu);
158                 script_setenv("BUNDLE", bundle_id + 7, 1);
159                 return 0;
160         }
161
162         /*
163          * Check if the bundle ID is already in the database.
164          */
165         unit = -1;
166         tdb_writelock(pppdb);
167         key.dptr = bundle_id;
168         key.dsize = p - bundle_id;
169         pid = tdb_fetch(pppdb, key);
170         if (pid.dptr != NULL) {
171                 /* bundle ID exists, see if the pppd record exists */
172                 rec = tdb_fetch(pppdb, pid);
173                 if (rec.dptr != NULL) {
174                         /* it is, parse the interface number */
175                         parse_num(rec.dptr, "IFNAME=ppp", &unit);
176                         /* check the pid value */
177                         if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
178                             || !process_exists(pppd_pid)
179                             || !owns_unit(pid, unit))
180                                 unit = -1;
181                         free(rec.dptr);
182                 }
183                 free(pid.dptr);
184         }
185
186         if (unit >= 0) {
187                 /* attach to existing unit */
188                 if (bundle_attach(unit)) {
189                         set_ifunit(0);
190                         script_setenv("BUNDLE", bundle_id + 7, 0);
191                         tdb_writeunlock(pppdb);
192                         info("Link attached to %s", ifname);
193                         return 1;
194                 }
195                 /* attach failed because bundle doesn't exist */
196         }
197
198         /* we have to make a new bundle */
199         make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
200         set_ifunit(1);
201         netif_set_mtu(0, mtu);
202         script_setenv("BUNDLE", bundle_id + 7, 1);
203         tdb_writeunlock(pppdb);
204         info("New bundle %s created", ifname);
205         return 0;
206 }
207
208 static int
209 parse_num(str, key, valp)
210      char *str;
211      const char *key;
212      int *valp;
213 {
214         char *p, *endp;
215         int i;
216
217         p = strstr(str, key);
218         if (p != 0) {
219                 p += strlen(key);
220                 i = strtol(p, &endp, 10);
221                 if (endp != p && (*endp == 0 || *endp == ';')) {
222                         *valp = i;
223                         return 1;
224                 }
225         }
226         return 0;
227 }
228
229 /*
230  * Check whether the pppd identified by `key' still owns ppp unit `unit'.
231  */
232 static int
233 owns_unit(key, unit)
234      TDB_DATA key;
235      int unit;
236 {
237         char ifkey[32];
238         TDB_DATA kd, vd;
239         int ret = 0;
240
241         slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit);
242         kd.dptr = ifkey;
243         kd.dsize = strlen(ifkey);
244         vd = tdb_fetch(pppdb, kd);
245         if (vd.dptr != NULL) {
246                 ret = vd.dsize == key.dsize
247                         && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
248                 free(vd.dptr);
249         }
250         return ret;
251 }
252
253 static int
254 get_default_epdisc(ep)
255      struct epdisc *ep;
256 {
257         char *p;
258         struct hostent *hp;
259         u_int32_t addr;
260
261         /* First try for an ethernet MAC address */
262         p = get_first_ethernet();
263         if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
264                 ep->class = EPD_MAC;
265                 ep->length = 6;
266                 return 1;
267         }
268
269         /* see if our hostname corresponds to a reasonable IP address */
270         hp = gethostbyname(hostname);
271         if (hp != NULL) {
272                 addr = *(u_int32_t *)hp->h_addr;
273                 if (!bad_ip_adrs(addr)) {
274                         addr = ntohl(addr);
275                         if (!LOCAL_IP_ADDR(addr)) {
276                                 ep->class = EPD_IP;
277                                 set_ip_epdisc(ep, addr);
278                                 return 1;
279                         }
280                 }
281         }
282
283         return 0;
284 }
285
286 /*
287  * epdisc_to_str - make a printable string from an endpoint discriminator.
288  */
289
290 static char *endp_class_names[] = {
291     "null", "local", "IP", "MAC", "magic", "phone"
292 };
293
294 char *
295 epdisc_to_str(ep)
296      struct epdisc *ep;
297 {
298         static char str[MAX_ENDP_LEN*3+8];
299         u_char *p = ep->value;
300         int i, mask = 0;
301         char *q, c, c2;
302
303         if (ep->class == EPD_NULL && ep->length == 0)
304                 return "null";
305         if (ep->class == EPD_IP && ep->length == 4) {
306                 u_int32_t addr;
307
308                 GETLONG(addr, p);
309                 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
310                 return str;
311         }
312
313         c = ':';
314         c2 = '.';
315         if (ep->class == EPD_MAC && ep->length == 6)
316                 c2 = ':';
317         else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
318                 mask = 3;
319         q = str;
320         if (ep->class <= EPD_PHONENUM)
321                 q += slprintf(q, sizeof(str)-1, "%s",
322                               endp_class_names[ep->class]);
323         else
324                 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
325         c = ':';
326         for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
327                 if ((i & mask) == 0) {
328                         *q++ = c;
329                         c = c2;
330                 }
331                 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
332         }
333         return str;
334 }
335
336 static int hexc_val(int c)
337 {
338         if (c >= 'a')
339                 return c - 'a' + 10;
340         if (c >= 'A')
341                 return c - 'A' + 10;
342         return c - '0';
343 }
344
345 int
346 str_to_epdisc(ep, str)
347      struct epdisc *ep;
348      char *str;
349 {
350         int i, l;
351         char *p, *endp;
352
353         for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
354                 int sl = strlen(endp_class_names[i]);
355                 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
356                         str += sl;
357                         break;
358                 }
359         }
360         if (i > EPD_PHONENUM) {
361                 /* not a class name, try a decimal class number */
362                 i = strtol(str, &endp, 10);
363                 if (endp == str)
364                         return 0;       /* can't parse class number */
365                 str = endp;
366         }
367         ep->class = i;
368         if (*str == 0) {
369                 ep->length = 0;
370                 return 1;
371         }
372         if (*str != ':' && *str != '.')
373                 return 0;
374         ++str;
375
376         if (i == EPD_IP) {
377                 u_int32_t addr;
378                 i = parse_dotted_ip(str, &addr);
379                 if (i == 0 || str[i] != 0)
380                         return 0;
381                 set_ip_epdisc(ep, addr);
382                 return 1;
383         }
384         if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
385                 ep->length = 6;
386                 return 1;
387         }
388
389         p = str;
390         for (l = 0; l < MAX_ENDP_LEN; ++l) {
391                 if (*str == 0)
392                         break;
393                 if (p <= str)
394                         for (p = str; isxdigit(*p); ++p)
395                                 ;
396                 i = p - str;
397                 if (i == 0)
398                         return 0;
399                 ep->value[l] = hexc_val(*str++);
400                 if ((i & 1) == 0)
401                         ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
402                 if (*str == ':' || *str == '.')
403                         ++str;
404         }
405         if (*str != 0 || (ep->class == EPD_MAC && l != 6))
406                 return 0;
407         ep->length = l;
408         return 1;
409 }
410