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