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"
70 #include "descriptor.h"
89 peerid_Init(struct peerid *peer)
91 peer->enddisc.class = 0;
92 *peer->enddisc.address = '\0';
93 peer->enddisc.len = 0;
94 *peer->authname = '\0';
98 peerid_Equal(const struct peerid *p1, const struct peerid *p2)
100 return !strcmp(p1->authname, p2->authname) &&
101 p1->enddisc.class == p2->enddisc.class &&
102 p1->enddisc.len == p2->enddisc.len &&
103 !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len);
107 inc_seq(unsigned is12bit, u_int32_t seq)
111 if (seq & 0xfffff000)
113 } else if (seq & 0xff000000)
119 isbefore(unsigned is12bit, u_int32_t seq1, u_int32_t seq2)
121 u_int32_t max = (is12bit ? 0xfff : 0xffffff) - 0x200;
124 if (seq2 < 0x200 || seq2 > seq1)
126 } else if ((seq1 > 0x200 || seq2 <= max) && seq1 < seq2)
133 mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header)
135 if (mp->local_is12bit) {
138 ua_ntohs(MBUF_CTOP(m), &val);
140 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
143 header->begin = val & 0x8000 ? 1 : 0;
144 header->end = val & 0x4000 ? 1 : 0;
145 header->seq = val & 0x0fff;
148 ua_ntohl(MBUF_CTOP(m), &header->seq);
149 if (header->seq & 0x3f000000) {
150 log_Printf(LogWARN, "Oops - MP header without required zero bits\n");
153 header->begin = header->seq & 0x80000000 ? 1 : 0;
154 header->end = header->seq & 0x40000000 ? 1 : 0;
155 header->seq &= 0x00ffffff;
161 mp_LayerStart(void *v, struct fsm *fp)
163 /* The given FSM (ccp) is about to start up ! */
167 mp_LayerUp(void *v, struct fsm *fp)
169 /* The given fsm (ccp) is now up */
173 mp_LayerDown(void *v, struct fsm *fp)
175 /* The given FSM (ccp) has been told to come down */
179 mp_LayerFinish(void *v, struct fsm *fp)
181 /* The given fsm (ccp) is now down */
182 if (fp->state == ST_CLOSED && fp->open_mode == OPEN_PASSIVE)
183 fsm_Open(fp); /* CCP goes to ST_STOPPED */
189 struct mp *mp = (struct mp *)v;
192 percent = mp->link.throughput.OctetsPerSecond * 800 / mp->bundle->bandwidth;
193 if (percent >= mp->cfg.autoload.max) {
194 log_Printf(LogDEBUG, "%d%% saturation - bring a link up ?\n", percent);
195 bundle_AutoAdjust(mp->bundle, percent, AUTO_UP);
196 } else if (percent <= mp->cfg.autoload.min) {
197 log_Printf(LogDEBUG, "%d%% saturation - bring a link down ?\n", percent);
198 bundle_AutoAdjust(mp->bundle, percent, AUTO_DOWN);
203 mp_StopAutoloadTimer(struct mp *mp)
205 throughput_stop(&mp->link.throughput);
209 mp_CheckAutoloadTimer(struct mp *mp)
211 if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period) {
212 throughput_destroy(&mp->link.throughput);
213 throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
214 throughput_callback(&mp->link.throughput, mp_UpDown, mp);
217 if (bundle_WantAutoloadTimer(mp->bundle))
218 throughput_start(&mp->link.throughput, "MP throughput", 1);
220 mp_StopAutoloadTimer(mp);
224 mp_RestartAutoloadTimer(struct mp *mp)
226 if (mp->link.throughput.SamplePeriod != mp->cfg.autoload.period)
227 mp_CheckAutoloadTimer(mp);
229 throughput_clear(&mp->link.throughput, THROUGHPUT_OVERALL, NULL);
233 mp_Init(struct mp *mp, struct bundle *bundle)
235 mp->peer_is12bit = mp->local_is12bit = 0;
236 mp->peer_mrru = mp->local_mrru = 0;
238 peerid_Init(&mp->peer);
247 mp->link.type = LOGICAL_LINK;
248 mp->link.name = "mp";
249 mp->link.len = sizeof *mp;
251 mp->cfg.autoload.period = SAMPLE_PERIOD;
252 mp->cfg.autoload.min = mp->cfg.autoload.max = 0;
253 throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
254 throughput_callback(&mp->link.throughput, mp_UpDown, mp);
255 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
256 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
257 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
259 mp->fsmp.LayerStart = mp_LayerStart;
260 mp->fsmp.LayerUp = mp_LayerUp;
261 mp->fsmp.LayerDown = mp_LayerDown;
262 mp->fsmp.LayerFinish = mp_LayerFinish;
263 mp->fsmp.object = mp;
265 mpserver_Init(&mp->server);
268 mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED;
269 mp->cfg.negenddisc = NEG_ENABLED|NEG_ACCEPTED;
270 mp->cfg.enddisc.class = 0;
271 *mp->cfg.enddisc.address = '\0';
272 mp->cfg.enddisc.len = 0;
274 lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL);
275 ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp);
277 link_EmptyStack(&mp->link);
278 link_Stack(&mp->link, &protolayer);
279 link_Stack(&mp->link, &ccplayer);
280 link_Stack(&mp->link, &vjlayer);
282 link_Stack(&mp->link, &natlayer);
287 mp_Up(struct mp *mp, struct datalink *dl)
289 struct lcp *lcp = &dl->physical->link.lcp;
292 /* We're adding a link - do a last validation on our parameters */
293 if (!peerid_Equal(&dl->peer, &mp->peer)) {
294 log_Printf(LogPHASE, "%s: Inappropriate peer !\n", dl->name);
297 if (mp->local_mrru != lcp->want_mrru ||
298 mp->peer_mrru != lcp->his_mrru ||
299 mp->local_is12bit != lcp->want_shortseq ||
300 mp->peer_is12bit != lcp->his_shortseq) {
301 log_Printf(LogPHASE, "%s: Invalid MRRU/SHORTSEQ MP parameters !\n",
307 /* First link in multilink mode */
309 mp->local_mrru = lcp->want_mrru;
310 mp->peer_mrru = lcp->his_mrru;
311 mp->local_is12bit = lcp->want_shortseq;
312 mp->peer_is12bit = lcp->his_shortseq;
315 throughput_destroy(&mp->link.throughput);
316 throughput_init(&mp->link.throughput, mp->cfg.autoload.period);
317 throughput_callback(&mp->link.throughput, mp_UpDown, mp);
318 memset(mp->link.Queue, '\0', sizeof mp->link.Queue);
319 memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in);
320 memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out);
328 * Now we create our server socket.
329 * If it already exists, join it. Otherwise, create and own it
331 switch (mpserver_Open(&mp->server, &mp->peer)) {
332 case MPSERVER_CONNECTED:
333 log_Printf(LogPHASE, "mp: Transfer link on %s\n",
334 mp->server.socket.sun_path);
335 mp->server.send.dl = dl; /* Defer 'till it's safe to send */
337 case MPSERVER_FAILED:
339 case MPSERVER_LISTENING:
340 log_Printf(LogPHASE, "mp: Listening on %s\n", mp->server.socket.sun_path);
341 log_Printf(LogPHASE, " First link: %s\n", dl->name);
343 /* Re-point our IPCP layer at our MP link */
344 ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link);
346 /* Our lcp's already up 'cos of the NULL parent */
347 if (ccp_SetOpenMode(&mp->link.ccp)) {
348 fsm_Up(&mp->link.ccp.fsm);
349 fsm_Open(&mp->link.ccp.fsm);
361 mp_Down(struct mp *mp)
367 mp_StopAutoloadTimer(mp);
369 /* Don't want any more of these */
370 mpserver_Close(&mp->server);
372 /* CCP goes down with a bang */
373 fsm2initial(&mp->link.ccp.fsm);
375 /* Received fragments go in the bit-bucket */
377 next = mp->inbufs->m_nextpkt;
382 peerid_Init(&mp->peer);
388 mp_linkInit(struct mp_link *mplink)
391 mplink->bandwidth = 0;
395 mp_Assemble(struct mp *mp, struct mbuf *m, struct physical *p)
397 struct mp_header mh, h;
398 struct mbuf *q, *last;
402 * When `m' and `p' are NULL, it means our oldest link has gone down.
403 * We want to determine a new min, and process any intermediate stuff
407 if (m && mp_ReadHeader(mp, m, &mh) == 0) {
414 p->dl->mp.seq = mh.seq;
416 seq = mp->seq.min_in;
418 if (mp->seq.min_in == seq) {
420 * We've received new data on the link that has our min (oldest) seq.
421 * Figure out which link now has the smallest (oldest) seq.
425 mp->seq.min_in = (u_int32_t)-1;
426 for (dl = mp->bundle->links; dl; dl = dl->next)
427 if (dl->state == DATALINK_OPEN &&
428 (mp->seq.min_in == -1 ||
429 isbefore(mp->local_is12bit, dl->mp.seq, mp->seq.min_in)))
430 mp->seq.min_in = dl->mp.seq;
434 * Now process as many of our fragments as we can, adding our new
435 * fragment in as we go, and ordering with the oldest at the top of
445 seq = mp->seq.next_in;
448 mp_ReadHeader(mp, q, &h);
449 if (m && isbefore(mp->local_is12bit, mh.seq, h.seq)) {
450 /* Our received fragment fits in before this one, so link it in */
462 /* we're missing something :-( */
463 if (isbefore(mp->local_is12bit, seq, mp->seq.min_in)) {
464 /* we're never gonna get it */
467 /* Zap all older fragments */
468 while (mp->inbufs != q) {
469 log_Printf(LogDEBUG, "Drop frag\n");
470 next = mp->inbufs->m_nextpkt;
476 * Zap everything until the next `end' fragment OR just before
477 * the next `begin' fragment OR 'till seq.min_in - whichever
481 mp_ReadHeader(mp, mp->inbufs, &h);
483 /* We might be able to process this ! */
484 h.seq--; /* We're gonna look for fragment with h.seq+1 */
487 next = mp->inbufs->m_nextpkt;
488 log_Printf(LogDEBUG, "Drop frag %u\n", h.seq);
491 } while (mp->inbufs && (isbefore(mp->local_is12bit, mp->seq.min_in,
495 * Continue processing things from here.
496 * This deals with the possibility that we received a fragment
497 * on the slowest link that invalidates some of our data (because
498 * of the hole at `q'), but where there are subsequent `whole'
499 * packets that have already been received.
502 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
506 /* we may still receive the missing fragment */
509 /* We've got something, reassemble */
510 struct mbuf **frag = &q;
516 mp->inbufs = mp->inbufs->m_nextpkt;
517 len = mp_ReadHeader(mp, *frag, &h);
520 (*frag)->m_offset += len;
521 (*frag)->m_len -= len;
522 (*frag)->m_nextpkt = NULL;
523 if (frag == &q && !h.begin) {
524 log_Printf(LogWARN, "Oops - MP frag %lu should have a begin flag\n",
528 } else if (frag != &q && h.begin) {
529 log_Printf(LogWARN, "Oops - MP frag %lu should have an end flag\n",
532 * Stuff our fragment back at the front of the queue and zap
533 * our half-assembed packet.
535 (*frag)->m_nextpkt = mp->inbufs;
541 h.end = 0; /* just in case it's a whole packet */
544 frag = &(*frag)->m_next;
545 while (*frag != NULL);
550 log_Printf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n",
551 first, (u_long)h.seq, m_length(q));
552 link_PullPacket(&mp->link, MBUF_CTOP(q), q->m_len, mp->bundle);
556 mp->seq.next_in = seq = inc_seq(mp->local_is12bit, h.seq);
560 /* Look for the next fragment */
561 seq = inc_seq(mp->local_is12bit, seq);
568 /* We still have to find a home for our new fragment */
570 for (q = mp->inbufs; q; last = q, q = q->m_nextpkt) {
571 mp_ReadHeader(mp, q, &h);
572 if (isbefore(mp->local_is12bit, mh.seq, h.seq))
575 /* Our received fragment fits in here */
585 mp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
587 struct physical *p = link2physical(l);
589 if (!bundle->ncp.mp.active)
590 /* Let someone else deal with it ! */
594 log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
597 m_settype(bp, MB_MPIN);
598 mp_Assemble(&bundle->ncp.mp, bp, p);
605 mp_Output(struct mp *mp, struct bundle *bundle, struct link *l,
606 struct mbuf *m, u_int32_t begin, u_int32_t end)
610 /* Stuff an MP header on the front of our packet and send it */
612 if (mp->peer_is12bit) {
615 val = (begin << 15) | (end << 14) | (u_int16_t)mp->out.seq;
616 ua_htons(&val, prepend);
617 m = m_prepend(m, prepend, 2, 0);
621 val = (begin << 31) | (end << 30) | (u_int32_t)mp->out.seq;
622 ua_htonl(&val, prepend);
623 m = m_prepend(m, prepend, 4, 0);
625 if (log_IsKept(LogDEBUG))
626 log_Printf(LogDEBUG, "MP[frag %d]: Send %d bytes on link `%s'\n",
627 mp->out.seq, m_length(m), l->name);
628 mp->out.seq = inc_seq(mp->peer_is12bit, mp->out.seq);
630 link_PushPacket(l, m, bundle, LINK_QUEUES(l) - 1, PROTO_MP);
634 mp_FillQueues(struct bundle *bundle)
636 struct mp *mp = &bundle->ncp.mp;
637 struct datalink *dl, *fdl;
638 size_t total, add, len;
639 int thislink, nlinks;
640 u_int32_t begin, end;
643 thislink = nlinks = 0;
644 for (fdl = NULL, dl = bundle->links; dl; dl = dl->next) {
645 /* Include non-open links here as mp->out.link will stay more correct */
647 if (thislink == mp->out.link)
663 for (dl = fdl; nlinks > 0; dl = dl->next, nlinks--, thislink++) {
669 if (dl->state != DATALINK_OPEN)
672 if (dl->physical->out)
673 /* this link has suffered a short write. Let it continue */
676 add = link_QueueLen(&dl->physical->link);
679 /* this link has got stuff already queued. Let it continue */
682 if (!link_QueueLen(&mp->link) && !ip_PushPacket(&mp->link, bundle))
683 /* Nothing else to send */
686 m = link_Dequeue(&mp->link);
692 if (dl->state == DATALINK_OPEN) {
693 /* Write at most his_mru bytes to the physical link */
694 if (len <= dl->physical->link.lcp.his_mru) {
697 m_settype(mo, MB_MPOUT);
699 /* It's > his_mru, chop the packet (`m') into bits */
700 mo = m_get(dl->physical->link.lcp.his_mru, MB_MPOUT);
702 m = mbuf_Read(m, MBUF_CTOP(mo), mo->m_len);
704 mp_Output(mp, bundle, &dl->physical->link, mo, begin, end);
719 mp->out.link = thislink; /* Start here next time */
725 mp_SetDatalinkBandwidth(struct cmdargs const *arg)
729 if (arg->argc != arg->argn+1)
732 val = atoi(arg->argv[arg->argn]);
734 log_Printf(LogWARN, "The link bandwidth must be greater than zero\n");
737 arg->cx->mp.bandwidth = val;
739 if (arg->cx->state == DATALINK_OPEN)
740 bundle_CalculateBandwidth(arg->bundle);
746 mp_ShowStatus(struct cmdargs const *arg)
748 struct mp *mp = &arg->bundle->ncp.mp;
750 prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in");
756 prompt_Printf(arg->prompt, "Socket: %s\n",
757 mp->server.socket.sun_path);
758 for (m = mp->inbufs; m; m = m->m_nextpkt) {
762 prompt_Printf(arg->prompt, "Pending frags: %d", bufs);
765 unsigned long first, last;
767 first = mp_ReadHeader(mp, mp->inbufs, &mh) ? mh.seq : 0;
768 last = mp_ReadHeader(mp, lm, &mh) ? mh.seq : 0;
769 prompt_Printf(arg->prompt, " (Have %lu - %lu, want %lu, lowest %lu)\n",
770 first, last, (unsigned long)mp->seq.next_in,
771 (unsigned long)mp->seq.min_in);
772 prompt_Printf(arg->prompt, " First has %sbegin bit and "
773 "%send bit", mh.begin ? "" : "no ", mh.end ? "" : "no ");
775 prompt_Printf(arg->prompt, "\n");
778 prompt_Printf(arg->prompt, "\nMy Side:\n");
780 prompt_Printf(arg->prompt, " Output SEQ: %u\n", mp->out.seq);
781 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru);
782 prompt_Printf(arg->prompt, " Short Seq: %s\n",
783 mp->local_is12bit ? "on" : "off");
785 prompt_Printf(arg->prompt, " Discriminator: %s\n",
786 mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address,
787 mp->cfg.enddisc.len));
789 prompt_Printf(arg->prompt, "\nHis Side:\n");
791 prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname);
792 prompt_Printf(arg->prompt, " Input SEQ: %u\n", mp->seq.next_in);
793 prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru);
794 prompt_Printf(arg->prompt, " Short Seq: %s\n",
795 mp->peer_is12bit ? "on" : "off");
797 prompt_Printf(arg->prompt, " Discriminator: %s\n",
798 mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address,
799 mp->peer.enddisc.len));
801 prompt_Printf(arg->prompt, "\nDefaults:\n");
803 prompt_Printf(arg->prompt, " MRRU: ");
805 prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru);
807 prompt_Printf(arg->prompt, "disabled\n");
808 prompt_Printf(arg->prompt, " Short Seq: %s\n",
809 command_ShowNegval(mp->cfg.shortseq));
810 prompt_Printf(arg->prompt, " Discriminator: %s\n",
811 command_ShowNegval(mp->cfg.negenddisc));
812 prompt_Printf(arg->prompt, " AutoLoad: min %d%%, max %d%%,"
813 " period %d secs\n", mp->cfg.autoload.min,
814 mp->cfg.autoload.max, mp->cfg.autoload.period);
820 mp_Enddisc(u_char c, const char *address, int len)
822 static char result[100]; /* Used immediately after it's returned */
827 sprintf(result, "Null Class");
831 snprintf(result, sizeof result, "Local Addr: %.*s", len, address);
836 snprintf(result, sizeof result, "IP %s",
837 inet_ntoa(*(const struct in_addr *)address));
839 sprintf(result, "IP[%d] ???", len);
844 const u_char *m = (const u_char *)address;
845 snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x",
846 m[0], m[1], m[2], m[3], m[4], m[5]);
848 sprintf(result, "MAC[%d] ???", len);
852 sprintf(result, "Magic: 0x");
853 header = strlen(result);
854 if (len > sizeof result - header - 1)
855 len = sizeof result - header - 1;
856 for (f = 0; f < len; f++)
857 sprintf(result + header + 2 * f, "%02x", address[f]);
861 snprintf(result, sizeof result, "PSN: %.*s", len, address);
865 sprintf(result, "%d: ", (int)c);
866 header = strlen(result);
867 if (len > sizeof result - header - 1)
868 len = sizeof result - header - 1;
869 for (f = 0; f < len; f++)
870 sprintf(result + header + 2 * f, "%02x", address[f]);
877 mp_SetEnddisc(struct cmdargs const *arg)
879 struct mp *mp = &arg->bundle->ncp.mp;
882 switch (bundle_Phase(arg->bundle)) {
885 case PHASE_ESTABLISH:
886 /* Make sure none of our links are DATALINK_LCP or greater */
887 if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
888 log_Printf(LogWARN, "enddisc: Only changable before"
889 " LCP negotiations\n");
894 log_Printf(LogWARN, "enddisc: Only changable at phase DEAD/ESTABLISH\n");
898 if (arg->argc == arg->argn) {
899 mp->cfg.enddisc.class = 0;
900 *mp->cfg.enddisc.address = '\0';
901 mp->cfg.enddisc.len = 0;
902 } else if (arg->argc > arg->argn) {
903 if (!strcasecmp(arg->argv[arg->argn], "label")) {
904 mp->cfg.enddisc.class = ENDDISC_LOCAL;
905 strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label);
906 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
907 } else if (!strcasecmp(arg->argv[arg->argn], "ip")) {
908 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
909 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
911 addr = arg->bundle->ncp.ipcp.my_ip;
912 memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr);
913 mp->cfg.enddisc.class = ENDDISC_IP;
914 mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr;
915 } else if (!strcasecmp(arg->argv[arg->argn], "mac")) {
916 struct sockaddr_dl hwaddr;
919 if (arg->bundle->ncp.ipcp.my_ip.s_addr == INADDR_ANY)
920 addr = arg->bundle->ncp.ipcp.cfg.my_range.ipaddr;
922 addr = arg->bundle->ncp.ipcp.my_ip;
924 s = ID0socket(AF_INET, SOCK_DGRAM, 0);
926 log_Printf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno));
929 if (get_ether_addr(s, addr, &hwaddr)) {
930 mp->cfg.enddisc.class = ENDDISC_MAC;
931 memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen,
933 mp->cfg.enddisc.len = hwaddr.sdl_alen;
935 log_Printf(LogWARN, "set enddisc: Can't locate MAC address for %s\n",
941 } else if (!strcasecmp(arg->argv[arg->argn], "magic")) {
945 for (f = 0; f < 20; f += sizeof(long))
946 *(long *)(mp->cfg.enddisc.address + f) = random();
947 mp->cfg.enddisc.class = ENDDISC_MAGIC;
948 mp->cfg.enddisc.len = 20;
949 } else if (!strcasecmp(arg->argv[arg->argn], "psn")) {
950 if (arg->argc > arg->argn+1) {
951 mp->cfg.enddisc.class = ENDDISC_PSN;
952 strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]);
953 mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address);
955 log_Printf(LogWARN, "PSN endpoint requires additional data\n");
959 log_Printf(LogWARN, "%s: Unrecognised endpoint type\n",
960 arg->argv[arg->argn]);
969 mpserver_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e,
972 struct mpserver *s = descriptor2mpserver(d);
976 if (s->send.dl != NULL) {
977 /* We've connect()ed */
978 if (!link_QueueLen(&s->send.dl->physical->link) &&
979 !s->send.dl->physical->out) {
980 /* Only send if we've transmitted all our data (i.e. the ConfigAck) */
981 result -= datalink_RemoveFromSet(s->send.dl, r, w, e);
982 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
986 /* Never read from a datalink that's on death row ! */
987 result -= datalink_RemoveFromSet(s->send.dl, r, NULL, NULL);
988 } else if (r && s->fd >= 0) {
992 log_Printf(LogTIMER, "mp: fdset(r) %d\n", s->fd);
999 mpserver_IsSet(struct fdescriptor *d, const fd_set *fdset)
1001 struct mpserver *s = descriptor2mpserver(d);
1002 return s->fd >= 0 && FD_ISSET(s->fd, fdset);
1006 mpserver_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
1008 struct mpserver *s = descriptor2mpserver(d);
1010 bundle_ReceiveDatalink(bundle, s->fd);
1014 mpserver_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
1016 /* We never want to write here ! */
1017 log_Printf(LogALERT, "mpserver_Write: Internal error: Bad call !\n");
1022 mpserver_Init(struct mpserver *s)
1024 s->desc.type = MPSERVER_DESCRIPTOR;
1025 s->desc.UpdateSet = mpserver_UpdateSet;
1026 s->desc.IsSet = mpserver_IsSet;
1027 s->desc.Read = mpserver_Read;
1028 s->desc.Write = mpserver_Write;
1031 memset(&s->socket, '\0', sizeof s->socket);
1035 mpserver_Open(struct mpserver *s, struct peerid *peer)
1041 log_Printf(LogALERT, "Internal error ! mpserver already open\n");
1045 l = snprintf(s->socket.sun_path, sizeof s->socket.sun_path, "%sppp-%s-%02x-",
1046 _PATH_VARRUN, peer->authname, peer->enddisc.class);
1048 for (f = 0; f < peer->enddisc.len && l < sizeof s->socket.sun_path - 2; f++) {
1049 snprintf(s->socket.sun_path + l, sizeof s->socket.sun_path - l,
1050 "%02x", *(u_char *)(peer->enddisc.address+f));
1054 s->socket.sun_family = AF_LOCAL;
1055 s->socket.sun_len = sizeof s->socket;
1056 s->fd = ID0socket(PF_LOCAL, SOCK_DGRAM, 0);
1058 log_Printf(LogERROR, "mpserver: socket(): %s\n", strerror(errno));
1059 return MPSERVER_FAILED;
1062 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *)&s->socket,
1067 * Try to bind the socket. If we succeed we play server, if we fail
1068 * we connect() and hand the link off.
1071 if (ID0bind_un(s->fd, &s->socket) < 0) {
1072 if (errno != EADDRINUSE) {
1073 log_Printf(LogPHASE, "mpserver: can't create bundle socket %s (%s)\n",
1074 s->socket.sun_path, strerror(errno));
1078 return MPSERVER_FAILED;
1081 /* So we're the sender */
1083 if (ID0connect_un(s->fd, &s->socket) < 0) {
1084 log_Printf(LogPHASE, "mpserver: can't connect to bundle socket %s (%s)\n",
1085 s->socket.sun_path, strerror(errno));
1086 if (errno == ECONNREFUSED)
1087 log_Printf(LogPHASE, " The previous server died badly !\n");
1090 return MPSERVER_FAILED;
1093 /* Donate our link to the other guy */
1094 return MPSERVER_CONNECTED;
1097 return MPSERVER_LISTENING;
1101 mpserver_Close(struct mpserver *s)
1103 if (s->send.dl != NULL) {
1104 bundle_SendDatalink(s->send.dl, s->fd, &s->socket);
1107 } else if (s->fd >= 0) {
1109 if (ID0unlink(s->socket.sun_path) == -1)
1110 log_Printf(LogERROR, "%s: Failed to remove: %s\n", s->socket.sun_path,
1112 memset(&s->socket, '\0', sizeof s->socket);
1118 mp_LinkLost(struct mp *mp, struct datalink *dl)
1120 if (mp->seq.min_in == dl->mp.seq)
1121 /* We've lost the link that's holding everything up ! */
1122 mp_Assemble(mp, NULL, NULL);
1126 mp_DeleteQueue(struct mp *mp)
1128 link_DeleteQueue(&mp->link);