ppp_generic.c 66 KB

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