ppp_generic.c 66 KB

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