[질문] 임의의 network layer에서 특정 ip에 대한 arp cache 값 읽어오는 방법

skjean의 이미지

제목 대로입니다.

임의의 network layer에서 특정 ip에 대한 MAC address를
읽어오고 싶은데요.. 이경우 커널의 어떤 함수를 호출해야
하는지 알고 싶습니다.

예를 들어, dev_queue_xmit()을 호출하기 직전에 이전 arp에
의해서 얻어온 destination MAC을 바꾸고 싶은거죠..
이때 arp table을 query하는 dest IP address는 원래 IP
packet의 dest IP address가 아니고, arp cache에 있는 다른 IP 입니다.

결국은 특정 dest IP "A"에 대해서 MAC "A"가 아닌 MAC "B"가
붙어 나가게 하고 싶은 것입니다. (물론 MAC "B"는 ARP Cache에
있고, 이는 IP "B"에 대한 MAC 입니다.)

어떤 커널 함수를 어떤 파라미터를 불러야 할지 한 수
부탁드립니다.

감사합니다. ^^

kuaaan의 이미지

UNP 442page를 참고하세요. ^^

----------------------------------------------
한번뿐인 인생....
미친듯이 살아보자!
----------------------------------------------

wariua의 이미지

net/ipv4/arp.c에 있는 두 가지 함수를 참조하실 수 있습니다.

static int arp_req_get(struct arpreq *r, struct net_device *dev)
: ioctl(SIOCGARP) 호출하면 불리는 함수입니다.

int arp_find(unsigned char *haddr, struct sk_buff *skb)
: 내보낼 sk_buff 구조체의 데이터를 이용해서 알아낸 주소를 haddr에 기록하는 함수입니다.

캐시 최근 사용 시각 갱신 등을 신경쓰지 않는다면 대략 다음 루틴 정도로 되지 않을까 싶습니다.

...
#include <net/arp.h>
...

u32 addr = ...; // dest IP address
struct net_device *dev = ...; // network device
char hwaddr[HWADDR_LEN]; // MAC address

struct neighbour *n;

n = neigh_lookup(&arp_tbl, &paddr, dev);
if (n) {
    read_lock_bh(&n->lock);
    memcpy(hwaddr, n->ha, dev->addr_len);
    read_unlock_bh(&n->lock);
    neigh_release(n);
}

2.6.6 소스를 기준으로 했습니다.

$PWD `date`

skjean의 이미지

ip_route_output()과 arp_find()를 이용해서 해결했습니다.

그런데, UNP가 무엇인가요?

dev_queue_xmit() 이전에 ip_route_output(&rt, ... )를 호출하여
rt->rt_gateway 값을 얻어오고 이를 이용하여 arp_find()를 호출하면
arp cache에 있는 MAC을 부가하여 전송하더라구요.. ^^;;
물론 이때 rt의 인자는 제가 원하는 IP를 부가했구요..

ip_route_output(&rt, ... )
((struct rtable*)skb->dst)->rt_gateway = rt->rt_gateway;
arp_find(eth->h_dest, skb)

답변에 감사드립니다. 많은 도움 되었습니다. ^^
좋은 하루 되셔요~~~

superkkt의 이미지

skjean wrote:
그런데, UNP가 무엇인가요?

책 이름입니다. UNIX NETWORK PROGRAMMING (STEVENS)

======================
BLOG : http://superkkt.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.