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.
22 #include <netinet/in.h>
29 bool endpoint_specified; /* user gave explicit endpoint discriminator */
30 char *bundle_id; /* identifier for our bundle */
32 extern TDB_CONTEXT *pppdb;
35 static int get_default_epdisc __P((struct epdisc *));
36 static int parse_num __P((char *str, const char *key, int *valp));
37 static int owns_unit __P((TDB_DATA pid, int unit));
39 #define set_ip_epdisc(ep, addr) do { \
41 ep->value[0] = addr >> 24; \
42 ep->value[1] = addr >> 16; \
43 ep->value[2] = addr >> 8; \
44 ep->value[3] = addr; \
47 #define LOCAL_IP_ADDR(addr) \
48 (((addr) & 0xff000000) == 0x0a000000 /* 10.x.x.x */ \
49 || ((addr) & 0xfff00000) == 0xac100000 /* 172.16.x.x */ \
50 || ((addr) & 0xffff0000) == 0xc0a80000) /* 192.168.x.x */
52 #define process_exists(n) (kill(0, (n)) == 0 || errno != ESRCH)
57 lcp_options *wo = &lcp_wantoptions[0];
58 lcp_options *ao = &lcp_allowoptions[0];
62 /* if we're doing multilink, we have to negotiate MRRU */
64 /* mrru not specified, default to mru */
71 if (!wo->neg_endpoint && !noendpoint) {
72 /* get a default endpoint value */
73 wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
75 dbglog("using default endpoint %s",
76 epdisc_to_str(&wo->endpoint));
81 * Make a new bundle or join us to an existing bundle
82 * if we are doing multilink.
87 lcp_options *go = &lcp_gotoptions[0];
88 lcp_options *ho = &lcp_hisoptions[0];
92 TDB_DATA key, pid, rec;
94 if (!go->neg_mrru || !ho->neg_mrru) {
95 /* not doing multilink */
97 notice("oops, multilink negotiated only for receive");
99 make_new_bundle(0, 0, 0, 0);
105 * Find the appropriate bundle or join a new one.
106 * First we make up a name for the bundle.
107 * The length estimate is worst-case assuming every
108 * character has to be quoted.
110 l = 4 * strlen(peer_authname) + 10;
111 if (ho->neg_endpoint)
112 l += 3 * ho->endpoint.length + 8;
114 l += 3 * strlen(bundle_name) + 2;
115 bundle_id = malloc(l);
117 novm("bundle identifier");
120 p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
121 if (ho->neg_endpoint || bundle_name)
123 if (ho->neg_endpoint)
124 p += slprintf(p, bundle_id+l-p, "%s",
125 epdisc_to_str(&ho->endpoint));
127 p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
128 dbglog("bundle_id = %s", bundle_id+7);
131 * Check if the bundle ID is already in the database.
135 key.dptr = bundle_id;
136 key.dsize = p - bundle_id;
137 tdb_writelock(pppdb);
138 pid = tdb_fetch(pppdb, key);
139 if (pid.dptr != NULL) {
140 /* bundle ID exists, see if the pppd record still exists */
141 rec = tdb_fetch(pppdb, pid);
142 if (rec.dptr != NULL) {
143 /* it is, parse the interface number */
144 parse_num(rec.dptr, "IFNAME=ppp", &unit);
145 /* check the pid value */
146 parse_num(rec.dptr, "PPPD_PID=", &pppd_pid);
147 if (!process_exists(pppd_pid) || !owns_unit(pid, unit))
155 /* attach to existing unit, if the pid still exists */
156 if (bundle_attach(unit)) {
157 dbglog("attached link to interface %d", ifunit);
159 script_setenv("BUNDLE", bundle_id + 7, 0);
160 tdb_writeunlock(pppdb);
163 /* attach failed because bundle doesn't exist */
166 /* we have to make a new bundle */
167 make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
169 script_setenv("BUNDLE", bundle_id + 7, 1);
170 tdb_writeunlock(pppdb);
175 parse_num(str, key, valp)
183 p = strstr(str, key);
186 i = strtol(p, &endp, 10);
187 if (endp != p && (*endp == 0 || *endp == ';')) {
196 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
207 slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit);
209 kd.dsize = strlen(ifkey);
210 vd = tdb_fetch(pppdb, kd);
211 if (vd.dptr != NULL) {
212 ret = vd.dsize == key.dsize
213 && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
220 get_default_epdisc(ep)
227 /* First try for an ethernet MAC address */
228 p = get_first_ethernet();
229 if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
235 /* see if our hostname corresponds to a reasonable IP address */
236 hp = gethostbyname(hostname);
238 addr = *(u_int32_t *)hp->h_addr;
239 if (!bad_ip_adrs(addr)) {
241 if (!LOCAL_IP_ADDR(addr)) {
243 set_ip_epdisc(ep, addr);
253 * epdisc_to_str - make a printable string from an endpoint discriminator.
256 static char *endp_class_names[] = {
257 "null", "local", "IP", "MAC", "magic", "phone"
264 static char str[MAX_ENDP_LEN*3+8];
265 u_char *p = ep->value;
269 if (ep->class == EPD_NULL && ep->length == 0)
271 if (ep->class == EPD_IP && ep->length == 4) {
275 slprintf(str, sizeof(str), "IP:%I", htonl(addr));
281 if (ep->class == EPD_MAC && ep->length == 6)
283 else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
286 if (ep->class <= EPD_PHONENUM)
287 q += slprintf(q, sizeof(str)-1, "%s",
288 endp_class_names[ep->class]);
290 q += slprintf(q, sizeof(str)-1, "%d", ep->class);
292 for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
293 if ((i & mask) == 0) {
297 q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
302 static int hexc_val(int c)
312 str_to_epdisc(ep, str)
319 for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
320 int sl = strlen(endp_class_names[i]);
321 if (strncasecmp(str, endp_class_names[i], sl) == 0) {
326 if (i > EPD_PHONENUM) {
327 /* not a class name, try a decimal class number */
328 i = strtol(str, &endp, 10);
330 return 0; /* can't parse class number */
338 if (*str != ':' && *str != '.')
344 i = parse_dotted_ip(str, &addr);
345 if (i == 0 || str[i] != 0)
347 set_ip_epdisc(ep, addr);
348 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
351 if (i == EPD_MAC && get_if_hwaddr(ep->value, str) >= 0) {
353 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
358 for (l = 0; l < MAX_ENDP_LEN; ++l) {
362 for (p = str; isxdigit(*p); ++p)
367 ep->value[l] = hexc_val(*str++);
369 ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
370 if (*str == ':' || *str == '.')
373 if (*str != 0 || (ep->class == EPD_MAC && l != 6))
376 dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));