udp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. * The User Datagram Protocol (UDP).
  7. *
  8. * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  13. * Alan Cox, <Alan.Cox@linux.org>
  14. * Hirokazu Takahashi, <taka@valinux.co.jp>
  15. *
  16. * Fixes:
  17. * Alan Cox : verify_area() calls
  18. * Alan Cox : stopped close while in use off icmp
  19. * messages. Not a fix but a botch that
  20. * for udp at least is 'valid'.
  21. * Alan Cox : Fixed icmp handling properly
  22. * Alan Cox : Correct error for oversized datagrams
  23. * Alan Cox : Tidied select() semantics.
  24. * Alan Cox : udp_err() fixed properly, also now
  25. * select and read wake correctly on errors
  26. * Alan Cox : udp_send verify_area moved to avoid mem leak
  27. * Alan Cox : UDP can count its memory
  28. * Alan Cox : send to an unknown connection causes
  29. * an ECONNREFUSED off the icmp, but
  30. * does NOT close.
  31. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  32. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  33. * bug no longer crashes it.
  34. * Fred Van Kempen : Net2e support for sk->broadcast.
  35. * Alan Cox : Uses skb_free_datagram
  36. * Alan Cox : Added get/set sockopt support.
  37. * Alan Cox : Broadcasting without option set returns EACCES.
  38. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  39. * Alan Cox : Use ip_tos and ip_ttl
  40. * Alan Cox : SNMP Mibs
  41. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  42. * Matt Dillon : UDP length checks.
  43. * Alan Cox : Smarter af_inet used properly.
  44. * Alan Cox : Use new kernel side addressing.
  45. * Alan Cox : Incorrect return on truncated datagram receive.
  46. * Arnt Gulbrandsen : New udp_send and stuff
  47. * Alan Cox : Cache last socket
  48. * Alan Cox : Route cache
  49. * Jon Peatfield : Minor efficiency fix to sendto().
  50. * Mike Shaver : RFC1122 checks.
  51. * Alan Cox : Nonblocking error fix.
  52. * Willy Konynenberg : Transparent proxying support.
  53. * Mike McLagan : Routing by source
  54. * David S. Miller : New socket lookup architecture.
  55. * Last socket cache retained as it
  56. * does have a high hit rate.
  57. * Olaf Kirch : Don't linearise iovec on sendmsg.
  58. * Andi Kleen : Some cleanups, cache destination entry
  59. * for connect.
  60. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  61. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  62. * return ENOTCONN for unconnected sockets (POSIX)
  63. * Janos Farkas : don't deliver multi/broadcasts to a different
  64. * bound-to-device socket
  65. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  66. * datagrams.
  67. * Hirokazu Takahashi : sendfile() on UDP works now.
  68. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  69. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  70. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  71. * a single port at the same time.
  72. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  73. * James Chapman : Add L2TP encapsulation type.
  74. *
  75. *
  76. * This program is free software; you can redistribute it and/or
  77. * modify it under the terms of the GNU General Public License
  78. * as published by the Free Software Foundation; either version
  79. * 2 of the License, or (at your option) any later version.
  80. */
  81. #include <asm/system.h>
  82. #include <asm/uaccess.h>
  83. #include <asm/ioctls.h>
  84. #include <linux/types.h>
  85. #include <linux/fcntl.h>
  86. #include <linux/module.h>
  87. #include <linux/socket.h>
  88. #include <linux/sockios.h>
  89. #include <linux/igmp.h>
  90. #include <linux/in.h>
  91. #include <linux/errno.h>
  92. #include <linux/timer.h>
  93. #include <linux/mm.h>
  94. #include <linux/inet.h>
  95. #include <linux/netdevice.h>
  96. #include <net/tcp_states.h>
  97. #include <linux/skbuff.h>
  98. #include <linux/proc_fs.h>
  99. #include <linux/seq_file.h>
  100. #include <net/net_namespace.h>
  101. #include <net/icmp.h>
  102. #include <net/route.h>
  103. #include <net/checksum.h>
  104. #include <net/xfrm.h>
  105. #include "udp_impl.h"
  106. /*
  107. * Snmp MIB for the UDP layer
  108. */
  109. DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
  110. EXPORT_SYMBOL(udp_statistics);
  111. DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
  112. EXPORT_SYMBOL(udp_stats_in6);
  113. struct hlist_head udp_hash[UDP_HTABLE_SIZE];
  114. DEFINE_RWLOCK(udp_hash_lock);
  115. static inline int __udp_lib_lport_inuse(__u16 num,
  116. const struct hlist_head udptable[])
  117. {
  118. struct sock *sk;
  119. struct hlist_node *node;
  120. sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)])
  121. if (sk->sk_hash == num)
  122. return 1;
  123. return 0;
  124. }
  125. /**
  126. * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  127. *
  128. * @sk: socket struct in question
  129. * @snum: port number to look up
  130. * @udptable: hash list table, must be of UDP_HTABLE_SIZE
  131. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  132. */
  133. int __udp_lib_get_port(struct sock *sk, unsigned short snum,
  134. struct hlist_head udptable[],
  135. int (*saddr_comp)(const struct sock *sk1,
  136. const struct sock *sk2 ) )
  137. {
  138. struct hlist_node *node;
  139. struct hlist_head *head;
  140. struct sock *sk2;
  141. int error = 1;
  142. write_lock_bh(&udp_hash_lock);
  143. if (!snum) {
  144. int i, low, high, remaining;
  145. unsigned rover, best, best_size_so_far;
  146. inet_get_local_port_range(&low, &high);
  147. remaining = (high - low) + 1;
  148. best_size_so_far = UINT_MAX;
  149. best = rover = net_random() % remaining + low;
  150. /* 1st pass: look for empty (or shortest) hash chain */
  151. for (i = 0; i < UDP_HTABLE_SIZE; i++) {
  152. int size = 0;
  153. head = &udptable[rover & (UDP_HTABLE_SIZE - 1)];
  154. if (hlist_empty(head))
  155. goto gotit;
  156. sk_for_each(sk2, node, head) {
  157. if (++size >= best_size_so_far)
  158. goto next;
  159. }
  160. best_size_so_far = size;
  161. best = rover;
  162. next:
  163. /* fold back if end of range */
  164. if (++rover > high)
  165. rover = low + ((rover - low)
  166. & (UDP_HTABLE_SIZE - 1));
  167. }
  168. /* 2nd pass: find hole in shortest hash chain */
  169. rover = best;
  170. for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) {
  171. if (! __udp_lib_lport_inuse(rover, udptable))
  172. goto gotit;
  173. rover += UDP_HTABLE_SIZE;
  174. if (rover > high)
  175. rover = low + ((rover - low)
  176. & (UDP_HTABLE_SIZE - 1));
  177. }
  178. /* All ports in use! */
  179. goto fail;
  180. gotit:
  181. snum = rover;
  182. } else {
  183. head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
  184. sk_for_each(sk2, node, head)
  185. if (sk2->sk_hash == snum &&
  186. sk2 != sk &&
  187. (!sk2->sk_reuse || !sk->sk_reuse) &&
  188. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
  189. || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  190. (*saddr_comp)(sk, sk2) )
  191. goto fail;
  192. }
  193. inet_sk(sk)->num = snum;
  194. sk->sk_hash = snum;
  195. if (sk_unhashed(sk)) {
  196. head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
  197. sk_add_node(sk, head);
  198. sock_prot_inc_use(sk->sk_prot);
  199. }
  200. error = 0;
  201. fail:
  202. write_unlock_bh(&udp_hash_lock);
  203. return error;
  204. }
  205. int udp_get_port(struct sock *sk, unsigned short snum,
  206. int (*scmp)(const struct sock *, const struct sock *))
  207. {
  208. return __udp_lib_get_port(sk, snum, udp_hash, scmp);
  209. }
  210. int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
  211. {
  212. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  213. return ( !ipv6_only_sock(sk2) &&
  214. (!inet1->rcv_saddr || !inet2->rcv_saddr ||
  215. inet1->rcv_saddr == inet2->rcv_saddr ));
  216. }
  217. static inline int udp_v4_get_port(struct sock *sk, unsigned short snum)
  218. {
  219. return udp_get_port(sk, snum, ipv4_rcv_saddr_equal);
  220. }
  221. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  222. * harder than this. -DaveM
  223. */
  224. static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport,
  225. __be32 daddr, __be16 dport,
  226. int dif, struct hlist_head udptable[])
  227. {
  228. struct sock *sk, *result = NULL;
  229. struct hlist_node *node;
  230. unsigned short hnum = ntohs(dport);
  231. int badness = -1;
  232. read_lock(&udp_hash_lock);
  233. sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
  234. struct inet_sock *inet = inet_sk(sk);
  235. if (sk->sk_hash == hnum && !ipv6_only_sock(sk)) {
  236. int score = (sk->sk_family == PF_INET ? 1 : 0);
  237. if (inet->rcv_saddr) {
  238. if (inet->rcv_saddr != daddr)
  239. continue;
  240. score+=2;
  241. }
  242. if (inet->daddr) {
  243. if (inet->daddr != saddr)
  244. continue;
  245. score+=2;
  246. }
  247. if (inet->dport) {
  248. if (inet->dport != sport)
  249. continue;
  250. score+=2;
  251. }
  252. if (sk->sk_bound_dev_if) {
  253. if (sk->sk_bound_dev_if != dif)
  254. continue;
  255. score+=2;
  256. }
  257. if (score == 9) {
  258. result = sk;
  259. break;
  260. } else if (score > badness) {
  261. result = sk;
  262. badness = score;
  263. }
  264. }
  265. }
  266. if (result)
  267. sock_hold(result);
  268. read_unlock(&udp_hash_lock);
  269. return result;
  270. }
  271. static inline struct sock *udp_v4_mcast_next(struct sock *sk,
  272. __be16 loc_port, __be32 loc_addr,
  273. __be16 rmt_port, __be32 rmt_addr,
  274. int dif)
  275. {
  276. struct hlist_node *node;
  277. struct sock *s = sk;
  278. unsigned short hnum = ntohs(loc_port);
  279. sk_for_each_from(s, node) {
  280. struct inet_sock *inet = inet_sk(s);
  281. if (s->sk_hash != hnum ||
  282. (inet->daddr && inet->daddr != rmt_addr) ||
  283. (inet->dport != rmt_port && inet->dport) ||
  284. (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
  285. ipv6_only_sock(s) ||
  286. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  287. continue;
  288. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  289. continue;
  290. goto found;
  291. }
  292. s = NULL;
  293. found:
  294. return s;
  295. }
  296. /*
  297. * This routine is called by the ICMP module when it gets some
  298. * sort of error condition. If err < 0 then the socket should
  299. * be closed and the error returned to the user. If err > 0
  300. * it's just the icmp type << 8 | icmp code.
  301. * Header points to the ip header of the error packet. We move
  302. * on past this. Then (as it used to claim before adjustment)
  303. * header points to the first 8 bytes of the udp header. We need
  304. * to find the appropriate port.
  305. */
  306. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[])
  307. {
  308. struct inet_sock *inet;
  309. struct iphdr *iph = (struct iphdr*)skb->data;
  310. struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
  311. const int type = icmp_hdr(skb)->type;
  312. const int code = icmp_hdr(skb)->code;
  313. struct sock *sk;
  314. int harderr;
  315. int err;
  316. sk = __udp4_lib_lookup(iph->daddr, uh->dest, iph->saddr, uh->source,
  317. skb->dev->ifindex, udptable );
  318. if (sk == NULL) {
  319. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  320. return; /* No socket for error */
  321. }
  322. err = 0;
  323. harderr = 0;
  324. inet = inet_sk(sk);
  325. switch (type) {
  326. default:
  327. case ICMP_TIME_EXCEEDED:
  328. err = EHOSTUNREACH;
  329. break;
  330. case ICMP_SOURCE_QUENCH:
  331. goto out;
  332. case ICMP_PARAMETERPROB:
  333. err = EPROTO;
  334. harderr = 1;
  335. break;
  336. case ICMP_DEST_UNREACH:
  337. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  338. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  339. err = EMSGSIZE;
  340. harderr = 1;
  341. break;
  342. }
  343. goto out;
  344. }
  345. err = EHOSTUNREACH;
  346. if (code <= NR_ICMP_UNREACH) {
  347. harderr = icmp_err_convert[code].fatal;
  348. err = icmp_err_convert[code].errno;
  349. }
  350. break;
  351. }
  352. /*
  353. * RFC1122: OK. Passes ICMP errors back to application, as per
  354. * 4.1.3.3.
  355. */
  356. if (!inet->recverr) {
  357. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  358. goto out;
  359. } else {
  360. ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
  361. }
  362. sk->sk_err = err;
  363. sk->sk_error_report(sk);
  364. out:
  365. sock_put(sk);
  366. }
  367. void udp_err(struct sk_buff *skb, u32 info)
  368. {
  369. return __udp4_lib_err(skb, info, udp_hash);
  370. }
  371. /*
  372. * Throw away all pending data and cancel the corking. Socket is locked.
  373. */
  374. static void udp_flush_pending_frames(struct sock *sk)
  375. {
  376. struct udp_sock *up = udp_sk(sk);
  377. if (up->pending) {
  378. up->len = 0;
  379. up->pending = 0;
  380. ip_flush_pending_frames(sk);
  381. }
  382. }
  383. /**
  384. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  385. * @sk: socket we are sending on
  386. * @skb: sk_buff containing the filled-in UDP header
  387. * (checksum field must be zeroed out)
  388. */
  389. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  390. __be32 src, __be32 dst, int len )
  391. {
  392. unsigned int offset;
  393. struct udphdr *uh = udp_hdr(skb);
  394. __wsum csum = 0;
  395. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  396. /*
  397. * Only one fragment on the socket.
  398. */
  399. skb->csum_start = skb_transport_header(skb) - skb->head;
  400. skb->csum_offset = offsetof(struct udphdr, check);
  401. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  402. } else {
  403. /*
  404. * HW-checksum won't work as there are two or more
  405. * fragments on the socket so that all csums of sk_buffs
  406. * should be together
  407. */
  408. offset = skb_transport_offset(skb);
  409. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  410. skb->ip_summed = CHECKSUM_NONE;
  411. skb_queue_walk(&sk->sk_write_queue, skb) {
  412. csum = csum_add(csum, skb->csum);
  413. }
  414. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  415. if (uh->check == 0)
  416. uh->check = CSUM_MANGLED_0;
  417. }
  418. }
  419. /*
  420. * Push out all pending data as one UDP datagram. Socket is locked.
  421. */
  422. static int udp_push_pending_frames(struct sock *sk)
  423. {
  424. struct udp_sock *up = udp_sk(sk);
  425. struct inet_sock *inet = inet_sk(sk);
  426. struct flowi *fl = &inet->cork.fl;
  427. struct sk_buff *skb;
  428. struct udphdr *uh;
  429. int err = 0;
  430. int is_udplite = IS_UDPLITE(sk);
  431. __wsum csum = 0;
  432. /* Grab the skbuff where UDP header space exists. */
  433. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  434. goto out;
  435. /*
  436. * Create a UDP header
  437. */
  438. uh = udp_hdr(skb);
  439. uh->source = fl->fl_ip_sport;
  440. uh->dest = fl->fl_ip_dport;
  441. uh->len = htons(up->len);
  442. uh->check = 0;
  443. if (is_udplite) /* UDP-Lite */
  444. csum = udplite_csum_outgoing(sk, skb);
  445. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  446. skb->ip_summed = CHECKSUM_NONE;
  447. goto send;
  448. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  449. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len);
  450. goto send;
  451. } else /* `normal' UDP */
  452. csum = udp_csum_outgoing(sk, skb);
  453. /* add protocol-dependent pseudo-header */
  454. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  455. sk->sk_protocol, csum );
  456. if (uh->check == 0)
  457. uh->check = CSUM_MANGLED_0;
  458. send:
  459. err = ip_push_pending_frames(sk);
  460. out:
  461. up->len = 0;
  462. up->pending = 0;
  463. if (!err)
  464. UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
  465. return err;
  466. }
  467. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  468. size_t len)
  469. {
  470. struct inet_sock *inet = inet_sk(sk);
  471. struct udp_sock *up = udp_sk(sk);
  472. int ulen = len;
  473. struct ipcm_cookie ipc;
  474. struct rtable *rt = NULL;
  475. int free = 0;
  476. int connected = 0;
  477. __be32 daddr, faddr, saddr;
  478. __be16 dport;
  479. u8 tos;
  480. int err, is_udplite = IS_UDPLITE(sk);
  481. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  482. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  483. if (len > 0xFFFF)
  484. return -EMSGSIZE;
  485. /*
  486. * Check the flags.
  487. */
  488. if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
  489. return -EOPNOTSUPP;
  490. ipc.opt = NULL;
  491. if (up->pending) {
  492. /*
  493. * There are pending frames.
  494. * The socket lock must be held while it's corked.
  495. */
  496. lock_sock(sk);
  497. if (likely(up->pending)) {
  498. if (unlikely(up->pending != AF_INET)) {
  499. release_sock(sk);
  500. return -EINVAL;
  501. }
  502. goto do_append_data;
  503. }
  504. release_sock(sk);
  505. }
  506. ulen += sizeof(struct udphdr);
  507. /*
  508. * Get and verify the address.
  509. */
  510. if (msg->msg_name) {
  511. struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
  512. if (msg->msg_namelen < sizeof(*usin))
  513. return -EINVAL;
  514. if (usin->sin_family != AF_INET) {
  515. if (usin->sin_family != AF_UNSPEC)
  516. return -EAFNOSUPPORT;
  517. }
  518. daddr = usin->sin_addr.s_addr;
  519. dport = usin->sin_port;
  520. if (dport == 0)
  521. return -EINVAL;
  522. } else {
  523. if (sk->sk_state != TCP_ESTABLISHED)
  524. return -EDESTADDRREQ;
  525. daddr = inet->daddr;
  526. dport = inet->dport;
  527. /* Open fast path for connected socket.
  528. Route will not be used, if at least one option is set.
  529. */
  530. connected = 1;
  531. }
  532. ipc.addr = inet->saddr;
  533. ipc.oif = sk->sk_bound_dev_if;
  534. if (msg->msg_controllen) {
  535. err = ip_cmsg_send(msg, &ipc);
  536. if (err)
  537. return err;
  538. if (ipc.opt)
  539. free = 1;
  540. connected = 0;
  541. }
  542. if (!ipc.opt)
  543. ipc.opt = inet->opt;
  544. saddr = ipc.addr;
  545. ipc.addr = faddr = daddr;
  546. if (ipc.opt && ipc.opt->srr) {
  547. if (!daddr)
  548. return -EINVAL;
  549. faddr = ipc.opt->faddr;
  550. connected = 0;
  551. }
  552. tos = RT_TOS(inet->tos);
  553. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  554. (msg->msg_flags & MSG_DONTROUTE) ||
  555. (ipc.opt && ipc.opt->is_strictroute)) {
  556. tos |= RTO_ONLINK;
  557. connected = 0;
  558. }
  559. if (ipv4_is_multicast(daddr)) {
  560. if (!ipc.oif)
  561. ipc.oif = inet->mc_index;
  562. if (!saddr)
  563. saddr = inet->mc_addr;
  564. connected = 0;
  565. }
  566. if (connected)
  567. rt = (struct rtable*)sk_dst_check(sk, 0);
  568. if (rt == NULL) {
  569. struct flowi fl = { .oif = ipc.oif,
  570. .nl_u = { .ip4_u =
  571. { .daddr = faddr,
  572. .saddr = saddr,
  573. .tos = tos } },
  574. .proto = sk->sk_protocol,
  575. .uli_u = { .ports =
  576. { .sport = inet->sport,
  577. .dport = dport } } };
  578. security_sk_classify_flow(sk, &fl);
  579. err = ip_route_output_flow(&rt, &fl, sk, 1);
  580. if (err) {
  581. if (err == -ENETUNREACH)
  582. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  583. goto out;
  584. }
  585. err = -EACCES;
  586. if ((rt->rt_flags & RTCF_BROADCAST) &&
  587. !sock_flag(sk, SOCK_BROADCAST))
  588. goto out;
  589. if (connected)
  590. sk_dst_set(sk, dst_clone(&rt->u.dst));
  591. }
  592. if (msg->msg_flags&MSG_CONFIRM)
  593. goto do_confirm;
  594. back_from_confirm:
  595. saddr = rt->rt_src;
  596. if (!ipc.addr)
  597. daddr = ipc.addr = rt->rt_dst;
  598. lock_sock(sk);
  599. if (unlikely(up->pending)) {
  600. /* The socket is already corked while preparing it. */
  601. /* ... which is an evident application bug. --ANK */
  602. release_sock(sk);
  603. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  604. err = -EINVAL;
  605. goto out;
  606. }
  607. /*
  608. * Now cork the socket to pend data.
  609. */
  610. inet->cork.fl.fl4_dst = daddr;
  611. inet->cork.fl.fl_ip_dport = dport;
  612. inet->cork.fl.fl4_src = saddr;
  613. inet->cork.fl.fl_ip_sport = inet->sport;
  614. up->pending = AF_INET;
  615. do_append_data:
  616. up->len += ulen;
  617. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  618. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  619. sizeof(struct udphdr), &ipc, rt,
  620. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  621. if (err)
  622. udp_flush_pending_frames(sk);
  623. else if (!corkreq)
  624. err = udp_push_pending_frames(sk);
  625. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  626. up->pending = 0;
  627. release_sock(sk);
  628. out:
  629. ip_rt_put(rt);
  630. if (free)
  631. kfree(ipc.opt);
  632. if (!err)
  633. return len;
  634. /*
  635. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  636. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  637. * we don't have a good statistic (IpOutDiscards but it can be too many
  638. * things). We could add another new stat but at least for now that
  639. * seems like overkill.
  640. */
  641. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  642. UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
  643. }
  644. return err;
  645. do_confirm:
  646. dst_confirm(&rt->u.dst);
  647. if (!(msg->msg_flags&MSG_PROBE) || len)
  648. goto back_from_confirm;
  649. err = 0;
  650. goto out;
  651. }
  652. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  653. size_t size, int flags)
  654. {
  655. struct udp_sock *up = udp_sk(sk);
  656. int ret;
  657. if (!up->pending) {
  658. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  659. /* Call udp_sendmsg to specify destination address which
  660. * sendpage interface can't pass.
  661. * This will succeed only when the socket is connected.
  662. */
  663. ret = udp_sendmsg(NULL, sk, &msg, 0);
  664. if (ret < 0)
  665. return ret;
  666. }
  667. lock_sock(sk);
  668. if (unlikely(!up->pending)) {
  669. release_sock(sk);
  670. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  671. return -EINVAL;
  672. }
  673. ret = ip_append_page(sk, page, offset, size, flags);
  674. if (ret == -EOPNOTSUPP) {
  675. release_sock(sk);
  676. return sock_no_sendpage(sk->sk_socket, page, offset,
  677. size, flags);
  678. }
  679. if (ret < 0) {
  680. udp_flush_pending_frames(sk);
  681. goto out;
  682. }
  683. up->len += size;
  684. if (!(up->corkflag || (flags&MSG_MORE)))
  685. ret = udp_push_pending_frames(sk);
  686. if (!ret)
  687. ret = size;
  688. out:
  689. release_sock(sk);
  690. return ret;
  691. }
  692. /*
  693. * IOCTL requests applicable to the UDP protocol
  694. */
  695. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  696. {
  697. switch (cmd) {
  698. case SIOCOUTQ:
  699. {
  700. int amount = atomic_read(&sk->sk_wmem_alloc);
  701. return put_user(amount, (int __user *)arg);
  702. }
  703. case SIOCINQ:
  704. {
  705. struct sk_buff *skb;
  706. unsigned long amount;
  707. amount = 0;
  708. spin_lock_bh(&sk->sk_receive_queue.lock);
  709. skb = skb_peek(&sk->sk_receive_queue);
  710. if (skb != NULL) {
  711. /*
  712. * We will only return the amount
  713. * of this packet since that is all
  714. * that will be read.
  715. */
  716. amount = skb->len - sizeof(struct udphdr);
  717. }
  718. spin_unlock_bh(&sk->sk_receive_queue.lock);
  719. return put_user(amount, (int __user *)arg);
  720. }
  721. default:
  722. return -ENOIOCTLCMD;
  723. }
  724. return 0;
  725. }
  726. /*
  727. * This should be easy, if there is something there we
  728. * return it, otherwise we block.
  729. */
  730. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  731. size_t len, int noblock, int flags, int *addr_len)
  732. {
  733. struct inet_sock *inet = inet_sk(sk);
  734. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  735. struct sk_buff *skb;
  736. unsigned int ulen, copied;
  737. int peeked;
  738. int err;
  739. int is_udplite = IS_UDPLITE(sk);
  740. /*
  741. * Check any passed addresses
  742. */
  743. if (addr_len)
  744. *addr_len=sizeof(*sin);
  745. if (flags & MSG_ERRQUEUE)
  746. return ip_recv_error(sk, msg, len);
  747. try_again:
  748. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  749. &peeked, &err);
  750. if (!skb)
  751. goto out;
  752. ulen = skb->len - sizeof(struct udphdr);
  753. copied = len;
  754. if (copied > ulen)
  755. copied = ulen;
  756. else if (copied < ulen)
  757. msg->msg_flags |= MSG_TRUNC;
  758. /*
  759. * If checksum is needed at all, try to do it while copying the
  760. * data. If the data is truncated, or if we only want a partial
  761. * coverage checksum (UDP-Lite), do it before the copy.
  762. */
  763. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  764. if (udp_lib_checksum_complete(skb))
  765. goto csum_copy_err;
  766. }
  767. if (skb_csum_unnecessary(skb))
  768. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  769. msg->msg_iov, copied );
  770. else {
  771. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  772. if (err == -EINVAL)
  773. goto csum_copy_err;
  774. }
  775. if (err)
  776. goto out_free;
  777. if (!peeked)
  778. UDP_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite);
  779. sock_recv_timestamp(msg, sk, skb);
  780. /* Copy the address. */
  781. if (sin)
  782. {
  783. sin->sin_family = AF_INET;
  784. sin->sin_port = udp_hdr(skb)->source;
  785. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  786. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  787. }
  788. if (inet->cmsg_flags)
  789. ip_cmsg_recv(msg, skb);
  790. err = copied;
  791. if (flags & MSG_TRUNC)
  792. err = ulen;
  793. out_free:
  794. skb_free_datagram(sk, skb);
  795. out:
  796. return err;
  797. csum_copy_err:
  798. if (!skb_kill_datagram(sk, skb, flags))
  799. UDP_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite);
  800. if (noblock)
  801. return -EAGAIN;
  802. goto try_again;
  803. }
  804. int udp_disconnect(struct sock *sk, int flags)
  805. {
  806. struct inet_sock *inet = inet_sk(sk);
  807. /*
  808. * 1003.1g - break association.
  809. */
  810. sk->sk_state = TCP_CLOSE;
  811. inet->daddr = 0;
  812. inet->dport = 0;
  813. sk->sk_bound_dev_if = 0;
  814. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  815. inet_reset_saddr(sk);
  816. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  817. sk->sk_prot->unhash(sk);
  818. inet->sport = 0;
  819. }
  820. sk_dst_reset(sk);
  821. return 0;
  822. }
  823. /* returns:
  824. * -1: error
  825. * 0: success
  826. * >0: "udp encap" protocol resubmission
  827. *
  828. * Note that in the success and error cases, the skb is assumed to
  829. * have either been requeued or freed.
  830. */
  831. int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  832. {
  833. struct udp_sock *up = udp_sk(sk);
  834. int rc;
  835. int is_udplite = IS_UDPLITE(sk);
  836. /*
  837. * Charge it to the socket, dropping if the queue is full.
  838. */
  839. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  840. goto drop;
  841. nf_reset(skb);
  842. if (up->encap_type) {
  843. /*
  844. * This is an encapsulation socket so pass the skb to
  845. * the socket's udp_encap_rcv() hook. Otherwise, just
  846. * fall through and pass this up the UDP socket.
  847. * up->encap_rcv() returns the following value:
  848. * =0 if skb was successfully passed to the encap
  849. * handler or was discarded by it.
  850. * >0 if skb should be passed on to UDP.
  851. * <0 if skb should be resubmitted as proto -N
  852. */
  853. /* if we're overly short, let UDP handle it */
  854. if (skb->len > sizeof(struct udphdr) &&
  855. up->encap_rcv != NULL) {
  856. int ret;
  857. ret = (*up->encap_rcv)(sk, skb);
  858. if (ret <= 0) {
  859. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS,
  860. is_udplite);
  861. return -ret;
  862. }
  863. }
  864. /* FALLTHROUGH -- it's a UDP Packet */
  865. }
  866. /*
  867. * UDP-Lite specific tests, ignored on UDP sockets
  868. */
  869. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  870. /*
  871. * MIB statistics other than incrementing the error count are
  872. * disabled for the following two types of errors: these depend
  873. * on the application settings, not on the functioning of the
  874. * protocol stack as such.
  875. *
  876. * RFC 3828 here recommends (sec 3.3): "There should also be a
  877. * way ... to ... at least let the receiving application block
  878. * delivery of packets with coverage values less than a value
  879. * provided by the application."
  880. */
  881. if (up->pcrlen == 0) { /* full coverage was set */
  882. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  883. "%d while full coverage %d requested\n",
  884. UDP_SKB_CB(skb)->cscov, skb->len);
  885. goto drop;
  886. }
  887. /* The next case involves violating the min. coverage requested
  888. * by the receiver. This is subtle: if receiver wants x and x is
  889. * greater than the buffersize/MTU then receiver will complain
  890. * that it wants x while sender emits packets of smaller size y.
  891. * Therefore the above ...()->partial_cov statement is essential.
  892. */
  893. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  894. LIMIT_NETDEBUG(KERN_WARNING
  895. "UDPLITE: coverage %d too small, need min %d\n",
  896. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  897. goto drop;
  898. }
  899. }
  900. if (sk->sk_filter) {
  901. if (udp_lib_checksum_complete(skb))
  902. goto drop;
  903. }
  904. if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
  905. /* Note that an ENOMEM error is charged twice */
  906. if (rc == -ENOMEM)
  907. UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite);
  908. goto drop;
  909. }
  910. return 0;
  911. drop:
  912. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
  913. kfree_skb(skb);
  914. return -1;
  915. }
  916. /*
  917. * Multicasts and broadcasts go to each listener.
  918. *
  919. * Note: called only from the BH handler context,
  920. * so we don't need to lock the hashes.
  921. */
  922. static int __udp4_lib_mcast_deliver(struct sk_buff *skb,
  923. struct udphdr *uh,
  924. __be32 saddr, __be32 daddr,
  925. struct hlist_head udptable[])
  926. {
  927. struct sock *sk;
  928. int dif;
  929. read_lock(&udp_hash_lock);
  930. sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  931. dif = skb->dev->ifindex;
  932. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  933. if (sk) {
  934. struct sock *sknext = NULL;
  935. do {
  936. struct sk_buff *skb1 = skb;
  937. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  938. uh->source, saddr, dif);
  939. if (sknext)
  940. skb1 = skb_clone(skb, GFP_ATOMIC);
  941. if (skb1) {
  942. int ret = udp_queue_rcv_skb(sk, skb1);
  943. if (ret > 0)
  944. /* we should probably re-process instead
  945. * of dropping packets here. */
  946. kfree_skb(skb1);
  947. }
  948. sk = sknext;
  949. } while (sknext);
  950. } else
  951. kfree_skb(skb);
  952. read_unlock(&udp_hash_lock);
  953. return 0;
  954. }
  955. /* Initialize UDP checksum. If exited with zero value (success),
  956. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  957. * Otherwise, csum completion requires chacksumming packet body,
  958. * including udp header and folding it to skb->csum.
  959. */
  960. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  961. int proto)
  962. {
  963. const struct iphdr *iph;
  964. int err;
  965. UDP_SKB_CB(skb)->partial_cov = 0;
  966. UDP_SKB_CB(skb)->cscov = skb->len;
  967. if (proto == IPPROTO_UDPLITE) {
  968. err = udplite_checksum_init(skb, uh);
  969. if (err)
  970. return err;
  971. }
  972. iph = ip_hdr(skb);
  973. if (uh->check == 0) {
  974. skb->ip_summed = CHECKSUM_UNNECESSARY;
  975. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  976. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  977. proto, skb->csum))
  978. skb->ip_summed = CHECKSUM_UNNECESSARY;
  979. }
  980. if (!skb_csum_unnecessary(skb))
  981. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  982. skb->len, proto, 0);
  983. /* Probably, we should checksum udp header (it should be in cache
  984. * in any case) and data in tiny packets (< rx copybreak).
  985. */
  986. return 0;
  987. }
  988. /*
  989. * All we need to do is get the socket, and then do a checksum.
  990. */
  991. int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
  992. int proto)
  993. {
  994. struct sock *sk;
  995. struct udphdr *uh = udp_hdr(skb);
  996. unsigned short ulen;
  997. struct rtable *rt = (struct rtable*)skb->dst;
  998. __be32 saddr = ip_hdr(skb)->saddr;
  999. __be32 daddr = ip_hdr(skb)->daddr;
  1000. /*
  1001. * Validate the packet.
  1002. */
  1003. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1004. goto drop; /* No space for header. */
  1005. ulen = ntohs(uh->len);
  1006. if (ulen > skb->len)
  1007. goto short_packet;
  1008. if (proto == IPPROTO_UDP) {
  1009. /* UDP validates ulen. */
  1010. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1011. goto short_packet;
  1012. uh = udp_hdr(skb);
  1013. }
  1014. if (udp4_csum_init(skb, uh, proto))
  1015. goto csum_error;
  1016. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1017. return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable);
  1018. sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest,
  1019. inet_iif(skb), udptable);
  1020. if (sk != NULL) {
  1021. int ret = udp_queue_rcv_skb(sk, skb);
  1022. sock_put(sk);
  1023. /* a return value > 0 means to resubmit the input, but
  1024. * it wants the return to be -protocol, or 0
  1025. */
  1026. if (ret > 0)
  1027. return -ret;
  1028. return 0;
  1029. }
  1030. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1031. goto drop;
  1032. nf_reset(skb);
  1033. /* No socket. Drop packet silently, if checksum is wrong */
  1034. if (udp_lib_checksum_complete(skb))
  1035. goto csum_error;
  1036. UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1037. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1038. /*
  1039. * Hmm. We got an UDP packet to a port to which we
  1040. * don't wanna listen. Ignore it.
  1041. */
  1042. kfree_skb(skb);
  1043. return 0;
  1044. short_packet:
  1045. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1046. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1047. NIPQUAD(saddr),
  1048. ntohs(uh->source),
  1049. ulen,
  1050. skb->len,
  1051. NIPQUAD(daddr),
  1052. ntohs(uh->dest));
  1053. goto drop;
  1054. csum_error:
  1055. /*
  1056. * RFC1122: OK. Discards the bad packet silently (as far as
  1057. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1058. */
  1059. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1060. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1061. NIPQUAD(saddr),
  1062. ntohs(uh->source),
  1063. NIPQUAD(daddr),
  1064. ntohs(uh->dest),
  1065. ulen);
  1066. drop:
  1067. UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1068. kfree_skb(skb);
  1069. return 0;
  1070. }
  1071. int udp_rcv(struct sk_buff *skb)
  1072. {
  1073. return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP);
  1074. }
  1075. int udp_destroy_sock(struct sock *sk)
  1076. {
  1077. lock_sock(sk);
  1078. udp_flush_pending_frames(sk);
  1079. release_sock(sk);
  1080. return 0;
  1081. }
  1082. /*
  1083. * Socket option code for UDP
  1084. */
  1085. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1086. char __user *optval, int optlen,
  1087. int (*push_pending_frames)(struct sock *))
  1088. {
  1089. struct udp_sock *up = udp_sk(sk);
  1090. int val;
  1091. int err = 0;
  1092. int is_udplite = IS_UDPLITE(sk);
  1093. if (optlen<sizeof(int))
  1094. return -EINVAL;
  1095. if (get_user(val, (int __user *)optval))
  1096. return -EFAULT;
  1097. switch (optname) {
  1098. case UDP_CORK:
  1099. if (val != 0) {
  1100. up->corkflag = 1;
  1101. } else {
  1102. up->corkflag = 0;
  1103. lock_sock(sk);
  1104. (*push_pending_frames)(sk);
  1105. release_sock(sk);
  1106. }
  1107. break;
  1108. case UDP_ENCAP:
  1109. switch (val) {
  1110. case 0:
  1111. case UDP_ENCAP_ESPINUDP:
  1112. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1113. up->encap_rcv = xfrm4_udp_encap_rcv;
  1114. /* FALLTHROUGH */
  1115. case UDP_ENCAP_L2TPINUDP:
  1116. up->encap_type = val;
  1117. break;
  1118. default:
  1119. err = -ENOPROTOOPT;
  1120. break;
  1121. }
  1122. break;
  1123. /*
  1124. * UDP-Lite's partial checksum coverage (RFC 3828).
  1125. */
  1126. /* The sender sets actual checksum coverage length via this option.
  1127. * The case coverage > packet length is handled by send module. */
  1128. case UDPLITE_SEND_CSCOV:
  1129. if (!is_udplite) /* Disable the option on UDP sockets */
  1130. return -ENOPROTOOPT;
  1131. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1132. val = 8;
  1133. up->pcslen = val;
  1134. up->pcflag |= UDPLITE_SEND_CC;
  1135. break;
  1136. /* The receiver specifies a minimum checksum coverage value. To make
  1137. * sense, this should be set to at least 8 (as done below). If zero is
  1138. * used, this again means full checksum coverage. */
  1139. case UDPLITE_RECV_CSCOV:
  1140. if (!is_udplite) /* Disable the option on UDP sockets */
  1141. return -ENOPROTOOPT;
  1142. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1143. val = 8;
  1144. up->pcrlen = val;
  1145. up->pcflag |= UDPLITE_RECV_CC;
  1146. break;
  1147. default:
  1148. err = -ENOPROTOOPT;
  1149. break;
  1150. }
  1151. return err;
  1152. }
  1153. int udp_setsockopt(struct sock *sk, int level, int optname,
  1154. char __user *optval, int optlen)
  1155. {
  1156. if (level == SOL_UDP || level == SOL_UDPLITE)
  1157. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1158. udp_push_pending_frames);
  1159. return ip_setsockopt(sk, level, optname, optval, optlen);
  1160. }
  1161. #ifdef CONFIG_COMPAT
  1162. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1163. char __user *optval, int optlen)
  1164. {
  1165. if (level == SOL_UDP || level == SOL_UDPLITE)
  1166. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1167. udp_push_pending_frames);
  1168. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1169. }
  1170. #endif
  1171. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1172. char __user *optval, int __user *optlen)
  1173. {
  1174. struct udp_sock *up = udp_sk(sk);
  1175. int val, len;
  1176. if (get_user(len,optlen))
  1177. return -EFAULT;
  1178. len = min_t(unsigned int, len, sizeof(int));
  1179. if (len < 0)
  1180. return -EINVAL;
  1181. switch (optname) {
  1182. case UDP_CORK:
  1183. val = up->corkflag;
  1184. break;
  1185. case UDP_ENCAP:
  1186. val = up->encap_type;
  1187. break;
  1188. /* The following two cannot be changed on UDP sockets, the return is
  1189. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1190. case UDPLITE_SEND_CSCOV:
  1191. val = up->pcslen;
  1192. break;
  1193. case UDPLITE_RECV_CSCOV:
  1194. val = up->pcrlen;
  1195. break;
  1196. default:
  1197. return -ENOPROTOOPT;
  1198. }
  1199. if (put_user(len, optlen))
  1200. return -EFAULT;
  1201. if (copy_to_user(optval, &val,len))
  1202. return -EFAULT;
  1203. return 0;
  1204. }
  1205. int udp_getsockopt(struct sock *sk, int level, int optname,
  1206. char __user *optval, int __user *optlen)
  1207. {
  1208. if (level == SOL_UDP || level == SOL_UDPLITE)
  1209. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1210. return ip_getsockopt(sk, level, optname, optval, optlen);
  1211. }
  1212. #ifdef CONFIG_COMPAT
  1213. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1214. char __user *optval, int __user *optlen)
  1215. {
  1216. if (level == SOL_UDP || level == SOL_UDPLITE)
  1217. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1218. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1219. }
  1220. #endif
  1221. /**
  1222. * udp_poll - wait for a UDP event.
  1223. * @file - file struct
  1224. * @sock - socket
  1225. * @wait - poll table
  1226. *
  1227. * This is same as datagram poll, except for the special case of
  1228. * blocking sockets. If application is using a blocking fd
  1229. * and a packet with checksum error is in the queue;
  1230. * then it could get return from select indicating data available
  1231. * but then block when reading it. Add special case code
  1232. * to work around these arguably broken applications.
  1233. */
  1234. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1235. {
  1236. unsigned int mask = datagram_poll(file, sock, wait);
  1237. struct sock *sk = sock->sk;
  1238. int is_lite = IS_UDPLITE(sk);
  1239. /* Check for false positives due to checksum errors */
  1240. if ( (mask & POLLRDNORM) &&
  1241. !(file->f_flags & O_NONBLOCK) &&
  1242. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1243. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1244. struct sk_buff *skb;
  1245. spin_lock_bh(&rcvq->lock);
  1246. while ((skb = skb_peek(rcvq)) != NULL &&
  1247. udp_lib_checksum_complete(skb)) {
  1248. UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite);
  1249. __skb_unlink(skb, rcvq);
  1250. kfree_skb(skb);
  1251. }
  1252. spin_unlock_bh(&rcvq->lock);
  1253. /* nothing to see, move along */
  1254. if (skb == NULL)
  1255. mask &= ~(POLLIN | POLLRDNORM);
  1256. }
  1257. return mask;
  1258. }
  1259. DEFINE_PROTO_INUSE(udp)
  1260. struct proto udp_prot = {
  1261. .name = "UDP",
  1262. .owner = THIS_MODULE,
  1263. .close = udp_lib_close,
  1264. .connect = ip4_datagram_connect,
  1265. .disconnect = udp_disconnect,
  1266. .ioctl = udp_ioctl,
  1267. .destroy = udp_destroy_sock,
  1268. .setsockopt = udp_setsockopt,
  1269. .getsockopt = udp_getsockopt,
  1270. .sendmsg = udp_sendmsg,
  1271. .recvmsg = udp_recvmsg,
  1272. .sendpage = udp_sendpage,
  1273. .backlog_rcv = udp_queue_rcv_skb,
  1274. .hash = udp_lib_hash,
  1275. .unhash = udp_lib_unhash,
  1276. .get_port = udp_v4_get_port,
  1277. .obj_size = sizeof(struct udp_sock),
  1278. #ifdef CONFIG_COMPAT
  1279. .compat_setsockopt = compat_udp_setsockopt,
  1280. .compat_getsockopt = compat_udp_getsockopt,
  1281. #endif
  1282. REF_PROTO_INUSE(udp)
  1283. };
  1284. /* ------------------------------------------------------------------------ */
  1285. #ifdef CONFIG_PROC_FS
  1286. static struct sock *udp_get_first(struct seq_file *seq)
  1287. {
  1288. struct sock *sk;
  1289. struct udp_iter_state *state = seq->private;
  1290. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1291. struct hlist_node *node;
  1292. sk_for_each(sk, node, state->hashtable + state->bucket) {
  1293. if (sk->sk_family == state->family)
  1294. goto found;
  1295. }
  1296. }
  1297. sk = NULL;
  1298. found:
  1299. return sk;
  1300. }
  1301. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1302. {
  1303. struct udp_iter_state *state = seq->private;
  1304. do {
  1305. sk = sk_next(sk);
  1306. try_again:
  1307. ;
  1308. } while (sk && sk->sk_family != state->family);
  1309. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1310. sk = sk_head(state->hashtable + state->bucket);
  1311. goto try_again;
  1312. }
  1313. return sk;
  1314. }
  1315. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1316. {
  1317. struct sock *sk = udp_get_first(seq);
  1318. if (sk)
  1319. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1320. --pos;
  1321. return pos ? NULL : sk;
  1322. }
  1323. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1324. {
  1325. read_lock(&udp_hash_lock);
  1326. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1327. }
  1328. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1329. {
  1330. struct sock *sk;
  1331. if (v == (void *)1)
  1332. sk = udp_get_idx(seq, 0);
  1333. else
  1334. sk = udp_get_next(seq, v);
  1335. ++*pos;
  1336. return sk;
  1337. }
  1338. static void udp_seq_stop(struct seq_file *seq, void *v)
  1339. {
  1340. read_unlock(&udp_hash_lock);
  1341. }
  1342. static int udp_seq_open(struct inode *inode, struct file *file)
  1343. {
  1344. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1345. struct seq_file *seq;
  1346. int rc = -ENOMEM;
  1347. struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  1348. if (!s)
  1349. goto out;
  1350. s->family = afinfo->family;
  1351. s->hashtable = afinfo->hashtable;
  1352. s->seq_ops.start = udp_seq_start;
  1353. s->seq_ops.next = udp_seq_next;
  1354. s->seq_ops.show = afinfo->seq_show;
  1355. s->seq_ops.stop = udp_seq_stop;
  1356. rc = seq_open(file, &s->seq_ops);
  1357. if (rc)
  1358. goto out_kfree;
  1359. seq = file->private_data;
  1360. seq->private = s;
  1361. out:
  1362. return rc;
  1363. out_kfree:
  1364. kfree(s);
  1365. goto out;
  1366. }
  1367. /* ------------------------------------------------------------------------ */
  1368. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1369. {
  1370. struct proc_dir_entry *p;
  1371. int rc = 0;
  1372. if (!afinfo)
  1373. return -EINVAL;
  1374. afinfo->seq_fops->owner = afinfo->owner;
  1375. afinfo->seq_fops->open = udp_seq_open;
  1376. afinfo->seq_fops->read = seq_read;
  1377. afinfo->seq_fops->llseek = seq_lseek;
  1378. afinfo->seq_fops->release = seq_release_private;
  1379. p = proc_net_fops_create(&init_net, afinfo->name, S_IRUGO, afinfo->seq_fops);
  1380. if (p)
  1381. p->data = afinfo;
  1382. else
  1383. rc = -ENOMEM;
  1384. return rc;
  1385. }
  1386. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1387. {
  1388. if (!afinfo)
  1389. return;
  1390. proc_net_remove(&init_net, afinfo->name);
  1391. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1392. }
  1393. /* ------------------------------------------------------------------------ */
  1394. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1395. {
  1396. struct inet_sock *inet = inet_sk(sp);
  1397. __be32 dest = inet->daddr;
  1398. __be32 src = inet->rcv_saddr;
  1399. __u16 destp = ntohs(inet->dport);
  1400. __u16 srcp = ntohs(inet->sport);
  1401. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1402. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1403. bucket, src, srcp, dest, destp, sp->sk_state,
  1404. atomic_read(&sp->sk_wmem_alloc),
  1405. atomic_read(&sp->sk_rmem_alloc),
  1406. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1407. atomic_read(&sp->sk_refcnt), sp);
  1408. }
  1409. int udp4_seq_show(struct seq_file *seq, void *v)
  1410. {
  1411. if (v == SEQ_START_TOKEN)
  1412. seq_printf(seq, "%-127s\n",
  1413. " sl local_address rem_address st tx_queue "
  1414. "rx_queue tr tm->when retrnsmt uid timeout "
  1415. "inode");
  1416. else {
  1417. char tmpbuf[129];
  1418. struct udp_iter_state *state = seq->private;
  1419. udp4_format_sock(v, tmpbuf, state->bucket);
  1420. seq_printf(seq, "%-127s\n", tmpbuf);
  1421. }
  1422. return 0;
  1423. }
  1424. /* ------------------------------------------------------------------------ */
  1425. static struct file_operations udp4_seq_fops;
  1426. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1427. .owner = THIS_MODULE,
  1428. .name = "udp",
  1429. .family = AF_INET,
  1430. .hashtable = udp_hash,
  1431. .seq_show = udp4_seq_show,
  1432. .seq_fops = &udp4_seq_fops,
  1433. };
  1434. int __init udp4_proc_init(void)
  1435. {
  1436. return udp_proc_register(&udp4_seq_afinfo);
  1437. }
  1438. void udp4_proc_exit(void)
  1439. {
  1440. udp_proc_unregister(&udp4_seq_afinfo);
  1441. }
  1442. #endif /* CONFIG_PROC_FS */
  1443. EXPORT_SYMBOL(udp_disconnect);
  1444. EXPORT_SYMBOL(udp_hash);
  1445. EXPORT_SYMBOL(udp_hash_lock);
  1446. EXPORT_SYMBOL(udp_ioctl);
  1447. EXPORT_SYMBOL(udp_get_port);
  1448. EXPORT_SYMBOL(udp_prot);
  1449. EXPORT_SYMBOL(udp_sendmsg);
  1450. EXPORT_SYMBOL(udp_lib_getsockopt);
  1451. EXPORT_SYMBOL(udp_lib_setsockopt);
  1452. EXPORT_SYMBOL(udp_poll);
  1453. #ifdef CONFIG_PROC_FS
  1454. EXPORT_SYMBOL(udp_proc_register);
  1455. EXPORT_SYMBOL(udp_proc_unregister);
  1456. #endif