13b0f8d2eSBrian Somers /*- 23b0f8d2eSBrian Somers * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 33b0f8d2eSBrian Somers * All rights reserved. 43b0f8d2eSBrian Somers * 53b0f8d2eSBrian Somers * Redistribution and use in source and binary forms, with or without 63b0f8d2eSBrian Somers * modification, are permitted provided that the following conditions 73b0f8d2eSBrian Somers * are met: 83b0f8d2eSBrian Somers * 1. Redistributions of source code must retain the above copyright 93b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer. 103b0f8d2eSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 113b0f8d2eSBrian Somers * notice, this list of conditions and the following disclaimer in the 123b0f8d2eSBrian Somers * documentation and/or other materials provided with the distribution. 133b0f8d2eSBrian Somers * 143b0f8d2eSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 153b0f8d2eSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 163b0f8d2eSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 173b0f8d2eSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 183b0f8d2eSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 193b0f8d2eSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 203b0f8d2eSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 213b0f8d2eSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 223b0f8d2eSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 233b0f8d2eSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 243b0f8d2eSBrian Somers * SUCH DAMAGE. 253b0f8d2eSBrian Somers * 26643f4904SBrian Somers * $Id: mp.c,v 1.1.2.12 1998/04/24 19:15:45 brian Exp $ 273b0f8d2eSBrian Somers */ 283b0f8d2eSBrian Somers 293b0f8d2eSBrian Somers #include <sys/types.h> 303b0f8d2eSBrian Somers #include <netinet/in.h> 313b0f8d2eSBrian Somers #include <netinet/in_systm.h> 323b0f8d2eSBrian Somers #include <netinet/ip.h> 3349052c95SBrian Somers #include <arpa/inet.h> 3408676022SBrian Somers #include <net/if_dl.h> 3508676022SBrian Somers #include <sys/socket.h> 363b0f8d2eSBrian Somers 3708676022SBrian Somers #include <errno.h> 383b0f8d2eSBrian Somers #include <stdlib.h> 3949052c95SBrian Somers #include <stdio.h> 403b0f8d2eSBrian Somers #include <string.h> 413b0f8d2eSBrian Somers #include <termios.h> 4208676022SBrian Somers #include <unistd.h> 433b0f8d2eSBrian Somers 443b0f8d2eSBrian Somers #include "command.h" 453b0f8d2eSBrian Somers #include "mbuf.h" 463b0f8d2eSBrian Somers #include "log.h" 473b0f8d2eSBrian Somers #include "defs.h" 483b0f8d2eSBrian Somers #include "timer.h" 493b0f8d2eSBrian Somers #include "fsm.h" 503b0f8d2eSBrian Somers #include "iplist.h" 513b0f8d2eSBrian Somers #include "throughput.h" 523b0f8d2eSBrian Somers #include "slcompress.h" 533b0f8d2eSBrian Somers #include "ipcp.h" 543b0f8d2eSBrian Somers #include "auth.h" 553b0f8d2eSBrian Somers #include "lcp.h" 563b0f8d2eSBrian Somers #include "lqr.h" 573b0f8d2eSBrian Somers #include "hdlc.h" 583b0f8d2eSBrian Somers #include "async.h" 593b0f8d2eSBrian Somers #include "ccp.h" 603b0f8d2eSBrian Somers #include "link.h" 613b0f8d2eSBrian Somers #include "descriptor.h" 623b0f8d2eSBrian Somers #include "physical.h" 633b0f8d2eSBrian Somers #include "chat.h" 643b0f8d2eSBrian Somers #include "lcpproto.h" 653b0f8d2eSBrian Somers #include "filter.h" 663b0f8d2eSBrian Somers #include "mp.h" 673b0f8d2eSBrian Somers #include "chap.h" 683b0f8d2eSBrian Somers #include "datalink.h" 693b0f8d2eSBrian Somers #include "bundle.h" 703b0f8d2eSBrian Somers #include "ip.h" 7149052c95SBrian Somers #include "prompt.h" 7208676022SBrian Somers #include "id.h" 7308676022SBrian Somers #include "arp.h" 743b0f8d2eSBrian Somers 75643f4904SBrian Somers void 76643f4904SBrian Somers peerid_Init(struct peerid *peer) 77643f4904SBrian Somers { 78643f4904SBrian Somers peer->enddisc.class = 0; 79643f4904SBrian Somers *peer->enddisc.address = '\0'; 80643f4904SBrian Somers peer->enddisc.len = 0; 81643f4904SBrian Somers *peer->authname = '\0'; 82643f4904SBrian Somers } 83643f4904SBrian Somers 84643f4904SBrian Somers int 85643f4904SBrian Somers peerid_Equal(const struct peerid *p1, const struct peerid *p2) 86643f4904SBrian Somers { 87643f4904SBrian Somers return !strcmp(p1->authname, p2->authname) && 88643f4904SBrian Somers p1->enddisc.class == p2->enddisc.class && 89643f4904SBrian Somers p1->enddisc.len == p2->enddisc.len && 90643f4904SBrian Somers !memcmp(p1->enddisc.address, p2->enddisc.address, p1->enddisc.len); 91643f4904SBrian Somers } 92643f4904SBrian Somers 933b0f8d2eSBrian Somers static u_int32_t 943b0f8d2eSBrian Somers inc_seq(struct mp *mp, u_int32_t seq) 953b0f8d2eSBrian Somers { 963b0f8d2eSBrian Somers seq++; 9749052c95SBrian Somers if (mp->peer_is12bit) { 983b0f8d2eSBrian Somers if (seq & 0xfffff000) 993b0f8d2eSBrian Somers seq = 0; 1003b0f8d2eSBrian Somers } else if (seq & 0xff000000) 1013b0f8d2eSBrian Somers seq = 0; 1023b0f8d2eSBrian Somers return seq; 1033b0f8d2eSBrian Somers } 1043b0f8d2eSBrian Somers 1053b0f8d2eSBrian Somers static int 1063b0f8d2eSBrian Somers mp_ReadHeader(struct mp *mp, struct mbuf *m, struct mp_header *header) 1073b0f8d2eSBrian Somers { 10849052c95SBrian Somers if (mp->local_is12bit) { 109ce828a6eSBrian Somers header->seq = ntohs(*(u_int16_t *)MBUF_CTOP(m)); 1103b0f8d2eSBrian Somers if (header->seq & 0x3000) { 1113b0f8d2eSBrian Somers LogPrintf(LogWARN, "Oops - MP header without required zero bits\n"); 1123b0f8d2eSBrian Somers return 0; 1133b0f8d2eSBrian Somers } 114ce828a6eSBrian Somers header->begin = header->seq & 0x8000 ? 1 : 0; 115ce828a6eSBrian Somers header->end = header->seq & 0x4000 ? 1 : 0; 1163b0f8d2eSBrian Somers header->seq &= 0x0fff; 1173b0f8d2eSBrian Somers return 2; 1183b0f8d2eSBrian Somers } else { 119ce828a6eSBrian Somers header->seq = ntohl(*(u_int32_t *)MBUF_CTOP(m)); 1203b0f8d2eSBrian Somers if (header->seq & 0x3f000000) { 1213b0f8d2eSBrian Somers LogPrintf(LogWARN, "Oops - MP header without required zero bits\n"); 1223b0f8d2eSBrian Somers return 0; 1233b0f8d2eSBrian Somers } 124ce828a6eSBrian Somers header->begin = header->seq & 0x80000000 ? 1 : 0; 125ce828a6eSBrian Somers header->end = header->seq & 0x40000000 ? 1 : 0; 1263b0f8d2eSBrian Somers header->seq &= 0x00ffffff; 1273b0f8d2eSBrian Somers return 4; 1283b0f8d2eSBrian Somers } 1293b0f8d2eSBrian Somers } 1303b0f8d2eSBrian Somers 1313b0f8d2eSBrian Somers static void 1323b0f8d2eSBrian Somers mp_LayerStart(void *v, struct fsm *fp) 1333b0f8d2eSBrian Somers { 13449052c95SBrian Somers /* The given FSM (ccp) is about to start up ! */ 1353b0f8d2eSBrian Somers } 1363b0f8d2eSBrian Somers 1373b0f8d2eSBrian Somers static void 1383b0f8d2eSBrian Somers mp_LayerUp(void *v, struct fsm *fp) 1393b0f8d2eSBrian Somers { 14049052c95SBrian Somers /* The given fsm (ccp) is now up */ 1413b0f8d2eSBrian Somers } 1423b0f8d2eSBrian Somers 1433b0f8d2eSBrian Somers static void 1443b0f8d2eSBrian Somers mp_LayerDown(void *v, struct fsm *fp) 1453b0f8d2eSBrian Somers { 14649052c95SBrian Somers /* The given FSM (ccp) has been told to come down */ 1473b0f8d2eSBrian Somers } 1483b0f8d2eSBrian Somers 1493b0f8d2eSBrian Somers static void 1503b0f8d2eSBrian Somers mp_LayerFinish(void *v, struct fsm *fp) 1513b0f8d2eSBrian Somers { 15249052c95SBrian Somers /* The given fsm (ccp) is now down */ 1533b0f8d2eSBrian Somers } 1543b0f8d2eSBrian Somers 1553b0f8d2eSBrian Somers void 1563b0f8d2eSBrian Somers mp_Init(struct mp *mp, struct bundle *bundle) 1573b0f8d2eSBrian Somers { 15849052c95SBrian Somers mp->peer_is12bit = mp->local_is12bit = 0; 15949052c95SBrian Somers mp->peer_mrru = mp->local_mrru = 0; 160643f4904SBrian Somers 161643f4904SBrian Somers peerid_Init(&mp->peer); 162643f4904SBrian Somers 1633b0f8d2eSBrian Somers mp->seq.out = 0; 1643b0f8d2eSBrian Somers mp->seq.min_in = 0; 1653b0f8d2eSBrian Somers mp->seq.next_in = 0; 1663b0f8d2eSBrian Somers mp->inbufs = NULL; 1673b0f8d2eSBrian Somers mp->bundle = bundle; 1683b0f8d2eSBrian Somers 1693b0f8d2eSBrian Somers mp->link.type = MP_LINK; 1703b0f8d2eSBrian Somers mp->link.name = "mp"; 1713b0f8d2eSBrian Somers mp->link.len = sizeof *mp; 172643f4904SBrian Somers 1733b0f8d2eSBrian Somers throughput_init(&mp->link.throughput); 1743b0f8d2eSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 1753b0f8d2eSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 1763b0f8d2eSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 1773b0f8d2eSBrian Somers 1783b0f8d2eSBrian Somers mp->fsmp.LayerStart = mp_LayerStart; 1793b0f8d2eSBrian Somers mp->fsmp.LayerUp = mp_LayerUp; 1803b0f8d2eSBrian Somers mp->fsmp.LayerDown = mp_LayerDown; 1813b0f8d2eSBrian Somers mp->fsmp.LayerFinish = mp_LayerFinish; 1823b0f8d2eSBrian Somers mp->fsmp.object = mp; 1833b0f8d2eSBrian Somers 18449052c95SBrian Somers mp->cfg.mrru = 0; 18549052c95SBrian Somers mp->cfg.shortseq = NEG_ENABLED|NEG_ACCEPTED; 18649052c95SBrian Somers mp->cfg.enddisc.class = 0; 18749052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 18849052c95SBrian Somers mp->cfg.enddisc.len = 0; 18949052c95SBrian Somers 1903b0f8d2eSBrian Somers lcp_Init(&mp->link.lcp, mp->bundle, &mp->link, NULL); 1913b0f8d2eSBrian Somers ccp_Init(&mp->link.ccp, mp->bundle, &mp->link, &mp->fsmp); 19249052c95SBrian Somers } 19349052c95SBrian Somers 19449052c95SBrian Somers int 195643f4904SBrian Somers mp_Up(struct mp *mp, const struct peerid *peer, u_short local_mrru, 196643f4904SBrian Somers u_short peer_mrru, int local_shortseq, int peer_shortseq) 19749052c95SBrian Somers { 19849052c95SBrian Somers if (mp->active) { 19949052c95SBrian Somers /* We're adding a link - do a last validation on our parameters */ 200643f4904SBrian Somers if (!peerid_Equal(peer, &mp->peer)) { 201643f4904SBrian Somers LogPrintf(LogPHASE, "Inappropriate peer !\n"); 202643f4904SBrian Somers return 0; 203643f4904SBrian Somers } 20449052c95SBrian Somers if (mp->local_mrru != local_mrru || 20549052c95SBrian Somers mp->peer_mrru != peer_mrru || 20649052c95SBrian Somers mp->local_is12bit != local_shortseq || 20749052c95SBrian Somers mp->peer_is12bit != peer_shortseq) { 20849052c95SBrian Somers LogPrintf(LogPHASE, "Invalid MRRU/SHORTSEQ MP parameters !\n"); 20949052c95SBrian Somers return 0; 21049052c95SBrian Somers } 21149052c95SBrian Somers } else { 21249052c95SBrian Somers /* First link in multilink mode */ 21349052c95SBrian Somers 21449052c95SBrian Somers mp->local_mrru = local_mrru; 21549052c95SBrian Somers mp->peer_mrru = peer_mrru; 21649052c95SBrian Somers mp->local_is12bit = local_shortseq; 21749052c95SBrian Somers mp->peer_is12bit = peer_shortseq; 218643f4904SBrian Somers mp->peer = *peer; 21949052c95SBrian Somers 220673903ecSBrian Somers throughput_init(&mp->link.throughput); 221673903ecSBrian Somers memset(mp->link.Queue, '\0', sizeof mp->link.Queue); 222673903ecSBrian Somers memset(mp->link.proto_in, '\0', sizeof mp->link.proto_in); 223673903ecSBrian Somers memset(mp->link.proto_out, '\0', sizeof mp->link.proto_out); 224673903ecSBrian Somers 225673903ecSBrian Somers mp->seq.out = 0; 226673903ecSBrian Somers mp->seq.min_in = 0; 227673903ecSBrian Somers mp->seq.next_in = 0; 228673903ecSBrian Somers 22949052c95SBrian Somers /* Re-point our IPCP layer at our MP link */ 230ce828a6eSBrian Somers ipcp_SetLink(&mp->bundle->ncp.ipcp, &mp->link); 2313b0f8d2eSBrian Somers 2323b0f8d2eSBrian Somers /* Our lcp's already up 'cos of the NULL parent */ 2333b0f8d2eSBrian Somers FsmUp(&mp->link.ccp.fsm); 2343b0f8d2eSBrian Somers FsmOpen(&mp->link.ccp.fsm); 2353b0f8d2eSBrian Somers 2363b0f8d2eSBrian Somers mp->active = 1; 23749052c95SBrian Somers } 2383b0f8d2eSBrian Somers 23949052c95SBrian Somers return 1; 2403b0f8d2eSBrian Somers } 2413b0f8d2eSBrian Somers 2423b0f8d2eSBrian Somers void 243673903ecSBrian Somers mp_Down(struct mp *mp) 244673903ecSBrian Somers { 245673903ecSBrian Somers if (mp->active) { 246673903ecSBrian Somers struct mbuf *next; 247673903ecSBrian Somers 248673903ecSBrian Somers /* CCP goes down with a bank */ 249673903ecSBrian Somers FsmDown(&mp->link.ccp.fsm); 250673903ecSBrian Somers FsmClose(&mp->link.ccp.fsm); 251673903ecSBrian Somers 252673903ecSBrian Somers /* Received fragments go in the bit-bucket */ 253673903ecSBrian Somers while (mp->inbufs) { 254673903ecSBrian Somers next = mp->inbufs->pnext; 255673903ecSBrian Somers pfree(mp->inbufs); 256673903ecSBrian Somers mp->inbufs = next; 257673903ecSBrian Somers } 258673903ecSBrian Somers 259643f4904SBrian Somers peerid_Init(&mp->peer); 260673903ecSBrian Somers mp->active = 0; 261673903ecSBrian Somers } 262673903ecSBrian Somers } 263673903ecSBrian Somers 264673903ecSBrian Somers void 2653b0f8d2eSBrian Somers mp_linkInit(struct mp_link *mplink) 2663b0f8d2eSBrian Somers { 2673b0f8d2eSBrian Somers mplink->seq = 0; 2683b0f8d2eSBrian Somers mplink->weight = 1500; 2693b0f8d2eSBrian Somers } 2703b0f8d2eSBrian Somers 2713b0f8d2eSBrian Somers void 2723b0f8d2eSBrian Somers mp_Input(struct mp *mp, struct mbuf *m, struct physical *p) 2733b0f8d2eSBrian Somers { 2743b0f8d2eSBrian Somers struct mp_header mh, h; 2753b0f8d2eSBrian Somers struct mbuf *q, *last; 2763b0f8d2eSBrian Somers int32_t seq; 2773b0f8d2eSBrian Somers 278cdbbb6b5SBrian Somers if (mp_ReadHeader(mp, m, &mh) == 0) { 2793b0f8d2eSBrian Somers pfree(m); 2803b0f8d2eSBrian Somers return; 2813b0f8d2eSBrian Somers } 2823b0f8d2eSBrian Somers 2833b0f8d2eSBrian Somers seq = p->dl->mp.seq; 2843b0f8d2eSBrian Somers p->dl->mp.seq = mh.seq; 2853b0f8d2eSBrian Somers if (mp->seq.min_in == seq) { 2863b0f8d2eSBrian Somers /* 2873b0f8d2eSBrian Somers * We've received new data on the link that has our min (oldest) seq. 2883b0f8d2eSBrian Somers * Figure out which link now has the smallest (oldest) seq. 2893b0f8d2eSBrian Somers */ 2903b0f8d2eSBrian Somers struct datalink *dl; 2913b0f8d2eSBrian Somers 2923b0f8d2eSBrian Somers mp->seq.min_in = p->dl->mp.seq; 2933b0f8d2eSBrian Somers for (dl = mp->bundle->links; dl; dl = dl->next) 2943b0f8d2eSBrian Somers if (mp->seq.min_in > dl->mp.seq) 2953b0f8d2eSBrian Somers mp->seq.min_in = dl->mp.seq; 2963b0f8d2eSBrian Somers } 2973b0f8d2eSBrian Somers 2983b0f8d2eSBrian Somers /* 2993b0f8d2eSBrian Somers * Now process as many of our fragments as we can, adding our new 3003b0f8d2eSBrian Somers * fragment in as we go, and ordering with the oldest at the top of 3013b0f8d2eSBrian Somers * the queue. 3023b0f8d2eSBrian Somers */ 3033b0f8d2eSBrian Somers 3043b0f8d2eSBrian Somers if (!mp->inbufs) { 3053b0f8d2eSBrian Somers mp->inbufs = m; 3063b0f8d2eSBrian Somers m = NULL; 3073b0f8d2eSBrian Somers } 3083b0f8d2eSBrian Somers 3093b0f8d2eSBrian Somers last = NULL; 3103b0f8d2eSBrian Somers seq = mp->seq.next_in; 3113b0f8d2eSBrian Somers q = mp->inbufs; 3123b0f8d2eSBrian Somers while (q) { 3133b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 3143b0f8d2eSBrian Somers if (m && h.seq > mh.seq) { 3153b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 3163b0f8d2eSBrian Somers if (last) 3173b0f8d2eSBrian Somers last->pnext = m; 3183b0f8d2eSBrian Somers else 3193b0f8d2eSBrian Somers mp->inbufs = m; 3203b0f8d2eSBrian Somers m->pnext = q; 3213b0f8d2eSBrian Somers q = m; 3223b0f8d2eSBrian Somers h = mh; 3233b0f8d2eSBrian Somers m = NULL; 3243b0f8d2eSBrian Somers } 3253b0f8d2eSBrian Somers 3263b0f8d2eSBrian Somers if (h.seq != seq) { 3273b0f8d2eSBrian Somers /* we're missing something :-( */ 3283b0f8d2eSBrian Somers if (mp->seq.min_in > seq) { 3293b0f8d2eSBrian Somers /* we're never gonna get it */ 3303b0f8d2eSBrian Somers struct mbuf *next; 3313b0f8d2eSBrian Somers 3323b0f8d2eSBrian Somers /* Zap all older fragments */ 3333b0f8d2eSBrian Somers while (mp->inbufs != q) { 334ce828a6eSBrian Somers LogPrintf(LogDEBUG, "Drop frag\n"); 3353b0f8d2eSBrian Somers next = mp->inbufs->pnext; 3363b0f8d2eSBrian Somers pfree(mp->inbufs); 3373b0f8d2eSBrian Somers mp->inbufs = next; 3383b0f8d2eSBrian Somers } 3393b0f8d2eSBrian Somers 3403b0f8d2eSBrian Somers /* 3413b0f8d2eSBrian Somers * Zap everything until the next `end' fragment OR just before 3423b0f8d2eSBrian Somers * the next `begin' fragment OR 'till seq.min_in - whichever 3433b0f8d2eSBrian Somers * comes first. 3443b0f8d2eSBrian Somers */ 3453b0f8d2eSBrian Somers do { 3463b0f8d2eSBrian Somers mp_ReadHeader(mp, mp->inbufs, &h); 3473b0f8d2eSBrian Somers if (h.begin) { 348ce828a6eSBrian Somers /* We might be able to process this ! */ 3493b0f8d2eSBrian Somers h.seq--; /* We're gonna look for fragment with h.seq+1 */ 3503b0f8d2eSBrian Somers break; 3513b0f8d2eSBrian Somers } 3523b0f8d2eSBrian Somers next = mp->inbufs->pnext; 353ce828a6eSBrian Somers LogPrintf(LogDEBUG, "Drop frag %u\n", h.seq); 3543b0f8d2eSBrian Somers pfree(mp->inbufs); 3553b0f8d2eSBrian Somers mp->inbufs = next; 356ce828a6eSBrian Somers } while (mp->inbufs && (h.seq >= mp->seq.min_in || h.end)); 3573b0f8d2eSBrian Somers 3583b0f8d2eSBrian Somers /* 3593b0f8d2eSBrian Somers * Continue processing things from here. 3603b0f8d2eSBrian Somers * This deals with the possibility that we received a fragment 3613b0f8d2eSBrian Somers * on the slowest link that invalidates some of our data (because 3623b0f8d2eSBrian Somers * of the hole at `q'), but where there are subsequent `whole' 3633b0f8d2eSBrian Somers * packets that have already been received. 3643b0f8d2eSBrian Somers */ 3653b0f8d2eSBrian Somers 3663b0f8d2eSBrian Somers mp->seq.next_in = seq = h.seq + 1; 3673b0f8d2eSBrian Somers last = NULL; 3683b0f8d2eSBrian Somers q = mp->inbufs; 3693b0f8d2eSBrian Somers } else 3703b0f8d2eSBrian Somers /* we may still receive the missing fragment */ 3713b0f8d2eSBrian Somers break; 3723b0f8d2eSBrian Somers } else if (h.end) { 3733b0f8d2eSBrian Somers /* We've got something, reassemble */ 3743b0f8d2eSBrian Somers struct mbuf **frag = &q; 3753b0f8d2eSBrian Somers int len; 376ce828a6eSBrian Somers u_long first = -1; 3773b0f8d2eSBrian Somers 3783b0f8d2eSBrian Somers do { 3793b0f8d2eSBrian Somers *frag = mp->inbufs; 3803b0f8d2eSBrian Somers mp->inbufs = mp->inbufs->pnext; 3813b0f8d2eSBrian Somers len = mp_ReadHeader(mp, *frag, &h); 382ce828a6eSBrian Somers if (first == -1) 383ce828a6eSBrian Somers first = h.seq; 3843b0f8d2eSBrian Somers (*frag)->offset += len; 3853b0f8d2eSBrian Somers (*frag)->cnt -= len; 3863b0f8d2eSBrian Somers (*frag)->pnext = NULL; 3873b0f8d2eSBrian Somers if (frag == &q && !h.begin) { 3883b0f8d2eSBrian Somers LogPrintf(LogWARN, "Oops - MP frag %lu should have a begin flag\n", 3893b0f8d2eSBrian Somers (u_long)h.seq); 3903b0f8d2eSBrian Somers pfree(q); 3913b0f8d2eSBrian Somers q = NULL; 3923b0f8d2eSBrian Somers } else if (frag != &q && h.begin) { 3933b0f8d2eSBrian Somers LogPrintf(LogWARN, "Oops - MP frag %lu should have an end flag\n", 3943b0f8d2eSBrian Somers (u_long)h.seq - 1); 3953b0f8d2eSBrian Somers /* 3963b0f8d2eSBrian Somers * Stuff our fragment back at the front of the queue and zap 3973b0f8d2eSBrian Somers * our half-assembed packet. 3983b0f8d2eSBrian Somers */ 3993b0f8d2eSBrian Somers (*frag)->pnext = mp->inbufs; 4003b0f8d2eSBrian Somers mp->inbufs = *frag; 4013b0f8d2eSBrian Somers *frag = NULL; 4023b0f8d2eSBrian Somers pfree(q); 4033b0f8d2eSBrian Somers q = NULL; 4043b0f8d2eSBrian Somers frag = &q; 4053b0f8d2eSBrian Somers h.end = 0; /* just in case it's a whole packet */ 4063b0f8d2eSBrian Somers } else 4073b0f8d2eSBrian Somers do 4083b0f8d2eSBrian Somers frag = &(*frag)->next; 409ce828a6eSBrian Somers while (*frag != NULL); 4103b0f8d2eSBrian Somers } while (!h.end); 4113b0f8d2eSBrian Somers 4123b0f8d2eSBrian Somers if (q) { 413673903ecSBrian Somers u_short proto; 414673903ecSBrian Somers u_char ch; 415673903ecSBrian Somers 4163b0f8d2eSBrian Somers q = mbread(q, &ch, 1); 417673903ecSBrian Somers proto = ch; 418673903ecSBrian Somers if (!(proto & 1)) { 419673903ecSBrian Somers q = mbread(q, &ch, 1); 420673903ecSBrian Somers proto <<= 8; 4213b0f8d2eSBrian Somers proto += ch; 422673903ecSBrian Somers } 423673903ecSBrian Somers if (LogIsKept(LogDEBUG)) 424673903ecSBrian Somers LogPrintf(LogDEBUG, "MP: Reassembled frags %ld-%lu, length %d\n", 425673903ecSBrian Somers first, (u_long)h.seq, plength(q)); 4263b0f8d2eSBrian Somers hdlc_DecodePacket(mp->bundle, proto, q, &mp->link); 4273b0f8d2eSBrian Somers } 4283b0f8d2eSBrian Somers 4293b0f8d2eSBrian Somers mp->seq.next_in = seq = h.seq + 1; 4303b0f8d2eSBrian Somers last = NULL; 4313b0f8d2eSBrian Somers q = mp->inbufs; 4323b0f8d2eSBrian Somers } else { 4333b0f8d2eSBrian Somers /* Look for the next fragment */ 4343b0f8d2eSBrian Somers seq++; 4353b0f8d2eSBrian Somers last = q; 4363b0f8d2eSBrian Somers q = q->pnext; 4373b0f8d2eSBrian Somers } 4383b0f8d2eSBrian Somers } 4393b0f8d2eSBrian Somers 4403b0f8d2eSBrian Somers if (m) { 4413b0f8d2eSBrian Somers /* We still have to find a home for our new fragment */ 4423b0f8d2eSBrian Somers last = NULL; 4433b0f8d2eSBrian Somers for (q = mp->inbufs; q; last = q, q = q->pnext) { 4443b0f8d2eSBrian Somers mp_ReadHeader(mp, q, &h); 4453b0f8d2eSBrian Somers if (h.seq > mh.seq) { 4463b0f8d2eSBrian Somers /* Our received fragment fits in before this one, so link it in */ 4473b0f8d2eSBrian Somers if (last) 4483b0f8d2eSBrian Somers last->pnext = m; 4493b0f8d2eSBrian Somers else 4503b0f8d2eSBrian Somers mp->inbufs = m; 4513b0f8d2eSBrian Somers m->pnext = q; 4523b0f8d2eSBrian Somers break; 4533b0f8d2eSBrian Somers } 4543b0f8d2eSBrian Somers } 4553b0f8d2eSBrian Somers } 4563b0f8d2eSBrian Somers } 4573b0f8d2eSBrian Somers 4583b0f8d2eSBrian Somers static void 4593b0f8d2eSBrian Somers mp_Output(struct mp *mp, struct link *l, struct mbuf *m, int begin, int end) 4603b0f8d2eSBrian Somers { 4613b0f8d2eSBrian Somers struct mbuf *mo; 4623b0f8d2eSBrian Somers 463ce828a6eSBrian Somers /* Stuff an MP header on the front of our packet and send it */ 4643b0f8d2eSBrian Somers mo = mballoc(4, MB_MP); 4653b0f8d2eSBrian Somers mo->next = m; 46649052c95SBrian Somers if (mp->peer_is12bit) { 467ce828a6eSBrian Somers u_int16_t *seq16; 468ce828a6eSBrian Somers 469ce828a6eSBrian Somers seq16 = (u_int16_t *)MBUF_CTOP(mo); 470ce828a6eSBrian Somers *seq16 = htons((begin << 15) | (end << 14) | (u_int16_t)mp->seq.out); 4713b0f8d2eSBrian Somers mo->cnt = 2; 4723b0f8d2eSBrian Somers } else { 473ce828a6eSBrian Somers u_int32_t *seq32; 474ce828a6eSBrian Somers 475ce828a6eSBrian Somers seq32 = (u_int32_t *)MBUF_CTOP(mo); 476ce828a6eSBrian Somers *seq32 = htonl((begin << 31) | (end << 30) | (u_int32_t)mp->seq.out); 4773b0f8d2eSBrian Somers mo->cnt = 4; 4783b0f8d2eSBrian Somers } 479673903ecSBrian Somers if (LogIsKept(LogDEBUG)) 480673903ecSBrian Somers LogPrintf(LogDEBUG, "MP[frag %d]: Send %d bytes on %s\n", 481ce828a6eSBrian Somers mp->seq.out, plength(mo), l->name); 4823b0f8d2eSBrian Somers mp->seq.out = inc_seq(mp, mp->seq.out); 4833b0f8d2eSBrian Somers 4843b0f8d2eSBrian Somers HdlcOutput(l, PRI_NORMAL, PROTO_MP, mo); 4853b0f8d2eSBrian Somers } 4863b0f8d2eSBrian Somers 4873b0f8d2eSBrian Somers int 4883b0f8d2eSBrian Somers mp_FillQueues(struct bundle *bundle) 4893b0f8d2eSBrian Somers { 4903b0f8d2eSBrian Somers struct mp *mp = &bundle->ncp.mp; 4913b0f8d2eSBrian Somers struct datalink *dl; 4923b0f8d2eSBrian Somers int total, add, len, begin, end, looped; 4933b0f8d2eSBrian Somers struct mbuf *m, *mo; 4943b0f8d2eSBrian Somers 4953b0f8d2eSBrian Somers /* 4963b0f8d2eSBrian Somers * XXX: This routine is fairly simplistic. It should re-order the 4973b0f8d2eSBrian Somers * links based on the amount of data less than the links weight 4983b0f8d2eSBrian Somers * that was queued. That way we'd ``prefer'' the least used 4993b0f8d2eSBrian Somers * links the next time 'round. 5003b0f8d2eSBrian Somers */ 5013b0f8d2eSBrian Somers 5023b0f8d2eSBrian Somers total = 0; 5033b0f8d2eSBrian Somers for (dl = bundle->links; dl; dl = dl->next) { 5043b0f8d2eSBrian Somers if (dl->physical->out) 5053b0f8d2eSBrian Somers /* this link has suffered a short write. Let it continue */ 5063b0f8d2eSBrian Somers continue; 5073b0f8d2eSBrian Somers add = link_QueueLen(&dl->physical->link); 5083b0f8d2eSBrian Somers total += add; 5093b0f8d2eSBrian Somers if (add) 5103b0f8d2eSBrian Somers /* this link has got stuff already queued. Let it continue */ 5113b0f8d2eSBrian Somers continue; 5123b0f8d2eSBrian Somers if (!link_QueueLen(&mp->link) && !IpFlushPacket(&mp->link, bundle)) 5133b0f8d2eSBrian Somers /* Nothing else to send */ 5143b0f8d2eSBrian Somers break; 5153b0f8d2eSBrian Somers 5163b0f8d2eSBrian Somers m = link_Dequeue(&mp->link); 5173b0f8d2eSBrian Somers len = plength(m); 5183b0f8d2eSBrian Somers add += len; 5193b0f8d2eSBrian Somers begin = 1; 5203b0f8d2eSBrian Somers end = 0; 5213b0f8d2eSBrian Somers looped = 0; 5223b0f8d2eSBrian Somers 5233b0f8d2eSBrian Somers for (; !end; dl = dl->next) { 5243b0f8d2eSBrian Somers if (dl == NULL) { 5253b0f8d2eSBrian Somers /* Keep going 'till we get rid of the whole of `m' */ 5263b0f8d2eSBrian Somers looped = 1; 5273b0f8d2eSBrian Somers dl = bundle->links; 5283b0f8d2eSBrian Somers } 5293b0f8d2eSBrian Somers if (len <= dl->mp.weight + LINK_MINWEIGHT) { 5303b0f8d2eSBrian Somers mo = m; 5313b0f8d2eSBrian Somers end = 1; 5323b0f8d2eSBrian Somers } else { 5333b0f8d2eSBrian Somers mo = mballoc(dl->mp.weight, MB_MP); 5343b0f8d2eSBrian Somers mo->cnt = dl->mp.weight; 5353b0f8d2eSBrian Somers len -= mo->cnt; 5363b0f8d2eSBrian Somers m = mbread(m, MBUF_CTOP(mo), mo->cnt); 5373b0f8d2eSBrian Somers } 5383b0f8d2eSBrian Somers mp_Output(mp, &dl->physical->link, mo, begin, end); 5393b0f8d2eSBrian Somers begin = 0; 5403b0f8d2eSBrian Somers } 541ce828a6eSBrian Somers if (!dl || looped) 5423b0f8d2eSBrian Somers break; 5433b0f8d2eSBrian Somers } 5443b0f8d2eSBrian Somers 5453b0f8d2eSBrian Somers return total; 5463b0f8d2eSBrian Somers } 5473b0f8d2eSBrian Somers 5483b0f8d2eSBrian Somers int 5493b0f8d2eSBrian Somers mp_SetDatalinkWeight(struct cmdargs const *arg) 5503b0f8d2eSBrian Somers { 5513b0f8d2eSBrian Somers int val; 5523b0f8d2eSBrian Somers 55325092092SBrian Somers if (arg->argc != arg->argn+1) 5543b0f8d2eSBrian Somers return -1; 5553b0f8d2eSBrian Somers 55625092092SBrian Somers val = atoi(arg->argv[arg->argn]); 5573b0f8d2eSBrian Somers if (val < LINK_MINWEIGHT) { 5583b0f8d2eSBrian Somers LogPrintf(LogWARN, "Link weights must not be less than %d\n", 5593b0f8d2eSBrian Somers LINK_MINWEIGHT); 5603b0f8d2eSBrian Somers return 1; 5613b0f8d2eSBrian Somers } 5623b0f8d2eSBrian Somers arg->cx->mp.weight = val; 5633b0f8d2eSBrian Somers return 0; 5643b0f8d2eSBrian Somers } 56549052c95SBrian Somers 56649052c95SBrian Somers int 56749052c95SBrian Somers mp_ShowStatus(struct cmdargs const *arg) 56849052c95SBrian Somers { 56949052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 57049052c95SBrian Somers 57149052c95SBrian Somers prompt_Printf(arg->prompt, "Multilink is %sactive\n", mp->active ? "" : "in"); 57249052c95SBrian Somers 57349052c95SBrian Somers prompt_Printf(arg->prompt, "\nMy Side:\n"); 57449052c95SBrian Somers if (mp->active) { 57549052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->local_mrru); 57649052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 57749052c95SBrian Somers mp->local_is12bit ? "on" : "off"); 57849052c95SBrian Somers } 579643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 58049052c95SBrian Somers mp_Enddisc(mp->cfg.enddisc.class, mp->cfg.enddisc.address, 58149052c95SBrian Somers mp->cfg.enddisc.len)); 58249052c95SBrian Somers 58349052c95SBrian Somers prompt_Printf(arg->prompt, "\nHis Side:\n"); 58449052c95SBrian Somers if (mp->active) { 585643f4904SBrian Somers prompt_Printf(arg->prompt, " Auth Name: %s\n", mp->peer.authname); 58649052c95SBrian Somers prompt_Printf(arg->prompt, " Next SEQ: %u\n", mp->seq.out); 58749052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: %u\n", mp->peer_mrru); 58849052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 58949052c95SBrian Somers mp->peer_is12bit ? "on" : "off"); 59049052c95SBrian Somers } 591643f4904SBrian Somers prompt_Printf(arg->prompt, " Discriminator: %s\n", 592643f4904SBrian Somers mp_Enddisc(mp->peer.enddisc.class, mp->peer.enddisc.address, 593643f4904SBrian Somers mp->peer.enddisc.len)); 59449052c95SBrian Somers 59549052c95SBrian Somers prompt_Printf(arg->prompt, "\nDefaults:\n"); 59649052c95SBrian Somers 59749052c95SBrian Somers prompt_Printf(arg->prompt, " MRRU: "); 59849052c95SBrian Somers if (mp->cfg.mrru) 59949052c95SBrian Somers prompt_Printf(arg->prompt, "%d (multilink enabled)\n", mp->cfg.mrru); 60049052c95SBrian Somers else 60149052c95SBrian Somers prompt_Printf(arg->prompt, "disabled\n"); 60249052c95SBrian Somers prompt_Printf(arg->prompt, " Short Seq: %s\n", 60349052c95SBrian Somers command_ShowNegval(mp->cfg.shortseq)); 60449052c95SBrian Somers 60549052c95SBrian Somers return 0; 60649052c95SBrian Somers } 60749052c95SBrian Somers 60849052c95SBrian Somers const char * 60949052c95SBrian Somers mp_Enddisc(u_char c, const char *address, int len) 61049052c95SBrian Somers { 61149052c95SBrian Somers static char result[100]; 61249052c95SBrian Somers int f, header; 61349052c95SBrian Somers 61449052c95SBrian Somers switch (c) { 61508676022SBrian Somers case ENDDISC_NULL: 61649052c95SBrian Somers sprintf(result, "Null Class"); 61749052c95SBrian Somers break; 61849052c95SBrian Somers 61908676022SBrian Somers case ENDDISC_LOCAL: 62049052c95SBrian Somers snprintf(result, sizeof result, "Local Addr: %.*s", len, address); 62149052c95SBrian Somers break; 62249052c95SBrian Somers 62308676022SBrian Somers case ENDDISC_IP: 62449052c95SBrian Somers if (len == 4) 62549052c95SBrian Somers snprintf(result, sizeof result, "IP %s", 62649052c95SBrian Somers inet_ntoa(*(const struct in_addr *)address)); 62749052c95SBrian Somers else 62849052c95SBrian Somers sprintf(result, "IP[%d] ???", len); 62949052c95SBrian Somers break; 63049052c95SBrian Somers 63108676022SBrian Somers case ENDDISC_MAC: 63249052c95SBrian Somers if (len == 6) { 63349052c95SBrian Somers const u_char *m = (const u_char *)address; 63449052c95SBrian Somers snprintf(result, sizeof result, "MAC %02x:%02x:%02x:%02x:%02x:%02x", 63549052c95SBrian Somers m[0], m[1], m[2], m[3], m[4], m[5]); 63649052c95SBrian Somers } else 63749052c95SBrian Somers sprintf(result, "MAC[%d] ???", len); 63849052c95SBrian Somers break; 63949052c95SBrian Somers 64008676022SBrian Somers case ENDDISC_MAGIC: 64149052c95SBrian Somers sprintf(result, "Magic: 0x"); 64249052c95SBrian Somers header = strlen(result); 64349052c95SBrian Somers if (len > sizeof result - header - 1) 64449052c95SBrian Somers len = sizeof result - header - 1; 64549052c95SBrian Somers for (f = 0; f < len; f++) 64649052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 64749052c95SBrian Somers break; 64849052c95SBrian Somers 64908676022SBrian Somers case ENDDISC_PSN: 65049052c95SBrian Somers snprintf(result, sizeof result, "PSN: %.*s", len, address); 65149052c95SBrian Somers break; 65249052c95SBrian Somers 65349052c95SBrian Somers default: 65449052c95SBrian Somers sprintf(result, "%d: ", (int)c); 65549052c95SBrian Somers header = strlen(result); 65649052c95SBrian Somers if (len > sizeof result - header - 1) 65749052c95SBrian Somers len = sizeof result - header - 1; 65849052c95SBrian Somers for (f = 0; f < len; f++) 65949052c95SBrian Somers sprintf(result + header + 2 * f, "%02x", address[f]); 66049052c95SBrian Somers break; 66149052c95SBrian Somers } 66249052c95SBrian Somers return result; 66349052c95SBrian Somers } 66449052c95SBrian Somers 66549052c95SBrian Somers int 66649052c95SBrian Somers mp_SetEnddisc(struct cmdargs const *arg) 66749052c95SBrian Somers { 66849052c95SBrian Somers struct mp *mp = &arg->bundle->ncp.mp; 669d47dceb8SBrian Somers struct in_addr addr; 67049052c95SBrian Somers 67149052c95SBrian Somers if (bundle_Phase(arg->bundle) != PHASE_DEAD) { 67249052c95SBrian Somers LogPrintf(LogWARN, "set enddisc: Only available at phase DEAD\n"); 67349052c95SBrian Somers return 1; 67449052c95SBrian Somers } 67549052c95SBrian Somers 67649052c95SBrian Somers if (arg->argc == arg->argn) { 67749052c95SBrian Somers mp->cfg.enddisc.class = 0; 67849052c95SBrian Somers *mp->cfg.enddisc.address = '\0'; 67949052c95SBrian Somers mp->cfg.enddisc.len = 0; 68049052c95SBrian Somers } else if (arg->argc > arg->argn) 68108676022SBrian Somers if (!strcasecmp(arg->argv[arg->argn], "label")) { 68208676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_LOCAL; 68308676022SBrian Somers strcpy(mp->cfg.enddisc.address, arg->bundle->cfg.label); 68408676022SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 68508676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "ip")) { 686d47dceb8SBrian Somers if (arg->bundle->ncp.ipcp.my_ifip.s_addr == INADDR_ANY) 687d47dceb8SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 688d47dceb8SBrian Somers else 689d47dceb8SBrian Somers addr = arg->bundle->ncp.ipcp.my_ifip; 690d47dceb8SBrian Somers memcpy(mp->cfg.enddisc.address, &addr.s_addr, sizeof addr.s_addr); 69108676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_IP; 69249052c95SBrian Somers mp->cfg.enddisc.len = sizeof arg->bundle->ncp.ipcp.my_ip.s_addr; 69308676022SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "mac")) { 69408676022SBrian Somers struct sockaddr_dl hwaddr; 69508676022SBrian Somers int s; 69608676022SBrian Somers 697d47dceb8SBrian Somers if (arg->bundle->ncp.ipcp.my_ifip.s_addr == INADDR_ANY) 698d47dceb8SBrian Somers addr = arg->bundle->ncp.ipcp.my_ip; 699d47dceb8SBrian Somers else 700d47dceb8SBrian Somers addr = arg->bundle->ncp.ipcp.my_ifip; 701d47dceb8SBrian Somers 70208676022SBrian Somers s = ID0socket(AF_INET, SOCK_DGRAM, 0); 70308676022SBrian Somers if (s < 0) { 70408676022SBrian Somers LogPrintf(LogERROR, "set enddisc: socket(): %s\n", strerror(errno)); 70508676022SBrian Somers return 2; 70608676022SBrian Somers } 707d47dceb8SBrian Somers if (get_ether_addr(s, addr, &hwaddr)) { 70808676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAC; 70908676022SBrian Somers memcpy(mp->cfg.enddisc.address, hwaddr.sdl_data + hwaddr.sdl_nlen, 71008676022SBrian Somers hwaddr.sdl_alen); 71108676022SBrian Somers mp->cfg.enddisc.len = hwaddr.sdl_alen; 71208676022SBrian Somers } else { 71308676022SBrian Somers LogPrintf(LogWARN, "set enddisc: Can't locate MAC address for %s\n", 71408676022SBrian Somers inet_ntoa(arg->bundle->ncp.ipcp.cfg.my_range.ipaddr)); 71508676022SBrian Somers close(s); 71608676022SBrian Somers return 4; 71708676022SBrian Somers } 71808676022SBrian Somers close(s); 71949052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "magic")) { 72049052c95SBrian Somers int f; 72149052c95SBrian Somers 72249052c95SBrian Somers randinit(); 72349052c95SBrian Somers for (f = 0; f < 20; f += sizeof(long)) 72449052c95SBrian Somers *(long *)(mp->cfg.enddisc.address + f) = random(); 72508676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_MAGIC; 72649052c95SBrian Somers mp->cfg.enddisc.len = 20; 72749052c95SBrian Somers } else if (!strcasecmp(arg->argv[arg->argn], "psn")) { 72849052c95SBrian Somers if (arg->argc > arg->argn+1) { 72908676022SBrian Somers mp->cfg.enddisc.class = ENDDISC_PSN; 73049052c95SBrian Somers strcpy(mp->cfg.enddisc.address, arg->argv[arg->argn+1]); 73149052c95SBrian Somers mp->cfg.enddisc.len = strlen(mp->cfg.enddisc.address); 73249052c95SBrian Somers } else { 73349052c95SBrian Somers LogPrintf(LogWARN, "PSN endpoint requires additional data\n"); 73408676022SBrian Somers return 5; 73549052c95SBrian Somers } 73649052c95SBrian Somers } else { 73749052c95SBrian Somers LogPrintf(LogWARN, "%s: Unrecognised endpoint type\n", 73849052c95SBrian Somers arg->argv[arg->argn]); 73908676022SBrian Somers return 6; 74049052c95SBrian Somers } 74149052c95SBrian Somers 74249052c95SBrian Somers return 0; 74349052c95SBrian Somers } 744