2 * multilink.c - support routines for multilink.
4 * Copyright (c) 2000 Paul Mackerras.
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.
20 #include <netinet/in.h>
27 bool endpoint_specified; /* user gave explicit endpoint discriminator */
28 char *bundle_id; /* identifier for our bundle */
30 extern TDB_CONTEXT *pppdb;
33 static int get_default_epdisc __P((struct epdisc *));
35 #define set_ip_epdisc(ep, addr) do { \
37 ep->value[0] = addr >> 24; \
38 ep->value[1] = addr >> 16; \
39 ep->value[2] = addr >> 8; \
40 ep->value[3] = addr; \
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 */
52 lcp_options *wo = &lcp_wantoptions[0];
53 lcp_options *ao = &lcp_allowoptions[0];
57 /* if we're doing multilink, we have to negotiate MRRU */
59 /* mrru not specified, default to mru */
66 if (!wo->neg_endpoint && !noendpoint) {
67 /* get a default endpoint value */
68 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
70 info("using default endpoint %s",
71 epdisc_to_str(&wo->endpoint));
74 #endif /* HAVE_MULTILINK */
78 * Make a new bundle or join us to an existing bundle
79 * if we are doing multilink.
84 lcp_options *go = &lcp_gotoptions[0];
85 lcp_options *ho = &lcp_hisoptions[0];
89 TDB_DATA key, pid, rec;
91 if (!go->neg_mrru || !ho->neg_mrru) {
92 /* not doing multilink */
94 notice("oops, multilink negotiated only for receive");
96 make_new_bundle(0, 0, 0, 0);
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.
107 l = 4 * strlen(peer_authname) + 10;
108 if (ho->neg_endpoint)
109 l += 3 * ho->endpoint.length + 8;
111 l += 3 * strlen(bundle_name) + 2;
112 bundle_id = malloc(l);
114 novm("bundle identifier");
117 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
118 if (ho->neg_endpoint || bundle_name)
120 if (ho->neg_endpoint)
121 p += slprintf(p, bundle_id+l-p, "%s",
122 epdisc_to_str(&ho->endpoint));
124 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
125 info("bundle_id = %s", bundle_id+7);
128 * Check if the bundle ID is already in the database.
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");
141 p += 10; /* skip to unit number */
142 i = strtol(p, &endp, 10);
143 if (endp != p && (*endp == 0 || *endp == ';'))
152 /* attach to existing unit */
153 if (bundle_attach(unit)) {
154 info("attached link to interface %d", ifunit);
156 script_setenv("BUNDLE", bundle_id + 7, 0);
159 /* attach failed because bundle doesn't exist */
162 /* we have to make a new bundle */
163 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
165 script_setenv("BUNDLE", bundle_id + 7, 1);
168 #endif /* HAVE_MULTILINK */
170 #ifdef /* HAVE_MULTILINK */
172 get_default_epdisc(ep)
179 /* First try for an ethernet MAC address */
180 p = get_first_ethernet();
181 if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
187 /* see if our hostname corresponds to a reasonable IP address */
188 hp = gethostbyname(hostname);
190 addr = *(u_int32_t *)hp->h_addr;
191 if (!bad_ip_adrs(addr)) {
193 if (!LOCAL_IP_ADDR(addr)) {
195 set_ip_epdisc(ep, addr);
203 #endif /* HAVE_MULTILINK */
206 * epdisc_to_str - make a printable string from an endpoint discriminator.
209 static char *endp_class_names[] = {
210 "null", "local", "IP", "MAC", "magic", "phone"
217 static char str[MAX_ENDP_LEN*3+8];
218 u_char *p = ep->value;
222 if (ep->class == EPD_NULL && ep->length == 0)
224 if (ep->class == EPD_IP && ep->length == 4) {
228 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
234 if (ep->class == EPD_MAC && ep->length == 6)
236 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
239 if (ep->class <= EPD_PHONENUM)
240 q += slprintf(q, sizeof(str)-1, "%s",
241 endp_class_names[ep->class]);
243 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
245 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
246 if ((i & mask) == 0) {
250 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
255 #ifdef HAVE_MULTILINK
256 static int hexc_val(int c)
264 #endif /* HAVE_MULTILINK */
266 #ifdef HAVE_MULTILINK
268 str_to_epdisc(ep, str)
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) {
282 if (i > EPD_PHONENUM) {
283 /* not a class name, try a decimal class number */
284 i = strtol(str, &endp, 10);
286 return 0; /* can't parse class number */
294 if (*str != ':' && *str != '.')
300 i = parse_dotted_ip(str, &addr);
301 if (i == 0 || str[i] != 0)
303 set_ip_epdisc(ep, addr);
304 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
307 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
309 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
314 for (l = 0; l < MAX_ENDP_LEN; ++l) {
318 for (p = str; isxdigit(*p); ++p)
323 ep->value[l] = hexc_val(*str++);
325 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
326 if (*str == ':' || *str == '.')
329 if (*str != 0 || (ep->class == EPD_MAC && l != 6))
332 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
335 #endif /* HAVE_MULTILINK */