netfilter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #ifndef __LINUX_NETFILTER_H
  2. #define __LINUX_NETFILTER_H
  3. #ifdef __KERNEL__
  4. #include <linux/init.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/net.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/if.h>
  9. #include <linux/in.h>
  10. #include <linux/in6.h>
  11. #include <linux/wait.h>
  12. #include <linux/list.h>
  13. #include <net/net_namespace.h>
  14. #endif
  15. #include <linux/types.h>
  16. #include <linux/compiler.h>
  17. /* Responses from hook functions. */
  18. #define NF_DROP 0
  19. #define NF_ACCEPT 1
  20. #define NF_STOLEN 2
  21. #define NF_QUEUE 3
  22. #define NF_REPEAT 4
  23. #define NF_STOP 5
  24. #define NF_MAX_VERDICT NF_STOP
  25. /* we overload the higher bits for encoding auxiliary data such as the queue
  26. * number. Not nice, but better than additional function arguments. */
  27. #define NF_VERDICT_MASK 0x0000ffff
  28. #define NF_VERDICT_BITS 16
  29. #define NF_VERDICT_QMASK 0xffff0000
  30. #define NF_VERDICT_QBITS 16
  31. #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
  32. /* only for userspace compatibility */
  33. #ifndef __KERNEL__
  34. /* Generic cache responses from hook functions.
  35. <= 0x2000 is used for protocol-flags. */
  36. #define NFC_UNKNOWN 0x4000
  37. #define NFC_ALTERED 0x8000
  38. #endif
  39. enum nf_inet_hooks {
  40. NF_INET_PRE_ROUTING,
  41. NF_INET_LOCAL_IN,
  42. NF_INET_FORWARD,
  43. NF_INET_LOCAL_OUT,
  44. NF_INET_POST_ROUTING,
  45. NF_INET_NUMHOOKS
  46. };
  47. enum {
  48. NFPROTO_UNSPEC = 0,
  49. NFPROTO_IPV4 = 2,
  50. NFPROTO_ARP = 3,
  51. NFPROTO_BRIDGE = 7,
  52. NFPROTO_IPV6 = 10,
  53. NFPROTO_DECNET = 12,
  54. NFPROTO_NUMPROTO,
  55. };
  56. union nf_inet_addr {
  57. __u32 all[4];
  58. __be32 ip;
  59. __be32 ip6[4];
  60. struct in_addr in;
  61. struct in6_addr in6;
  62. };
  63. #ifdef __KERNEL__
  64. #ifdef CONFIG_NETFILTER
  65. static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
  66. const union nf_inet_addr *a2)
  67. {
  68. return a1->all[0] == a2->all[0] &&
  69. a1->all[1] == a2->all[1] &&
  70. a1->all[2] == a2->all[2] &&
  71. a1->all[3] == a2->all[3];
  72. }
  73. extern void netfilter_init(void);
  74. /* Largest hook number + 1 */
  75. #define NF_MAX_HOOKS 8
  76. struct sk_buff;
  77. typedef unsigned int nf_hookfn(unsigned int hooknum,
  78. struct sk_buff *skb,
  79. const struct net_device *in,
  80. const struct net_device *out,
  81. int (*okfn)(struct sk_buff *));
  82. struct nf_hook_ops
  83. {
  84. struct list_head list;
  85. /* User fills in from here down. */
  86. nf_hookfn *hook;
  87. struct module *owner;
  88. u_int8_t pf;
  89. unsigned int hooknum;
  90. /* Hooks are ordered in ascending priority. */
  91. int priority;
  92. };
  93. struct nf_sockopt_ops
  94. {
  95. struct list_head list;
  96. u_int8_t pf;
  97. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  98. int set_optmin;
  99. int set_optmax;
  100. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  101. int (*compat_set)(struct sock *sk, int optval,
  102. void __user *user, unsigned int len);
  103. int get_optmin;
  104. int get_optmax;
  105. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  106. int (*compat_get)(struct sock *sk, int optval,
  107. void __user *user, int *len);
  108. /* Use the module struct to lock set/get code in place */
  109. struct module *owner;
  110. };
  111. /* Function to register/unregister hook points. */
  112. int nf_register_hook(struct nf_hook_ops *reg);
  113. void nf_unregister_hook(struct nf_hook_ops *reg);
  114. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  115. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  116. /* Functions to register get/setsockopt ranges (non-inclusive). You
  117. need to check permissions yourself! */
  118. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  119. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  120. #ifdef CONFIG_SYSCTL
  121. /* Sysctl registration */
  122. extern struct ctl_path nf_net_netfilter_sysctl_path[];
  123. extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
  124. #endif /* CONFIG_SYSCTL */
  125. extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  126. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  127. struct net_device *indev, struct net_device *outdev,
  128. int (*okfn)(struct sk_buff *), int thresh);
  129. /**
  130. * nf_hook_thresh - call a netfilter hook
  131. *
  132. * Returns 1 if the hook has allowed the packet to pass. The function
  133. * okfn must be invoked by the caller in this case. Any other return
  134. * value indicates the packet has been consumed by the hook.
  135. */
  136. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  137. struct sk_buff *skb,
  138. struct net_device *indev,
  139. struct net_device *outdev,
  140. int (*okfn)(struct sk_buff *), int thresh,
  141. int cond)
  142. {
  143. if (!cond)
  144. return 1;
  145. #ifndef CONFIG_NETFILTER_DEBUG
  146. if (list_empty(&nf_hooks[pf][hook]))
  147. return 1;
  148. #endif
  149. return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  150. }
  151. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  152. struct net_device *indev, struct net_device *outdev,
  153. int (*okfn)(struct sk_buff *))
  154. {
  155. return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
  156. }
  157. /* Activate hook; either okfn or kfree_skb called, unless a hook
  158. returns NF_STOLEN (in which case, it's up to the hook to deal with
  159. the consequences).
  160. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  161. accepted.
  162. */
  163. /* RR:
  164. > I don't want nf_hook to return anything because people might forget
  165. > about async and trust the return value to mean "packet was ok".
  166. AK:
  167. Just document it clearly, then you can expect some sense from kernel
  168. coders :)
  169. */
  170. /* This is gross, but inline doesn't cut it for avoiding the function
  171. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  172. /* HX: It's slightly less gross now. */
  173. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  174. ({int __ret; \
  175. if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
  176. __ret = (okfn)(skb); \
  177. __ret;})
  178. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
  179. ({int __ret; \
  180. if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
  181. __ret = (okfn)(skb); \
  182. __ret;})
  183. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  184. NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
  185. /* Call setsockopt() */
  186. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  187. int len);
  188. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  189. int *len);
  190. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  191. char __user *opt, int len);
  192. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  193. char __user *opt, int *len);
  194. /* Call this before modifying an existing packet: ensures it is
  195. modifiable and linear to the point you care about (writable_len).
  196. Returns true or false. */
  197. extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
  198. struct flowi;
  199. struct nf_queue_entry;
  200. struct nf_afinfo {
  201. unsigned short family;
  202. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  203. unsigned int dataoff, u_int8_t protocol);
  204. __sum16 (*checksum_partial)(struct sk_buff *skb,
  205. unsigned int hook,
  206. unsigned int dataoff,
  207. unsigned int len,
  208. u_int8_t protocol);
  209. int (*route)(struct dst_entry **dst, struct flowi *fl);
  210. void (*saveroute)(const struct sk_buff *skb,
  211. struct nf_queue_entry *entry);
  212. int (*reroute)(struct sk_buff *skb,
  213. const struct nf_queue_entry *entry);
  214. int route_key_size;
  215. };
  216. extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
  217. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  218. {
  219. return rcu_dereference(nf_afinfo[family]);
  220. }
  221. static inline __sum16
  222. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  223. u_int8_t protocol, unsigned short family)
  224. {
  225. const struct nf_afinfo *afinfo;
  226. __sum16 csum = 0;
  227. rcu_read_lock();
  228. afinfo = nf_get_afinfo(family);
  229. if (afinfo)
  230. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  231. rcu_read_unlock();
  232. return csum;
  233. }
  234. static inline __sum16
  235. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  236. unsigned int dataoff, unsigned int len,
  237. u_int8_t protocol, unsigned short family)
  238. {
  239. const struct nf_afinfo *afinfo;
  240. __sum16 csum = 0;
  241. rcu_read_lock();
  242. afinfo = nf_get_afinfo(family);
  243. if (afinfo)
  244. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  245. protocol);
  246. rcu_read_unlock();
  247. return csum;
  248. }
  249. extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  250. extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
  251. #include <net/flow.h>
  252. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  253. static inline void
  254. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  255. {
  256. #ifdef CONFIG_NF_NAT_NEEDED
  257. void (*decodefn)(struct sk_buff *, struct flowi *);
  258. if (family == AF_INET) {
  259. rcu_read_lock();
  260. decodefn = rcu_dereference(ip_nat_decode_session);
  261. if (decodefn)
  262. decodefn(skb, fl);
  263. rcu_read_unlock();
  264. }
  265. #endif
  266. }
  267. #ifdef CONFIG_PROC_FS
  268. #include <linux/proc_fs.h>
  269. extern struct proc_dir_entry *proc_net_netfilter;
  270. #endif
  271. #else /* !CONFIG_NETFILTER */
  272. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  273. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  274. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  275. struct sk_buff *skb,
  276. struct net_device *indev,
  277. struct net_device *outdev,
  278. int (*okfn)(struct sk_buff *), int thresh,
  279. int cond)
  280. {
  281. return okfn(skb);
  282. }
  283. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  284. struct net_device *indev, struct net_device *outdev,
  285. int (*okfn)(struct sk_buff *))
  286. {
  287. return 1;
  288. }
  289. struct flowi;
  290. static inline void
  291. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  292. {
  293. }
  294. #endif /*CONFIG_NETFILTER*/
  295. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  296. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  297. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  298. extern void (*nf_ct_destroy)(struct nf_conntrack *);
  299. #else
  300. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  301. #endif
  302. static inline struct net *nf_pre_routing_net(const struct net_device *in,
  303. const struct net_device *out)
  304. {
  305. #ifdef CONFIG_NET_NS
  306. return in->nd_net;
  307. #else
  308. return &init_net;
  309. #endif
  310. }
  311. static inline struct net *nf_local_in_net(const struct net_device *in,
  312. const struct net_device *out)
  313. {
  314. #ifdef CONFIG_NET_NS
  315. return in->nd_net;
  316. #else
  317. return &init_net;
  318. #endif
  319. }
  320. static inline struct net *nf_forward_net(const struct net_device *in,
  321. const struct net_device *out)
  322. {
  323. #ifdef CONFIG_NET_NS
  324. BUG_ON(in->nd_net != out->nd_net);
  325. return in->nd_net;
  326. #else
  327. return &init_net;
  328. #endif
  329. }
  330. static inline struct net *nf_local_out_net(const struct net_device *in,
  331. const struct net_device *out)
  332. {
  333. #ifdef CONFIG_NET_NS
  334. return out->nd_net;
  335. #else
  336. return &init_net;
  337. #endif
  338. }
  339. static inline struct net *nf_post_routing_net(const struct net_device *in,
  340. const struct net_device *out)
  341. {
  342. #ifdef CONFIG_NET_NS
  343. return out->nd_net;
  344. #else
  345. return &init_net;
  346. #endif
  347. }
  348. #endif /*__KERNEL__*/
  349. #endif /*__LINUX_NETFILTER_H*/