acpi: Narrow workaround for broken interrupt settings
[dragonfly.git] / usr.sbin / ppp / ncp.c
bloba2f925e1fd7b26056046f534f73b7d95b3841437
1 /*-
2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/ppp/ncp.c,v 1.5.2.1 2002/09/01 02:12:29 brian Exp $
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>
35 #include <sys/un.h>
37 #include <errno.h>
38 #include <resolv.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <termios.h>
44 #include "layer.h"
45 #include "defs.h"
46 #include "command.h"
47 #include "mbuf.h"
48 #include "log.h"
49 #include "timer.h"
50 #include "fsm.h"
51 #include "iplist.h"
52 #include "throughput.h"
53 #include "slcompress.h"
54 #include "lqr.h"
55 #include "hdlc.h"
56 #include "lcp.h"
57 #include "ncpaddr.h"
58 #include "ipcp.h"
59 #include "filter.h"
60 #include "descriptor.h"
61 #include "async.h"
62 #include "ccp.h"
63 #include "link.h"
64 #include "physical.h"
65 #include "mp.h"
66 #ifndef NORADIUS
67 #include "radius.h"
68 #endif
69 #include "ipv6cp.h"
70 #include "ncp.h"
71 #include "bundle.h"
72 #include "prompt.h"
73 #include "route.h"
74 #include "iface.h"
75 #include "chat.h"
76 #include "auth.h"
77 #include "chap.h"
78 #include "cbcp.h"
79 #include "datalink.h"
82 static u_short default_urgent_tcp_ports[] = {
83 21, /* ftp */
84 22, /* ssh */
85 23, /* telnet */
86 513, /* login */
87 514, /* shell */
88 543, /* klogin */
89 544 /* kshell */
92 #define NDEFTCPPORTS NELEM(default_urgent_tcp_ports)
94 void
95 ncp_Init(struct ncp *ncp, struct bundle *bundle)
97 ncp->afq = AF_INET;
98 ncp->route = NULL;
100 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
101 ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
102 memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
103 NDEFTCPPORTS * sizeof(u_short));
104 ncp->cfg.urgent.tos = 1;
106 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
107 ncp->cfg.urgent.udp.port = NULL;
109 mp_Init(&ncp->mp, bundle);
111 /* Send over the first physical link by default */
112 ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
113 &bundle->fsm);
114 #ifndef NOINET6
115 ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
116 &bundle->fsm);
117 #endif
120 void
121 ncp_Destroy(struct ncp *ncp)
123 ipcp_Destroy(&ncp->ipcp);
124 #ifndef NOINET6
125 ipv6cp_Destroy(&ncp->ipv6cp);
126 #endif
128 if (ncp->cfg.urgent.tcp.maxports) {
129 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
130 free(ncp->cfg.urgent.tcp.port);
131 ncp->cfg.urgent.tcp.port = NULL;
133 if (ncp->cfg.urgent.udp.maxports) {
134 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
135 free(ncp->cfg.urgent.udp.port);
136 ncp->cfg.urgent.udp.port = NULL;
141 ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
143 int res = 0;
145 #ifndef NOINET6
146 if (Enabled(bundle, OPT_IPCP)) {
147 #endif
148 fsm_Up(&ncp->ipcp.fsm);
149 fsm_Open(&ncp->ipcp.fsm);
150 res++;
151 #ifndef NOINET6
154 if (Enabled(bundle, OPT_IPV6CP)) {
155 fsm_Up(&ncp->ipv6cp.fsm);
156 fsm_Open(&ncp->ipv6cp.fsm);
157 res++;
159 #endif
161 return res;
164 void
165 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
167 switch (ncprange_family(&addr->ifa)) {
168 case AF_INET:
169 ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
170 break;
171 #ifndef NOINET6
172 case AF_INET6:
173 ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
174 break;
175 #endif
179 void
180 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
182 if (ncprange_family(&addr->ifa) == AF_INET)
183 ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
186 void
187 ncp_SetLink(struct ncp *ncp, struct link *l)
189 ipcp_SetLink(&ncp->ipcp, l);
190 #ifndef NOINET6
191 ipv6cp_SetLink(&ncp->ipv6cp, l);
192 #endif
196 * Enqueue a packet of the given address family. Nothing will make it
197 * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
199 void
200 ncp_Enqueue(struct ncp *ncp, int af, unsigned pri, char *ptr, int count)
202 #ifndef NOINET6
203 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
204 #endif
205 struct ipcp *ipcp = &ncp->ipcp;
206 struct mbuf *bp;
209 * We allocate an extra 6 bytes, four at the front and two at the end.
210 * This is an optimisation so that we need to do less work in
211 * m_prepend() in acf_LayerPush() and proto_LayerPush() and
212 * appending in hdlc_LayerPush().
215 switch (af) {
216 case AF_INET:
217 if (pri >= IPCP_QUEUES(ipcp)) {
218 log_Printf(LogERROR, "Can't store in ip queue %u\n", pri);
219 break;
222 bp = m_get(count + 6, MB_IPOUT);
223 bp->m_offset += 4;
224 bp->m_len -= 6;
225 memcpy(MBUF_CTOP(bp), ptr, count);
226 m_enqueue(ipcp->Queue + pri, bp);
227 break;
229 #ifndef NOINET6
230 case AF_INET6:
231 if (pri >= IPV6CP_QUEUES(ipcp)) {
232 log_Printf(LogERROR, "Can't store in ipv6 queue %u\n", pri);
233 break;
236 bp = m_get(count + 6, MB_IPOUT);
237 bp->m_offset += 4;
238 bp->m_len -= 6;
239 memcpy(MBUF_CTOP(bp), ptr, count);
240 m_enqueue(ipv6cp->Queue + pri, bp);
241 break;
242 #endif
244 default:
245 log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
250 * How many packets are queued to go out ?
252 size_t
253 ncp_QueueLen(struct ncp *ncp)
255 size_t result;
257 result = ipcp_QueueLen(&ncp->ipcp);
258 #ifndef NOINET6
259 result += ipv6cp_QueueLen(&ncp->ipv6cp);
260 #endif
261 result += mp_QueueLen(&ncp->mp); /* Usually empty */
263 return result;
267 * Ditch all queued packets. This is usually done after our choked timer
268 * has fired - which happens because we couldn't send any traffic over
269 * any links for some time.
271 void
272 ncp_DeleteQueues(struct ncp *ncp)
274 #ifndef NOINET6
275 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
276 #endif
277 struct ipcp *ipcp = &ncp->ipcp;
278 struct mp *mp = &ncp->mp;
279 struct mqueue *q;
281 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
282 while (q->top)
283 m_freem(m_dequeue(q));
285 #ifndef NOINET6
286 for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
287 while (q->top)
288 m_freem(m_dequeue(q));
289 #endif
291 link_DeleteQueue(&mp->link); /* Usually empty anyway */
295 * Arrange that each of our links has at least one packet. We keep the
296 * number of packets queued at the link level to a minimum so that the
297 * loss of a link in multi-link mode results in the minimum number of
298 * dropped packets.
300 size_t
301 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
303 size_t total;
305 if (bundle->ncp.mp.active)
306 total = mp_FillPhysicalQueues(bundle);
307 else {
308 struct datalink *dl;
309 size_t add;
311 for (total = 0, dl = bundle->links; dl; dl = dl->next)
312 if (dl->state == DATALINK_OPEN) {
313 add = link_QueueLen(&dl->physical->link);
314 if (add == 0 && dl->physical->out == NULL)
315 add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
316 total += add;
320 return total + ncp_QueueLen(&bundle->ncp);
324 * Push a packet into the given link. ``af'' is used as a persistent record
325 * of what is to be pushed next, coming either from mp->out or ncp->afq.
328 ncp_PushPacket(struct ncp *ncp __unused, int *af, struct link *l)
330 struct bundle *bundle = l->lcp.fsm.bundle;
331 int res;
333 #ifndef NOINET6
334 if (*af == AF_INET) {
335 if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
336 *af = AF_INET6;
337 else
338 res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
339 } else {
340 if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
341 *af = AF_INET;
342 else
343 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
345 #else
346 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
347 #endif
349 return res;
353 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
355 unsigned f;
357 for (f = 0; f < range->nports; f++)
358 if (range->port[f] == src || range->port[f] == dst)
359 return 1;
361 return 0;
364 void
365 ncp_AddUrgentPort(struct port_range *range, u_short port)
367 u_short *newport;
368 unsigned p;
370 if (range->nports == range->maxports) {
371 range->maxports += 10;
372 newport = (u_short *)realloc(range->port,
373 range->maxports * sizeof(u_short));
374 if (newport == NULL) {
375 log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
376 strerror(errno));
377 range->maxports -= 10;
378 return;
380 range->port = newport;
383 for (p = 0; p < range->nports; p++)
384 if (range->port[p] == port) {
385 log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
386 break;
387 } else if (range->port[p] > port) {
388 memmove(range->port + p + 1, range->port + p,
389 (range->nports - p) * sizeof(u_short));
390 range->port[p] = port;
391 range->nports++;
392 break;
395 if (p == range->nports)
396 range->port[range->nports++] = port;
399 void
400 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
402 unsigned p;
404 for (p = 0; p < range->nports; p++)
405 if (range->port[p] == port) {
406 if (p + 1 != range->nports)
407 memmove(range->port + p, range->port + p + 1,
408 (range->nports - p - 1) * sizeof(u_short));
409 range->nports--;
410 return;
413 if (p == range->nports)
414 log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
417 void
418 ncp_ClearUrgentPorts(struct port_range *range)
420 range->nports = 0;
424 ncp_Show(struct cmdargs const *arg)
426 struct ncp *ncp = &arg->bundle->ncp;
427 unsigned p;
429 #ifndef NOINET6
430 prompt_Printf(arg->prompt, "Next queued AF: %s\n",
431 ncp->afq == AF_INET6 ? "inet6" : "inet");
432 #endif
434 if (ncp->route) {
435 prompt_Printf(arg->prompt, "\n");
436 route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
439 prompt_Printf(arg->prompt, "\nDefaults:\n");
440 prompt_Printf(arg->prompt, " sendpipe: ");
441 if (ncp->cfg.sendpipe > 0)
442 prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
443 else
444 prompt_Printf(arg->prompt, "unspecified\n");
445 prompt_Printf(arg->prompt, " recvpipe: ");
446 if (ncp->cfg.recvpipe > 0)
447 prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
448 else
449 prompt_Printf(arg->prompt, "unspecified\n");
451 prompt_Printf(arg->prompt, "\n Urgent ports\n");
452 prompt_Printf(arg->prompt, " TCP: ");
453 if (ncp->cfg.urgent.tcp.nports == 0)
454 prompt_Printf(arg->prompt, "none");
455 else
456 for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
457 if (p)
458 prompt_Printf(arg->prompt, ", ");
459 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
462 prompt_Printf(arg->prompt, "\n UDP: ");
463 if (ncp->cfg.urgent.udp.nports == 0)
464 prompt_Printf(arg->prompt, "none");
465 else
466 for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
467 if (p)
468 prompt_Printf(arg->prompt, ", ");
469 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
471 prompt_Printf(arg->prompt, "\n TOS: %s\n\n",
472 ncp->cfg.urgent.tos ? "yes" : "no");
474 return 0;
478 ncp_LayersOpen(struct ncp *ncp)
480 int n;
482 n = !!(ncp->ipcp.fsm.state == ST_OPENED);
483 #ifndef NOINET6
484 n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
485 #endif
487 return n;
491 ncp_LayersUnfinished(struct ncp *ncp)
493 int n = 0;
495 if (ncp->ipcp.fsm.state > ST_CLOSED ||
496 ncp->ipcp.fsm.state == ST_STARTING)
497 n++;
499 #ifndef NOINET6
500 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
501 ncp->ipv6cp.fsm.state == ST_STARTING)
502 n++;
503 #endif
505 return n;
508 void
509 ncp_Close(struct ncp *ncp)
511 if (ncp->ipcp.fsm.state > ST_CLOSED ||
512 ncp->ipcp.fsm.state == ST_STARTING)
513 fsm_Close(&ncp->ipcp.fsm);
515 #ifndef NOINET6
516 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
517 ncp->ipv6cp.fsm.state == ST_STARTING)
518 fsm_Close(&ncp->ipv6cp.fsm);
519 #endif
522 void
523 ncp2initial(struct ncp *ncp)
525 fsm2initial(&ncp->ipcp.fsm);
526 #ifndef NOINET6
527 fsm2initial(&ncp->ipv6cp.fsm);
528 #endif