ppp_generic.c 66 KB

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