ppp_generic.c 67 KB

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