1/* 2 * Synchronous PPP/Cisco/Frame Relay link level subroutines. 3 * Keepalive protocol implemented in both Cisco and PPP modes. 4 */ 5/*- 6 * Copyright (C) 1994-2000 Cronyx Engineering. 7 * Author: Serge Vakulenko, <vak@cronyx.ru> 8 * --- 4 unchanged lines hidden (view full) --- 13 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 14 * 15 * Authors grant any other persons or organisations permission to use 16 * or modify this software as long as this message is kept with the software, 17 * all derivative works or modified versions. 18 * 19 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997 20 * |
21 * $FreeBSD: head/sys/net/if_spppsubr.c 241686 2012-10-18 13:57:24Z andre $ |
22 */ 23 24#include <sys/param.h> 25 26#include "opt_inet.h" 27#include "opt_inet6.h" 28#include "opt_ipx.h" 29 --- 756 unchanged lines hidden (view full) --- 786 */ 787static int 788sppp_output(struct ifnet *ifp, struct mbuf *m, 789 struct sockaddr *dst, struct route *ro) 790{ 791 struct sppp *sp = IFP2SP(ifp); 792 struct ppp_header *h; 793 struct ifqueue *ifq = NULL; |
794 int error, rv = 0; |
795#ifdef INET 796 int ipproto = PPP_IP; 797#endif 798 int debug = ifp->if_flags & IFF_DEBUG; 799 |
800 SPPP_LOCK(sp); 801 802 if (!(ifp->if_flags & IFF_UP) || 803 (!(ifp->if_flags & IFF_AUTO) && 804 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) { 805#ifdef INET6 806 drop: 807#endif 808 m_freem (m); 809 SPPP_UNLOCK(sp); |
810 return (ENETDOWN); 811 } 812 813 if ((ifp->if_flags & IFF_AUTO) && 814 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 815#ifdef INET6 816 /* 817 * XXX --- 7 unchanged lines hidden (view full) --- 825 !(sp->confflags & CONF_ENABLE_IPV6)) 826 goto drop; 827#endif 828 /* 829 * Interface is not yet running, but auto-dial. Need 830 * to start LCP for it. 831 */ 832 ifp->if_drv_flags |= IFF_DRV_RUNNING; |
833 lcp.Open(sp); |
834 } 835 836#ifdef INET 837 if (dst->sa_family == AF_INET) { 838 /* XXX Check mbuf length here? */ 839 struct ip *ip = mtod (m, struct ip*); 840 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl); 841 --- 7 unchanged lines hidden (view full) --- 849 * - don't let packets with src ip addr 0 thru 850 * - we flag TCP packets with src ip 0 as an error 851 */ 852 853 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */ 854 { 855 m_freem(m); 856 SPPP_UNLOCK(sp); |
857 if(ip->ip_p == IPPROTO_TCP) 858 return(EADDRNOTAVAIL); 859 else 860 return(0); 861 } 862 863 /* 864 * Put low delay, telnet, rlogin and ftp control packets --- 28 unchanged lines hidden (view full) --- 893 ipproto = PPP_VJ_UCOMP; 894 break; 895 case TYPE_IP: 896 ipproto = PPP_IP; 897 break; 898 default: 899 m_freem(m); 900 SPPP_UNLOCK(sp); |
901 return (EINVAL); 902 } 903 } 904#endif 905 906#ifdef INET6 907 if (dst->sa_family == AF_INET6) { 908 /* XXX do something tricky here? */ --- 13 unchanged lines hidden (view full) --- 922 */ 923 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT); 924 if (! m) { 925nobufs: if (debug) 926 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n", 927 SPP_ARGS(ifp)); 928 ++ifp->if_oerrors; 929 SPPP_UNLOCK(sp); |
930 return (ENOBUFS); 931 } 932 /* 933 * May want to check size of packet 934 * (albeit due to the implementation it's always enough) 935 */ 936 h = mtod (m, struct ppp_header*); 937 if (sp->pp_mode == IFF_CISCO) { --- 50 unchanged lines hidden (view full) --- 988 h->protocol = htons (sp->pp_mode == IFF_CISCO ? 989 ETHERTYPE_IPX : PPP_IPX); 990 break; 991#endif 992 default: 993 m_freem (m); 994 ++ifp->if_oerrors; 995 SPPP_UNLOCK(sp); |
996 return (EAFNOSUPPORT); 997 } 998 999 /* 1000 * Queue message on interface, and start output if interface 1001 * not yet active. 1002 */ 1003out: 1004 if (ifq != NULL) 1005 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3)); 1006 else 1007 IFQ_HANDOFF_ADJ(ifp, m, 3, error); 1008 if (error) { 1009 ++ifp->if_oerrors; 1010 SPPP_UNLOCK(sp); |
1011 return (rv? rv: ENOBUFS); 1012 } 1013 SPPP_UNLOCK(sp); |
1014 /* 1015 * Unlike in sppp_input(), we can always bump the timestamp 1016 * here since sppp_output() is only called on behalf of 1017 * network-layer traffic; control-layer traffic is handled 1018 * by sppp_cp_send(). 1019 */ 1020 sp->pp_last_sent = time_uptime; 1021 return (0); --- 100 unchanged lines hidden (view full) --- 1122 1123/* 1124 * Check if the output queue is empty. 1125 */ 1126int 1127sppp_isempty(struct ifnet *ifp) 1128{ 1129 struct sppp *sp = IFP2SP(ifp); |
1130 int empty; |
1131 |
1132 SPPP_LOCK(sp); 1133 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head && 1134 !SP2IFP(sp)->if_snd.ifq_head; 1135 SPPP_UNLOCK(sp); |
1136 return (empty); 1137} 1138 1139/* 1140 * Get next packet to send. 1141 */ 1142struct mbuf * 1143sppp_dequeue(struct ifnet *ifp) 1144{ 1145 struct sppp *sp = IFP2SP(ifp); 1146 struct mbuf *m; |
1147 |
1148 SPPP_LOCK(sp); 1149 /* 1150 * Process only the control protocol queue until we have at 1151 * least one NCP open. 1152 * 1153 * Do always serve all three queues in Cisco mode. 1154 */ 1155 IF_DEQUEUE(&sp->pp_cpq, m); 1156 if (m == NULL && 1157 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO || 1158 sp->pp_mode == PP_FR)) { 1159 IF_DEQUEUE(&sp->pp_fastq, m); 1160 if (m == NULL) 1161 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m); 1162 } 1163 SPPP_UNLOCK(sp); |
1164 return m; 1165} 1166 1167/* 1168 * Pick the next packet, do not remove it from the queue. 1169 */ 1170struct mbuf * 1171sppp_pick(struct ifnet *ifp) 1172{ 1173 struct sppp *sp = IFP2SP(ifp); 1174 struct mbuf *m; |
1175 |
1176 SPPP_LOCK(sp); 1177 1178 m = sp->pp_cpq.ifq_head; 1179 if (m == NULL && 1180 (sp->pp_phase == PHASE_NETWORK || 1181 sp->pp_mode == IFF_CISCO || 1182 sp->pp_mode == PP_FR)) 1183 if ((m = sp->pp_fastq.ifq_head) == NULL) 1184 m = SP2IFP(sp)->if_snd.ifq_head; 1185 SPPP_UNLOCK(sp); |
1186 return (m); 1187} 1188 1189/* 1190 * Process an ioctl request. Called on low priority level. 1191 */ 1192int 1193sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data) 1194{ 1195 struct ifreq *ifr = (struct ifreq*) data; 1196 struct sppp *sp = IFP2SP(ifp); |
1197 int rv, going_up, going_down, newmode; |
1198 |
1199 SPPP_LOCK(sp); 1200 rv = 0; 1201 switch (cmd) { 1202 case SIOCAIFADDR: 1203 case SIOCSIFDSTADDR: 1204 break; 1205 1206 case SIOCSIFADDR: --- 89 unchanged lines hidden (view full) --- 1296 case SIOCSIFGENERIC: 1297 rv = sppp_params(sp, cmd, data); 1298 break; 1299 1300 default: 1301 rv = ENOTTY; 1302 } 1303 SPPP_UNLOCK(sp); |
1304 return rv; 1305} 1306 1307/* 1308 * Cisco framing implementation. 1309 */ 1310 1311/* --- 733 unchanged lines hidden (view full) --- 2045 break; 2046 } 2047} 2048 2049static void 2050sppp_to_event(const struct cp *cp, struct sppp *sp) 2051{ 2052 STDDCL; |
2053 |
2054 SPPP_LOCK(sp); 2055 if (debug) 2056 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n", 2057 SPP_ARGS(ifp), cp->name, 2058 sppp_state_name(sp->state[cp->protoidx]), 2059 sp->rst_counter[cp->protoidx]); 2060 2061 if (--sp->rst_counter[cp->protoidx] < 0) --- 33 unchanged lines hidden (view full) --- 2095 case STATE_ACK_SENT: 2096 (cp->scr)(sp); 2097 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout, 2098 cp->TO, (void *)sp); 2099 break; 2100 } 2101 2102 SPPP_UNLOCK(sp); |
2103} 2104 2105/* 2106 * Change the state of a control protocol in the state automaton. 2107 * Takes care of starting/stopping the restart timer. 2108 */ 2109static void 2110sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate) --- 1886 unchanged lines hidden (view full) --- 3997/* 3998 * Handle incoming CHAP packets. 3999 */ 4000static void 4001sppp_chap_input(struct sppp *sp, struct mbuf *m) 4002{ 4003 STDDCL; 4004 struct lcp_header *h; |
4005 int len; |
4006 u_char *value, *name, digest[AUTHKEYLEN], dsize; 4007 int value_len, name_len; 4008 MD5_CTX ctx; 4009 4010 len = m->m_pkthdr.len; 4011 if (len < 4) { 4012 if (debug) 4013 log(LOG_DEBUG, --- 60 unchanged lines hidden (view full) --- 4074 log(LOG_DEBUG, SPP_FMT "chap success", 4075 SPP_ARGS(ifp)); 4076 if (len > 4) { 4077 log(-1, ": "); 4078 sppp_print_string((char*)(h + 1), len - 4); 4079 } 4080 log(-1, "\n"); 4081 } |
4082 SPPP_LOCK(sp); 4083 sp->pp_flags &= ~PP_NEEDAUTH; 4084 if (sp->myauth.proto == PPP_CHAP && 4085 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 4086 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) { 4087 /* 4088 * We are authenticator for CHAP but didn't 4089 * complete yet. Leave it to tlu to proceed 4090 * to network phase. 4091 */ 4092 SPPP_UNLOCK(sp); |
4093 break; 4094 } 4095 SPPP_UNLOCK(sp); |
4096 sppp_phase_network(sp); 4097 break; 4098 4099 case CHAP_FAILURE: 4100 if (debug) { 4101 log(LOG_INFO, SPP_FMT "chap failure", 4102 SPP_ARGS(ifp)); 4103 if (len > 4) { --- 145 unchanged lines hidden (view full) --- 4249 sppp_cp_change_state(&chap, sp, STATE_CLOSED); 4250} 4251 4252static void 4253sppp_chap_TO(void *cookie) 4254{ 4255 struct sppp *sp = (struct sppp *)cookie; 4256 STDDCL; |
4257 |
4258 SPPP_LOCK(sp); 4259 if (debug) 4260 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n", 4261 SPP_ARGS(ifp), 4262 sppp_state_name(sp->state[IDX_CHAP]), 4263 sp->rst_counter[IDX_CHAP]); 4264 4265 if (--sp->rst_counter[IDX_CHAP] < 0) --- 14 unchanged lines hidden (view full) --- 4280 case STATE_REQ_SENT: 4281 chap.scr(sp); 4282 /* sppp_cp_change_state() will restart the timer */ 4283 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT); 4284 break; 4285 } 4286 4287 SPPP_UNLOCK(sp); |
4288} 4289 4290static void 4291sppp_chap_tlu(struct sppp *sp) 4292{ 4293 STDDCL; |
4294 int i; |
4295 4296 i = 0; 4297 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 4298 4299 /* 4300 * Some broken CHAP implementations (Conware CoNet, firmware 4301 * 4.0.?) don't want to re-authenticate their CHAP once the 4302 * initial challenge-response exchange has taken place. --- 14 unchanged lines hidden (view full) --- 4317 SPP_ARGS(ifp), 4318 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu"); 4319 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) 4320 log(-1, "next re-challenge in %d seconds\n", i); 4321 else 4322 log(-1, "re-challenging supressed\n"); 4323 } 4324 |
4325 SPPP_LOCK(sp); 4326 /* indicate to LCP that we need to be closed down */ 4327 sp->lcp.protos |= (1 << IDX_CHAP); 4328 4329 if (sp->pp_flags & PP_NEEDAUTH) { 4330 /* 4331 * Remote is authenticator, but his auth proto didn't 4332 * complete yet. Defer the transition to network 4333 * phase. 4334 */ 4335 SPPP_UNLOCK(sp); |
4336 return; 4337 } 4338 SPPP_UNLOCK(sp); |
4339 4340 /* 4341 * If we are already in phase network, we are done here. This 4342 * is the case if this is a dummy tlu event after a re-challenge. 4343 */ 4344 if (sp->pp_phase != PHASE_NETWORK) 4345 sppp_phase_network(sp); 4346} --- 52 unchanged lines hidden (view full) --- 4399 4400/* 4401 * Handle incoming PAP packets. */ 4402static void 4403sppp_pap_input(struct sppp *sp, struct mbuf *m) 4404{ 4405 STDDCL; 4406 struct lcp_header *h; |
4407 int len; |
4408 u_char *name, *passwd, mlen; 4409 int name_len, passwd_len; 4410 4411 len = m->m_pkthdr.len; 4412 if (len < 5) { 4413 if (debug) 4414 log(LOG_DEBUG, 4415 SPP_FMT "pap invalid packet length: %d bytes\n", --- 70 unchanged lines hidden (view full) --- 4486 SPP_ARGS(ifp)); 4487 name_len = *((char *)h); 4488 if (len > 5 && name_len) { 4489 log(-1, ": "); 4490 sppp_print_string((char*)(h+1), name_len); 4491 } 4492 log(-1, "\n"); 4493 } |
4494 SPPP_LOCK(sp); 4495 sp->pp_flags &= ~PP_NEEDAUTH; 4496 if (sp->myauth.proto == PPP_PAP && 4497 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 4498 (sp->lcp.protos & (1 << IDX_PAP)) == 0) { 4499 /* 4500 * We are authenticator for PAP but didn't 4501 * complete yet. Leave it to tlu to proceed 4502 * to network phase. 4503 */ 4504 SPPP_UNLOCK(sp); |
4505 break; 4506 } 4507 SPPP_UNLOCK(sp); |
4508 sppp_phase_network(sp); 4509 break; 4510 4511 case PAP_NAK: 4512 callout_stop (&sp->pap_my_to_ch); 4513 if (debug) { 4514 log(LOG_INFO, SPP_FMT "pap failure", 4515 SPP_ARGS(ifp)); --- 64 unchanged lines hidden (view full) --- 4580 * That's the timeout routine if we are authenticator. Since the 4581 * authenticator is basically passive in PAP, we can't do much here. 4582 */ 4583static void 4584sppp_pap_TO(void *cookie) 4585{ 4586 struct sppp *sp = (struct sppp *)cookie; 4587 STDDCL; |
4588 |
4589 SPPP_LOCK(sp); 4590 if (debug) 4591 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n", 4592 SPP_ARGS(ifp), 4593 sppp_state_name(sp->state[IDX_PAP]), 4594 sp->rst_counter[IDX_PAP]); 4595 4596 if (--sp->rst_counter[IDX_PAP] < 0) --- 9 unchanged lines hidden (view full) --- 4606 switch (sp->state[IDX_PAP]) { 4607 case STATE_REQ_SENT: 4608 /* sppp_cp_change_state() will restart the timer */ 4609 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT); 4610 break; 4611 } 4612 4613 SPPP_UNLOCK(sp); |
4614} 4615 4616/* 4617 * That's the timeout handler if we are peer. Since the peer is active, 4618 * we need to retransmit our PAP request since it is apparently lost. 4619 * XXX We should impose a max counter. 4620 */ 4621static void --- 10 unchanged lines hidden (view full) --- 4632 pap.scr(sp); 4633 SPPP_UNLOCK(sp); 4634} 4635 4636static void 4637sppp_pap_tlu(struct sppp *sp) 4638{ 4639 STDDCL; |
4640 4641 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure; 4642 4643 if (debug) 4644 log(LOG_DEBUG, SPP_FMT "%s tlu\n", 4645 SPP_ARGS(ifp), pap.name); 4646 |
4647 SPPP_LOCK(sp); 4648 /* indicate to LCP that we need to be closed down */ 4649 sp->lcp.protos |= (1 << IDX_PAP); 4650 4651 if (sp->pp_flags & PP_NEEDAUTH) { 4652 /* 4653 * Remote is authenticator, but his auth proto didn't 4654 * complete yet. Defer the transition to network 4655 * phase. 4656 */ 4657 SPPP_UNLOCK(sp); |
4658 return; 4659 } 4660 SPPP_UNLOCK(sp); |
4661 sppp_phase_network(sp); 4662} 4663 4664static void 4665sppp_pap_tld(struct sppp *sp) 4666{ 4667 STDDCL; 4668 --- 121 unchanged lines hidden (view full) --- 4790/* 4791 * Send keepalive packets, every 10 seconds. 4792 */ 4793static void 4794sppp_keepalive(void *dummy) 4795{ 4796 struct sppp *sp = (struct sppp*)dummy; 4797 struct ifnet *ifp = SP2IFP(sp); |
4798 |
4799 SPPP_LOCK(sp); 4800 /* Keepalive mode disabled or channel down? */ 4801 if (! (sp->pp_flags & PP_KEEPALIVE) || 4802 ! (ifp->if_drv_flags & IFF_DRV_RUNNING)) 4803 goto out; 4804 4805 if (sp->pp_mode == PP_FR) { 4806 sppp_fr_keepalive (sp); --- 26 unchanged lines hidden (view full) --- 4833 else if (sp->pp_phase >= PHASE_AUTHENTICATE) { 4834 long nmagic = htonl (sp->lcp.magic); 4835 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP]; 4836 sppp_cp_send (sp, PPP_LCP, ECHO_REQ, 4837 sp->lcp.echoid, 4, &nmagic); 4838 } 4839out: 4840 SPPP_UNLOCK(sp); |
4841 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive, 4842 (void *)sp); 4843} 4844 4845/* 4846 * Get both IP addresses. 4847 */ 4848void --- 33 unchanged lines hidden (view full) --- 4882 if_addr_runlock(ifp); 4883 4884 if (dst) *dst = ntohl(ddst); 4885 if (src) *src = ntohl(ssrc); 4886} 4887 4888#ifdef INET 4889/* |
4890 * Set my IP address. |
4891 */ 4892static void 4893sppp_set_ip_addr(struct sppp *sp, u_long src) 4894{ 4895 STDDCL; 4896 struct ifaddr *ifa; 4897 struct sockaddr_in *si; 4898 struct in_ifaddr *ia; --- 100 unchanged lines hidden (view full) --- 4999 */ 5000static void 5001sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr) 5002{ 5003 /* TBD */ 5004} 5005 5006/* |
5007 * Set my IPv6 address. |
5008 */ 5009static void 5010sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src) 5011{ 5012 STDDCL; 5013 struct ifaddr *ifa; 5014 struct sockaddr_in6 *sin6; 5015 --- 431 unchanged lines hidden --- |