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((n), 0) == 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
);
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];
86 lcp_options
*ao
= &lcp_allowoptions
[0];
90 TDB_DATA key
, pid
, rec
;
92 if (!go
->neg_mrru
|| !ho
->neg_mrru
) {
93 /* not doing multilink */
95 notice("oops, multilink negotiated only for receive");
96 mtu
= ho
->neg_mru
? ho
->mru
: PPP_MRU
;
100 /* already have a bundle */
101 cfg_bundle(0, 0, 0, 0);
102 netif_set_mtu(0, mtu
);
105 make_new_bundle(0, 0, 0, 0);
107 netif_set_mtu(0, mtu
);
112 * Find the appropriate bundle or join a new one.
113 * First we make up a name for the bundle.
114 * The length estimate is worst-case assuming every
115 * character has to be quoted.
117 l
= 4 * strlen(peer_authname
) + 10;
118 if (ho
->neg_endpoint
)
119 l
+= 3 * ho
->endpoint
.length
+ 8;
121 l
+= 3 * strlen(bundle_name
) + 2;
122 bundle_id
= malloc(l
);
124 novm("bundle identifier");
127 p
+= slprintf(p
, l
-1, "BUNDLE=\"%q\"", peer_authname
);
128 if (ho
->neg_endpoint
|| bundle_name
)
130 if (ho
->neg_endpoint
)
131 p
+= slprintf(p
, bundle_id
+l
-p
, "%s",
132 epdisc_to_str(&ho
->endpoint
));
134 p
+= slprintf(p
, bundle_id
+l
-p
, "/%v", bundle_name
);
137 * For demand mode, we only need to configure the bundle
138 * and attach the link.
140 mtu
= MIN(ho
->mrru
, ao
->mru
);
142 cfg_bundle(go
->mrru
, ho
->mrru
, go
->neg_ssnhf
, ho
->neg_ssnhf
);
143 netif_set_mtu(0, mtu
);
144 script_setenv("BUNDLE", bundle_id
+ 7, 1);
149 * Check if the bundle ID is already in the database.
152 tdb_writelock(pppdb
);
153 key
.dptr
= bundle_id
;
154 key
.dsize
= p
- bundle_id
;
155 pid
= tdb_fetch(pppdb
, key
);
156 if (pid
.dptr
!= NULL
) {
157 /* bundle ID exists, see if the pppd record exists */
158 rec
= tdb_fetch(pppdb
, pid
);
159 if (rec
.dptr
!= NULL
) {
160 /* it is, parse the interface number */
161 parse_num(rec
.dptr
, "IFNAME=ppp", &unit
);
162 /* check the pid value */
163 if (!parse_num(rec
.dptr
, "PPPD_PID=", &pppd_pid
)
164 || !process_exists(pppd_pid
)
165 || !owns_unit(pid
, unit
))
173 /* attach to existing unit */
174 if (bundle_attach(unit
)) {
176 script_setenv("BUNDLE", bundle_id
+ 7, 0);
177 tdb_writeunlock(pppdb
);
178 info("Link attached to %s", ifname
);
181 /* attach failed because bundle doesn't exist */
184 /* we have to make a new bundle */
185 make_new_bundle(go
->mrru
, ho
->mrru
, go
->neg_ssnhf
, ho
->neg_ssnhf
);
187 netif_set_mtu(0, mtu
);
188 script_setenv("BUNDLE", bundle_id
+ 7, 1);
189 tdb_writeunlock(pppdb
);
190 info("New bundle %s created", ifname
);
195 parse_num(str
, key
, valp
)
203 p
= strstr(str
, key
);
206 i
= strtol(p
, &endp
, 10);
207 if (endp
!= p
&& (*endp
== 0 || *endp
== ';')) {
216 * Check whether the pppd identified by `key' still owns ppp unit `unit'.
227 slprintf(ifkey
, sizeof(ifkey
), "IFNAME=ppp%d", unit
);
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;
240 get_default_epdisc(ep
)
247 /* First try for an ethernet MAC address */
248 p
= get_first_ethernet();
249 if (p
!= 0 && get_if_hwaddr(ep
->value
, p
) >= 0) {
255 /* see if our hostname corresponds to a reasonable IP address */
256 hp
= gethostbyname(hostname
);
258 addr
= *(u_int32_t
*)hp
->h_addr
;
259 if (!bad_ip_adrs(addr
)) {
261 if (!LOCAL_IP_ADDR(addr
)) {
263 set_ip_epdisc(ep
, addr
);
273 * epdisc_to_str - make a printable string from an endpoint discriminator.
276 static char *endp_class_names
[] = {
277 "null", "local", "IP", "MAC", "magic", "phone"
284 static char str
[MAX_ENDP_LEN
*3+8];
285 u_char
*p
= ep
->value
;
289 if (ep
->class == EPD_NULL
&& ep
->length
== 0)
291 if (ep
->class == EPD_IP
&& ep
->length
== 4) {
295 slprintf(str
, sizeof(str
), "IP:%I", htonl(addr
));
301 if (ep
->class == EPD_MAC
&& ep
->length
== 6)
303 else if (ep
->class == EPD_MAGIC
&& (ep
->length
% 4) == 0)
306 if (ep
->class <= EPD_PHONENUM
)
307 q
+= slprintf(q
, sizeof(str
)-1, "%s",
308 endp_class_names
[ep
->class]);
310 q
+= slprintf(q
, sizeof(str
)-1, "%d", ep
->class);
312 for (i
= 0; i
< ep
->length
&& i
< MAX_ENDP_LEN
; ++i
) {
313 if ((i
& mask
) == 0) {
317 q
+= slprintf(q
, str
+ sizeof(str
) - q
, "%.2x", ep
->value
[i
]);
322 static int hexc_val(int c
)
332 str_to_epdisc(ep
, str
)
339 for (i
= EPD_NULL
; i
<= EPD_PHONENUM
; ++i
) {
340 int sl
= strlen(endp_class_names
[i
]);
341 if (strncasecmp(str
, endp_class_names
[i
], sl
) == 0) {
346 if (i
> EPD_PHONENUM
) {
347 /* not a class name, try a decimal class number */
348 i
= strtol(str
, &endp
, 10);
350 return 0; /* can't parse class number */
358 if (*str
!= ':' && *str
!= '.')
364 i
= parse_dotted_ip(str
, &addr
);
365 if (i
== 0 || str
[i
] != 0)
367 set_ip_epdisc(ep
, addr
);
370 if (i
== EPD_MAC
&& get_if_hwaddr(ep
->value
, str
) >= 0) {
376 for (l
= 0; l
< MAX_ENDP_LEN
; ++l
) {
380 for (p
= str
; isxdigit(*p
); ++p
)
385 ep
->value
[l
] = hexc_val(*str
++);
387 ep
->value
[l
] = (ep
->value
[l
] << 4) + hexc_val(*str
++);
388 if (*str
== ':' || *str
== '.')
391 if (*str
!= 0 || (ep
->class == EPD_MAC
&& l
!= 6))