af_packet.c 69 KB

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