protocol.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * INET protocol dispatch tables.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. *
  11. * Fixes:
  12. * Alan Cox : Ahah! udp icmp errors don't work because
  13. * udp_err is never called!
  14. * Alan Cox : Added new fields for init and ready for
  15. * proper fragmentation (_NO_ 4K limits!)
  16. * Richard Colella : Hang on hash collision
  17. * Vince Laviano : Modified inet_del_protocol() to correctly
  18. * maintain copy bit.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. */
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/in.h>
  33. #include <linux/inet.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/timer.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <linux/skbuff.h>
  39. #include <net/sock.h>
  40. #include <net/icmp.h>
  41. #include <net/udp.h>
  42. #include <net/ipip.h>
  43. #include <linux/igmp.h>
  44. struct net_protocol *inet_protos[MAX_INET_PROTOS] ____cacheline_aligned_in_smp;
  45. static DEFINE_SPINLOCK(inet_proto_lock);
  46. /*
  47. * Add a protocol handler to the hash tables
  48. */
  49. int inet_add_protocol(struct net_protocol *prot, unsigned char protocol)
  50. {
  51. int hash, ret;
  52. hash = protocol & (MAX_INET_PROTOS - 1);
  53. spin_lock_bh(&inet_proto_lock);
  54. if (inet_protos[hash]) {
  55. ret = -1;
  56. } else {
  57. inet_protos[hash] = prot;
  58. ret = 0;
  59. }
  60. spin_unlock_bh(&inet_proto_lock);
  61. return ret;
  62. }
  63. /*
  64. * Remove a protocol from the hash tables.
  65. */
  66. int inet_del_protocol(struct net_protocol *prot, unsigned char protocol)
  67. {
  68. int hash, ret;
  69. hash = protocol & (MAX_INET_PROTOS - 1);
  70. spin_lock_bh(&inet_proto_lock);
  71. if (inet_protos[hash] == prot) {
  72. inet_protos[hash] = NULL;
  73. ret = 0;
  74. } else {
  75. ret = -1;
  76. }
  77. spin_unlock_bh(&inet_proto_lock);
  78. synchronize_net();
  79. return ret;
  80. }
  81. EXPORT_SYMBOL(inet_add_protocol);
  82. EXPORT_SYMBOL(inet_del_protocol);