2 * Copyright (c) 2001 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_systm.h>
31 #include <netinet/in.h>
32 #include <netinet/ip.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
36 #include <net/route.h>
59 #include "throughput.h"
60 #include "slcompress.h"
68 #include "descriptor.h"
95 static u_short default_urgent_tcp_ports[] = {
105 static u_short default_urgent_udp_ports[] = { };
107 #define NDEFTCPPORTS \
108 (sizeof default_urgent_tcp_ports / sizeof default_urgent_tcp_ports[0])
109 #define NDEFUDPPORTS \
110 (sizeof default_urgent_udp_ports / sizeof default_urgent_udp_ports[0])
113 ncp_Init(struct ncp *ncp, struct bundle *bundle)
118 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
119 ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
120 memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
121 NDEFTCPPORTS * sizeof(u_short));
122 ncp->cfg.urgent.tos = 1;
124 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = NDEFUDPPORTS;
125 ncp->cfg.urgent.udp.port = (u_short *)malloc(NDEFUDPPORTS * sizeof(u_short));
126 memcpy(ncp->cfg.urgent.udp.port, default_urgent_udp_ports,
127 NDEFUDPPORTS * sizeof(u_short));
130 mp_Init(&ncp->mp, bundle);
132 /* Send over the first physical link by default */
133 ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
136 ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
142 ncp_Destroy(struct ncp *ncp)
144 ipcp_Destroy(&ncp->ipcp);
146 ipv6cp_Destroy(&ncp->ipv6cp);
149 if (ncp->cfg.urgent.tcp.maxports) {
150 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
151 free(ncp->cfg.urgent.tcp.port);
152 ncp->cfg.urgent.tcp.port = NULL;
154 if (ncp->cfg.urgent.udp.maxports) {
155 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
156 free(ncp->cfg.urgent.udp.port);
157 ncp->cfg.urgent.udp.port = NULL;
162 ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
167 if (Enabled(bundle, OPT_IPCP)) {
169 fsm_Up(&ncp->ipcp.fsm);
170 fsm_Open(&ncp->ipcp.fsm);
175 if (Enabled(bundle, OPT_IPV6CP)) {
176 fsm_Up(&ncp->ipv6cp.fsm);
177 fsm_Open(&ncp->ipv6cp.fsm);
186 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
188 switch (ncprange_family(&addr->ifa)) {
190 ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
194 ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
201 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
203 if (ncprange_family(&addr->ifa) == AF_INET)
204 ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
208 ncp_SetLink(struct ncp *ncp, struct link *l)
210 ipcp_SetLink(&ncp->ipcp, l);
212 ipv6cp_SetLink(&ncp->ipv6cp, l);
217 * Enqueue a packet of the given address family. Nothing will make it
218 * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
221 ncp_Enqueue(struct ncp *ncp, int af, int pri, char *ptr, int count)
224 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
226 struct ipcp *ipcp = &ncp->ipcp;
230 * We allocate an extra 6 bytes, four at the front and two at the end.
231 * This is an optimisation so that we need to do less work in
232 * m_prepend() in acf_LayerPush() and proto_LayerPush() and
233 * appending in hdlc_LayerPush().
238 if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) {
239 log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
243 bp = m_get(count + 6, MB_IPOUT);
246 memcpy(MBUF_CTOP(bp), ptr, count);
247 m_enqueue(ipcp->Queue + pri, bp);
252 if (pri < 0 || pri >= IPV6CP_QUEUES(ipcp)) {
253 log_Printf(LogERROR, "Can't store in ipv6 queue %d\n", pri);
257 bp = m_get(count + 6, MB_IPOUT);
260 memcpy(MBUF_CTOP(bp), ptr, count);
261 m_enqueue(ipv6cp->Queue + pri, bp);
266 log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
271 * How many packets are queued to go out ?
274 ncp_QueueLen(struct ncp *ncp)
278 result = ipcp_QueueLen(&ncp->ipcp);
280 result += ipv6cp_QueueLen(&ncp->ipv6cp);
282 result += mp_QueueLen(&ncp->mp); /* Usually empty */
288 * Ditch all queued packets. This is usually done after our choked timer
289 * has fired - which happens because we couldn't send any traffic over
290 * any links for some time.
293 ncp_DeleteQueues(struct ncp *ncp)
296 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
298 struct ipcp *ipcp = &ncp->ipcp;
299 struct mp *mp = &ncp->mp;
302 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
304 m_freem(m_dequeue(q));
307 for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
309 m_freem(m_dequeue(q));
312 link_DeleteQueue(&mp->link); /* Usually empty anyway */
316 * Arrange that each of our links has at least one packet. We keep the
317 * number of packets queued at the link level to a minimum so that the
318 * loss of a link in multi-link mode results in the minimum number of
322 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
326 if (bundle->ncp.mp.active)
327 total = mp_FillPhysicalQueues(bundle);
332 for (total = 0, dl = bundle->links; dl; dl = dl->next)
333 if (dl->state == DATALINK_OPEN) {
334 add = link_QueueLen(&dl->physical->link);
335 if (add == 0 && dl->physical->out == NULL)
336 add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
341 return total + ncp_QueueLen(&bundle->ncp);
345 * Push a packet into the given link. ``af'' is used as a persistent record
346 * of what is to be pushed next, coming either from mp->out or ncp->afq.
349 ncp_PushPacket(struct ncp *ncp, int *af, struct link *l)
351 struct bundle *bundle = l->lcp.fsm.bundle;
355 if (*af == AF_INET) {
356 if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
359 res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
361 if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
364 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
367 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
374 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
378 for (f = 0; f < range->nports; f++)
379 if (range->port[f] == src || range->port[f] == dst)
386 ncp_AddUrgentPort(struct port_range *range, u_short port)
391 if (range->nports == range->maxports) {
392 range->maxports += 10;
393 newport = (u_short *)realloc(range->port,
394 range->maxports * sizeof(u_short));
395 if (newport == NULL) {
396 log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
398 range->maxports -= 10;
401 range->port = newport;
404 for (p = 0; p < range->nports; p++)
405 if (range->port[p] == port) {
406 log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
408 } else if (range->port[p] > port) {
409 memmove(range->port + p + 1, range->port + p,
410 (range->nports - p) * sizeof(u_short));
411 range->port[p] = port;
416 if (p == range->nports)
417 range->port[range->nports++] = port;
421 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
425 for (p = 0; p < range->nports; p++)
426 if (range->port[p] == port) {
427 if (p != range->nports - 1)
428 memmove(range->port + p, range->port + p + 1,
429 (range->nports - p - 1) * sizeof(u_short));
434 if (p == range->nports)
435 log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
439 ncp_ClearUrgentPorts(struct port_range *range)
445 ncp_Show(struct cmdargs const *arg)
447 struct ncp *ncp = &arg->bundle->ncp;
451 prompt_Printf(arg->prompt, "Next queued AF: %s\n",
452 ncp->afq == AF_INET6 ? "inet6" : "inet");
456 prompt_Printf(arg->prompt, "\n");
457 route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
460 prompt_Printf(arg->prompt, "\nDefaults:\n");
461 prompt_Printf(arg->prompt, " sendpipe: ");
462 if (ncp->cfg.sendpipe > 0)
463 prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
465 prompt_Printf(arg->prompt, "unspecified\n");
466 prompt_Printf(arg->prompt, " recvpipe: ");
467 if (ncp->cfg.recvpipe > 0)
468 prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
470 prompt_Printf(arg->prompt, "unspecified\n");
472 prompt_Printf(arg->prompt, "\n Urgent ports\n");
473 prompt_Printf(arg->prompt, " TCP: ");
474 if (ncp->cfg.urgent.tcp.nports == 0)
475 prompt_Printf(arg->prompt, "none");
477 for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
479 prompt_Printf(arg->prompt, ", ");
480 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
483 prompt_Printf(arg->prompt, "\n UDP: ");
484 if (ncp->cfg.urgent.udp.nports == 0)
485 prompt_Printf(arg->prompt, "none");
487 for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
489 prompt_Printf(arg->prompt, ", ");
490 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
492 prompt_Printf(arg->prompt, "\n TOS: %s\n\n",
493 ncp->cfg.urgent.tos ? "yes" : "no");
499 ncp_LayersOpen(struct ncp *ncp)
503 n = !!(ncp->ipcp.fsm.state == ST_OPENED);
505 n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
512 ncp_LayersUnfinished(struct ncp *ncp)
516 if (ncp->ipcp.fsm.state > ST_CLOSED ||
517 ncp->ipcp.fsm.state == ST_STARTING)
521 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
522 ncp->ipv6cp.fsm.state == ST_STARTING)
530 ncp_Close(struct ncp *ncp)
532 if (ncp->ipcp.fsm.state > ST_CLOSED ||
533 ncp->ipcp.fsm.state == ST_STARTING)
534 fsm_Close(&ncp->ipcp.fsm);
537 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
538 ncp->ipv6cp.fsm.state == ST_STARTING)
539 fsm_Close(&ncp->ipv6cp.fsm);
544 ncp2initial(struct ncp *ncp)
546 fsm2initial(&ncp->ipcp.fsm);
548 fsm2initial(&ncp->ipv6cp.fsm);