udp.c 41 KB

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