ppp_generic.c 66 KB

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