udp.c 39 KB

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