MFC r1.6 r1.30 r1.28 (HEAD):
[dragonfly.git] / usr.sbin / ppp / ncp.c
blob97a8edc494acdf65218f62dae0ee758cedd78ccf
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 $
27 * $DragonFly: src/usr.sbin/ppp/ncp.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30 #include <sys/param.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/in.h>
33 #include <netinet/ip.h>
34 #include <sys/socket.h>
35 #include <net/route.h>
36 #include <sys/un.h>
38 #include <errno.h>
39 #include <resolv.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <termios.h>
45 #include "layer.h"
46 #include "defs.h"
47 #include "command.h"
48 #include "mbuf.h"
49 #include "log.h"
50 #include "timer.h"
51 #include "fsm.h"
52 #include "iplist.h"
53 #include "throughput.h"
54 #include "slcompress.h"
55 #include "lqr.h"
56 #include "hdlc.h"
57 #include "lcp.h"
58 #include "ncpaddr.h"
59 #include "ipcp.h"
60 #include "filter.h"
61 #include "descriptor.h"
62 #include "async.h"
63 #include "ccp.h"
64 #include "link.h"
65 #include "physical.h"
66 #include "mp.h"
67 #ifndef NORADIUS
68 #include "radius.h"
69 #endif
70 #include "ipv6cp.h"
71 #include "ncp.h"
72 #include "bundle.h"
73 #include "prompt.h"
74 #include "route.h"
75 #include "iface.h"
76 #include "chat.h"
77 #include "auth.h"
78 #include "chap.h"
79 #include "cbcp.h"
80 #include "datalink.h"
83 static u_short default_urgent_tcp_ports[] = {
84 21, /* ftp */
85 22, /* ssh */
86 23, /* telnet */
87 513, /* login */
88 514, /* shell */
89 543, /* klogin */
90 544 /* kshell */
93 static u_short default_urgent_udp_ports[] = { };
95 #define NDEFTCPPORTS \
96 (sizeof default_urgent_tcp_ports / sizeof default_urgent_tcp_ports[0])
97 #define NDEFUDPPORTS \
98 (sizeof default_urgent_udp_ports / sizeof default_urgent_udp_ports[0])
100 void
101 ncp_Init(struct ncp *ncp, struct bundle *bundle)
103 ncp->afq = AF_INET;
104 ncp->route = NULL;
106 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
107 ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
108 memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
109 NDEFTCPPORTS * sizeof(u_short));
110 ncp->cfg.urgent.tos = 1;
112 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = NDEFUDPPORTS;
113 ncp->cfg.urgent.udp.port = (u_short *)malloc(NDEFUDPPORTS * sizeof(u_short));
114 memcpy(ncp->cfg.urgent.udp.port, default_urgent_udp_ports,
115 NDEFUDPPORTS * sizeof(u_short));
118 mp_Init(&ncp->mp, bundle);
120 /* Send over the first physical link by default */
121 ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
122 &bundle->fsm);
123 #ifndef NOINET6
124 ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
125 &bundle->fsm);
126 #endif
129 void
130 ncp_Destroy(struct ncp *ncp)
132 ipcp_Destroy(&ncp->ipcp);
133 #ifndef NOINET6
134 ipv6cp_Destroy(&ncp->ipv6cp);
135 #endif
137 if (ncp->cfg.urgent.tcp.maxports) {
138 ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
139 free(ncp->cfg.urgent.tcp.port);
140 ncp->cfg.urgent.tcp.port = NULL;
142 if (ncp->cfg.urgent.udp.maxports) {
143 ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
144 free(ncp->cfg.urgent.udp.port);
145 ncp->cfg.urgent.udp.port = NULL;
150 ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
152 int res = 0;
154 #ifndef NOINET6
155 if (Enabled(bundle, OPT_IPCP)) {
156 #endif
157 fsm_Up(&ncp->ipcp.fsm);
158 fsm_Open(&ncp->ipcp.fsm);
159 res++;
160 #ifndef NOINET6
163 if (Enabled(bundle, OPT_IPV6CP)) {
164 fsm_Up(&ncp->ipv6cp.fsm);
165 fsm_Open(&ncp->ipv6cp.fsm);
166 res++;
168 #endif
170 return res;
173 void
174 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
176 switch (ncprange_family(&addr->ifa)) {
177 case AF_INET:
178 ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
179 break;
180 #ifndef NOINET6
181 case AF_INET6:
182 ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
183 break;
184 #endif
188 void
189 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
191 if (ncprange_family(&addr->ifa) == AF_INET)
192 ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
195 void
196 ncp_SetLink(struct ncp *ncp, struct link *l)
198 ipcp_SetLink(&ncp->ipcp, l);
199 #ifndef NOINET6
200 ipv6cp_SetLink(&ncp->ipv6cp, l);
201 #endif
205 * Enqueue a packet of the given address family. Nothing will make it
206 * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
208 void
209 ncp_Enqueue(struct ncp *ncp, int af, int pri, char *ptr, int count)
211 #ifndef NOINET6
212 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
213 #endif
214 struct ipcp *ipcp = &ncp->ipcp;
215 struct mbuf *bp;
218 * We allocate an extra 6 bytes, four at the front and two at the end.
219 * This is an optimisation so that we need to do less work in
220 * m_prepend() in acf_LayerPush() and proto_LayerPush() and
221 * appending in hdlc_LayerPush().
224 switch (af) {
225 case AF_INET:
226 if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) {
227 log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
228 break;
231 bp = m_get(count + 6, MB_IPOUT);
232 bp->m_offset += 4;
233 bp->m_len -= 6;
234 memcpy(MBUF_CTOP(bp), ptr, count);
235 m_enqueue(ipcp->Queue + pri, bp);
236 break;
238 #ifndef NOINET6
239 case AF_INET6:
240 if (pri < 0 || pri >= IPV6CP_QUEUES(ipcp)) {
241 log_Printf(LogERROR, "Can't store in ipv6 queue %d\n", pri);
242 break;
245 bp = m_get(count + 6, MB_IPOUT);
246 bp->m_offset += 4;
247 bp->m_len -= 6;
248 memcpy(MBUF_CTOP(bp), ptr, count);
249 m_enqueue(ipv6cp->Queue + pri, bp);
250 break;
251 #endif
253 default:
254 log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
259 * How many packets are queued to go out ?
261 size_t
262 ncp_QueueLen(struct ncp *ncp)
264 size_t result;
266 result = ipcp_QueueLen(&ncp->ipcp);
267 #ifndef NOINET6
268 result += ipv6cp_QueueLen(&ncp->ipv6cp);
269 #endif
270 result += mp_QueueLen(&ncp->mp); /* Usually empty */
272 return result;
276 * Ditch all queued packets. This is usually done after our choked timer
277 * has fired - which happens because we couldn't send any traffic over
278 * any links for some time.
280 void
281 ncp_DeleteQueues(struct ncp *ncp)
283 #ifndef NOINET6
284 struct ipv6cp *ipv6cp = &ncp->ipv6cp;
285 #endif
286 struct ipcp *ipcp = &ncp->ipcp;
287 struct mp *mp = &ncp->mp;
288 struct mqueue *q;
290 for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
291 while (q->top)
292 m_freem(m_dequeue(q));
294 #ifndef NOINET6
295 for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
296 while (q->top)
297 m_freem(m_dequeue(q));
298 #endif
300 link_DeleteQueue(&mp->link); /* Usually empty anyway */
304 * Arrange that each of our links has at least one packet. We keep the
305 * number of packets queued at the link level to a minimum so that the
306 * loss of a link in multi-link mode results in the minimum number of
307 * dropped packets.
309 size_t
310 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
312 size_t total;
314 if (bundle->ncp.mp.active)
315 total = mp_FillPhysicalQueues(bundle);
316 else {
317 struct datalink *dl;
318 size_t add;
320 for (total = 0, dl = bundle->links; dl; dl = dl->next)
321 if (dl->state == DATALINK_OPEN) {
322 add = link_QueueLen(&dl->physical->link);
323 if (add == 0 && dl->physical->out == NULL)
324 add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
325 total += add;
329 return total + ncp_QueueLen(&bundle->ncp);
333 * Push a packet into the given link. ``af'' is used as a persistent record
334 * of what is to be pushed next, coming either from mp->out or ncp->afq.
337 ncp_PushPacket(struct ncp *ncp, int *af, struct link *l)
339 struct bundle *bundle = l->lcp.fsm.bundle;
340 int res;
342 #ifndef NOINET6
343 if (*af == AF_INET) {
344 if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
345 *af = AF_INET6;
346 else
347 res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
348 } else {
349 if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
350 *af = AF_INET;
351 else
352 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
354 #else
355 res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
356 #endif
358 return res;
362 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
364 int f;
366 for (f = 0; f < range->nports; f++)
367 if (range->port[f] == src || range->port[f] == dst)
368 return 1;
370 return 0;
373 void
374 ncp_AddUrgentPort(struct port_range *range, u_short port)
376 u_short *newport;
377 int p;
379 if (range->nports == range->maxports) {
380 range->maxports += 10;
381 newport = (u_short *)realloc(range->port,
382 range->maxports * sizeof(u_short));
383 if (newport == NULL) {
384 log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
385 strerror(errno));
386 range->maxports -= 10;
387 return;
389 range->port = newport;
392 for (p = 0; p < range->nports; p++)
393 if (range->port[p] == port) {
394 log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
395 break;
396 } else if (range->port[p] > port) {
397 memmove(range->port + p + 1, range->port + p,
398 (range->nports - p) * sizeof(u_short));
399 range->port[p] = port;
400 range->nports++;
401 break;
404 if (p == range->nports)
405 range->port[range->nports++] = port;
408 void
409 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
411 int p;
413 for (p = 0; p < range->nports; p++)
414 if (range->port[p] == port) {
415 if (p != range->nports - 1)
416 memmove(range->port + p, range->port + p + 1,
417 (range->nports - p - 1) * sizeof(u_short));
418 range->nports--;
419 return;
422 if (p == range->nports)
423 log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
426 void
427 ncp_ClearUrgentPorts(struct port_range *range)
429 range->nports = 0;
433 ncp_Show(struct cmdargs const *arg)
435 struct ncp *ncp = &arg->bundle->ncp;
436 int p;
438 #ifndef NOINET6
439 prompt_Printf(arg->prompt, "Next queued AF: %s\n",
440 ncp->afq == AF_INET6 ? "inet6" : "inet");
441 #endif
443 if (ncp->route) {
444 prompt_Printf(arg->prompt, "\n");
445 route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
448 prompt_Printf(arg->prompt, "\nDefaults:\n");
449 prompt_Printf(arg->prompt, " sendpipe: ");
450 if (ncp->cfg.sendpipe > 0)
451 prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
452 else
453 prompt_Printf(arg->prompt, "unspecified\n");
454 prompt_Printf(arg->prompt, " recvpipe: ");
455 if (ncp->cfg.recvpipe > 0)
456 prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
457 else
458 prompt_Printf(arg->prompt, "unspecified\n");
460 prompt_Printf(arg->prompt, "\n Urgent ports\n");
461 prompt_Printf(arg->prompt, " TCP: ");
462 if (ncp->cfg.urgent.tcp.nports == 0)
463 prompt_Printf(arg->prompt, "none");
464 else
465 for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
466 if (p)
467 prompt_Printf(arg->prompt, ", ");
468 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
471 prompt_Printf(arg->prompt, "\n UDP: ");
472 if (ncp->cfg.urgent.udp.nports == 0)
473 prompt_Printf(arg->prompt, "none");
474 else
475 for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
476 if (p)
477 prompt_Printf(arg->prompt, ", ");
478 prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
480 prompt_Printf(arg->prompt, "\n TOS: %s\n\n",
481 ncp->cfg.urgent.tos ? "yes" : "no");
483 return 0;
487 ncp_LayersOpen(struct ncp *ncp)
489 int n;
491 n = !!(ncp->ipcp.fsm.state == ST_OPENED);
492 #ifndef NOINET6
493 n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
494 #endif
496 return n;
500 ncp_LayersUnfinished(struct ncp *ncp)
502 int n = 0;
504 if (ncp->ipcp.fsm.state > ST_CLOSED ||
505 ncp->ipcp.fsm.state == ST_STARTING)
506 n++;
508 #ifndef NOINET6
509 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
510 ncp->ipv6cp.fsm.state == ST_STARTING)
511 n++;
512 #endif
514 return n;
517 void
518 ncp_Close(struct ncp *ncp)
520 if (ncp->ipcp.fsm.state > ST_CLOSED ||
521 ncp->ipcp.fsm.state == ST_STARTING)
522 fsm_Close(&ncp->ipcp.fsm);
524 #ifndef NOINET6
525 if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
526 ncp->ipv6cp.fsm.state == ST_STARTING)
527 fsm_Close(&ncp->ipv6cp.fsm);
528 #endif
531 void
532 ncp2initial(struct ncp *ncp)
534 fsm2initial(&ncp->ipcp.fsm);
535 #ifndef NOINET6
536 fsm2initial(&ncp->ipv6cp.fsm);
537 #endif