af_packet.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * PACKET - implements raw packet sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  11. *
  12. * Fixes:
  13. * Alan Cox : verify_area() now used correctly
  14. * Alan Cox : new skbuff lists, look ma no backlogs!
  15. * Alan Cox : tidied skbuff lists.
  16. * Alan Cox : Now uses generic datagram routines I
  17. * added. Also fixed the peek/read crash
  18. * from all old Linux datagram code.
  19. * Alan Cox : Uses the improved datagram code.
  20. * Alan Cox : Added NULL's for socket options.
  21. * Alan Cox : Re-commented the code.
  22. * Alan Cox : Use new kernel side addressing
  23. * Rob Janssen : Correct MTU usage.
  24. * Dave Platt : Counter leaks caused by incorrect
  25. * interrupt locking and some slightly
  26. * dubious gcc output. Can you read
  27. * compiler: it said _VOLATILE_
  28. * Richard Kooijman : Timestamp fixes.
  29. * Alan Cox : New buffers. Use sk->mac.raw.
  30. * Alan Cox : sendmsg/recvmsg support.
  31. * Alan Cox : Protocol setting support
  32. * Alexey Kuznetsov : Untied from IPv4 stack.
  33. * Cyrus Durgin : Fixed kerneld for kmod.
  34. * Michal Ostrowski : Module initialization cleanup.
  35. * Ulises Alonso : Frame number limit removal and
  36. * packet_set_ring memory leak.
  37. * Eric Biederman : Allow for > 8 byte hardware addresses.
  38. * The convention is that longer addresses
  39. * will simply extend the hardware address
  40. * byte arrays at the end of sockaddr_ll
  41. * and packet_mreq.
  42. * Johann Baudy : Added TX RING.
  43. *
  44. * This program is free software; you can redistribute it and/or
  45. * modify it under the terms of the GNU General Public License
  46. * as published by the Free Software Foundation; either version
  47. * 2 of the License, or (at your option) any later version.
  48. *
  49. */
  50. #include <linux/types.h>
  51. #include <linux/mm.h>
  52. #include <linux/capability.h>
  53. #include <linux/fcntl.h>
  54. #include <linux/socket.h>
  55. #include <linux/in.h>
  56. #include <linux/inet.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/if_packet.h>
  59. #include <linux/wireless.h>
  60. #include <linux/kernel.h>
  61. #include <linux/kmod.h>
  62. #include <linux/slab.h>
  63. #include <linux/vmalloc.h>
  64. #include <net/net_namespace.h>
  65. #include <net/ip.h>
  66. #include <net/protocol.h>
  67. #include <linux/skbuff.h>
  68. #include <net/sock.h>
  69. #include <linux/errno.h>
  70. #include <linux/timer.h>
  71. #include <asm/system.h>
  72. #include <asm/uaccess.h>
  73. #include <asm/ioctls.h>
  74. #include <asm/page.h>
  75. #include <asm/cacheflush.h>
  76. #include <asm/io.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <linux/poll.h>
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/mutex.h>
  83. #include <linux/if_vlan.h>
  84. #include <linux/virtio_net.h>
  85. #include <linux/errqueue.h>
  86. #include <linux/net_tstamp.h>
  87. #ifdef CONFIG_INET
  88. #include <net/inet_common.h>
  89. #endif
  90. /*
  91. Assumptions:
  92. - if device has no dev->hard_header routine, it adds and removes ll header
  93. inside itself. In this case ll header is invisible outside of device,
  94. but higher levels still should reserve dev->hard_header_len.
  95. Some devices are enough clever to reallocate skb, when header
  96. will not fit to reserved space (tunnel), another ones are silly
  97. (PPP).
  98. - packet socket receives packets with pulled ll header,
  99. so that SOCK_RAW should push it back.
  100. On receive:
  101. -----------
  102. Incoming, dev->hard_header!=NULL
  103. mac_header -> ll header
  104. data -> data
  105. Outgoing, dev->hard_header!=NULL
  106. mac_header -> ll header
  107. data -> ll header
  108. Incoming, dev->hard_header==NULL
  109. mac_header -> UNKNOWN position. It is very likely, that it points to ll
  110. header. PPP makes it, that is wrong, because introduce
  111. assymetry between rx and tx paths.
  112. data -> data
  113. Outgoing, dev->hard_header==NULL
  114. mac_header -> data. ll header is still not built!
  115. data -> data
  116. Resume
  117. If dev->hard_header==NULL we are unlikely to restore sensible ll header.
  118. On transmit:
  119. ------------
  120. dev->hard_header != NULL
  121. mac_header -> ll header
  122. data -> ll header
  123. dev->hard_header == NULL (ll header is added by device, we cannot control it)
  124. mac_header -> data
  125. data -> data
  126. We should set nh.raw on output to correct posistion,
  127. packet classifier depends on it.
  128. */
  129. /* Private packet socket structures. */
  130. struct packet_mclist {
  131. struct packet_mclist *next;
  132. int ifindex;
  133. int count;
  134. unsigned short type;
  135. unsigned short alen;
  136. unsigned char addr[MAX_ADDR_LEN];
  137. };
  138. /* identical to struct packet_mreq except it has
  139. * a longer address field.
  140. */
  141. struct packet_mreq_max {
  142. int mr_ifindex;
  143. unsigned short mr_type;
  144. unsigned short mr_alen;
  145. unsigned char mr_address[MAX_ADDR_LEN];
  146. };
  147. static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
  148. int closing, int tx_ring);
  149. struct pgv {
  150. char *buffer;
  151. };
  152. struct packet_ring_buffer {
  153. struct pgv *pg_vec;
  154. unsigned int head;
  155. unsigned int frames_per_block;
  156. unsigned int frame_size;
  157. unsigned int frame_max;
  158. unsigned int pg_vec_order;
  159. unsigned int pg_vec_pages;
  160. unsigned int pg_vec_len;
  161. atomic_t pending;
  162. };
  163. struct packet_sock;
  164. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
  165. static void packet_flush_mclist(struct sock *sk);
  166. struct packet_fanout;
  167. struct packet_sock {
  168. /* struct sock has to be the first member of packet_sock */
  169. struct sock sk;
  170. struct packet_fanout *fanout;
  171. struct tpacket_stats stats;
  172. struct packet_ring_buffer rx_ring;
  173. struct packet_ring_buffer tx_ring;
  174. int copy_thresh;
  175. spinlock_t bind_lock;
  176. struct mutex pg_vec_lock;
  177. unsigned int running:1, /* prot_hook is attached*/
  178. auxdata:1,
  179. origdev:1,
  180. has_vnet_hdr:1;
  181. int ifindex; /* bound device */
  182. __be16 num;
  183. struct packet_mclist *mclist;
  184. atomic_t mapped;
  185. enum tpacket_versions tp_version;
  186. unsigned int tp_hdrlen;
  187. unsigned int tp_reserve;
  188. unsigned int tp_loss:1;
  189. unsigned int tp_tstamp;
  190. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  191. };
  192. #define PACKET_FANOUT_MAX 256
  193. struct packet_fanout {
  194. #ifdef CONFIG_NET_NS
  195. struct net *net;
  196. #endif
  197. unsigned int num_members;
  198. u16 id;
  199. u8 type;
  200. u8 pad;
  201. atomic_t rr_cur;
  202. struct list_head list;
  203. struct sock *arr[PACKET_FANOUT_MAX];
  204. spinlock_t lock;
  205. atomic_t sk_ref;
  206. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  207. };
  208. struct packet_skb_cb {
  209. unsigned int origlen;
  210. union {
  211. struct sockaddr_pkt pkt;
  212. struct sockaddr_ll ll;
  213. } sa;
  214. };
  215. #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
  216. static inline struct packet_sock *pkt_sk(struct sock *sk)
  217. {
  218. return (struct packet_sock *)sk;
  219. }
  220. static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
  221. static void __fanout_link(struct sock *sk, struct packet_sock *po);
  222. /* register_prot_hook must be invoked with the po->bind_lock held,
  223. * or from a context in which asynchronous accesses to the packet
  224. * socket is not possible (packet_create()).
  225. */
  226. static void register_prot_hook(struct sock *sk)
  227. {
  228. struct packet_sock *po = pkt_sk(sk);
  229. if (!po->running) {
  230. if (po->fanout)
  231. __fanout_link(sk, po);
  232. else
  233. dev_add_pack(&po->prot_hook);
  234. sock_hold(sk);
  235. po->running = 1;
  236. }
  237. }
  238. /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
  239. * held. If the sync parameter is true, we will temporarily drop
  240. * the po->bind_lock and do a synchronize_net to make sure no
  241. * asynchronous packet processing paths still refer to the elements
  242. * of po->prot_hook. If the sync parameter is false, it is the
  243. * callers responsibility to take care of this.
  244. */
  245. static void __unregister_prot_hook(struct sock *sk, bool sync)
  246. {
  247. struct packet_sock *po = pkt_sk(sk);
  248. po->running = 0;
  249. if (po->fanout)
  250. __fanout_unlink(sk, po);
  251. else
  252. __dev_remove_pack(&po->prot_hook);
  253. __sock_put(sk);
  254. if (sync) {
  255. spin_unlock(&po->bind_lock);
  256. synchronize_net();
  257. spin_lock(&po->bind_lock);
  258. }
  259. }
  260. static void unregister_prot_hook(struct sock *sk, bool sync)
  261. {
  262. struct packet_sock *po = pkt_sk(sk);
  263. if (po->running)
  264. __unregister_prot_hook(sk, sync);
  265. }
  266. static inline __pure struct page *pgv_to_page(void *addr)
  267. {
  268. if (is_vmalloc_addr(addr))
  269. return vmalloc_to_page(addr);
  270. return virt_to_page(addr);
  271. }
  272. static void __packet_set_status(struct packet_sock *po, void *frame, int status)
  273. {
  274. union {
  275. struct tpacket_hdr *h1;
  276. struct tpacket2_hdr *h2;
  277. void *raw;
  278. } h;
  279. h.raw = frame;
  280. switch (po->tp_version) {
  281. case TPACKET_V1:
  282. h.h1->tp_status = status;
  283. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  284. break;
  285. case TPACKET_V2:
  286. h.h2->tp_status = status;
  287. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  288. break;
  289. default:
  290. pr_err("TPACKET version not supported\n");
  291. BUG();
  292. }
  293. smp_wmb();
  294. }
  295. static int __packet_get_status(struct packet_sock *po, void *frame)
  296. {
  297. union {
  298. struct tpacket_hdr *h1;
  299. struct tpacket2_hdr *h2;
  300. void *raw;
  301. } h;
  302. smp_rmb();
  303. h.raw = frame;
  304. switch (po->tp_version) {
  305. case TPACKET_V1:
  306. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  307. return h.h1->tp_status;
  308. case TPACKET_V2:
  309. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  310. return h.h2->tp_status;
  311. default:
  312. pr_err("TPACKET version not supported\n");
  313. BUG();
  314. return 0;
  315. }
  316. }
  317. static void *packet_lookup_frame(struct packet_sock *po,
  318. struct packet_ring_buffer *rb,
  319. unsigned int position,
  320. int status)
  321. {
  322. unsigned int pg_vec_pos, frame_offset;
  323. union {
  324. struct tpacket_hdr *h1;
  325. struct tpacket2_hdr *h2;
  326. void *raw;
  327. } h;
  328. pg_vec_pos = position / rb->frames_per_block;
  329. frame_offset = position % rb->frames_per_block;
  330. h.raw = rb->pg_vec[pg_vec_pos].buffer +
  331. (frame_offset * rb->frame_size);
  332. if (status != __packet_get_status(po, h.raw))
  333. return NULL;
  334. return h.raw;
  335. }
  336. static inline void *packet_current_frame(struct packet_sock *po,
  337. struct packet_ring_buffer *rb,
  338. int status)
  339. {
  340. return packet_lookup_frame(po, rb, rb->head, status);
  341. }
  342. static inline void *packet_previous_frame(struct packet_sock *po,
  343. struct packet_ring_buffer *rb,
  344. int status)
  345. {
  346. unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
  347. return packet_lookup_frame(po, rb, previous, status);
  348. }
  349. static inline void packet_increment_head(struct packet_ring_buffer *buff)
  350. {
  351. buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
  352. }
  353. static void packet_sock_destruct(struct sock *sk)
  354. {
  355. skb_queue_purge(&sk->sk_error_queue);
  356. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  357. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  358. if (!sock_flag(sk, SOCK_DEAD)) {
  359. pr_err("Attempt to release alive packet socket: %p\n", sk);
  360. return;
  361. }
  362. sk_refcnt_debug_dec(sk);
  363. }
  364. static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
  365. {
  366. int x = atomic_read(&f->rr_cur) + 1;
  367. if (x >= num)
  368. x = 0;
  369. return x;
  370. }
  371. static struct sock *fanout_demux_hash(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
  372. {
  373. u32 idx, hash = skb->rxhash;
  374. idx = ((u64)hash * num) >> 32;
  375. return f->arr[idx];
  376. }
  377. static struct sock *fanout_demux_lb(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
  378. {
  379. int cur, old;
  380. cur = atomic_read(&f->rr_cur);
  381. while ((old = atomic_cmpxchg(&f->rr_cur, cur,
  382. fanout_rr_next(f, num))) != cur)
  383. cur = old;
  384. return f->arr[cur];
  385. }
  386. static int packet_rcv_fanout_hash(struct sk_buff *skb, struct net_device *dev,
  387. struct packet_type *pt, struct net_device *orig_dev)
  388. {
  389. struct packet_fanout *f = pt->af_packet_priv;
  390. unsigned int num = f->num_members;
  391. struct packet_sock *po;
  392. struct sock *sk;
  393. if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
  394. !num) {
  395. kfree_skb(skb);
  396. return 0;
  397. }
  398. skb_get_rxhash(skb);
  399. sk = fanout_demux_hash(f, skb, num);
  400. po = pkt_sk(sk);
  401. return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
  402. }
  403. static int packet_rcv_fanout_lb(struct sk_buff *skb, struct net_device *dev,
  404. struct packet_type *pt, struct net_device *orig_dev)
  405. {
  406. struct packet_fanout *f = pt->af_packet_priv;
  407. unsigned int num = f->num_members;
  408. struct packet_sock *po;
  409. struct sock *sk;
  410. if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
  411. !num) {
  412. kfree_skb(skb);
  413. return 0;
  414. }
  415. sk = fanout_demux_lb(f, skb, num);
  416. po = pkt_sk(sk);
  417. return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
  418. }
  419. static DEFINE_MUTEX(fanout_mutex);
  420. static LIST_HEAD(fanout_list);
  421. static void __fanout_link(struct sock *sk, struct packet_sock *po)
  422. {
  423. struct packet_fanout *f = po->fanout;
  424. spin_lock(&f->lock);
  425. f->arr[f->num_members] = sk;
  426. smp_wmb();
  427. f->num_members++;
  428. spin_unlock(&f->lock);
  429. }
  430. static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
  431. {
  432. struct packet_fanout *f = po->fanout;
  433. int i;
  434. spin_lock(&f->lock);
  435. for (i = 0; i < f->num_members; i++) {
  436. if (f->arr[i] == sk)
  437. break;
  438. }
  439. BUG_ON(i >= f->num_members);
  440. f->arr[i] = f->arr[f->num_members - 1];
  441. f->num_members--;
  442. spin_unlock(&f->lock);
  443. }
  444. static int fanout_add(struct sock *sk, u16 id, u8 type)
  445. {
  446. struct packet_sock *po = pkt_sk(sk);
  447. struct packet_fanout *f, *match;
  448. int err;
  449. switch (type) {
  450. case PACKET_FANOUT_HASH:
  451. case PACKET_FANOUT_LB:
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. if (!po->running)
  457. return -EINVAL;
  458. if (po->fanout)
  459. return -EALREADY;
  460. mutex_lock(&fanout_mutex);
  461. match = NULL;
  462. list_for_each_entry(f, &fanout_list, list) {
  463. if (f->id == id &&
  464. read_pnet(&f->net) == sock_net(sk)) {
  465. match = f;
  466. break;
  467. }
  468. }
  469. if (!match) {
  470. match = kzalloc(sizeof(*match), GFP_KERNEL);
  471. if (match) {
  472. write_pnet(&match->net, sock_net(sk));
  473. match->id = id;
  474. match->type = type;
  475. atomic_set(&match->rr_cur, 0);
  476. INIT_LIST_HEAD(&match->list);
  477. spin_lock_init(&match->lock);
  478. atomic_set(&match->sk_ref, 0);
  479. match->prot_hook.type = po->prot_hook.type;
  480. match->prot_hook.dev = po->prot_hook.dev;
  481. switch (type) {
  482. case PACKET_FANOUT_HASH:
  483. match->prot_hook.func = packet_rcv_fanout_hash;
  484. break;
  485. case PACKET_FANOUT_LB:
  486. match->prot_hook.func = packet_rcv_fanout_lb;
  487. break;
  488. }
  489. match->prot_hook.af_packet_priv = match;
  490. dev_add_pack(&match->prot_hook);
  491. list_add(&match->list, &fanout_list);
  492. }
  493. }
  494. err = -ENOMEM;
  495. if (match) {
  496. err = -EINVAL;
  497. if (match->type == type &&
  498. match->prot_hook.type == po->prot_hook.type &&
  499. match->prot_hook.dev == po->prot_hook.dev) {
  500. err = -ENOSPC;
  501. if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
  502. __dev_remove_pack(&po->prot_hook);
  503. po->fanout = match;
  504. atomic_inc(&match->sk_ref);
  505. __fanout_link(sk, po);
  506. err = 0;
  507. }
  508. }
  509. }
  510. mutex_unlock(&fanout_mutex);
  511. return err;
  512. }
  513. static void fanout_release(struct sock *sk)
  514. {
  515. struct packet_sock *po = pkt_sk(sk);
  516. struct packet_fanout *f;
  517. f = po->fanout;
  518. if (!f)
  519. return;
  520. po->fanout = NULL;
  521. mutex_lock(&fanout_mutex);
  522. if (atomic_dec_and_test(&f->sk_ref)) {
  523. list_del(&f->list);
  524. dev_remove_pack(&f->prot_hook);
  525. kfree(f);
  526. }
  527. mutex_unlock(&fanout_mutex);
  528. }
  529. static const struct proto_ops packet_ops;
  530. static const struct proto_ops packet_ops_spkt;
  531. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
  532. struct packet_type *pt, struct net_device *orig_dev)
  533. {
  534. struct sock *sk;
  535. struct sockaddr_pkt *spkt;
  536. /*
  537. * When we registered the protocol we saved the socket in the data
  538. * field for just this event.
  539. */
  540. sk = pt->af_packet_priv;
  541. /*
  542. * Yank back the headers [hope the device set this
  543. * right or kerboom...]
  544. *
  545. * Incoming packets have ll header pulled,
  546. * push it back.
  547. *
  548. * For outgoing ones skb->data == skb_mac_header(skb)
  549. * so that this procedure is noop.
  550. */
  551. if (skb->pkt_type == PACKET_LOOPBACK)
  552. goto out;
  553. if (!net_eq(dev_net(dev), sock_net(sk)))
  554. goto out;
  555. skb = skb_share_check(skb, GFP_ATOMIC);
  556. if (skb == NULL)
  557. goto oom;
  558. /* drop any routing info */
  559. skb_dst_drop(skb);
  560. /* drop conntrack reference */
  561. nf_reset(skb);
  562. spkt = &PACKET_SKB_CB(skb)->sa.pkt;
  563. skb_push(skb, skb->data - skb_mac_header(skb));
  564. /*
  565. * The SOCK_PACKET socket receives _all_ frames.
  566. */
  567. spkt->spkt_family = dev->type;
  568. strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  569. spkt->spkt_protocol = skb->protocol;
  570. /*
  571. * Charge the memory to the socket. This is done specifically
  572. * to prevent sockets using all the memory up.
  573. */
  574. if (sock_queue_rcv_skb(sk, skb) == 0)
  575. return 0;
  576. out:
  577. kfree_skb(skb);
  578. oom:
  579. return 0;
  580. }
  581. /*
  582. * Output a raw packet to a device layer. This bypasses all the other
  583. * protocol layers and you must therefore supply it with a complete frame
  584. */
  585. static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
  586. struct msghdr *msg, size_t len)
  587. {
  588. struct sock *sk = sock->sk;
  589. struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
  590. struct sk_buff *skb = NULL;
  591. struct net_device *dev;
  592. __be16 proto = 0;
  593. int err;
  594. /*
  595. * Get and verify the address.
  596. */
  597. if (saddr) {
  598. if (msg->msg_namelen < sizeof(struct sockaddr))
  599. return -EINVAL;
  600. if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
  601. proto = saddr->spkt_protocol;
  602. } else
  603. return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
  604. /*
  605. * Find the device first to size check it
  606. */
  607. saddr->spkt_device[13] = 0;
  608. retry:
  609. rcu_read_lock();
  610. dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
  611. err = -ENODEV;
  612. if (dev == NULL)
  613. goto out_unlock;
  614. err = -ENETDOWN;
  615. if (!(dev->flags & IFF_UP))
  616. goto out_unlock;
  617. /*
  618. * You may not queue a frame bigger than the mtu. This is the lowest level
  619. * raw protocol and you must do your own fragmentation at this level.
  620. */
  621. err = -EMSGSIZE;
  622. if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
  623. goto out_unlock;
  624. if (!skb) {
  625. size_t reserved = LL_RESERVED_SPACE(dev);
  626. unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
  627. rcu_read_unlock();
  628. skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
  629. if (skb == NULL)
  630. return -ENOBUFS;
  631. /* FIXME: Save some space for broken drivers that write a hard
  632. * header at transmission time by themselves. PPP is the notable
  633. * one here. This should really be fixed at the driver level.
  634. */
  635. skb_reserve(skb, reserved);
  636. skb_reset_network_header(skb);
  637. /* Try to align data part correctly */
  638. if (hhlen) {
  639. skb->data -= hhlen;
  640. skb->tail -= hhlen;
  641. if (len < hhlen)
  642. skb_reset_network_header(skb);
  643. }
  644. err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  645. if (err)
  646. goto out_free;
  647. goto retry;
  648. }
  649. if (len > (dev->mtu + dev->hard_header_len)) {
  650. /* Earlier code assumed this would be a VLAN pkt,
  651. * double-check this now that we have the actual
  652. * packet in hand.
  653. */
  654. struct ethhdr *ehdr;
  655. skb_reset_mac_header(skb);
  656. ehdr = eth_hdr(skb);
  657. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  658. err = -EMSGSIZE;
  659. goto out_unlock;
  660. }
  661. }
  662. skb->protocol = proto;
  663. skb->dev = dev;
  664. skb->priority = sk->sk_priority;
  665. skb->mark = sk->sk_mark;
  666. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  667. if (err < 0)
  668. goto out_unlock;
  669. dev_queue_xmit(skb);
  670. rcu_read_unlock();
  671. return len;
  672. out_unlock:
  673. rcu_read_unlock();
  674. out_free:
  675. kfree_skb(skb);
  676. return err;
  677. }
  678. static inline unsigned int run_filter(const struct sk_buff *skb,
  679. const struct sock *sk,
  680. unsigned int res)
  681. {
  682. struct sk_filter *filter;
  683. rcu_read_lock();
  684. filter = rcu_dereference(sk->sk_filter);
  685. if (filter != NULL)
  686. res = SK_RUN_FILTER(filter, skb);
  687. rcu_read_unlock();
  688. return res;
  689. }
  690. /*
  691. * This function makes lazy skb cloning in hope that most of packets
  692. * are discarded by BPF.
  693. *
  694. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  695. * and skb->cb are mangled. It works because (and until) packets
  696. * falling here are owned by current CPU. Output packets are cloned
  697. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  698. * sequencially, so that if we return skb to original state on exit,
  699. * we will not harm anyone.
  700. */
  701. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  702. struct packet_type *pt, struct net_device *orig_dev)
  703. {
  704. struct sock *sk;
  705. struct sockaddr_ll *sll;
  706. struct packet_sock *po;
  707. u8 *skb_head = skb->data;
  708. int skb_len = skb->len;
  709. unsigned int snaplen, res;
  710. if (skb->pkt_type == PACKET_LOOPBACK)
  711. goto drop;
  712. sk = pt->af_packet_priv;
  713. po = pkt_sk(sk);
  714. if (!net_eq(dev_net(dev), sock_net(sk)))
  715. goto drop;
  716. skb->dev = dev;
  717. if (dev->header_ops) {
  718. /* The device has an explicit notion of ll header,
  719. * exported to higher levels.
  720. *
  721. * Otherwise, the device hides details of its frame
  722. * structure, so that corresponding packet head is
  723. * never delivered to user.
  724. */
  725. if (sk->sk_type != SOCK_DGRAM)
  726. skb_push(skb, skb->data - skb_mac_header(skb));
  727. else if (skb->pkt_type == PACKET_OUTGOING) {
  728. /* Special case: outgoing packets have ll header at head */
  729. skb_pull(skb, skb_network_offset(skb));
  730. }
  731. }
  732. snaplen = skb->len;
  733. res = run_filter(skb, sk, snaplen);
  734. if (!res)
  735. goto drop_n_restore;
  736. if (snaplen > res)
  737. snaplen = res;
  738. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  739. (unsigned)sk->sk_rcvbuf)
  740. goto drop_n_acct;
  741. if (skb_shared(skb)) {
  742. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  743. if (nskb == NULL)
  744. goto drop_n_acct;
  745. if (skb_head != skb->data) {
  746. skb->data = skb_head;
  747. skb->len = skb_len;
  748. }
  749. kfree_skb(skb);
  750. skb = nskb;
  751. }
  752. BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
  753. sizeof(skb->cb));
  754. sll = &PACKET_SKB_CB(skb)->sa.ll;
  755. sll->sll_family = AF_PACKET;
  756. sll->sll_hatype = dev->type;
  757. sll->sll_protocol = skb->protocol;
  758. sll->sll_pkttype = skb->pkt_type;
  759. if (unlikely(po->origdev))
  760. sll->sll_ifindex = orig_dev->ifindex;
  761. else
  762. sll->sll_ifindex = dev->ifindex;
  763. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  764. PACKET_SKB_CB(skb)->origlen = skb->len;
  765. if (pskb_trim(skb, snaplen))
  766. goto drop_n_acct;
  767. skb_set_owner_r(skb, sk);
  768. skb->dev = NULL;
  769. skb_dst_drop(skb);
  770. /* drop conntrack reference */
  771. nf_reset(skb);
  772. spin_lock(&sk->sk_receive_queue.lock);
  773. po->stats.tp_packets++;
  774. skb->dropcount = atomic_read(&sk->sk_drops);
  775. __skb_queue_tail(&sk->sk_receive_queue, skb);
  776. spin_unlock(&sk->sk_receive_queue.lock);
  777. sk->sk_data_ready(sk, skb->len);
  778. return 0;
  779. drop_n_acct:
  780. po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
  781. drop_n_restore:
  782. if (skb_head != skb->data && skb_shared(skb)) {
  783. skb->data = skb_head;
  784. skb->len = skb_len;
  785. }
  786. drop:
  787. consume_skb(skb);
  788. return 0;
  789. }
  790. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  791. struct packet_type *pt, struct net_device *orig_dev)
  792. {
  793. struct sock *sk;
  794. struct packet_sock *po;
  795. struct sockaddr_ll *sll;
  796. union {
  797. struct tpacket_hdr *h1;
  798. struct tpacket2_hdr *h2;
  799. void *raw;
  800. } h;
  801. u8 *skb_head = skb->data;
  802. int skb_len = skb->len;
  803. unsigned int snaplen, res;
  804. unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
  805. unsigned short macoff, netoff, hdrlen;
  806. struct sk_buff *copy_skb = NULL;
  807. struct timeval tv;
  808. struct timespec ts;
  809. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  810. if (skb->pkt_type == PACKET_LOOPBACK)
  811. goto drop;
  812. sk = pt->af_packet_priv;
  813. po = pkt_sk(sk);
  814. if (!net_eq(dev_net(dev), sock_net(sk)))
  815. goto drop;
  816. if (dev->header_ops) {
  817. if (sk->sk_type != SOCK_DGRAM)
  818. skb_push(skb, skb->data - skb_mac_header(skb));
  819. else if (skb->pkt_type == PACKET_OUTGOING) {
  820. /* Special case: outgoing packets have ll header at head */
  821. skb_pull(skb, skb_network_offset(skb));
  822. }
  823. }
  824. if (skb->ip_summed == CHECKSUM_PARTIAL)
  825. status |= TP_STATUS_CSUMNOTREADY;
  826. snaplen = skb->len;
  827. res = run_filter(skb, sk, snaplen);
  828. if (!res)
  829. goto drop_n_restore;
  830. if (snaplen > res)
  831. snaplen = res;
  832. if (sk->sk_type == SOCK_DGRAM) {
  833. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  834. po->tp_reserve;
  835. } else {
  836. unsigned maclen = skb_network_offset(skb);
  837. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  838. (maclen < 16 ? 16 : maclen)) +
  839. po->tp_reserve;
  840. macoff = netoff - maclen;
  841. }
  842. if (macoff + snaplen > po->rx_ring.frame_size) {
  843. if (po->copy_thresh &&
  844. atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
  845. (unsigned)sk->sk_rcvbuf) {
  846. if (skb_shared(skb)) {
  847. copy_skb = skb_clone(skb, GFP_ATOMIC);
  848. } else {
  849. copy_skb = skb_get(skb);
  850. skb_head = skb->data;
  851. }
  852. if (copy_skb)
  853. skb_set_owner_r(copy_skb, sk);
  854. }
  855. snaplen = po->rx_ring.frame_size - macoff;
  856. if ((int)snaplen < 0)
  857. snaplen = 0;
  858. }
  859. spin_lock(&sk->sk_receive_queue.lock);
  860. h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
  861. if (!h.raw)
  862. goto ring_is_full;
  863. packet_increment_head(&po->rx_ring);
  864. po->stats.tp_packets++;
  865. if (copy_skb) {
  866. status |= TP_STATUS_COPY;
  867. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  868. }
  869. if (!po->stats.tp_drops)
  870. status &= ~TP_STATUS_LOSING;
  871. spin_unlock(&sk->sk_receive_queue.lock);
  872. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  873. switch (po->tp_version) {
  874. case TPACKET_V1:
  875. h.h1->tp_len = skb->len;
  876. h.h1->tp_snaplen = snaplen;
  877. h.h1->tp_mac = macoff;
  878. h.h1->tp_net = netoff;
  879. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  880. && shhwtstamps->syststamp.tv64)
  881. tv = ktime_to_timeval(shhwtstamps->syststamp);
  882. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  883. && shhwtstamps->hwtstamp.tv64)
  884. tv = ktime_to_timeval(shhwtstamps->hwtstamp);
  885. else if (skb->tstamp.tv64)
  886. tv = ktime_to_timeval(skb->tstamp);
  887. else
  888. do_gettimeofday(&tv);
  889. h.h1->tp_sec = tv.tv_sec;
  890. h.h1->tp_usec = tv.tv_usec;
  891. hdrlen = sizeof(*h.h1);
  892. break;
  893. case TPACKET_V2:
  894. h.h2->tp_len = skb->len;
  895. h.h2->tp_snaplen = snaplen;
  896. h.h2->tp_mac = macoff;
  897. h.h2->tp_net = netoff;
  898. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  899. && shhwtstamps->syststamp.tv64)
  900. ts = ktime_to_timespec(shhwtstamps->syststamp);
  901. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  902. && shhwtstamps->hwtstamp.tv64)
  903. ts = ktime_to_timespec(shhwtstamps->hwtstamp);
  904. else if (skb->tstamp.tv64)
  905. ts = ktime_to_timespec(skb->tstamp);
  906. else
  907. getnstimeofday(&ts);
  908. h.h2->tp_sec = ts.tv_sec;
  909. h.h2->tp_nsec = ts.tv_nsec;
  910. if (vlan_tx_tag_present(skb)) {
  911. h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
  912. status |= TP_STATUS_VLAN_VALID;
  913. } else {
  914. h.h2->tp_vlan_tci = 0;
  915. }
  916. h.h2->tp_padding = 0;
  917. hdrlen = sizeof(*h.h2);
  918. break;
  919. default:
  920. BUG();
  921. }
  922. sll = h.raw + TPACKET_ALIGN(hdrlen);
  923. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  924. sll->sll_family = AF_PACKET;
  925. sll->sll_hatype = dev->type;
  926. sll->sll_protocol = skb->protocol;
  927. sll->sll_pkttype = skb->pkt_type;
  928. if (unlikely(po->origdev))
  929. sll->sll_ifindex = orig_dev->ifindex;
  930. else
  931. sll->sll_ifindex = dev->ifindex;
  932. __packet_set_status(po, h.raw, status);
  933. smp_mb();
  934. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  935. {
  936. u8 *start, *end;
  937. end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
  938. for (start = h.raw; start < end; start += PAGE_SIZE)
  939. flush_dcache_page(pgv_to_page(start));
  940. }
  941. #endif
  942. sk->sk_data_ready(sk, 0);
  943. drop_n_restore:
  944. if (skb_head != skb->data && skb_shared(skb)) {
  945. skb->data = skb_head;
  946. skb->len = skb_len;
  947. }
  948. drop:
  949. kfree_skb(skb);
  950. return 0;
  951. ring_is_full:
  952. po->stats.tp_drops++;
  953. spin_unlock(&sk->sk_receive_queue.lock);
  954. sk->sk_data_ready(sk, 0);
  955. kfree_skb(copy_skb);
  956. goto drop_n_restore;
  957. }
  958. static void tpacket_destruct_skb(struct sk_buff *skb)
  959. {
  960. struct packet_sock *po = pkt_sk(skb->sk);
  961. void *ph;
  962. BUG_ON(skb == NULL);
  963. if (likely(po->tx_ring.pg_vec)) {
  964. ph = skb_shinfo(skb)->destructor_arg;
  965. BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
  966. BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
  967. atomic_dec(&po->tx_ring.pending);
  968. __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
  969. }
  970. sock_wfree(skb);
  971. }
  972. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  973. void *frame, struct net_device *dev, int size_max,
  974. __be16 proto, unsigned char *addr)
  975. {
  976. union {
  977. struct tpacket_hdr *h1;
  978. struct tpacket2_hdr *h2;
  979. void *raw;
  980. } ph;
  981. int to_write, offset, len, tp_len, nr_frags, len_max;
  982. struct socket *sock = po->sk.sk_socket;
  983. struct page *page;
  984. void *data;
  985. int err;
  986. ph.raw = frame;
  987. skb->protocol = proto;
  988. skb->dev = dev;
  989. skb->priority = po->sk.sk_priority;
  990. skb->mark = po->sk.sk_mark;
  991. skb_shinfo(skb)->destructor_arg = ph.raw;
  992. switch (po->tp_version) {
  993. case TPACKET_V2:
  994. tp_len = ph.h2->tp_len;
  995. break;
  996. default:
  997. tp_len = ph.h1->tp_len;
  998. break;
  999. }
  1000. if (unlikely(tp_len > size_max)) {
  1001. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  1002. return -EMSGSIZE;
  1003. }
  1004. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1005. skb_reset_network_header(skb);
  1006. data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
  1007. to_write = tp_len;
  1008. if (sock->type == SOCK_DGRAM) {
  1009. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  1010. NULL, tp_len);
  1011. if (unlikely(err < 0))
  1012. return -EINVAL;
  1013. } else if (dev->hard_header_len) {
  1014. /* net device doesn't like empty head */
  1015. if (unlikely(tp_len <= dev->hard_header_len)) {
  1016. pr_err("packet size is too short (%d < %d)\n",
  1017. tp_len, dev->hard_header_len);
  1018. return -EINVAL;
  1019. }
  1020. skb_push(skb, dev->hard_header_len);
  1021. err = skb_store_bits(skb, 0, data,
  1022. dev->hard_header_len);
  1023. if (unlikely(err))
  1024. return err;
  1025. data += dev->hard_header_len;
  1026. to_write -= dev->hard_header_len;
  1027. }
  1028. err = -EFAULT;
  1029. offset = offset_in_page(data);
  1030. len_max = PAGE_SIZE - offset;
  1031. len = ((to_write > len_max) ? len_max : to_write);
  1032. skb->data_len = to_write;
  1033. skb->len += to_write;
  1034. skb->truesize += to_write;
  1035. atomic_add(to_write, &po->sk.sk_wmem_alloc);
  1036. while (likely(to_write)) {
  1037. nr_frags = skb_shinfo(skb)->nr_frags;
  1038. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  1039. pr_err("Packet exceed the number of skb frags(%lu)\n",
  1040. MAX_SKB_FRAGS);
  1041. return -EFAULT;
  1042. }
  1043. page = pgv_to_page(data);
  1044. data += len;
  1045. flush_dcache_page(page);
  1046. get_page(page);
  1047. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  1048. to_write -= len;
  1049. offset = 0;
  1050. len_max = PAGE_SIZE;
  1051. len = ((to_write > len_max) ? len_max : to_write);
  1052. }
  1053. return tp_len;
  1054. }
  1055. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  1056. {
  1057. struct sk_buff *skb;
  1058. struct net_device *dev;
  1059. __be16 proto;
  1060. bool need_rls_dev = false;
  1061. int err, reserve = 0;
  1062. void *ph;
  1063. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1064. int tp_len, size_max;
  1065. unsigned char *addr;
  1066. int len_sum = 0;
  1067. int status = 0;
  1068. mutex_lock(&po->pg_vec_lock);
  1069. err = -EBUSY;
  1070. if (saddr == NULL) {
  1071. dev = po->prot_hook.dev;
  1072. proto = po->num;
  1073. addr = NULL;
  1074. } else {
  1075. err = -EINVAL;
  1076. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1077. goto out;
  1078. if (msg->msg_namelen < (saddr->sll_halen
  1079. + offsetof(struct sockaddr_ll,
  1080. sll_addr)))
  1081. goto out;
  1082. proto = saddr->sll_protocol;
  1083. addr = saddr->sll_addr;
  1084. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  1085. need_rls_dev = true;
  1086. }
  1087. err = -ENXIO;
  1088. if (unlikely(dev == NULL))
  1089. goto out;
  1090. reserve = dev->hard_header_len;
  1091. err = -ENETDOWN;
  1092. if (unlikely(!(dev->flags & IFF_UP)))
  1093. goto out_put;
  1094. size_max = po->tx_ring.frame_size
  1095. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  1096. if (size_max > dev->mtu + reserve)
  1097. size_max = dev->mtu + reserve;
  1098. do {
  1099. ph = packet_current_frame(po, &po->tx_ring,
  1100. TP_STATUS_SEND_REQUEST);
  1101. if (unlikely(ph == NULL)) {
  1102. schedule();
  1103. continue;
  1104. }
  1105. status = TP_STATUS_SEND_REQUEST;
  1106. skb = sock_alloc_send_skb(&po->sk,
  1107. LL_ALLOCATED_SPACE(dev)
  1108. + sizeof(struct sockaddr_ll),
  1109. 0, &err);
  1110. if (unlikely(skb == NULL))
  1111. goto out_status;
  1112. tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
  1113. addr);
  1114. if (unlikely(tp_len < 0)) {
  1115. if (po->tp_loss) {
  1116. __packet_set_status(po, ph,
  1117. TP_STATUS_AVAILABLE);
  1118. packet_increment_head(&po->tx_ring);
  1119. kfree_skb(skb);
  1120. continue;
  1121. } else {
  1122. status = TP_STATUS_WRONG_FORMAT;
  1123. err = tp_len;
  1124. goto out_status;
  1125. }
  1126. }
  1127. skb->destructor = tpacket_destruct_skb;
  1128. __packet_set_status(po, ph, TP_STATUS_SENDING);
  1129. atomic_inc(&po->tx_ring.pending);
  1130. status = TP_STATUS_SEND_REQUEST;
  1131. err = dev_queue_xmit(skb);
  1132. if (unlikely(err > 0)) {
  1133. err = net_xmit_errno(err);
  1134. if (err && __packet_get_status(po, ph) ==
  1135. TP_STATUS_AVAILABLE) {
  1136. /* skb was destructed already */
  1137. skb = NULL;
  1138. goto out_status;
  1139. }
  1140. /*
  1141. * skb was dropped but not destructed yet;
  1142. * let's treat it like congestion or err < 0
  1143. */
  1144. err = 0;
  1145. }
  1146. packet_increment_head(&po->tx_ring);
  1147. len_sum += tp_len;
  1148. } while (likely((ph != NULL) ||
  1149. ((!(msg->msg_flags & MSG_DONTWAIT)) &&
  1150. (atomic_read(&po->tx_ring.pending))))
  1151. );
  1152. err = len_sum;
  1153. goto out_put;
  1154. out_status:
  1155. __packet_set_status(po, ph, status);
  1156. kfree_skb(skb);
  1157. out_put:
  1158. if (need_rls_dev)
  1159. dev_put(dev);
  1160. out:
  1161. mutex_unlock(&po->pg_vec_lock);
  1162. return err;
  1163. }
  1164. static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  1165. size_t reserve, size_t len,
  1166. size_t linear, int noblock,
  1167. int *err)
  1168. {
  1169. struct sk_buff *skb;
  1170. /* Under a page? Don't bother with paged skb. */
  1171. if (prepad + len < PAGE_SIZE || !linear)
  1172. linear = len;
  1173. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1174. err);
  1175. if (!skb)
  1176. return NULL;
  1177. skb_reserve(skb, reserve);
  1178. skb_put(skb, linear);
  1179. skb->data_len = len - linear;
  1180. skb->len += len - linear;
  1181. return skb;
  1182. }
  1183. static int packet_snd(struct socket *sock,
  1184. struct msghdr *msg, size_t len)
  1185. {
  1186. struct sock *sk = sock->sk;
  1187. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1188. struct sk_buff *skb;
  1189. struct net_device *dev;
  1190. __be16 proto;
  1191. bool need_rls_dev = false;
  1192. unsigned char *addr;
  1193. int err, reserve = 0;
  1194. struct virtio_net_hdr vnet_hdr = { 0 };
  1195. int offset = 0;
  1196. int vnet_hdr_len;
  1197. struct packet_sock *po = pkt_sk(sk);
  1198. unsigned short gso_type = 0;
  1199. /*
  1200. * Get and verify the address.
  1201. */
  1202. if (saddr == NULL) {
  1203. dev = po->prot_hook.dev;
  1204. proto = po->num;
  1205. addr = NULL;
  1206. } else {
  1207. err = -EINVAL;
  1208. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1209. goto out;
  1210. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  1211. goto out;
  1212. proto = saddr->sll_protocol;
  1213. addr = saddr->sll_addr;
  1214. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  1215. need_rls_dev = true;
  1216. }
  1217. err = -ENXIO;
  1218. if (dev == NULL)
  1219. goto out_unlock;
  1220. if (sock->type == SOCK_RAW)
  1221. reserve = dev->hard_header_len;
  1222. err = -ENETDOWN;
  1223. if (!(dev->flags & IFF_UP))
  1224. goto out_unlock;
  1225. if (po->has_vnet_hdr) {
  1226. vnet_hdr_len = sizeof(vnet_hdr);
  1227. err = -EINVAL;
  1228. if (len < vnet_hdr_len)
  1229. goto out_unlock;
  1230. len -= vnet_hdr_len;
  1231. err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
  1232. vnet_hdr_len);
  1233. if (err < 0)
  1234. goto out_unlock;
  1235. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1236. (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  1237. vnet_hdr.hdr_len))
  1238. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  1239. vnet_hdr.csum_offset + 2;
  1240. err = -EINVAL;
  1241. if (vnet_hdr.hdr_len > len)
  1242. goto out_unlock;
  1243. if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1244. switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1245. case VIRTIO_NET_HDR_GSO_TCPV4:
  1246. gso_type = SKB_GSO_TCPV4;
  1247. break;
  1248. case VIRTIO_NET_HDR_GSO_TCPV6:
  1249. gso_type = SKB_GSO_TCPV6;
  1250. break;
  1251. case VIRTIO_NET_HDR_GSO_UDP:
  1252. gso_type = SKB_GSO_UDP;
  1253. break;
  1254. default:
  1255. goto out_unlock;
  1256. }
  1257. if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1258. gso_type |= SKB_GSO_TCP_ECN;
  1259. if (vnet_hdr.gso_size == 0)
  1260. goto out_unlock;
  1261. }
  1262. }
  1263. err = -EMSGSIZE;
  1264. if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
  1265. goto out_unlock;
  1266. err = -ENOBUFS;
  1267. skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
  1268. LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
  1269. msg->msg_flags & MSG_DONTWAIT, &err);
  1270. if (skb == NULL)
  1271. goto out_unlock;
  1272. skb_set_network_header(skb, reserve);
  1273. err = -EINVAL;
  1274. if (sock->type == SOCK_DGRAM &&
  1275. (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
  1276. goto out_free;
  1277. /* Returns -EFAULT on error */
  1278. err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
  1279. if (err)
  1280. goto out_free;
  1281. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  1282. if (err < 0)
  1283. goto out_free;
  1284. if (!gso_type && (len > dev->mtu + reserve)) {
  1285. /* Earlier code assumed this would be a VLAN pkt,
  1286. * double-check this now that we have the actual
  1287. * packet in hand.
  1288. */
  1289. struct ethhdr *ehdr;
  1290. skb_reset_mac_header(skb);
  1291. ehdr = eth_hdr(skb);
  1292. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  1293. err = -EMSGSIZE;
  1294. goto out_free;
  1295. }
  1296. }
  1297. skb->protocol = proto;
  1298. skb->dev = dev;
  1299. skb->priority = sk->sk_priority;
  1300. skb->mark = sk->sk_mark;
  1301. if (po->has_vnet_hdr) {
  1302. if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1303. if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
  1304. vnet_hdr.csum_offset)) {
  1305. err = -EINVAL;
  1306. goto out_free;
  1307. }
  1308. }
  1309. skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
  1310. skb_shinfo(skb)->gso_type = gso_type;
  1311. /* Header must be checked, and gso_segs computed. */
  1312. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1313. skb_shinfo(skb)->gso_segs = 0;
  1314. len += vnet_hdr_len;
  1315. }
  1316. /*
  1317. * Now send it
  1318. */
  1319. err = dev_queue_xmit(skb);
  1320. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  1321. goto out_unlock;
  1322. if (need_rls_dev)
  1323. dev_put(dev);
  1324. return len;
  1325. out_free:
  1326. kfree_skb(skb);
  1327. out_unlock:
  1328. if (dev && need_rls_dev)
  1329. dev_put(dev);
  1330. out:
  1331. return err;
  1332. }
  1333. static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
  1334. struct msghdr *msg, size_t len)
  1335. {
  1336. struct sock *sk = sock->sk;
  1337. struct packet_sock *po = pkt_sk(sk);
  1338. if (po->tx_ring.pg_vec)
  1339. return tpacket_snd(po, msg);
  1340. else
  1341. return packet_snd(sock, msg, len);
  1342. }
  1343. /*
  1344. * Close a PACKET socket. This is fairly simple. We immediately go
  1345. * to 'closed' state and remove our protocol entry in the device list.
  1346. */
  1347. static int packet_release(struct socket *sock)
  1348. {
  1349. struct sock *sk = sock->sk;
  1350. struct packet_sock *po;
  1351. struct net *net;
  1352. struct tpacket_req req;
  1353. if (!sk)
  1354. return 0;
  1355. net = sock_net(sk);
  1356. po = pkt_sk(sk);
  1357. spin_lock_bh(&net->packet.sklist_lock);
  1358. sk_del_node_init_rcu(sk);
  1359. sock_prot_inuse_add(net, sk->sk_prot, -1);
  1360. spin_unlock_bh(&net->packet.sklist_lock);
  1361. spin_lock(&po->bind_lock);
  1362. unregister_prot_hook(sk, false);
  1363. if (po->prot_hook.dev) {
  1364. dev_put(po->prot_hook.dev);
  1365. po->prot_hook.dev = NULL;
  1366. }
  1367. spin_unlock(&po->bind_lock);
  1368. packet_flush_mclist(sk);
  1369. memset(&req, 0, sizeof(req));
  1370. if (po->rx_ring.pg_vec)
  1371. packet_set_ring(sk, &req, 1, 0);
  1372. if (po->tx_ring.pg_vec)
  1373. packet_set_ring(sk, &req, 1, 1);
  1374. fanout_release(sk);
  1375. synchronize_net();
  1376. /*
  1377. * Now the socket is dead. No more input will appear.
  1378. */
  1379. sock_orphan(sk);
  1380. sock->sk = NULL;
  1381. /* Purge queues */
  1382. skb_queue_purge(&sk->sk_receive_queue);
  1383. sk_refcnt_debug_release(sk);
  1384. sock_put(sk);
  1385. return 0;
  1386. }
  1387. /*
  1388. * Attach a packet hook.
  1389. */
  1390. static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
  1391. {
  1392. struct packet_sock *po = pkt_sk(sk);
  1393. if (po->fanout)
  1394. return -EINVAL;
  1395. lock_sock(sk);
  1396. spin_lock(&po->bind_lock);
  1397. unregister_prot_hook(sk, true);
  1398. po->num = protocol;
  1399. po->prot_hook.type = protocol;
  1400. if (po->prot_hook.dev)
  1401. dev_put(po->prot_hook.dev);
  1402. po->prot_hook.dev = dev;
  1403. po->ifindex = dev ? dev->ifindex : 0;
  1404. if (protocol == 0)
  1405. goto out_unlock;
  1406. if (!dev || (dev->flags & IFF_UP)) {
  1407. register_prot_hook(sk);
  1408. } else {
  1409. sk->sk_err = ENETDOWN;
  1410. if (!sock_flag(sk, SOCK_DEAD))
  1411. sk->sk_error_report(sk);
  1412. }
  1413. out_unlock:
  1414. spin_unlock(&po->bind_lock);
  1415. release_sock(sk);
  1416. return 0;
  1417. }
  1418. /*
  1419. * Bind a packet socket to a device
  1420. */
  1421. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  1422. int addr_len)
  1423. {
  1424. struct sock *sk = sock->sk;
  1425. char name[15];
  1426. struct net_device *dev;
  1427. int err = -ENODEV;
  1428. /*
  1429. * Check legality
  1430. */
  1431. if (addr_len != sizeof(struct sockaddr))
  1432. return -EINVAL;
  1433. strlcpy(name, uaddr->sa_data, sizeof(name));
  1434. dev = dev_get_by_name(sock_net(sk), name);
  1435. if (dev)
  1436. err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
  1437. return err;
  1438. }
  1439. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1440. {
  1441. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  1442. struct sock *sk = sock->sk;
  1443. struct net_device *dev = NULL;
  1444. int err;
  1445. /*
  1446. * Check legality
  1447. */
  1448. if (addr_len < sizeof(struct sockaddr_ll))
  1449. return -EINVAL;
  1450. if (sll->sll_family != AF_PACKET)
  1451. return -EINVAL;
  1452. if (sll->sll_ifindex) {
  1453. err = -ENODEV;
  1454. dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
  1455. if (dev == NULL)
  1456. goto out;
  1457. }
  1458. err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
  1459. out:
  1460. return err;
  1461. }
  1462. static struct proto packet_proto = {
  1463. .name = "PACKET",
  1464. .owner = THIS_MODULE,
  1465. .obj_size = sizeof(struct packet_sock),
  1466. };
  1467. /*
  1468. * Create a packet of type SOCK_PACKET.
  1469. */
  1470. static int packet_create(struct net *net, struct socket *sock, int protocol,
  1471. int kern)
  1472. {
  1473. struct sock *sk;
  1474. struct packet_sock *po;
  1475. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  1476. int err;
  1477. if (!capable(CAP_NET_RAW))
  1478. return -EPERM;
  1479. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  1480. sock->type != SOCK_PACKET)
  1481. return -ESOCKTNOSUPPORT;
  1482. sock->state = SS_UNCONNECTED;
  1483. err = -ENOBUFS;
  1484. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
  1485. if (sk == NULL)
  1486. goto out;
  1487. sock->ops = &packet_ops;
  1488. if (sock->type == SOCK_PACKET)
  1489. sock->ops = &packet_ops_spkt;
  1490. sock_init_data(sock, sk);
  1491. po = pkt_sk(sk);
  1492. sk->sk_family = PF_PACKET;
  1493. po->num = proto;
  1494. sk->sk_destruct = packet_sock_destruct;
  1495. sk_refcnt_debug_inc(sk);
  1496. /*
  1497. * Attach a protocol block
  1498. */
  1499. spin_lock_init(&po->bind_lock);
  1500. mutex_init(&po->pg_vec_lock);
  1501. po->prot_hook.func = packet_rcv;
  1502. if (sock->type == SOCK_PACKET)
  1503. po->prot_hook.func = packet_rcv_spkt;
  1504. po->prot_hook.af_packet_priv = sk;
  1505. if (proto) {
  1506. po->prot_hook.type = proto;
  1507. register_prot_hook(sk);
  1508. }
  1509. spin_lock_bh(&net->packet.sklist_lock);
  1510. sk_add_node_rcu(sk, &net->packet.sklist);
  1511. sock_prot_inuse_add(net, &packet_proto, 1);
  1512. spin_unlock_bh(&net->packet.sklist_lock);
  1513. return 0;
  1514. out:
  1515. return err;
  1516. }
  1517. static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
  1518. {
  1519. struct sock_exterr_skb *serr;
  1520. struct sk_buff *skb, *skb2;
  1521. int copied, err;
  1522. err = -EAGAIN;
  1523. skb = skb_dequeue(&sk->sk_error_queue);
  1524. if (skb == NULL)
  1525. goto out;
  1526. copied = skb->len;
  1527. if (copied > len) {
  1528. msg->msg_flags |= MSG_TRUNC;
  1529. copied = len;
  1530. }
  1531. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1532. if (err)
  1533. goto out_free_skb;
  1534. sock_recv_timestamp(msg, sk, skb);
  1535. serr = SKB_EXT_ERR(skb);
  1536. put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
  1537. sizeof(serr->ee), &serr->ee);
  1538. msg->msg_flags |= MSG_ERRQUEUE;
  1539. err = copied;
  1540. /* Reset and regenerate socket error */
  1541. spin_lock_bh(&sk->sk_error_queue.lock);
  1542. sk->sk_err = 0;
  1543. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  1544. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  1545. spin_unlock_bh(&sk->sk_error_queue.lock);
  1546. sk->sk_error_report(sk);
  1547. } else
  1548. spin_unlock_bh(&sk->sk_error_queue.lock);
  1549. out_free_skb:
  1550. kfree_skb(skb);
  1551. out:
  1552. return err;
  1553. }
  1554. /*
  1555. * Pull a packet from our receive queue and hand it to the user.
  1556. * If necessary we block.
  1557. */
  1558. static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
  1559. struct msghdr *msg, size_t len, int flags)
  1560. {
  1561. struct sock *sk = sock->sk;
  1562. struct sk_buff *skb;
  1563. int copied, err;
  1564. struct sockaddr_ll *sll;
  1565. int vnet_hdr_len = 0;
  1566. err = -EINVAL;
  1567. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  1568. goto out;
  1569. #if 0
  1570. /* What error should we return now? EUNATTACH? */
  1571. if (pkt_sk(sk)->ifindex < 0)
  1572. return -ENODEV;
  1573. #endif
  1574. if (flags & MSG_ERRQUEUE) {
  1575. err = packet_recv_error(sk, msg, len);
  1576. goto out;
  1577. }
  1578. /*
  1579. * Call the generic datagram receiver. This handles all sorts
  1580. * of horrible races and re-entrancy so we can forget about it
  1581. * in the protocol layers.
  1582. *
  1583. * Now it will return ENETDOWN, if device have just gone down,
  1584. * but then it will block.
  1585. */
  1586. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  1587. /*
  1588. * An error occurred so return it. Because skb_recv_datagram()
  1589. * handles the blocking we don't see and worry about blocking
  1590. * retries.
  1591. */
  1592. if (skb == NULL)
  1593. goto out;
  1594. if (pkt_sk(sk)->has_vnet_hdr) {
  1595. struct virtio_net_hdr vnet_hdr = { 0 };
  1596. err = -EINVAL;
  1597. vnet_hdr_len = sizeof(vnet_hdr);
  1598. if (len < vnet_hdr_len)
  1599. goto out_free;
  1600. len -= vnet_hdr_len;
  1601. if (skb_is_gso(skb)) {
  1602. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1603. /* This is a hint as to how much should be linear. */
  1604. vnet_hdr.hdr_len = skb_headlen(skb);
  1605. vnet_hdr.gso_size = sinfo->gso_size;
  1606. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1607. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1608. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1609. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1610. else if (sinfo->gso_type & SKB_GSO_UDP)
  1611. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1612. else if (sinfo->gso_type & SKB_GSO_FCOE)
  1613. goto out_free;
  1614. else
  1615. BUG();
  1616. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1617. vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1618. } else
  1619. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1620. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1621. vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1622. vnet_hdr.csum_start = skb_checksum_start_offset(skb);
  1623. vnet_hdr.csum_offset = skb->csum_offset;
  1624. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1625. vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1626. } /* else everything is zero */
  1627. err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
  1628. vnet_hdr_len);
  1629. if (err < 0)
  1630. goto out_free;
  1631. }
  1632. /*
  1633. * If the address length field is there to be filled in, we fill
  1634. * it in now.
  1635. */
  1636. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1637. if (sock->type == SOCK_PACKET)
  1638. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  1639. else
  1640. msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
  1641. /*
  1642. * You lose any data beyond the buffer you gave. If it worries a
  1643. * user program they can ask the device for its MTU anyway.
  1644. */
  1645. copied = skb->len;
  1646. if (copied > len) {
  1647. copied = len;
  1648. msg->msg_flags |= MSG_TRUNC;
  1649. }
  1650. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1651. if (err)
  1652. goto out_free;
  1653. sock_recv_ts_and_drops(msg, sk, skb);
  1654. if (msg->msg_name)
  1655. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
  1656. msg->msg_namelen);
  1657. if (pkt_sk(sk)->auxdata) {
  1658. struct tpacket_auxdata aux;
  1659. aux.tp_status = TP_STATUS_USER;
  1660. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1661. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  1662. aux.tp_len = PACKET_SKB_CB(skb)->origlen;
  1663. aux.tp_snaplen = skb->len;
  1664. aux.tp_mac = 0;
  1665. aux.tp_net = skb_network_offset(skb);
  1666. if (vlan_tx_tag_present(skb)) {
  1667. aux.tp_vlan_tci = vlan_tx_tag_get(skb);
  1668. aux.tp_status |= TP_STATUS_VLAN_VALID;
  1669. } else {
  1670. aux.tp_vlan_tci = 0;
  1671. }
  1672. aux.tp_padding = 0;
  1673. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  1674. }
  1675. /*
  1676. * Free or return the buffer as appropriate. Again this
  1677. * hides all the races and re-entrancy issues from us.
  1678. */
  1679. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  1680. out_free:
  1681. skb_free_datagram(sk, skb);
  1682. out:
  1683. return err;
  1684. }
  1685. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  1686. int *uaddr_len, int peer)
  1687. {
  1688. struct net_device *dev;
  1689. struct sock *sk = sock->sk;
  1690. if (peer)
  1691. return -EOPNOTSUPP;
  1692. uaddr->sa_family = AF_PACKET;
  1693. rcu_read_lock();
  1694. dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
  1695. if (dev)
  1696. strncpy(uaddr->sa_data, dev->name, 14);
  1697. else
  1698. memset(uaddr->sa_data, 0, 14);
  1699. rcu_read_unlock();
  1700. *uaddr_len = sizeof(*uaddr);
  1701. return 0;
  1702. }
  1703. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  1704. int *uaddr_len, int peer)
  1705. {
  1706. struct net_device *dev;
  1707. struct sock *sk = sock->sk;
  1708. struct packet_sock *po = pkt_sk(sk);
  1709. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  1710. if (peer)
  1711. return -EOPNOTSUPP;
  1712. sll->sll_family = AF_PACKET;
  1713. sll->sll_ifindex = po->ifindex;
  1714. sll->sll_protocol = po->num;
  1715. sll->sll_pkttype = 0;
  1716. rcu_read_lock();
  1717. dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
  1718. if (dev) {
  1719. sll->sll_hatype = dev->type;
  1720. sll->sll_halen = dev->addr_len;
  1721. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  1722. } else {
  1723. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  1724. sll->sll_halen = 0;
  1725. }
  1726. rcu_read_unlock();
  1727. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  1728. return 0;
  1729. }
  1730. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  1731. int what)
  1732. {
  1733. switch (i->type) {
  1734. case PACKET_MR_MULTICAST:
  1735. if (i->alen != dev->addr_len)
  1736. return -EINVAL;
  1737. if (what > 0)
  1738. return dev_mc_add(dev, i->addr);
  1739. else
  1740. return dev_mc_del(dev, i->addr);
  1741. break;
  1742. case PACKET_MR_PROMISC:
  1743. return dev_set_promiscuity(dev, what);
  1744. break;
  1745. case PACKET_MR_ALLMULTI:
  1746. return dev_set_allmulti(dev, what);
  1747. break;
  1748. case PACKET_MR_UNICAST:
  1749. if (i->alen != dev->addr_len)
  1750. return -EINVAL;
  1751. if (what > 0)
  1752. return dev_uc_add(dev, i->addr);
  1753. else
  1754. return dev_uc_del(dev, i->addr);
  1755. break;
  1756. default:
  1757. break;
  1758. }
  1759. return 0;
  1760. }
  1761. static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
  1762. {
  1763. for ( ; i; i = i->next) {
  1764. if (i->ifindex == dev->ifindex)
  1765. packet_dev_mc(dev, i, what);
  1766. }
  1767. }
  1768. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  1769. {
  1770. struct packet_sock *po = pkt_sk(sk);
  1771. struct packet_mclist *ml, *i;
  1772. struct net_device *dev;
  1773. int err;
  1774. rtnl_lock();
  1775. err = -ENODEV;
  1776. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  1777. if (!dev)
  1778. goto done;
  1779. err = -EINVAL;
  1780. if (mreq->mr_alen > dev->addr_len)
  1781. goto done;
  1782. err = -ENOBUFS;
  1783. i = kmalloc(sizeof(*i), GFP_KERNEL);
  1784. if (i == NULL)
  1785. goto done;
  1786. err = 0;
  1787. for (ml = po->mclist; ml; ml = ml->next) {
  1788. if (ml->ifindex == mreq->mr_ifindex &&
  1789. ml->type == mreq->mr_type &&
  1790. ml->alen == mreq->mr_alen &&
  1791. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1792. ml->count++;
  1793. /* Free the new element ... */
  1794. kfree(i);
  1795. goto done;
  1796. }
  1797. }
  1798. i->type = mreq->mr_type;
  1799. i->ifindex = mreq->mr_ifindex;
  1800. i->alen = mreq->mr_alen;
  1801. memcpy(i->addr, mreq->mr_address, i->alen);
  1802. i->count = 1;
  1803. i->next = po->mclist;
  1804. po->mclist = i;
  1805. err = packet_dev_mc(dev, i, 1);
  1806. if (err) {
  1807. po->mclist = i->next;
  1808. kfree(i);
  1809. }
  1810. done:
  1811. rtnl_unlock();
  1812. return err;
  1813. }
  1814. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  1815. {
  1816. struct packet_mclist *ml, **mlp;
  1817. rtnl_lock();
  1818. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  1819. if (ml->ifindex == mreq->mr_ifindex &&
  1820. ml->type == mreq->mr_type &&
  1821. ml->alen == mreq->mr_alen &&
  1822. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1823. if (--ml->count == 0) {
  1824. struct net_device *dev;
  1825. *mlp = ml->next;
  1826. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1827. if (dev)
  1828. packet_dev_mc(dev, ml, -1);
  1829. kfree(ml);
  1830. }
  1831. rtnl_unlock();
  1832. return 0;
  1833. }
  1834. }
  1835. rtnl_unlock();
  1836. return -EADDRNOTAVAIL;
  1837. }
  1838. static void packet_flush_mclist(struct sock *sk)
  1839. {
  1840. struct packet_sock *po = pkt_sk(sk);
  1841. struct packet_mclist *ml;
  1842. if (!po->mclist)
  1843. return;
  1844. rtnl_lock();
  1845. while ((ml = po->mclist) != NULL) {
  1846. struct net_device *dev;
  1847. po->mclist = ml->next;
  1848. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1849. if (dev != NULL)
  1850. packet_dev_mc(dev, ml, -1);
  1851. kfree(ml);
  1852. }
  1853. rtnl_unlock();
  1854. }
  1855. static int
  1856. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  1857. {
  1858. struct sock *sk = sock->sk;
  1859. struct packet_sock *po = pkt_sk(sk);
  1860. int ret;
  1861. if (level != SOL_PACKET)
  1862. return -ENOPROTOOPT;
  1863. switch (optname) {
  1864. case PACKET_ADD_MEMBERSHIP:
  1865. case PACKET_DROP_MEMBERSHIP:
  1866. {
  1867. struct packet_mreq_max mreq;
  1868. int len = optlen;
  1869. memset(&mreq, 0, sizeof(mreq));
  1870. if (len < sizeof(struct packet_mreq))
  1871. return -EINVAL;
  1872. if (len > sizeof(mreq))
  1873. len = sizeof(mreq);
  1874. if (copy_from_user(&mreq, optval, len))
  1875. return -EFAULT;
  1876. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  1877. return -EINVAL;
  1878. if (optname == PACKET_ADD_MEMBERSHIP)
  1879. ret = packet_mc_add(sk, &mreq);
  1880. else
  1881. ret = packet_mc_drop(sk, &mreq);
  1882. return ret;
  1883. }
  1884. case PACKET_RX_RING:
  1885. case PACKET_TX_RING:
  1886. {
  1887. struct tpacket_req req;
  1888. if (optlen < sizeof(req))
  1889. return -EINVAL;
  1890. if (pkt_sk(sk)->has_vnet_hdr)
  1891. return -EINVAL;
  1892. if (copy_from_user(&req, optval, sizeof(req)))
  1893. return -EFAULT;
  1894. return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
  1895. }
  1896. case PACKET_COPY_THRESH:
  1897. {
  1898. int val;
  1899. if (optlen != sizeof(val))
  1900. return -EINVAL;
  1901. if (copy_from_user(&val, optval, sizeof(val)))
  1902. return -EFAULT;
  1903. pkt_sk(sk)->copy_thresh = val;
  1904. return 0;
  1905. }
  1906. case PACKET_VERSION:
  1907. {
  1908. int val;
  1909. if (optlen != sizeof(val))
  1910. return -EINVAL;
  1911. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1912. return -EBUSY;
  1913. if (copy_from_user(&val, optval, sizeof(val)))
  1914. return -EFAULT;
  1915. switch (val) {
  1916. case TPACKET_V1:
  1917. case TPACKET_V2:
  1918. po->tp_version = val;
  1919. return 0;
  1920. default:
  1921. return -EINVAL;
  1922. }
  1923. }
  1924. case PACKET_RESERVE:
  1925. {
  1926. unsigned int val;
  1927. if (optlen != sizeof(val))
  1928. return -EINVAL;
  1929. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1930. return -EBUSY;
  1931. if (copy_from_user(&val, optval, sizeof(val)))
  1932. return -EFAULT;
  1933. po->tp_reserve = val;
  1934. return 0;
  1935. }
  1936. case PACKET_LOSS:
  1937. {
  1938. unsigned int val;
  1939. if (optlen != sizeof(val))
  1940. return -EINVAL;
  1941. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1942. return -EBUSY;
  1943. if (copy_from_user(&val, optval, sizeof(val)))
  1944. return -EFAULT;
  1945. po->tp_loss = !!val;
  1946. return 0;
  1947. }
  1948. case PACKET_AUXDATA:
  1949. {
  1950. int val;
  1951. if (optlen < sizeof(val))
  1952. return -EINVAL;
  1953. if (copy_from_user(&val, optval, sizeof(val)))
  1954. return -EFAULT;
  1955. po->auxdata = !!val;
  1956. return 0;
  1957. }
  1958. case PACKET_ORIGDEV:
  1959. {
  1960. int val;
  1961. if (optlen < sizeof(val))
  1962. return -EINVAL;
  1963. if (copy_from_user(&val, optval, sizeof(val)))
  1964. return -EFAULT;
  1965. po->origdev = !!val;
  1966. return 0;
  1967. }
  1968. case PACKET_VNET_HDR:
  1969. {
  1970. int val;
  1971. if (sock->type != SOCK_RAW)
  1972. return -EINVAL;
  1973. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1974. return -EBUSY;
  1975. if (optlen < sizeof(val))
  1976. return -EINVAL;
  1977. if (copy_from_user(&val, optval, sizeof(val)))
  1978. return -EFAULT;
  1979. po->has_vnet_hdr = !!val;
  1980. return 0;
  1981. }
  1982. case PACKET_TIMESTAMP:
  1983. {
  1984. int val;
  1985. if (optlen != sizeof(val))
  1986. return -EINVAL;
  1987. if (copy_from_user(&val, optval, sizeof(val)))
  1988. return -EFAULT;
  1989. po->tp_tstamp = val;
  1990. return 0;
  1991. }
  1992. case PACKET_FANOUT:
  1993. {
  1994. int val;
  1995. if (optlen != sizeof(val))
  1996. return -EINVAL;
  1997. if (copy_from_user(&val, optval, sizeof(val)))
  1998. return -EFAULT;
  1999. return fanout_add(sk, val & 0xffff, val >> 16);
  2000. }
  2001. default:
  2002. return -ENOPROTOOPT;
  2003. }
  2004. }
  2005. static int packet_getsockopt(struct socket *sock, int level, int optname,
  2006. char __user *optval, int __user *optlen)
  2007. {
  2008. int len;
  2009. int val;
  2010. struct sock *sk = sock->sk;
  2011. struct packet_sock *po = pkt_sk(sk);
  2012. void *data;
  2013. struct tpacket_stats st;
  2014. if (level != SOL_PACKET)
  2015. return -ENOPROTOOPT;
  2016. if (get_user(len, optlen))
  2017. return -EFAULT;
  2018. if (len < 0)
  2019. return -EINVAL;
  2020. switch (optname) {
  2021. case PACKET_STATISTICS:
  2022. if (len > sizeof(struct tpacket_stats))
  2023. len = sizeof(struct tpacket_stats);
  2024. spin_lock_bh(&sk->sk_receive_queue.lock);
  2025. st = po->stats;
  2026. memset(&po->stats, 0, sizeof(st));
  2027. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2028. st.tp_packets += st.tp_drops;
  2029. data = &st;
  2030. break;
  2031. case PACKET_AUXDATA:
  2032. if (len > sizeof(int))
  2033. len = sizeof(int);
  2034. val = po->auxdata;
  2035. data = &val;
  2036. break;
  2037. case PACKET_ORIGDEV:
  2038. if (len > sizeof(int))
  2039. len = sizeof(int);
  2040. val = po->origdev;
  2041. data = &val;
  2042. break;
  2043. case PACKET_VNET_HDR:
  2044. if (len > sizeof(int))
  2045. len = sizeof(int);
  2046. val = po->has_vnet_hdr;
  2047. data = &val;
  2048. break;
  2049. case PACKET_VERSION:
  2050. if (len > sizeof(int))
  2051. len = sizeof(int);
  2052. val = po->tp_version;
  2053. data = &val;
  2054. break;
  2055. case PACKET_HDRLEN:
  2056. if (len > sizeof(int))
  2057. len = sizeof(int);
  2058. if (copy_from_user(&val, optval, len))
  2059. return -EFAULT;
  2060. switch (val) {
  2061. case TPACKET_V1:
  2062. val = sizeof(struct tpacket_hdr);
  2063. break;
  2064. case TPACKET_V2:
  2065. val = sizeof(struct tpacket2_hdr);
  2066. break;
  2067. default:
  2068. return -EINVAL;
  2069. }
  2070. data = &val;
  2071. break;
  2072. case PACKET_RESERVE:
  2073. if (len > sizeof(unsigned int))
  2074. len = sizeof(unsigned int);
  2075. val = po->tp_reserve;
  2076. data = &val;
  2077. break;
  2078. case PACKET_LOSS:
  2079. if (len > sizeof(unsigned int))
  2080. len = sizeof(unsigned int);
  2081. val = po->tp_loss;
  2082. data = &val;
  2083. break;
  2084. case PACKET_TIMESTAMP:
  2085. if (len > sizeof(int))
  2086. len = sizeof(int);
  2087. val = po->tp_tstamp;
  2088. data = &val;
  2089. break;
  2090. case PACKET_FANOUT:
  2091. if (len > sizeof(int))
  2092. len = sizeof(int);
  2093. val = (po->fanout ?
  2094. ((u32)po->fanout->id |
  2095. ((u32)po->fanout->type << 16)) :
  2096. 0);
  2097. data = &val;
  2098. break;
  2099. default:
  2100. return -ENOPROTOOPT;
  2101. }
  2102. if (put_user(len, optlen))
  2103. return -EFAULT;
  2104. if (copy_to_user(optval, data, len))
  2105. return -EFAULT;
  2106. return 0;
  2107. }
  2108. static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  2109. {
  2110. struct sock *sk;
  2111. struct hlist_node *node;
  2112. struct net_device *dev = data;
  2113. struct net *net = dev_net(dev);
  2114. rcu_read_lock();
  2115. sk_for_each_rcu(sk, node, &net->packet.sklist) {
  2116. struct packet_sock *po = pkt_sk(sk);
  2117. switch (msg) {
  2118. case NETDEV_UNREGISTER:
  2119. if (po->mclist)
  2120. packet_dev_mclist(dev, po->mclist, -1);
  2121. /* fallthrough */
  2122. case NETDEV_DOWN:
  2123. if (dev->ifindex == po->ifindex) {
  2124. spin_lock(&po->bind_lock);
  2125. if (po->running) {
  2126. __unregister_prot_hook(sk, false);
  2127. sk->sk_err = ENETDOWN;
  2128. if (!sock_flag(sk, SOCK_DEAD))
  2129. sk->sk_error_report(sk);
  2130. }
  2131. if (msg == NETDEV_UNREGISTER) {
  2132. po->ifindex = -1;
  2133. if (po->prot_hook.dev)
  2134. dev_put(po->prot_hook.dev);
  2135. po->prot_hook.dev = NULL;
  2136. }
  2137. spin_unlock(&po->bind_lock);
  2138. }
  2139. break;
  2140. case NETDEV_UP:
  2141. if (dev->ifindex == po->ifindex) {
  2142. spin_lock(&po->bind_lock);
  2143. if (po->num)
  2144. register_prot_hook(sk);
  2145. spin_unlock(&po->bind_lock);
  2146. }
  2147. break;
  2148. }
  2149. }
  2150. rcu_read_unlock();
  2151. return NOTIFY_DONE;
  2152. }
  2153. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  2154. unsigned long arg)
  2155. {
  2156. struct sock *sk = sock->sk;
  2157. switch (cmd) {
  2158. case SIOCOUTQ:
  2159. {
  2160. int amount = sk_wmem_alloc_get(sk);
  2161. return put_user(amount, (int __user *)arg);
  2162. }
  2163. case SIOCINQ:
  2164. {
  2165. struct sk_buff *skb;
  2166. int amount = 0;
  2167. spin_lock_bh(&sk->sk_receive_queue.lock);
  2168. skb = skb_peek(&sk->sk_receive_queue);
  2169. if (skb)
  2170. amount = skb->len;
  2171. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2172. return put_user(amount, (int __user *)arg);
  2173. }
  2174. case SIOCGSTAMP:
  2175. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  2176. case SIOCGSTAMPNS:
  2177. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  2178. #ifdef CONFIG_INET
  2179. case SIOCADDRT:
  2180. case SIOCDELRT:
  2181. case SIOCDARP:
  2182. case SIOCGARP:
  2183. case SIOCSARP:
  2184. case SIOCGIFADDR:
  2185. case SIOCSIFADDR:
  2186. case SIOCGIFBRDADDR:
  2187. case SIOCSIFBRDADDR:
  2188. case SIOCGIFNETMASK:
  2189. case SIOCSIFNETMASK:
  2190. case SIOCGIFDSTADDR:
  2191. case SIOCSIFDSTADDR:
  2192. case SIOCSIFFLAGS:
  2193. return inet_dgram_ops.ioctl(sock, cmd, arg);
  2194. #endif
  2195. default:
  2196. return -ENOIOCTLCMD;
  2197. }
  2198. return 0;
  2199. }
  2200. static unsigned int packet_poll(struct file *file, struct socket *sock,
  2201. poll_table *wait)
  2202. {
  2203. struct sock *sk = sock->sk;
  2204. struct packet_sock *po = pkt_sk(sk);
  2205. unsigned int mask = datagram_poll(file, sock, wait);
  2206. spin_lock_bh(&sk->sk_receive_queue.lock);
  2207. if (po->rx_ring.pg_vec) {
  2208. if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
  2209. mask |= POLLIN | POLLRDNORM;
  2210. }
  2211. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2212. spin_lock_bh(&sk->sk_write_queue.lock);
  2213. if (po->tx_ring.pg_vec) {
  2214. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  2215. mask |= POLLOUT | POLLWRNORM;
  2216. }
  2217. spin_unlock_bh(&sk->sk_write_queue.lock);
  2218. return mask;
  2219. }
  2220. /* Dirty? Well, I still did not learn better way to account
  2221. * for user mmaps.
  2222. */
  2223. static void packet_mm_open(struct vm_area_struct *vma)
  2224. {
  2225. struct file *file = vma->vm_file;
  2226. struct socket *sock = file->private_data;
  2227. struct sock *sk = sock->sk;
  2228. if (sk)
  2229. atomic_inc(&pkt_sk(sk)->mapped);
  2230. }
  2231. static void packet_mm_close(struct vm_area_struct *vma)
  2232. {
  2233. struct file *file = vma->vm_file;
  2234. struct socket *sock = file->private_data;
  2235. struct sock *sk = sock->sk;
  2236. if (sk)
  2237. atomic_dec(&pkt_sk(sk)->mapped);
  2238. }
  2239. static const struct vm_operations_struct packet_mmap_ops = {
  2240. .open = packet_mm_open,
  2241. .close = packet_mm_close,
  2242. };
  2243. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  2244. unsigned int len)
  2245. {
  2246. int i;
  2247. for (i = 0; i < len; i++) {
  2248. if (likely(pg_vec[i].buffer)) {
  2249. if (is_vmalloc_addr(pg_vec[i].buffer))
  2250. vfree(pg_vec[i].buffer);
  2251. else
  2252. free_pages((unsigned long)pg_vec[i].buffer,
  2253. order);
  2254. pg_vec[i].buffer = NULL;
  2255. }
  2256. }
  2257. kfree(pg_vec);
  2258. }
  2259. static inline char *alloc_one_pg_vec_page(unsigned long order)
  2260. {
  2261. char *buffer = NULL;
  2262. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  2263. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  2264. buffer = (char *) __get_free_pages(gfp_flags, order);
  2265. if (buffer)
  2266. return buffer;
  2267. /*
  2268. * __get_free_pages failed, fall back to vmalloc
  2269. */
  2270. buffer = vzalloc((1 << order) * PAGE_SIZE);
  2271. if (buffer)
  2272. return buffer;
  2273. /*
  2274. * vmalloc failed, lets dig into swap here
  2275. */
  2276. gfp_flags &= ~__GFP_NORETRY;
  2277. buffer = (char *)__get_free_pages(gfp_flags, order);
  2278. if (buffer)
  2279. return buffer;
  2280. /*
  2281. * complete and utter failure
  2282. */
  2283. return NULL;
  2284. }
  2285. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  2286. {
  2287. unsigned int block_nr = req->tp_block_nr;
  2288. struct pgv *pg_vec;
  2289. int i;
  2290. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  2291. if (unlikely(!pg_vec))
  2292. goto out;
  2293. for (i = 0; i < block_nr; i++) {
  2294. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  2295. if (unlikely(!pg_vec[i].buffer))
  2296. goto out_free_pgvec;
  2297. }
  2298. out:
  2299. return pg_vec;
  2300. out_free_pgvec:
  2301. free_pg_vec(pg_vec, order, block_nr);
  2302. pg_vec = NULL;
  2303. goto out;
  2304. }
  2305. static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
  2306. int closing, int tx_ring)
  2307. {
  2308. struct pgv *pg_vec = NULL;
  2309. struct packet_sock *po = pkt_sk(sk);
  2310. int was_running, order = 0;
  2311. struct packet_ring_buffer *rb;
  2312. struct sk_buff_head *rb_queue;
  2313. __be16 num;
  2314. int err;
  2315. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  2316. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  2317. err = -EBUSY;
  2318. if (!closing) {
  2319. if (atomic_read(&po->mapped))
  2320. goto out;
  2321. if (atomic_read(&rb->pending))
  2322. goto out;
  2323. }
  2324. if (req->tp_block_nr) {
  2325. /* Sanity tests and some calculations */
  2326. err = -EBUSY;
  2327. if (unlikely(rb->pg_vec))
  2328. goto out;
  2329. switch (po->tp_version) {
  2330. case TPACKET_V1:
  2331. po->tp_hdrlen = TPACKET_HDRLEN;
  2332. break;
  2333. case TPACKET_V2:
  2334. po->tp_hdrlen = TPACKET2_HDRLEN;
  2335. break;
  2336. }
  2337. err = -EINVAL;
  2338. if (unlikely((int)req->tp_block_size <= 0))
  2339. goto out;
  2340. if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
  2341. goto out;
  2342. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  2343. po->tp_reserve))
  2344. goto out;
  2345. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  2346. goto out;
  2347. rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
  2348. if (unlikely(rb->frames_per_block <= 0))
  2349. goto out;
  2350. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  2351. req->tp_frame_nr))
  2352. goto out;
  2353. err = -ENOMEM;
  2354. order = get_order(req->tp_block_size);
  2355. pg_vec = alloc_pg_vec(req, order);
  2356. if (unlikely(!pg_vec))
  2357. goto out;
  2358. }
  2359. /* Done */
  2360. else {
  2361. err = -EINVAL;
  2362. if (unlikely(req->tp_frame_nr))
  2363. goto out;
  2364. }
  2365. lock_sock(sk);
  2366. /* Detach socket from network */
  2367. spin_lock(&po->bind_lock);
  2368. was_running = po->running;
  2369. num = po->num;
  2370. if (was_running) {
  2371. po->num = 0;
  2372. __unregister_prot_hook(sk, false);
  2373. }
  2374. spin_unlock(&po->bind_lock);
  2375. synchronize_net();
  2376. err = -EBUSY;
  2377. mutex_lock(&po->pg_vec_lock);
  2378. if (closing || atomic_read(&po->mapped) == 0) {
  2379. err = 0;
  2380. spin_lock_bh(&rb_queue->lock);
  2381. swap(rb->pg_vec, pg_vec);
  2382. rb->frame_max = (req->tp_frame_nr - 1);
  2383. rb->head = 0;
  2384. rb->frame_size = req->tp_frame_size;
  2385. spin_unlock_bh(&rb_queue->lock);
  2386. swap(rb->pg_vec_order, order);
  2387. swap(rb->pg_vec_len, req->tp_block_nr);
  2388. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  2389. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  2390. tpacket_rcv : packet_rcv;
  2391. skb_queue_purge(rb_queue);
  2392. if (atomic_read(&po->mapped))
  2393. pr_err("packet_mmap: vma is busy: %d\n",
  2394. atomic_read(&po->mapped));
  2395. }
  2396. mutex_unlock(&po->pg_vec_lock);
  2397. spin_lock(&po->bind_lock);
  2398. if (was_running) {
  2399. po->num = num;
  2400. register_prot_hook(sk);
  2401. }
  2402. spin_unlock(&po->bind_lock);
  2403. release_sock(sk);
  2404. if (pg_vec)
  2405. free_pg_vec(pg_vec, order, req->tp_block_nr);
  2406. out:
  2407. return err;
  2408. }
  2409. static int packet_mmap(struct file *file, struct socket *sock,
  2410. struct vm_area_struct *vma)
  2411. {
  2412. struct sock *sk = sock->sk;
  2413. struct packet_sock *po = pkt_sk(sk);
  2414. unsigned long size, expected_size;
  2415. struct packet_ring_buffer *rb;
  2416. unsigned long start;
  2417. int err = -EINVAL;
  2418. int i;
  2419. if (vma->vm_pgoff)
  2420. return -EINVAL;
  2421. mutex_lock(&po->pg_vec_lock);
  2422. expected_size = 0;
  2423. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2424. if (rb->pg_vec) {
  2425. expected_size += rb->pg_vec_len
  2426. * rb->pg_vec_pages
  2427. * PAGE_SIZE;
  2428. }
  2429. }
  2430. if (expected_size == 0)
  2431. goto out;
  2432. size = vma->vm_end - vma->vm_start;
  2433. if (size != expected_size)
  2434. goto out;
  2435. start = vma->vm_start;
  2436. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2437. if (rb->pg_vec == NULL)
  2438. continue;
  2439. for (i = 0; i < rb->pg_vec_len; i++) {
  2440. struct page *page;
  2441. void *kaddr = rb->pg_vec[i].buffer;
  2442. int pg_num;
  2443. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  2444. page = pgv_to_page(kaddr);
  2445. err = vm_insert_page(vma, start, page);
  2446. if (unlikely(err))
  2447. goto out;
  2448. start += PAGE_SIZE;
  2449. kaddr += PAGE_SIZE;
  2450. }
  2451. }
  2452. }
  2453. atomic_inc(&po->mapped);
  2454. vma->vm_ops = &packet_mmap_ops;
  2455. err = 0;
  2456. out:
  2457. mutex_unlock(&po->pg_vec_lock);
  2458. return err;
  2459. }
  2460. static const struct proto_ops packet_ops_spkt = {
  2461. .family = PF_PACKET,
  2462. .owner = THIS_MODULE,
  2463. .release = packet_release,
  2464. .bind = packet_bind_spkt,
  2465. .connect = sock_no_connect,
  2466. .socketpair = sock_no_socketpair,
  2467. .accept = sock_no_accept,
  2468. .getname = packet_getname_spkt,
  2469. .poll = datagram_poll,
  2470. .ioctl = packet_ioctl,
  2471. .listen = sock_no_listen,
  2472. .shutdown = sock_no_shutdown,
  2473. .setsockopt = sock_no_setsockopt,
  2474. .getsockopt = sock_no_getsockopt,
  2475. .sendmsg = packet_sendmsg_spkt,
  2476. .recvmsg = packet_recvmsg,
  2477. .mmap = sock_no_mmap,
  2478. .sendpage = sock_no_sendpage,
  2479. };
  2480. static const struct proto_ops packet_ops = {
  2481. .family = PF_PACKET,
  2482. .owner = THIS_MODULE,
  2483. .release = packet_release,
  2484. .bind = packet_bind,
  2485. .connect = sock_no_connect,
  2486. .socketpair = sock_no_socketpair,
  2487. .accept = sock_no_accept,
  2488. .getname = packet_getname,
  2489. .poll = packet_poll,
  2490. .ioctl = packet_ioctl,
  2491. .listen = sock_no_listen,
  2492. .shutdown = sock_no_shutdown,
  2493. .setsockopt = packet_setsockopt,
  2494. .getsockopt = packet_getsockopt,
  2495. .sendmsg = packet_sendmsg,
  2496. .recvmsg = packet_recvmsg,
  2497. .mmap = packet_mmap,
  2498. .sendpage = sock_no_sendpage,
  2499. };
  2500. static const struct net_proto_family packet_family_ops = {
  2501. .family = PF_PACKET,
  2502. .create = packet_create,
  2503. .owner = THIS_MODULE,
  2504. };
  2505. static struct notifier_block packet_netdev_notifier = {
  2506. .notifier_call = packet_notifier,
  2507. };
  2508. #ifdef CONFIG_PROC_FS
  2509. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  2510. __acquires(RCU)
  2511. {
  2512. struct net *net = seq_file_net(seq);
  2513. rcu_read_lock();
  2514. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  2515. }
  2516. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2517. {
  2518. struct net *net = seq_file_net(seq);
  2519. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  2520. }
  2521. static void packet_seq_stop(struct seq_file *seq, void *v)
  2522. __releases(RCU)
  2523. {
  2524. rcu_read_unlock();
  2525. }
  2526. static int packet_seq_show(struct seq_file *seq, void *v)
  2527. {
  2528. if (v == SEQ_START_TOKEN)
  2529. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  2530. else {
  2531. struct sock *s = sk_entry(v);
  2532. const struct packet_sock *po = pkt_sk(s);
  2533. seq_printf(seq,
  2534. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  2535. s,
  2536. atomic_read(&s->sk_refcnt),
  2537. s->sk_type,
  2538. ntohs(po->num),
  2539. po->ifindex,
  2540. po->running,
  2541. atomic_read(&s->sk_rmem_alloc),
  2542. sock_i_uid(s),
  2543. sock_i_ino(s));
  2544. }
  2545. return 0;
  2546. }
  2547. static const struct seq_operations packet_seq_ops = {
  2548. .start = packet_seq_start,
  2549. .next = packet_seq_next,
  2550. .stop = packet_seq_stop,
  2551. .show = packet_seq_show,
  2552. };
  2553. static int packet_seq_open(struct inode *inode, struct file *file)
  2554. {
  2555. return seq_open_net(inode, file, &packet_seq_ops,
  2556. sizeof(struct seq_net_private));
  2557. }
  2558. static const struct file_operations packet_seq_fops = {
  2559. .owner = THIS_MODULE,
  2560. .open = packet_seq_open,
  2561. .read = seq_read,
  2562. .llseek = seq_lseek,
  2563. .release = seq_release_net,
  2564. };
  2565. #endif
  2566. static int __net_init packet_net_init(struct net *net)
  2567. {
  2568. spin_lock_init(&net->packet.sklist_lock);
  2569. INIT_HLIST_HEAD(&net->packet.sklist);
  2570. if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
  2571. return -ENOMEM;
  2572. return 0;
  2573. }
  2574. static void __net_exit packet_net_exit(struct net *net)
  2575. {
  2576. proc_net_remove(net, "packet");
  2577. }
  2578. static struct pernet_operations packet_net_ops = {
  2579. .init = packet_net_init,
  2580. .exit = packet_net_exit,
  2581. };
  2582. static void __exit packet_exit(void)
  2583. {
  2584. unregister_netdevice_notifier(&packet_netdev_notifier);
  2585. unregister_pernet_subsys(&packet_net_ops);
  2586. sock_unregister(PF_PACKET);
  2587. proto_unregister(&packet_proto);
  2588. }
  2589. static int __init packet_init(void)
  2590. {
  2591. int rc = proto_register(&packet_proto, 0);
  2592. if (rc != 0)
  2593. goto out;
  2594. sock_register(&packet_family_ops);
  2595. register_pernet_subsys(&packet_net_ops);
  2596. register_netdevice_notifier(&packet_netdev_notifier);
  2597. out:
  2598. return rc;
  2599. }
  2600. module_init(packet_init);
  2601. module_exit(packet_exit);
  2602. MODULE_LICENSE("GPL");
  2603. MODULE_ALIAS_NETPROTO(PF_PACKET);