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