]> git.ozlabs.org Git - ppp.git/blob - pppd/multilink.c
596fd816d24e5332989db20388e5e36dd88a4ca1
[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 <netinet/in.h>
21
22 #include "pppd.h"
23 #include "fsm.h"
24 #include "lcp.h"
25 #include "tdb.h"
26
27 bool endpoint_specified;        /* user gave explicit endpoint discriminator */
28 char *bundle_id;                /* identifier for our bundle */
29
30 extern TDB_CONTEXT *pppdb;
31 extern char db_key[];
32
33 static int get_default_epdisc __P((struct epdisc *));
34
35 #define set_ip_epdisc(ep, addr) do {    \
36         ep->length = 4;                 \
37         ep->value[0] = addr >> 24;      \
38         ep->value[1] = addr >> 16;      \
39         ep->value[2] = addr >> 8;       \
40         ep->value[3] = addr;            \
41 } while (0)
42
43 #define LOCAL_IP_ADDR(addr)                                               \
44         (((addr) & 0xff000000) == 0x0a000000            /* 10.x.x.x */    \
45          || ((addr) & 0xfff00000) == 0xac100000         /* 172.16.x.x */  \
46          || ((addr) & 0xffff0000) == 0xc0a80000)        /* 192.168.x.x */
47
48 #ifdef HAVE_MULTILINK
49 void
50 mp_check_options()
51 {
52         lcp_options *wo = &lcp_wantoptions[0];
53         lcp_options *ao = &lcp_allowoptions[0];
54
55         if (!multilink)
56                 return;
57         /* if we're doing multilink, we have to negotiate MRRU */
58         if (!wo->neg_mrru) {
59                 /* mrru not specified, default to mru */
60                 wo->mrru = wo->mru;
61                 wo->neg_mrru = 1;
62         }
63         ao->mrru = ao->mru;
64         ao->neg_mrru = 1;
65
66         if (!wo->neg_endpoint && !noendpoint) {
67                 /* get a default endpoint value */
68                 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
69                 if (wo->neg_endpoint)
70                         info("using default endpoint %s",
71                              epdisc_to_str(&wo->endpoint));
72         }
73 }
74 #endif /* HAVE_MULTILINK */
75
76 #ifdef HAVE_MULTILINK
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         int unit;
87         int i, l;
88         char *p, *endp;
89         TDB_DATA key, pid, rec;
90
91         if (!go->neg_mrru || !ho->neg_mrru) {
92                 /* not doing multilink */
93                 if (go->neg_mrru)
94                         notice("oops, multilink negotiated only for receive");
95                 multilink = 0;
96                 make_new_bundle(0, 0, 0, 0);
97                 set_ifunit(1);
98                 return 0;
99         }
100
101         /*
102          * Find the appropriate bundle or join a new one.
103          * First we make up a name for the bundle.
104          * The length estimate is worst-case assuming every
105          * character has to be quoted.
106          */
107         l = 4 * strlen(peer_authname) + 10;
108         if (ho->neg_endpoint)
109                 l += 3 * ho->endpoint.length + 8;
110         if (bundle_name)
111                 l += 3 * strlen(bundle_name) + 2;
112         bundle_id = malloc(l);
113         if (bundle_id == 0)
114                 novm("bundle identifier");
115
116         p = bundle_id;
117         p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
118         if (ho->neg_endpoint || bundle_name)
119                 *p++ = '/';
120         if (ho->neg_endpoint)
121                 p += slprintf(p, bundle_id+l-p, "%s",
122                               epdisc_to_str(&ho->endpoint));
123         if (bundle_name)
124                 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
125         info("bundle_id = %s", bundle_id+7);
126
127         /*
128          * Check if the bundle ID is already in the database.
129          */
130         unit = -1;
131         key.dptr = bundle_id;
132         key.dsize = p - bundle_id;
133         pid = tdb_fetch(pppdb, key);
134         if (pid.dptr != NULL) {
135                 /* bundle ID exists, see if the pppd record still exists */
136                 rec = tdb_fetch(pppdb, pid);
137                 if (rec.dptr != NULL) {
138                         /* it is, parse the interface number */
139                         p = strstr(rec.dptr, "IFNAME=ppp");
140                         if (p != 0) {
141                                 p += 10;        /* skip to unit number */
142                                 i = strtol(p, &endp, 10);
143                                 if (endp != p && (*endp == 0 || *endp == ';'))
144                                         unit = i;
145                         }
146                         free(rec.dptr);
147                 }
148                 free(pid.dptr);
149         }
150
151         if (unit >= 0) {
152                 /* attach to existing unit */
153                 if (bundle_attach(unit)) {
154                         info("attached link to interface %d", ifunit);
155                         set_ifunit(0);
156                         script_setenv("BUNDLE", bundle_id + 7, 0);
157                         return 1;
158                 }
159                 /* attach failed because bundle doesn't exist */
160         }
161
162         /* we have to make a new bundle */
163         make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
164         set_ifunit(1);
165         script_setenv("BUNDLE", bundle_id + 7, 1);
166         return 0;
167 }
168 #endif /* HAVE_MULTILINK */
169
170 #ifdef /* HAVE_MULTILINK */
171 static int
172 get_default_epdisc(ep)
173      struct epdisc *ep;
174 {
175         char *p;
176         struct hostent *hp;
177         u_int32_t addr;
178
179         /* First try for an ethernet MAC address */
180         p = get_first_ethernet();
181         if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
182                 ep->class = EPD_MAC;
183                 ep->length = 6;
184                 return 1;
185         }
186
187         /* see if our hostname corresponds to a reasonable IP address */
188         hp = gethostbyname(hostname);
189         if (hp != NULL) {
190                 addr = *(u_int32_t *)hp->h_addr;
191                 if (!bad_ip_adrs(addr)) {
192                         addr = ntohl(addr);
193                         if (!LOCAL_IP_ADDR(addr)) {
194                                 ep->class = EPD_IP;
195                                 set_ip_epdisc(ep, addr);
196                                 return 1;
197                         }
198                 }
199         }
200
201         return 0;
202 }
203 #endif /* HAVE_MULTILINK */
204
205 /*
206  * epdisc_to_str - make a printable string from an endpoint discriminator.
207  */
208
209 static char *endp_class_names[] = {
210     "null", "local", "IP", "MAC", "magic", "phone"
211 };
212
213 char *
214 epdisc_to_str(ep)
215      struct epdisc *ep;
216 {
217         static char str[MAX_ENDP_LEN*3+8];
218         u_char *p = ep->value;
219         int i, mask = 0;
220         char *q, c, c2;
221
222         if (ep->class == EPD_NULL && ep->length == 0)
223                 return "null";
224         if (ep->class == EPD_IP && ep->length == 4) {
225                 u_int32_t addr;
226
227                 GETLONG(addr, p);
228                 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
229                 return str;
230         }
231
232         c = ':';
233         c2 = '.';
234         if (ep->class == EPD_MAC && ep->length == 6)
235                 c2 = ':';
236         else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
237                 mask = 3;
238         q = str;
239         if (ep->class <= EPD_PHONENUM)
240                 q += slprintf(q, sizeof(str)-1, "%s",
241                               endp_class_names[ep->class]);
242         else
243                 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
244         c = ':';
245         for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
246                 if ((i & mask) == 0) {
247                         *q++ = c;
248                         c = c2;
249                 }
250                 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
251         }
252         return str;
253 }
254
255 #ifdef HAVE_MULTILINK
256 static int hexc_val(int c)
257 {
258         if (c >= 'a')
259                 return c - 'a' + 10;
260         if (c >= 'A')
261                 return c - 'A' + 10;
262         return c - '0';
263 }
264 #endif /* HAVE_MULTILINK */
265
266 #ifdef HAVE_MULTILINK
267 int
268 str_to_epdisc(ep, str)
269      struct epdisc *ep;
270      char *str;
271 {
272         int i, l;
273         char *p, *endp;
274
275         for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
276                 int sl = strlen(endp_class_names[i]);
277                 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
278                         str += sl;
279                         break;
280                 }
281         }
282         if (i > EPD_PHONENUM) {
283                 /* not a class name, try a decimal class number */
284                 i = strtol(str, &endp, 10);
285                 if (endp == str)
286                         return 0;       /* can't parse class number */
287                 str = endp;
288         }
289         ep->class = i;
290         if (*str == 0) {
291                 ep->length = 0;
292                 return 1;
293         }
294         if (*str != ':' && *str != '.')
295                 return 0;
296         ++str;
297
298         if (i == EPD_IP) {
299                 u_int32_t addr;
300                 i = parse_dotted_ip(str, &addr);
301                 if (i == 0 || str[i] != 0)
302                         return 0;
303                 set_ip_epdisc(ep, addr);
304                 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
305                 return 1;
306         }
307         if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
308                 ep->length = 6;
309                 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
310                 return 1;
311         }
312
313         p = str;
314         for (l = 0; l < MAX_ENDP_LEN; ++l) {
315                 if (*str == 0)
316                         break;
317                 if (p <= str)
318                         for (p = str; isxdigit(*p); ++p)
319                                 ;
320                 i = p - str;
321                 if (i == 0)
322                         return 0;
323                 ep->value[l] = hexc_val(*str++);
324                 if ((i & 1) == 0)
325                         ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
326                 if (*str == ':' || *str == '.')
327                         ++str;
328         }
329         if (*str != 0 || (ep->class == EPD_MAC && l != 6))
330                 return 0;
331         ep->length = l;
332         dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
333         return 1;
334 }
335 #endif /* HAVE_MULTILINK */
336