ppp_generic.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  1. /*
  2. * Generic PPP layer for Linux.
  3. *
  4. * Copyright 1999-2002 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * The generic PPP layer handles the PPP network interfaces, the
  12. * /dev/ppp device, packet and VJ compression, and multilink.
  13. * It talks to PPP `channels' via the interface defined in
  14. * include/linux/ppp_channel.h. Channels provide the basic means for
  15. * sending and receiving PPP frames on some kind of communications
  16. * channel.
  17. *
  18. * Part of the code in this driver was inspired by the old async-only
  19. * PPP driver, written by Michael Callahan and Al Longyear, and
  20. * subsequently hacked by Paul Mackerras.
  21. *
  22. * ==FILEVERSION 20041108==
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kmod.h>
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/idr.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/poll.h>
  32. #include <linux/ppp_defs.h>
  33. #include <linux/filter.h>
  34. #include <linux/if_ppp.h>
  35. #include <linux/ppp_channel.h>
  36. #include <linux/ppp-comp.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/rtnetlink.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/ip.h>
  41. #include <linux/tcp.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/rwsem.h>
  45. #include <linux/stddef.h>
  46. #include <linux/device.h>
  47. #include <linux/mutex.h>
  48. #include <net/slhc_vj.h>
  49. #include <asm/atomic.h>
  50. #define PPP_VERSION "2.4.2"
  51. /*
  52. * Network protocols we support.
  53. */
  54. #define NP_IP 0 /* Internet Protocol V4 */
  55. #define NP_IPV6 1 /* Internet Protocol V6 */
  56. #define NP_IPX 2 /* IPX protocol */
  57. #define NP_AT 3 /* Appletalk protocol */
  58. #define NP_MPLS_UC 4 /* MPLS unicast */
  59. #define NP_MPLS_MC 5 /* MPLS multicast */
  60. #define NUM_NP 6 /* Number of NPs. */
  61. #define MPHDRLEN 6 /* multilink protocol header length */
  62. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  63. #define MIN_FRAG_SIZE 64
  64. /*
  65. * An instance of /dev/ppp can be associated with either a ppp
  66. * interface unit or a ppp channel. In both cases, file->private_data
  67. * points to one of these.
  68. */
  69. struct ppp_file {
  70. enum {
  71. INTERFACE=1, CHANNEL
  72. } kind;
  73. struct sk_buff_head xq; /* pppd transmit queue */
  74. struct sk_buff_head rq; /* receive queue for pppd */
  75. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  76. atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
  77. int hdrlen; /* space to leave for headers */
  78. int index; /* interface unit / channel number */
  79. int dead; /* unit/channel has been shut down */
  80. };
  81. #define PF_TO_X(pf, X) container_of(pf, X, file)
  82. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  83. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  84. /*
  85. * Data structure describing one ppp unit.
  86. * A ppp unit corresponds to a ppp network interface device
  87. * and represents a multilink bundle.
  88. * It can have 0 or more ppp channels connected to it.
  89. */
  90. struct ppp {
  91. struct ppp_file file; /* stuff for read/write/poll 0 */
  92. struct file *owner; /* file that owns this unit 48 */
  93. struct list_head channels; /* list of attached channels 4c */
  94. int n_channels; /* how many channels are attached 54 */
  95. spinlock_t rlock; /* lock for receive side 58 */
  96. spinlock_t wlock; /* lock for transmit side 5c */
  97. int mru; /* max receive unit 60 */
  98. unsigned int flags; /* control bits 64 */
  99. unsigned int xstate; /* transmit state bits 68 */
  100. unsigned int rstate; /* receive state bits 6c */
  101. int debug; /* debug flags 70 */
  102. struct slcompress *vj; /* state for VJ header compression */
  103. enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
  104. struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
  105. struct compressor *xcomp; /* transmit packet compressor 8c */
  106. void *xc_state; /* its internal state 90 */
  107. struct compressor *rcomp; /* receive decompressor 94 */
  108. void *rc_state; /* its internal state 98 */
  109. unsigned long last_xmit; /* jiffies when last pkt sent 9c */
  110. unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
  111. struct net_device *dev; /* network interface device a4 */
  112. #ifdef CONFIG_PPP_MULTILINK
  113. int nxchan; /* next channel to send something on */
  114. u32 nxseq; /* next sequence number to send */
  115. int mrru; /* MP: max reconst. receive unit */
  116. u32 nextseq; /* MP: seq no of next packet */
  117. u32 minseq; /* MP: min of most recent seqnos */
  118. struct sk_buff_head mrq; /* MP: receive reconstruction queue */
  119. #endif /* CONFIG_PPP_MULTILINK */
  120. #ifdef CONFIG_PPP_FILTER
  121. struct sock_filter *pass_filter; /* filter for packets to pass */
  122. struct sock_filter *active_filter;/* filter for pkts to reset idle */
  123. unsigned pass_len, active_len;
  124. #endif /* CONFIG_PPP_FILTER */
  125. };
  126. /*
  127. * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
  128. * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
  129. * SC_MUST_COMP
  130. * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
  131. * Bits in xstate: SC_COMP_RUN
  132. */
  133. #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
  134. |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
  135. |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
  136. /*
  137. * Private data structure for each channel.
  138. * This includes the data structure used for multilink.
  139. */
  140. struct channel {
  141. struct ppp_file file; /* stuff for read/write/poll */
  142. struct list_head list; /* link in all/new_channels list */
  143. struct ppp_channel *chan; /* public channel data structure */
  144. struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
  145. spinlock_t downl; /* protects `chan', file.xq dequeue */
  146. struct ppp *ppp; /* ppp unit we're connected to */
  147. struct list_head clist; /* link in list of channels per unit */
  148. rwlock_t upl; /* protects `ppp' */
  149. #ifdef CONFIG_PPP_MULTILINK
  150. u8 avail; /* flag used in multilink stuff */
  151. u8 had_frag; /* >= 1 fragments have been sent */
  152. u32 lastseq; /* MP: last sequence # received */
  153. #endif /* CONFIG_PPP_MULTILINK */
  154. };
  155. /*
  156. * SMP locking issues:
  157. * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
  158. * list and the ppp.n_channels field, you need to take both locks
  159. * before you modify them.
  160. * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
  161. * channel.downl.
  162. */
  163. /*
  164. * all_ppp_mutex protects the all_ppp_units mapping.
  165. * It also ensures that finding a ppp unit in the all_ppp_units map
  166. * and updating its file.refcnt field is atomic.
  167. */
  168. static DEFINE_MUTEX(all_ppp_mutex);
  169. static atomic_t ppp_unit_count = ATOMIC_INIT(0);
  170. static DEFINE_IDR(ppp_units_idr);
  171. /*
  172. * all_channels_lock protects all_channels and last_channel_index,
  173. * and the atomicity of find a channel and updating its file.refcnt
  174. * field.
  175. */
  176. static DEFINE_SPINLOCK(all_channels_lock);
  177. static LIST_HEAD(all_channels);
  178. static LIST_HEAD(new_channels);
  179. static int last_channel_index;
  180. static atomic_t channel_count = ATOMIC_INIT(0);
  181. /* Get the PPP protocol number from a skb */
  182. #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
  183. /* We limit the length of ppp->file.rq to this (arbitrary) value */
  184. #define PPP_MAX_RQLEN 32
  185. /*
  186. * Maximum number of multilink fragments queued up.
  187. * This has to be large enough to cope with the maximum latency of
  188. * the slowest channel relative to the others. Strictly it should
  189. * depend on the number of channels and their characteristics.
  190. */
  191. #define PPP_MP_MAX_QLEN 128
  192. /* Multilink header bits. */
  193. #define B 0x80 /* this fragment begins a packet */
  194. #define E 0x40 /* this fragment ends a packet */
  195. /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
  196. #define seq_before(a, b) ((s32)((a) - (b)) < 0)
  197. #define seq_after(a, b) ((s32)((a) - (b)) > 0)
  198. /* Prototypes. */
  199. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  200. unsigned int cmd, unsigned long arg);
  201. static void ppp_xmit_process(struct ppp *ppp);
  202. static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
  203. static void ppp_push(struct ppp *ppp);
  204. static void ppp_channel_push(struct channel *pch);
  205. static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
  206. struct channel *pch);
  207. static void ppp_receive_error(struct ppp *ppp);
  208. static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
  209. static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
  210. struct sk_buff *skb);
  211. #ifdef CONFIG_PPP_MULTILINK
  212. static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
  213. struct channel *pch);
  214. static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
  215. static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
  216. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
  217. #endif /* CONFIG_PPP_MULTILINK */
  218. static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
  219. static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
  220. static void ppp_ccp_closed(struct ppp *ppp);
  221. static struct compressor *find_compressor(int type);
  222. static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
  223. static struct ppp *ppp_create_interface(int unit, int *retp);
  224. static void init_ppp_file(struct ppp_file *pf, int kind);
  225. static void ppp_shutdown_interface(struct ppp *ppp);
  226. static void ppp_destroy_interface(struct ppp *ppp);
  227. static struct ppp *ppp_find_unit(int unit);
  228. static struct channel *ppp_find_channel(int unit);
  229. static int ppp_connect_channel(struct channel *pch, int unit);
  230. static int ppp_disconnect_channel(struct channel *pch);
  231. static void ppp_destroy_channel(struct channel *pch);
  232. static int unit_get(struct idr *p, void *ptr);
  233. static void unit_put(struct idr *p, int n);
  234. static void *unit_find(struct idr *p, int n);
  235. static struct class *ppp_class;
  236. /* Translates a PPP protocol number to a NP index (NP == network protocol) */
  237. static inline int proto_to_npindex(int proto)
  238. {
  239. switch (proto) {
  240. case PPP_IP:
  241. return NP_IP;
  242. case PPP_IPV6:
  243. return NP_IPV6;
  244. case PPP_IPX:
  245. return NP_IPX;
  246. case PPP_AT:
  247. return NP_AT;
  248. case PPP_MPLS_UC:
  249. return NP_MPLS_UC;
  250. case PPP_MPLS_MC:
  251. return NP_MPLS_MC;
  252. }
  253. return -EINVAL;
  254. }
  255. /* Translates an NP index into a PPP protocol number */
  256. static const int npindex_to_proto[NUM_NP] = {
  257. PPP_IP,
  258. PPP_IPV6,
  259. PPP_IPX,
  260. PPP_AT,
  261. PPP_MPLS_UC,
  262. PPP_MPLS_MC,
  263. };
  264. /* Translates an ethertype into an NP index */
  265. static inline int ethertype_to_npindex(int ethertype)
  266. {
  267. switch (ethertype) {
  268. case ETH_P_IP:
  269. return NP_IP;
  270. case ETH_P_IPV6:
  271. return NP_IPV6;
  272. case ETH_P_IPX:
  273. return NP_IPX;
  274. case ETH_P_PPPTALK:
  275. case ETH_P_ATALK:
  276. return NP_AT;
  277. case ETH_P_MPLS_UC:
  278. return NP_MPLS_UC;
  279. case ETH_P_MPLS_MC:
  280. return NP_MPLS_MC;
  281. }
  282. return -1;
  283. }
  284. /* Translates an NP index into an ethertype */
  285. static const int npindex_to_ethertype[NUM_NP] = {
  286. ETH_P_IP,
  287. ETH_P_IPV6,
  288. ETH_P_IPX,
  289. ETH_P_PPPTALK,
  290. ETH_P_MPLS_UC,
  291. ETH_P_MPLS_MC,
  292. };
  293. /*
  294. * Locking shorthand.
  295. */
  296. #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
  297. #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
  298. #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
  299. #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
  300. #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
  301. ppp_recv_lock(ppp); } while (0)
  302. #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
  303. ppp_xmit_unlock(ppp); } while (0)
  304. /*
  305. * /dev/ppp device routines.
  306. * The /dev/ppp device is used by pppd to control the ppp unit.
  307. * It supports the read, write, ioctl and poll functions.
  308. * Open instances of /dev/ppp can be in one of three states:
  309. * unattached, attached to a ppp unit, or attached to a ppp channel.
  310. */
  311. static int ppp_open(struct inode *inode, struct file *file)
  312. {
  313. cycle_kernel_lock();
  314. /*
  315. * This could (should?) be enforced by the permissions on /dev/ppp.
  316. */
  317. if (!capable(CAP_NET_ADMIN))
  318. return -EPERM;
  319. return 0;
  320. }
  321. static int ppp_release(struct inode *unused, struct file *file)
  322. {
  323. struct ppp_file *pf = file->private_data;
  324. struct ppp *ppp;
  325. if (pf) {
  326. file->private_data = NULL;
  327. if (pf->kind == INTERFACE) {
  328. ppp = PF_TO_PPP(pf);
  329. if (file == ppp->owner)
  330. ppp_shutdown_interface(ppp);
  331. }
  332. if (atomic_dec_and_test(&pf->refcnt)) {
  333. switch (pf->kind) {
  334. case INTERFACE:
  335. ppp_destroy_interface(PF_TO_PPP(pf));
  336. break;
  337. case CHANNEL:
  338. ppp_destroy_channel(PF_TO_CHANNEL(pf));
  339. break;
  340. }
  341. }
  342. }
  343. return 0;
  344. }
  345. static ssize_t ppp_read(struct file *file, char __user *buf,
  346. size_t count, loff_t *ppos)
  347. {
  348. struct ppp_file *pf = file->private_data;
  349. DECLARE_WAITQUEUE(wait, current);
  350. ssize_t ret;
  351. struct sk_buff *skb = NULL;
  352. ret = count;
  353. if (!pf)
  354. return -ENXIO;
  355. add_wait_queue(&pf->rwait, &wait);
  356. for (;;) {
  357. set_current_state(TASK_INTERRUPTIBLE);
  358. skb = skb_dequeue(&pf->rq);
  359. if (skb)
  360. break;
  361. ret = 0;
  362. if (pf->dead)
  363. break;
  364. if (pf->kind == INTERFACE) {
  365. /*
  366. * Return 0 (EOF) on an interface that has no
  367. * channels connected, unless it is looping
  368. * network traffic (demand mode).
  369. */
  370. struct ppp *ppp = PF_TO_PPP(pf);
  371. if (ppp->n_channels == 0
  372. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  373. break;
  374. }
  375. ret = -EAGAIN;
  376. if (file->f_flags & O_NONBLOCK)
  377. break;
  378. ret = -ERESTARTSYS;
  379. if (signal_pending(current))
  380. break;
  381. schedule();
  382. }
  383. set_current_state(TASK_RUNNING);
  384. remove_wait_queue(&pf->rwait, &wait);
  385. if (!skb)
  386. goto out;
  387. ret = -EOVERFLOW;
  388. if (skb->len > count)
  389. goto outf;
  390. ret = -EFAULT;
  391. if (copy_to_user(buf, skb->data, skb->len))
  392. goto outf;
  393. ret = skb->len;
  394. outf:
  395. kfree_skb(skb);
  396. out:
  397. return ret;
  398. }
  399. static ssize_t ppp_write(struct file *file, const char __user *buf,
  400. size_t count, loff_t *ppos)
  401. {
  402. struct ppp_file *pf = file->private_data;
  403. struct sk_buff *skb;
  404. ssize_t ret;
  405. if (!pf)
  406. return -ENXIO;
  407. ret = -ENOMEM;
  408. skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
  409. if (!skb)
  410. goto out;
  411. skb_reserve(skb, pf->hdrlen);
  412. ret = -EFAULT;
  413. if (copy_from_user(skb_put(skb, count), buf, count)) {
  414. kfree_skb(skb);
  415. goto out;
  416. }
  417. skb_queue_tail(&pf->xq, skb);
  418. switch (pf->kind) {
  419. case INTERFACE:
  420. ppp_xmit_process(PF_TO_PPP(pf));
  421. break;
  422. case CHANNEL:
  423. ppp_channel_push(PF_TO_CHANNEL(pf));
  424. break;
  425. }
  426. ret = count;
  427. out:
  428. return ret;
  429. }
  430. /* No kernel lock - fine */
  431. static unsigned int ppp_poll(struct file *file, poll_table *wait)
  432. {
  433. struct ppp_file *pf = file->private_data;
  434. unsigned int mask;
  435. if (!pf)
  436. return 0;
  437. poll_wait(file, &pf->rwait, wait);
  438. mask = POLLOUT | POLLWRNORM;
  439. if (skb_peek(&pf->rq))
  440. mask |= POLLIN | POLLRDNORM;
  441. if (pf->dead)
  442. mask |= POLLHUP;
  443. else if (pf->kind == INTERFACE) {
  444. /* see comment in ppp_read */
  445. struct ppp *ppp = PF_TO_PPP(pf);
  446. if (ppp->n_channels == 0
  447. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  448. mask |= POLLIN | POLLRDNORM;
  449. }
  450. return mask;
  451. }
  452. #ifdef CONFIG_PPP_FILTER
  453. static int get_filter(void __user *arg, struct sock_filter **p)
  454. {
  455. struct sock_fprog uprog;
  456. struct sock_filter *code = NULL;
  457. int len, err;
  458. if (copy_from_user(&uprog, arg, sizeof(uprog)))
  459. return -EFAULT;
  460. if (!uprog.len) {
  461. *p = NULL;
  462. return 0;
  463. }
  464. len = uprog.len * sizeof(struct sock_filter);
  465. code = kmalloc(len, GFP_KERNEL);
  466. if (code == NULL)
  467. return -ENOMEM;
  468. if (copy_from_user(code, uprog.filter, len)) {
  469. kfree(code);
  470. return -EFAULT;
  471. }
  472. err = sk_chk_filter(code, uprog.len);
  473. if (err) {
  474. kfree(code);
  475. return err;
  476. }
  477. *p = code;
  478. return uprog.len;
  479. }
  480. #endif /* CONFIG_PPP_FILTER */
  481. static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  482. {
  483. struct ppp_file *pf = file->private_data;
  484. struct ppp *ppp;
  485. int err = -EFAULT, val, val2, i;
  486. struct ppp_idle idle;
  487. struct npioctl npi;
  488. int unit, cflags;
  489. struct slcompress *vj;
  490. void __user *argp = (void __user *)arg;
  491. int __user *p = argp;
  492. if (!pf)
  493. return ppp_unattached_ioctl(pf, file, cmd, arg);
  494. if (cmd == PPPIOCDETACH) {
  495. /*
  496. * We have to be careful here... if the file descriptor
  497. * has been dup'd, we could have another process in the
  498. * middle of a poll using the same file *, so we had
  499. * better not free the interface data structures -
  500. * instead we fail the ioctl. Even in this case, we
  501. * shut down the interface if we are the owner of it.
  502. * Actually, we should get rid of PPPIOCDETACH, userland
  503. * (i.e. pppd) could achieve the same effect by closing
  504. * this fd and reopening /dev/ppp.
  505. */
  506. err = -EINVAL;
  507. lock_kernel();
  508. if (pf->kind == INTERFACE) {
  509. ppp = PF_TO_PPP(pf);
  510. if (file == ppp->owner)
  511. ppp_shutdown_interface(ppp);
  512. }
  513. if (atomic_long_read(&file->f_count) <= 2) {
  514. ppp_release(NULL, file);
  515. err = 0;
  516. } else
  517. printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%ld\n",
  518. atomic_long_read(&file->f_count));
  519. unlock_kernel();
  520. return err;
  521. }
  522. if (pf->kind == CHANNEL) {
  523. struct channel *pch;
  524. struct ppp_channel *chan;
  525. lock_kernel();
  526. pch = PF_TO_CHANNEL(pf);
  527. switch (cmd) {
  528. case PPPIOCCONNECT:
  529. if (get_user(unit, p))
  530. break;
  531. err = ppp_connect_channel(pch, unit);
  532. break;
  533. case PPPIOCDISCONN:
  534. err = ppp_disconnect_channel(pch);
  535. break;
  536. default:
  537. down_read(&pch->chan_sem);
  538. chan = pch->chan;
  539. err = -ENOTTY;
  540. if (chan && chan->ops->ioctl)
  541. err = chan->ops->ioctl(chan, cmd, arg);
  542. up_read(&pch->chan_sem);
  543. }
  544. unlock_kernel();
  545. return err;
  546. }
  547. if (pf->kind != INTERFACE) {
  548. /* can't happen */
  549. printk(KERN_ERR "PPP: not interface or channel??\n");
  550. return -EINVAL;
  551. }
  552. lock_kernel();
  553. ppp = PF_TO_PPP(pf);
  554. switch (cmd) {
  555. case PPPIOCSMRU:
  556. if (get_user(val, p))
  557. break;
  558. ppp->mru = val;
  559. err = 0;
  560. break;
  561. case PPPIOCSFLAGS:
  562. if (get_user(val, p))
  563. break;
  564. ppp_lock(ppp);
  565. cflags = ppp->flags & ~val;
  566. ppp->flags = val & SC_FLAG_BITS;
  567. ppp_unlock(ppp);
  568. if (cflags & SC_CCP_OPEN)
  569. ppp_ccp_closed(ppp);
  570. err = 0;
  571. break;
  572. case PPPIOCGFLAGS:
  573. val = ppp->flags | ppp->xstate | ppp->rstate;
  574. if (put_user(val, p))
  575. break;
  576. err = 0;
  577. break;
  578. case PPPIOCSCOMPRESS:
  579. err = ppp_set_compress(ppp, arg);
  580. break;
  581. case PPPIOCGUNIT:
  582. if (put_user(ppp->file.index, p))
  583. break;
  584. err = 0;
  585. break;
  586. case PPPIOCSDEBUG:
  587. if (get_user(val, p))
  588. break;
  589. ppp->debug = val;
  590. err = 0;
  591. break;
  592. case PPPIOCGDEBUG:
  593. if (put_user(ppp->debug, p))
  594. break;
  595. err = 0;
  596. break;
  597. case PPPIOCGIDLE:
  598. idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  599. idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
  600. if (copy_to_user(argp, &idle, sizeof(idle)))
  601. break;
  602. err = 0;
  603. break;
  604. case PPPIOCSMAXCID:
  605. if (get_user(val, p))
  606. break;
  607. val2 = 15;
  608. if ((val >> 16) != 0) {
  609. val2 = val >> 16;
  610. val &= 0xffff;
  611. }
  612. vj = slhc_init(val2+1, val+1);
  613. if (!vj) {
  614. printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
  615. err = -ENOMEM;
  616. break;
  617. }
  618. ppp_lock(ppp);
  619. if (ppp->vj)
  620. slhc_free(ppp->vj);
  621. ppp->vj = vj;
  622. ppp_unlock(ppp);
  623. err = 0;
  624. break;
  625. case PPPIOCGNPMODE:
  626. case PPPIOCSNPMODE:
  627. if (copy_from_user(&npi, argp, sizeof(npi)))
  628. break;
  629. err = proto_to_npindex(npi.protocol);
  630. if (err < 0)
  631. break;
  632. i = err;
  633. if (cmd == PPPIOCGNPMODE) {
  634. err = -EFAULT;
  635. npi.mode = ppp->npmode[i];
  636. if (copy_to_user(argp, &npi, sizeof(npi)))
  637. break;
  638. } else {
  639. ppp->npmode[i] = npi.mode;
  640. /* we may be able to transmit more packets now (??) */
  641. netif_wake_queue(ppp->dev);
  642. }
  643. err = 0;
  644. break;
  645. #ifdef CONFIG_PPP_FILTER
  646. case PPPIOCSPASS:
  647. {
  648. struct sock_filter *code;
  649. err = get_filter(argp, &code);
  650. if (err >= 0) {
  651. ppp_lock(ppp);
  652. kfree(ppp->pass_filter);
  653. ppp->pass_filter = code;
  654. ppp->pass_len = err;
  655. ppp_unlock(ppp);
  656. err = 0;
  657. }
  658. break;
  659. }
  660. case PPPIOCSACTIVE:
  661. {
  662. struct sock_filter *code;
  663. err = get_filter(argp, &code);
  664. if (err >= 0) {
  665. ppp_lock(ppp);
  666. kfree(ppp->active_filter);
  667. ppp->active_filter = code;
  668. ppp->active_len = err;
  669. ppp_unlock(ppp);
  670. err = 0;
  671. }
  672. break;
  673. }
  674. #endif /* CONFIG_PPP_FILTER */
  675. #ifdef CONFIG_PPP_MULTILINK
  676. case PPPIOCSMRRU:
  677. if (get_user(val, p))
  678. break;
  679. ppp_recv_lock(ppp);
  680. ppp->mrru = val;
  681. ppp_recv_unlock(ppp);
  682. err = 0;
  683. break;
  684. #endif /* CONFIG_PPP_MULTILINK */
  685. default:
  686. err = -ENOTTY;
  687. }
  688. unlock_kernel();
  689. return err;
  690. }
  691. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  692. unsigned int cmd, unsigned long arg)
  693. {
  694. int unit, err = -EFAULT;
  695. struct ppp *ppp;
  696. struct channel *chan;
  697. int __user *p = (int __user *)arg;
  698. lock_kernel();
  699. switch (cmd) {
  700. case PPPIOCNEWUNIT:
  701. /* Create a new ppp unit */
  702. if (get_user(unit, p))
  703. break;
  704. ppp = ppp_create_interface(unit, &err);
  705. if (!ppp)
  706. break;
  707. file->private_data = &ppp->file;
  708. ppp->owner = file;
  709. err = -EFAULT;
  710. if (put_user(ppp->file.index, p))
  711. break;
  712. err = 0;
  713. break;
  714. case PPPIOCATTACH:
  715. /* Attach to an existing ppp unit */
  716. if (get_user(unit, p))
  717. break;
  718. mutex_lock(&all_ppp_mutex);
  719. err = -ENXIO;
  720. ppp = ppp_find_unit(unit);
  721. if (ppp) {
  722. atomic_inc(&ppp->file.refcnt);
  723. file->private_data = &ppp->file;
  724. err = 0;
  725. }
  726. mutex_unlock(&all_ppp_mutex);
  727. break;
  728. case PPPIOCATTCHAN:
  729. if (get_user(unit, p))
  730. break;
  731. spin_lock_bh(&all_channels_lock);
  732. err = -ENXIO;
  733. chan = ppp_find_channel(unit);
  734. if (chan) {
  735. atomic_inc(&chan->file.refcnt);
  736. file->private_data = &chan->file;
  737. err = 0;
  738. }
  739. spin_unlock_bh(&all_channels_lock);
  740. break;
  741. default:
  742. err = -ENOTTY;
  743. }
  744. unlock_kernel();
  745. return err;
  746. }
  747. static const struct file_operations ppp_device_fops = {
  748. .owner = THIS_MODULE,
  749. .read = ppp_read,
  750. .write = ppp_write,
  751. .poll = ppp_poll,
  752. .unlocked_ioctl = ppp_ioctl,
  753. .open = ppp_open,
  754. .release = ppp_release
  755. };
  756. #define PPP_MAJOR 108
  757. /* Called at boot time if ppp is compiled into the kernel,
  758. or at module load time (from init_module) if compiled as a module. */
  759. static int __init ppp_init(void)
  760. {
  761. int err;
  762. printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
  763. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  764. if (!err) {
  765. ppp_class = class_create(THIS_MODULE, "ppp");
  766. if (IS_ERR(ppp_class)) {
  767. err = PTR_ERR(ppp_class);
  768. goto out_chrdev;
  769. }
  770. device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL,
  771. "ppp");
  772. }
  773. out:
  774. if (err)
  775. printk(KERN_ERR "failed to register PPP device (%d)\n", err);
  776. return err;
  777. out_chrdev:
  778. unregister_chrdev(PPP_MAJOR, "ppp");
  779. goto out;
  780. }
  781. /*
  782. * Network interface unit routines.
  783. */
  784. static int
  785. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  786. {
  787. struct ppp *ppp = netdev_priv(dev);
  788. int npi, proto;
  789. unsigned char *pp;
  790. npi = ethertype_to_npindex(ntohs(skb->protocol));
  791. if (npi < 0)
  792. goto outf;
  793. /* Drop, accept or reject the packet */
  794. switch (ppp->npmode[npi]) {
  795. case NPMODE_PASS:
  796. break;
  797. case NPMODE_QUEUE:
  798. /* it would be nice to have a way to tell the network
  799. system to queue this one up for later. */
  800. goto outf;
  801. case NPMODE_DROP:
  802. case NPMODE_ERROR:
  803. goto outf;
  804. }
  805. /* Put the 2-byte PPP protocol number on the front,
  806. making sure there is room for the address and control fields. */
  807. if (skb_cow_head(skb, PPP_HDRLEN))
  808. goto outf;
  809. pp = skb_push(skb, 2);
  810. proto = npindex_to_proto[npi];
  811. pp[0] = proto >> 8;
  812. pp[1] = proto;
  813. netif_stop_queue(dev);
  814. skb_queue_tail(&ppp->file.xq, skb);
  815. ppp_xmit_process(ppp);
  816. return 0;
  817. outf:
  818. kfree_skb(skb);
  819. ++ppp->dev->stats.tx_dropped;
  820. return 0;
  821. }
  822. static int
  823. ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  824. {
  825. struct ppp *ppp = netdev_priv(dev);
  826. int err = -EFAULT;
  827. void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
  828. struct ppp_stats stats;
  829. struct ppp_comp_stats cstats;
  830. char *vers;
  831. switch (cmd) {
  832. case SIOCGPPPSTATS:
  833. ppp_get_stats(ppp, &stats);
  834. if (copy_to_user(addr, &stats, sizeof(stats)))
  835. break;
  836. err = 0;
  837. break;
  838. case SIOCGPPPCSTATS:
  839. memset(&cstats, 0, sizeof(cstats));
  840. if (ppp->xc_state)
  841. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  842. if (ppp->rc_state)
  843. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  844. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  845. break;
  846. err = 0;
  847. break;
  848. case SIOCGPPPVER:
  849. vers = PPP_VERSION;
  850. if (copy_to_user(addr, vers, strlen(vers) + 1))
  851. break;
  852. err = 0;
  853. break;
  854. default:
  855. err = -EINVAL;
  856. }
  857. return err;
  858. }
  859. static const struct net_device_ops ppp_netdev_ops = {
  860. .ndo_start_xmit = ppp_start_xmit,
  861. .ndo_do_ioctl = ppp_net_ioctl,
  862. };
  863. static void ppp_setup(struct net_device *dev)
  864. {
  865. dev->netdev_ops = &ppp_netdev_ops;
  866. dev->hard_header_len = PPP_HDRLEN;
  867. dev->mtu = PPP_MTU;
  868. dev->addr_len = 0;
  869. dev->tx_queue_len = 3;
  870. dev->type = ARPHRD_PPP;
  871. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  872. }
  873. /*
  874. * Transmit-side routines.
  875. */
  876. /*
  877. * Called to do any work queued up on the transmit side
  878. * that can now be done.
  879. */
  880. static void
  881. ppp_xmit_process(struct ppp *ppp)
  882. {
  883. struct sk_buff *skb;
  884. ppp_xmit_lock(ppp);
  885. if (ppp->dev) {
  886. ppp_push(ppp);
  887. while (!ppp->xmit_pending
  888. && (skb = skb_dequeue(&ppp->file.xq)))
  889. ppp_send_frame(ppp, skb);
  890. /* If there's no work left to do, tell the core net
  891. code that we can accept some more. */
  892. if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
  893. netif_wake_queue(ppp->dev);
  894. }
  895. ppp_xmit_unlock(ppp);
  896. }
  897. static inline struct sk_buff *
  898. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  899. {
  900. struct sk_buff *new_skb;
  901. int len;
  902. int new_skb_size = ppp->dev->mtu +
  903. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  904. int compressor_skb_size = ppp->dev->mtu +
  905. ppp->xcomp->comp_extra + PPP_HDRLEN;
  906. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  907. if (!new_skb) {
  908. if (net_ratelimit())
  909. printk(KERN_ERR "PPP: no memory (comp pkt)\n");
  910. return NULL;
  911. }
  912. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  913. skb_reserve(new_skb,
  914. ppp->dev->hard_header_len - PPP_HDRLEN);
  915. /* compressor still expects A/C bytes in hdr */
  916. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  917. new_skb->data, skb->len + 2,
  918. compressor_skb_size);
  919. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  920. kfree_skb(skb);
  921. skb = new_skb;
  922. skb_put(skb, len);
  923. skb_pull(skb, 2); /* pull off A/C bytes */
  924. } else if (len == 0) {
  925. /* didn't compress, or CCP not up yet */
  926. kfree_skb(new_skb);
  927. new_skb = skb;
  928. } else {
  929. /*
  930. * (len < 0)
  931. * MPPE requires that we do not send unencrypted
  932. * frames. The compressor will return -1 if we
  933. * should drop the frame. We cannot simply test
  934. * the compress_proto because MPPE and MPPC share
  935. * the same number.
  936. */
  937. if (net_ratelimit())
  938. printk(KERN_ERR "ppp: compressor dropped pkt\n");
  939. kfree_skb(skb);
  940. kfree_skb(new_skb);
  941. new_skb = NULL;
  942. }
  943. return new_skb;
  944. }
  945. /*
  946. * Compress and send a frame.
  947. * The caller should have locked the xmit path,
  948. * and xmit_pending should be 0.
  949. */
  950. static void
  951. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  952. {
  953. int proto = PPP_PROTO(skb);
  954. struct sk_buff *new_skb;
  955. int len;
  956. unsigned char *cp;
  957. if (proto < 0x8000) {
  958. #ifdef CONFIG_PPP_FILTER
  959. /* check if we should pass this packet */
  960. /* the filter instructions are constructed assuming
  961. a four-byte PPP header on each packet */
  962. *skb_push(skb, 2) = 1;
  963. if (ppp->pass_filter
  964. && sk_run_filter(skb, ppp->pass_filter,
  965. ppp->pass_len) == 0) {
  966. if (ppp->debug & 1)
  967. printk(KERN_DEBUG "PPP: outbound frame not passed\n");
  968. kfree_skb(skb);
  969. return;
  970. }
  971. /* if this packet passes the active filter, record the time */
  972. if (!(ppp->active_filter
  973. && sk_run_filter(skb, ppp->active_filter,
  974. ppp->active_len) == 0))
  975. ppp->last_xmit = jiffies;
  976. skb_pull(skb, 2);
  977. #else
  978. /* for data packets, record the time */
  979. ppp->last_xmit = jiffies;
  980. #endif /* CONFIG_PPP_FILTER */
  981. }
  982. ++ppp->dev->stats.tx_packets;
  983. ppp->dev->stats.tx_bytes += skb->len - 2;
  984. switch (proto) {
  985. case PPP_IP:
  986. if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
  987. break;
  988. /* try to do VJ TCP header compression */
  989. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  990. GFP_ATOMIC);
  991. if (!new_skb) {
  992. printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
  993. goto drop;
  994. }
  995. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  996. cp = skb->data + 2;
  997. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  998. new_skb->data + 2, &cp,
  999. !(ppp->flags & SC_NO_TCP_CCID));
  1000. if (cp == skb->data + 2) {
  1001. /* didn't compress */
  1002. kfree_skb(new_skb);
  1003. } else {
  1004. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1005. proto = PPP_VJC_COMP;
  1006. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1007. } else {
  1008. proto = PPP_VJC_UNCOMP;
  1009. cp[0] = skb->data[2];
  1010. }
  1011. kfree_skb(skb);
  1012. skb = new_skb;
  1013. cp = skb_put(skb, len + 2);
  1014. cp[0] = 0;
  1015. cp[1] = proto;
  1016. }
  1017. break;
  1018. case PPP_CCP:
  1019. /* peek at outbound CCP frames */
  1020. ppp_ccp_peek(ppp, skb, 0);
  1021. break;
  1022. }
  1023. /* try to do packet compression */
  1024. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state
  1025. && proto != PPP_LCP && proto != PPP_CCP) {
  1026. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1027. if (net_ratelimit())
  1028. printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
  1029. goto drop;
  1030. }
  1031. skb = pad_compress_skb(ppp, skb);
  1032. if (!skb)
  1033. goto drop;
  1034. }
  1035. /*
  1036. * If we are waiting for traffic (demand dialling),
  1037. * queue it up for pppd to receive.
  1038. */
  1039. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1040. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1041. goto drop;
  1042. skb_queue_tail(&ppp->file.rq, skb);
  1043. wake_up_interruptible(&ppp->file.rwait);
  1044. return;
  1045. }
  1046. ppp->xmit_pending = skb;
  1047. ppp_push(ppp);
  1048. return;
  1049. drop:
  1050. if (skb)
  1051. kfree_skb(skb);
  1052. ++ppp->dev->stats.tx_errors;
  1053. }
  1054. /*
  1055. * Try to send the frame in xmit_pending.
  1056. * The caller should have the xmit path locked.
  1057. */
  1058. static void
  1059. ppp_push(struct ppp *ppp)
  1060. {
  1061. struct list_head *list;
  1062. struct channel *pch;
  1063. struct sk_buff *skb = ppp->xmit_pending;
  1064. if (!skb)
  1065. return;
  1066. list = &ppp->channels;
  1067. if (list_empty(list)) {
  1068. /* nowhere to send the packet, just drop it */
  1069. ppp->xmit_pending = NULL;
  1070. kfree_skb(skb);
  1071. return;
  1072. }
  1073. if ((ppp->flags & SC_MULTILINK) == 0) {
  1074. /* not doing multilink: send it down the first channel */
  1075. list = list->next;
  1076. pch = list_entry(list, struct channel, clist);
  1077. spin_lock_bh(&pch->downl);
  1078. if (pch->chan) {
  1079. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1080. ppp->xmit_pending = NULL;
  1081. } else {
  1082. /* channel got unregistered */
  1083. kfree_skb(skb);
  1084. ppp->xmit_pending = NULL;
  1085. }
  1086. spin_unlock_bh(&pch->downl);
  1087. return;
  1088. }
  1089. #ifdef CONFIG_PPP_MULTILINK
  1090. /* Multilink: fragment the packet over as many links
  1091. as can take the packet at the moment. */
  1092. if (!ppp_mp_explode(ppp, skb))
  1093. return;
  1094. #endif /* CONFIG_PPP_MULTILINK */
  1095. ppp->xmit_pending = NULL;
  1096. kfree_skb(skb);
  1097. }
  1098. #ifdef CONFIG_PPP_MULTILINK
  1099. /*
  1100. * Divide a packet to be transmitted into fragments and
  1101. * send them out the individual links.
  1102. */
  1103. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1104. {
  1105. int len, fragsize;
  1106. int i, bits, hdrlen, mtu;
  1107. int flen;
  1108. int navail, nfree;
  1109. int nbigger;
  1110. unsigned char *p, *q;
  1111. struct list_head *list;
  1112. struct channel *pch;
  1113. struct sk_buff *frag;
  1114. struct ppp_channel *chan;
  1115. nfree = 0; /* # channels which have no packet already queued */
  1116. navail = 0; /* total # of usable channels (not deregistered) */
  1117. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1118. i = 0;
  1119. list_for_each_entry(pch, &ppp->channels, clist) {
  1120. navail += pch->avail = (pch->chan != NULL);
  1121. if (pch->avail) {
  1122. if (skb_queue_empty(&pch->file.xq) ||
  1123. !pch->had_frag) {
  1124. pch->avail = 2;
  1125. ++nfree;
  1126. }
  1127. if (!pch->had_frag && i < ppp->nxchan)
  1128. ppp->nxchan = i;
  1129. }
  1130. ++i;
  1131. }
  1132. /*
  1133. * Don't start sending this packet unless at least half of
  1134. * the channels are free. This gives much better TCP
  1135. * performance if we have a lot of channels.
  1136. */
  1137. if (nfree == 0 || nfree < navail / 2)
  1138. return 0; /* can't take now, leave it in xmit_pending */
  1139. /* Do protocol field compression (XXX this should be optional) */
  1140. p = skb->data;
  1141. len = skb->len;
  1142. if (*p == 0) {
  1143. ++p;
  1144. --len;
  1145. }
  1146. /*
  1147. * Decide on fragment size.
  1148. * We create a fragment for each free channel regardless of
  1149. * how small they are (i.e. even 0 length) in order to minimize
  1150. * the time that it will take to detect when a channel drops
  1151. * a fragment.
  1152. */
  1153. fragsize = len;
  1154. if (nfree > 1)
  1155. fragsize = DIV_ROUND_UP(fragsize, nfree);
  1156. /* nbigger channels get fragsize bytes, the rest get fragsize-1,
  1157. except if nbigger==0, then they all get fragsize. */
  1158. nbigger = len % nfree;
  1159. /* skip to the channel after the one we last used
  1160. and start at that one */
  1161. list = &ppp->channels;
  1162. for (i = 0; i < ppp->nxchan; ++i) {
  1163. list = list->next;
  1164. if (list == &ppp->channels) {
  1165. i = 0;
  1166. break;
  1167. }
  1168. }
  1169. /* create a fragment for each channel */
  1170. bits = B;
  1171. while (nfree > 0 || len > 0) {
  1172. list = list->next;
  1173. if (list == &ppp->channels) {
  1174. i = 0;
  1175. continue;
  1176. }
  1177. pch = list_entry(list, struct channel, clist);
  1178. ++i;
  1179. if (!pch->avail)
  1180. continue;
  1181. /*
  1182. * Skip this channel if it has a fragment pending already and
  1183. * we haven't given a fragment to all of the free channels.
  1184. */
  1185. if (pch->avail == 1) {
  1186. if (nfree > 0)
  1187. continue;
  1188. } else {
  1189. --nfree;
  1190. pch->avail = 1;
  1191. }
  1192. /* check the channel's mtu and whether it is still attached. */
  1193. spin_lock_bh(&pch->downl);
  1194. if (pch->chan == NULL) {
  1195. /* can't use this channel, it's being deregistered */
  1196. spin_unlock_bh(&pch->downl);
  1197. pch->avail = 0;
  1198. if (--navail == 0)
  1199. break;
  1200. continue;
  1201. }
  1202. /*
  1203. * Create a fragment for this channel of
  1204. * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
  1205. * If mtu+2-hdrlen < 4, that is a ridiculously small
  1206. * MTU, so we use mtu = 2 + hdrlen.
  1207. */
  1208. if (fragsize > len)
  1209. fragsize = len;
  1210. flen = fragsize;
  1211. mtu = pch->chan->mtu + 2 - hdrlen;
  1212. if (mtu < 4)
  1213. mtu = 4;
  1214. if (flen > mtu)
  1215. flen = mtu;
  1216. if (flen == len && nfree == 0)
  1217. bits |= E;
  1218. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1219. if (!frag)
  1220. goto noskb;
  1221. q = skb_put(frag, flen + hdrlen);
  1222. /* make the MP header */
  1223. q[0] = PPP_MP >> 8;
  1224. q[1] = PPP_MP;
  1225. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1226. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1227. q[3] = ppp->nxseq;
  1228. } else {
  1229. q[2] = bits;
  1230. q[3] = ppp->nxseq >> 16;
  1231. q[4] = ppp->nxseq >> 8;
  1232. q[5] = ppp->nxseq;
  1233. }
  1234. /*
  1235. * Copy the data in.
  1236. * Unfortunately there is a bug in older versions of
  1237. * the Linux PPP multilink reconstruction code where it
  1238. * drops 0-length fragments. Therefore we make sure the
  1239. * fragment has at least one byte of data. Any bytes
  1240. * we add in this situation will end up as padding on the
  1241. * end of the reconstructed packet.
  1242. */
  1243. if (flen == 0)
  1244. *skb_put(frag, 1) = 0;
  1245. else
  1246. memcpy(q + hdrlen, p, flen);
  1247. /* try to send it down the channel */
  1248. chan = pch->chan;
  1249. if (!skb_queue_empty(&pch->file.xq) ||
  1250. !chan->ops->start_xmit(chan, frag))
  1251. skb_queue_tail(&pch->file.xq, frag);
  1252. pch->had_frag = 1;
  1253. p += flen;
  1254. len -= flen;
  1255. ++ppp->nxseq;
  1256. bits = 0;
  1257. spin_unlock_bh(&pch->downl);
  1258. if (--nbigger == 0 && fragsize > 0)
  1259. --fragsize;
  1260. }
  1261. ppp->nxchan = i;
  1262. return 1;
  1263. noskb:
  1264. spin_unlock_bh(&pch->downl);
  1265. if (ppp->debug & 1)
  1266. printk(KERN_ERR "PPP: no memory (fragment)\n");
  1267. ++ppp->dev->stats.tx_errors;
  1268. ++ppp->nxseq;
  1269. return 1; /* abandon the frame */
  1270. }
  1271. #endif /* CONFIG_PPP_MULTILINK */
  1272. /*
  1273. * Try to send data out on a channel.
  1274. */
  1275. static void
  1276. ppp_channel_push(struct channel *pch)
  1277. {
  1278. struct sk_buff *skb;
  1279. struct ppp *ppp;
  1280. spin_lock_bh(&pch->downl);
  1281. if (pch->chan) {
  1282. while (!skb_queue_empty(&pch->file.xq)) {
  1283. skb = skb_dequeue(&pch->file.xq);
  1284. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1285. /* put the packet back and try again later */
  1286. skb_queue_head(&pch->file.xq, skb);
  1287. break;
  1288. }
  1289. }
  1290. } else {
  1291. /* channel got deregistered */
  1292. skb_queue_purge(&pch->file.xq);
  1293. }
  1294. spin_unlock_bh(&pch->downl);
  1295. /* see if there is anything from the attached unit to be sent */
  1296. if (skb_queue_empty(&pch->file.xq)) {
  1297. read_lock_bh(&pch->upl);
  1298. ppp = pch->ppp;
  1299. if (ppp)
  1300. ppp_xmit_process(ppp);
  1301. read_unlock_bh(&pch->upl);
  1302. }
  1303. }
  1304. /*
  1305. * Receive-side routines.
  1306. */
  1307. /* misuse a few fields of the skb for MP reconstruction */
  1308. #define sequence priority
  1309. #define BEbits cb[0]
  1310. static inline void
  1311. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1312. {
  1313. ppp_recv_lock(ppp);
  1314. /* ppp->dev == 0 means interface is closing down */
  1315. if (ppp->dev)
  1316. ppp_receive_frame(ppp, skb, pch);
  1317. else
  1318. kfree_skb(skb);
  1319. ppp_recv_unlock(ppp);
  1320. }
  1321. void
  1322. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1323. {
  1324. struct channel *pch = chan->ppp;
  1325. int proto;
  1326. if (!pch || skb->len == 0) {
  1327. kfree_skb(skb);
  1328. return;
  1329. }
  1330. proto = PPP_PROTO(skb);
  1331. read_lock_bh(&pch->upl);
  1332. if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  1333. /* put it on the channel queue */
  1334. skb_queue_tail(&pch->file.rq, skb);
  1335. /* drop old frames if queue too long */
  1336. while (pch->file.rq.qlen > PPP_MAX_RQLEN
  1337. && (skb = skb_dequeue(&pch->file.rq)))
  1338. kfree_skb(skb);
  1339. wake_up_interruptible(&pch->file.rwait);
  1340. } else {
  1341. ppp_do_recv(pch->ppp, skb, pch);
  1342. }
  1343. read_unlock_bh(&pch->upl);
  1344. }
  1345. /* Put a 0-length skb in the receive queue as an error indication */
  1346. void
  1347. ppp_input_error(struct ppp_channel *chan, int code)
  1348. {
  1349. struct channel *pch = chan->ppp;
  1350. struct sk_buff *skb;
  1351. if (!pch)
  1352. return;
  1353. read_lock_bh(&pch->upl);
  1354. if (pch->ppp) {
  1355. skb = alloc_skb(0, GFP_ATOMIC);
  1356. if (skb) {
  1357. skb->len = 0; /* probably unnecessary */
  1358. skb->cb[0] = code;
  1359. ppp_do_recv(pch->ppp, skb, pch);
  1360. }
  1361. }
  1362. read_unlock_bh(&pch->upl);
  1363. }
  1364. /*
  1365. * We come in here to process a received frame.
  1366. * The receive side of the ppp unit is locked.
  1367. */
  1368. static void
  1369. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1370. {
  1371. if (pskb_may_pull(skb, 2)) {
  1372. #ifdef CONFIG_PPP_MULTILINK
  1373. /* XXX do channel-level decompression here */
  1374. if (PPP_PROTO(skb) == PPP_MP)
  1375. ppp_receive_mp_frame(ppp, skb, pch);
  1376. else
  1377. #endif /* CONFIG_PPP_MULTILINK */
  1378. ppp_receive_nonmp_frame(ppp, skb);
  1379. return;
  1380. }
  1381. if (skb->len > 0)
  1382. /* note: a 0-length skb is used as an error indication */
  1383. ++ppp->dev->stats.rx_length_errors;
  1384. kfree_skb(skb);
  1385. ppp_receive_error(ppp);
  1386. }
  1387. static void
  1388. ppp_receive_error(struct ppp *ppp)
  1389. {
  1390. ++ppp->dev->stats.rx_errors;
  1391. if (ppp->vj)
  1392. slhc_toss(ppp->vj);
  1393. }
  1394. static void
  1395. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  1396. {
  1397. struct sk_buff *ns;
  1398. int proto, len, npi;
  1399. /*
  1400. * Decompress the frame, if compressed.
  1401. * Note that some decompressors need to see uncompressed frames
  1402. * that come in as well as compressed frames.
  1403. */
  1404. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)
  1405. && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  1406. skb = ppp_decompress_frame(ppp, skb);
  1407. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  1408. goto err;
  1409. proto = PPP_PROTO(skb);
  1410. switch (proto) {
  1411. case PPP_VJC_COMP:
  1412. /* decompress VJ compressed packets */
  1413. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1414. goto err;
  1415. if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
  1416. /* copy to a new sk_buff with more tailroom */
  1417. ns = dev_alloc_skb(skb->len + 128);
  1418. if (!ns) {
  1419. printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
  1420. goto err;
  1421. }
  1422. skb_reserve(ns, 2);
  1423. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  1424. kfree_skb(skb);
  1425. skb = ns;
  1426. }
  1427. else
  1428. skb->ip_summed = CHECKSUM_NONE;
  1429. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  1430. if (len <= 0) {
  1431. printk(KERN_DEBUG "PPP: VJ decompression error\n");
  1432. goto err;
  1433. }
  1434. len += 2;
  1435. if (len > skb->len)
  1436. skb_put(skb, len - skb->len);
  1437. else if (len < skb->len)
  1438. skb_trim(skb, len);
  1439. proto = PPP_IP;
  1440. break;
  1441. case PPP_VJC_UNCOMP:
  1442. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1443. goto err;
  1444. /* Until we fix the decompressor need to make sure
  1445. * data portion is linear.
  1446. */
  1447. if (!pskb_may_pull(skb, skb->len))
  1448. goto err;
  1449. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  1450. printk(KERN_ERR "PPP: VJ uncompressed error\n");
  1451. goto err;
  1452. }
  1453. proto = PPP_IP;
  1454. break;
  1455. case PPP_CCP:
  1456. ppp_ccp_peek(ppp, skb, 1);
  1457. break;
  1458. }
  1459. ++ppp->dev->stats.rx_packets;
  1460. ppp->dev->stats.rx_bytes += skb->len - 2;
  1461. npi = proto_to_npindex(proto);
  1462. if (npi < 0) {
  1463. /* control or unknown frame - pass it to pppd */
  1464. skb_queue_tail(&ppp->file.rq, skb);
  1465. /* limit queue length by dropping old frames */
  1466. while (ppp->file.rq.qlen > PPP_MAX_RQLEN
  1467. && (skb = skb_dequeue(&ppp->file.rq)))
  1468. kfree_skb(skb);
  1469. /* wake up any process polling or blocking on read */
  1470. wake_up_interruptible(&ppp->file.rwait);
  1471. } else {
  1472. /* network protocol frame - give it to the kernel */
  1473. #ifdef CONFIG_PPP_FILTER
  1474. /* check if the packet passes the pass and active filters */
  1475. /* the filter instructions are constructed assuming
  1476. a four-byte PPP header on each packet */
  1477. if (ppp->pass_filter || ppp->active_filter) {
  1478. if (skb_cloned(skb) &&
  1479. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  1480. goto err;
  1481. *skb_push(skb, 2) = 0;
  1482. if (ppp->pass_filter
  1483. && sk_run_filter(skb, ppp->pass_filter,
  1484. ppp->pass_len) == 0) {
  1485. if (ppp->debug & 1)
  1486. printk(KERN_DEBUG "PPP: inbound frame "
  1487. "not passed\n");
  1488. kfree_skb(skb);
  1489. return;
  1490. }
  1491. if (!(ppp->active_filter
  1492. && sk_run_filter(skb, ppp->active_filter,
  1493. ppp->active_len) == 0))
  1494. ppp->last_recv = jiffies;
  1495. __skb_pull(skb, 2);
  1496. } else
  1497. #endif /* CONFIG_PPP_FILTER */
  1498. ppp->last_recv = jiffies;
  1499. if ((ppp->dev->flags & IFF_UP) == 0
  1500. || ppp->npmode[npi] != NPMODE_PASS) {
  1501. kfree_skb(skb);
  1502. } else {
  1503. /* chop off protocol */
  1504. skb_pull_rcsum(skb, 2);
  1505. skb->dev = ppp->dev;
  1506. skb->protocol = htons(npindex_to_ethertype[npi]);
  1507. skb_reset_mac_header(skb);
  1508. netif_rx(skb);
  1509. }
  1510. }
  1511. return;
  1512. err:
  1513. kfree_skb(skb);
  1514. ppp_receive_error(ppp);
  1515. }
  1516. static struct sk_buff *
  1517. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  1518. {
  1519. int proto = PPP_PROTO(skb);
  1520. struct sk_buff *ns;
  1521. int len;
  1522. /* Until we fix all the decompressor's need to make sure
  1523. * data portion is linear.
  1524. */
  1525. if (!pskb_may_pull(skb, skb->len))
  1526. goto err;
  1527. if (proto == PPP_COMP) {
  1528. int obuff_size;
  1529. switch(ppp->rcomp->compress_proto) {
  1530. case CI_MPPE:
  1531. obuff_size = ppp->mru + PPP_HDRLEN + 1;
  1532. break;
  1533. default:
  1534. obuff_size = ppp->mru + PPP_HDRLEN;
  1535. break;
  1536. }
  1537. ns = dev_alloc_skb(obuff_size);
  1538. if (!ns) {
  1539. printk(KERN_ERR "ppp_decompress_frame: no memory\n");
  1540. goto err;
  1541. }
  1542. /* the decompressor still expects the A/C bytes in the hdr */
  1543. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  1544. skb->len + 2, ns->data, obuff_size);
  1545. if (len < 0) {
  1546. /* Pass the compressed frame to pppd as an
  1547. error indication. */
  1548. if (len == DECOMP_FATALERROR)
  1549. ppp->rstate |= SC_DC_FERROR;
  1550. kfree_skb(ns);
  1551. goto err;
  1552. }
  1553. kfree_skb(skb);
  1554. skb = ns;
  1555. skb_put(skb, len);
  1556. skb_pull(skb, 2); /* pull off the A/C bytes */
  1557. } else {
  1558. /* Uncompressed frame - pass to decompressor so it
  1559. can update its dictionary if necessary. */
  1560. if (ppp->rcomp->incomp)
  1561. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  1562. skb->len + 2);
  1563. }
  1564. return skb;
  1565. err:
  1566. ppp->rstate |= SC_DC_ERROR;
  1567. ppp_receive_error(ppp);
  1568. return skb;
  1569. }
  1570. #ifdef CONFIG_PPP_MULTILINK
  1571. /*
  1572. * Receive a multilink frame.
  1573. * We put it on the reconstruction queue and then pull off
  1574. * as many completed frames as we can.
  1575. */
  1576. static void
  1577. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1578. {
  1579. u32 mask, seq;
  1580. struct channel *ch;
  1581. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1582. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  1583. goto err; /* no good, throw it away */
  1584. /* Decode sequence number and begin/end bits */
  1585. if (ppp->flags & SC_MP_SHORTSEQ) {
  1586. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  1587. mask = 0xfff;
  1588. } else {
  1589. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  1590. mask = 0xffffff;
  1591. }
  1592. skb->BEbits = skb->data[2];
  1593. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  1594. /*
  1595. * Do protocol ID decompression on the first fragment of each packet.
  1596. */
  1597. if ((skb->BEbits & B) && (skb->data[0] & 1))
  1598. *skb_push(skb, 1) = 0;
  1599. /*
  1600. * Expand sequence number to 32 bits, making it as close
  1601. * as possible to ppp->minseq.
  1602. */
  1603. seq |= ppp->minseq & ~mask;
  1604. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  1605. seq += mask + 1;
  1606. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  1607. seq -= mask + 1; /* should never happen */
  1608. skb->sequence = seq;
  1609. pch->lastseq = seq;
  1610. /*
  1611. * If this packet comes before the next one we were expecting,
  1612. * drop it.
  1613. */
  1614. if (seq_before(seq, ppp->nextseq)) {
  1615. kfree_skb(skb);
  1616. ++ppp->dev->stats.rx_dropped;
  1617. ppp_receive_error(ppp);
  1618. return;
  1619. }
  1620. /*
  1621. * Reevaluate minseq, the minimum over all channels of the
  1622. * last sequence number received on each channel. Because of
  1623. * the increasing sequence number rule, we know that any fragment
  1624. * before `minseq' which hasn't arrived is never going to arrive.
  1625. * The list of channels can't change because we have the receive
  1626. * side of the ppp unit locked.
  1627. */
  1628. list_for_each_entry(ch, &ppp->channels, clist) {
  1629. if (seq_before(ch->lastseq, seq))
  1630. seq = ch->lastseq;
  1631. }
  1632. if (seq_before(ppp->minseq, seq))
  1633. ppp->minseq = seq;
  1634. /* Put the fragment on the reconstruction queue */
  1635. ppp_mp_insert(ppp, skb);
  1636. /* If the queue is getting long, don't wait any longer for packets
  1637. before the start of the queue. */
  1638. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
  1639. struct sk_buff *skb = skb_peek(&ppp->mrq);
  1640. if (seq_before(ppp->minseq, skb->sequence))
  1641. ppp->minseq = skb->sequence;
  1642. }
  1643. /* Pull completed packets off the queue and receive them. */
  1644. while ((skb = ppp_mp_reconstruct(ppp)))
  1645. ppp_receive_nonmp_frame(ppp, skb);
  1646. return;
  1647. err:
  1648. kfree_skb(skb);
  1649. ppp_receive_error(ppp);
  1650. }
  1651. /*
  1652. * Insert a fragment on the MP reconstruction queue.
  1653. * The queue is ordered by increasing sequence number.
  1654. */
  1655. static void
  1656. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  1657. {
  1658. struct sk_buff *p;
  1659. struct sk_buff_head *list = &ppp->mrq;
  1660. u32 seq = skb->sequence;
  1661. /* N.B. we don't need to lock the list lock because we have the
  1662. ppp unit receive-side lock. */
  1663. skb_queue_walk(list, p) {
  1664. if (seq_before(seq, p->sequence))
  1665. break;
  1666. }
  1667. __skb_queue_before(list, p, skb);
  1668. }
  1669. /*
  1670. * Reconstruct a packet from the MP fragment queue.
  1671. * We go through increasing sequence numbers until we find a
  1672. * complete packet, or we get to the sequence number for a fragment
  1673. * which hasn't arrived but might still do so.
  1674. */
  1675. static struct sk_buff *
  1676. ppp_mp_reconstruct(struct ppp *ppp)
  1677. {
  1678. u32 seq = ppp->nextseq;
  1679. u32 minseq = ppp->minseq;
  1680. struct sk_buff_head *list = &ppp->mrq;
  1681. struct sk_buff *p, *next;
  1682. struct sk_buff *head, *tail;
  1683. struct sk_buff *skb = NULL;
  1684. int lost = 0, len = 0;
  1685. if (ppp->mrru == 0) /* do nothing until mrru is set */
  1686. return NULL;
  1687. head = list->next;
  1688. tail = NULL;
  1689. for (p = head; p != (struct sk_buff *) list; p = next) {
  1690. next = p->next;
  1691. if (seq_before(p->sequence, seq)) {
  1692. /* this can't happen, anyway ignore the skb */
  1693. printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
  1694. p->sequence, seq);
  1695. head = next;
  1696. continue;
  1697. }
  1698. if (p->sequence != seq) {
  1699. /* Fragment `seq' is missing. If it is after
  1700. minseq, it might arrive later, so stop here. */
  1701. if (seq_after(seq, minseq))
  1702. break;
  1703. /* Fragment `seq' is lost, keep going. */
  1704. lost = 1;
  1705. seq = seq_before(minseq, p->sequence)?
  1706. minseq + 1: p->sequence;
  1707. next = p;
  1708. continue;
  1709. }
  1710. /*
  1711. * At this point we know that all the fragments from
  1712. * ppp->nextseq to seq are either present or lost.
  1713. * Also, there are no complete packets in the queue
  1714. * that have no missing fragments and end before this
  1715. * fragment.
  1716. */
  1717. /* B bit set indicates this fragment starts a packet */
  1718. if (p->BEbits & B) {
  1719. head = p;
  1720. lost = 0;
  1721. len = 0;
  1722. }
  1723. len += p->len;
  1724. /* Got a complete packet yet? */
  1725. if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
  1726. if (len > ppp->mrru + 2) {
  1727. ++ppp->dev->stats.rx_length_errors;
  1728. printk(KERN_DEBUG "PPP: reconstructed packet"
  1729. " is too long (%d)\n", len);
  1730. } else if (p == head) {
  1731. /* fragment is complete packet - reuse skb */
  1732. tail = p;
  1733. skb = skb_get(p);
  1734. break;
  1735. } else if ((skb = dev_alloc_skb(len)) == NULL) {
  1736. ++ppp->dev->stats.rx_missed_errors;
  1737. printk(KERN_DEBUG "PPP: no memory for "
  1738. "reconstructed packet");
  1739. } else {
  1740. tail = p;
  1741. break;
  1742. }
  1743. ppp->nextseq = seq + 1;
  1744. }
  1745. /*
  1746. * If this is the ending fragment of a packet,
  1747. * and we haven't found a complete valid packet yet,
  1748. * we can discard up to and including this fragment.
  1749. */
  1750. if (p->BEbits & E)
  1751. head = next;
  1752. ++seq;
  1753. }
  1754. /* If we have a complete packet, copy it all into one skb. */
  1755. if (tail != NULL) {
  1756. /* If we have discarded any fragments,
  1757. signal a receive error. */
  1758. if (head->sequence != ppp->nextseq) {
  1759. if (ppp->debug & 1)
  1760. printk(KERN_DEBUG " missed pkts %u..%u\n",
  1761. ppp->nextseq, head->sequence-1);
  1762. ++ppp->dev->stats.rx_dropped;
  1763. ppp_receive_error(ppp);
  1764. }
  1765. if (head != tail)
  1766. /* copy to a single skb */
  1767. for (p = head; p != tail->next; p = p->next)
  1768. skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
  1769. ppp->nextseq = tail->sequence + 1;
  1770. head = tail->next;
  1771. }
  1772. /* Discard all the skbuffs that we have copied the data out of
  1773. or that we can't use. */
  1774. while ((p = list->next) != head) {
  1775. __skb_unlink(p, list);
  1776. kfree_skb(p);
  1777. }
  1778. return skb;
  1779. }
  1780. #endif /* CONFIG_PPP_MULTILINK */
  1781. /*
  1782. * Channel interface.
  1783. */
  1784. /*
  1785. * Create a new, unattached ppp channel.
  1786. */
  1787. int
  1788. ppp_register_channel(struct ppp_channel *chan)
  1789. {
  1790. struct channel *pch;
  1791. pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  1792. if (!pch)
  1793. return -ENOMEM;
  1794. pch->ppp = NULL;
  1795. pch->chan = chan;
  1796. chan->ppp = pch;
  1797. init_ppp_file(&pch->file, CHANNEL);
  1798. pch->file.hdrlen = chan->hdrlen;
  1799. #ifdef CONFIG_PPP_MULTILINK
  1800. pch->lastseq = -1;
  1801. #endif /* CONFIG_PPP_MULTILINK */
  1802. init_rwsem(&pch->chan_sem);
  1803. spin_lock_init(&pch->downl);
  1804. rwlock_init(&pch->upl);
  1805. spin_lock_bh(&all_channels_lock);
  1806. pch->file.index = ++last_channel_index;
  1807. list_add(&pch->list, &new_channels);
  1808. atomic_inc(&channel_count);
  1809. spin_unlock_bh(&all_channels_lock);
  1810. return 0;
  1811. }
  1812. /*
  1813. * Return the index of a channel.
  1814. */
  1815. int ppp_channel_index(struct ppp_channel *chan)
  1816. {
  1817. struct channel *pch = chan->ppp;
  1818. if (pch)
  1819. return pch->file.index;
  1820. return -1;
  1821. }
  1822. /*
  1823. * Return the PPP unit number to which a channel is connected.
  1824. */
  1825. int ppp_unit_number(struct ppp_channel *chan)
  1826. {
  1827. struct channel *pch = chan->ppp;
  1828. int unit = -1;
  1829. if (pch) {
  1830. read_lock_bh(&pch->upl);
  1831. if (pch->ppp)
  1832. unit = pch->ppp->file.index;
  1833. read_unlock_bh(&pch->upl);
  1834. }
  1835. return unit;
  1836. }
  1837. /*
  1838. * Disconnect a channel from the generic layer.
  1839. * This must be called in process context.
  1840. */
  1841. void
  1842. ppp_unregister_channel(struct ppp_channel *chan)
  1843. {
  1844. struct channel *pch = chan->ppp;
  1845. if (!pch)
  1846. return; /* should never happen */
  1847. chan->ppp = NULL;
  1848. /*
  1849. * This ensures that we have returned from any calls into the
  1850. * the channel's start_xmit or ioctl routine before we proceed.
  1851. */
  1852. down_write(&pch->chan_sem);
  1853. spin_lock_bh(&pch->downl);
  1854. pch->chan = NULL;
  1855. spin_unlock_bh(&pch->downl);
  1856. up_write(&pch->chan_sem);
  1857. ppp_disconnect_channel(pch);
  1858. spin_lock_bh(&all_channels_lock);
  1859. list_del(&pch->list);
  1860. spin_unlock_bh(&all_channels_lock);
  1861. pch->file.dead = 1;
  1862. wake_up_interruptible(&pch->file.rwait);
  1863. if (atomic_dec_and_test(&pch->file.refcnt))
  1864. ppp_destroy_channel(pch);
  1865. }
  1866. /*
  1867. * Callback from a channel when it can accept more to transmit.
  1868. * This should be called at BH/softirq level, not interrupt level.
  1869. */
  1870. void
  1871. ppp_output_wakeup(struct ppp_channel *chan)
  1872. {
  1873. struct channel *pch = chan->ppp;
  1874. if (!pch)
  1875. return;
  1876. ppp_channel_push(pch);
  1877. }
  1878. /*
  1879. * Compression control.
  1880. */
  1881. /* Process the PPPIOCSCOMPRESS ioctl. */
  1882. static int
  1883. ppp_set_compress(struct ppp *ppp, unsigned long arg)
  1884. {
  1885. int err;
  1886. struct compressor *cp, *ocomp;
  1887. struct ppp_option_data data;
  1888. void *state, *ostate;
  1889. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  1890. err = -EFAULT;
  1891. if (copy_from_user(&data, (void __user *) arg, sizeof(data))
  1892. || (data.length <= CCP_MAX_OPTION_LENGTH
  1893. && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
  1894. goto out;
  1895. err = -EINVAL;
  1896. if (data.length > CCP_MAX_OPTION_LENGTH
  1897. || ccp_option[1] < 2 || ccp_option[1] > data.length)
  1898. goto out;
  1899. cp = try_then_request_module(
  1900. find_compressor(ccp_option[0]),
  1901. "ppp-compress-%d", ccp_option[0]);
  1902. if (!cp)
  1903. goto out;
  1904. err = -ENOBUFS;
  1905. if (data.transmit) {
  1906. state = cp->comp_alloc(ccp_option, data.length);
  1907. if (state) {
  1908. ppp_xmit_lock(ppp);
  1909. ppp->xstate &= ~SC_COMP_RUN;
  1910. ocomp = ppp->xcomp;
  1911. ostate = ppp->xc_state;
  1912. ppp->xcomp = cp;
  1913. ppp->xc_state = state;
  1914. ppp_xmit_unlock(ppp);
  1915. if (ostate) {
  1916. ocomp->comp_free(ostate);
  1917. module_put(ocomp->owner);
  1918. }
  1919. err = 0;
  1920. } else
  1921. module_put(cp->owner);
  1922. } else {
  1923. state = cp->decomp_alloc(ccp_option, data.length);
  1924. if (state) {
  1925. ppp_recv_lock(ppp);
  1926. ppp->rstate &= ~SC_DECOMP_RUN;
  1927. ocomp = ppp->rcomp;
  1928. ostate = ppp->rc_state;
  1929. ppp->rcomp = cp;
  1930. ppp->rc_state = state;
  1931. ppp_recv_unlock(ppp);
  1932. if (ostate) {
  1933. ocomp->decomp_free(ostate);
  1934. module_put(ocomp->owner);
  1935. }
  1936. err = 0;
  1937. } else
  1938. module_put(cp->owner);
  1939. }
  1940. out:
  1941. return err;
  1942. }
  1943. /*
  1944. * Look at a CCP packet and update our state accordingly.
  1945. * We assume the caller has the xmit or recv path locked.
  1946. */
  1947. static void
  1948. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  1949. {
  1950. unsigned char *dp;
  1951. int len;
  1952. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  1953. return; /* no header */
  1954. dp = skb->data + 2;
  1955. switch (CCP_CODE(dp)) {
  1956. case CCP_CONFREQ:
  1957. /* A ConfReq starts negotiation of compression
  1958. * in one direction of transmission,
  1959. * and hence brings it down...but which way?
  1960. *
  1961. * Remember:
  1962. * A ConfReq indicates what the sender would like to receive
  1963. */
  1964. if(inbound)
  1965. /* He is proposing what I should send */
  1966. ppp->xstate &= ~SC_COMP_RUN;
  1967. else
  1968. /* I am proposing to what he should send */
  1969. ppp->rstate &= ~SC_DECOMP_RUN;
  1970. break;
  1971. case CCP_TERMREQ:
  1972. case CCP_TERMACK:
  1973. /*
  1974. * CCP is going down, both directions of transmission
  1975. */
  1976. ppp->rstate &= ~SC_DECOMP_RUN;
  1977. ppp->xstate &= ~SC_COMP_RUN;
  1978. break;
  1979. case CCP_CONFACK:
  1980. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  1981. break;
  1982. len = CCP_LENGTH(dp);
  1983. if (!pskb_may_pull(skb, len + 2))
  1984. return; /* too short */
  1985. dp += CCP_HDRLEN;
  1986. len -= CCP_HDRLEN;
  1987. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  1988. break;
  1989. if (inbound) {
  1990. /* we will start receiving compressed packets */
  1991. if (!ppp->rc_state)
  1992. break;
  1993. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  1994. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  1995. ppp->rstate |= SC_DECOMP_RUN;
  1996. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  1997. }
  1998. } else {
  1999. /* we will soon start sending compressed packets */
  2000. if (!ppp->xc_state)
  2001. break;
  2002. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2003. ppp->file.index, 0, ppp->debug))
  2004. ppp->xstate |= SC_COMP_RUN;
  2005. }
  2006. break;
  2007. case CCP_RESETACK:
  2008. /* reset the [de]compressor */
  2009. if ((ppp->flags & SC_CCP_UP) == 0)
  2010. break;
  2011. if (inbound) {
  2012. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2013. ppp->rcomp->decomp_reset(ppp->rc_state);
  2014. ppp->rstate &= ~SC_DC_ERROR;
  2015. }
  2016. } else {
  2017. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2018. ppp->xcomp->comp_reset(ppp->xc_state);
  2019. }
  2020. break;
  2021. }
  2022. }
  2023. /* Free up compression resources. */
  2024. static void
  2025. ppp_ccp_closed(struct ppp *ppp)
  2026. {
  2027. void *xstate, *rstate;
  2028. struct compressor *xcomp, *rcomp;
  2029. ppp_lock(ppp);
  2030. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2031. ppp->xstate = 0;
  2032. xcomp = ppp->xcomp;
  2033. xstate = ppp->xc_state;
  2034. ppp->xc_state = NULL;
  2035. ppp->rstate = 0;
  2036. rcomp = ppp->rcomp;
  2037. rstate = ppp->rc_state;
  2038. ppp->rc_state = NULL;
  2039. ppp_unlock(ppp);
  2040. if (xstate) {
  2041. xcomp->comp_free(xstate);
  2042. module_put(xcomp->owner);
  2043. }
  2044. if (rstate) {
  2045. rcomp->decomp_free(rstate);
  2046. module_put(rcomp->owner);
  2047. }
  2048. }
  2049. /* List of compressors. */
  2050. static LIST_HEAD(compressor_list);
  2051. static DEFINE_SPINLOCK(compressor_list_lock);
  2052. struct compressor_entry {
  2053. struct list_head list;
  2054. struct compressor *comp;
  2055. };
  2056. static struct compressor_entry *
  2057. find_comp_entry(int proto)
  2058. {
  2059. struct compressor_entry *ce;
  2060. list_for_each_entry(ce, &compressor_list, list) {
  2061. if (ce->comp->compress_proto == proto)
  2062. return ce;
  2063. }
  2064. return NULL;
  2065. }
  2066. /* Register a compressor */
  2067. int
  2068. ppp_register_compressor(struct compressor *cp)
  2069. {
  2070. struct compressor_entry *ce;
  2071. int ret;
  2072. spin_lock(&compressor_list_lock);
  2073. ret = -EEXIST;
  2074. if (find_comp_entry(cp->compress_proto))
  2075. goto out;
  2076. ret = -ENOMEM;
  2077. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2078. if (!ce)
  2079. goto out;
  2080. ret = 0;
  2081. ce->comp = cp;
  2082. list_add(&ce->list, &compressor_list);
  2083. out:
  2084. spin_unlock(&compressor_list_lock);
  2085. return ret;
  2086. }
  2087. /* Unregister a compressor */
  2088. void
  2089. ppp_unregister_compressor(struct compressor *cp)
  2090. {
  2091. struct compressor_entry *ce;
  2092. spin_lock(&compressor_list_lock);
  2093. ce = find_comp_entry(cp->compress_proto);
  2094. if (ce && ce->comp == cp) {
  2095. list_del(&ce->list);
  2096. kfree(ce);
  2097. }
  2098. spin_unlock(&compressor_list_lock);
  2099. }
  2100. /* Find a compressor. */
  2101. static struct compressor *
  2102. find_compressor(int type)
  2103. {
  2104. struct compressor_entry *ce;
  2105. struct compressor *cp = NULL;
  2106. spin_lock(&compressor_list_lock);
  2107. ce = find_comp_entry(type);
  2108. if (ce) {
  2109. cp = ce->comp;
  2110. if (!try_module_get(cp->owner))
  2111. cp = NULL;
  2112. }
  2113. spin_unlock(&compressor_list_lock);
  2114. return cp;
  2115. }
  2116. /*
  2117. * Miscelleneous stuff.
  2118. */
  2119. static void
  2120. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2121. {
  2122. struct slcompress *vj = ppp->vj;
  2123. memset(st, 0, sizeof(*st));
  2124. st->p.ppp_ipackets = ppp->dev->stats.rx_packets;
  2125. st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
  2126. st->p.ppp_ibytes = ppp->dev->stats.rx_bytes;
  2127. st->p.ppp_opackets = ppp->dev->stats.tx_packets;
  2128. st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
  2129. st->p.ppp_obytes = ppp->dev->stats.tx_bytes;
  2130. if (!vj)
  2131. return;
  2132. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2133. st->vj.vjs_compressed = vj->sls_o_compressed;
  2134. st->vj.vjs_searches = vj->sls_o_searches;
  2135. st->vj.vjs_misses = vj->sls_o_misses;
  2136. st->vj.vjs_errorin = vj->sls_i_error;
  2137. st->vj.vjs_tossed = vj->sls_i_tossed;
  2138. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2139. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2140. }
  2141. /*
  2142. * Stuff for handling the lists of ppp units and channels
  2143. * and for initialization.
  2144. */
  2145. /*
  2146. * Create a new ppp interface unit. Fails if it can't allocate memory
  2147. * or if there is already a unit with the requested number.
  2148. * unit == -1 means allocate a new number.
  2149. */
  2150. static struct ppp *
  2151. ppp_create_interface(int unit, int *retp)
  2152. {
  2153. struct ppp *ppp;
  2154. struct net_device *dev = NULL;
  2155. int ret = -ENOMEM;
  2156. int i;
  2157. dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
  2158. if (!dev)
  2159. goto out1;
  2160. ppp = netdev_priv(dev);
  2161. ppp->dev = dev;
  2162. ppp->mru = PPP_MRU;
  2163. init_ppp_file(&ppp->file, INTERFACE);
  2164. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  2165. for (i = 0; i < NUM_NP; ++i)
  2166. ppp->npmode[i] = NPMODE_PASS;
  2167. INIT_LIST_HEAD(&ppp->channels);
  2168. spin_lock_init(&ppp->rlock);
  2169. spin_lock_init(&ppp->wlock);
  2170. #ifdef CONFIG_PPP_MULTILINK
  2171. ppp->minseq = -1;
  2172. skb_queue_head_init(&ppp->mrq);
  2173. #endif /* CONFIG_PPP_MULTILINK */
  2174. ret = -EEXIST;
  2175. mutex_lock(&all_ppp_mutex);
  2176. if (unit < 0) {
  2177. unit = unit_get(&ppp_units_idr, ppp);
  2178. if (unit < 0) {
  2179. *retp = unit;
  2180. goto out2;
  2181. }
  2182. } else {
  2183. if (unit_find(&ppp_units_idr, unit))
  2184. goto out2; /* unit already exists */
  2185. else {
  2186. /* darn, someone is cheating us? */
  2187. *retp = -EINVAL;
  2188. goto out2;
  2189. }
  2190. }
  2191. /* Initialize the new ppp unit */
  2192. ppp->file.index = unit;
  2193. sprintf(dev->name, "ppp%d", unit);
  2194. ret = register_netdev(dev);
  2195. if (ret != 0) {
  2196. unit_put(&ppp_units_idr, unit);
  2197. printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
  2198. dev->name, ret);
  2199. goto out2;
  2200. }
  2201. atomic_inc(&ppp_unit_count);
  2202. mutex_unlock(&all_ppp_mutex);
  2203. *retp = 0;
  2204. return ppp;
  2205. out2:
  2206. mutex_unlock(&all_ppp_mutex);
  2207. free_netdev(dev);
  2208. out1:
  2209. *retp = ret;
  2210. return NULL;
  2211. }
  2212. /*
  2213. * Initialize a ppp_file structure.
  2214. */
  2215. static void
  2216. init_ppp_file(struct ppp_file *pf, int kind)
  2217. {
  2218. pf->kind = kind;
  2219. skb_queue_head_init(&pf->xq);
  2220. skb_queue_head_init(&pf->rq);
  2221. atomic_set(&pf->refcnt, 1);
  2222. init_waitqueue_head(&pf->rwait);
  2223. }
  2224. /*
  2225. * Take down a ppp interface unit - called when the owning file
  2226. * (the one that created the unit) is closed or detached.
  2227. */
  2228. static void ppp_shutdown_interface(struct ppp *ppp)
  2229. {
  2230. struct net_device *dev;
  2231. mutex_lock(&all_ppp_mutex);
  2232. ppp_lock(ppp);
  2233. dev = ppp->dev;
  2234. ppp->dev = NULL;
  2235. ppp_unlock(ppp);
  2236. /* This will call dev_close() for us. */
  2237. if (dev) {
  2238. unregister_netdev(dev);
  2239. free_netdev(dev);
  2240. }
  2241. unit_put(&ppp_units_idr, ppp->file.index);
  2242. ppp->file.dead = 1;
  2243. ppp->owner = NULL;
  2244. wake_up_interruptible(&ppp->file.rwait);
  2245. mutex_unlock(&all_ppp_mutex);
  2246. }
  2247. /*
  2248. * Free the memory used by a ppp unit. This is only called once
  2249. * there are no channels connected to the unit and no file structs
  2250. * that reference the unit.
  2251. */
  2252. static void ppp_destroy_interface(struct ppp *ppp)
  2253. {
  2254. atomic_dec(&ppp_unit_count);
  2255. if (!ppp->file.dead || ppp->n_channels) {
  2256. /* "can't happen" */
  2257. printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
  2258. "n_channels=%d !\n", ppp, ppp->file.dead,
  2259. ppp->n_channels);
  2260. return;
  2261. }
  2262. ppp_ccp_closed(ppp);
  2263. if (ppp->vj) {
  2264. slhc_free(ppp->vj);
  2265. ppp->vj = NULL;
  2266. }
  2267. skb_queue_purge(&ppp->file.xq);
  2268. skb_queue_purge(&ppp->file.rq);
  2269. #ifdef CONFIG_PPP_MULTILINK
  2270. skb_queue_purge(&ppp->mrq);
  2271. #endif /* CONFIG_PPP_MULTILINK */
  2272. #ifdef CONFIG_PPP_FILTER
  2273. kfree(ppp->pass_filter);
  2274. ppp->pass_filter = NULL;
  2275. kfree(ppp->active_filter);
  2276. ppp->active_filter = NULL;
  2277. #endif /* CONFIG_PPP_FILTER */
  2278. if (ppp->xmit_pending)
  2279. kfree_skb(ppp->xmit_pending);
  2280. kfree(ppp);
  2281. }
  2282. /*
  2283. * Locate an existing ppp unit.
  2284. * The caller should have locked the all_ppp_mutex.
  2285. */
  2286. static struct ppp *
  2287. ppp_find_unit(int unit)
  2288. {
  2289. return unit_find(&ppp_units_idr, unit);
  2290. }
  2291. /*
  2292. * Locate an existing ppp channel.
  2293. * The caller should have locked the all_channels_lock.
  2294. * First we look in the new_channels list, then in the
  2295. * all_channels list. If found in the new_channels list,
  2296. * we move it to the all_channels list. This is for speed
  2297. * when we have a lot of channels in use.
  2298. */
  2299. static struct channel *
  2300. ppp_find_channel(int unit)
  2301. {
  2302. struct channel *pch;
  2303. list_for_each_entry(pch, &new_channels, list) {
  2304. if (pch->file.index == unit) {
  2305. list_move(&pch->list, &all_channels);
  2306. return pch;
  2307. }
  2308. }
  2309. list_for_each_entry(pch, &all_channels, list) {
  2310. if (pch->file.index == unit)
  2311. return pch;
  2312. }
  2313. return NULL;
  2314. }
  2315. /*
  2316. * Connect a PPP channel to a PPP interface unit.
  2317. */
  2318. static int
  2319. ppp_connect_channel(struct channel *pch, int unit)
  2320. {
  2321. struct ppp *ppp;
  2322. int ret = -ENXIO;
  2323. int hdrlen;
  2324. mutex_lock(&all_ppp_mutex);
  2325. ppp = ppp_find_unit(unit);
  2326. if (!ppp)
  2327. goto out;
  2328. write_lock_bh(&pch->upl);
  2329. ret = -EINVAL;
  2330. if (pch->ppp)
  2331. goto outl;
  2332. ppp_lock(ppp);
  2333. if (pch->file.hdrlen > ppp->file.hdrlen)
  2334. ppp->file.hdrlen = pch->file.hdrlen;
  2335. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  2336. if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
  2337. ppp->dev->hard_header_len = hdrlen;
  2338. list_add_tail(&pch->clist, &ppp->channels);
  2339. ++ppp->n_channels;
  2340. pch->ppp = ppp;
  2341. atomic_inc(&ppp->file.refcnt);
  2342. ppp_unlock(ppp);
  2343. ret = 0;
  2344. outl:
  2345. write_unlock_bh(&pch->upl);
  2346. out:
  2347. mutex_unlock(&all_ppp_mutex);
  2348. return ret;
  2349. }
  2350. /*
  2351. * Disconnect a channel from its ppp unit.
  2352. */
  2353. static int
  2354. ppp_disconnect_channel(struct channel *pch)
  2355. {
  2356. struct ppp *ppp;
  2357. int err = -EINVAL;
  2358. write_lock_bh(&pch->upl);
  2359. ppp = pch->ppp;
  2360. pch->ppp = NULL;
  2361. write_unlock_bh(&pch->upl);
  2362. if (ppp) {
  2363. /* remove it from the ppp unit's list */
  2364. ppp_lock(ppp);
  2365. list_del(&pch->clist);
  2366. if (--ppp->n_channels == 0)
  2367. wake_up_interruptible(&ppp->file.rwait);
  2368. ppp_unlock(ppp);
  2369. if (atomic_dec_and_test(&ppp->file.refcnt))
  2370. ppp_destroy_interface(ppp);
  2371. err = 0;
  2372. }
  2373. return err;
  2374. }
  2375. /*
  2376. * Free up the resources used by a ppp channel.
  2377. */
  2378. static void ppp_destroy_channel(struct channel *pch)
  2379. {
  2380. atomic_dec(&channel_count);
  2381. if (!pch->file.dead) {
  2382. /* "can't happen" */
  2383. printk(KERN_ERR "ppp: destroying undead channel %p !\n",
  2384. pch);
  2385. return;
  2386. }
  2387. skb_queue_purge(&pch->file.xq);
  2388. skb_queue_purge(&pch->file.rq);
  2389. kfree(pch);
  2390. }
  2391. static void __exit ppp_cleanup(void)
  2392. {
  2393. /* should never happen */
  2394. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  2395. printk(KERN_ERR "PPP: removing module but units remain!\n");
  2396. unregister_chrdev(PPP_MAJOR, "ppp");
  2397. device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
  2398. class_destroy(ppp_class);
  2399. idr_destroy(&ppp_units_idr);
  2400. }
  2401. /*
  2402. * Units handling. Caller must protect concurrent access
  2403. * by holding all_ppp_mutex
  2404. */
  2405. /* get new free unit number and associate pointer with it */
  2406. static int unit_get(struct idr *p, void *ptr)
  2407. {
  2408. int unit, err;
  2409. again:
  2410. if (idr_pre_get(p, GFP_KERNEL) == 0) {
  2411. printk(KERN_ERR "Out of memory expanding drawable idr\n");
  2412. return -ENOMEM;
  2413. }
  2414. err = idr_get_new_above(p, ptr, 0, &unit);
  2415. if (err == -EAGAIN)
  2416. goto again;
  2417. return unit;
  2418. }
  2419. /* put unit number back to a pool */
  2420. static void unit_put(struct idr *p, int n)
  2421. {
  2422. idr_remove(p, n);
  2423. }
  2424. /* get pointer associated with the number */
  2425. static void *unit_find(struct idr *p, int n)
  2426. {
  2427. return idr_find(p, n);
  2428. }
  2429. /* Module/initialization stuff */
  2430. module_init(ppp_init);
  2431. module_exit(ppp_cleanup);
  2432. EXPORT_SYMBOL(ppp_register_channel);
  2433. EXPORT_SYMBOL(ppp_unregister_channel);
  2434. EXPORT_SYMBOL(ppp_channel_index);
  2435. EXPORT_SYMBOL(ppp_unit_number);
  2436. EXPORT_SYMBOL(ppp_input);
  2437. EXPORT_SYMBOL(ppp_input_error);
  2438. EXPORT_SYMBOL(ppp_output_wakeup);
  2439. EXPORT_SYMBOL(ppp_register_compressor);
  2440. EXPORT_SYMBOL(ppp_unregister_compressor);
  2441. MODULE_LICENSE("GPL");
  2442. MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
  2443. MODULE_ALIAS("/dev/ppp");