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  * $OpenBSD: ncp.c,v 1.4 2005/07/17 19:13:25 brad Exp $
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 __RCSID("$MirOS: src/usr.sbin/ppp/ppp/ncp.c,v 1.3 2005/12/04 15:02:27 tg Exp $");
82 
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 */
91 };
92 
93 static u_short default_urgent_udp_ports[] = { };
94 
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])
99 
100 void
ncp_Init(struct ncp * ncp,struct bundle * bundle)101 ncp_Init(struct ncp *ncp, struct bundle *bundle)
102 {
103   ncp->afq = AF_INET;
104   ncp->route = NULL;
105 
106   ncp->cfg.urgent.tcp.port = (u_short *)malloc(NDEFTCPPORTS * sizeof(u_short));
107   if (ncp->cfg.urgent.tcp.port == NULL) {
108     log_Printf(LogERROR, "ncp_Init: Out of memory allocating urgent ports\n");
109     ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
110   } else {
111     ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = NDEFTCPPORTS;
112     memcpy(ncp->cfg.urgent.tcp.port, default_urgent_tcp_ports,
113 	   NDEFTCPPORTS * sizeof(u_short));
114   }
115   ncp->cfg.urgent.tos = 1;
116 
117   ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = NDEFUDPPORTS;
118   ncp->cfg.urgent.udp.port = (u_short *)malloc(NDEFUDPPORTS * sizeof(u_short));
119   if (NDEFUDPPORTS > 0)
120     memcpy(ncp->cfg.urgent.udp.port, default_urgent_udp_ports,
121          NDEFUDPPORTS * sizeof(u_short));
122 
123 
124   mp_Init(&ncp->mp, bundle);
125 
126   /* Send over the first physical link by default */
127   ipcp_Init(&ncp->ipcp, bundle, &bundle->links->physical->link,
128             &bundle->fsm);
129 #ifndef NOINET6
130   ipv6cp_Init(&ncp->ipv6cp, bundle, &bundle->links->physical->link,
131               &bundle->fsm);
132 #endif
133 }
134 
135 void
ncp_Destroy(struct ncp * ncp)136 ncp_Destroy(struct ncp *ncp)
137 {
138   ipcp_Destroy(&ncp->ipcp);
139 #ifndef NOINET6
140   ipv6cp_Destroy(&ncp->ipv6cp);
141 #endif
142 
143   if (ncp->cfg.urgent.tcp.maxports) {
144     ncp->cfg.urgent.tcp.nports = ncp->cfg.urgent.tcp.maxports = 0;
145     free(ncp->cfg.urgent.tcp.port);
146     ncp->cfg.urgent.tcp.port = NULL;
147   }
148   if (ncp->cfg.urgent.udp.maxports) {
149     ncp->cfg.urgent.udp.nports = ncp->cfg.urgent.udp.maxports = 0;
150     free(ncp->cfg.urgent.udp.port);
151     ncp->cfg.urgent.udp.port = NULL;
152   }
153 }
154 
155 int
ncp_fsmStart(struct ncp * ncp,struct bundle * bundle)156 ncp_fsmStart(struct ncp *ncp, struct bundle *bundle)
157 {
158   int res = 0;
159 
160 #ifndef NOINET6
161   if (Enabled(bundle, OPT_IPCP)) {
162 #endif
163     fsm_Up(&ncp->ipcp.fsm);
164     fsm_Open(&ncp->ipcp.fsm);
165     res++;
166 #ifndef NOINET6
167   }
168 
169   if (Enabled(bundle, OPT_IPV6CP)) {
170     fsm_Up(&ncp->ipv6cp.fsm);
171     fsm_Open(&ncp->ipv6cp.fsm);
172     res++;
173   }
174 #endif
175 
176   return res;
177 }
178 
179 void
ncp_IfaceAddrAdded(struct ncp * ncp,const struct iface_addr * addr)180 ncp_IfaceAddrAdded(struct ncp *ncp, const struct iface_addr *addr)
181 {
182   switch (ncprange_family(&addr->ifa)) {
183   case AF_INET:
184     ipcp_IfaceAddrAdded(&ncp->ipcp, addr);
185     break;
186 #ifndef NOINET6
187   case AF_INET6:
188     ipv6cp_IfaceAddrAdded(&ncp->ipv6cp, addr);
189     break;
190 #endif
191   }
192 }
193 
194 void
ncp_IfaceAddrDeleted(struct ncp * ncp,const struct iface_addr * addr)195 ncp_IfaceAddrDeleted(struct ncp *ncp, const struct iface_addr *addr)
196 {
197   if (ncprange_family(&addr->ifa) == AF_INET)
198     ipcp_IfaceAddrDeleted(&ncp->ipcp, addr);
199 }
200 
201 void
ncp_SetLink(struct ncp * ncp,struct link * l)202 ncp_SetLink(struct ncp *ncp, struct link *l)
203 {
204   ipcp_SetLink(&ncp->ipcp, l);
205 #ifndef NOINET6
206   ipv6cp_SetLink(&ncp->ipv6cp, l);
207 #endif
208 }
209 
210 /*
211  * Enqueue a packet of the given address family.  Nothing will make it
212  * down to the physical link level 'till ncp_FillPhysicalQueues() is used.
213  */
214 void
ncp_Enqueue(struct ncp * ncp,int af,int pri,char * ptr,int count)215 ncp_Enqueue(struct ncp *ncp, int af, int pri, char *ptr, int count)
216 {
217 #ifndef NOINET6
218   struct ipv6cp *ipv6cp = &ncp->ipv6cp;
219 #endif
220   struct ipcp *ipcp = &ncp->ipcp;
221   struct mbuf *bp;
222   u_char *tmp;
223 
224   /*
225    * We allocate an extra 6 bytes, four at the front and two at the end.
226    * This is an optimisation so that we need to do less work in
227    * m_prepend() in acf_LayerPush() and proto_LayerPush() and
228    * appending in hdlc_LayerPush().
229    */
230 
231   switch (af) {
232   case AF_INET:
233     if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) {
234       log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
235       break;
236     }
237 
238     bp = m_get(count + 6, MB_IPOUT);
239     bp->m_offset += 4;
240     bp->m_len -= 6;
241     if ((tmp = MBUF_CTOP(bp)) != NULL)
242       memcpy(tmp, ptr, count);
243     m_enqueue(ipcp->Queue + pri, bp);
244     break;
245 
246 #ifndef NOINET6
247   case AF_INET6:
248     if (pri < 0 || pri >= IPV6CP_QUEUES(ipcp)) {
249       log_Printf(LogERROR, "Can't store in ipv6 queue %d\n", pri);
250       break;
251     }
252 
253     bp = m_get(count + 6, MB_IPOUT);
254     bp->m_offset += 4;
255     bp->m_len -= 6;
256     if ((tmp = MBUF_CTOP(bp)) != NULL)
257       memcpy(tmp, ptr, count);
258     m_enqueue(ipv6cp->Queue + pri, bp);
259     break;
260 #endif
261 
262   default:
263       log_Printf(LogERROR, "Can't enqueue protocol family %d\n", af);
264   }
265 }
266 
267 /*
268  * How many packets are queued to go out ?
269  */
270 size_t
ncp_QueueLen(struct ncp * ncp)271 ncp_QueueLen(struct ncp *ncp)
272 {
273   size_t result;
274 
275   result = ipcp_QueueLen(&ncp->ipcp);
276 #ifndef NOINET6
277   result += ipv6cp_QueueLen(&ncp->ipv6cp);
278 #endif
279   result += mp_QueueLen(&ncp->mp);	/* Usually empty */
280 
281   return result;
282 }
283 
284 /*
285  * Ditch all queued packets.  This is usually done after our choked timer
286  * has fired - which happens because we couldn't send any traffic over
287  * any links for some time.
288  */
289 void
ncp_DeleteQueues(struct ncp * ncp)290 ncp_DeleteQueues(struct ncp *ncp)
291 {
292 #ifndef NOINET6
293   struct ipv6cp *ipv6cp = &ncp->ipv6cp;
294 #endif
295   struct ipcp *ipcp = &ncp->ipcp;
296   struct mp *mp = &ncp->mp;
297   struct mqueue *q;
298 
299   for (q = ipcp->Queue; q < ipcp->Queue + IPCP_QUEUES(ipcp); q++)
300     while (q->top)
301       m_freem(m_dequeue(q));
302 
303 #ifndef NOINET6
304   for (q = ipv6cp->Queue; q < ipv6cp->Queue + IPV6CP_QUEUES(ipv6cp); q++)
305     while (q->top)
306       m_freem(m_dequeue(q));
307 #endif
308 
309   link_DeleteQueue(&mp->link);	/* Usually empty anyway */
310 }
311 
312 /*
313  * Arrange that each of our links has at least one packet.  We keep the
314  * number of packets queued at the link level to a minimum so that the
315  * loss of a link in multi-link mode results in the minimum number of
316  * dropped packets.
317  */
318 size_t
ncp_FillPhysicalQueues(struct ncp * ncp,struct bundle * bundle)319 ncp_FillPhysicalQueues(struct ncp *ncp, struct bundle *bundle)
320 {
321   size_t total;
322 
323   if (bundle->ncp.mp.active)
324     total = mp_FillPhysicalQueues(bundle);
325   else {
326     struct datalink *dl;
327     size_t add;
328 
329     for (total = 0, dl = bundle->links; dl; dl = dl->next)
330       if (dl->state == DATALINK_OPEN) {
331         add = link_QueueLen(&dl->physical->link);
332         if (add == 0 && dl->physical->out == NULL)
333           add = ncp_PushPacket(ncp, &ncp->afq, &dl->physical->link);
334         total += add;
335       }
336   }
337 
338   return total + ncp_QueueLen(&bundle->ncp);
339 }
340 
341 /*
342  * Push a packet into the given link.  ``af'' is used as a persistent record
343  * of what is to be pushed next, coming either from mp->out or ncp->afq.
344  */
345 int
ncp_PushPacket(struct ncp * ncp,int * af,struct link * l)346 ncp_PushPacket(struct ncp *ncp, int *af, struct link *l)
347 {
348   struct bundle *bundle = l->lcp.fsm.bundle;
349   int res;
350 
351 #ifndef NOINET6
352   if (*af == AF_INET) {
353     if ((res = ipcp_PushPacket(&bundle->ncp.ipcp, l)))
354       *af = AF_INET6;
355     else
356       res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l);
357   } else {
358     if ((res = ipv6cp_PushPacket(&bundle->ncp.ipv6cp, l)))
359       *af = AF_INET;
360     else
361       res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
362   }
363 #else
364   res = ipcp_PushPacket(&bundle->ncp.ipcp, l);
365 #endif
366 
367   return res;
368 }
369 
370 int
ncp_IsUrgentPort(struct port_range * range,u_short src,u_short dst)371 ncp_IsUrgentPort(struct port_range *range, u_short src, u_short dst)
372 {
373   int f;
374 
375   for (f = 0; f < range->nports; f++)
376     if (range->port[f] == src || range->port[f] == dst)
377       return 1;
378 
379   return 0;
380 }
381 
382 void
ncp_AddUrgentPort(struct port_range * range,u_short port)383 ncp_AddUrgentPort(struct port_range *range, u_short port)
384 {
385   u_short *newport;
386   int p;
387 
388   if (range->nports == range->maxports) {
389     range->maxports += 10;
390     newport = (u_short *)realloc(range->port,
391                                  range->maxports * sizeof(u_short));
392     if (newport == NULL) {
393       log_Printf(LogERROR, "ncp_AddUrgentPort: realloc: %s\n",
394                  strerror(errno));
395       range->maxports -= 10;
396       return;
397     }
398     range->port = newport;
399   }
400 
401   for (p = 0; p < range->nports; p++)
402     if (range->port[p] == port) {
403       log_Printf(LogWARN, "%u: Port already set to urgent\n", port);
404       break;
405     } else if (range->port[p] > port) {
406       memmove(range->port + p + 1, range->port + p,
407               (range->nports - p) * sizeof(u_short));
408       range->port[p] = port;
409       range->nports++;
410       break;
411     }
412 
413   if (p == range->nports)
414     range->port[range->nports++] = port;
415 }
416 
417 void
ncp_RemoveUrgentPort(struct port_range * range,u_short port)418 ncp_RemoveUrgentPort(struct port_range *range, u_short port)
419 {
420   int p;
421 
422   for (p = 0; p < range->nports; p++)
423     if (range->port[p] == port) {
424       if (p != range->nports - 1)
425         memmove(range->port + p, range->port + p + 1,
426                 (range->nports - p - 1) * sizeof(u_short));
427       range->nports--;
428       return;
429     }
430 
431   if (p == range->nports)
432     log_Printf(LogWARN, "%u: Port not set to urgent\n", port);
433 }
434 
435 void
ncp_ClearUrgentPorts(struct port_range * range)436 ncp_ClearUrgentPorts(struct port_range *range)
437 {
438   range->nports = 0;
439 }
440 
441 int
ncp_Show(struct cmdargs const * arg)442 ncp_Show(struct cmdargs const *arg)
443 {
444   struct ncp *ncp = &arg->bundle->ncp;
445   int p;
446 
447 #ifndef NOINET6
448   prompt_Printf(arg->prompt, "Next queued AF: %s\n",
449                 ncp->afq == AF_INET6 ? "inet6" : "inet");
450 #endif
451 
452   if (ncp->route) {
453     prompt_Printf(arg->prompt, "\n");
454     route_ShowSticky(arg->prompt, ncp->route, "Sticky routes", 1);
455   }
456 
457   prompt_Printf(arg->prompt, "\nDefaults:\n");
458   prompt_Printf(arg->prompt, "  sendpipe:      ");
459   if (ncp->cfg.sendpipe > 0)
460     prompt_Printf(arg->prompt, "%-20ld\n", ncp->cfg.sendpipe);
461   else
462     prompt_Printf(arg->prompt, "unspecified\n");
463   prompt_Printf(arg->prompt, "  recvpipe:      ");
464   if (ncp->cfg.recvpipe > 0)
465     prompt_Printf(arg->prompt, "%ld\n", ncp->cfg.recvpipe);
466   else
467     prompt_Printf(arg->prompt, "unspecified\n");
468 
469   prompt_Printf(arg->prompt, "\n  Urgent ports\n");
470   prompt_Printf(arg->prompt, "         TCP:    ");
471   if (ncp->cfg.urgent.tcp.nports == 0)
472     prompt_Printf(arg->prompt, "none");
473   else
474     for (p = 0; p < ncp->cfg.urgent.tcp.nports; p++) {
475       if (p)
476         prompt_Printf(arg->prompt, ", ");
477       prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.tcp.port[p]);
478     }
479 
480   prompt_Printf(arg->prompt, "\n         UDP:    ");
481   if (ncp->cfg.urgent.udp.nports == 0)
482     prompt_Printf(arg->prompt, "none");
483   else
484     for (p = 0; p < ncp->cfg.urgent.udp.nports; p++) {
485       if (p)
486         prompt_Printf(arg->prompt, ", ");
487       prompt_Printf(arg->prompt, "%u", ncp->cfg.urgent.udp.port[p]);
488     }
489   prompt_Printf(arg->prompt, "\n         TOS:    %s\n\n",
490                 ncp->cfg.urgent.tos ? "yes" : "no");
491 
492   return 0;
493 }
494 
495 int
ncp_LayersOpen(struct ncp * ncp)496 ncp_LayersOpen(struct ncp *ncp)
497 {
498   int n;
499 
500   n = !!(ncp->ipcp.fsm.state == ST_OPENED);
501 #ifndef NOINET6
502   n += !!(ncp->ipv6cp.fsm.state == ST_OPENED);
503 #endif
504 
505   return n;
506 }
507 
508 int
ncp_LayersUnfinished(struct ncp * ncp)509 ncp_LayersUnfinished(struct ncp *ncp)
510 {
511   int n = 0;
512 
513   if (ncp->ipcp.fsm.state > ST_CLOSED ||
514       ncp->ipcp.fsm.state == ST_STARTING)
515     n++;
516 
517 #ifndef NOINET6
518   if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
519       ncp->ipv6cp.fsm.state == ST_STARTING)
520     n++;
521 #endif
522 
523   return n;
524 }
525 
526 void
ncp_Close(struct ncp * ncp)527 ncp_Close(struct ncp *ncp)
528 {
529   if (ncp->ipcp.fsm.state > ST_CLOSED ||
530       ncp->ipcp.fsm.state == ST_STARTING)
531     fsm_Close(&ncp->ipcp.fsm);
532 
533 #ifndef NOINET6
534   if (ncp->ipv6cp.fsm.state > ST_CLOSED ||
535       ncp->ipv6cp.fsm.state == ST_STARTING)
536     fsm_Close(&ncp->ipv6cp.fsm);
537 #endif
538 }
539 
540 void
ncp2initial(struct ncp * ncp)541 ncp2initial(struct ncp *ncp)
542 {
543   fsm2initial(&ncp->ipcp.fsm);
544 #ifndef NOINET6
545   fsm2initial(&ncp->ipv6cp.fsm);
546 #endif
547 }
548