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