2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
60 #include "throughput.h"
61 #include "slcompress.h"
71 #include "descriptor.h"
91 peerid_Init(struct peerid *peer)
93 peer->enddisc.class = 0;
94 *peer->enddisc.address = '\0';
95 peer->enddisc.len = 0;
96 *peer->authname = '\0';
100 peerid_Equal(const struct peerid *p1, const struct peerid *p2)
102 return !strcmp(p1->authname, p2->authname) &&
103 p1->enddisc.class == p2->enddisc.class &&
104 p1->enddisc.len == p2->enddisc.len &&
105 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
109 inc_seq(unsigned is12bit, u_int32_t seq)
113 if (seq & 0xfffff000)
115 } else if (seq & 0xff000000)
121 isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
123 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
126 if (seq2 < 0x200 || seq2 > seq1)
128 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
135 mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
137 if (mp->local_is12bit) {
140 ua_ntohs(MBUF_CTOP(m), &val);
142 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
145 header->begin = val & 0x8000 ? 1 : 0;
146 header->end = val & 0x4000 ? 1 : 0;
147 header->seq = val & 0x0fff;
150 ua_ntohl(MBUF_CTOP(m), &header->seq);
151 if (header->seq & 0x3f000000) {
152 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
155 header->begin = header->seq & 0x80000000 ? 1 : 0;
156 header->end = header->seq & 0x40000000 ? 1 : 0;
157 header->seq &= 0x00ffffff;
163 mp_LayerStart(void *v, struct fsm *fp)
165 /* The given FSM (ccp) is about to start up ! */
169 mp_LayerUp(void *v, struct fsm *fp)
171 /* The given fsm (ccp) is now up */
173 bundle_CalculateBandwidth(fp->bundle); /* Against ccp_MTUOverhead */
177 mp_LayerDown(void *v, struct fsm *fp)
179 /* The given FSM (ccp) has been told to come down */
183 mp_LayerFinish(void *v, struct fsm *fp)
185 /* The given fsm (ccp) is now down */
186 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
187 fsm_Open(fp); /* CCP goes to ST_STOPPED */
193 struct mp *mp = (struct mp *)v;
196 percent = MAX(mp->link.stats.total.in.OctetsPerSecond,
197 mp->link.stats.total.out.OctetsPerSecond) * 800 /
198 mp->bundle->bandwidth;
199 if (percent >= mp->cfg.autoload.max) {
200 log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
201 bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
202 } else if (percent <= mp->cfg.autoload.min) {
203 log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
204 bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
209 mp_StopAutoloadTimer(struct mp *mp)
211 throughput_stop(&mp->link.stats.total);
215 mp_CheckAutoloadTimer(struct mp *mp)
217 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period) {
218 throughput_destroy(&mp->link.stats.total);
219 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
220 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
223 if (bundle_WantAutoloadTimer(mp->bundle))
224 throughput_start(&mp->link.stats.total, "MP throughput", 1);
226 mp_StopAutoloadTimer(mp);
230 mp_RestartAutoloadTimer(struct mp *mp)
232 if (mp->link.stats.total.SamplePeriod != mp->cfg.autoload.period)
233 mp_CheckAutoloadTimer(mp);
235 throughput_clear(&mp->link.stats.total, THROUGHPUT_OVERALL, NULL);
239 mp_Init(struct mp *mp, struct bundle *bundle)
241 mp->peer_is12bit = mp->local_is12bit = 0;
242 mp->peer_mrru = mp->local_mrru = 0;
244 peerid_Init(&mp->peer);
248 mp->out.af = AF_INET;
254 mp->link.type = LOGICAL_LINK;
255 mp->link.name = "mp";
256 mp->link.len = sizeof *mp;
258 mp->cfg.autoload.period = SAMPLE_PERIOD;
259 mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
260 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
261 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
262 mp->link.stats.parent = NULL;
263 mp->link.stats.gather = 0; /* Let the physical links gather stats */
264 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
265 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
266 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
268 mp->fsmp.LayerStart = mp_LayerStart;
269 mp->fsmp.LayerUp = mp_LayerUp;
270 mp->fsmp.LayerDown = mp_LayerDown;
271 mp->fsmp.LayerFinish = mp_LayerFinish;
272 mp->fsmp.object = mp;
274 mpserver_Init(&mp->server);
277 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
278 mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
279 mp->cfg.enddisc.class = 0;
280 *mp->cfg.enddisc.address = '\0';
281 mp->cfg.enddisc.len = 0;
283 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
284 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
286 link_EmptyStack(&mp->link);
287 link_Stack(&mp->link, &protolayer);
288 link_Stack(&mp->link, &ccplayer);
289 link_Stack(&mp->link, &vjlayer);
291 link_Stack(&mp->link, &natlayer);
296 mp_Up(struct mp *mp, struct datalink *dl)
298 struct lcp *lcp = &dl->physical->link.lcp;
301 /* We're adding a link - do a last validation on our parameters */
302 if (!peerid_Equal(&dl->peer, &mp->peer)) {
303 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
304 log_Printf(LogPHASE, " Attached to peer %s/%s\n", mp->peer.authname,
305 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
306 mp->peer.enddisc.len));
307 log_Printf(LogPHASE, " New link is peer %s/%s\n", dl->peer.authname,
308 mp_Enddisc(dl->peer.enddisc.class, dl->peer.enddisc.address,
309 dl->peer.enddisc.len));
312 if (mp->local_mrru != lcp->want_mrru ||
313 mp->peer_mrru != lcp->his_mrru ||
314 mp->local_is12bit != lcp->want_shortseq ||
315 mp->peer_is12bit != lcp->his_shortseq) {
316 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
322 /* First link in multilink mode */
324 mp->local_mrru = lcp->want_mrru;
325 mp->peer_mrru = lcp->his_mrru;
326 mp->local_is12bit = lcp->want_shortseq;
327 mp->peer_is12bit = lcp->his_shortseq;
330 throughput_destroy(&mp->link.stats.total);
331 throughput_init(&mp->link.stats.total, mp->cfg.autoload.period);
332 throughput_callback(&mp->link.stats.total, mp_UpDown, mp);
333 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
334 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
335 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
337 /* Tell the link who it belongs to */
338 dl->physical->link.stats.parent = &mp->link.stats.total;
342 mp->out.af = AF_INET;
347 * Now we create our server socket.
348 * If it already exists, join it. Otherwise, create and own it
350 switch (mpserver_Open(&mp->server, &mp->peer)) {
351 case MPSERVER_CONNECTED:
352 log_Printf(LogPHASE, "mp: Transfer link on %s\n",
353 mp->server.socket.sun_path);
354 mp->server.send.dl = dl; /* Defer 'till it's safe to send */
356 case MPSERVER_FAILED:
358 case MPSERVER_LISTENING:
359 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
360 log_Printf(LogPHASE, " First link: %s\n", dl->name);
362 /* Re-point our NCP layers at our MP link */
363 ncp_SetLink(&mp->bundle->ncp, &mp->link);
365 /* Our lcp's already up 'cos of the NULL parent */
366 if (ccp_SetOpenMode(&mp->link.ccp)) {
367 fsm_Up(&mp->link.ccp.fsm);
368 fsm_Open(&mp->link.ccp.fsm);
380 mp_Down(struct mp *mp)
386 mp_StopAutoloadTimer(mp);
388 /* Don't want any more of these */
389 mpserver_Close(&mp->server);
391 /* CCP goes down with a bang */
392 fsm2initial(&mp->link.ccp.fsm);
394 /* Received fragments go in the bit-bucket */
396 next = mp->inbufs->m_nextpkt;
401 peerid_Init(&mp->peer);
407 mp_linkInit(struct mp_link *mplink)
410 mplink->bandwidth = 0;
414 mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
416 struct mp_header mh, h;
417 struct mbuf *q, *last;
421 * When `m' and `p' are NULL, it means our oldest link has gone down.
422 * We want to determine a new min, and process any intermediate stuff
426 if (m && mp_ReadHeader(mp, m, &mh) == 0) {
433 p->dl->mp.seq = mh.seq;
435 seq = mp->seq.min_in;
437 if (mp->seq.min_in == seq) {
439 * We've received new data on the link that has our min (oldest) seq.
440 * Figure out which link now has the smallest (oldest) seq.
444 mp->seq.min_in = (u_int32_t)-1;
445 for (dl = mp->bundle->links; dl; dl = dl->next)
446 if (dl->state == DATALINK_OPEN &&
447 (mp->seq.min_in == -1 ||
448 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
449 mp->seq.min_in = dl->mp.seq;
453 * Now process as many of our fragments as we can, adding our new
454 * fragment in as we go, and ordering with the oldest at the top of
459 seq = mp->seq.next_in;
471 mp_ReadHeader(mp, q, &h);
473 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
474 /* Our received fragment fits in before this one, so link it in */
487 /* we're missing something :-( */
488 if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
489 /* we're never gonna get it */
492 /* Zap all older fragments */
493 while (mp->inbufs != q) {
494 log_Printf(LogDEBUG, "Drop frag\n");
495 next = mp->inbufs->m_nextpkt;
501 * Zap everything until the next `end' fragment OR just before
502 * the next `begin' fragment OR 'till seq.min_in - whichever
506 mp_ReadHeader(mp, mp->inbufs, &h);
508 /* We might be able to process this ! */
509 h.seq--; /* We're gonna look for fragment with h.seq+1 */
512 next = mp->inbufs->m_nextpkt;
513 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
516 } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
520 * Continue processing things from here.
521 * This deals with the possibility that we received a fragment
522 * on the slowest link that invalidates some of our data (because
523 * of the hole at `q'), but where there are subsequent `whole'
524 * packets that have already been received.
527 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
531 /* we may still receive the missing fragment */
534 /* We've got something, reassemble */
535 struct mbuf **frag = &q;
541 mp->inbufs = mp->inbufs->m_nextpkt;
542 len = mp_ReadHeader(mp, *frag, &h);
545 if (frag == &q && !h.begin) {
546 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
550 } else if (frag != &q && h.begin) {
551 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
554 * Stuff our fragment back at the front of the queue and zap
555 * our half-assembled packet.
557 (*frag)->m_nextpkt = mp->inbufs;
563 h.end = 0; /* just in case it's a whole packet */
565 (*frag)->m_offset += len;
566 (*frag)->m_len -= len;
567 (*frag)->m_nextpkt = NULL;
569 frag = &(*frag)->m_next;
570 while (*frag != NULL);
576 log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
577 first, (u_long)h.seq, m_length(q));
578 link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
582 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
586 /* Look for the next fragment */
587 seq = inc_seq(mp->local_is12bit, seq);
594 /* We still have to find a home for our new fragment */
596 for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
597 mp_ReadHeader(mp, q, &h);
598 if (isbefore(mp->local_is12bit, mh.seq, h.seq))
601 /* Our received fragment fits in here */
611 mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
613 struct physical *p = link2physical(l);
615 if (!bundle->ncp.mp.active)
616 /* Let someone else deal with it ! */
620 log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
623 m_settype(bp, MB_MPIN);
624 mp_Assemble(&bundle->ncp.mp, bp, p);
631 mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
632 struct mbuf *m, u_int32_t begin, u_int32_t end)
636 /* Stuff an MP header on the front of our packet and send it */
638 if (mp->peer_is12bit) {
641 val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
642 ua_htons(&val, prepend);
643 m = m_prepend(m, prepend, 2, 0);
647 val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
648 ua_htonl(&val, prepend);
649 m = m_prepend(m, prepend, 4, 0);
651 if (log_IsKept(LogDEBUG))
652 log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
653 mp->out.seq, m_length(m), l->name);
654 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
656 if (l->ccp.fsm.state != ST_OPENED && ccp_Required(&l->ccp)) {
657 log_Printf(LogPHASE, "%s: Not transmitting... waiting for CCP\n", l->name);
661 link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
665 mp_FillPhysicalQueues(struct bundle *bundle)
667 struct mp *mp = &bundle->ncp.mp;
668 struct datalink *dl, *fdl;
669 size_t total, add, len;
670 int thislink, nlinks, nopenlinks, sendasip;
671 u_int32_t begin, end;
673 struct link *bestlink;
675 thislink = nlinks = nopenlinks = 0;
676 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
677 /* Include non-open links here as mp->out.link will stay more correct */
679 if (thislink == mp->out.link)
685 if (dl->state == DATALINK_OPEN)
697 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
703 if (dl->state != DATALINK_OPEN)
706 if (dl->physical->out)
707 /* this link has suffered a short write. Let it continue */
710 add = link_QueueLen(&dl->physical->link);
712 /* this link has got stuff already queued. Let it continue */
717 if (!mp_QueueLen(mp)) {
721 * If there's only a single open link in our bundle and we haven't got
722 * MP level link compression, queue outbound traffic directly via that
723 * link's protocol stack rather than using the MP link. This results
724 * in the outbound traffic going out as PROTO_IP or PROTO_IPV6 rather
729 sendasip = nopenlinks < 2;
731 if (dl->physical->link.lcp.his_mru < mp->peer_mrru) {
733 * Actually, forget it. This test is done against the MRRU rather
734 * than the packet size so that we don't end up sending some data
735 * in MP fragments and some data in PROTO_IP packets. That's just
736 * too likely to upset some ppp implementations.
743 bestlink = sendasip ? &dl->physical->link : &mp->link;
744 if (!ncp_PushPacket(&bundle->ncp, &mp->out.af, bestlink))
745 break; /* Nothing else to send */
748 log_Printf(LogDEBUG, "Don't send data as PROTO_IP, MRU < MRRU\n");
750 log_Printf(LogDEBUG, "Sending data as PROTO_IP, not PROTO_MP\n");
753 add = link_QueueLen(&dl->physical->link);
755 /* this link has got stuff already queued. Let it continue */
762 m = link_Dequeue(&mp->link);
769 if (dl->state == DATALINK_OPEN) {
770 /* Write at most his_mru bytes to the physical link */
771 if (len <= dl->physical->link.lcp.his_mru) {
774 m_settype(mo, MB_MPOUT);
776 /* It's > his_mru, chop the packet (`m') into bits */
777 mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
779 m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
781 mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
797 mp->out.link = thislink; /* Start here next time */
803 mp_SetDatalinkBandwidth(struct cmdargs const *arg)
807 if (arg->argc != arg->argn+1)
810 val = atoi(arg->argv[arg->argn]);
812 log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
815 arg->cx->mp.bandwidth = val;
817 if (arg->cx->state == DATALINK_OPEN)
818 bundle_CalculateBandwidth(arg->bundle);
824 mp_ShowStatus(struct cmdargs const *arg)
826 struct mp *mp = &arg->bundle->ncp.mp;
828 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
834 prompt_Printf(arg->prompt, "Socket: %s\n",
835 mp->server.socket.sun_path);
836 for (m = mp->inbufs; m; m = m->m_nextpkt) {
840 prompt_Printf(arg->prompt, "Pending frags: %d", bufs);
843 unsigned long first, last;
845 first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
846 last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
847 prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
848 first, last, (unsigned long)mp->seq.next_in,
849 (unsigned long)mp->seq.min_in);
850 prompt_Printf(arg->prompt, " First has %sbegin bit and "
851 "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
853 prompt_Printf(arg->prompt, "\n");
856 prompt_Printf(arg->prompt, "\nMy Side:\n");
858 prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq);
859 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru);
860 prompt_Printf(arg->prompt, " Short Seq: %s\n",
861 mp->local_is12bit ? "on" : "off");
863 prompt_Printf(arg->prompt, " Discriminator: %s\n",
864 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
865 mp->cfg.enddisc.len));
867 prompt_Printf(arg->prompt, "\nHis Side:\n");
869 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname);
870 prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in);
871 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru);
872 prompt_Printf(arg->prompt, " Short Seq: %s\n",
873 mp->peer_is12bit ? "on" : "off");
875 prompt_Printf(arg->prompt, " Discriminator: %s\n",
876 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
877 mp->peer.enddisc.len));
879 prompt_Printf(arg->prompt, "\nDefaults:\n");
881 prompt_Printf(arg->prompt, " MRRU: ");
883 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
885 prompt_Printf(arg->prompt, "disabled\n");
886 prompt_Printf(arg->prompt, " Short Seq: %s\n",
887 command_ShowNegval(mp->cfg.shortseq));
888 prompt_Printf(arg->prompt, " Discriminator: %s\n",
889 command_ShowNegval(mp->cfg.negenddisc));
890 prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%,"
891 " period %d secs\n", mp->cfg.autoload.min,
892 mp->cfg.autoload.max, mp->cfg.autoload.period);
898 mp_Enddisc(u_char c, const char *address, int len)
900 static char result[100]; /* Used immediately after it's returned */
905 sprintf(result, "Null Class");
909 snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
914 snprintf(result, sizeof result, "IP %s",
915 inet_ntoa(*(const struct in_addr *)address));
917 sprintf(result, "IP[%d] ???", len);
922 const u_char *m = (const u_char *)address;
923 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
924 m[0], m[1], m[2], m[3], m[4], m[5]);
926 sprintf(result, "MAC[%d] ???", len);
930 sprintf(result, "Magic: 0x");
931 header = strlen(result);
932 if (len > sizeof result - header - 1)
933 len = sizeof result - header - 1;
934 for (f = 0; f < len; f++)
935 sprintf(result + header + 2 * f, "%02x", address[f]);
939 snprintf(result, sizeof result, "PSN: %.*s", len, address);
943 sprintf(result, "%d: ", (int)c);
944 header = strlen(result);
945 if (len > sizeof result - header - 1)
946 len = sizeof result - header - 1;
947 for (f = 0; f < len; f++)
948 sprintf(result + header + 2 * f, "%02x", address[f]);
955 mp_SetEnddisc(struct cmdargs const *arg)
957 struct mp *mp = &arg->bundle->ncp.mp;
960 switch (bundle_Phase(arg->bundle)) {
963 case PHASE_ESTABLISH:
964 /* Make sure none of our links are DATALINK_LCP or greater */
965 if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
966 log_Printf(LogWARN, "enddisc: Only changable before"
967 " LCP negotiations\n");
972 log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
976 if (arg->argc == arg->argn) {
977 mp->cfg.enddisc.class = 0;
978 *mp->cfg.enddisc.address = '\0';
979 mp->cfg.enddisc.len = 0;
980 } else if (arg->argc > arg->argn) {
981 if (!strcasecmp(arg->argv[arg->argn], "label")) {
982 mp->cfg.enddisc.class = ENDDISC_LOCAL;
983 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
984 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
985 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
986 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
987 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
989 addr = arg->bundle->ncp.ipcp.my_ip;
990 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
991 mp->cfg.enddisc.class = ENDDISC_IP;
992 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
993 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
994 struct sockaddr_dl hwaddr;
997 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
998 ncprange_getip4addr(&arg->bundle->ncp.ipcp.cfg.my_range, &addr);
1000 addr = arg->bundle->ncp.ipcp.my_ip;
1002 s = ID0socket(PF_INET, SOCK_DGRAM, 0);
1004 log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
1007 if (arp_EtherAddr(s, addr, &hwaddr, 1)) {
1008 mp->cfg.enddisc.class = ENDDISC_MAC;
1009 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
1011 mp->cfg.enddisc.len = hwaddr.sdl_alen;
1013 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
1019 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
1023 for (f = 0; f < 20; f += sizeof(long))
1024 *(long *)(mp->cfg.enddisc.address + f) = random();
1025 mp->cfg.enddisc.class = ENDDISC_MAGIC;
1026 mp->cfg.enddisc.len = 20;
1027 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
1028 if (arg->argc > arg->argn+1) {
1029 mp->cfg.enddisc.class = ENDDISC_PSN;
1030 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
1031 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
1033 log_Printf(LogWARN, "PSN endpoint requires additional data\n");
1037 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
1038 arg->argv[arg->argn]);
1047 mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
1050 struct mpserver *s = descriptor2mpserver(d);
1054 if (s->send.dl != NULL) {
1055 /* We've connect()ed */
1056 if (!link_QueueLen(&s->send.dl->physical->link) &&
1057 !s->send.dl->physical->out) {
1058 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
1059 result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
1060 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1064 /* Never read from a datalink that's on death row ! */
1065 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
1066 } else if (r && s->fd >= 0) {
1070 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
1077 mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
1079 struct mpserver *s = descriptor2mpserver(d);
1080 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
1084 mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
1086 struct mpserver *s = descriptor2mpserver(d);
1088 bundle_ReceiveDatalink(bundle, s->fd);
1092 mpserver_Write(struct fdescriptor *d, struct bundle *bundle,
1093 const fd_set *fdset)
1095 /* We never want to write here ! */
1096 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
1101 mpserver_Init(struct mpserver *s)
1103 s->desc.type = MPSERVER_DESCRIPTOR;
1104 s->desc.UpdateSet = mpserver_UpdateSet;
1105 s->desc.IsSet = mpserver_IsSet;
1106 s->desc.Read = mpserver_Read;
1107 s->desc.Write = mpserver_Write;
1110 memset(&s->socket, '\0', sizeof s->socket);
1114 mpserver_Open(struct mpserver *s, struct peerid *peer)
1120 log_Printf(LogALERT, "Internal error ! mpserver already open\n");
1124 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
1125 _PATH_VARRUN, peer->authname, peer->enddisc.class);
1127 log_Printf(LogERROR, "mpserver: snprintf(): %s\n", strerror(errno));
1128 return MPSERVER_FAILED;
1131 for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
1132 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
1133 "%02x", *(u_char *)(peer->enddisc.address+f));
1137 s->socket.sun_family = AF_LOCAL;
1138 s->socket.sun_len = sizeof s->socket;
1139 s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
1141 log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
1142 return MPSERVER_FAILED;
1145 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
1150 * Try to bind the socket. If we succeed we play server, if we fail
1151 * we connect() and hand the link off.
1154 if (ID0bind_un(s->fd, &s->socket) < 0) {
1155 if (errno != EADDRINUSE) {
1156 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
1157 s->socket.sun_path, strerror(errno));
1161 return MPSERVER_FAILED;
1164 /* So we're the sender */
1166 if (ID0connect_un(s->fd, &s->socket) < 0) {
1167 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
1168 s->socket.sun_path, strerror(errno));
1169 if (errno == ECONNREFUSED)
1170 log_Printf(LogPHASE, " The previous server died badly !\n");
1173 return MPSERVER_FAILED;
1176 /* Donate our link to the other guy */
1177 return MPSERVER_CONNECTED;
1180 return MPSERVER_LISTENING;
1184 mpserver_Close(struct mpserver *s)
1186 if (s->send.dl != NULL) {
1187 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1190 } else if (s->fd >= 0) {
1192 if (ID0unlink(s->socket.sun_path) == -1)
1193 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
1195 memset(&s->socket, '\0', sizeof s->socket);
1201 mp_LinkLost(struct mp *mp, struct datalink *dl)
1203 if (mp->seq.min_in == dl->mp.seq)
1204 /* We've lost the link that's holding everything up ! */
1205 mp_Assemble(mp, NULL, NULL);
1209 mp_QueueLen(struct mp *mp)
1211 return link_QueueLen(&mp->link);