udp.c 43 KB

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