udp.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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(net, 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. struct net *net = sock_net(sk);
  586. security_sk_classify_flow(sk, &fl);
  587. err = ip_route_output_flow(net, &rt, &fl, sk, 1);
  588. if (err) {
  589. if (err == -ENETUNREACH)
  590. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  591. goto out;
  592. }
  593. err = -EACCES;
  594. if ((rt->rt_flags & RTCF_BROADCAST) &&
  595. !sock_flag(sk, SOCK_BROADCAST))
  596. goto out;
  597. if (connected)
  598. sk_dst_set(sk, dst_clone(&rt->u.dst));
  599. }
  600. if (msg->msg_flags&MSG_CONFIRM)
  601. goto do_confirm;
  602. back_from_confirm:
  603. saddr = rt->rt_src;
  604. if (!ipc.addr)
  605. daddr = ipc.addr = rt->rt_dst;
  606. lock_sock(sk);
  607. if (unlikely(up->pending)) {
  608. /* The socket is already corked while preparing it. */
  609. /* ... which is an evident application bug. --ANK */
  610. release_sock(sk);
  611. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  612. err = -EINVAL;
  613. goto out;
  614. }
  615. /*
  616. * Now cork the socket to pend data.
  617. */
  618. inet->cork.fl.fl4_dst = daddr;
  619. inet->cork.fl.fl_ip_dport = dport;
  620. inet->cork.fl.fl4_src = saddr;
  621. inet->cork.fl.fl_ip_sport = inet->sport;
  622. up->pending = AF_INET;
  623. do_append_data:
  624. up->len += ulen;
  625. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  626. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  627. sizeof(struct udphdr), &ipc, rt,
  628. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  629. if (err)
  630. udp_flush_pending_frames(sk);
  631. else if (!corkreq)
  632. err = udp_push_pending_frames(sk);
  633. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  634. up->pending = 0;
  635. release_sock(sk);
  636. out:
  637. ip_rt_put(rt);
  638. if (free)
  639. kfree(ipc.opt);
  640. if (!err)
  641. return len;
  642. /*
  643. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  644. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  645. * we don't have a good statistic (IpOutDiscards but it can be too many
  646. * things). We could add another new stat but at least for now that
  647. * seems like overkill.
  648. */
  649. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  650. UDP_INC_STATS_USER(sock_net(sk),
  651. UDP_MIB_SNDBUFERRORS, is_udplite);
  652. }
  653. return err;
  654. do_confirm:
  655. dst_confirm(&rt->u.dst);
  656. if (!(msg->msg_flags&MSG_PROBE) || len)
  657. goto back_from_confirm;
  658. err = 0;
  659. goto out;
  660. }
  661. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  662. size_t size, int flags)
  663. {
  664. struct udp_sock *up = udp_sk(sk);
  665. int ret;
  666. if (!up->pending) {
  667. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  668. /* Call udp_sendmsg to specify destination address which
  669. * sendpage interface can't pass.
  670. * This will succeed only when the socket is connected.
  671. */
  672. ret = udp_sendmsg(NULL, sk, &msg, 0);
  673. if (ret < 0)
  674. return ret;
  675. }
  676. lock_sock(sk);
  677. if (unlikely(!up->pending)) {
  678. release_sock(sk);
  679. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  680. return -EINVAL;
  681. }
  682. ret = ip_append_page(sk, page, offset, size, flags);
  683. if (ret == -EOPNOTSUPP) {
  684. release_sock(sk);
  685. return sock_no_sendpage(sk->sk_socket, page, offset,
  686. size, flags);
  687. }
  688. if (ret < 0) {
  689. udp_flush_pending_frames(sk);
  690. goto out;
  691. }
  692. up->len += size;
  693. if (!(up->corkflag || (flags&MSG_MORE)))
  694. ret = udp_push_pending_frames(sk);
  695. if (!ret)
  696. ret = size;
  697. out:
  698. release_sock(sk);
  699. return ret;
  700. }
  701. /*
  702. * IOCTL requests applicable to the UDP protocol
  703. */
  704. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  705. {
  706. switch (cmd) {
  707. case SIOCOUTQ:
  708. {
  709. int amount = atomic_read(&sk->sk_wmem_alloc);
  710. return put_user(amount, (int __user *)arg);
  711. }
  712. case SIOCINQ:
  713. {
  714. struct sk_buff *skb;
  715. unsigned long amount;
  716. amount = 0;
  717. spin_lock_bh(&sk->sk_receive_queue.lock);
  718. skb = skb_peek(&sk->sk_receive_queue);
  719. if (skb != NULL) {
  720. /*
  721. * We will only return the amount
  722. * of this packet since that is all
  723. * that will be read.
  724. */
  725. amount = skb->len - sizeof(struct udphdr);
  726. }
  727. spin_unlock_bh(&sk->sk_receive_queue.lock);
  728. return put_user(amount, (int __user *)arg);
  729. }
  730. default:
  731. return -ENOIOCTLCMD;
  732. }
  733. return 0;
  734. }
  735. /*
  736. * This should be easy, if there is something there we
  737. * return it, otherwise we block.
  738. */
  739. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  740. size_t len, int noblock, int flags, int *addr_len)
  741. {
  742. struct inet_sock *inet = inet_sk(sk);
  743. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  744. struct sk_buff *skb;
  745. unsigned int ulen, copied;
  746. int peeked;
  747. int err;
  748. int is_udplite = IS_UDPLITE(sk);
  749. /*
  750. * Check any passed addresses
  751. */
  752. if (addr_len)
  753. *addr_len=sizeof(*sin);
  754. if (flags & MSG_ERRQUEUE)
  755. return ip_recv_error(sk, msg, len);
  756. try_again:
  757. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  758. &peeked, &err);
  759. if (!skb)
  760. goto out;
  761. ulen = skb->len - sizeof(struct udphdr);
  762. copied = len;
  763. if (copied > ulen)
  764. copied = ulen;
  765. else if (copied < ulen)
  766. msg->msg_flags |= MSG_TRUNC;
  767. /*
  768. * If checksum is needed at all, try to do it while copying the
  769. * data. If the data is truncated, or if we only want a partial
  770. * coverage checksum (UDP-Lite), do it before the copy.
  771. */
  772. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  773. if (udp_lib_checksum_complete(skb))
  774. goto csum_copy_err;
  775. }
  776. if (skb_csum_unnecessary(skb))
  777. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  778. msg->msg_iov, copied );
  779. else {
  780. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  781. if (err == -EINVAL)
  782. goto csum_copy_err;
  783. }
  784. if (err)
  785. goto out_free;
  786. if (!peeked)
  787. UDP_INC_STATS_USER(sock_net(sk),
  788. UDP_MIB_INDATAGRAMS, is_udplite);
  789. sock_recv_timestamp(msg, sk, skb);
  790. /* Copy the address. */
  791. if (sin)
  792. {
  793. sin->sin_family = AF_INET;
  794. sin->sin_port = udp_hdr(skb)->source;
  795. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  796. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  797. }
  798. if (inet->cmsg_flags)
  799. ip_cmsg_recv(msg, skb);
  800. err = copied;
  801. if (flags & MSG_TRUNC)
  802. err = ulen;
  803. out_free:
  804. lock_sock(sk);
  805. skb_free_datagram(sk, skb);
  806. release_sock(sk);
  807. out:
  808. return err;
  809. csum_copy_err:
  810. lock_sock(sk);
  811. if (!skb_kill_datagram(sk, skb, flags))
  812. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  813. release_sock(sk);
  814. if (noblock)
  815. return -EAGAIN;
  816. goto try_again;
  817. }
  818. int udp_disconnect(struct sock *sk, int flags)
  819. {
  820. struct inet_sock *inet = inet_sk(sk);
  821. /*
  822. * 1003.1g - break association.
  823. */
  824. sk->sk_state = TCP_CLOSE;
  825. inet->daddr = 0;
  826. inet->dport = 0;
  827. sk->sk_bound_dev_if = 0;
  828. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  829. inet_reset_saddr(sk);
  830. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  831. sk->sk_prot->unhash(sk);
  832. inet->sport = 0;
  833. }
  834. sk_dst_reset(sk);
  835. return 0;
  836. }
  837. /* returns:
  838. * -1: error
  839. * 0: success
  840. * >0: "udp encap" protocol resubmission
  841. *
  842. * Note that in the success and error cases, the skb is assumed to
  843. * have either been requeued or freed.
  844. */
  845. int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  846. {
  847. struct udp_sock *up = udp_sk(sk);
  848. int rc;
  849. int is_udplite = IS_UDPLITE(sk);
  850. /*
  851. * Charge it to the socket, dropping if the queue is full.
  852. */
  853. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  854. goto drop;
  855. nf_reset(skb);
  856. if (up->encap_type) {
  857. /*
  858. * This is an encapsulation socket so pass the skb to
  859. * the socket's udp_encap_rcv() hook. Otherwise, just
  860. * fall through and pass this up the UDP socket.
  861. * up->encap_rcv() returns the following value:
  862. * =0 if skb was successfully passed to the encap
  863. * handler or was discarded by it.
  864. * >0 if skb should be passed on to UDP.
  865. * <0 if skb should be resubmitted as proto -N
  866. */
  867. /* if we're overly short, let UDP handle it */
  868. if (skb->len > sizeof(struct udphdr) &&
  869. up->encap_rcv != NULL) {
  870. int ret;
  871. ret = (*up->encap_rcv)(sk, skb);
  872. if (ret <= 0) {
  873. UDP_INC_STATS_BH(sock_net(sk),
  874. UDP_MIB_INDATAGRAMS,
  875. is_udplite);
  876. return -ret;
  877. }
  878. }
  879. /* FALLTHROUGH -- it's a UDP Packet */
  880. }
  881. /*
  882. * UDP-Lite specific tests, ignored on UDP sockets
  883. */
  884. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  885. /*
  886. * MIB statistics other than incrementing the error count are
  887. * disabled for the following two types of errors: these depend
  888. * on the application settings, not on the functioning of the
  889. * protocol stack as such.
  890. *
  891. * RFC 3828 here recommends (sec 3.3): "There should also be a
  892. * way ... to ... at least let the receiving application block
  893. * delivery of packets with coverage values less than a value
  894. * provided by the application."
  895. */
  896. if (up->pcrlen == 0) { /* full coverage was set */
  897. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  898. "%d while full coverage %d requested\n",
  899. UDP_SKB_CB(skb)->cscov, skb->len);
  900. goto drop;
  901. }
  902. /* The next case involves violating the min. coverage requested
  903. * by the receiver. This is subtle: if receiver wants x and x is
  904. * greater than the buffersize/MTU then receiver will complain
  905. * that it wants x while sender emits packets of smaller size y.
  906. * Therefore the above ...()->partial_cov statement is essential.
  907. */
  908. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  909. LIMIT_NETDEBUG(KERN_WARNING
  910. "UDPLITE: coverage %d too small, need min %d\n",
  911. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  912. goto drop;
  913. }
  914. }
  915. if (sk->sk_filter) {
  916. if (udp_lib_checksum_complete(skb))
  917. goto drop;
  918. }
  919. if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
  920. /* Note that an ENOMEM error is charged twice */
  921. if (rc == -ENOMEM) {
  922. UDP_INC_STATS_BH(sock_net(sk),
  923. UDP_MIB_RCVBUFERRORS, is_udplite);
  924. atomic_inc(&sk->sk_drops);
  925. }
  926. goto drop;
  927. }
  928. return 0;
  929. drop:
  930. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  931. kfree_skb(skb);
  932. return -1;
  933. }
  934. /*
  935. * Multicasts and broadcasts go to each listener.
  936. *
  937. * Note: called only from the BH handler context,
  938. * so we don't need to lock the hashes.
  939. */
  940. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  941. struct udphdr *uh,
  942. __be32 saddr, __be32 daddr,
  943. struct hlist_head udptable[])
  944. {
  945. struct sock *sk;
  946. int dif;
  947. read_lock(&udp_hash_lock);
  948. sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]);
  949. dif = skb->dev->ifindex;
  950. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  951. if (sk) {
  952. struct sock *sknext = NULL;
  953. do {
  954. struct sk_buff *skb1 = skb;
  955. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  956. uh->source, saddr, dif);
  957. if (sknext)
  958. skb1 = skb_clone(skb, GFP_ATOMIC);
  959. if (skb1) {
  960. int ret = 0;
  961. bh_lock_sock_nested(sk);
  962. if (!sock_owned_by_user(sk))
  963. ret = udp_queue_rcv_skb(sk, skb1);
  964. else
  965. sk_add_backlog(sk, skb1);
  966. bh_unlock_sock(sk);
  967. if (ret > 0)
  968. /* we should probably re-process instead
  969. * of dropping packets here. */
  970. kfree_skb(skb1);
  971. }
  972. sk = sknext;
  973. } while (sknext);
  974. } else
  975. kfree_skb(skb);
  976. read_unlock(&udp_hash_lock);
  977. return 0;
  978. }
  979. /* Initialize UDP checksum. If exited with zero value (success),
  980. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  981. * Otherwise, csum completion requires chacksumming packet body,
  982. * including udp header and folding it to skb->csum.
  983. */
  984. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  985. int proto)
  986. {
  987. const struct iphdr *iph;
  988. int err;
  989. UDP_SKB_CB(skb)->partial_cov = 0;
  990. UDP_SKB_CB(skb)->cscov = skb->len;
  991. if (proto == IPPROTO_UDPLITE) {
  992. err = udplite_checksum_init(skb, uh);
  993. if (err)
  994. return err;
  995. }
  996. iph = ip_hdr(skb);
  997. if (uh->check == 0) {
  998. skb->ip_summed = CHECKSUM_UNNECESSARY;
  999. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1000. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1001. proto, skb->csum))
  1002. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1003. }
  1004. if (!skb_csum_unnecessary(skb))
  1005. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1006. skb->len, proto, 0);
  1007. /* Probably, we should checksum udp header (it should be in cache
  1008. * in any case) and data in tiny packets (< rx copybreak).
  1009. */
  1010. return 0;
  1011. }
  1012. /*
  1013. * All we need to do is get the socket, and then do a checksum.
  1014. */
  1015. int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
  1016. int proto)
  1017. {
  1018. struct sock *sk;
  1019. struct udphdr *uh = udp_hdr(skb);
  1020. unsigned short ulen;
  1021. struct rtable *rt = (struct rtable*)skb->dst;
  1022. __be32 saddr = ip_hdr(skb)->saddr;
  1023. __be32 daddr = ip_hdr(skb)->daddr;
  1024. struct net *net = dev_net(skb->dev);
  1025. /*
  1026. * Validate the packet.
  1027. */
  1028. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1029. goto drop; /* No space for header. */
  1030. ulen = ntohs(uh->len);
  1031. if (ulen > skb->len)
  1032. goto short_packet;
  1033. if (proto == IPPROTO_UDP) {
  1034. /* UDP validates ulen. */
  1035. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1036. goto short_packet;
  1037. uh = udp_hdr(skb);
  1038. }
  1039. if (udp4_csum_init(skb, uh, proto))
  1040. goto csum_error;
  1041. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1042. return __udp4_lib_mcast_deliver(net, skb, uh,
  1043. saddr, daddr, udptable);
  1044. sk = __udp4_lib_lookup(net, saddr, uh->source, daddr,
  1045. uh->dest, inet_iif(skb), udptable);
  1046. if (sk != NULL) {
  1047. int ret = 0;
  1048. bh_lock_sock_nested(sk);
  1049. if (!sock_owned_by_user(sk))
  1050. ret = udp_queue_rcv_skb(sk, skb);
  1051. else
  1052. sk_add_backlog(sk, skb);
  1053. bh_unlock_sock(sk);
  1054. sock_put(sk);
  1055. /* a return value > 0 means to resubmit the input, but
  1056. * it wants the return to be -protocol, or 0
  1057. */
  1058. if (ret > 0)
  1059. return -ret;
  1060. return 0;
  1061. }
  1062. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1063. goto drop;
  1064. nf_reset(skb);
  1065. /* No socket. Drop packet silently, if checksum is wrong */
  1066. if (udp_lib_checksum_complete(skb))
  1067. goto csum_error;
  1068. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1069. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1070. /*
  1071. * Hmm. We got an UDP packet to a port to which we
  1072. * don't wanna listen. Ignore it.
  1073. */
  1074. kfree_skb(skb);
  1075. return 0;
  1076. short_packet:
  1077. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n",
  1078. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1079. NIPQUAD(saddr),
  1080. ntohs(uh->source),
  1081. ulen,
  1082. skb->len,
  1083. NIPQUAD(daddr),
  1084. ntohs(uh->dest));
  1085. goto drop;
  1086. csum_error:
  1087. /*
  1088. * RFC1122: OK. Discards the bad packet silently (as far as
  1089. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1090. */
  1091. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n",
  1092. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1093. NIPQUAD(saddr),
  1094. ntohs(uh->source),
  1095. NIPQUAD(daddr),
  1096. ntohs(uh->dest),
  1097. ulen);
  1098. drop:
  1099. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1100. kfree_skb(skb);
  1101. return 0;
  1102. }
  1103. int udp_rcv(struct sk_buff *skb)
  1104. {
  1105. return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP);
  1106. }
  1107. void udp_destroy_sock(struct sock *sk)
  1108. {
  1109. lock_sock(sk);
  1110. udp_flush_pending_frames(sk);
  1111. release_sock(sk);
  1112. }
  1113. /*
  1114. * Socket option code for UDP
  1115. */
  1116. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1117. char __user *optval, int optlen,
  1118. int (*push_pending_frames)(struct sock *))
  1119. {
  1120. struct udp_sock *up = udp_sk(sk);
  1121. int val;
  1122. int err = 0;
  1123. int is_udplite = IS_UDPLITE(sk);
  1124. if (optlen<sizeof(int))
  1125. return -EINVAL;
  1126. if (get_user(val, (int __user *)optval))
  1127. return -EFAULT;
  1128. switch (optname) {
  1129. case UDP_CORK:
  1130. if (val != 0) {
  1131. up->corkflag = 1;
  1132. } else {
  1133. up->corkflag = 0;
  1134. lock_sock(sk);
  1135. (*push_pending_frames)(sk);
  1136. release_sock(sk);
  1137. }
  1138. break;
  1139. case UDP_ENCAP:
  1140. switch (val) {
  1141. case 0:
  1142. case UDP_ENCAP_ESPINUDP:
  1143. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1144. up->encap_rcv = xfrm4_udp_encap_rcv;
  1145. /* FALLTHROUGH */
  1146. case UDP_ENCAP_L2TPINUDP:
  1147. up->encap_type = val;
  1148. break;
  1149. default:
  1150. err = -ENOPROTOOPT;
  1151. break;
  1152. }
  1153. break;
  1154. /*
  1155. * UDP-Lite's partial checksum coverage (RFC 3828).
  1156. */
  1157. /* The sender sets actual checksum coverage length via this option.
  1158. * The case coverage > packet length is handled by send module. */
  1159. case UDPLITE_SEND_CSCOV:
  1160. if (!is_udplite) /* Disable the option on UDP sockets */
  1161. return -ENOPROTOOPT;
  1162. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1163. val = 8;
  1164. up->pcslen = val;
  1165. up->pcflag |= UDPLITE_SEND_CC;
  1166. break;
  1167. /* The receiver specifies a minimum checksum coverage value. To make
  1168. * sense, this should be set to at least 8 (as done below). If zero is
  1169. * used, this again means full checksum coverage. */
  1170. case UDPLITE_RECV_CSCOV:
  1171. if (!is_udplite) /* Disable the option on UDP sockets */
  1172. return -ENOPROTOOPT;
  1173. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1174. val = 8;
  1175. up->pcrlen = val;
  1176. up->pcflag |= UDPLITE_RECV_CC;
  1177. break;
  1178. default:
  1179. err = -ENOPROTOOPT;
  1180. break;
  1181. }
  1182. return err;
  1183. }
  1184. int udp_setsockopt(struct sock *sk, int level, int optname,
  1185. char __user *optval, int optlen)
  1186. {
  1187. if (level == SOL_UDP || level == SOL_UDPLITE)
  1188. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1189. udp_push_pending_frames);
  1190. return ip_setsockopt(sk, level, optname, optval, optlen);
  1191. }
  1192. #ifdef CONFIG_COMPAT
  1193. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1194. char __user *optval, int optlen)
  1195. {
  1196. if (level == SOL_UDP || level == SOL_UDPLITE)
  1197. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1198. udp_push_pending_frames);
  1199. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1200. }
  1201. #endif
  1202. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1203. char __user *optval, int __user *optlen)
  1204. {
  1205. struct udp_sock *up = udp_sk(sk);
  1206. int val, len;
  1207. if (get_user(len,optlen))
  1208. return -EFAULT;
  1209. len = min_t(unsigned int, len, sizeof(int));
  1210. if (len < 0)
  1211. return -EINVAL;
  1212. switch (optname) {
  1213. case UDP_CORK:
  1214. val = up->corkflag;
  1215. break;
  1216. case UDP_ENCAP:
  1217. val = up->encap_type;
  1218. break;
  1219. /* The following two cannot be changed on UDP sockets, the return is
  1220. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1221. case UDPLITE_SEND_CSCOV:
  1222. val = up->pcslen;
  1223. break;
  1224. case UDPLITE_RECV_CSCOV:
  1225. val = up->pcrlen;
  1226. break;
  1227. default:
  1228. return -ENOPROTOOPT;
  1229. }
  1230. if (put_user(len, optlen))
  1231. return -EFAULT;
  1232. if (copy_to_user(optval, &val,len))
  1233. return -EFAULT;
  1234. return 0;
  1235. }
  1236. int udp_getsockopt(struct sock *sk, int level, int optname,
  1237. char __user *optval, int __user *optlen)
  1238. {
  1239. if (level == SOL_UDP || level == SOL_UDPLITE)
  1240. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1241. return ip_getsockopt(sk, level, optname, optval, optlen);
  1242. }
  1243. #ifdef CONFIG_COMPAT
  1244. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1245. char __user *optval, int __user *optlen)
  1246. {
  1247. if (level == SOL_UDP || level == SOL_UDPLITE)
  1248. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1249. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1250. }
  1251. #endif
  1252. /**
  1253. * udp_poll - wait for a UDP event.
  1254. * @file - file struct
  1255. * @sock - socket
  1256. * @wait - poll table
  1257. *
  1258. * This is same as datagram poll, except for the special case of
  1259. * blocking sockets. If application is using a blocking fd
  1260. * and a packet with checksum error is in the queue;
  1261. * then it could get return from select indicating data available
  1262. * but then block when reading it. Add special case code
  1263. * to work around these arguably broken applications.
  1264. */
  1265. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1266. {
  1267. unsigned int mask = datagram_poll(file, sock, wait);
  1268. struct sock *sk = sock->sk;
  1269. int is_lite = IS_UDPLITE(sk);
  1270. /* Check for false positives due to checksum errors */
  1271. if ( (mask & POLLRDNORM) &&
  1272. !(file->f_flags & O_NONBLOCK) &&
  1273. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1274. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1275. struct sk_buff *skb;
  1276. spin_lock_bh(&rcvq->lock);
  1277. while ((skb = skb_peek(rcvq)) != NULL &&
  1278. udp_lib_checksum_complete(skb)) {
  1279. UDP_INC_STATS_BH(sock_net(sk),
  1280. UDP_MIB_INERRORS, is_lite);
  1281. __skb_unlink(skb, rcvq);
  1282. kfree_skb(skb);
  1283. }
  1284. spin_unlock_bh(&rcvq->lock);
  1285. /* nothing to see, move along */
  1286. if (skb == NULL)
  1287. mask &= ~(POLLIN | POLLRDNORM);
  1288. }
  1289. return mask;
  1290. }
  1291. struct proto udp_prot = {
  1292. .name = "UDP",
  1293. .owner = THIS_MODULE,
  1294. .close = udp_lib_close,
  1295. .connect = ip4_datagram_connect,
  1296. .disconnect = udp_disconnect,
  1297. .ioctl = udp_ioctl,
  1298. .destroy = udp_destroy_sock,
  1299. .setsockopt = udp_setsockopt,
  1300. .getsockopt = udp_getsockopt,
  1301. .sendmsg = udp_sendmsg,
  1302. .recvmsg = udp_recvmsg,
  1303. .sendpage = udp_sendpage,
  1304. .backlog_rcv = udp_queue_rcv_skb,
  1305. .hash = udp_lib_hash,
  1306. .unhash = udp_lib_unhash,
  1307. .get_port = udp_v4_get_port,
  1308. .memory_allocated = &udp_memory_allocated,
  1309. .sysctl_mem = sysctl_udp_mem,
  1310. .sysctl_wmem = &sysctl_udp_wmem_min,
  1311. .sysctl_rmem = &sysctl_udp_rmem_min,
  1312. .obj_size = sizeof(struct udp_sock),
  1313. .h.udp_hash = udp_hash,
  1314. #ifdef CONFIG_COMPAT
  1315. .compat_setsockopt = compat_udp_setsockopt,
  1316. .compat_getsockopt = compat_udp_getsockopt,
  1317. #endif
  1318. };
  1319. /* ------------------------------------------------------------------------ */
  1320. #ifdef CONFIG_PROC_FS
  1321. static struct sock *udp_get_first(struct seq_file *seq)
  1322. {
  1323. struct sock *sk;
  1324. struct udp_iter_state *state = seq->private;
  1325. struct net *net = seq_file_net(seq);
  1326. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1327. struct hlist_node *node;
  1328. sk_for_each(sk, node, state->hashtable + state->bucket) {
  1329. if (!net_eq(sock_net(sk), net))
  1330. continue;
  1331. if (sk->sk_family == state->family)
  1332. goto found;
  1333. }
  1334. }
  1335. sk = NULL;
  1336. found:
  1337. return sk;
  1338. }
  1339. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1340. {
  1341. struct udp_iter_state *state = seq->private;
  1342. struct net *net = seq_file_net(seq);
  1343. do {
  1344. sk = sk_next(sk);
  1345. try_again:
  1346. ;
  1347. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  1348. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1349. sk = sk_head(state->hashtable + state->bucket);
  1350. goto try_again;
  1351. }
  1352. return sk;
  1353. }
  1354. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1355. {
  1356. struct sock *sk = udp_get_first(seq);
  1357. if (sk)
  1358. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1359. --pos;
  1360. return pos ? NULL : sk;
  1361. }
  1362. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1363. __acquires(udp_hash_lock)
  1364. {
  1365. read_lock(&udp_hash_lock);
  1366. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  1367. }
  1368. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1369. {
  1370. struct sock *sk;
  1371. if (v == SEQ_START_TOKEN)
  1372. sk = udp_get_idx(seq, 0);
  1373. else
  1374. sk = udp_get_next(seq, v);
  1375. ++*pos;
  1376. return sk;
  1377. }
  1378. static void udp_seq_stop(struct seq_file *seq, void *v)
  1379. __releases(udp_hash_lock)
  1380. {
  1381. read_unlock(&udp_hash_lock);
  1382. }
  1383. static int udp_seq_open(struct inode *inode, struct file *file)
  1384. {
  1385. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1386. struct udp_iter_state *s;
  1387. int err;
  1388. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1389. sizeof(struct udp_iter_state));
  1390. if (err < 0)
  1391. return err;
  1392. s = ((struct seq_file *)file->private_data)->private;
  1393. s->family = afinfo->family;
  1394. s->hashtable = afinfo->hashtable;
  1395. return err;
  1396. }
  1397. /* ------------------------------------------------------------------------ */
  1398. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  1399. {
  1400. struct proc_dir_entry *p;
  1401. int rc = 0;
  1402. afinfo->seq_fops.open = udp_seq_open;
  1403. afinfo->seq_fops.read = seq_read;
  1404. afinfo->seq_fops.llseek = seq_lseek;
  1405. afinfo->seq_fops.release = seq_release_net;
  1406. afinfo->seq_ops.start = udp_seq_start;
  1407. afinfo->seq_ops.next = udp_seq_next;
  1408. afinfo->seq_ops.stop = udp_seq_stop;
  1409. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1410. &afinfo->seq_fops, afinfo);
  1411. if (!p)
  1412. rc = -ENOMEM;
  1413. return rc;
  1414. }
  1415. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  1416. {
  1417. proc_net_remove(net, afinfo->name);
  1418. }
  1419. /* ------------------------------------------------------------------------ */
  1420. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  1421. int bucket, int *len)
  1422. {
  1423. struct inet_sock *inet = inet_sk(sp);
  1424. __be32 dest = inet->daddr;
  1425. __be32 src = inet->rcv_saddr;
  1426. __u16 destp = ntohs(inet->dport);
  1427. __u16 srcp = ntohs(inet->sport);
  1428. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  1429. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
  1430. bucket, src, srcp, dest, destp, sp->sk_state,
  1431. atomic_read(&sp->sk_wmem_alloc),
  1432. atomic_read(&sp->sk_rmem_alloc),
  1433. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1434. atomic_read(&sp->sk_refcnt), sp,
  1435. atomic_read(&sp->sk_drops), len);
  1436. }
  1437. int udp4_seq_show(struct seq_file *seq, void *v)
  1438. {
  1439. if (v == SEQ_START_TOKEN)
  1440. seq_printf(seq, "%-127s\n",
  1441. " sl local_address rem_address st tx_queue "
  1442. "rx_queue tr tm->when retrnsmt uid timeout "
  1443. "inode ref pointer drops");
  1444. else {
  1445. struct udp_iter_state *state = seq->private;
  1446. int len;
  1447. udp4_format_sock(v, seq, state->bucket, &len);
  1448. seq_printf(seq, "%*s\n", 127 - len ,"");
  1449. }
  1450. return 0;
  1451. }
  1452. /* ------------------------------------------------------------------------ */
  1453. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1454. .name = "udp",
  1455. .family = AF_INET,
  1456. .hashtable = udp_hash,
  1457. .seq_fops = {
  1458. .owner = THIS_MODULE,
  1459. },
  1460. .seq_ops = {
  1461. .show = udp4_seq_show,
  1462. },
  1463. };
  1464. static int udp4_proc_init_net(struct net *net)
  1465. {
  1466. return udp_proc_register(net, &udp4_seq_afinfo);
  1467. }
  1468. static void udp4_proc_exit_net(struct net *net)
  1469. {
  1470. udp_proc_unregister(net, &udp4_seq_afinfo);
  1471. }
  1472. static struct pernet_operations udp4_net_ops = {
  1473. .init = udp4_proc_init_net,
  1474. .exit = udp4_proc_exit_net,
  1475. };
  1476. int __init udp4_proc_init(void)
  1477. {
  1478. return register_pernet_subsys(&udp4_net_ops);
  1479. }
  1480. void udp4_proc_exit(void)
  1481. {
  1482. unregister_pernet_subsys(&udp4_net_ops);
  1483. }
  1484. #endif /* CONFIG_PROC_FS */
  1485. void __init udp_init(void)
  1486. {
  1487. unsigned long limit;
  1488. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1489. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1490. * toward zero with the amount of memory, with a floor of 128 pages.
  1491. */
  1492. limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1493. limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1494. limit = max(limit, 128UL);
  1495. sysctl_udp_mem[0] = limit / 4 * 3;
  1496. sysctl_udp_mem[1] = limit;
  1497. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1498. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1499. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1500. }
  1501. EXPORT_SYMBOL(udp_disconnect);
  1502. EXPORT_SYMBOL(udp_hash);
  1503. EXPORT_SYMBOL(udp_hash_lock);
  1504. EXPORT_SYMBOL(udp_ioctl);
  1505. EXPORT_SYMBOL(udp_prot);
  1506. EXPORT_SYMBOL(udp_sendmsg);
  1507. EXPORT_SYMBOL(udp_lib_getsockopt);
  1508. EXPORT_SYMBOL(udp_lib_setsockopt);
  1509. EXPORT_SYMBOL(udp_poll);
  1510. EXPORT_SYMBOL(udp_lib_get_port);
  1511. #ifdef CONFIG_PROC_FS
  1512. EXPORT_SYMBOL(udp_proc_register);
  1513. EXPORT_SYMBOL(udp_proc_unregister);
  1514. #endif