]> git.ozlabs.org Git - ppp.git/blob - pppd/multilink.c
c3a782f284662df0fce296d1e83518bd9a654f5f
[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((n), 0) == 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         lcp_options *ao = &lcp_allowoptions[0];
90         int unit, pppd_pid;
91         int l, mtu;
92         char *p;
93         TDB_DATA key, pid, rec;
94
95         if (!go->neg_mrru || !ho->neg_mrru) {
96                 /* not doing multilink */
97                 if (go->neg_mrru)
98                         notice("oops, multilink negotiated only for receive");
99                 mtu = ho->neg_mru? ho->mru: PPP_MRU;
100                 if (mtu > ao->mru)
101                         mtu = ao->mru;
102                 if (demand) {
103                         /* already have a bundle */
104                         cfg_bundle(0, 0, 0, 0);
105                         netif_set_mtu(0, mtu);
106                         return 0;
107                 }
108                 make_new_bundle(0, 0, 0, 0);
109                 netif_set_mtu(0, mtu);
110                 set_ifunit(1);
111                 return 0;
112         }
113
114         /*
115          * Find the appropriate bundle or join a new one.
116          * First we make up a name for the bundle.
117          * The length estimate is worst-case assuming every
118          * character has to be quoted.
119          */
120         l = 4 * strlen(peer_authname) + 10;
121         if (ho->neg_endpoint)
122                 l += 3 * ho->endpoint.length + 8;
123         if (bundle_name)
124                 l += 3 * strlen(bundle_name) + 2;
125         bundle_id = malloc(l);
126         if (bundle_id == 0)
127                 novm("bundle identifier");
128
129         p = bundle_id;
130         p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
131         if (ho->neg_endpoint || bundle_name)
132                 *p++ = '/';
133         if (ho->neg_endpoint)
134                 p += slprintf(p, bundle_id+l-p, "%s",
135                               epdisc_to_str(&ho->endpoint));
136         if (bundle_name)
137                 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
138
139         /*
140          * For demand mode, we only need to configure the bundle
141          * and attach the link.
142          */
143         mtu = MIN(ho->mrru, ao->mru);
144         if (demand) {
145                 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
146                 netif_set_mtu(0, mtu);
147                 script_setenv("BUNDLE", bundle_id + 7, 1);
148                 return 0;
149         }
150
151         /*
152          * Check if the bundle ID is already in the database.
153          */
154         unit = -1;
155         tdb_writelock(pppdb);
156         key.dptr = bundle_id;
157         key.dsize = p - bundle_id;
158         pid = tdb_fetch(pppdb, key);
159         if (pid.dptr != NULL) {
160                 /* bundle ID exists, see if the pppd record exists */
161                 rec = tdb_fetch(pppdb, pid);
162                 if (rec.dptr != NULL) {
163                         /* it is, parse the interface number */
164                         parse_num(rec.dptr, "IFNAME=ppp", &unit);
165                         /* check the pid value */
166                         if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
167                             || !process_exists(pppd_pid)
168                             || !owns_unit(pid, unit))
169                                 unit = -1;
170                         free(rec.dptr);
171                 }
172                 free(pid.dptr);
173         }
174
175         if (unit >= 0) {
176                 /* attach to existing unit */
177                 if (bundle_attach(unit)) {
178                         set_ifunit(0);
179                         script_setenv("BUNDLE", bundle_id + 7, 0);
180                         tdb_writeunlock(pppdb);
181                         info("Link attached to %s", ifname);
182                         return 1;
183                 }
184                 /* attach failed because bundle doesn't exist */
185         }
186
187         /* we have to make a new bundle */
188         make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
189         netif_set_mtu(0, mtu);
190         set_ifunit(1);
191         script_setenv("BUNDLE", bundle_id + 7, 1);
192         tdb_writeunlock(pppdb);
193         info("New bundle %s created", ifname);
194         return 0;
195 }
196
197 static int
198 parse_num(str, key, valp)
199      char *str;
200      const char *key;
201      int *valp;
202 {
203         char *p, *endp;
204         int i;
205
206         p = strstr(str, key);
207         if (p != 0) {
208                 p += strlen(key);
209                 i = strtol(p, &endp, 10);
210                 if (endp != p && (*endp == 0 || *endp == ';')) {
211                         *valp = i;
212                         return 1;
213                 }
214         }
215         return 0;
216 }
217
218 /*
219  * Check whether the pppd identified by `key' still owns ppp unit `unit'.
220  */
221 static int
222 owns_unit(key, unit)
223      TDB_DATA key;
224      int unit;
225 {
226         char ifkey[32];
227         TDB_DATA kd, vd;
228         int ret = 0;
229
230         slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit);
231         kd.dptr = ifkey;
232         kd.dsize = strlen(ifkey);
233         vd = tdb_fetch(pppdb, kd);
234         if (vd.dptr != NULL) {
235                 ret = vd.dsize == key.dsize
236                         && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
237                 free(vd.dptr);
238         }
239         return ret;
240 }
241
242 static int
243 get_default_epdisc(ep)
244      struct epdisc *ep;
245 {
246         char *p;
247         struct hostent *hp;
248         u_int32_t addr;
249
250         /* First try for an ethernet MAC address */
251         p = get_first_ethernet();
252         if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
253                 ep->class = EPD_MAC;
254                 ep->length = 6;
255                 return 1;
256         }
257
258         /* see if our hostname corresponds to a reasonable IP address */
259         hp = gethostbyname(hostname);
260         if (hp != NULL) {
261                 addr = *(u_int32_t *)hp->h_addr;
262                 if (!bad_ip_adrs(addr)) {
263                         addr = ntohl(addr);
264                         if (!LOCAL_IP_ADDR(addr)) {
265                                 ep->class = EPD_IP;
266                                 set_ip_epdisc(ep, addr);
267                                 return 1;
268                         }
269                 }
270         }
271
272         return 0;
273 }
274
275 /*
276  * epdisc_to_str - make a printable string from an endpoint discriminator.
277  */
278
279 static char *endp_class_names[] = {
280     "null", "local", "IP", "MAC", "magic", "phone"
281 };
282
283 char *
284 epdisc_to_str(ep)
285      struct epdisc *ep;
286 {
287         static char str[MAX_ENDP_LEN*3+8];
288         u_char *p = ep->value;
289         int i, mask = 0;
290         char *q, c, c2;
291
292         if (ep->class == EPD_NULL && ep->length == 0)
293                 return "null";
294         if (ep->class == EPD_IP && ep->length == 4) {
295                 u_int32_t addr;
296
297                 GETLONG(addr, p);
298                 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
299                 return str;
300         }
301
302         c = ':';
303         c2 = '.';
304         if (ep->class == EPD_MAC && ep->length == 6)
305                 c2 = ':';
306         else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
307                 mask = 3;
308         q = str;
309         if (ep->class <= EPD_PHONENUM)
310                 q += slprintf(q, sizeof(str)-1, "%s",
311                               endp_class_names[ep->class]);
312         else
313                 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
314         c = ':';
315         for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
316                 if ((i & mask) == 0) {
317                         *q++ = c;
318                         c = c2;
319                 }
320                 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
321         }
322         return str;
323 }
324
325 static int hexc_val(int c)
326 {
327         if (c >= 'a')
328                 return c - 'a' + 10;
329         if (c >= 'A')
330                 return c - 'A' + 10;
331         return c - '0';
332 }
333
334 int
335 str_to_epdisc(ep, str)
336      struct epdisc *ep;
337      char *str;
338 {
339         int i, l;
340         char *p, *endp;
341
342         for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
343                 int sl = strlen(endp_class_names[i]);
344                 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
345                         str += sl;
346                         break;
347                 }
348         }
349         if (i > EPD_PHONENUM) {
350                 /* not a class name, try a decimal class number */
351                 i = strtol(str, &endp, 10);
352                 if (endp == str)
353                         return 0;       /* can't parse class number */
354                 str = endp;
355         }
356         ep->class = i;
357         if (*str == 0) {
358                 ep->length = 0;
359                 return 1;
360         }
361         if (*str != ':' && *str != '.')
362                 return 0;
363         ++str;
364
365         if (i == EPD_IP) {
366                 u_int32_t addr;
367                 i = parse_dotted_ip(str, &addr);
368                 if (i == 0 || str[i] != 0)
369                         return 0;
370                 set_ip_epdisc(ep, addr);
371                 return 1;
372         }
373         if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
374                 ep->length = 6;
375                 return 1;
376         }
377
378         p = str;
379         for (l = 0; l < MAX_ENDP_LEN; ++l) {
380                 if (*str == 0)
381                         break;
382                 if (p <= str)
383                         for (p = str; isxdigit(*p); ++p)
384                                 ;
385                 i = p - str;
386                 if (i == 0)
387                         return 0;
388                 ep->value[l] = hexc_val(*str++);
389                 if ((i & 1) == 0)
390                         ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
391                 if (*str == ':' || *str == '.')
392                         ++str;
393         }
394         if (*str != 0 || (ep->class == EPD_MAC && l != 6))
395                 return 0;
396         ep->length = l;
397         return 1;
398 }
399