ppp_generic.c 67 KB

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