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