dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / pppd / multilink.c
blobc3e0c97c69b547c8a9789a596d915be846785545
1 /*
2 * multilink.c - support routines for multilink.
4 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
5 * All rights reserved.
7 * Copyright (c) 2000 Paul Mackerras.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms. The name of the author may not be
13 * used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #pragma ident "%Z%%M% %I% %E% SMI"
21 #define RCSID "$Id: $"
23 #include <string.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <netdb.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <netinet/in.h>
31 #include "pppd.h"
32 #include "fsm.h"
33 #include "lcp.h"
35 #ifdef HAVE_MULTILINK
36 #include "tdb.h"
37 #endif
38 static const char rcsid[] = RCSID;
40 #define set_ip_epdisc(ep, addr) ( \
41 ep->length = 4, \
42 ep->value[0] = addr >> 24, \
43 ep->value[1] = addr >> 16, \
44 ep->value[2] = addr >> 8, \
45 ep->value[3] = addr \
48 #ifdef HAVE_MULTILINK
49 bool endpoint_specified; /* user gave explicit endpoint discriminator */
50 char *bundle_id; /* identifier for our bundle */
52 static int get_default_epdisc __P((struct epdisc *));
53 static int parse_num __P((char *str, const char *key, int *valp));
54 static int owns_unit __P((TDB_DATA pid, int unit));
56 #define process_exists(n) (kill(0, (n)) == 0 || errno != ESRCH)
58 void
59 mp_check_options()
61 lcp_options *wo = &lcp_wantoptions[0];
62 lcp_options *ao = &lcp_allowoptions[0];
64 if (!multilink)
65 return;
66 /* if we're doing multilink, we have to negotiate MRRU */
67 if (!wo->neg_mrru) {
68 /* mrru not specified, default to mru */
69 wo->mrru = wo->mru;
70 wo->neg_mrru = 1;
72 ao->mrru = ao->mru;
73 ao->neg_mrru = 1;
75 if (!wo->neg_endpoint && !noendpoint) {
76 /* get a default endpoint value */
77 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
78 if (wo->neg_endpoint)
79 dbglog("using default endpoint %s",
80 epdisc_to_str(&wo->endpoint));
85 * Make a new bundle or join us to an existing bundle
86 * if we are doing multilink.
88 int
89 mp_join_bundle()
91 lcp_options *go = &lcp_gotoptions[0];
92 lcp_options *ho = &lcp_hisoptions[0];
93 int unit, pppd_pid;
94 int l;
95 char *p;
96 TDB_DATA key, pid, rec;
98 if (!go->neg_mrru || !ho->neg_mrru) {
99 /* not doing multilink */
100 if (go->neg_mrru)
101 notice("oops, multilink negotiated only for receive");
102 multilink = 0;
103 if (demand) {
104 /* already have a bundle */
105 cfg_bundle(0, 0, 0, 0);
106 return 0;
108 make_new_bundle(0, 0, 0, 0);
109 set_ifunit(1);
110 return 0;
114 * Find the appropriate bundle or join a new one.
115 * First we make up a name for the bundle.
116 * The length estimate is worst-case assuming every
117 * character has to be quoted.
119 * Note - RFC 1990 requires that an unnegotiated endpoint
120 * discriminator value be equivalent to negotiating with class
121 * zero. Do not test ho->neg_endpoint here.
123 l = 4 * strlen(peer_authname) + 10;
124 l += 3 * ho->endpoint.length + 8;
125 if (bundle_name)
126 l += 3 * strlen(bundle_name) + 2;
127 bundle_id = malloc(l);
128 if (bundle_id == NULL)
129 novm("bundle identifier");
131 p = bundle_id;
132 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
133 *p++ = '/';
134 p += slprintf(p, bundle_id+l-p, "%s", epdisc_to_str(&ho->endpoint));
135 if (bundle_name)
136 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
137 dbglog("bundle_id = %s", bundle_id+7);
140 * For demand mode, we only need to configure the bundle
141 * and attach the link.
143 if (demand) {
144 cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
145 script_setenv("BUNDLE", bundle_id + 7, 1);
146 return 0;
150 * Check if the bundle ID is already in the database.
152 unit = -1;
153 tdb_writelock(pppdb);
154 key.dptr = bundle_id;
155 key.dsize = p - bundle_id;
156 pid = tdb_fetch(pppdb, key);
157 if (pid.dptr != NULL) {
158 /* bundle ID exists, see if the pppd record exists */
159 rec = tdb_fetch(pppdb, pid);
160 if (rec.dptr != NULL) {
161 /* it does, parse the interface number */
162 parse_num(rec.dptr, "IFNAME=ppp", &unit);
163 /* check the pid value */
164 if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
165 || !process_exists(pppd_pid)
166 || !owns_unit(pid, unit))
167 unit = -1;
168 free(rec.dptr);
170 free(pid.dptr);
173 if (unit >= 0) {
174 /* attach to existing unit */
175 if (bundle_attach(unit)) {
176 set_ifunit(0);
177 script_setenv("BUNDLE", bundle_id + 7, 0);
178 tdb_writeunlock(pppdb);
179 info("Link attached to %s", ifname);
180 return 1;
182 /* attach failed because bundle doesn't exist */
185 /* we have to make a new bundle */
186 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
187 set_ifunit(1);
188 script_setenv("BUNDLE", bundle_id + 7, 1);
189 tdb_writeunlock(pppdb);
190 info("New bundle %s created", ifname);
191 return 0;
194 static int
195 parse_num(str, key, valp)
196 char *str;
197 const char *key;
198 int *valp;
200 char *p, *endp;
201 int i;
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;
212 return 0;
216 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
218 static int
219 owns_unit(key, unit)
220 TDB_DATA key;
221 int unit;
223 char ifkey[32];
224 TDB_DATA kd, vd;
225 int ret = 0;
227 (void) 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);
236 return ret;
239 static int
240 get_default_epdisc(ep)
241 struct epdisc *ep;
243 struct hostent *hp;
244 u_int32_t addr;
246 if (get_first_hwaddr(ep->value, sizeof(ep->value))) {
247 ep->class = EPD_MAC;
248 ep->length = 6;
249 return 1;
252 /* see if our hostname corresponds to a reasonable IP address */
253 hp = gethostbyname(hostname);
254 if (hp != NULL) {
255 addr = *(u_int32_t *)hp->h_addr;
256 if (!bad_ip_adrs(addr)) {
257 addr = ntohl(addr);
258 if (!LOCAL_IP_ADDR(addr)) {
259 ep->class = EPD_IP;
260 set_ip_epdisc(ep, addr);
261 return 1;
266 return 0;
268 #endif /* HAVE_MULTILINK */
271 * epdisc_to_str - make a printable string from an endpoint discriminator.
274 static char *endp_class_names[] = {
275 "null", "local", "IP", "MAC", "magic", "phone"
278 char *
279 epdisc_to_str(ep)
280 struct epdisc *ep;
282 static char str[MAX_ENDP_LEN*3+8];
283 u_char *p = ep->value;
284 int i, mask = 0;
285 char *q, c, c2;
287 if (ep->class == EPD_NULL && ep->length == 0)
288 return "null";
289 if (ep->class == EPD_IP && ep->length == 4) {
290 u_int32_t addr;
292 GETLONG(addr, p);
293 (void) slprintf(str, sizeof(str), "IP:%I", htonl(addr));
294 return str;
297 c = ':';
298 c2 = '.';
299 if (ep->class == EPD_MAC && ep->length == 6)
300 c2 = ':';
301 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
302 mask = 3;
303 q = str;
304 if (ep->class <= EPD_PHONENUM)
305 q += slprintf(q, sizeof(str)-1, "%s",
306 endp_class_names[ep->class]);
307 else
308 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
309 c = ':';
310 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
311 if ((i & mask) == 0) {
312 *q++ = c;
313 c = c2;
315 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
317 return str;
320 static int hexc_val(int c)
322 if (c >= 'a')
323 return c - 'a' + 10;
324 if (c >= 'A')
325 return c - 'A' + 10;
326 return c - '0';
330 str_to_epdisc(ep, str)
331 struct epdisc *ep;
332 char *str;
334 int i, l;
335 char *p, *endp;
337 for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
338 int sl = strlen(endp_class_names[i]);
339 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
340 str += sl;
341 break;
344 if (i > EPD_PHONENUM) {
345 /* not a class name, try a decimal class number */
346 i = strtol(str, &endp, 10);
347 if (endp == str) {
348 option_error("cannot parse endpoint class in \"%s\"",
349 str);
350 return 0; /* can't parse class number */
352 str = endp;
354 ep->class = i;
355 if (*str == 0) {
356 ep->length = 0;
357 return 1;
359 if (*str != ':' && *str != '.') {
360 option_error("invalid class/value separator '%c'", *str);
361 return 0;
363 ++str;
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 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
372 return 1;
374 if (i == EPD_MAC &&
375 get_if_hwaddr(ep->value, sizeof(ep->value), str) >= 0) {
376 ep->length = 6;
377 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
378 return 1;
381 p = str;
382 for (l = 0; l < MAX_ENDP_LEN; ++l) {
383 if (*str == 0)
384 break;
385 if (p <= str)
386 for (p = str; isxdigit(*p); ++p)
388 i = p - str;
389 if (i == 0) {
390 option_error("no valid hex digits in \"%s\"", str);
391 return 0;
393 ep->value[l] = hexc_val(*str++);
394 if ((i & 1) == 0)
395 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
396 if (*str == ':' || *str == '.')
397 ++str;
399 if (*str != 0) {
400 option_error("too many bytes in value; max is %d",
401 MAX_ENDP_LEN);
402 return 0;
404 if (ep->class == EPD_MAC && l != 6) {
405 option_error("bad endpoint; MAC address must have 6 bytes");
406 return 0;
408 ep->length = l;
409 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
410 return 1;