ncp.c revision 134789
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3219820Sjeff * All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff * 1. Redistributions of source code must retain the above copyright
9219820Sjeff *    notice, this list of conditions and the following disclaimer.
10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11219820Sjeff *    notice, this list of conditions and the following disclaimer in the
12219820Sjeff *    documentation and/or other materials provided with the distribution.
13219820Sjeff *
14219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219820Sjeff * SUCH DAMAGE.
25219820Sjeff *
26219820Sjeff * $FreeBSD: head/usr.sbin/ppp/ncp.c 134789 2004-09-05 01:46:52Z brian $
27219820Sjeff */
28219820Sjeff
29219820Sjeff#include <sys/param.h>
30219820Sjeff#include <netinet/in_systm.h>
31219820Sjeff#include <netinet/in.h>
32219820Sjeff#include <netinet/ip.h>
33219820Sjeff#include <sys/socket.h>
34219820Sjeff#include <net/route.h>
35219820Sjeff#include <sys/un.h>
36219820Sjeff
37219820Sjeff#include <errno.h>
38219820Sjeff#include <resolv.h>
39219820Sjeff#include <stdarg.h>
40219820Sjeff#include <stdlib.h>
41219820Sjeff#include <string.h>
42219820Sjeff#include <termios.h>
43219820Sjeff
44219820Sjeff#include "layer.h"
45219820Sjeff#include "defs.h"
46219820Sjeff#include "command.h"
47219820Sjeff#include "mbuf.h"
48219820Sjeff#include "log.h"
49219820Sjeff#include "timer.h"
50219820Sjeff#include "fsm.h"
51219820Sjeff#include "iplist.h"
52219820Sjeff#include "throughput.h"
53219820Sjeff#include "slcompress.h"
54219820Sjeff#include "lqr.h"
55219820Sjeff#include "hdlc.h"
56219820Sjeff#include "lcp.h"
57219820Sjeff#include "ncpaddr.h"
58219820Sjeff#include "ipcp.h"
59219820Sjeff#include "filter.h"
60219820Sjeff#include "descriptor.h"
61219820Sjeff#include "async.h"
62219820Sjeff#include "ccp.h"
63219820Sjeff#include "link.h"
64219820Sjeff#include "physical.h"
65219820Sjeff#include "mp.h"
66219820Sjeff#ifndef NORADIUS
67219820Sjeff#include "radius.h"
68219820Sjeff#endif
69219820Sjeff#include "ipv6cp.h"
70219820Sjeff#include "ncp.h"
71219820Sjeff#include "bundle.h"
72219820Sjeff#include "prompt.h"
73219820Sjeff#include "route.h"
74219820Sjeff#include "iface.h"
75219820Sjeff#include "chat.h"
76219820Sjeff#include "auth.h"
77219820Sjeff#include "chap.h"
78219820Sjeff#include "cbcp.h"
79219820Sjeff#include "datalink.h"
80219820Sjeff
81219820Sjeff
82219820Sjeffstatic u_short default_urgent_tcp_ports[] = {
83219820Sjeff  21,	/* ftp */
84219820Sjeff  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
95void
96ncp_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
121void
122ncp_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
141int
142ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
143{
144  int res = 0;
145
146#ifndef NOINET6
147  if (Enabled(bundle, OPT_IPCP)) {
148#endif
149    fsm_Up(&ncp->ipcp.fsm);
150    fsm_Open(&ncp->ipcp.fsm);
151    res++;
152#ifndef NOINET6
153  }
154
155  if (Enabled(bundle, OPT_IPV6CP)) {
156    fsm_Up(&ncp->ipv6cp.fsm);
157    fsm_Open(&ncp->ipv6cp.fsm);
158    res++;
159  }
160#endif
161
162  return res;
163}
164
165void
166ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
167{
168  switch (ncprange_family(&addr->ifa)) {
169  case AF_INET:
170    ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
171    break;
172#ifndef NOINET6
173  case AF_INET6:
174    ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
175    break;
176#endif
177  }
178}
179
180void
181ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
182{
183  if (ncprange_family(&addr->ifa) == AF_INET)
184    ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
185}
186
187void
188ncp_SetLink(struct ncp *ncp, struct link *l)
189{
190  ipcp_SetLink(&ncp->ipcp, l);
191#ifndef NOINET6
192  ipv6cp_SetLink(&ncp->ipv6cp, l);
193#endif
194}
195
196/*
197 * Enqueue a packet of the given address family.  Nothing will make it
198 * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
199 */
200void
201ncp_Enqueue(struct ncp *ncp, int af, unsigned pri, char *ptr, int count)
202{
203#ifndef NOINET6
204  struct ipv6cp *ipv6cp = &ncp->ipv6cp;
205#endif
206  struct ipcp *ipcp = &ncp->ipcp;
207  struct mbuf *bp;
208
209  /*
210   * We allocate an extra 6 bytes, four at the front and two at the end.
211   * This is an optimisation so that we need to do less work in
212   * m_prepend() in acf_LayerPush() and proto_LayerPush() and
213   * appending in hdlc_LayerPush().
214   */
215
216  switch (af) {
217  case AF_INET:
218    if (pri >= IPCP_QUEUES(ipcp)) {
219      log_Printf(LogERROR, "Can't store in ip queue %u\n", pri);
220      break;
221    }
222
223    bp = m_get(count + 6, MB_IPOUT);
224    bp->m_offset += 4;
225    bp->m_len -= 6;
226    memcpy(MBUF_CTOP(bp), ptr, count);
227    m_enqueue(ipcp->Queue + pri, bp);
228    break;
229
230#ifndef NOINET6
231  case AF_INET6:
232    if (pri >= IPV6CP_QUEUES(ipcp)) {
233      log_Printf(LogERROR, "Can't store in ipv6 queue %u\n", pri);
234      break;
235    }
236
237    bp = m_get(count + 6, MB_IPOUT);
238    bp->m_offset += 4;
239    bp->m_len -= 6;
240    memcpy(MBUF_CTOP(bp), ptr, count);
241    m_enqueue(ipv6cp->Queue + pri, bp);
242    break;
243#endif
244
245  default:
246      log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
247  }
248}
249
250/*
251 * How many packets are queued to go out ?
252 */
253size_t
254ncp_QueueLen(struct ncp *ncp)
255{
256  size_t result;
257
258  result = ipcp_QueueLen(&ncp->ipcp);
259#ifndef NOINET6
260  result += ipv6cp_QueueLen(&ncp->ipv6cp);
261#endif
262  result += mp_QueueLen(&ncp->mp);	/* Usually empty */
263
264  return result;
265}
266
267/*
268 * Ditch all queued packets.  This is usually done after our choked timer
269 * has fired - which happens because we couldn't send any traffic over
270 * any links for some time.
271 */
272void
273ncp_DeleteQueues(struct ncp *ncp)
274{
275#ifndef NOINET6
276  struct ipv6cp *ipv6cp = &ncp->ipv6cp;
277#endif
278  struct ipcp *ipcp = &ncp->ipcp;
279  struct mp *mp = &ncp->mp;
280  struct mqueue *q;
281
282  for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
283    while (q->top)
284      m_freem(m_dequeue(q));
285
286#ifndef NOINET6
287  for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
288    while (q->top)
289      m_freem(m_dequeue(q));
290#endif
291
292  link_DeleteQueue(&mp->link);	/* Usually empty anyway */
293}
294
295/*
296 * Arrange that each of our links has at least one packet.  We keep the
297 * number of packets queued at the link level to a minimum so that the
298 * loss of a link in multi-link mode results in the minimum number of
299 * dropped packets.
300 */
301size_t
302ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
303{
304  size_t total;
305
306  if (bundle->ncp.mp.active)
307    total = mp_FillPhysicalQueues(bundle);
308  else {
309    struct datalink *dl;
310    size_t add;
311
312    for (total = 0, dl = bundle->links; dl; dl = dl->next)
313      if (dl->state == DATALINK_OPEN) {
314        add = link_QueueLen(&dl->physical->link);
315        if (add == 0 && dl->physical->out == NULL)
316          add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
317        total += add;
318      }
319  }
320
321  return total + ncp_QueueLen(&bundle->ncp);
322}
323
324/*
325 * Push a packet into the given link.  ``af'' is used as a persistent record
326 * of what is to be pushed next, coming either from mp->out or ncp->afq.
327 */
328int
329ncp_PushPacket(struct ncp *ncp __unused, int *af, struct link *l)
330{
331  struct bundle *bundle = l->lcp.fsm.bundle;
332  int res;
333
334#ifndef NOINET6
335  if (*af == AF_INET) {
336    if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
337      *af = AF_INET6;
338    else
339      res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
340  } else {
341    if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
342      *af = AF_INET;
343    else
344      res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
345  }
346#else
347  res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
348#endif
349
350  return res;
351}
352
353int
354ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
355{
356  unsigned f;
357
358  for (f = 0; f < range->nports; f++)
359    if (range->port[f] == src || range->port[f] == dst)
360      return 1;
361
362  return 0;
363}
364
365void
366ncp_AddUrgentPort(struct port_range *range, u_short port)
367{
368  u_short *newport;
369  unsigned p;
370
371  if (range->nports == range->maxports) {
372    range->maxports += 10;
373    newport = (u_short *)realloc(range->port,
374                                 range->maxports * sizeof(u_short));
375    if (newport == NULL) {
376      log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
377                 strerror(errno));
378      range->maxports -= 10;
379      return;
380    }
381    range->port = newport;
382  }
383
384  for (p = 0; p < range->nports; p++)
385    if (range->port[p] == port) {
386      log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
387      break;
388    } else if (range->port[p] > port) {
389      memmove(range->port + p + 1, range->port + p,
390              (range->nports - p) * sizeof(u_short));
391      range->port[p] = port;
392      range->nports++;
393      break;
394    }
395
396  if (p == range->nports)
397    range->port[range->nports++] = port;
398}
399
400void
401ncp_RemoveUrgentPort(struct port_range *range, u_short port)
402{
403  unsigned p;
404
405  for (p = 0; p < range->nports; p++)
406    if (range->port[p] == port) {
407      if (p + 1 != range->nports)
408        memmove(range->port + p, range->port + p + 1,
409                (range->nports - p - 1) * sizeof(u_short));
410      range->nports--;
411      return;
412    }
413
414  if (p == range->nports)
415    log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
416}
417
418void
419ncp_ClearUrgentPorts(struct port_range *range)
420{
421  range->nports = 0;
422}
423
424int
425ncp_Show(struct cmdargs const *arg)
426{
427  struct ncp *ncp = &arg->bundle->ncp;
428  unsigned p;
429
430#ifndef NOINET6
431  prompt_Printf(arg->prompt, "Next queued AF: %s\n",
432                ncp->afq == AF_INET6 ? "inet6" : "inet");
433#endif
434
435  if (ncp->route) {
436    prompt_Printf(arg->prompt, "\n");
437    route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
438  }
439
440  prompt_Printf(arg->prompt, "\nDefaults:\n");
441  prompt_Printf(arg->prompt, "  sendpipe:      ");
442  if (ncp->cfg.sendpipe > 0)
443    prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
444  else
445    prompt_Printf(arg->prompt, "unspecified\n");
446  prompt_Printf(arg->prompt, "  recvpipe:      ");
447  if (ncp->cfg.recvpipe > 0)
448    prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
449  else
450    prompt_Printf(arg->prompt, "unspecified\n");
451
452  prompt_Printf(arg->prompt, "\n  Urgent ports\n");
453  prompt_Printf(arg->prompt, "         TCP:    ");
454  if (ncp->cfg.urgent.tcp.nports == 0)
455    prompt_Printf(arg->prompt, "none");
456  else
457    for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
458      if (p)
459        prompt_Printf(arg->prompt, ", ");
460      prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
461    }
462
463  prompt_Printf(arg->prompt, "\n         UDP:    ");
464  if (ncp->cfg.urgent.udp.nports == 0)
465    prompt_Printf(arg->prompt, "none");
466  else
467    for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
468      if (p)
469        prompt_Printf(arg->prompt, ", ");
470      prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
471    }
472  prompt_Printf(arg->prompt, "\n         TOS:    %s\n\n",
473                ncp->cfg.urgent.tos ? "yes" : "no");
474
475  return 0;
476}
477
478int
479ncp_LayersOpen(struct ncp *ncp)
480{
481  int n;
482
483  n = !!(ncp->ipcp.fsm.state == ST_OPENED);
484#ifndef NOINET6
485  n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
486#endif
487
488  return n;
489}
490
491int
492ncp_LayersUnfinished(struct ncp *ncp)
493{
494  int n = 0;
495
496  if (ncp->ipcp.fsm.state > ST_CLOSED ||
497      ncp->ipcp.fsm.state == ST_STARTING)
498    n++;
499
500#ifndef NOINET6
501  if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
502      ncp->ipv6cp.fsm.state == ST_STARTING)
503    n++;
504#endif
505
506  return n;
507}
508
509void
510ncp_Close(struct ncp *ncp)
511{
512  if (ncp->ipcp.fsm.state > ST_CLOSED ||
513      ncp->ipcp.fsm.state == ST_STARTING)
514    fsm_Close(&ncp->ipcp.fsm);
515
516#ifndef NOINET6
517  if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
518      ncp->ipv6cp.fsm.state == ST_STARTING)
519    fsm_Close(&ncp->ipv6cp.fsm);
520#endif
521}
522
523void
524ncp2initial(struct ncp *ncp)
525{
526  fsm2initial(&ncp->ipcp.fsm);
527#ifndef NOINET6
528  fsm2initial(&ncp->ipv6cp.fsm);
529#endif
530}
531