af_packet.c 62 KB

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