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 */
51 lcp_options *wo = &lcp_wantoptions[0];
52 lcp_options *ao = &lcp_allowoptions[0];
56 /* if we're doing multilink, we have to negotiate MRRU */
58 /* mrru not specified, default to mru */
65 if (!wo->neg_endpoint && !noendpoint) {
66 /* get a default endpoint value */
67 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
69 info("using default endpoint %s",
70 epdisc_to_str(&wo->endpoint));
75 * Make a new bundle or join us to an existing bundle
76 * if we are doing multilink.
81 lcp_options *go = &lcp_gotoptions[0];
82 lcp_options *ho = &lcp_hisoptions[0];
86 TDB_DATA key, pid, rec;
88 if (!go->neg_mrru || !ho->neg_mrru) {
89 /* not doing multilink */
91 notice("oops, multilink negotiated only for receive");
93 make_new_bundle(0, 0, 0, 0);
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.
104 l = 4 * strlen(peer_authname) + 10;
105 if (ho->neg_endpoint)
106 l += 3 * ho->endpoint.length + 8;
108 l += 3 * strlen(bundle_name) + 2;
109 bundle_id = malloc(l);
111 novm("bundle identifier");
114 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
115 if (ho->neg_endpoint || bundle_name)
117 if (ho->neg_endpoint)
118 p += slprintf(p, bundle_id+l-p, "%s",
119 epdisc_to_str(&ho->endpoint));
121 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
122 info("bundle_id = %s", bundle_id+7);
125 * Check if the bundle ID is already in the database.
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");
138 p += 10; /* skip to unit number */
139 i = strtol(p, &endp, 10);
140 if (endp != p && (*endp == 0 || *endp == ';'))
149 /* attach to existing unit */
150 if (bundle_attach(unit)) {
151 info("attached link to interface %d", ifunit);
153 script_setenv("BUNDLE", bundle_id + 7, 0);
156 /* attach failed because bundle doesn't exist */
159 /* we have to make a new bundle */
160 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
162 script_setenv("BUNDLE", bundle_id + 7, 1);
167 get_default_epdisc(ep)
174 /* First try for an ethernet MAC address */
175 p = get_first_ethernet();
176 if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
182 /* see if our hostname corresponds to a reasonable IP address */
183 hp = gethostbyname(hostname);
185 addr = *(u_int32_t *)hp->h_addr;
186 if (!bad_ip_adrs(addr)) {
188 if (!LOCAL_IP_ADDR(addr)) {
190 set_ip_epdisc(ep, addr);
200 * epdisc_to_str - make a printable string from an endpoint discriminator.
203 static char *endp_class_names[] = {
204 "null", "local", "IP", "MAC", "magic", "phone"
211 static char str[MAX_ENDP_LEN*3+8];
212 u_char *p = ep->value;
216 if (ep->class == EPD_NULL && ep->length == 0)
218 if (ep->class == EPD_IP && ep->length == 4) {
222 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
228 if (ep->class == EPD_MAC && ep->length == 6)
230 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
233 if (ep->class <= EPD_PHONENUM)
234 q += slprintf(q, sizeof(str)-1, "%s",
235 endp_class_names[ep->class]);
237 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
239 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
240 if ((i & mask) == 0) {
244 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
249 static int hexc_val(int c)
259 str_to_epdisc(ep, str)
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) {
273 if (i > EPD_PHONENUM) {
274 /* not a class name, try a decimal class number */
275 i = strtol(str, &endp, 10);
277 return 0; /* can't parse class number */
285 if (*str != ':' && *str != '.')
291 i = parse_dotted_ip(str, &addr);
292 if (i == 0 || str[i] != 0)
294 set_ip_epdisc(ep, addr);
295 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
298 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
300 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
305 for (l = 0; l < MAX_ENDP_LEN; ++l) {
309 for (p = str; isxdigit(*p); ++p)
314 ep->value[l] = hexc_val(*str++);
316 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
317 if (*str == ':' || *str == '.')
320 if (*str != 0 || (ep->class == EPD_MAC && l != 6))
323 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));