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 <sys/socket.h>
34 #include <net/route.h>
51 #include "throughput.h"
52 #include "slcompress.h"
59 #include "descriptor.h"
81 static u_short default_urgent_tcp_ports[] = {
91 static u_short default_urgent_udp_ports[] = { };
93 #define NDEFTCPPORTS \
94 (sizeof default_urgent_tcp_ports / sizeof default_urgent_tcp_ports[0])
95 #define NDEFUDPPORTS \
96 (sizeof default_urgent_udp_ports / sizeof default_urgent_udp_ports[0])
99 ncp_Init(struct ncp *ncp, struct bundle *bundle)
104 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
105 ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
106 memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
107 NDEFTCPPORTS * sizeof(u_short));
108 ncp->cfg.urgent.tos = 1;
110 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = NDEFUDPPORTS;
111 ncp->cfg.urgent.udp.port = (u_short *)malloc(NDEFUDPPORTS * sizeof(u_short));
112 memcpy(ncp->cfg.urgent.udp.port, default_urgent_udp_ports,
113 NDEFUDPPORTS * sizeof(u_short));
116 mp_Init(&ncp->mp, bundle);
118 /* Send over the first physical link by default */
119 ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
122 ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
128 ncp_Destroy(struct ncp *ncp)
130 ipcp_Destroy(&ncp->ipcp);
132 ipv6cp_Destroy(&ncp->ipv6cp);
135 if (ncp->cfg.urgent.tcp.maxports) {
136 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
137 free(ncp->cfg.urgent.tcp.port);
138 ncp->cfg.urgent.tcp.port = NULL;
140 if (ncp->cfg.urgent.udp.maxports) {
141 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
142 free(ncp->cfg.urgent.udp.port);
143 ncp->cfg.urgent.udp.port = NULL;
148 ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
153 if (Enabled(bundle, OPT_IPCP)) {
155 fsm_Up(&ncp->ipcp.fsm);
156 fsm_Open(&ncp->ipcp.fsm);
161 if (Enabled(bundle, OPT_IPV6CP)) {
162 fsm_Up(&ncp->ipv6cp.fsm);
163 fsm_Open(&ncp->ipv6cp.fsm);
172 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
174 switch (ncprange_family(&addr->ifa)) {
176 ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
180 ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
187 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
189 if (ncprange_family(&addr->ifa) == AF_INET)
190 ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
194 ncp_SetLink(struct ncp *ncp, struct link *l)
196 ipcp_SetLink(&ncp->ipcp, l);
198 ipv6cp_SetLink(&ncp->ipv6cp, l);
203 * Enqueue a packet of the given address family. Nothing will make it
204 * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
207 ncp_Enqueue(struct ncp *ncp, int af, int pri, char *ptr, int count)
210 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
212 struct ipcp *ipcp = &ncp->ipcp;
216 * We allocate an extra 6 bytes, four at the front and two at the end.
217 * This is an optimisation so that we need to do less work in
218 * m_prepend() in acf_LayerPush() and proto_LayerPush() and
219 * appending in hdlc_LayerPush().
224 if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) {
225 log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
229 bp = m_get(count + 6, MB_IPOUT);
232 memcpy(MBUF_CTOP(bp), ptr, count);
233 m_enqueue(ipcp->Queue + pri, bp);
238 if (pri < 0 || pri >= IPV6CP_QUEUES(ipcp)) {
239 log_Printf(LogERROR, "Can't store in ipv6 queue %d\n", pri);
243 bp = m_get(count + 6, MB_IPOUT);
246 memcpy(MBUF_CTOP(bp), ptr, count);
247 m_enqueue(ipv6cp->Queue + pri, bp);
252 log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
257 * How many packets are queued to go out ?
260 ncp_QueueLen(struct ncp *ncp)
264 result = ipcp_QueueLen(&ncp->ipcp);
266 result += ipv6cp_QueueLen(&ncp->ipv6cp);
268 result += mp_QueueLen(&ncp->mp); /* Usually empty */
274 * Ditch all queued packets. This is usually done after our choked timer
275 * has fired - which happens because we couldn't send any traffic over
276 * any links for some time.
279 ncp_DeleteQueues(struct ncp *ncp)
282 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
284 struct ipcp *ipcp = &ncp->ipcp;
285 struct mp *mp = &ncp->mp;
288 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
290 m_freem(m_dequeue(q));
293 for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
295 m_freem(m_dequeue(q));
298 link_DeleteQueue(&mp->link); /* Usually empty anyway */
302 * Arrange that each of our links has at least one packet. We keep the
303 * number of packets queued at the link level to a minimum so that the
304 * loss of a link in multi-link mode results in the minimum number of
308 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
312 if (bundle->ncp.mp.active)
313 total = mp_FillPhysicalQueues(bundle);
318 for (total = 0, dl = bundle->links; dl; dl = dl->next)
319 if (dl->state == DATALINK_OPEN) {
320 add = link_QueueLen(&dl->physical->link);
321 if (add == 0 && dl->physical->out == NULL)
322 add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
327 return total + ncp_QueueLen(&bundle->ncp);
331 * Push a packet into the given link. ``af'' is used as a persistent record
332 * of what is to be pushed next, coming either from mp->out or ncp->afq.
335 ncp_PushPacket(struct ncp *ncp, int *af, struct link *l)
337 struct bundle *bundle = l->lcp.fsm.bundle;
341 if (*af == AF_INET) {
342 if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
345 res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
347 if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
350 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
353 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
360 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
364 for (f = 0; f < range->nports; f++)
365 if (range->port[f] == src || range->port[f] == dst)
372 ncp_AddUrgentPort(struct port_range *range, u_short port)
377 if (range->nports == range->maxports) {
378 range->maxports += 10;
379 newport = (u_short *)realloc(range->port,
380 range->maxports * sizeof(u_short));
381 if (newport == NULL) {
382 log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
384 range->maxports -= 10;
387 range->port = newport;
390 for (p = 0; p < range->nports; p++)
391 if (range->port[p] == port) {
392 log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
394 } else if (range->port[p] > port) {
395 memmove(range->port + p + 1, range->port + p,
396 (range->nports - p) * sizeof(u_short));
397 range->port[p] = port;
402 if (p == range->nports)
403 range->port[range->nports++] = port;
407 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
411 for (p = 0; p < range->nports; p++)
412 if (range->port[p] == port) {
413 if (p != range->nports - 1)
414 memmove(range->port + p, range->port + p + 1,
415 (range->nports - p - 1) * sizeof(u_short));
420 if (p == range->nports)
421 log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
425 ncp_ClearUrgentPorts(struct port_range *range)
431 ncp_Show(struct cmdargs const *arg)
433 struct ncp *ncp = &arg->bundle->ncp;
437 prompt_Printf(arg->prompt, "Next queued AF: %s\n",
438 ncp->afq == AF_INET6 ? "inet6" : "inet");
442 prompt_Printf(arg->prompt, "\n");
443 route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
446 prompt_Printf(arg->prompt, "\nDefaults:\n");
447 prompt_Printf(arg->prompt, " sendpipe: ");
448 if (ncp->cfg.sendpipe > 0)
449 prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
451 prompt_Printf(arg->prompt, "unspecified\n");
452 prompt_Printf(arg->prompt, " recvpipe: ");
453 if (ncp->cfg.recvpipe > 0)
454 prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
456 prompt_Printf(arg->prompt, "unspecified\n");
458 prompt_Printf(arg->prompt, "\n Urgent ports\n");
459 prompt_Printf(arg->prompt, " TCP: ");
460 if (ncp->cfg.urgent.tcp.nports == 0)
461 prompt_Printf(arg->prompt, "none");
463 for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
465 prompt_Printf(arg->prompt, ", ");
466 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
469 prompt_Printf(arg->prompt, "\n UDP: ");
470 if (ncp->cfg.urgent.udp.nports == 0)
471 prompt_Printf(arg->prompt, "none");
473 for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
475 prompt_Printf(arg->prompt, ", ");
476 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
478 prompt_Printf(arg->prompt, "\n TOS: %s\n\n",
479 ncp->cfg.urgent.tos ? "yes" : "no");
485 ncp_LayersOpen(struct ncp *ncp)
489 n = !!(ncp->ipcp.fsm.state == ST_OPENED);
491 n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
498 ncp_LayersUnfinished(struct ncp *ncp)
502 if (ncp->ipcp.fsm.state > ST_CLOSED ||
503 ncp->ipcp.fsm.state == ST_STARTING)
507 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
508 ncp->ipv6cp.fsm.state == ST_STARTING)
516 ncp_Close(struct ncp *ncp)
518 if (ncp->ipcp.fsm.state > ST_CLOSED ||
519 ncp->ipcp.fsm.state == ST_STARTING)
520 fsm_Close(&ncp->ipcp.fsm);
523 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
524 ncp->ipv6cp.fsm.state == ST_STARTING)
525 fsm_Close(&ncp->ipv6cp.fsm);
530 ncp2initial(struct ncp *ncp)
532 fsm2initial(&ncp->ipcp.fsm);
534 fsm2initial(&ncp->ipv6cp.fsm);