xref: /freebsd/usr.sbin/ppp/ncp.c (revision cec50dea12481dc578c0805c887ab2097e1c06c5)
1 /*-
2  * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $FreeBSD$
27  */
28 
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>
36 
37 #include <errno.h>
38 #include <resolv.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <termios.h>
43 
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"
80 
81 
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 */
90 };
91 
92 #define NDEFTCPPORTS \
93   (sizeof default_urgent_tcp_ports / sizeof default_urgent_tcp_ports[0])
94 
95 void
96 ncp_Init(struct ncp *ncp, struct bundle *bundle)
97 {
98   ncp->afq = AF_INET;
99   ncp->route = NULL;
100 
101   ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
102   ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
103   memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
104          NDEFTCPPORTS * sizeof(u_short));
105   ncp->cfg.urgent.tos = 1;
106 
107   ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
108   ncp->cfg.urgent.udp.port = NULL;
109 
110   mp_Init(&ncp->mp, bundle);
111 
112   /* Send over the first physical link by default */
113   ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
114             &bundle->fsm);
115 #ifndef NOINET6
116   ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
117               &bundle->fsm);
118 #endif
119 }
120 
121 void
122 ncp_Destroy(struct ncp *ncp)
123 {
124   ipcp_Destroy(&ncp->ipcp);
125 #ifndef NOINET6
126   ipv6cp_Destroy(&ncp->ipv6cp);
127 #endif
128 
129   if (ncp->cfg.urgent.tcp.maxports) {
130     ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
131     free(ncp->cfg.urgent.tcp.port);
132     ncp->cfg.urgent.tcp.port = NULL;
133   }
134   if (ncp->cfg.urgent.udp.maxports) {
135     ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
136     free(ncp->cfg.urgent.udp.port);
137     ncp->cfg.urgent.udp.port = NULL;
138   }
139 }
140 
141 int
142 ncp_fsmStart(struct ncp *ncp,
143 #ifdef NOINET6
144 	     struct bundle *bundle __unused
145 #else
146 	     struct bundle *bundle
147 #endif
148 	     )
149 {
150   int res = 0;
151 
152 #ifndef NOINET6
153   if (Enabled(bundle, OPT_IPCP)) {
154 #endif
155     fsm_Up(&ncp->ipcp.fsm);
156     fsm_Open(&ncp->ipcp.fsm);
157     res++;
158 #ifndef NOINET6
159   }
160 
161   if (Enabled(bundle, OPT_IPV6CP)) {
162     fsm_Up(&ncp->ipv6cp.fsm);
163     fsm_Open(&ncp->ipv6cp.fsm);
164     res++;
165   }
166 #endif
167 
168   return res;
169 }
170 
171 void
172 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
173 {
174   switch (ncprange_family(&addr->ifa)) {
175   case AF_INET:
176     ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
177     break;
178 #ifndef NOINET6
179   case AF_INET6:
180     ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
181     break;
182 #endif
183   }
184 }
185 
186 void
187 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
188 {
189   if (ncprange_family(&addr->ifa) == AF_INET)
190     ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
191 }
192 
193 void
194 ncp_SetLink(struct ncp *ncp, struct link *l)
195 {
196   ipcp_SetLink(&ncp->ipcp, l);
197 #ifndef NOINET6
198   ipv6cp_SetLink(&ncp->ipv6cp, l);
199 #endif
200 }
201 
202 /*
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.
205  */
206 void
207 ncp_Enqueue(struct ncp *ncp, int af, unsigned pri, char *ptr, int count)
208 {
209 #ifndef NOINET6
210   struct ipv6cp *ipv6cp = &ncp->ipv6cp;
211 #endif
212   struct ipcp *ipcp = &ncp->ipcp;
213   struct mbuf *bp;
214 
215   /*
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().
220    */
221 
222   switch (af) {
223   case AF_INET:
224     if (pri >= IPCP_QUEUES(ipcp)) {
225       log_Printf(LogERROR, "Can't store in ip queue %u\n", pri);
226       break;
227     }
228 
229     bp = m_get(count + 6, MB_IPOUT);
230     bp->m_offset += 4;
231     bp->m_len -= 6;
232     memcpy(MBUF_CTOP(bp), ptr, count);
233     m_enqueue(ipcp->Queue + pri, bp);
234     break;
235 
236 #ifndef NOINET6
237   case AF_INET6:
238     if (pri >= IPV6CP_QUEUES(ipcp)) {
239       log_Printf(LogERROR, "Can't store in ipv6 queue %u\n", pri);
240       break;
241     }
242 
243     bp = m_get(count + 6, MB_IPOUT);
244     bp->m_offset += 4;
245     bp->m_len -= 6;
246     memcpy(MBUF_CTOP(bp), ptr, count);
247     m_enqueue(ipv6cp->Queue + pri, bp);
248     break;
249 #endif
250 
251   default:
252       log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
253   }
254 }
255 
256 /*
257  * How many packets are queued to go out ?
258  */
259 size_t
260 ncp_QueueLen(struct ncp *ncp)
261 {
262   size_t result;
263 
264   result = ipcp_QueueLen(&ncp->ipcp);
265 #ifndef NOINET6
266   result += ipv6cp_QueueLen(&ncp->ipv6cp);
267 #endif
268   result += mp_QueueLen(&ncp->mp);	/* Usually empty */
269 
270   return result;
271 }
272 
273 /*
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.
277  */
278 void
279 ncp_DeleteQueues(struct ncp *ncp)
280 {
281 #ifndef NOINET6
282   struct ipv6cp *ipv6cp = &ncp->ipv6cp;
283 #endif
284   struct ipcp *ipcp = &ncp->ipcp;
285   struct mp *mp = &ncp->mp;
286   struct mqueue *q;
287 
288   for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
289     while (q->top)
290       m_freem(m_dequeue(q));
291 
292 #ifndef NOINET6
293   for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
294     while (q->top)
295       m_freem(m_dequeue(q));
296 #endif
297 
298   link_DeleteQueue(&mp->link);	/* Usually empty anyway */
299 }
300 
301 /*
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
305  * dropped packets.
306  */
307 size_t
308 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
309 {
310   size_t total;
311 
312   if (bundle->ncp.mp.active)
313     total = mp_FillPhysicalQueues(bundle);
314   else {
315     struct datalink *dl;
316     size_t add;
317 
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);
323         total += add;
324       }
325   }
326 
327   return total + ncp_QueueLen(&bundle->ncp);
328 }
329 
330 /*
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.
333  */
334 int
335 ncp_PushPacket(struct ncp *ncp __unused,
336 #ifdef NOINET6
337 	       int *af __unused,
338 #else
339 	       int *af,
340 #endif
341 	       struct link *l)
342 {
343   struct bundle *bundle = l->lcp.fsm.bundle;
344   int res;
345 
346 #ifndef NOINET6
347   if (*af == AF_INET) {
348     if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
349       *af = AF_INET6;
350     else
351       res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
352   } else {
353     if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
354       *af = AF_INET;
355     else
356       res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
357   }
358 #else
359   res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
360 #endif
361 
362   return res;
363 }
364 
365 int
366 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
367 {
368   unsigned f;
369 
370   for (f = 0; f < range->nports; f++)
371     if (range->port[f] == src || range->port[f] == dst)
372       return 1;
373 
374   return 0;
375 }
376 
377 void
378 ncp_AddUrgentPort(struct port_range *range, u_short port)
379 {
380   u_short *newport;
381   unsigned p;
382 
383   if (range->nports == range->maxports) {
384     range->maxports += 10;
385     newport = (u_short *)realloc(range->port,
386                                  range->maxports * sizeof(u_short));
387     if (newport == NULL) {
388       log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
389                  strerror(errno));
390       range->maxports -= 10;
391       return;
392     }
393     range->port = newport;
394   }
395 
396   for (p = 0; p < range->nports; p++)
397     if (range->port[p] == port) {
398       log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
399       break;
400     } else if (range->port[p] > port) {
401       memmove(range->port + p + 1, range->port + p,
402               (range->nports - p) * sizeof(u_short));
403       range->port[p] = port;
404       range->nports++;
405       break;
406     }
407 
408   if (p == range->nports)
409     range->port[range->nports++] = port;
410 }
411 
412 void
413 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
414 {
415   unsigned p;
416 
417   for (p = 0; p < range->nports; p++)
418     if (range->port[p] == port) {
419       if (p + 1 != range->nports)
420         memmove(range->port + p, range->port + p + 1,
421                 (range->nports - p - 1) * sizeof(u_short));
422       range->nports--;
423       return;
424     }
425 
426   if (p == range->nports)
427     log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
428 }
429 
430 void
431 ncp_ClearUrgentPorts(struct port_range *range)
432 {
433   range->nports = 0;
434 }
435 
436 int
437 ncp_Show(struct cmdargs const *arg)
438 {
439   struct ncp *ncp = &arg->bundle->ncp;
440   unsigned p;
441 
442 #ifndef NOINET6
443   prompt_Printf(arg->prompt, "Next queued AF: %s\n",
444                 ncp->afq == AF_INET6 ? "inet6" : "inet");
445 #endif
446 
447   if (ncp->route) {
448     prompt_Printf(arg->prompt, "\n");
449     route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
450   }
451 
452   prompt_Printf(arg->prompt, "\nDefaults:\n");
453   prompt_Printf(arg->prompt, "  sendpipe:      ");
454   if (ncp->cfg.sendpipe > 0)
455     prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
456   else
457     prompt_Printf(arg->prompt, "unspecified\n");
458   prompt_Printf(arg->prompt, "  recvpipe:      ");
459   if (ncp->cfg.recvpipe > 0)
460     prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
461   else
462     prompt_Printf(arg->prompt, "unspecified\n");
463 
464   prompt_Printf(arg->prompt, "\n  Urgent ports\n");
465   prompt_Printf(arg->prompt, "         TCP:    ");
466   if (ncp->cfg.urgent.tcp.nports == 0)
467     prompt_Printf(arg->prompt, "none");
468   else
469     for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
470       if (p)
471         prompt_Printf(arg->prompt, ", ");
472       prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
473     }
474 
475   prompt_Printf(arg->prompt, "\n         UDP:    ");
476   if (ncp->cfg.urgent.udp.nports == 0)
477     prompt_Printf(arg->prompt, "none");
478   else
479     for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
480       if (p)
481         prompt_Printf(arg->prompt, ", ");
482       prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
483     }
484   prompt_Printf(arg->prompt, "\n         TOS:    %s\n\n",
485                 ncp->cfg.urgent.tos ? "yes" : "no");
486 
487   return 0;
488 }
489 
490 int
491 ncp_LayersOpen(struct ncp *ncp)
492 {
493   int n;
494 
495   n = !!(ncp->ipcp.fsm.state == ST_OPENED);
496 #ifndef NOINET6
497   n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
498 #endif
499 
500   return n;
501 }
502 
503 int
504 ncp_LayersUnfinished(struct ncp *ncp)
505 {
506   int n = 0;
507 
508   if (ncp->ipcp.fsm.state > ST_CLOSED ||
509       ncp->ipcp.fsm.state == ST_STARTING)
510     n++;
511 
512 #ifndef NOINET6
513   if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
514       ncp->ipv6cp.fsm.state == ST_STARTING)
515     n++;
516 #endif
517 
518   return n;
519 }
520 
521 void
522 ncp_Close(struct ncp *ncp)
523 {
524   if (ncp->ipcp.fsm.state > ST_CLOSED ||
525       ncp->ipcp.fsm.state == ST_STARTING)
526     fsm_Close(&ncp->ipcp.fsm);
527 
528 #ifndef NOINET6
529   if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
530       ncp->ipv6cp.fsm.state == ST_STARTING)
531     fsm_Close(&ncp->ipv6cp.fsm);
532 #endif
533 }
534 
535 void
536 ncp2initial(struct ncp *ncp)
537 {
538   fsm2initial(&ncp->ipcp.fsm);
539 #ifndef NOINET6
540   fsm2initial(&ncp->ipv6cp.fsm);
541 #endif
542 }
543