ppp_generic.c 66 KB

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