Deleted Added
full compact
mp.c (134789) mp.c (134833)
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/ppp/mp.c 134789 2004-09-05 01:46:52Z brian $
26 * $FreeBSD: head/usr.sbin/ppp/mp.c 134833 2004-09-06 00:07:58Z marcel $
27 */
28
29#include <sys/param.h>
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32#include <netinet/ip.h>
33#include <arpa/inet.h>
34#include <net/if_dl.h>
35#include <sys/socket.h>
36#include <sys/un.h>
37
38#include <errno.h>
39#include <paths.h>
40#include <stdarg.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <sys/stat.h>
45#include <termios.h>
46#include <unistd.h>
47
48#include "layer.h"
49#ifndef NONAT
50#include "nat_cmd.h"
51#endif
52#include "vjcomp.h"
53#include "ua.h"
54#include "defs.h"
55#include "command.h"
56#include "mbuf.h"
57#include "log.h"
58#include "timer.h"
59#include "fsm.h"
60#include "iplist.h"
61#include "throughput.h"
62#include "slcompress.h"
63#include "lqr.h"
64#include "hdlc.h"
65#include "ncpaddr.h"
66#include "ipcp.h"
67#include "auth.h"
68#include "lcp.h"
69#include "async.h"
70#include "ccp.h"
71#include "link.h"
72#include "descriptor.h"
73#include "physical.h"
74#include "chat.h"
75#include "proto.h"
76#include "filter.h"
77#include "mp.h"
78#include "chap.h"
79#include "cbcp.h"
80#include "datalink.h"
81#ifndef NORADIUS
82#include "radius.h"
83#endif
84#include "ipv6cp.h"
85#include "ncp.h"
86#include "bundle.h"
87#include "prompt.h"
88#include "id.h"
89#include "arp.h"
90
91void
92peerid_Init(struct peerid *peer)
93{
94 peer->enddisc.class = 0;
95 *peer->enddisc.address = '\0';
96 peer->enddisc.len = 0;
97 *peer->authname = '\0';
98}
99
100int
101peerid_Equal(const struct peerid *p1, const struct peerid *p2)
102{
103 return !strcmp(p1->authname, p2->authname) &&
104 p1->enddisc.class == p2->enddisc.class &&
105 p1->enddisc.len == p2->enddisc.len &&
106 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
107}
108
109static u_int32_t
110inc_seq(unsigned is12bit, u_int32_t seq)
111{
112 seq++;
113 if (is12bit) {
114 if (seq & 0xfffff000)
115 seq = 0;
116 } else if (seq & 0xff000000)
117 seq = 0;
118 return seq;
119}
120
121static int
122isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
123{
124 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
125
126 if (seq1 > max) {
127 if (seq2 < 0x200 || seq2 > seq1)
128 return 1;
129 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
130 return 1;
131
132 return 0;
133}
134
135static int
136mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
137{
138 if (mp->local_is12bit) {
139 u_int16_t val;
140
141 ua_ntohs(MBUF_CTOP(m), &val);
142 if (val & 0x3000) {
143 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
144 return 0;
145 }
146 header->begin = val & 0x8000 ? 1 : 0;
147 header->end = val & 0x4000 ? 1 : 0;
148 header->seq = val & 0x0fff;
149 return 2;
150 } else {
151 ua_ntohl(MBUF_CTOP(m), &header->seq);
152 if (header->seq & 0x3f000000) {
153 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
154 return 0;
155 }
156 header->begin = header->seq & 0x80000000 ? 1 : 0;
157 header->end = header->seq & 0x40000000 ? 1 : 0;
158 header->seq &= 0x00ffffff;
159 return 4;
160 }
161}
162
163static void
164mp_LayerStart(void *v __unused, struct fsm *fp __unused)
165{
166 /* The given FSM (ccp) is about to start up ! */
167}
168
169static void
170mp_LayerUp(void *v __unused, struct fsm *fp)
171{
172 /* The given fsm (ccp) is now up */
173
174 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */
175}
176
177static void
178mp_LayerDown(void *v __unused, struct fsm *fp __unused)
179{
180 /* The given FSM (ccp) has been told to come down */
181}
182
183static void
184mp_LayerFinish(void *v __unused, struct fsm *fp)
185{
186 /* The given fsm (ccp) is now down */
187 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
188 fsm_Open(fp); /* CCP goes to ST_STOPPED */
189}
190
191static void
192mp_UpDown(void *v)
193{
194 struct mp *mp = (struct mp *)v;
195 int percent;
196
197 percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
198 mp->link.stats.total.out.OctetsPerSecond) * 800 /
199 mp->bundle->bandwidth;
200 if (percent >= mp->cfg.autoload.max) {
201 log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
202 bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
203 } else if (percent <= mp->cfg.autoload.min) {
204 log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
205 bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
206 }
207}
208
209void
210mp_StopAutoloadTimer(struct mp *mp)
211{
212 throughput_stop(&mp->link.stats.total);
213}
214
215void
216mp_CheckAutoloadTimer(struct mp *mp)
217{
218 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
219 throughput_destroy(&mp->link.stats.total);
220 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
221 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
222 }
223
224 if (bundle_WantAutoloadTimer(mp->bundle))
225 throughput_start(&mp->link.stats.total, "MP throughput", 1);
226 else
227 mp_StopAutoloadTimer(mp);
228}
229
230void
231mp_RestartAutoloadTimer(struct mp *mp)
232{
233 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
234 mp_CheckAutoloadTimer(mp);
235 else
236 throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
237}
238
239void
240mp_Init(struct mp *mp, struct bundle *bundle)
241{
242 mp->peer_is12bit = mp->local_is12bit = 0;
243 mp->peer_mrru = mp->local_mrru = 0;
244
245 peerid_Init(&mp->peer);
246
247 mp->out.seq = 0;
248 mp->out.link = 0;
249 mp->out.af = AF_INET;
250 mp->seq.min_in = 0;
251 mp->seq.next_in = 0;
252 mp->inbufs = NULL;
253 mp->bundle = bundle;
254
255 mp->link.type = LOGICAL_LINK;
256 mp->link.name = "mp";
257 mp->link.len = sizeof *mp;
258
259 mp->cfg.autoload.period = SAMPLE_PERIOD;
260 mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
261 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
262 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
263 mp->link.stats.parent = NULL;
264 mp->link.stats.gather = 0; /* Let the physical links gather stats */
265 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
266 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
267 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
268
269 mp->fsmp.LayerStart = mp_LayerStart;
270 mp->fsmp.LayerUp = mp_LayerUp;
271 mp->fsmp.LayerDown = mp_LayerDown;
272 mp->fsmp.LayerFinish = mp_LayerFinish;
273 mp->fsmp.object = mp;
274
275 mpserver_Init(&mp->server);
276
277 mp->cfg.mrru = 0;
278 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
279 mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
280 mp->cfg.enddisc.class = 0;
281 *mp->cfg.enddisc.address = '\0';
282 mp->cfg.enddisc.len = 0;
283
284 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
285 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
286
287 link_EmptyStack(&mp->link);
288 link_Stack(&mp->link, &protolayer);
289 link_Stack(&mp->link, &ccplayer);
290 link_Stack(&mp->link, &vjlayer);
291#ifndef NONAT
292 link_Stack(&mp->link, &natlayer);
293#endif
294}
295
296int
297mp_Up(struct mp *mp, struct datalink *dl)
298{
299 struct lcp *lcp = &dl->physical->link.lcp;
300
301 if (mp->active) {
302 /* We're adding a link - do a last validation on our parameters */
303 if (!peerid_Equal(&dl->peer, &mp->peer)) {
304 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
305 log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname,
306 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
307 mp->peer.enddisc.len));
308 log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname,
309 mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
310 dl->peer.enddisc.len));
311 return MP_FAILED;
312 }
313 if (mp->local_mrru != lcp->want_mrru ||
314 mp->peer_mrru != lcp->his_mrru ||
315 mp->local_is12bit != lcp->want_shortseq ||
316 mp->peer_is12bit != lcp->his_shortseq) {
317 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
318 dl->name);
319 return MP_FAILED;
320 }
321 return MP_ADDED;
322 } else {
323 /* First link in multilink mode */
324
325 mp->local_mrru = lcp->want_mrru;
326 mp->peer_mrru = lcp->his_mrru;
327 mp->local_is12bit = lcp->want_shortseq;
328 mp->peer_is12bit = lcp->his_shortseq;
329 mp->peer = dl->peer;
330
331 throughput_destroy(&mp->link.stats.total);
332 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
333 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
334 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
335 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
336 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
337
338 /* Tell the link who it belongs to */
339 dl->physical->link.stats.parent = &mp->link.stats.total;
340
341 mp->out.seq = 0;
342 mp->out.link = 0;
343 mp->out.af = AF_INET;
344 mp->seq.min_in = 0;
345 mp->seq.next_in = 0;
346
347 /*
348 * Now we create our server socket.
349 * If it already exists, join it. Otherwise, create and own it
350 */
351 switch (mpserver_Open(&mp->server, &mp->peer)) {
352 case MPSERVER_CONNECTED:
353 log_Printf(LogPHASE, "mp: Transfer link on %s\n",
354 mp->server.socket.sun_path);
355 mp->server.send.dl = dl; /* Defer 'till it's safe to send */
356 return MP_LINKSENT;
357 case MPSERVER_FAILED:
358 return MP_FAILED;
359 case MPSERVER_LISTENING:
360 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
361 log_Printf(LogPHASE, " First link: %s\n", dl->name);
362
363 /* Re-point our NCP layers at our MP link */
364 ncp_SetLink(&mp->bundle->ncp, &mp->link);
365
366 /* Our lcp's already up 'cos of the NULL parent */
367 if (ccp_SetOpenMode(&mp->link.ccp)) {
368 fsm_Up(&mp->link.ccp.fsm);
369 fsm_Open(&mp->link.ccp.fsm);
370 }
371
372 mp->active = 1;
373 break;
374 }
375 }
376
377 return MP_UP;
378}
379
380void
381mp_Down(struct mp *mp)
382{
383 if (mp->active) {
384 struct mbuf *next;
385
386 /* Stop that ! */
387 mp_StopAutoloadTimer(mp);
388
389 /* Don't want any more of these */
390 mpserver_Close(&mp->server);
391
392 /* CCP goes down with a bang */
393 fsm2initial(&mp->link.ccp.fsm);
394
395 /* Received fragments go in the bit-bucket */
396 while (mp->inbufs) {
397 next = mp->inbufs->m_nextpkt;
398 m_freem(mp->inbufs);
399 mp->inbufs = next;
400 }
401
402 peerid_Init(&mp->peer);
403 mp->active = 0;
404 }
405}
406
407void
408mp_linkInit(struct mp_link *mplink)
409{
410 mplink->seq = 0;
411 mplink->bandwidth = 0;
412}
413
414static void
415mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
416{
417 struct mp_header mh, h;
418 struct mbuf *q, *last;
419 u_int32_t seq;
420
421 /*
422 * When `m' and `p' are NULL, it means our oldest link has gone down.
423 * We want to determine a new min, and process any intermediate stuff
424 * as normal
425 */
426
427 if (m && mp_ReadHeader(mp, m, &mh) == 0) {
428 m_freem(m);
429 return;
430 }
431
432 if (p) {
433 seq = p->dl->mp.seq;
434 p->dl->mp.seq = mh.seq;
435 } else
436 seq = mp->seq.min_in;
437
438 if (mp->seq.min_in == seq) {
439 /*
440 * We've received new data on the link that has our min (oldest) seq.
441 * Figure out which link now has the smallest (oldest) seq.
442 */
443 struct datalink *dl;
444
445 mp->seq.min_in = (u_int32_t)-1;
446 for (dl = mp->bundle->links; dl; dl = dl->next)
447 if (dl->state == DATALINK_OPEN &&
448 (mp->seq.min_in == (u_int32_t)-1 ||
449 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
450 mp->seq.min_in = dl->mp.seq;
451 }
452
453 /*
454 * Now process as many of our fragments as we can, adding our new
455 * fragment in as we go, and ordering with the oldest at the top of
456 * the queue.
457 */
458
459 last = NULL;
460 seq = mp->seq.next_in;
461 q = mp->inbufs;
462 while (q || m) {
463 if (!q) {
464 if (last)
465 last->m_nextpkt = m;
466 else
467 mp->inbufs = m;
468 q = m;
469 m = NULL;
470 h = mh;
471 } else {
472 mp_ReadHeader(mp, q, &h);
473
474 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
475 /* Our received fragment fits in before this one, so link it in */
476 if (last)
477 last->m_nextpkt = m;
478 else
479 mp->inbufs = m;
480 m->m_nextpkt = q;
481 q = m;
482 h = mh;
483 m = NULL;
484 }
485 }
486
487 if (h.seq != seq) {
488 /* we're missing something :-( */
489 if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
490 /* we're never gonna get it */
491 struct mbuf *next;
492
493 /* Zap all older fragments */
494 while (mp->inbufs != q) {
495 log_Printf(LogDEBUG, "Drop frag\n");
496 next = mp->inbufs->m_nextpkt;
497 m_freem(mp->inbufs);
498 mp->inbufs = next;
499 }
500
501 /*
502 * Zap everything until the next `end' fragment OR just before
503 * the next `begin' fragment OR 'till seq.min_in - whichever
504 * comes first.
505 */
506 do {
507 mp_ReadHeader(mp, mp->inbufs, &h);
508 if (h.begin) {
509 /* We might be able to process this ! */
510 h.seq--; /* We're gonna look for fragment with h.seq+1 */
511 break;
512 }
513 next = mp->inbufs->m_nextpkt;
514 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
515 m_freem(mp->inbufs);
516 mp->inbufs = next;
517 } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
518 h.seq) || h.end));
519
520 /*
521 * Continue processing things from here.
522 * This deals with the possibility that we received a fragment
523 * on the slowest link that invalidates some of our data (because
524 * of the hole at `q'), but where there are subsequent `whole'
525 * packets that have already been received.
526 */
527
528 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
529 last = NULL;
530 q = mp->inbufs;
531 } else
532 /* we may still receive the missing fragment */
533 break;
534 } else if (h.end) {
535 /* We've got something, reassemble */
536 struct mbuf **frag = &q;
537 int len;
538 long long first = -1;
539
540 do {
541 *frag = mp->inbufs;
542 mp->inbufs = mp->inbufs->m_nextpkt;
543 len = mp_ReadHeader(mp, *frag, &h);
544 if (first == -1)
545 first = h.seq;
546 if (frag == &q && !h.begin) {
547 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
548 (u_long)h.seq);
549 m_freem(q);
550 q = NULL;
551 } else if (frag != &q && h.begin) {
552 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
553 (u_long)h.seq - 1);
554 /*
555 * Stuff our fragment back at the front of the queue and zap
556 * our half-assembled packet.
557 */
558 (*frag)->m_nextpkt = mp->inbufs;
559 mp->inbufs = *frag;
560 *frag = NULL;
561 m_freem(q);
562 q = NULL;
563 frag = &q;
564 h.end = 0; /* just in case it's a whole packet */
565 } else {
566 (*frag)->m_offset += len;
567 (*frag)->m_len -= len;
568 (*frag)->m_nextpkt = NULL;
569 do
570 frag = &(*frag)->m_next;
571 while (*frag != NULL);
572 }
573 } while (!h.end);
574
575 if (q) {
576 q = m_pullup(q);
27 */
28
29#include <sys/param.h>
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32#include <netinet/ip.h>
33#include <arpa/inet.h>
34#include <net/if_dl.h>
35#include <sys/socket.h>
36#include <sys/un.h>
37
38#include <errno.h>
39#include <paths.h>
40#include <stdarg.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <sys/stat.h>
45#include <termios.h>
46#include <unistd.h>
47
48#include "layer.h"
49#ifndef NONAT
50#include "nat_cmd.h"
51#endif
52#include "vjcomp.h"
53#include "ua.h"
54#include "defs.h"
55#include "command.h"
56#include "mbuf.h"
57#include "log.h"
58#include "timer.h"
59#include "fsm.h"
60#include "iplist.h"
61#include "throughput.h"
62#include "slcompress.h"
63#include "lqr.h"
64#include "hdlc.h"
65#include "ncpaddr.h"
66#include "ipcp.h"
67#include "auth.h"
68#include "lcp.h"
69#include "async.h"
70#include "ccp.h"
71#include "link.h"
72#include "descriptor.h"
73#include "physical.h"
74#include "chat.h"
75#include "proto.h"
76#include "filter.h"
77#include "mp.h"
78#include "chap.h"
79#include "cbcp.h"
80#include "datalink.h"
81#ifndef NORADIUS
82#include "radius.h"
83#endif
84#include "ipv6cp.h"
85#include "ncp.h"
86#include "bundle.h"
87#include "prompt.h"
88#include "id.h"
89#include "arp.h"
90
91void
92peerid_Init(struct peerid *peer)
93{
94 peer->enddisc.class = 0;
95 *peer->enddisc.address = '\0';
96 peer->enddisc.len = 0;
97 *peer->authname = '\0';
98}
99
100int
101peerid_Equal(const struct peerid *p1, const struct peerid *p2)
102{
103 return !strcmp(p1->authname, p2->authname) &&
104 p1->enddisc.class == p2->enddisc.class &&
105 p1->enddisc.len == p2->enddisc.len &&
106 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
107}
108
109static u_int32_t
110inc_seq(unsigned is12bit, u_int32_t seq)
111{
112 seq++;
113 if (is12bit) {
114 if (seq & 0xfffff000)
115 seq = 0;
116 } else if (seq & 0xff000000)
117 seq = 0;
118 return seq;
119}
120
121static int
122isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
123{
124 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
125
126 if (seq1 > max) {
127 if (seq2 < 0x200 || seq2 > seq1)
128 return 1;
129 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
130 return 1;
131
132 return 0;
133}
134
135static int
136mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
137{
138 if (mp->local_is12bit) {
139 u_int16_t val;
140
141 ua_ntohs(MBUF_CTOP(m), &val);
142 if (val & 0x3000) {
143 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
144 return 0;
145 }
146 header->begin = val & 0x8000 ? 1 : 0;
147 header->end = val & 0x4000 ? 1 : 0;
148 header->seq = val & 0x0fff;
149 return 2;
150 } else {
151 ua_ntohl(MBUF_CTOP(m), &header->seq);
152 if (header->seq & 0x3f000000) {
153 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
154 return 0;
155 }
156 header->begin = header->seq & 0x80000000 ? 1 : 0;
157 header->end = header->seq & 0x40000000 ? 1 : 0;
158 header->seq &= 0x00ffffff;
159 return 4;
160 }
161}
162
163static void
164mp_LayerStart(void *v __unused, struct fsm *fp __unused)
165{
166 /* The given FSM (ccp) is about to start up ! */
167}
168
169static void
170mp_LayerUp(void *v __unused, struct fsm *fp)
171{
172 /* The given fsm (ccp) is now up */
173
174 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */
175}
176
177static void
178mp_LayerDown(void *v __unused, struct fsm *fp __unused)
179{
180 /* The given FSM (ccp) has been told to come down */
181}
182
183static void
184mp_LayerFinish(void *v __unused, struct fsm *fp)
185{
186 /* The given fsm (ccp) is now down */
187 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
188 fsm_Open(fp); /* CCP goes to ST_STOPPED */
189}
190
191static void
192mp_UpDown(void *v)
193{
194 struct mp *mp = (struct mp *)v;
195 int percent;
196
197 percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
198 mp->link.stats.total.out.OctetsPerSecond) * 800 /
199 mp->bundle->bandwidth;
200 if (percent >= mp->cfg.autoload.max) {
201 log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
202 bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
203 } else if (percent <= mp->cfg.autoload.min) {
204 log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
205 bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
206 }
207}
208
209void
210mp_StopAutoloadTimer(struct mp *mp)
211{
212 throughput_stop(&mp->link.stats.total);
213}
214
215void
216mp_CheckAutoloadTimer(struct mp *mp)
217{
218 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
219 throughput_destroy(&mp->link.stats.total);
220 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
221 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
222 }
223
224 if (bundle_WantAutoloadTimer(mp->bundle))
225 throughput_start(&mp->link.stats.total, "MP throughput", 1);
226 else
227 mp_StopAutoloadTimer(mp);
228}
229
230void
231mp_RestartAutoloadTimer(struct mp *mp)
232{
233 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
234 mp_CheckAutoloadTimer(mp);
235 else
236 throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
237}
238
239void
240mp_Init(struct mp *mp, struct bundle *bundle)
241{
242 mp->peer_is12bit = mp->local_is12bit = 0;
243 mp->peer_mrru = mp->local_mrru = 0;
244
245 peerid_Init(&mp->peer);
246
247 mp->out.seq = 0;
248 mp->out.link = 0;
249 mp->out.af = AF_INET;
250 mp->seq.min_in = 0;
251 mp->seq.next_in = 0;
252 mp->inbufs = NULL;
253 mp->bundle = bundle;
254
255 mp->link.type = LOGICAL_LINK;
256 mp->link.name = "mp";
257 mp->link.len = sizeof *mp;
258
259 mp->cfg.autoload.period = SAMPLE_PERIOD;
260 mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
261 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
262 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
263 mp->link.stats.parent = NULL;
264 mp->link.stats.gather = 0; /* Let the physical links gather stats */
265 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
266 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
267 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
268
269 mp->fsmp.LayerStart = mp_LayerStart;
270 mp->fsmp.LayerUp = mp_LayerUp;
271 mp->fsmp.LayerDown = mp_LayerDown;
272 mp->fsmp.LayerFinish = mp_LayerFinish;
273 mp->fsmp.object = mp;
274
275 mpserver_Init(&mp->server);
276
277 mp->cfg.mrru = 0;
278 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
279 mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
280 mp->cfg.enddisc.class = 0;
281 *mp->cfg.enddisc.address = '\0';
282 mp->cfg.enddisc.len = 0;
283
284 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
285 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
286
287 link_EmptyStack(&mp->link);
288 link_Stack(&mp->link, &protolayer);
289 link_Stack(&mp->link, &ccplayer);
290 link_Stack(&mp->link, &vjlayer);
291#ifndef NONAT
292 link_Stack(&mp->link, &natlayer);
293#endif
294}
295
296int
297mp_Up(struct mp *mp, struct datalink *dl)
298{
299 struct lcp *lcp = &dl->physical->link.lcp;
300
301 if (mp->active) {
302 /* We're adding a link - do a last validation on our parameters */
303 if (!peerid_Equal(&dl->peer, &mp->peer)) {
304 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
305 log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname,
306 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
307 mp->peer.enddisc.len));
308 log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname,
309 mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
310 dl->peer.enddisc.len));
311 return MP_FAILED;
312 }
313 if (mp->local_mrru != lcp->want_mrru ||
314 mp->peer_mrru != lcp->his_mrru ||
315 mp->local_is12bit != lcp->want_shortseq ||
316 mp->peer_is12bit != lcp->his_shortseq) {
317 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
318 dl->name);
319 return MP_FAILED;
320 }
321 return MP_ADDED;
322 } else {
323 /* First link in multilink mode */
324
325 mp->local_mrru = lcp->want_mrru;
326 mp->peer_mrru = lcp->his_mrru;
327 mp->local_is12bit = lcp->want_shortseq;
328 mp->peer_is12bit = lcp->his_shortseq;
329 mp->peer = dl->peer;
330
331 throughput_destroy(&mp->link.stats.total);
332 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
333 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
334 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
335 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
336 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
337
338 /* Tell the link who it belongs to */
339 dl->physical->link.stats.parent = &mp->link.stats.total;
340
341 mp->out.seq = 0;
342 mp->out.link = 0;
343 mp->out.af = AF_INET;
344 mp->seq.min_in = 0;
345 mp->seq.next_in = 0;
346
347 /*
348 * Now we create our server socket.
349 * If it already exists, join it. Otherwise, create and own it
350 */
351 switch (mpserver_Open(&mp->server, &mp->peer)) {
352 case MPSERVER_CONNECTED:
353 log_Printf(LogPHASE, "mp: Transfer link on %s\n",
354 mp->server.socket.sun_path);
355 mp->server.send.dl = dl; /* Defer 'till it's safe to send */
356 return MP_LINKSENT;
357 case MPSERVER_FAILED:
358 return MP_FAILED;
359 case MPSERVER_LISTENING:
360 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
361 log_Printf(LogPHASE, " First link: %s\n", dl->name);
362
363 /* Re-point our NCP layers at our MP link */
364 ncp_SetLink(&mp->bundle->ncp, &mp->link);
365
366 /* Our lcp's already up 'cos of the NULL parent */
367 if (ccp_SetOpenMode(&mp->link.ccp)) {
368 fsm_Up(&mp->link.ccp.fsm);
369 fsm_Open(&mp->link.ccp.fsm);
370 }
371
372 mp->active = 1;
373 break;
374 }
375 }
376
377 return MP_UP;
378}
379
380void
381mp_Down(struct mp *mp)
382{
383 if (mp->active) {
384 struct mbuf *next;
385
386 /* Stop that ! */
387 mp_StopAutoloadTimer(mp);
388
389 /* Don't want any more of these */
390 mpserver_Close(&mp->server);
391
392 /* CCP goes down with a bang */
393 fsm2initial(&mp->link.ccp.fsm);
394
395 /* Received fragments go in the bit-bucket */
396 while (mp->inbufs) {
397 next = mp->inbufs->m_nextpkt;
398 m_freem(mp->inbufs);
399 mp->inbufs = next;
400 }
401
402 peerid_Init(&mp->peer);
403 mp->active = 0;
404 }
405}
406
407void
408mp_linkInit(struct mp_link *mplink)
409{
410 mplink->seq = 0;
411 mplink->bandwidth = 0;
412}
413
414static void
415mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
416{
417 struct mp_header mh, h;
418 struct mbuf *q, *last;
419 u_int32_t seq;
420
421 /*
422 * When `m' and `p' are NULL, it means our oldest link has gone down.
423 * We want to determine a new min, and process any intermediate stuff
424 * as normal
425 */
426
427 if (m && mp_ReadHeader(mp, m, &mh) == 0) {
428 m_freem(m);
429 return;
430 }
431
432 if (p) {
433 seq = p->dl->mp.seq;
434 p->dl->mp.seq = mh.seq;
435 } else
436 seq = mp->seq.min_in;
437
438 if (mp->seq.min_in == seq) {
439 /*
440 * We've received new data on the link that has our min (oldest) seq.
441 * Figure out which link now has the smallest (oldest) seq.
442 */
443 struct datalink *dl;
444
445 mp->seq.min_in = (u_int32_t)-1;
446 for (dl = mp->bundle->links; dl; dl = dl->next)
447 if (dl->state == DATALINK_OPEN &&
448 (mp->seq.min_in == (u_int32_t)-1 ||
449 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
450 mp->seq.min_in = dl->mp.seq;
451 }
452
453 /*
454 * Now process as many of our fragments as we can, adding our new
455 * fragment in as we go, and ordering with the oldest at the top of
456 * the queue.
457 */
458
459 last = NULL;
460 seq = mp->seq.next_in;
461 q = mp->inbufs;
462 while (q || m) {
463 if (!q) {
464 if (last)
465 last->m_nextpkt = m;
466 else
467 mp->inbufs = m;
468 q = m;
469 m = NULL;
470 h = mh;
471 } else {
472 mp_ReadHeader(mp, q, &h);
473
474 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
475 /* Our received fragment fits in before this one, so link it in */
476 if (last)
477 last->m_nextpkt = m;
478 else
479 mp->inbufs = m;
480 m->m_nextpkt = q;
481 q = m;
482 h = mh;
483 m = NULL;
484 }
485 }
486
487 if (h.seq != seq) {
488 /* we're missing something :-( */
489 if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
490 /* we're never gonna get it */
491 struct mbuf *next;
492
493 /* Zap all older fragments */
494 while (mp->inbufs != q) {
495 log_Printf(LogDEBUG, "Drop frag\n");
496 next = mp->inbufs->m_nextpkt;
497 m_freem(mp->inbufs);
498 mp->inbufs = next;
499 }
500
501 /*
502 * Zap everything until the next `end' fragment OR just before
503 * the next `begin' fragment OR 'till seq.min_in - whichever
504 * comes first.
505 */
506 do {
507 mp_ReadHeader(mp, mp->inbufs, &h);
508 if (h.begin) {
509 /* We might be able to process this ! */
510 h.seq--; /* We're gonna look for fragment with h.seq+1 */
511 break;
512 }
513 next = mp->inbufs->m_nextpkt;
514 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
515 m_freem(mp->inbufs);
516 mp->inbufs = next;
517 } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
518 h.seq) || h.end));
519
520 /*
521 * Continue processing things from here.
522 * This deals with the possibility that we received a fragment
523 * on the slowest link that invalidates some of our data (because
524 * of the hole at `q'), but where there are subsequent `whole'
525 * packets that have already been received.
526 */
527
528 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
529 last = NULL;
530 q = mp->inbufs;
531 } else
532 /* we may still receive the missing fragment */
533 break;
534 } else if (h.end) {
535 /* We've got something, reassemble */
536 struct mbuf **frag = &q;
537 int len;
538 long long first = -1;
539
540 do {
541 *frag = mp->inbufs;
542 mp->inbufs = mp->inbufs->m_nextpkt;
543 len = mp_ReadHeader(mp, *frag, &h);
544 if (first == -1)
545 first = h.seq;
546 if (frag == &q && !h.begin) {
547 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
548 (u_long)h.seq);
549 m_freem(q);
550 q = NULL;
551 } else if (frag != &q && h.begin) {
552 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
553 (u_long)h.seq - 1);
554 /*
555 * Stuff our fragment back at the front of the queue and zap
556 * our half-assembled packet.
557 */
558 (*frag)->m_nextpkt = mp->inbufs;
559 mp->inbufs = *frag;
560 *frag = NULL;
561 m_freem(q);
562 q = NULL;
563 frag = &q;
564 h.end = 0; /* just in case it's a whole packet */
565 } else {
566 (*frag)->m_offset += len;
567 (*frag)->m_len -= len;
568 (*frag)->m_nextpkt = NULL;
569 do
570 frag = &(*frag)->m_next;
571 while (*frag != NULL);
572 }
573 } while (!h.end);
574
575 if (q) {
576 q = m_pullup(q);
577 log_Printf(LogDEBUG, "MP: Reassembled frags %lu-%lu, length %d\n",
577 log_Printf(LogDEBUG, "MP: Reassembled frags %lu-%lu, length %zd\n",
578 (u_long)first, (u_long)h.seq, m_length(q));
579 link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
580 m_freem(q);
581 }
582
583 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
584 last = NULL;
585 q = mp->inbufs;
586 } else {
587 /* Look for the next fragment */
588 seq = inc_seq(mp->local_is12bit, seq);
589 last = q;
590 q = q->m_nextpkt;
591 }
592 }
593
594 if (m) {
595 /* We still have to find a home for our new fragment */
596 last = NULL;
597 for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
598 mp_ReadHeader(mp, q, &h);
599 if (isbefore(mp->local_is12bit, mh.seq, h.seq))
600 break;
601 }
602 /* Our received fragment fits in here */
603 if (last)
604 last->m_nextpkt = m;
605 else
606 mp->inbufs = m;
607 m->m_nextpkt = q;
608 }
609}
610
611struct mbuf *
612mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
613{
614 struct physical *p = link2physical(l);
615
616 if (!bundle->ncp.mp.active)
617 /* Let someone else deal with it ! */
618 return bp;
619
620 if (p == NULL) {
621 log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
622 m_freem(bp);
623 } else {
624 m_settype(bp, MB_MPIN);
625 mp_Assemble(&bundle->ncp.mp, bp, p);
626 }
627
628 return NULL;
629}
630
631static void
632mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
633 struct mbuf *m, u_int32_t begin, u_int32_t end)
634{
635 char prepend[4];
636
637 /* Stuff an MP header on the front of our packet and send it */
638
639 if (mp->peer_is12bit) {
640 u_int16_t val;
641
642 val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
643 ua_htons(&val, prepend);
644 m = m_prepend(m, prepend, 2, 0);
645 } else {
646 u_int32_t val;
647
648 val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
649 ua_htonl(&val, prepend);
650 m = m_prepend(m, prepend, 4, 0);
651 }
652 if (log_IsKept(LogDEBUG))
578 (u_long)first, (u_long)h.seq, m_length(q));
579 link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
580 m_freem(q);
581 }
582
583 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
584 last = NULL;
585 q = mp->inbufs;
586 } else {
587 /* Look for the next fragment */
588 seq = inc_seq(mp->local_is12bit, seq);
589 last = q;
590 q = q->m_nextpkt;
591 }
592 }
593
594 if (m) {
595 /* We still have to find a home for our new fragment */
596 last = NULL;
597 for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
598 mp_ReadHeader(mp, q, &h);
599 if (isbefore(mp->local_is12bit, mh.seq, h.seq))
600 break;
601 }
602 /* Our received fragment fits in here */
603 if (last)
604 last->m_nextpkt = m;
605 else
606 mp->inbufs = m;
607 m->m_nextpkt = q;
608 }
609}
610
611struct mbuf *
612mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
613{
614 struct physical *p = link2physical(l);
615
616 if (!bundle->ncp.mp.active)
617 /* Let someone else deal with it ! */
618 return bp;
619
620 if (p == NULL) {
621 log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
622 m_freem(bp);
623 } else {
624 m_settype(bp, MB_MPIN);
625 mp_Assemble(&bundle->ncp.mp, bp, p);
626 }
627
628 return NULL;
629}
630
631static void
632mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
633 struct mbuf *m, u_int32_t begin, u_int32_t end)
634{
635 char prepend[4];
636
637 /* Stuff an MP header on the front of our packet and send it */
638
639 if (mp->peer_is12bit) {
640 u_int16_t val;
641
642 val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
643 ua_htons(&val, prepend);
644 m = m_prepend(m, prepend, 2, 0);
645 } else {
646 u_int32_t val;
647
648 val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
649 ua_htonl(&val, prepend);
650 m = m_prepend(m, prepend, 4, 0);
651 }
652 if (log_IsKept(LogDEBUG))
653 log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
653 log_Printf(LogDEBUG, "MP[frag %d]: Send %zd bytes on link `%s'\n",
654 mp->out.seq, m_length(m), l->name);
655 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
656
657 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
658 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
659 return;
660 }
661
662 link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
663}
664
665int
666mp_FillPhysicalQueues(struct bundle *bundle)
667{
668 struct mp *mp = &bundle->ncp.mp;
669 struct datalink *dl, *fdl;
670 size_t total, add, len;
671 int thislink, nlinks, nopenlinks, sendasip;
672 u_int32_t begin, end;
673 struct mbuf *m, *mo;
674 struct link *bestlink;
675
676 thislink = nlinks = nopenlinks = 0;
677 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
678 /* Include non-open links here as mp->out.link will stay more correct */
679 if (!fdl) {
680 if (thislink == mp->out.link)
681 fdl = dl;
682 else
683 thislink++;
684 }
685 nlinks++;
686 if (dl->state == DATALINK_OPEN)
687 nopenlinks++;
688 }
689
690 if (!fdl) {
691 fdl = bundle->links;
692 if (!fdl)
693 return 0;
694 thislink = 0;
695 }
696
697 total = 0;
698 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
699 if (!dl) {
700 dl = bundle->links;
701 thislink = 0;
702 }
703
704 if (dl->state != DATALINK_OPEN)
705 continue;
706
707 if (dl->physical->out)
708 /* this link has suffered a short write. Let it continue */
709 continue;
710
711 add = link_QueueLen(&dl->physical->link);
712 if (add) {
713 /* this link has got stuff already queued. Let it continue */
714 total += add;
715 continue;
716 }
717
718 if (!mp_QueueLen(mp)) {
719 int mrutoosmall;
720
721 /*
722 * If there's only a single open link in our bundle and we haven't got
723 * MP level link compression, queue outbound traffic directly via that
724 * link's protocol stack rather than using the MP link. This results
725 * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather
726 * than PROTO_MP.
727 */
728
729 mrutoosmall = 0;
730 sendasip = nopenlinks < 2;
731 if (sendasip) {
732 if (dl->physical->link.lcp.his_mru < mp->peer_mrru) {
733 /*
734 * Actually, forget it. This test is done against the MRRU rather
735 * than the packet size so that we don't end up sending some data
736 * in MP fragments and some data in PROTO_IP packets. That's just
737 * too likely to upset some ppp implementations.
738 */
739 mrutoosmall = 1;
740 sendasip = 0;
741 }
742 }
743
744 bestlink = sendasip ? &dl->physical->link : &mp->link;
745 if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink))
746 break; /* Nothing else to send */
747
748 if (mrutoosmall)
749 log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n");
750 else if (sendasip)
751 log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n");
752
753 if (sendasip) {
754 add = link_QueueLen(&dl->physical->link);
755 if (add) {
756 /* this link has got stuff already queued. Let it continue */
757 total += add;
758 continue;
759 }
760 }
761 }
762
763 m = link_Dequeue(&mp->link);
764 if (m) {
765 len = m_length(m);
766 begin = 1;
767 end = 0;
768
769 while (!end) {
770 if (dl->state == DATALINK_OPEN) {
771 /* Write at most his_mru bytes to the physical link */
772 if (len <= dl->physical->link.lcp.his_mru) {
773 mo = m;
774 end = 1;
775 m_settype(mo, MB_MPOUT);
776 } else {
777 /* It's > his_mru, chop the packet (`m') into bits */
778 mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
779 len -= mo->m_len;
780 m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
781 }
782 mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
783 begin = 0;
784 }
785
786 if (!end) {
787 nlinks--;
788 dl = dl->next;
789 if (!dl) {
790 dl = bundle->links;
791 thislink = 0;
792 } else
793 thislink++;
794 }
795 }
796 }
797 }
798 mp->out.link = thislink; /* Start here next time */
799
800 return total;
801}
802
803int
804mp_SetDatalinkBandwidth(struct cmdargs const *arg)
805{
806 int val;
807
808 if (arg->argc != arg->argn+1)
809 return -1;
810
811 val = atoi(arg->argv[arg->argn]);
812 if (val <= 0) {
813 log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
814 return 1;
815 }
816 arg->cx->mp.bandwidth = val;
817
818 if (arg->cx->state == DATALINK_OPEN)
819 bundle_CalculateBandwidth(arg->bundle);
820
821 return 0;
822}
823
824int
825mp_ShowStatus(struct cmdargs const *arg)
826{
827 struct mp *mp = &arg->bundle->ncp.mp;
828
829 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
830 if (mp->active) {
831 struct mbuf *m, *lm;
832 int bufs = 0;
833
834 lm = NULL;
835 prompt_Printf(arg->prompt, "Socket: %s\n",
836 mp->server.socket.sun_path);
837 for (m = mp->inbufs; m; m = m->m_nextpkt) {
838 bufs++;
839 lm = m;
840 }
841 prompt_Printf(arg->prompt, "Pending frags: %d", bufs);
842 if (bufs) {
843 struct mp_header mh;
844 unsigned long first, last;
845
846 first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
847 last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
848 prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
849 first, last, (unsigned long)mp->seq.next_in,
850 (unsigned long)mp->seq.min_in);
851 prompt_Printf(arg->prompt, " First has %sbegin bit and "
852 "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
853 }
854 prompt_Printf(arg->prompt, "\n");
855 }
856
857 prompt_Printf(arg->prompt, "\nMy Side:\n");
858 if (mp->active) {
859 prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq);
860 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru);
861 prompt_Printf(arg->prompt, " Short Seq: %s\n",
862 mp->local_is12bit ? "on" : "off");
863 }
864 prompt_Printf(arg->prompt, " Discriminator: %s\n",
865 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
866 mp->cfg.enddisc.len));
867
868 prompt_Printf(arg->prompt, "\nHis Side:\n");
869 if (mp->active) {
870 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname);
871 prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in);
872 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru);
873 prompt_Printf(arg->prompt, " Short Seq: %s\n",
874 mp->peer_is12bit ? "on" : "off");
875 }
876 prompt_Printf(arg->prompt, " Discriminator: %s\n",
877 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
878 mp->peer.enddisc.len));
879
880 prompt_Printf(arg->prompt, "\nDefaults:\n");
881
882 prompt_Printf(arg->prompt, " MRRU: ");
883 if (mp->cfg.mrru)
884 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
885 else
886 prompt_Printf(arg->prompt, "disabled\n");
887 prompt_Printf(arg->prompt, " Short Seq: %s\n",
888 command_ShowNegval(mp->cfg.shortseq));
889 prompt_Printf(arg->prompt, " Discriminator: %s\n",
890 command_ShowNegval(mp->cfg.negenddisc));
891 prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%,"
892 " period %d secs\n", mp->cfg.autoload.min,
893 mp->cfg.autoload.max, mp->cfg.autoload.period);
894
895 return 0;
896}
897
898const char *
899mp_Enddisc(u_char c, const char *address, size_t len)
900{
901 static char result[100]; /* Used immediately after it's returned */
902 unsigned f, header;
903
904 switch (c) {
905 case ENDDISC_NULL:
906 sprintf(result, "Null Class");
907 break;
908
909 case ENDDISC_LOCAL:
654 mp->out.seq, m_length(m), l->name);
655 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
656
657 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
658 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
659 return;
660 }
661
662 link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
663}
664
665int
666mp_FillPhysicalQueues(struct bundle *bundle)
667{
668 struct mp *mp = &bundle->ncp.mp;
669 struct datalink *dl, *fdl;
670 size_t total, add, len;
671 int thislink, nlinks, nopenlinks, sendasip;
672 u_int32_t begin, end;
673 struct mbuf *m, *mo;
674 struct link *bestlink;
675
676 thislink = nlinks = nopenlinks = 0;
677 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
678 /* Include non-open links here as mp->out.link will stay more correct */
679 if (!fdl) {
680 if (thislink == mp->out.link)
681 fdl = dl;
682 else
683 thislink++;
684 }
685 nlinks++;
686 if (dl->state == DATALINK_OPEN)
687 nopenlinks++;
688 }
689
690 if (!fdl) {
691 fdl = bundle->links;
692 if (!fdl)
693 return 0;
694 thislink = 0;
695 }
696
697 total = 0;
698 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
699 if (!dl) {
700 dl = bundle->links;
701 thislink = 0;
702 }
703
704 if (dl->state != DATALINK_OPEN)
705 continue;
706
707 if (dl->physical->out)
708 /* this link has suffered a short write. Let it continue */
709 continue;
710
711 add = link_QueueLen(&dl->physical->link);
712 if (add) {
713 /* this link has got stuff already queued. Let it continue */
714 total += add;
715 continue;
716 }
717
718 if (!mp_QueueLen(mp)) {
719 int mrutoosmall;
720
721 /*
722 * If there's only a single open link in our bundle and we haven't got
723 * MP level link compression, queue outbound traffic directly via that
724 * link's protocol stack rather than using the MP link. This results
725 * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather
726 * than PROTO_MP.
727 */
728
729 mrutoosmall = 0;
730 sendasip = nopenlinks < 2;
731 if (sendasip) {
732 if (dl->physical->link.lcp.his_mru < mp->peer_mrru) {
733 /*
734 * Actually, forget it. This test is done against the MRRU rather
735 * than the packet size so that we don't end up sending some data
736 * in MP fragments and some data in PROTO_IP packets. That's just
737 * too likely to upset some ppp implementations.
738 */
739 mrutoosmall = 1;
740 sendasip = 0;
741 }
742 }
743
744 bestlink = sendasip ? &dl->physical->link : &mp->link;
745 if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink))
746 break; /* Nothing else to send */
747
748 if (mrutoosmall)
749 log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n");
750 else if (sendasip)
751 log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n");
752
753 if (sendasip) {
754 add = link_QueueLen(&dl->physical->link);
755 if (add) {
756 /* this link has got stuff already queued. Let it continue */
757 total += add;
758 continue;
759 }
760 }
761 }
762
763 m = link_Dequeue(&mp->link);
764 if (m) {
765 len = m_length(m);
766 begin = 1;
767 end = 0;
768
769 while (!end) {
770 if (dl->state == DATALINK_OPEN) {
771 /* Write at most his_mru bytes to the physical link */
772 if (len <= dl->physical->link.lcp.his_mru) {
773 mo = m;
774 end = 1;
775 m_settype(mo, MB_MPOUT);
776 } else {
777 /* It's > his_mru, chop the packet (`m') into bits */
778 mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
779 len -= mo->m_len;
780 m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
781 }
782 mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
783 begin = 0;
784 }
785
786 if (!end) {
787 nlinks--;
788 dl = dl->next;
789 if (!dl) {
790 dl = bundle->links;
791 thislink = 0;
792 } else
793 thislink++;
794 }
795 }
796 }
797 }
798 mp->out.link = thislink; /* Start here next time */
799
800 return total;
801}
802
803int
804mp_SetDatalinkBandwidth(struct cmdargs const *arg)
805{
806 int val;
807
808 if (arg->argc != arg->argn+1)
809 return -1;
810
811 val = atoi(arg->argv[arg->argn]);
812 if (val <= 0) {
813 log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
814 return 1;
815 }
816 arg->cx->mp.bandwidth = val;
817
818 if (arg->cx->state == DATALINK_OPEN)
819 bundle_CalculateBandwidth(arg->bundle);
820
821 return 0;
822}
823
824int
825mp_ShowStatus(struct cmdargs const *arg)
826{
827 struct mp *mp = &arg->bundle->ncp.mp;
828
829 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
830 if (mp->active) {
831 struct mbuf *m, *lm;
832 int bufs = 0;
833
834 lm = NULL;
835 prompt_Printf(arg->prompt, "Socket: %s\n",
836 mp->server.socket.sun_path);
837 for (m = mp->inbufs; m; m = m->m_nextpkt) {
838 bufs++;
839 lm = m;
840 }
841 prompt_Printf(arg->prompt, "Pending frags: %d", bufs);
842 if (bufs) {
843 struct mp_header mh;
844 unsigned long first, last;
845
846 first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
847 last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
848 prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
849 first, last, (unsigned long)mp->seq.next_in,
850 (unsigned long)mp->seq.min_in);
851 prompt_Printf(arg->prompt, " First has %sbegin bit and "
852 "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
853 }
854 prompt_Printf(arg->prompt, "\n");
855 }
856
857 prompt_Printf(arg->prompt, "\nMy Side:\n");
858 if (mp->active) {
859 prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq);
860 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru);
861 prompt_Printf(arg->prompt, " Short Seq: %s\n",
862 mp->local_is12bit ? "on" : "off");
863 }
864 prompt_Printf(arg->prompt, " Discriminator: %s\n",
865 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
866 mp->cfg.enddisc.len));
867
868 prompt_Printf(arg->prompt, "\nHis Side:\n");
869 if (mp->active) {
870 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname);
871 prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in);
872 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru);
873 prompt_Printf(arg->prompt, " Short Seq: %s\n",
874 mp->peer_is12bit ? "on" : "off");
875 }
876 prompt_Printf(arg->prompt, " Discriminator: %s\n",
877 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
878 mp->peer.enddisc.len));
879
880 prompt_Printf(arg->prompt, "\nDefaults:\n");
881
882 prompt_Printf(arg->prompt, " MRRU: ");
883 if (mp->cfg.mrru)
884 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
885 else
886 prompt_Printf(arg->prompt, "disabled\n");
887 prompt_Printf(arg->prompt, " Short Seq: %s\n",
888 command_ShowNegval(mp->cfg.shortseq));
889 prompt_Printf(arg->prompt, " Discriminator: %s\n",
890 command_ShowNegval(mp->cfg.negenddisc));
891 prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%,"
892 " period %d secs\n", mp->cfg.autoload.min,
893 mp->cfg.autoload.max, mp->cfg.autoload.period);
894
895 return 0;
896}
897
898const char *
899mp_Enddisc(u_char c, const char *address, size_t len)
900{
901 static char result[100]; /* Used immediately after it's returned */
902 unsigned f, header;
903
904 switch (c) {
905 case ENDDISC_NULL:
906 sprintf(result, "Null Class");
907 break;
908
909 case ENDDISC_LOCAL:
910 snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
910 snprintf(result, sizeof result, "Local Addr: %.*s", (int)len,
911 address);
911 break;
912
913 case ENDDISC_IP:
914 if (len == 4)
915 snprintf(result, sizeof result, "IP %s",
916 inet_ntoa(*(const struct in_addr *)address));
917 else
912 break;
913
914 case ENDDISC_IP:
915 if (len == 4)
916 snprintf(result, sizeof result, "IP %s",
917 inet_ntoa(*(const struct in_addr *)address));
918 else
918 sprintf(result, "IP[%d] ???", len);
919 sprintf(result, "IP[%zd] ???", len);
919 break;
920
921 case ENDDISC_MAC:
922 if (len == 6) {
923 const u_char *m = (const u_char *)address;
924 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
925 m[0], m[1], m[2], m[3], m[4], m[5]);
926 } else
920 break;
921
922 case ENDDISC_MAC:
923 if (len == 6) {
924 const u_char *m = (const u_char *)address;
925 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
926 m[0], m[1], m[2], m[3], m[4], m[5]);
927 } else
927 sprintf(result, "MAC[%d] ???", len);
928 sprintf(result, "MAC[%zd] ???", len);
928 break;
929
930 case ENDDISC_MAGIC:
931 sprintf(result, "Magic: 0x");
932 header = strlen(result);
933 if (len + header + 1 > sizeof result)
934 len = sizeof result - header - 1;
935 for (f = 0; f < len; f++)
936 sprintf(result + header + 2 * f, "%02x", address[f]);
937 break;
938
939 case ENDDISC_PSN:
929 break;
930
931 case ENDDISC_MAGIC:
932 sprintf(result, "Magic: 0x");
933 header = strlen(result);
934 if (len + header + 1 > sizeof result)
935 len = sizeof result - header - 1;
936 for (f = 0; f < len; f++)
937 sprintf(result + header + 2 * f, "%02x", address[f]);
938 break;
939
940 case ENDDISC_PSN:
940 snprintf(result, sizeof result, "PSN: %.*s", len, address);
941 snprintf(result, sizeof result, "PSN: %.*s", (int)len, address);
941 break;
942
943 default:
944 sprintf(result, "%d: ", (int)c);
945 header = strlen(result);
946 if (len + header + 1 > sizeof result)
947 len = sizeof result - header - 1;
948 for (f = 0; f < len; f++)
949 sprintf(result + header + 2 * f, "%02x", address[f]);
950 break;
951 }
952 return result;
953}
954
955int
956mp_SetEnddisc(struct cmdargs const *arg)
957{
958 struct mp *mp = &arg->bundle->ncp.mp;
959 struct in_addr addr;
960
961 switch (bundle_Phase(arg->bundle)) {
962 case PHASE_DEAD:
963 break;
964 case PHASE_ESTABLISH:
965 /* Make sure none of our links are DATALINK_LCP or greater */
966 if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
967 log_Printf(LogWARN, "enddisc: Only changable before"
968 " LCP negotiations\n");
969 return 1;
970 }
971 break;
972 default:
973 log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
974 return 1;
975 }
976
977 if (arg->argc == arg->argn) {
978 mp->cfg.enddisc.class = 0;
979 *mp->cfg.enddisc.address = '\0';
980 mp->cfg.enddisc.len = 0;
981 } else if (arg->argc > arg->argn) {
982 if (!strcasecmp(arg->argv[arg->argn], "label")) {
983 mp->cfg.enddisc.class = ENDDISC_LOCAL;
984 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
985 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
986 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
987 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
988 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
989 else
990 addr = arg->bundle->ncp.ipcp.my_ip;
991 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
992 mp->cfg.enddisc.class = ENDDISC_IP;
993 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
994 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
995 struct sockaddr_dl hwaddr;
996
997 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
998 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
999 else
1000 addr = arg->bundle->ncp.ipcp.my_ip;
1001
1002 if (arp_EtherAddr(addr, &hwaddr, 1)) {
1003 mp->cfg.enddisc.class = ENDDISC_MAC;
1004 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
1005 hwaddr.sdl_alen);
1006 mp->cfg.enddisc.len = hwaddr.sdl_alen;
1007 } else {
1008 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
1009 inet_ntoa(addr));
1010 return 4;
1011 }
1012 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
1013 int f;
1014
1015 randinit();
1016 for (f = 0; f < 20; f += sizeof(long))
1017 *(long *)(mp->cfg.enddisc.address + f) = random();
1018 mp->cfg.enddisc.class = ENDDISC_MAGIC;
1019 mp->cfg.enddisc.len = 20;
1020 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
1021 if (arg->argc > arg->argn+1) {
1022 mp->cfg.enddisc.class = ENDDISC_PSN;
1023 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
1024 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
1025 } else {
1026 log_Printf(LogWARN, "PSN endpoint requires additional data\n");
1027 return 5;
1028 }
1029 } else {
1030 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
1031 arg->argv[arg->argn]);
1032 return 6;
1033 }
1034 }
1035
1036 return 0;
1037}
1038
1039static int
1040mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
1041 int *n)
1042{
1043 struct mpserver *s = descriptor2mpserver(d);
1044 int result;
1045
1046 result = 0;
1047 if (s->send.dl != NULL) {
1048 /* We've connect()ed */
1049 if (!link_QueueLen(&s->send.dl->physical->link) &&
1050 !s->send.dl->physical->out) {
1051 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
1052 result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
1053 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1054 s->send.dl = NULL;
1055 s->fd = -1;
1056 } else
1057 /* Never read from a datalink that's on death row ! */
1058 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
1059 } else if (r && s->fd >= 0) {
1060 if (*n < s->fd + 1)
1061 *n = s->fd + 1;
1062 FD_SET(s->fd, r);
1063 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
1064 result++;
1065 }
1066 return result;
1067}
1068
1069static int
1070mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
1071{
1072 struct mpserver *s = descriptor2mpserver(d);
1073 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
1074}
1075
1076static void
1077mpserver_Read(struct fdescriptor *d, struct bundle *bundle,
1078 const fd_set *fdset __unused)
1079{
1080 struct mpserver *s = descriptor2mpserver(d);
1081
1082 bundle_ReceiveDatalink(bundle, s->fd);
1083}
1084
1085static int
1086mpserver_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
1087 const fd_set *fdset __unused)
1088{
1089 /* We never want to write here ! */
1090 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
1091 return 0;
1092}
1093
1094void
1095mpserver_Init(struct mpserver *s)
1096{
1097 s->desc.type = MPSERVER_DESCRIPTOR;
1098 s->desc.UpdateSet = mpserver_UpdateSet;
1099 s->desc.IsSet = mpserver_IsSet;
1100 s->desc.Read = mpserver_Read;
1101 s->desc.Write = mpserver_Write;
1102 s->send.dl = NULL;
1103 s->fd = -1;
1104 memset(&s->socket, '\0', sizeof s->socket);
1105}
1106
1107int
1108mpserver_Open(struct mpserver *s, struct peerid *peer)
1109{
1110 int f, l;
1111 mode_t mask;
1112
1113 if (s->fd != -1) {
1114 log_Printf(LogALERT, "Internal error ! mpserver already open\n");
1115 mpserver_Close(s);
1116 }
1117
1118 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
1119 _PATH_VARRUN, peer->authname, peer->enddisc.class);
1120 if (l < 0) {
1121 log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno));
1122 return MPSERVER_FAILED;
1123 }
1124
1125 for (f = 0;
1126 f < peer->enddisc.len && (size_t)l < sizeof s->socket.sun_path - 2;
1127 f++) {
1128 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
1129 "%02x", *(u_char *)(peer->enddisc.address+f));
1130 l += 2;
1131 }
1132
1133 s->socket.sun_family = AF_LOCAL;
1134 s->socket.sun_len = sizeof s->socket;
1135 s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
1136 if (s->fd < 0) {
1137 log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
1138 return MPSERVER_FAILED;
1139 }
1140
1141 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
1142 sizeof s->socket);
1143 mask = umask(0177);
1144
1145 /*
1146 * Try to bind the socket. If we succeed we play server, if we fail
1147 * we connect() and hand the link off.
1148 */
1149
1150 if (ID0bind_un(s->fd, &s->socket) < 0) {
1151 if (errno != EADDRINUSE) {
1152 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
1153 s->socket.sun_path, strerror(errno));
1154 umask(mask);
1155 close(s->fd);
1156 s->fd = -1;
1157 return MPSERVER_FAILED;
1158 }
1159
1160 /* So we're the sender */
1161 umask(mask);
1162 if (ID0connect_un(s->fd, &s->socket) < 0) {
1163 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
1164 s->socket.sun_path, strerror(errno));
1165 if (errno == ECONNREFUSED)
1166 log_Printf(LogPHASE, " The previous server died badly !\n");
1167 close(s->fd);
1168 s->fd = -1;
1169 return MPSERVER_FAILED;
1170 }
1171
1172 /* Donate our link to the other guy */
1173 return MPSERVER_CONNECTED;
1174 }
1175
1176 return MPSERVER_LISTENING;
1177}
1178
1179void
1180mpserver_Close(struct mpserver *s)
1181{
1182 if (s->send.dl != NULL) {
1183 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1184 s->send.dl = NULL;
1185 s->fd = -1;
1186 } else if (s->fd >= 0) {
1187 close(s->fd);
1188 if (ID0unlink(s->socket.sun_path) == -1)
1189 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
1190 strerror(errno));
1191 memset(&s->socket, '\0', sizeof s->socket);
1192 s->fd = -1;
1193 }
1194}
1195
1196void
1197mp_LinkLost(struct mp *mp, struct datalink *dl)
1198{
1199 if (mp->seq.min_in == dl->mp.seq)
1200 /* We've lost the link that's holding everything up ! */
1201 mp_Assemble(mp, NULL, NULL);
1202}
1203
1204size_t
1205mp_QueueLen(struct mp *mp)
1206{
1207 return link_QueueLen(&mp->link);
1208}
942 break;
943
944 default:
945 sprintf(result, "%d: ", (int)c);
946 header = strlen(result);
947 if (len + header + 1 > sizeof result)
948 len = sizeof result - header - 1;
949 for (f = 0; f < len; f++)
950 sprintf(result + header + 2 * f, "%02x", address[f]);
951 break;
952 }
953 return result;
954}
955
956int
957mp_SetEnddisc(struct cmdargs const *arg)
958{
959 struct mp *mp = &arg->bundle->ncp.mp;
960 struct in_addr addr;
961
962 switch (bundle_Phase(arg->bundle)) {
963 case PHASE_DEAD:
964 break;
965 case PHASE_ESTABLISH:
966 /* Make sure none of our links are DATALINK_LCP or greater */
967 if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
968 log_Printf(LogWARN, "enddisc: Only changable before"
969 " LCP negotiations\n");
970 return 1;
971 }
972 break;
973 default:
974 log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
975 return 1;
976 }
977
978 if (arg->argc == arg->argn) {
979 mp->cfg.enddisc.class = 0;
980 *mp->cfg.enddisc.address = '\0';
981 mp->cfg.enddisc.len = 0;
982 } else if (arg->argc > arg->argn) {
983 if (!strcasecmp(arg->argv[arg->argn], "label")) {
984 mp->cfg.enddisc.class = ENDDISC_LOCAL;
985 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
986 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
987 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
988 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
989 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
990 else
991 addr = arg->bundle->ncp.ipcp.my_ip;
992 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
993 mp->cfg.enddisc.class = ENDDISC_IP;
994 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
995 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
996 struct sockaddr_dl hwaddr;
997
998 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
999 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
1000 else
1001 addr = arg->bundle->ncp.ipcp.my_ip;
1002
1003 if (arp_EtherAddr(addr, &hwaddr, 1)) {
1004 mp->cfg.enddisc.class = ENDDISC_MAC;
1005 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
1006 hwaddr.sdl_alen);
1007 mp->cfg.enddisc.len = hwaddr.sdl_alen;
1008 } else {
1009 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
1010 inet_ntoa(addr));
1011 return 4;
1012 }
1013 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
1014 int f;
1015
1016 randinit();
1017 for (f = 0; f < 20; f += sizeof(long))
1018 *(long *)(mp->cfg.enddisc.address + f) = random();
1019 mp->cfg.enddisc.class = ENDDISC_MAGIC;
1020 mp->cfg.enddisc.len = 20;
1021 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
1022 if (arg->argc > arg->argn+1) {
1023 mp->cfg.enddisc.class = ENDDISC_PSN;
1024 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
1025 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
1026 } else {
1027 log_Printf(LogWARN, "PSN endpoint requires additional data\n");
1028 return 5;
1029 }
1030 } else {
1031 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
1032 arg->argv[arg->argn]);
1033 return 6;
1034 }
1035 }
1036
1037 return 0;
1038}
1039
1040static int
1041mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
1042 int *n)
1043{
1044 struct mpserver *s = descriptor2mpserver(d);
1045 int result;
1046
1047 result = 0;
1048 if (s->send.dl != NULL) {
1049 /* We've connect()ed */
1050 if (!link_QueueLen(&s->send.dl->physical->link) &&
1051 !s->send.dl->physical->out) {
1052 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
1053 result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
1054 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1055 s->send.dl = NULL;
1056 s->fd = -1;
1057 } else
1058 /* Never read from a datalink that's on death row ! */
1059 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
1060 } else if (r && s->fd >= 0) {
1061 if (*n < s->fd + 1)
1062 *n = s->fd + 1;
1063 FD_SET(s->fd, r);
1064 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
1065 result++;
1066 }
1067 return result;
1068}
1069
1070static int
1071mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
1072{
1073 struct mpserver *s = descriptor2mpserver(d);
1074 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
1075}
1076
1077static void
1078mpserver_Read(struct fdescriptor *d, struct bundle *bundle,
1079 const fd_set *fdset __unused)
1080{
1081 struct mpserver *s = descriptor2mpserver(d);
1082
1083 bundle_ReceiveDatalink(bundle, s->fd);
1084}
1085
1086static int
1087mpserver_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
1088 const fd_set *fdset __unused)
1089{
1090 /* We never want to write here ! */
1091 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
1092 return 0;
1093}
1094
1095void
1096mpserver_Init(struct mpserver *s)
1097{
1098 s->desc.type = MPSERVER_DESCRIPTOR;
1099 s->desc.UpdateSet = mpserver_UpdateSet;
1100 s->desc.IsSet = mpserver_IsSet;
1101 s->desc.Read = mpserver_Read;
1102 s->desc.Write = mpserver_Write;
1103 s->send.dl = NULL;
1104 s->fd = -1;
1105 memset(&s->socket, '\0', sizeof s->socket);
1106}
1107
1108int
1109mpserver_Open(struct mpserver *s, struct peerid *peer)
1110{
1111 int f, l;
1112 mode_t mask;
1113
1114 if (s->fd != -1) {
1115 log_Printf(LogALERT, "Internal error ! mpserver already open\n");
1116 mpserver_Close(s);
1117 }
1118
1119 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
1120 _PATH_VARRUN, peer->authname, peer->enddisc.class);
1121 if (l < 0) {
1122 log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno));
1123 return MPSERVER_FAILED;
1124 }
1125
1126 for (f = 0;
1127 f < peer->enddisc.len && (size_t)l < sizeof s->socket.sun_path - 2;
1128 f++) {
1129 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
1130 "%02x", *(u_char *)(peer->enddisc.address+f));
1131 l += 2;
1132 }
1133
1134 s->socket.sun_family = AF_LOCAL;
1135 s->socket.sun_len = sizeof s->socket;
1136 s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
1137 if (s->fd < 0) {
1138 log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
1139 return MPSERVER_FAILED;
1140 }
1141
1142 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
1143 sizeof s->socket);
1144 mask = umask(0177);
1145
1146 /*
1147 * Try to bind the socket. If we succeed we play server, if we fail
1148 * we connect() and hand the link off.
1149 */
1150
1151 if (ID0bind_un(s->fd, &s->socket) < 0) {
1152 if (errno != EADDRINUSE) {
1153 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
1154 s->socket.sun_path, strerror(errno));
1155 umask(mask);
1156 close(s->fd);
1157 s->fd = -1;
1158 return MPSERVER_FAILED;
1159 }
1160
1161 /* So we're the sender */
1162 umask(mask);
1163 if (ID0connect_un(s->fd, &s->socket) < 0) {
1164 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
1165 s->socket.sun_path, strerror(errno));
1166 if (errno == ECONNREFUSED)
1167 log_Printf(LogPHASE, " The previous server died badly !\n");
1168 close(s->fd);
1169 s->fd = -1;
1170 return MPSERVER_FAILED;
1171 }
1172
1173 /* Donate our link to the other guy */
1174 return MPSERVER_CONNECTED;
1175 }
1176
1177 return MPSERVER_LISTENING;
1178}
1179
1180void
1181mpserver_Close(struct mpserver *s)
1182{
1183 if (s->send.dl != NULL) {
1184 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1185 s->send.dl = NULL;
1186 s->fd = -1;
1187 } else if (s->fd >= 0) {
1188 close(s->fd);
1189 if (ID0unlink(s->socket.sun_path) == -1)
1190 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
1191 strerror(errno));
1192 memset(&s->socket, '\0', sizeof s->socket);
1193 s->fd = -1;
1194 }
1195}
1196
1197void
1198mp_LinkLost(struct mp *mp, struct datalink *dl)
1199{
1200 if (mp->seq.min_in == dl->mp.seq)
1201 /* We've lost the link that's holding everything up ! */
1202 mp_Assemble(mp, NULL, NULL);
1203}
1204
1205size_t
1206mp_QueueLen(struct mp *mp)
1207{
1208 return link_QueueLen(&mp->link);
1209}