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